deferredShadingFeaturesHLSL.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/hlsl/deferredShadingFeaturesHLSL.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 PBRConfigMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  35. {
  36. // Get the texture coord.
  37. Var *texCoord = getInTexCoord( "texCoord", "float2", 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( "fragout" );
  46. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  47. material->setStructName( "OUT" );
  48. }
  49. // create texture var
  50. Var * pbrConfigMap = new Var;
  51. pbrConfigMap->setType( "SamplerState" );
  52. pbrConfigMap->setName( "PBRConfigMap" );
  53. pbrConfigMap->uniform = true;
  54. pbrConfigMap->sampler = true;
  55. pbrConfigMap->constNum = Var::getTexUnitNum();
  56. Var* pbrConfigMapTex = new Var;
  57. pbrConfigMapTex->setName("PBRConfigMapTex");
  58. pbrConfigMapTex->setType("Texture2D");
  59. pbrConfigMapTex->uniform = true;
  60. pbrConfigMapTex->texture = true;
  61. pbrConfigMapTex->constNum = pbrConfigMap->constNum;
  62. LangElement *texOp = new GenOp(" @.Sample(@, @)", pbrConfigMapTex, pbrConfigMap, texCoord);
  63. Var * pbrConfig = (Var*)LangElement::find("PBRConfig");
  64. if (!pbrConfig) pbrConfig = new Var("PBRConfig", "float4");
  65. Var *metalness = (Var*)LangElement::find("metalness");
  66. if (!metalness) metalness = new Var("metalness", "float");
  67. Var *smoothness = (Var*)LangElement::find("smoothness");
  68. if (!smoothness) smoothness = new Var("smoothness", "float");
  69. meta->addStatement(new GenOp(" @ = @.r;\r\n", new DecOp(smoothness), texOp));
  70. meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(metalness), texOp));
  71. if (fd.features[MFT_InvertSmoothness])
  72. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  73. meta->addStatement(new GenOp(" @ = @.ggga;\r\n", new DecOp(pbrConfig), texOp));
  74. meta->addStatement(new GenOp(" @.bga = float3(@,@.g,@);\r\n", material, smoothness, pbrConfig, metalness));
  75. output = meta;
  76. }
  77. ShaderFeature::Resources PBRConfigMapHLSL::getResources( const MaterialFeatureData &fd )
  78. {
  79. Resources res;
  80. res.numTex = 1;
  81. res.numTexReg = 1;
  82. return res;
  83. }
  84. void PBRConfigMapHLSL::setTexData( Material::StageData &stageDat,
  85. const MaterialFeatureData &fd,
  86. RenderPassData &passData,
  87. U32 &texIndex )
  88. {
  89. GFXTextureObject *tex = stageDat.getTex(MFT_PBRConfigMap);
  90. if ( tex )
  91. {
  92. passData.mTexType[ texIndex ] = Material::Standard;
  93. passData.mSamplerNames[ texIndex ] = "PBRConfigMap";
  94. passData.mTexSlot[ texIndex++ ].texObject = tex;
  95. }
  96. }
  97. void PBRConfigMapHLSL::processVert( Vector<ShaderComponent*> &componentList,
  98. const MaterialFeatureData &fd )
  99. {
  100. MultiLine *meta = new MultiLine;
  101. getOutTexCoord( "texCoord",
  102. "float2",
  103. fd.features[MFT_TexAnim],
  104. meta,
  105. componentList );
  106. output = meta;
  107. }
  108. // Material Info Flags -> Red ( Flags ) of Material Info Buffer.
  109. void DeferredMatInfoFlagsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  110. {
  111. // search for material var
  112. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  113. if ( !material )
  114. {
  115. // create material var
  116. material = new Var;
  117. material->setType( "fragout" );
  118. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  119. material->setStructName( "OUT" );
  120. }
  121. Var *matInfoFlags = new Var;
  122. matInfoFlags->setType( "float" );
  123. matInfoFlags->setName( "matInfoFlags" );
  124. matInfoFlags->uniform = true;
  125. matInfoFlags->constSortPos = cspPotentialPrimitive;
  126. output = new GenOp( " @.r = @;\r\n", material, matInfoFlags );
  127. }
  128. // Spec Strength -> Blue Channel of Material Info Buffer.
  129. // Spec Power -> Alpha Channel ( of Material Info Buffer.
  130. void PBRConfigVarsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  131. {
  132. // search for material var
  133. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  134. if ( !material )
  135. {
  136. // create material var
  137. material = new Var;
  138. material->setType( "fragout" );
  139. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  140. material->setStructName( "OUT" );
  141. }
  142. Var *metalness = new Var("metalness", "float");
  143. metalness->uniform = true;
  144. metalness->constSortPos = cspPotentialPrimitive;
  145. Var *smoothness = new Var("smoothness", "float");
  146. smoothness->uniform = true;
  147. smoothness->constSortPos = cspPotentialPrimitive;
  148. MultiLine * meta = new MultiLine;
  149. //matinfo.g slot reserved for AO later
  150. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  151. meta->addStatement(new GenOp(" @.b = @;\r\n", material, smoothness));
  152. if (fd.features[MFT_InvertSmoothness])
  153. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  154. meta->addStatement(new GenOp(" @.a = @;\r\n", material, metalness));
  155. output = meta;
  156. }
  157. //deferred emissive
  158. void DeferredEmissiveHLSL::processPix(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  159. {
  160. //for now emission just uses the diffuse color, we could plug in a separate texture for emission at some stage
  161. Var *diffuseTargetVar = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  162. if (!diffuseTargetVar)
  163. return; //oh dear something is not right, maybe we should just write 0's instead
  164. // search for scene color target var
  165. Var *sceneColorVar = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  166. if (!sceneColorVar)
  167. {
  168. // create scene color target var
  169. sceneColorVar = new Var;
  170. sceneColorVar->setType("fragout");
  171. sceneColorVar->setName(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  172. sceneColorVar->setStructName("OUT");
  173. }
  174. output = new GenOp("@ = float4(@.rgb,0);", sceneColorVar, diffuseTargetVar);
  175. }