BsD3D9Device.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /** @addtogroup D3D9
  9. * @{
  10. */
  11. /** High level interface for a DX9 device. Each device represents a hardware adapter or a software emulation device. */
  12. class BS_D3D9_EXPORT D3D9Device
  13. {
  14. protected:
  15. /** Holds device specific render window resources. */
  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. * Attaches a new render window to this device. Caller must ensure the window is not attached to multiple devices.
  32. */
  33. void attachRenderWindow(const D3D9RenderWindowCore* renderWindow);
  34. /** Detaches the render window from this device. */
  35. void detachRenderWindow(const D3D9RenderWindowCore* renderWindow);
  36. /** Acquires the device. This will cause a device reset in case present parameters changed. */
  37. bool acquire();
  38. /** Release the device and all resources directly managed by it. */
  39. void release();
  40. /** Destroys the device and all resources directly managed by it. */
  41. void destroy();
  42. /** Checks is the device lost. If lost you will need to "acquire" the device. */
  43. bool isDeviceLost();
  44. /** Return internal DX9 device object. */
  45. IDirect3DDevice9* getD3D9Device() const;
  46. /**
  47. * Returns adapter number this device is tied to. This number corresponds to the adapter index returned by DX9 API.
  48. */
  49. UINT getAdapterNumber() const;
  50. /** Returns type of the device. */
  51. D3DDEVTYPE getDeviceType() const;
  52. /** Checks is the device multihead (manages multiple full-screen outputs). */
  53. bool isMultihead() const;
  54. /** Returns true if depth/stencil format can be automatically determined. */
  55. bool isAutoDepthStencil() const;
  56. /** Returns DX9 device capabilities. */
  57. const D3DCAPS9& getD3D9DeviceCaps() const;
  58. /** Returns DX9 format of the back buffer used by the primary window for this device. */
  59. D3DFORMAT getBackBufferFormat() const;
  60. /** Returns DX9 format of the depth stencil buffer used by the primary window for this device. */
  61. D3DFORMAT getDepthStencilFormat() const;
  62. /** Validates that the window is valid for this device. Will reset device if needed. */
  63. bool validate(D3D9RenderWindowCore* renderWindow);
  64. /** Invalidates the window so on the next call to validate, the device will be re-acquired. */
  65. void invalidate(const D3D9RenderWindowCore* renderWindow);
  66. /** Swap back and front buffers for the specified window. */
  67. void present(const D3D9RenderWindowCore* renderWindow);
  68. /** Returns internal DX9 represention of the depth/stencil buffer. */
  69. IDirect3DSurface9* getDepthBuffer(const D3D9RenderWindowCore* renderWindow);
  70. /** Returns internal DX9 represention of the backbuffer. */
  71. IDirect3DSurface9* getBackBuffer(const D3D9RenderWindowCore* renderWindow);
  72. /** Sets adapter index for the specified window. */
  73. void setAdapterOrdinalIndex(const D3D9RenderWindowCore* renderWindow, UINT32 adapterOrdinalInGroupIndex);
  74. /** Copies contents of the back or depth/stencil buffer in to the provided object. */
  75. void copyContentsToMemory(const D3D9RenderWindowCore* window, PixelData &dst, RenderTargetCore::FrameBuffer buffer);
  76. /** Resets bound pipeline states/streams to null. */
  77. void clearDeviceStreams();
  78. protected:
  79. friend class D3D9DeviceManager;
  80. friend class D3D9RenderAPI;
  81. typedef Map<const D3D9RenderWindowCore*, RenderWindowResources*> RenderWindowToResorucesMap;
  82. typedef RenderWindowToResorucesMap::iterator RenderWindowToResorucesIterator;
  83. /** Find iterator for the specified window in the render window resource list. */
  84. RenderWindowToResorucesIterator getRenderWindowIterator(const D3D9RenderWindowCore* renderWindow);
  85. /** Acquires the device for the provided render window. */
  86. bool acquire(const D3D9RenderWindowCore* renderWindow);
  87. /** Forcibly reset the device. */
  88. bool reset();
  89. /** Update presentation parameters from the active render window. */
  90. void updatePresentationParameters();
  91. /** Updates presentation parameter indices for all windows attached to this device. */
  92. void updateRenderWindowsIndices();
  93. /** Creates a new DX9 device object. */
  94. void createD3D9Device();
  95. /** Releases the DX9 device object. */
  96. void releaseD3D9Device();
  97. /** Releases all render window resources in the provided object. */
  98. void releaseRenderWindowResources(RenderWindowResources* renderWindowResources);
  99. /**
  100. * Acquires all render window resources from the provided render window, and stores them in the provided render
  101. * window resources object.
  102. */
  103. void acquireRenderWindowResources(RenderWindowToResorucesIterator it);
  104. /** Called when it has been detected that device has been lost. */
  105. void notifyDeviceLost();
  106. /** Checks if focus window changed and should device be reacquired. */
  107. void validateFocusWindow();
  108. /** Checks if back buffer size has changed and invalidates the window if it has. */
  109. void validateBackBufferSize(const D3D9RenderWindowCore* renderWindow);
  110. /** Checks if window monitor changed and re-links the window if needed. */
  111. bool validateDisplayMonitor(D3D9RenderWindowCore* renderWindow);
  112. /** Checks if device has been lost or active window invalidated and acquires the device if needed. */
  113. bool validateDeviceState(const D3D9RenderWindowCore* renderWindow);
  114. /** Checks if the render window contains a custom swap chain. */
  115. bool isSwapChainWindow(const D3D9RenderWindowCore* renderWindow);
  116. /** Returns primary window for this device. */
  117. const D3D9RenderWindowCore* getPrimaryWindow();
  118. /** Sets the shared window handle. */
  119. void setSharedWindowHandle(HWND hSharedHWND);
  120. protected:
  121. D3D9DeviceManager* mpDeviceManager; /**< The manager of this device instance. */
  122. IDirect3DDevice9* mpDevice; /**< Will hold the device interface. */
  123. UINT mAdapterNumber; /**< The adapter that this device belongs to. */
  124. HMONITOR mMonitor; /**< The monitor that this device belongs to. */
  125. D3DDEVTYPE mDeviceType; /**< Device type. */
  126. static HWND msSharedFocusWindow; /**< The shared focus window in case of multiple full screen render windows. */
  127. HWND mFocusWindow; /**< The focus window this device attached to. */
  128. DWORD mBehaviorFlags; /**< The behavior of this device. */
  129. /**
  130. * Presentation parameters which the device was created with. May be an array of presentation parameters in case of
  131. * multi-head device.
  132. */
  133. D3DPRESENT_PARAMETERS* mPresentationParams;
  134. UINT mPresentationParamsCount; /**< Number of presentation parameters elements. */
  135. D3DCAPS9 mD3D9DeviceCaps; /**< Device caps. */
  136. bool mD3D9DeviceCapsValid; /**< True if device caps initialized. */
  137. D3DDEVICE_CREATION_PARAMETERS mCreationParams; /**< Creation parameters. */
  138. bool mDeviceLost; /**< True if device entered lost state. */
  139. RenderWindowToResorucesMap mMapRenderWindowToResoruces; /**< Map between render window to resources. */
  140. };
  141. /** @} */
  142. }