Skip to content

Commit

Permalink
Accumulated changes - beta branch
Browse files Browse the repository at this point in the history
Spout.cpp
    - Restore GetSenderAdpater for testing
SpoutCopy.cpp
    - use std:: prefix for floor in rgba2rgbResample for Clang compatibility. PR#81
SpoutDirectX.cpp
    - Correct log notice in ReleaseDX11Texture to show texture instead of device
    - Move adapter pointer release from release device to destructor
SpoutFrameCount.cpp
    - Clean up logs in CreateAccessMutex and EnableFrameCount
    - Change "_uuidof" to "__uuidof" in CheckKeyedAccess. PR#81
SpoutGL.cpp
    - Add "No error: case comment to LinkGLDXtextures
    - Remove un-necessary wglDXSetResourceShareHandle from LinkGLDXtextures
    - Create m_bGLDXdone flag for GLDXready to avoid repeats
    - Change pointer comparision from >0 to nullptr in OpenSpout (PR #80)
    - Remove m_hInteropDevice created check in OpenSpout
    - Clean up logs in LoadGLextensions
    - Restore glBufferData method for in UnloadTexturePixels
      pending implementation of glFencSync for glMapBufferRange method
SpoutUtils.cpp
    - Change return logic of RemovePathFromRegistry
  • Loading branch information
leadedge committed Feb 24, 2022
1 parent d1ad5bc commit fea2bf5
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 93 deletions.
81 changes: 77 additions & 4 deletions SPOUTSDK/SpoutGL/Spout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
// 17.12.21 - Remove adapter gets from Sender/Receiver init
// Adapter index and name are retrieved with Get functions
// 20.12.21 - Restore log notice for ReleaseSender
// 24.02.22 - Restore GetSenderAdpater for testing
//
// ====================================================================================
/*
Expand Down Expand Up @@ -303,6 +304,8 @@ Spout::Spout()
// Initialize adapter name global
// Adapter index and name are retrieved with create sender or receiver
m_AdapterName[0] = 0;
m_bAdapt = false; // Receiver adapt to the sender adapter

}

Spout::~Spout()
Expand Down Expand Up @@ -1145,6 +1148,73 @@ bool Spout::SetAdapter(int index)
return true;
}

//---------------------------------------------------------
// Function: GetSenderAdapter
// Get sender adapter index and name for a given sender
//
// Testing only.
// Note that OpenDX11shareHandle fails and can crash if the share handle
// has been created using a different graphics adapter (see spoutDirectX)
// Try/Catch can catch the exception but not recommended for general use.
int Spout::GetSenderAdapter(const char* sendername, char* adaptername, int maxchars)
{
if (!sendername || !sendername[0])
return -1;

int senderadapter = -1;
ID3D11Texture2D* pSharedTexture = nullptr;
ID3D11Device* pDummyDevice = nullptr;
ID3D11DeviceContext* pContext = nullptr;
IDXGIAdapter* pAdapter = nullptr;

// Get the current device adapter pointer (could be null default)
IDXGIAdapter* pCurrentAdapter = spoutdx.GetAdapterPointer();

SpoutLogNotice("Spout::GetSenderAdapter - testing for sender adapter (%s)", sendername);

SharedTextureInfo info;
if (sendernames.getSharedInfo(sendername, &info)) {
int nAdapters = spoutdx.GetNumAdapters();
for (int i = 0; i < nAdapters; i++) {
pAdapter = spoutdx.GetAdapterPointer(i);
if (pAdapter) {
SpoutLogNotice(" testing adapter %d", i);
// Set the adapter pointer for CreateDX11device to use temporarily
spoutdx.SetAdapterPointer(pAdapter);
// Create a dummy device using this adapter
pDummyDevice = spoutdx.CreateDX11device();
if (pDummyDevice) {
// Try to open the share handle with the device created from the adapter
if (spoutdx.OpenDX11shareHandle(pDummyDevice, &pSharedTexture, LongToHandle((long)info.shareHandle))) {
// break as soon as it succeeds
SpoutLogNotice(" found sender adapter %d (0x%.7X)", i, PtrToUint(pAdapter));
senderadapter = i;
// Return the adapter name
if (adaptername)
spoutdx.GetAdapterName(i, adaptername, maxchars);
pDummyDevice->GetImmediateContext(&pContext);
if (pContext) pContext->Flush();
pDummyDevice->Release();
pAdapter->Release();
break;
}
pDummyDevice->GetImmediateContext(&pContext);
if (pContext) pContext->Flush();
pDummyDevice->Release();
}
pAdapter->Release();
}
}
}

// Set the SpoutDirectX class adapter pointer back to what it was
spoutdx.SetAdapterPointer(pCurrentAdapter);

return senderadapter;

}


//---------------------------------------------------------
// Function: GetAdapterInfo
// Get the current adapter description
Expand Down Expand Up @@ -2011,13 +2081,15 @@ bool Spout::ReceiveSenderData()

// Get a new shared texture pointer (m_pSharedTexture)
if (!spoutdx.OpenDX11shareHandle(spoutdx.GetDX11Device(), &m_pSharedTexture, dxShareHandle)) {

// If this fails, something is wrong.
// The sender graphics adapter might be different or some other reason.
// Error log is generated in OpenDX11shareHandle.
// SpoutLogWarning("Spout::ReceiveSenderData - could not retrieve sender texture from share handle");
// The sender graphics adapter might be different.
// Warning not required here -Error log is generated in OpenDX11shareHandle.

// Retain the share handle so we don't query the same sender again.
// m_pSharedTexture is null but will not be used.
// Return true and wait until another sender is selected.
// Wait until another sender is selected or the shared texture handle is valid.

return true;
}
}
Expand Down Expand Up @@ -2062,6 +2134,7 @@ bool Spout::ReceiveSenderData()

}


//---------------------------------------------------------
// Check whether SpoutPanel opened and return the new sender name
bool Spout::CheckSpoutPanel(char *sendername, int maxchars)
Expand Down
9 changes: 7 additions & 2 deletions SPOUTSDK/SpoutGL/Spout.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ class SPOUT_DLLEXP Spout : public spoutGL {
int GetAdapter();
// Set graphics adapter for output
bool SetAdapter(int index = 0);
// Get sender adapter index and name for a given sender
int GetSenderAdapter(const char* sendername, char* adaptername = nullptr, int maxchars = 256);
// Get the current adapter description
bool GetAdapterInfo(char *renderdescription, char *displaydescription, int maxchars);


//
// 2.006 compatibility
//
Expand Down Expand Up @@ -242,13 +245,15 @@ class SPOUT_DLLEXP Spout : public spoutGL {
void InitReceiver(const char * sendername, unsigned int width, unsigned int height, DWORD dwFormat);
// Receiver find sender and retrieve information
bool ReceiveSenderData();

//
// Class global
// Class globals
//

// Graphics adapter name
char m_AdapterName[256];
bool m_bAdapt; // Receiver adapt to the sender adapter


};

Expand Down
5 changes: 3 additions & 2 deletions SPOUTSDK/SpoutGL/SpoutCopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
09.12.20 - Correct movsd line pitch in RemovePadding
13.03.21 - Change CopyPixels and FlipBuffer to accept GL_LUMINANCE
09.07.21 - memcpy_sse2 - return for null dst or src
21.02.22 - use std:: prefix for floor in rgba2rgbResample for Clang compatibility. PR#81
*/
Expand Down Expand Up @@ -1102,8 +1103,8 @@ void spoutCopy::rgba2bgrResample(const void* source, void* dest,
unsigned int pixel, nearestMatch;
for (i = 0; i < destHeight; i++) {
for (j = 0; j < destWidth; j++) {
px = floor((float)j*x_ratio);
py = floor((float)i*y_ratio);
px = std::floor((float)j*x_ratio);
py = std::floor((float)i*y_ratio);
if (bInvert)
pixel = (destHeight - i - 1)*destWidth * 3 + j * 3; // flip vertically
else
Expand Down
1 change: 1 addition & 0 deletions SPOUTSDK/SpoutGL/SpoutCopy.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <intrin.h> // for cpuid to test for SSE2
#include <emmintrin.h> // for SSE2
#include <tmmintrin.h> // for SSSE3
#include <cmath> // For compatibility with Clang. PR#81


class SPOUT_DLLEXP spoutCopy {
Expand Down
57 changes: 33 additions & 24 deletions SPOUTSDK/SpoutGL/SpoutDirectX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
// 17.12.21 - Use passed texture pointer directly in CreateDX11Texture
// 26.12.21 - Context flush after texture release in ReleaseDX11Texture
// Clean up DebugLog function
// 25.01.22 - Correct log notice in ReleaseDX11Texture to show texture instead of device
// Move adapter pointer release from release device to destructor
//
// ====================================================================================
/*
Expand Down Expand Up @@ -159,6 +161,12 @@ spoutDirectX::spoutDirectX() {

spoutDirectX::~spoutDirectX() {

// Release adapter pointer if specified by SetAdapter
if (m_pAdapterDX11) {
m_pAdapterDX11->Release();
m_pAdapterDX11 = nullptr;
}

}

// Function: OpenDirectX11
Expand All @@ -170,10 +178,10 @@ bool spoutDirectX::OpenDirectX11(ID3D11Device* pDevice)
// Can be set externally (See also - SetDX11device)
if (m_pd3dDevice) {
if (m_bClassDevice) {
SpoutLogNotice("spoutDirectX::OpenDirectX11(0x%.7X) - Class device initialized", PtrToUint(m_pd3dDevice));
SpoutLogNotice("spoutDirectX::OpenDirectX11(0x%.7X) - Class device already initialized", PtrToUint(m_pd3dDevice));
}
else {
SpoutLogNotice("spoutDirectX::OpenDirectX11(0x%.7X) - External device", PtrToUint(m_pd3dDevice));
SpoutLogNotice("spoutDirectX::OpenDirectX11(0x%.7X) - External device used", PtrToUint(m_pd3dDevice));
}
return true;
}
Expand Down Expand Up @@ -203,10 +211,6 @@ bool spoutDirectX::OpenDirectX11(ID3D11Device* pDevice)
return false;
}

// ?J DEBUG
SpoutLogNotice(" Device (0x%.7X) - Context (0x%.7X)", PtrToUint(m_pd3dDevice), PtrToUint(m_pImmediateContext));
// SpoutLogNotice(" Device (0x%.7X)", PtrToUint(m_pd3dDevice));

return true;

}
Expand All @@ -233,7 +237,7 @@ void spoutDirectX::CloseDirectX11()
// An application device was used (SetDX11Device). Do not release it.
SpoutLogNotice("spoutDirectX::CloseDirectX11 - external device used (0x%.7X)", PtrToUint(m_pd3dDevice));
// Release adapter pointer if specified by SetAdapter
// Normally done in ReleaseDX11Device
// Normally done in destructor
if (m_pAdapterDX11)
m_pAdapterDX11->Release();
m_pAdapterDX11 = nullptr;
Expand Down Expand Up @@ -285,9 +289,12 @@ ID3D11Device* spoutDirectX::CreateDX11device()
ID3D11Device* pd3dDevice = nullptr;
HRESULT hr = S_OK;
UINT createDeviceFlags = 0;

// Adapter pointer null by default
// or specified by SetAdapter or SetAdapterPointer
IDXGIAdapter* pAdapterDX11 = m_pAdapterDX11;

SpoutLogNotice("spoutDirectX::CreateDX11device - pAdapterDX11 (0x%.7X)", PtrToUint(m_pAdapterDX11) );
SpoutLogNotice("spoutDirectX::CreateDX11device - pAdapter (0x%.7X)", PtrToUint(pAdapterDX11) );

//
// If the project is in a debug build, enable debugging via SDK Layers with this flag.
Expand All @@ -300,9 +307,11 @@ ID3D11Device* spoutDirectX::CreateDX11device()
// See also : void spoutDirectX::DebugLog
//

/*
#if defined(_DEBUG)
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
*/

// GL/DX interop Spec
// ID3D11Device can only be used on WDDM operating systems : Must be multithreaded
Expand Down Expand Up @@ -389,7 +398,7 @@ ID3D11Device* spoutDirectX::CreateDX11device()

// All OK - return the device pointer to the caller
// m_pImmediateContext has also been created
SpoutLogNotice(" device (0x%.7X)", PtrToUint(pd3dDevice) );
SpoutLogNotice(" Device (0x%.7X) - Context (0x%.7X)", PtrToUint(pd3dDevice), PtrToUint(m_pImmediateContext));

return pd3dDevice;

Expand Down Expand Up @@ -672,7 +681,6 @@ bool spoutDirectX::OpenDX11shareHandle(ID3D11Device* pDevice, ID3D11Texture2D**
return false;
}


// Can get sender format here
// ID3D11Texture2D * texturePointer = *ppSharedTexture;
// D3D11_TEXTURE2D_DESC td;
Expand Down Expand Up @@ -730,14 +738,19 @@ bool spoutDirectX::SetAdapter(int index)
SpoutLogNotice("spoutDirectX::SetAdapter(%d) [%s]", index, adaptername);

// Get the adapter pointer for DX11 CreateDevice to use
if (m_pAdapterDX11) m_pAdapterDX11->Release();
if (m_pAdapterDX11) {
m_pAdapterDX11->Release();
m_pAdapterDX11 = nullptr;
}

pAdapter = GetAdapterPointer(index);
if(!pAdapter) {
SpoutLogError("spoutDirectX::SetAdapter - could not get pointer for adapter %d", index);
return false;
}

// Set the global adapter index (used for DX9 and to retrieve the index)
// Set the global adapter index
// (used for DX11 create device and to retrieve the index)
m_AdapterIndex = index;

// Set the adapter pointer for DX11
Expand All @@ -747,6 +760,7 @@ bool spoutDirectX::SetAdapter(int index)

// For >= 2.007 only DX11 test is supported
SpoutLogNotice(" creating test device");
// printf(" testing adapter %d (0x%7.7X) [%s]", m_AdapterIndex, PtrToUint(m_pAdapterDX11), adaptername);

// Try to create a DirectX 11 device for this adapter
ID3D11Device* pd3dDevice = CreateDX11device();
Expand All @@ -757,13 +771,15 @@ bool spoutDirectX::SetAdapter(int index)
m_pAdapterDX11 = nullptr;
return false;
}

// Close the device because this is just a test
// See : https://github.com/leadedge/Spout2/issues/17
ReleaseDX11Device(pd3dDevice);
pd3dDevice = nullptr;

// Selected adapter OK
SpoutLogNotice(" successfully set adapter %d [%s]", m_AdapterIndex, adaptername);
SpoutLogNotice(" successfully set adapter %d (0x%7.7X) [%s]",
m_AdapterIndex, PtrToUint(m_pAdapterDX11), adaptername);

return true;

Expand Down Expand Up @@ -872,7 +888,7 @@ int spoutDirectX::GetNumAdapters()
// Is there a first first output on this adapter ?
if (adapter1_ptr->EnumOutputs(0, &p_output) == DXGI_ERROR_NOT_FOUND) {
// Warning only
SpoutLogWarning("spoutDirectX::GetNumAdapters Adapter(%d) : No outputs", i);
SpoutLogWarning("spoutDirectX::GetNumAdapters - No outputs for Adapter %d : %S", i, desc.Description);
}
else {
p_output->Release();
Expand Down Expand Up @@ -1037,15 +1053,15 @@ unsigned long spoutDirectX::ReleaseDX11Texture(ID3D11Device* pd3dDevice, ID3D11T
return 0;
}

SpoutLogNotice("spoutDirectX::ReleaseDX11Texture (0x%.7X)", PtrToUint(pd3dDevice) );
SpoutLogNotice("spoutDirectX::ReleaseDX11Texture (0x%.7X)", PtrToUint(pTexture) );
unsigned long refcount = pTexture->Release();
pTexture = nullptr;

// The device will be live, so warn if refcount > 1
if (refcount > 1) {
SpoutLogWarning("spoutDirectX::ReleaseDX11Texture - refcount = %lu", refcount);
DebugLog(pd3dDevice, "spoutDirectX::ReleaseDX11Texture - refcount = %lu\n", refcount);
printf("spoutDirectX::ReleaseDX11Texture - refcount = %lu\n", refcount);
// printf("spoutDirectX::ReleaseDX11Texture - refcount = %lu\n", refcount);
}

// Calling Flush will destroy any objects whose destruction has been deferred.
Expand All @@ -1064,12 +1080,6 @@ unsigned long spoutDirectX::ReleaseDX11Device(ID3D11Device* pd3dDevice)
if (!pd3dDevice)
return 0;

// Release adapter pointer if specified by SetAdapter
if (m_pAdapterDX11) {
m_pAdapterDX11->Release();
m_pAdapterDX11 = nullptr;
}

// Release the global context or there is an outstanding ref count
// when the device is released
if (m_pImmediateContext) {
Expand Down Expand Up @@ -1156,14 +1166,13 @@ void spoutDirectX::DebugLog(ID3D11Device* pd3dDevice, const char* format, ...)
if (!pd3dDevice)
return;


//
// Output for debug build
// Manually remove the comment block below if you have D3D11_1SDKLayers.dll installed.
// See comments in : ID3D11Device* spoutDirectX::CreateDX11device()
//

// Construct the log here to avoid UNREFERENCED_PARAMETER warning
// Construct the log here in any case to avoid UNREFERENCED_PARAMETER warning
char dlog[128];
va_list args;
va_start(args, format);
Expand Down
Loading

0 comments on commit fea2bf5

Please sign in to comment.