depthHLSL.cpp 6.2 KB

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