Hi everyone, new poster here. I’ve been trying to implement tiled deferred shading and I’ve been stuck on a problem for some time now. I’m trying to store an array of structs consisting of point lights that I want to initalize and send to a compute shader where I can process it further. I’m using a Shader Storage Buffer Object for this and I’m trying to use glMapBufferRange to provide the values. I’ve added some checks so that if the array size is 0 I’ll paint the screen red and if it’s larger than 0 it’s yellow, so far I can’t seem to get it to work.
Here’s the compute shader:
#version 430
#define MAX_WORK_GROUP_SIZE 16
#define SCREEN_WIDTH 1280.0f
#define SCREEN_HEIGHT 720.0f
uniform sampler2D positionMap;
uniform sampler2D colorMap;
uniform sampler2D normalMap;
uniform sampler2D depthMap;
layout(binding = 4, rgba32f) uniform writeonly image2D finalImage;
layout(binding = 5, rgba32f) uniform writeonly image2D otherImage;
struct PointLight
{
vec3 position; //4,8,12
vec3 color; // 16,20, 24
float radius; //28
float diffuseIntensity; //32
float ambientIntensity; //36
float Constant; //40
float Linear; //44
float Exp; //48
};
layout(std430, binding = 6) buffer BufferObject
{
PointLight pointLights[];
};
shared uint minDepth;
shared uint maxDepth;
layout(local_size_x = MAX_WORK_GROUP_SIZE, local_size_y = MAX_WORK_GROUP_SIZE)in;
void main()
{
if(gl_LocalInvocationIndex == 0){
minDepth = 0xFFFFFFFF;
maxDepth = 0;
}
ivec2 pixelPos = ivec2(gl_GlobalInvocationID.xy);
vec2 uv = vec2(pixelPos.x / SCREEN_WIDTH, pixelPos.y / SCREEN_HEIGHT);
float d = texture(depthMap,uv).z;
uint depth = uint(d * 0xFFFFFFFF);
//compares the content of minDepth to depth and writes the minimum value to minDepth
atomicMin(minDepth, depth);
// barrier();
//compares the content of maxDepth to depth and writes the maximum value to the maxDepth
atomicMax(maxDepth, depth);
///Write a single texel into an image
/* barrier();
imageStore(finalImage, pixelPos, vec4(float(float(maxDepth) / float(0xFFFFFFFF))));
barrier();
imageStore(otherImage, pixelPos, vec4(float(float(minDepth) / float(0xFFFFFFFF))));
*/
PointLight p = pointLights[0];
PointLight p2 = pointLights[1];
if(pointLights.length() == 0)
{
barrier();
imageStore(finalImage, pixelPos, vec4(1.0,0.0,0.0,1.0));
barrier();
imageStore(otherImage, pixelPos, vec4(1.0,0.0,0.0,1.0));
}
if(pointLights.length() > 0)
{
barrier();
imageStore(finalImage, pixelPos, vec4(1.0,1.0,0.0,1.0));
barrier();
imageStore(otherImage, pixelPos, vec4(1.0,1.0,0.0,1.0));
}
}
Here’s how I’m trying to initalize the buffer with some test values:
My3dVector currentColor(1.0f,1.0f,1.0f);
glGenBuffers(1,&m_pointLightBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER,m_pointLightBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER,NUM_OF_LIGHTS*sizeof(struct TDPointLight), NULL, GL_STATIC_DRAW);
struct TDPointLight* pointlights = (struct TDPointLight*) glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, NUM_OF_LIGHTS*sizeof(struct TDPointLight), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT );
int shit = ARRAY_SIZE_IN_ELEMENTS(pointlights);
for(int i = 0; i < NUM_OF_LIGHTS; ++i)
{
float Max = 80.0f;
float Min = -80.0f;
float MaxZ = 80.0f;
float MinZ = -80.0f;
float ranx = ((float(rand()) / float(RAND_MAX)) * (Max - Min)) + Min;
float ranz = ((float(rand()) / float(RAND_MAX)) * (Max - Min)) + Min;
int maxCol = 8;
int minCol = 1;
//int ranCol = ((rand() / RAND_MAX) * (maxCol - minCol)) + minCol;
int ranCol = (rand()%(maxCol-minCol))+minCol;
if(ranCol == 0)
printf("error, color 8 doesnt exist");
if(ranCol == 1)
currentColor = COLOR_WHITE;
if(ranCol == 2)
currentColor = COLOR_RED;
if(ranCol == 3)
currentColor = COLOR_GREEN;
if(ranCol == 4)
currentColor = COLOR_CYAN;
if(ranCol == 5)
currentColor = COLOR_BLUE;
if(ranCol == 6)
currentColor = COLOR_PURPLE;
if(ranCol == 7)
currentColor = COLOR_ORANGE;
if(ranCol == 8)
printf("error, color 8 doesnt exist");
pointlights[i].position = My3dVector(1.0f,1.0f,1.0f);
pointlights[i].color = My3dVector(1.0f,0.0f,0.0f);
pointlights[i].radius = 10.0f;
pointlights[i].diffuseIntensity = 10.0f;
pointlights[i].ambientIntensity = 0.1f;
//pointlights[i].color = currentColor;
//pointlights[i].position = My3dVector(ranx,3.0f,ranz);
//m_pointLight[i].m_Position = My3dVector(0.0f,2.0f,0.0f);
pointlights[i].Constant = 0.0f;
pointlights[i].Linear = 0.0f;
pointlights[i].Exp = 0.6f;
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
Then the process goes something like:
- Use the compute shader
- Set all the uniforms, then bind some deferred textures and other things using this code
for(unsigned int i = 0; i<ARRAY_SIZE_IN_ELEMENTS(m_textures); ++i)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, m_textures[TDGBuffer_TEXTURE_TYPE_POSITION + i]);
}
glUniform1i(glGetUniformLocation(program,"depthMap"),3);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D,m_depthTexture);
//glActiveTexture(GL_TEXTURE4);
//glBindTexture(GL_TEXTURE_2D,m_finalTexture);
glBindImageTexture(4, m_finalTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
glBindImageTexture(5,m_otherTexture,0,GL_FALSE,0,GL_WRITE_ONLY,GL_RGBA32F);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER,6,m_pointLightBuffer);
Then finally in the main loop call these functions
glDispatchCompute((window_width/MAX_WORK_GROUP_SIZE), (window_height/MAX_WORK_GROUP_SIZE), 1);
glFinish();
It doesn’t work with any memory barriers anywhere in the code, but as I’m not modifying the contents anywhere this shouldn’t be a problem as it just gets initialized once and then remains the same forever.