I am trying to set up a Shader Storage Buffer Object to create an array of 16-bit data and an array of 8-bit data. I want to pass the 16-bit data to the shader, and then perform some calculations within the shader to stretch the 16-bit data to fit into an 8-bit format and read it out.
However, I’ve found that every time I read the data within the shader, it’s in 32-bit format, which means that in each workgroup, I can only process two 16-bit pieces of data and four 8-bit pieces of data. But this does not allow for a one-to-one mapping because I want to put the 16-bit data at the same index into the 8-bit data.
What should I do to make the one-to-one mapping work properly?
genBuffer:
// some gpu init
uint16_t data[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 511};
glGenBuffers(1, &data_buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, data_buffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(uint16_t) * 10, (void*)data, GL_STREAM_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, data_buffer);
glGenBuffers(1, &output_buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, output_buffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, output_buffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(uint8_t) * 10, nullptr, GL_DYNAMIC_READ);
... // gpu use, dispatch, wait
// print output data
GLuint* ptr = (GLuint*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(uint8_t) * 10, GL_MAP_READ_BIT);
uint8_t* tData = (uint8_t*)ptr;
for (int i = 0; i < 10; i++) {
std::cout << " " << static_cast<unsigned int>(*(tData + i));
}
shader glsl:
#version 320 es
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(std430) buffer;
layout(binding = 2) writeonly buffer Output {
uint elements[];
} output_data;
layout(binding = 1) readonly buffer Input0 {
uint elements[];
} input_data0;
void main()
{
uint ident = gl_GlobalInvocationID.x;
// some processs
//output_data.elements[ident] = input_data0.elements[ident];
// test
output_data.elements[ident] = 4u;
}
i try to run this code, then print
4 0 0 0 4 0 0 0 4 0
What I hope for the output is:
4 4 4 4 4 4 4 4 4 4
lanzr Zen Lanzr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.