advancedLightBufferConditioner.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "lighting/advanced/advancedLightBufferConditioner.h"
  24. #include "shaderGen/shaderOp.h"
  25. #include "gfx/gfxDevice.h"
  26. #include "core/util/safeDelete.h"
  27. AdvancedLightBufferConditioner::~AdvancedLightBufferConditioner()
  28. {
  29. }
  30. Var *AdvancedLightBufferConditioner::_conditionOutput( Var *unconditionedOutput, MultiLine *meta )
  31. {
  32. Var *conditionedOutput = new Var;
  33. if(GFX->getAdapterType() == OpenGL)
  34. conditionedOutput->setType("vec4");
  35. else
  36. conditionedOutput->setType("float4");
  37. DecOp *outputDecl = new DecOp(conditionedOutput);
  38. if(mColorFormat == RGB)
  39. {
  40. conditionedOutput->setName("rgbLightInfoOut");
  41. // If this is a 16 bit integer format, scale up/down the values. All other
  42. // formats just write out the full 0..1
  43. if(getBufferFormat() == GFXFormatR16G16B16A16)
  44. meta->addStatement( new GenOp( " @ = max(4.0, (float4(lightColor, specular) * NL_att + float4(bufferSample.rgb, 0.0)) / 4.0);\r\n", outputDecl ) );
  45. else
  46. meta->addStatement( new GenOp( " @ = float4(lightColor, 0) * NL_att + float4(bufferSample.rgb, specular);\r\n", outputDecl ) );
  47. }
  48. else
  49. {
  50. // Input u'v' assumed to be scaled
  51. conditionedOutput->setName("luvLightInfoOut");
  52. meta->addStatement( new GenOp( " @ = float4( lerp(bufferSample.xy, lightColor.xy, saturate(NL_att / bufferSample.z) * 0.5),\r\n", outputDecl ) );
  53. meta->addStatement( new GenOp( " bufferSample.z + NL_att, bufferSample.w + saturate(specular * NL_att) );\r\n" ) );
  54. }
  55. return conditionedOutput;
  56. }
  57. Var *AdvancedLightBufferConditioner::_unconditionInput( Var *conditionedInput, MultiLine *meta )
  58. {
  59. if(mColorFormat == RGB)
  60. {
  61. if(getBufferFormat() == GFXFormatR16G16B16A16)
  62. meta->addStatement( new GenOp( " lightColor = @.rgb * 4.0;\r\n", conditionedInput ) );
  63. else
  64. meta->addStatement( new GenOp( " lightColor = @.rgb;\r\n", conditionedInput ) );
  65. meta->addStatement( new GenOp( " NL_att = dot(@.rgb, float3(0.3576, 0.7152, 0.1192));\r\n", conditionedInput ) );
  66. }
  67. else
  68. {
  69. meta->addStatement( new GenOp( " // TODO: This clamps HDR values.\r\n" ) );
  70. meta->addStatement( new GenOp( " NL_att = @.b;\r\n", conditionedInput ) );
  71. meta->addStatement( new GenOp( " lightColor = DecodeLuv(float3(saturate(NL_att), @.rg * 0.62));\r\n", conditionedInput ) );
  72. }
  73. meta->addStatement( new GenOp( " specular = @.a;\r\n", conditionedInput ) );
  74. return NULL;
  75. }
  76. Var *AdvancedLightBufferConditioner::printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta )
  77. {
  78. Var *methodVar = new Var;
  79. methodVar->setName(methodName);
  80. DecOp *methodDecl = new DecOp(methodVar);
  81. Var *lightColor = new Var;
  82. lightColor->setName("lightColor");
  83. DecOp *lightColorDecl = new DecOp(lightColor);
  84. Var *NLAtt = new Var;
  85. NLAtt->setName("NL_att");
  86. DecOp *NLAttDecl = new DecOp(NLAtt);
  87. Var *specular = new Var;
  88. specular->setName("specular");
  89. DecOp *specularDecl = new DecOp(specular);
  90. Var *bufferSample = new Var;
  91. bufferSample->setName("bufferSample");
  92. DecOp *bufferSampleDecl = new DecOp(bufferSample);
  93. const bool isCondition = ( methodType == ConditionerFeature::ConditionMethod );
  94. if(GFX->getAdapterType() == OpenGL)
  95. {
  96. methodVar->setType(avar("%s", isCondition ? "vec4" : "void"));
  97. lightColor->setType(avar("%s vec3", isCondition ? "in" : "out"));
  98. NLAtt->setType(avar("%s float", isCondition ? "in" : "out"));
  99. specular->setType(avar("%s float", isCondition ? "in" : "out"));
  100. bufferSample->setType("in vec4");
  101. }
  102. else
  103. {
  104. methodVar->setType(avar("inline %s", isCondition ? "float4" : "void"));
  105. lightColor->setType(avar("%s float3", isCondition ? "in" : "out"));
  106. NLAtt->setType(avar("%s float", isCondition ? "in" : "out"));
  107. specular->setType(avar("%s float", isCondition ? "in" : "out"));
  108. bufferSample->setType("in float4");
  109. }
  110. // If this is LUV, print methods to convert RGB<->LUV as needed
  111. if(mColorFormat == LUV)
  112. {
  113. if(!isCondition)
  114. {
  115. meta->addStatement( new GenOp( "float3 DecodeLuv(float3 Luv)\r\n{\r\n" ) );
  116. meta->addStatement( new GenOp( " float2 xy = float2(9.0f, 4.0f) * Luv.yz / (dot(Luv.yz, float2(6.0f, -16.0f)) + 12.0f);\r\n" ) );
  117. meta->addStatement( new GenOp( " float Ld = Luv.x;\r\n" ) );
  118. meta->addStatement( new GenOp( " float3 XYZ = float3(xy.x, Ld, 1.0f - xy.x - xy.y);\r\n" ) );
  119. meta->addStatement( new GenOp( " XYZ.xz = XYZ.xz * Ld / xy.y;\r\n" ) );
  120. meta->addStatement( new GenOp( " const float3x3 XYZ2RGB =\r\n" ) );
  121. meta->addStatement( new GenOp( " {\r\n" ) );
  122. meta->addStatement( new GenOp( " 2.5651f, -1.1665f, -0.3986f,\r\n" ) );
  123. meta->addStatement( new GenOp( " -1.0217f, 1.9777f, 0.0439f,\r\n" ) );
  124. meta->addStatement( new GenOp( " 0.0753f, -0.2543f, 1.1892f\r\n" ) );
  125. meta->addStatement( new GenOp( " };\r\n" ) );
  126. meta->addStatement( new GenOp( " return tMul(XYZ2RGB, XYZ);\r\n" ) );
  127. meta->addStatement( new GenOp( "}\r\n\r\n" ) );
  128. }
  129. else
  130. {
  131. // Shouldn't need this
  132. }
  133. }
  134. // Method header and opening bracket
  135. if(isCondition)
  136. {
  137. // All parameters are input parameters, and the return value is float4.
  138. // If this is an LUV buffer format, than the previous pixel value is needed
  139. // for interpolation.
  140. meta->addStatement( new GenOp( "@(@, @, @, @)\r\n", methodDecl, lightColorDecl, NLAttDecl, specularDecl, bufferSampleDecl ) );
  141. }
  142. else
  143. {
  144. // Sample as input, parameters as output. Void return.
  145. meta->addStatement( new GenOp( "@(@, @, @, @)\r\n", methodDecl, bufferSampleDecl, lightColorDecl, NLAttDecl, specularDecl ) );
  146. }
  147. meta->addStatement( new GenOp( "{\r\n" ) );
  148. // We don't use this way of passing var's around, so this should cause a crash
  149. // if something uses this improperly
  150. return ( isCondition ? NULL : bufferSample );
  151. }
  152. void AdvancedLightBufferConditioner::printMethodFooter( ConditionerFeature::MethodType methodType, Var *retVar, Stream &stream, MultiLine *meta )
  153. {
  154. // Return and closing bracket
  155. if(methodType == ConditionerFeature::ConditionMethod)
  156. meta->addStatement( new GenOp( "\r\n return @;\r\n", retVar ) );
  157. // Uncondition will assign output parameters
  158. meta->addStatement( new GenOp( "}\r\n" ) );
  159. }