paraboloidHLSL.cpp 6.4 KB

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