在交错的 openGL 顶点缓冲区对象之间复制

发布于 2024-10-17 15:15:15 字数 891 浏览 2 评论 0原文

使用 opengl 3.3、radeon 3870HD、c++..

我有关于交错数据数组的问题。我在向量中得到了我的应用程序结构,它作为数据发送到缓冲区对象。像这样的东西:

struct data{
  int a;
  int b;
  int c;
};

std::vector<data> datVec;
...
glBufferData(GL_ARRAY_BUFFER, sizeof(data)*datVec.size(), &datVec[0], GL_DYNAMIC_DRAW);

没关系,我经常使用这个东西。但我创建的是交错数组,因此数据如下:

a1,b1,c1,a2,b2,c2,a3,b3,c3

现在我将这个东西发送到 GPU 中进行处理,并通过转换反馈将其读回缓冲区,例如 b 变量。所以它看起来像:

bU1, bU2, bU3

我想将更新的值复制到交错缓冲区中,这可以通过像 glCopyBufferSubData 这样的单个命令来完成吗?这个不合适,因为它只需要偏移量和大小而不是步长(可能类似于 c++ 中的 memcpy)...结果应该如下所示:

a1, bU1, c1, a2, bU2, c2, a3, bU3, c3

如果没有,是否有比我的这 2 个更好的方法?

  1. 映射更新的缓冲区,将值复制到应用程序中的临时存储中,取消映射更新,映射数据缓冲区并遍历它设置新值

  2. 在常量缓冲区和变量缓冲区上设置单独的缓冲区。常量将随着时间的推移保持不变,但使用 glCopyBufferSubData 可以在一次调用中更新变量。

谢谢

using opengl 3.3, radeon 3870HD, c++..

I got question about interleaved arrays of data. I got in my application structure in vector, which is send as data to buffer object. Something like this:

struct data{
  int a;
  int b;
  int c;
};

std::vector<data> datVec;
...
glBufferData(GL_ARRAY_BUFFER, sizeof(data)*datVec.size(), &datVec[0], GL_DYNAMIC_DRAW);

this is ok I use this thing very often. But what I create is interleaved array so data are like:

a1,b1,c1,a2,b2,c2,a3,b3,c3

Now I send this thing down for processing in GPU and with transform feedback I read back into buffer for example b variables. So it looks like:

bU1, bU2, bU3

I'd like to copy updated values into interleaved buffer, can this be done with some single command like glCopyBufferSubData? This one isn't suitable as it only takes offset and size not stride (probably it's something like memcpy in c++)... The result should look like:

a1, bU1, c1, a2, bU2, c2, a3, bU3, c3

If not is there better approach than these 2 mine?

  1. map updated buffer, copy values into temp storage in app, unmap updated, map data buffer and itterating through it set new values

  2. separate buffers on constant buffer and variable buffer. constant will stay same over time but using glCopyBufferSubData the variable one can be updated in single call..

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆消瘦 2024-10-24 15:15:15

glMapBuffer 似乎是您正在做的事情的更好的解决方案。

据我所知,基本思想是将缓冲区映射到您的地址空间,然后使用您自己的更新方法手动更新缓冲区(可能是迭代循环)。

glBindBuffer(GL_ARRAY_BUFFER, buffer_id);
void *buffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);

if (buffer == NULL)
  //Handle Error, usually means lack of virtual memory

for (int i = 1; i < bufferLen; i += stride /* 3, in this case */)
  buffer[i] = newValue;

glUnmapBuffer(GL_ARRAY_BUFFER);

glMapBuffer seems like a better solution for what you are doing.

The basic idea, from what I can tell, is to map the buffer into your address space, and then update the buffer manually using your own update method (iterative loop likely).

glBindBuffer(GL_ARRAY_BUFFER, buffer_id);
void *buffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);

if (buffer == NULL)
  //Handle Error, usually means lack of virtual memory

for (int i = 1; i < bufferLen; i += stride /* 3, in this case */)
  buffer[i] = newValue;

glUnmapBuffer(GL_ARRAY_BUFFER);
二智少女猫性小仙女 2024-10-24 15:15:15

我会将动态部分与静态部分分开(你的观点2)。

如果您仍希望将它们交错保存到单个缓冲区中,并且您有一些空闲视频内存,则可以执行以下操作:

  1. 将原始交错数组复制到备份数组中。这需要所有组件的内存,而不仅仅是动态组件,就像最初那样。
  2. 将反馈转换为原始交错,保持静态值不变。

I would separate the dynamic part with a static one (your point 2).

If you still want to keep them interleaved into a single buffer, and you have some spare video memory, you can do the following:

  1. Copy the original interleaved array into a backup one. This requires memory for all components rather than only dynamic ones, how it was originally.
  2. Transform Feedback into the original interleaved, carrying the static values unchanged.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文