paraboloidHLSL.cpp 6.4 KB

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