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