PostFX_LookupTable.fx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. color.xyz = max(0, color.xyz);
  73. #define TONEMAPPER 2
  74. #if TONEMAPPER == 0
  75. color.xyz = LinearToGamma(color.xyz);
  76. #elif TONEMAPPER == 1
  77. color.xyz /= 1 + color.xyz;
  78. #elif TONEMAPPER == 2
  79. float3 white = 6.0;
  80. color.xyz *= white;
  81. color.xyz = color.xyz * (1 + color.xyz / (white * white)) / (1 + color.xyz);
  82. #else
  83. float3 ld = 0.002;
  84. float linReference = 0.18;
  85. float logReference = 444;
  86. float logGamma = 0.45;
  87. color.xyz = (log10(0.4 * color.xyz / linReference) / ld * logGamma + logReference) / 1023.0;
  88. //color.xyz = clamp(color.xyz, 0, 1);
  89. float FilmLutWidth = 256;
  90. float Padding = .5/FilmLutWidth;
  91. //color.xyz = lerp(Padding,1-Padding,color.xyz);
  92. // apply response lookup and color grading for target display
  93. color.x = tex2D(SAMPLER(FilmTonemappingLookupTexture), float2(color.x, .5)).r;
  94. color.y = tex2D(SAMPLER(FilmTonemappingLookupTexture), float2(color.y, .5)).r;
  95. color.z = tex2D(SAMPLER(FilmTonemappingLookupTexture), float2(color.z, .5)).r;
  96. #endif
  97. }
  98. #endif
  99. if (applyLUT)
  100. {
  101. #if defined (EA_PLATFORM_XENON)
  102. // TODO: LLatta 2008-09-13
  103. // This is a workaround for a Xenon pipeline bug.
  104. // This fix here is a less risky fix at this stage of the project.
  105. // At the earliest convenience fix the actual issue in CVolumeTexture::SaveImage
  106. // See the comment in code\AssetCompilers\0.0-dev\Modules\TextureCompiler\Source\xenon\VolumeTexture.cpp
  107. float4 tableColor = tex3D(SAMPLER(LookupTexture), color.xzy).xzyw;
  108. #else
  109. float4 tableColor = tex3D(SAMPLER(LookupTexture), color.xyz);
  110. #endif
  111. color = lerp(color, tableColor, BlendFactor.x);
  112. }
  113. return color;
  114. }
  115. #if defined(EA_PLATFORM_PS3)
  116. float4 DefaultPS_Default(float2 TexCoord : TEXCOORD0) : COLOR
  117. {
  118. return DefaultPS(TexCoord, true, true);
  119. }
  120. float4 DefaultPS_Default_M(float2 TexCoord : TEXCOORD0) : COLOR
  121. {
  122. return DefaultPS(TexCoord, true, false);
  123. }
  124. float4 DefaultPS_ResolveOnly(float2 TexCoord : TEXCOORD0) : COLOR
  125. {
  126. return DefaultPS(TexCoord, false, true);
  127. }
  128. float4 DefaultPS_ResolveOnly_M(float2 TexCoord : TEXCOORD0) : COLOR
  129. {
  130. return DefaultPS(TexCoord, false, false);
  131. }
  132. #endif // #if defined(EA_PLATFORM_PS3)
  133. // ----------------------------------------------------------------------------
  134. technique Default
  135. {
  136. pass P0
  137. {
  138. VertexShader = compile VS_2_0 DefaultVS();
  139. #if defined(EA_PLATFORM_PS3)
  140. PixelShader = compile PS_2_0 DefaultPS_Default();
  141. #else // #if defined(EA_PLATFORM_PS3)
  142. PixelShader = compile PS_2_0 DefaultPS(true, true);
  143. #endif // #if defined(EA_PLATFORM_PS3)
  144. ZEnable = false;
  145. ZWriteEnable = false;
  146. CullMode = None;
  147. AlphaBlendEnable = false;
  148. AlphaTestEnable = false;
  149. }
  150. }
  151. // ----------------------------------------------------------------------------
  152. technique Default_M
  153. {
  154. pass P0
  155. {
  156. VertexShader = compile VS_2_0 DefaultVS();
  157. #if defined(EA_PLATFORM_PS3)
  158. PixelShader = compile PS_2_0 DefaultPS_Default_M();
  159. #else // #if defined(EA_PLATFORM_PS3)
  160. PixelShader = compile PS_2_0 DefaultPS(true, false);
  161. #endif // #if defined(EA_PLATFORM_PS3)
  162. ZEnable = false;
  163. ZWriteEnable = false;
  164. CullMode = None;
  165. AlphaBlendEnable = false;
  166. AlphaTestEnable = false;
  167. }
  168. }
  169. // ----------------------------------------------------------------------------
  170. technique ResolveOnly
  171. {
  172. pass P0
  173. {
  174. VertexShader = compile VS_2_0 DefaultVS();
  175. #if defined(EA_PLATFORM_PS3)
  176. PixelShader = compile PS_2_0 DefaultPS_ResolveOnly();
  177. #else // #if defined(EA_PLATFORM_PS3)
  178. PixelShader = compile PS_2_0 DefaultPS(false, true);
  179. #endif // #if defined(EA_PLATFORM_PS3)
  180. ZEnable = false;
  181. ZWriteEnable = false;
  182. CullMode = None;
  183. AlphaBlendEnable = false;
  184. AlphaTestEnable = false;
  185. }
  186. }
  187. // ----------------------------------------------------------------------------
  188. technique ResolveOnly_M
  189. {
  190. pass P0
  191. {
  192. VertexShader = compile VS_2_0 DefaultVS();
  193. #if defined(EA_PLATFORM_PS3)
  194. PixelShader = compile PS_2_0 DefaultPS_ResolveOnly_M();
  195. #else // #if defined(EA_PLATFORM_PS3)
  196. PixelShader = compile PS_2_0 DefaultPS(false, false);
  197. #endif // #if defined(EA_PLATFORM_PS3)
  198. ZEnable = false;
  199. ZWriteEnable = false;
  200. CullMode = None;
  201. AlphaBlendEnable = false;
  202. AlphaTestEnable = false;
  203. }
  204. }