Renderer.h 16 KB

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