deferredShadingFeaturesGLSL.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/glsl/deferredShadingFeaturesGLSL.h"
  24. #include "lighting/advanced/advancedLightBinManager.h"
  25. #include "shaderGen/langElement.h"
  26. #include "shaderGen/shaderOp.h"
  27. #include "shaderGen/conditionerFeature.h"
  28. #include "renderInstance/renderPrePassMgr.h"
  29. #include "materials/processedMaterial.h"
  30. #include "materials/materialFeatureTypes.h"
  31. //****************************************************************************
  32. // Deferred Shading Features
  33. //****************************************************************************
  34. // Specular Map -> Blue of Material Buffer ( greyscaled )
  35. // Gloss Map (Alpha Channel of Specular Map) -> Alpha ( Spec Power ) of Material Info Buffer.
  36. void DeferredSpecMapGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  37. {
  38. // Get the texture coord.
  39. Var *texCoord = getInTexCoord( "texCoord", "vec2", true, componentList );
  40. // search for color var
  41. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  42. MultiLine * meta = new MultiLine;
  43. if ( !material )
  44. {
  45. // create color var
  46. material = new Var;
  47. material->setType( "vec4" );
  48. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  49. material->setStructName("OUT");
  50. }
  51. // create texture var
  52. Var *specularMap = new Var;
  53. specularMap->setType( "sampler2D" );
  54. specularMap->setName( "specularMap" );
  55. specularMap->uniform = true;
  56. specularMap->sampler = true;
  57. specularMap->constNum = Var::getTexUnitNum();
  58. LangElement *texOp = new GenOp( "tex2D(@, @)", specularMap, texCoord );
  59. //matinfo.g slot reserved for AO later
  60. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  61. meta->addStatement(new GenOp(" @.b = dot(tex2D(@, @).rgb, vec3(0.3, 0.59, 0.11));\r\n", material, specularMap, texCoord));
  62. meta->addStatement(new GenOp(" @.a = tex2D(@, @).a;\r\n", material, specularMap, texCoord));
  63. output = meta;
  64. }
  65. ShaderFeature::Resources DeferredSpecMapGLSL::getResources( const MaterialFeatureData &fd )
  66. {
  67. Resources res;
  68. res.numTex = 1;
  69. res.numTexReg = 1;
  70. return res;
  71. }
  72. void DeferredSpecMapGLSL::setTexData( Material::StageData &stageDat,
  73. const MaterialFeatureData &fd,
  74. RenderPassData &passData,
  75. U32 &texIndex )
  76. {
  77. GFXTextureObject *tex = stageDat.getTex( MFT_SpecularMap );
  78. if ( tex )
  79. {
  80. passData.mTexType[ texIndex ] = Material::Standard;
  81. passData.mSamplerNames[ texIndex ] = "specularMap";
  82. passData.mTexSlot[ texIndex++ ].texObject = tex;
  83. }
  84. }
  85. void DeferredSpecMapGLSL::processVert( Vector<ShaderComponent*> &componentList,
  86. const MaterialFeatureData &fd )
  87. {
  88. MultiLine *meta = new MultiLine;
  89. getOutTexCoord( "texCoord",
  90. "vec2",
  91. true,
  92. fd.features[MFT_TexAnim],
  93. meta,
  94. componentList );
  95. output = meta;
  96. }
  97. // Material Info Flags -> Red ( Flags ) of Material Info Buffer.
  98. void DeferredMatInfoFlagsGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  99. {
  100. MultiLine *meta = new MultiLine;
  101. // search for material var
  102. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  103. if ( !material )
  104. {
  105. // create material var
  106. material = new Var;
  107. material->setType( "vec4" );
  108. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  109. material->setStructName("OUT");
  110. }
  111. Var *matInfoFlags = new Var;
  112. matInfoFlags->setType( "float" );
  113. matInfoFlags->setName( "matInfoFlags" );
  114. matInfoFlags->uniform = true;
  115. matInfoFlags->constSortPos = cspPotentialPrimitive;
  116. meta->addStatement(output = new GenOp(" @.r = @;\r\n", material, matInfoFlags));
  117. output = meta;
  118. }
  119. // Spec Strength -> Blue Channel of Material Info Buffer.
  120. // Spec Power -> Alpha Channel ( of Material Info Buffer.
  121. void DeferredSpecVarsGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  122. {
  123. // search for material var
  124. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  125. if ( !material )
  126. {
  127. // create material var
  128. material = new Var;
  129. material->setType( "vec4" );
  130. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  131. material->setStructName("OUT");
  132. }
  133. Var *specStrength = new Var;
  134. specStrength->setType( "float" );
  135. specStrength->setName( "specularStrength" );
  136. specStrength->uniform = true;
  137. specStrength->constSortPos = cspPotentialPrimitive;
  138. Var *specPower = new Var;
  139. specPower->setType("float");
  140. specPower->setName("specularPower");
  141. specPower->uniform = true;
  142. specPower->constSortPos = cspPotentialPrimitive;
  143. MultiLine *meta = new MultiLine;
  144. //matinfo.g slot reserved for AO later
  145. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  146. meta->addStatement(new GenOp(" @.b = @/128;\r\n", material, specStrength));
  147. meta->addStatement(new GenOp(" @.a = @/5;\r\n", material, specPower));
  148. output = meta;
  149. }
  150. // Black -> Blue and Alpha of Color Buffer (representing no specular)
  151. void DeferredEmptySpecGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  152. {
  153. // search for material var
  154. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  155. if ( !material )
  156. {
  157. // create material var
  158. material = new Var;
  159. material->setType( "vec4" );
  160. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  161. material->setStructName("OUT");
  162. }
  163. MultiLine * meta = new MultiLine;
  164. //matinfo.g slot reserved for AO later
  165. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  166. meta->addStatement(new GenOp(" @.ba = vec2(0.0);\r\n", material));
  167. output = meta;
  168. }