Renderer2D.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/Drawable.h"
  24. #include "../Math/Frustum.h"
  25. namespace Urho3D
  26. {
  27. class Drawable2D;
  28. class IndexBuffer;
  29. class Material;
  30. class Technique;
  31. class VertexBuffer;
  32. struct FrameInfo;
  33. struct SourceBatch2D;
  34. /// 2D view batch info.
  35. /// @nobind
  36. struct ViewBatchInfo2D
  37. {
  38. /// Construct.
  39. ViewBatchInfo2D();
  40. /// Vertex buffer update frame number.
  41. unsigned vertexBufferUpdateFrameNumber_;
  42. /// Index count.
  43. unsigned indexCount_;
  44. /// Vertex count.
  45. unsigned vertexCount_;
  46. /// Vertex buffer.
  47. SharedPtr<VertexBuffer> vertexBuffer_;
  48. /// Batch updated frame number.
  49. unsigned batchUpdatedFrameNumber_;
  50. /// Source batches.
  51. PODVector<const SourceBatch2D*> sourceBatches_;
  52. /// Batch count.
  53. unsigned batchCount_;
  54. /// Distances.
  55. PODVector<float> distances_;
  56. /// Materials.
  57. Vector<SharedPtr<Material> > materials_;
  58. /// Geometries.
  59. Vector<SharedPtr<Geometry> > geometries_;
  60. };
  61. /// 2D renderer component.
  62. class URHO3D_API Renderer2D : public Drawable
  63. {
  64. URHO3D_OBJECT(Renderer2D, Drawable);
  65. friend void CheckDrawableVisibilityWork(const WorkItem* item, unsigned threadIndex);
  66. public:
  67. /// Construct.
  68. explicit Renderer2D(Context* context);
  69. /// Destruct.
  70. ~Renderer2D() override;
  71. /// Register object factory.
  72. static void RegisterObject(Context* context);
  73. /// Process octree raycast. May be called from a worker thread.
  74. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  75. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  76. void UpdateBatches(const FrameInfo& frame) override;
  77. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update).
  78. void UpdateGeometry(const FrameInfo& frame) override;
  79. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  80. UpdateGeometryType GetUpdateGeometryType() override;
  81. /// Add Drawable2D.
  82. void AddDrawable(Drawable2D* drawable);
  83. /// Remove Drawable2D.
  84. void RemoveDrawable(Drawable2D* drawable);
  85. /// Return material by texture and blend mode.
  86. Material* GetMaterial(Texture2D* texture, BlendMode blendMode);
  87. /// Check visibility.
  88. bool CheckVisibility(Drawable2D* drawable) const;
  89. private:
  90. /// Recalculate the world-space bounding box.
  91. void OnWorldBoundingBoxUpdate() override;
  92. /// Create material by texture and blend mode.
  93. SharedPtr<Material> CreateMaterial(Texture2D* texture, BlendMode blendMode);
  94. /// Handle view update begin event. Determine Drawable2D's and their batches here.
  95. void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
  96. /// Get all drawables in node.
  97. void GetDrawables(PODVector<Drawable2D*>& drawables, Node* node);
  98. /// Update view batch info.
  99. void UpdateViewBatchInfo(ViewBatchInfo2D& viewBatchInfo, Camera* camera);
  100. /// Add view batch.
  101. void AddViewBatch(ViewBatchInfo2D& viewBatchInfo, Material* material,
  102. unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount, float distance);
  103. /// Index buffer.
  104. SharedPtr<IndexBuffer> indexBuffer_;
  105. /// Material.
  106. SharedPtr<Material> material_;
  107. /// Drawables.
  108. PODVector<Drawable2D*> drawables_;
  109. /// View frame info for current frame.
  110. FrameInfo frame_;
  111. /// View batch info.
  112. HashMap<Camera*, ViewBatchInfo2D> viewBatchInfos_;
  113. /// Frustum for current frame.
  114. Frustum frustum_;
  115. /// View mask of current camera for visibility checking.
  116. unsigned viewMask_;
  117. /// Cached materials.
  118. HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > > cachedMaterials_;
  119. /// Cached techniques per blend mode.
  120. HashMap<int, SharedPtr<Technique> > cachedTechniques_;
  121. };
  122. }