View.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Batch.h"
  24. #include "HashSet.h"
  25. #include "List.h"
  26. #include "Object.h"
  27. #include "Polyhedron.h"
  28. namespace Urho3D
  29. {
  30. class Camera;
  31. class DebugRenderer;
  32. class Light;
  33. class Drawable;
  34. class OcclusionBuffer;
  35. class Octree;
  36. class RenderPath;
  37. class RenderSurface;
  38. class Technique;
  39. class Texture2D;
  40. class Viewport;
  41. class Zone;
  42. struct RenderPathCommand;
  43. struct WorkItem;
  44. /// Intermediate light processing result.
  45. struct LightQueryResult
  46. {
  47. /// Light.
  48. Light* light_;
  49. /// Lit geometries.
  50. PODVector<Drawable*> litGeometries_;
  51. /// Shadow casters.
  52. PODVector<Drawable*> shadowCasters_;
  53. /// Shadow cameras.
  54. Camera* shadowCameras_[MAX_LIGHT_SPLITS];
  55. /// Shadow caster start indices.
  56. unsigned shadowCasterBegin_[MAX_LIGHT_SPLITS];
  57. /// Shadow caster end indices.
  58. unsigned shadowCasterEnd_[MAX_LIGHT_SPLITS];
  59. /// Combined bounding box of shadow casters in light view or projection space.
  60. BoundingBox shadowCasterBox_[MAX_LIGHT_SPLITS];
  61. /// Shadow camera near splits (directional lights only.)
  62. float shadowNearSplits_[MAX_LIGHT_SPLITS];
  63. /// Shadow camera far splits (directional lights only.)
  64. float shadowFarSplits_[MAX_LIGHT_SPLITS];
  65. /// Shadow map split count.
  66. unsigned numSplits_;
  67. };
  68. /// Scene render pass info.
  69. struct ScenePassInfo
  70. {
  71. /// Pass name hash.
  72. StringHash pass_;
  73. /// Allow instancing flag.
  74. bool allowInstancing_;
  75. /// Mark to stencil flag.
  76. bool markToStencil_;
  77. /// Light scissor optimization flag.
  78. bool useScissor_;
  79. /// Vertex light flag.
  80. bool vertexLights_;
  81. /// Batch queue.
  82. BatchQueue* batchQueue_;
  83. };
  84. /// 3D rendering view. Includes the main view(s) and any auxiliary views, but not shadow cameras.
  85. class View : public Object
  86. {
  87. friend void CheckVisibilityWork(const WorkItem* item, unsigned threadIndex);
  88. friend void ProcessLightWork(const WorkItem* item, unsigned threadIndex);
  89. OBJECT(View);
  90. public:
  91. /// Construct.
  92. View(Context* context);
  93. /// Destruct.
  94. virtual ~View();
  95. /// Define with rendertarget and viewport. Return true if successful.
  96. bool Define(RenderSurface* renderTarget, Viewport* viewport);
  97. /// Update and cull objects and construct rendering batches.
  98. void Update(const FrameInfo& frame);
  99. /// Render batches.
  100. void Render();
  101. /// Return graphics subsystem.
  102. Graphics* GetGraphics() const;
  103. /// Return renderer subsystem.
  104. Renderer* GetRenderer() const;
  105. /// Return scene.
  106. Scene* GetScene() const { return scene_; }
  107. /// Return octree.
  108. Octree* GetOctree() const { return octree_; }
  109. /// Return camera.
  110. Camera* GetCamera() const { return camera_; }
  111. /// Return the rendertarget. 0 if using the backbuffer.
  112. RenderSurface* GetRenderTarget() const { return renderTarget_; }
  113. /// Return geometry objects.
  114. const PODVector<Drawable*>& GetGeometries() const { return geometries_; }
  115. /// Return occluder objects.
  116. const PODVector<Drawable*>& GetOccluders() const { return occluders_; }
  117. /// Return lights.
  118. const PODVector<Light*>& GetLights() const { return lights_; }
  119. /// Return light batch queues.
  120. const Vector<LightBatchQueue>& GetLightQueues() const { return lightQueues_; }
  121. private:
  122. /// Query the octree for drawable objects.
  123. void GetDrawables();
  124. /// Construct batches from the drawable objects.
  125. void GetBatches();
  126. /// Update geometries and sort batches.
  127. void UpdateGeometries();
  128. /// Get pixel lit batches for a certain light and drawable.
  129. void GetLitBatches(Drawable* drawable, LightBatchQueue& lightQueue, BatchQueue* alphaQueue, bool useLitBase);
  130. /// Execute render commands.
  131. void ExecuteRenderPathCommands();
  132. /// Set rendertargets for current render command.
  133. void SetRenderTargets(const RenderPathCommand& command);
  134. /// Set textures for current render command.
  135. void SetTextures(const RenderPathCommand& command);
  136. /// Perform a quad rendering command.
  137. void RenderQuad(const RenderPathCommand& command);
  138. /// Check if a command reads the rendered scene.
  139. bool CheckViewportRead(const RenderPathCommand& command);
  140. /// Allocate needed screen buffers.
  141. void AllocateScreenBuffers();
  142. /// Blit the viewport from one surface to another.
  143. void BlitFramebuffer(Texture2D* source, RenderSurface* destination, bool depthWrite);
  144. /// Draw a fullscreen quad. Shaders and renderstates must have been set beforehand.
  145. void DrawFullscreenQuad(bool nearQuad);
  146. /// Query for occluders as seen from a camera.
  147. void UpdateOccluders(PODVector<Drawable*>& occluders, Camera* camera);
  148. /// Draw occluders to occlusion buffer.
  149. void DrawOccluders(OcclusionBuffer* buffer, const PODVector<Drawable*>& occluders);
  150. /// Query for lit geometries and shadow casters for a light.
  151. void ProcessLight(LightQueryResult& query, unsigned threadIndex);
  152. /// Process shadow casters' visibilities and build their combined view- or projection-space bounding box.
  153. void ProcessShadowCasters(LightQueryResult& query, const PODVector<Drawable*>& drawables, unsigned splitIndex);
  154. /// Set up initial shadow camera view(s).
  155. void SetupShadowCameras(LightQueryResult& query);
  156. /// Set up a directional light shadow camera
  157. void SetupDirLightShadowCamera(Camera* shadowCamera, Light* light, float nearSplit, float farSplit);
  158. /// Finalize shadow camera view after shadow casters and the shadow map are known.
  159. void FinalizeShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& shadowCasterBox);
  160. /// Quantize a directional light shadow camera view to eliminate swimming.
  161. void QuantizeDirLightShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& viewBox);
  162. /// Check visibility of one shadow caster.
  163. bool IsShadowCasterVisible(Drawable* drawable, BoundingBox lightViewBox, Camera* shadowCamera, const Matrix3x4& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  164. /// Return the viewport for a shadow map split.
  165. IntRect GetShadowMapViewport(Light* light, unsigned splitIndex, Texture2D* shadowMap);
  166. /// Find and set a new zone for a drawable when it has moved.
  167. void FindZone(Drawable* drawable);
  168. /// Return the drawable's zone, or camera zone if it has override mode enabled.
  169. Zone* GetZone(Drawable* drawable);
  170. /// Return the drawable's light mask, considering also its zone.
  171. unsigned GetLightMask(Drawable* drawable);
  172. /// Return the drawable's shadow mask, considering also its zone.
  173. unsigned GetShadowMask(Drawable* drawable);
  174. /// Return hash code for a vertex light queue.
  175. unsigned long long GetVertexLightQueueHash(const PODVector<Light*>& vertexLights);
  176. /// Return material technique, considering the drawable's LOD distance.
  177. Technique* GetTechnique(Drawable* drawable, Material* material);
  178. /// Check if material should render an auxiliary view (if it has a camera attached.)
  179. void CheckMaterialForAuxView(Material* material);
  180. /// Choose shaders for a batch and add it to queue.
  181. void AddBatchToQueue(BatchQueue& queue, Batch& batch, Technique* tech, bool allowInstancing = true, bool allowShadows = true);
  182. /// Prepare instancing buffer by filling it with all instance transforms.
  183. void PrepareInstancingBuffer();
  184. /// Set up a light volume rendering batch.
  185. void SetupLightVolumeBatch(Batch& batch);
  186. /// Render a shadow map.
  187. void RenderShadowMap(const LightBatchQueue& queue);
  188. /// Return the proper depth-stencil surface to use for a rendertarget.
  189. RenderSurface* GetDepthStencil(RenderSurface* renderTarget);
  190. /// Graphics subsystem.
  191. WeakPtr<Graphics> graphics_;
  192. /// Renderer subsystem.
  193. WeakPtr<Renderer> renderer_;
  194. /// Scene to use.
  195. Scene* scene_;
  196. /// Octree to use.
  197. Octree* octree_;
  198. /// Camera to use.
  199. Camera* camera_;
  200. /// Camera's scene node.
  201. Node* cameraNode_;
  202. /// Zone the camera is inside, or default zone if not assigned.
  203. Zone* cameraZone_;
  204. /// Zone at far clip plane.
  205. Zone* farClipZone_;
  206. /// Occlusion buffer for the main camera.
  207. OcclusionBuffer* occlusionBuffer_;
  208. /// Destination color rendertarget.
  209. RenderSurface* renderTarget_;
  210. /// Effective color rendertarget to use, may be different if screenbuffers are used.
  211. RenderSurface* currentRenderTarget_;
  212. /// Viewport rectangle.
  213. IntRect viewRect_;
  214. /// Viewport size.
  215. IntVector2 viewSize_;
  216. /// Rendertarget size.
  217. IntVector2 rtSize_;
  218. /// Information of the frame being rendered.
  219. FrameInfo frame_;
  220. /// Combined bounding box of visible geometries.
  221. BoundingBox sceneBox_;
  222. /// Write screenbuffer index.
  223. unsigned writeBuffer_;
  224. /// Read screenbuffer index.
  225. unsigned readBuffer_;
  226. /// Minimum Z value of the visible scene.
  227. float minZ_;
  228. /// Maximum Z value of the visible scene.
  229. float maxZ_;
  230. /// Material quality level.
  231. int materialQuality_;
  232. /// Maximum number of occluder triangles.
  233. int maxOccluderTriangles_;
  234. /// Highest zone priority currently visible.
  235. int highestZonePriority_;
  236. /// Camera zone's override flag.
  237. bool cameraZoneOverride_;
  238. /// Draw shadows flag.
  239. bool drawShadows_;
  240. /// Deferred flag. Inferred from the existence of a light volume command in the renderpath.
  241. bool deferred_;
  242. /// Renderpath.
  243. const RenderPath* renderPath_;
  244. /// Intermediate screen buffers used in pingpong copies and OpenGL deferred framebuffer blit.
  245. PODVector<Texture2D*> screenBuffers_;
  246. /// Per-thread octree query results.
  247. Vector<PODVector<Drawable*> > tempDrawables_;
  248. /// Visible zones.
  249. PODVector<Zone*> zones_;
  250. /// Visible geometry objects.
  251. PODVector<Drawable*> geometries_;
  252. /// Geometry objects visible in shadow maps.
  253. PODVector<Drawable*> shadowGeometries_;
  254. /// Geometry objects that will be updated in the main thread.
  255. PODVector<Drawable*> nonThreadedGeometries_;
  256. /// Geometry objects that will be updated in worker threads.
  257. PODVector<Drawable*> threadedGeometries_;
  258. /// Occluder objects.
  259. PODVector<Drawable*> occluders_;
  260. /// Lights.
  261. PODVector<Light*> lights_;
  262. /// Light volume vertex shaders.
  263. PODVector<ShaderVariation*> lightVS_;
  264. /// Light volume pixel shaders.
  265. PODVector<ShaderVariation*> lightPS_;
  266. /// Drawables that limit their maximum light count.
  267. HashSet<Drawable*> maxLightsDrawables_;
  268. /// Rendertargets defined by the renderpath.
  269. HashMap<StringHash, Texture2D*> renderTargets_;
  270. /// Intermediate light processing results.
  271. Vector<LightQueryResult> lightQueryResults_;
  272. /// Info for scene render passes defined by the renderpath.
  273. Vector<ScenePassInfo> scenePasses_;
  274. /// Per-pixel light queues.
  275. Vector<LightBatchQueue> lightQueues_;
  276. /// Per-vertex light queues.
  277. HashMap<unsigned long long, LightBatchQueue> vertexLightQueues_;
  278. /// Batch queues.
  279. HashMap<StringHash, BatchQueue> batchQueues_;
  280. };
  281. }