Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
13 KiB

  1. //-----------------------------------------------------------------------------
  2. // File: D3DSaver.h
  3. //
  4. // Desc: Framework for screensavers that use Direct3D 8.0.
  5. //
  6. // Copyright (c) 2000 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #ifndef _D3DSAVER_H
  9. #define _D3DSAVER_H
  10. //-----------------------------------------------------------------------------
  11. // Error codes
  12. //-----------------------------------------------------------------------------
  13. enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF };
  14. #define D3DAPPERR_NODIRECT3D 0x82000001
  15. #define D3DAPPERR_NOWINDOW 0x82000002
  16. #define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003
  17. #define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004
  18. #define D3DAPPERR_NOHARDWAREDEVICE 0x82000005
  19. #define D3DAPPERR_HALNOTCOMPATIBLE 0x82000006
  20. #define D3DAPPERR_NOWINDOWEDHAL 0x82000007
  21. #define D3DAPPERR_NODESKTOPHAL 0x82000008
  22. #define D3DAPPERR_NOHALTHISMODE 0x82000009
  23. #define D3DAPPERR_NONZEROREFCOUNT 0x8200000a
  24. #define D3DAPPERR_MEDIANOTFOUND 0x8200000b
  25. #define D3DAPPERR_RESIZEFAILED 0x8200000c
  26. #define D3DAPPERR_INITDEVICEOBJECTSFAILED 0x8200000d
  27. #define D3DAPPERR_CREATEDEVICEFAILED 0x8200000e
  28. #define D3DAPPERR_NOPREVIEW 0x8200000f
  29. //-----------------------------------------------------------------------------
  30. // Constants
  31. //-----------------------------------------------------------------------------
  32. #define MAX_DISPLAYS 9
  33. #define NO_ADAPTER 0xffffffff
  34. #define NO_MONITOR 0xffffffff
  35. //***************************************************************************************
  36. // Modes of operation for screensaver
  37. enum SaverMode
  38. {
  39. sm_config, // Config dialog box
  40. sm_preview, // Mini preview window in Display Properties dialog
  41. sm_full, // Full-on screensaver mode
  42. sm_test, // Test mode
  43. sm_passwordchange // Change password
  44. };
  45. // Prototype for VerifyScreenSavePwd() in password.cpl, used on Win9x
  46. typedef BOOL (PASCAL * VERIFYPWDPROC) (HWND);
  47. //-----------------------------------------------------------------------------
  48. // Name: struct D3DModeInfo
  49. // Desc: Structure for holding information about a display mode
  50. //-----------------------------------------------------------------------------
  51. struct D3DModeInfo
  52. {
  53. DWORD Width; // Screen width in this mode
  54. DWORD Height; // Screen height in this mode
  55. D3DFORMAT Format; // Pixel format in this mode
  56. DWORD dwBehavior; // Hardware / Software / Mixed vertex processing
  57. D3DFORMAT DepthStencilFormat; // Which depth/stencil format to use with this mode
  58. };
  59. //-----------------------------------------------------------------------------
  60. // Name: struct D3DWindowedModeInfo
  61. // Desc: Structure for holding information about a display mode
  62. //-----------------------------------------------------------------------------
  63. struct D3DWindowedModeInfo
  64. {
  65. D3DFORMAT DisplayFormat;
  66. D3DFORMAT BackBufferFormat;
  67. DWORD dwBehavior; // Hardware / Software / Mixed vertex processing
  68. D3DFORMAT DepthStencilFormat; // Which depth/stencil format to use with this mode
  69. };
  70. //-----------------------------------------------------------------------------
  71. // Name: struct D3DDeviceInfo
  72. // Desc: Structure for holding information about a Direct3D device, including
  73. // a list of modes compatible with this device
  74. //-----------------------------------------------------------------------------
  75. struct D3DDeviceInfo
  76. {
  77. // Device data
  78. D3DDEVTYPE DeviceType; // Reference, HAL, etc.
  79. D3DCAPS8 d3dCaps; // Capabilities of this device
  80. const TCHAR* strDesc; // Name of this device
  81. BOOL bCanDoWindowed; // Whether this device can work in windowed mode
  82. // Modes for this device
  83. DWORD dwNumModes;
  84. D3DModeInfo modes[150];
  85. // Current state
  86. DWORD dwCurrentMode;
  87. BOOL bWindowed;
  88. D3DMULTISAMPLE_TYPE MultiSampleType;
  89. };
  90. //-----------------------------------------------------------------------------
  91. // Name: struct D3DAdapterInfo
  92. // Desc: Structure for holding information about an adapter, including a list
  93. // of devices available on this adapter
  94. //-----------------------------------------------------------------------------
  95. struct D3DAdapterInfo
  96. {
  97. // Adapter data
  98. DWORD iMonitor; // Which MonitorInfo corresponds to this adapter
  99. D3DADAPTER_IDENTIFIER8 d3dAdapterIdentifier;
  100. D3DDISPLAYMODE d3ddmDesktop; // Desktop display mode for this adapter
  101. // Devices for this adapter
  102. DWORD dwNumDevices;
  103. D3DDeviceInfo devices[3];
  104. BOOL bHasHAL;
  105. BOOL bHasAppCompatHAL;
  106. BOOL bHasSW;
  107. BOOL bHasAppCompatSW;
  108. // User's preferred mode settings for this adapter
  109. DWORD dwUserPrefWidth;
  110. DWORD dwUserPrefHeight;
  111. D3DFORMAT d3dfmtUserPrefFormat;
  112. BOOL bLeaveBlack; // If TRUE, don't render to this display
  113. BOOL bDisableHW; // If TRUE, don't use HAL on this display
  114. // Current state
  115. DWORD dwCurrentDevice;
  116. HWND hWndDevice;
  117. };
  118. //-----------------------------------------------------------------------------
  119. // Name: struct MonitorInfo
  120. // Desc: Structure for holding information about a monitor
  121. //-----------------------------------------------------------------------------
  122. struct MonitorInfo
  123. {
  124. TCHAR strDeviceName[128];
  125. TCHAR strMonitorName[128];
  126. HMONITOR hMonitor;
  127. RECT rcScreen;
  128. DWORD iAdapter; // Which D3DAdapterInfo corresponds to this monitor
  129. HWND hWnd;
  130. // Error message state
  131. FLOAT xError;
  132. FLOAT yError;
  133. FLOAT widthError;
  134. FLOAT heightError;
  135. FLOAT xVelError;
  136. FLOAT yVelError;
  137. };
  138. //-----------------------------------------------------------------------------
  139. // Name: struct RenderUnit
  140. // Desc:
  141. //-----------------------------------------------------------------------------
  142. struct RenderUnit
  143. {
  144. UINT iAdapter;
  145. UINT iMonitor;
  146. D3DDEVTYPE DeviceType; // Reference, HAL, etc.
  147. DWORD dwBehavior;
  148. IDirect3DDevice8* pd3dDevice;
  149. D3DPRESENT_PARAMETERS d3dpp;
  150. BOOL bDeviceObjectsInited; // InitDeviceObjects was called
  151. BOOL bDeviceObjectsRestored; // RestoreDeviceObjects was called
  152. TCHAR strDeviceStats[90];// String to hold D3D device stats
  153. TCHAR strFrameStats[40]; // String to hold frame stats
  154. };
  155. //-----------------------------------------------------------------------------
  156. // Name: class CD3DScreensaver
  157. // Desc: D3D screensaver class
  158. //-----------------------------------------------------------------------------
  159. class CD3DScreensaver
  160. {
  161. public:
  162. CD3DScreensaver();
  163. virtual HRESULT Create( HINSTANCE hInstance );
  164. virtual INT Run();
  165. HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType = 0 );
  166. protected:
  167. SaverMode ParseCommandLine( TCHAR* pstrCommandLine );
  168. VOID ChangePassword();
  169. HRESULT DoSaver();
  170. virtual VOID DoConfig() { }
  171. virtual VOID ReadSettings() {};
  172. VOID ReadScreenSettings( HKEY hkeyParent );
  173. VOID WriteScreenSettings( HKEY hkeyParent );
  174. virtual VOID DoPaint( HWND hwnd, HDC hdc );
  175. HRESULT Initialize3DEnvironment();
  176. VOID Cleanup3DEnvironment();
  177. HRESULT Render3DEnvironment();
  178. static LRESULT CALLBACK SaverProcStub( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  179. virtual LRESULT SaverProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  180. VOID InterruptSaver();
  181. VOID ShutdownSaver();
  182. VOID DoScreenSettingsDialog( HWND hwndParent );
  183. static INT_PTR CALLBACK ScreenSettingsDlgProcStub( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  184. INT_PTR ScreenSettingsDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  185. VOID SetupAdapterPage(HWND hWnd);
  186. HRESULT CreateSaverWindow();
  187. HRESULT BuildDeviceList();
  188. BOOL FindDepthStencilFormat( UINT iAdapter, D3DDEVTYPE DeviceType,
  189. D3DFORMAT TargetFormat, D3DFORMAT* pDepthStencilFormat );
  190. HRESULT CheckWindowedFormat( UINT iAdapter, D3DWindowedModeInfo* pD3DWindowedModeInfo );
  191. HRESULT CreateFullscreenRenderUnit( RenderUnit* pRenderUnit );
  192. HRESULT CreateWindowedRenderUnit( RenderUnit* pRenderUnit );
  193. BOOL FindNextLowerMode( D3DDeviceInfo* pD3DDeviceInfo );
  194. VOID SwitchToRenderUnit( UINT iRenderUnit );
  195. HRESULT SetProjectionMatrix( FLOAT fNear, FLOAT fFar );
  196. virtual VOID UpdateDeviceStats();
  197. virtual VOID UpdateFrameStats();
  198. virtual BOOL GetTextForError( HRESULT hr, TCHAR* pszError, DWORD dwNumChars );
  199. VOID UpdateErrorBox();
  200. VOID EnumMonitors( VOID );
  201. BOOL GetBestAdapter( DWORD* piAdapter );
  202. virtual VOID SetDevice( UINT iDevice ) { }
  203. virtual HRESULT RegisterSoftwareDevice() { return S_OK; }
  204. virtual HRESULT ConfirmDevice(D3DCAPS8* pCaps, DWORD dwBehavior,
  205. D3DFORMAT fmtBackBuffer) { return S_OK; }
  206. virtual HRESULT ConfirmMode( LPDIRECT3DDEVICE8 pd3dDev ) { return S_OK; }
  207. virtual HRESULT OneTimeSceneInit() { return S_OK; }
  208. virtual HRESULT InitDeviceObjects() { return S_OK; }
  209. virtual HRESULT RestoreDeviceObjects() { return S_OK; }
  210. virtual HRESULT FrameMove() { return S_OK; }
  211. virtual HRESULT Render() { return S_OK; }
  212. virtual HRESULT InvalidateDeviceObjects() { return S_OK; }
  213. virtual HRESULT DeleteDeviceObjects() { return S_OK; }
  214. virtual HRESULT FinalCleanup() { return S_OK; }
  215. protected:
  216. SaverMode m_SaverMode; // sm_config, sm_full, sm_preview, etc.
  217. BOOL m_bAllScreensSame; // If TRUE, show same image on all screens
  218. HWND m_hWnd; // Focus window and device window on primary
  219. HWND m_hWndParent;
  220. HINSTANCE m_hInstance;
  221. BOOL m_bWaitForInputIdle; // Used to pause when preview starts
  222. DWORD m_dwSaverMouseMoveCount;
  223. BOOL m_bIs9x;
  224. HINSTANCE m_hPasswordDLL;
  225. VERIFYPWDPROC m_VerifySaverPassword;
  226. BOOL m_bCheckingSaverPassword;
  227. BOOL m_bWindowed;
  228. // Variables for non-fatal error management
  229. BOOL m_bErrorMode; // Whether to display an error
  230. HRESULT m_hrError; // Error code to display
  231. TCHAR m_szError[400]; // Error message text
  232. MonitorInfo m_Monitors[MAX_DISPLAYS];
  233. DWORD m_dwNumMonitors;
  234. RenderUnit m_RenderUnits[MAX_DISPLAYS];
  235. DWORD m_dwNumRenderUnits;
  236. D3DAdapterInfo* m_Adapters[MAX_DISPLAYS];
  237. DWORD m_dwNumAdapters;
  238. IDirect3D8* m_pD3D;
  239. IDirect3DDevice8* m_pd3dDevice; // Current D3D device
  240. RECT m_rcRenderTotal; // Rect of entire area to be rendered
  241. RECT m_rcRenderCurDevice; // Rect of render area of current device
  242. D3DSURFACE_DESC m_d3dsdBackBuffer; // Info on back buffer for current device
  243. TCHAR m_strWindowTitle[200]; // Title for the app's window
  244. BOOL m_bAllowRef; // Whether to allow REF D3D device
  245. BOOL m_bUseDepthBuffer; // Whether to autocreate depthbuffer
  246. BOOL m_bMultithreaded; // Whether to make D3D thread-safe
  247. BOOL m_bOneScreenOnly; // Only ever show screensaver on one screen
  248. TCHAR m_strRegPath[200]; // Where to store registry info
  249. DWORD m_dwMinDepthBits; // Minimum number of bits needed in depth buffer
  250. DWORD m_dwMinStencilBits; // Minimum number of bits needed in stencil buffer
  251. D3DSWAPEFFECT m_SwapEffectFullscreen; // SwapEffect to use in fullscreen Present()
  252. D3DSWAPEFFECT m_SwapEffectWindowed; // SwapEffect to use in windowed Present()
  253. // Variables for timing
  254. FLOAT m_fTime; // Current time in seconds
  255. FLOAT m_fElapsedTime; // Time elapsed since last frame
  256. FLOAT m_fFPS; // Instanteous frame rate
  257. TCHAR m_strDeviceStats[90];// D3D device stats for current device
  258. TCHAR m_strFrameStats[40]; // Frame stats for current device
  259. };
  260. #endif