I hacked the engine so that I can set and read a shadow map in my material.
The issue is that the screen-to-shadow matrix seems to have a 1 frame delay: this results in the shadow map to be projected incorrectly onto the scene whenever
the camera moves. So as long as the camera is stationary the projection is correct. Movement or rotation causes glithcing.
Any idea why this happens?
I pass the matrix to my UMaterialInstanceDynamic material packed in four Vector4 params.
In C++ I tried setting the matrix (vector params) using game thread code and directly be calling the code that is executed on the render thread when settings parameters.
This code which sets the matrix to the UMaterialInstanceDynamic is executed right after the part where the shadows is projected into for lighting computation,
so all the variables should be valid then, as that is also the time at which I copy the shadow map.
So the only thing that I can see could be wrong is that UMaterialInstanceDynamic are lagging one frame somehow, though I have no idea how to fix it right now.
But it still lags for 1 frame, even though debugger shows that this is executed on the render thread BEFORE the post process material sets these values in FRCPassPostProcessMaterial::Process.
I was using a dynamic POST PROCESS material: what Unreal does is that it copies post process dynamic materials (something to do with blending): this copying happens on the game thread.
So the solution was to loop through all copied post process materials in the pool (mid pool) and found the one that was a copy of my “real” material and also set the parameters on it.