BsD3D9Device.h 8.0 KB

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