View.h 12 KB

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