BsD3D9Device.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsD3D9Prerequisites.h"
  5. #include "BsRenderTarget.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief High level interface for a DX9 device. Each device represents
  10. * a hardware adapter or a software emulation device.
  11. */
  12. class BS_D3D9_EXPORT D3D9Device
  13. {
  14. protected:
  15. /**
  16. * @brief Holds device specific render window resources.
  17. */
  18. struct RenderWindowResources
  19. {
  20. IDirect3DSwapChain9* swapChain; /**< Swap chain interface. */
  21. UINT32 adapterOrdinalInGroupIndex; /**< Relative index of the render window in the group. */
  22. UINT32 presentParametersIndex; /**< Index of present parameter in the shared array of the device. */
  23. IDirect3DSurface9* backBuffer; /**< The back buffer of the render window. */
  24. IDirect3DSurface9* depthBuffer; /**< The depth buffer of the render window. */
  25. D3DPRESENT_PARAMETERS presentParameters; /**< Present parameters of the render window. */
  26. bool acquired; /**< True if resources acquired. */
  27. };
  28. public:
  29. D3D9Device(D3D9DeviceManager* deviceManager, UINT adapterNumber, HMONITOR hMonitor,
  30. D3DDEVTYPE devType, DWORD behaviorFlags);
  31. ~D3D9Device();
  32. /**
  33. * @brief Attaches a new render window to this device. Caller must ensure
  34. * the window is not attached to multiple devices.
  35. */
  36. void attachRenderWindow(const D3D9RenderWindowCore* renderWindow);
  37. /**
  38. * @brief Detaches the render window from this device.
  39. */
  40. void detachRenderWindow(const D3D9RenderWindowCore* renderWindow);
  41. /**
  42. * @brief Acquires the device. This will cause a device reset in case present parameters changed.
  43. */
  44. bool acquire();
  45. /**
  46. * @brief Release the device and all resources directly managed by it.
  47. */
  48. void release();
  49. /**
  50. * @brief Destroys the device and all resources directly managed by it.
  51. */
  52. void destroy();
  53. /**
  54. * @brief Checks is the device lost. If lost you will need to "acquire" the device.
  55. */
  56. bool isDeviceLost();
  57. /**
  58. * @brief Return internal DX9 device object.
  59. */
  60. IDirect3DDevice9* getD3D9Device() const;
  61. /**
  62. * @brief Returns adapter number this device is tied to. This number corresponds to the
  63. * adapter index returned by DX9 API.
  64. */
  65. UINT getAdapterNumber() const;
  66. /**
  67. * @brief Returns type of the device.
  68. */
  69. D3DDEVTYPE getDeviceType() const;
  70. /**
  71. * @brief Checks is the device multihead (manages multiple full-screen outputs).
  72. */
  73. bool isMultihead() const;
  74. /**
  75. * @brief Returns true if depth/stencil format can be automatically determined.
  76. */
  77. bool isAutoDepthStencil() const;
  78. /**
  79. * @brief Returns DX9 device capabilities.
  80. */
  81. const D3DCAPS9& getD3D9DeviceCaps() const;
  82. /**
  83. * @brief Returns DX9 format of the back buffer used by the primary window for this device.
  84. */
  85. D3DFORMAT getBackBufferFormat() const;
  86. /**
  87. * @brief Returns DX9 format of the depth stencil buffer used by the primary window for this device.
  88. */
  89. D3DFORMAT getDepthStencilFormat() const;
  90. /**
  91. * @brief Validates that the window is valid for this device. Will reset device if needed.
  92. */
  93. bool validate(D3D9RenderWindowCore* renderWindow);
  94. /**
  95. * @brief Invalidates the window so on the next call to validate, the device will be re-acquired.
  96. */
  97. void invalidate(const D3D9RenderWindowCore* renderWindow);
  98. /**
  99. * @brief Swap back and front buffers for the specified window.
  100. */
  101. void present(const D3D9RenderWindowCore* renderWindow);
  102. /**
  103. * @brief Returns internal DX9 represention of the depth/stencil buffer.
  104. */
  105. IDirect3DSurface9* getDepthBuffer(const D3D9RenderWindowCore* renderWindow);
  106. /**
  107. * @brief Returns internal DX9 represention of the backbuffer.
  108. */
  109. IDirect3DSurface9* getBackBuffer(const D3D9RenderWindowCore* renderWindow);
  110. /**
  111. * @brief Sets adapter index for the specified window.
  112. */
  113. void setAdapterOrdinalIndex(const D3D9RenderWindowCore* renderWindow, UINT32 adapterOrdinalInGroupIndex);
  114. /**
  115. * @brief Copies contents of the back or depth/stencil buffer in to the provided object.
  116. */
  117. void copyContentsToMemory(const D3D9RenderWindowCore* window, PixelData &dst, RenderTargetCore::FrameBuffer buffer);
  118. /**
  119. * @brief Resets bound pipeline states/streams to null.
  120. */
  121. void clearDeviceStreams();
  122. protected:
  123. friend class D3D9DeviceManager;
  124. friend class D3D9RenderAPI;
  125. typedef Map<const D3D9RenderWindowCore*, RenderWindowResources*> RenderWindowToResorucesMap;
  126. typedef RenderWindowToResorucesMap::iterator RenderWindowToResorucesIterator;
  127. /**
  128. * @brief Find iterator for the specified window in the render window resource list.
  129. */
  130. RenderWindowToResorucesIterator getRenderWindowIterator(const D3D9RenderWindowCore* renderWindow);
  131. /**
  132. * @brief Acquires the device for the provided render window.
  133. */
  134. bool acquire(const D3D9RenderWindowCore* renderWindow);
  135. /**
  136. * @brief Forcibly reset the device.
  137. */
  138. bool reset();
  139. /**
  140. * @brief Update presentation parameters from the active render window.
  141. */
  142. void updatePresentationParameters();
  143. /**
  144. * @brief Updates presentation parameter indices for all windows attached to this device.
  145. */
  146. void updateRenderWindowsIndices();
  147. /**
  148. * @brief Creates a new DX9 device object.
  149. */
  150. void createD3D9Device();
  151. /**
  152. * @brief Releases the DX9 device object.
  153. */
  154. void releaseD3D9Device();
  155. /**
  156. * @brief Releases all render window resources in the provided object.
  157. */
  158. void releaseRenderWindowResources(RenderWindowResources* renderWindowResources);
  159. /**
  160. * @brief Acquires all render window resources from the provided render window,
  161. * and stores them in the provided render window resources object.
  162. */
  163. void acquireRenderWindowResources(RenderWindowToResorucesIterator it);
  164. /**
  165. * @brief Called when it has been detected that device has been lost.
  166. */
  167. void notifyDeviceLost();
  168. /**
  169. * @brief Checks if focus window changed and should device be reacquired.
  170. */
  171. void validateFocusWindow();
  172. /**
  173. * @brief Checks if back buffer size has changed and invalidates the window if it has.
  174. */
  175. void validateBackBufferSize(const D3D9RenderWindowCore* renderWindow);
  176. /**
  177. * @brief Checks if window monitor changed and re-links the window if needed.
  178. */
  179. bool validateDisplayMonitor(D3D9RenderWindowCore* renderWindow);
  180. /**
  181. * @brief Checks if device has been lost or active window invalidated and acquires the device if needed.
  182. */
  183. bool validateDeviceState(const D3D9RenderWindowCore* renderWindow);
  184. /**
  185. * @brief Checks if the render window contains a custom swap chain.
  186. */
  187. bool isSwapChainWindow(const D3D9RenderWindowCore* renderWindow);
  188. /**
  189. * @brief Returns primary window for this device.
  190. */
  191. const D3D9RenderWindowCore* getPrimaryWindow();
  192. /**
  193. * @brief Sets the shared window handle.
  194. */
  195. void setSharedWindowHandle(HWND hSharedHWND);
  196. protected:
  197. D3D9DeviceManager* mpDeviceManager; /**< The manager of this device instance. */
  198. IDirect3DDevice9* mpDevice; /**< Will hold the device interface. */
  199. UINT mAdapterNumber; /**< The adapter that this device belongs to. */
  200. HMONITOR mMonitor; /**< The monitor that this device belongs to. */
  201. D3DDEVTYPE mDeviceType; /**< Device type. */
  202. static HWND msSharedFocusWindow; /**< The shared focus window in case of multiple full screen render windows. */
  203. HWND mFocusWindow; /**< The focus window this device attached to. */
  204. DWORD mBehaviorFlags; /**< The behavior of this device. */
  205. /** Presentation parameters which the device was created with. May be
  206. * an array of presentation parameters in case of multi-head device.
  207. */
  208. D3DPRESENT_PARAMETERS* mPresentationParams;
  209. UINT mPresentationParamsCount; /**< Number of presentation parameters elements. */
  210. D3DCAPS9 mD3D9DeviceCaps; /**< Device caps. */
  211. bool mD3D9DeviceCapsValid; /**< True if device caps initialized. */
  212. D3DDEVICE_CREATION_PARAMETERS mCreationParams; /**< Creation parameters. */
  213. bool mDeviceLost; /**< True if device entered lost state. */
  214. RenderWindowToResorucesMap mMapRenderWindowToResoruces; /**< Map between render window to resources. */
  215. };
  216. }