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