deferredShadingFeaturesGLSL.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/renderDeferredMgr.h"
  29. #include "materials/processedMaterial.h"
  30. #include "materials/materialFeatureTypes.h"
  31. //****************************************************************************
  32. // Deferred Shading Features
  33. //****************************************************************************
  34. void PBRConfigMapGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  35. {
  36. // Get the texture coord.
  37. Var *texCoord = getInTexCoord( "texCoord", "vec2", componentList );
  38. // search for color var
  39. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  40. MultiLine * meta = new MultiLine;
  41. if ( !material )
  42. {
  43. // create color var
  44. material = new Var;
  45. material->setType( "vec4" );
  46. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  47. material->setStructName("OUT");
  48. }
  49. // create texture var
  50. Var *pbrConfigMap = new Var;
  51. pbrConfigMap->setType( "sampler2D" );
  52. pbrConfigMap->setName( "pbrConfigMap" );
  53. pbrConfigMap->uniform = true;
  54. pbrConfigMap->sampler = true;
  55. pbrConfigMap->constNum = Var::getTexUnitNum();
  56. LangElement *texOp = new GenOp( "tex2D(@, @)", pbrConfigMap, texCoord );
  57. Var *pbrConfig = (Var*)LangElement::find("PBRConfig");
  58. if (!pbrConfig) pbrConfig = new Var("PBRConfig", "vec4");
  59. Var *metalness = (Var*)LangElement::find("metalness");
  60. if (!metalness) metalness = new Var("metalness", "float");
  61. Var *smoothness = (Var*)LangElement::find("smoothness");
  62. if (!smoothness) smoothness = new Var("smoothness", "float");
  63. meta->addStatement(new GenOp(" @ = @.r;\r\n", new DecOp(smoothness), texOp));
  64. meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(metalness), texOp));
  65. if (fd.features[MFT_InvertSmoothness])
  66. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  67. meta->addStatement(new GenOp(" @ = @.ggga;\r\n", new DecOp(pbrConfig), texOp));
  68. meta->addStatement(new GenOp(" @.bga = vec3(@,@.g,@);\r\n", material, smoothness, pbrConfig, metalness));
  69. output = meta;
  70. }
  71. ShaderFeature::Resources PBRConfigMapGLSL::getResources( const MaterialFeatureData &fd )
  72. {
  73. Resources res;
  74. res.numTex = 1;
  75. res.numTexReg = 1;
  76. return res;
  77. }
  78. void PBRConfigMapGLSL::setTexData( Material::StageData &stageDat,
  79. const MaterialFeatureData &fd,
  80. RenderPassData &passData,
  81. U32 &texIndex )
  82. {
  83. GFXTextureObject *tex = stageDat.getTex(MFT_PBRConfigMap);
  84. if ( tex )
  85. {
  86. passData.mTexType[ texIndex ] = Material::Standard;
  87. passData.mSamplerNames[ texIndex ] = "pbrConfigMap";
  88. passData.mTexSlot[ texIndex++ ].texObject = tex;
  89. }
  90. }
  91. void PBRConfigMapGLSL::processVert( Vector<ShaderComponent*> &componentList,
  92. const MaterialFeatureData &fd )
  93. {
  94. MultiLine *meta = new MultiLine;
  95. getOutTexCoord( "texCoord",
  96. "vec2",
  97. fd.features[MFT_TexAnim],
  98. meta,
  99. componentList );
  100. output = meta;
  101. }
  102. // Material Info Flags -> Red ( Flags ) of Material Info Buffer.
  103. void MatInfoFlagsGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  104. {
  105. MultiLine *meta = new MultiLine;
  106. // search for material var
  107. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  108. if ( !material )
  109. {
  110. // create material var
  111. material = new Var;
  112. material->setType( "vec4" );
  113. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  114. material->setStructName("OUT");
  115. }
  116. Var *matInfoFlags = new Var;
  117. matInfoFlags->setType( "float" );
  118. matInfoFlags->setName( "matInfoFlags" );
  119. matInfoFlags->uniform = true;
  120. matInfoFlags->constSortPos = cspPotentialPrimitive;
  121. meta->addStatement(output = new GenOp(" @.r = @;\r\n", material, matInfoFlags));
  122. output = meta;
  123. }
  124. // Spec Strength -> Blue Channel of Material Info Buffer.
  125. // Spec Power -> Alpha Channel ( of Material Info Buffer.
  126. void PBRConfigVarsGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  127. {
  128. // search for material var
  129. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  130. if ( !material )
  131. {
  132. // create material var
  133. material = new Var;
  134. material->setType( "vec4" );
  135. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  136. material->setStructName("OUT");
  137. }
  138. Var *metalness = new Var("metalness", "float");
  139. metalness->uniform = true;
  140. metalness->constSortPos = cspPotentialPrimitive;
  141. Var *smoothness = new Var("smoothness", "float");
  142. smoothness->uniform = true;
  143. smoothness->constSortPos = cspPotentialPrimitive;
  144. MultiLine *meta = new MultiLine;
  145. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  146. meta->addStatement(new GenOp(" @.b = @;\r\n", material, smoothness));
  147. if (fd.features[MFT_InvertSmoothness])
  148. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  149. meta->addStatement(new GenOp(" @.a = @;\r\n", material, metalness));
  150. output = meta;
  151. }