NVIDIAGameWorks / NRI

Low-level abstract render interface
MIT License
219 stars 25 forks source link

viewport and clipping rectangle #100

Closed pollend closed 4 weeks ago

pollend commented 1 month ago

I've been debugging a problem where the viewport region doesn't seem correct, but the scissor rectangle works doesn't work how I expect it to work. so passing in a negative value for y seems to place the viewport rectangle in the correct place I was expecting. so y would move the viewport above the screen through my testing. I was wondering if this is just me or an inconsistency with the vulkan implementation or is this expected behavior.

image image

dzhdanNV commented 1 month ago

Many years ago (probably) there was a discussion about it. If I remember correctly, the idea was to make VK (it's VK, right?) closer to D3D. D3D window origin is upper left, VK window origin is lower left. I don't remember and can't verify right now, but we make VK origin upper left to avoid divergence. What's your expectation here, what's origin do you expect? Can the problem be reproduced in an NRI sample? (will get back to my PC on Monday)

pollend commented 1 month ago

I'm using a negative y value for the origin. Top down is negative and left to right is positive., but the clipping rectangle is as expected. Doesn't look like it's expected behaviour.

dzhdanNV commented 1 month ago

Several comments:

dzhdanNV commented 1 month ago

Yes, I will add something like bool viewportOriginBottomLeft to the device creation / wrapping desc (false is an implicit default, matching the current behavior). If it's set to true VK and D3D12 will switch to the bottom-left origin, while D3D11 will have to report an error (there is no an easy way to change the viewport origin).

Viewport inversion in D3D12 has been added in a relatively old Agility SDK (OPTIONS9) - https://microsoft.github.io/DirectX-Specs/d3d/VulkanOn12.html#inverted-viewports.

dzhdanNV commented 1 month ago

OK, I have already tested the following changes:

pollend commented 1 month ago

Seems good to me. Thanks for the quick response. umm, i don't think you need to do this change bool viewportOriginBottomLeft. Is there any reason to do this beyond just legacy behavior? I don't really need to move the viewport origin to the bottom left not sure if this is going to be a common use case at all. at worse people can easily flip the origin in the logic.

dzhdanNV commented 1 month ago

In v1.153:

Please, check on your side.

dzhdanNV commented 1 month ago

i don't think you need to do this change bool viewportOriginBottomLeft

For your case you only need to update to the latest. The flipping bug has been fixed in the VK implementation. When you passed -y you compensated wrong behavior, so:

(OLD) vkViewport.y = viewport.height - (-viewport.y) == (FIXED) vkViewport.y = viewport.height + viewport.y

I just went further, what if someone uses NRI in an already written app with the old-school OGL-like viewport origin (it affects shaders and potentially even matrices), now NRI should support this too in both VK and D3D12.