Source

Thread

1/ @SebAaltonen

This is how you generate a cube in vertex shader. Works with any amount of cubes. Also includes the backface culling trick (only include the positive cube faces to index buffer).

Backface culling trick relies on the fact that only 3 faces of a cube can be visible at once.

2/ @SebAaltonen

Measured performance advantage of the backface culling trick. On Intel Gen 11 GPU. 1 million untextured cubes.

6 face cube = 16.1 ms
3 face cube (mirror faces at runtime) = 10.5 ms

53% performance increase!

3/ @SebAaltonen

If you use the mirroring trick, you need to disable the backface culling from the rasterizer state. Mirroring also flips the faces (except when mirrored even times as that brings the determinant back to positive).

4/ @SebAaltonen

Shader Playground link to my vertex shader:

http://shader-playground.timjones.io/efcd86ce6a954d91ac0a4355f38bb7d4

5/ @SebAaltonen

The cost of the backface mirroring:
9 full rate instructions in the box vertex shader.

I think I can optimize this further if needed. Let’s try…

6/ @SebAaltonen

If you are scared that the compiler can’t get rid of the branches. Try this. On AMD compiler it results in 1 full rate ALU more than the trivial implementation. On Intel Gen 11 the frame time is identical with 1M cubes. So yeah.

7/ @SebAaltonen

For completeness: This is the index buffer I use. Repeat the same 3 or 6 faces for max cubes supported (add 8 for each iteration).

Full index buffer generation code also included…


8/ @SebAaltonen

ARM Mali compiler results: 11.5 ALU cycles. 12 L/S cycles. Thus ALU is free. Without backface culling trick, it’s 10.5 ALU cycles. Insignificant difference.

http://shader-playground.timjones.io/2b3fd2d6a2e6f6bd1100283fbe956677

9/ @SebAaltonen

Image of the cube is here. The Y and Z arrows should actually point to the other direction. Oops :)

https://x.com/SebAaltonen/status/1315982782439591938?s=20

10/ @SebAaltonen

If you are using this cube rendering trick to render actual concrete cube faces (with normals), you need to use SV_IsFrontFace input in pixel shader to detect whether a backface is rendered and flip the normal.

Branches: 2020-12-07-Wunkolo-i-ended-up-going-with-an-approach-where-i-read