Renderer.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Container/HashSet.h"
  25. #include "../Core/Mutex.h"
  26. #include "../Graphics/Batch.h"
  27. #include "../Graphics/Drawable.h"
  28. #include "../Graphics/Viewport.h"
  29. #include "../Math/Color.h"
  30. namespace Urho3D
  31. {
  32. class Geometry;
  33. class Drawable;
  34. class Light;
  35. class Material;
  36. class Pass;
  37. class Technique;
  38. class Octree;
  39. class Graphics;
  40. class RenderPath;
  41. class RenderSurface;
  42. class ResourceCache;
  43. class Scene;
  44. class Skeleton;
  45. class OcclusionBuffer;
  46. class Technique;
  47. class Texture;
  48. class Texture2D;
  49. class TextureCube;
  50. class View;
  51. class Zone;
  52. struct BatchQueue;
  53. static const int SHADOW_MIN_PIXELS = 64;
  54. static const int INSTANCING_BUFFER_DEFAULT_SIZE = 1024;
  55. /// Light vertex shader variations.
  56. enum LightVSVariation
  57. {
  58. LVS_DIR = 0,
  59. LVS_SPOT,
  60. LVS_POINT,
  61. LVS_SHADOW,
  62. LVS_SPOTSHADOW,
  63. LVS_POINTSHADOW,
  64. LVS_SHADOWNORMALOFFSET,
  65. LVS_SPOTSHADOWNORMALOFFSET,
  66. LVS_POINTSHADOWNORMALOFFSET,
  67. MAX_LIGHT_VS_VARIATIONS
  68. };
  69. /// Per-vertex light vertex shader variations.
  70. enum VertexLightVSVariation
  71. {
  72. VLVS_NOLIGHTS = 0,
  73. VLVS_1LIGHT,
  74. VLVS_2LIGHTS,
  75. VLVS_3LIGHTS,
  76. VLVS_4LIGHTS,
  77. MAX_VERTEXLIGHT_VS_VARIATIONS
  78. };
  79. /// Light pixel shader variations.
  80. enum LightPSVariation
  81. {
  82. LPS_NONE = 0,
  83. LPS_SPOT,
  84. LPS_POINT,
  85. LPS_POINTMASK,
  86. LPS_SPEC,
  87. LPS_SPOTSPEC,
  88. LPS_POINTSPEC,
  89. LPS_POINTMASKSPEC,
  90. LPS_SHADOW,
  91. LPS_SPOTSHADOW,
  92. LPS_POINTSHADOW,
  93. LPS_POINTMASKSHADOW,
  94. LPS_SHADOWSPEC,
  95. LPS_SPOTSHADOWSPEC,
  96. LPS_POINTSHADOWSPEC,
  97. LPS_POINTMASKSHADOWSPEC,
  98. MAX_LIGHT_PS_VARIATIONS
  99. };
  100. /// Deferred light volume vertex shader variations.
  101. enum DeferredLightVSVariation
  102. {
  103. DLVS_NONE = 0,
  104. DLVS_DIR,
  105. DLVS_ORTHO,
  106. DLVS_ORTHODIR,
  107. MAX_DEFERRED_LIGHT_VS_VARIATIONS
  108. };
  109. /// Deferred light volume pixels shader variations.
  110. enum DeferredLightPSVariation
  111. {
  112. DLPS_NONE = 0,
  113. DLPS_SPOT,
  114. DLPS_POINT,
  115. DLPS_POINTMASK,
  116. DLPS_SPEC,
  117. DLPS_SPOTSPEC,
  118. DLPS_POINTSPEC,
  119. DLPS_POINTMASKSPEC,
  120. DLPS_SHADOW,
  121. DLPS_SPOTSHADOW,
  122. DLPS_POINTSHADOW,
  123. DLPS_POINTMASKSHADOW,
  124. DLPS_SHADOWSPEC,
  125. DLPS_SPOTSHADOWSPEC,
  126. DLPS_POINTSHADOWSPEC,
  127. DLPS_POINTMASKSHADOWSPEC,
  128. DLPS_SHADOWNORMALOFFSET,
  129. DLPS_SPOTSHADOWNORMALOFFSET,
  130. DLPS_POINTSHADOWNORMALOFFSET,
  131. DLPS_POINTMASKSHADOWNORMALOFFSET,
  132. DLPS_SHADOWSPECNORMALOFFSET,
  133. DLPS_SPOTSHADOWSPECNORMALOFFSET,
  134. DLPS_POINTSHADOWSPECNORMALOFFSET,
  135. DLPS_POINTMASKSHADOWSPECNORMALOFFSET,
  136. DLPS_ORTHO,
  137. DLPS_ORTHOSPOT,
  138. DLPS_ORTHOPOINT,
  139. DLPS_ORTHOPOINTMASK,
  140. DLPS_ORTHOSPEC,
  141. DLPS_ORTHOSPOTSPEC,
  142. DLPS_ORTHOPOINTSPEC,
  143. DLPS_ORTHOPOINTMASKSPEC,
  144. DLPS_ORTHOSHADOW,
  145. DLPS_ORTHOSPOTSHADOW,
  146. DLPS_ORTHOPOINTSHADOW,
  147. DLPS_ORTHOPOINTMASKSHADOW,
  148. DLPS_ORTHOSHADOWSPEC,
  149. DLPS_ORTHOSPOTSHADOWSPEC,
  150. DLPS_ORTHOPOINTSHADOWSPEC,
  151. DLPS_ORTHOPOINTMASKSHADOWSPEC,
  152. DLPS_ORTHOSHADOWNORMALOFFSET,
  153. DLPS_ORTHOSPOTSHADOWNORMALOFFSET,
  154. DLPS_ORTHOPOINTSHADOWNORMALOFFSET,
  155. DLPS_ORTHOPOINTMASKSHADOWNORMALOFFSET,
  156. DLPS_ORTHOSHADOWSPECNORMALOFFSET,
  157. DLPS_ORTHOSPOTSHADOWSPECNORMALOFFSET,
  158. DLPS_ORTHOPOINTSHADOWSPECNORMALOFFSET,
  159. DLPS_ORTHOPOINTMASKSHADOWSPECNORMALOFFSET,
  160. MAX_DEFERRED_LIGHT_PS_VARIATIONS
  161. };
  162. /// High-level rendering subsystem. Manages drawing of 3D views.
  163. class URHO3D_API Renderer : public Object
  164. {
  165. URHO3D_OBJECT(Renderer, Object);
  166. public:
  167. using ShadowMapFilter = void(Object::*)(View* view, Texture2D* shadowMap, float blurScale);
  168. /// Construct.
  169. explicit Renderer(Context* context);
  170. /// Destruct.
  171. ~Renderer() override;
  172. /// Set number of backbuffer viewports to render.
  173. /// @property
  174. void SetNumViewports(unsigned num);
  175. /// Set a backbuffer viewport.
  176. /// @property{set_viewports}
  177. void SetViewport(unsigned index, Viewport* viewport);
  178. /// Set default renderpath.
  179. /// @property
  180. void SetDefaultRenderPath(RenderPath* renderPath);
  181. /// Set default renderpath from an XML file.
  182. void SetDefaultRenderPath(XMLFile* xmlFile);
  183. /// Set default non-textured material technique.
  184. /// @property
  185. void SetDefaultTechnique(Technique* technique);
  186. /// Set HDR rendering on/off.
  187. /// @property{set_hdrRendering}
  188. void SetHDRRendering(bool enable);
  189. /// Set specular lighting on/off.
  190. /// @property
  191. void SetSpecularLighting(bool enable);
  192. /// Set default texture max anisotropy level.
  193. /// @property
  194. void SetTextureAnisotropy(int level);
  195. /// Set default texture filtering.
  196. /// @property
  197. void SetTextureFilterMode(TextureFilterMode mode);
  198. /// Set texture quality level. See the QUALITY constants in GraphicsDefs.h.
  199. /// @property
  200. void SetTextureQuality(MaterialQuality quality);
  201. /// Set material quality level. See the QUALITY constants in GraphicsDefs.h.
  202. /// @property
  203. void SetMaterialQuality(MaterialQuality quality);
  204. /// Set shadows on/off.
  205. /// @property
  206. void SetDrawShadows(bool enable);
  207. /// Set shadow map resolution.
  208. /// @property
  209. void SetShadowMapSize(int size);
  210. /// Set shadow quality mode. See the SHADOWQUALITY enum in GraphicsDefs.h.
  211. /// @property
  212. void SetShadowQuality(ShadowQuality quality);
  213. /// Set shadow softness, only works when SHADOWQUALITY_BLUR_VSM is used.
  214. /// @property
  215. void SetShadowSoftness(float shadowSoftness);
  216. /// Set shadow parameters when VSM is used, they help to reduce light bleeding. LightBleeding must be in [0, 1].
  217. void SetVSMShadowParameters(float minVariance, float lightBleedingReduction);
  218. /// Set VSM shadow map multisampling level. Default 1 (no multisampling).
  219. /// @property{set_vsmMultiSample}
  220. void SetVSMMultiSample(int multiSample);
  221. /// Set post processing filter to the shadow map.
  222. /// @nobind
  223. void SetShadowMapFilter(Object* instance, ShadowMapFilter functionPtr);
  224. /// Set reuse of shadow maps. Default is true. If disabled, also transparent geometry can be shadowed.
  225. /// @property
  226. void SetReuseShadowMaps(bool enable);
  227. /// Set maximum number of shadow maps created for one resolution. Only has effect if reuse of shadow maps is disabled.
  228. /// @property
  229. void SetMaxShadowMaps(int shadowMaps);
  230. /// Set dynamic instancing on/off. When on (default), drawables using the same static-type geometry and material will be automatically combined to an instanced draw call.
  231. /// @property
  232. void SetDynamicInstancing(bool enable);
  233. /// Set number of extra instancing buffer elements. Default is 0. Extra 4-vectors are available through TEXCOORD7 and further.
  234. /// @property
  235. void SetNumExtraInstancingBufferElements(int elements);
  236. /// Set minimum number of instances required in a batch group to render as instanced.
  237. /// @property
  238. void SetMinInstances(int instances);
  239. /// Set maximum number of sorted instances per batch group. If exceeded, instances are rendered unsorted.
  240. /// @property
  241. void SetMaxSortedInstances(int instances);
  242. /// Set maximum number of occluder triangles.
  243. /// @property
  244. void SetMaxOccluderTriangles(int triangles);
  245. /// Set occluder buffer width.
  246. /// @property
  247. void SetOcclusionBufferSize(int size);
  248. /// Set required screen size (1.0 = full screen) for occluders.
  249. /// @property
  250. void SetOccluderSizeThreshold(float screenSize);
  251. /// Set whether to thread occluder rendering. Default false.
  252. /// @property
  253. void SetThreadedOcclusion(bool enable);
  254. /// Set shadow depth bias multiplier for mobile platforms to counteract possible worse shadow map precision. Default 1.0 (no effect).
  255. /// @property
  256. void SetMobileShadowBiasMul(float mul);
  257. /// Set shadow depth bias addition for mobile platforms to counteract possible worse shadow map precision. Default 0.0 (no effect).
  258. /// @property
  259. void SetMobileShadowBiasAdd(float add);
  260. /// Set shadow normal offset multiplier for mobile platforms to counteract possible worse shadow map precision. Default 1.0 (no effect).
  261. /// @property
  262. void SetMobileNormalOffsetMul(float mul);
  263. /// Force reload of shaders.
  264. void ReloadShaders();
  265. /// Apply post processing filter to the shadow map. Called by View.
  266. void ApplyShadowMapFilter(View* view, Texture2D* shadowMap, float blurScale);
  267. /// Return number of backbuffer viewports.
  268. /// @property
  269. unsigned GetNumViewports() const { return viewports_.Size(); }
  270. /// Return backbuffer viewport by index.
  271. /// @property{get_viewports}
  272. Viewport* GetViewport(unsigned index) const;
  273. /// Return nth backbuffer viewport associated to a scene. Index 0 returns the first.
  274. Viewport* GetViewportForScene(Scene* scene, unsigned index) const;
  275. /// Return default renderpath.
  276. /// @property
  277. RenderPath* GetDefaultRenderPath() const;
  278. /// Return default non-textured material technique.
  279. /// @property
  280. Technique* GetDefaultTechnique() const;
  281. /// Return whether HDR rendering is enabled.
  282. /// @property{get_hdrRendering}
  283. bool GetHDRRendering() const { return hdrRendering_; }
  284. /// Return whether specular lighting is enabled.
  285. /// @property
  286. bool GetSpecularLighting() const { return specularLighting_; }
  287. /// Return whether drawing shadows is enabled.
  288. /// @property
  289. bool GetDrawShadows() const { return drawShadows_; }
  290. /// Return default texture max. anisotropy level.
  291. /// @property
  292. int GetTextureAnisotropy() const { return textureAnisotropy_; }
  293. /// Return default texture filtering mode.
  294. /// @property
  295. TextureFilterMode GetTextureFilterMode() const { return textureFilterMode_; }
  296. /// Return texture quality level.
  297. /// @property
  298. MaterialQuality GetTextureQuality() const { return textureQuality_; }
  299. /// Return material quality level.
  300. /// @property
  301. MaterialQuality GetMaterialQuality() const { return materialQuality_; }
  302. /// Return shadow map resolution.
  303. /// @property
  304. int GetShadowMapSize() const { return shadowMapSize_; }
  305. /// Return shadow quality.
  306. /// @property
  307. ShadowQuality GetShadowQuality() const { return shadowQuality_; }
  308. /// Return shadow softness.
  309. /// @property
  310. float GetShadowSoftness() const { return shadowSoftness_; }
  311. /// Return VSM shadow parameters.
  312. /// @property{get_vsmShadowParameters}
  313. Vector2 GetVSMShadowParameters() const { return vsmShadowParams_; };
  314. /// Return VSM shadow multisample level.
  315. /// @property{get_vsmMultiSample}
  316. int GetVSMMultiSample() const { return vsmMultiSample_; }
  317. /// Return whether shadow maps are reused.
  318. /// @property
  319. bool GetReuseShadowMaps() const { return reuseShadowMaps_; }
  320. /// Return maximum number of shadow maps per resolution.
  321. /// @property
  322. int GetMaxShadowMaps() const { return maxShadowMaps_; }
  323. /// Return whether dynamic instancing is in use.
  324. /// @property
  325. bool GetDynamicInstancing() const { return dynamicInstancing_; }
  326. /// Return number of extra instancing buffer elements.
  327. /// @property
  328. int GetNumExtraInstancingBufferElements() const { return numExtraInstancingBufferElements_; };
  329. /// Return minimum number of instances required in a batch group to render as instanced.
  330. /// @property
  331. int GetMinInstances() const { return minInstances_; }
  332. /// Return maximum number of sorted instances per batch group.
  333. /// @property
  334. int GetMaxSortedInstances() const { return maxSortedInstances_; }
  335. /// Return maximum number of occluder triangles.
  336. /// @property
  337. int GetMaxOccluderTriangles() const { return maxOccluderTriangles_; }
  338. /// Return occlusion buffer width.
  339. /// @property
  340. int GetOcclusionBufferSize() const { return occlusionBufferSize_; }
  341. /// Return occluder screen size threshold.
  342. /// @property
  343. float GetOccluderSizeThreshold() const { return occluderSizeThreshold_; }
  344. /// Return whether occlusion rendering is threaded.
  345. /// @property
  346. bool GetThreadedOcclusion() const { return threadedOcclusion_; }
  347. /// Return shadow depth bias multiplier for mobile platforms.
  348. /// @property
  349. float GetMobileShadowBiasMul() const { return mobileShadowBiasMul_; }
  350. /// Return shadow depth bias addition for mobile platforms.
  351. /// @property
  352. float GetMobileShadowBiasAdd() const { return mobileShadowBiasAdd_; }
  353. /// Return shadow normal offset multiplier for mobile platforms.
  354. /// @property
  355. float GetMobileNormalOffsetMul() const { return mobileNormalOffsetMul_; }
  356. /// Return number of views rendered.
  357. /// @property
  358. unsigned GetNumViews() const { return views_.Size(); }
  359. /// Return number of primitives rendered.
  360. /// @property
  361. unsigned GetNumPrimitives() const { return numPrimitives_; }
  362. /// Return number of batches rendered.
  363. /// @property
  364. unsigned GetNumBatches() const { return numBatches_; }
  365. /// Return number of geometries rendered.
  366. /// @property
  367. unsigned GetNumGeometries(bool allViews = false) const;
  368. /// Return number of lights rendered.
  369. /// @property
  370. unsigned GetNumLights(bool allViews = false) const;
  371. /// Return number of shadow maps rendered.
  372. /// @property
  373. unsigned GetNumShadowMaps(bool allViews = false) const;
  374. /// Return number of occluders rendered.
  375. /// @property
  376. unsigned GetNumOccluders(bool allViews = false) const;
  377. /// Return the default zone.
  378. /// @property
  379. Zone* GetDefaultZone() const { return defaultZone_; }
  380. /// Return the default material.
  381. /// @property
  382. Material* GetDefaultMaterial() const { return defaultMaterial_; }
  383. /// Return the default range attenuation texture.
  384. /// @property
  385. Texture2D* GetDefaultLightRamp() const { return defaultLightRamp_; }
  386. /// Return the default spotlight attenuation texture.
  387. /// @property
  388. Texture2D* GetDefaultLightSpot() const { return defaultLightSpot_; }
  389. /// Return the shadowed pointlight face selection cube map.
  390. TextureCube* GetFaceSelectCubeMap() const { return faceSelectCubeMap_; }
  391. /// Return the shadowed pointlight indirection cube map.
  392. TextureCube* GetIndirectionCubeMap() const { return indirectionCubeMap_; }
  393. /// Return the instancing vertex buffer.
  394. VertexBuffer* GetInstancingBuffer() const { return dynamicInstancing_ ? instancingBuffer_.Get() : nullptr; }
  395. /// Return the frame update parameters.
  396. const FrameInfo& GetFrameInfo() const { return frame_; }
  397. /// Update for rendering. Called by HandleRenderUpdate().
  398. void Update(float timeStep);
  399. /// Render. Called by Engine.
  400. void Render();
  401. /// Add debug geometry to the debug renderer.
  402. void DrawDebugGeometry(bool depthTest);
  403. /// Queue a render surface's viewports for rendering. Called by the surface, or by View.
  404. void QueueRenderSurface(RenderSurface* renderTarget);
  405. /// Queue a viewport for rendering. Null surface means backbuffer.
  406. void QueueViewport(RenderSurface* renderTarget, Viewport* viewport);
  407. /// Return volume geometry for a light.
  408. Geometry* GetLightGeometry(Light* light);
  409. /// Return quad geometry used in postprocessing.
  410. Geometry* GetQuadGeometry();
  411. /// Allocate a shadow map. If shadow map reuse is disabled, a different map is returned each time.
  412. Texture2D* GetShadowMap(Light* light, Camera* camera, unsigned viewWidth, unsigned viewHeight);
  413. /// Allocate a rendertarget or depth-stencil texture for deferred rendering or postprocessing. Should only be called during actual rendering, not before.
  414. Texture* GetScreenBuffer
  415. (int width, int height, unsigned format, int multiSample, bool autoResolve, bool cubemap, bool filtered, bool srgb, unsigned persistentKey = 0);
  416. /// Allocate a depth-stencil surface that does not need to be readable. Should only be called during actual rendering, not before.
  417. RenderSurface* GetDepthStencil(int width, int height, int multiSample, bool autoResolve);
  418. /// Allocate an occlusion buffer.
  419. OcclusionBuffer* GetOcclusionBuffer(Camera* camera);
  420. /// Allocate a temporary shadow camera and a scene node for it. Is thread-safe.
  421. Camera* GetShadowCamera();
  422. /// Mark a view as prepared by the specified culling camera.
  423. void StorePreparedView(View* view, Camera* camera);
  424. /// Return a prepared view if exists for the specified camera. Used to avoid duplicate view preparation CPU work.
  425. View* GetPreparedView(Camera* camera);
  426. /// Choose shaders for a forward rendering batch. The related batch queue is provided in case it has extra shader compilation defines.
  427. void SetBatchShaders(Batch& batch, Technique* tech, bool allowShadows, const BatchQueue& queue);
  428. /// Choose shaders for a deferred light volume batch.
  429. void SetLightVolumeBatchShaders
  430. (Batch& batch, Camera* camera, const String& vsName, const String& psName, const String& vsDefines, const String& psDefines);
  431. /// Set cull mode while taking possible projection flipping into account.
  432. void SetCullMode(CullMode mode, Camera* camera);
  433. /// Ensure sufficient size of the instancing vertex buffer. Return true if successful.
  434. bool ResizeInstancingBuffer(unsigned numInstances);
  435. /// Optimize a light by scissor rectangle.
  436. void OptimizeLightByScissor(Light* light, Camera* camera);
  437. /// Optimize a light by marking it to the stencil buffer and setting a stencil test.
  438. void OptimizeLightByStencil(Light* light, Camera* camera);
  439. /// Return a scissor rectangle for a light.
  440. const Rect& GetLightScissor(Light* light, Camera* camera);
  441. /// Return a view or its source view if it uses one. Used internally for render statistics.
  442. static View* GetActualView(View* view);
  443. private:
  444. /// Initialize when screen mode initially set.
  445. void Initialize();
  446. /// Reload shaders.
  447. void LoadShaders();
  448. /// Reload shaders for a material pass. The related batch queue is provided in case it has extra shader compilation defines.
  449. void LoadPassShaders(Pass* pass, Vector<SharedPtr<ShaderVariation> >& vertexShaders, Vector<SharedPtr<ShaderVariation> >& pixelShaders, const BatchQueue& queue);
  450. /// Release shaders used in materials.
  451. void ReleaseMaterialShaders();
  452. /// Reload textures.
  453. void ReloadTextures();
  454. /// Create light volume geometries.
  455. void CreateGeometries();
  456. /// Create instancing vertex buffer.
  457. void CreateInstancingBuffer();
  458. /// Create point light shadow indirection texture data.
  459. void SetIndirectionTextureData();
  460. /// Update a queued viewport for rendering.
  461. void UpdateQueuedViewport(unsigned index);
  462. /// Prepare for rendering of a new view.
  463. void PrepareViewRender();
  464. /// Remove unused occlusion and screen buffers.
  465. void RemoveUnusedBuffers();
  466. /// Reset shadow map allocation counts.
  467. void ResetShadowMapAllocations();
  468. /// Reset screem buffer allocation counts.
  469. void ResetScreenBufferAllocations();
  470. /// Remove all shadow maps. Called when global shadow map resolution or format is changed.
  471. void ResetShadowMaps();
  472. /// Remove all occlusion and screen buffers.
  473. void ResetBuffers();
  474. /// Find variations for shadow shaders.
  475. String GetShadowVariations() const;
  476. /// Handle screen mode event.
  477. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  478. /// Handle render update event.
  479. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  480. /// Blur the shadow map.
  481. void BlurShadowMap(View* view, Texture2D* shadowMap, float blurScale);
  482. /// Graphics subsystem.
  483. WeakPtr<Graphics> graphics_;
  484. /// Default renderpath.
  485. SharedPtr<RenderPath> defaultRenderPath_;
  486. /// Default non-textured material technique.
  487. SharedPtr<Technique> defaultTechnique_;
  488. /// Default zone.
  489. SharedPtr<Zone> defaultZone_;
  490. /// Directional light quad geometry.
  491. SharedPtr<Geometry> dirLightGeometry_;
  492. /// Spot light volume geometry.
  493. SharedPtr<Geometry> spotLightGeometry_;
  494. /// Point light volume geometry.
  495. SharedPtr<Geometry> pointLightGeometry_;
  496. /// Instance stream vertex buffer.
  497. SharedPtr<VertexBuffer> instancingBuffer_;
  498. /// Default material.
  499. SharedPtr<Material> defaultMaterial_;
  500. /// Default range attenuation texture.
  501. SharedPtr<Texture2D> defaultLightRamp_;
  502. /// Default spotlight attenuation texture.
  503. SharedPtr<Texture2D> defaultLightSpot_;
  504. /// Face selection cube map for shadowed pointlights.
  505. SharedPtr<TextureCube> faceSelectCubeMap_;
  506. /// Indirection cube map for shadowed pointlights.
  507. SharedPtr<TextureCube> indirectionCubeMap_;
  508. /// Reusable scene nodes with shadow camera components.
  509. Vector<SharedPtr<Node> > shadowCameraNodes_;
  510. /// Reusable occlusion buffers.
  511. Vector<SharedPtr<OcclusionBuffer> > occlusionBuffers_;
  512. /// Shadow maps by resolution.
  513. HashMap<int, Vector<SharedPtr<Texture2D> > > shadowMaps_;
  514. /// Shadow map dummy color buffers by resolution.
  515. HashMap<int, SharedPtr<Texture2D> > colorShadowMaps_;
  516. /// Shadow map allocations by resolution.
  517. HashMap<int, PODVector<Light*> > shadowMapAllocations_;
  518. /// Instance of shadow map filter.
  519. Object* shadowMapFilterInstance_{};
  520. /// Function pointer of shadow map filter.
  521. ShadowMapFilter shadowMapFilter_{};
  522. /// Screen buffers by resolution and format.
  523. HashMap<unsigned long long, Vector<SharedPtr<Texture> > > screenBuffers_;
  524. /// Current screen buffer allocations by resolution and format.
  525. HashMap<unsigned long long, unsigned> screenBufferAllocations_;
  526. /// Cache for light scissor queries.
  527. HashMap<Pair<Light*, Camera*>, Rect> lightScissorCache_;
  528. /// Backbuffer viewports.
  529. Vector<SharedPtr<Viewport> > viewports_;
  530. /// Render surface viewports queued for update.
  531. Vector<Pair<WeakPtr<RenderSurface>, WeakPtr<Viewport> > > queuedViewports_;
  532. /// Views that have been processed this frame.
  533. Vector<WeakPtr<View> > views_;
  534. /// Prepared views by culling camera.
  535. HashMap<Camera*, WeakPtr<View> > preparedViews_;
  536. /// Octrees that have been updated during the frame.
  537. HashSet<Octree*> updatedOctrees_;
  538. /// Techniques for which missing shader error has been displayed.
  539. HashSet<Technique*> shaderErrorDisplayed_;
  540. /// Mutex for shadow camera allocation.
  541. Mutex rendererMutex_;
  542. /// Current variation names for deferred light volume shaders.
  543. Vector<String> deferredLightPSVariations_;
  544. /// Frame info for rendering.
  545. FrameInfo frame_;
  546. /// Texture anisotropy level.
  547. int textureAnisotropy_{4};
  548. /// Texture filtering mode.
  549. TextureFilterMode textureFilterMode_{FILTER_TRILINEAR};
  550. /// Texture quality level.
  551. MaterialQuality textureQuality_{QUALITY_HIGH};
  552. /// Material quality level.
  553. MaterialQuality materialQuality_{QUALITY_HIGH};
  554. /// Shadow map resolution.
  555. int shadowMapSize_{1024};
  556. /// Shadow quality.
  557. ShadowQuality shadowQuality_{SHADOWQUALITY_PCF_16BIT};
  558. /// Shadow softness, only works when SHADOWQUALITY_BLUR_VSM is used.
  559. float shadowSoftness_{1.0f};
  560. /// Shadow parameters when VSM is used, they help to reduce light bleeding.
  561. Vector2 vsmShadowParams_{0.0000001f, 0.9f};
  562. /// Multisample level for VSM shadows.
  563. int vsmMultiSample_{1};
  564. /// Maximum number of shadow maps per resolution.
  565. int maxShadowMaps_{1};
  566. /// Minimum number of instances required in a batch group to render as instanced.
  567. int minInstances_{2};
  568. /// Maximum sorted instances per batch group.
  569. int maxSortedInstances_{1000};
  570. /// Maximum occluder triangles.
  571. int maxOccluderTriangles_{5000};
  572. /// Occlusion buffer width.
  573. int occlusionBufferSize_{256};
  574. /// Occluder screen size threshold.
  575. float occluderSizeThreshold_{0.025f};
  576. /// Mobile platform shadow depth bias multiplier.
  577. float mobileShadowBiasMul_{1.0f};
  578. /// Mobile platform shadow depth bias addition.
  579. float mobileShadowBiasAdd_{};
  580. /// Mobile platform shadow normal offset multiplier.
  581. float mobileNormalOffsetMul_{1.0f};
  582. /// Number of occlusion buffers in use.
  583. unsigned numOcclusionBuffers_{};
  584. /// Number of temporary shadow cameras in use.
  585. unsigned numShadowCameras_{};
  586. /// Number of primitives (3D geometry only).
  587. unsigned numPrimitives_{};
  588. /// Number of batches (3D geometry only).
  589. unsigned numBatches_{};
  590. /// Frame number on which shaders last changed.
  591. unsigned shadersChangedFrameNumber_{M_MAX_UNSIGNED};
  592. /// Current stencil value for light optimization.
  593. unsigned char lightStencilValue_{};
  594. /// HDR rendering flag.
  595. bool hdrRendering_{};
  596. /// Specular lighting flag.
  597. bool specularLighting_{true};
  598. /// Draw shadows flag.
  599. bool drawShadows_{true};
  600. /// Shadow map reuse flag.
  601. bool reuseShadowMaps_{true};
  602. /// Dynamic instancing flag.
  603. bool dynamicInstancing_{true};
  604. /// Number of extra instancing data elements.
  605. int numExtraInstancingBufferElements_{};
  606. /// Threaded occlusion rendering flag.
  607. bool threadedOcclusion_{};
  608. /// Shaders need reloading flag.
  609. bool shadersDirty_{true};
  610. /// Initialized flag.
  611. bool initialized_{};
  612. /// Flag for views needing reset.
  613. bool resetViews_{};
  614. };
  615. }