paraboloidHLSL.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/paraboloidHLSL.h"
  24. #include "lighting/lightInfo.h"
  25. #include "materials/sceneData.h"
  26. #include "materials/materialFeatureTypes.h"
  27. #include "materials/materialFeatureData.h"
  28. #include "gfx/gfxShader.h"
  29. void ParaboloidVertTransformHLSL::processVert( Vector<ShaderComponent*> &componentList,
  30. const MaterialFeatureData &fd )
  31. {
  32. MultiLine *meta = new MultiLine;
  33. // First check for an input position from a previous feature
  34. // then look for the default vertex position.
  35. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  36. if ( !inPosition )
  37. inPosition = (Var*)LangElement::find( "position" );
  38. const bool isSinglePass = fd.features[ MFT_IsSinglePassParaboloid ];
  39. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  40. // Grab connector out position.
  41. Var *outPosition = connectComp->getElement( RT_POSITION );
  42. outPosition->setName( "hpos" );
  43. outPosition->setStructName( "OUT" );
  44. // Get the atlas scale.
  45. Var *atlasScale = new Var;
  46. atlasScale->setType( "float2" );
  47. atlasScale->setName( "atlasScale" );
  48. atlasScale->uniform = true;
  49. atlasScale->constSortPos = cspPass;
  50. // Transform into camera space
  51. Var *worldViewOnly = getWorldView( componentList, fd.features[MFT_UseInstancing], meta );
  52. // So what we're doing here is transforming into camera space, and
  53. // then directly manipulate into shadowmap space.
  54. //
  55. // http://www.gamedev.net/reference/articles/article2308.asp
  56. // Swizzle z and y post-transform
  57. meta->addStatement( new GenOp( " @ = mul(@, float4(@.xyz,1)).xzyw;\r\n", outPosition, worldViewOnly, inPosition ) );
  58. meta->addStatement( new GenOp( " float L = length(@.xyz);\r\n", outPosition ) );
  59. if ( isSinglePass )
  60. {
  61. // Flip the z in the back case
  62. Var *outIsBack = connectComp->getElement( RT_TEXCOORD );
  63. outIsBack->setType( "float" );
  64. outIsBack->setName( "isBack" );
  65. outIsBack->setStructName( "OUT" );
  66. meta->addStatement( new GenOp( " bool isBack = @.z < 0.0;\r\n", outPosition ) );
  67. meta->addStatement( new GenOp( " @ = isBack ? -1.0 : 1.0;\r\n", outIsBack ) );
  68. meta->addStatement( new GenOp( " if ( isBack ) @.z = [email protected];\r\n", outPosition, outPosition ) );
  69. }
  70. meta->addStatement( new GenOp( " @ /= L;\r\n", outPosition ) );
  71. meta->addStatement( new GenOp( " @.z = @.z + 1.0;\r\n", outPosition, outPosition ) );
  72. meta->addStatement( new GenOp( " @.xy /= @.z;\r\n", outPosition, outPosition ) );
  73. // Get the light parameters.
  74. Var *lightParams = new Var;
  75. lightParams->setType( "float4" );
  76. lightParams->setName( "lightParams" );
  77. lightParams->uniform = true;
  78. lightParams->constSortPos = cspPass;
  79. // TODO: If we change other shadow shaders to write out
  80. // linear depth, than fix this as well!
  81. //
  82. // (L - zNear)/(lightParams.x - zNear);
  83. //
  84. meta->addStatement( new GenOp( " @.z = L / @.x;\r\n", outPosition, lightParams ) );
  85. meta->addStatement( new GenOp( " @.w = 1.0;\r\n", outPosition ) );
  86. // Pass unmodified to pixel shader to allow it to clip properly.
  87. Var *outPosXY = connectComp->getElement( RT_TEXCOORD );
  88. outPosXY->setType( "float2" );
  89. outPosXY->setName( "posXY" );
  90. outPosXY->setStructName( "OUT" );
  91. meta->addStatement( new GenOp( " @ = @.xy;\r\n", outPosXY, outPosition ) );
  92. // Scale and offset so it shows up in the atlas properly.
  93. meta->addStatement( new GenOp( " @.xy *= @.xy;\r\n", outPosition, atlasScale ) );
  94. if ( isSinglePass )
  95. meta->addStatement( new GenOp( " @.x += isBack ? 0.5 : -0.5;\r\n", outPosition ) );
  96. else
  97. {
  98. Var *atlasOffset = new Var;
  99. atlasOffset->setType( "float2" );
  100. atlasOffset->setName( "atlasXOffset" );
  101. atlasOffset->uniform = true;
  102. atlasOffset->constSortPos = cspPass;
  103. meta->addStatement( new GenOp( " @.xy += @;\r\n", outPosition, atlasOffset ) );
  104. }
  105. output = meta;
  106. }
  107. void ParaboloidVertTransformHLSL::processPix( Vector<ShaderComponent*> &componentList,
  108. const MaterialFeatureData &fd )
  109. {
  110. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  111. MultiLine *meta = new MultiLine;
  112. const bool isSinglePass = fd.features[ MFT_IsSinglePassParaboloid ];
  113. if ( isSinglePass )
  114. {
  115. // Cull things on the back side of the map.
  116. Var *isBack = connectComp->getElement( RT_TEXCOORD );
  117. isBack->setName( "isBack" );
  118. isBack->setStructName( "IN" );
  119. isBack->setType( "float" );
  120. meta->addStatement( new GenOp( " clip( abs( @ ) - 0.999 );\r\n", isBack ) );
  121. }
  122. // Cull pixels outside of the valid paraboloid.
  123. Var *posXY = connectComp->getElement( RT_TEXCOORD );
  124. posXY->setName( "posXY" );
  125. posXY->setStructName( "IN" );
  126. posXY->setType( "float2" );
  127. meta->addStatement( new GenOp( " clip( 1.0 - abs(@.x) );\r\n", posXY ) );
  128. output = meta;
  129. }
  130. ShaderFeature::Resources ParaboloidVertTransformHLSL::getResources( const MaterialFeatureData &fd )
  131. {
  132. Resources temp;
  133. temp.numTexReg = 2;
  134. return temp;
  135. }