From 9e480db31ce760f65829afa2b19798d20d58e0bd Mon Sep 17 00:00:00 2001 From: Aiden Date: Wed, 6 May 2026 09:31:44 +1000 Subject: [PATCH] Further refactor --- CMakeLists.txt | 3 ++ .../gl/GlRenderConstants.h | 9 ++++ .../gl/GlShaderSources.cpp | 38 +++++++++++++++ .../gl/GlShaderSources.h | 4 ++ .../gl/OpenGLComposite.cpp | 47 ++----------------- 5 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 apps/LoopThroughWithOpenGLCompositing/gl/GlRenderConstants.h create mode 100644 apps/LoopThroughWithOpenGLCompositing/gl/GlShaderSources.cpp create mode 100644 apps/LoopThroughWithOpenGLCompositing/gl/GlShaderSources.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a11ef1..4be480f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,7 +49,10 @@ set(APP_SOURCES "${APP_DIR}/decklink/DeckLinkFrameTransfer.h" "${APP_DIR}/gl/GLExtensions.cpp" "${APP_DIR}/gl/GLExtensions.h" + "${APP_DIR}/gl/GlRenderConstants.h" "${APP_DIR}/gl/GlScopedObjects.h" + "${APP_DIR}/gl/GlShaderSources.cpp" + "${APP_DIR}/gl/GlShaderSources.h" "${APP_DIR}/gl/OpenGLComposite.cpp" "${APP_DIR}/gl/OpenGLComposite.h" "${APP_DIR}/gl/OpenGLCompositeRuntimeControls.cpp" diff --git a/apps/LoopThroughWithOpenGLCompositing/gl/GlRenderConstants.h b/apps/LoopThroughWithOpenGLCompositing/gl/GlRenderConstants.h new file mode 100644 index 0000000..17bf1ff --- /dev/null +++ b/apps/LoopThroughWithOpenGLCompositing/gl/GlRenderConstants.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +constexpr GLuint kDecodedVideoTextureUnit = 1; +constexpr GLuint kSourceHistoryTextureUnitBase = 2; +constexpr GLuint kPackedVideoTextureUnit = 2; +constexpr GLuint kGlobalParamsBindingPoint = 0; +constexpr unsigned kPrerollFrameCount = 8; diff --git a/apps/LoopThroughWithOpenGLCompositing/gl/GlShaderSources.cpp b/apps/LoopThroughWithOpenGLCompositing/gl/GlShaderSources.cpp new file mode 100644 index 0000000..6a30536 --- /dev/null +++ b/apps/LoopThroughWithOpenGLCompositing/gl/GlShaderSources.cpp @@ -0,0 +1,38 @@ +#include "GlShaderSources.h" + +const char* kFullscreenTriangleVertexShaderSource = + "#version 430 core\n" + "out vec2 vTexCoord;\n" + "void main()\n" + "{\n" + " vec2 positions[3] = vec2[3](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));\n" + " vec2 texCoords[3] = vec2[3](vec2(0.0, 0.0), vec2(2.0, 0.0), vec2(0.0, 2.0));\n" + " gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);\n" + " vTexCoord = texCoords[gl_VertexID];\n" + "}\n"; + +const char* kDecodeFragmentShaderSource = + "#version 430 core\n" + "layout(binding = 2) uniform sampler2D uPackedVideoInput;\n" + "uniform vec2 uPackedVideoResolution;\n" + "uniform vec2 uDecodedVideoResolution;\n" + "in vec2 vTexCoord;\n" + "layout(location = 0) out vec4 fragColor;\n" + "vec4 rec709YCbCr2rgba(float Y, float Cb, float Cr, float a)\n" + "{\n" + " Y = (Y * 256.0 - 16.0) / 219.0;\n" + " Cb = (Cb * 256.0 - 16.0) / 224.0 - 0.5;\n" + " Cr = (Cr * 256.0 - 16.0) / 224.0 - 0.5;\n" + " return vec4(Y + 1.5748 * Cr, Y - 0.1873 * Cb - 0.4681 * Cr, Y + 1.8556 * Cb, a);\n" + "}\n" + "void main()\n" + "{\n" + " vec2 correctedUv = vec2(vTexCoord.x, 1.0 - vTexCoord.y);\n" + " ivec2 decodedSize = ivec2(max(uDecodedVideoResolution, vec2(1.0, 1.0)));\n" + " ivec2 outputCoord = clamp(ivec2(correctedUv * vec2(decodedSize)), ivec2(0, 0), decodedSize - ivec2(1, 1));\n" + " ivec2 packedSize = ivec2(max(uPackedVideoResolution, vec2(1.0, 1.0)));\n" + " ivec2 packedCoord = ivec2(clamp(outputCoord.x / 2, 0, packedSize.x - 1), clamp(outputCoord.y, 0, packedSize.y - 1));\n" + " vec4 macroPixel = texelFetch(uPackedVideoInput, packedCoord, 0);\n" + " float ySample = (outputCoord.x & 1) != 0 ? macroPixel.a : macroPixel.g;\n" + " fragColor = rec709YCbCr2rgba(ySample, macroPixel.b, macroPixel.r, 1.0);\n" + "}\n"; diff --git a/apps/LoopThroughWithOpenGLCompositing/gl/GlShaderSources.h b/apps/LoopThroughWithOpenGLCompositing/gl/GlShaderSources.h new file mode 100644 index 0000000..2e67ec0 --- /dev/null +++ b/apps/LoopThroughWithOpenGLCompositing/gl/GlShaderSources.h @@ -0,0 +1,4 @@ +#pragma once + +extern const char* kFullscreenTriangleVertexShaderSource; +extern const char* kDecodeFragmentShaderSource; diff --git a/apps/LoopThroughWithOpenGLCompositing/gl/OpenGLComposite.cpp b/apps/LoopThroughWithOpenGLCompositing/gl/OpenGLComposite.cpp index 2c97251..27caff3 100644 --- a/apps/LoopThroughWithOpenGLCompositing/gl/OpenGLComposite.cpp +++ b/apps/LoopThroughWithOpenGLCompositing/gl/OpenGLComposite.cpp @@ -43,7 +43,9 @@ #include "DeckLinkFrameTransfer.h" #include "OpenGLComposite.h" #include "GLExtensions.h" +#include "GlRenderConstants.h" #include "GlScopedObjects.h" +#include "GlShaderSources.h" #include "OscServer.h" #include "RuntimeControlBridge.h" #include "Std140Buffer.h" @@ -63,47 +65,6 @@ namespace { -constexpr GLuint kDecodedVideoTextureUnit = 1; -constexpr GLuint kSourceHistoryTextureUnitBase = 2; -constexpr GLuint kPackedVideoTextureUnit = 2; -constexpr GLuint kGlobalParamsBindingPoint = 0; -constexpr unsigned kPrerollFrameCount = 8; -const char* kVertexShaderSource = - "#version 430 core\n" - "out vec2 vTexCoord;\n" - "void main()\n" - "{\n" - " vec2 positions[3] = vec2[3](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));\n" - " vec2 texCoords[3] = vec2[3](vec2(0.0, 0.0), vec2(2.0, 0.0), vec2(0.0, 2.0));\n" - " gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);\n" - " vTexCoord = texCoords[gl_VertexID];\n" - "}\n"; -const char* kDecodeFragmentShaderSource = - "#version 430 core\n" - "layout(binding = 2) uniform sampler2D uPackedVideoInput;\n" - "uniform vec2 uPackedVideoResolution;\n" - "uniform vec2 uDecodedVideoResolution;\n" - "in vec2 vTexCoord;\n" - "layout(location = 0) out vec4 fragColor;\n" - "vec4 rec709YCbCr2rgba(float Y, float Cb, float Cr, float a)\n" - "{\n" - " Y = (Y * 256.0 - 16.0) / 219.0;\n" - " Cb = (Cb * 256.0 - 16.0) / 224.0 - 0.5;\n" - " Cr = (Cr * 256.0 - 16.0) / 224.0 - 0.5;\n" - " return vec4(Y + 1.5748 * Cr, Y - 0.1873 * Cb - 0.4681 * Cr, Y + 1.8556 * Cb, a);\n" - "}\n" - "void main()\n" - "{\n" - " vec2 correctedUv = vec2(vTexCoord.x, 1.0 - vTexCoord.y);\n" - " ivec2 decodedSize = ivec2(max(uDecodedVideoResolution, vec2(1.0, 1.0)));\n" - " ivec2 outputCoord = clamp(ivec2(correctedUv * vec2(decodedSize)), ivec2(0, 0), decodedSize - ivec2(1, 1));\n" - " ivec2 packedSize = ivec2(max(uPackedVideoResolution, vec2(1.0, 1.0)));\n" - " ivec2 packedCoord = ivec2(clamp(outputCoord.x / 2, 0, packedSize.x - 1), clamp(outputCoord.y, 0, packedSize.y - 1));\n" - " vec4 macroPixel = texelFetch(uPackedVideoInput, packedCoord, 0);\n" - " float ySample = (outputCoord.x & 1) != 0 ? macroPixel.a : macroPixel.g;\n" - " fragColor = rec709YCbCr2rgba(ySample, macroPixel.b, macroPixel.r, 1.0);\n" - "}\n"; - void CopyErrorMessage(const std::string& message, int errorMessageSize, char* errorMessage) { if (!errorMessage || errorMessageSize <= 0) @@ -1224,7 +1185,7 @@ bool OpenGLComposite::compileSingleLayerProgram(const RuntimeRenderState& state, std::string fragmentShaderSource; std::string loadError; std::vector textureBindings; - const char* vertexSource = kVertexShaderSource; + const char* vertexSource = kFullscreenTriangleVertexShaderSource; if (!mRuntimeHost->BuildLayerFragmentShaderSource(state.layerId, fragmentShaderSource, loadError)) { @@ -1397,7 +1358,7 @@ bool OpenGLComposite::compileDecodeShader(int errorMessageSize, char* errorMessa GLsizei errorBufferSize = 0; GLint compileResult = GL_FALSE; GLint linkResult = GL_FALSE; - const char* vertexSource = kVertexShaderSource; + const char* vertexSource = kFullscreenTriangleVertexShaderSource; const char* fragmentSource = kDecodeFragmentShaderSource; ScopedGlShader newVertexShader(glCreateShader(GL_VERTEX_SHADER));