OGLGraphics.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Core/Mutex.h"
  25. #include "../../Core/Object.h"
  26. #include "../../Graphics/GraphicsDefs.h"
  27. #include "../../Math/Color.h"
  28. #include "../../Math/Plane.h"
  29. #include "../../Math/Rect.h"
  30. #include "../../Resource/Image.h"
  31. namespace Atomic
  32. {
  33. class ConstantBuffer;
  34. class File;
  35. class Image;
  36. class IndexBuffer;
  37. class GPUObject;
  38. class GraphicsImpl;
  39. class RenderSurface;
  40. class Shader;
  41. class ShaderPrecache;
  42. class ShaderProgram;
  43. class ShaderVariation;
  44. class Texture;
  45. class Texture2D;
  46. class TextureCube;
  47. class Vector3;
  48. class Vector4;
  49. class VertexBuffer;
  50. typedef HashMap<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram> > ShaderProgramMap;
  51. static const unsigned NUM_SCREEN_BUFFERS = 2;
  52. /// CPU-side scratch buffer for vertex data updates.
  53. struct ScratchBuffer
  54. {
  55. ScratchBuffer() :
  56. size_(0),
  57. reserved_(false)
  58. {
  59. }
  60. /// Buffer data.
  61. SharedArrayPtr<unsigned char> data_;
  62. /// Data size.
  63. unsigned size_;
  64. /// Reserved flag.
  65. bool reserved_;
  66. };
  67. /// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
  68. class ATOMIC_API Graphics : public Object
  69. {
  70. OBJECT(Graphics);
  71. public:
  72. /// Construct.
  73. Graphics(Context* context_);
  74. /// Destruct. Release the OpenGL context and close the window.
  75. virtual ~Graphics();
  76. /// Set external window handle. Only effective before setting the initial screen mode. On Windows it is necessary to set up OpenGL pixel format manually for the window.
  77. void SetExternalWindow(void* window);
  78. /// Set window icon.
  79. void SetWindowIcon(Image* windowIcon);
  80. /// Set window title.
  81. void SetWindowTitle(const String& windowTitle);
  82. /// Set window position.
  83. void SetWindowPosition(const IntVector2& position);
  84. /// Set window position.
  85. void SetWindowPosition(int x, int y);
  86. /// Set window size.
  87. void SetWindowSize(int width, int height);
  88. /// Center window.
  89. void CenterWindow();
  90. /// Bring the window to front with focus
  91. void RaiseWindow();
  92. /// Get the SDL_Window as a void* to avoid having to include the graphics implementation
  93. void* GetSDLWindow();
  94. /// Set screen mode. Return true if successful.
  95. bool SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool vsync, bool tripleBuffer, int multiSample, bool maximize);
  96. /// Set screen resolution only. Return true if successful.
  97. bool SetMode(int width, int height);
  98. /// Set whether the main window uses sRGB conversion on write.
  99. void SetSRGB(bool enable);
  100. /// Set whether to flush the GPU command buffer to prevent multiple frames being queued and uneven frame timesteps. Not yet 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.
  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);
  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. /// Draw non-indexed geometry.
  121. void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
  122. /// Draw indexed geometry.
  123. void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
  124. /// Draw indexed, instanced geometry.
  125. void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
  126. unsigned instanceCount);
  127. /// Set vertex buffer.
  128. void SetVertexBuffer(VertexBuffer* buffer);
  129. /// Set multiple vertex buffers.
  130. bool SetVertexBuffers
  131. (const PODVector<VertexBuffer*>& buffers, const PODVector<unsigned>& elementMasks, unsigned instanceOffset = 0);
  132. /// Set multiple vertex buffers.
  133. bool SetVertexBuffers
  134. (const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>& elementMasks, unsigned instanceOffset = 0);
  135. /// Set index buffer.
  136. void SetIndexBuffer(IndexBuffer* buffer);
  137. /// Set shaders.
  138. void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
  139. /// Set shader float constants.
  140. void SetShaderParameter(StringHash param, const float* data, unsigned count);
  141. /// Set shader float constant.
  142. void SetShaderParameter(StringHash param, float value);
  143. /// Set shader color constant.
  144. void SetShaderParameter(StringHash param, const Color& color);
  145. /// Set shader 2D vector constant.
  146. void SetShaderParameter(StringHash param, const Vector2& vector);
  147. /// Set shader 3x3 matrix constant.
  148. void SetShaderParameter(StringHash param, const Matrix3& matrix);
  149. /// Set shader 3D vector constant.
  150. void SetShaderParameter(StringHash param, const Vector3& vector);
  151. /// Set shader 4x4 matrix constant.
  152. void SetShaderParameter(StringHash param, const Matrix4& matrix);
  153. /// Set shader 4D vector constant.
  154. void SetShaderParameter(StringHash param, const Vector4& vector);
  155. /// Set shader 4x3 matrix constant.
  156. void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
  157. /// Set shader constant from a variant. Supported variant types: bool, float, vector2, vector3, vector4, color.
  158. void SetShaderParameter(StringHash param, const Variant& value);
  159. /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
  160. bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
  161. /// Check whether a shader parameter exists on the currently set shaders.
  162. bool HasShaderParameter(StringHash param);
  163. /// Check whether the current shader program uses a texture unit.
  164. bool HasTextureUnit(TextureUnit unit);
  165. /// Clear remembered shader parameter source group.
  166. void ClearParameterSource(ShaderParameterGroup group);
  167. /// Clear remembered shader parameter sources.
  168. void ClearParameterSources();
  169. /// Clear remembered transform shader parameter sources.
  170. void ClearTransformSources();
  171. /// Set texture.
  172. void SetTexture(unsigned index, Texture* texture);
  173. /// Bind texture unit 0 for update. Called by Texture.
  174. void SetTextureForUpdate(Texture* texture);
  175. /// Set default texture filtering mode.
  176. void SetDefaultTextureFilterMode(TextureFilterMode mode);
  177. /// Set texture anisotropy.
  178. void SetTextureAnisotropy(unsigned level);
  179. /// Dirty texture parameters of all textures (when global settings change.)
  180. void SetTextureParametersDirty();
  181. /// Reset all rendertargets, depth-stencil surface and viewport.
  182. void ResetRenderTargets();
  183. /// Reset specific rendertarget.
  184. void ResetRenderTarget(unsigned index);
  185. /// Reset depth-stencil surface.
  186. void ResetDepthStencil();
  187. /// Set rendertarget.
  188. void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
  189. /// Set rendertarget.
  190. void SetRenderTarget(unsigned index, Texture2D* texture);
  191. /// Set depth-stencil surface.
  192. void SetDepthStencil(RenderSurface* depthStencil);
  193. /// Set depth-stencil surface.
  194. void SetDepthStencil(Texture2D* texture);
  195. /// Set viewport.
  196. void SetViewport(const IntRect& rect);
  197. /// Set blending mode.
  198. void SetBlendMode(BlendMode mode);
  199. /// Set color write on/off.
  200. void SetColorWrite(bool enable);
  201. /// Set hardware culling mode.
  202. void SetCullMode(CullMode mode);
  203. /// Set depth bias.
  204. void SetDepthBias(float constantBias, float slopeScaledBias);
  205. /// Set depth compare.
  206. void SetDepthTest(CompareMode mode);
  207. /// Set depth write on/off.
  208. void SetDepthWrite(bool enable);
  209. /// Set polygon fill mode.
  210. void SetFillMode(FillMode mode);
  211. /// Set scissor test.
  212. void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
  213. /// Set scissor test.
  214. void SetScissorTest(bool enable, const IntRect& rect);
  215. /// Set stencil test.
  216. void SetStencilTest
  217. (bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP,
  218. unsigned stencilRef = 0, unsigned compareMask = M_MAX_UNSIGNED, unsigned writeMask = M_MAX_UNSIGNED);
  219. /// Set a custom clipping plane. The plane is specified in world space, but is dependent on the view and projection matrices.
  220. void SetClipPlane(bool enable, const Plane& clipPlane = Plane::UP, const Matrix3x4& view = Matrix3x4::IDENTITY,
  221. const Matrix4& projection = Matrix4::IDENTITY);
  222. /// Begin dumping shader variation names to an XML file for precaching.
  223. void BeginDumpShaders(const String& fileName);
  224. /// End dumping shader variations names.
  225. void EndDumpShaders();
  226. /// Precache shader variations from an XML file generated with BeginDumpShaders().
  227. void PrecacheShaders(Deserializer& source);
  228. /// Return whether rendering initialized.
  229. bool IsInitialized() const;
  230. /// Return graphics implementation, which holds the actual API-specific resources.
  231. GraphicsImpl* GetImpl() const { return impl_; }
  232. /// Return OS-specific external window handle. Null if not in use.
  233. void* GetExternalWindow() const { return externalWindow_; }
  234. /// Return window title.
  235. const String& GetWindowTitle() const { return windowTitle_; }
  236. /// Return graphics API name.
  237. const String& GetApiName() const { return apiName_; }
  238. /// Return window position.
  239. IntVector2 GetWindowPosition() const;
  240. /// Return window width.
  241. int GetWidth() const { return width_; }
  242. /// Return window height.
  243. int GetHeight() const { return height_; }
  244. /// Return multisample mode (1 = no multisampling.)
  245. int GetMultiSample() const { return multiSample_; }
  246. /// Return whether window is fullscreen.
  247. bool GetFullscreen() const { return fullscreen_; }
  248. /// Return whether window is borderless.
  249. bool GetBorderless() const { return borderless_; }
  250. /// Return whether window is resizable.
  251. bool GetResizable() const { return resizable_; }
  252. /// Return whether vertical sync is on.
  253. bool GetVSync() const { return vsync_; }
  254. /// Return whether triple buffering is enabled.
  255. bool GetTripleBuffer() const { return tripleBuffer_; }
  256. /// Return whether the main window is using sRGB conversion on write.
  257. bool GetSRGB() const { return sRGB_; }
  258. /// Return whether the GPU command buffer is flushed each frame. Not yet implemented on OpenGL.
  259. bool GetFlushGPU() const { return false; }
  260. /// Return whether OpenGL 2 use is forced.
  261. bool GetForceGL2() const { return forceGL2_; }
  262. /// Return allowed screen orientations.
  263. const String& GetOrientations() const { return orientations_; }
  264. /// Return whether device is lost, and can not yet render.
  265. bool IsDeviceLost() const;
  266. /// Return number of primitives drawn this frame.
  267. unsigned GetNumPrimitives() const { return numPrimitives_; }
  268. /// Return number of batches drawn this frame.
  269. unsigned GetNumBatches() const { return numBatches_; }
  270. /// Return dummy color texture format for shadow maps. 0 if not needed, may be nonzero on OS X to work around an Intel driver issue.
  271. unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
  272. /// Return shadow map depth texture format, or 0 if not supported.
  273. unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
  274. /// Return 24-bit shadow map depth texture format, or 0 if not supported.
  275. unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
  276. /// Return whether hardware instancing is supported.
  277. bool GetInstancingSupport() const { return instancingSupport_; }
  278. /// Return whether light pre-pass rendering is supported.
  279. bool GetLightPrepassSupport() const { return lightPrepassSupport_; }
  280. /// Return whether deferred rendering is supported.
  281. bool GetDeferredSupport() const { return deferredSupport_; }
  282. /// Return whether anisotropic texture filtering is supported.
  283. bool GetAnisotropySupport() const { return anisotropySupport_; }
  284. /// Return whether shadow map depth compare is done in hardware. Always true on OpenGL.
  285. bool GetHardwareShadowSupport() const { return true; }
  286. /// Return whether a readable hardware depth format is available.
  287. bool GetReadableDepthSupport() const { return GetReadableDepthFormat() != 0; }
  288. /// Return whether sRGB conversion on texture sampling is supported.
  289. bool GetSRGBSupport() const { return sRGBSupport_; }
  290. /// Return whether sRGB conversion on rendertarget writing is supported.
  291. bool GetSRGBWriteSupport() const { return sRGBWriteSupport_; }
  292. /// Return supported fullscreen resolutions.
  293. PODVector<IntVector2> GetResolutions() const;
  294. /// Return supported multisampling levels.
  295. PODVector<int> GetMultiSampleLevels() const;
  296. /// Return the desktop resolution.
  297. IntVector2 GetDesktopResolution() const;
  298. /// Return hardware format for a compressed image format, or 0 if unsupported.
  299. unsigned GetFormat(CompressedFormat format) const;
  300. /// Return a shader variation by name and defines.
  301. ShaderVariation* GetShader(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
  302. /// Return a shader variation by name and defines.
  303. ShaderVariation* GetShader(ShaderType type, const char* name, const char* defines) const;
  304. /// Return current vertex buffer by index.
  305. VertexBuffer* GetVertexBuffer(unsigned index) const;
  306. /// Return index buffer.
  307. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  308. /// Return vertex shader.
  309. ShaderVariation* GetVertexShader() const { return vertexShader_; }
  310. /// Return pixel shader.
  311. ShaderVariation* GetPixelShader() const { return pixelShader_; }
  312. /// Return shader program.
  313. ShaderProgram* GetShaderProgram() const { return shaderProgram_; }
  314. /// Return texture unit index by name.
  315. TextureUnit GetTextureUnit(const String& name);
  316. /// Return texture unit name by index.
  317. const String& GetTextureUnitName(TextureUnit unit);
  318. /// Return texture by texture unit index.
  319. Texture* GetTexture(unsigned index) const;
  320. /// Return default texture filtering mode.
  321. TextureFilterMode GetDefaultTextureFilterMode() const { return defaultTextureFilterMode_; }
  322. /// Return rendertarget by index.
  323. RenderSurface* GetRenderTarget(unsigned index) const;
  324. /// Return depth-stencil surface.
  325. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  326. /// Return readable depth-stencil texture. Not created automatically on OpenGL.
  327. Texture2D* GetDepthTexture() const { return 0; }
  328. /// Return the viewport coordinates.
  329. IntRect GetViewport() const { return viewport_; }
  330. /// Return texture anisotropy.
  331. unsigned GetTextureAnisotropy() const { return textureAnisotropy_; }
  332. /// Return blending mode.
  333. BlendMode GetBlendMode() const { return blendMode_; }
  334. /// Return whether color write is enabled.
  335. bool GetColorWrite() const { return colorWrite_; }
  336. /// Return hardware culling mode.
  337. CullMode GetCullMode() const { return cullMode_; }
  338. /// Return depth constant bias.
  339. float GetDepthConstantBias() const { return constantDepthBias_; }
  340. /// Return depth slope scaled bias.
  341. float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
  342. /// Return depth compare mode.
  343. CompareMode GetDepthTest() const { return depthTestMode_; }
  344. /// Return whether depth write is enabled.
  345. bool GetDepthWrite() const { return depthWrite_; }
  346. /// Return polygon fill mode.
  347. FillMode GetFillMode() const { return fillMode_; }
  348. /// Return whether stencil test is enabled.
  349. bool GetStencilTest() const { return stencilTest_; }
  350. /// Return whether scissor test is enabled.
  351. bool GetScissorTest() const { return scissorTest_; }
  352. /// Return scissor rectangle coordinates.
  353. const IntRect& GetScissorRect() const { return scissorRect_; }
  354. /// Return stencil compare mode.
  355. CompareMode GetStencilTestMode() const { return stencilTestMode_; }
  356. /// Return stencil operation to do if stencil test passes.
  357. StencilOp GetStencilPass() const { return stencilPass_; }
  358. /// Return stencil operation to do if stencil test fails.
  359. StencilOp GetStencilFail() const { return stencilFail_; }
  360. /// Return stencil operation to do if depth compare fails.
  361. StencilOp GetStencilZFail() const { return stencilZFail_; }
  362. /// Return stencil reference value.
  363. unsigned GetStencilRef() const { return stencilRef_; }
  364. /// Return stencil compare bitmask.
  365. unsigned GetStencilCompareMask() const { return stencilCompareMask_; }
  366. /// Return stencil write bitmask.
  367. unsigned GetStencilWriteMask() const { return stencilWriteMask_; }
  368. /// Return whether a custom clipping plane is in use.
  369. bool GetUseClipPlane() const { return useClipPlane_; }
  370. /// Return rendertarget width and height.
  371. IntVector2 GetRenderTargetDimensions() const;
  372. /// Window was resized through user interaction. Called by Input subsystem.
  373. void WindowResized();
  374. /// Window was moved through user interaction. Called by Input subsystem.
  375. void WindowMoved();
  376. /// Add a GPU object to keep track of. Called by GPUObject.
  377. void AddGPUObject(GPUObject* object);
  378. /// Remove a GPU object. Called by GPUObject.
  379. void RemoveGPUObject(GPUObject* object);
  380. /// Reserve a CPU-side scratch buffer.
  381. void* ReserveScratchBuffer(unsigned size);
  382. /// Free a CPU-side scratch buffer.
  383. void FreeScratchBuffer(void* buffer);
  384. /// Clean up too large scratch buffers.
  385. void CleanupScratchBuffers();
  386. /// Clean up a render surface from all FBOs.
  387. void CleanupRenderSurface(RenderSurface* surface);
  388. /// Clean up shader programs when a shader variation is released or destroyed.
  389. void CleanupShaderPrograms(ShaderVariation* variation);
  390. /// Reserve a constant buffer.
  391. ConstantBuffer* GetOrCreateConstantBuffer(unsigned bindingIndex, unsigned size);
  392. /// Release/clear GPU objects and optionally close the window.
  393. void Release(bool clearGPUObjects, bool closeWindow);
  394. /// Restore GPU objects and reinitialize state. Requires an open window.
  395. void Restore();
  396. /// Maximize the Window.
  397. void Maximize();
  398. /// Minimize the Window.
  399. void Minimize();
  400. /// Mark the FBO needing an update.
  401. void MarkFBODirty();
  402. /// Bind a VBO, avoiding redundant operation.
  403. void SetVBO(unsigned object);
  404. /// Bind a UBO, avoiding redundant operation.
  405. void SetUBO(unsigned object);
  406. /// Return the API-specific alpha texture format.
  407. static unsigned GetAlphaFormat();
  408. /// Return the API-specific luminance texture format.
  409. static unsigned GetLuminanceFormat();
  410. /// Return the API-specific luminance alpha texture format.
  411. static unsigned GetLuminanceAlphaFormat();
  412. /// Return the API-specific RGB texture format.
  413. static unsigned GetRGBFormat();
  414. /// Return the API-specific RGBA texture format.
  415. static unsigned GetRGBAFormat();
  416. /// Return the API-specific RGBA 16-bit texture format.
  417. static unsigned GetRGBA16Format();
  418. /// Return the API-specific RGBA 16-bit float texture format.
  419. static unsigned GetRGBAFloat16Format();
  420. /// Return the API-specific RGBA 32-bit float texture format.
  421. static unsigned GetRGBAFloat32Format();
  422. /// Return the API-specific RG 16-bit texture format.
  423. static unsigned GetRG16Format();
  424. /// Return the API-specific RG 16-bit float texture format.
  425. static unsigned GetRGFloat16Format();
  426. /// Return the API-specific RG 32-bit float texture format.
  427. static unsigned GetRGFloat32Format();
  428. /// Return the API-specific single channel 16-bit float texture format.
  429. static unsigned GetFloat16Format();
  430. /// Return the API-specific single channel 32-bit float texture format.
  431. static unsigned GetFloat32Format();
  432. /// Return the API-specific linear depth texture format.
  433. static unsigned GetLinearDepthFormat();
  434. /// Return the API-specific hardware depth-stencil texture format.
  435. static unsigned GetDepthStencilFormat();
  436. /// Return the API-specific readable hardware depth format, or 0 if not supported.
  437. static unsigned GetReadableDepthFormat();
  438. /// Return the API-specific texture format from a textual description, for example "rgb".
  439. static unsigned GetFormat(const String& formatName);
  440. /// Return UV offset required for pixel perfect rendering.
  441. static const Vector2& GetPixelUVOffset() { return pixelUVOffset; }
  442. /// Return maximum number of supported bones for skinning.
  443. static unsigned GetMaxBones();
  444. /// Return whether is using an OpenGL 3 context.
  445. static bool GetGL3Support() { return gl3Support; }
  446. // ATOMIC BEGIN
  447. /// Return the current monitor number
  448. int GetCurrentMonitor();
  449. /// Return the available monitors number
  450. int GetNumMonitors();
  451. /// Return true if window is maximized
  452. bool GetMaximized();
  453. /// Return monitor resolution
  454. IntVector2 GetMonitorResolution(int monitorId) const;
  455. // ATOMIC END
  456. private:
  457. /// Create the application window icon.
  458. void CreateWindowIcon();
  459. /// Check supported rendering features.
  460. void CheckFeatureSupport();
  461. /// Prepare for draw call. Update constant buffers and setup the FBO.
  462. void PrepareDraw();
  463. /// Clean up all framebuffers. Called when destroying the context.
  464. void CleanupFramebuffers();
  465. /// Reset cached rendering state.
  466. void ResetCachedState();
  467. /// Initialize texture unit mappings.
  468. void SetTextureUnitMappings();
  469. /// Create a framebuffer using either extension or core functionality.
  470. unsigned CreateFramebuffer();
  471. /// Delete a framebuffer using either extension or core functionality.
  472. void DeleteFramebuffer(unsigned fbo);
  473. /// Bind a framebuffer using either extension or core functionality.
  474. void BindFramebuffer(unsigned fbo);
  475. /// Bind a framebuffer color attachment using either extension or core functionality.
  476. void BindColorAttachment(unsigned index, unsigned target, unsigned object);
  477. /// Bind a framebuffer depth attachment using either extension or core functionality.
  478. void BindDepthAttachment(unsigned object, bool isRenderBuffer);
  479. /// Bind a framebuffer stencil attachment using either extension or core functionality.
  480. void BindStencilAttachment(unsigned object, bool isRenderBuffer);
  481. /// Check FBO completeness using either extension or core functionality.
  482. bool CheckFramebuffer();
  483. /// Mutex for accessing the GPU objects vector from several threads.
  484. Mutex gpuObjectMutex_;
  485. /// Implementation.
  486. GraphicsImpl* impl_;
  487. /// Window title.
  488. String windowTitle_;
  489. /// Window Icon File Name
  490. Image* windowIcon_;
  491. /// External window, null if not in use (default.)
  492. void* externalWindow_;
  493. /// Window width.
  494. int width_;
  495. /// Window height.
  496. int height_;
  497. /// Window position.
  498. IntVector2 position_;
  499. /// Multisampling mode.
  500. int multiSample_;
  501. /// Fullscreen flag.
  502. bool fullscreen_;
  503. /// Borderless flag.
  504. bool borderless_;
  505. /// Resizable flag.
  506. bool resizable_;
  507. /// Vertical sync flag.
  508. bool vsync_;
  509. /// Triple buffering flag.
  510. bool tripleBuffer_;
  511. /// sRGB conversion on write flag for the main window.
  512. bool sRGB_;
  513. /// Force OpenGL 2 use flag.
  514. bool forceGL2_;
  515. /// Instancing support flag.
  516. bool instancingSupport_;
  517. /// Light prepass support flag.
  518. bool lightPrepassSupport_;
  519. /// Deferred rendering support flag.
  520. bool deferredSupport_;
  521. /// Anisotropic filtering support flag.
  522. bool anisotropySupport_;
  523. /// DXT format support flag.
  524. bool dxtTextureSupport_;
  525. /// ETC1 format support flag.
  526. bool etcTextureSupport_;
  527. /// PVRTC formats support flag.
  528. bool pvrtcTextureSupport_;
  529. /// sRGB conversion on read support flag.
  530. bool sRGBSupport_;
  531. /// sRGB conversion on write support flag.
  532. bool sRGBWriteSupport_;
  533. /// Number of primitives this frame.
  534. unsigned numPrimitives_;
  535. /// Number of batches this frame.
  536. unsigned numBatches_;
  537. /// Largest scratch buffer request this frame.
  538. unsigned maxScratchBufferRequest_;
  539. /// GPU objects.
  540. PODVector<GPUObject*> gpuObjects_;
  541. /// Scratch buffers.
  542. Vector<ScratchBuffer> scratchBuffers_;
  543. /// Shadow map dummy color texture format.
  544. unsigned dummyColorFormat_;
  545. /// Shadow map depth texture format.
  546. unsigned shadowMapFormat_;
  547. /// Shadow map 24-bit depth texture format.
  548. unsigned hiresShadowMapFormat_;
  549. /// Vertex buffers in use.
  550. VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS];
  551. /// Element mask in use.
  552. unsigned elementMasks_[MAX_VERTEX_STREAMS];
  553. /// Index buffer in use.
  554. IndexBuffer* indexBuffer_;
  555. /// Vertex shader in use.
  556. ShaderVariation* vertexShader_;
  557. /// Pixel shader in use.
  558. ShaderVariation* pixelShader_;
  559. /// Shader program in use.
  560. ShaderProgram* shaderProgram_;
  561. /// Linked shader programs.
  562. ShaderProgramMap shaderPrograms_;
  563. /// Textures in use.
  564. Texture* textures_[MAX_TEXTURE_UNITS];
  565. /// OpenGL texture types in use.
  566. unsigned textureTypes_[MAX_TEXTURE_UNITS];
  567. /// Texture unit mappings.
  568. HashMap<String, TextureUnit> textureUnits_;
  569. /// All constant buffers.
  570. HashMap<unsigned, SharedPtr<ConstantBuffer> > constantBuffers_;
  571. /// Currently bound constant buffers.
  572. ConstantBuffer* currentConstantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2];
  573. /// Dirty constant buffers.
  574. PODVector<ConstantBuffer*> dirtyConstantBuffers_;
  575. /// Rendertargets in use.
  576. RenderSurface* renderTargets_[MAX_RENDERTARGETS];
  577. /// Depth-stencil surface in use.
  578. RenderSurface* depthStencil_;
  579. /// Viewport coordinates.
  580. IntRect viewport_;
  581. /// Texture anisotropy level.
  582. unsigned textureAnisotropy_;
  583. /// Blending mode.
  584. BlendMode blendMode_;
  585. /// Color write enable.
  586. bool colorWrite_;
  587. /// Hardware culling mode.
  588. CullMode cullMode_;
  589. /// Depth constant bias.
  590. float constantDepthBias_;
  591. /// Depth slope scaled bias.
  592. float slopeScaledDepthBias_;
  593. /// Depth compare mode.
  594. CompareMode depthTestMode_;
  595. /// Depth write enable flag.
  596. bool depthWrite_;
  597. /// Polygon fill mode.
  598. FillMode fillMode_;
  599. /// Scissor test rectangle.
  600. IntRect scissorRect_;
  601. /// Scissor test enable flag.
  602. bool scissorTest_;
  603. /// Current custom clip plane in post-projection space.
  604. Vector4 clipPlane_;
  605. /// Stencil test compare mode.
  606. CompareMode stencilTestMode_;
  607. /// Stencil operation on pass.
  608. StencilOp stencilPass_;
  609. /// Stencil operation on fail.
  610. StencilOp stencilFail_;
  611. /// Stencil operation on depth fail.
  612. StencilOp stencilZFail_;
  613. /// Stencil test reference value.
  614. unsigned stencilRef_;
  615. /// Stencil compare bitmask.
  616. unsigned stencilCompareMask_;
  617. /// Stencil write bitmask.
  618. unsigned stencilWriteMask_;
  619. /// Stencil test enable flag.
  620. bool stencilTest_;
  621. /// Custom clip plane enable flag.
  622. bool useClipPlane_;
  623. /// Last used instance data offset.
  624. unsigned lastInstanceOffset_;
  625. /// Default texture filtering mode.
  626. TextureFilterMode defaultTextureFilterMode_;
  627. /// Map for additional depth textures, to emulate Direct3D9 ability to mix render texture and backbuffer rendering.
  628. HashMap<int, SharedPtr<Texture2D> > depthTextures_;
  629. /// Base directory for shaders.
  630. String shaderPath_;
  631. /// File extension for shaders.
  632. String shaderExtension_;
  633. /// Last used shader in shader variation query.
  634. mutable WeakPtr<Shader> lastShader_;
  635. /// Last used shader name in shader variation query.
  636. mutable String lastShaderName_;
  637. /// Shader precache utility.
  638. SharedPtr<ShaderPrecache> shaderPrecache_;
  639. /// Allowed screen orientations.
  640. String orientations_;
  641. /// Graphics API name.
  642. String apiName_;
  643. /// Pixel perfect UV offset.
  644. static const Vector2 pixelUVOffset;
  645. /// Flag for OpenGL 3 support.
  646. static bool gl3Support;
  647. };
  648. /// Register Graphics library objects.
  649. void ATOMIC_API RegisterGraphicsLibrary(Context* context_);
  650. }