deferredShadingFeaturesHLSL.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/renderPrePassMgr.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", true, 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( "sampler2D" );
  54. specularMap->setName( "specularMap" );
  55. specularMap->uniform = true;
  56. specularMap->sampler = true;
  57. specularMap->constNum = Var::getTexUnitNum();
  58. Var* specularMapTex = NULL;
  59. if (mIsDirect3D11)
  60. {
  61. specularMap->setType("SamplerState");
  62. specularMapTex = new Var;
  63. specularMapTex->setName("specularMapTex");
  64. specularMapTex->setType("Texture2D");
  65. specularMapTex->uniform = true;
  66. specularMapTex->texture = true;
  67. specularMapTex->constNum = specularMap->constNum;
  68. }
  69. //matinfo.g slot reserved for AO later
  70. Var* specColor = new Var;
  71. specColor->setName("specColor");
  72. specColor->setType("float4");
  73. LangElement *specColorElem = new DecOp(specColor);
  74. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  75. //sample specular map
  76. if (mIsDirect3D11)
  77. meta->addStatement(new GenOp(" @ = @.Sample(@, @);\r\n", specColorElem, specularMapTex, specularMap, texCoord));
  78. else
  79. meta->addStatement(new GenOp(" @ = tex2D(@, @);\r\n", specColorElem, specularMap, texCoord));
  80. meta->addStatement(new GenOp(" @.b = dot(@.rgb, float3(0.3, 0.59, 0.11));\r\n", material, specColor));
  81. meta->addStatement(new GenOp(" @.a = @.a;\r\n", material, specColor));
  82. output = meta;
  83. }
  84. ShaderFeature::Resources DeferredSpecMapHLSL::getResources( const MaterialFeatureData &fd )
  85. {
  86. Resources res;
  87. res.numTex = 1;
  88. res.numTexReg = 1;
  89. return res;
  90. }
  91. void DeferredSpecMapHLSL::setTexData( Material::StageData &stageDat,
  92. const MaterialFeatureData &fd,
  93. RenderPassData &passData,
  94. U32 &texIndex )
  95. {
  96. GFXTextureObject *tex = stageDat.getTex( MFT_SpecularMap );
  97. if ( tex )
  98. {
  99. passData.mTexType[ texIndex ] = Material::Standard;
  100. passData.mSamplerNames[ texIndex ] = "specularMap";
  101. passData.mTexSlot[ texIndex++ ].texObject = tex;
  102. }
  103. }
  104. void DeferredSpecMapHLSL::processVert( Vector<ShaderComponent*> &componentList,
  105. const MaterialFeatureData &fd )
  106. {
  107. MultiLine *meta = new MultiLine;
  108. getOutTexCoord( "texCoord",
  109. "float2",
  110. true,
  111. fd.features[MFT_TexAnim],
  112. meta,
  113. componentList );
  114. output = meta;
  115. }
  116. // Material Info Flags -> Red ( Flags ) of Material Info Buffer.
  117. void DeferredMatInfoFlagsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  118. {
  119. // search for material var
  120. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  121. if ( !material )
  122. {
  123. // create material var
  124. material = new Var;
  125. material->setType( "fragout" );
  126. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  127. material->setStructName( "OUT" );
  128. }
  129. Var *matInfoFlags = new Var;
  130. matInfoFlags->setType( "float" );
  131. matInfoFlags->setName( "matInfoFlags" );
  132. matInfoFlags->uniform = true;
  133. matInfoFlags->constSortPos = cspPotentialPrimitive;
  134. output = new GenOp( " @.r = @;\r\n", material, matInfoFlags );
  135. }
  136. // Spec Strength -> Blue Channel of Material Info Buffer.
  137. // Spec Power -> Alpha Channel ( of Material Info Buffer.
  138. void DeferredSpecVarsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  139. {
  140. // search for material var
  141. Var *material = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  142. if ( !material )
  143. {
  144. // create material var
  145. material = new Var;
  146. material->setType( "fragout" );
  147. material->setName( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  148. material->setStructName( "OUT" );
  149. }
  150. Var *specStrength = new Var;
  151. specStrength->setType( "float" );
  152. specStrength->setName( "specularStrength" );
  153. specStrength->uniform = true;
  154. specStrength->constSortPos = cspPotentialPrimitive;
  155. Var *specPower = new Var;
  156. specPower->setType( "float" );
  157. specPower->setName( "specularPower" );
  158. specPower->uniform = true;
  159. specPower->constSortPos = cspPotentialPrimitive;
  160. MultiLine * meta = new MultiLine;
  161. //matinfo.g slot reserved for AO later
  162. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  163. meta->addStatement(new GenOp(" @.a = @/128;\r\n", material, specPower));
  164. meta->addStatement(new GenOp(" @.b = @/5;\r\n", material, specStrength));
  165. output = meta;
  166. }
  167. // Black -> Blue and Alpha of matinfo Buffer (representing no specular), White->G (representing No AO)
  168. void DeferredEmptySpecHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  169. {
  170. // search for material var
  171. Var *material = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  172. if (!material)
  173. {
  174. // create color var
  175. material = new Var;
  176. material->setType("fragout");
  177. material->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
  178. material->setStructName("OUT");
  179. }
  180. MultiLine * meta = new MultiLine;
  181. //matinfo.g slot reserved for AO later
  182. meta->addStatement(new GenOp(" @.g = 1.0;\r\n", material));
  183. meta->addStatement(new GenOp(" @.ba = 0.0;\r\n", material));
  184. output = meta;
  185. }