如何查询SSBO结构的对齐方式/步幅? [英] How do I query the alignment/stride for an SSBO struct?
问题描述
我不确定哪种结构布局最适合我的应用程序:shared
,packed
,std140
,std430
.我并不是要对每种情况进行解释,因为这些信息很容易找到,很难弄清楚每种情况对供应商兼容性/性能的影响.如果shared
是默认值,我怀疑这是一个很好的起点.
I'm not sure which structure layout is most suited for my application: shared
, packed
,std140
, std430
. I'm not asking for an explanation of each, that information is easy to find, it's just hard to figure out the impact each will have on vendor compatibility/performance. If shared
is the default, I'm suspecting that's a good starting point.
据我所知,在使用shared
或packed
时,我必须查询对齐方式/偏移量,因为它是特定于实现的.
From what I can gather, I have to query the alignment/offsets when using shared
or packed
, because it's implementation specific.
用于查询它的API是什么?在绑定计算着色器时,是否有一些参数传递给glGetShaderiv
,让我可以找出对齐方式?
What's the API for for querying it? Is there some parameter I pass to glGetShaderiv
while the compute shader is bound, that lets me figure out the alignments?
推荐答案
使用GL_SHADER_STORAGE_BLOCK nofollow noreferrer> glGetProgramInterface
来获取
着色器存储缓冲区对象和最大名称长度.
缓冲区变量的最大名称长度可以从程序接口GL_BUFFER_VARIABLE
中获取:
Use glGetProgramInterface
with the parameter GL_SHADER_STORAGE_BLOCK
to get the number of the
Shader Storage Buffer Objects and the maximum name length.
The maximum name length of the buffer variables can be get from the program interface GL_BUFFER_VARIABLE
:
GLuint prog_obj; // shader program object
GLint no_of, ssbo_max_len, var_max_len;
glGetProgramInterfaceiv(prog_obj, GL_SHADER_STORAGE_BLOCK, GL_ACTIVE_RESOURCES, &no_of);
glGetProgramInterfaceiv(prog_obj, GL_SHADER_STORAGE_BLOCK, GL_MAX_NAME_LENGTH, &ssbo_max_len);
glGetProgramInterfaceiv(prog_obj, GL_BUFFER_VARIABLE, GL_MAX_NAME_LENGTH, &var_max_len);
可以通过> glGetProgramResourceIndex
:
The name of the SSBO can be get by glGetProgramResourceName
and a resource index by glGetProgramResourceIndex
:
std::vector< GLchar >name( max_len );
for( int i_resource = 0; i_resource < no_of; i_resource++ ) {
// get name of the shader storage block
GLsizei strLength;
glGetProgramResourceName(
prog_obj, GL_SHADER_STORAGE_BLOCK, i_resource, ssbo_max_len, &strLength, name.data());
// get resource index of the shader storage block
GLint resInx = glGetProgramResourceIndex(prog_obj, GL_SHADER_STORAGE_BLOCK, name.data());
// [...]
}
可以通过 glGetProgramResource
来检索着色器存储块的数据. .另请参见程序自省.
从程序接口,GL_SHADER_STORAGE_BLOCK
和着色器存储块资源resInx
获取缓冲区变量的数量及其索引:
Get the number of of buffer variables and its indices from program interface and GL_SHADER_STORAGE_BLOCK
and the shader storage block resource resInx
:
for( int i_resource = 0; i_resource < no_of; i_resource++ ) {
// [...]
GLint resInx = ...
// get number of the buffer variables in the shader storage block
GLenum prop = GL_NUM_ACTIVE_VARIABLES;
GLint num_var;
glGetProgramResourceiv(
prog_obj, GL_SHADER_STORAGE_BLOCK, resInx, 1, &prop,
1, nullptr, &num_var);
// get resource indices of the buffer variables
std::vector<GLint> vars(num_var);
prop = GL_ACTIVE_VARIABLES;
glGetProgramResourceiv(
prog_obj, GL_SHADER_STORAGE_BLOCK, resInx,
1, &prop, (GLsizei)vars.size(), nullptr, vars.data());
// [...]
}
从程序接口GL_BUFFER_VARIABLE
和资源索引vars[]
获取相对于缓冲区的基数及其名称的缓冲区变量的偏移量(以基本机器单位为单位):
Get the offsets of the buffer variables, in basic machine units, relative to the base of buffer and its names from the program interface GL_BUFFER_VARIABLE
and the resource indices vars[]
:
for( int i_resource = 0; i_resource < no_of; i_resource++ ) {
// [...]
std::vector<GLint> offsets(num_var);
std::vector<std::string> var_names(num_var);
for (GLint i = 0; i < num_var; i++) {
// get offset of buffer variable relative to SSBO
GLenum prop = GL_OFFSET;
glGetProgramResourceiv(
prog_obj, GL_BUFFER_VARIABLE, vars[i],
1, &prop, (GLsizei)offsets.size(), nullptr, &offsets[i]);
// get name of buffer variable
std::vector<GLchar>var_name(var_max_len);
GLsizei strLength;
glGetProgramResourceName(
prog_obj, GL_BUFFER_VARIABLE, vars[i],
var_max_len, &strLength, var_name.data());
var_names[i] = var_name.data();
}
// [...]
}
另请参见 ARB_shader_storage_buffer_object
这篇关于如何查询SSBO结构的对齐方式/步幅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!