Shader issue - messed up Shadow when sampling uv from screenposition

Hi there
Context:
for testing to render 3D elements into a 2D environment I have 2 cameras placed in the exact same position.
One is rendering into a RenderTexture and the other will display a Quad reading from it.
To make it fit the same position I use the ScreenPosition as my uv for my texture.

Issue:
Everything works fine Except for Shadows.
I tried it in shadergraph and using shader hlsl but with no clue on what is going on in the shadow caster pass.

Test and repro steps:
I did a checker texture with every tile marked to be “human readable” and see how much the uvs are incorrect in the shadows.
The uber simple shader graph:


The check Texture with marked tiles:

Scrambled shadow uvs with shadergraph:

changing position to see that the screenspace uvs are correct for other passes:

Other test using hlsl
For my hlsl version I tried in the fragment shader a simple
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
but it leads to even more scrambled results (in fact uv even changes if I hover the hierarchy randomly)

Uvs with HLSL are also scrambled:

The unlit pass is however correct with the same computation:

Any help on what could be going on would be much appreciated.

If I understand correctly, this is due to the view and projection matrix that are set differently for the shadowcaster pass to be Light oriented instead of Camera oriented.
But, if I am correct, is there a way to acces the camera view and projection Matrix during the shadow caster pass? if not I guess that what I am trying to do cannot be done?

So just in case anyone is interested, to fix that don’t use the Transform functions in your shaderGraph as it will be incorrect in the ShadowCastingPass.
Insteaed you can use the matrices that always refers to the camera:


float4 positionWS= float4(TransformObjectToWorld(positionOS.xyz),1.0); //This one is always correct as it doesn't change through passes
float4 positionVS= mul(unity_WorldToCamera,positionWS); //we use this camera related matrix instead of TransformWorldToView to be the same in all passes
float4 positionCS= mul(unity_CameraProjection, positionVS); // from view space to projection space, you can do this in one line to do as TransformWorldToHClip: positionCS= mul(unity_CameraProjection,mul(unity_WorldToCamera,positionWS));