Hi KaiHH,
thank you very much for your fast reply.
As you probably see, I'm not really used to working with the gpu yet

I have a few questions there after implementing your suggestions.
I understand that the use of glBufferSubData() is clearly wrong for
reading values

What I don't understand is why I should bind to index 1 when the documentation of glBindBufferBase
says this is the "index of the binding point within the array specified by target" and target is GL_ATOMIC_COUNTER_BUFFER.
I set it to 1 for now, as you suggested.
About the barriers: I use the GL_SHADER_IMAGE_ACCESS_BARRIER_BIT for the actual image output of the compute shader. Should I add the GL_ATOMIC_COUNTER_BARRIER_BIT to it or use GL_ALL_BARRIER_BITS like I do below?
I post the full code of the compute function here (including the changes from your suggestion)
It all worked fine without the new atomic counter part

private void compute() {
atomicBuffer = glGenBuffers();
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, atomicBuffer);
glBufferData(GL_ATOMIC_COUNTER_BUFFER, 1, GL_DYNAMIC_COPY);
List<Byte> counters = new ArrayList<>();
ByteBuffer counter = glMapBuffer(GL_ATOMIC_COUNTER_BUFFER, GL_WRITE_ONLY);
counter.put((byte) 42);
counter.rewind();
glUnmapBuffer(GL_ATOMIC_COUNTER_BUFFER);
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, atomicBuffer);
// Compute
glUseProgram(computeProgram);
glDispatchCompute(numGroupsX, numGroupsY, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
/* Display framebuffer texture ( here quadProgram is used with fragment and vertex shader only to display the output of the compute shader, which works well.
*
*/
glUseProgram(quadProgram);
glDrawArrays(GL_TRIANGLES, 0, 3);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
/* Get atomic counter values
*
*/
ByteBuffer counter2 = ByteBuffer.allocate(2); // <<-- When I use allocate(1) here, the program crashes without exception or error-log, with any bigger value I get Message: GL_INVALID_VALUE error generated. Offset and/or size is out of range.
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 1, atomicBuffer);
glGetBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, counter2);
while (counter2.hasRemaining()) {
byte val = counter2.get();
System.out.println(val);
counters.add(val);
}
System.out.println(Arrays.toString(counters.toArray()));
}
private void create_vao_fb_vs_fs_compute(int w, int h) {
create_vao();
create_framebuffer(w, h);
create_vs_fs();
create_compute();
}
private void create_vao() {
glBindVertexArray(glGenVertexArrays());
}
private void create_framebuffer(int w, int h) {
framebuffer = glGenTextures();
glBindTexture(GL_TEXTURE_2D, framebuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, w, h);
glBindImageTexture(0, framebuffer, 0, false, 0, GL_WRITE_ONLY, GL_RGBA8);
}
private void create_vs_fs() {
quadProgram = glCreateProgram();
vsShader = glCreateShader(GL_VERTEX_SHADER);
glAttachShader(quadProgram, vsShader);
fsShader = glCreateShader(GL_FRAGMENT_SHADER);
glAttachShader(quadProgram, fsShader);
}
private void create_compute() {
computeProgram = glCreateProgram();
computeShader = glCreateShader(GL_COMPUTE_SHADER);
glAttachShader(computeProgram, computeShader);
}
Output is:
[LWJGL] OpenGL debug message
ID: 0x501
Source: API
Type: ERROR
Severity: HIGH
Message: GL_INVALID_VALUE error generated. Offset and/or size is out of range.
0
0
[0, 0]
Am I at least a bit on the right track?
Edit: Another problem is that I probably need long values instead of byte to count numIntersections in the computeShader, because there are at least some billions of them....