OGLGraphics.h 30 KB

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