Graphics.h 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/ArrayPtr.h"
  5. #include "../Container/HashSet.h"
  6. #include "../Core/Mutex.h"
  7. #include "../Core/Object.h"
  8. #include "../GraphicsAPI/GraphicsDefs.h"
  9. #include "../GraphicsAPI/ShaderVariation.h"
  10. #include "../Math/Color.h"
  11. #include "../Math/Plane.h"
  12. #include "../Math/Rect.h"
  13. #include "../Resource/Image.h"
  14. struct SDL_Window;
  15. namespace Urho3D
  16. {
  17. class ConstantBuffer;
  18. class File;
  19. class Image;
  20. class IndexBuffer;
  21. class GPUObject;
  22. class RenderSurface;
  23. class Shader;
  24. class ShaderPrecache;
  25. class ShaderVariation;
  26. class Texture;
  27. class Texture2D;
  28. class Texture2DArray;
  29. class TextureCube;
  30. class Vector3;
  31. class Vector4;
  32. class VertexBuffer;
  33. #ifdef URHO3D_OPENGL
  34. class GraphicsImpl_OGL;
  35. #endif
  36. #ifdef URHO3D_D3D11
  37. class GraphicsImpl_D3D11;
  38. #endif
  39. struct ShaderParameter;
  40. #ifdef URHO3D_OPENGL
  41. // Note: ShaderProgram_OGL class is purposefully API-specific. It should not be used by Urho3D client applications.
  42. class ShaderProgram_OGL;
  43. #endif
  44. #ifdef URHO3D_D3D11
  45. // Note: ShaderProgram_D3D11 class is purposefully API-specific. It should not be used by Urho3D client applications.
  46. class ShaderProgram_D3D11;
  47. #endif
  48. /// CPU-side scratch buffer for vertex data updates.
  49. struct ScratchBuffer
  50. {
  51. ScratchBuffer() :
  52. size_(0),
  53. reserved_(false)
  54. {
  55. }
  56. /// Buffer data.
  57. SharedArrayPtr<u8> data_;
  58. /// Data size.
  59. i32 size_;
  60. /// Reserved flag.
  61. bool reserved_;
  62. };
  63. /// Screen mode parameters.
  64. struct ScreenModeParams
  65. {
  66. /// Whether to use fullscreen mode.
  67. bool fullscreen_{};
  68. /// Whether to hide window borders. Window is always borderless in fullscreen.
  69. bool borderless_{};
  70. /// Whether the window is resizable.
  71. bool resizable_{};
  72. /// Whether the high DPI is enabled.
  73. /// TODO: Explain what exactly it means.
  74. bool highDPI_{};
  75. /// Whether the vertical synchronization is used.
  76. bool vsync_{};
  77. /// Whether the triple bufferization is used.
  78. bool tripleBuffer_{};
  79. /// Level of multisampling.
  80. int multiSample_{ 1 };
  81. /// Monitor for fullscreen mode. Has no effect in windowed mode.
  82. int monitor_{};
  83. /// Refresh rate. 0 to pick automatically.
  84. int refreshRate_{};
  85. /// Compare contents except vsync flag.
  86. bool EqualsExceptVSync(const ScreenModeParams& rhs) const
  87. {
  88. return fullscreen_ == rhs.fullscreen_
  89. && borderless_ == rhs.borderless_
  90. && resizable_ == rhs.resizable_
  91. && highDPI_ == rhs.highDPI_
  92. // && vsync_ == rhs.vsync_
  93. && tripleBuffer_ == rhs.tripleBuffer_
  94. && multiSample_ == rhs.multiSample_
  95. && monitor_ == rhs.monitor_
  96. && refreshRate_ == rhs.refreshRate_;
  97. }
  98. /// Compare for equality with another parameter set.
  99. bool operator ==(const ScreenModeParams& rhs) const
  100. {
  101. return vsync_ == rhs.vsync_ && EqualsExceptVSync(rhs);
  102. }
  103. /// Compare for inequality with another parameter set.
  104. bool operator !=(const ScreenModeParams& rhs) const { return !(*this == rhs); }
  105. };
  106. /// Window mode parameters.
  107. struct WindowModeParams
  108. {
  109. /// Width of the window. 0 to pick automatically.
  110. int width_{};
  111. /// Height of the window. 0 to pick automatically.
  112. int height_{};
  113. /// Screen mode parameters.
  114. ScreenModeParams screenParams_;
  115. };
  116. /// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
  117. class URHO3D_API Graphics : public Object
  118. {
  119. URHO3D_OBJECT(Graphics, Object);
  120. public:
  121. /// Construct.
  122. explicit Graphics(Context* context, GAPI gapi);
  123. /// Destruct. Release the Direct3D11 device and close the window.
  124. ~Graphics() override;
  125. /// Set external window handle. Only effective before setting the initial screen mode.
  126. void SetExternalWindow(void* window);
  127. /// Set window title.
  128. /// @property
  129. void SetWindowTitle(const String& windowTitle);
  130. /// Set window icon.
  131. /// @property
  132. void SetWindowIcon(Image* windowIcon);
  133. /// Set window position. Sets initial position if window is not created yet.
  134. /// @property
  135. void SetWindowPosition(const IntVector2& position);
  136. /// Set window position. Sets initial position if window is not created yet.
  137. void SetWindowPosition(int x, int y);
  138. /// Set screen mode. Return true if successful.
  139. /// Don't use SetScreenMode if ToggleFullscreen is used directly or indirectly.
  140. bool SetScreenMode(int width, int height, const ScreenModeParams& params, bool maximize = false);
  141. /// Set screen resolution only. Return true if successful.
  142. /// Don't use SetScreenMode if ToggleFullscreen is used directly or indirectly.
  143. bool SetScreenMode(int width, int height);
  144. /// Set window modes to be rotated by ToggleFullscreen. Apply primary window settings immeditally.
  145. /// Window may be maximized if requested and possible. Return true if successful.
  146. bool SetWindowModes(const WindowModeParams& windowMode, const WindowModeParams& secondaryWindowMode, bool maximize = false);
  147. /// Set default window modes. Return true if successful.
  148. bool SetDefaultWindowModes(int width, int height, const ScreenModeParams& params);
  149. /// Set default window modes. Deprecated. Return true if successful.
  150. bool SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable,
  151. bool highDPI, bool vsync, bool tripleBuffer, int multiSample, int monitor, int refreshRate);
  152. /// Set screen resolution only. Deprecated. Return true if successful.
  153. bool SetMode(int width, int height);
  154. /// Set whether the main window uses sRGB conversion on write.
  155. /// @property
  156. void SetSRGB(bool enable);
  157. /// Set whether rendering output is dithered. Default true on OpenGL. No effect on Direct3D.
  158. /// @property
  159. void SetDither(bool enable);
  160. /// Set whether to flush the GPU command buffer to prevent multiple frames being queued and uneven frame timesteps. Default off, may decrease performance if enabled. Not currently implemented on OpenGL.
  161. /// @property
  162. void SetFlushGPU(bool enable);
  163. /// Set forced use of OpenGL 2 even if OpenGL 3 is available. Must be called before setting the screen mode for the first time. Default false. No effect on Direct3D9 & 11.
  164. void SetForceGL2(bool enable);
  165. /// Set allowed screen orientations as a space-separated list of "LandscapeLeft", "LandscapeRight", "Portrait" and "PortraitUpsideDown". Affects currently only iOS platform.
  166. /// @property
  167. void SetOrientations(const String& orientations);
  168. /// Toggle between full screen and windowed mode. Return true if successful.
  169. bool ToggleFullscreen();
  170. /// Close the window.
  171. void Close();
  172. /// Take a screenshot. Return true if successful.
  173. bool TakeScreenShot(Image& destImage);
  174. /// Begin frame rendering. Return true if device available and can render.
  175. bool BeginFrame();
  176. /// End frame rendering and swap buffers.
  177. void EndFrame();
  178. /// Clear any or all of rendertarget, depth buffer and stencil buffer.
  179. void Clear(ClearTargetFlags flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, u32 stencil = 0);
  180. /// Resolve multisampled backbuffer to a texture rendertarget. The texture's size should match the viewport size.
  181. bool ResolveToTexture(Texture2D* destination, const IntRect& viewport);
  182. /// Resolve a multisampled texture on itself.
  183. bool ResolveToTexture(Texture2D* texture);
  184. /// Resolve a multisampled cube texture on itself.
  185. bool ResolveToTexture(TextureCube* texture);
  186. /// Draw non-indexed geometry.
  187. void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
  188. /// Draw indexed geometry.
  189. void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
  190. /// Draw indexed geometry with vertex index offset.
  191. void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount);
  192. /// Draw indexed, instanced geometry. An instancing vertex buffer must be set.
  193. void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
  194. unsigned instanceCount);
  195. /// Draw indexed, instanced geometry with vertex index offset.
  196. void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex,
  197. unsigned vertexCount, unsigned instanceCount);
  198. /// Set vertex buffer.
  199. void SetVertexBuffer(VertexBuffer* buffer);
  200. /// Set multiple vertex buffers.
  201. /// @nobind
  202. bool SetVertexBuffers(const Vector<VertexBuffer*>& buffers, unsigned instanceOffset = 0);
  203. /// Set multiple vertex buffers.
  204. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer>>& buffers, unsigned instanceOffset = 0);
  205. /// Set index buffer.
  206. void SetIndexBuffer(IndexBuffer* buffer);
  207. /// Set shaders.
  208. void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
  209. /// Set shader float constants.
  210. void SetShaderParameter(StringHash param, const float* data, unsigned count);
  211. /// Set shader float constant.
  212. void SetShaderParameter(StringHash param, float value);
  213. /// Set shader integer constant.
  214. void SetShaderParameter(StringHash param, int value);
  215. /// Set shader boolean constant.
  216. void SetShaderParameter(StringHash param, bool value);
  217. /// Set shader color constant.
  218. void SetShaderParameter(StringHash param, const Color& color);
  219. /// Set shader 2D vector constant.
  220. void SetShaderParameter(StringHash param, const Vector2& vector);
  221. /// Set shader 3x3 matrix constant.
  222. void SetShaderParameter(StringHash param, const Matrix3& matrix);
  223. /// Set shader 3D vector constant.
  224. void SetShaderParameter(StringHash param, const Vector3& vector);
  225. /// Set shader 4x4 matrix constant.
  226. void SetShaderParameter(StringHash param, const Matrix4& matrix);
  227. /// Set shader 4D vector constant.
  228. void SetShaderParameter(StringHash param, const Vector4& vector);
  229. /// Set shader 3x4 matrix constant.
  230. void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
  231. /// Set shader constant from a variant. Supported variant types: bool, float, vector2, vector3, vector4, color.
  232. void SetShaderParameter(StringHash param, const Variant& value);
  233. /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
  234. bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
  235. /// Check whether a shader parameter exists on the currently set shaders.
  236. bool HasShaderParameter(StringHash param);
  237. /// Check whether the current vertex or pixel shader uses a texture unit.
  238. bool HasTextureUnit(TextureUnit unit);
  239. /// Clear remembered shader parameter source group.
  240. void ClearParameterSource(ShaderParameterGroup group);
  241. /// Clear remembered shader parameter sources.
  242. void ClearParameterSources();
  243. /// Clear remembered transform shader parameter sources.
  244. void ClearTransformSources();
  245. /// Set texture.
  246. void SetTexture(unsigned index, Texture* texture);
  247. /// Set default texture filtering mode. Called by Renderer before rendering.
  248. void SetDefaultTextureFilterMode(TextureFilterMode mode);
  249. /// Set default texture anisotropy level. Called by Renderer before rendering.
  250. void SetDefaultTextureAnisotropy(unsigned level);
  251. /// Reset all rendertargets, depth-stencil surface and viewport.
  252. void ResetRenderTargets();
  253. /// Reset specific rendertarget.
  254. void ResetRenderTarget(unsigned index);
  255. /// Reset depth-stencil surface.
  256. void ResetDepthStencil();
  257. /// Set rendertarget.
  258. void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
  259. /// Set rendertarget.
  260. void SetRenderTarget(unsigned index, Texture2D* texture);
  261. /// Set depth-stencil surface.
  262. void SetDepthStencil(RenderSurface* depthStencil);
  263. /// Set depth-stencil surface.
  264. void SetDepthStencil(Texture2D* texture);
  265. /// Set viewport.
  266. void SetViewport(const IntRect& rect);
  267. /// Set blending and alpha-to-coverage modes. Alpha-to-coverage is not supported on Direct3D9.
  268. void SetBlendMode(BlendMode mode, bool alphaToCoverage = false);
  269. /// Set color write on/off.
  270. void SetColorWrite(bool enable);
  271. /// Set hardware culling mode.
  272. void SetCullMode(CullMode mode);
  273. /// Set depth bias.
  274. void SetDepthBias(float constantBias, float slopeScaledBias);
  275. /// Set depth compare.
  276. void SetDepthTest(CompareMode mode);
  277. /// Set depth write on/off.
  278. void SetDepthWrite(bool enable);
  279. /// Set polygon fill mode.
  280. void SetFillMode(FillMode mode);
  281. /// Set line antialiasing on/off.
  282. void SetLineAntiAlias(bool enable);
  283. /// Set scissor test.
  284. void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
  285. /// Set scissor test.
  286. void SetScissorTest(bool enable, const IntRect& rect);
  287. /// Set stencil test.
  288. void SetStencilTest
  289. (bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP,
  290. u32 stencilRef = 0, u32 compareMask = M_U32_MASK_ALL_BITS, u32 writeMask = M_U32_MASK_ALL_BITS);
  291. /// Set a custom clipping plane. The plane is specified in world space, but is dependent on the view and projection matrices.
  292. void SetClipPlane(bool enable, const Plane& clipPlane = Plane::UP, const Matrix3x4& view = Matrix3x4::IDENTITY,
  293. const Matrix4& projection = Matrix4::IDENTITY);
  294. /// Begin dumping shader variation names to an XML file for precaching.
  295. void BeginDumpShaders(const String& fileName);
  296. /// End dumping shader variations names.
  297. void EndDumpShaders();
  298. /// Precache shader variations from an XML file generated with BeginDumpShaders().
  299. void PrecacheShaders(Deserializer& source);
  300. /// Set shader cache directory, Direct3D only. This can either be an absolute path or a path within the resource system.
  301. /// @property
  302. void SetShaderCacheDir(const String& path);
  303. /// Return whether rendering initialized.
  304. /// @property
  305. bool IsInitialized() const;
  306. #ifdef URHO3D_OPENGL
  307. /// Return graphics implementation, which holds the actual API-specific resources.
  308. GraphicsImpl_OGL* GetImpl_OGL() const
  309. {
  310. assert(Graphics::GetGAPI() == GAPI_OPENGL);
  311. return static_cast<GraphicsImpl_OGL*>(impl_);
  312. }
  313. #endif
  314. #ifdef URHO3D_D3D11
  315. /// Return graphics implementation, which holds the actual API-specific resources.
  316. GraphicsImpl_D3D11* GetImpl_D3D11() const
  317. {
  318. assert(Graphics::GetGAPI() == GAPI_D3D11);
  319. return static_cast<GraphicsImpl_D3D11*>(impl_);
  320. }
  321. #endif
  322. /// Return OS-specific external window handle. Null if not in use.
  323. void* GetExternalWindow() const { return externalWindow_; }
  324. /// Return SDL window.
  325. SDL_Window* GetWindow() const { return window_; }
  326. /// Return window title.
  327. /// @property
  328. const String& GetWindowTitle() const { return windowTitle_; }
  329. /// Return graphics API name.
  330. /// @property
  331. const String& GetApiName() const { return apiName_; }
  332. /// Return window position.
  333. /// @property
  334. IntVector2 GetWindowPosition() const;
  335. /// Return window width in pixels.
  336. /// @property
  337. int GetWidth() const { return width_; }
  338. /// Return window height in pixels.
  339. /// @property
  340. int GetHeight() const { return height_; }
  341. /// Return screen mode parameters.
  342. const ScreenModeParams& GetScreenModeParams() const { return screenParams_; }
  343. /// Return multisample mode (1 = no multisampling).
  344. /// @property
  345. int GetMultiSample() const { return screenParams_.multiSample_; }
  346. /// Return window size in pixels.
  347. /// @property
  348. IntVector2 GetSize() const { return IntVector2(width_, height_); }
  349. /// Return whether window is fullscreen.
  350. /// @property
  351. bool GetFullscreen() const { return screenParams_.fullscreen_; }
  352. /// Return whether window is borderless.
  353. /// @property
  354. bool GetBorderless() const { return screenParams_.borderless_; }
  355. /// Return whether window is resizable.
  356. /// @property
  357. bool GetResizable() const { return screenParams_.resizable_; }
  358. /// Return whether window is high DPI.
  359. bool GetHighDPI() const { return screenParams_.highDPI_; }
  360. /// Return whether vertical sync is on.
  361. /// @property
  362. bool GetVSync() const { return screenParams_.vsync_; }
  363. /// Return refresh rate when using vsync in fullscreen
  364. int GetRefreshRate() const { return screenParams_.refreshRate_; }
  365. /// Return the current monitor index. Effective on in fullscreen
  366. int GetMonitor() const { return screenParams_.monitor_; }
  367. /// Return whether triple buffering is enabled.
  368. /// @property
  369. bool GetTripleBuffer() const { return screenParams_.tripleBuffer_; }
  370. /// Return whether the main window is using sRGB conversion on write.
  371. /// @property
  372. bool GetSRGB() const { return sRGB_; }
  373. /// Return whether rendering output is dithered. Always false on Direct3D.
  374. /// @property
  375. bool GetDither() const;
  376. /// Return whether the GPU command buffer is flushed each frame.
  377. /// @property
  378. bool GetFlushGPU() const { return flushGPU_; }
  379. /// Return whether OpenGL 2 use is forced. Effective only on OpenGL.
  380. bool GetForceGL2() const { return forceGL2_; }
  381. /// Return allowed screen orientations.
  382. /// @property
  383. const String& GetOrientations() const { return orientations_; }
  384. /// Return whether graphics context is lost and can not render or load GPU resources.
  385. /// @property
  386. bool IsDeviceLost() const;
  387. /// Return number of primitives drawn this frame.
  388. /// @property
  389. unsigned GetNumPrimitives() const { return numPrimitives_; }
  390. /// Return number of batches drawn this frame.
  391. /// @property
  392. unsigned GetNumBatches() const { return numBatches_; }
  393. /// Return dummy color texture format for shadow maps. Is "NULL" (consume no video memory) if supported.
  394. unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
  395. /// Return shadow map depth texture format, or 0 if not supported.
  396. unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
  397. /// Return 24-bit shadow map depth texture format, or 0 if not supported.
  398. unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
  399. /// Return whether hardware instancing is supported.
  400. /// @property
  401. bool GetInstancingSupport() const { return instancingSupport_; }
  402. /// Return whether light pre-pass rendering is supported.
  403. /// @property
  404. bool GetLightPrepassSupport() const { return lightPrepassSupport_; }
  405. /// Return whether deferred rendering is supported.
  406. /// @property
  407. bool GetDeferredSupport() const { return deferredSupport_; }
  408. /// Return whether anisotropic texture filtering is supported.
  409. bool GetAnisotropySupport() const { return anisotropySupport_; }
  410. /// Return whether shadow map depth compare is done in hardware.
  411. /// @property
  412. bool GetHardwareShadowSupport() const { return hardwareShadowSupport_; }
  413. /// Return whether a readable hardware depth format is available.
  414. /// @property
  415. bool GetReadableDepthSupport() const { return GetReadableDepthFormat() != 0; }
  416. /// Return whether sRGB conversion on texture sampling is supported.
  417. /// @property
  418. bool GetSRGBSupport() const { return sRGBSupport_; }
  419. /// Return whether sRGB conversion on rendertarget writing is supported.
  420. /// @property
  421. bool GetSRGBWriteSupport() const { return sRGBWriteSupport_; }
  422. /// Return supported fullscreen resolutions (third component is refreshRate). Will be empty if listing the resolutions is not supported on the platform (e.g. Web).
  423. /// @property
  424. Vector<IntVector3> GetResolutions(int monitor) const;
  425. /// Return index of the best resolution for requested width, height and refresh rate.
  426. i32 FindBestResolutionIndex(int monitor, int width, int height, int refreshRate) const;
  427. /// Return supported multisampling levels.
  428. /// @property
  429. Vector<int> GetMultiSampleLevels() const;
  430. /// Return the desktop resolution.
  431. /// @property
  432. IntVector2 GetDesktopResolution(int monitor) const;
  433. /// Return the number of currently connected monitors.
  434. /// @property
  435. int GetMonitorCount() const;
  436. /// Returns the index of the display containing the center of the window on success or a negative error code on failure.
  437. /// @property
  438. int GetCurrentMonitor() const;
  439. /// Returns true if window is maximized or runs in full screen mode.
  440. /// @property
  441. bool GetMaximized() const;
  442. /// Return display dpi information: (hdpi, vdpi, ddpi). On failure returns zero vector.
  443. /// @property
  444. Vector3 GetDisplayDPI(int monitor=0) const;
  445. /// Return hardware format for a compressed image format, or 0 if unsupported.
  446. unsigned GetFormat(CompressedFormat format) const;
  447. /// Return a shader variation by name and defines.
  448. ShaderVariation* GetShader(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
  449. /// Return a shader variation by name and defines.
  450. ShaderVariation* GetShader(ShaderType type, const char* name, const char* defines) const;
  451. /// Return current vertex buffer by index.
  452. VertexBuffer* GetVertexBuffer(unsigned index) const;
  453. /// Return current index buffer.
  454. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  455. /// Return current vertex shader.
  456. ShaderVariation* GetVertexShader() const { return vertexShader_; }
  457. /// Return current pixel shader.
  458. ShaderVariation* GetPixelShader() const { return pixelShader_; }
  459. #ifdef URHO3D_OPENGL
  460. // Note: ShaderProgram_OGL class is purposefully API-specific. It should not be used by Urho3D client applications.
  461. /// Return shader program. This is an API-specific class and should not be used by applications.
  462. ShaderProgram_OGL* GetShaderProgram_OGL() const;
  463. /// Clean up shader parameters when a shader variation is released or destroyed.
  464. void CleanupShaderPrograms_OGL(ShaderVariation* variation);
  465. #endif
  466. #ifdef URHO3D_D3D11
  467. // Note: ShaderProgram_D3D11 class is purposefully API-specific. It should not be used by Urho3D client applications.
  468. /// Return shader program. This is an API-specific class and should not be used by applications.
  469. ShaderProgram_D3D11* GetShaderProgram_D3D11() const;
  470. /// Clean up shader parameters when a shader variation is released or destroyed.
  471. void CleanupShaderPrograms_D3D11(ShaderVariation* variation);
  472. #endif
  473. /// Return texture unit index by name.
  474. TextureUnit GetTextureUnit(const String& name);
  475. /// Return texture unit name by index.
  476. const String& GetTextureUnitName(TextureUnit unit);
  477. /// Return current texture by texture unit index.
  478. Texture* GetTexture(unsigned index) const;
  479. /// Return default texture filtering mode.
  480. TextureFilterMode GetDefaultTextureFilterMode() const { return defaultTextureFilterMode_; }
  481. /// Return default texture max. anisotropy level.
  482. unsigned GetDefaultTextureAnisotropy() const { return defaultTextureAnisotropy_; }
  483. /// Return current rendertarget by index.
  484. RenderSurface* GetRenderTarget(unsigned index) const;
  485. /// Return current depth-stencil surface.
  486. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  487. /// Return the viewport coordinates.
  488. IntRect GetViewport() const { return viewport_; }
  489. /// Return blending mode.
  490. BlendMode GetBlendMode() const { return blendMode_; }
  491. /// Return whether alpha-to-coverage is enabled.
  492. bool GetAlphaToCoverage() const { return alphaToCoverage_; }
  493. /// Return whether color write is enabled.
  494. bool GetColorWrite() const { return colorWrite_; }
  495. /// Return hardware culling mode.
  496. CullMode GetCullMode() const { return cullMode_; }
  497. /// Return depth constant bias.
  498. float GetDepthConstantBias() const { return constantDepthBias_; }
  499. /// Return depth slope scaled bias.
  500. float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
  501. /// Return depth compare mode.
  502. CompareMode GetDepthTest() const { return depthTestMode_; }
  503. /// Return whether depth write is enabled.
  504. bool GetDepthWrite() const { return depthWrite_; }
  505. /// Return polygon fill mode.
  506. FillMode GetFillMode() const { return fillMode_; }
  507. /// Return whether line antialiasing is enabled.
  508. bool GetLineAntiAlias() const { return lineAntiAlias_; }
  509. /// Return whether stencil test is enabled.
  510. bool GetStencilTest() const { return stencilTest_; }
  511. /// Return whether scissor test is enabled.
  512. bool GetScissorTest() const { return scissorTest_; }
  513. /// Return scissor rectangle coordinates.
  514. const IntRect& GetScissorRect() const { return scissorRect_; }
  515. /// Return stencil compare mode.
  516. CompareMode GetStencilTestMode() const { return stencilTestMode_; }
  517. /// Return stencil operation to do if stencil test passes.
  518. StencilOp GetStencilPass() const { return stencilPass_; }
  519. /// Return stencil operation to do if stencil test fails.
  520. StencilOp GetStencilFail() const { return stencilFail_; }
  521. /// Return stencil operation to do if depth compare fails.
  522. StencilOp GetStencilZFail() const { return stencilZFail_; }
  523. /// Return stencil reference value.
  524. u32 GetStencilRef() const { return stencilRef_; }
  525. /// Return stencil compare bitmask.
  526. u32 GetStencilCompareMask() const { return stencilCompareMask_; }
  527. /// Return stencil write bitmask.
  528. u32 GetStencilWriteMask() const { return stencilWriteMask_; }
  529. /// Return whether a custom clipping plane is in use.
  530. bool GetUseClipPlane() const { return useClipPlane_; }
  531. /// Return shader cache directory, Direct3D only.
  532. /// @property
  533. const String& GetShaderCacheDir() const { return shaderCacheDir_; }
  534. /// Return current rendertarget width and height.
  535. IntVector2 GetRenderTargetDimensions() const;
  536. /// Window was resized through user interaction. Called by Input subsystem.
  537. void OnWindowResized();
  538. /// Window was moved through user interaction. Called by Input subsystem.
  539. void OnWindowMoved();
  540. #ifdef URHO3D_OPENGL
  541. /// Restore GPU objects and reinitialize state. Requires an open window. Used only on OpenGL.
  542. void Restore_OGL();
  543. /// Mark the FBO needing an update. Used only on OpenGL.
  544. void MarkFBODirty_OGL();
  545. /// Bind a VBO, avoiding redundant operation. Used only on OpenGL.
  546. void SetVBO_OGL(unsigned object);
  547. /// Bind a UBO, avoiding redundant operation. Used only on OpenGL.
  548. void SetUBO_OGL(unsigned object);
  549. /// Clean up a render surface from all FBOs. Used only on OpenGL.
  550. void CleanupRenderSurface_OGL(RenderSurface* surface);
  551. /// Bind texture unit 0 for update. Called by Texture. Used only on OpenGL.
  552. void SetTextureForUpdate_OGL(Texture* texture);
  553. /// Get Renderer name. Used on OpenGL
  554. const String& GetRendererName() const { return rendererName_; }
  555. /// Get Version string. Used on OpenGL
  556. const String& GetVersionString() const { return versionString_; }
  557. #endif // def URHO3D_OPENGL
  558. /// Maximize the window.
  559. void Maximize();
  560. /// Minimize the window.
  561. void Minimize();
  562. /// Raises window if it was minimized.
  563. void Raise() const;
  564. /// Add a GPU object to keep track of. Called by GPUObject.
  565. void AddGPUObject(GPUObject* object);
  566. /// Remove a GPU object. Called by GPUObject.
  567. void RemoveGPUObject(GPUObject* object);
  568. /// Reserve a CPU-side scratch buffer.
  569. void* ReserveScratchBuffer(i32 size);
  570. /// Free a CPU-side scratch buffer.
  571. void FreeScratchBuffer(void* buffer);
  572. /// Clean up too large scratch buffers.
  573. void CleanupScratchBuffers();
  574. /// Get or create a constant buffer. Will be shared between shaders if possible. Used only on OpenGL and DirectX 11.
  575. /// @nobind
  576. ConstantBuffer* GetOrCreateConstantBuffer(ShaderType type, unsigned index, unsigned size);
  577. /// Get used graphics API.
  578. static GAPI GetGAPI() { return gapi; }
  579. /// Set GAP manually. Used for headless mode only https://github.com/urho3d/Urho3D/issues/3040
  580. static GAPI SetGAPI(GAPI value) { return gapi = value; }
  581. /// Return the API-specific alpha texture format.
  582. static unsigned GetAlphaFormat();
  583. /// Return the API-specific luminance texture format.
  584. static unsigned GetLuminanceFormat();
  585. /// Return the API-specific luminance alpha texture format.
  586. static unsigned GetLuminanceAlphaFormat();
  587. /// Return the API-specific RGB texture format.
  588. static unsigned GetRGBFormat();
  589. /// Return the API-specific RGBA texture format.
  590. static unsigned GetRGBAFormat();
  591. /// Return the API-specific RGBA 16-bit texture format.
  592. static unsigned GetRGBA16Format();
  593. /// Return the API-specific RGBA 16-bit float texture format.
  594. static unsigned GetRGBAFloat16Format();
  595. /// Return the API-specific RGBA 32-bit float texture format.
  596. static unsigned GetRGBAFloat32Format();
  597. /// Return the API-specific RG 16-bit texture format.
  598. static unsigned GetRG16Format();
  599. /// Return the API-specific RG 16-bit float texture format.
  600. static unsigned GetRGFloat16Format();
  601. /// Return the API-specific RG 32-bit float texture format.
  602. static unsigned GetRGFloat32Format();
  603. /// Return the API-specific single channel 16-bit float texture format.
  604. static unsigned GetFloat16Format();
  605. /// Return the API-specific single channel 32-bit float texture format.
  606. static unsigned GetFloat32Format();
  607. /// Return the API-specific linear depth texture format.
  608. static unsigned GetLinearDepthFormat();
  609. /// Return the API-specific hardware depth-stencil texture format.
  610. static unsigned GetDepthStencilFormat();
  611. /// Return the API-specific readable hardware depth format, or 0 if not supported.
  612. static unsigned GetReadableDepthFormat();
  613. /// Return the API-specific texture format from a textual description, for example "rgb".
  614. static unsigned GetFormat(const String& formatName);
  615. /// Return maximum number of supported bones for skinning.
  616. static unsigned GetMaxBones();
  617. /// Return whether is using an OpenGL 3 context. Return always false on Direct3D9 & Direct3D11.
  618. static bool GetGL3Support();
  619. private:
  620. /// Create the application window icon.
  621. void CreateWindowIcon();
  622. /// Adjust parameters according to the platform. Fill in missing paramters and resolve possible conflicts.
  623. void AdjustScreenMode(int& newWidth, int& newHeight, ScreenModeParams& params, bool& maximize) const;
  624. /// Called when screen mode is successfully changed by the backend.
  625. void OnScreenModeChanged();
  626. #ifdef URHO3D_D3D11
  627. /// Create the application window.
  628. bool OpenWindow_D3D11(int width, int height, bool resizable, bool borderless);
  629. /// Adjust the window for new resolution and fullscreen mode.
  630. void AdjustWindow_D3D11(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless, bool& newResizable, int& monitor);
  631. /// Create the Direct3D11 device and swap chain. Requires an open window. Can also be called again to recreate swap chain. Return true on success.
  632. bool CreateDevice_D3D11(int width, int height);
  633. /// Update Direct3D11 swap chain state for a new mode and create views for the backbuffer & default depth buffer. Return true on success.
  634. bool UpdateSwapChain_D3D11(int width, int height);
  635. /// Create intermediate texture for multisampled backbuffer resolve. No-op if already exists.
  636. void CreateResolveTexture_D3D11();
  637. /// Process dirtied state before draw.
  638. void PrepareDraw_D3D11();
  639. /// Check supported rendering features.
  640. void CheckFeatureSupport_D3D11();
  641. /// Reset cached rendering state.
  642. void ResetCachedState_D3D11();
  643. /// Initialize texture unit mappings.
  644. void SetTextureUnitMappings_D3D11();
  645. /// Dirty texture parameters of all textures (when global settings change). Used on OpenGL and DirectX 11.
  646. void SetTextureParametersDirty_D3D11();
  647. #endif // def URHO3D_D3D11
  648. #ifdef URHO3D_OPENGL
  649. /// Clean up all framebuffers. Called when destroying the context. Used only on OpenGL.
  650. void CleanupFramebuffers_OGL();
  651. /// Create a framebuffer using either extension or core functionality. Used only on OpenGL.
  652. unsigned CreateFramebuffer_OGL();
  653. /// Delete a framebuffer using either extension or core functionality. Used only on OpenGL.
  654. void DeleteFramebuffer_OGL(unsigned fbo);
  655. /// Bind a framebuffer using either extension or core functionality. Used only on OpenGL.
  656. void BindFramebuffer_OGL(unsigned fbo);
  657. /// Bind a framebuffer color attachment using either extension or core functionality. Used only on OpenGL.
  658. void BindColorAttachment_OGL(unsigned index, unsigned target, unsigned object, bool isRenderBuffer);
  659. /// Bind a framebuffer depth attachment using either extension or core functionality. Used only on OpenGL.
  660. void BindDepthAttachment_OGL(unsigned object, bool isRenderBuffer);
  661. /// Bind a framebuffer stencil attachment using either extension or core functionality. Used only on OpenGL.
  662. void BindStencilAttachment_OGL(unsigned object, bool isRenderBuffer);
  663. /// Check FBO completeness using either extension or core functionality. Used only on OpenGL.
  664. bool CheckFramebuffer_OGL();
  665. /// Set vertex attrib divisor. No-op if unsupported. Used only on OpenGL.
  666. void SetVertexAttribDivisor_OGL(unsigned location, unsigned divisor);
  667. /// Release/clear GPU objects and optionally close the window. Used only on OpenGL.
  668. void Release_OGL(bool clearGPUObjects, bool closeWindow);
  669. /// Process dirtied state before draw.
  670. void PrepareDraw_OGL();
  671. /// Check supported rendering features.
  672. void CheckFeatureSupport_OGL();
  673. /// Reset cached rendering state.
  674. void ResetCachedState_OGL();
  675. /// Initialize texture unit mappings.
  676. void SetTextureUnitMappings_OGL();
  677. /// Dirty texture parameters of all textures (when global settings change). Used on OpenGL and DirectX 11.
  678. void SetTextureParametersDirty_OGL();
  679. #endif // def URHO3D_OPENGL
  680. // For proxy functions
  681. #ifdef URHO3D_OPENGL
  682. void Constructor_OGL();
  683. void Destructor_OGL();
  684. bool SetScreenMode_OGL(int width, int height, const ScreenModeParams& params, bool maximize);
  685. void SetSRGB_OGL(bool enable);
  686. void SetDither_OGL(bool enable);
  687. void SetFlushGPU_OGL(bool enable);
  688. void SetForceGL2_OGL(bool enable);
  689. void Close_OGL();
  690. bool TakeScreenShot_OGL(Image& destImage);
  691. bool BeginFrame_OGL();
  692. void EndFrame_OGL();
  693. void Clear_OGL(ClearTargetFlags flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, u32 stencil = 0);
  694. bool ResolveToTexture_OGL(Texture2D* destination, const IntRect& viewport);
  695. bool ResolveToTexture_OGL(Texture2D* texture);
  696. bool ResolveToTexture_OGL(TextureCube* texture);
  697. void Draw_OGL(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
  698. void Draw_OGL(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
  699. void Draw_OGL(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount);
  700. void DrawInstanced_OGL(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount, unsigned instanceCount);
  701. void DrawInstanced_OGL(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount, unsigned instanceCount);
  702. void SetVertexBuffer_OGL(VertexBuffer* buffer);
  703. bool SetVertexBuffers_OGL(const Vector<VertexBuffer*>& buffers, unsigned instanceOffset = 0);
  704. bool SetVertexBuffers_OGL(const Vector<SharedPtr<VertexBuffer>>& buffers, unsigned instanceOffset = 0);
  705. void SetIndexBuffer_OGL(IndexBuffer* buffer);
  706. void SetShaders_OGL(ShaderVariation* vs, ShaderVariation* ps);
  707. void SetShaderParameter_OGL(StringHash param, const float* data, unsigned count);
  708. void SetShaderParameter_OGL(StringHash param, float value);
  709. void SetShaderParameter_OGL(StringHash param, int value);
  710. void SetShaderParameter_OGL(StringHash param, bool value);
  711. void SetShaderParameter_OGL(StringHash param, const Color& color);
  712. void SetShaderParameter_OGL(StringHash param, const Vector2& vector);
  713. void SetShaderParameter_OGL(StringHash param, const Matrix3& matrix);
  714. void SetShaderParameter_OGL(StringHash param, const Vector3& vector);
  715. void SetShaderParameter_OGL(StringHash param, const Matrix4& matrix);
  716. void SetShaderParameter_OGL(StringHash param, const Vector4& vector);
  717. void SetShaderParameter_OGL(StringHash param, const Matrix3x4& matrix);
  718. bool NeedParameterUpdate_OGL(ShaderParameterGroup group, const void* source);
  719. bool HasShaderParameter_OGL(StringHash param);
  720. bool HasTextureUnit_OGL(TextureUnit unit);
  721. void ClearParameterSource_OGL(ShaderParameterGroup group);
  722. void ClearParameterSources_OGL();
  723. void ClearTransformSources_OGL();
  724. void SetTexture_OGL(unsigned index, Texture* texture);
  725. void SetDefaultTextureFilterMode_OGL(TextureFilterMode mode);
  726. void SetDefaultTextureAnisotropy_OGL(unsigned level);
  727. void ResetRenderTargets_OGL();
  728. void ResetRenderTarget_OGL(unsigned index);
  729. void ResetDepthStencil_OGL();
  730. void SetRenderTarget_OGL(unsigned index, RenderSurface* renderTarget);
  731. void SetRenderTarget_OGL(unsigned index, Texture2D* texture);
  732. void SetDepthStencil_OGL(RenderSurface* depthStencil);
  733. void SetDepthStencil_OGL(Texture2D* texture);
  734. void SetViewport_OGL(const IntRect& rect);
  735. void SetBlendMode_OGL(BlendMode mode, bool alphaToCoverage = false);
  736. void SetColorWrite_OGL(bool enable);
  737. void SetCullMode_OGL(CullMode mode);
  738. void SetDepthBias_OGL(float constantBias, float slopeScaledBias);
  739. void SetDepthTest_OGL(CompareMode mode);
  740. void SetDepthWrite_OGL(bool enable);
  741. void SetFillMode_OGL(FillMode mode);
  742. void SetLineAntiAlias_OGL(bool enable);
  743. void SetScissorTest_OGL(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
  744. void SetScissorTest_OGL(bool enable, const IntRect& rect);
  745. void SetClipPlane_OGL(bool enable, const Plane& clipPlane, const Matrix3x4& view, const Matrix4& projection);
  746. void SetStencilTest_OGL(bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP, u32 stencilRef = 0, u32 compareMask = M_U32_MASK_ALL_BITS, u32 writeMask = M_U32_MASK_ALL_BITS);
  747. bool IsInitialized_OGL() const;
  748. bool GetDither_OGL() const;
  749. bool IsDeviceLost_OGL() const;
  750. Vector<int> GetMultiSampleLevels_OGL() const;
  751. unsigned GetFormat_OGL(CompressedFormat format) const;
  752. ShaderVariation* GetShader_OGL(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
  753. ShaderVariation* GetShader_OGL(ShaderType type, const char* name, const char* defines) const;
  754. VertexBuffer* GetVertexBuffer_OGL(unsigned index) const;
  755. TextureUnit GetTextureUnit_OGL(const String& name);
  756. const String& GetTextureUnitName_OGL(TextureUnit unit);
  757. Texture* GetTexture_OGL(unsigned index) const;
  758. RenderSurface* GetRenderTarget_OGL(unsigned index) const;
  759. IntVector2 GetRenderTargetDimensions_OGL() const;
  760. void OnWindowResized_OGL();
  761. void OnWindowMoved_OGL();
  762. ConstantBuffer* GetOrCreateConstantBuffer_OGL(ShaderType type, unsigned index, unsigned size);
  763. static unsigned GetMaxBones_OGL();
  764. static bool GetGL3Support_OGL();
  765. static unsigned GetAlphaFormat_OGL();
  766. static unsigned GetLuminanceFormat_OGL();
  767. static unsigned GetLuminanceAlphaFormat_OGL();
  768. static unsigned GetRGBFormat_OGL();
  769. static unsigned GetRGBAFormat_OGL();
  770. static unsigned GetRGBA16Format_OGL();
  771. static unsigned GetRGBAFloat16Format_OGL();
  772. static unsigned GetRGBAFloat32Format_OGL();
  773. static unsigned GetRG16Format_OGL();
  774. static unsigned GetRGFloat16Format_OGL();
  775. static unsigned GetRGFloat32Format_OGL();
  776. static unsigned GetFloat16Format_OGL();
  777. static unsigned GetFloat32Format_OGL();
  778. static unsigned GetLinearDepthFormat_OGL();
  779. static unsigned GetDepthStencilFormat_OGL();
  780. static unsigned GetReadableDepthFormat_OGL();
  781. static unsigned GetFormat_OGL(const String& formatName);
  782. #endif // def URHO3D_OPENGL
  783. #ifdef URHO3D_D3D11
  784. void Constructor_D3D11();
  785. void Destructor_D3D11();
  786. bool SetScreenMode_D3D11(int width, int height, const ScreenModeParams& params, bool maximize);
  787. void SetSRGB_D3D11(bool enable);
  788. void SetDither_D3D11(bool enable);
  789. void SetFlushGPU_D3D11(bool enable);
  790. void SetForceGL2_D3D11(bool enable);
  791. void Close_D3D11();
  792. bool TakeScreenShot_D3D11(Image& destImage);
  793. bool BeginFrame_D3D11();
  794. void EndFrame_D3D11();
  795. void Clear_D3D11(ClearTargetFlags flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, u32 stencil = 0);
  796. bool ResolveToTexture_D3D11(Texture2D* destination, const IntRect& viewport);
  797. bool ResolveToTexture_D3D11(Texture2D* texture);
  798. bool ResolveToTexture_D3D11(TextureCube* texture);
  799. void Draw_D3D11(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
  800. void Draw_D3D11(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
  801. void Draw_D3D11(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount);
  802. void DrawInstanced_D3D11(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount, unsigned instanceCount);
  803. void DrawInstanced_D3D11(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount, unsigned instanceCount);
  804. void SetVertexBuffer_D3D11(VertexBuffer* buffer);
  805. bool SetVertexBuffers_D3D11(const Vector<VertexBuffer*>& buffers, unsigned instanceOffset = 0);
  806. bool SetVertexBuffers_D3D11(const Vector<SharedPtr<VertexBuffer>>& buffers, unsigned instanceOffset = 0);
  807. void SetIndexBuffer_D3D11(IndexBuffer* buffer);
  808. void SetShaders_D3D11(ShaderVariation* vs, ShaderVariation* ps);
  809. void SetShaderParameter_D3D11(StringHash param, const float* data, unsigned count);
  810. void SetShaderParameter_D3D11(StringHash param, float value);
  811. void SetShaderParameter_D3D11(StringHash param, int value);
  812. void SetShaderParameter_D3D11(StringHash param, bool value);
  813. void SetShaderParameter_D3D11(StringHash param, const Color& color);
  814. void SetShaderParameter_D3D11(StringHash param, const Vector2& vector);
  815. void SetShaderParameter_D3D11(StringHash param, const Matrix3& matrix);
  816. void SetShaderParameter_D3D11(StringHash param, const Vector3& vector);
  817. void SetShaderParameter_D3D11(StringHash param, const Matrix4& matrix);
  818. void SetShaderParameter_D3D11(StringHash param, const Vector4& vector);
  819. void SetShaderParameter_D3D11(StringHash param, const Matrix3x4& matrix);
  820. bool NeedParameterUpdate_D3D11(ShaderParameterGroup group, const void* source);
  821. bool HasShaderParameter_D3D11(StringHash param);
  822. bool HasTextureUnit_D3D11(TextureUnit unit);
  823. void ClearParameterSource_D3D11(ShaderParameterGroup group);
  824. void ClearParameterSources_D3D11();
  825. void ClearTransformSources_D3D11();
  826. void SetTexture_D3D11(unsigned index, Texture* texture);
  827. void SetDefaultTextureFilterMode_D3D11(TextureFilterMode mode);
  828. void SetDefaultTextureAnisotropy_D3D11(unsigned level);
  829. void ResetRenderTargets_D3D11();
  830. void ResetRenderTarget_D3D11(unsigned index);
  831. void ResetDepthStencil_D3D11();
  832. void SetRenderTarget_D3D11(unsigned index, RenderSurface* renderTarget);
  833. void SetRenderTarget_D3D11(unsigned index, Texture2D* texture);
  834. void SetDepthStencil_D3D11(RenderSurface* depthStencil);
  835. void SetDepthStencil_D3D11(Texture2D* texture);
  836. void SetViewport_D3D11(const IntRect& rect);
  837. void SetBlendMode_D3D11(BlendMode mode, bool alphaToCoverage = false);
  838. void SetColorWrite_D3D11(bool enable);
  839. void SetCullMode_D3D11(CullMode mode);
  840. void SetDepthBias_D3D11(float constantBias, float slopeScaledBias);
  841. void SetDepthTest_D3D11(CompareMode mode);
  842. void SetDepthWrite_D3D11(bool enable);
  843. void SetFillMode_D3D11(FillMode mode);
  844. void SetLineAntiAlias_D3D11(bool enable);
  845. void SetScissorTest_D3D11(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
  846. void SetScissorTest_D3D11(bool enable, const IntRect& rect);
  847. void SetClipPlane_D3D11(bool enable, const Plane& clipPlane, const Matrix3x4& view, const Matrix4& projection);
  848. void SetStencilTest_D3D11(bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP, u32 stencilRef = 0, u32 compareMask = M_U32_MASK_ALL_BITS, u32 writeMask = M_U32_MASK_ALL_BITS);
  849. bool IsInitialized_D3D11() const;
  850. bool GetDither_D3D11() const;
  851. bool IsDeviceLost_D3D11() const;
  852. Vector<int> GetMultiSampleLevels_D3D11() const;
  853. unsigned GetFormat_D3D11(CompressedFormat format) const;
  854. ShaderVariation* GetShader_D3D11(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
  855. ShaderVariation* GetShader_D3D11(ShaderType type, const char* name, const char* defines) const;
  856. VertexBuffer* GetVertexBuffer_D3D11(unsigned index) const;
  857. TextureUnit GetTextureUnit_D3D11(const String& name);
  858. const String& GetTextureUnitName_D3D11(TextureUnit unit);
  859. Texture* GetTexture_D3D11(unsigned index) const;
  860. RenderSurface* GetRenderTarget_D3D11(unsigned index) const;
  861. IntVector2 GetRenderTargetDimensions_D3D11() const;
  862. void OnWindowResized_D3D11();
  863. void OnWindowMoved_D3D11();
  864. ConstantBuffer* GetOrCreateConstantBuffer_D3D11(ShaderType type, unsigned index, unsigned size);
  865. static unsigned GetMaxBones_D3D11();
  866. static bool GetGL3Support_D3D11();
  867. static unsigned GetAlphaFormat_D3D11();
  868. static unsigned GetLuminanceFormat_D3D11();
  869. static unsigned GetLuminanceAlphaFormat_D3D11();
  870. static unsigned GetRGBFormat_D3D11();
  871. static unsigned GetRGBAFormat_D3D11();
  872. static unsigned GetRGBA16Format_D3D11();
  873. static unsigned GetRGBAFloat16Format_D3D11();
  874. static unsigned GetRGBAFloat32Format_D3D11();
  875. static unsigned GetRG16Format_D3D11();
  876. static unsigned GetRGFloat16Format_D3D11();
  877. static unsigned GetRGFloat32Format_D3D11();
  878. static unsigned GetFloat16Format_D3D11();
  879. static unsigned GetFloat32Format_D3D11();
  880. static unsigned GetLinearDepthFormat_D3D11();
  881. static unsigned GetDepthStencilFormat_D3D11();
  882. static unsigned GetReadableDepthFormat_D3D11();
  883. static unsigned GetFormat_D3D11(const String& formatName);
  884. #endif // def URHO3D_D3D11
  885. /// Mutex for accessing the GPU objects vector from several threads.
  886. Mutex gpuObjectMutex_;
  887. /// Implementation.
  888. void* impl_;
  889. /// SDL window.
  890. SDL_Window* window_{};
  891. /// Window title.
  892. String windowTitle_;
  893. /// Window icon image.
  894. WeakPtr<Image> windowIcon_;
  895. /// External window, null if not in use (default.)
  896. void* externalWindow_{};
  897. /// Most recently applied window mode. It may not represent actual window state
  898. /// if window was resized by user or Graphics::SetScreenMode was explicitly called.
  899. WindowModeParams primaryWindowMode_;
  900. /// Secondary window mode to be applied on Graphics::ToggleFullscreen.
  901. WindowModeParams secondaryWindowMode_;
  902. /// Window width in pixels.
  903. int width_{};
  904. /// Window height in pixels.
  905. int height_{};
  906. /// Window position.
  907. IntVector2 position_;
  908. /// Screen mode parameters.
  909. ScreenModeParams screenParams_;
  910. /// Flush GPU command buffer flag.
  911. bool flushGPU_{};
  912. /// Force OpenGL 2 flag. Only used on OpenGL.
  913. bool forceGL2_{};
  914. /// sRGB conversion on write flag for the main window.
  915. bool sRGB_{};
  916. /// Light pre-pass rendering support flag.
  917. bool lightPrepassSupport_{};
  918. /// Deferred rendering support flag.
  919. bool deferredSupport_{};
  920. /// Anisotropic filtering support flag.
  921. bool anisotropySupport_{};
  922. /// DXT format support flag.
  923. bool dxtTextureSupport_{};
  924. /// ETC1 format support flag.
  925. bool etcTextureSupport_{};
  926. /// ETC2 format support flag.
  927. bool etc2TextureSupport_{};
  928. /// PVRTC formats support flag.
  929. bool pvrtcTextureSupport_{};
  930. /// Hardware shadow map depth compare support flag.
  931. bool hardwareShadowSupport_{};
  932. /// Instancing support flag.
  933. bool instancingSupport_{};
  934. /// sRGB conversion on read support flag.
  935. bool sRGBSupport_{};
  936. /// sRGB conversion on write support flag.
  937. bool sRGBWriteSupport_{};
  938. /// Number of primitives this frame.
  939. unsigned numPrimitives_{};
  940. /// Number of batches this frame.
  941. unsigned numBatches_{};
  942. /// Largest scratch buffer request this frame.
  943. i32 maxScratchBufferRequest_{};
  944. /// GPU objects.
  945. Vector<GPUObject*> gpuObjects_;
  946. /// Scratch buffers.
  947. Vector<ScratchBuffer> scratchBuffers_;
  948. /// Shadow map dummy color texture format.
  949. unsigned dummyColorFormat_{};
  950. /// Shadow map depth texture format.
  951. unsigned shadowMapFormat_{};
  952. /// Shadow map 24-bit depth texture format.
  953. unsigned hiresShadowMapFormat_{};
  954. /// Vertex buffers in use.
  955. VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS]{};
  956. /// Index buffer in use.
  957. IndexBuffer* indexBuffer_{};
  958. /// Current vertex declaration hash.
  959. hash64 vertexDeclarationHash_{};
  960. /// Current primitive type.
  961. unsigned primitiveType_{};
  962. /// Vertex shader in use.
  963. ShaderVariation* vertexShader_{};
  964. /// Pixel shader in use.
  965. ShaderVariation* pixelShader_{};
  966. /// Textures in use.
  967. Texture* textures_[MAX_TEXTURE_UNITS]{};
  968. /// Texture unit mappings.
  969. HashMap<String, TextureUnit> textureUnits_;
  970. /// Rendertargets in use.
  971. RenderSurface* renderTargets_[MAX_RENDERTARGETS]{};
  972. /// Depth-stencil surface in use.
  973. RenderSurface* depthStencil_{};
  974. /// Viewport coordinates.
  975. IntRect viewport_;
  976. /// Default texture filtering mode.
  977. TextureFilterMode defaultTextureFilterMode_{FILTER_TRILINEAR};
  978. /// Default texture max. anisotropy level.
  979. unsigned defaultTextureAnisotropy_{4};
  980. /// Blending mode.
  981. BlendMode blendMode_{};
  982. /// Alpha-to-coverage enable.
  983. bool alphaToCoverage_{};
  984. /// Color write enable.
  985. bool colorWrite_{};
  986. /// Hardware culling mode.
  987. CullMode cullMode_{};
  988. /// Depth constant bias.
  989. float constantDepthBias_{};
  990. /// Depth slope scaled bias.
  991. float slopeScaledDepthBias_{};
  992. /// Depth compare mode.
  993. CompareMode depthTestMode_{};
  994. /// Depth write enable flag.
  995. bool depthWrite_{};
  996. /// Line antialiasing enable flag.
  997. bool lineAntiAlias_{};
  998. /// Polygon fill mode.
  999. FillMode fillMode_{};
  1000. /// Scissor test enable flag.
  1001. bool scissorTest_{};
  1002. /// Scissor test rectangle.
  1003. IntRect scissorRect_;
  1004. /// Stencil test compare mode.
  1005. CompareMode stencilTestMode_{};
  1006. /// Stencil operation on pass.
  1007. StencilOp stencilPass_{};
  1008. /// Stencil operation on fail.
  1009. StencilOp stencilFail_{};
  1010. /// Stencil operation on depth fail.
  1011. StencilOp stencilZFail_{};
  1012. /// Stencil test reference value.
  1013. u32 stencilRef_{};
  1014. /// Stencil compare bitmask.
  1015. u32 stencilCompareMask_{};
  1016. /// Stencil write bitmask.
  1017. u32 stencilWriteMask_{};
  1018. /// Current custom clip plane in post-projection space.
  1019. Vector4 clipPlane_;
  1020. /// Stencil test enable flag.
  1021. bool stencilTest_{};
  1022. /// Custom clip plane enable flag.
  1023. bool useClipPlane_{};
  1024. /// Remembered shader parameter sources.
  1025. const void* shaderParameterSources_[MAX_SHADER_PARAMETER_GROUPS]{};
  1026. /// Base directory for shaders.
  1027. String shaderPath_;
  1028. /// Cache directory for Direct3D binary shaders.
  1029. String shaderCacheDir_;
  1030. /// File extension for shaders.
  1031. String shaderExtension_;
  1032. /// Last used shader in shader variation query.
  1033. mutable WeakPtr<Shader> lastShader_;
  1034. /// Last used shader name in shader variation query.
  1035. mutable String lastShaderName_;
  1036. /// Shader precache utility.
  1037. SharedPtr<ShaderPrecache> shaderPrecache_;
  1038. /// Allowed screen orientations.
  1039. String orientations_;
  1040. /// Graphics API name.
  1041. String apiName_;
  1042. #ifdef URHO3D_OPENGL
  1043. /// Renderer name (usually GPU name)
  1044. String rendererName_;
  1045. /// Version of GL drivers
  1046. String versionString_;
  1047. #endif
  1048. /// OpenGL3 support flag.
  1049. inline static bool gl3Support;
  1050. /// Used graphics API.
  1051. inline static GAPI gapi;
  1052. };
  1053. /// Register Graphics library objects.
  1054. /// @nobind
  1055. void URHO3D_API RegisterGraphicsLibrary(Context* context);
  1056. } // namespace Urho3D