deferredShadingFeaturesGLSL.cpp 10 KB

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