简体   繁体   中英

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.

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. 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? 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).

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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