BsRenderQueue.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsVector3.h"
  6. #include "BsSubMesh.h"
  7. namespace bs { namespace ct
  8. {
  9. /** @addtogroup Renderer-Engine-Internal
  10. * @{
  11. */
  12. /**
  13. * Controls if and how a render queue groups renderable objects by material in order to reduce number of state changes.
  14. */
  15. enum class StateReduction
  16. {
  17. None, /**< No grouping based on material will be done. */
  18. Material, /**< Elements will be grouped by material first, by distance second. */
  19. Distance /**< Elements will be grouped by distance first, material second. */
  20. };
  21. /** Contains data needed for performing a single rendering pass. */
  22. struct BS_EXPORT RenderQueueElement
  23. {
  24. RenderQueueElement()
  25. :renderElem(nullptr), passIdx(0), applyPass(true)
  26. { }
  27. RenderableElement* renderElem;
  28. UINT32 passIdx;
  29. bool applyPass;
  30. };
  31. /**
  32. * Render objects determines rendering order of objects contained within it. Rendering order is determined by object
  33. * material, and can influence rendering of transparent or opaque objects, or be used to improve performance by grouping
  34. * similar objects together.
  35. */
  36. class BS_EXPORT RenderQueue
  37. {
  38. /** Data used for renderable element sorting. Represents a single pass for a single mesh. */
  39. struct SortableElement
  40. {
  41. UINT32 seqIdx;
  42. INT32 priority;
  43. float distFromCamera;
  44. UINT32 shaderId;
  45. UINT32 passIdx;
  46. };
  47. public:
  48. RenderQueue(StateReduction grouping = StateReduction::Distance);
  49. virtual ~RenderQueue() { }
  50. /**
  51. * Adds a new entry to the render queue.
  52. *
  53. * @param[in] element Renderable element to add to the queue.
  54. * @param[in] distFromCamera Distance of this object from the camera. Used for distance sorting.
  55. */
  56. void add(RenderableElement* element, float distFromCamera);
  57. /** Clears all render operations from the queue. */
  58. void clear();
  59. /** Sorts all the render operations using user-defined rules. */
  60. virtual void sort();
  61. /** Returns a list of sorted render elements. Caller must ensure sort() is called before this method. */
  62. const Vector<RenderQueueElement>& getSortedElements() const;
  63. /**
  64. * Controls if and how a render queue groups renderable objects by material in order to reduce number of state
  65. * changes.
  66. */
  67. void setStateReduction(StateReduction mode) { mStateReductionMode = mode; }
  68. protected:
  69. /** Callback used for sorting elements with no material grouping. */
  70. static bool elementSorterNoGroup(UINT32 aIdx, UINT32 bIdx, const Vector<SortableElement>& lookup);
  71. /** Callback used for sorting elements with preferred material grouping. */
  72. static bool elementSorterPreferGroup(UINT32 aIdx, UINT32 bIdx, const Vector<SortableElement>& lookup);
  73. /** Callback used for sorting elements with material grouping after sorting. */
  74. static bool elementSorterPreferSort(UINT32 aIdx, UINT32 bIdx, const Vector<SortableElement>& lookup);
  75. Vector<SortableElement> mSortableElements;
  76. Vector<UINT32> mSortableElementIdx;
  77. Vector<RenderableElement*> mElements;
  78. Vector<RenderQueueElement> mSortedRenderElements;
  79. StateReduction mStateReductionMode;
  80. };
  81. /** @} */
  82. }}