PPSSAODownsample.bsl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "$ENGINE$\PPBase.bslinc"
  2. #include "$ENGINE$\PerCameraData.bslinc"
  3. technique PPSSAODownsample
  4. {
  5. mixin PPBase;
  6. mixin PerCameraData;
  7. code
  8. {
  9. [internal]
  10. cbuffer Input
  11. {
  12. float2 gPixelSize;
  13. float gInvDepthThreshold;
  14. }
  15. SamplerState gInputSamp;
  16. Texture2D gDepthTex;
  17. Texture2D gNormalsTex;
  18. float4 fsmain(VStoFS input) : SV_Target0
  19. {
  20. float2 uvs[4];
  21. uvs[0] = input.uv0 + float2(-0.5f, -0.5f) * gPixelSize;
  22. uvs[1] = input.uv0 + float2(-0.5f, 0.5f) * gPixelSize;
  23. uvs[2] = input.uv0 + float2( 0.5f, -0.5f) * gPixelSize;
  24. uvs[3] = input.uv0 + float2( 0.5f, 0.5f) * gPixelSize;
  25. float4 samples[4];
  26. [unroll]
  27. for(int i = 0; i < 4; i++)
  28. {
  29. samples[i].xyz = gNormalsTex.Sample(gInputSamp, uvs[i]).xyz;
  30. samples[i].w = convertFromDeviceZ(gDepthTex.Sample(gInputSamp, uvs[i]).r);
  31. }
  32. float maxZ = max(max(samples[0].w, samples[1].w), max(samples[2].w, samples[3].w));
  33. float3 weightedSum = 0.0f;
  34. float weightSum = 0.00001f; // Avoid division by 0
  35. [unroll]
  36. for(int i = 0; i < 4; i++)
  37. {
  38. float weight = saturate(1.0f - abs(samples[i].w - maxZ) * gInvDepthThreshold);
  39. weightedSum += samples[i].xyz * weight;
  40. weightSum += weight;
  41. }
  42. return float4(weightedSum / weightSum, maxZ);
  43. }
  44. };
  45. };