renderBinManager.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef _RENDERBINMANAGER_H_
  23. #define _RENDERBINMANAGER_H_
  24. #ifndef _CONSOLEOBJECT_H_
  25. #include "console/consoleObject.h"
  26. #endif
  27. #ifndef _RENDERPASSMANAGER_H_
  28. #include "renderInstance/renderPassManager.h"
  29. #endif
  30. #ifndef _BASEMATINSTANCE_H_
  31. #include "materials/baseMatInstance.h"
  32. #endif
  33. #ifndef _UTIL_DELEGATE_H_
  34. #include "core/util/delegate.h"
  35. #endif
  36. class SceneRenderState;
  37. /// This delegate is used in derived RenderBinManager classes
  38. /// to allow material instances to be overriden.
  39. typedef Delegate<BaseMatInstance*(BaseMatInstance*)> MaterialOverrideDelegate;
  40. /// The RenderBinManager manages and renders lists of MainSortElem, which
  41. /// is a light wrapper around RenderInst.
  42. class RenderBinManager : public SimObject
  43. {
  44. typedef SimObject Parent;
  45. friend class RenderPassManager;
  46. public:
  47. RenderBinManager( const RenderInstType& ritype = RenderInstType::Invalid,
  48. F32 renderOrder = 1.0f,
  49. F32 processAddOrder = 1.0f );
  50. virtual ~RenderBinManager() {}
  51. // SimObject
  52. void onRemove();
  53. virtual void addElement( RenderInst *inst );
  54. virtual void sort();
  55. virtual void render( SceneRenderState *state ) {}
  56. virtual void clear();
  57. // Manager info
  58. F32 getProcessAddOrder() const { return mProcessAddOrder; }
  59. void setProcessAddOrder(F32 processAddOrder) { mProcessAddOrder = processAddOrder; }
  60. F32 getRenderOrder() const { return mRenderOrder; }
  61. void setRenderOrder(F32 renderOrder) { mRenderOrder = renderOrder; }
  62. /// Returns the primary render instance type.
  63. const RenderInstType& getRenderInstType() { return mRenderInstType; }
  64. /// Returns the render pass this bin is registered to.
  65. RenderPassManager* getRenderPass() const { return mRenderPass; }
  66. /// QSort callback function
  67. static S32 FN_CDECL cmpKeyFunc(const void* p1, const void* p2);
  68. DECLARE_CONOBJECT(RenderBinManager);
  69. static void initPersistFields();
  70. MaterialOverrideDelegate& getMatOverrideDelegate() { return mMatOverrideDelegate; }
  71. struct MainSortElem
  72. {
  73. RenderInst *inst;
  74. U32 key;
  75. U32 key2;
  76. };
  77. protected:
  78. void setRenderPass( RenderPassManager *rpm );
  79. /// Called from derived bins to add additional
  80. /// render instance types to be notified about.
  81. void notifyType( const RenderInstType &type );
  82. Vector< MainSortElem > mElementList; // List of our instances
  83. F32 mProcessAddOrder; // Where in the list do we process RenderInstance additions?
  84. F32 mRenderOrder; // Where in the list do we render?
  85. /// The primary render instance type this bin supports.
  86. RenderInstType mRenderInstType;
  87. /// The list of additional render instance types
  88. /// this bin wants to process.
  89. Vector<RenderInstType> mOtherTypes;
  90. /// The render pass manager this bin is registered with.
  91. RenderPassManager *mRenderPass;
  92. MaterialOverrideDelegate mMatOverrideDelegate;
  93. virtual void setupSGData(MeshRenderInst *ri, SceneData &data );
  94. virtual void internalAddElement(RenderInst* inst);
  95. /// A inlined helper method for testing if the next
  96. /// MeshRenderInst requires a new batch/pass.
  97. inline bool newPassNeeded( MeshRenderInst *ri, MeshRenderInst* nextRI ) const;
  98. /// Inlined utility function which gets the material from the
  99. /// RenderInst if available, otherwise, return NULL.
  100. inline BaseMatInstance* getMaterial( RenderInst *inst ) const;
  101. // Limits bin to rendering in basic lighting only.
  102. bool mBasicOnly;
  103. };
  104. inline bool RenderBinManager::newPassNeeded( MeshRenderInst *ri, MeshRenderInst* nextRI ) const
  105. {
  106. if ( ri == nextRI )
  107. return false;
  108. // We can depend completely on the state hint to check
  109. // for changes in the material as it uniquely identifies it.
  110. if ( ri->matInst->getStateHint() != nextRI->matInst->getStateHint() )
  111. return true;
  112. if ( ri->vertBuff != nextRI->vertBuff ||
  113. ri->primBuff != nextRI->primBuff ||
  114. ri->prim != nextRI->prim ||
  115. ri->primBuffIndex != nextRI->primBuffIndex ||
  116. // NOTE: Keep an eye on this... should we find a more
  117. // optimal test for light set changes?
  118. //
  119. dMemcmp( ri->lights, nextRI->lights, sizeof( ri->lights ) ) != 0 )
  120. return true;
  121. return false;
  122. }
  123. inline BaseMatInstance* RenderBinManager::getMaterial( RenderInst *inst ) const
  124. {
  125. if ( inst->type == RenderPassManager::RIT_Mesh ||
  126. inst->type == RenderPassManager::RIT_Decal ||
  127. inst->type == RenderPassManager::RIT_DecalRoad ||
  128. inst->type == RenderPassManager::RIT_Translucent )
  129. return static_cast<MeshRenderInst*>(inst)->matInst;
  130. return NULL;
  131. }
  132. #endif // _RENDERBINMANAGER_H_