From b3879ff1d73828b7a723ccf0388fc03fd095c8fa Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Mon, 30 Oct 2023 00:44:39 +1100 Subject: [PATCH] [host] windows: the vertex shader is common to all post-processors --- host/platform/Windows/capture/DXGI/src/dxgi.c | 72 +++++++++++++- .../Windows/capture/DXGI/src/dxgi_capture.h | 1 + .../capture/DXGI/src/pp/sdrwhitelevel.c | 94 +++++-------------- 3 files changed, 95 insertions(+), 72 deletions(-) diff --git a/host/platform/Windows/capture/DXGI/src/dxgi.c b/host/platform/Windows/capture/DXGI/src/dxgi.c index 5a9b934e..2d26caa9 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi.c +++ b/host/platform/Windows/capture/DXGI/src/dxgi.c @@ -271,6 +271,57 @@ static bool dxgi_create(CaptureGetPointerBuffer getPointerBufferFn, CapturePostP return true; } +static bool initVertexShader(void) +{ + static const char * vshaderSrc = + "void main(\n" + " in uint vertexID : SV_VERTEXID,\n" + " out float4 position : SV_POSITION,\n" + " out float2 texCoord : TEXCOORD0)\n" + "{\n" + " float2 positions[4] =\n" + " {\n" + " float2(-1.0, 1.0),\n" + " float2( 1.0, 1.0),\n" + " float2(-1.0, -1.0),\n" + " float2( 1.0, -1.0)\n" + " };\n" + "\n" + " float2 texCoords[4] =\n" + " {\n" + " float2(0.0, 0.0),\n" + " float2(1.0, 0.0),\n" + " float2(0.0, 1.0),\n" + " float2(1.0, 1.0)\n" + " };\n" + "\n" + " position = float4(positions[vertexID], 0.0, 1.0);\n" + " texCoord = texCoords[vertexID];\n" + "}"; + + // compile and create the vertex shader + comRef_defineLocal(ID3DBlob, byteCode); + if (!compileShader(byteCode, "main", "vs_5_0", vshaderSrc)) + return false; + + comRef_defineLocal(ID3D11VertexShader, vshader); + HRESULT status = ID3D11Device_CreateVertexShader( + *this->device, + ID3D10Blob_GetBufferPointer(*byteCode), + ID3D10Blob_GetBufferSize (*byteCode), + NULL, + vshader); + + if (FAILED(status)) + { + DEBUG_WINERROR("Failed to create the vertex shader", status); + return false; + } + + comRef_toGlobal(this->vshader, vshader); + return true; +} + static bool dxgi_init(void) { DEBUG_ASSERT(this); @@ -758,6 +809,9 @@ static bool dxgi_init(void) vector_create(&this->texture[i].pp, sizeof(PostProcessInstance), 0); } + if (!initVertexShader()) + goto fail; + const D3D11_VIEWPORT vp = { .TopLeftX = 0.0f, @@ -1371,8 +1425,24 @@ static ID3D11Texture2D * ppRun(Texture * tex, ID3D11Texture2D * src) inst->src = src; } + // set the vertex shader + ID3D11DeviceContext_VSSetShader( + *this->deviceContext, *this->vshader, NULL, 0); + // run the post processor - src = inst->pp->run(inst->opaque, inst->srv); + ID3D11Texture2D * out = inst->pp->run(inst->opaque, inst->srv); + + // if the post processor returned a different texture then draw to run it + if (out == src) + continue; + + // draw the full screen quad + ID3D11DeviceContext_IASetPrimitiveTopology( + *this->deviceContext, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + ID3D11DeviceContext_Draw(*this->deviceContext, 4, 0); + + // the output is now the input + src = out; } return src; diff --git a/host/platform/Windows/capture/DXGI/src/dxgi_capture.h b/host/platform/Windows/capture/DXGI/src/dxgi_capture.h index 9e8d06ac..6b7572f4 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi_capture.h +++ b/host/platform/Windows/capture/DXGI/src/dxgi_capture.h @@ -94,6 +94,7 @@ struct DXGIInterface DXGI_FORMAT dxgiSrcFormat, dxgiFormat; bool hdr; DXGI_COLOR_SPACE_TYPE dxgiColorSpace; + ID3D11VertexShader ** vshader; struct DXGICopyBackend * backend; CaptureGetPointerBuffer getPointerBufferFn; diff --git a/host/platform/Windows/capture/DXGI/src/pp/sdrwhitelevel.c b/host/platform/Windows/capture/DXGI/src/pp/sdrwhitelevel.c index a11bd42f..067f3352 100644 --- a/host/platform/Windows/capture/DXGI/src/pp/sdrwhitelevel.c +++ b/host/platform/Windows/capture/DXGI/src/pp/sdrwhitelevel.c @@ -32,7 +32,6 @@ typedef struct SDRWhiteLevel ID3D11Device ** device; ID3D11DeviceContext ** context; - ID3D11VertexShader ** vshader; ID3D11PixelShader ** pshader; ID3D11SamplerState ** sampler; ID3D11Buffer ** buffer; @@ -56,49 +55,6 @@ struct ShaderConsts } __attribute__((aligned(16))); -static const char * vshader = - "void main(\n" - " in uint vertexID : SV_VERTEXID,\n" - " out float4 position : SV_POSITION,\n" - " out float2 texCoord : TEXCOORD0)\n" - "{\n" - " float2 positions[4] =\n" - " {\n" - " float2(-1.0, 1.0),\n" - " float2( 1.0, 1.0),\n" - " float2(-1.0, -1.0),\n" - " float2( 1.0, -1.0)\n" - " };\n" - "\n" - " float2 texCoords[4] =\n" - " {\n" - " float2(0.0, 0.0),\n" - " float2(1.0, 0.0),\n" - " float2(0.0, 1.0),\n" - " float2(1.0, 1.0)\n" - " };\n" - "\n" - " position = float4(positions[vertexID], 0.0, 1.0);\n" - " texCoord = texCoords[vertexID];\n" - "}"; - -static const char * pshader = - "Texture2D gInputTexture : register(t0);\n" - "SamplerState gSamplerState : register(s0);\n" - "cbuffer gConsts : register(b0)\n" - "{\n" - " float SDRWhiteLevel;" - "};\n" - "\n" - "float4 main(\n" - " float4 position : SV_POSITION,\n" - " float2 texCoord : TEXCOORD0) : SV_TARGET" - "{\n" - " float4 color = gInputTexture.Sample(gSamplerState, texCoord);\n" - " color.rgb *= SDRWhiteLevel;\n" - " return color;\n" - "}\n"; - static void updateConsts(void); static bool sdrWhiteLevel_setup( @@ -132,34 +88,34 @@ static bool sdrWhiteLevel_setup( goto exit; } - // compile and create the vertex shader + static const char * pshaderSrc = + "Texture2D gInputTexture : register(t0);\n" + "SamplerState gSamplerState : register(s0);\n" + "cbuffer gConsts : register(b0)\n" + "{\n" + " float SDRWhiteLevel;" + "};\n" + "\n" + "float4 main(\n" + " float4 position : SV_POSITION,\n" + " float2 texCoord : TEXCOORD0) : SV_TARGET" + "{\n" + " float4 color = gInputTexture.Sample(gSamplerState, texCoord);\n" + " color.rgb *= SDRWhiteLevel;\n" + " return color;\n" + "}\n"; + comRef_defineLocal(ID3DBlob, byteCode); - if (!compileShader(byteCode, "main", "vs_5_0", vshader)) - goto exit; - - status = ID3D11Device_CreateVertexShader( - *this.device, - ID3D10Blob_GetBufferPointer(*byteCode), - ID3D10Blob_GetBufferSize (*byteCode), - NULL, - (ID3D11VertexShader **)comRef_newGlobal(&this.vshader)); - - if (FAILED(status)) - { - DEBUG_WINERROR("Failed to create the vertex shader", status); - goto exit; - } - - comRef_release(byteCode); - if (!compileShader(byteCode, "main", "ps_5_0", pshader)) + if (!compileShader(byteCode, "main", "ps_5_0", pshaderSrc)) goto exit; + comRef_defineLocal(ID3D11PixelShader, pshader); status = ID3D11Device_CreatePixelShader( *this.device, ID3D10Blob_GetBufferPointer(*byteCode), ID3D10Blob_GetBufferSize (*byteCode), NULL, - (ID3D11PixelShader **)comRef_newGlobal(&this.pshader)); + pshader); if (FAILED(status)) { @@ -167,6 +123,8 @@ static bool sdrWhiteLevel_setup( goto exit; } + comRef_toGlobal(this.pshader, pshader); + const D3D11_SAMPLER_DESC samplerDesc = { .Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR, @@ -320,8 +278,7 @@ static ID3D11Texture2D * sdrWhiteLevel_run(void * opaque, updateConsts(); - // set the vertex and pixel shader - ID3D11DeviceContext_VSSetShader(*this.context, *this.vshader, NULL, 0); + // set the pixel shader & resource ID3D11DeviceContext_PSSetShader(*this.context, *this.pshader, NULL, 0); // set the pixel shader resources @@ -332,11 +289,6 @@ static ID3D11Texture2D * sdrWhiteLevel_run(void * opaque, // set the render target ID3D11DeviceContext_OMSetRenderTargets(*this.context, 1, inst->target, NULL); - ID3D11DeviceContext_IASetPrimitiveTopology( - *this.context, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); - - ID3D11DeviceContext_Draw(*this.context, 4, 0); - return *inst->tex; }