deferredShadingFeaturesHLSL.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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::RenderTarget2 : 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. meta->addStatement(new GenOp(" @ = @.r;\r\n", new DecOp(smoothness), texOp));
  84. meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(metalness), texOp));
  85. if (fd.features[MFT_InvertSmoothness])
  86. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  87. if (!fd.features[MFT_isDeferred])
  88. meta->addStatement(new GenOp(" @ = @.ggga;\r\n", pbrConfig, texOp));
  89. meta->addStatement(new GenOp(" @.bga = float3(@,@.g,@);\r\n", pbrConfig, smoothness, pbrConfig, metalness));
  90. output = meta;
  91. }
  92. ShaderFeature::Resources PBRConfigMapHLSL::getResources( const MaterialFeatureData &fd )
  93. {
  94. Resources res;
  95. res.numTex = 1;
  96. res.numTexReg = 1;
  97. return res;
  98. }
  99. void PBRConfigMapHLSL::setTexData( Material::StageData &stageDat,
  100. const MaterialFeatureData &fd,
  101. RenderPassData &passData,
  102. U32 &texIndex )
  103. {
  104. GFXTextureObject *tex = stageDat.getTex(MFT_PBRConfigMap);
  105. if ( tex )
  106. {
  107. passData.mTexType[ texIndex ] = Material::Standard;
  108. passData.mSamplerNames[ texIndex ] = "PBRConfigMap";
  109. passData.mTexSlot[ texIndex++ ].texObject = tex;
  110. }
  111. }
  112. void PBRConfigMapHLSL::processVert( Vector<ShaderComponent*> &componentList,
  113. const MaterialFeatureData &fd )
  114. {
  115. MultiLine *meta = new MultiLine;
  116. getOutTexCoord( "texCoord",
  117. "float2",
  118. fd.features[MFT_TexAnim],
  119. meta,
  120. componentList );
  121. output = meta;
  122. }
  123. // Material Info Flags -> Red ( Flags ) of Material Info Buffer.
  124. void DeferredMatInfoFlagsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  125. {
  126. // search for material var
  127. Var* pbrConfig;
  128. if (fd.features[MFT_isDeferred])
  129. {
  130. pbrConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  131. if (!pbrConfig)
  132. {
  133. // create material var
  134. pbrConfig = new Var;
  135. pbrConfig->setType("fragout");
  136. pbrConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  137. pbrConfig->setStructName("OUT");
  138. }
  139. }
  140. else
  141. {
  142. pbrConfig = (Var*)LangElement::find("PBRConfig");
  143. if (!pbrConfig) pbrConfig = new Var("PBRConfig", "float4");
  144. }
  145. Var *matInfoFlags = new Var;
  146. matInfoFlags->setType( "float" );
  147. matInfoFlags->setName( "matInfoFlags" );
  148. matInfoFlags->uniform = true;
  149. matInfoFlags->constSortPos = cspPotentialPrimitive;
  150. output = new GenOp( " @.r = @;\r\n", pbrConfig, matInfoFlags );
  151. }
  152. U32 PBRConfigVarsHLSL::getOutputTargets(const MaterialFeatureData& fd) const
  153. {
  154. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  155. }
  156. void PBRConfigVarsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  157. {
  158. MultiLine* meta = new MultiLine;
  159. Var* pbrConfig;
  160. if (fd.features[MFT_isDeferred])
  161. {
  162. pbrConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  163. if (!pbrConfig)
  164. {
  165. // create material var
  166. pbrConfig = new Var;
  167. pbrConfig->setType("fragout");
  168. pbrConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  169. pbrConfig->setStructName("OUT");
  170. }
  171. }
  172. else
  173. {
  174. pbrConfig = (Var*)LangElement::find("PBRConfig");
  175. if (!pbrConfig) pbrConfig = new Var("PBRConfig", "float4");
  176. meta->addStatement(new GenOp(" @;\r\n", new DecOp(pbrConfig)));
  177. }
  178. Var *metalness = new Var("metalness", "float");
  179. metalness->uniform = true;
  180. metalness->constSortPos = cspPotentialPrimitive;
  181. Var *smoothness = new Var("smoothness", "float");
  182. smoothness->uniform = true;
  183. smoothness->constSortPos = cspPotentialPrimitive;
  184. //matinfo.g slot reserved for AO later
  185. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", pbrConfig));
  186. meta->addStatement(new GenOp(" @.b = @;\r\n", pbrConfig, smoothness));
  187. if (fd.features[MFT_InvertSmoothness])
  188. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  189. meta->addStatement(new GenOp(" @.a = @;\r\n", pbrConfig, metalness));
  190. output = meta;
  191. }
  192. //deferred emissive
  193. void DeferredEmissiveHLSL::processPix(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  194. {
  195. //for now emission just uses the diffuse color, we could plug in a separate texture for emission at some stage
  196. Var *diffuseTargetVar = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  197. if (!diffuseTargetVar)
  198. return; //oh dear something is not right, maybe we should just write 0's instead
  199. // search for scene color target var
  200. Var *sceneColorVar = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  201. if (!sceneColorVar)
  202. {
  203. // create scene color target var
  204. sceneColorVar = new Var;
  205. sceneColorVar->setType("fragout");
  206. sceneColorVar->setName(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  207. sceneColorVar->setStructName("OUT");
  208. }
  209. output = new GenOp("@ = float4(@.rgb,0);", sceneColorVar, diffuseTargetVar);
  210. }