Skip to content

Commit

Permalink
Output texture is now working
Browse files Browse the repository at this point in the history
  • Loading branch information
medcelerate committed Jul 7, 2024
1 parent 6b878bf commit 8638c1f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 59 deletions.
3 changes: 3 additions & 0 deletions FFGLTouchEngine/FFGLTouchEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(SolutionDir)lib\TouchEngine\TouchEngine.lib;$(SolutionDir)lib\Spout\Spout_static.lib;$(SolutionDir)lib\Glew\glew32s.lib;opengl32.lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>copy /Y "$(ProjectDir)Build\$(Platform)\$(Configuration)\FFGLTouchEngine.dll" "$(SolutionDir)..\..\Resolume Arena\Plugins\FFGLTouchEngine.dll"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\FFGL\ffglex\FFGLFBO.cpp" />
Expand Down
107 changes: 57 additions & 50 deletions FFGLTouchEngine/src/TouchEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,13 @@ FFResult FFGLTouchEngine::InitGL(const FFGLViewportStruct* vp)
return FF_FAIL;
}

SPReceiver.SetActiveSender(SpoutID.c_str());




// Create the input texture

// Set the viewport size

Width = vp->width;
Height = vp->height;
OutputWidth = vp->width;
OutputHeight = vp->height;

if (!CreateInputTexture(Width, Height)) {
return FF_FAIL;
}

if (FilePath.empty())
{
Expand Down Expand Up @@ -283,38 +275,57 @@ FFResult FFGLTouchEngine::ProcessOpenGL(ProcessOpenGLStruct* pGL)

RawTextureToSend->GetDesc(&RawTextureDesc);

HANDLE dxShareHandle = nullptr;
if (!isSpoutInitialized) {
SPDirectx.CreateSharedDX11Texture(D3DDevice.Get(), Width, Height, RawTextureDesc.Format, &D3DTextureOutput, dxShareHandle);
isSpoutInitialized = SPSender.CreateSender(SpoutID.c_str(), Width, Height, dxShareHandle, (DWORD)RawTextureDesc.Format);
SPFrameCount.CreateAccessMutex(SpoutID.c_str());
SPFrameCount.EnableFrameCount(SpoutID.c_str());
}
if (!isInteropInitialized) {
OutputInterop.SetSenderName(SpoutID.c_str());

if (!OutputInterop.OpenDirectX11(D3DDevice.Get())) {
FFGLLog::LogToHost("Failed to open DirectX11");
return FF_FAIL;
}

if (RawTextureDesc.Width != Width || RawTextureDesc.Height != Height) {
Width = RawTextureDesc.Width;
Height = RawTextureDesc.Height;
if (D3DTextureOutput != nullptr)
D3DTextureOutput->Release();
if (!OutputInterop.CreateInterop(OutputWidth, OutputHeight, DXGI_FORMAT_B8G8R8A8_UNORM, false)) {
FFGLLog::LogToHost("Failed to create interop");
return FF_FAIL;
}

SPDirectx.CreateSharedDX11Texture(D3DDevice.Get(), Width, Height, RawTextureDesc.Format, &D3DTextureOutput, dxShareHandle);
SPSender.UpdateSender(SpoutID.c_str(), Width, Height, dxShareHandle, (DWORD)RawTextureDesc.Format);
OutputInterop.frame.CreateAccessMutex("mutex");

if (!OutputInterop.spoutdx.CreateDX11Texture(D3DDevice.Get(), OutputWidth, OutputHeight, DXGI_FORMAT_B8G8R8A8_UNORM, &D3DTextureOutput)) {
FFGLLog::LogToHost("Failed to create DX11 texture");
return FF_FAIL;
}

InitializeGlTexture(SpoutTexture, OutputWidth, OutputHeight);

isInteropInitialized = true;
}

IDXGIKeyedMutex* keyedMutex;
auto mapIt = TextureMutexMap.find(RawTextureToSend);
if (mapIt == TextureMutexMap.end())
{
auto y = RawTextureToSend->QueryInterface<IDXGIKeyedMutex>(&keyedMutex);
if (keyedMutex == nullptr) {
if (RawTextureDesc.Width != OutputWidth || RawTextureDesc.Height != OutputHeight) {
OutputWidth = RawTextureDesc.Width;
OutputHeight = RawTextureDesc.Height;

if (!OutputInterop.CleanupInterop()) {
FFGLLog::LogToHost("Failed to cleanup interop");
return FF_FAIL;
}

if (!OutputInterop.CreateInterop(OutputWidth, OutputHeight, DXGI_FORMAT_B8G8R8A8_UNORM, false)) {
FFGLLog::LogToHost("Failed to create interop");
return FF_FAIL;
}
TextureMutexMap[RawTextureToSend] = keyedMutex;

OutputInterop.spoutdx.CreateDX11Texture(D3DDevice.Get(), OutputWidth, OutputHeight, DXGI_FORMAT_B8G8R8A8_UNORM, &D3DTextureOutput);

InitializeGlTexture(SpoutTexture, OutputWidth, OutputHeight);
}
else
{
keyedMutex = mapIt->second;

IDXGIKeyedMutex* keyedMutex;
RawTextureToSend->QueryInterface<IDXGIKeyedMutex>(&keyedMutex);

if (keyedMutex == nullptr) {
return FF_FAIL;
}

//Dynamically change texture size here when wxH changes

TESemaphore* semaphore = nullptr;
Expand All @@ -336,11 +347,8 @@ FFResult FFGLTouchEngine::ProcessOpenGL(ProcessOpenGLStruct* pGL)
keyedMutex->AcquireSync(waitValue, INFINITE);
Microsoft::WRL::ComPtr<ID3D11DeviceContext> devContext;
D3DDevice->GetImmediateContext(&devContext);
if (SPFrameCount.CheckAccess()) {
devContext->CopyResource(D3DTextureOutput.Get(), RawTextureToSend);
SPFrameCount.SetNewFrame();
SPFrameCount.AllowAccess();
}
devContext->CopyResource(D3DTextureOutput.Get(), RawTextureToSend);
OutputInterop.WriteTexture(D3DTextureOutput.GetAddressOf());
keyedMutex->ReleaseSync(waitValue + 1);
result = TEInstanceAddTextureTransfer(instance, TETextureToSend, semaphore, waitValue +1);
if (result != TEResultSuccess)
Expand All @@ -349,25 +357,24 @@ FFResult FFGLTouchEngine::ProcessOpenGL(ProcessOpenGLStruct* pGL)
}
devContext->Flush();
devContext->Release();
keyedMutex->Release();

}

}

OutputInterop.ReadGLDXtexture(SpoutTexture, GL_TEXTURE_2D, OutputWidth, OutputHeight, true, pGL->HostFBO);

//Receiver the texture from spout
if (SPReceiver.ReceiveTexture(SpoutTexture, GL_TEXTURE_2D, true, pGL->HostFBO)) {
if (SPReceiver.IsUpdated()) {
InitializeGlTexture(SpoutTexture, SPReceiver.GetSenderWidth(), SPReceiver.GetSenderHeight());
}

ffglex::ScopedShaderBinding shaderBinding(shader.GetGLID());
ffglex::ScopedSamplerActivation activateSampler(0);
ffglex::Scoped2DTextureBinding textureBinding(SpoutTexture);
shader.Set("InputTexture", 0);
shader.Set("MaxUV", 1.0f, 1.0f);
quad.Draw();
ffglex::ScopedShaderBinding shaderBinding(shader.GetGLID());
ffglex::ScopedSamplerActivation activateSampler(0);
ffglex::Scoped2DTextureBinding textureBinding(SpoutTexture);
shader.Set("InputTexture", 0);
shader.Set("MaxUV", 1.0f, 1.0f);
quad.Draw();

}


}

Expand Down
14 changes: 5 additions & 9 deletions FFGLTouchEngine/src/TouchEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#include "TouchEngine/TouchObject.h"
#include "TouchEngine/TEGraphicsContext.h"
#include "TouchEngine/TED3D11.h"
#include "SpoutGL/SpoutDirectX.h"
#include "SpoutGL/SpoutReceiver.h"
#include "SpoutGL/SpoutSender.h"
#include "Thumbnail.h"


Expand Down Expand Up @@ -74,15 +73,12 @@ class FFGLTouchEngine : public CFFGLPlugin

//Spout Configs
std::string SpoutID;
SpoutReceiver SPReceiver;
spoutDirectX SPDirectx;
spoutSenderNames SPSender;
spoutFrameCount SPFrameCount;
bool isSpoutInitialized = false;
Spout OutputInterop;
bool isInteropInitialized = false;


int Width = 0;
int Height = 0;
int OutputWidth = 0;
int OutputHeight = 0;


std::string FilePath;
Expand Down

0 comments on commit 8638c1f

Please sign in to comment.