Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MotionVectorsCommon.hlsl 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef UNIVERSAL_PIPELINE_MOTIONVECTORSCOMMON_INCLUDED
  2. #define UNIVERSAL_PIPELINE_MOTIONVECTORSCOMMON_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRendering.hlsl"
  4. // This is required to avoid artifacts ("gaps" in the _MotionVectorTexture) on some platform
  5. void ApplyMotionVectorZBias(inout float4 positionCS)
  6. {
  7. #if defined(UNITY_REVERSED_Z)
  8. positionCS.z -= unity_MotionVectorsParams.z * positionCS.w;
  9. #else
  10. positionCS.z += unity_MotionVectorsParams.z * positionCS.w;
  11. #endif
  12. }
  13. float2 CalcNdcMotionVectorFromCsPositions(float4 posCS, float4 prevPosCS)
  14. {
  15. // Note: unity_MotionVectorsParams.y is 0 is forceNoMotion is enabled
  16. bool forceNoMotion = unity_MotionVectorsParams.y == 0.0;
  17. if (forceNoMotion)
  18. return float2(0.0, 0.0);
  19. // Non-uniform raster needs to keep the posNDC values in float to avoid additional conversions
  20. // since uv remap functions use floats
  21. float2 posNDC = posCS.xy * rcp(posCS.w);
  22. float2 prevPosNDC = prevPosCS.xy * rcp(prevPosCS.w);
  23. float2 velocity;
  24. #if defined(SUPPORTS_FOVEATED_RENDERING_NON_UNIFORM_RASTER)
  25. UNITY_BRANCH if (_FOVEATED_RENDERING_NON_UNIFORM_RASTER)
  26. {
  27. // Convert velocity from NDC space (-1..1) to screen UV 0..1 space since FoveatedRendering remap needs that range.
  28. float2 posUV = RemapFoveatedRenderingResolve(posNDC * 0.5 + 0.5);
  29. float2 prevPosUV = RemapFoveatedRenderingPrevFrameLinearToNonUniform(prevPosNDC * 0.5 + 0.5);
  30. // Calculate forward velocity
  31. velocity = (posUV - prevPosUV);
  32. #if UNITY_UV_STARTS_AT_TOP
  33. velocity.y = -velocity.y;
  34. #endif
  35. }
  36. else
  37. #endif
  38. {
  39. // Calculate forward velocity
  40. velocity = (posNDC.xy - prevPosNDC.xy);
  41. #if UNITY_UV_STARTS_AT_TOP
  42. velocity.y = -velocity.y;
  43. #endif
  44. // Convert velocity from NDC space (-1..1) to UV 0..1 space
  45. // Note: It doesn't mean we don't have negative values, we store negative or positive offset in UV space.
  46. // Note: ((posNDC * 0.5 + 0.5) - (prevPosNDC * 0.5 + 0.5)) = (velocity * 0.5)
  47. velocity.xy *= 0.5;
  48. }
  49. return velocity;
  50. }
  51. // Same as CalcNdcMotionVectorFromCsPositions but returns vec3 ndc space motion vector.
  52. // Also Application SpaceWarp does not support non-uniform foveated rendering so the relevant foveated rendering code is not in this variant.
  53. float3 CalcAswNdcMotionVectorFromCsPositions(float4 posCS, float4 prevPosCS)
  54. {
  55. float3 posNDC = posCS.xyz * rcp(posCS.w);
  56. float3 prevPosNDC = prevPosCS.xyz * rcp(prevPosCS.w);
  57. float3 velocity;
  58. // Calculate forward velocity
  59. velocity = (posNDC.xyz - prevPosNDC.xyz);
  60. return velocity;
  61. }
  62. #endif