depthHLSL.cpp 6.1 KB

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