renderMeshMgr.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. docsURL;
  63. Parent::initPersistFields();
  64. }
  65. //-----------------------------------------------------------------------------
  66. // add element
  67. //-----------------------------------------------------------------------------
  68. void RenderMeshMgr::addElement( RenderInst *inst )
  69. {
  70. // If this instance is translucent handle it in RenderTranslucentMgr
  71. if (inst->translucentSort)
  72. return;
  73. AssertFatal( inst->defaultKey != 0, "RenderMeshMgr::addElement() - Got null sort key... did you forget to set it?" );
  74. internalAddElement(inst);
  75. }
  76. //-----------------------------------------------------------------------------
  77. // render
  78. //-----------------------------------------------------------------------------
  79. void RenderMeshMgr::render(SceneRenderState * state)
  80. {
  81. PROFILE_SCOPE(RenderMeshMgr_render);
  82. // Early out if nothing to draw.
  83. if(!mElementList.size())
  84. return;
  85. GFXDEBUGEVENT_SCOPE( RenderMeshMgr_Render, ColorI::GREEN );
  86. // Automagically save & restore our viewport and transforms.
  87. GFXTransformSaver saver;
  88. // Restore transforms
  89. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  90. matrixSet.restoreSceneViewProjection();
  91. // init loop data
  92. GFXTextureObject *lastLM = NULL;
  93. GFXCubemap *lastCubemap = NULL;
  94. GFXTextureObject *lastReflectTex = NULL;
  95. GFXTextureObject *lastMiscTex = NULL;
  96. GFXTextureObject *lastAccuTex = NULL;
  97. SceneData sgData;
  98. sgData.init( state );
  99. U32 binSize = mElementList.size();
  100. for( U32 j=0; j<binSize; )
  101. {
  102. MeshRenderInst *ri = static_cast<MeshRenderInst*>(mElementList[j].inst);
  103. setupSGData( ri, sgData );
  104. BaseMatInstance *mat = ri->matInst;
  105. // If we have an override delegate then give it a
  106. // chance to swap the material with another.
  107. if ( mMatOverrideDelegate )
  108. {
  109. mat = mMatOverrideDelegate( mat );
  110. if ( !mat )
  111. {
  112. j++;
  113. continue;
  114. }
  115. }
  116. if( !mat )
  117. mat = MATMGR->getWarningMatInstance();
  118. // Check if bin is disabled in advanced lighting.
  119. // Allow forward rendering pass on custom materials.
  120. if ( ( MATMGR->getDeferredEnabled() && mBasicOnly && !mat->isCustomMaterial() ) )
  121. {
  122. j++;
  123. continue;
  124. }
  125. U32 matListEnd = j;
  126. lastMiscTex = sgData.miscTex;
  127. U32 a;
  128. while( mat && mat->setupPass(state, sgData ) )
  129. {
  130. for( a=j; a<binSize; a++ )
  131. {
  132. MeshRenderInst *passRI = static_cast<MeshRenderInst*>(mElementList[a].inst);
  133. // Check to see if we need to break this batch.
  134. if ( newPassNeeded( ri, passRI ) ||
  135. lastMiscTex != passRI->miscTex )
  136. {
  137. lastLM = NULL;
  138. break;
  139. }
  140. matrixSet.setWorld(*passRI->objectToWorld);
  141. matrixSet.setView(*passRI->worldToCamera);
  142. matrixSet.setProjection(*passRI->projection);
  143. mat->setTransforms(matrixSet, state);
  144. // Setup HW skinning transforms if applicable
  145. if (mat->usesHardwareSkinning())
  146. {
  147. mat->setNodeTransforms(passRI->mNodeTransforms, passRI->mNodeTransformCount);
  148. }
  149. //push along any overriden fields that are instance-specific as well
  150. if (passRI->mCustomShaderData.size() > 0)
  151. {
  152. mat->setCustomShaderData(passRI->mCustomShaderData);
  153. }
  154. setupSGData( passRI, sgData );
  155. mat->setSceneInfo( state, sgData );
  156. // If we're instanced then don't render yet.
  157. if ( mat->isInstanced() )
  158. {
  159. // Let the material increment the instance buffer, but
  160. // break the batch if it runs out of room for more.
  161. if ( !mat->stepInstance() )
  162. {
  163. a++;
  164. break;
  165. }
  166. continue;
  167. }
  168. // TODO: This could proably be done in a cleaner way.
  169. //
  170. // This section of code is dangerous, it overwrites the
  171. // lightmap values in sgData. This could be a problem when multiple
  172. // render instances use the same multi-pass material. When
  173. // the first pass is done, setupPass() is called again on
  174. // the material, but the lightmap data has been changed in
  175. // sgData to the lightmaps in the last renderInstance rendered.
  176. // This section sets the lightmap data for the current batch.
  177. // For the first iteration, it sets the same lightmap data,
  178. // however the redundancy will be caught by GFXDevice and not
  179. // actually sent to the card. This is done for simplicity given
  180. // the possible condition mentioned above. Better to set always
  181. // than to get bogged down into special case detection.
  182. //-------------------------------------
  183. bool dirty = false;
  184. // set the lightmaps if different
  185. if( passRI->lightmap && passRI->lightmap != lastLM )
  186. {
  187. sgData.lightmap = passRI->lightmap;
  188. lastLM = passRI->lightmap;
  189. dirty = true;
  190. }
  191. // set the cubemap if different.
  192. if ( passRI->cubemap != lastCubemap )
  193. {
  194. sgData.cubemap = passRI->cubemap;
  195. lastCubemap = passRI->cubemap;
  196. dirty = true;
  197. }
  198. if ( passRI->reflectTex != lastReflectTex )
  199. {
  200. sgData.reflectTex = passRI->reflectTex;
  201. lastReflectTex = passRI->reflectTex;
  202. dirty = true;
  203. }
  204. // Update accumulation texture if it changed.
  205. // Note: accumulation texture can be NULL, and must be updated.
  206. if ( passRI->accuTex != lastAccuTex )
  207. {
  208. sgData.accuTex = passRI->accuTex;
  209. lastAccuTex = passRI->accuTex;
  210. dirty = true;
  211. }
  212. if ( dirty )
  213. mat->setTextureStages( state, sgData );
  214. // Setup the vertex and index buffers.
  215. mat->setBuffers( passRI->vertBuff, passRI->primBuff );
  216. // Render this sucker.
  217. if ( passRI->prim )
  218. GFX->drawPrimitive( *passRI->prim );
  219. else
  220. GFX->drawPrimitive( passRI->primBuffIndex );
  221. }
  222. // Draw the instanced batch.
  223. if ( mat->isInstanced() )
  224. {
  225. // Sets the buffers including the instancing stream.
  226. mat->setBuffers( ri->vertBuff, ri->primBuff );
  227. // Render the instanced stream.
  228. if ( ri->prim )
  229. GFX->drawPrimitive( *ri->prim );
  230. else
  231. GFX->drawPrimitive( ri->primBuffIndex );
  232. }
  233. matListEnd = a;
  234. }
  235. // force increment if none happened, otherwise go to end of batch
  236. j = ( j == matListEnd ) ? j+1 : matListEnd;
  237. }
  238. }