如何有效地用零初始化纹理?
我需要用零初始化 OpenGL 2D 纹理。
为什么?我使用 OpenCL 渲染此纹理。粗略地说,渲染阶段是:
- 创建 RGBA 纹理,用零初始化(因此它包含透明黑色像素)
- OpenCL:计算某些对象可见的像素颜色并将它们写入此纹理
- OpenGL:将环境映射添加到透明像素并显示最终结果 因此,
我不需要将任何实际纹理数据传递给 glTexImage2d
。但根据规范,当我传递 data=NULL 时,纹理内存已分配但未初始化。
分配一个充满零的客户端内存并传递给 glTexImage2D
并不是一个很好的选择,尽管我只能想到一个。
编辑:我实际上只想初始化 alpha 分量(无论如何 RGB 都会被覆盖),但我怀疑这会让情况变得更容易。
I need to initialize an OpenGL 2D texture with zeroes.
Why? I render to this texture with OpenCL. Roughly, rendering stages are:
- Create RGBA texture, initialize with zeros (so it contains transparent black pixels)
- OpenCL: compute color of pixels where some objects are visible and write them to this texture
- OpenGL: add environment mapping to the transparent pixels and display final image
I therefore do not need to pass any actual texture data to glTexImage2d
. But according to the spec, when I pass data=NULL, the texture memory is allocated but not initialized.
Allocating a client-side memory filled with zeroes and passing to glTexImage2D
is not a very good option although only one I can think of.
EDIT: I actually want to init only alpha component (RGB will get overriden anyway), but I doubt this make the situation any easier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有几种方法可以做到这一点:
1:给定纹理的宽度和高度:
为了获得最大效率,您应该在调用它和 OpenCL 开始工作之间留出一些时间。
2:将其附加到 FBO 并使用 glClearBuffer。
这看起来应该更快,因为没有 CPU-GPU DMA 正在进行。但 FBO 的绑定和解除绑定可能会导致效率低下。但话又说回来,也可能不会。对其进行分析以确保。
请注意,这仅适用于可用作渲染目标的图像格式。所以不是压缩纹理。
3:如果有足够的驱动程序,您可以使用新的 ARB_clear_texture 扩展:
注意这仅适用于可以通过标准像素传输提供数据的图像格式。所以不是压缩格式。
There are several ways to do this:
1: Given a width and a height for a texture:
For maximum efficiency, you should leave some time between when you call this and when OpenCL starts its work.
2: Attach it to an FBO and use glClearBuffer.
This seems like it should be faster, since there is no CPU-GPU DMA going on. But the binding and unbinding of FBOs may introduce inefficiencies. Then again, it may not. Profile it to make sure.
Note that this only works for image formats that can be used as render targets. So not compressed textures.
3: Given sufficient drivers, you can use the new ARB_clear_texture extension:
Note that this only works for image formats whose data can be provided by standard pixel transfers. So not compressed formats.
请注意:新的 GL_ARB_clear_texture (4.4 中的核心)扩展专门解决了这个问题。
Just a heads up: The new GL_ARB_clear_texture (core in 4.4) extension specifically addresses this problem.
glTexSubImage
填充整个图像:使用Pixel Buffer从gpu复制数据,这种方法更快更好
我。初始化PBO
二.
glTexSubImage
is filled with the whole image:use Pixel Buffer to copy data from gpu, this method is faster and better
i. init PBO
ii.
也许您可以将纹理设置为第一遍的渲染目标并绘制黑色图元。由于 RAM 和显卡之间没有内存传输,因此速度可能会更快。
Maybe you can set the texture as the rendering target for the first pass and draw a black primitive. It may be faster since there are no memory transfer between the RAM and graphic card.