renderTranslucentMgr.cpp 12 KB

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