deferredShadingFeaturesHLSL.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. U32 PBRConfigMapHLSL::getOutputTargets(const MaterialFeatureData& fd) const
  35. {
  36. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  37. }
  38. void PBRConfigMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  39. {
  40. // Get the texture coord.
  41. Var *texCoord = getInTexCoord( "texCoord", "float2", componentList );
  42. MultiLine* meta = new MultiLine;
  43. Var* pbrConfig;
  44. if (fd.features[MFT_isDeferred])
  45. {
  46. pbrConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  47. if (!pbrConfig)
  48. {
  49. // create material var
  50. pbrConfig = new Var;
  51. pbrConfig->setType("fragout");
  52. pbrConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  53. pbrConfig->setStructName("OUT");
  54. }
  55. }
  56. else
  57. {
  58. pbrConfig = (Var*)LangElement::find("PBRConfig");
  59. if (!pbrConfig)
  60. {
  61. pbrConfig = new Var("PBRConfig", "float4");
  62. meta->addStatement(new GenOp(" @;\r\n", new DecOp(pbrConfig)));
  63. }
  64. }
  65. // create texture var
  66. Var * pbrConfigMap = new Var;
  67. pbrConfigMap->setType( "SamplerState" );
  68. pbrConfigMap->setName( "PBRConfigMap" );
  69. pbrConfigMap->uniform = true;
  70. pbrConfigMap->sampler = true;
  71. pbrConfigMap->constNum = Var::getTexUnitNum();
  72. Var* pbrConfigMapTex = new Var;
  73. pbrConfigMapTex->setName("PBRConfigMapTex");
  74. pbrConfigMapTex->setType("Texture2D");
  75. pbrConfigMapTex->uniform = true;
  76. pbrConfigMapTex->texture = true;
  77. pbrConfigMapTex->constNum = pbrConfigMap->constNum;
  78. LangElement *texOp = new GenOp("@.Sample(@, @)", pbrConfigMapTex, pbrConfigMap, texCoord);
  79. Var *metalness = (Var*)LangElement::find("metalness");
  80. if (!metalness) metalness = new Var("metalness", "float");
  81. Var *smoothness = (Var*)LangElement::find("smoothness");
  82. if (!smoothness) smoothness = new Var("smoothness", "float");
  83. Var* ao = (Var*)LangElement::find("ao");
  84. if (!ao) ao = new Var("ao", "float");
  85. meta->addStatement(new GenOp(" @.bga = @.rgb;\r\n", pbrConfig, texOp));
  86. meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(smoothness), pbrConfig));
  87. if (fd.features[MFT_InvertSmoothness])
  88. {
  89. meta->addStatement(new GenOp(" @.b = [email protected];\r\n", pbrConfig, pbrConfig));
  90. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  91. }
  92. meta->addStatement(new GenOp(" @ = @.g;\r\n", new DecOp(ao), pbrConfig));
  93. meta->addStatement(new GenOp(" @ = @.a;\r\n", new DecOp(metalness), pbrConfig));
  94. if (fd.features[MFT_GlowMap])
  95. {
  96. Var* glowMul = new Var("glowMul", "float");
  97. glowMul->uniform = true;
  98. glowMul->constSortPos = cspPotentialPrimitive;
  99. ShaderFeature::OutputTarget inTarg = ShaderFeature::DefaultTarget;
  100. ShaderFeature::OutputTarget outTarg = ShaderFeature::DefaultTarget;
  101. if (fd.features[MFT_isDeferred])
  102. {
  103. inTarg = ShaderFeature::RenderTarget1;
  104. outTarg = ShaderFeature::RenderTarget3;
  105. }
  106. Var* diffuseColor = (Var*)LangElement::find(getOutputTargetVarName(inTarg));
  107. Var* emissionColor = (Var*)LangElement::find(getOutputTargetVarName(outTarg));
  108. meta->addStatement(new GenOp(" @.rgb += @.rgb*float3(@,@,@)*@.aaa;\r\n", emissionColor, diffuseColor, glowMul, glowMul, glowMul, texOp));
  109. }
  110. output = meta;
  111. }
  112. ShaderFeature::Resources PBRConfigMapHLSL::getResources( const MaterialFeatureData &fd )
  113. {
  114. Resources res;
  115. res.numTex = 1;
  116. res.numTexReg = 1;
  117. return res;
  118. }
  119. void PBRConfigMapHLSL::setTexData( Material::StageData &stageDat,
  120. const MaterialFeatureData &fd,
  121. RenderPassData &passData,
  122. U32 &texIndex )
  123. {
  124. GFXTextureObject *tex = stageDat.getTex(MFT_PBRConfigMap);
  125. if ( tex )
  126. {
  127. passData.mTexType[ texIndex ] = Material::Standard;
  128. passData.mSamplerNames[ texIndex ] = "PBRConfigMap";
  129. passData.mTexSlot[ texIndex++ ].texObject = tex;
  130. }
  131. }
  132. void PBRConfigMapHLSL::processVert( Vector<ShaderComponent*> &componentList,
  133. const MaterialFeatureData &fd )
  134. {
  135. MultiLine *meta = new MultiLine;
  136. getOutTexCoord( "texCoord",
  137. "float2",
  138. fd.features[MFT_TexAnim],
  139. meta,
  140. componentList );
  141. output = meta;
  142. }
  143. U32 MatInfoFlagsHLSL::getOutputTargets(const MaterialFeatureData& fd) const
  144. {
  145. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  146. }
  147. // Material Info Flags -> Red ( Flags ) of Material Info Buffer.
  148. void MatInfoFlagsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  149. {
  150. // search for material var
  151. Var* pbrConfig;
  152. if (fd.features[MFT_isDeferred])
  153. {
  154. pbrConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  155. if (!pbrConfig)
  156. {
  157. // create material var
  158. pbrConfig = new Var;
  159. pbrConfig->setType("fragout");
  160. pbrConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  161. pbrConfig->setStructName("OUT");
  162. }
  163. }
  164. else
  165. {
  166. pbrConfig = (Var*)LangElement::find("PBRConfig");
  167. if (!pbrConfig) pbrConfig = new Var("PBRConfig", "float4");
  168. }
  169. Var *matInfoFlags = new Var;
  170. matInfoFlags->setType( "float" );
  171. matInfoFlags->setName( "matInfoFlags" );
  172. matInfoFlags->uniform = true;
  173. matInfoFlags->constSortPos = cspPotentialPrimitive;
  174. output = new GenOp( " @.r = @;\r\n", pbrConfig, matInfoFlags );
  175. }
  176. U32 PBRConfigVarsHLSL::getOutputTargets(const MaterialFeatureData& fd) const
  177. {
  178. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  179. }
  180. void PBRConfigVarsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  181. {
  182. MultiLine* meta = new MultiLine;
  183. Var* pbrConfig;
  184. if (fd.features[MFT_isDeferred])
  185. {
  186. pbrConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  187. if (!pbrConfig)
  188. {
  189. // create material var
  190. pbrConfig = new Var;
  191. pbrConfig->setType("fragout");
  192. pbrConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  193. pbrConfig->setStructName("OUT");
  194. }
  195. }
  196. else
  197. {
  198. pbrConfig = (Var*)LangElement::find("PBRConfig");
  199. if (!pbrConfig) pbrConfig = new Var("PBRConfig", "float4");
  200. meta->addStatement(new GenOp(" @;\r\n", new DecOp(pbrConfig)));
  201. }
  202. Var *metalness = new Var("metalness", "float");
  203. metalness->uniform = true;
  204. metalness->constSortPos = cspPotentialPrimitive;
  205. Var *smoothness = new Var("smoothness", "float");
  206. smoothness->uniform = true;
  207. smoothness->constSortPos = cspPotentialPrimitive;
  208. //matinfo.g slot reserved for AO later
  209. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", pbrConfig));
  210. meta->addStatement(new GenOp(" @.b = @;\r\n", pbrConfig, smoothness));
  211. if (fd.features[MFT_InvertSmoothness])
  212. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  213. meta->addStatement(new GenOp(" @.a = @;\r\n", pbrConfig, metalness));
  214. output = meta;
  215. }
  216. //deferred emissive
  217. void DeferredEmissiveHLSL::processPix(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  218. {
  219. //for now emission just uses the diffuse color, we could plug in a separate texture for emission at some stage
  220. Var *diffuseTargetVar = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  221. if (!diffuseTargetVar)
  222. return; //oh dear something is not right, maybe we should just write 0's instead
  223. // search for scene color target var
  224. Var *sceneColorVar = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  225. if (!sceneColorVar)
  226. {
  227. // create scene color target var
  228. sceneColorVar = new Var;
  229. sceneColorVar->setType("fragout");
  230. sceneColorVar->setName(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  231. sceneColorVar->setStructName("OUT");
  232. }
  233. output = new GenOp("@ = float4(@.rgb,0);", sceneColorVar, diffuseTargetVar);
  234. }