renderGlowMgr.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "renderInstance/renderGlowMgr.h"
  24. #include "scene/sceneManager.h"
  25. #include "scene/sceneRenderState.h"
  26. #include "materials/sceneData.h"
  27. #include "materials/matInstance.h"
  28. #include "materials/materialFeatureTypes.h"
  29. #include "materials/processedMaterial.h"
  30. #include "postFx/postEffect.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "gfx/gfxDebugEvent.h"
  33. #include "math/util/matrixSet.h"
  34. IMPLEMENT_CONOBJECT( RenderGlowMgr );
  35. ConsoleDocClass( RenderGlowMgr,
  36. "@brief A render bin for the glow pass.\n\n"
  37. "When the glow buffer PostEffect is enabled this bin gathers mesh render "
  38. "instances with glow materials and renders them to the #glowbuffer offscreen "
  39. "render target.\n\n"
  40. "This render target is then used by the 'GlowPostFx' PostEffect to blur and "
  41. "render the glowing portions of the screen.\n\n"
  42. "@ingroup RenderBin\n" );
  43. const MatInstanceHookType RenderGlowMgr::GlowMaterialHook::Type( "Glow" );
  44. RenderGlowMgr::GlowMaterialHook::GlowMaterialHook( BaseMatInstance *matInst )
  45. : mGlowMatInst( NULL )
  46. {
  47. mGlowMatInst = (MatInstance*)matInst->getMaterial()->createMatInstance();
  48. mGlowMatInst->getFeaturesDelegate().bind( &GlowMaterialHook::_overrideFeatures );
  49. mGlowMatInst->init( matInst->getRequestedFeatures(),
  50. matInst->getVertexFormat() );
  51. }
  52. RenderGlowMgr::GlowMaterialHook::~GlowMaterialHook()
  53. {
  54. SAFE_DELETE( mGlowMatInst );
  55. }
  56. void RenderGlowMgr::GlowMaterialHook::_overrideFeatures( ProcessedMaterial *mat,
  57. U32 stageNum,
  58. MaterialFeatureData &fd,
  59. const FeatureSet &features )
  60. {
  61. // If this isn't a glow pass... then add the glow mask feature.
  62. if ( mat->getMaterial() &&
  63. !mat->getMaterial()->mGlow[stageNum] )
  64. fd.features.addFeature( MFT_GlowMask );
  65. // Don't allow fog or HDR encoding on
  66. // the glow materials.
  67. fd.features.removeFeature( MFT_Fog );
  68. fd.features.removeFeature( MFT_HDROut );
  69. }
  70. RenderGlowMgr::RenderGlowMgr()
  71. : RenderTexTargetBinManager( RenderPassManager::RIT_Mesh,
  72. 1.0f,
  73. 1.0f,
  74. GFXFormatR8G8B8A8,
  75. Point2I( 512, 512 ) )
  76. {
  77. notifyType( RenderPassManager::RIT_Decal );
  78. notifyType( RenderPassManager::RIT_Translucent );
  79. mNamedTarget.registerWithName( "glowbuffer" );
  80. mTargetSizeType = WindowSize;
  81. }
  82. RenderGlowMgr::~RenderGlowMgr()
  83. {
  84. }
  85. PostEffect* RenderGlowMgr::getGlowEffect()
  86. {
  87. if ( !mGlowEffect )
  88. mGlowEffect = dynamic_cast<PostEffect*>( Sim::findObject( "GlowPostFx" ) );
  89. return mGlowEffect;
  90. }
  91. bool RenderGlowMgr::isGlowEnabled()
  92. {
  93. return getGlowEffect() && getGlowEffect()->isEnabled();
  94. }
  95. void RenderGlowMgr::addElement( RenderInst *inst )
  96. {
  97. // Skip out if we don't have the glow post
  98. // effect enabled at this time.
  99. if ( !isGlowEnabled() )
  100. return;
  101. // TODO: We need to get the scene state here in a more reliable
  102. // manner so we can skip glow in a non-diffuse render pass.
  103. //if ( !mParentManager->getSceneManager()->getSceneState()->isDiffusePass() )
  104. //return RenderBinManager::arSkipped;
  105. // Skip it if we don't have a glowing material.
  106. BaseMatInstance *matInst = getMaterial( inst );
  107. if ( !matInst || !matInst->hasGlow() )
  108. return;
  109. internalAddElement(inst);
  110. }
  111. void RenderGlowMgr::render( SceneRenderState *state )
  112. {
  113. PROFILE_SCOPE( RenderGlowMgr_Render );
  114. if ( !isGlowEnabled() )
  115. return;
  116. const U32 binSize = mElementList.size();
  117. // If this is a non-diffuse pass or we have no objects to
  118. // render then tell the effect to skip rendering.
  119. if ( !state->isDiffusePass() || binSize == 0 )
  120. {
  121. getGlowEffect()->setSkip( true );
  122. return;
  123. }
  124. GFXDEBUGEVENT_SCOPE( RenderGlowMgr_Render, ColorI::GREEN );
  125. GFXTransformSaver saver;
  126. // Tell the superclass we're about to render, preserve contents
  127. const bool isRenderingToTarget = _onPreRender( state, true );
  128. // Clear all the buffers to black.
  129. GFX->clear( GFXClearTarget, ColorI::BLACK, 1.0f, 0);
  130. // Restore transforms
  131. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  132. matrixSet.restoreSceneViewProjection();
  133. // init loop data
  134. SceneData sgData;
  135. sgData.init( state, SceneData::GlowBin );
  136. for( U32 j=0; j<binSize; )
  137. {
  138. MeshRenderInst *ri = static_cast<MeshRenderInst*>(mElementList[j].inst);
  139. setupSGData( ri, sgData );
  140. BaseMatInstance *mat = ri->matInst;
  141. GlowMaterialHook *hook = mat->getHook<GlowMaterialHook>();
  142. if ( !hook )
  143. {
  144. hook = new GlowMaterialHook( ri->matInst );
  145. ri->matInst->addHook( hook );
  146. }
  147. BaseMatInstance *glowMat = hook->getMatInstance();
  148. U32 matListEnd = j;
  149. while( glowMat && glowMat->setupPass( state, sgData ) )
  150. {
  151. U32 a;
  152. for( a=j; a<binSize; a++ )
  153. {
  154. MeshRenderInst *passRI = static_cast<MeshRenderInst*>(mElementList[a].inst);
  155. if ( newPassNeeded( ri, passRI ) )
  156. break;
  157. matrixSet.setWorld(*passRI->objectToWorld);
  158. matrixSet.setView(*passRI->worldToCamera);
  159. matrixSet.setProjection(*passRI->projection);
  160. glowMat->setTransforms(matrixSet, state);
  161. glowMat->setSceneInfo(state, sgData);
  162. glowMat->setBuffers(passRI->vertBuff, passRI->primBuff);
  163. if ( passRI->prim )
  164. GFX->drawPrimitive( *passRI->prim );
  165. else
  166. GFX->drawPrimitive( passRI->primBuffIndex );
  167. }
  168. matListEnd = a;
  169. setupSGData( ri, sgData );
  170. }
  171. // force increment if none happened, otherwise go to end of batch
  172. j = ( j == matListEnd ) ? j+1 : matListEnd;
  173. }
  174. // Finish up.
  175. if ( isRenderingToTarget )
  176. _onPostRender();
  177. // Make sure the effect is gonna render.
  178. getGlowEffect()->setSkip( false );
  179. }