renderMeshMgr.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/renderMeshMgr.h"
  24. #include "console/consoleTypes.h"
  25. #include "gfx/gfxTransformSaver.h"
  26. #include "gfx/gfxPrimitiveBuffer.h"
  27. #include "materials/sceneData.h"
  28. #include "materials/processedMaterial.h"
  29. #include "materials/materialManager.h"
  30. #include "scene/sceneRenderState.h"
  31. #include "gfx/gfxDebugEvent.h"
  32. #include "math/util/matrixSet.h"
  33. IMPLEMENT_CONOBJECT(RenderMeshMgr);
  34. ConsoleDocClass( RenderMeshMgr,
  35. "@brief A render bin for mesh rendering.\n\n"
  36. "This is the primary render bin in Torque which does most of the "
  37. "work of rendering DTS shapes and arbitrary mesh geometry. It knows "
  38. "how to render mesh instances using materials and supports hardware mesh "
  39. "instancing.\n\n"
  40. "@ingroup RenderBin\n" );
  41. RenderMeshMgr::RenderMeshMgr()
  42. : RenderBinManager(RenderPassManager::RIT_Mesh, 1.0f, 1.0f)
  43. {
  44. }
  45. RenderMeshMgr::RenderMeshMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder)
  46. : RenderBinManager(riType, renderOrder, processAddOrder)
  47. {
  48. }
  49. void RenderMeshMgr::init()
  50. {
  51. GFXStateBlockDesc d;
  52. d.cullDefined = true;
  53. d.cullMode = GFXCullCCW;
  54. d.samplersDefined = true;
  55. d.samplers[0] = GFXSamplerStateDesc::getWrapLinear();
  56. mNormalSB = GFX->createStateBlock(d);
  57. d.cullMode = GFXCullCW;
  58. mReflectSB = GFX->createStateBlock(d);
  59. }
  60. void RenderMeshMgr::initPersistFields()
  61. {
  62. Parent::initPersistFields();
  63. }
  64. //-----------------------------------------------------------------------------
  65. // add element
  66. //-----------------------------------------------------------------------------
  67. void RenderMeshMgr::addElement( RenderInst *inst )
  68. {
  69. // If this instance is translucent handle it in RenderTranslucentMgr
  70. if (inst->translucentSort)
  71. return;
  72. AssertFatal( inst->defaultKey != 0, "RenderMeshMgr::addElement() - Got null sort key... did you forget to set it?" );
  73. internalAddElement(inst);
  74. }
  75. //-----------------------------------------------------------------------------
  76. // render
  77. //-----------------------------------------------------------------------------
  78. void RenderMeshMgr::render(SceneRenderState * state)
  79. {
  80. PROFILE_SCOPE(RenderMeshMgr_render);
  81. // Early out if nothing to draw.
  82. if(!mElementList.size())
  83. return;
  84. GFXDEBUGEVENT_SCOPE( RenderMeshMgr_Render, ColorI::GREEN );
  85. // Automagically save & restore our viewport and transforms.
  86. GFXTransformSaver saver;
  87. // Restore transforms
  88. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  89. matrixSet.restoreSceneViewProjection();
  90. // init loop data
  91. GFXTextureObject *lastLM = NULL;
  92. GFXCubemap *lastCubemap = NULL;
  93. GFXTextureObject *lastReflectTex = NULL;
  94. GFXTextureObject *lastMiscTex = NULL;
  95. GFXTextureObject *lastAccuTex = NULL;
  96. SceneData sgData;
  97. sgData.init( state );
  98. U32 binSize = mElementList.size();
  99. for( U32 j=0; j<binSize; )
  100. {
  101. MeshRenderInst *ri = static_cast<MeshRenderInst*>(mElementList[j].inst);
  102. setupSGData( ri, sgData );
  103. BaseMatInstance *mat = ri->matInst;
  104. // If we have an override delegate then give it a
  105. // chance to swap the material with another.
  106. if ( mMatOverrideDelegate )
  107. {
  108. mat = mMatOverrideDelegate( mat );
  109. if ( !mat )
  110. {
  111. j++;
  112. continue;
  113. }
  114. }
  115. if( !mat )
  116. mat = MATMGR->getWarningMatInstance();
  117. // Check if bin is disabled in advanced lighting.
  118. // Allow forward rendering pass on custom materials.
  119. if ( ( MATMGR->getPrePassEnabled() && mBasicOnly && !mat->isCustomMaterial() ) )
  120. {
  121. j++;
  122. continue;
  123. }
  124. U32 matListEnd = j;
  125. lastMiscTex = sgData.miscTex;
  126. U32 a;
  127. while( mat && mat->setupPass(state, sgData ) )
  128. {
  129. for( a=j; a<binSize; a++ )
  130. {
  131. MeshRenderInst *passRI = static_cast<MeshRenderInst*>(mElementList[a].inst);
  132. // Check to see if we need to break this batch.
  133. if ( newPassNeeded( ri, passRI ) ||
  134. lastMiscTex != passRI->miscTex )
  135. {
  136. lastLM = NULL;
  137. break;
  138. }
  139. matrixSet.setWorld(*passRI->objectToWorld);
  140. matrixSet.setView(*passRI->worldToCamera);
  141. matrixSet.setProjection(*passRI->projection);
  142. mat->setTransforms(matrixSet, state);
  143. setupSGData( passRI, sgData );
  144. mat->setSceneInfo( state, sgData );
  145. // If we're instanced then don't render yet.
  146. if ( mat->isInstanced() )
  147. {
  148. // Let the material increment the instance buffer, but
  149. // break the batch if it runs out of room for more.
  150. if ( !mat->stepInstance() )
  151. {
  152. a++;
  153. break;
  154. }
  155. continue;
  156. }
  157. // TODO: This could proably be done in a cleaner way.
  158. //
  159. // This section of code is dangerous, it overwrites the
  160. // lightmap values in sgData. This could be a problem when multiple
  161. // render instances use the same multi-pass material. When
  162. // the first pass is done, setupPass() is called again on
  163. // the material, but the lightmap data has been changed in
  164. // sgData to the lightmaps in the last renderInstance rendered.
  165. // This section sets the lightmap data for the current batch.
  166. // For the first iteration, it sets the same lightmap data,
  167. // however the redundancy will be caught by GFXDevice and not
  168. // actually sent to the card. This is done for simplicity given
  169. // the possible condition mentioned above. Better to set always
  170. // than to get bogged down into special case detection.
  171. //-------------------------------------
  172. bool dirty = false;
  173. // set the lightmaps if different
  174. if( passRI->lightmap && passRI->lightmap != lastLM )
  175. {
  176. sgData.lightmap = passRI->lightmap;
  177. lastLM = passRI->lightmap;
  178. dirty = true;
  179. }
  180. // set the cubemap if different.
  181. if ( passRI->cubemap != lastCubemap )
  182. {
  183. sgData.cubemap = passRI->cubemap;
  184. lastCubemap = passRI->cubemap;
  185. dirty = true;
  186. }
  187. if ( passRI->reflectTex != lastReflectTex )
  188. {
  189. sgData.reflectTex = passRI->reflectTex;
  190. lastReflectTex = passRI->reflectTex;
  191. dirty = true;
  192. }
  193. // Update accumulation texture if it changed.
  194. // Note: accumulation texture can be NULL, and must be updated.
  195. if ( passRI->accuTex != lastAccuTex )
  196. {
  197. sgData.accuTex = passRI->accuTex;
  198. lastAccuTex = lastAccuTex;
  199. dirty = true;
  200. }
  201. if ( dirty )
  202. mat->setTextureStages( state, sgData );
  203. // Setup the vertex and index buffers.
  204. mat->setBuffers( passRI->vertBuff, passRI->primBuff );
  205. // Render this sucker.
  206. if ( passRI->prim )
  207. GFX->drawPrimitive( *passRI->prim );
  208. else
  209. GFX->drawPrimitive( passRI->primBuffIndex );
  210. }
  211. // Draw the instanced batch.
  212. if ( mat->isInstanced() )
  213. {
  214. // Sets the buffers including the instancing stream.
  215. mat->setBuffers( ri->vertBuff, ri->primBuff );
  216. // Render the instanced stream.
  217. if ( ri->prim )
  218. GFX->drawPrimitive( *ri->prim );
  219. else
  220. GFX->drawPrimitive( ri->primBuffIndex );
  221. }
  222. matListEnd = a;
  223. }
  224. // force increment if none happened, otherwise go to end of batch
  225. j = ( j == matListEnd ) ? j+1 : matListEnd;
  226. }
  227. }