pixSpecularHLSL.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. lmColor = new GenOp( "tex2D(@, @)", lightMap, lmCoord );
  69. }
  70. final = new GenOp( "@ * float4(@.rgb,0)", specMul, lmColor );
  71. }
  72. // If we have a normal map then mask the specular
  73. if ( fd.features[MFT_SpecularMap] )
  74. {
  75. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  76. if (specularColor)
  77. final = new GenOp( "@ * @", final, specularColor );
  78. }
  79. else if ( fd.features[MFT_NormalMap] && !fd.features[MFT_IsDXTnm] )
  80. {
  81. Var *bumpColor = (Var*)LangElement::find( "bumpNormal" );
  82. final = new GenOp( "@ * @.a", final, bumpColor );
  83. }
  84. // Add the specular to the final color.
  85. // search for color var
  86. Var *color = (Var*)LangElement::find( "col" );
  87. meta->addStatement( new GenOp( " @.rgb += ( @ ).rgb;\r\n", color, final ) );
  88. output = meta;
  89. }
  90. ShaderFeature::Resources PixelSpecularHLSL::getResources( const MaterialFeatureData &fd )
  91. {
  92. Resources res;
  93. return res;
  94. }
  95. void SpecularMapHLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  96. {
  97. MultiLine *meta = new MultiLine;
  98. // Add the texture coords.
  99. getOutTexCoord("texCoord",
  100. "float2",
  101. true,
  102. fd.features[MFT_TexAnim],
  103. meta,
  104. componentList);
  105. output = meta;
  106. }
  107. void SpecularMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  108. {
  109. // Get the texture coord.
  110. Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList );
  111. // create texture var
  112. Var *specularMap = new Var;
  113. specularMap->setType( "sampler2D" );
  114. specularMap->setName( "specularMap" );
  115. specularMap->uniform = true;
  116. specularMap->sampler = true;
  117. specularMap->constNum = Var::getTexUnitNum();
  118. LangElement *texOp = new GenOp( "tex2D(@, @)", specularMap, texCoord );
  119. Var *specularColor = new Var( "specularColor", "float4" );
  120. output = new GenOp( " @ = @;\r\n", new DecOp( specularColor ), texOp );
  121. }
  122. ShaderFeature::Resources SpecularMapHLSL::getResources( const MaterialFeatureData &fd )
  123. {
  124. Resources res;
  125. res.numTex = 1;
  126. return res;
  127. }
  128. void SpecularMapHLSL::setTexData( Material::StageData &stageDat,
  129. const MaterialFeatureData &fd,
  130. RenderPassData &passData,
  131. U32 &texIndex )
  132. {
  133. GFXTextureObject *tex = stageDat.getTex( MFT_SpecularMap );
  134. if ( tex )
  135. {
  136. passData.mTexType[ texIndex ] = Material::Standard;
  137. passData.mSamplerNames[ texIndex ] = "specularMap";
  138. passData.mTexSlot[ texIndex++ ].texObject = tex;
  139. }
  140. }