View.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "List.h"
  27. #include "Object.h"
  28. #include "Polyhedron.h"
  29. class Camera;
  30. class DebugRenderer;
  31. class Light;
  32. class Drawable;
  33. class OcclusionBuffer;
  34. class Octree;
  35. class RenderSurface;
  36. class Technique;
  37. class Texture2D;
  38. class Viewport;
  39. class Zone;
  40. struct WorkItem;
  41. /// Intermediate light processing result.
  42. struct LightQueryResult
  43. {
  44. /// Light.
  45. Light* light_;
  46. /// Lit geometries.
  47. PODVector<Drawable*> litGeometries_;
  48. /// Shadow casters.
  49. PODVector<Drawable*> shadowCasters_;
  50. /// Shadow cameras.
  51. Camera* shadowCameras_[MAX_LIGHT_SPLITS];
  52. /// Shadow caster start indices.
  53. unsigned shadowCasterBegin_[MAX_LIGHT_SPLITS];
  54. /// Shadow caster end indices.
  55. unsigned shadowCasterEnd_[MAX_LIGHT_SPLITS];
  56. /// Combined bounding box of shadow casters in light view or projection space.
  57. BoundingBox shadowCasterBox_[MAX_LIGHT_SPLITS];
  58. /// Shadow camera near splits (directional lights only.)
  59. float shadowNearSplits_[MAX_LIGHT_SPLITS];
  60. /// Shadow camera far splits (directional lights only.)
  61. float shadowFarSplits_[MAX_LIGHT_SPLITS];
  62. /// Shadow map split count.
  63. unsigned numSplits_;
  64. };
  65. /// 3D rendering view. Includes the main view(s) and any auxiliary views, but not shadow cameras.
  66. class View : public Object
  67. {
  68. friend void CheckVisibilityWork(const WorkItem* item, unsigned threadIndex);
  69. friend void ProcessLightWork(const WorkItem* item, unsigned threadIndex);
  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, Viewport* viewport);
  78. /// Update and cull objects 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 the rendertarget. 0 if using the backbuffer.
  87. RenderSurface* GetRenderTarget() const { return renderTarget_; }
  88. /// Return geometry objects.
  89. const PODVector<Drawable*>& GetGeometries() const { return geometries_; }
  90. /// Return occluder objects.
  91. const PODVector<Drawable*>& GetOccluders() const { return occluders_; }
  92. /// Return lights.
  93. const PODVector<Light*>& GetLights() const { return lights_; }
  94. /// Return light batch queues.
  95. const Vector<LightBatchQueue>& GetLightQueues() const { return lightQueues_; }
  96. private:
  97. /// Query the octree for drawable objects.
  98. void GetDrawables();
  99. /// Construct batches from the drawable objects.
  100. void GetBatches();
  101. /// Update geometries and sort batches.
  102. void UpdateGeometries();
  103. /// Get pixel lit batches for a certain light and drawable.
  104. void GetLitBatches(Drawable* drawable, LightBatchQueue& lightQueue);
  105. /// Render batches using forward rendering.
  106. void RenderBatchesForward();
  107. /// Render batches using light pre-pass or deferred rendering.
  108. void RenderBatchesDeferred();
  109. /// Allocate needed screen buffers for post-processing and/or framebuffer blitting.
  110. void AllocateScreenBuffers();
  111. /// Blit the framebuffer to destination. Used in OpenGL deferred rendering modes.
  112. void BlitFramebuffer();
  113. /// Run post-processing effects.
  114. void RunPostProcesses();
  115. /// Query for occluders as seen from a camera.
  116. void UpdateOccluders(PODVector<Drawable*>& occluders, Camera* camera);
  117. /// Draw occluders to occlusion buffer.
  118. void DrawOccluders(OcclusionBuffer* buffer, const PODVector<Drawable*>& occluders);
  119. /// Query for lit geometries and shadow casters for a light.
  120. void ProcessLight(LightQueryResult& query, unsigned threadIndex);
  121. /// Process shadow casters' visibilities and build their combined view- or projection-space bounding box.
  122. void ProcessShadowCasters(LightQueryResult& query, const PODVector<Drawable*>& drawables, unsigned splitIndex);
  123. /// %Set up initial shadow camera view(s).
  124. void SetupShadowCameras(LightQueryResult& query);
  125. /// %Set up a directional light shadow camera
  126. void SetupDirLightShadowCamera(Camera* shadowCamera, Light* light, float nearSplit, float farSplit);
  127. /// Finalize shadow camera view after shadow casters and the shadow map are known.
  128. void FinalizeShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& shadowCasterBox);
  129. /// Quantize a directional light shadow camera view to eliminate swimming.
  130. void QuantizeDirLightShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& viewBox);
  131. /// Check visibility of one shadow caster.
  132. bool IsShadowCasterVisible(Drawable* drawable, BoundingBox lightViewBox, Camera* shadowCamera, const Matrix3x4& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  133. /// Return the viewport for a shadow map split.
  134. IntRect GetShadowMapViewport(Light* light, unsigned splitIndex, Texture2D* shadowMap);
  135. /// Optimize light rendering by setting up a scissor rectangle.
  136. void OptimizeLightByScissor(Light* light);
  137. /// Optimize spot or point light rendering by drawing its volume to the stencil buffer.
  138. void OptimizeLightByStencil(Light* light);
  139. /// Return scissor rectangle for a light.
  140. const Rect& GetLightScissor(Light* light);
  141. /// Split directional or point light for shadow rendering.
  142. unsigned SplitLight(Light* light);
  143. /// Find and set a new zone for a drawable when it has moved.
  144. void FindZone(Drawable* drawable, unsigned threadIndex);
  145. /// Return the drawable's zone, or camera zone if it has override mode enabled.
  146. Zone* GetZone(Drawable* drawable);
  147. /// Return the drawable's light mask, considering also its zone.
  148. unsigned GetLightMask(Drawable* drawable);
  149. /// Return the drawable's shadow mask, considering also its zone.
  150. unsigned GetShadowMask(Drawable* drawable);
  151. /// Return hash code for a vertex light queue.
  152. unsigned long long GetVertexLightQueueHash(const PODVector<Light*>& vertexLights);
  153. /// Return material technique, considering the drawable's LOD distance.
  154. Technique* GetTechnique(Drawable* drawable, Material*& material);
  155. /// Check if material should render an auxiliary view (if it has a camera attached.)
  156. void CheckMaterialForAuxView(Material* material);
  157. /// Choose shaders for a batch and add it to queue.
  158. void AddBatchToQueue(BatchQueue& queue, Batch& batch, Technique* tech, bool allowInstancing = true, bool allowShadows = true);
  159. /// Prepare instancing buffer by filling it with all instance transforms.
  160. void PrepareInstancingBuffer();
  161. /// %Set up a light volume rendering batch.
  162. void SetupLightVolumeBatch(Batch& batch);
  163. /// Draw a full screen quad (either near or far.) Shaders must have been set beforehand.
  164. void DrawFullscreenQuad(Camera* camera, bool nearQuad);
  165. /// Render a shadow map.
  166. void RenderShadowMap(const LightBatchQueue& queue);
  167. /// Return the proper depth-stencil surface to use for a rendertarget.
  168. RenderSurface* GetDepthStencil(RenderSurface* renderTarget);
  169. /// Graphics subsystem.
  170. WeakPtr<Graphics> graphics_;
  171. /// Renderer subsystem.
  172. WeakPtr<Renderer> renderer_;
  173. /// Octree to use.
  174. Octree* octree_;
  175. /// Camera to use.
  176. Camera* camera_;
  177. /// Camera's scene node.
  178. Node* cameraNode_;
  179. /// Zone the camera is inside, or default zone if not assigned.
  180. Zone* cameraZone_;
  181. /// Zone at far clip plane.
  182. Zone* farClipZone_;
  183. /// Occlusion buffer for the main camera.
  184. OcclusionBuffer* occlusionBuffer_;
  185. /// Color rendertarget to use.
  186. RenderSurface* renderTarget_;
  187. /// Viewport rectangle.
  188. IntRect viewRect_;
  189. /// Viewport size.
  190. IntVector2 viewSize_;
  191. /// Rendertarget size.
  192. IntVector2 rtSize_;
  193. /// Information of the frame being rendered.
  194. FrameInfo frame_;
  195. /// Combined bounding box of visible geometries.
  196. BoundingBox sceneBox_;
  197. /// Minimum Z value of the visible scene.
  198. float minZ_;
  199. /// Maximum Z value of the visible scene.
  200. float maxZ_;
  201. /// Rendering mode.
  202. RenderMode renderMode_;
  203. /// Material quality level.
  204. int materialQuality_;
  205. /// Maximum number of occluder triangles.
  206. int maxOccluderTriangles_;
  207. /// Highest zone priority currently visible.
  208. int highestZonePriority_;
  209. /// Camera zone's override flag.
  210. bool cameraZoneOverride_;
  211. /// Draw shadows flag.
  212. bool drawShadows_;
  213. /// Whether objects with zero lightmask exist. In light pre-pass mode this means skipping some optimizations.
  214. bool hasZeroLightMask_;
  215. /// Post-processing effects.
  216. Vector<SharedPtr<PostProcess> > postProcesses_;
  217. /// Intermediate screen buffers used in postprocessing and OpenGL deferred framebuffer blit.
  218. PODVector<Texture2D*> screenBuffers_;
  219. /// Per-thread octree query results.
  220. Vector<PODVector<Drawable*> > tempDrawables_;
  221. /// Visible zones.
  222. PODVector<Zone*> zones_;
  223. /// Visible geometry objects.
  224. PODVector<Drawable*> geometries_;
  225. /// Geometry objects visible in shadow maps.
  226. PODVector<Drawable*> shadowGeometries_;
  227. /// Geometry objects that will be updated in the main thread.
  228. PODVector<Drawable*> nonThreadedGeometries_;
  229. /// Geometry objects that will be updated in worker threads.
  230. PODVector<Drawable*> threadedGeometries_;
  231. /// Occluder objects.
  232. PODVector<Drawable*> occluders_;
  233. /// Lights.
  234. PODVector<Light*> lights_;
  235. /// Drawables that limit their maximum light count.
  236. HashSet<Drawable*> maxLightsDrawables_;
  237. /// Intermediate light processing results.
  238. Vector<LightQueryResult> lightQueryResults_;
  239. /// Per-pixel light queues.
  240. Vector<LightBatchQueue> lightQueues_;
  241. /// Per-vertex light queues.
  242. HashMap<unsigned long long, LightBatchQueue> vertexLightQueues_;
  243. /// Base pass batches.
  244. BatchQueue baseQueue_;
  245. /// Deferred rendering G-buffer batches.
  246. BatchQueue gbufferQueue_;
  247. /// Pre-transparent pass batches.
  248. BatchQueue preAlphaQueue_;
  249. /// Transparent geometry batches.
  250. BatchQueue alphaQueue_;
  251. /// Post-transparent pass batches.
  252. BatchQueue postAlphaQueue_;
  253. };