PPTonemapping.bsl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "$ENGINE$\PPTonemapCommon.bslinc"
  2. technique PPTonemapping
  3. {
  4. mixin PPTonemapCommon;
  5. depth
  6. {
  7. read = false;
  8. write = false;
  9. };
  10. code
  11. {
  12. struct VStoFS
  13. {
  14. float4 position : SV_POSITION;
  15. float2 uv0 : TEXCOORD0;
  16. float exposureScale : TEXCOORD1;
  17. };
  18. struct VertexInput
  19. {
  20. float2 screenPos : POSITION;
  21. float2 uv0 : TEXCOORD0;
  22. };
  23. Texture2D gEyeAdaptationTex;
  24. VStoFS vsmain(VertexInput input)
  25. {
  26. VStoFS output;
  27. output.position = float4(input.screenPos, 0, 1);
  28. output.uv0 = input.uv0;
  29. output.exposureScale = gEyeAdaptationTex.Load(int3(0, 0, 0)).r;
  30. return output;
  31. }
  32. #if MSAA
  33. Texture2DMS<float4> gInputTex;
  34. #else
  35. SamplerState gInputSamp;
  36. Texture2D gInputTex;
  37. #endif
  38. SamplerState gColorLUTSamp;
  39. Texture3D gColorLUT;
  40. cbuffer Input
  41. {
  42. float gRawGamma;
  43. float gManualExposureScale;
  44. uint gNumSamples;
  45. }
  46. float3 ColorLookupTable(float3 linearColor)
  47. {
  48. float3 logColor = LinearToLogColor(linearColor);
  49. float3 UVW = logColor * ((LUT_SIZE - 1) / (float)LUT_SIZE) + (0.5f / LUT_SIZE);
  50. float3 gradedColor = gColorLUT.Sample(gColorLUTSamp, UVW).rgb;
  51. return gradedColor;
  52. }
  53. float3 tonemapSample(float3 samp, float exposureScale)
  54. {
  55. #if AUTO_EXPOSURE
  56. samp = samp * exposureScale;
  57. #else
  58. samp = samp * gManualExposureScale;
  59. #endif
  60. #if GAMMA_ONLY
  61. return pow(samp, gRawGamma);
  62. #else
  63. return ColorLookupTable(samp);
  64. #endif
  65. }
  66. float4 fsmain(VStoFS input) : SV_Target0
  67. {
  68. float4 sceneColor = 0;
  69. #if MSAA
  70. // Note: Ideally I'd want to use the MSAA coverage texture here, so I can only average samples for pixels
  71. // that really need it. But because forward rendering doesn't write to MSAA coverage I can't do it as I
  72. // don't have up-to-date coverage information. It might be good to find a way around this.
  73. for(uint i = 0; i < gNumSamples; ++i)
  74. sceneColor.rgb += tonemapSample(gInputTex.Load(trunc(input.uv0), i).rgb, input.exposureScale);
  75. sceneColor.rgb /= gNumSamples;
  76. #else
  77. sceneColor.rgb = tonemapSample(gInputTex.Sample(gInputSamp, input.uv0).rgb, input.exposureScale);
  78. #endif
  79. // Output luma in gamma-space, for FXAA
  80. // Note: This can be avoided if FXAA is not used
  81. sceneColor.a = dot(sceneColor.rgb, float3(0.299, 0.587, 0.114));
  82. return sceneColor;
  83. }
  84. };
  85. };