View.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "HashSet.h"
  26. #include "Object.h"
  27. #include "Set.h"
  28. class Camera;
  29. class DebugRenderer;
  30. class Light;
  31. class Drawable;
  32. class OcclusionBuffer;
  33. class RenderSurface;
  34. class Zone;
  35. struct Viewport;
  36. static const int MAX_LIGHT_SPLITS = 6;
  37. /// Geometry view space depth minimum and maximum values
  38. struct GeometryDepthBounds
  39. {
  40. float min_;
  41. float max_;
  42. };
  43. /// Helper structure for checking whether a transparent object is already lit by a certain light
  44. struct LitTransparencyCheck
  45. {
  46. /// Construct undefined
  47. LitTransparencyCheck()
  48. {
  49. }
  50. /// Construct
  51. LitTransparencyCheck(Light* light, Drawable* drawable, unsigned batchIndex) :
  52. light_(light),
  53. drawable_(drawable),
  54. batchIndex_(batchIndex)
  55. {
  56. }
  57. /// Test for equality with another lit transparency check
  58. bool operator == (const LitTransparencyCheck& rhs) const { return (light_ == rhs.light_) && (drawable_ == rhs.drawable_) && (batchIndex_ == rhs.batchIndex_); }
  59. /// Test for inequality with another lit transparency check
  60. bool operator != (const LitTransparencyCheck& rhs) const { return (light_ != rhs.light_) || (drawable_ != rhs.drawable_) || (batchIndex_ != rhs.batchIndex_); }
  61. /// Return hash value for HashSet & HashMap
  62. unsigned ToHash() const { return ((unsigned)light_) / sizeof(Light) + ((unsigned)drawable_) / sizeof(Drawable) + batchIndex_; }
  63. Light* light_;
  64. Drawable* drawable_;
  65. unsigned batchIndex_;
  66. };
  67. /// A rendering view. Includes the main view(s) and any auxiliary views, but not shadow cameras
  68. class View : public Object
  69. {
  70. OBJECT(View);
  71. public:
  72. /// Construct
  73. View(Context* context);
  74. /// Destruct
  75. virtual ~View();
  76. /// Define with rendertarget and viewport. Return true if successful
  77. bool Define(RenderSurface* renderTarget, const Viewport& viewport);
  78. /// Update culling and construct rendering batches
  79. void Update(const FrameInfo& frame);
  80. /// Render batches
  81. void Render();
  82. /// Return octree
  83. Octree* GetOctree() const { return octree_; }
  84. /// Return camera
  85. Camera* GetCamera() const { return camera_; }
  86. /// Return zone
  87. Zone* GetZone() const { return zone_; }
  88. /// Return the render target. 0 if using the backbuffer
  89. RenderSurface* GetRenderTarget() const { return renderTarget_; }
  90. /// Return the depth stencil. 0 if using the backbuffer's depth stencil
  91. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  92. /// Return geometry objects
  93. const PODVector<Drawable*>& GetGeometries() const { return geometries_; }
  94. /// Return occluder objects
  95. const PODVector<Drawable*>& GetOccluders() const { return occluders_; }
  96. /// Return directional light shadow rendering occluders
  97. const PODVector<Drawable*>& GetShadowOccluders() const { return shadowOccluders_; }
  98. /// Return lights
  99. const PODVector<Light*>& GetLights() const { return lights_; }
  100. /// Return light batch queues
  101. const Vector<LightBatchQueue>& GetLightQueues() const { return lightQueues_; }
  102. private:
  103. /// Query the octree for scene nodes
  104. void GetDrawables();
  105. /// Construct batches from the scene nodes
  106. void GetBatches();
  107. /// Get lit batches for a certain light and drawable
  108. void GetLitBatches(Drawable* drawable, Light* light, Light* SplitLight, LightBatchQueue* lightQueue, HashSet<LitTransparencyCheck>& litTransparencies, PassType gBufferPass);
  109. /// Render batches, forward mode
  110. void RenderBatchesForward();
  111. /// Render batches, deferred mode
  112. void RenderBatchesDeferred();
  113. /// Query for occluders as seen from a camera
  114. void UpdateOccluders(PODVector<Drawable*>& occluders, Camera* camera);
  115. /// Draw occluders to occlusion buffer
  116. void DrawOccluders(OcclusionBuffer* buffer, const PODVector<Drawable*>& occluders);
  117. /// Query for lit geometries and shadow casters for a light
  118. unsigned ProcessLight(Light* light);
  119. /// Generate combined bounding boxes for lit geometries and shadow casters and check shadow caster visibility
  120. void ProcessLightQuery(unsigned splitIndex, const PODVector<Drawable*>& result, BoundingBox& geometryBox, BoundingBox& shadowSpaceBox, bool getLitGeometries, bool GetShadowCasters);
  121. /// Check visibility of one shadow caster
  122. bool IsShadowCasterVisible(Drawable* drawable, BoundingBox lightViewBox, Camera* shadowCamera, const Matrix3x4& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  123. /// Set up initial shadow camera view
  124. void SetupShadowCamera(Light* light, bool shadowOcclusion = false);
  125. /// Focus shadow camera to use shadow map texture space more optimally
  126. void FocusShadowCamera(Light* light, const BoundingBox& geometryBox, const BoundingBox& shadowCasterBox);
  127. /// Quantize the directional light shadow camera view to eliminate artefacts
  128. void QuantizeDirShadowCamera(Light* light, const BoundingBox& viewBox);
  129. /// Optimize light rendering by setting up a scissor rectangle
  130. void OptimizeLightByScissor(Light* light);
  131. /// Return scissor rectangle for a light
  132. const Rect& GetLightScissor(Light* light);
  133. /// Split directional or point light for shadow rendering
  134. unsigned SplitLight(Light* light);
  135. /// Return material technique, considering the drawable's LOD distance
  136. Technique* GetTechnique(Drawable* drawable, Material*& material);
  137. /// Check if material should render an auxiliary view (if it has a camera attached)
  138. void CheckMaterialForAuxView(Material* material);
  139. /// Sort all batches
  140. void SortBatches();
  141. /// Prepare instancing buffer by filling it with all instance transforms
  142. void PrepareInstancingBuffer();
  143. /// Calculate view-global shader parameters
  144. void CalculateShaderParameters();
  145. /// Draw a split light to stencil buffer
  146. void DrawSplitLightToStencil(Camera& camera, Light* light, bool clear = false);
  147. /// Draw everything in a batch queue, priority batches first
  148. void RenderBatchQueue(const BatchQueue& queue, bool useScissor = false, bool useLightBuffer = false, bool disableScissor = true);
  149. /// Draw a forward (shadowed) light batch queue
  150. void RenderForwardLightBatchQueue(const BatchQueue& queue, Light* forwardQueueLight);
  151. /// Render a shadow map
  152. void RenderShadowMap(const LightBatchQueue& queue);
  153. /// Graphics
  154. WeakPtr<Graphics> graphics_;
  155. /// Renderer
  156. WeakPtr<Renderer> renderer_;
  157. /// Octree to use
  158. Octree* octree_;
  159. /// Camera to use
  160. Camera* camera_;
  161. /// Zone to get global rendering settings from
  162. Zone* zone_;
  163. /// Color buffer to use
  164. RenderSurface* renderTarget_;
  165. /// Depth buffer to use
  166. RenderSurface* depthStencil_;
  167. /// Screen rectangle
  168. IntRect screenRect_;
  169. /// Render target width
  170. int width_;
  171. /// Render target height
  172. int height_;
  173. /// Rendering mode
  174. RenderMode mode_;
  175. /// Draw shadows flag
  176. bool drawShadows_;
  177. /// Material quality level
  178. int materialQuality_;
  179. /// Maximum number of occluder triangles
  180. int maxOccluderTriangles_;
  181. /// Jitter counter for temporal antialiasing
  182. int jitterCounter_;
  183. /// Previous view matrix for temporal antialiasing
  184. Matrix3x4 lastCameraView_;
  185. /// Information of the frame being rendered
  186. FrameInfo frame_;
  187. /// Combined bounding box of visible geometries
  188. BoundingBox sceneBox_;
  189. /// Combined bounding box of visible geometries in view space
  190. BoundingBox sceneViewBox_;
  191. /// Cache for light scissor queries
  192. Map<Light*, Rect> lightScissorCache_;
  193. /// Current split lights being processed
  194. Light* splitLights_[MAX_LIGHT_SPLITS];
  195. /// Current lit geometries being processed
  196. PODVector<Drawable*> litGeometries_[MAX_LIGHT_SPLITS];
  197. /// Current shadow casters being processed
  198. PODVector<Drawable*> shadowCasters_[MAX_LIGHT_SPLITS];
  199. /// Temporary drawable query result
  200. PODVector<Drawable*> tempDrawables_;
  201. /// Geometry objects
  202. PODVector<Drawable*> geometries_;
  203. /// Occluder objects
  204. PODVector<Drawable*> occluders_;
  205. /// Directional light shadow rendering occluders
  206. PODVector<Drawable*> shadowOccluders_;
  207. /// Depth minimum and maximum values for visible geometries
  208. PODVector<GeometryDepthBounds> geometryDepthBounds_;
  209. /// Lights
  210. PODVector<Light*> lights_;
  211. /// G-buffer size error displayed
  212. Set<RenderSurface*> gBufferErrorDisplayed_;
  213. /// View-global shader parameters
  214. HashMap<ShaderParameter, Vector4> shaderParameters_;
  215. /// G-buffer batches
  216. BatchQueue gBufferQueue_;
  217. /// Base pass batches
  218. BatchQueue baseQueue_;
  219. /// Extra pass batches
  220. BatchQueue extraQueue_;
  221. /// Transparent geometry batches
  222. BatchQueue transparentQueue_;
  223. /// Unshadowed light volume batches
  224. BatchQueue noShadowLightQueue_;
  225. /// Shadowed light queues
  226. Vector<LightBatchQueue> lightQueues_;
  227. };