View.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /// Per-thread geometry, light and scene range collection structure.
  85. struct PerThreadSceneResult
  86. {
  87. /// Geometry objects.
  88. PODVector<Drawable*> geometries_;
  89. /// Lights.
  90. PODVector<Light*> lights_;
  91. /// Scene minimum Z value.
  92. float minZ_;
  93. /// Scene maximum Z value.
  94. float maxZ_;
  95. };
  96. /// 3D rendering view. Includes the main view(s) and any auxiliary views, but not shadow cameras.
  97. class URHO3D_API View : public Object
  98. {
  99. friend void CheckVisibilityWork(const WorkItem* item, unsigned threadIndex);
  100. friend void ProcessLightWork(const WorkItem* item, unsigned threadIndex);
  101. OBJECT(View);
  102. public:
  103. /// Construct.
  104. View(Context* context);
  105. /// Destruct.
  106. virtual ~View();
  107. /// Define with rendertarget and viewport. Return true if successful.
  108. bool Define(RenderSurface* renderTarget, Viewport* viewport);
  109. /// Update and cull objects and construct rendering batches.
  110. void Update(const FrameInfo& frame);
  111. /// Render batches.
  112. void Render();
  113. /// Return graphics subsystem.
  114. Graphics* GetGraphics() const;
  115. /// Return renderer subsystem.
  116. Renderer* GetRenderer() const;
  117. /// Return scene.
  118. Scene* GetScene() const { return scene_; }
  119. /// Return octree.
  120. Octree* GetOctree() const { return octree_; }
  121. /// Return camera.
  122. Camera* GetCamera() const { return camera_; }
  123. /// Return the rendertarget. 0 if using the backbuffer.
  124. RenderSurface* GetRenderTarget() const { return renderTarget_; }
  125. /// Return geometry objects.
  126. const PODVector<Drawable*>& GetGeometries() const { return geometries_; }
  127. /// Return occluder objects.
  128. const PODVector<Drawable*>& GetOccluders() const { return occluders_; }
  129. /// Return lights.
  130. const PODVector<Light*>& GetLights() const { return lights_; }
  131. /// Return light batch queues.
  132. const Vector<LightBatchQueue>& GetLightQueues() const { return lightQueues_; }
  133. private:
  134. /// Query the octree for drawable objects.
  135. void GetDrawables();
  136. /// Construct batches from the drawable objects.
  137. void GetBatches();
  138. /// Update geometries and sort batches.
  139. void UpdateGeometries();
  140. /// Get pixel lit batches for a certain light and drawable.
  141. void GetLitBatches(Drawable* drawable, LightBatchQueue& lightQueue, BatchQueue* alphaQueue, bool useLitBase);
  142. /// Execute render commands.
  143. void ExecuteRenderPathCommands();
  144. /// Set rendertargets for current render command.
  145. void SetRenderTargets(RenderPathCommand& command);
  146. /// Set textures for current render command.
  147. void SetTextures(RenderPathCommand& command);
  148. /// Perform a quad rendering command.
  149. void RenderQuad(RenderPathCommand& command);
  150. /// Check if a command reads the rendered scene.
  151. bool CheckViewportRead(const RenderPathCommand& command);
  152. /// Allocate needed screen buffers.
  153. void AllocateScreenBuffers();
  154. /// Blit the viewport from one surface to another.
  155. void BlitFramebuffer(Texture2D* source, RenderSurface* destination, bool depthWrite);
  156. /// Draw a fullscreen quad. Shaders and renderstates must have been set beforehand.
  157. void DrawFullscreenQuad(bool nearQuad);
  158. /// Query for occluders as seen from a camera.
  159. void UpdateOccluders(PODVector<Drawable*>& occluders, Camera* camera);
  160. /// Draw occluders to occlusion buffer.
  161. void DrawOccluders(OcclusionBuffer* buffer, const PODVector<Drawable*>& occluders);
  162. /// Query for lit geometries and shadow casters for a light.
  163. void ProcessLight(LightQueryResult& query, unsigned threadIndex);
  164. /// Process shadow casters' visibilities and build their combined view- or projection-space bounding box.
  165. void ProcessShadowCasters(LightQueryResult& query, const PODVector<Drawable*>& drawables, unsigned splitIndex);
  166. /// Set up initial shadow camera view(s).
  167. void SetupShadowCameras(LightQueryResult& query);
  168. /// Set up a directional light shadow camera
  169. void SetupDirLightShadowCamera(Camera* shadowCamera, Light* light, float nearSplit, float farSplit);
  170. /// Finalize shadow camera view after shadow casters and the shadow map are known.
  171. void FinalizeShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& shadowCasterBox);
  172. /// Quantize a directional light shadow camera view to eliminate swimming.
  173. void QuantizeDirLightShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& viewBox);
  174. /// Check visibility of one shadow caster.
  175. bool IsShadowCasterVisible(Drawable* drawable, BoundingBox lightViewBox, Camera* shadowCamera, const Matrix3x4& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  176. /// Return the viewport for a shadow map split.
  177. IntRect GetShadowMapViewport(Light* light, unsigned splitIndex, Texture2D* shadowMap);
  178. /// Find and set a new zone for a drawable when it has moved.
  179. void FindZone(Drawable* drawable);
  180. /// Return the drawable's zone, or camera zone if it has override mode enabled.
  181. Zone* GetZone(Drawable* drawable);
  182. /// Return the drawable's light mask, considering also its zone.
  183. unsigned GetLightMask(Drawable* drawable);
  184. /// Return the drawable's shadow mask, considering also its zone.
  185. unsigned GetShadowMask(Drawable* drawable);
  186. /// Return hash code for a vertex light queue.
  187. unsigned long long GetVertexLightQueueHash(const PODVector<Light*>& vertexLights);
  188. /// Return material technique, considering the drawable's LOD distance.
  189. Technique* GetTechnique(Drawable* drawable, Material* material);
  190. /// Check if material should render an auxiliary view (if it has a camera attached.)
  191. void CheckMaterialForAuxView(Material* material);
  192. /// Choose shaders for a batch and add it to queue.
  193. void AddBatchToQueue(BatchQueue& queue, Batch& batch, Technique* tech, bool allowInstancing = true, bool allowShadows = true);
  194. /// Prepare instancing buffer by filling it with all instance transforms.
  195. void PrepareInstancingBuffer();
  196. /// Set up a light volume rendering batch.
  197. void SetupLightVolumeBatch(Batch& batch);
  198. /// Render a shadow map.
  199. void RenderShadowMap(const LightBatchQueue& queue);
  200. /// Return the proper depth-stencil surface to use for a rendertarget.
  201. RenderSurface* GetDepthStencil(RenderSurface* renderTarget);
  202. /// Graphics subsystem.
  203. WeakPtr<Graphics> graphics_;
  204. /// Renderer subsystem.
  205. WeakPtr<Renderer> renderer_;
  206. /// Scene to use.
  207. Scene* scene_;
  208. /// Octree to use.
  209. Octree* octree_;
  210. /// Camera to use.
  211. Camera* camera_;
  212. /// Camera's scene node.
  213. Node* cameraNode_;
  214. /// Zone the camera is inside, or default zone if not assigned.
  215. Zone* cameraZone_;
  216. /// Zone at far clip plane.
  217. Zone* farClipZone_;
  218. /// Occlusion buffer for the main camera.
  219. OcclusionBuffer* occlusionBuffer_;
  220. /// Destination color rendertarget.
  221. RenderSurface* renderTarget_;
  222. /// Color rendertarget active for the current renderpath command.
  223. RenderSurface* currentRenderTarget_;
  224. /// Viewport rectangle.
  225. IntRect viewRect_;
  226. /// Viewport size.
  227. IntVector2 viewSize_;
  228. /// Rendertarget size.
  229. IntVector2 rtSize_;
  230. /// Information of the frame being rendered.
  231. FrameInfo frame_;
  232. /// Write screenbuffer index.
  233. unsigned writeBuffer_;
  234. /// Read screenbuffer index.
  235. unsigned readBuffer_;
  236. /// Minimum Z value of the visible scene.
  237. float minZ_;
  238. /// Maximum Z value of the visible scene.
  239. float maxZ_;
  240. /// Material quality level.
  241. int materialQuality_;
  242. /// Maximum number of occluder triangles.
  243. int maxOccluderTriangles_;
  244. /// Minimum number of instances required in a batch group to render as instanced.
  245. int minInstances_;
  246. /// Highest zone priority currently visible.
  247. int highestZonePriority_;
  248. /// Camera zone's override flag.
  249. bool cameraZoneOverride_;
  250. /// Draw shadows flag.
  251. bool drawShadows_;
  252. /// Deferred flag. Inferred from the existence of a light volume command in the renderpath.
  253. bool deferred_;
  254. /// Deferred ambient pass flag. This means that the destination rendertarget is being written to at the same time as albedo/normal/depth buffers, and needs to be RGBA on OpenGL.
  255. bool deferredAmbient_;
  256. /// Renderpath.
  257. RenderPath* renderPath_;
  258. /// Intermediate screen buffers used in pingpong copies and OpenGL deferred framebuffer blit.
  259. PODVector<Texture2D*> screenBuffers_;
  260. /// Per-thread octree query results.
  261. Vector<PODVector<Drawable*> > tempDrawables_;
  262. /// Per-thread geometries, lights and Z range collection results.
  263. Vector<PerThreadSceneResult> sceneResults_;
  264. /// Visible zones.
  265. PODVector<Zone*> zones_;
  266. /// Visible geometry objects.
  267. PODVector<Drawable*> geometries_;
  268. /// Geometry objects visible in shadow maps.
  269. PODVector<Drawable*> shadowGeometries_;
  270. /// Geometry objects that will be updated in the main thread.
  271. PODVector<Drawable*> nonThreadedGeometries_;
  272. /// Geometry objects that will be updated in worker threads.
  273. PODVector<Drawable*> threadedGeometries_;
  274. /// Occluder objects.
  275. PODVector<Drawable*> occluders_;
  276. /// Lights.
  277. PODVector<Light*> lights_;
  278. /// Light volume vertex shaders.
  279. PODVector<ShaderVariation*> lightVS_;
  280. /// Light volume pixel shaders.
  281. PODVector<ShaderVariation*> lightPS_;
  282. /// Drawables that limit their maximum light count.
  283. HashSet<Drawable*> maxLightsDrawables_;
  284. /// Rendertargets defined by the renderpath.
  285. HashMap<StringHash, Texture2D*> renderTargets_;
  286. /// Intermediate light processing results.
  287. Vector<LightQueryResult> lightQueryResults_;
  288. /// Info for scene render passes defined by the renderpath.
  289. Vector<ScenePassInfo> scenePasses_;
  290. /// Per-pixel light queues.
  291. Vector<LightBatchQueue> lightQueues_;
  292. /// Per-vertex light queues.
  293. HashMap<unsigned long long, LightBatchQueue> vertexLightQueues_;
  294. /// Batch queues.
  295. HashMap<StringHash, BatchQueue> batchQueues_;
  296. /// Hash of the GBuffer pass, or null if none.
  297. StringHash gBufferPassName_;
  298. /// Hash of the opaque forward base pass.
  299. StringHash basePassName_;
  300. /// Hash of the alpha pass.
  301. StringHash alphaPassName_;
  302. /// Hash of the forward light pass.
  303. StringHash lightPassName_;
  304. /// Hash of the litbase pass.
  305. StringHash litBasePassName_;
  306. /// Hash of the litalpha pass.
  307. StringHash litAlphaPassName_;
  308. };
  309. }