deferredShadingFeaturesHLSL.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 DeferredOrmMapHLSL::getOutputTargets(const MaterialFeatureData& fd) const
  35. {
  36. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  37. }
  38. void DeferredOrmMapHLSL::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* ormConfig;
  44. if (fd.features[MFT_isDeferred])
  45. {
  46. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  47. if (!ormConfig)
  48. {
  49. // create material var
  50. ormConfig = new Var;
  51. ormConfig->setType("fragout");
  52. ormConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  53. ormConfig->setStructName("OUT");
  54. }
  55. }
  56. else
  57. {
  58. ormConfig = (Var*)LangElement::find("ORMConfig");
  59. if (!ormConfig)
  60. {
  61. ormConfig = new Var("ORMConfig", "float4");
  62. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  63. }
  64. }
  65. // create texture var
  66. Var * ormMap = new Var;
  67. ormMap->setType( "SamplerState" );
  68. ormMap->setName( "ORMConfigMap" );
  69. ormMap->uniform = true;
  70. ormMap->sampler = true;
  71. ormMap->constNum = Var::getTexUnitNum();
  72. Var* ormMapTex = new Var;
  73. ormMapTex->setName("ORMConfigMapTex");
  74. ormMapTex->setType("Texture2D");
  75. ormMapTex->uniform = true;
  76. ormMapTex->texture = true;
  77. ormMapTex->constNum = ormMap->constNum;
  78. LangElement *texOp = new GenOp("@.Sample(@, @)", ormMapTex, ormMap, texCoord);
  79. Var *metalness = (Var*)LangElement::find("metalness");
  80. if (!metalness) metalness = new Var("metalness", "float");
  81. Var *roughness = (Var*)LangElement::find("roughness");
  82. if (!roughness) roughness = new Var("roughness", "float");
  83. Var* ao = (Var*)LangElement::find("ao");
  84. if (!ao) ao = new Var("ao", "float");
  85. meta->addStatement(new GenOp(" @.gba = @.rgb;\r\n", ormConfig, texOp));
  86. meta->addStatement(new GenOp(" @ = @.g;\r\n", new DecOp(ao), ormConfig));
  87. meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(roughness), ormConfig));
  88. if (fd.features[MFT_InvertRoughness])
  89. {
  90. meta->addStatement(new GenOp(" @.b = [email protected];\r\n", ormConfig, ormConfig));
  91. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", roughness, roughness));
  92. }
  93. meta->addStatement(new GenOp(" @ = @.a;\r\n", new DecOp(metalness), ormConfig));
  94. output = meta;
  95. }
  96. ShaderFeature::Resources DeferredOrmMapHLSL::getResources( const MaterialFeatureData &fd )
  97. {
  98. Resources res;
  99. res.numTex = 1;
  100. res.numTexReg = 1;
  101. return res;
  102. }
  103. void DeferredOrmMapHLSL::setTexData( Material::StageData &stageDat,
  104. const MaterialFeatureData &fd,
  105. RenderPassData &passData,
  106. U32 &texIndex )
  107. {
  108. GFXTextureObject *tex = stageDat.getTex(MFT_OrmMap);
  109. if ( tex )
  110. {
  111. passData.mTexType[ texIndex ] = Material::Standard;
  112. passData.mSamplerNames[ texIndex ] = "ORMConfigMap";
  113. passData.mTexSlot[ texIndex++ ].texObject = tex;
  114. }
  115. }
  116. void DeferredOrmMapHLSL::processVert( Vector<ShaderComponent*> &componentList,
  117. const MaterialFeatureData &fd )
  118. {
  119. MultiLine *meta = new MultiLine;
  120. getOutTexCoord( "texCoord",
  121. "float2",
  122. fd.features[MFT_TexAnim],
  123. meta,
  124. componentList );
  125. output = meta;
  126. }
  127. U32 MatInfoFlagsHLSL::getOutputTargets(const MaterialFeatureData& fd) const
  128. {
  129. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  130. }
  131. // Material Info Flags -> Red ( Flags ) of Material Info Buffer.
  132. void MatInfoFlagsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  133. {
  134. // search for material var
  135. Var* ormConfig;
  136. if (fd.features[MFT_isDeferred])
  137. {
  138. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  139. if (!ormConfig)
  140. {
  141. // create material var
  142. ormConfig = new Var;
  143. ormConfig->setType("fragout");
  144. ormConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  145. ormConfig->setStructName("OUT");
  146. }
  147. }
  148. else
  149. {
  150. ormConfig = (Var*)LangElement::find("ORMConfig");
  151. if (!ormConfig) ormConfig = new Var("ORMConfig", "float4");
  152. }
  153. Var *matInfoFlags = new Var;
  154. matInfoFlags->setType( "float" );
  155. matInfoFlags->setName( "matInfoFlags" );
  156. matInfoFlags->uniform = true;
  157. matInfoFlags->constSortPos = cspPotentialPrimitive;
  158. output = new GenOp( " @.r = @;\r\n", ormConfig, matInfoFlags );
  159. }
  160. U32 ORMConfigVarsHLSL::getOutputTargets(const MaterialFeatureData& fd) const
  161. {
  162. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  163. }
  164. void ORMConfigVarsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  165. {
  166. MultiLine* meta = new MultiLine;
  167. Var* ormConfig;
  168. if (fd.features[MFT_isDeferred])
  169. {
  170. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  171. if (!ormConfig)
  172. {
  173. // create material var
  174. ormConfig = new Var;
  175. ormConfig->setType("fragout");
  176. ormConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  177. ormConfig->setStructName("OUT");
  178. }
  179. }
  180. else
  181. {
  182. ormConfig = (Var*)LangElement::find("ORMConfig");
  183. if (!ormConfig) ormConfig = new Var("ORMConfig", "float4");
  184. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  185. }
  186. Var *metalness = new Var("metalness", "float");
  187. metalness->uniform = true;
  188. metalness->constSortPos = cspPotentialPrimitive;
  189. Var *roughness = new Var("roughness", "float");
  190. roughness->uniform = true;
  191. roughness->constSortPos = cspPotentialPrimitive;
  192. //matinfo.g slot reserved for AO later
  193. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", ormConfig));
  194. meta->addStatement(new GenOp(" @.b = @;\r\n", ormConfig, roughness));
  195. if (fd.features[MFT_InvertRoughness])
  196. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", roughness, roughness));
  197. meta->addStatement(new GenOp(" @.a = @;\r\n", ormConfig, metalness));
  198. output = meta;
  199. }
  200. U32 GlowMapHLSL::getOutputTargets(const MaterialFeatureData& fd) const
  201. {
  202. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  203. }
  204. //deferred emissive
  205. void GlowMapHLSL::processPix(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  206. {
  207. Var* texCoord = getInTexCoord("texCoord", "float2", componentList);
  208. // create texture var
  209. Var* glowMap = new Var;
  210. glowMap->setType("SamplerState");
  211. glowMap->setName("glowMap");
  212. glowMap->uniform = true;
  213. glowMap->sampler = true;
  214. glowMap->constNum = Var::getTexUnitNum();
  215. Var* glowMapTex = new Var;
  216. glowMapTex->setName("glowMapTex");
  217. glowMapTex->setType("Texture2D");
  218. glowMapTex->uniform = true;
  219. glowMapTex->texture = true;
  220. glowMapTex->constNum = glowMap->constNum;
  221. LangElement* texOp = new GenOp("@.Sample(@, @)", glowMapTex, glowMap, texCoord);
  222. Var* glowMul = new Var("glowMul", "float");
  223. glowMul->uniform = true;
  224. glowMul->constSortPos = cspPotentialPrimitive;
  225. Var *targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  226. if (fd.features[MFT_isDeferred])
  227. {
  228. targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  229. if (!targ)
  230. {
  231. // create scene color target var
  232. targ = new Var;
  233. targ->setType("fragout");
  234. targ->setName(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  235. targ->setStructName("OUT");
  236. output = new GenOp("@ = float4(@.rgb*@,0);", targ, texOp, glowMul);
  237. }
  238. else
  239. {
  240. output = new GenOp("@ += float4(@.rgb*@,0);", targ, texOp, glowMul);
  241. }
  242. }
  243. else
  244. {
  245. output = new GenOp("@ += float4(@.rgb*@,@.a);", targ, texOp, glowMul, targ);
  246. }
  247. }
  248. ShaderFeature::Resources GlowMapHLSL::getResources(const MaterialFeatureData& fd)
  249. {
  250. Resources res;
  251. res.numTex = 1;
  252. res.numTexReg = 1;
  253. return res;
  254. }
  255. void GlowMapHLSL::setTexData(Material::StageData& stageDat,
  256. const MaterialFeatureData& fd,
  257. RenderPassData& passData,
  258. U32& texIndex)
  259. {
  260. GFXTextureObject* tex = stageDat.getTex(MFT_GlowMap);
  261. if (tex)
  262. {
  263. passData.mTexType[texIndex] = Material::Standard;
  264. passData.mSamplerNames[texIndex] = "glowMap";
  265. passData.mTexSlot[texIndex++].texObject = tex;
  266. }
  267. }