user1896853
user1896853

Reputation: 117

How to access values of row_major matrix in shaders using shader storage blocks?

I want to access value of row_major matrix mat2x4 from compute shader using shader storage block, but always getting wrong result. Getting correct result for mat2, mat3 and mat4. my shader as follows. Using glsl430 version.

layout(std140, row_major) buffer BufferMat{

mat2 row_mat2;
mat3 row_mat3;
mat4 row_mat4;
mat2x4 row_mat24;
}

void main()
{
row_mat2=mat2(1.0, 2.0, 3.0, 4.0);
row_mat3=mat3(1.0, 2.0, 3.0,  4.0, 5.0, 6.0,  7.0, 8.0, 9.0);
row_mat4=mat4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,  9.0, 10.0, 11.0, 12.0,  13.0, 14.0, 15.0, 16.0);

row_mat24=mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
}

Using glGetProgramResourceIndex, glShaderStorageBlockBinding, and glGetProgramResourceiv to get buffer block index and bind it. Getting the index and value of BUFFER_VARIABLE as follows.

index=glGetProgramResourceIndex(progId, GL_BUFFER_VARIABLE, "row_mat24");

glGetProgramResourceiv(progId, GL_BUFFER_VARIABLE, index, 3, &prop[1],3* sizeof(GLsizei), &length, params);
fbuffer = (float*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, params[0], 4, GL_MAP_READ_BIT);

'prop' has value GLenum prop[4]={GL_BUFFER_DATA_SIZE, GL_OFFSET, GL_TYPE, GL_MATRIX_STRIDE}; Getting value as:

printf("1 type = mat24 Value =%f %f %f %f", *(fbuffer), *(fbuffer+1), *(fbuffer+2), *(fbuffer+3));

printf("2 type = mat24 Value =%f %f %f %f", *(fbuffer+4), *(fbuffer+5), *(fbuffer+6), *(fbuffer+7));

result :

1 type = mat24 Value =5.000000 6.000000 7.000000 8.000000
2 type = mat24 Value =1.000000 2.000000 -0.000000 0.000000

Upvotes: 0

Views: 394

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

What possible reason do you have for doing sizeof (params [1])? That will always return 4 or 8 on most systems depending on the size the compiler uses for int. sizeof operates on types at compile-time, it does not evaluate expressions at run-time. So as far as sizeof is concerned, sizeof (params [1]) means: "I had an integer pointer, then I used an array subscript to change the expression to an integer ... what is the size of an integer?" You might as well replace that with sizeof (int) because it means the same thing. You are not mapping enough memory to read more than one or two floating-point values as a result.

What is more, since you overwrite the pointer that stored the memory you allocated one line earlier by the mapped address, you are also creating an unrecoverable memory leak.

Upvotes: 1

Related Questions