Brak opisu
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.

CameraMotionVectors.shader 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. Shader "Hidden/Universal Render Pipeline/CameraMotionVectors"
  2. {
  3. SubShader
  4. {
  5. Pass
  6. {
  7. Name "Camera Motion Vectors"
  8. Cull Off
  9. ZWrite On
  10. HLSLPROGRAM
  11. #pragma target 3.5
  12. #pragma vertex vert
  13. #pragma fragment frag
  14. // -------------------------------------
  15. // Includes
  16. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  17. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  18. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"
  19. #include_with_pragmas "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRenderingKeywords.hlsl"
  20. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRendering.hlsl"
  21. // -------------------------------------
  22. // Structs
  23. struct Attributes
  24. {
  25. uint vertexID : SV_VertexID;
  26. UNITY_VERTEX_INPUT_INSTANCE_ID
  27. };
  28. struct Varyings
  29. {
  30. float4 position : SV_POSITION;
  31. UNITY_VERTEX_OUTPUT_STEREO
  32. };
  33. // -------------------------------------
  34. // Vertex
  35. Varyings vert(Attributes input)
  36. {
  37. Varyings output;
  38. UNITY_SETUP_INSTANCE_ID(input);
  39. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  40. // TODO: Use Core Blitter vert.
  41. output.position = GetFullScreenTriangleVertexPosition(input.vertexID);
  42. return output;
  43. }
  44. // -------------------------------------
  45. // Fragment
  46. half4 frag(Varyings input, out float outDepth : SV_Depth) : SV_Target
  47. {
  48. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  49. float2 uv = input.position.xy / _ScaledScreenParams.xy;
  50. float depth = SampleSceneDepth(uv).x;
  51. outDepth = depth; // Write depth out unmodified
  52. #if !UNITY_REVERSED_Z
  53. depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(uv).x);
  54. #endif
  55. #if defined(SUPPORTS_FOVEATED_RENDERING_NON_UNIFORM_RASTER)
  56. UNITY_BRANCH if (_FOVEATED_RENDERING_NON_UNIFORM_RASTER)
  57. {
  58. // Get the UVs from non-unifrom space to linear space to determine the right world-space position
  59. uv = RemapFoveatedRenderingNonUniformToLinear(uv);
  60. }
  61. #endif
  62. // Reconstruct world position
  63. float3 posWS = ComputeWorldSpacePosition(uv, depth, UNITY_MATRIX_I_VP);
  64. // Multiply with current and previous non-jittered view projection
  65. float4 posCS = mul(_NonJitteredViewProjMatrix, float4(posWS.xyz, 1.0));
  66. float4 prevPosCS = mul(_PrevViewProjMatrix, float4(posWS.xyz, 1.0));
  67. // Non-uniform raster needs to keep the posNDC values in float to avoid additional conversions
  68. // since uv remap functions use floats
  69. float2 posNDC = posCS.xy * rcp(posCS.w);
  70. float2 prevPosNDC = prevPosCS.xy * rcp(prevPosCS.w);
  71. float2 velocity;
  72. #if defined(SUPPORTS_FOVEATED_RENDERING_NON_UNIFORM_RASTER)
  73. UNITY_BRANCH if (_FOVEATED_RENDERING_NON_UNIFORM_RASTER)
  74. {
  75. // Convert velocity from NDC space (-1..1) to screen UV 0..1 space since FoveatedRendering remap needs that range.
  76. // Also return both position in non-uniform UV space to get the right velocity vector
  77. float2 posUV = RemapFoveatedRenderingResolve(posNDC * 0.5f + 0.5f);
  78. float2 prevPosUV = RemapFoveatedRenderingPrevFrameLinearToNonUniform(prevPosNDC * 0.5f + 0.5f);
  79. // Calculate forward velocity
  80. velocity = (posUV - prevPosUV);
  81. #if UNITY_UV_STARTS_AT_TOP
  82. velocity.y = -velocity.y;
  83. #endif
  84. }
  85. else
  86. #endif
  87. {
  88. // Calculate forward velocity
  89. velocity = (posNDC - prevPosNDC);
  90. // TODO: test that velocity.y is correct
  91. #if UNITY_UV_STARTS_AT_TOP
  92. velocity.y = -velocity.y;
  93. #endif
  94. // Convert velocity from NDC space (-1..1) to screen UV 0..1 space
  95. // Note: It doesn't mean we don't have negative values, we store negative or positive offset in the UV space.
  96. // Note: ((posNDC * 0.5 + 0.5) - (prevPosNDC * 0.5 + 0.5)) = (velocity * 0.5)
  97. velocity.xy *= 0.5;
  98. }
  99. return float4(velocity, 0, 0);
  100. }
  101. ENDHLSL
  102. }
  103. }
  104. }