简体   繁体   English

从 GLSL 顶点着色器中检索数据

[英]Retrieving data from GLSL vertex shader

I have lots of transparent primitives I want to sort before drawing, but to get the Z coordinate, the sorting key, I have perform in software all the transformations performed by the vertex shader in hardware.我有很多透明基元要在绘制之前进行排序,但是为了获得 Z 坐标、排序键,我已经在软件中执行了硬件中顶点着色器执行的所有转换。

I thought I could optimize the process by retrieving the Z coordinate from the shader, and using it to sort the primitives in the next frame.我认为我可以通过从着色器中检索 Z 坐标并使用它对下一帧中的图元进行排序来优化该过程。 Since the order of primitives is not expected to change drastically between frames, it shall provide fair enough ordering.由于原语的顺序预计不会在帧之间发生剧烈变化,因此它应提供足够公平的顺序。 So, I'd like to know how to get batch of Z coordinates back from the vertex shader?那么,我想知道如何从顶点着色器中获取一批 Z 坐标? Is it possible?可能吗?

You can use Transform Feedback to get data from the Vertex Shader output into a Buffer which you can later read, but i don't think this will gain you much, you should profile which solution is best (experimentally).您可以使用变换反馈将数据从顶点着色器 output 获取到缓冲区中,稍后您可以读取该缓冲区,但我认为这不会对您有太大帮助,您应该分析哪种解决方案是最好的(实验)。

Instead of looking at each primitive individually you should sort based on connected meshes' bounding volumes.而不是单独查看每个图元,您应该根据连接的网格的边界体积进行排序。 This kind of sorting comes more or less for free if you organize the geometry in a spatial subdivision structure like Kd or BSP tree.如果您在 Kd 或 BSP 树等空间细分结构中组织几何图形,这种排序或多或少是免费的。 Then the sorting problem is reduced to implementing a traversal of the tree in the right direction -- basically it boils down to a depth first traversal with the branches traversed in a near←→far fashion.然后将排序问题简化为在正确的方向上实现树的遍历——基本上它归结为深度优先遍历,分支以近←→远的方式遍历。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM