From ab36fe24ec3f3aa6f41be6caaa1e043445d5f7d7 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Wed, 21 May 2025 07:10:54 +0100 Subject: [PATCH 1/2] fixes for opengl uniform buffer loading This fix allows uniform buffers to be used with glsl shaders. Small issue with uniform buffers not being initialized correctly. --- Engine/source/gfx/gl/gfxGLDevice.cpp | 4 ++++ Engine/source/gfx/gl/gfxGLShader.cpp | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Engine/source/gfx/gl/gfxGLDevice.cpp b/Engine/source/gfx/gl/gfxGLDevice.cpp index 148a68648..766fbccd8 100644 --- a/Engine/source/gfx/gl/gfxGLDevice.cpp +++ b/Engine/source/gfx/gl/gfxGLDevice.cpp @@ -323,9 +323,13 @@ GLuint GFXGLDevice::getDeviceBuffer(const GFXShaderConstDesc desc) GLuint uboHandle; glGenBuffers(1, &uboHandle); + glBindBuffer(GL_UNIFORM_BUFFER, uboHandle); + glBufferData(GL_UNIFORM_BUFFER, desc.size, NULL, GL_DYNAMIC_DRAW); // allocate once mDeviceBufferMap[name] = uboHandle; + glBindBuffer(GL_UNIFORM_BUFFER, 0); + return uboHandle; } diff --git a/Engine/source/gfx/gl/gfxGLShader.cpp b/Engine/source/gfx/gl/gfxGLShader.cpp index e13ff1a52..ee156a6e0 100644 --- a/Engine/source/gfx/gl/gfxGLShader.cpp +++ b/Engine/source/gfx/gl/gfxGLShader.cpp @@ -713,7 +713,8 @@ void GFXGLShader::initConstantDescs() // fill out ubo desc. desc.name = String((char*)uboName); - desc.bindPoint = uboBinding; + desc.bindPoint = uboBinding == 0 ? glGetUniformBlockIndex(mProgram, uboName) : uboBinding; + glUniformBlockBinding(mProgram, glGetUniformBlockIndex(mProgram, uboName), desc.bindPoint); desc.size = uboSize; desc.constType = GFXSCT_ConstBuffer; desc.samplerReg = -1; @@ -888,7 +889,7 @@ void GFXGLShader::initHandles() // Index element 1 of the name to skip the '$' we inserted earier. GLint loc = glGetUniformLocation(mProgram, &desc.name.c_str()[1]); - AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str())); + //AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str())); HandleMap::Iterator handle = mHandles.find(desc.name); S32 sampler = -1; From f40c20d2f4c2227e0023dd05dffc012a2e817784 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Wed, 21 May 2025 07:16:45 +0100 Subject: [PATCH 2/2] Update gfxGLShader.cpp restore assertfatal if the bindPoint ==-1 as these require a location, ubo uniforms location returns -1 --- Engine/source/gfx/gl/gfxGLShader.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Engine/source/gfx/gl/gfxGLShader.cpp b/Engine/source/gfx/gl/gfxGLShader.cpp index ee156a6e0..69ea74f34 100644 --- a/Engine/source/gfx/gl/gfxGLShader.cpp +++ b/Engine/source/gfx/gl/gfxGLShader.cpp @@ -889,7 +889,8 @@ void GFXGLShader::initHandles() // Index element 1 of the name to skip the '$' we inserted earier. GLint loc = glGetUniformLocation(mProgram, &desc.name.c_str()[1]); - //AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str())); + // The location for uniforms inside a UBO come back as -1. + // AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str())); HandleMap::Iterator handle = mHandles.find(desc.name); S32 sampler = -1; @@ -908,6 +909,7 @@ void GFXGLShader::initHandles() { if (desc.bindPoint == -1) { + AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str())); desc.bindPoint = loc; mHandles[desc.name]->mUBOUniform = false; } @@ -922,6 +924,7 @@ void GFXGLShader::initHandles() { if (desc.bindPoint == -1) { + AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str())); desc.bindPoint = loc; mHandles[desc.name] = new GFXGLShaderConstHandle(this, desc); mHandles[desc.name]->mUBOUniform = false;