TemporalBlendCS.hlsl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: threadId
  3. // CHECK: textureLoad
  4. // CHECK: sampleLevel
  5. // CHECK: textureLoad
  6. // CHECK: FMin
  7. // CHECK: textureStore
  8. //
  9. // Copyright (c) Microsoft. All rights reserved.
  10. // This code is licensed under the MIT License (MIT).
  11. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  12. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  13. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  14. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  15. //
  16. // Developed by Minigraph
  17. //
  18. // Author: James Stanard
  19. //
  20. #include "ShaderUtility.hlsli"
  21. #include "MotionBlurRS.hlsli"
  22. #define USE_LINEAR_Z
  23. Texture2D<float3> SrcColor : register(t0);
  24. Texture2D<float2> ReprojectionBuffer : register(t1);
  25. Texture2D<float4> TemporalIn : register(t2);
  26. RWTexture2D<float3> DstColor : register(u0); // final output color (blurred and temporally blended)
  27. RWTexture2D<float4> TemporalOut : register(u1); // color to save for next frame including its validity in alpha
  28. SamplerState LinearSampler : register(s0);
  29. cbuffer ConstantBuffer_x : register(b0)
  30. {
  31. float2 RcpBufferDim; // 1 / width, 1 / height
  32. float TemporalBlendFactor;
  33. }
  34. struct MRT
  35. {
  36. float3 BlendedColor : SV_Target0;
  37. float4 TemporalOut : SV_Target1;
  38. };
  39. [RootSignature(MotionBlur_RootSig)]
  40. [numthreads( 8, 8, 1 )]
  41. void main( uint3 Gid : SV_GroupID, uint GI : SV_GroupIndex, uint3 GTid : SV_GroupThreadID, uint3 DTid : SV_DispatchThreadID )
  42. {
  43. uint2 st = DTid.xy;
  44. float2 position = st + 0.5;
  45. float2 curUV = position * RcpBufferDim;
  46. float2 velocity = ReprojectionBuffer[st];
  47. float2 preUV = (position + velocity) * RcpBufferDim;
  48. #if 1
  49. float4 lastColor = TemporalIn.SampleLevel( LinearSampler, preUV, 0 );
  50. #else
  51. float4 lastColor = 0.25 * (
  52. TemporalIn.SampleLevel(LinearSampler, preUV + float2(+0.5, +0.5) * RcpBufferDim, 0) +
  53. TemporalIn.SampleLevel(LinearSampler, preUV + float2(+0.5, -0.5) * RcpBufferDim, 0) +
  54. TemporalIn.SampleLevel(LinearSampler, preUV + float2(-0.5, +0.5) * RcpBufferDim, 0) +
  55. TemporalIn.SampleLevel(LinearSampler, preUV + float2(-0.5, -0.5) * RcpBufferDim, 0));
  56. #endif
  57. float3 thisColor = SrcColor[st];
  58. float thisValidity = 1.0;
  59. #if 1
  60. // 2x super sampling with no feedback
  61. float3 displayColor = lerp( thisColor, lastColor.rgb, 0.5 * min(thisValidity, lastColor.a) );
  62. float4 savedColor = float4( thisColor, thisValidity );
  63. #else
  64. // 4x super sampling via controlled feedback
  65. float3 displayColor = lerp( thisColor, lastColor.rgb, TemporalBlendFactor * min(thisValidity, lastColor.a) );
  66. float4 savedColor = float4( displayColor, thisValidity );
  67. #endif
  68. DstColor[st] = displayColor;
  69. TemporalOut[st] = savedColor;
  70. }