3

Major edit:

I have one shader setting the values of an SSBO:

#version 430

//Input variables
in vec3 f_pos;
in vec3 f_norm;
in vec3 f_uv;

struct Voxel_Node
{
    vec4 color;
    vec4 normal;
    uint children[8];
};
//Buffer for the rest of the tree
layout(std430, binding = 0) buffer tree_buffer
{
    uint t_index;
    uint pad1;
    uint pad2;
    uint pad3;
    Voxel_Node tree[];
};

void main()
{
    tree[0].children[7] = 1;
    tree[1].children[0] = 2;
    tree[2].children[7] = 3;
    tree[3].children[0] = 4;
    tree[4].children[7] = 5;

    tree[0].normal = vec4(0,0,1,1);
    tree[1].normal = vec4(0,1,0,1);
    tree[2].normal = vec4(0,1,1,1);
    tree[3].normal = vec4(1,0,0,1);
    tree[4].normal = vec4(1,0,1,1);
    tree[5].normal = vec4(1,1,0,1);
}

And one shader reading from the SSBO:

#version 430
#pragma optimize (off)

in vec2 f_coord;

out vec4 fragment_color;

struct Voxel_Node
{
    vec4 color;
    vec4 normal;
    uint children[8];
};
//Buffer for the rest of the tree
layout(std430, binding = 0) buffer tree_buffer
{
    uint t_index;
    uint pad1;
    uint pad2;
    uint pad3;
    Voxel_Node tree[];
};


void main()
{
    fragment_color = vec4(tree[1].children[0]);
    int pls = int(tree[1].children[0]);
    //set 1
    fragment_color = vec4(pls);         //white
    //fragment_color = vec4(pls-1);     //white
    //fragment_color = vec4(pls-2);     //black
    //fragment_color = vec4(pls)/2.f;   //grey

    //Set 2
    fragment_color = tree[pls].normal;      //blue
    //fragment_color = tree[pls+2].normal;  //cyan
    //fragment_color = tree[2].normal;      //cyan
}

There are 2 sets of tests. first, comment out the tree[value].normal block.

So that the fragment color depends on the value of pls. Only one of the 2 blocks may be uncomented at a time and only one line within each block. The values on the right are the resulting color of the test.

The first block of tests is successful all of those are the expected outputs. For the second block of tests,the first output should be blue and the secddn output should not be cyan.

According to these tests the value of pls is 2 when read by the first set of tests but 0 when read by the second set.

There is no other logic than trying to read the contents of the SSBO, what could cause this kind of behaviour?

Edit:

I think it may be due to this but I am not sure:

https://www.khronos.org/opengl/wiki/Memory_Model#Incoherent_memory_access

Update:

Setting the SSBO to be of constant size "solved" the issue, however I'd rather be able to use varying size ssbo's.

coherent layout(std430, binding = 0) buffer tree_buffer
{
    uint t_index;
    uint pad1;
    uint pad2;
    uint pad3;
    Voxel_Node tree[8];
};

Modifying the code to the above results in teh expected behaviour

Makogan
  • 8,208
  • 7
  • 44
  • 112
  • Do you have proper memory barriers between the execution of the two? – derhass Nov 11 '18 at 16:30
  • I call ' glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); ' in between the 2 calls, is that enough? – Makogan Nov 11 '18 at 16:32
  • The contents are fine btw, I colored the screen with the values of tree[0].normal; tree[1].normal; ... And the colors matched initialization – Makogan Nov 11 '18 at 16:36
  • Yeah, that should be correct. – derhass Nov 11 '18 at 16:36
  • Have you tried debugging if theres any OpenGL error during the execution?. Also, the color Blue you see, is it your clear color? or it is from tree[0] normal? – Nadir Nov 11 '18 at 17:59
  • My clear color is black, that blue comes from tree[0].normal (changing it in the first shader affects the color in the second). I technically do have some GL errors but 2 of them are uniforms that couldn't be found (they are being optimized out). So that on its own should not break things. The other issue is a notification telling me that one of my shaders is being recompiled, but that on its own should not break things either. – Makogan Nov 11 '18 at 18:05
  • How did you create the SSBO? And is it mapped? – Rhu Mage Nov 16 '18 at 10:55
  • I will add the cpp code when I get home. it is made in the CPU by doing something similar to: glGenBuffers(1, &ssbo); glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo); glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(shader_data), &shader_data, GL_DYNAMIC_COPY); – Makogan Nov 16 '18 at 14:59

0 Answers0