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