2
0

renderTranslucentMgr.cpp 11 KB

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