PacktPublishing / Mastering-Graphics-Programming-with-Vulkan

MIT License
514 stars 65 forks source link

Question about frustum aligned 3D texture #51

Closed omd24 closed 5 months ago

omd24 commented 5 months ago

Not sure if this is the right place to ask. In chapter 10, a 3D texture is introduced which is frustum aligned. I don't understand this mapping between the texture and the camera frustum. The book briefly mentions

This mapping is already happening in the different stages of rendering, for example, when multiplying a vertex position for the view-projection matrix, so it is not something new.

But looking at the shader code, the function world_from_froxel doesn't take screen size or resolution into account, so, how do we ensure that the froxels cover the whole screen (assuming the Z direction is just hard coded to have 128 slices)?

omd24 commented 5 months ago

I'm not sure if this is accurate, but I can think of it this way: We create a 128x128x128 texture, where 128x128 represents our screen resolution (i.e., we are using low resolution). When discussing the mapping of corresponding threads for those 128x128 pixels (disregarding the 128 slices in the Z direction), each thread ID is mapped to a UV coordinate between 0 and 1, akin to a screen coordinate. From there, it makes sense to transition from screen-space to world space using an inverse view projection transform. Is this correct, or am I misunderstanding something?

theWatchmen commented 5 months ago

Thanks for your question! Your intuition is correct. The screen resolution doesn't matter in relation to the 3D texture dimensions. There is one dimension we do need to know, and that's the size of the 3D texture. If you look at uv_from_froxels it uses the width and height of the texture to go from thread ID to [0-1]. That gives us the UV coordinate for the texture, while z is simply done as thread ID.z / number of slices in the 3D texture. There are some additional tweaks to the depth, but then we use it the depth value to compute the world position using the inverse view projection. You have probably already found it in the code, all of this happens in world_from_froxel.

Hope this helps, let us know if you have any further questions!

omd24 commented 5 months ago

Nice. Thank you for the clarification.