pixSpecularGLSL.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/GLSL/pixSpecularGLSL.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. PixelSpecularGLSL::PixelSpecularGLSL()
  31. : mDep(ShaderGen::smCommonShaderPath + String("/gl/lighting.glsl" ))
  32. {
  33. addDependency( &mDep );
  34. }
  35. void PixelSpecularGLSL::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 PixelSpecularGLSL::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. lmColor = new GenOp( "texture(@, @)", lightMap, lmCoord );
  70. }
  71. final = new GenOp( "@ * vec4(@.rgb,0)", specMul, lmColor );
  72. }
  73. // If we have a normal map then mask the specular
  74. if ( fd.features[MFT_SpecularMap] )
  75. {
  76. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  77. if (specularColor)
  78. final = new GenOp( "@ * @", final, specularColor );
  79. }
  80. else if ( fd.features[MFT_NormalMap] && !fd.features[MFT_IsBC3nm] && !fd.features[MFT_IsBC5nm])
  81. {
  82. Var *bumpColor = (Var*)LangElement::find( "bumpNormal" );
  83. final = new GenOp( "@ * @.a", final, bumpColor );
  84. }
  85. // Add the specular to the final color.
  86. // search for color var
  87. Var *color = (Var*)LangElement::find( "col" );
  88. meta->addStatement( new GenOp( " @.rgb += ( @ ).rgb;\r\n", color, final ) );
  89. output = meta;
  90. }
  91. ShaderFeature::Resources PixelSpecularGLSL::getResources( const MaterialFeatureData &fd )
  92. {
  93. Resources res;
  94. return res;
  95. }
  96. void SpecularMapGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  97. {
  98. MultiLine *meta = new MultiLine;
  99. // Add the texture coords.
  100. getOutTexCoord("texCoord",
  101. "vec2",
  102. fd.features[MFT_TexAnim],
  103. meta,
  104. componentList);
  105. output = meta;
  106. }
  107. void SpecularMapGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  108. {
  109. // Get the texture coord.
  110. Var *texCoord = getInTexCoord( "texCoord", "vec2", 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( "texture(@, @)", specularMap, texCoord );
  119. Var *specularColor = new Var( "specularColor", "vec4" );
  120. output = new GenOp( " @ = @;\r\n", new DecOp( specularColor ), texOp );
  121. }
  122. ShaderFeature::Resources SpecularMapGLSL::getResources( const MaterialFeatureData &fd )
  123. {
  124. Resources res;
  125. res.numTex = 1;
  126. return res;
  127. }
  128. void SpecularMapGLSL::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. }