Unity clip space in dx11

Hello,

I discovered something weird: when I draw something in clip-space, that is with identity modelview and projection matrices. In DX11 mode, my geometry is drawn when it’s within the [-1;1] range, not the [0;1] range I would expect from Direct3D. Why is this?

I am writing a native plugin and I need clip space values between [0;1] for interop with my Direct3D code.

Thanks for helping,
Jeroen

Clip space has always been in the range -1 to 1 in DirectX. Or at least since DirectX 9.

I’m pretty sure it isn’t, as in my native d3d11 app it only renders within the 0 to 1 range.

DirectX is -1 to 1 on the x and y, 0 to 1 on the z, so you’re both right?

I should have made it clearer that I was talking about the depth direction.

I don’t have any explanation for why you would be seeing stuff in -1 to 1. Unity uses a reversed Z with DirectX which will flip the order of things, but that still shouldn’t cause stuff outside of the 0 to 1 range to show. Are you sure you’re seeing stuff through the whole -1 to 1 range, or is it just -1 to 0?

Ok, I’ve done some more testing. It appears that when I draw in my native plugin, the geometry is correctly clipped between 0 to 1. When I draw with the GL functions in OnRenderObject, however they are clipped to be within -1 to 1. I have double checked this.

So it appears drawing within Unity itself adheres to the Opengl convention. But how can they achieve this when everything is drawn in DirectX? Is the clip volume adjustable?

I would assume they’re keeping to OpenGL conventions with the GL functions and translating them to the current platform behind the scenes. Unity was original Mac only, so I many of the legacy underpinnings are OpenGL based. i.e.: the matrix you pass to the GL function is not the one used to render on the GPU.

Ah, I though it was -1 to 1 in all three directions actually. But that might indeed be because of a behind the scenes conversion.

Basically, the default for Z is 0.0 to 1.0, but it’s configurable. OpenGL is -1 to 1 in the Z by default, though there are extensions that give you the same control as DirectX.