renderMeshMgr.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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->getDeferredEnabled() && 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. // Setup HW skinning transforms if applicable
  144. if (mat->usesHardwareSkinning())
  145. {
  146. mat->setNodeTransforms(passRI->mNodeTransforms, passRI->mNodeTransformCount);
  147. }
  148. //push along any overriden fields that are instance-specific as well
  149. if (passRI->mCustomShaderData.size() > 0)
  150. {
  151. mat->setCustomShaderData(passRI->mCustomShaderData);
  152. }
  153. setupSGData( passRI, sgData );
  154. mat->setSceneInfo( state, sgData );
  155. // If we're instanced then don't render yet.
  156. if ( mat->isInstanced() )
  157. {
  158. // Let the material increment the instance buffer, but
  159. // break the batch if it runs out of room for more.
  160. if ( !mat->stepInstance() )
  161. {
  162. a++;
  163. break;
  164. }
  165. continue;
  166. }
  167. // TODO: This could proably be done in a cleaner way.
  168. //
  169. // This section of code is dangerous, it overwrites the
  170. // lightmap values in sgData. This could be a problem when multiple
  171. // render instances use the same multi-pass material. When
  172. // the first pass is done, setupPass() is called again on
  173. // the material, but the lightmap data has been changed in
  174. // sgData to the lightmaps in the last renderInstance rendered.
  175. // This section sets the lightmap data for the current batch.
  176. // For the first iteration, it sets the same lightmap data,
  177. // however the redundancy will be caught by GFXDevice and not
  178. // actually sent to the card. This is done for simplicity given
  179. // the possible condition mentioned above. Better to set always
  180. // than to get bogged down into special case detection.
  181. //-------------------------------------
  182. bool dirty = false;
  183. // set the lightmaps if different
  184. if( passRI->lightmap && passRI->lightmap != lastLM )
  185. {
  186. sgData.lightmap = passRI->lightmap;
  187. lastLM = passRI->lightmap;
  188. dirty = true;
  189. }
  190. // set the cubemap if different.
  191. if ( passRI->cubemap != lastCubemap )
  192. {
  193. sgData.cubemap = passRI->cubemap;
  194. lastCubemap = passRI->cubemap;
  195. dirty = true;
  196. }
  197. if ( passRI->reflectTex != lastReflectTex )
  198. {
  199. sgData.reflectTex = passRI->reflectTex;
  200. lastReflectTex = passRI->reflectTex;
  201. dirty = true;
  202. }
  203. // Update accumulation texture if it changed.
  204. // Note: accumulation texture can be NULL, and must be updated.
  205. if ( passRI->accuTex != lastAccuTex )
  206. {
  207. sgData.accuTex = passRI->accuTex;
  208. lastAccuTex = passRI->accuTex;
  209. dirty = true;
  210. }
  211. if ( dirty )
  212. mat->setTextureStages( state, sgData );
  213. // Setup the vertex and index buffers.
  214. mat->setBuffers( passRI->vertBuff, passRI->primBuff );
  215. // Render this sucker.
  216. if ( passRI->prim )
  217. GFX->drawPrimitive( *passRI->prim );
  218. else
  219. GFX->drawPrimitive( passRI->primBuffIndex );
  220. }
  221. // Draw the instanced batch.
  222. if ( mat->isInstanced() )
  223. {
  224. // Sets the buffers including the instancing stream.
  225. mat->setBuffers( ri->vertBuff, ri->primBuff );
  226. // Render the instanced stream.
  227. if ( ri->prim )
  228. GFX->drawPrimitive( *ri->prim );
  229. else
  230. GFX->drawPrimitive( ri->primBuffIndex );
  231. }
  232. matListEnd = a;
  233. }
  234. // force increment if none happened, otherwise go to end of batch
  235. j = ( j == matListEnd ) ? j+1 : matListEnd;
  236. }
  237. }