Leaked source code of windows server 2003
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.

1033 lines
34 KiB

  1. /****************************************************************************
  2. *
  3. * File: testagp.cpp
  4. * Project: DxDiag (DirectX Diagnostic Tool)
  5. * Author: Jason Sandlin (jasonsa@microsoft.com)
  6. * Purpose: Test AGP Texturing functionality on this machine
  7. *
  8. * (C) Copyright 2000 Microsoft Corp. All rights reserved.
  9. *
  10. ****************************************************************************/
  11. #include <Windows.h>
  12. #define DIRECTDRAW_VERSION 0x0700 // run on DX7 and later versions
  13. #include <ddraw.h>
  14. #define DIRECT3D_VERSION 0x0700 // run on DX7 and later versions
  15. #define D3D_OVERLOADS
  16. #include <d3d.h>
  17. #include "reginfo.h"
  18. #include "sysinfo.h"
  19. #include "dispinfo.h"
  20. #include "testagp.h"
  21. #include "resource.h"
  22. #ifndef ReleasePpo
  23. #define ReleasePpo(ppo) \
  24. if (*(ppo) != NULL) \
  25. { \
  26. (*(ppo))->Release(); \
  27. *(ppo) = NULL; \
  28. } \
  29. else (VOID)0
  30. #endif
  31. enum TESTID
  32. {
  33. TESTID_LOAD_D3D8_DLL=1,
  34. TESTID_GET_D3DCREATE8,
  35. TESTID_D3DCREATE8,
  36. TESTID_ENUMADAPTERMODES,
  37. TESTID_GETDEVICECAPS,
  38. TESTID_NOMODEFOUND,
  39. TESTID_CREATE_TEST_WINDOW,
  40. TESTID_CREATE_DEVICE,
  41. TESTID_GETBACKBUFFER,
  42. TESTID_GETDESC,
  43. TESTID_CREATE_VERTEX_BUFFER,
  44. TESTID_CREATE_INDEX_BUFFER,
  45. TESTID_LOCK,
  46. TESTID_UNLOCK,
  47. TESTID_SETLIGHT,
  48. TESTID_LIGHTENABLE,
  49. TESTID_SETTRANSFORM,
  50. TESTID_SETRENDERSTATE,
  51. TESTID_CREATETEXTURE,
  52. TESTID_SETTEXTURESTAGESTATE,
  53. TESTID_SETTEXTURE,
  54. TESTID_SETVERTEXSHADER,
  55. TESTID_USER_CANCELLED,
  56. TESTID_VIEWPORT_CLEAR,
  57. TESTID_BEGINSCENE,
  58. TESTID_SETMATERIAL,
  59. TESTID_SETSTREAMSOURCE,
  60. TESTID_SETINDICES,
  61. TESTID_DRAW_INDEXED_PRIMITIVE,
  62. TESTID_ENDSCENE,
  63. TESTID_PRESENT,
  64. TESTID_USER_VERIFY_D3D7_RENDERING,
  65. TESTID_USER_VERIFY_D3D8_RENDERING,
  66. TESTID_LOAD_DDRAW_DLL,
  67. TESTID_GET_DIRECTDRAWCREATE,
  68. TESTID_DIRECTDRAWCREATE,
  69. TESTID_SETCOOPERATIVELEVEL_FULLSCREEN,
  70. TESTID_SETCOOPERATIVELEVEL_NORMAL,
  71. TESTID_SETDISPLAYMODE,
  72. TESTID_CREATEPRIMARYSURFACE_FLIP_ONEBACK,
  73. TESTID_GETATTACHEDSURFACE,
  74. TESTID_QUERY_D3D,
  75. TESTID_SETVIEWPORT,
  76. TESTID_ENUMTEXTUREFORMATS,
  77. TESTID_CREATESURFACE,
  78. TESTID_GETDC,
  79. TESTID_RELEASEDC,
  80. };
  81. BOOL BTranslateError(HRESULT hr, TCHAR* psz, BOOL bEnglish = FALSE); // from main.cpp (yuck)
  82. typedef HRESULT (WINAPI* LPDIRECTDRAWCREATEEX)(GUID FAR * lpGuid, LPVOID *lplpDD, REFIID iid,IUnknown FAR *pUnkOuter );
  83. static HRESULT Test3D(BOOL bUseTexture, HWND hwndMain, LPDIRECTDRAW7 pdd, GUID guid3DDevice, LONG* piStepThatFailed);
  84. static HRESULT CreateTestWindow(HWND hwndMain, HWND* phwnd);
  85. static HRESULT D3DUtil_SetProjectionMatrix( D3DMATRIX& mat, FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane, FLOAT fFarPlane );
  86. static HRESULT CreateTexture( LPDIRECTDRAWSURFACE7* ppdds, LPDIRECTDRAW7 pdd, LPDIRECT3DDEVICE7 pd3dDevice, TCHAR* strName, LONG* piStepThatFailed);
  87. static HRESULT CALLBACK TextureSearchCallback( DDPIXELFORMAT* pddpf, VOID* param );
  88. /****************************************************************************
  89. *
  90. * TestAGP
  91. *
  92. ****************************************************************************/
  93. VOID TestD3Dv7(BOOL bUseTexture, HWND hwndMain, DisplayInfo* pDisplayInfo)
  94. {
  95. HRESULT hr = S_OK;
  96. TCHAR szPath[MAX_PATH];
  97. HINSTANCE hInstDDraw = NULL;
  98. LPDIRECTDRAWCREATEEX pDDCreateEx = NULL;
  99. LPDIRECTDRAW7 pdd = NULL;
  100. BOOL bTestHardwareRendering = FALSE;
  101. TCHAR sz[300];
  102. TCHAR szTitle[300];
  103. if( pDisplayInfo == NULL )
  104. return;
  105. LoadString(NULL, IDS_APPFULLNAME, szTitle, MAX_PATH);
  106. // Load ddraw.dll
  107. GetSystemDirectory(szPath, MAX_PATH);
  108. lstrcat(szPath, TEXT("\\ddraw.dll"));
  109. hInstDDraw = LoadLibrary(szPath);
  110. if (hInstDDraw == NULL)
  111. {
  112. pDisplayInfo->m_testResultD3D7.m_iStepThatFailed = TESTID_LOAD_DDRAW_DLL;
  113. pDisplayInfo->m_testResultD3D7.m_hr = DDERR_NOTFOUND;
  114. goto LEnd;
  115. }
  116. // Get DirectDrawCreate entry point
  117. pDDCreateEx = (LPDIRECTDRAWCREATEEX)GetProcAddress(hInstDDraw, "DirectDrawCreateEx");
  118. if (pDDCreateEx == NULL)
  119. {
  120. pDisplayInfo->m_testResultD3D7.m_iStepThatFailed = TESTID_GET_DIRECTDRAWCREATE;
  121. pDisplayInfo->m_testResultD3D7.m_hr = DDERR_NOTFOUND;
  122. goto LEnd;
  123. }
  124. // Call DirectDrawCreateEx
  125. if (FAILED(hr = pDDCreateEx(&pDisplayInfo->m_guid, (void**)&pdd, IID_IDirectDraw7, NULL)))
  126. {
  127. pDisplayInfo->m_testResultD3D7.m_iStepThatFailed = TESTID_DIRECTDRAWCREATE;
  128. pDisplayInfo->m_testResultD3D7.m_hr = hr;
  129. goto LEnd;
  130. }
  131. // Get DirectDraw caps
  132. DDCAPS ddcapsHAL;
  133. DDCAPS ddcapsHEL;
  134. ddcapsHAL.dwSize = sizeof(ddcapsHAL);
  135. ddcapsHEL.dwSize = sizeof(ddcapsHEL);
  136. if (FAILED(hr = pdd->GetCaps(&ddcapsHAL, &ddcapsHEL)))
  137. {
  138. pDisplayInfo->m_testResultD3D7.m_iStepThatFailed = TESTID_GETDEVICECAPS;
  139. pDisplayInfo->m_testResultD3D7.m_hr = hr;
  140. goto LEnd;
  141. }
  142. POINT ptMouse;
  143. GetCursorPos(&ptMouse);
  144. if (FAILED(hr = Test3D(bUseTexture, hwndMain, pdd, IID_IDirect3DHALDevice, &pDisplayInfo->m_testResultD3D7.m_iStepThatFailed)))
  145. {
  146. pDisplayInfo->m_testResultD3D7.m_hr = hr;
  147. goto LEnd;
  148. }
  149. SetCursorPos( ptMouse.x, ptMouse.y );
  150. ReleasePpo(&pdd);
  151. if (pDisplayInfo->m_testResultD3D7.m_iStepThatFailed == TESTID_USER_CANCELLED)
  152. {
  153. LoadString(NULL, IDS_YOUCANCELLED, sz, 300);
  154. MessageBox(hwndMain, sz, szTitle, MB_OK);
  155. pDisplayInfo->m_testResultD3D7.m_bCancelled = TRUE;
  156. goto LEnd;
  157. }
  158. LoadString(NULL, IDS_CONFIRMD3DTEST, sz, 300);
  159. if (IDNO == MessageBox(hwndMain, sz, szTitle, MB_YESNO))
  160. {
  161. pDisplayInfo->m_testResultD3D7.m_iStepThatFailed = TESTID_USER_VERIFY_D3D7_RENDERING;
  162. pDisplayInfo->m_testResultD3D7.m_hr = S_OK;
  163. goto LEnd;
  164. }
  165. LEnd:
  166. ReleasePpo(&pdd);
  167. if (hInstDDraw != NULL)
  168. FreeLibrary(hInstDDraw);
  169. }
  170. /****************************************************************************
  171. *
  172. * Test3D - Generate a spinning 3D cube
  173. *
  174. ****************************************************************************/
  175. HRESULT Test3D(BOOL bUseTexture, HWND hwndMain, LPDIRECTDRAW7 pdd, GUID guid3DDevice, LONG* piStepThatFailed)
  176. {
  177. HRESULT hr;
  178. HWND hwnd = NULL;
  179. LPDIRECTDRAWSURFACE7 pddsFront = NULL;
  180. LPDIRECTDRAWSURFACE7 pddsBack = NULL;
  181. LPDIRECT3D7 pd3d = NULL;
  182. LPDIRECT3DDEVICE7 pd3ddev = NULL;
  183. LPDIRECT3DLIGHT pLight = NULL;
  184. LPDIRECTDRAWSURFACE7 pddsTexture = NULL;
  185. BOOL bCooperativeLevelSet = FALSE;
  186. BOOL bDisplayModeSet = FALSE;
  187. DDSURFACEDESC2 ddsd;
  188. D3DDEVICEDESC7 ddDesc;
  189. DDSCAPS2 ddscaps;
  190. D3DVIEWPORT7 vp;
  191. D3DLIGHT7 lightdata;
  192. D3DMATRIX mat;
  193. D3DMATRIX matRotY;
  194. D3DMATRIX matRotX;
  195. RECT rcBack;
  196. DWORD dwWidth;
  197. DWORD dwHeight;
  198. FLOAT fRotY;
  199. FLOAT fRotX;
  200. INT i;
  201. static const D3DVERTEX vertexArrayFront[] =
  202. {
  203. D3DVERTEX(D3DVECTOR(-1.0, -1.0, -1.0), D3DVECTOR(0.0, 0.0, -1.0), 1.0f, 0.0f),
  204. D3DVERTEX(D3DVECTOR( 1.0, -1.0, -1.0), D3DVECTOR(0.0, 0.0, -1.0), 0.0f, 0.0f),
  205. D3DVERTEX(D3DVECTOR(-1.0, 1.0, -1.0), D3DVECTOR(0.0, 0.0, -1.0), 1.0f, 1.0f),
  206. D3DVERTEX(D3DVECTOR( 1.0, 1.0, -1.0), D3DVECTOR(0.0, 0.0, -1.0), 0.0f, 1.0f),
  207. };
  208. static const WORD indexArrayFront[] =
  209. {
  210. 0, 2, 1,
  211. 2, 3, 1,
  212. };
  213. static const D3DVERTEX vertexArrayBack[] =
  214. {
  215. D3DVERTEX(D3DVECTOR(-1.0, -1.0, 1.0), D3DVECTOR(0.0, 0.0, 1.0), 0.0f, 0.0f),
  216. D3DVERTEX(D3DVECTOR( 1.0, -1.0, 1.0), D3DVECTOR(0.0, 0.0, 1.0), 1.0f, 0.0f),
  217. D3DVERTEX(D3DVECTOR(-1.0, 1.0, 1.0), D3DVECTOR(0.0, 0.0, 1.0), 0.0f, 1.0f),
  218. D3DVERTEX(D3DVECTOR( 1.0, 1.0, 1.0), D3DVECTOR(0.0, 0.0, 1.0), 1.0f, 1.0f),
  219. };
  220. static const WORD indexArrayBack[] =
  221. {
  222. 0, 1, 2,
  223. 2, 1, 3,
  224. };
  225. static const D3DVERTEX vertexArrayLeft[] =
  226. {
  227. D3DVERTEX(D3DVECTOR(-1.0, -1.0, -1.0), D3DVECTOR(-1.0, 0.0, 0.0), 0.0f, 0.0f),
  228. D3DVERTEX(D3DVECTOR(-1.0, -1.0, 1.0), D3DVECTOR(-1.0, 0.0, 0.0), 1.0f, 0.0f),
  229. D3DVERTEX(D3DVECTOR(-1.0, 1.0, -1.0), D3DVECTOR(-1.0, 0.0, 0.0), 0.0f, 1.0f),
  230. D3DVERTEX(D3DVECTOR(-1.0, 1.0, 1.0), D3DVECTOR(-1.0, 0.0, 0.0), 1.0f, 1.0f),
  231. };
  232. static const WORD indexArrayLeft[] =
  233. {
  234. 0, 1, 2,
  235. 2, 1, 3,
  236. };
  237. static const D3DVERTEX vertexArrayRight[] =
  238. {
  239. D3DVERTEX(D3DVECTOR(1.0, -1.0, -1.0), D3DVECTOR(1.0, 0.0, 0.0), 1.0f, 0.0f),
  240. D3DVERTEX(D3DVECTOR(1.0, -1.0, 1.0), D3DVECTOR(1.0, 0.0, 0.0), 0.0f, 0.0f),
  241. D3DVERTEX(D3DVECTOR(1.0, 1.0, -1.0), D3DVECTOR(1.0, 0.0, 0.0), 1.0f, 1.0f),
  242. D3DVERTEX(D3DVECTOR(1.0, 1.0, 1.0), D3DVECTOR(1.0, 0.0, 0.0), 0.0f, 1.0f),
  243. };
  244. static const WORD indexArrayRight[] =
  245. {
  246. 0, 2, 1,
  247. 2, 3, 1,
  248. };
  249. static const D3DVERTEX vertexArrayTop[] =
  250. {
  251. D3DVERTEX(D3DVECTOR(-1.0, 1.0, -1.0), D3DVECTOR(0.0, 1.0, 0.0), 0.0f, 1.0f),
  252. D3DVERTEX(D3DVECTOR( 1.0, 1.0, -1.0), D3DVECTOR(0.0, 1.0, 0.0), 1.0f, 1.0f),
  253. D3DVERTEX(D3DVECTOR(-1.0, 1.0, 1.0), D3DVECTOR(0.0, 1.0, 0.0), 0.0f, 0.0f),
  254. D3DVERTEX(D3DVECTOR( 1.0, 1.0, 1.0), D3DVECTOR(0.0, 1.0, 0.0), 1.0f, 0.0f),
  255. };
  256. static const WORD indexArrayTop[] =
  257. {
  258. 0, 2, 1,
  259. 2, 3, 1,
  260. };
  261. static const D3DVERTEX vertexArrayBottom[] =
  262. {
  263. D3DVERTEX(D3DVECTOR(-1.0, -1.0, -1.0), D3DVECTOR(0.0, -1.0, 0.0), 1.0f, 1.0f),
  264. D3DVERTEX(D3DVECTOR( 1.0, -1.0, -1.0), D3DVECTOR(0.0, -1.0, 0.0), 0.0f, 1.0f),
  265. D3DVERTEX(D3DVECTOR(-1.0, -1.0, 1.0), D3DVECTOR(0.0, -1.0, 0.0), 1.0f, 0.0f),
  266. D3DVERTEX(D3DVECTOR( 1.0, -1.0, 1.0), D3DVECTOR(0.0, -1.0, 0.0), 0.0f, 0.0f),
  267. };
  268. static const WORD indexArrayBottom[] =
  269. {
  270. 0, 1, 2,
  271. 2, 1, 3,
  272. };
  273. D3DMATERIAL7 mtrlRed;
  274. ZeroMemory( &mtrlRed, sizeof(D3DMATERIAL7) );
  275. mtrlRed.dcvDiffuse.r = mtrlRed.dcvAmbient.r = 1.0f;
  276. mtrlRed.dcvDiffuse.g = mtrlRed.dcvAmbient.g = 0.0f;
  277. mtrlRed.dcvDiffuse.b = mtrlRed.dcvAmbient.b = 0.0f;
  278. mtrlRed.dcvDiffuse.a = mtrlRed.dcvAmbient.a = 1.0f;
  279. D3DMATERIAL7 mtrlGreen;
  280. ZeroMemory( &mtrlGreen, sizeof(D3DMATERIAL7) );
  281. mtrlGreen.dcvDiffuse.r = mtrlGreen.dcvAmbient.r = 0.0f;
  282. mtrlGreen.dcvDiffuse.g = mtrlGreen.dcvAmbient.g = 1.0f;
  283. mtrlGreen.dcvDiffuse.b = mtrlGreen.dcvAmbient.b = 0.0f;
  284. mtrlGreen.dcvDiffuse.a = mtrlGreen.dcvAmbient.a = 1.0f;
  285. D3DMATERIAL7 mtrlBlue;
  286. ZeroMemory( &mtrlBlue, sizeof(D3DMATERIAL7) );
  287. mtrlBlue.dcvDiffuse.r = mtrlBlue.dcvAmbient.r = 0.0f;
  288. mtrlBlue.dcvDiffuse.g = mtrlBlue.dcvAmbient.g = 0.0f;
  289. mtrlBlue.dcvDiffuse.b = mtrlBlue.dcvAmbient.b = 1.0f;
  290. mtrlBlue.dcvDiffuse.a = mtrlBlue.dcvAmbient.a = 1.0f;
  291. ShowCursor(FALSE);
  292. // Create test window
  293. if (FAILED(hr = CreateTestWindow(hwndMain, &hwnd)))
  294. {
  295. *piStepThatFailed = TESTID_CREATE_TEST_WINDOW;
  296. goto LEnd;
  297. }
  298. // Set cooperative level
  299. if (FAILED(hr = pdd->SetCooperativeLevel(hwnd,
  300. DDSCL_ALLOWREBOOT | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
  301. {
  302. *piStepThatFailed = TESTID_SETCOOPERATIVELEVEL_FULLSCREEN;
  303. goto LEnd;
  304. }
  305. bCooperativeLevelSet = TRUE;
  306. // Set display mode
  307. if (FAILED(hr = pdd->SetDisplayMode(640, 480, 16, 0, 0)))
  308. {
  309. TCHAR szMessage[300];
  310. TCHAR szTitle[100];
  311. pdd->SetCooperativeLevel(hwnd, DDSCL_NORMAL);
  312. bCooperativeLevelSet = FALSE;
  313. SendMessage(hwnd, WM_CLOSE, 0, 0);
  314. hwnd = NULL;
  315. LoadString(NULL, IDS_SETDISPLAYMODEFAILED, szMessage, 300);
  316. LoadString(NULL, IDS_APPFULLNAME, szTitle, 100);
  317. MessageBox(hwndMain, szMessage, szTitle, MB_OK);
  318. *piStepThatFailed = TESTID_SETDISPLAYMODE;
  319. goto LEnd;
  320. }
  321. bDisplayModeSet = TRUE;
  322. // Create front/back buffers
  323. ZeroMemory(&ddsd, sizeof(ddsd));
  324. ddsd.dwSize = sizeof(ddsd);
  325. ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  326. ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP |
  327. DDSCAPS_COMPLEX | DDSCAPS_3DDEVICE;
  328. ddsd.dwBackBufferCount = 1;
  329. if (FAILED(hr = pdd->CreateSurface(&ddsd, &pddsFront, NULL)))
  330. {
  331. *piStepThatFailed = TESTID_CREATEPRIMARYSURFACE_FLIP_ONEBACK;
  332. goto LEnd;
  333. }
  334. if( NULL == pddsFront )
  335. {
  336. *piStepThatFailed = TESTID_CREATEPRIMARYSURFACE_FLIP_ONEBACK;
  337. goto LEnd;
  338. }
  339. ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
  340. if (FAILED(hr = pddsFront->GetSurfaceDesc(&ddsd)))
  341. {
  342. *piStepThatFailed = TESTID_GETDESC;
  343. goto LEnd;
  344. }
  345. dwWidth = ddsd.dwWidth;
  346. dwHeight = ddsd.dwHeight;
  347. SetRect(&rcBack, 0, 0, dwWidth, dwHeight);
  348. // Get ptr to back buffer
  349. ZeroMemory( &ddscaps, sizeof(ddscaps) );
  350. ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
  351. if (FAILED(hr = pddsFront->GetAttachedSurface(&ddscaps, &pddsBack)))
  352. {
  353. *piStepThatFailed = TESTID_GETATTACHEDSURFACE;
  354. goto LEnd;
  355. }
  356. if( NULL == pddsBack )
  357. {
  358. *piStepThatFailed = TESTID_GETATTACHEDSURFACE;
  359. goto LEnd;
  360. }
  361. // Note: no Z-buffer is created...backface culling works for this test
  362. // Get D3D ptr
  363. if (FAILED(hr = pdd->QueryInterface(IID_IDirect3D7, (VOID**)&pd3d)))
  364. {
  365. *piStepThatFailed = TESTID_QUERY_D3D;
  366. goto LEnd;
  367. }
  368. if( NULL == pd3d )
  369. {
  370. *piStepThatFailed = TESTID_QUERY_D3D;
  371. goto LEnd;
  372. }
  373. // Create device
  374. if (FAILED(hr = pd3d->CreateDevice(guid3DDevice, pddsBack, &pd3ddev)))
  375. {
  376. *piStepThatFailed = TESTID_CREATE_DEVICE;
  377. goto LEnd;
  378. }
  379. if( NULL == pd3ddev )
  380. {
  381. *piStepThatFailed = TESTID_CREATE_DEVICE;
  382. goto LEnd;
  383. }
  384. // Set the viewport
  385. vp.dwX = 0;
  386. vp.dwY = 0;
  387. vp.dwWidth = dwWidth;
  388. vp.dwHeight = dwHeight;
  389. vp.dvMinZ = 0.0f;
  390. vp.dvMaxZ = 1.0f;
  391. if (FAILED(hr = pd3ddev->SetViewport(&vp)))
  392. {
  393. *piStepThatFailed = TESTID_SETVIEWPORT;
  394. goto LEnd;
  395. }
  396. // Add a light
  397. ZeroMemory(&lightdata, sizeof(lightdata));
  398. lightdata.dltType = D3DLIGHT_DIRECTIONAL;
  399. lightdata.dcvDiffuse.r = 1.0f;
  400. lightdata.dcvDiffuse.g = 1.0f;
  401. lightdata.dcvDiffuse.b = 1.0f;
  402. lightdata.dvDirection.x = 0.0f;
  403. lightdata.dvDirection.y = 0.0f;
  404. lightdata.dvDirection.z = 1.0f;
  405. if (FAILED(hr = pd3ddev->SetLight( 0, &lightdata)))
  406. {
  407. *piStepThatFailed = TESTID_SETLIGHT;
  408. goto LEnd;
  409. }
  410. if (FAILED(hr = pd3ddev->LightEnable(0, TRUE)))
  411. {
  412. *piStepThatFailed = TESTID_LIGHTENABLE;
  413. goto LEnd;
  414. }
  415. // Set up matrices
  416. mat = D3DMATRIX(1.0f, 0.0f, 0.0f, 0.0f,
  417. 0.0f, 1.0f, 0.0f, 0.0f,
  418. 0.0f, 0.0f, 1.0f, 0.0f,
  419. 0.0f, 0.0f, 0.0f, 1.0f);
  420. if (FAILED(hr = pd3ddev->SetTransform(D3DTRANSFORMSTATE_WORLD, &mat)))
  421. {
  422. *piStepThatFailed = TESTID_SETTRANSFORM;
  423. goto LEnd;
  424. }
  425. mat = D3DMATRIX(1.0f, 0.0f, 0.0f, 0.0f,
  426. 0.0f, 1.0f, 0.0f, 0.0f,
  427. 0.0f, 0.0f, 1.0f, 0.0f,
  428. 0.0f, 0.0f, 5.0f, 1.0f);
  429. if (FAILED(hr = pd3ddev->SetTransform(D3DTRANSFORMSTATE_VIEW, &mat)))
  430. {
  431. *piStepThatFailed = TESTID_SETTRANSFORM;
  432. goto LEnd;
  433. }
  434. D3DUtil_SetProjectionMatrix( mat, 60.0f * 3.14159f / 180.0f, (float) dwHeight / (float) dwWidth, 1.0f, 1000.0f );
  435. if (FAILED(hr = pd3ddev->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &mat)))
  436. {
  437. *piStepThatFailed = TESTID_SETTRANSFORM;
  438. goto LEnd;
  439. }
  440. fRotY = 3.14f;
  441. fRotX = 0.0f;
  442. if (FAILED(hr = pd3ddev->SetRenderState(D3DRENDERSTATE_DITHERENABLE, TRUE)))
  443. {
  444. *piStepThatFailed = TESTID_SETRENDERSTATE;
  445. goto LEnd;
  446. }
  447. if (FAILED(hr = pd3ddev->SetRenderState( D3DRENDERSTATE_AMBIENT, 0x40404040 )))
  448. {
  449. *piStepThatFailed = TESTID_SETRENDERSTATE;
  450. goto LEnd;
  451. }
  452. if( bUseTexture )
  453. {
  454. D3DMATERIAL7 mtrl;
  455. ZeroMemory( &mtrl, sizeof(D3DMATERIAL7) );
  456. mtrl.dcvDiffuse.r = mtrl.dcvAmbient.r = 1.0f;
  457. mtrl.dcvDiffuse.g = mtrl.dcvAmbient.g = 1.0f;
  458. mtrl.dcvDiffuse.b = mtrl.dcvAmbient.b = 1.0f;
  459. mtrl.dcvDiffuse.a = mtrl.dcvAmbient.a = 1.0f;
  460. if (FAILED(hr = pd3ddev->SetMaterial( &mtrl )))
  461. {
  462. *piStepThatFailed = TESTID_SETRENDERSTATE;
  463. goto LEnd;
  464. }
  465. if (FAILED(hr = CreateTexture( &pddsTexture, pdd, pd3ddev, TEXT("DIRECTX"), piStepThatFailed)))
  466. goto LEnd;
  467. if( FAILED( hr = pd3ddev->GetCaps( &ddDesc ) ) )
  468. {
  469. *piStepThatFailed = TESTID_GETDEVICECAPS;
  470. goto LEnd;
  471. }
  472. if( ddDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR )
  473. {
  474. if (FAILED(hr = pd3ddev->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTFN_LINEAR )))
  475. {
  476. *piStepThatFailed = TESTID_SETTEXTURESTAGESTATE;
  477. goto LEnd;
  478. }
  479. }
  480. if( ddDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT )
  481. {
  482. if (FAILED(hr = pd3ddev->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTFN_LINEAR )))
  483. {
  484. *piStepThatFailed = TESTID_SETTEXTURESTAGESTATE;
  485. goto LEnd;
  486. }
  487. }
  488. if (FAILED(hr = pd3ddev->SetTexture( 0, pddsTexture )))
  489. {
  490. *piStepThatFailed = TESTID_SETTEXTURE;
  491. goto LEnd;
  492. }
  493. }
  494. // Here's the draw loop:
  495. MSG msg;
  496. for (i = 0; i < 600; i++)
  497. {
  498. if (PeekMessage(&msg, hwnd, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE))
  499. {
  500. *piStepThatFailed = TESTID_USER_CANCELLED;
  501. goto LEnd;
  502. }
  503. matRotY = D3DMATRIX((FLOAT)cos(fRotY), 0.0f, (FLOAT)sin(fRotY), 0.0f,
  504. 0.0f, 1.0f, 0.0f, 0.0f,
  505. (FLOAT)-sin(fRotY), 0.0f, (FLOAT)cos(fRotY), 0.0f,
  506. 0.0f, 0.0f, 0.0f, 1.0f);
  507. matRotX = D3DMATRIX(1.0f, 0.0f, 0.0f, 0.0f,
  508. 0.0f, (FLOAT)cos(fRotX), (FLOAT)sin(fRotX), 0.0f,
  509. 0.0f, (FLOAT)-sin(fRotX), (FLOAT)cos(fRotX), 0.0f,
  510. 0.0f, 0.0f, 0.0f, 1.0f);
  511. mat = matRotY * matRotX;
  512. if (FAILED(hr = pd3ddev->SetTransform(D3DTRANSFORMSTATE_WORLD, &mat)))
  513. {
  514. *piStepThatFailed = TESTID_SETTRANSFORM;
  515. goto LEnd;
  516. }
  517. if (FAILED(hr = pd3ddev->Clear( 0, NULL, D3DCLEAR_TARGET,
  518. 0x00000000, 1.0f, 0L )))
  519. {
  520. *piStepThatFailed = TESTID_VIEWPORT_CLEAR;
  521. goto LEnd;
  522. }
  523. if (FAILED(hr = pd3ddev->BeginScene()))
  524. {
  525. if( hr == DDERR_SURFACELOST )
  526. {
  527. *piStepThatFailed = TESTID_USER_CANCELLED;
  528. hr = S_OK;
  529. }
  530. else
  531. *piStepThatFailed = TESTID_BEGINSCENE;
  532. goto LEnd;
  533. }
  534. if( !bUseTexture )
  535. {
  536. if (FAILED(hr = pd3ddev->SetMaterial( &mtrlGreen )))
  537. {
  538. *piStepThatFailed = TESTID_SETRENDERSTATE;
  539. goto LEnd;
  540. }
  541. }
  542. if (FAILED(hr = pd3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
  543. D3DFVF_VERTEX, (VOID*)vertexArrayFront, 4, (WORD*)indexArrayFront, 6, 0)))
  544. {
  545. *piStepThatFailed = TESTID_DRAW_INDEXED_PRIMITIVE;
  546. goto LEnd;
  547. }
  548. if (FAILED(hr = pd3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
  549. D3DFVF_VERTEX, (VOID*)vertexArrayBack, 4, (WORD*)indexArrayBack, 6, 0)))
  550. {
  551. *piStepThatFailed = TESTID_DRAW_INDEXED_PRIMITIVE;
  552. goto LEnd;
  553. }
  554. if( !bUseTexture )
  555. {
  556. if (FAILED(hr = pd3ddev->SetMaterial( &mtrlRed )))
  557. {
  558. *piStepThatFailed = TESTID_SETRENDERSTATE;
  559. goto LEnd;
  560. }
  561. }
  562. if (FAILED(hr = pd3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
  563. D3DFVF_VERTEX, (VOID*)vertexArrayLeft, 4, (WORD*)indexArrayLeft, 6, 0)))
  564. {
  565. *piStepThatFailed = TESTID_DRAW_INDEXED_PRIMITIVE;
  566. goto LEnd;
  567. }
  568. if (FAILED(hr = pd3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
  569. D3DFVF_VERTEX, (VOID*)vertexArrayRight, 4, (WORD*)indexArrayRight, 6, 0)))
  570. {
  571. *piStepThatFailed = TESTID_DRAW_INDEXED_PRIMITIVE;
  572. goto LEnd;
  573. }
  574. if( !bUseTexture )
  575. {
  576. if (FAILED(hr = pd3ddev->SetMaterial( &mtrlBlue )))
  577. {
  578. *piStepThatFailed = TESTID_SETRENDERSTATE;
  579. goto LEnd;
  580. }
  581. }
  582. if (FAILED(hr = pd3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
  583. D3DFVF_VERTEX, (VOID*)vertexArrayTop, 4, (WORD*)indexArrayTop, 6, 0)))
  584. {
  585. *piStepThatFailed = TESTID_DRAW_INDEXED_PRIMITIVE;
  586. goto LEnd;
  587. }
  588. if (FAILED(hr = pd3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
  589. D3DFVF_VERTEX, (VOID*)vertexArrayBottom, 4, (WORD*)indexArrayBottom, 6, 0)))
  590. {
  591. *piStepThatFailed = TESTID_DRAW_INDEXED_PRIMITIVE;
  592. goto LEnd;
  593. }
  594. if (FAILED(hr = pd3ddev->EndScene()))
  595. {
  596. *piStepThatFailed = TESTID_ENDSCENE;
  597. goto LEnd;
  598. }
  599. if (FAILED(hr = pddsFront->Flip(NULL, DDFLIP_WAIT)))
  600. {
  601. *piStepThatFailed = TESTID_PRESENT;
  602. goto LEnd;
  603. }
  604. fRotY += 0.05f;
  605. fRotX += 0.02f;
  606. Sleep(10);
  607. }
  608. LEnd:
  609. ShowCursor(TRUE);
  610. ReleasePpo(&pddsTexture);
  611. ReleasePpo(&pd3ddev);
  612. ReleasePpo(&pd3d);
  613. ReleasePpo(&pddsBack);
  614. ReleasePpo(&pddsFront);
  615. if (bCooperativeLevelSet)
  616. {
  617. if (FAILED(hr))
  618. {
  619. // Something has already failed, so report that failure
  620. // rather than any failure of SetCooperativeLevel
  621. pdd->SetCooperativeLevel(hwnd, DDSCL_NORMAL);
  622. }
  623. else
  624. {
  625. if (FAILED(hr = pdd->SetCooperativeLevel(hwnd, DDSCL_NORMAL)))
  626. {
  627. *piStepThatFailed = TESTID_SETCOOPERATIVELEVEL_NORMAL;
  628. }
  629. }
  630. }
  631. if (hwnd != NULL)
  632. SendMessage(hwnd, WM_CLOSE, 0, 0);
  633. if (bDisplayModeSet)
  634. {
  635. if (FAILED(hr))
  636. {
  637. // Something has already failed, so report that failure
  638. // rather than any failure of RestoreDisplayMode
  639. pdd->RestoreDisplayMode();
  640. }
  641. else
  642. {
  643. // Nothing has failed yet, so report any failure of RestoreDisplayMode
  644. if (FAILED(hr = pdd->RestoreDisplayMode()))
  645. return hr;
  646. }
  647. }
  648. return hr;
  649. }
  650. /****************************************************************************
  651. *
  652. * CreateTestWindow
  653. *
  654. ****************************************************************************/
  655. HRESULT CreateTestWindow(HWND hwndMain, HWND* phwnd)
  656. {
  657. static BOOL bClassRegistered = FALSE;
  658. WNDCLASS wndClass;
  659. TCHAR* pszClass = TEXT("DxDiag AGP7 Test Window"); // Don't need to localize
  660. HINSTANCE hInst = (HINSTANCE)GetWindowLongPtr(hwndMain, GWLP_HINSTANCE);
  661. TCHAR szTitle[200];
  662. if (!bClassRegistered)
  663. {
  664. ZeroMemory(&wndClass, sizeof(wndClass));
  665. wndClass.style = CS_HREDRAW | CS_VREDRAW;
  666. wndClass.lpfnWndProc = DefWindowProc;
  667. wndClass.cbClsExtra = 0;
  668. wndClass.cbWndExtra = 0;
  669. wndClass.hInstance = hInst;
  670. wndClass.hIcon = NULL;
  671. wndClass.hCursor = NULL;
  672. wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  673. wndClass.lpszMenuName = NULL;
  674. wndClass.lpszClassName = pszClass;
  675. if (NULL == RegisterClass(&wndClass))
  676. return E_FAIL;
  677. bClassRegistered = TRUE;
  678. }
  679. LoadString(NULL, IDS_APPFULLNAME, szTitle, 200);
  680. *phwnd = CreateWindow(pszClass, szTitle, WS_OVERLAPPED,
  681. 0, 0, 0, 0, hwndMain, NULL, hInst, NULL);
  682. if (*phwnd == NULL)
  683. return E_FAIL;
  684. ShowWindow(*phwnd, SW_SHOW);
  685. return S_OK;
  686. }
  687. //-----------------------------------------------------------------------------
  688. // Name: D3DUtil_SetProjectionMatrix()
  689. // Desc: Sets the passed in 4x4 matrix to a perpsective projection matrix built
  690. // from the field-of-view (fov, in y), aspect ratio, near plane (D),
  691. // and far plane (F). Note that the projection matrix is normalized for
  692. // element [3][4] to be 1.0. This is performed so that W-based range fog
  693. // will work correctly.
  694. //-----------------------------------------------------------------------------
  695. HRESULT D3DUtil_SetProjectionMatrix( D3DMATRIX& mat, FLOAT fFOV, FLOAT fAspect,
  696. FLOAT fNearPlane, FLOAT fFarPlane )
  697. {
  698. if( fabs(fFarPlane-fNearPlane) < 0.01f )
  699. return E_INVALIDARG;
  700. if( fabs(sin(fFOV/2)) < 0.01f )
  701. return E_INVALIDARG;
  702. FLOAT w = fAspect * ( cosf(fFOV/2)/sinf(fFOV/2) );
  703. FLOAT h = 1.0f * ( cosf(fFOV/2)/sinf(fFOV/2) );
  704. FLOAT Q = fFarPlane / ( fFarPlane - fNearPlane );
  705. ZeroMemory( &mat, sizeof(D3DMATRIX) );
  706. mat._11 = w;
  707. mat._22 = h;
  708. mat._33 = Q;
  709. mat._34 = 1.0f;
  710. mat._43 = -Q*fNearPlane;
  711. return S_OK;
  712. }
  713. //-----------------------------------------------------------------------------
  714. // Name: TextureSearchCallback()
  715. // Desc: Enumeration callback routine to find a 16-bit texture format. This
  716. // function is invoked by the ID3DDevice::EnumTextureFormats() function
  717. // to sort through all the available texture formats for a given device.
  718. // The pixel format of each enumerated texture format is passed into the
  719. // "pddpf" parameter. The 2nd parameter is to be used however the app
  720. // sees fit. In this case, we are using it as an output parameter to
  721. // return a normal 16-bit texture format.
  722. //-----------------------------------------------------------------------------
  723. static HRESULT CALLBACK TextureSearchCallback( DDPIXELFORMAT* pddpf, VOID* param )
  724. {
  725. // Note: Return with DDENUMRET_OK to continue enumerating more formats.
  726. // Skip any funky modes
  727. if( pddpf->dwFlags & (DDPF_LUMINANCE|DDPF_BUMPLUMINANCE|DDPF_BUMPDUDV) )
  728. return DDENUMRET_OK;
  729. // Skip any FourCC formats
  730. if( pddpf->dwFourCC != 0 )
  731. return DDENUMRET_OK;
  732. // Skip alpha modes
  733. if( pddpf->dwFlags&DDPF_ALPHAPIXELS )
  734. return DDENUMRET_OK;
  735. // We only want certain format, so skip all others
  736. if( pddpf->dwRGBBitCount == 32 && ((DDPIXELFORMAT*)param)->dwRGBBitCount == 0 ||
  737. pddpf->dwRGBBitCount == 16 )
  738. {
  739. // We found a good match. Copy the current pixel format to our output
  740. // parameter
  741. memcpy( (DDPIXELFORMAT*)param, pddpf, sizeof(DDPIXELFORMAT) );
  742. }
  743. // Have we found the best match?
  744. if( pddpf->dwRGBBitCount == 16 )
  745. return DDENUMRET_CANCEL;
  746. // Keep looking.
  747. return DDENUMRET_OK;
  748. }
  749. //-----------------------------------------------------------------------------
  750. // Name: CreateTexture()
  751. // Desc: Is passed a filename and creates a local Bitmap from that file. Some
  752. // logic and file parsing code could go here to support other image
  753. // file formats.
  754. //-----------------------------------------------------------------------------
  755. HRESULT CreateTexture( LPDIRECTDRAWSURFACE7* ppdds,
  756. LPDIRECTDRAW7 pdd, LPDIRECT3DDEVICE7 pd3dDevice,
  757. TCHAR* strName, LONG* piStepThatFailed)
  758. {
  759. HRESULT hr;
  760. LPDIRECTDRAWSURFACE7 pddsTexture = NULL;
  761. HBITMAP hbm = NULL;
  762. D3DDEVICEDESC7 ddDesc;
  763. BITMAP bm;
  764. DWORD dwWidth;
  765. DWORD dwHeight;
  766. DDSURFACEDESC2 ddsd;
  767. LPDIRECTDRAWSURFACE7 pddsRender = NULL;
  768. HDC hdcTexture = NULL;
  769. HDC hdcBitmap = NULL;
  770. //////////////////////////////////////////////
  771. // Verify args
  772. //////////////////////////////////////////////
  773. if( NULL == ppdds ||
  774. NULL == pdd ||
  775. NULL == pd3dDevice ||
  776. NULL == strName ||
  777. NULL == piStepThatFailed )
  778. {
  779. // Unknown error - shouldn't happen, but this prevent crashs
  780. *piStepThatFailed = 0xFFFF;
  781. return E_FAIL;
  782. }
  783. //////////////////////////////////////////////
  784. // Load image
  785. //////////////////////////////////////////////
  786. hbm = (HBITMAP)LoadImage( GetModuleHandle(NULL), strName,
  787. IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
  788. if( NULL == hbm )
  789. {
  790. *piStepThatFailed = TESTID_CREATETEXTURE;
  791. hr = E_FAIL;
  792. goto LEnd;
  793. }
  794. //////////////////////////////////////////////
  795. // Get caps on device and hbm
  796. //////////////////////////////////////////////
  797. if( FAILED( hr = pd3dDevice->GetCaps( &ddDesc ) ) )
  798. {
  799. *piStepThatFailed = TESTID_GETDEVICECAPS;
  800. goto LEnd;
  801. }
  802. if( 0 == GetObject( hbm, sizeof(BITMAP), &bm ) )
  803. {
  804. *piStepThatFailed = TESTID_CREATETEXTURE;
  805. hr = E_FAIL;
  806. goto LEnd;
  807. }
  808. dwWidth = (DWORD)bm.bmWidth;
  809. dwHeight = (DWORD)bm.bmHeight;
  810. //////////////////////////////////////////////
  811. // Setup the new surface desc for the texture.
  812. //////////////////////////////////////////////
  813. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC2) );
  814. ddsd.dwSize = sizeof(DDSURFACEDESC2);
  815. ddsd.dwFlags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|
  816. DDSD_PIXELFORMAT|DDSD_TEXTURESTAGE;
  817. ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE|DDSCAPS_VIDEOMEMORY|DDSCAPS_NONLOCALVIDMEM;
  818. ddsd.dwWidth = dwWidth;
  819. ddsd.dwHeight = dwHeight;
  820. // Adjust width and height, if the driver requires it
  821. if( ddDesc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2 )
  822. {
  823. for( ddsd.dwWidth=1; dwWidth>ddsd.dwWidth; ddsd.dwWidth<<=1 );
  824. for( ddsd.dwHeight=1; dwHeight>ddsd.dwHeight; ddsd.dwHeight<<=1 );
  825. }
  826. if( ddDesc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_SQUAREONLY )
  827. {
  828. if( ddsd.dwWidth > ddsd.dwHeight )
  829. ddsd.dwHeight = ddsd.dwWidth;
  830. else
  831. ddsd.dwWidth = ddsd.dwHeight;
  832. }
  833. // Look for a 16-bit texture format
  834. if( FAILED( hr = pd3dDevice->EnumTextureFormats( TextureSearchCallback,
  835. &ddsd.ddpfPixelFormat ) ) )
  836. {
  837. *piStepThatFailed = TESTID_ENUMTEXTUREFORMATS;
  838. goto LEnd;
  839. }
  840. if( 0L == ddsd.ddpfPixelFormat.dwRGBBitCount )
  841. {
  842. *piStepThatFailed = TESTID_ENUMTEXTUREFORMATS;
  843. goto LEnd;
  844. }
  845. //////////////////////////////////////////////
  846. // Create a new surface for the texture
  847. //////////////////////////////////////////////
  848. if( FAILED( hr = pdd->CreateSurface( &ddsd, &pddsTexture, NULL ) ) )
  849. {
  850. ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  851. if( FAILED( hr = pdd->CreateSurface( &ddsd, &pddsTexture, NULL ) ) )
  852. {
  853. *piStepThatFailed = TESTID_CREATESURFACE;
  854. goto LEnd;
  855. }
  856. }
  857. if( NULL == pddsTexture )
  858. {
  859. *piStepThatFailed = TESTID_CREATESURFACE;
  860. hr = E_FAIL;
  861. goto LEnd;
  862. }
  863. //////////////////////////////////////////////
  864. // Get DCs from bitmap and surface
  865. //////////////////////////////////////////////
  866. hdcBitmap = CreateCompatibleDC( NULL );
  867. if( NULL == hdcBitmap )
  868. {
  869. *piStepThatFailed = TESTID_CREATETEXTURE;
  870. hr = E_FAIL;
  871. goto LEnd;
  872. }
  873. if( NULL == SelectObject( hdcBitmap, hbm ) )
  874. {
  875. *piStepThatFailed = TESTID_CREATETEXTURE;
  876. hr = E_FAIL;
  877. goto LEnd;
  878. }
  879. // Get a DC for the surface
  880. if( FAILED( hr = pddsTexture->GetDC( &hdcTexture ) ) )
  881. {
  882. *piStepThatFailed = TESTID_GETDC;
  883. goto LEnd;
  884. }
  885. if( NULL == hdcTexture )
  886. {
  887. *piStepThatFailed = TESTID_GETDC;
  888. goto LEnd;
  889. }
  890. //////////////////////////////////////////////
  891. // Copy the bitmap image to the surface.
  892. //////////////////////////////////////////////
  893. if( 0 == BitBlt( hdcTexture, 0, 0, bm.bmWidth, bm.bmHeight, hdcBitmap,
  894. 0, 0, SRCCOPY ) )
  895. {
  896. if( pddsTexture )
  897. {
  898. // Try to release the DC first
  899. pddsTexture->ReleaseDC( hdcTexture );
  900. }
  901. *piStepThatFailed = TESTID_CREATETEXTURE;
  902. hr = E_FAIL;
  903. goto LEnd;
  904. }
  905. //////////////////////////////////////////////
  906. // Cleanup
  907. //////////////////////////////////////////////
  908. if( FAILED( hr = pddsTexture->ReleaseDC( hdcTexture ) ) )
  909. {
  910. *piStepThatFailed = TESTID_RELEASEDC;
  911. goto LEnd;
  912. }
  913. LEnd:
  914. if( hdcBitmap )
  915. DeleteDC( hdcBitmap );
  916. if( hbm )
  917. DeleteObject( hbm );
  918. // Return the newly created texture
  919. // pddsTexture will be cleaned up in parent fn.
  920. *ppdds = pddsTexture;
  921. return hr;
  922. }