2
0

renderBinManager.cpp 6.2 KB

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