GLSL 动态索引数组
我使用 DirectX(带有 XNA)已经有一段时间了,最近改用了 OpenGL。我真的很喜欢它,但有一件事让我很恼火。
我一直在尝试实现一些需要在顶点着色器中动态索引的东西,但我被告知这需要 SM 4.0 的等效项。但我知道即使使用 SM 2.0,甚至可能是 1.0,这也适用于 DX。 XNA 的实例化示例使用它在仅 SM2.0 的卡上进行实例化 http:// /create.msdn.com/en-US/education/catalog/sample/mesh_instancing。
编译器不可能将其“展开”到一个巨大的 if 语句列表中,因为这肯定会超出 SM2 对 250 个实例的指令限制。
那么,DX 是否做了一些我用 OpenGL 无法做到的技巧,我可以操纵 OpenGL 来做同样的事情,还是 OpenGL 没有公开的硬件功能?
I've been using DirectX (with XNA) for a while now, and have recently switched to OpenGL. I'm really loving it, but one thing has got me annoyed.
I've been trying to implement something that requires dynamic indexing in the vertex shader, but I've been told that this requires the equivilant of SM 4.0. However I know that this works in DX even with SM 2.0, possibly even 1.0. XNA's instancing sample uses this to do instancing on SM2.0 only cards http://create.msdn.com/en-US/education/catalog/sample/mesh_instancing.
The compiler can't have been "unrolling" it into a giant list of if statements, since this would surely exceed the instruction limit on SM2 for our 250 instances.
So is DX doing some trickery that I can't do with OpenGL, can I manipulate OpenGL to do the same, or is it a hardware feature that OpenGL doesn't expose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 glUniform3fv 之类的东西上传一个用于您的光线方向的数组,然后(假设我理解您想要正确执行的操作)您只需要顶点格式将索引包含到该数组中(因此这些数组会有很多重复)索引(如果索引每个网格仅更改一次或其他)。如果您还不知道,可以使用 glGetAttribLocation + glVertexAttribPointer 将任意顶点属性发送到着色器(而不是使用已弃用的内置属性,如 gl_Vertex、gl_Normal 等)。
You can upload an array for your light directions with something like glUniform3fv, then (assuming I understand what you're trying to do correctly) you just need your vertex format to include an index into this array (so there be lots of duplication of these indices if the index only changes once per mesh or something). If you don't already know, you can use glGetAttribLocation + glVertexAttribPointer to send arbitrary vertex attributes like this to the shader (as opposed to using the deprecated built-in attributes like gl_Vertex, gl_Normal, etc).
从您的链接:
不是加粗的部分。因此,在此基础上,您应该能够在着色器模型 3 上执行类似的实例化。着色器模型 2 的实例化通常使用矩阵调色板执行。简而言之,您可以通过一次性上传大量变换矩阵来在一次调用中渲染多个网格。这减少了绘制调用并提高了速度。
不管怎样,对于 OpenGL 来说,完成这个扩展有很多麻烦,因此你需要着色器 4。但是,你仍然可以在 yoru 顶点结构中粘贴每个顶点矩阵调色板索引,并使用着色器进行矩阵调色板渲染...
From your link:
Not the emboldened part. So ont hat basis you should be able to do similar instancing on shader model 3. Shader model 2's instancing is usually performed using a matrix palette. It sumply means you can render multiple meshes in one call by uploading a load of transformation matrices in one go. This reduces draw calls and improves speed.
Anyway for OpenGL there was a lot of troubles finalising this extension and hence you need shader 4. You CAN, however, still stick a per vertex matrix palette index in yoru vertex structure and do matrix palette rendering using a shader...