renderBinManager.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/renderBinManager.h"
  24. #include "console/consoleTypes.h"
  25. #include "materials/matInstance.h"
  26. #include "scene/sceneManager.h"
  27. #include "console/engineAPI.h"
  28. IMPLEMENT_CONOBJECT(RenderBinManager);
  29. RenderBinManager::RenderBinManager( const RenderInstType& ritype, F32 renderOrder, F32 processAddOrder ) :
  30. mRenderInstType( ritype ),
  31. mRenderOrder( renderOrder ),
  32. mProcessAddOrder( processAddOrder ),
  33. mRenderPass( NULL )
  34. {
  35. VECTOR_SET_ASSOCIATION( mElementList );
  36. mElementList.reserve( 2048 );
  37. }
  38. ConsoleDocClass( RenderBinManager,
  39. "@brief The abstract base for all render bins.\n\n"
  40. "The render bins are used by the engine as a high level method to order and batch rendering "
  41. "operations.\n"
  42. "@ingroup RenderBin\n" );
  43. void RenderBinManager::initPersistFields()
  44. {
  45. addField( "binType", TypeRealString, Offset(mRenderInstType.mName, RenderBinManager),
  46. "Sets the render bin type which limits what render instances are added to this bin." );
  47. addField("renderOrder", TypeF32, Offset(mRenderOrder, RenderBinManager),
  48. "Defines the order for rendering in relation to other bins." );
  49. addField("processAddOrder", TypeF32, Offset(mProcessAddOrder, RenderBinManager),
  50. "Defines the order for adding instances in relation to other bins." );
  51. Parent::initPersistFields();
  52. }
  53. void RenderBinManager::onRemove()
  54. {
  55. // Tell the render pass to remove us when
  56. // we're being unregistered.
  57. if ( mRenderPass )
  58. mRenderPass->removeManager( this );
  59. Parent::onRemove();
  60. }
  61. void RenderBinManager::notifyType( const RenderInstType &type )
  62. {
  63. // Avoid duplicate types.
  64. if ( !type.isValid() ||
  65. mRenderInstType == type ||
  66. mOtherTypes.contains( type ) )
  67. return;
  68. mOtherTypes.push_back( type );
  69. // Register for the signal if the pass
  70. // has already been assigned.
  71. if ( mRenderPass )
  72. mRenderPass->getAddSignal(type).notify( this, &RenderBinManager::addElement, mProcessAddOrder );
  73. }
  74. void RenderBinManager::setRenderPass( RenderPassManager *rpm )
  75. {
  76. if ( mRenderPass )
  77. {
  78. if ( mRenderInstType.isValid() )
  79. mRenderPass->getAddSignal(mRenderInstType).remove( this, &RenderBinManager::addElement );
  80. for ( U32 i=0; i < mOtherTypes.size(); i++ )
  81. mRenderPass->getAddSignal(mOtherTypes[i]).remove( this, &RenderBinManager::addElement );
  82. }
  83. mRenderPass = rpm;
  84. if ( mRenderPass )
  85. {
  86. if ( mRenderInstType.isValid() )
  87. mRenderPass->getAddSignal(mRenderInstType).notify( this, &RenderBinManager::addElement, mProcessAddOrder );
  88. for ( U32 i=0; i < mOtherTypes.size(); i++ )
  89. mRenderPass->getAddSignal(mOtherTypes[i]).notify( this, &RenderBinManager::addElement, mProcessAddOrder );
  90. }
  91. }
  92. void RenderBinManager::addElement( RenderInst *inst )
  93. {
  94. internalAddElement(inst);
  95. }
  96. void RenderBinManager::internalAddElement(RenderInst* inst)
  97. {
  98. mElementList.increment();
  99. MainSortElem &elem = mElementList.last();
  100. elem.inst = inst;
  101. elem.key = inst->defaultKey;
  102. elem.key2 = inst->defaultKey2;
  103. }
  104. void RenderBinManager::clear()
  105. {
  106. mElementList.clear();
  107. }
  108. void RenderBinManager::sort()
  109. {
  110. dQsort( mElementList.address(), mElementList.size(), sizeof(MainSortElem), cmpKeyFunc);
  111. }
  112. S32 FN_CDECL RenderBinManager::cmpKeyFunc(const void* p1, const void* p2)
  113. {
  114. const MainSortElem* mse1 = (const MainSortElem*) p1;
  115. const MainSortElem* mse2 = (const MainSortElem*) p2;
  116. S32 test1 = S32(mse2->key) - S32(mse1->key);
  117. return ( test1 == 0 ) ? S32(mse1->key2) - S32(mse2->key2) : test1;
  118. }
  119. void RenderBinManager::setupSGData( MeshRenderInst *ri, SceneData &data )
  120. {
  121. PROFILE_SCOPE( RenderBinManager_setupSGData );
  122. // NOTE: We do not reset or clear the scene state
  123. // here as the caller has initialized non-RI members
  124. // himself and we must preserve them.
  125. //
  126. // It also saves a bunch of CPU as this is called for
  127. // every MeshRenderInst in every pass.
  128. dMemcpy( data.lights, ri->lights, sizeof( data.lights ) );
  129. data.objTrans = ri->objectToWorld;
  130. data.backBuffTex = ri->backBuffTex;
  131. data.cubemap = ri->cubemap;
  132. data.miscTex = ri->miscTex;
  133. data.reflectTex = ri->reflectTex;
  134. data.lightmap = ri->lightmap;
  135. data.visibility = ri->visibility;
  136. data.materialHint = ri->materialHint;
  137. }
  138. DefineEngineMethod( RenderBinManager, getBinType, const char*, (),,
  139. "Returns the bin type string." )
  140. {
  141. return object->getRenderInstType().getName();
  142. }