pixSpecularHLSL.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "shaderGen/HLSL/pixSpecularHLSL.h"
  24. #include "materials/processedMaterial.h"
  25. #include "materials/materialFeatureTypes.h"
  26. #include "shaderGen/shaderOp.h"
  27. #include "shaderGen/shaderGenVars.h"
  28. #include "gfx/gfxStructs.h"
  29. PixelSpecularHLSL::PixelSpecularHLSL()
  30. : mDep( "shaders/common/lighting.hlsl" )
  31. {
  32. addDependency( &mDep );
  33. }
  34. void PixelSpecularHLSL::processVert( Vector<ShaderComponent*> &componentList,
  35. const MaterialFeatureData &fd )
  36. {
  37. AssertFatal( fd.features[MFT_RTLighting],
  38. "PixelSpecularHLSL requires RTLighting to be enabled!" );
  39. // Nothing to do here... MFT_RTLighting should have
  40. // taken care of passing everything to the pixel shader.
  41. }
  42. void PixelSpecularHLSL::processPix( Vector<ShaderComponent*> &componentList,
  43. const MaterialFeatureData &fd )
  44. {
  45. AssertFatal( fd.features[MFT_RTLighting],
  46. "PixelSpecularHLSL requires RTLighting to be enabled!" );
  47. // RTLighting should have spit out the 4 specular
  48. // powers for the 4 potential lights on this pass.
  49. //
  50. // This can sometimes be NULL if RTLighting skips out
  51. // on us for lightmaps or missing normals.
  52. Var *specular = (Var*)LangElement::find( "specular" );
  53. if ( !specular )
  54. return;
  55. MultiLine *meta = new MultiLine;
  56. LangElement *specMul = new GenOp( "@", specular );
  57. LangElement *final = specMul;
  58. // mask out with lightmap if present
  59. if ( fd.features[MFT_LightMap] )
  60. {
  61. LangElement *lmColor = NULL;
  62. // find lightmap color
  63. lmColor = LangElement::find( "lmColor" );
  64. if ( !lmColor )
  65. {
  66. LangElement * lightMap = LangElement::find( "lightMap" );
  67. LangElement * lmCoord = LangElement::find( "texCoord2" );
  68. LangElement * lightMapTex = LangElement::find("lightMapTex"); //used only DX11 shaders
  69. if (lightMapTex)
  70. lmColor = new GenOp("@.Sample(@, @)", lightMapTex, lightMap, lmCoord);
  71. else
  72. lmColor = new GenOp("tex2D(@, @)", lightMap, lmCoord);
  73. }
  74. final = new GenOp( "@ * float4(@.rgb,0)", specMul, lmColor );
  75. }
  76. // If we have a normal map then mask the specular
  77. if ( fd.features[MFT_SpecularMap] )
  78. {
  79. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  80. if (specularColor)
  81. final = new GenOp( "@ * @", final, specularColor );
  82. }
  83. else if ( fd.features[MFT_NormalMap] && !fd.features[MFT_IsDXTnm] )
  84. {
  85. Var *bumpColor = (Var*)LangElement::find( "bumpNormal" );
  86. final = new GenOp( "@ * @.a", final, bumpColor );
  87. }
  88. // Add the specular to the final color.
  89. // search for color var
  90. Var *color = (Var*)LangElement::find( "col" );
  91. meta->addStatement( new GenOp( " @.rgb += ( @ ).rgb;\r\n", color, final ) );
  92. output = meta;
  93. }
  94. ShaderFeature::Resources PixelSpecularHLSL::getResources( const MaterialFeatureData &fd )
  95. {
  96. Resources res;
  97. return res;
  98. }
  99. void SpecularMapHLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  100. {
  101. MultiLine *meta = new MultiLine;
  102. // Add the texture coords.
  103. getOutTexCoord("texCoord",
  104. "float2",
  105. true,
  106. fd.features[MFT_TexAnim],
  107. meta,
  108. componentList);
  109. output = meta;
  110. }
  111. void SpecularMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  112. {
  113. // Get the texture coord.
  114. Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList );
  115. // create texture var
  116. Var *specularMap = new Var;
  117. specularMap->setType( "sampler2D" );
  118. specularMap->setName( "specularMap" );
  119. specularMap->uniform = true;
  120. specularMap->sampler = true;
  121. specularMap->constNum = Var::getTexUnitNum();
  122. Var *specularMapTex = NULL;
  123. if (mIsDirect3D11)
  124. {
  125. specularMap->setType("SamplerState");
  126. specularMapTex = new Var;
  127. specularMapTex->setName("specularMapTex");
  128. specularMapTex->setType("Texture2D");
  129. specularMapTex->uniform = true;
  130. specularMapTex->texture = true;
  131. specularMapTex->constNum = specularMap->constNum;
  132. }
  133. else
  134. {
  135. specularMap->setType("sampler2D");
  136. }
  137. LangElement *texOp = NULL;
  138. if (specularMapTex)
  139. texOp = new GenOp("@.Sample(@, @)", specularMapTex, specularMap, texCoord);
  140. else
  141. texOp = new GenOp("tex2D(@, @)", specularMap, texCoord);
  142. Var *specularColor = new Var("specularColor", "float4");
  143. output = new GenOp(" @ = @;\r\n", new DecOp(specularColor), texOp);
  144. }
  145. ShaderFeature::Resources SpecularMapHLSL::getResources( const MaterialFeatureData &fd )
  146. {
  147. Resources res;
  148. res.numTex = 1;
  149. return res;
  150. }
  151. void SpecularMapHLSL::setTexData( Material::StageData &stageDat,
  152. const MaterialFeatureData &fd,
  153. RenderPassData &passData,
  154. U32 &texIndex )
  155. {
  156. GFXTextureObject *tex = stageDat.getTex( MFT_SpecularMap );
  157. if ( tex )
  158. {
  159. passData.mTexType[ texIndex ] = Material::Standard;
  160. passData.mSamplerNames[ texIndex ] = "specularMap";
  161. passData.mTexSlot[ texIndex++ ].texObject = tex;
  162. }
  163. }