renderTranslucentMgr.cpp 10.0 KB

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