[compute shader] efficient way to fetch values from generic buffer object

hi all,

in OpenCL, big non fixed size generic arrays of data are commonly passed to the global GPU memory as cl_mem objects from application side (unformatted byte buffers within global memory scope) which are seen in the OpenCL kernels (equivalent to shader programs in GLSL) as traditional C pointers: typeName* varName Thus, accessing a value from theses buffers is simply done by using the operator []: typeName a = varName[i]. How about GLSL, specially for compute shaders?

In my experience with vertex shaders, I pass generic buffers as texture buffers like:

glGenBuffers(1, &tex_Buffer);
glActiveTexture(GL_TEXTUREi);//with i from 0 to max
glBindTexture(GL_TEXTURE_BUFFER, tex_Buffer);
glBindBuffer(GL_TEXTURE_BUFFER, tex_Buffer);
glBufferData(GL_TEXTURE_BUFFER, data_size_in_bytes, data_array, GL_STATIC_DRAW);
glTexBuffer(GL_TEXTURE_BUFFER, buffer_data_type, tex_Buffer);// be buffer_data_type GL_RGB32F, GL_R8UI or whatever.

and in the shader program I declare uniform (i/u)samplerBuffer varName and fetch values from the buffer like texelFetch(varName, i). Is that the best way to do it for generic type non fixed size and random read/write access to global scope data buffers?

By the way, what about 64-bits integer and double arrays, what to do, what kind of buffer to use? (I understand that high floating precision is not a primary goal with computer shader)

I’ve read about buffers, instead of samplerBuffer, but they seem to be intended to other usages (very fast access but they are more limited in size and not specially designed for random access).

thx for your time,

lao

By the way, what about 64-bits integer and double arrays, what to do, what kind of buffer to use? (I understand that high floating precision is not a primary goal with computer shader)

64b integers aren’t supported in GLSL, but you could emulate 64b ints with 32b int math, and store the result in 2 32b ints (either a ivec2, or consecutive locations in a buffer or texture buffer object).

It seems for doubles you’d need to use buffer interface blocks, as presented in GL_ARB_shader_storage_buffer_object. GL_ARB_vertex_shader_fp64 defines how doubles fit into uniform blocks (though rather tersely), and you can randomly access the buffer with array indices.

Otherwise, for 32b FP and integer buffers, texture buffers would suffice for many cases.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.