pixSpecularHLSL.cpp 6.2 KB

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