zeux / niagara

A Vulkan renderer written from scratch on stream
MIT License
1.26k stars 72 forks source link

Frustum culling with symmetry doesn't work for non-identity view matrix. #16

Closed Eichenherz closed 3 years ago

Eichenherz commented 3 years ago

The other components of this : vec4 frustumX = normalizePlane(projectionT[3] + projectionT[0]); // x + w < 0 vec4 frustumY = normalizePlane(projectionT[3] + projectionT[1]); // y + w < 0

cullData.frustum[0] = frustumX.x; cullData.frustum[1] = frustumX.z; cullData.frustum[2] = frustumY.y; cullData.frustum[3] = frustumY.z;

are not always 0 , so discarding them will result into faulty culling. ( something similar : https://drive.google.com/file/d/1lTC3M-KOs6g4Z38tmGTbieJOkYjNhWUO/view?usp=sharing, this is from my engine btw )

But the number of planes can be reduced from 6 to 4 since the near and far planes result in a trivial test.

zeux commented 3 years ago

I believe this still works if you do the culling in view-space - the projection "matrix" here is literally that, not a view-projection one, so if you transform the sphere to view space before running the same culling code it should work.

(of course it doesn't work for asymmetrical projection matrices that you can encounter in VR)

Eichenherz commented 3 years ago

Guess you're right. But I'm not sure there's much benefit doing it this way vs getting the 4 planes. Anyways I though I should open this issue in case someone else encounters this issue. ( Maybe it's time for niagara to get it's own Discord server ? :) ) Thanks for the replay !

zeux commented 3 years ago

Maybe it's time for niagara to get it's own Discord server ? :) )

Thankfully GitHub now has Discussions :) But yeah the frustum culling implementation was chosen for simplicity given the lack of a view transform and to show how the math works out. I don't know if it's worth doing this way in a more complex case or not, although it's worth noting that a view transform is affine, so to transform a bounding sphere it's sufficient to transform the center - as such maybe it's still a good idea.

Eichenherz commented 3 years ago

I know about the discussions, even opened a couple, but still, Discord is more "alive" ":-)
Here's an example of the "compacted" frustum culling: https://github.com/vblanco20-1/vulkan-guide/blob/engine/shaders/indirect_cull.comp#L116