renderGlowMgr.cpp 9.3 KB

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