Renderer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 "Color.h"
  26. #include "Drawable.h"
  27. #include "RenderSurface.h"
  28. #include <set>
  29. class DebugRenderer;
  30. class Geometry;
  31. class Drawable;
  32. class Light;
  33. class Material;
  34. class Pass;
  35. class Technique;
  36. class Octree;
  37. class Graphics;
  38. class ResourceCache;
  39. class Skeleton;
  40. class OcclusionBuffer;
  41. class Texture2D;
  42. class View;
  43. class Zone;
  44. static const int SHADOW_MIN_PIXELS = 64;
  45. static const int NUM_SHADOWMAP_RESOLUTIONS = 3;
  46. static const int MIN_INSTANCES = 3;
  47. static const int INSTANCING_BUFFER_DEFAULT_SIZE = 1024;
  48. /// Light vertex shader variations
  49. enum LightVSVariation
  50. {
  51. LVS_NONE = 0,
  52. LVS_SPOT,
  53. LVS_SHADOW,
  54. LVS_SPOTSHADOW,
  55. MAX_LIGHT_VS_VARIATIONS
  56. };
  57. /// Light pixel shader variations
  58. enum LightPSVariation
  59. {
  60. LPS_NONE = 0,
  61. LPS_SPEC,
  62. LPS_SHADOW,
  63. LPS_SHADOWSPEC,
  64. LPS_SPOT,
  65. LPS_SPOTSPEC,
  66. LPS_SPOTSHADOW,
  67. LPS_SPOTSHADOWSPEC,
  68. LPS_POINT,
  69. LPS_POINTSPEC,
  70. LPS_POINTSHADOW,
  71. LPS_POINTSHADOWSPEC,
  72. LPS_POINTMASK,
  73. LPS_POINTMASKSPEC,
  74. LPS_POINTMASKSHADOW,
  75. LPS_POINTMASKSHADOWSPEC,
  76. MAX_LIGHT_PS_VARIATIONS
  77. };
  78. /// Deferred light volume vertex shader variations
  79. enum DeferredLightVSVariation
  80. {
  81. DLVS_NONE = 0,
  82. DLVS_DIR,
  83. DLVS_ORTHO,
  84. DLVS_ORTHODIR,
  85. MAX_DEFERRED_LIGHT_VS_VARIATIONS
  86. };
  87. /// Deferred light volume pixels shader variations
  88. enum DeferredLightPSVariation
  89. {
  90. DLPS_NONE = 0,
  91. DLPS_SPEC,
  92. DLPS_SHADOW,
  93. DLPS_SHADOWSPEC,
  94. DLPS_SPOT,
  95. DLPS_SPOTSPEC,
  96. DLPS_SPOTSHADOW,
  97. DLPS_SPOTSHADOWSPEC,
  98. DLPS_POINT,
  99. DLPS_POINTSPEC,
  100. DLPS_POINTSHADOW,
  101. DLPS_POINTSHADOWSPEC,
  102. DLPS_POINTMASK,
  103. DLPS_POINTMASKSPEC,
  104. DLPS_POINTMASKSHADOW,
  105. DLPS_POINTMASKSHADOWSPEC,
  106. DLPS_ORTHO,
  107. DLPS_ORTHOSPEC,
  108. DLPS_ORTHOSHADOW,
  109. DLPS_ORTHOSHADOWSPEC,
  110. DLPS_ORTHOSPOT,
  111. DLPS_ORTHOSPOTSPEC,
  112. DLPS_ORTHOSPOTSHADOW,
  113. DLPS_ORTHOSPOTSHADOWSPEC,
  114. DLPS_ORTHOPOINT,
  115. DLPS_ORTHOPOINTSPEC,
  116. DLPS_ORTHOPOINTSHADOW,
  117. DLPS_ORTHOPOINTSHADOWSPEC,
  118. DLPS_ORTHOPOINTMASK,
  119. DLPS_ORTHOPOINTMASKSPEC,
  120. DLPS_ORTHOPOINTMASKSHADOW,
  121. DLPS_ORTHOPOINTMASKSHADOWSPEC,
  122. MAX_DEFERRED_LIGHT_PS_VARIATIONS
  123. };
  124. /// High-level rendering subsystem. Manages drawing of 3D views
  125. class Renderer : public Object
  126. {
  127. OBJECT(Object);
  128. friend class View;
  129. public:
  130. /// Construct
  131. Renderer(Context* context);
  132. /// Destruct
  133. virtual ~Renderer();
  134. /// Set number of viewports to render
  135. void SetNumViewports(unsigned num);
  136. /// Set a viewport
  137. void SetViewport(unsigned index, const Viewport& viewport);
  138. /// Set specular lighting on/off
  139. void SetSpecularLighting(bool enable);
  140. /// Set shadows on/off
  141. void SetDrawShadows(bool enable);
  142. /// Set texture anisotropy
  143. void SetTextureAnisotropy(int level);
  144. /// Set texture filtering
  145. void SetTextureFilterMode(TextureFilterMode mode);
  146. /// Set texture quality level
  147. void SetTextureQuality(int quality);
  148. /// Set material quality level
  149. void SetMaterialQuality(int quality);
  150. /// Set shadow map resolution
  151. void SetShadowMapSize(int size);
  152. /// Set shadow map 24-bit depth on/off
  153. void SetShadowMapHiresDepth(bool enable);
  154. /// Set reuse of shadowmaps. Default is true, disabling allows transparent geometry shadowing
  155. void SetReuseShadowMaps(bool enable);
  156. /// Set number of full, half and quarter size shadowmaps. Only has effect if reuse of shadowmaps is disabled first
  157. void SetNumShadowMaps(unsigned full, unsigned half, unsigned quarter);
  158. /// Set dynamic instancing on/off
  159. void SetDynamicInstancing(bool enable);
  160. /// Set maximum number of occluder trianges
  161. void SetMaxOccluderTriangles(int triangles);
  162. /// Set occluder buffer width
  163. void SetOcclusionBufferSize(int size);
  164. /// Set required size (1.0 = full screen) for occluders
  165. void SetOccluderSizeThreshold(float screenSize);
  166. /// Return number of viewports
  167. unsigned GetNumViewports() const { return viewports_.size(); }
  168. /// Return viewport
  169. const Viewport& GetViewport(unsigned index) const;
  170. /// Return whether specular lighting is enabled
  171. bool GetSpecularLighting() const { return specularLighting_; }
  172. /// Return whether drawing shadows is enabled
  173. bool GetDrawShadows() const { return drawShadows_; }
  174. /// Return texture anisotropy
  175. int GetTextureAnisotropy() const { return textureAnisotropy_; }
  176. /// Return texture filtering
  177. TextureFilterMode GetTextureFilterMode() const { return textureFilterMode_; }
  178. /// Return texture quality level
  179. int GetTextureQuality() const { return textureQuality_; }
  180. /// Return material quality level
  181. int GetMaterialQuality() const { return materialQuality_; }
  182. /// Return shadow map resolution
  183. int GetShadowMapSize() const { return shadowMapSize_; }
  184. /// Return whether shadow maps use 24-bit depth
  185. bool GetShadowMapHiresDepth() const { return shadowMapHiresDepth_; }
  186. /// Return whether shadow maps are reused
  187. bool GetReuseShadowMaps() const { return reuseShadowMaps_; }
  188. /// Return number of full resolution shadow maps
  189. unsigned GetNumFullShadowMaps() const { return shadowMaps_[0].size(); }
  190. /// Return number of half resolution shadow maps
  191. unsigned GetNumHalfShadowMaps() const { return shadowMaps_[1].size(); }
  192. /// Return number of quarter resolution shadow maps
  193. unsigned GetNumQuarterShadowMaps() const { return shadowMaps_[2].size(); }
  194. /// Return whether dynamic instancing is in use
  195. bool GetDynamicInstancing() const { return dynamicInstancing_; }
  196. /// Return maximum number of occluder triangles
  197. int GetMaxOccluderTriangles() const { return maxOccluderTriangles_; }
  198. /// Return occlusion buffer width
  199. int GetOcclusionBufferSize() const { return occlusionBufferSize_; }
  200. /// Return occluder screen size threshold
  201. float GetOccluderSizeThreshold() const { return occluderSizeThreshold_; }
  202. /// Return number of views rendered
  203. unsigned GetNumViews() const { return numViews_; }
  204. /// Return number of primitives rendered
  205. unsigned GetNumPrimitives() const { return numPrimitives_; }
  206. /// Return number of batches rendered
  207. unsigned GetNumBatches() const { return numBatches_; }
  208. /// Return number of geometries rendered
  209. unsigned GetNumGeometries(bool allViews = false) const;
  210. /// Return number of lights rendered
  211. unsigned GetNumLights(bool allViews = false) const;
  212. /// Return number of shadow maps rendered
  213. unsigned GetNumShadowMaps(bool allViews = false) const;
  214. /// Return number of occluders rendered
  215. unsigned GetNumOccluders(bool allViews = false) const;
  216. /// Return number of directional light shadow occluders rendered
  217. unsigned GetNumShadowOccluders(bool allViews = false) const;
  218. /// Return an occlusion buffer for inspection
  219. const OcclusionBuffer* GetOcclusionBuffer(float aspectRatio, bool halfResolution = false);
  220. /// Return the default zone
  221. Zone* GetDefaultZone() const { return defaultZone_; }
  222. /// Return the default material
  223. Material* GetDefaultMaterial() const { return defaultMaterial_; }
  224. /// Return the default range attenuation texture
  225. Texture2D* GetDefaultLightRamp() const { return defaultLightRamp_; }
  226. /// Return the default spotlight attenuation texture
  227. Texture2D* GetDefaultLightSpot() const { return defaultLightSpot; }
  228. /// Return a vertex shader by name
  229. VertexShader* GetVertexShader(const std::string& name, bool checkExists = false) const;
  230. /// Return a pixel shader by name
  231. PixelShader* GetPixelShader(const std::string& name, bool checkExists = false) const;
  232. /// Return the frame update parameters
  233. const FrameInfo& GetFrameInfo() { return frame_; }
  234. /// Update for rendering. Called by handleRenderUpdate()
  235. void Update(float timeStep);
  236. /// Render. Called by Engine
  237. void Render();
  238. /// Add debug geometry to the debug graphics(s)
  239. void DrawDebugGeometry(bool depthTest);
  240. private:
  241. /// Initialize when screen mode initially set
  242. void Initialize();
  243. /// Clear views from previous frame
  244. void ResetViews();
  245. /// Add a view. Return true if successful
  246. bool AddView(RenderSurface* renderTarget, const Viewport& viewport);
  247. /// Return an occlusion buffer for use
  248. OcclusionBuffer* GetOrCreateOcclusionBuffer(Camera* camera, int maxOccluderTriangles, bool halfResolution = false);
  249. /// Return volume geometry for a light
  250. Geometry* GetLightGeometry(Light* light);
  251. /// Return shadow map by resolution. If shadow map reuse is disabled, a different map is returned each time
  252. Texture2D* GetShadowMap(float resolution);
  253. /// Reset shadow map use count
  254. void ResetShadowMapUseCount();
  255. /// Choose shaders for a batch
  256. void setBatchShaders(Batch& batch, Technique* technique, Pass* pass, bool allowShadows = true);
  257. /// Choose light volume shaders for a batch
  258. void SetLightVolumeShaders(Batch& batch);
  259. /// Reload shaders
  260. void LoadShaders();
  261. /// Reload shaders for a material technique
  262. void LoadMaterialShaders(Technique* technique);
  263. /// Reload shaders for a material pass
  264. void LoadPassShaders(Technique* technique, PassType pass, bool allowShadows = true);
  265. /// Release shaders used in materials
  266. void ReleaseMaterialShaders();
  267. /// Reload textures
  268. void ReloadTextures();
  269. /// Create light volume geometries
  270. void CreateGeometries();
  271. /// Create instancing vertex buffer
  272. void CreateInstancingBuffer();
  273. /// Ensure sufficient size of the instancing vertex buffer. Return true if successful
  274. bool ResizeInstancingBuffer(unsigned numInstances);
  275. /// Create shadow maps. Return true if successful
  276. bool CreateShadowMaps();
  277. /// Split a light into several for shadow mapping
  278. unsigned SplitLight(Light* light);
  279. /// Allocate a shadow camera and a scene node for it
  280. Camera* CreateShadowCamera();
  281. /// Allocate (if necessary) and clone a light. Attach it to a temporary scene node
  282. Light* CreateSplitLight(Light* original);
  283. /// Allocate a temporary scene node for attaching a split light or a shadow camera
  284. Node* CreateTempNode();
  285. /// Set up a light volume rendering batch
  286. void SetupLightBatch(Batch& batch);
  287. /// Draw a full screen quad (either near or far)
  288. void DrawFullScreenQuad(Camera& camera, VertexShader* vs, PixelShader* ps, bool nearQuad);
  289. /// Handle screen mode event
  290. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  291. /// Handle render update event
  292. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  293. /// Graphics
  294. WeakPtr<Graphics> graphics_;
  295. /// Resource cache
  296. WeakPtr<ResourceCache> cache_;
  297. /// Default zone
  298. SharedPtr<Zone> defaultZone_;
  299. /// Directional light geometry
  300. SharedPtr<Geometry> dirLightGeometry_;
  301. /// Point light volume geometry
  302. SharedPtr<Geometry> pointLightGeometry_;
  303. /// Spot light volume geometry
  304. SharedPtr<Geometry> spotLightGeometry_;
  305. /// Instance stream vertex buffer
  306. SharedPtr<VertexBuffer> instancingBuffer_;
  307. /// Default material
  308. SharedPtr<Material> defaultMaterial_;
  309. /// Default range attenuation texture
  310. SharedPtr<Texture2D> defaultLightRamp_;
  311. /// Default spotlight attenuation texture
  312. SharedPtr<Texture2D> defaultLightSpot;
  313. /// Shadow maps by resolution
  314. std::vector<SharedPtr<Texture2D> > shadowMaps_[NUM_SHADOWMAP_RESOLUTIONS];
  315. /// Shadow map dummy color textures by resolution
  316. SharedPtr<Texture2D> colorShadowMaps_[NUM_SHADOWMAP_RESOLUTIONS];
  317. /// Shadow map use count if reusing is disabled. Is reset for each view
  318. unsigned shadowMapUseCount_[NUM_SHADOWMAP_RESOLUTIONS];
  319. /// Stencil rendering vertex shader
  320. SharedPtr<VertexShader> stencilVS_;
  321. /// Stencil rendering pixel shader
  322. SharedPtr<PixelShader> stencilPS_;
  323. /// Light vertex shaders
  324. std::vector<SharedPtr<VertexShader> > lightVS_;
  325. /// Light pixel shaders
  326. std::vector<SharedPtr<PixelShader> > lightPS_;
  327. /// Reusable shadow cameras
  328. std::vector<SharedPtr<Camera> > shadowCameraStore_;
  329. /// Reusable split lights
  330. std::vector<SharedPtr<Light> > splitLightStore_;
  331. /// Reusable temporary scene nodes
  332. std::vector<SharedPtr<Node> > tempNodeStore_;
  333. /// Occlusion buffers
  334. std::map<int, SharedPtr<OcclusionBuffer> > occlusionBuffers_;
  335. /// Viewports
  336. std::vector<Viewport> viewports_;
  337. /// Views
  338. std::vector<SharedPtr<View> > views_;
  339. /// Octrees that have been updated during the frame
  340. std::set<Octree*> updateOctrees_;
  341. /// Techniques for which missing shader error has been displayed
  342. std::set<Technique*> shaderErrorDisplayed_;
  343. /// Vertex shader format
  344. std::string vsFormat_;
  345. /// Pixel shader format
  346. std::string psFormat_;
  347. /// Base directory for shaders
  348. std::string shaderPath_;
  349. /// Light shader base name (deferred and prepass have different light shaders)
  350. std::string lightShaderName_;
  351. /// Number of views
  352. unsigned numViews_;
  353. /// Number of shadow cameras
  354. unsigned numShadowCameras_;
  355. /// Number of split lights
  356. unsigned numSplitLights_;
  357. /// Number of temporary scene nodes
  358. unsigned numTempNodes_;
  359. /// Number of primitives (3D geometry only)
  360. unsigned numPrimitives_;
  361. /// Number of batches (3D geometry only)
  362. unsigned numBatches_;
  363. /// Specular lighting flag
  364. bool specularLighting_;
  365. /// Draw shadows flag
  366. bool drawShadows_;
  367. /// Texture anisotropy level
  368. int textureAnisotropy_;
  369. /// Texture filtering mode
  370. TextureFilterMode textureFilterMode_;
  371. /// Texture quality level
  372. int textureQuality_;
  373. /// Material quality level
  374. int materialQuality_;
  375. /// Shadow map resolution
  376. int shadowMapSize_;
  377. /// Shadow map 24-bit depth flag
  378. bool shadowMapHiresDepth_;
  379. /// Shadow map reuse flag
  380. bool reuseShadowMaps_;
  381. /// Dynamic instancing flag
  382. bool dynamicInstancing_;
  383. /// Maximum occluder triangles
  384. int maxOccluderTriangles_;
  385. /// Occlusion buffer width
  386. int occlusionBufferSize_;
  387. /// Occluder screen size threshold
  388. float occluderSizeThreshold_;
  389. /// Frame number on which shaders last changed
  390. unsigned shadersChangedFrameNumber_;
  391. /// Frame info for rendering
  392. FrameInfo frame_;
  393. /// Shaders need reloading flag
  394. bool shadersDirty_;
  395. /// Initialized flag
  396. bool initialized_;
  397. };