OcclusionBuffer.h 8.0 KB

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