renderTranslucentMgr.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/renderTranslucentMgr.h"
  24. #include "materials/sceneData.h"
  25. #include "scene/sceneManager.h"
  26. #include "scene/sceneObject.h"
  27. #include "scene/sceneRenderState.h"
  28. #include "materials/matInstance.h"
  29. #include "gfx/gfxPrimitiveBuffer.h"
  30. #include "gfx/gfxTransformSaver.h"
  31. #include "gfx/gfxDebugEvent.h"
  32. #include "renderInstance/renderParticleMgr.h"
  33. #include "math/util/matrixSet.h"
  34. #define HIGH_NUM ((U32(-1)/2) - 1)
  35. IMPLEMENT_CONOBJECT(RenderTranslucentMgr);
  36. ConsoleDocClass( RenderTranslucentMgr,
  37. "@brief A render bin for rendering translucent meshes.\n\n"
  38. "This bin is used to render translucent render mesh instances and render object "
  39. "instances. It is generally ordered late in the RenderPassManager after all opaque "
  40. "geometry bins.\n\n"
  41. "@ingroup RenderBin\n" );
  42. RenderTranslucentMgr::RenderTranslucentMgr()
  43. : RenderBinManager( RenderPassManager::RIT_Translucent, 1.0f, 1.0f ), mParticleRenderMgr(NULL)
  44. {
  45. notifyType( RenderPassManager::RIT_ObjectTranslucent );
  46. notifyType( RenderPassManager::RIT_Particle );
  47. }
  48. RenderTranslucentMgr::~RenderTranslucentMgr()
  49. {
  50. }
  51. void RenderTranslucentMgr::setupSGData(MeshRenderInst *ri, SceneData &data )
  52. {
  53. Parent::setupSGData( ri, data );
  54. // We do not support these in the translucent bin.
  55. data.backBuffTex = NULL;
  56. data.cubemap = NULL;
  57. data.lightmap = NULL;
  58. }
  59. void RenderTranslucentMgr::addElement( RenderInst *inst )
  60. {
  61. // Right off the bat if its not translucent skip it.
  62. if ( !inst->translucentSort )
  63. return;
  64. // What type of instance is this.
  65. const bool isMeshInst = inst->type == RenderPassManager::RIT_Translucent;
  66. // Get its material if its a mesh.
  67. BaseMatInstance* matInst = NULL;
  68. if ( isMeshInst )
  69. matInst = static_cast<MeshRenderInst*>( inst )->matInst;
  70. // If the material isn't translucent the skip it.
  71. if ( matInst && !matInst->getMaterial()->isTranslucent() )
  72. return;
  73. // We made it this far, add the instance.
  74. mElementList.increment();
  75. MainSortElem& elem = mElementList.last();
  76. elem.inst = inst;
  77. // Override the instances default key to be the sort distance. All
  78. // the pointer dereferencing is in there to prevent us from losing
  79. // information when converting to a U32.
  80. elem.key = *((U32*)&inst->sortDistSq);
  81. AssertFatal( inst->defaultKey != 0, "RenderTranslucentMgr::addElement() - Got null sort key... did you forget to set it?" );
  82. // Then use the instances primary key as our secondary key
  83. elem.key2 = inst->defaultKey;
  84. }
  85. GFXStateBlockRef RenderTranslucentMgr::_getStateBlock( U8 transFlag )
  86. {
  87. if ( mStateBlocks[transFlag].isValid() )
  88. return mStateBlocks[transFlag];
  89. GFXStateBlockDesc d;
  90. d.cullDefined = true;
  91. d.cullMode = GFXCullNone;
  92. d.blendDefined = true;
  93. d.blendEnable = true;
  94. d.blendSrc = (GFXBlend)((transFlag >> 4) & 0x0f);
  95. d.blendDest = (GFXBlend)(transFlag & 0x0f);
  96. d.alphaDefined = true;
  97. // See http://www.garagegames.com/mg/forums/result.thread.php?qt=81397
  98. d.alphaTestEnable = (d.blendSrc == GFXBlendSrcAlpha && (d.blendDest == GFXBlendInvSrcAlpha || d.blendDest == GFXBlendOne));
  99. d.alphaTestRef = 1;
  100. d.alphaTestFunc = GFXCmpGreaterEqual;
  101. d.zDefined = true;
  102. d.zWriteEnable = false;
  103. d.samplersDefined = true;
  104. d.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  105. d.samplers[0].alphaOp = GFXTOPModulate;
  106. d.samplers[0].alphaArg1 = GFXTATexture;
  107. d.samplers[0].alphaArg2 = GFXTADiffuse;
  108. mStateBlocks[transFlag] = GFX->createStateBlock(d);
  109. return mStateBlocks[transFlag];
  110. }
  111. void RenderTranslucentMgr::render( SceneRenderState *state )
  112. {
  113. PROFILE_SCOPE(RenderTranslucentMgr_render);
  114. // Early out if nothing to draw.
  115. if(!mElementList.size())
  116. return;
  117. GFXDEBUGEVENT_SCOPE(RenderTranslucentMgr_Render, ColorI::BLUE);
  118. // Find the particle render manager (if we don't have it)
  119. if(mParticleRenderMgr == NULL)
  120. {
  121. RenderPassManager *rpm = state->getRenderPass();
  122. for( U32 i = 0; i < rpm->getManagerCount(); i++ )
  123. {
  124. RenderBinManager *bin = rpm->getManager(i);
  125. if( bin->getRenderInstType() == RenderParticleMgr::RIT_Particles )
  126. {
  127. mParticleRenderMgr = reinterpret_cast<RenderParticleMgr *>(bin);
  128. break;
  129. }
  130. }
  131. }
  132. GFXTransformSaver saver;
  133. SceneData sgData;
  134. sgData.init( state );
  135. GFXVertexBuffer * lastVB = NULL;
  136. GFXPrimitiveBuffer * lastPB = NULL;
  137. // Restore transforms
  138. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  139. matrixSet.restoreSceneViewProjection();
  140. U32 binSize = mElementList.size();
  141. for( U32 j=0; j<binSize; )
  142. {
  143. RenderInst *baseRI = mElementList[j].inst;
  144. U32 matListEnd = j;
  145. // render these separately...
  146. if ( baseRI->type == RenderPassManager::RIT_ObjectTranslucent )
  147. {
  148. ObjectRenderInst* objRI = static_cast<ObjectRenderInst*>(baseRI);
  149. objRI->renderDelegate( objRI, state, NULL );
  150. lastVB = NULL;
  151. lastPB = NULL;
  152. j++;
  153. continue;
  154. }
  155. else if ( baseRI->type == RenderPassManager::RIT_Particle )
  156. {
  157. ParticleRenderInst *ri = static_cast<ParticleRenderInst*>(baseRI);
  158. // Tell Particle RM to draw the system. (This allows the particle render manager
  159. // to manage drawing offscreen particle systems, and allows the systems
  160. // to be composited back into the scene with proper translucent
  161. // sorting order)
  162. mParticleRenderMgr->renderInstance(ri, state);
  163. lastVB = NULL; // no longer valid, null it
  164. lastPB = NULL; // no longer valid, null it
  165. j++;
  166. continue;
  167. }
  168. else if ( baseRI->type == RenderPassManager::RIT_Translucent )
  169. {
  170. MeshRenderInst* ri = static_cast<MeshRenderInst*>(baseRI);
  171. BaseMatInstance *mat = ri->matInst;
  172. setupSGData( ri, sgData );
  173. while( mat->setupPass( state, sgData ) )
  174. {
  175. U32 a;
  176. for( a=j; a<binSize; a++ )
  177. {
  178. RenderInst* nextRI = mElementList[a].inst;
  179. if ( nextRI->type != RenderPassManager::RIT_Translucent )
  180. break;
  181. MeshRenderInst *passRI = static_cast<MeshRenderInst*>(nextRI);
  182. // Check to see if we need to break this batch.
  183. if ( newPassNeeded( ri, passRI ) )
  184. break;
  185. // Z sorting and stuff is still not working in this mgr...
  186. setupSGData( passRI, sgData );
  187. mat->setSceneInfo(state, sgData);
  188. matrixSet.setWorld(*passRI->objectToWorld);
  189. matrixSet.setView(*passRI->worldToCamera);
  190. matrixSet.setProjection(*passRI->projection);
  191. mat->setTransforms(matrixSet, state);
  192. // If we're instanced then don't render yet.
  193. if ( mat->isInstanced() )
  194. {
  195. // Let the material increment the instance buffer, but
  196. // break the batch if it runs out of room for more.
  197. if ( !mat->stepInstance() )
  198. {
  199. a++;
  200. break;
  201. }
  202. continue;
  203. }
  204. // Setup the vertex and index buffers.
  205. mat->setBuffers( passRI->vertBuff, passRI->primBuff );
  206. // Render this sucker.
  207. if ( passRI->prim )
  208. GFX->drawPrimitive( *passRI->prim );
  209. else
  210. GFX->drawPrimitive( passRI->primBuffIndex );
  211. }
  212. // Draw the instanced batch.
  213. if ( mat->isInstanced() )
  214. {
  215. // Sets the buffers including the instancing stream.
  216. mat->setBuffers( ri->vertBuff, ri->primBuff );
  217. // Render the instanced stream.
  218. if ( ri->prim )
  219. GFX->drawPrimitive( *ri->prim );
  220. else
  221. GFX->drawPrimitive( ri->primBuffIndex );
  222. }
  223. matListEnd = a;
  224. }
  225. // force increment if none happened, otherwise go to end of batch
  226. j = ( j == matListEnd ) ? j+1 : matListEnd;
  227. }
  228. }
  229. }