gpt4 book ai didi

opengl - 如何用GL_UNIFORM_BLOCK_DATA_SIZE解读glGetActiveUniformBlockiv的含义

转载 作者:行者123 更新时间:2023-12-02 04:54:50 25 4
gpt4 key购买 nike

假设我有以下顶点着色器代码:

#version 330
uniform mat4 ProjectionMatrix, CameraMatrix, SingleModelMatrix;
uniform uint SingleModel;
layout (std140) uniform ModelBlock {
mat4 ModelMatrices[128];
};

void main(void) {
... something that uses ModelMatrices
}

如果在我的程序中,我对表示上述着色器的程序对象发出以下 OpenGL 调用:

getUniformBlockParameter(GL_UNIFORM_BLOCK_DATA_SIZE)

我通过 Intel HD Graphics 4000 卡发现了以下内容:

GLUniformBlock[
name: ModelBlock,
blockSize: 8192,
buffer: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192],
metadata: {
ModelMatrices=GLUniformBlockAttributeMetadata[
name: ModelMatrices,
offset: 0,
arrayStride: 64,
matrixStride: 16,
matrixOrder: 0
]},
uniforms: GLUniformInterface[
uniforms: {
ModelMatrices=GLUFloatMat4 [
name:ModelMatrices,
idx:2,
loc:-1,
size:128,
glType:8b5c]
}
]
]

我应该如何解释 blockSize 参数? OpenGL 3 sdk docs声明:

If pname is GL_UNIFORM_BLOCK_DATA_SIZE, the implementation-dependent minimum total buffer object size, in basic machine units, required to hold all active uniforms in the uniform block identified by uniformBlockIndex is returned. It is neither guaranteed nor expected that a given implementation will arrange uniform values as tightly packed in a buffer object. The exception to this is the std140 uniform block layout, which guarantees specific packing behavior and does not require the application to query for offsets and strides. In this case the minimum size may still be queried, even though it is determined in advance based only on the uniform block declaration.

那么在这种情况下,我应该如何测量 blockSize 中的 8192 数字?在 float 中?在字节中?

如果我计算数字,一个 4x4 矩阵 mat4 uniform 共有 16 个 float 组件,它们适合 64 个 bytes,所以至少我需要大约 16 x 4 x 128 字节来存储 128 个矩阵,这确实是 8192

为什么硬件也要求 64(bytes?)数组步长和 16(bytes?)矩阵步长,但只要求 8192 字节? getUniformBlockParameter(GL_UNIFORM_BLOCK_DATA_SIZE) 不应该为对齐/填充目的请求更多空间吗?

最佳答案

数组跨度是从一个数组元素的开始到下一个元素的开始的字节偏移量。本例中的数组元素是 mat4,大小为 64 字节。所以阵列步幅尽可能小。

矩阵 步幅是矩阵数据的一列/行到下一列/行的字节偏移量。每个 mat4 的列或行大小都是 vec4,这意味着它的大小为 16 字节。因此,矩阵列/行再次紧密排列。

因此 8KiB 是此类存储的预期大小。

也就是说,这完全无关紧要。你根本不应该费心去查询这些东西。通过使用 std140 布局,您已经强制 OpenGL 采用特定布局。其中一个特别要求 mat4 具有 64 字节的数组跨度和 16 字节的矩阵跨度。您不必为此要求实现;标准要求

你只需要询问你使用的是shared还是packed

关于opengl - 如何用GL_UNIFORM_BLOCK_DATA_SIZE解读glGetActiveUniformBlockiv的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com