OcclusionBuffer.h 8.0 KB

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