PPBokehDOFCombine.bsl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "$ENGINE$\DepthOfFieldCommon.bslinc"
  2. #if MSAA_MODE != 0
  3. #define MSAA_COUNT 2
  4. #endif
  5. #include "$ENGINE$\DepthInput.bslinc"
  6. #include "$ENGINE$\PPBase.bslinc"
  7. #include "$ENGINE$\PerCameraData.bslinc"
  8. shader PPBokehDOF
  9. {
  10. mixin DepthOfFieldCommon;
  11. mixin PPBase;
  12. mixin DepthInput;
  13. mixin PerCameraData;
  14. variations
  15. {
  16. // 0 - None
  17. // 1 - Resolve single sample only
  18. // 2 - Resolve all samples
  19. MSAA_MODE = { 0, 1, 2 };
  20. };
  21. code
  22. {
  23. cbuffer Params
  24. {
  25. float2 gLayerUVScaleOffset;
  26. float2 gFocusedImageSize;
  27. };
  28. Texture2D gUnfocusedTex;
  29. SamplerState gUnfocusedSampler
  30. {
  31. AddressU = CLAMP;
  32. AddressV = CLAMP;
  33. AddressW = CLAMP;
  34. };
  35. #if MSAA_MODE == 0
  36. Texture2D gFocusedTex;
  37. SamplerState gFocusedSampler;
  38. #else
  39. Texture2DMS<float4> gFocusedTex;
  40. #endif
  41. float4 fsmain(VStoFS input
  42. #if MSAA_MODE == 2
  43. ,uint sampleIdx : SV_SampleIndex
  44. #endif
  45. ) : SV_Target0
  46. {
  47. float2 uv = input.uv0;
  48. float2 screenPos = uv * gFocusedImageSize;
  49. #if MSAA_MODE == 0
  50. float deviceZ = gDepthBufferTex.SampleLevel(gDepthBufferSamp, uv, 0).r;
  51. float3 focusedColor = gFocusedTex.Sample(gFocusedSampler, uv).rgb;
  52. #elif MSAA_MODE == 1
  53. float deviceZ = gDepthBufferTex.Load(screenPos, 0).r;
  54. float3 focusedColor = gFocusedTex.Load(screenPos, 0).rgb;
  55. #else
  56. float deviceZ = gDepthBufferTex.Load(screenPos, sampleIdx).r;
  57. float3 focusedColor = gFocusedTex.Load(screenPos, sampleIdx).rgb;
  58. #endif
  59. float depth = -convertFromDeviceZ(deviceZ);
  60. float2 halfUV = uv;
  61. halfUV.y = halfUV.y * gLayerUVScaleOffset.x;
  62. float4 blurredFocusedAndFar = gUnfocusedTex.Sample(gUnfocusedSampler, halfUV);
  63. float4 blurredNear = gUnfocusedTex.Sample(gUnfocusedSampler, halfUV + float2(0, gLayerUVScaleOffset.y));
  64. float2 layer = computeLayerContributions(depth);
  65. float farMask = layer.g;
  66. float focusedMask = 1.0f - saturate(circleOfConfusionPhysical(depth));
  67. float nearMask = layer.r;
  68. float4 combined = 0;
  69. // Blend half-res far layer
  70. combined.rgb = lerp(focusedColor, blurredFocusedAndFar.rgb / max(blurredFocusedAndFar.a, 0.001f), farMask);
  71. // Blend full-res in-focus area
  72. combined = lerp(combined, float4(focusedColor, 1), focusedMask);
  73. // Blend half-res near layer
  74. combined = lerp(combined, float4(blurredNear.rgb / max(blurredNear.a, 0.001f), 0), nearMask);
  75. float weight = combined.a;
  76. return float4(lerp(combined.rgb, focusedColor.rgb, weight), 1.0f);
  77. }
  78. };
  79. };