paraboloidGLSL.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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( " @ = vec4(@ * vec4(@.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( "outIsBack" );
  64. meta->addStatement( new GenOp( " bool isBack = @.z < 0.0;\r\n", outPosition ) );
  65. meta->addStatement( new GenOp( " @ = isBack ? -1.0 : 1.0;\r\n", outIsBack ) );
  66. meta->addStatement( new GenOp( " if ( isBack ) @.z = [email protected];\r\n", outPosition, outPosition ) );
  67. }
  68. meta->addStatement( new GenOp( " @ /= L;\r\n", outPosition ) );
  69. meta->addStatement( new GenOp( " @.z = @.z + 1.0;\r\n", outPosition, outPosition ) );
  70. meta->addStatement( new GenOp( " @.xy /= @.z;\r\n", outPosition, outPosition ) );
  71. // Get the light parameters.
  72. Var *lightParams = new Var;
  73. lightParams->setType( "vec4" );
  74. lightParams->setName( "lightParams" );
  75. lightParams->uniform = true;
  76. lightParams->constSortPos = cspPass;
  77. // TODO: If we change other shadow shaders to write out
  78. // linear depth, than fix this as well!
  79. //
  80. // (L - 1.0)/(lightParams.x - 1.0);
  81. //
  82. meta->addStatement( new GenOp( " @.z = L / @.x;\r\n", outPosition, lightParams ) );
  83. meta->addStatement( new GenOp( " @.w = 1.0;\r\n", outPosition ) );
  84. // Pass unmodified to pixel shader to allow it to clip properly.
  85. Var *outPosXY = connectComp->getElement( RT_TEXCOORD );
  86. outPosXY->setType( "vec2" );
  87. outPosXY->setName( "outPosXY" );
  88. meta->addStatement( new GenOp( " @ = @.xy;\r\n", outPosXY, outPosition ) );
  89. // Scale and offset so it shows up in the atlas properly.
  90. meta->addStatement( new GenOp( " @.xy *= @.xy;\r\n", outPosition, atlasScale ) );
  91. if ( isSinglePass )
  92. meta->addStatement( new GenOp( " @.x += isBack ? 0.5 : -0.5;\r\n", outPosition ) );
  93. else
  94. {
  95. Var *atlasOffset = new Var;
  96. atlasOffset->setType( "vec2" );
  97. atlasOffset->setName( "atlasXOffset" );
  98. atlasOffset->uniform = true;
  99. atlasOffset->constSortPos = cspPass;
  100. meta->addStatement( new GenOp( " @.xy += @;\r\n", outPosition, atlasOffset ) );
  101. }
  102. output = meta;
  103. }
  104. void ParaboloidVertTransformGLSL::processPix( Vector<ShaderComponent*> &componentList,
  105. const MaterialFeatureData &fd )
  106. {
  107. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  108. MultiLine *meta = new MultiLine;
  109. const bool isSinglePass = fd.features[ MFT_IsSinglePassParaboloid ];
  110. if ( isSinglePass )
  111. {
  112. // Cull things on the back side of the map.
  113. Var *isBack = connectComp->getElement( RT_TEXCOORD );
  114. isBack->setName( "outIsBack" );
  115. isBack->setType( "float" );
  116. meta->addStatement( new GenOp( " if ( ( abs( @ ) - 0.999 ) < 0 ) discard;\r\n", isBack ) );
  117. }
  118. // Cull pixels outside of the valid paraboloid.
  119. Var *posXY = connectComp->getElement( RT_TEXCOORD );
  120. posXY->setName( "outPosXY" );
  121. posXY->setType( "vec2" );
  122. meta->addStatement( new GenOp( " if ( ( 1.0 - length( @ ) ) < 0 ) discard;\r\n", posXY ) );
  123. output = meta;
  124. }
  125. ShaderFeature::Resources ParaboloidVertTransformGLSL::getResources( const MaterialFeatureData &fd )
  126. {
  127. Resources temp;
  128. temp.numTexReg = 2;
  129. return temp;
  130. }