[英]Multiple datatypes within a vertex buffer (openGL)
I'm studying openGL and am attempting to write a basic program.我正在研究 openGL 并试图编写一个基本程序。 I either misunderstand something, or simply cannot find the answers I'm looking for.
我要么误解了某些东西,要么根本找不到我正在寻找的答案。
I am trying to add data (of multiple data types) to a vertex buffer.我正在尝试将数据(多种数据类型)添加到顶点缓冲区。 I don't know if this is bad practice or not.
我不知道这是否是不好的做法。 But I figured it would save a lot of space using int for some of my data instead of floats for everything.
但我认为它会为我的一些数据使用 int 而不是为所有数据使用 float 来节省大量空间。
Is it possible to add multiple data types into a single vertex buffer?是否可以将多种数据类型添加到单个顶点缓冲区中? If so, how?
如果是这样,怎么做? Is there a best practice with this?
这有最佳实践吗?
I know how to define each attribute with glVertexAttribPoint
but then how did I add the data with glBufferSubData
?我知道如何使用
glVertexAttribPoint
定义每个属性,但是我如何使用glBufferSubData
添加数据? Right now, my problem is that glBufferSubData
requires an array and arrays can only have 1 datatype, I believe.现在,我的问题是
glBufferSubData
需要一个数组,我相信 arrays 只能有 1 个数据类型。
Sidestepping your actual question:回避你的实际问题:
But I figured it would save a lot of space using int for some of my data instead of floats for everything.
但我认为它会为我的一些数据使用 int 而不是为所有数据使用 float 来节省大量空间。
No, you don't save anything.不,你不保存任何东西。
sizeof(int) == sizeof(float)
on all platforms you're going to be concerned about. sizeof(int) == sizeof(float)
在您将要关注的所有平台上。
Anyway, you can mix multiple datatypes perfectly fine inside a buffer object, the key here is the stride parameter for glVertexAttribPointer.无论如何,您可以在缓冲区 object 内完美混合多种数据类型,这里的关键是 glVertexAttribPointer 的步幅参数。 This is usually called a "array of structs".
这通常称为“结构数组”。 The following (C) is perfectly fine:
以下(C)非常好:
struct foo {
GLfloat a[2];
GLbyte b[3];
};
enum { N = 10 };
foo arr[N];
glBufferData(GL_ARRAY_BUFFER, N*sizeof(foo), arr, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(foo), (void*)offsetof(foo, a));
glVertexAttribPointer(0, 3, GL_BYTE, GL_FALSE, sizeof(foo), (void*)offsetof(foo, b));
Now here's something important to know about "arrays of structs": They are usually less efficient than "structs of arrays" for two reasons:现在有一些关于“结构数组”的重要知识:它们通常比“数组结构”效率低,原因有两个:
Elements of a structure must be self-aligned;结构的元素必须是自对齐的; depending on what's inside a structure that requirement may mandate the use of padding bytes, ie use more space
根据结构内部的内容,该要求可能要求使用填充字节,即使用更多空间
If only a subset of elements in a structure are required, due to the way memory fetches work on GPUs, a lot of the data being loaded is thrown away.如果只需要结构中的元素子集,由于 memory 在 GPU 上获取工作的方式,很多正在加载的数据都会被丢弃。 On GPUs usually streches of consecutive memory are fetched, which usually caches very well due to the high locality of most graphics operations.
在 GPU 上,通常会获取连续的 memory 片段,由于大多数图形操作的高局部性,这些片段通常可以很好地缓存。
In this appnote by Nvidia they're calling it "streams" instead of "arrays.在 Nvidia 的这个 appnote 中,他们将其称为“流”而不是“数组”。
https://developer.nvidia.com/gpugems/gpugems2/part-iv-general-purpose-computation-gpus-primer/chapter-33-implementing-efficient https://developer.nvidia.com/gpugems/gpugems2/part-iv-general-purpose-computation-gpus-primer/chapter-33-implementing-efficient
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.