OGLGraphics.h 30 KB

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