Graphics.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Container/HashSet.h"
  25. #include "../Core/Mutex.h"
  26. #include "../Core/Object.h"
  27. #include "../Graphics/GraphicsDefs.h"
  28. #include "../Graphics/ShaderVariation.h"
  29. #include "../Math/Color.h"
  30. #include "../Math/Plane.h"
  31. #include "../Math/Rect.h"
  32. #include "../Resource/Image.h"
  33. struct SDL_Window;
  34. namespace Atomic
  35. {
  36. class ConstantBuffer;
  37. class File;
  38. class Image;
  39. class IndexBuffer;
  40. class GPUObject;
  41. class GraphicsImpl;
  42. class RenderSurface;
  43. class Shader;
  44. class ShaderPrecache;
  45. class ShaderProgram;
  46. class ShaderVariation;
  47. class Texture;
  48. class Texture2D;
  49. class Texture2DArray;
  50. class TextureCube;
  51. class Vector3;
  52. class Vector4;
  53. class VertexBuffer;
  54. class VertexDeclaration;
  55. struct ShaderParameter;
  56. /// CPU-side scratch buffer for vertex data updates.
  57. struct ScratchBuffer
  58. {
  59. ScratchBuffer() :
  60. size_(0),
  61. reserved_(false)
  62. {
  63. }
  64. /// Buffer data.
  65. SharedArrayPtr<unsigned char> data_;
  66. /// Data size.
  67. unsigned size_;
  68. /// Reserved flag.
  69. bool reserved_;
  70. };
  71. /// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
  72. class ATOMIC_API Graphics : public Object
  73. {
  74. ATOMIC_OBJECT(Graphics, Object);
  75. public:
  76. /// Construct.
  77. Graphics(Context* context);
  78. /// Destruct. Release the Direct3D11 device and close the window.
  79. virtual ~Graphics();
  80. /// Set external window handle. Only effective before setting the initial screen mode.
  81. void SetExternalWindow(void* window);
  82. /// Set window title.
  83. void SetWindowTitle(const String& windowTitle);
  84. /// Set window icon.
  85. void SetWindowIcon(Image* windowIcon);
  86. /// Set window position. Sets initial position if window is not created yet.
  87. void SetWindowPosition(const IntVector2& position);
  88. /// Set window position. Sets initial position if window is not created yet.
  89. void SetWindowPosition(int x, int y);
  90. /// Set screen mode. Return true if successful.
  91. bool SetMode
  92. (int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync, bool tripleBuffer,
  93. int multiSample, int monitor, int refreshRate);
  94. /// Set screen resolution only. Return true if successful.
  95. bool SetMode(int width, int height);
  96. /// Set whether the main window uses sRGB conversion on write.
  97. void SetSRGB(bool enable);
  98. /// Set whether rendering output is dithered. Default true on OpenGL. No effect on Direct3D.
  99. void SetDither(bool enable);
  100. /// 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.
  101. void SetFlushGPU(bool enable);
  102. /// 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.
  103. void SetForceGL2(bool enable);
  104. /// Set allowed screen orientations as a space-separated list of "LandscapeLeft", "LandscapeRight", "Portrait" and "PortraitUpsideDown". Affects currently only iOS platform.
  105. void SetOrientations(const String& orientations);
  106. /// Toggle between full screen and windowed mode. Return true if successful.
  107. bool ToggleFullscreen();
  108. /// Close the window.
  109. void Close();
  110. /// Take a screenshot. Return true if successful.
  111. bool TakeScreenShot(Image* destImage); // ATOMIC FIX: Image* must be a pointer type (for script bindings)
  112. /// Begin frame rendering. Return true if device available and can render.
  113. bool BeginFrame();
  114. /// End frame rendering and swap buffers.
  115. void EndFrame();
  116. /// Clear any or all of rendertarget, depth buffer and stencil buffer.
  117. void Clear(unsigned flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, unsigned stencil = 0);
  118. /// Resolve multisampled backbuffer to a texture rendertarget. The texture's size should match the viewport size.
  119. bool ResolveToTexture(Texture2D* destination, const IntRect& viewport);
  120. /// Resolve a multisampled texture on itself.
  121. bool ResolveToTexture(Texture2D* texture);
  122. /// Resolve a multisampled cube texture on itself.
  123. bool ResolveToTexture(TextureCube* texture);
  124. /// Draw non-indexed geometry.
  125. void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
  126. /// Draw indexed geometry.
  127. void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
  128. /// Draw indexed geometry with vertex index offset.
  129. void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount);
  130. /// Draw indexed, instanced geometry. An instancing vertex buffer must be set.
  131. void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
  132. unsigned instanceCount);
  133. /// Draw indexed, instanced geometry with vertex index offset.
  134. void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex,
  135. unsigned vertexCount, unsigned instanceCount);
  136. /// Set vertex buffer.
  137. void SetVertexBuffer(VertexBuffer* buffer);
  138. /// Set multiple vertex buffers.
  139. bool SetVertexBuffers(const PODVector<VertexBuffer*>& buffers, unsigned instanceOffset = 0);
  140. /// Set multiple vertex buffers.
  141. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, unsigned instanceOffset = 0);
  142. /// Set index buffer.
  143. void SetIndexBuffer(IndexBuffer* buffer);
  144. /// Set shaders.
  145. void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
  146. /// Set shader float constants.
  147. void SetShaderParameter(StringHash param, const float* data, unsigned count);
  148. /// Set shader float constant.
  149. void SetShaderParameter(StringHash param, float value);
  150. /// Set shader integer constant.
  151. void SetShaderParameter(StringHash param, int value);
  152. /// Set shader boolean constant.
  153. void SetShaderParameter(StringHash param, bool value);
  154. /// Set shader color constant.
  155. void SetShaderParameter(StringHash param, const Color& color);
  156. /// Set shader 2D vector constant.
  157. void SetShaderParameter(StringHash param, const Vector2& vector);
  158. /// Set shader 3x3 matrix constant.
  159. void SetShaderParameter(StringHash param, const Matrix3& matrix);
  160. /// Set shader 3D vector constant.
  161. void SetShaderParameter(StringHash param, const Vector3& vector);
  162. /// Set shader 4x4 matrix constant.
  163. void SetShaderParameter(StringHash param, const Matrix4& matrix);
  164. /// Set shader 4D vector constant.
  165. void SetShaderParameter(StringHash param, const Vector4& vector);
  166. /// Set shader 3x4 matrix constant.
  167. void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
  168. /// Set shader constant from a variant. Supported variant types: bool, float, vector2, vector3, vector4, color.
  169. void SetShaderParameter(StringHash param, const Variant& value);
  170. /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
  171. bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
  172. /// Check whether a shader parameter exists on the currently set shaders.
  173. bool HasShaderParameter(StringHash param);
  174. /// Check whether the current vertex or pixel shader uses a texture unit.
  175. bool HasTextureUnit(TextureUnit unit);
  176. /// Clear remembered shader parameter source group.
  177. void ClearParameterSource(ShaderParameterGroup group);
  178. /// Clear remembered shader parameter sources.
  179. void ClearParameterSources();
  180. /// Clear remembered transform shader parameter sources.
  181. void ClearTransformSources();
  182. /// Set texture.
  183. void SetTexture(unsigned index, Texture* texture);
  184. /// Bind texture unit 0 for update. Called by Texture. Used only on OpenGL.
  185. void SetTextureForUpdate(Texture* texture);
  186. /// Dirty texture parameters of all textures (when global settings change.)
  187. void SetTextureParametersDirty();
  188. /// Set default texture filtering mode. Called by Renderer before rendering.
  189. void SetDefaultTextureFilterMode(TextureFilterMode mode);
  190. /// Set default texture anisotropy level. Called by Renderer before rendering.
  191. void SetDefaultTextureAnisotropy(unsigned level);
  192. /// Reset all rendertargets, depth-stencil surface and viewport.
  193. void ResetRenderTargets();
  194. /// Reset specific rendertarget.
  195. void ResetRenderTarget(unsigned index);
  196. /// Reset depth-stencil surface.
  197. void ResetDepthStencil();
  198. /// Set rendertarget.
  199. void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
  200. /// Set rendertarget.
  201. void SetRenderTarget(unsigned index, Texture2D* texture);
  202. /// Set depth-stencil surface.
  203. void SetDepthStencil(RenderSurface* depthStencil);
  204. /// Set depth-stencil surface.
  205. void SetDepthStencil(Texture2D* texture);
  206. /// Set viewport.
  207. void SetViewport(const IntRect& rect);
  208. /// Set blending and alpha-to-coverage modes. Alpha-to-coverage is not supported on Direct3D9.
  209. void SetBlendMode(BlendMode mode, bool alphaToCoverage = false);
  210. /// Set color write on/off.
  211. void SetColorWrite(bool enable);
  212. /// Set hardware culling mode.
  213. void SetCullMode(CullMode mode);
  214. /// Set depth bias.
  215. void SetDepthBias(float constantBias, float slopeScaledBias);
  216. /// Set depth compare.
  217. void SetDepthTest(CompareMode mode);
  218. /// Set depth write on/off.
  219. void SetDepthWrite(bool enable);
  220. /// Set polygon fill mode.
  221. void SetFillMode(FillMode mode);
  222. /// Set line antialiasing on/off.
  223. void SetLineAntiAlias(bool enable);
  224. /// Set scissor test.
  225. void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
  226. /// Set scissor test.
  227. void SetScissorTest(bool enable, const IntRect& rect);
  228. /// Set stencil test.
  229. void SetStencilTest
  230. (bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP,
  231. unsigned stencilRef = 0, unsigned compareMask = M_MAX_UNSIGNED, unsigned writeMask = M_MAX_UNSIGNED);
  232. /// Set a custom clipping plane. The plane is specified in world space, but is dependent on the view and projection matrices.
  233. void SetClipPlane(bool enable, const Plane& clipPlane = Plane::UP, const Matrix3x4& view = Matrix3x4::IDENTITY,
  234. const Matrix4& projection = Matrix4::IDENTITY);
  235. /// Begin dumping shader variation names to an XML file for precaching.
  236. void BeginDumpShaders(const String& fileName);
  237. /// End dumping shader variations names.
  238. void EndDumpShaders();
  239. /// Precache shader variations from an XML file generated with BeginDumpShaders().
  240. void PrecacheShaders(Deserializer& source);
  241. /// Set shader cache directory, Direct3D only. This can either be an absolute path or a path within the resource system.
  242. void SetShaderCacheDir(const String& path);
  243. /// Return whether rendering initialized.
  244. bool IsInitialized() const;
  245. /// Return graphics implementation, which holds the actual API-specific resources.
  246. GraphicsImpl* GetImpl() const { return impl_; }
  247. /// Return OS-specific external window handle. Null if not in use.
  248. void* GetExternalWindow() const { return externalWindow_; }
  249. /// Return SDL window.
  250. SDL_Window* GetWindow() const { return window_; }
  251. /// Return window title.
  252. const String& GetWindowTitle() const { return windowTitle_; }
  253. /// Return graphics API name.
  254. const String& GetApiName() const { return apiName_; }
  255. /// Return window position.
  256. IntVector2 GetWindowPosition() const;
  257. /// Return window width in pixels.
  258. int GetWidth() const { return width_; }
  259. /// Return window height in pixels.
  260. int GetHeight() const { return height_; }
  261. /// Return multisample mode (1 = no multisampling.)
  262. int GetMultiSample() const { return multiSample_; }
  263. /// Return window size in pixels.
  264. IntVector2 GetSize() const { return IntVector2(width_, height_); }
  265. /// Return whether window is fullscreen.
  266. bool GetFullscreen() const { return fullscreen_; }
  267. /// Return whether window is borderless.
  268. bool GetBorderless() const { return borderless_; }
  269. /// Return whether window is resizable.
  270. bool GetResizable() const { return resizable_; }
  271. /// Return whether window is high DPI.
  272. bool GetHighDPI() const { return highDPI_; }
  273. /// Return whether vertical sync is on.
  274. bool GetVSync() const { return vsync_; }
  275. /// Return refresh rate when using vsync in fullscreen
  276. int GetRefreshRate() const { return refreshRate_; }
  277. /// Return the current monitor index. Effective on in fullscreen
  278. int GetMonitor() const { return monitor_; }
  279. /// Return whether triple buffering is enabled.
  280. bool GetTripleBuffer() const { return tripleBuffer_; }
  281. /// Return whether the main window is using sRGB conversion on write.
  282. bool GetSRGB() const { return sRGB_; }
  283. /// Return whether rendering output is dithered.
  284. bool GetDither() const;
  285. /// Return whether the GPU command buffer is flushed each frame.
  286. bool GetFlushGPU() const { return flushGPU_; }
  287. /// Return whether OpenGL 2 use is forced. Effective only on OpenGL.
  288. bool GetForceGL2() const { return forceGL2_; }
  289. /// Return allowed screen orientations.
  290. const String& GetOrientations() const { return orientations_; }
  291. /// Return whether graphics context is lost and can not render or load GPU resources.
  292. bool IsDeviceLost() const;
  293. /// Return number of primitives drawn this frame.
  294. unsigned GetNumPrimitives() const { return numPrimitives_; }
  295. /// Return number of batches drawn this frame.
  296. unsigned GetNumBatches() const { return numBatches_; }
  297. /// Return dummy color texture format for shadow maps. Is "NULL" (consume no video memory) if supported.
  298. unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
  299. /// Return shadow map depth texture format, or 0 if not supported.
  300. unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
  301. /// Return 24-bit shadow map depth texture format, or 0 if not supported.
  302. unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
  303. /// Return whether hardware instancing is supported.
  304. bool GetInstancingSupport() const { return instancingSupport_; }
  305. /// Return whether light pre-pass rendering is supported.
  306. bool GetLightPrepassSupport() const { return lightPrepassSupport_; }
  307. /// Return whether deferred rendering is supported.
  308. bool GetDeferredSupport() const { return deferredSupport_; }
  309. /// Return whether anisotropic texture filtering is supported.
  310. bool GetAnisotropySupport() const { return anisotropySupport_; }
  311. /// Return whether shadow map depth compare is done in hardware.
  312. bool GetHardwareShadowSupport() const { return hardwareShadowSupport_; }
  313. /// Return whether a readable hardware depth format is available.
  314. bool GetReadableDepthSupport() const { return GetReadableDepthFormat() != 0; }
  315. /// Return whether sRGB conversion on texture sampling is supported.
  316. bool GetSRGBSupport() const { return sRGBSupport_; }
  317. /// Return whether sRGB conversion on rendertarget writing is supported.
  318. bool GetSRGBWriteSupport() const { return sRGBWriteSupport_; }
  319. /// Return supported fullscreen resolutions (third component is refreshRate). Will be empty if listing the resolutions is not supported on the platform (e.g. Web).
  320. PODVector<IntVector3> GetResolutions(int monitor) const;
  321. /// Return supported multisampling levels.
  322. PODVector<int> GetMultiSampleLevels() const;
  323. /// Return the desktop resolution.
  324. IntVector2 GetDesktopResolution(int monitor) const;
  325. /// Return the number of currently connected monitors.
  326. int GetMonitorCount() const;
  327. /// Return hardware format for a compressed image format, or 0 if unsupported.
  328. unsigned GetFormat(CompressedFormat format) const;
  329. /// Return a shader variation by name and defines.
  330. ShaderVariation* GetShader(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
  331. /// Return a shader variation by name and defines.
  332. ShaderVariation* GetShader(ShaderType type, const char* name, const char* defines) const;
  333. /// Return current vertex buffer by index.
  334. VertexBuffer* GetVertexBuffer(unsigned index) const;
  335. /// Return current index buffer.
  336. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  337. /// Return current vertex shader.
  338. ShaderVariation* GetVertexShader() const { return vertexShader_; }
  339. /// Return current pixel shader.
  340. ShaderVariation* GetPixelShader() const { return pixelShader_; }
  341. /// Return shader program. This is an API-specific class and should not be used by applications.
  342. ShaderProgram* GetShaderProgram() const;
  343. /// Return texture unit index by name.
  344. TextureUnit GetTextureUnit(const String& name);
  345. /// Return texture unit name by index.
  346. const String& GetTextureUnitName(TextureUnit unit);
  347. /// Return current texture by texture unit index.
  348. Texture* GetTexture(unsigned index) const;
  349. /// Return default texture filtering mode.
  350. TextureFilterMode GetDefaultTextureFilterMode() const { return defaultTextureFilterMode_; }
  351. /// Return default texture max. anisotropy level.
  352. unsigned GetDefaultTextureAnisotropy() const { return defaultTextureAnisotropy_; }
  353. /// Return current rendertarget by index.
  354. RenderSurface* GetRenderTarget(unsigned index) const;
  355. /// Return current depth-stencil surface.
  356. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  357. /// Return the viewport coordinates.
  358. IntRect GetViewport() const { return viewport_; }
  359. /// Return blending mode.
  360. BlendMode GetBlendMode() const { return blendMode_; }
  361. /// Return whether alpha-to-coverage is enabled.
  362. bool GetAlphaToCoverage() const { return alphaToCoverage_; }
  363. /// Return whether color write is enabled.
  364. bool GetColorWrite() const { return colorWrite_; }
  365. /// Return hardware culling mode.
  366. CullMode GetCullMode() const { return cullMode_; }
  367. /// Return depth constant bias.
  368. float GetDepthConstantBias() const { return constantDepthBias_; }
  369. /// Return depth slope scaled bias.
  370. float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
  371. /// Return depth compare mode.
  372. CompareMode GetDepthTest() const { return depthTestMode_; }
  373. /// Return whether depth write is enabled.
  374. bool GetDepthWrite() const { return depthWrite_; }
  375. /// Return polygon fill mode.
  376. FillMode GetFillMode() const { return fillMode_; }
  377. /// Return whether line antialiasing is enabled.
  378. bool GetLineAntiAlias() const { return lineAntiAlias_; }
  379. /// Return whether stencil test is enabled.
  380. bool GetStencilTest() const { return stencilTest_; }
  381. /// Return whether scissor test is enabled.
  382. bool GetScissorTest() const { return scissorTest_; }
  383. /// Return scissor rectangle coordinates.
  384. const IntRect& GetScissorRect() const { return scissorRect_; }
  385. /// Return stencil compare mode.
  386. CompareMode GetStencilTestMode() const { return stencilTestMode_; }
  387. /// Return stencil operation to do if stencil test passes.
  388. StencilOp GetStencilPass() const { return stencilPass_; }
  389. /// Return stencil operation to do if stencil test fails.
  390. StencilOp GetStencilFail() const { return stencilFail_; }
  391. /// Return stencil operation to do if depth compare fails.
  392. StencilOp GetStencilZFail() const { return stencilZFail_; }
  393. /// Return stencil reference value.
  394. unsigned GetStencilRef() const { return stencilRef_; }
  395. /// Return stencil compare bitmask.
  396. unsigned GetStencilCompareMask() const { return stencilCompareMask_; }
  397. /// Return stencil write bitmask.
  398. unsigned GetStencilWriteMask() const { return stencilWriteMask_; }
  399. /// Return whether a custom clipping plane is in use.
  400. bool GetUseClipPlane() const { return useClipPlane_; }
  401. /// Return shader cache directory, Direct3D only.
  402. const String& GetShaderCacheDir() const { return shaderCacheDir_; }
  403. /// Return current rendertarget width and height.
  404. IntVector2 GetRenderTargetDimensions() const;
  405. /// Window was resized through user interaction. Called by Input subsystem.
  406. void OnWindowResized();
  407. /// Window was moved through user interaction. Called by Input subsystem.
  408. void OnWindowMoved();
  409. /// Restore GPU objects and reinitialize state. Requires an open window. Used only on OpenGL.
  410. void Restore();
  411. /// Maximize the window.
  412. void Maximize();
  413. /// Minimize the window.
  414. void Minimize();
  415. /// Add a GPU object to keep track of. Called by GPUObject.
  416. void AddGPUObject(GPUObject* object);
  417. /// Remove a GPU object. Called by GPUObject.
  418. void RemoveGPUObject(GPUObject* object);
  419. /// Reserve a CPU-side scratch buffer.
  420. void* ReserveScratchBuffer(unsigned size);
  421. /// Free a CPU-side scratch buffer.
  422. void FreeScratchBuffer(void* buffer);
  423. /// Clean up too large scratch buffers.
  424. void CleanupScratchBuffers();
  425. /// Clean up shader parameters when a shader variation is released or destroyed.
  426. void CleanupShaderPrograms(ShaderVariation* variation);
  427. /// Clean up a render surface from all FBOs. Used only on OpenGL.
  428. void CleanupRenderSurface(RenderSurface* surface);
  429. /// Get or create a constant buffer. Will be shared between shaders if possible.
  430. ConstantBuffer* GetOrCreateConstantBuffer(ShaderType type, unsigned index, unsigned size);
  431. /// Mark the FBO needing an update. Used only on OpenGL.
  432. void MarkFBODirty();
  433. /// Bind a VBO, avoiding redundant operation. Used only on OpenGL.
  434. void SetVBO(unsigned object);
  435. /// Bind a UBO, avoiding redundant operation. Used only on OpenGL.
  436. void SetUBO(unsigned object);
  437. /// Return the API-specific alpha texture format.
  438. static unsigned GetAlphaFormat();
  439. /// Return the API-specific luminance texture format.
  440. static unsigned GetLuminanceFormat();
  441. /// Return the API-specific luminance alpha texture format.
  442. static unsigned GetLuminanceAlphaFormat();
  443. /// Return the API-specific RGB texture format.
  444. static unsigned GetRGBFormat();
  445. /// Return the API-specific RGBA texture format.
  446. static unsigned GetRGBAFormat();
  447. /// Return the API-specific RGBA 16-bit texture format.
  448. static unsigned GetRGBA16Format();
  449. /// Return the API-specific RGBA 16-bit float texture format.
  450. static unsigned GetRGBAFloat16Format();
  451. /// Return the API-specific RGBA 32-bit float texture format.
  452. static unsigned GetRGBAFloat32Format();
  453. /// Return the API-specific RG 16-bit texture format.
  454. static unsigned GetRG16Format();
  455. /// Return the API-specific RG 16-bit float texture format.
  456. static unsigned GetRGFloat16Format();
  457. /// Return the API-specific RG 32-bit float texture format.
  458. static unsigned GetRGFloat32Format();
  459. /// Return the API-specific single channel 16-bit float texture format.
  460. static unsigned GetFloat16Format();
  461. /// Return the API-specific single channel 32-bit float texture format.
  462. static unsigned GetFloat32Format();
  463. /// Return the API-specific linear depth texture format.
  464. static unsigned GetLinearDepthFormat();
  465. /// Return the API-specific hardware depth-stencil texture format.
  466. static unsigned GetDepthStencilFormat();
  467. /// Return the API-specific readable hardware depth format, or 0 if not supported.
  468. static unsigned GetReadableDepthFormat();
  469. /// Return the API-specific texture format from a textual description, for example "rgb".
  470. static unsigned GetFormat(const String& formatName);
  471. /// Return UV offset required for pixel perfect rendering.
  472. static const Vector2& GetPixelUVOffset() { return pixelUVOffset; }
  473. /// Return maximum number of supported bones for skinning.
  474. static unsigned GetMaxBones();
  475. /// Return whether is using an OpenGL 3 context. Return always false on Direct3D9 & Direct3D11.
  476. static bool GetGL3Support();
  477. // ATOMIC BEGIN
  478. /// Get the SDL_Window as a void* to avoid having to include the graphics implementation
  479. void* GetSDLWindow() { return window_; }
  480. int GetCurrentMonitor();
  481. int GetNumMonitors();
  482. bool GetMaximized();
  483. IntVector2 GetMonitorResolution(int monitorId) const;
  484. void RaiseWindow();
  485. /// Return number of passes drawn this frame
  486. static unsigned GetNumPasses() { return numPasses_; }
  487. /// Set number of passes drawn this frame
  488. static void SetNumPasses(unsigned value) { numPasses_ = value; }
  489. /// Return number of single render pass primitives drawn this frame (D3D9 Only)
  490. static unsigned GetSinglePassPrimitives() { return numSinglePassPrimitives_; }
  491. /// Set number of single render pass primitives drawn this frame (D3D9 Only)
  492. static void SetSinglePassPrimitives(unsigned value) { numSinglePassPrimitives_ = value; }
  493. // ATOMIC END
  494. private:
  495. /// Create the application window.
  496. bool OpenWindow(int width, int height, bool resizable, bool borderless);
  497. /// Create the application window icon.
  498. void CreateWindowIcon();
  499. /// Adjust the window for new resolution and fullscreen mode.
  500. void AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless, int& monitor);
  501. /// Create the Direct3D11 device and swap chain. Requires an open window. Can also be called again to recreate swap chain. Return true on success.
  502. bool CreateDevice(int width, int height, int multiSample);
  503. /// Update Direct3D11 swap chain state for a new mode and create views for the backbuffer & default depth buffer. Return true on success.
  504. bool UpdateSwapChain(int width, int height);
  505. /// Create the Direct3D9 interface.
  506. bool CreateInterface();
  507. /// Create the Direct3D9 device.
  508. bool CreateDevice(unsigned adapter, unsigned deviceType);
  509. /// Reset the Direct3D9 device.
  510. void ResetDevice();
  511. /// Notify all GPU resources so they can release themselves as needed. Used only on Direct3D9.
  512. void OnDeviceLost();
  513. /// Notify all GPU resources so they can recreate themselves as needed. Used only on Direct3D9.
  514. void OnDeviceReset();
  515. /// Set vertex buffer stream frequency. Used only on Direct3D9.
  516. void SetStreamFrequency(unsigned index, unsigned frequency);
  517. /// Reset stream frequencies. Used only on Direct3D9.
  518. void ResetStreamFrequencies();
  519. /// Check supported rendering features.
  520. void CheckFeatureSupport();
  521. /// Reset cached rendering state.
  522. void ResetCachedState();
  523. /// Initialize texture unit mappings.
  524. void SetTextureUnitMappings();
  525. /// Process dirtied state before draw.
  526. void PrepareDraw();
  527. /// Create intermediate texture for multisampled backbuffer resolve. No-op if already exists.
  528. void CreateResolveTexture();
  529. /// Clean up all framebuffers. Called when destroying the context. Used only on OpenGL.
  530. void CleanupFramebuffers();
  531. /// Create a framebuffer using either extension or core functionality. Used only on OpenGL.
  532. unsigned CreateFramebuffer();
  533. /// Delete a framebuffer using either extension or core functionality. Used only on OpenGL.
  534. void DeleteFramebuffer(unsigned fbo);
  535. /// Bind a framebuffer using either extension or core functionality. Used only on OpenGL.
  536. void BindFramebuffer(unsigned fbo);
  537. /// Bind a framebuffer color attachment using either extension or core functionality. Used only on OpenGL.
  538. void BindColorAttachment(unsigned index, unsigned target, unsigned object, bool isRenderBuffer);
  539. /// Bind a framebuffer depth attachment using either extension or core functionality. Used only on OpenGL.
  540. void BindDepthAttachment(unsigned object, bool isRenderBuffer);
  541. /// Bind a framebuffer stencil attachment using either extension or core functionality. Used only on OpenGL.
  542. void BindStencilAttachment(unsigned object, bool isRenderBuffer);
  543. /// Check FBO completeness using either extension or core functionality. Used only on OpenGL.
  544. bool CheckFramebuffer();
  545. /// Set vertex attrib divisor. No-op if unsupported. Used only on OpenGL.
  546. void SetVertexAttribDivisor(unsigned location, unsigned divisor);
  547. /// Release/clear GPU objects and optionally close the window. Used only on OpenGL.
  548. void Release(bool clearGPUObjects, bool closeWindow);
  549. /// Mutex for accessing the GPU objects vector from several threads.
  550. Mutex gpuObjectMutex_;
  551. /// Implementation.
  552. GraphicsImpl* impl_;
  553. /// SDL window.
  554. SDL_Window* window_;
  555. /// Window title.
  556. String windowTitle_;
  557. /// Window icon image.
  558. WeakPtr<Image> windowIcon_;
  559. /// External window, null if not in use (default.)
  560. void* externalWindow_;
  561. /// Window width in pixels.
  562. int width_;
  563. /// Window height in pixels.
  564. int height_;
  565. /// Window position.
  566. IntVector2 position_;
  567. /// Multisampling mode.
  568. int multiSample_;
  569. /// Fullscreen flag.
  570. bool fullscreen_;
  571. /// Borderless flag.
  572. bool borderless_;
  573. /// Resizable flag.
  574. bool resizable_;
  575. /// High DPI flag.
  576. bool highDPI_;
  577. /// Vertical sync flag.
  578. bool vsync_;
  579. /// Refresh rate in Hz. Only used in fullscreen, 0 when windowed
  580. int refreshRate_;
  581. /// Monitor index. Only used in fullscreen, 0 when windowed
  582. int monitor_;
  583. /// Triple buffering flag.
  584. bool tripleBuffer_;
  585. /// Flush GPU command buffer flag.
  586. bool flushGPU_;
  587. /// Force OpenGL 2 flag. Only used on OpenGL.
  588. bool forceGL2_;
  589. /// sRGB conversion on write flag for the main window.
  590. bool sRGB_;
  591. /// Light pre-pass rendering support flag.
  592. bool lightPrepassSupport_;
  593. /// Deferred rendering support flag.
  594. bool deferredSupport_;
  595. /// Anisotropic filtering support flag.
  596. bool anisotropySupport_;
  597. /// DXT format support flag.
  598. bool dxtTextureSupport_;
  599. /// ETC1 format support flag.
  600. bool etcTextureSupport_;
  601. /// PVRTC formats support flag.
  602. bool pvrtcTextureSupport_;
  603. /// Hardware shadow map depth compare support flag.
  604. bool hardwareShadowSupport_;
  605. /// Instancing support flag.
  606. bool instancingSupport_;
  607. /// sRGB conversion on read support flag.
  608. bool sRGBSupport_;
  609. /// sRGB conversion on write support flag.
  610. bool sRGBWriteSupport_;
  611. /// Number of primitives this frame.
  612. unsigned numPrimitives_;
  613. /// Number of batches this frame.
  614. unsigned numBatches_;
  615. /// Largest scratch buffer request this frame.
  616. unsigned maxScratchBufferRequest_;
  617. /// GPU objects.
  618. PODVector<GPUObject*> gpuObjects_;
  619. /// Scratch buffers.
  620. Vector<ScratchBuffer> scratchBuffers_;
  621. /// Shadow map dummy color texture format.
  622. unsigned dummyColorFormat_;
  623. /// Shadow map depth texture format.
  624. unsigned shadowMapFormat_;
  625. /// Shadow map 24-bit depth texture format.
  626. unsigned hiresShadowMapFormat_;
  627. /// Vertex buffers in use.
  628. VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS];
  629. /// Index buffer in use.
  630. IndexBuffer* indexBuffer_;
  631. /// Current vertex declaration hash.
  632. unsigned long long vertexDeclarationHash_;
  633. /// Current primitive type.
  634. unsigned primitiveType_;
  635. /// Vertex shader in use.
  636. ShaderVariation* vertexShader_;
  637. /// Pixel shader in use.
  638. ShaderVariation* pixelShader_;
  639. /// Textures in use.
  640. Texture* textures_[MAX_TEXTURE_UNITS];
  641. /// Texture unit mappings.
  642. HashMap<String, TextureUnit> textureUnits_;
  643. /// Rendertargets in use.
  644. RenderSurface* renderTargets_[MAX_RENDERTARGETS];
  645. /// Depth-stencil surface in use.
  646. RenderSurface* depthStencil_;
  647. /// Viewport coordinates.
  648. IntRect viewport_;
  649. /// Default texture filtering mode.
  650. TextureFilterMode defaultTextureFilterMode_;
  651. /// Default texture max. anisotropy level.
  652. unsigned defaultTextureAnisotropy_;
  653. /// Blending mode.
  654. BlendMode blendMode_;
  655. /// Alpha-to-coverage enable.
  656. bool alphaToCoverage_;
  657. /// Color write enable.
  658. bool colorWrite_;
  659. /// Hardware culling mode.
  660. CullMode cullMode_;
  661. /// Depth constant bias.
  662. float constantDepthBias_;
  663. /// Depth slope scaled bias.
  664. float slopeScaledDepthBias_;
  665. /// Depth compare mode.
  666. CompareMode depthTestMode_;
  667. /// Depth write enable flag.
  668. bool depthWrite_;
  669. /// Line antialiasing enable flag.
  670. bool lineAntiAlias_;
  671. /// Polygon fill mode.
  672. FillMode fillMode_;
  673. /// Scissor test enable flag.
  674. bool scissorTest_;
  675. /// Scissor test rectangle.
  676. IntRect scissorRect_;
  677. /// Stencil test compare mode.
  678. CompareMode stencilTestMode_;
  679. /// Stencil operation on pass.
  680. StencilOp stencilPass_;
  681. /// Stencil operation on fail.
  682. StencilOp stencilFail_;
  683. /// Stencil operation on depth fail.
  684. StencilOp stencilZFail_;
  685. /// Stencil test reference value.
  686. unsigned stencilRef_;
  687. /// Stencil compare bitmask.
  688. unsigned stencilCompareMask_;
  689. /// Stencil write bitmask.
  690. unsigned stencilWriteMask_;
  691. /// Current custom clip plane in post-projection space.
  692. Vector4 clipPlane_;
  693. /// Stencil test enable flag.
  694. bool stencilTest_;
  695. /// Custom clip plane enable flag.
  696. bool useClipPlane_;
  697. /// Remembered shader parameter sources.
  698. const void* shaderParameterSources_[MAX_SHADER_PARAMETER_GROUPS];
  699. /// Base directory for shaders.
  700. String shaderPath_;
  701. /// Cache directory for Direct3D binary shaders.
  702. String shaderCacheDir_;
  703. /// File extension for shaders.
  704. String shaderExtension_;
  705. /// Last used shader in shader variation query.
  706. mutable WeakPtr<Shader> lastShader_;
  707. /// Last used shader name in shader variation query.
  708. mutable String lastShaderName_;
  709. /// Shader precache utility.
  710. SharedPtr<ShaderPrecache> shaderPrecache_;
  711. /// Allowed screen orientations.
  712. String orientations_;
  713. /// Graphics API name.
  714. String apiName_;
  715. /// Pixel perfect UV offset.
  716. static const Vector2 pixelUVOffset;
  717. /// OpenGL3 support flag.
  718. static bool gl3Support;
  719. // ATOMIC BEGIN
  720. static unsigned numPasses_;
  721. static unsigned numSinglePassPrimitives_;
  722. // ATOMIC END
  723. };
  724. /// Register Graphics library objects.
  725. void ATOMIC_API RegisterGraphicsLibrary(Context* context);
  726. }