CameraMotionBlurPrePassCS.hlsl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: threadId
  3. // CHECK: Sqrt
  4. // CHECK: FMax
  5. // CHECK: Saturate
  6. // CHECK: Round_ni
  7. //
  8. // Copyright (c) Microsoft. All rights reserved.
  9. // This code is licensed under the MIT License (MIT).
  10. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  11. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  12. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  13. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  14. //
  15. // Developed by Minigraph
  16. //
  17. // Author: James Stanard
  18. //
  19. #include "MotionBlurRS.hlsli"
  20. // We can use the original depth buffer or a linearized one. In this case, we use linear Z because
  21. // we have discarded the 32-bit depth buffer but still retain a 16-bit linear buffer (previously
  22. // used by SSAO.) Note that hyperbolic Z is reversed by default (TBD) for increased precision, so
  23. // its Z=0 maps to the far plane. With linear Z, Z=0 maps to the eye position. Both extend to Z=1.
  24. //#define USE_LINEAR_Z
  25. Texture2D<float3> ColorBuffer : register(t0);
  26. Texture2D<float> DepthBuffer : register(t1);
  27. RWTexture2D<float4> PrepBuffer : register(u0);
  28. RWTexture2D<float2> MotionBuffer : register(u1);
  29. RWTexture2D<float2> ReprojBuffer : register(u2);
  30. cbuffer ConstantBuffer_x : register(b1)
  31. {
  32. matrix CurToPrevXForm;
  33. }
  34. float4 GetSampleData( uint2 st )
  35. {
  36. float2 CurPixel = st + 0.5;
  37. float Depth = DepthBuffer[st];
  38. #ifdef USE_LINEAR_Z
  39. float4 HPos = float4( CurPixel * Depth, 1.0, Depth );
  40. #else
  41. float4 HPos = float4( CurPixel, Depth, 1.0 );
  42. #endif
  43. float4 PrevHPos = mul( CurToPrevXForm, HPos );
  44. float2 MotionVec = PrevHPos.xy / PrevHPos.w - CurPixel;
  45. float speed = length(MotionVec);
  46. ReprojBuffer[st] = MotionVec;
  47. MotionBuffer[st] = MotionVec / max(32, speed);
  48. // Clamp speed at 4 pixels and normalize it.
  49. return float4(ColorBuffer[st], 1.0) * saturate(speed / 4);
  50. }
  51. [RootSignature(MotionBlur_RootSig)]
  52. [numthreads( 8, 8, 1 )]
  53. void main( uint3 Gid : SV_GroupID, uint GI : SV_GroupIndex, uint3 GTid : SV_GroupThreadID, uint3 DTid : SV_DispatchThreadID )
  54. {
  55. uint2 corner = DTid.xy << 1;
  56. float4 sample0 = GetSampleData( corner + uint2(0, 0) );
  57. float4 sample1 = GetSampleData( corner + uint2(1, 0) );
  58. float4 sample2 = GetSampleData( corner + uint2(0, 1) );
  59. float4 sample3 = GetSampleData( corner + uint2(1, 1) );
  60. float combinedMotionWeight = sample0.a + sample1.a + sample2.a + sample3.a;
  61. PrepBuffer[DTid.xy] = floor(0.25 * combinedMotionWeight * 3.0) / 3.0 * float4(
  62. (sample0.rgb + sample1.rgb + sample2.rgb + sample3.rgb) / combinedMotionWeight, 1.0 );
  63. }