paraboloidGLSL.cpp 6.4 KB

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