renderGlowMgr.cpp 8.9 KB

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