renderGlowMgr.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. fd.features.addFeature( MFT_Imposter );
  72. }
  73. RenderGlowMgr::RenderGlowMgr()
  74. : RenderTexTargetBinManager( RenderPassManager::RIT_Mesh,
  75. 1.0f,
  76. 1.0f,
  77. GFXFormatR8G8B8A8,
  78. Point2I( 512, 512 ) )
  79. {
  80. notifyType( RenderPassManager::RIT_Decal );
  81. notifyType( RenderPassManager::RIT_DecalRoad );
  82. notifyType( RenderPassManager::RIT_Translucent );
  83. notifyType( RenderPassManager::RIT_Particle );
  84. mParticleRenderMgr = NULL;
  85. mNamedTarget.registerWithName( "glowbuffer" );
  86. mTargetSizeType = WindowSize;
  87. }
  88. RenderGlowMgr::~RenderGlowMgr()
  89. {
  90. }
  91. PostEffect* RenderGlowMgr::getGlowEffect()
  92. {
  93. if ( !mGlowEffect )
  94. mGlowEffect = dynamic_cast<PostEffect*>( Sim::findObject( "GlowPostFx" ) );
  95. return mGlowEffect;
  96. }
  97. bool RenderGlowMgr::isGlowEnabled()
  98. {
  99. return getGlowEffect() && getGlowEffect()->isEnabled();
  100. }
  101. void RenderGlowMgr::addElement( RenderInst *inst )
  102. {
  103. // Skip out if we don't have the glow post
  104. // effect enabled at this time.
  105. if ( !isGlowEnabled() )
  106. return;
  107. // TODO: We need to get the scene state here in a more reliable
  108. // manner so we can skip glow in a non-diffuse render pass.
  109. //if ( !mParentManager->getSceneManager()->getSceneState()->isDiffusePass() )
  110. //return RenderBinManager::arSkipped;
  111. ParticleRenderInst *particleInst = NULL;
  112. if(inst->type == RenderPassManager::RIT_Particle)
  113. particleInst = static_cast<ParticleRenderInst*>(inst);
  114. if(particleInst && particleInst->glow)
  115. {
  116. internalAddElement(inst);
  117. return;
  118. }
  119. // Skip it if we don't have a glowing material.
  120. BaseMatInstance *matInst = getMaterial( inst );
  121. if ( !matInst || !matInst->hasGlow() )
  122. return;
  123. internalAddElement(inst);
  124. }
  125. void RenderGlowMgr::render( SceneRenderState *state )
  126. {
  127. PROFILE_SCOPE( RenderGlowMgr_Render );
  128. if ( !isGlowEnabled() )
  129. return;
  130. const U32 binSize = mElementList.size();
  131. // If this is a non-diffuse pass or we have no objects to
  132. // render then tell the effect to skip rendering.
  133. if ( !state->isDiffusePass() || binSize == 0 )
  134. {
  135. getGlowEffect()->setSkip( true );
  136. return;
  137. }
  138. GFXDEBUGEVENT_SCOPE( RenderGlowMgr_Render, ColorI::GREEN );
  139. GFXTransformSaver saver;
  140. // Respect the current viewport
  141. mNamedTarget.setViewport(GFX->getViewport());
  142. // Tell the superclass we're about to render, preserve contents
  143. const bool isRenderingToTarget = _onPreRender( state, true );
  144. // Clear all the buffers to black.
  145. GFX->clear( GFXClearTarget, ColorI::BLACK, 1.0f, 0);
  146. // Restore transforms
  147. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  148. matrixSet.restoreSceneViewProjection();
  149. // init loop data
  150. SceneData sgData;
  151. sgData.init( state, SceneData::GlowBin );
  152. for( U32 j=0; j<binSize; )
  153. {
  154. RenderInst *_ri = mElementList[j].inst;
  155. if(_ri->type == RenderPassManager::RIT_Particle)
  156. {
  157. // Find the particle render manager (if we don't have it)
  158. if(mParticleRenderMgr == NULL)
  159. {
  160. RenderPassManager *rpm = state->getRenderPass();
  161. for( U32 i = 0; i < rpm->getManagerCount(); i++ )
  162. {
  163. RenderBinManager *bin = rpm->getManager(i);
  164. if( bin->getRenderInstType() == RenderParticleMgr::RIT_Particles )
  165. {
  166. mParticleRenderMgr = reinterpret_cast<RenderParticleMgr *>(bin);
  167. break;
  168. }
  169. }
  170. }
  171. ParticleRenderInst *ri = static_cast<ParticleRenderInst*>(_ri);
  172. GFX->setStateBlock(mParticleRenderMgr->_getHighResStateBlock(ri));
  173. mParticleRenderMgr->_getShaderConsts().mShaderConsts->setSafe(mParticleRenderMgr->_getShaderConsts().mModelViewProjSC, *ri->modelViewProj);
  174. mParticleRenderMgr->renderParticle(ri, state);
  175. j++;
  176. continue;
  177. }
  178. MeshRenderInst *ri = static_cast<MeshRenderInst*>(_ri);
  179. setupSGData( ri, sgData );
  180. BaseMatInstance *mat = ri->matInst;
  181. GlowMaterialHook *hook = mat->getHook<GlowMaterialHook>();
  182. if ( !hook )
  183. {
  184. hook = new GlowMaterialHook( ri->matInst );
  185. ri->matInst->addHook( hook );
  186. }
  187. BaseMatInstance *glowMat = hook->getMatInstance();
  188. U32 matListEnd = j;
  189. while( glowMat && glowMat->setupPass( state, sgData ) )
  190. {
  191. U32 a;
  192. for( a=j; a<binSize; a++ )
  193. {
  194. if (mElementList[a].inst->type == RenderPassManager::RIT_Particle)
  195. break;
  196. MeshRenderInst *passRI = static_cast<MeshRenderInst*>(mElementList[a].inst);
  197. if ( newPassNeeded( ri, passRI ) )
  198. break;
  199. matrixSet.setWorld(*passRI->objectToWorld);
  200. matrixSet.setView(*passRI->worldToCamera);
  201. matrixSet.setProjection(*passRI->projection);
  202. glowMat->setTransforms(matrixSet, state);
  203. glowMat->setSceneInfo(state, sgData);
  204. glowMat->setBuffers(passRI->vertBuff, passRI->primBuff);
  205. if ( passRI->prim )
  206. GFX->drawPrimitive( *passRI->prim );
  207. else
  208. GFX->drawPrimitive( passRI->primBuffIndex );
  209. }
  210. matListEnd = a;
  211. setupSGData( ri, sgData );
  212. }
  213. // force increment if none happened, otherwise go to end of batch
  214. j = ( j == matListEnd ) ? j+1 : matListEnd;
  215. }
  216. // Finish up.
  217. if ( isRenderingToTarget )
  218. _onPostRender();
  219. // Make sure the effect is gonna render.
  220. getGlowEffect()->setSkip( false );
  221. }