mcicda: Exclude unused headers.
[wine] / dlls / d3d9 / tests / stateblock.c
1 /*
2  * Copyright (C) 2005 Henri Verbeet
3  * Copyright (C) 2006 Ivan Gyurdiev
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #define COBJMACROS
21 #include <d3d9.h>
22 #include "wine/test.h"
23
24 static HMODULE d3d9_handle = 0;
25
26 static DWORD texture_stages;
27
28 static HWND create_window(void)
29 {
30     WNDCLASS wc = {0};
31     wc.lpfnWndProc = &DefWindowProc;
32     wc.lpszClassName = "d3d9_test_wc";
33     RegisterClass(&wc);
34
35     return CreateWindow("d3d9_test_wc", "d3d9_test",
36             0, 0, 0, 0, 0, 0, 0, 0, 0);
37 }
38
39 static HRESULT init_d3d9(
40     IDirect3DDevice9** device,
41     D3DPRESENT_PARAMETERS* device_pparams)
42 {
43     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
44     IDirect3D9 *d3d9_ptr = 0;
45     HRESULT hres;
46     HWND window;
47
48     d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
49     ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
50     if (!d3d9_create) return E_FAIL;
51     
52     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
53     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
54     if (!d3d9_ptr) return E_FAIL;
55
56     window = create_window();
57
58     ZeroMemory(device_pparams, sizeof(D3DPRESENT_PARAMETERS));
59     device_pparams->Windowed = TRUE;
60     device_pparams->hDeviceWindow = window;
61     device_pparams->SwapEffect = D3DSWAPEFFECT_DISCARD;
62
63     hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, window,
64         D3DCREATE_SOFTWARE_VERTEXPROCESSING, device_pparams, device);
65     ok(hres == D3D_OK, "IDirect3D_CreateDevice returned: 0x%x\n", hres);
66     return hres;
67 }
68
69 static void test_begin_end_state_block(IDirect3DDevice9 *device_ptr)
70 {
71     HRESULT hret = 0;
72     IDirect3DStateBlock9 *state_block_ptr = 0;
73
74     /* Should succeed */
75     hret = IDirect3DDevice9_BeginStateBlock(device_ptr);
76     ok(hret == D3D_OK, "BeginStateBlock returned: hret 0x%x. Expected hret 0x%x. Aborting.\n", hret, D3D_OK);
77     if (hret != D3D_OK) return;
78
79     /* Calling BeginStateBlock while recording should return D3DERR_INVALIDCALL */
80     hret = IDirect3DDevice9_BeginStateBlock(device_ptr);
81     ok(hret == D3DERR_INVALIDCALL, "BeginStateBlock returned: hret 0x%x. Expected hret 0x%x. Aborting.\n", hret, D3DERR_INVALIDCALL);
82     if (hret != D3DERR_INVALIDCALL) return;
83
84     /* Should succeed */
85     state_block_ptr = (IDirect3DStateBlock9 *)0xdeadbeef;
86     hret = IDirect3DDevice9_EndStateBlock(device_ptr, &state_block_ptr);
87     ok(hret == D3D_OK && state_block_ptr != 0 && state_block_ptr != (IDirect3DStateBlock9 *)0xdeadbeef, 
88         "EndStateBlock returned: hret 0x%x, state_block_ptr %p. "
89         "Expected hret 0x%x, state_block_ptr != %p, state_block_ptr != 0xdeadbeef.\n", hret, state_block_ptr, D3D_OK, NULL);
90
91     /* Calling EndStateBlock while not recording should return D3DERR_INVALIDCALL. state_block_ptr should not be touched. */
92     state_block_ptr = (IDirect3DStateBlock9 *)0xdeadbeef;
93     hret = IDirect3DDevice9_EndStateBlock(device_ptr, &state_block_ptr);
94     ok(hret == D3DERR_INVALIDCALL && state_block_ptr == (IDirect3DStateBlock9 *)0xdeadbeef, 
95         "EndStateBlock returned: hret 0x%x, state_block_ptr %p. "
96         "Expected hret 0x%x, state_block_ptr 0xdeadbeef.\n", hret, state_block_ptr, D3DERR_INVALIDCALL);
97 }
98
99 /* ============================ State Testing Framework ========================== */
100
101 typedef struct state_test {
102     const char* test_name;
103
104     /* The initial data is usually the same
105      * as the default data, but a write can have side effects.
106      * The initial data is tested first, before any writes take place
107      * The default data can be tested after a write */
108     const void* initial_data;
109
110     /* The default data is the standard state to compare
111      * against, and restore to */
112     const void* default_data;
113
114     /* The test data is the experiment data to try
115      * in - what we want to write
116      * out - what windows will actually write (not necessarily the same)  */
117     const void* test_data_in;
118     const void* test_data_out;
119
120     /* The poison data is the data to preinitialize the return buffer to */
121     const void* poison_data;
122     
123     /* Return buffer */
124     void* return_data;
125
126     /* Size of the data samples above */
127     unsigned int data_size;
128
129     /* Test resource management handlers */
130     HRESULT (*setup_handler) (struct state_test* test);
131     void (*teardown_handler) (struct state_test* test);
132
133     /* Test data handlers */
134     void (*set_handler) (IDirect3DDevice9* device, const struct state_test* test, const void* data_in);
135     void (*get_handler) (IDirect3DDevice9* device, const struct state_test* test, void* data_out);
136     void (*print_handler) (const struct state_test* test, const void* data);
137
138     /* Test arguments */
139     const void* test_arg;
140
141     /* Test-specific context data */
142     void* test_context;
143
144 } state_test;
145
146 /* See below for explanation of the flags */
147 #define EVENT_OK             0x00
148 #define EVENT_CHECK_DEFAULT  0x01 
149 #define EVENT_CHECK_INITIAL  0x02
150 #define EVENT_CHECK_TEST     0x04
151 #define EVENT_ERROR          0x08
152 #define EVENT_APPLY_DATA     0x10
153
154 typedef struct event {
155    int (*event_fn) (IDirect3DDevice9* device, void* arg);
156    int status;
157 } event;
158
159 /* This is an event-machine, which tests things.
160  * It tests get and set operations for a batch of states, based on
161  * results from the event function, which directs what's to be done */
162
163 static void execute_test_chain(
164     IDirect3DDevice9* device,
165     state_test* test,
166     unsigned int ntests,
167     event *event,
168     unsigned int nevents,
169     void* event_arg) {
170
171     int outcome;
172     unsigned int i = 0, j;
173
174     /* For each queued event */
175     for (j=0; j < nevents; j++) {
176
177         /* Execute the next event handler (if available) or just set the supplied status */
178         outcome = event[j].status;
179         if (event[j].event_fn)
180             outcome |= event[j].event_fn(device, event_arg);
181
182         /* Now verify correct outcome depending on what was signaled by the handler.
183          * An EVENT_CHECK_TEST signal means check the returned data against the test_data (out).
184          * An EVENT_CHECK_DEFAULT signal means check the returned data against the default_data.
185          * An EVENT_CHECK_INITIAL signal means check the returned data against the initial_data.
186          * An EVENT_ERROR signal means the test isn't going to work, exit the event loop.
187          * An EVENT_APPLY_DATA signal means load the test data (after checks) */
188
189         if (outcome & EVENT_ERROR) {
190             trace("Test %s, Stage %u in error state, aborting\n", test[i].test_name, j);
191             break;
192
193         } else if (outcome & EVENT_CHECK_TEST || outcome & EVENT_CHECK_DEFAULT || outcome & EVENT_CHECK_INITIAL) {
194
195             for (i=0; i < ntests; i++) {
196
197                 memcpy(test[i].return_data, test[i].poison_data, test[i].data_size);
198                 test[i].get_handler(device, &test[i], test[i].return_data);
199
200                 if (outcome & EVENT_CHECK_TEST) {
201
202                     BOOL test_failed = memcmp(test[i].test_data_out, test[i].return_data, test[i].data_size);
203                     ok(!test_failed, "Test %s, Stage %u: returned data does not match test data [csize=%u]\n",
204                         test[i].test_name, j, test[i].data_size);
205
206                     if (test_failed && test[i].print_handler) {
207                         trace("Returned data was:\n");
208                         test[i].print_handler(&test[i], test[i].return_data);
209                         trace("Test data was:\n");
210                         test[i].print_handler(&test[i], test[i].test_data_out);
211                     }
212                 }
213
214                 else if (outcome & EVENT_CHECK_DEFAULT) {
215                   
216                     BOOL test_failed = memcmp(test[i].default_data, test[i].return_data, test[i].data_size);
217                     ok (!test_failed, "Test %s, Stage %u: returned data does not match default data [csize=%u]\n",
218                         test[i].test_name, j, test[i].data_size);
219
220                     if (test_failed && test[i].print_handler) {
221                         trace("Returned data was:\n");
222                         test[i].print_handler(&test[i], test[i].return_data);
223                         trace("Default data was:\n");
224                         test[i].print_handler(&test[i], test[i].default_data);
225                     }
226                 }
227
228                 else if (outcome & EVENT_CHECK_INITIAL) {
229                     
230                     BOOL test_failed = memcmp(test[i].initial_data, test[i].return_data, test[i].data_size);
231                     ok (!test_failed, "Test %s, Stage %u: returned data does not match initial data [csize=%u]\n",
232                         test[i].test_name, j, test[i].data_size);
233
234                     if (test_failed && test[i].print_handler) {
235                         trace("Returned data was:\n");
236                         test[i].print_handler(&test[i], test[i].return_data);
237                         trace("Initial data was:\n");
238                         test[i].print_handler(&test[i], test[i].initial_data);
239                     }
240                 }
241             }
242         }
243
244         if (outcome & EVENT_APPLY_DATA) {
245             for (i=0; i < ntests; i++)
246                 test[i].set_handler(device, &test[i], test[i].test_data_in);
247         }
248      }
249
250      /* Attempt to reset any changes made */
251      for (i=0; i < ntests; i++)
252          test[i].set_handler(device, &test[i], test[i].default_data);
253 }
254
255 typedef struct event_data {
256     IDirect3DStateBlock9* stateblock;
257     IDirect3DSurface9* original_render_target;
258 } event_data;
259
260 static int switch_render_target(
261     IDirect3DDevice9* device,
262     void* data) {
263   
264     HRESULT hret;
265     D3DPRESENT_PARAMETERS present_parameters;
266     event_data* edata = (event_data*) data;
267     IDirect3DSwapChain9* swapchain = NULL;
268     IDirect3DSurface9* backbuffer = NULL;
269
270     /* Parameters for new swapchain */
271     ZeroMemory(&present_parameters, sizeof(present_parameters));
272     present_parameters.Windowed = TRUE;
273     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
274
275     /* Create new swapchain */
276     hret = IDirect3DDevice9_CreateAdditionalSwapChain(device, &present_parameters, &swapchain);
277     ok (hret == D3D_OK, "CreateAdditionalSwapChain returned %#x.\n", hret);
278     if (hret != D3D_OK) goto error;
279
280     /* Get its backbuffer */
281     hret = IDirect3DSwapChain9_GetBackBuffer(swapchain, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
282     ok (hret == D3D_OK, "GetBackBuffer returned %#x.\n", hret);
283     if (hret != D3D_OK) goto error;
284
285     /* Save the current render target */
286     hret = IDirect3DDevice9_GetRenderTarget(device, 0, &edata->original_render_target); 
287     ok (hret == D3D_OK, "GetRenderTarget returned %#x.\n", hret);
288     if (hret != D3D_OK) goto error;
289
290     /* Set the new swapchain's backbuffer as a render target */
291     hret = IDirect3DDevice9_SetRenderTarget(device, 0, backbuffer);
292     ok (hret == D3D_OK, "SetRenderTarget returned %#x.\n", hret);
293     if (hret != D3D_OK) goto error;
294
295     IUnknown_Release(backbuffer);
296     IUnknown_Release(swapchain);
297     return EVENT_OK;
298
299     error:
300     if (backbuffer) IUnknown_Release(backbuffer);
301     if (swapchain) IUnknown_Release(swapchain);
302     return EVENT_ERROR;
303 }
304
305 static int revert_render_target( 
306     IDirect3DDevice9* device,
307     void* data) {
308
309     HRESULT hret;
310     event_data* edata = (event_data*) data;
311    
312     /* Reset the old render target */
313     hret = IDirect3DDevice9_SetRenderTarget(device, 0, edata->original_render_target);
314     ok (hret == D3D_OK, "SetRenderTarget returned %#x.\n", hret);
315     if (hret != D3D_OK) {
316         IUnknown_Release(edata->original_render_target);
317         return EVENT_ERROR;
318     }
319
320     IUnknown_Release(edata->original_render_target);
321     return EVENT_OK;
322 }
323
324 static int begin_stateblock(
325     IDirect3DDevice9* device,
326     void* data) {
327
328     HRESULT hret;
329
330     data = NULL;
331     hret = IDirect3DDevice9_BeginStateBlock(device);
332     ok(hret == D3D_OK, "BeginStateBlock returned %#x.\n", hret);
333     if (hret != D3D_OK) return EVENT_ERROR;
334     return EVENT_OK;
335 }
336
337 static int end_stateblock(
338     IDirect3DDevice9* device,
339     void* data) {
340
341     HRESULT hret;
342     event_data* edata = (event_data*) data;
343
344     hret = IDirect3DDevice9_EndStateBlock(device, &edata->stateblock);
345     ok(hret == D3D_OK, "EndStateBlock returned %#x.\n", hret);
346     if (hret != D3D_OK) return EVENT_ERROR;
347     return EVENT_OK;
348 }
349
350 static int abort_stateblock(
351     IDirect3DDevice9* device,
352     void* data) {
353
354     event_data* edata = (event_data*) data;
355
356     IUnknown_Release(edata->stateblock);
357     return EVENT_OK;
358 }
359
360 static int apply_stateblock(
361     IDirect3DDevice9* device,
362     void* data) {
363
364     event_data* edata = (event_data*) data;
365     HRESULT hret;
366
367     hret = IDirect3DStateBlock9_Apply(edata->stateblock);
368     ok(hret == D3D_OK, "Apply returned %#x.\n", hret);
369     if (hret != D3D_OK) {
370         IUnknown_Release(edata->stateblock);
371         return EVENT_ERROR;
372     }
373
374     IUnknown_Release(edata->stateblock);
375     return EVENT_OK;
376 }
377
378 static int capture_stateblock(
379     IDirect3DDevice9* device,
380     void* data) {
381
382     HRESULT hret;
383     event_data* edata = (event_data*) data;
384
385     hret = IDirect3DStateBlock9_Capture(edata->stateblock);
386     ok(hret == D3D_OK, "Capture returned %#x.\n", hret);
387     if (hret != D3D_OK)
388         return EVENT_ERROR;
389
390     return EVENT_OK;
391 }
392
393 static void execute_test_chain_all(
394     IDirect3DDevice9* device,
395     state_test* test,
396     unsigned int ntests) {
397
398     unsigned int i;
399     event_data arg;
400
401     event read_events[] = {
402         { NULL, EVENT_CHECK_INITIAL }
403     };
404
405     event write_read_events[] = {
406         { NULL, EVENT_APPLY_DATA },
407         { NULL, EVENT_CHECK_TEST }
408     };
409
410     event abort_stateblock_events[] = {
411         { begin_stateblock, EVENT_APPLY_DATA },
412         { end_stateblock, EVENT_OK },
413         { abort_stateblock, EVENT_CHECK_DEFAULT }
414     };
415
416     event apply_stateblock_events[] = {
417         { begin_stateblock, EVENT_APPLY_DATA },
418         { end_stateblock, EVENT_OK },
419         { apply_stateblock, EVENT_CHECK_TEST }
420     };
421
422     event capture_reapply_stateblock_events[] = {
423           { begin_stateblock, EVENT_APPLY_DATA },
424           { end_stateblock, EVENT_OK },
425           { capture_stateblock, EVENT_CHECK_DEFAULT | EVENT_APPLY_DATA },
426           { apply_stateblock, EVENT_CHECK_DEFAULT }
427     };
428
429     event rendertarget_switch_events[] = {
430           { NULL, EVENT_APPLY_DATA },
431           { switch_render_target, EVENT_CHECK_TEST },
432           { revert_render_target, EVENT_OK } 
433     };
434
435     event rendertarget_stateblock_events[] = {
436           { begin_stateblock, EVENT_APPLY_DATA },
437           { switch_render_target, EVENT_CHECK_DEFAULT },
438           { end_stateblock, EVENT_OK },
439           { revert_render_target, EVENT_OK },
440           { apply_stateblock, EVENT_CHECK_TEST }
441     };
442
443     /* Setup each test for execution */
444     for (i=0; i < ntests; i++) {
445         if (test[i].setup_handler(&test[i]) != D3D_OK) {
446             ok(FALSE, "Test \"%s\" failed setup, aborting\n", test[i].test_name);
447             return;
448         }
449     }
450
451     trace("Running initial read state tests\n");
452     execute_test_chain(device, test, ntests, read_events, 1, NULL);
453
454     trace("Running write-read state tests\n");
455     execute_test_chain(device, test, ntests, write_read_events, 2, NULL);
456
457     trace("Running stateblock abort state tests\n");
458     execute_test_chain(device, test, ntests, abort_stateblock_events, 3, &arg);
459
460     trace("Running stateblock apply state tests\n");
461     execute_test_chain(device, test, ntests, apply_stateblock_events, 3, &arg);
462
463     trace("Running stateblock capture/reapply state tests\n");
464     execute_test_chain(device, test, ntests, capture_reapply_stateblock_events, 4, &arg);
465   
466     trace("Running rendertarget switch state tests\n");
467     execute_test_chain(device, test, ntests, rendertarget_switch_events, 3, &arg);
468
469     trace("Running stateblock apply over rendertarget switch interrupt tests\n");
470     execute_test_chain(device, test, ntests, rendertarget_stateblock_events, 5, &arg);
471
472     /* Cleanup resources */
473     for (i=0; i < ntests; i++)
474         test[i].teardown_handler(&test[i]);
475 }
476
477 /* =================== State test: Pixel and Vertex Shader constants ============ */
478
479 typedef struct shader_constant_data {
480     int int_constant[4];     /* 1x4 integer constant */
481     float float_constant[4]; /* 1x4 float constant */
482     BOOL bool_constant[4];   /* 4x1 boolean constants */
483 } shader_constant_data;
484
485 typedef struct shader_constant_arg {
486     unsigned int idx;
487     BOOL pshader;
488 } shader_constant_arg;
489
490 typedef struct shader_constant_context {
491     shader_constant_data return_data_buffer;
492 } shader_constant_context;
493
494 static void shader_constant_print_handler(
495     const state_test* test,
496     const void* data) {
497
498     const shader_constant_data* scdata = data;
499
500     trace("Integer constant = { %#x, %#x, %#x, %#x }\n",
501         scdata->int_constant[0], scdata->int_constant[1],
502         scdata->int_constant[2], scdata->int_constant[3]);
503
504     trace("Float constant = { %f, %f, %f, %f }\n",
505         scdata->float_constant[0], scdata->float_constant[1],
506         scdata->float_constant[2], scdata->float_constant[3]);
507
508     trace("Boolean constants = [ %#x, %#x, %#x, %#x ]\n",
509         scdata->bool_constant[0], scdata->bool_constant[1],
510         scdata->bool_constant[2], scdata->bool_constant[3]);
511 }
512
513 static void shader_constant_set_handler(
514     IDirect3DDevice9* device, const state_test* test, const void* data) {
515
516     HRESULT hret;
517     const shader_constant_data* scdata = data;
518     const shader_constant_arg* scarg = test->test_arg;
519     unsigned int index = scarg->idx;
520
521     if (!scarg->pshader) {
522         hret = IDirect3DDevice9_SetVertexShaderConstantI(device, index, scdata->int_constant, 1);
523         ok(hret == D3D_OK, "SetVertexShaderConstantI returned %#x.\n", hret);
524         hret = IDirect3DDevice9_SetVertexShaderConstantF(device, index, scdata->float_constant, 1);
525         ok(hret == D3D_OK, "SetVertexShaderConstantF returned %#x.\n", hret);
526         hret = IDirect3DDevice9_SetVertexShaderConstantB(device, index, scdata->bool_constant, 4);
527         ok(hret == D3D_OK, "SetVertexShaderConstantB returned %#x.\n", hret);
528
529     } else {
530         hret = IDirect3DDevice9_SetPixelShaderConstantI(device, index, scdata->int_constant, 1);
531         ok(hret == D3D_OK, "SetPixelShaderConstantI returned %#x.\n", hret);
532         hret = IDirect3DDevice9_SetPixelShaderConstantF(device, index, scdata->float_constant, 1);
533         ok(hret == D3D_OK, "SetPixelShaderConstantF returned %#x.\n", hret);
534         hret = IDirect3DDevice9_SetPixelShaderConstantB(device, index, scdata->bool_constant, 4);
535         ok(hret == D3D_OK, "SetPixelShaderConstantB returned %#x.\n", hret);
536     }
537 }
538
539 static void shader_constant_get_handler(
540     IDirect3DDevice9* device, const state_test* test, void* data) {
541
542     HRESULT hret;
543     shader_constant_data* scdata = (shader_constant_data*) data;
544     const shader_constant_arg* scarg = test->test_arg;
545     unsigned int index = scarg->idx;
546
547     if (!scarg->pshader) {
548         hret = IDirect3DDevice9_GetVertexShaderConstantI(device, index, scdata->int_constant, 1);
549         ok(hret == D3D_OK, "GetVertexShaderConstantI returned %#x.\n", hret);
550         hret = IDirect3DDevice9_GetVertexShaderConstantF(device, index, scdata->float_constant, 1);
551         ok(hret == D3D_OK, "GetVertexShaderConstantF returned %#x.\n", hret);
552         hret = IDirect3DDevice9_GetVertexShaderConstantB(device, index, scdata->bool_constant, 4);
553         ok(hret == D3D_OK, "GetVertexShaderConstantB returned %#x.\n", hret);
554
555     } else {
556         hret = IDirect3DDevice9_GetPixelShaderConstantI(device, index, scdata->int_constant, 1);
557         ok(hret == D3D_OK, "GetPixelShaderConstantI returned %#x.\n", hret);
558         hret = IDirect3DDevice9_GetPixelShaderConstantF(device, index, scdata->float_constant, 1);
559         ok(hret == D3D_OK, "GetPixelShaderConstantF returned %#x.\n", hret);
560         hret = IDirect3DDevice9_GetPixelShaderConstantB(device, index, scdata->bool_constant, 4);
561         ok(hret == D3D_OK, "GetPixelShaderConstantB returned %#x.\n", hret);
562     }
563 }
564
565 static const shader_constant_data shader_constant_poison_data = {
566     { 0x1337c0de, 0x1337c0de, 0x1337c0de, 0x1337c0de },
567     { 1.0f, 2.0f, 3.0f, 4.0f },
568     { FALSE, TRUE, FALSE, TRUE }
569 };
570
571 static const shader_constant_data shader_constant_default_data = {
572         { 0, 0, 0, 0 }, { 0.0f, 0.0f, 0.0f, 0.0f }, { 0, 0, 0, 0 }
573 };
574
575 static const shader_constant_data shader_constant_test_data = {
576     { 0xdead0000, 0xdead0001, 0xdead0002, 0xdead0003 },
577     { 0.0f, 0.0f, 0.0f, 0.0f },
578     { TRUE, FALSE, FALSE, TRUE }
579 };
580
581 static HRESULT shader_constant_setup_handler(
582     state_test* test) {
583
584     shader_constant_context *ctx = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(shader_constant_context));
585     if (ctx == NULL) return E_FAIL;
586     test->test_context = ctx;
587      
588     test->return_data = &ctx->return_data_buffer;
589     test->test_data_in = &shader_constant_test_data;
590     test->test_data_out = &shader_constant_test_data;
591     test->default_data = &shader_constant_default_data;
592     test->initial_data = &shader_constant_default_data;
593     test->poison_data = &shader_constant_poison_data;
594
595     test->data_size = sizeof(shader_constant_data);
596     
597     return D3D_OK;
598 }
599
600 static void shader_constant_teardown_handler(
601     state_test* test) {
602     
603     HeapFree(GetProcessHeap(), 0, test->test_context);
604 }
605
606 static void shader_constants_queue_test(
607     state_test* test,
608     const shader_constant_arg* test_arg) {
609
610     test->setup_handler = shader_constant_setup_handler;
611     test->teardown_handler = shader_constant_teardown_handler;
612     test->set_handler = shader_constant_set_handler;
613     test->get_handler = shader_constant_get_handler;
614     test->print_handler = shader_constant_print_handler;
615     test->test_name = test_arg->pshader? "set_get_pshader_constants": "set_get_vshader_constants";
616     test->test_arg = test_arg;
617 }
618
619 /* =================== State test: Lights ===================================== */
620
621 typedef struct light_data {
622     D3DLIGHT9 light;
623     BOOL enabled;
624     HRESULT get_light_result;
625     HRESULT get_enabled_result;
626 } light_data;
627
628 typedef struct light_arg {
629     unsigned int idx;
630 } light_arg;
631
632 typedef struct light_context {
633     light_data return_data_buffer;
634 } light_context;
635
636 static void light_print_handler(
637     const state_test* test,
638     const void* data) {
639
640     const light_data* ldata = data;
641
642     trace("Get Light return value: %#x\n", ldata->get_light_result);
643     trace("Get Light enable return value: %#x\n", ldata->get_enabled_result);
644
645     trace("Light Enabled = %u\n", ldata->enabled);
646     trace("Light Type = %u\n", ldata->light.Type);
647     trace("Light Diffuse = { %f, %f, %f, %f }\n",
648         ldata->light.Diffuse.r, ldata->light.Diffuse.g,
649         ldata->light.Diffuse.b, ldata->light.Diffuse.a);
650     trace("Light Specular = { %f, %f, %f, %f}\n",
651         ldata->light.Specular.r, ldata->light.Specular.g,
652         ldata->light.Specular.b, ldata->light.Specular.a);
653     trace("Light Ambient = { %f, %f, %f, %f }\n",
654         ldata->light.Ambient.r, ldata->light.Ambient.g,
655         ldata->light.Ambient.b, ldata->light.Ambient.a);
656     trace("Light Position = { %f, %f, %f }\n",
657         ldata->light.Position.x, ldata->light.Position.y, ldata->light.Position.z);
658     trace("Light Direction = { %f, %f, %f }\n",
659         ldata->light.Direction.x, ldata->light.Direction.y, ldata->light.Direction.z);
660     trace("Light Range = %f\n", ldata->light.Range);
661     trace("Light Fallof = %f\n", ldata->light.Falloff);
662     trace("Light Attenuation0 = %f\n", ldata->light.Attenuation0);
663     trace("Light Attenuation1 = %f\n", ldata->light.Attenuation1);
664     trace("Light Attenuation2 = %f\n", ldata->light.Attenuation2);
665     trace("Light Theta = %f\n", ldata->light.Theta);
666     trace("Light Phi = %f\n", ldata->light.Phi);
667 }
668
669 static void light_set_handler(
670     IDirect3DDevice9* device, const state_test* test, const void* data) {
671
672     HRESULT hret;
673     const light_data* ldata = data;
674     const light_arg* larg = test->test_arg;
675     unsigned int index = larg->idx;
676
677     hret = IDirect3DDevice9_SetLight(device, index, &ldata->light);
678     ok(hret == D3D_OK, "SetLight returned %#x.\n", hret);
679
680     hret = IDirect3DDevice9_LightEnable(device, index, ldata->enabled);
681     ok(hret == D3D_OK, "SetLightEnable returned %#x.\n", hret);
682 }
683
684 static void light_get_handler(
685     IDirect3DDevice9* device, const state_test* test, void* data) {
686
687     HRESULT hret;
688     light_data* ldata = data;
689     const light_arg* larg = test->test_arg;
690     unsigned int index = larg->idx;
691
692     hret = IDirect3DDevice9_GetLightEnable(device, index, &ldata->enabled);
693     ldata->get_enabled_result = hret;
694
695     hret = IDirect3DDevice9_GetLight(device, index, &ldata->light);
696     ldata->get_light_result = hret;
697 }
698
699 static const light_data light_poison_data = 
700     { { 0x1337c0de,
701         { 7.0, 4.0, 2.0, 1.0 }, { 7.0, 4.0, 2.0, 1.0 }, { 7.0, 4.0, 2.0, 1.0 },
702         { 3.3, 4.4, 5.5 }, { 6.6, 7.7, 8.8 },
703         12.12, 13.13, 14.14, 15.15, 16.16, 17.17, 18.18 }, 1, 0x1337c0de, 0x1337c0de };
704
705 static const light_data light_default_data =
706     { { D3DLIGHT_DIRECTIONAL,
707         { 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 },
708         { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0 },
709         0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, 0, D3D_OK, D3D_OK };
710
711 /* This is used for the initial read state (before a write causes side effects)
712  * The proper return status is D3DERR_INVALIDCALL */
713 static const light_data light_initial_data =
714     { { 0x1337c0de,
715         { 7.0, 4.0, 2.0, 1.0 }, { 7.0, 4.0, 2.0, 1.0 }, { 7.0, 4.0, 2.0, 1.0 },
716         { 3.3, 4.4, 5.5 }, { 6.6, 7.7, 8.8 },
717         12.12, 13.13, 14.14, 15.15, 16.16, 17.17, 18.18 }, 1, D3DERR_INVALIDCALL, D3DERR_INVALIDCALL };
718
719 static const light_data light_test_data_in =
720     { { 1,
721         { 2.0, 2.0, 2.0, 2.0 }, { 3.0, 3.0, 3.0, 3.0 }, { 4.0, 4.0, 4.0, 4.0 },
722         { 5.0, 5.0, 5.0 }, { 6.0, 6.0, 6.0 },
723         7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 }, 1, D3D_OK, D3D_OK};
724
725 /* SetLight will use 128 as the "enabled" value */
726 static const light_data light_test_data_out = 
727     { { 1,
728         { 2.0, 2.0, 2.0, 2.0 }, { 3.0, 3.0, 3.0, 3.0 }, { 4.0, 4.0, 4.0, 4.0 },
729         { 5.0, 5.0, 5.0 }, { 6.0, 6.0, 6.0 },
730         7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 }, 128, D3D_OK, D3D_OK};
731
732 static HRESULT light_setup_handler(
733     state_test* test) {
734      
735     light_context *ctx = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(light_context));
736     if (ctx == NULL) return E_FAIL;
737     test->test_context = ctx;
738  
739     test->return_data = &ctx->return_data_buffer;
740     test->test_data_in = &light_test_data_in;
741     test->test_data_out = &light_test_data_out;
742     test->default_data = &light_default_data;
743     test->initial_data = &light_initial_data;
744     test->poison_data = &light_poison_data;
745
746     test->data_size = sizeof(light_data);
747     
748     return D3D_OK;
749 }
750
751 static void light_teardown_handler(
752     state_test* test) {
753     
754     HeapFree(GetProcessHeap(), 0, test->test_context);
755 }
756
757 static void lights_queue_test(
758     state_test* test,
759     const light_arg* test_arg) {
760
761     test->setup_handler = light_setup_handler;
762     test->teardown_handler = light_teardown_handler;
763     test->set_handler = light_set_handler;
764     test->get_handler = light_get_handler;
765     test->print_handler = light_print_handler;
766     test->test_name = "set_get_light";
767     test->test_arg = test_arg;
768 }
769
770 /* =================== State test: Transforms ===================================== */
771
772 typedef struct transform_data {
773
774     D3DMATRIX view;
775     D3DMATRIX projection;
776     D3DMATRIX texture0;
777     D3DMATRIX texture7;
778     D3DMATRIX world0;
779     D3DMATRIX world255;
780
781 } transform_data;
782
783 typedef struct transform_context {
784     transform_data return_data_buffer;
785 } transform_context;
786
787 static inline void print_matrix(
788     const char* name, const D3DMATRIX* matrix) {
789
790     trace("%s Matrix = {\n", name);
791     trace("    %f %f %f %f\n", U(*matrix).m[0][0], U(*matrix).m[1][0], U(*matrix).m[2][0], U(*matrix).m[3][0]);
792     trace("    %f %f %f %f\n", U(*matrix).m[0][1], U(*matrix).m[1][1], U(*matrix).m[2][1], U(*matrix).m[3][1]);
793     trace("    %f %f %f %f\n", U(*matrix).m[0][2], U(*matrix).m[1][2], U(*matrix).m[2][2], U(*matrix).m[3][2]);
794     trace("    %f %f %f %f\n", U(*matrix).m[0][3], U(*matrix).m[1][3], U(*matrix).m[2][3], U(*matrix).m[3][3]);
795     trace("}\n");
796 }
797
798 static void transform_print_handler(
799     const state_test* test,
800     const void* data) {
801
802     const transform_data* tdata = data;
803
804     print_matrix("View", &tdata->view);
805     print_matrix("Projection", &tdata->projection);
806     print_matrix("Texture0", &tdata->texture0);
807     print_matrix("Texture7", &tdata->texture7);
808     print_matrix("World0", &tdata->world0);
809     print_matrix("World255", &tdata->world255);
810 }
811
812 static void transform_set_handler(
813     IDirect3DDevice9* device, const state_test* test, const void* data) {
814
815     HRESULT hret;
816     const transform_data* tdata = data;
817
818     hret = IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, &tdata->view);
819     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
820
821     hret = IDirect3DDevice9_SetTransform(device, D3DTS_PROJECTION, &tdata->projection);
822     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
823
824     hret = IDirect3DDevice9_SetTransform(device, D3DTS_TEXTURE0, &tdata->texture0);
825     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
826
827     hret = IDirect3DDevice9_SetTransform(device, D3DTS_TEXTURE0 + texture_stages - 1, &tdata->texture7);
828     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
829
830     hret = IDirect3DDevice9_SetTransform(device, D3DTS_WORLD, &tdata->world0);
831     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
832
833     hret = IDirect3DDevice9_SetTransform(device, D3DTS_WORLDMATRIX(255), &tdata->world255);
834     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
835 }
836
837 static void transform_get_handler(
838     IDirect3DDevice9* device, const state_test* test, void* data) {
839
840     HRESULT hret;
841     transform_data* tdata = (transform_data*) data;
842
843     hret = IDirect3DDevice9_GetTransform(device, D3DTS_VIEW, &tdata->view);
844     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
845
846     hret = IDirect3DDevice9_GetTransform(device, D3DTS_PROJECTION, &tdata->projection);
847     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
848
849     hret = IDirect3DDevice9_GetTransform(device, D3DTS_TEXTURE0, &tdata->texture0);
850     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
851
852     hret = IDirect3DDevice9_GetTransform(device, D3DTS_TEXTURE0 + texture_stages - 1, &tdata->texture7);
853     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
854
855     hret = IDirect3DDevice9_GetTransform(device, D3DTS_WORLD, &tdata->world0);
856     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
857
858     hret = IDirect3DDevice9_GetTransform(device, D3DTS_WORLDMATRIX(255), &tdata->world255);
859     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
860 }
861
862 static const transform_data transform_default_data = {
863       { { { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } } },
864       { { { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } } },
865       { { { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } } },
866       { { { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } } },
867       { { { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } } },
868       { { { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } } }
869 };
870
871 static const transform_data transform_poison_data = {
872       { { { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
873         9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 } } },
874
875       { { { 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0,
876         25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0 } } },
877
878       { { { 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0,
879         41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0 } } },
880
881       { { { 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0,
882         57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0 } } },
883
884       { { { 64.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0,
885         73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0 } } },
886
887       { { { 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0,
888         89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0} } },
889 };
890
891 static const transform_data transform_test_data = {
892       { { { 1.2, 3.4, -5.6, 7.2, 10.11, -12.13, 14.15, -1.5,
893         23.56, 12.89, 44.56, -1.0, 2.3, 0.0, 4.4, 5.5 } } },
894
895       { { { 9.2, 38.7, -6.6, 7.2, 10.11, -12.13, 77.15, -1.5,
896         23.56, 12.89, 14.56, -1.0, 12.3, 0.0, 4.4, 5.5 } } },
897
898       { { { 10.2, 3.4, 0.6, 7.2, 10.11, -12.13, 14.15, -1.5,
899         23.54, 12.9, 44.56, -1.0, 2.3, 0.0, 4.4, 5.5 } } },
900
901       { { { 1.2, 3.4, -5.6, 7.2, 10.11, -12.13, -14.5, -1.5,
902         2.56, 12.89, 23.56, -1.0, 112.3, 0.0, 4.4, 2.5 } } },
903
904       { { { 1.2, 31.41, 58.6, 7.2, 10.11, -12.13, -14.5, -1.5,
905         2.56, 12.89, 11.56, -1.0, 112.3, 0.0, 44.4, 2.5 } } },
906
907       { { { 1.20, 3.4, -5.6, 7.0, 10.11, -12.156, -14.5, -1.5,
908         2.56, 1.829, 23.6, -1.0, 112.3, 0.0, 41.4, 2.5 } } },
909 };
910
911 static HRESULT transform_setup_handler(
912     state_test* test) {
913
914     transform_context *ctx = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(transform_context));
915     if (ctx == NULL) return E_FAIL;
916     test->test_context = ctx;
917
918     test->return_data = &ctx->return_data_buffer;
919     test->test_data_in = &transform_test_data;
920     test->test_data_out = &transform_test_data;
921     test->default_data = &transform_default_data;
922     test->initial_data = &transform_default_data;
923     test->poison_data = &transform_poison_data;
924
925     test->data_size = sizeof(transform_data);
926
927     return D3D_OK;
928 }
929
930 static void transform_teardown_handler(
931     state_test* test) {
932     
933     HeapFree(GetProcessHeap(), 0, test->test_context);
934 }
935
936 static void transform_queue_test(
937     state_test* test) {
938
939     test->setup_handler = transform_setup_handler;
940     test->teardown_handler = transform_teardown_handler;
941     test->set_handler = transform_set_handler;
942     test->get_handler = transform_get_handler;
943     test->print_handler = transform_print_handler;
944     test->test_name = "set_get_transforms";
945     test->test_arg = NULL;
946 }
947
948 /* =================== State test: Render States ===================================== */
949
950 #define D3D9_RENDER_STATES 102
951 const D3DRENDERSTATETYPE render_state_indices[] = {
952
953     D3DRS_ZENABLE,
954     D3DRS_FILLMODE,
955     D3DRS_SHADEMODE,
956     D3DRS_ZWRITEENABLE,
957     D3DRS_ALPHATESTENABLE,
958     D3DRS_LASTPIXEL,
959     D3DRS_SRCBLEND,
960     D3DRS_DESTBLEND,
961     D3DRS_CULLMODE,
962     D3DRS_ZFUNC,
963     D3DRS_ALPHAREF,
964     D3DRS_ALPHAFUNC,
965     D3DRS_DITHERENABLE,
966     D3DRS_ALPHABLENDENABLE,
967     D3DRS_FOGENABLE,
968     D3DRS_SPECULARENABLE,
969     D3DRS_FOGCOLOR,
970     D3DRS_FOGTABLEMODE,
971     D3DRS_FOGSTART,
972     D3DRS_FOGEND,
973     D3DRS_FOGDENSITY,
974     D3DRS_RANGEFOGENABLE,
975     D3DRS_STENCILENABLE,
976     D3DRS_STENCILFAIL,
977     D3DRS_STENCILZFAIL,
978     D3DRS_STENCILPASS,
979     D3DRS_STENCILFUNC,
980     D3DRS_STENCILREF,
981     D3DRS_STENCILMASK,
982     D3DRS_STENCILWRITEMASK,
983     D3DRS_TEXTUREFACTOR,
984     D3DRS_WRAP0,
985     D3DRS_WRAP1,
986     D3DRS_WRAP2,
987     D3DRS_WRAP3,
988     D3DRS_WRAP4,
989     D3DRS_WRAP5,
990     D3DRS_WRAP6,
991     D3DRS_WRAP7,
992     D3DRS_CLIPPING,
993     D3DRS_LIGHTING,
994     D3DRS_AMBIENT,
995     D3DRS_FOGVERTEXMODE,
996     D3DRS_COLORVERTEX,
997     D3DRS_LOCALVIEWER,
998     D3DRS_NORMALIZENORMALS,
999     D3DRS_DIFFUSEMATERIALSOURCE,
1000     D3DRS_SPECULARMATERIALSOURCE,
1001     D3DRS_AMBIENTMATERIALSOURCE,
1002     D3DRS_EMISSIVEMATERIALSOURCE,
1003     D3DRS_VERTEXBLEND,
1004     D3DRS_CLIPPLANEENABLE,
1005 #if 0 /* Driver dependent, increase array size to enable */
1006     D3DRS_POINTSIZE,
1007 #endif
1008     D3DRS_POINTSIZE_MIN,
1009     D3DRS_POINTSPRITEENABLE,
1010     D3DRS_POINTSCALEENABLE,
1011     D3DRS_POINTSCALE_A,
1012     D3DRS_POINTSCALE_B,
1013     D3DRS_POINTSCALE_C,
1014     D3DRS_MULTISAMPLEANTIALIAS,
1015     D3DRS_MULTISAMPLEMASK,
1016     D3DRS_PATCHEDGESTYLE,
1017     D3DRS_DEBUGMONITORTOKEN,
1018     D3DRS_POINTSIZE_MAX,
1019     D3DRS_INDEXEDVERTEXBLENDENABLE,
1020     D3DRS_COLORWRITEENABLE,
1021     D3DRS_TWEENFACTOR,
1022     D3DRS_BLENDOP,
1023     D3DRS_POSITIONDEGREE,
1024     D3DRS_NORMALDEGREE,
1025     D3DRS_SCISSORTESTENABLE,
1026     D3DRS_SLOPESCALEDEPTHBIAS,
1027     D3DRS_ANTIALIASEDLINEENABLE,
1028     D3DRS_MINTESSELLATIONLEVEL,
1029     D3DRS_MAXTESSELLATIONLEVEL,
1030     D3DRS_ADAPTIVETESS_X,
1031     D3DRS_ADAPTIVETESS_Y,
1032     D3DRS_ADAPTIVETESS_Z,
1033     D3DRS_ADAPTIVETESS_W,
1034     D3DRS_ENABLEADAPTIVETESSELLATION,
1035     D3DRS_TWOSIDEDSTENCILMODE,
1036     D3DRS_CCW_STENCILFAIL,
1037     D3DRS_CCW_STENCILZFAIL,
1038     D3DRS_CCW_STENCILPASS,
1039     D3DRS_CCW_STENCILFUNC,
1040     D3DRS_COLORWRITEENABLE1,
1041     D3DRS_COLORWRITEENABLE2,
1042     D3DRS_COLORWRITEENABLE3,
1043     D3DRS_BLENDFACTOR,
1044     D3DRS_SRGBWRITEENABLE,
1045     D3DRS_DEPTHBIAS,
1046     D3DRS_WRAP8,
1047     D3DRS_WRAP9,
1048     D3DRS_WRAP10,
1049     D3DRS_WRAP11,
1050     D3DRS_WRAP12,
1051     D3DRS_WRAP13,
1052     D3DRS_WRAP14,
1053     D3DRS_WRAP15,
1054     D3DRS_SEPARATEALPHABLENDENABLE,
1055     D3DRS_SRCBLENDALPHA,
1056     D3DRS_DESTBLENDALPHA,
1057     D3DRS_BLENDOPALPHA,
1058 };
1059
1060 typedef struct render_state_data {
1061     DWORD states[D3D9_RENDER_STATES];
1062 } render_state_data;
1063
1064 typedef struct render_state_arg {
1065     D3DPRESENT_PARAMETERS* device_pparams;
1066 } render_state_arg;
1067
1068 typedef struct render_state_context {
1069    render_state_data return_data_buffer;
1070    render_state_data default_data_buffer;
1071    render_state_data test_data_buffer;
1072    render_state_data poison_data_buffer;
1073 } render_state_context;
1074
1075 static void render_state_set_handler(
1076     IDirect3DDevice9* device, const state_test* test, const void* data) {
1077
1078     HRESULT hret;
1079     const render_state_data* rsdata = data;
1080     unsigned int i;
1081
1082     for (i = 0; i < D3D9_RENDER_STATES; i++) {
1083         hret = IDirect3DDevice9_SetRenderState(device, render_state_indices[i], rsdata->states[i]);
1084         ok(hret == D3D_OK, "SetRenderState returned %#x.\n", hret);
1085     }
1086 }
1087
1088 static void render_state_get_handler(
1089     IDirect3DDevice9* device, const state_test* test, void* data) {
1090
1091     HRESULT hret;
1092     render_state_data* rsdata = (render_state_data*) data;
1093     unsigned int i = 0;
1094
1095     for (i = 0; i < D3D9_RENDER_STATES; i++) {
1096         hret = IDirect3DDevice9_GetRenderState(device, render_state_indices[i], &rsdata->states[i]);
1097         ok(hret == D3D_OK, "GetRenderState returned %#x.\n", hret);
1098     }
1099 }
1100
1101 static void render_state_print_handler(
1102     const state_test* test,
1103     const void* data) {
1104
1105     const render_state_data* rsdata = data;
1106
1107     unsigned int i;
1108     for (i = 0; i < D3D9_RENDER_STATES; i++)
1109         trace("Index = %u, Value = %#x\n", i, rsdata->states[i]);
1110 }
1111
1112 static inline DWORD to_dword(float fl) {
1113     return *((DWORD*) &fl);
1114 }
1115
1116 static void render_state_default_data_init(
1117     D3DPRESENT_PARAMETERS* device_pparams,
1118     render_state_data* data) {
1119
1120    unsigned int idx = 0;
1121
1122    data->states[idx++] = device_pparams->EnableAutoDepthStencil?
1123                          D3DZB_TRUE : D3DZB_FALSE;  /* ZENABLE */
1124    data->states[idx++] = D3DFILL_SOLID;         /* FILLMODE */
1125    data->states[idx++] = D3DSHADE_GOURAUD;      /* SHADEMODE */
1126    data->states[idx++] = TRUE;                  /* ZWRITEENABLE */
1127    data->states[idx++] = FALSE;                 /* ALPHATESTENABLE */
1128    data->states[idx++] = TRUE;                  /* LASTPIXEL */
1129    data->states[idx++] = D3DBLEND_ONE;          /* SRCBLEND */
1130    data->states[idx++] = D3DBLEND_ZERO;         /* DESTBLEND */
1131    data->states[idx++] = D3DCULL_CCW;           /* CULLMODE */
1132    data->states[idx++] = D3DCMP_LESSEQUAL;      /* ZFUNC */
1133    data->states[idx++] = 0;                     /* ALPHAREF */
1134    data->states[idx++] = D3DCMP_ALWAYS;         /* ALPHAFUNC */
1135    data->states[idx++] = FALSE;                 /* DITHERENABLE */
1136    data->states[idx++] = FALSE;                 /* ALPHABLENDENABLE */
1137    data->states[idx++] = FALSE;                 /* FOGENABLE */
1138    data->states[idx++] = FALSE;                 /* SPECULARENABLE */
1139    data->states[idx++] = 0;                     /* FOGCOLOR */
1140    data->states[idx++] = D3DFOG_NONE;           /* FOGTABLEMODE */
1141    data->states[idx++] = to_dword(0.0f);        /* FOGSTART */
1142    data->states[idx++] = to_dword(1.0f);        /* FOGEND */
1143    data->states[idx++] = to_dword(1.0f);        /* FOGDENSITY */
1144    data->states[idx++] = FALSE;                 /* RANGEFOGENABLE */
1145    data->states[idx++] = FALSE;                 /* STENCILENABLE */
1146    data->states[idx++] = D3DSTENCILOP_KEEP;     /* STENCILFAIL */
1147    data->states[idx++] = D3DSTENCILOP_KEEP;     /* STENCILZFAIL */
1148    data->states[idx++] = D3DSTENCILOP_KEEP;     /* STENCILPASS */
1149    data->states[idx++] = D3DCMP_ALWAYS;         /* STENCILFUNC */
1150    data->states[idx++] = 0;                     /* STENCILREF */
1151    data->states[idx++] = 0xFFFFFFFF;            /* STENCILMASK */
1152    data->states[idx++] = 0xFFFFFFFF;            /* STENCILWRITEMASK */
1153    data->states[idx++] = 0xFFFFFFFF;            /* TEXTUREFACTOR */
1154    data->states[idx++] = 0;                     /* WRAP 0 */
1155    data->states[idx++] = 0;                     /* WRAP 1 */
1156    data->states[idx++] = 0;                     /* WRAP 2 */
1157    data->states[idx++] = 0;                     /* WRAP 3 */
1158    data->states[idx++] = 0;                     /* WRAP 4 */
1159    data->states[idx++] = 0;                     /* WRAP 5 */
1160    data->states[idx++] = 0;                     /* WRAP 6 */
1161    data->states[idx++] = 0;                     /* WRAP 7 */
1162    data->states[idx++] = TRUE;                  /* CLIPPING */
1163    data->states[idx++] = TRUE;                  /* LIGHTING */
1164    data->states[idx++] = 0;                     /* AMBIENT */
1165    data->states[idx++] = D3DFOG_NONE;           /* FOGVERTEXMODE */
1166    data->states[idx++] = TRUE;                  /* COLORVERTEX */
1167    data->states[idx++] = TRUE;                  /* LOCALVIEWER */
1168    data->states[idx++] = FALSE;                 /* NORMALIZENORMALS */
1169    data->states[idx++] = D3DMCS_COLOR1;         /* DIFFUSEMATERIALSOURCE */
1170    data->states[idx++] = D3DMCS_COLOR2;         /* SPECULARMATERIALSOURCE */
1171    data->states[idx++] = D3DMCS_MATERIAL;       /* AMBIENTMATERIALSOURCE */
1172    data->states[idx++] = D3DMCS_MATERIAL;       /* EMISSIVEMATERIALSOURCE */
1173    data->states[idx++] = D3DVBF_DISABLE;        /* VERTEXBLEND */
1174    data->states[idx++] = 0;                     /* CLIPPLANEENABLE */
1175 #if 0 /* Driver dependent, increase array size to enable */
1176    data->states[idx++] = to_dword(1.0f);       /* POINTSIZE */
1177 #endif
1178    data->states[idx++] = to_dword(1.0f);        /* POINTSIZEMIN */
1179    data->states[idx++] = FALSE;                 /* POINTSPRITEENABLE */
1180    data->states[idx++] = FALSE;                 /* POINTSCALEENABLE */
1181    data->states[idx++] = to_dword(1.0f);        /* POINTSCALE_A */
1182    data->states[idx++] = to_dword(0.0f);        /* POINTSCALE_B */
1183    data->states[idx++] = to_dword(0.0f);        /* POINTSCALE_C */
1184    data->states[idx++] = TRUE;                  /* MULTISAMPLEANTIALIAS */
1185    data->states[idx++] = 0xFFFFFFFF;            /* MULTISAMPLEMASK */
1186    data->states[idx++] = D3DPATCHEDGE_DISCRETE; /* PATCHEDGESTYLE */ 
1187    data->states[idx++] = 0xbaadcafe;            /* DEBUGMONITORTOKEN */
1188    data->states[idx++] = to_dword(64.0f);       /* POINTSIZE_MAX */
1189    data->states[idx++] = FALSE;                 /* INDEXEDVERTEXBLENDENABLE */
1190    data->states[idx++] = 0x0000000F;            /* COLORWRITEENABLE */
1191    data->states[idx++] = to_dword(0.0f);        /* TWEENFACTOR */
1192    data->states[idx++] = D3DBLENDOP_ADD;        /* BLENDOP */
1193    data->states[idx++] = D3DDEGREE_CUBIC;       /* POSITIONDEGREE */
1194    data->states[idx++] = D3DDEGREE_LINEAR;      /* NORMALDEGREE */
1195    data->states[idx++] = FALSE;                 /* SCISSORTESTENABLE */
1196    data->states[idx++] = to_dword(0.0f);        /* SLOPESCALEDEPTHBIAS */
1197    data->states[idx++] = FALSE;                 /* ANTIALIASEDLINEENABLE */
1198    data->states[idx++] = to_dword(1.0f);        /* MINTESSELATIONLEVEL */
1199    data->states[idx++] = to_dword(1.0f);        /* MAXTESSELATIONLEVEL */
1200    data->states[idx++] = to_dword(0.0f);        /* ADAPTIVETESS_X */
1201    data->states[idx++] = to_dword(0.0f);        /* ADAPTIVETESS_Y */
1202    data->states[idx++] = to_dword(1.0f);        /* ADAPTIVETESS_Z */
1203    data->states[idx++] = to_dword(0.0f);        /* ADAPTIVETESS_W */
1204    data->states[idx++] = FALSE;                 /* ENABLEADAPTIVETESSELATION */
1205    data->states[idx++] = FALSE;                 /* TWOSIDEDSTENCILMODE */
1206    data->states[idx++] = D3DSTENCILOP_KEEP;     /* CCW_STENCILFAIL */
1207    data->states[idx++] = D3DSTENCILOP_KEEP;     /* CCW_STENCILZFAIL */
1208    data->states[idx++] = D3DSTENCILOP_KEEP;     /* CCW_STENCILPASS */
1209    data->states[idx++] = D3DCMP_ALWAYS;         /* CCW_STENCILFUNC */
1210    data->states[idx++] = 0x0000000F;            /* COLORWRITEENABLE1 */
1211    data->states[idx++] = 0x0000000F;            /* COLORWRITEENABLE2 */
1212    data->states[idx++] = 0x0000000F;            /* COLORWRITEENABLE3 */
1213    data->states[idx++] = 0xFFFFFFFF;            /* BLENDFACTOR */
1214    data->states[idx++] = 0;                     /* SRGBWRITEENABLE */
1215    data->states[idx++] = to_dword(0.0f);        /* DEPTHBIAS */
1216    data->states[idx++] = 0;                     /* WRAP8 */
1217    data->states[idx++] = 0;                     /* WRAP9 */
1218    data->states[idx++] = 0;                     /* WRAP10 */
1219    data->states[idx++] = 0;                     /* WRAP11 */
1220    data->states[idx++] = 0;                     /* WRAP12 */
1221    data->states[idx++] = 0;                     /* WRAP13 */
1222    data->states[idx++] = 0;                     /* WRAP14 */
1223    data->states[idx++] = 0;                     /* WRAP15 */
1224    data->states[idx++] = FALSE;                 /* SEPARATEALPHABLENDENABLE */
1225    data->states[idx++] = D3DBLEND_ONE;          /* SRCBLENDALPHA */
1226    data->states[idx++] = D3DBLEND_ZERO;         /* DESTBLENDALPHA */
1227    data->states[idx++] = TRUE;                  /* BLENDOPALPHA */
1228 }
1229
1230 static void render_state_poison_data_init(
1231     render_state_data* data) {
1232
1233    unsigned int i;
1234    for (i = 0; i < D3D9_RENDER_STATES; i++)
1235        data->states[i] = 0x1337c0de;
1236 }
1237  
1238 static void render_state_test_data_init(
1239     render_state_data* data) {
1240
1241    unsigned int idx = 0;
1242    data->states[idx++] = D3DZB_USEW;            /* ZENABLE */
1243    data->states[idx++] = D3DFILL_WIREFRAME;     /* FILLMODE */
1244    data->states[idx++] = D3DSHADE_PHONG;        /* SHADEMODE */
1245    data->states[idx++] = FALSE;                 /* ZWRITEENABLE */
1246    data->states[idx++] = TRUE;                  /* ALPHATESTENABLE */
1247    data->states[idx++] = FALSE;                 /* LASTPIXEL */
1248    data->states[idx++] = D3DBLEND_SRCALPHASAT;  /* SRCBLEND */
1249    data->states[idx++] = D3DBLEND_INVDESTALPHA; /* DESTBLEND */
1250    data->states[idx++] = D3DCULL_CW;            /* CULLMODE */
1251    data->states[idx++] = D3DCMP_NOTEQUAL;       /* ZFUNC */
1252    data->states[idx++] = 10;                    /* ALPHAREF */
1253    data->states[idx++] = D3DCMP_GREATER;        /* ALPHAFUNC */
1254    data->states[idx++] = TRUE;                  /* DITHERENABLE */
1255    data->states[idx++] = TRUE;                  /* ALPHABLENDENABLE */
1256    data->states[idx++] = TRUE;                  /* FOGENABLE */
1257    data->states[idx++] = TRUE;                  /* SPECULARENABLE */
1258    data->states[idx++] = 255 << 31;             /* FOGCOLOR */
1259    data->states[idx++] = D3DFOG_EXP;            /* FOGTABLEMODE */
1260    data->states[idx++] = to_dword(0.1f);        /* FOGSTART */
1261    data->states[idx++] = to_dword(0.8f);        /* FOGEND */
1262    data->states[idx++] = to_dword(0.5f);        /* FOGDENSITY */
1263    data->states[idx++] = TRUE;                  /* RANGEFOGENABLE */
1264    data->states[idx++] = TRUE;                  /* STENCILENABLE */
1265    data->states[idx++] = D3DSTENCILOP_INCRSAT;  /* STENCILFAIL */
1266    data->states[idx++] = D3DSTENCILOP_REPLACE;  /* STENCILZFAIL */
1267    data->states[idx++] = D3DSTENCILOP_INVERT;   /* STENCILPASS */
1268    data->states[idx++] = D3DCMP_LESS;           /* STENCILFUNC */
1269    data->states[idx++] = 10;                    /* STENCILREF */
1270    data->states[idx++] = 0xFF00FF00;            /* STENCILMASK */
1271    data->states[idx++] = 0x00FF00FF;            /* STENCILWRITEMASK */
1272    data->states[idx++] = 0xF0F0F0F0;            /* TEXTUREFACTOR */
1273    data->states[idx++] = D3DWRAPCOORD_0 | D3DWRAPCOORD_2;                                   /* WRAP 0 */
1274    data->states[idx++] = D3DWRAPCOORD_1 | D3DWRAPCOORD_3;                                   /* WRAP 1 */
1275    data->states[idx++] = D3DWRAPCOORD_2 | D3DWRAPCOORD_3;                                   /* WRAP 2 */
1276    data->states[idx++] = D3DWRAPCOORD_3 | D3DWRAPCOORD_0;                                   /* WRAP 4 */
1277    data->states[idx++] = D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2;                  /* WRAP 5 */
1278    data->states[idx++] = D3DWRAPCOORD_1 | D3DWRAPCOORD_3 | D3DWRAPCOORD_2;                  /* WRAP 6 */
1279    data->states[idx++] = D3DWRAPCOORD_2 | D3DWRAPCOORD_1 | D3DWRAPCOORD_0;                  /* WRAP 7 */
1280    data->states[idx++] = D3DWRAPCOORD_1 | D3DWRAPCOORD_0 | D3DWRAPCOORD_2 | D3DWRAPCOORD_3; /* WRAP 8 */
1281    data->states[idx++] = FALSE;                 /* CLIPPING */
1282    data->states[idx++] = FALSE;                 /* LIGHTING */
1283    data->states[idx++] = 255 << 16;             /* AMBIENT */
1284    data->states[idx++] = D3DFOG_EXP2;           /* FOGVERTEXMODE */
1285    data->states[idx++] = FALSE;                 /* COLORVERTEX */
1286    data->states[idx++] = FALSE;                 /* LOCALVIEWER */
1287    data->states[idx++] = TRUE;                  /* NORMALIZENORMALS */
1288    data->states[idx++] = D3DMCS_COLOR2;         /* DIFFUSEMATERIALSOURCE */
1289    data->states[idx++] = D3DMCS_MATERIAL;       /* SPECULARMATERIALSOURCE */
1290    data->states[idx++] = D3DMCS_COLOR1;         /* AMBIENTMATERIALSOURCE */
1291    data->states[idx++] = D3DMCS_COLOR2;         /* EMISSIVEMATERIALSOURCE */
1292    data->states[idx++] = D3DVBF_3WEIGHTS;       /* VERTEXBLEND */
1293    data->states[idx++] = 0xf1f1f1f1;            /* CLIPPLANEENABLE */
1294 #if 0 /* Driver dependent, increase array size to enable */
1295    data->states[idx++] = to_dword(32.0f);       /* POINTSIZE */
1296 #endif
1297    data->states[idx++] = to_dword(0.7f);        /* POINTSIZEMIN */
1298    data->states[idx++] = TRUE;                  /* POINTSPRITEENABLE */
1299    data->states[idx++] = TRUE;                  /* POINTSCALEENABLE */
1300    data->states[idx++] = to_dword(0.7f);        /* POINTSCALE_A */
1301    data->states[idx++] = to_dword(0.5f);        /* POINTSCALE_B */
1302    data->states[idx++] = to_dword(0.4f);        /* POINTSCALE_C */
1303    data->states[idx++] = FALSE;                 /* MULTISAMPLEANTIALIAS */
1304    data->states[idx++] = 0xABCDDBCA;            /* MULTISAMPLEMASK */
1305    data->states[idx++] = D3DPATCHEDGE_CONTINUOUS; /* PATCHEDGESTYLE */
1306    data->states[idx++] = D3DDMT_DISABLE;        /* DEBUGMONITORTOKEN */
1307    data->states[idx++] = to_dword(77.0f);       /* POINTSIZE_MAX */
1308    data->states[idx++] = TRUE;                  /* INDEXEDVERTEXBLENDENABLE */
1309    data->states[idx++] = 0x00000009;            /* COLORWRITEENABLE */
1310    data->states[idx++] = to_dword(0.2f);        /* TWEENFACTOR */
1311    data->states[idx++] = D3DBLENDOP_REVSUBTRACT;/* BLENDOP */
1312    data->states[idx++] = D3DDEGREE_LINEAR;      /* POSITIONDEGREE */
1313    data->states[idx++] = D3DDEGREE_CUBIC;       /* NORMALDEGREE */
1314    data->states[idx++] = TRUE;                  /* SCISSORTESTENABLE */
1315    data->states[idx++] = to_dword(0.33f);       /* SLOPESCALEDEPTHBIAS */
1316    data->states[idx++] = TRUE;                  /* ANTIALIASEDLINEENABLE */
1317    data->states[idx++] = to_dword(0.8f);        /* MINTESSELATIONLEVEL */
1318    data->states[idx++] = to_dword(0.8f);        /* MAXTESSELATIONLEVEL */
1319    data->states[idx++] = to_dword(0.2f);        /* ADAPTIVETESS_X */
1320    data->states[idx++] = to_dword(0.3f);        /* ADAPTIVETESS_Y */
1321    data->states[idx++] = to_dword(0.6f);        /* ADAPTIVETESS_Z */
1322    data->states[idx++] = to_dword(0.4f);        /* ADAPTIVETESS_W */
1323    data->states[idx++] = TRUE;                  /* ENABLEADAPTIVETESSELATION */
1324    data->states[idx++] = TRUE;                  /* TWOSIDEDSTENCILMODE */
1325    data->states[idx++] = D3DSTENCILOP_ZERO;     /* CCW_STENCILFAIL */
1326    data->states[idx++] = D3DSTENCILOP_DECR;     /* CCW_STENCILZFAIL */
1327    data->states[idx++] = D3DSTENCILOP_INCR;     /* CCW_STENCILPASS */
1328    data->states[idx++] = D3DCMP_ALWAYS;         /* CCW_STENCILFUNC */
1329    data->states[idx++] = 0x00000007;            /* COLORWRITEENABLE1 */
1330    data->states[idx++] = 0x00000008;            /* COLORWRITEENABLE2 */
1331    data->states[idx++] = 0x00000004;            /* COLORWRITEENABLE3 */
1332    data->states[idx++] = 0xF0F1F2F3;            /* BLENDFACTOR */
1333    data->states[idx++] = 1;                     /* SRGBWRITEENABLE */
1334    data->states[idx++] = to_dword(0.3f);        /* DEPTHBIAS */
1335    data->states[idx++] = D3DWRAPCOORD_0 | D3DWRAPCOORD_2;                                   /* WRAP 8 */
1336    data->states[idx++] = D3DWRAPCOORD_1 | D3DWRAPCOORD_3;                                   /* WRAP 9 */
1337    data->states[idx++] = D3DWRAPCOORD_2 | D3DWRAPCOORD_3;                                   /* WRAP 10 */
1338    data->states[idx++] = D3DWRAPCOORD_3 | D3DWRAPCOORD_0;                                   /* WRAP 11 */
1339    data->states[idx++] = D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2;                  /* WRAP 12 */
1340    data->states[idx++] = D3DWRAPCOORD_1 | D3DWRAPCOORD_3 | D3DWRAPCOORD_2;                  /* WRAP 13 */
1341    data->states[idx++] = D3DWRAPCOORD_2 | D3DWRAPCOORD_1 | D3DWRAPCOORD_0;                  /* WRAP 14 */
1342    data->states[idx++] = D3DWRAPCOORD_1 | D3DWRAPCOORD_0 | D3DWRAPCOORD_2 | D3DWRAPCOORD_3; /* WRAP 15 */
1343    data->states[idx++] = TRUE;                  /* SEPARATEALPHABLENDENABLE */
1344    data->states[idx++] = D3DBLEND_ZERO;         /* SRCBLENDALPHA */
1345    data->states[idx++] = D3DBLEND_ONE;          /* DESTBLENDALPHA */
1346    data->states[idx++] = FALSE;                 /* BLENDOPALPHA */
1347 }
1348
1349 static HRESULT render_state_setup_handler(
1350     state_test* test) {
1351
1352     const render_state_arg* rsarg = test->test_arg;
1353
1354     render_state_context *ctx = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(render_state_context));
1355     if (ctx == NULL) return E_FAIL;
1356     test->test_context = ctx;
1357
1358     test->return_data = &ctx->return_data_buffer;
1359     test->default_data = &ctx->default_data_buffer;
1360     test->initial_data = &ctx->default_data_buffer;
1361     test->test_data_in = &ctx->test_data_buffer;
1362     test->test_data_out = &ctx->test_data_buffer;
1363     test->poison_data = &ctx->poison_data_buffer;
1364
1365     render_state_default_data_init(rsarg->device_pparams, &ctx->default_data_buffer);
1366     render_state_test_data_init(&ctx->test_data_buffer);
1367     render_state_poison_data_init(&ctx->poison_data_buffer);
1368
1369     test->data_size = sizeof(render_state_data);
1370
1371     return D3D_OK;
1372 }
1373
1374 static void render_state_teardown_handler(
1375     state_test* test) {
1376
1377     HeapFree(GetProcessHeap(), 0, test->test_context);
1378 }
1379
1380 static void render_states_queue_test(
1381     state_test* test,
1382     const render_state_arg* test_arg) {
1383
1384     test->setup_handler = render_state_setup_handler;
1385     test->teardown_handler = render_state_teardown_handler;
1386     test->set_handler = render_state_set_handler;
1387     test->get_handler = render_state_get_handler;
1388     test->print_handler = render_state_print_handler;
1389     test->test_name = "set_get_render_states";
1390     test->test_arg = test_arg;
1391 }
1392
1393 /* =================== Main state tests function =============================== */
1394
1395 static void test_state_management(
1396     IDirect3DDevice9 *device,
1397     D3DPRESENT_PARAMETERS *device_pparams) {
1398
1399     HRESULT hret;
1400     D3DCAPS9 caps;
1401
1402     /* Test count: 2 for shader constants 
1403                    1 for lights
1404                    1 for transforms
1405                    1 for render states
1406      */
1407     const int max_tests = 5;
1408     state_test tests[5];
1409     unsigned int tcount = 0;
1410
1411     shader_constant_arg pshader_constant_arg;
1412     shader_constant_arg vshader_constant_arg;
1413     render_state_arg render_state_arg;
1414     light_arg light_arg;
1415
1416     hret = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1417     ok(hret == D3D_OK, "GetDeviceCaps returned %#x.\n", hret);
1418     if (hret != D3D_OK) return;
1419
1420     texture_stages = caps.MaxTextureBlendStages;
1421
1422     /* Zero test memory */
1423     memset(tests, 0, sizeof(state_test) * max_tests);
1424
1425     if (caps.VertexShaderVersion & 0xffff) {
1426         vshader_constant_arg.idx = 0;
1427         vshader_constant_arg.pshader = FALSE;
1428         shader_constants_queue_test(&tests[tcount], &vshader_constant_arg);
1429         tcount++;
1430     }
1431
1432     if (caps.PixelShaderVersion & 0xffff) {
1433         pshader_constant_arg.idx = 0;
1434         pshader_constant_arg.pshader = TRUE;
1435         shader_constants_queue_test(&tests[tcount], &pshader_constant_arg);
1436         tcount++;
1437     }
1438
1439     light_arg.idx = 0;
1440     lights_queue_test(&tests[tcount], &light_arg);
1441     tcount++;
1442
1443     transform_queue_test(&tests[tcount]);
1444     tcount++;
1445
1446     render_state_arg.device_pparams = device_pparams;
1447     render_states_queue_test(&tests[tcount], &render_state_arg);
1448     tcount++;
1449
1450     execute_test_chain_all(device, tests, tcount);
1451 }
1452
1453 START_TEST(stateblock)
1454 {
1455     IDirect3DDevice9 *device_ptr = NULL;
1456     D3DPRESENT_PARAMETERS device_pparams;
1457     HRESULT hret;
1458
1459     d3d9_handle = LoadLibraryA("d3d9.dll");
1460     if (!d3d9_handle)
1461     {
1462         skip("Could not load d3d9.dll\n");
1463         return;
1464     }
1465
1466     hret = init_d3d9(&device_ptr, &device_pparams);
1467     if (hret != D3D_OK) return;
1468
1469     test_begin_end_state_block(device_ptr);
1470     test_state_management(device_ptr, &device_pparams);
1471
1472     if (device_ptr) IUnknown_Release(device_ptr);
1473 }