View.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "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 Zone;
  39. struct Viewport;
  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 render target and viewport. Return true if successful.
  85. bool Define(RenderSurface* renderTarget, const 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 render target. 0 if using the backbuffer.
  95. RenderSurface* GetRenderTarget() const { return renderTarget_; }
  96. /// Return the depth stencil. 0 if using the backbuffer's depth stencil.
  97. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  98. /// Return geometry objects.
  99. const PODVector<Drawable*>& GetGeometries() const { return geometries_; }
  100. /// Return occluder objects.
  101. const PODVector<Drawable*>& GetOccluders() const { return occluders_; }
  102. /// Return lights.
  103. const PODVector<Light*>& GetLights() const { return lights_; }
  104. /// Return light batch queues.
  105. const List<LightBatchQueue>& GetLightQueues() const { return lightQueues_; }
  106. private:
  107. /// Query the octree for drawable objects.
  108. void GetDrawables();
  109. /// Construct batches from the drawable objects.
  110. void GetBatches();
  111. /// Update geometries and sort batches.
  112. void UpdateGeometries();
  113. /// Get pixel lit batches for a certain light and drawable.
  114. void GetLitBatches(Drawable* drawable, LightBatchQueue& lightQueue);
  115. /// Render batches using forward rendering.
  116. void RenderBatchesForward();
  117. /// Render batches using light pre-pass rendering.
  118. void RenderBatchesLightPrepass();
  119. /// Query for occluders as seen from a camera.
  120. void UpdateOccluders(PODVector<Drawable*>& occluders, Camera* camera);
  121. /// Draw occluders to occlusion buffer.
  122. void DrawOccluders(OcclusionBuffer* buffer, const PODVector<Drawable*>& occluders);
  123. /// Query for lit geometries and shadow casters for a light.
  124. void ProcessLight(LightQueryResult& query, unsigned threadIndex);
  125. /// Process shadow casters' visibilities and build their combined view- or projection-space bounding box.
  126. void ProcessShadowCasters(LightQueryResult& query, const PODVector<Drawable*>& drawables, unsigned splitIndex);
  127. /// %Set up initial shadow camera view(s).
  128. void SetupShadowCameras(LightQueryResult& query);
  129. /// %Set up a directional light shadow camera
  130. void SetupDirLightShadowCamera(Camera* shadowCamera, Light* light, float nearSplit, float farSplit);
  131. /// Finalize shadow camera view after shadow casters and the shadow map are known.
  132. void FinalizeShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& shadowCasterBox);
  133. /// Quantize a directional light shadow camera view to eliminate swimming.
  134. void QuantizeDirLightShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& viewBox);
  135. /// Check visibility of one shadow caster.
  136. bool IsShadowCasterVisible(Drawable* drawable, BoundingBox lightViewBox, Camera* shadowCamera, const Matrix3x4& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  137. /// Return the viewport for a shadow map split.
  138. IntRect GetShadowMapViewport(Light* light, unsigned splitIndex, Texture2D* shadowMap);
  139. /// Optimize light rendering by setting up a scissor rectangle.
  140. void OptimizeLightByScissor(Light* light);
  141. /// Optimize spot or point light rendering by drawing its volume to the stencil buffer.
  142. void OptimizeLightByStencil(Light* light);
  143. /// Return scissor rectangle for a light.
  144. const Rect& GetLightScissor(Light* light);
  145. /// Split directional or point light for shadow rendering.
  146. unsigned SplitLight(Light* light);
  147. /// Find and set a new zone for a drawable when it has moved.
  148. void FindZone(Drawable* drawable, unsigned threadIndex);
  149. /// Return the drawable's zone, or camera zone if it has override mode enabled.
  150. Zone* GetZone(Drawable* drawable);
  151. /// Return the drawable's light mask, considering also its zone.
  152. unsigned GetLightMask(Drawable* drawable);
  153. /// Return the drawable's shadow mask, considering also its zone.
  154. unsigned GetShadowMask(Drawable* drawable);
  155. /// Return hash code for a vertex light queue.
  156. unsigned long long GetVertexLightQueueHash(const PODVector<Light*>& vertexLights);
  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. /// Finalize a batch. Convert it to instanced if possible, choose shaders for it, and calculate the sort key.
  162. void FinalizeBatch(Batch& batch, Technique* tech, Pass* pass, bool allowInstancing = true, bool allowShadows = true);
  163. /// Prepare instancing buffer by filling it with all instance transforms.
  164. void PrepareInstancingBuffer();
  165. /// %Set up a light volume rendering batch.
  166. void SetupLightBatch(Batch& batch);
  167. /// Draw a full screen quad (either near or far.) Shaders must have been set beforehand.
  168. void DrawFullscreenQuad(Camera& camera, bool nearQuad);
  169. /// Render everything in a batch queue, priority batches first.
  170. void RenderBatchQueue(const BatchQueue& queue, bool useScissor = false);
  171. /// Render batches lit by a specific light.
  172. void RenderLightBatchQueue(const BatchQueue& queue, Light* forwardQueueLight);
  173. /// Render a shadow map.
  174. void RenderShadowMap(const LightBatchQueue& queue);
  175. /// Graphics subsystem.
  176. WeakPtr<Graphics> graphics_;
  177. /// Renderer subsystem.
  178. WeakPtr<Renderer> renderer_;
  179. /// Octree to use.
  180. Octree* octree_;
  181. /// Camera to use.
  182. Camera* camera_;
  183. /// Zone the camera is inside, or default zone if not assigned.
  184. Zone* cameraZone_;
  185. /// Zone at far clip plane.
  186. Zone* farClipZone_;
  187. /// Occlusion buffer for the main camera.
  188. OcclusionBuffer* occlusionBuffer_;
  189. /// Color buffer to use.
  190. RenderSurface* renderTarget_;
  191. /// Depth buffer to use.
  192. RenderSurface* depthStencil_;
  193. /// Screen rectangle.
  194. IntRect screenRect_;
  195. /// Render target width.
  196. int width_;
  197. /// Render target height.
  198. int height_;
  199. /// Draw shadows flag.
  200. bool drawShadows_;
  201. /// Material quality level.
  202. int materialQuality_;
  203. /// Maximum number of occluder triangles.
  204. int maxOccluderTriangles_;
  205. /// Highest zone priority currently visible.
  206. int highestZonePriority_;
  207. /// Start index of unculled drawables. These will not be tested for occlusion.
  208. unsigned unculledDrawableStart_;
  209. /// Information of the frame being rendered.
  210. FrameInfo frame_;
  211. /// Camera frustum.
  212. Frustum frustum_;
  213. /// Combined bounding box of visible geometries.
  214. BoundingBox sceneBox_;
  215. /// Combined bounding box of visible geometries in view space.
  216. BoundingBox sceneViewBox_;
  217. /// Volume for frustum clipping.
  218. Polyhedron frustumVolume_;
  219. /// Per-thread octree query results.
  220. Vector<PODVector<Drawable*> > tempDrawables_;
  221. /// Per-thread octree zone query results.
  222. Vector<PODVector<Zone*> > tempZones_;
  223. /// Visible zones.
  224. PODVector<Zone*> zones_;
  225. /// Visible geometry objects.
  226. PODVector<Drawable*> geometries_;
  227. /// All geometry objects, including shadow casters not visible in the main view.
  228. PODVector<Drawable*> allGeometries_;
  229. /// Geometry objects that will be updated in the main thread.
  230. PODVector<Drawable*> nonThreadedGeometries_;
  231. /// Geometry objects that will be updated in worker threads.
  232. PODVector<Drawable*> threadedGeometries_;
  233. /// Occluder objects.
  234. PODVector<Drawable*> occluders_;
  235. /// Depth minimum and maximum values for visible geometries.
  236. PODVector<GeometryDepthBounds> geometryDepthBounds_;
  237. /// Lights.
  238. PODVector<Light*> lights_;
  239. /// Render surfaces for which a G-buffer size error has already been logged, to prevent log spam.
  240. HashSet<RenderSurface*> gBufferErrorDisplayed_;
  241. /// Drawables that limit their maximum light count.
  242. HashSet<Drawable*> maxLightsDrawables_;
  243. /// Lookup map for the processed lights' light queues.
  244. Map<Light*, LightBatchQueue*> lightQueueMapping_;
  245. /// Cache for light scissor queries.
  246. HashMap<Light*, Rect> lightScissorCache_;
  247. /// Base pass batches.
  248. BatchQueue baseQueue_;
  249. /// Pre-transparent pass batches.
  250. BatchQueue preAlphaQueue_;
  251. /// Light pre-pass G-buffer batches.
  252. BatchQueue gbufferQueue_;
  253. /// Transparent geometry batches.
  254. BatchQueue alphaQueue_;
  255. /// Post-transparent pass batches.
  256. BatchQueue postAlphaQueue_;
  257. /// Intermediate light processing results.
  258. Vector<LightQueryResult> lightQueryResults_;
  259. /// Per-pixel light queues.
  260. List<LightBatchQueue> lightQueues_;
  261. /// Per-vertex light queues.
  262. HashMap<unsigned long long, LightBatchQueue> vertexLightQueues_;
  263. /// Current stencil value for light optimization.
  264. unsigned char lightStencilValue_;
  265. /// Camera zone's override flag.
  266. bool cameraZoneOverride_;
  267. };