renderMeshMgr.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. SceneData sgData;
  96. sgData.init( state );
  97. U32 binSize = mElementList.size();
  98. for( U32 j=0; j<binSize; )
  99. {
  100. MeshRenderInst *ri = static_cast<MeshRenderInst*>(mElementList[j].inst);
  101. setupSGData( ri, sgData );
  102. BaseMatInstance *mat = ri->matInst;
  103. // If we have an override delegate then give it a
  104. // chance to swap the material with another.
  105. if ( mMatOverrideDelegate )
  106. {
  107. mat = mMatOverrideDelegate( mat );
  108. if ( !mat )
  109. {
  110. j++;
  111. continue;
  112. }
  113. }
  114. if( !mat )
  115. mat = MATMGR->getWarningMatInstance();
  116. U32 matListEnd = j;
  117. lastMiscTex = sgData.miscTex;
  118. U32 a;
  119. while( mat && mat->setupPass(state, sgData ) )
  120. {
  121. for( a=j; a<binSize; a++ )
  122. {
  123. MeshRenderInst *passRI = static_cast<MeshRenderInst*>(mElementList[a].inst);
  124. // Check to see if we need to break this batch.
  125. if ( newPassNeeded( ri, passRI ) ||
  126. lastMiscTex != passRI->miscTex )
  127. {
  128. lastLM = NULL;
  129. break;
  130. }
  131. matrixSet.setWorld(*passRI->objectToWorld);
  132. matrixSet.setView(*passRI->worldToCamera);
  133. matrixSet.setProjection(*passRI->projection);
  134. mat->setTransforms(matrixSet, state);
  135. setupSGData( passRI, sgData );
  136. mat->setSceneInfo( state, sgData );
  137. // If we're instanced then don't render yet.
  138. if ( mat->isInstanced() )
  139. {
  140. // Let the material increment the instance buffer, but
  141. // break the batch if it runs out of room for more.
  142. if ( !mat->stepInstance() )
  143. {
  144. a++;
  145. break;
  146. }
  147. continue;
  148. }
  149. // TODO: This could proably be done in a cleaner way.
  150. //
  151. // This section of code is dangerous, it overwrites the
  152. // lightmap values in sgData. This could be a problem when multiple
  153. // render instances use the same multi-pass material. When
  154. // the first pass is done, setupPass() is called again on
  155. // the material, but the lightmap data has been changed in
  156. // sgData to the lightmaps in the last renderInstance rendered.
  157. // This section sets the lightmap data for the current batch.
  158. // For the first iteration, it sets the same lightmap data,
  159. // however the redundancy will be caught by GFXDevice and not
  160. // actually sent to the card. This is done for simplicity given
  161. // the possible condition mentioned above. Better to set always
  162. // than to get bogged down into special case detection.
  163. //-------------------------------------
  164. bool dirty = false;
  165. // set the lightmaps if different
  166. if( passRI->lightmap && passRI->lightmap != lastLM )
  167. {
  168. sgData.lightmap = passRI->lightmap;
  169. lastLM = passRI->lightmap;
  170. dirty = true;
  171. }
  172. // set the cubemap if different.
  173. if ( passRI->cubemap != lastCubemap )
  174. {
  175. sgData.cubemap = passRI->cubemap;
  176. lastCubemap = passRI->cubemap;
  177. dirty = true;
  178. }
  179. if ( passRI->reflectTex != lastReflectTex )
  180. {
  181. sgData.reflectTex = passRI->reflectTex;
  182. lastReflectTex = passRI->reflectTex;
  183. dirty = true;
  184. }
  185. if ( dirty )
  186. mat->setTextureStages( state, sgData );
  187. // Setup the vertex and index buffers.
  188. mat->setBuffers( passRI->vertBuff, passRI->primBuff );
  189. // Render this sucker.
  190. if ( passRI->prim )
  191. GFX->drawPrimitive( *passRI->prim );
  192. else
  193. GFX->drawPrimitive( passRI->primBuffIndex );
  194. }
  195. // Draw the instanced batch.
  196. if ( mat->isInstanced() )
  197. {
  198. // Sets the buffers including the instancing stream.
  199. mat->setBuffers( ri->vertBuff, ri->primBuff );
  200. // Render the instanced stream.
  201. if ( ri->prim )
  202. GFX->drawPrimitive( *ri->prim );
  203. else
  204. GFX->drawPrimitive( ri->primBuffIndex );
  205. }
  206. matListEnd = a;
  207. }
  208. // force increment if none happened, otherwise go to end of batch
  209. j = ( j == matListEnd ) ? j+1 : matListEnd;
  210. }
  211. }