Using a geometric shader for instantiation

So, I want to draw a lot of squares (or even cubes) and came across this beautiful thing called a geometric shader.

I somehow understand how this works now, and I could probably manipulate it in drawing a cube for each vertex in the vertex buffer, but I'm not sure if he did it right. The geometric shader occurs between the vertex shader and the fragment shader, so it works with vertices in the screen space . But I need them to make transformations in the world.

So, is it normal that my vertex shader simply connects the inputs to the geometric shader, and the geometric shader is multiplied by the modelviewproj matrix after creating the primitives? This should not be a problem for a unified shader architecture, but I still feel nauseous when redundant shader shader backups.

Are there any alternatives? Or is this really the β€œright” way to do this?

+4
source share
1 answer

This is normal.

Also, consider using glDrawArraysInstanced rendering ( glDrawArraysInstanced , glDrawElementsInstanced ) with a vertex attribute divider ( glVertexAttribDivisor ). This way you can accomplish the same task without a geometric shader at all.

For example, you can have regular cube geometry. Then you have a special vertex attribute that carries the cube positions that you want for each instance. You must bind it with divisor = 1, which will make it promoted for every instance drawn. Then draw a cube using glDraw*Instanced , specifying the number of instances.

You can also select input from textures using gl_VertexID or gl_InstanceID for coordinates.

+3
source

Source: https://habr.com/ru/post/1339705/


All Articles