View.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #ifndef RENDERER_VIEW_H
  24. #define RENDERER_VIEW_H
  25. #include "Batch.h"
  26. #include "RefCount.h"
  27. class Camera;
  28. class DebugRenderer;
  29. class Light;
  30. class GeometryNode;
  31. class OcclusionBuffer;
  32. class RenderSurface;
  33. class Zone;
  34. struct Viewport;
  35. static const int MAX_LIGHT_SPLITS = 6;
  36. //! Geometry view space depth minimum and maximum values
  37. struct GeometryDepthBounds
  38. {
  39. float mMin;
  40. float mMax;
  41. };
  42. //! Shadowed light batch queue
  43. struct LightBatchQueue
  44. {
  45. //! Light scene node
  46. Light* mLight;
  47. //! Shadow rendering batches
  48. std::vector<Batch> mShadowBatches;
  49. //! Light volume or opaque rendering batches
  50. std::vector<Batch> mBatches;
  51. //! Sorted shadow batch pointers
  52. std::vector<Batch*> mSortedShadowBatches;
  53. //! Sorted batch pointers
  54. std::vector<Batch*> mSortedBatches;
  55. //! Last split flag (for clearing the stencil buffer)
  56. bool mLastSplit;
  57. };
  58. //! A rendering view. Includes the main view(s) and any auxiliary views, but not shadow cameras
  59. class View : public RefCounted
  60. {
  61. public:
  62. //! Construct with pipeline pointer
  63. View(Pipeline* pipeline);
  64. //! Destruct
  65. virtual ~View();
  66. //! Define with rendertarget and viewport. Return true if successful
  67. bool define(RenderSurface* renderTarget, const Viewport& viewport);
  68. //! Update culling and construct rendering batches
  69. void update(const FrameInfo& frame);
  70. //! Render batches
  71. void render();
  72. //! Return octree
  73. Octree* getOctree() const { return mOctree; }
  74. //! Return camera
  75. Camera* getCamera() const { return mCamera; }
  76. //! Return zone
  77. Zone* getZone() const { return mZone; }
  78. //! Return the render target. 0 if using the backbuffer
  79. RenderSurface* getRenderTarget() const { return mRenderTarget; }
  80. //! Return the depth stencil. 0 if using the backbuffer's depth stencil
  81. RenderSurface* getDepthStencil() const { return mDepthStencil; }
  82. //! Return geometry scene nodes
  83. const std::vector<GeometryNode*>& getGeometries() const { return mGeometries; }
  84. //! Return occluder scene nodes
  85. const std::vector<GeometryNode*>& getOccluders() const { return mOccluders; }
  86. //! Return directional light shadow rendering occluders
  87. const std::vector<GeometryNode*>& getShadowOccluders() const { return mShadowOccluders; }
  88. //! Return light scene nodes
  89. const std::vector<Light*>& getLights() const { return mLights; }
  90. //! Return light batch queues
  91. const std::vector<LightBatchQueue>& getLightQueues() const { return mLightQueues; }
  92. private:
  93. //! Query the octree for scene nodes
  94. void getNodes();
  95. //! Construct batches from the scene nodes
  96. void getBatches();
  97. //! Construct batches for deferred or light prepass rendering
  98. void getBatchesDeferred();
  99. //! Construct batches for forward rendering
  100. void getBatchesForward();
  101. //! Render in deferred or light prepass mode
  102. void renderBatchesDeferred();
  103. //! Render in forward mode
  104. void renderBatchesForward();
  105. //! Clear shader parameter sources
  106. void clearLastParameterSources();
  107. //! Query for occluders as seen from a camera
  108. void updateOccluders(std::vector<GeometryNode*>& occluders, Camera& camera);
  109. //! Draw occluders to occlusion buffer
  110. void drawOccluders(OcclusionBuffer* buffer, const std::vector<GeometryNode*>& occluders);
  111. //! Query for lit geometries and shadow casters for a light
  112. unsigned processLight(Light* light);
  113. //! Generate combined bounding boxes for lit geometries and shadow casters and check shadow caster visibility
  114. void processLightQuery(unsigned splitIndex, const std::vector<VolumeNode*>& result, BoundingBox& geometryBox, BoundingBox& shadowSpaceBox, bool getLitGeometries, bool getShadowCasters);
  115. //! Check visibility of one shadow caster
  116. bool isShadowCasterVisible(GeometryNode* geom, BoundingBox lightViewBox, const Camera& shadowCamera, const Matrix4x3& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  117. //! Set up initial shadow camera view
  118. void setupShadowCamera(Light* light, bool shadowOcclusion = false);
  119. //! Focus shadow camera to use shadow map texture space more optimally
  120. void focusShadowCamera(Light* light, const BoundingBox& geometryBox, const BoundingBox& shadowCasterBox);
  121. //! Quantize the directional light shadow camera view to eliminate artefacts
  122. void quantizeDirShadowCamera(Light* light, const BoundingBox& viewBox);
  123. //! Optimize light rendering by setting up a scissor rectangle
  124. void optimizeLightByScissor(Light* light);
  125. //! Return scissor rectangle for a light
  126. const Rect& getLightScissor(Light* light);
  127. //! Split directional or point light for shadow rendering
  128. unsigned splitLight(Light* light);
  129. //! Return material technique for a scene node's geometry
  130. MaterialTechnique* getMaterialTechnique(GeometryNode* node, unsigned index);
  131. //! Check if material technique should render an auxiliary view (if it has a camera attached)
  132. void checkTechniqueForAuxView(MaterialTechnique* technique);
  133. //! Sort a batch list
  134. static void sortBatches(std::vector<Batch>& originalBatches, std::vector<Batch*>& sortedBatches);
  135. //! Pipeline
  136. Pipeline* mPipeline;
  137. //! Octree to use
  138. Octree* mOctree;
  139. //! Camera to use
  140. Camera* mCamera;
  141. //! Zone to get global rendering settings from
  142. Zone* mZone;
  143. //! Color buffer to use
  144. RenderSurface* mRenderTarget;
  145. //! Depth buffer to use
  146. RenderSurface* mDepthStencil;
  147. //! Screen rectangle
  148. IntRect mScreenRect;
  149. //! Render target width
  150. int mWidth;
  151. //! Render target height
  152. int mHeight;
  153. //! Draw shadows flag
  154. bool mDrawShadows;
  155. //! Shader Model 3 support flag
  156. bool mSM3Support;
  157. //! Light detail level
  158. int mLightDetailLevel;
  159. //! Material quality level
  160. int mMaterialQuality;
  161. //! Maximum number of occluder triangles
  162. int mMaxOccluderTriangles;
  163. //! Information of the frame being rendered
  164. FrameInfo mFrame;
  165. //! Combined bounding box of visible geometries
  166. BoundingBox mSceneBox;
  167. //! Combined bounding box of visible geometries in view space
  168. BoundingBox mSceneViewBox;
  169. //! Cache for light scissor queries
  170. std::map<Light*, Rect> mLightScissorCache;
  171. //! Geometry scene nodes
  172. std::vector<GeometryNode*> mGeometries;
  173. //! Occluder scene nodes
  174. std::vector<GeometryNode*> mOccluders;
  175. //! Directional light shadow rendering occluder scene nodes
  176. std::vector<GeometryNode*> mShadowOccluders;
  177. //! Depth minimum and maximum values for visible geometries
  178. std::vector<GeometryDepthBounds> mGeometryDepthBounds;
  179. //! Light scene nodes
  180. std::vector<Light*> mLights;
  181. //! G-buffer batch queue
  182. std::vector<Batch> mGBufferQueue;
  183. //! Negative light volumes batch queue
  184. std::vector<Batch> mNegativeLightQueue;
  185. //! Non-shadowcasting light volumes batch queue
  186. std::vector<Batch> mNoShadowLightQueue;
  187. //! Ambient pass batch queue
  188. std::vector<Batch> mAmbientQueue;
  189. //! Negative ambient pass batch queue
  190. std::vector<Batch> mNegativeQueue;
  191. //! Forward pass batch queue
  192. std::vector<Batch> mForwardQueue;
  193. //! Post-opaque pass batch queue
  194. std::vector<Batch> mPostOpaqueQueue;
  195. //! Transparent geometries batch queue
  196. std::vector<Batch> mTransparentQueue;
  197. //! Shadowed light batch queues
  198. std::vector<LightBatchQueue> mLightQueues;
  199. //! Sorted G-buffer batch queue
  200. std::vector<Batch*> mGBufferQueueSorted;
  201. //! Sorted negative light volumes batch queue
  202. std::vector<Batch*> mNegativeLightQueueSorted;
  203. //! Sorted non-shadowcasting light volumes batch queue
  204. std::vector<Batch*> mNoShadowLightQueueSorted;
  205. //! Sorted ambient pass batch queue
  206. std::vector<Batch*> mAmbientQueueSorted;
  207. //! Sorted negative ambient pass batch queue
  208. std::vector<Batch*> mNegativeQueueSorted;
  209. //! Sorted forward pass batch queue
  210. std::vector<Batch*> mForwardQueueSorted;
  211. //! Sorted post-opaque pass batch queue
  212. std::vector<Batch*> mPostOpaqueQueueSorted;
  213. //! Sorted transparent geometries batch queue
  214. std::vector<Batch*> mTransparentQueueSorted;
  215. //! Default zone if no other zone found
  216. static Zone sDefaultZone;
  217. //! Current split lights
  218. static Light* sSplitLights[MAX_LIGHT_SPLITS];
  219. //! Current light's lit geometries
  220. static std::vector<GeometryNode*> sLitGeometries[MAX_LIGHT_SPLITS];
  221. //! Current light's shadow casters
  222. static std::vector<GeometryNode*> sShadowCasters[MAX_LIGHT_SPLITS];
  223. };
  224. #endif // RENDERER_VIEW_H