0

I'm working on my very first compute shader and I want to use it generate points as part of a larger project but I seem to be missing something.

How do I make everything play nice?

This is my compute shader code (after specifying version 450):

struct point {
    vec3 p;
    float padding;
    vec4 c;
};

layout(std140, binding=4) buffer particlebuffer{
      point pt[];
};

void main() {
     int index = int(gl_GlobalInvocationID);
     pt[index].p = vec3(index/50,index/50,index/50);
     pt[index].c = vec4(1.0, 0.0, 0.0, 1.0);
}

(I read somewhere that the compute shader works best with structs that contain padding.)

I also have a vertex and fragment shader that I have tested on actual points, called shaderProgram. I know that shaderProgram works well with my mvp matrix.

Here's my vertex shader:

uniform mat4 mvp;

layout (location = 0) in vec3 position;

in vec4 color;
out vec4 vertcolor;

void main()
{
    gl_Position = mvp * vec4(position, 1.0f);
    endcolor = color;
}

And here's my frag shader:

in vec4 vertcolor;
out vec4 finalcolor;

void main()
{
    finalcolor = vertcolor;
}

My shaders compile with no issues. After reading this post on using compute shader buffer data in vertex shader, I wrote the following code to initialize a GL_SHADER_STORAGE_BUFFER to hold the generated points, as well as a VAO to receive these points:

glGenBuffers(1, &particlebuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, particlebuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, 8 * numparticles, &particlebuffer, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, particlebuffer);

glUseProgram(compShaderProgram);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);

glUseProgram(shaderProgram);
glGenVertexArrays(1, &computeVAO);
glBindBuffer(GL_ARRAY_BUFFER, particlebuffer)

positionLoc = glGetUniformLocation(shaderProgram, "position");
glEnableVertexAttribArray(positionLoc);
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);

colorLoc = glGetUniformLocation(shaderProgram, "color");
glEnableVertexAttribArray(colorLoc);
glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid *)16);

glBindVertexArray(0);

And then I have this in my draw loop:

glUseProgram(shaderProgram);
glBindVertexArray(computeVAO);
glDrawArrays(GL_POINTS, 0, numparticles);
glBindVertexArray(0);

I'm fairly new to OpenGL - any help and suggestions would be greatly appreciated

  • I'm not sure but I think your struct should be { vec4 p; vec4 c; }. This question ( http://stackoverflow.com/questions/38172696/should-i-ever-use-a-vec3-inside-of-a-uniform-buffer-or-shader-storage-buffer-o ) might be related – cmourglia Jul 09 '16 at 06:26
  • Hi Zouch, thanks for the suggestion. But when I change the struct I still get no points - is there anything that I would have to change in the main code after creating a { vec4 p; vec4 c; } struct inside my comp shader? – jacques-vr Jul 12 '16 at 19:09

0 Answers0