diff --git a/src/gl/dynlights/gl_lightbuffer.cpp b/src/gl/dynlights/gl_lightbuffer.cpp index c507b1657..b787be9e2 100644 --- a/src/gl/dynlights/gl_lightbuffer.cpp +++ b/src/gl/dynlights/gl_lightbuffer.cpp @@ -52,13 +52,15 @@ FLightBuffer::FLightBuffer() if (gl.flags & RFL_SHADER_STORAGE_BUFFER) { mBufferType = GL_SHADER_STORAGE_BUFFER; - mBlockAlign = 0; + mBlockAlign = -1; + mBlockSize = BUFFER_SIZE; } else { mBufferType = GL_UNIFORM_BUFFER; - mBlockSize = 2048;// gl.maxuniformblock / 4 - 100; - mBlockAlign = 1024;// ((mBlockSize * 2) & ~(gl.uniformblockalignment - 1)) / 4; // count in vec4's + mBlockSize = gl.maxuniformblock / 16; + if (mBlockSize > 2048) mBlockSize = 2048; // we don't really need a larger buffer + mBlockAlign = mBlockSize / 2; } glGenBuffers(1, &mBufferId); @@ -92,6 +94,30 @@ int FLightBuffer::UploadLights(FDynLightData &data) int size2 = data.arrays[2].Size()/4; int totalsize = size0 + size1 + size2 + 1; + if (mBlockAlign >= 0 && totalsize + (mIndex % mBlockAlign) > mBlockSize) + { + mIndex = ((mIndex + mBlockAlign) / mBlockAlign) * mBlockAlign; + + // can't be rendered all at once. + if (totalsize > mBlockSize) + { + int diff = totalsize - mBlockSize; + + size2 -= diff; + if (size2 < 0) + { + size1 += size2; + size2 = 0; + } + if (size1 < 0) + { + size0 += size1; + size1 = 0; + } + totalsize = size0 + size1 + size2 + 1; + } + } + if (totalsize <= 1) return -1; if (mIndex + totalsize > BUFFER_SIZE)