OGLGraphics.h 30 KB

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