PDA

View Full Version : Compute shader output



Felagund
10-24-2016, 07:57 AM
Hello

I need a help.
I try to copy data from one buffer to another buffer in compute shader and then bind it to GL_ARRAY_BUFFER for render.
Simple example with two triangles works and example with many points not.

Compute shader code:

#version 430
layout (local_size_x = 1) in;

layout (std430, binding = 1) buffer InBuf
{
vec3 input[];
};

layout (std430, binding = 2) buffer OutBuf
{
vec3 output[];
};

void main()
{
uint pos = gl_GlobalInvocationID.x;
output[pos] = input[pos];
}

Application code:

glGenBuffers(1, &depthPixelBuf);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, inputBuf);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, points.data(), GL_STATIC_COPY);

glGenBuffers(1, &depthPointBuf);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, outputBuf);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, NULL, GL_DYNAMIC_DRAW);

glUseProgram(computeProgram);
glDispatchCompute(points.size(), 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

glUseProgram(program);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, outputBuf);
glVertexAttribPointer(vertexAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, points.size());

This example works with about 50000 points from camera. If I do like this:

glBindBuffer(GL_ARRAY_BUFFER, inputBuf);
rendering is OK. So I don't understand what a problem.

GClements
10-24-2016, 08:19 AM
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);


This should be GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT.

The memory barrier needs to indicate the nature of subsequent reads which will be synchronised with stores performed in the shader. In this case, the reads are via vertex attribute arrays.

I have no idea whether this is related to your problem, though.

Felagund
10-24-2016, 08:26 AM
I try not only GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT, but it doesn't metter