D3D9Graphics.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 "../../Container/HashSet.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. #include "../../Graphics/GraphicsDefs.h"
  32. namespace Atomic
  33. {
  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 ShaderVariation;
  43. class Texture;
  44. class Texture2D;
  45. class TextureCube;
  46. class Vector3;
  47. class Vector4;
  48. class VertexBuffer;
  49. class VertexDeclaration;
  50. struct ShaderParameter;
  51. /// CPU-side scratch buffer for vertex data updates.
  52. struct ScratchBuffer
  53. {
  54. ScratchBuffer() :
  55. size_(0),
  56. reserved_(false)
  57. {
  58. }
  59. /// Buffer data.
  60. SharedArrayPtr<unsigned char> data_;
  61. /// Data size.
  62. unsigned size_;
  63. /// Reserved flag.
  64. bool reserved_;
  65. };
  66. /// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
  67. class ATOMIC_API Graphics : public Object
  68. {
  69. OBJECT(Graphics);
  70. public:
  71. /// Construct.
  72. Graphics(Context* context);
  73. /// Destruct. Release the Direct3D9 device and close the window.
  74. virtual ~Graphics();
  75. /// Set external window handle. Only effective before setting the initial screen mode.
  76. void SetExternalWindow(void* window);
  77. /// Set window title.
  78. void SetWindowTitle(const String& windowTitle);
  79. /// Set window icon.
  80. void SetWindowIcon(Image* windowIcon);
  81. /// Set window position. Sets initial position if window is not created yet.
  82. void SetWindowPosition(const IntVector2& position);
  83. /// Set window position. Sets initial position if window is not created yet.
  84. void SetWindowPosition(int x, int y);
  85. /// Set window size.
  86. void SetWindowSize(int width, int height);
  87. /// Center window.
  88. void CenterWindow();
  89. /// Bring the window to front with focus
  90. void RaiseWindow();
  91. /// Set screen mode. Return true if successful.
  92. bool SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool vsync, bool tripleBuffer, int multiSample);
  93. /// Set screen resolution only. Return true if successful.
  94. bool SetMode(int width, int height);
  95. /// Set whether the main window uses sRGB conversion on write.
  96. void SetSRGB(bool enable);
  97. /// Set whether to flush the GPU command buffer to prevent multiple frames being queued and uneven frame timesteps. Default off, may decrease performance if enabled.
  98. void SetFlushGPU(bool enable);
  99. /// Set allowed screen orientations as a space-separated list of "LandscapeLeft", "LandscapeRight", "Portrait" and "PortraitUpsideDown". Affects currently only iOS platform.
  100. void SetOrientations(const String& orientations);
  101. /// Toggle between full screen and windowed mode. Return true if successful.
  102. bool ToggleFullscreen();
  103. /// Close the window.
  104. void Close();
  105. /// Take a screenshot. Return true if successful.
  106. bool TakeScreenShot(Image* destImage);
  107. /// Begin frame rendering. Return true if device available and can render.
  108. bool BeginFrame();
  109. /// End frame rendering and swap buffers.
  110. void EndFrame();
  111. /// Clear any or all of rendertarget, depth buffer and stencil buffer.
  112. void Clear(unsigned flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, unsigned stencil = 0);
  113. /// Resolve multisampled backbuffer to a texture rendertarget. The texture's size should match the viewport size.
  114. bool ResolveToTexture(Texture2D* destination, const IntRect& viewport);
  115. /// Draw non-indexed geometry.
  116. void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
  117. /// Draw indexed geometry.
  118. void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
  119. /// Draw indexed, instanced geometry. An instancing vertex buffer must be set.
  120. void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount, unsigned instanceCount);
  121. /// Set vertex buffer.
  122. void SetVertexBuffer(VertexBuffer* buffer);
  123. /// Set multiple vertex buffers.
  124. bool SetVertexBuffers(const PODVector<VertexBuffer*>& buffers, const PODVector<unsigned>& elementMasks, unsigned instanceOffset = 0);
  125. /// Set multiple vertex buffers.
  126. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>& elementMasks, unsigned instanceOffset = 0);
  127. /// Set index buffer.
  128. void SetIndexBuffer(IndexBuffer* buffer);
  129. /// Set shaders.
  130. void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
  131. /// Set shader float constants.
  132. void SetShaderParameter(StringHash param, const float* data, unsigned count);
  133. /// Set shader float constant.
  134. void SetShaderParameter(StringHash param, float value);
  135. /// Set shader boolean constant.
  136. void SetShaderParameter(StringHash param, bool value);
  137. /// Set shader color constant.
  138. void SetShaderParameter(StringHash param, const Color& color);
  139. /// Set shader 2D vector constant.
  140. void SetShaderParameter(StringHash param, const Vector2& vector);
  141. /// Set shader 3x3 matrix constant.
  142. void SetShaderParameter(StringHash param, const Matrix3& matrix);
  143. /// Set shader 3D vector constant.
  144. void SetShaderParameter(StringHash param, const Vector3& vector);
  145. /// Set shader 4x4 matrix constant.
  146. void SetShaderParameter(StringHash param, const Matrix4& matrix);
  147. /// Set shader 4D vector constant.
  148. void SetShaderParameter(StringHash param, const Vector4& vector);
  149. /// Set shader 3x4 matrix constant.
  150. void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
  151. /// Set shader constant from a variant. Supported variant types: bool, float, vector2, vector3, vector4, color.
  152. void SetShaderParameter(StringHash param, const Variant& value);
  153. /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
  154. bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
  155. /// Check whether a shader parameter exists on the currently set shaders.
  156. bool HasShaderParameter(StringHash param);
  157. /// Check whether the current pixel shader uses a texture unit.
  158. bool HasTextureUnit(TextureUnit unit);
  159. /// Clear remembered shader parameter source group.
  160. void ClearParameterSource(ShaderParameterGroup group);
  161. /// Clear remembered shader parameter sources.
  162. void ClearParameterSources();
  163. /// Clear remembered transform shader parameter sources.
  164. void ClearTransformSources();
  165. /// Set texture.
  166. void SetTexture(unsigned index, Texture* texture);
  167. /// Set default texture filtering mode.
  168. void SetDefaultTextureFilterMode(TextureFilterMode mode);
  169. /// Set texture anisotropy.
  170. void SetTextureAnisotropy(unsigned level);
  171. /// Reset all rendertargets, depth-stencil surface and viewport.
  172. void ResetRenderTargets();
  173. /// Reset specific rendertarget.
  174. void ResetRenderTarget(unsigned index);
  175. /// Reset depth-stencil surface.
  176. void ResetDepthStencil();
  177. /// Set rendertarget.
  178. void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
  179. /// Set rendertarget.
  180. void SetRenderTarget(unsigned index, Texture2D* texture);
  181. /// Set depth-stencil surface.
  182. void SetDepthStencil(RenderSurface* depthStencil);
  183. /// Set depth-stencil surface.
  184. void SetDepthStencil(Texture2D* texture);
  185. /// Set viewport.
  186. void SetViewport(const IntRect& rect);
  187. /// Set blending mode.
  188. void SetBlendMode(BlendMode mode);
  189. /// Set color write on/off.
  190. void SetColorWrite(bool enable);
  191. /// Set hardware culling mode.
  192. void SetCullMode(CullMode mode);
  193. /// Set depth bias.
  194. void SetDepthBias(float constantBias, float slopeScaledBias);
  195. /// Set depth compare.
  196. void SetDepthTest(CompareMode mode);
  197. /// Set depth write on/off.
  198. void SetDepthWrite(bool enable);
  199. /// Set antialiased drawing mode on/off. Default is on if the backbuffer is multisampled. Has no effect when backbuffer is not multisampled.
  200. void SetDrawAntialiased(bool enable);
  201. /// Set polygon fill mode.
  202. void SetFillMode(FillMode mode);
  203. /// Set scissor test.
  204. void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
  205. /// Set scissor test.
  206. void SetScissorTest(bool enable, const IntRect& rect);
  207. /// Set stencil test.
  208. 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);
  209. /// Set a custom clipping plane. The plane is specified in world space, but is dependent on the view and projection matrices.
  210. void SetClipPlane(bool enable, const Plane& clipPlane = Plane::UP, const Matrix3x4& view = Matrix3x4::IDENTITY, const Matrix4& projection = Matrix4::IDENTITY);
  211. /// Set vertex buffer stream frequency.
  212. void SetStreamFrequency(unsigned index, unsigned frequency);
  213. /// Reset stream frequencies.
  214. void ResetStreamFrequencies();
  215. /// Set force Shader Model 2 flag. Only effective before setting the initial screen mode.
  216. void SetForceSM2(bool enable);
  217. /// Begin dumping shader variation names to an XML file for precaching.
  218. void BeginDumpShaders(const String& fileName);
  219. /// End dumping shader variations names.
  220. void EndDumpShaders();
  221. /// Precache shader variations from an XML file generated with BeginDumpShaders().
  222. void PrecacheShaders(Deserializer& source);
  223. /// Return whether rendering initialized.
  224. bool IsInitialized() const;
  225. /// Return graphics implementation, which holds the actual API-specific resources.
  226. GraphicsImpl* GetImpl() const { return impl_; }
  227. /// Return OS-specific external window handle. Null if not in use.
  228. void* GetExternalWindow() const { return externalWindow_; }
  229. /// Return window title.
  230. const String& GetWindowTitle() const { return windowTitle_; }
  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 resizable.
  242. bool GetResizable() const { return resizable_; }
  243. /// Return whether window is borderless.
  244. bool GetBorderless() const { return borderless_; }
  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.
  252. bool GetFlushGPU() const { return flushGPU_; }
  253. /// Return allowed screen orientations.
  254. const String& GetOrientations() const { return orientations_; }
  255. /// Return whether Direct3D device is lost, and can not yet render. This happens during fullscreen resolution switching.
  256. bool IsDeviceLost() const { return deviceLost_; }
  257. /// Return number of primitives drawn this frame.
  258. unsigned GetNumPrimitives() const { return numPrimitives_; }
  259. /// Return number of batches drawn this frame.
  260. unsigned GetNumBatches() const { return numBatches_; }
  261. /// Return dummy color texture format for shadow maps. Is "NULL" (consume no video memory) if supported.
  262. unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
  263. /// Return shadow map depth texture format, or 0 if not supported.
  264. unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
  265. /// Return 24-bit shadow map depth texture format, or 0 if not supported.
  266. unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
  267. /// Return whether Shader Model 3 is supported.
  268. bool GetSM3Support() const { return hasSM3_; }
  269. /// Return whether hardware instancing is supported.
  270. bool GetInstancingSupport() const { return hasSM3_; }
  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 shadow map depth compare is done in hardware.
  276. bool GetHardwareShadowSupport() const { return hardwareShadowSupport_; }
  277. /// Return whether a readable hardware depth format is available.
  278. bool GetReadableDepthSupport() const { return GetReadableDepthFormat() != 0; }
  279. /// Return whether stream offset is supported.
  280. bool GetStreamOffsetSupport() const { return streamOffsetSupport_; }
  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.
  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 current index buffer.
  300. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  301. /// Return current vertex declaration.
  302. VertexDeclaration* GetVertexDeclaration() const { return vertexDeclaration_; }
  303. /// Return current vertex shader.
  304. ShaderVariation* GetVertexShader() const { return vertexShader_; }
  305. /// Return current pixel shader.
  306. ShaderVariation* GetPixelShader() const { return pixelShader_; }
  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 current 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 current rendertarget by index.
  316. RenderSurface* GetRenderTarget(unsigned index) const;
  317. /// Return current depth-stencil surface.
  318. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  319. /// Return the viewport coordinates.
  320. IntRect GetViewport() const { return viewport_; }
  321. /// Return texture anisotropy.
  322. unsigned GetTextureAnisotropy() const { return textureAnisotropy_; }
  323. /// Return blending mode.
  324. BlendMode GetBlendMode() const { return blendMode_; }
  325. /// Return whether color write is enabled.
  326. bool GetColorWrite() const { return colorWrite_; }
  327. /// Return hardware culling mode.
  328. CullMode GetCullMode() const { return cullMode_; }
  329. /// Return depth constant bias.
  330. float GetDepthConstantBias() const { return constantDepthBias_; }
  331. /// Return depth slope scaled bias.
  332. float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
  333. /// Return depth compare mode.
  334. CompareMode GetDepthTest() const { return depthTestMode_; }
  335. /// Return whether depth write is enabled.
  336. bool GetDepthWrite() const { return depthWrite_; }
  337. /// Return whether antialiased drawing mode is enabled.
  338. bool GetDrawAntialiased() const { return drawAntialiased_; }
  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 stream frequency by vertex buffer index.
  364. unsigned GetStreamFrequency(unsigned index) const;
  365. /// Return rendertarget width and height.
  366. IntVector2 GetRenderTargetDimensions() const;
  367. /// Return force Shader Model 2 flag.
  368. bool GetForceSM2() const { return forceSM2_; }
  369. /// Window was resized through user interaction. Called by Input subsystem.
  370. void WindowResized();
  371. /// Window was moved through user interaction. Called by Input subsystem.
  372. void WindowMoved();
  373. /// Maximize the Window.
  374. void Maximize();
  375. /// Minimize the Window.
  376. void Minimize();
  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 shader parameters when a shader variation is released or destroyed.
  388. void CleanupShaderParameters(ShaderVariation* variation);
  389. /// Return the API-specific alpha texture format.
  390. static unsigned GetAlphaFormat();
  391. /// Return the API-specific luminance texture format.
  392. static unsigned GetLuminanceFormat();
  393. /// Return the API-specific luminance alpha texture format.
  394. static unsigned GetLuminanceAlphaFormat();
  395. /// Return the API-specific RGB texture format.
  396. static unsigned GetRGBFormat();
  397. /// Return the API-specific RGBA texture format.
  398. static unsigned GetRGBAFormat();
  399. /// Return the API-specific RGBA 16-bit texture format.
  400. static unsigned GetRGBA16Format();
  401. /// Return the API-specific RGBA 16-bit float texture format.
  402. static unsigned GetRGBAFloat16Format();
  403. /// Return the API-specific RGBA 32-bit float texture format.
  404. static unsigned GetRGBAFloat32Format();
  405. /// Return the API-specific RG 16-bit texture format.
  406. static unsigned GetRG16Format();
  407. /// Return the API-specific RG 16-bit float texture format.
  408. static unsigned GetRGFloat16Format();
  409. /// Return the API-specific RG 32-bit float texture format.
  410. static unsigned GetRGFloat32Format();
  411. /// Return the API-specific single channel 16-bit float texture format.
  412. static unsigned GetFloat16Format();
  413. /// Return the API-specific single channel 32-bit float texture format.
  414. static unsigned GetFloat32Format();
  415. /// Return the API-specific linear depth texture format.
  416. static unsigned GetLinearDepthFormat();
  417. /// Return the API-specific hardware depth-stencil texture format.
  418. static unsigned GetDepthStencilFormat();
  419. /// Return the API-specific readable hardware depth format, or 0 if not supported.
  420. static unsigned GetReadableDepthFormat();
  421. /// Return the API-specific texture format from a textual description, for example "rgb".
  422. static unsigned GetFormat(const String& formatName);
  423. private:
  424. /// Create the application window.
  425. bool OpenWindow(int width, int height, bool resizable, bool borderless);
  426. /// Create the application window icon.
  427. void CreateWindowIcon();
  428. /// Adjust the window for new resolution and fullscreen mode.
  429. void AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless);
  430. /// Create the Direct3D interface.
  431. bool CreateInterface();
  432. /// Create the Direct3D device.
  433. bool CreateDevice(unsigned adapter, unsigned deviceType);
  434. /// Check supported rendering features.
  435. void CheckFeatureSupport();
  436. /// Reset the Direct3D device.
  437. void ResetDevice();
  438. /// Notify all GPU resources so they can release themselves as needed.
  439. void OnDeviceLost();
  440. /// Notify all GPU resources so they can recreate themselves as needed.
  441. void OnDeviceReset();
  442. /// Reset cached rendering state.
  443. void ResetCachedState();
  444. /// Initialize texture unit mappings.
  445. void SetTextureUnitMappings();
  446. /// Mutex for accessing the GPU objects vector from several threads.
  447. Mutex gpuObjectMutex_;
  448. /// Implementation.
  449. GraphicsImpl* impl_;
  450. /// Window title.
  451. String windowTitle_;
  452. /// Window Icon File Name
  453. Image* windowIcon_;
  454. /// External window, null if not in use (default.)
  455. void* externalWindow_;
  456. /// Window width.
  457. int width_;
  458. /// Window height.
  459. int height_;
  460. /// Window position.
  461. IntVector2 position_;
  462. /// Multisampling mode.
  463. int multiSample_;
  464. /// Fullscreen flag.
  465. bool fullscreen_;
  466. /// Borderless flag.
  467. bool borderless_;
  468. /// Resizable flag.
  469. bool resizable_;
  470. /// Vertical sync flag.
  471. bool vsync_;
  472. /// Triple buffering flag.
  473. bool tripleBuffer_;
  474. /// Flush GPU command buffer flag.
  475. bool flushGPU_;
  476. /// sRGB conversion on write flag for the main window.
  477. bool sRGB_;
  478. /// Direct3D device lost flag.
  479. bool deviceLost_;
  480. /// Flush query issued flag.
  481. bool queryIssued_;
  482. /// Light pre-pass rendering support flag.
  483. bool lightPrepassSupport_;
  484. /// Deferred rendering support flag.
  485. bool deferredSupport_;
  486. /// Hardware shadow map depth compare support flag.
  487. bool hardwareShadowSupport_;
  488. /// Stream offset support flag.
  489. bool streamOffsetSupport_;
  490. /// sRGB conversion on read support flag.
  491. bool sRGBSupport_;
  492. /// sRGB conversion on write support flag.
  493. bool sRGBWriteSupport_;
  494. /// Shader Model 3 flag.
  495. bool hasSM3_;
  496. /// Force Shader Model 2 flag.
  497. bool forceSM2_;
  498. /// Number of primitives this frame.
  499. unsigned numPrimitives_;
  500. /// Number of batches this frame.
  501. unsigned numBatches_;
  502. /// Largest scratch buffer request this frame.
  503. unsigned maxScratchBufferRequest_;
  504. /// GPU objects.
  505. Vector<GPUObject*> gpuObjects_;
  506. /// Scratch buffers.
  507. Vector<ScratchBuffer> scratchBuffers_;
  508. /// Vertex declarations.
  509. HashMap<unsigned long long, SharedPtr<VertexDeclaration> > vertexDeclarations_;
  510. /// Shadow map dummy color texture format.
  511. unsigned dummyColorFormat_;
  512. /// Shadow map depth texture format.
  513. unsigned shadowMapFormat_;
  514. /// Shadow map 24-bit depth texture format.
  515. unsigned hiresShadowMapFormat_;
  516. /// Vertex buffers in use.
  517. VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS];
  518. /// Stream frequencies by vertex buffer.
  519. unsigned streamFrequencies_[MAX_VERTEX_STREAMS];
  520. /// Stream offsets by vertex buffer.
  521. unsigned streamOffsets_[MAX_VERTEX_STREAMS];
  522. /// Index buffer in use.
  523. IndexBuffer* indexBuffer_;
  524. /// Vertex declaration in use.
  525. VertexDeclaration* vertexDeclaration_;
  526. /// Vertex shader in use.
  527. ShaderVariation* vertexShader_;
  528. /// Pixel shader in use.
  529. ShaderVariation* pixelShader_;
  530. /// Textures in use.
  531. Texture* textures_[MAX_TEXTURE_UNITS];
  532. /// Texture unit mappings.
  533. HashMap<String, TextureUnit> textureUnits_;
  534. /// Rendertargets in use.
  535. RenderSurface* renderTargets_[MAX_RENDERTARGETS];
  536. /// Depth-stencil surface in use.
  537. RenderSurface* depthStencil_;
  538. /// Viewport coordinates.
  539. IntRect viewport_;
  540. /// Texture anisotropy level.
  541. unsigned textureAnisotropy_;
  542. /// Blending mode.
  543. BlendMode blendMode_;
  544. /// Color write enable.
  545. bool colorWrite_;
  546. /// Hardware culling mode.
  547. CullMode cullMode_;
  548. /// Depth constant bias.
  549. float constantDepthBias_;
  550. /// Depth slope scaled bias.
  551. float slopeScaledDepthBias_;
  552. /// Depth compare mode.
  553. CompareMode depthTestMode_;
  554. /// Depth write enable flag.
  555. bool depthWrite_;
  556. /// Polygon fill mode.
  557. FillMode fillMode_;
  558. /// Scissor test rectangle.
  559. IntRect scissorRect_;
  560. /// Scissor test enable flag.
  561. bool scissorTest_;
  562. /// Stencil test compare mode.
  563. CompareMode stencilTestMode_;
  564. /// Stencil operation on pass.
  565. StencilOp stencilPass_;
  566. /// Stencil operation on fail.
  567. StencilOp stencilFail_;
  568. /// Stencil operation on depth fail.
  569. StencilOp stencilZFail_;
  570. /// Stencil test reference value.
  571. unsigned stencilRef_;
  572. /// Stencil compare bitmask.
  573. unsigned stencilCompareMask_;
  574. /// Stencil write bitmask.
  575. unsigned stencilWriteMask_;
  576. /// Stencil test enable flag.
  577. bool stencilTest_;
  578. /// Custom clip plane enable flag.
  579. bool useClipPlane_;
  580. /// Draw antialiased mode flag.
  581. bool drawAntialiased_;
  582. /// Default texture filtering mode.
  583. TextureFilterMode defaultTextureFilterMode_;
  584. /// Shader parameters for all vertex/pixel shader combinations.
  585. HashMap<Pair<ShaderVariation*, ShaderVariation*>, HashMap<StringHash, Pair<ShaderType, unsigned> > > shaderParameters_;
  586. /// Current active shader parameters.
  587. HashMap<StringHash, Pair<ShaderType, unsigned> >* currentShaderParameters_;
  588. /// Remembered shader parameter sources.
  589. const void* shaderParameterSources_[MAX_SHADER_PARAMETER_GROUPS];
  590. /// Base directory for shaders.
  591. String shaderPath_;
  592. /// File extension for shaders.
  593. String shaderExtension_;
  594. /// Last used shader in shader variation query.
  595. mutable WeakPtr<Shader> lastShader_;
  596. /// Last used shader name in shader variation query.
  597. mutable String lastShaderName_;
  598. /// Shader precache utility.
  599. SharedPtr<ShaderPrecache> shaderPrecache_;
  600. /// Allowed screen orientations.
  601. String orientations_;
  602. };
  603. /// Register Graphics library objects.
  604. void ATOMIC_API RegisterGraphicsLibrary(Context* context);
  605. }