View.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Batch.h"
  25. #include "Object.h"
  26. class Camera;
  27. class DebugRenderer;
  28. class Light;
  29. class Drawable;
  30. class OcclusionBuffer;
  31. class RenderSurface;
  32. class Zone;
  33. struct Viewport;
  34. static const int MAX_LIGHT_SPLITS = 6;
  35. /// Geometry view space depth minimum and maximum values
  36. struct GeometryDepthBounds
  37. {
  38. float min_;
  39. float max_;
  40. };
  41. /// Helper structure for checking whether a transparent object is already lit by a certain light
  42. struct LitTransparencyCheck
  43. {
  44. /// Construct undefined
  45. LitTransparencyCheck()
  46. {
  47. }
  48. /// Construct
  49. LitTransparencyCheck(Light* light, Drawable* drawable, unsigned batchIndex) :
  50. light_(light),
  51. drawable_(drawable),
  52. batchIndex_(batchIndex)
  53. {
  54. }
  55. /// Test for equality with another lit transparency check
  56. bool operator == (const LitTransparencyCheck& rhs) const { return (light_ == rhs.light_) && (drawable_ == rhs.drawable_) && (batchIndex_ == rhs.batchIndex_); }
  57. /// Test for inequality with another lit transparency check
  58. bool operator != (const LitTransparencyCheck& rhs) const { return (light_ != rhs.light_) || (drawable_ != rhs.drawable_) || (batchIndex_ != rhs.batchIndex_); }
  59. /// Test if less than another lit transparency check
  60. bool operator < (const LitTransparencyCheck& rhs) const
  61. {
  62. if (light_ == rhs.light_)
  63. {
  64. if (drawable_ == rhs.drawable_)
  65. return batchIndex_ < rhs.batchIndex_;
  66. else
  67. return drawable_ < rhs.drawable_;
  68. }
  69. else
  70. return light_ < rhs.light_;
  71. }
  72. /// Test if greater than another lit transparency check
  73. bool operator > (const LitTransparencyCheck& rhs) const
  74. {
  75. if (light_ == rhs.light_)
  76. {
  77. if (drawable_ == rhs.drawable_)
  78. return batchIndex_ > rhs.batchIndex_;
  79. else
  80. return drawable_ > rhs.drawable_;
  81. }
  82. else
  83. return light_ > rhs.light_;
  84. }
  85. Light* light_;
  86. Drawable* drawable_;
  87. unsigned batchIndex_;
  88. };
  89. /// A rendering view. Includes the main view(s) and any auxiliary views, but not shadow cameras
  90. class View : public Object
  91. {
  92. OBJECT(View);
  93. public:
  94. /// Construct
  95. View(Context* context);
  96. /// Destruct
  97. virtual ~View();
  98. /// Define with rendertarget and viewport. Return true if successful
  99. bool Define(RenderSurface* renderTarget, const Viewport& viewport);
  100. /// Update culling and construct rendering batches
  101. void Update(const FrameInfo& frame);
  102. /// Render batches
  103. void Render();
  104. /// Return octree
  105. Octree* GetOctree() const { return octree_; }
  106. /// Return camera
  107. Camera* GetCamera() const { return camera_; }
  108. /// Return zone
  109. Zone* GetZone() const { return zone_; }
  110. /// Return the render target. 0 if using the backbuffer
  111. RenderSurface* GetRenderTarget() const { return renderTarget_; }
  112. /// Return the depth stencil. 0 if using the backbuffer's depth stencil
  113. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  114. /// Return geometry objects
  115. const Vector<Drawable*>& GetGeometries() const { return geometries_; }
  116. /// Return occluder objects
  117. const Vector<Drawable*>& GetOccluders() const { return occluders_; }
  118. /// Return directional light shadow rendering occluders
  119. const Vector<Drawable*>& GetShadowOccluders() const { return shadowOccluders_; }
  120. /// Return lights
  121. const Vector<Light*>& GetLights() const { return lights_; }
  122. /// Return light batch queues
  123. const Vector<LightBatchQueue>& GetLightQueues() const { return lightQueues_; }
  124. private:
  125. /// Query the octree for scene nodes
  126. void GetDrawables();
  127. /// Construct batches from the scene nodes
  128. void GetBatches();
  129. /// Get lit batches for a certain light and drawable
  130. void GetLitBatches(Drawable* drawable, Light* light, Light* SplitLight, LightBatchQueue* lightQueue, Set<LitTransparencyCheck>& litTransparencies, PassType gBufferPass);
  131. /// Render batches, forward mode
  132. void RenderBatcheforward();
  133. /// Render batches, deferred mode
  134. void RenderBatchesDeferred();
  135. /// Query for occluders as seen from a camera
  136. void UpdateOccluders(Vector<Drawable*>& occluders, Camera* camera);
  137. /// Draw occluders to occlusion buffer
  138. void DrawOccluders(OcclusionBuffer* buffer, const Vector<Drawable*>& occluders);
  139. /// Query for lit geometries and shadow casters for a light
  140. unsigned ProcessLight(Light* light);
  141. /// Generate combined bounding boxes for lit geometries and shadow casters and check shadow caster visibility
  142. void ProcessLightQuery(unsigned splitIndex, const Vector<Drawable*>& result, BoundingBox& geometryBox, BoundingBox& shadowSpaceBox, bool getLitGeometries, bool GetShadowCasters);
  143. /// Check visibility of one shadow caster
  144. bool IsShadowCasterVisible(Drawable* drawable, BoundingBox lightViewBox, Camera* shadowCamera, const Matrix4x3& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  145. /// Set up initial shadow camera view
  146. void SetupShadowCamera(Light* light, bool shadowOcclusion = false);
  147. /// Focus shadow camera to use shadow map texture space more optimally
  148. void FocusShadowCamera(Light* light, const BoundingBox& geometryBox, const BoundingBox& shadowCasterBox);
  149. /// Quantize the directional light shadow camera view to eliminate artefacts
  150. void QuantizeDirShadowCamera(Light* light, const BoundingBox& viewBox);
  151. /// Optimize light rendering by setting up a scissor rectangle
  152. void OptimizeLightByScissor(Light* light);
  153. /// Return scissor rectangle for a light
  154. const Rect& GetLightScissor(Light* light);
  155. /// Split directional or point light for shadow rendering
  156. unsigned SplitLight(Light* light);
  157. /// Return material technique, considering the drawable's LOD distance
  158. Technique* GetTechnique(Drawable* drawable, Material*& material);
  159. /// Check if material should render an auxiliary view (if it has a camera attached)
  160. void CheckMaterialForAuxView(Material* material);
  161. /// Sort all batches
  162. void SortBatches();
  163. /// Prepare instancing buffer by filling it with all instance transforms
  164. void PrepareInstancingBuffer();
  165. /// Set per-view shader parameters
  166. void SetShaderParameters();
  167. /// Draw a split light to stencil buffer
  168. void DrawSplitLightToStencil(Camera& camera, Light* light, bool clear = false);
  169. /// Draw everything in a batch queue, priority batches first
  170. void RenderBatchQueue(const BatchQueue& queue, bool useScissor = false, bool useLightBuffer = false);
  171. /// Draw a forward (shadowed) light batch queue
  172. void RenderForwardLightBatchQueue(const BatchQueue& queue, Light* forwardQueueLight);
  173. /// Render a shadow map
  174. void RenderShadowMap(const LightBatchQueue& queue);
  175. /// Graphics
  176. WeakPtr<Graphics> graphics_;
  177. /// Renderer
  178. WeakPtr<Renderer> renderer_;
  179. /// Octree to use
  180. Octree* octree_;
  181. /// Camera to use
  182. Camera* camera_;
  183. /// Zone to get global rendering settings from
  184. Zone* zone_;
  185. /// Color buffer to use
  186. RenderSurface* renderTarget_;
  187. /// Depth buffer to use
  188. RenderSurface* depthStencil_;
  189. /// Screen rectangle
  190. IntRect screenRect_;
  191. /// Render target width
  192. int width_;
  193. /// Render target height
  194. int height_;
  195. /// Rendering mode
  196. RenderMode mode_;
  197. /// Draw shadows flag
  198. bool drawShadows_;
  199. /// Shader Model 3 support flag
  200. bool hasSM3_;
  201. /// Material quality level
  202. int materialQuality_;
  203. /// Maximum number of occluder triangles
  204. int maxOccluderTriangles_;
  205. /// Jitter counter for temporal antialiasing
  206. int jitterCounter_;
  207. /// Previous view matrix for temporal antialiasing
  208. Matrix4x3 lastCameraView_;
  209. /// Information of the frame being rendered
  210. FrameInfo frame_;
  211. /// Combined bounding box of visible geometries
  212. BoundingBox sceneBox_;
  213. /// Combined bounding box of visible geometries in view space
  214. BoundingBox sceneViewBox_;
  215. /// Cache for light scissor queries
  216. Map<Light*, Rect> lightScissorCache_;
  217. /// Current split lights being processed
  218. Light* splitLights_[MAX_LIGHT_SPLITS];
  219. /// Current lit geometries being processed
  220. Vector<Drawable*> litGeometries_[MAX_LIGHT_SPLITS];
  221. /// Current shadow casters being processed
  222. Vector<Drawable*> shadowCasters_[MAX_LIGHT_SPLITS];
  223. /// Temporary drawable query result
  224. Vector<Drawable*> tempDrawables_;
  225. /// Geometry objects
  226. Vector<Drawable*> geometries_;
  227. /// Occluder objects
  228. Vector<Drawable*> occluders_;
  229. /// Directional light shadow rendering occluders
  230. Vector<Drawable*> shadowOccluders_;
  231. /// Depth minimum and maximum values for visible geometries
  232. Vector<GeometryDepthBounds> geometryDepthBounds_;
  233. /// Lights
  234. Vector<Light*> lights_;
  235. /// G-buffer size error displayed
  236. Set<RenderSurface*> gBufferErrorDisplayed_;
  237. /// G-buffer batches
  238. BatchQueue gBufferQueue_;
  239. /// Base pass batches
  240. BatchQueue baseQueue_;
  241. /// Extra pass batches
  242. BatchQueue extraQueue_;
  243. /// Transparent geometry batches
  244. BatchQueue transparentQueue_;
  245. /// Unshadowed light volume batches
  246. BatchQueue noShadowLightQueue_;
  247. /// Shadowed light queues
  248. Vector<LightBatchQueue> lightQueues_;
  249. };