renderTranslucentMgr.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. mStateBlocks[transFlag] = GFX->createStateBlock(d);
  107. return mStateBlocks[transFlag];
  108. }
  109. void RenderTranslucentMgr::render( SceneRenderState *state )
  110. {
  111. PROFILE_SCOPE(RenderTranslucentMgr_render);
  112. // Early out if nothing to draw.
  113. if(!mElementList.size())
  114. return;
  115. GFXDEBUGEVENT_SCOPE(RenderTranslucentMgr_Render, ColorI::BLUE);
  116. // init loop data
  117. GFXTextureObject *lastLM = NULL;
  118. GFXCubemap *lastCubemap = NULL;
  119. GFXTextureObject *lastReflectTex = NULL;
  120. GFXTextureObject *lastAccuTex = NULL;
  121. // Find the particle render manager (if we don't have it)
  122. if(mParticleRenderMgr == NULL)
  123. {
  124. RenderPassManager *rpm = state->getRenderPass();
  125. for( U32 i = 0; i < rpm->getManagerCount(); i++ )
  126. {
  127. RenderBinManager *bin = rpm->getManager(i);
  128. if( bin->getRenderInstType() == RenderParticleMgr::RIT_Particles )
  129. {
  130. mParticleRenderMgr = reinterpret_cast<RenderParticleMgr *>(bin);
  131. break;
  132. }
  133. }
  134. }
  135. GFXTransformSaver saver;
  136. SceneData sgData;
  137. sgData.init( state );
  138. GFXVertexBuffer * lastVB = NULL;
  139. GFXPrimitiveBuffer * lastPB = NULL;
  140. // Restore transforms
  141. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  142. matrixSet.restoreSceneViewProjection();
  143. U32 binSize = mElementList.size();
  144. for( U32 j=0; j<binSize; )
  145. {
  146. RenderInst *baseRI = mElementList[j].inst;
  147. U32 matListEnd = j;
  148. // render these separately...
  149. if ( baseRI->type == RenderPassManager::RIT_ObjectTranslucent )
  150. {
  151. ObjectRenderInst* objRI = static_cast<ObjectRenderInst*>(baseRI);
  152. objRI->renderDelegate( objRI, state, NULL );
  153. lastVB = NULL;
  154. lastPB = NULL;
  155. j++;
  156. continue;
  157. }
  158. else if (baseRI->type == RenderPassManager::RIT_VolumetricFog)
  159. {
  160. ObjectRenderInst* objRI = static_cast<ObjectRenderInst*>(baseRI);
  161. objRI->renderDelegate(objRI, state, NULL);
  162. lastVB = NULL;
  163. lastPB = NULL;
  164. j++;
  165. continue;
  166. }
  167. else if ( baseRI->type == RenderPassManager::RIT_Particle )
  168. {
  169. ParticleRenderInst *ri = static_cast<ParticleRenderInst*>(baseRI);
  170. // Tell Particle RM to draw the system. (This allows the particle render manager
  171. // to manage drawing offscreen particle systems, and allows the systems
  172. // to be composited back into the scene with proper translucent
  173. // sorting order)
  174. mParticleRenderMgr->renderInstance(ri, state);
  175. lastVB = NULL; // no longer valid, null it
  176. lastPB = NULL; // no longer valid, null it
  177. j++;
  178. continue;
  179. }
  180. else if ( baseRI->type == RenderPassManager::RIT_Translucent )
  181. {
  182. MeshRenderInst* ri = static_cast<MeshRenderInst*>(baseRI);
  183. BaseMatInstance *mat = ri->matInst;
  184. setupSGData( ri, sgData );
  185. while( mat->setupPass( state, sgData ) )
  186. {
  187. U32 a;
  188. for( a=j; a<binSize; a++ )
  189. {
  190. RenderInst* nextRI = mElementList[a].inst;
  191. if ( nextRI->type != RenderPassManager::RIT_Translucent )
  192. break;
  193. MeshRenderInst *passRI = static_cast<MeshRenderInst*>(nextRI);
  194. // Check to see if we need to break this batch.
  195. if ( newPassNeeded( ri, passRI ) )
  196. break;
  197. // Z sorting and stuff is still not working in this mgr...
  198. setupSGData( passRI, sgData );
  199. mat->setSceneInfo(state, sgData);
  200. matrixSet.setWorld(*passRI->objectToWorld);
  201. matrixSet.setView(*passRI->worldToCamera);
  202. matrixSet.setProjection(*passRI->projection);
  203. mat->setTransforms(matrixSet, state);
  204. // Setup HW skinning transforms if applicable
  205. if (mat->usesHardwareSkinning())
  206. {
  207. mat->setNodeTransforms(passRI->mNodeTransforms, passRI->mNodeTransformCount);
  208. }
  209. //push along any overriden fields that are instance-specific as well
  210. if (passRI->mCustomShaderData.size() > 0)
  211. {
  212. mat->setCustomShaderData(passRI->mCustomShaderData);
  213. }
  214. // If we're instanced then don't render yet.
  215. if ( mat->isInstanced() )
  216. {
  217. // Let the material increment the instance buffer, but
  218. // break the batch if it runs out of room for more.
  219. if ( !mat->stepInstance() )
  220. {
  221. a++;
  222. break;
  223. }
  224. continue;
  225. }
  226. bool dirty = false;
  227. // set the lightmaps if different
  228. if (passRI->lightmap && passRI->lightmap != lastLM)
  229. {
  230. sgData.lightmap = passRI->lightmap;
  231. lastLM = passRI->lightmap;
  232. dirty = true;
  233. }
  234. // set the cubemap if different.
  235. if (passRI->cubemap != lastCubemap)
  236. {
  237. sgData.cubemap = passRI->cubemap;
  238. lastCubemap = passRI->cubemap;
  239. dirty = true;
  240. }
  241. if (passRI->reflectTex != lastReflectTex)
  242. {
  243. sgData.reflectTex = passRI->reflectTex;
  244. lastReflectTex = passRI->reflectTex;
  245. dirty = true;
  246. }
  247. // Update accumulation texture if it changed.
  248. // Note: accumulation texture can be NULL, and must be updated.
  249. if (passRI->accuTex != lastAccuTex)
  250. {
  251. sgData.accuTex = passRI->accuTex;
  252. lastAccuTex = passRI->accuTex;
  253. dirty = true;
  254. }
  255. if (dirty)
  256. mat->setTextureStages(state, sgData);
  257. // Setup the vertex and index buffers.
  258. mat->setBuffers( passRI->vertBuff, passRI->primBuff );
  259. // Render this sucker.
  260. if ( passRI->prim )
  261. GFX->drawPrimitive( *passRI->prim );
  262. else
  263. GFX->drawPrimitive( passRI->primBuffIndex );
  264. }
  265. // Draw the instanced batch.
  266. if ( mat->isInstanced() )
  267. {
  268. // Sets the buffers including the instancing stream.
  269. mat->setBuffers( ri->vertBuff, ri->primBuff );
  270. // Render the instanced stream.
  271. if ( ri->prim )
  272. GFX->drawPrimitive( *ri->prim );
  273. else
  274. GFX->drawPrimitive( ri->primBuffIndex );
  275. }
  276. matListEnd = a;
  277. }
  278. // force increment if none happened, otherwise go to end of batch
  279. j = ( j == matListEnd ) ? j+1 : matListEnd;
  280. }
  281. }
  282. }