PostFX_LookupTable.fx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2005 Electronic Arts Inc
  3. //
  4. // Image post processing effect performing a color conversion through a lookup table
  5. //////////////////////////////////////////////////////////////////////////////
  6. #include "Common.fxh"
  7. #include "CommonPostFX.fxh"
  8. #include "Gamma.fxh"
  9. SAMPLER_2D_BEGIN( FrameBufferSampler,
  10. string SasBindAddress = "PostEffect.FrameBufferTexture";
  11. )
  12. MinFilter = Linear;
  13. MagFilter = Linear;
  14. MipFilter = Point;
  15. AddressU = Clamp;
  16. AddressV = Clamp;
  17. SAMPLER_2D_END
  18. SAMPLER_3D_BEGIN( LookupTexture,
  19. string SasBindAddress = "PostEffect.LookupTable.LookupTexture";
  20. )
  21. MinFilter = Linear;
  22. MagFilter = Linear;
  23. MipFilter = Point;
  24. AddressU = Clamp;
  25. AddressV = Clamp;
  26. AddressW = Clamp;
  27. SAMPLER_3D_END
  28. SAMPLER_2D_BEGIN( FilmTonemappingLookupTexture,
  29. string SasBindAddress = "PostEffect.LookupTable.FilmTonemapping";
  30. )
  31. MinFilter = Linear;
  32. MagFilter = Linear;
  33. MipFilter = Point;
  34. AddressU = Clamp;
  35. AddressV = Clamp;
  36. SAMPLER_2D_END
  37. float4 BlendFactor
  38. <
  39. string SasBindAddress = "PostEffect.LookupTable.BlendFactor";
  40. > = float4(1.0, 1.0, 1.0, 1.0);
  41. float3 ExposureLevel
  42. <
  43. string SasBindAddress = "PostEffect.LookupTable.ExposureLevel";
  44. > = float3(1.0, 1.0, 1.0);
  45. // ----------------------------------------------------------------------------
  46. struct VSOutput
  47. {
  48. float4 Position : POSITION;
  49. float2 TexCoord : TEXCOORD0;
  50. };
  51. // ----------------------------------------------------------------------------
  52. VSOutput DefaultVS(float3 Position : POSITION, float2 TexCoord : TEXCOORD0)
  53. {
  54. VSOutput Out;
  55. Out.Position = float4(Position, 1);
  56. Out.TexCoord = TexCoord;
  57. return Out;
  58. }
  59. // ----------------------------------------------------------------------------
  60. #if defined(EA_PLATFORM_PS3)
  61. float4 DefaultPS(float2 TexCoord : TEXCOORD0, uniform bool applyLUT, uniform bool applyGammaCorrection)
  62. #else // #if defined(EA_PLATFORM_PS3)
  63. float4 DefaultPS(float2 TexCoord : TEXCOORD0, uniform bool applyLUT, uniform bool applyGammaCorrection) : COLOR
  64. #endif // #if defined(EA_PLATFORM_PS3)
  65. {
  66. float4 color = tex2D(SAMPLER(FrameBufferSampler), TexCoord);
  67. color.xyz = UncompressRenderTargetColor(color.xyz);
  68. color.xyz *= ExposureLevel;
  69. #if defined(ENABLE_GAMMA_CONVERSION)
  70. if (applyGammaCorrection)
  71. {
  72. #define TONEMAPPER 2
  73. #if TONEMAPPER == 0
  74. color.xyz = LinearToGamma(color.xyz);
  75. #elif TONEMAPPER == 1
  76. color.xyz /= 1 + color.xyz;
  77. #elif TONEMAPPER == 2
  78. float3 white = 6.0;
  79. color.xyz *= white;
  80. color.xyz = color.xyz * (1 + color.xyz / (white * white)) / (1 + color.xyz);
  81. #else
  82. float3 ld = 0.002;
  83. float linReference = 0.18;
  84. float logReference = 444;
  85. float logGamma = 0.45;
  86. color.xyz = (log10(0.4 * color.xyz / linReference) / ld * logGamma + logReference) / 1023.0;
  87. //color.xyz = clamp(color.xyz, 0, 1);
  88. float FilmLutWidth = 256;
  89. float Padding = .5/FilmLutWidth;
  90. //color.xyz = lerp(Padding,1-Padding,color.xyz);
  91. // apply response lookup and color grading for target display
  92. color.x = tex2D(SAMPLER(FilmTonemappingLookupTexture), float2(color.x, .5)).r;
  93. color.y = tex2D(SAMPLER(FilmTonemappingLookupTexture), float2(color.y, .5)).r;
  94. color.z = tex2D(SAMPLER(FilmTonemappingLookupTexture), float2(color.z, .5)).r;
  95. #endif
  96. }
  97. #endif
  98. if (applyLUT)
  99. {
  100. #if defined (EA_PLATFORM_XENON)
  101. // TODO: LLatta 2008-09-13
  102. // This is a workaround for a Xenon pipeline bug.
  103. // This fix here is a less risky fix at this stage of the project.
  104. // At the earliest convenience fix the actual issue in CVolumeTexture::SaveImage
  105. // See the comment in code\AssetCompilers\0.0-dev\Modules\TextureCompiler\Source\xenon\VolumeTexture.cpp
  106. float4 tableColor = tex3D(SAMPLER(LookupTexture), color.xzy).xzyw;
  107. #else
  108. float4 tableColor = tex3D(SAMPLER(LookupTexture), color.xyz);
  109. #endif
  110. color = lerp(color, tableColor, BlendFactor.x);
  111. }
  112. return color;
  113. }
  114. #if defined(EA_PLATFORM_PS3)
  115. float4 DefaultPS_Default(float2 TexCoord : TEXCOORD0) : COLOR
  116. {
  117. return DefaultPS(TexCoord, true, true);
  118. }
  119. float4 DefaultPS_Default_M(float2 TexCoord : TEXCOORD0) : COLOR
  120. {
  121. return DefaultPS(TexCoord, true, false);
  122. }
  123. float4 DefaultPS_ResolveOnly(float2 TexCoord : TEXCOORD0) : COLOR
  124. {
  125. return DefaultPS(TexCoord, false, true);
  126. }
  127. float4 DefaultPS_ResolveOnly_M(float2 TexCoord : TEXCOORD0) : COLOR
  128. {
  129. return DefaultPS(TexCoord, false, false);
  130. }
  131. #endif // #if defined(EA_PLATFORM_PS3)
  132. // ----------------------------------------------------------------------------
  133. technique Default
  134. {
  135. pass P0
  136. {
  137. VertexShader = compile VS_2_0 DefaultVS();
  138. #if defined(EA_PLATFORM_PS3)
  139. PixelShader = compile PS_2_0 DefaultPS_Default();
  140. #else // #if defined(EA_PLATFORM_PS3)
  141. PixelShader = compile PS_2_0 DefaultPS(true, true);
  142. #endif // #if defined(EA_PLATFORM_PS3)
  143. ZEnable = false;
  144. ZWriteEnable = false;
  145. CullMode = None;
  146. AlphaBlendEnable = false;
  147. AlphaTestEnable = false;
  148. }
  149. }
  150. // ----------------------------------------------------------------------------
  151. technique Default_M
  152. {
  153. pass P0
  154. {
  155. VertexShader = compile VS_2_0 DefaultVS();
  156. #if defined(EA_PLATFORM_PS3)
  157. PixelShader = compile PS_2_0 DefaultPS_Default_M();
  158. #else // #if defined(EA_PLATFORM_PS3)
  159. PixelShader = compile PS_2_0 DefaultPS(true, false);
  160. #endif // #if defined(EA_PLATFORM_PS3)
  161. ZEnable = false;
  162. ZWriteEnable = false;
  163. CullMode = None;
  164. AlphaBlendEnable = false;
  165. AlphaTestEnable = false;
  166. }
  167. }
  168. // ----------------------------------------------------------------------------
  169. technique ResolveOnly
  170. {
  171. pass P0
  172. {
  173. VertexShader = compile VS_2_0 DefaultVS();
  174. #if defined(EA_PLATFORM_PS3)
  175. PixelShader = compile PS_2_0 DefaultPS_ResolveOnly();
  176. #else // #if defined(EA_PLATFORM_PS3)
  177. PixelShader = compile PS_2_0 DefaultPS(false, true);
  178. #endif // #if defined(EA_PLATFORM_PS3)
  179. ZEnable = false;
  180. ZWriteEnable = false;
  181. CullMode = None;
  182. AlphaBlendEnable = false;
  183. AlphaTestEnable = false;
  184. }
  185. }
  186. // ----------------------------------------------------------------------------
  187. technique ResolveOnly_M
  188. {
  189. pass P0
  190. {
  191. VertexShader = compile VS_2_0 DefaultVS();
  192. #if defined(EA_PLATFORM_PS3)
  193. PixelShader = compile PS_2_0 DefaultPS_ResolveOnly_M();
  194. #else // #if defined(EA_PLATFORM_PS3)
  195. PixelShader = compile PS_2_0 DefaultPS(false, false);
  196. #endif // #if defined(EA_PLATFORM_PS3)
  197. ZEnable = false;
  198. ZWriteEnable = false;
  199. CullMode = None;
  200. AlphaBlendEnable = false;
  201. AlphaTestEnable = false;
  202. }
  203. }