D3D11Graphics.h 29 KB

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