OcclusionBuffer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Core/Object.h"
  6. #include "../Core/Timer.h"
  7. #include "../Container/ArrayPtr.h"
  8. #include "../GraphicsAPI/GraphicsDefs.h"
  9. #include "../Math/Frustum.h"
  10. namespace Urho3D
  11. {
  12. class BoundingBox;
  13. class Camera;
  14. class IndexBuffer;
  15. class IntRect;
  16. class VertexBuffer;
  17. struct Edge;
  18. struct Gradients;
  19. /// Occlusion hierarchy depth value.
  20. struct DepthValue
  21. {
  22. /// Minimum value.
  23. int min_;
  24. /// Maximum value.
  25. int max_;
  26. };
  27. /// Per-thread occlusion buffer data.
  28. struct OcclusionBufferData
  29. {
  30. /// Full buffer data with safety padding.
  31. SharedArrayPtr<int> dataWithSafety_;
  32. /// Buffer data.
  33. int* data_;
  34. /// Use flag.
  35. bool used_;
  36. };
  37. /// Stored occlusion render job.
  38. struct OcclusionBatch
  39. {
  40. /// Model matrix.
  41. Matrix3x4 model_;
  42. /// Vertex data pointer.
  43. const void* vertexData_;
  44. /// Vertex size in bytes.
  45. unsigned vertexSize_;
  46. /// Index data pointer. Null if using non-indexed geometry.
  47. const void* indexData_;
  48. /// Index size in bytes.
  49. unsigned indexSize_;
  50. /// Draw start. First index for indexed geometry, otherwise first vertex.
  51. unsigned drawStart_;
  52. /// Index or vertex count.
  53. unsigned drawCount_;
  54. };
  55. /// Software renderer for occlusion.
  56. class URHO3D_API OcclusionBuffer : public Object
  57. {
  58. URHO3D_OBJECT(OcclusionBuffer, Object);
  59. public:
  60. /// Construct.
  61. explicit OcclusionBuffer(Context* context);
  62. /// Destruct.
  63. ~OcclusionBuffer() override;
  64. /// Set occlusion buffer size and whether to reserve multiple buffers for threading optimization.
  65. bool SetSize(int width, int height, bool threaded);
  66. /// Set camera view to render from.
  67. void SetView(Camera* camera);
  68. /// Set maximum triangles to render.
  69. void SetMaxTriangles(unsigned triangles);
  70. /// Set culling mode.
  71. void SetCullMode(CullMode mode);
  72. /// Reset number of triangles.
  73. void Reset();
  74. /// Clear the buffer.
  75. void Clear();
  76. /// Submit a triangle mesh to the buffer using non-indexed geometry. Return true if did not overflow the allowed triangle count.
  77. bool AddTriangles(const Matrix3x4& model, const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount);
  78. /// Submit a triangle mesh to the buffer using indexed geometry. Return true if did not overflow the allowed triangle count.
  79. bool AddTriangles(const Matrix3x4& model, const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize,
  80. unsigned indexStart, unsigned indexCount);
  81. /// Draw submitted batches. Uses worker threads if enabled during SetSize().
  82. void DrawTriangles();
  83. /// Build reduced size mip levels.
  84. void BuildDepthHierarchy();
  85. /// Reset last used timer.
  86. void ResetUseTimer();
  87. /// Return highest level depth values.
  88. int* GetBuffer() const { return buffers_.Size() ? buffers_[0].data_ : nullptr; }
  89. /// Return view transform matrix.
  90. const Matrix3x4& GetView() const { return view_; }
  91. /// Return projection matrix.
  92. const Matrix4& GetProjection() const { return projection_; }
  93. /// Return buffer width.
  94. int GetWidth() const { return width_; }
  95. /// Return buffer height.
  96. int GetHeight() const { return height_; }
  97. /// Return number of rendered triangles.
  98. unsigned GetNumTriangles() const { return numTriangles_; }
  99. /// Return maximum number of triangles.
  100. unsigned GetMaxTriangles() const { return maxTriangles_; }
  101. /// Return culling mode.
  102. CullMode GetCullMode() const { return cullMode_; }
  103. /// Return whether is using threads to speed up rendering.
  104. bool IsThreaded() const { return buffers_.Size() > 1; }
  105. /// Test a bounding box for visibility. For best performance, build depth hierarchy first.
  106. bool IsVisible(const BoundingBox& worldSpaceBox) const;
  107. /// Return time since last use in milliseconds.
  108. unsigned GetUseTimer();
  109. /// Draw a batch. Called internally.
  110. void DrawBatch(const OcclusionBatch& batch, i32 threadIndex);
  111. private:
  112. /// Apply modelview transform to vertex.
  113. inline Vector4 ModelTransform(const Matrix4& transform, const Vector3& vertex) const;
  114. /// Apply projection and viewport transform to vertex.
  115. inline Vector3 ViewportTransform(const Vector4& vertex) const;
  116. /// Clip an edge.
  117. inline Vector4 ClipEdge(const Vector4& v0, const Vector4& v1, float d0, float d1) const;
  118. /// Return signed area of a triangle. If negative, is clockwise.
  119. inline float SignedArea(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
  120. /// Calculate viewport transform.
  121. void CalculateViewport();
  122. /// Draw a triangle.
  123. void DrawTriangle(Vector4* vertices, i32 threadIndex);
  124. /// Clip vertices against a plane.
  125. void ClipVertices(const Vector4& plane, Vector4* vertices, bool* triangles, unsigned& numTriangles);
  126. /// Draw a clipped triangle.
  127. void DrawTriangle2D(const Vector3* vertices, bool clockwise, i32 threadIndex);
  128. /// Clear a thread work buffer.
  129. void ClearBuffer(i32 threadIndex);
  130. /// Merge thread work buffers into the first buffer.
  131. void MergeBuffers();
  132. /// Highest-level buffer data per thread.
  133. Vector<OcclusionBufferData> buffers_;
  134. /// Reduced size depth buffers.
  135. Vector<SharedArrayPtr<DepthValue>> mipBuffers_;
  136. /// Submitted render jobs.
  137. Vector<OcclusionBatch> batches_;
  138. /// Buffer width.
  139. int width_{};
  140. /// Buffer height.
  141. int height_{};
  142. /// Number of rendered triangles.
  143. unsigned numTriangles_{};
  144. /// Maximum number of triangles.
  145. unsigned maxTriangles_;
  146. /// Culling mode.
  147. CullMode cullMode_{CULL_CCW};
  148. /// Depth hierarchy needs update flag.
  149. bool depthHierarchyDirty_{true};
  150. /// Culling reverse flag.
  151. bool reverseCulling_{};
  152. /// View transform matrix.
  153. Matrix3x4 view_;
  154. /// Projection matrix.
  155. Matrix4 projection_;
  156. /// Combined view and projection matrix.
  157. Matrix4 viewProj_;
  158. /// Last used timer.
  159. Timer useTimer_;
  160. /// Near clip distance.
  161. float nearClip_{};
  162. /// Far clip distance.
  163. float farClip_{};
  164. /// X scaling for viewport transform.
  165. float scaleX_{};
  166. /// Y scaling for viewport transform.
  167. float scaleY_{};
  168. /// X offset for viewport transform.
  169. float offsetX_{};
  170. /// Y offset for viewport transform.
  171. float offsetY_{};
  172. /// Combined X projection and viewport transform.
  173. float projOffsetScaleX_{};
  174. /// Combined Y projection and viewport transform.
  175. float projOffsetScaleY_{};
  176. };
  177. }