renderBinManager.cpp 6.2 KB

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