deferredShadingFeaturesHLSL.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Specular Map -> Blue of Material Buffer ( greyscaled )
  35. // Gloss Map (Alpha Channel of Specular Map) -> Alpha ( Spec Power ) of Material Info Buffer.
  36. void DeferredSpecMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  37. {
  38. // Get the texture coord.
  39. Var *texCoord = getInTexCoord( "texCoord", "float2", componentList );
  40. // search for color var
  41. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  42. MultiLine * meta = new MultiLine;
  43. if ( !material )
  44. {
  45. // create color var
  46. material = new Var;
  47. material->setType( "fragout" );
  48. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  49. material->setStructName( "OUT" );
  50. }
  51. // create texture var
  52. Var *specularMap = new Var;
  53. specularMap->setType( "SamplerState" );
  54. specularMap->setName( "specularMap" );
  55. specularMap->uniform = true;
  56. specularMap->sampler = true;
  57. specularMap->constNum = Var::getTexUnitNum();
  58. Var* specularMapTex = new Var;
  59. specularMapTex->setName("specularMapTex");
  60. specularMapTex->setType("Texture2D");
  61. specularMapTex->uniform = true;
  62. specularMapTex->texture = true;
  63. specularMapTex->constNum = specularMap->constNum;
  64. LangElement *texOp = new GenOp(" @.Sample(@, @)", specularMapTex, specularMap, texCoord);
  65. Var *specularColor = (Var*)LangElement::find("specularColor");
  66. if (!specularColor) specularColor = new Var("specularColor", "float4");
  67. Var *metalness = (Var*)LangElement::find("metalness");
  68. if (!metalness) metalness = new Var("metalness", "float");
  69. Var *smoothness = (Var*)LangElement::find("smoothness");
  70. if (!smoothness) smoothness = new Var("smoothness", "float");
  71. meta->addStatement(new GenOp(" @ = @.r;\r\n", new DecOp(smoothness), texOp));
  72. meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(metalness), texOp));
  73. if (fd.features[MFT_InvertSmoothness])
  74. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  75. meta->addStatement(new GenOp(" @ = @.ggga;\r\n", new DecOp(specularColor), texOp));
  76. meta->addStatement(new GenOp(" @.bga = float3(@,@.g,@);\r\n", material, smoothness, specularColor, metalness));
  77. output = meta;
  78. }
  79. ShaderFeature::Resources DeferredSpecMapHLSL::getResources( const MaterialFeatureData &fd )
  80. {
  81. Resources res;
  82. res.numTex = 1;
  83. res.numTexReg = 1;
  84. return res;
  85. }
  86. void DeferredSpecMapHLSL::setTexData( Material::StageData &stageDat,
  87. const MaterialFeatureData &fd,
  88. RenderPassData &passData,
  89. U32 &texIndex )
  90. {
  91. GFXTextureObject *tex = stageDat.getTex( MFT_SpecularMap );
  92. if ( tex )
  93. {
  94. passData.mTexType[ texIndex ] = Material::Standard;
  95. passData.mSamplerNames[ texIndex ] = "specularMap";
  96. passData.mTexSlot[ texIndex++ ].texObject = tex;
  97. }
  98. }
  99. void DeferredSpecMapHLSL::processVert( Vector<ShaderComponent*> &componentList,
  100. const MaterialFeatureData &fd )
  101. {
  102. MultiLine *meta = new MultiLine;
  103. getOutTexCoord( "texCoord",
  104. "float2",
  105. fd.features[MFT_TexAnim],
  106. meta,
  107. componentList );
  108. output = meta;
  109. }
  110. // Material Info Flags -> Red ( Flags ) of Material Info Buffer.
  111. void DeferredMatInfoFlagsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  112. {
  113. // search for material var
  114. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  115. if ( !material )
  116. {
  117. // create material var
  118. material = new Var;
  119. material->setType( "fragout" );
  120. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  121. material->setStructName( "OUT" );
  122. }
  123. Var *matInfoFlags = new Var;
  124. matInfoFlags->setType( "float" );
  125. matInfoFlags->setName( "matInfoFlags" );
  126. matInfoFlags->uniform = true;
  127. matInfoFlags->constSortPos = cspPotentialPrimitive;
  128. output = new GenOp( " @.r = @;\r\n", material, matInfoFlags );
  129. }
  130. // Spec Strength -> Blue Channel of Material Info Buffer.
  131. // Spec Power -> Alpha Channel ( of Material Info Buffer.
  132. void DeferredSpecVarsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  133. {
  134. // search for material var
  135. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  136. if ( !material )
  137. {
  138. // create material var
  139. material = new Var;
  140. material->setType( "fragout" );
  141. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  142. material->setStructName( "OUT" );
  143. }
  144. Var *metalness = new Var("metalness", "float");
  145. metalness->uniform = true;
  146. metalness->constSortPos = cspPotentialPrimitive;
  147. Var *smoothness = new Var("smoothness", "float");
  148. smoothness->uniform = true;
  149. smoothness->constSortPos = cspPotentialPrimitive;
  150. MultiLine * meta = new MultiLine;
  151. //matinfo.g slot reserved for AO later
  152. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  153. meta->addStatement(new GenOp(" @.b = @;\r\n", material, smoothness));
  154. if (fd.features[MFT_InvertSmoothness])
  155. meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
  156. meta->addStatement(new GenOp(" @.a = @;\r\n", material, metalness));
  157. output = meta;
  158. }