Problem trying to run compute shader

Hi,

I’m trying to write a basic compute shader, the ultimate goal will be to deform the vertices using a noise function but to start with just setting the vertex points to 1.0f.

The shader doesn’t seem to be modifying any of the values, as they are being read the same as they went in.

Here is the basic glsl code:

#version 460 core

layout(local_size_x = 1) in;

layout(std430, binding = 0) buffer Vertex {
    vec3 outVertices[];
};

void main() {
    outVertices[gl_GlobalInvocationID.x * 3] = vec3(1.0f);
}

and this C++ code:

ComputeShader computeShader("addSimplexNoise.comp.glsl");


glUseProgram(computeShader.getShaderProgram());

GLuint vertexSSBO;
glGenBuffers(1, &vertexSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, vertexSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_DYNAMIC_DRAW);

glDispatchCompute(vertices.size(), 1, 1);

glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vertexSSBO);


glm::vec3 *ptr;
int numVertices = vertices.size();
ptr = (glm::vec3 *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);

vertices.clear();

for (int i = 0; i < numVertices; i++) {
    vertices.push_back(ptr[i]);
}

glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

This apparently has to keep being repeated: memory barriers describe how you’re going to use the memory, not how you wrote to it. You aren’t using the data written as an SSBO; you’re mapping it for host reading. That’s GL_BUFFER_UPDATE_BARRIER_BIT.

Thank you for letting me know I’ve used the wrong barrier, however the code doesn’t work using that or GL_ALL_BARRIER_BITS.

Thanks for your help, I feel quite embarrassed but the issue was unrelated to openGL, but rather an error in my CMAKE file due to my IDE refactoring incorrectly. I changed the filename for my shader, however the new file wasn’t being copied.