depthGLSL.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/depthGLSL.h"
  24. #include "materials/materialFeatureTypes.h"
  25. void EyeSpaceDepthOutGLSL::processVert( Vector<ShaderComponent*> &componentList,
  26. const MaterialFeatureData &fd )
  27. {
  28. MultiLine *meta = new MultiLine;
  29. output = meta;
  30. // grab output
  31. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  32. Var *outWSEyeVec = connectComp->getElement( RT_TEXCOORD );
  33. outWSEyeVec->setName( "outWSEyeVec" );
  34. // grab incoming vert position
  35. Var *wsPosition = new Var( "depthPos", "vec3" );
  36. getWsPosition( componentList, fd.features[MFT_UseInstancing], meta, new DecOp( wsPosition ) );
  37. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  38. if( !eyePos )
  39. {
  40. eyePos = new Var;
  41. eyePos->setType("vec3");
  42. eyePos->setName("eyePosWorld");
  43. eyePos->uniform = true;
  44. eyePos->constSortPos = cspPass;
  45. }
  46. meta->addStatement( new GenOp( " @ = vec4( @.xyz - @, 1 );\r\n", outWSEyeVec, wsPosition, eyePos ) );
  47. }
  48. void EyeSpaceDepthOutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  49. const MaterialFeatureData &fd )
  50. {
  51. MultiLine *meta = new MultiLine;
  52. // grab connector position
  53. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  54. Var *wsEyeVec = connectComp->getElement( RT_TEXCOORD );
  55. wsEyeVec->setName( "outWSEyeVec" );
  56. wsEyeVec->setType( "vec4" );
  57. wsEyeVec->mapsToSampler = false;
  58. wsEyeVec->uniform = false;
  59. // get shader constants
  60. Var *vEye = new Var;
  61. vEye->setType("vec3");
  62. vEye->setName("vEye");
  63. vEye->uniform = true;
  64. vEye->constSortPos = cspPass;
  65. // Expose the depth to the depth format feature
  66. Var *depthOut = new Var;
  67. depthOut->setType("float");
  68. depthOut->setName(getOutputVarName());
  69. LangElement *depthOutDecl = new DecOp( depthOut );
  70. meta->addStatement( new GenOp( " @ = dot(@, (@.xyz / @.w));\r\n", depthOutDecl, vEye, wsEyeVec, wsEyeVec ) );
  71. // If there isn't an output conditioner for the pre-pass, than just write
  72. // out the depth to rgba and return.
  73. if( !fd.features[MFT_PrePassConditioner] )
  74. meta->addStatement( new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(@)", depthOut ), Material::None ) ) );
  75. output = meta;
  76. }
  77. ShaderFeature::Resources EyeSpaceDepthOutGLSL::getResources( const MaterialFeatureData &fd )
  78. {
  79. Resources temp;
  80. // Passing from VS->PS:
  81. // - world space position (wsPos)
  82. temp.numTexReg = 1;
  83. return temp;
  84. }
  85. void DepthOutGLSL::processVert( Vector<ShaderComponent*> &componentList,
  86. const MaterialFeatureData &fd )
  87. {
  88. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  89. // Grab the output vert.
  90. Var *outPosition = (Var*)LangElement::find( "gl_Position" );
  91. // Grab our output depth.
  92. Var *outDepth = connectComp->getElement( RT_TEXCOORD );
  93. outDepth->setName( "outDepth" );
  94. outDepth->setType( "float" );
  95. output = new GenOp( " @ = @.z / @.w;\r\n", outDepth, outPosition, outPosition );
  96. }
  97. void DepthOutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  98. const MaterialFeatureData &fd )
  99. {
  100. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  101. // grab connector position
  102. Var *depthVar = connectComp->getElement( RT_TEXCOORD );
  103. depthVar->setName( "outDepth" );
  104. depthVar->setType( "float" );
  105. depthVar->mapsToSampler = false;
  106. depthVar->uniform = false;
  107. /*
  108. // Expose the depth to the depth format feature
  109. Var *depthOut = new Var;
  110. depthOut->setType("float");
  111. depthOut->setName(getOutputVarName());
  112. */
  113. LangElement *depthOut = new GenOp( "vec4( @, @ * @, 0, 1 )", depthVar, depthVar, depthVar );
  114. output = new GenOp( " @;\r\n", assignColor( depthOut, Material::None ) );
  115. }
  116. ShaderFeature::Resources DepthOutGLSL::getResources( const MaterialFeatureData &fd )
  117. {
  118. // We pass the depth to the pixel shader.
  119. Resources temp;
  120. temp.numTexReg = 1;
  121. return temp;
  122. }