View.h 12 KB

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