Rendering to RenderTarget inverts value of ddy in pixel shader?

I have made a shader to render the surface normals of objects. After making it work I decided to render it to a RenderTarget so I can store it as a NormalMap, but when I did so the normals inverted. After investigating I found that ddy gives the negative value compared to when I am rendering straight to the backbuffer. Anything I can do to fix this? I can just invert the value in the shader to make it work when using RenderTargets but I’d rather have it work on both the backbuffer and a RenderTarget.

still puzzling what “ddy” is … are you talking about Tangent/Binormal?

I would actually check the CullMode, if that is not what you expect you’re drawing the backfaces and that would switch the outcome of the Normalmap (because Tangent Data is now virtually inverted) - the normal (of the triangle) cannot change - it’s just a unrelated item in the data buffer and does not change with context

Ddy gives the partial derivative of any input relative to screen-space y-coordinate. https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-ddy

In the pixel shader you can use it to obtain the surface normal by taking the cross product of ddx and ddy of the world position. An example can be seen here Flat shading/low poly .

The culling mode is not changed. Just making an effect that is rendering the ddy of the worldposition of a flat surface will show that the value is flipped if it is rendered to a render target compared to just rendered to the backbuffer. Perhaps the coordinate system is flipped internally when using render targets?

That actually would explain something weird thinking back to a odd behavior i had to account for with rendertargetcubes were i had to deal with a apparent handedness flip.