d3d9/tests: Limit the back buffer to 800x600 in the scissor size 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(
267     IDirect3DDevice9* device,
268     void* data) {
269   
270     HRESULT hret;
271     D3DPRESENT_PARAMETERS present_parameters;
272     event_data* edata = data;
273     IDirect3DSwapChain9* swapchain = NULL;
274     IDirect3DSurface9* backbuffer = NULL;
275
276     /* Parameters for new swapchain */
277     ZeroMemory(&present_parameters, sizeof(present_parameters));
278     present_parameters.Windowed = TRUE;
279     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
280
281     /* Create new swapchain */
282     hret = IDirect3DDevice9_CreateAdditionalSwapChain(device, &present_parameters, &swapchain);
283     ok (hret == D3D_OK, "CreateAdditionalSwapChain returned %#x.\n", hret);
284     if (hret != D3D_OK) goto error;
285
286     /* Get its backbuffer */
287     hret = IDirect3DSwapChain9_GetBackBuffer(swapchain, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
288     ok (hret == D3D_OK, "GetBackBuffer returned %#x.\n", hret);
289     if (hret != D3D_OK) goto error;
290
291     /* Save the current render target */
292     hret = IDirect3DDevice9_GetRenderTarget(device, 0, &edata->original_render_target); 
293     ok (hret == D3D_OK, "GetRenderTarget returned %#x.\n", hret);
294     if (hret != D3D_OK) goto error;
295
296     /* Set the new swapchain's backbuffer as a render target */
297     hret = IDirect3DDevice9_SetRenderTarget(device, 0, backbuffer);
298     ok (hret == D3D_OK, "SetRenderTarget returned %#x.\n", hret);
299     if (hret != D3D_OK) goto error;
300
301     IUnknown_Release(backbuffer);
302     edata->new_swap_chain = swapchain;
303     return EVENT_OK;
304
305     error:
306     if (backbuffer) IUnknown_Release(backbuffer);
307     if (swapchain) IUnknown_Release(swapchain);
308     return EVENT_ERROR;
309 }
310
311 static int revert_render_target( 
312     IDirect3DDevice9* device,
313     void* data) {
314
315     HRESULT hret;
316     event_data* edata = data;
317
318     /* Reset the old render target */
319     hret = IDirect3DDevice9_SetRenderTarget(device, 0, edata->original_render_target);
320     ok (hret == D3D_OK, "SetRenderTarget returned %#x.\n", hret);
321     if (hret != D3D_OK) {
322         IUnknown_Release(edata->original_render_target);
323         return EVENT_ERROR;
324     }
325
326     IUnknown_Release(edata->original_render_target);
327
328     IUnknown_Release(edata->new_swap_chain);
329     return EVENT_OK;
330 }
331
332 static int begin_stateblock(
333     IDirect3DDevice9* device,
334     void* data) {
335
336     HRESULT hret;
337
338     data = NULL;
339     hret = IDirect3DDevice9_BeginStateBlock(device);
340     ok(hret == D3D_OK, "BeginStateBlock returned %#x.\n", hret);
341     if (hret != D3D_OK) return EVENT_ERROR;
342     return EVENT_OK;
343 }
344
345 static int end_stateblock(
346     IDirect3DDevice9* device,
347     void* data) {
348
349     HRESULT hret;
350     event_data* edata = data;
351
352     hret = IDirect3DDevice9_EndStateBlock(device, &edata->stateblock);
353     ok(hret == D3D_OK, "EndStateBlock returned %#x.\n", hret);
354     if (hret != D3D_OK) return EVENT_ERROR;
355     return EVENT_OK;
356 }
357
358 static int abort_stateblock(
359     IDirect3DDevice9* device,
360     void* data) {
361
362     event_data* edata = data;
363
364     IUnknown_Release(edata->stateblock);
365     return EVENT_OK;
366 }
367
368 static int apply_stateblock(
369     IDirect3DDevice9* device,
370     void* data) {
371
372     event_data* edata = data;
373     HRESULT hret;
374
375     hret = IDirect3DStateBlock9_Apply(edata->stateblock);
376     ok(hret == D3D_OK, "Apply returned %#x.\n", hret);
377     if (hret != D3D_OK) {
378         IUnknown_Release(edata->stateblock);
379         return EVENT_ERROR;
380     }
381
382     IUnknown_Release(edata->stateblock);
383     return EVENT_OK;
384 }
385
386 static int capture_stateblock(
387     IDirect3DDevice9* device,
388     void* data) {
389
390     HRESULT hret;
391     event_data* edata = data;
392
393     hret = IDirect3DStateBlock9_Capture(edata->stateblock);
394     ok(hret == D3D_OK, "Capture returned %#x.\n", hret);
395     if (hret != D3D_OK)
396         return EVENT_ERROR;
397
398     return EVENT_OK;
399 }
400
401 static void execute_test_chain_all(
402     IDirect3DDevice9* device,
403     state_test* test,
404     unsigned int ntests) {
405
406     unsigned int i;
407     event_data arg;
408
409     event read_events[] = {
410         { NULL, EVENT_CHECK_INITIAL }
411     };
412
413     event write_read_events[] = {
414         { NULL, EVENT_APPLY_DATA },
415         { NULL, EVENT_CHECK_TEST }
416     };
417
418     event abort_stateblock_events[] = {
419         { begin_stateblock, EVENT_APPLY_DATA },
420         { end_stateblock, EVENT_OK },
421         { abort_stateblock, EVENT_CHECK_DEFAULT }
422     };
423
424     event apply_stateblock_events[] = {
425         { begin_stateblock, EVENT_APPLY_DATA },
426         { end_stateblock, EVENT_OK },
427         { apply_stateblock, EVENT_CHECK_TEST }
428     };
429
430     event capture_reapply_stateblock_events[] = {
431           { begin_stateblock, EVENT_APPLY_DATA },
432           { end_stateblock, EVENT_OK },
433           { capture_stateblock, EVENT_CHECK_DEFAULT | EVENT_APPLY_DATA },
434           { apply_stateblock, EVENT_CHECK_DEFAULT }
435     };
436
437     event rendertarget_switch_events[] = {
438           { NULL, EVENT_APPLY_DATA },
439           { switch_render_target, EVENT_CHECK_TEST },
440           { revert_render_target, EVENT_OK } 
441     };
442
443     event rendertarget_stateblock_events[] = {
444           { begin_stateblock, EVENT_APPLY_DATA },
445           { switch_render_target, EVENT_CHECK_DEFAULT },
446           { end_stateblock, EVENT_OK },
447           { revert_render_target, EVENT_OK },
448           { apply_stateblock, EVENT_CHECK_TEST }
449     };
450
451     /* Setup each test for execution */
452     for (i=0; i < ntests; i++) {
453         if (test[i].setup_handler(&test[i]) != D3D_OK) {
454             ok(FALSE, "Test \"%s\" failed setup, aborting\n", test[i].test_name);
455             return;
456         }
457     }
458
459     trace("Running initial read state tests\n");
460     execute_test_chain(device, test, ntests, read_events, 1, NULL);
461
462     trace("Running write-read state tests\n");
463     execute_test_chain(device, test, ntests, write_read_events, 2, NULL);
464
465     trace("Running stateblock abort state tests\n");
466     execute_test_chain(device, test, ntests, abort_stateblock_events, 3, &arg);
467
468     trace("Running stateblock apply state tests\n");
469     execute_test_chain(device, test, ntests, apply_stateblock_events, 3, &arg);
470
471     trace("Running stateblock capture/reapply state tests\n");
472     execute_test_chain(device, test, ntests, capture_reapply_stateblock_events, 4, &arg);
473   
474     trace("Running rendertarget switch state tests\n");
475     execute_test_chain(device, test, ntests, rendertarget_switch_events, 3, &arg);
476
477     trace("Running stateblock apply over rendertarget switch interrupt tests\n");
478     execute_test_chain(device, test, ntests, rendertarget_stateblock_events, 5, &arg);
479
480     /* Cleanup resources */
481     for (i=0; i < ntests; i++)
482         test[i].teardown_handler(&test[i]);
483 }
484
485 /* =================== State test: Pixel and Vertex Shader constants ============ */
486
487 typedef struct shader_constant_data {
488     int int_constant[4];     /* 1x4 integer constant */
489     float float_constant[4]; /* 1x4 float constant */
490     BOOL bool_constant[4];   /* 4x1 boolean constants */
491 } shader_constant_data;
492
493 typedef struct shader_constant_arg {
494     unsigned int idx;
495     BOOL pshader;
496 } shader_constant_arg;
497
498 typedef struct shader_constant_context {
499     shader_constant_data return_data_buffer;
500 } shader_constant_context;
501
502 static void shader_constant_print_handler(
503     const state_test* test,
504     const void* data) {
505
506     const shader_constant_data* scdata = data;
507
508     trace("Integer constant = { %#x, %#x, %#x, %#x }\n",
509         scdata->int_constant[0], scdata->int_constant[1],
510         scdata->int_constant[2], scdata->int_constant[3]);
511
512     trace("Float constant = { %f, %f, %f, %f }\n",
513         scdata->float_constant[0], scdata->float_constant[1],
514         scdata->float_constant[2], scdata->float_constant[3]);
515
516     trace("Boolean constants = [ %#x, %#x, %#x, %#x ]\n",
517         scdata->bool_constant[0], scdata->bool_constant[1],
518         scdata->bool_constant[2], scdata->bool_constant[3]);
519 }
520
521 static void shader_constant_set_handler(
522     IDirect3DDevice9* device, const state_test* test, const void* data) {
523
524     HRESULT hret;
525     const shader_constant_data* scdata = data;
526     const shader_constant_arg* scarg = test->test_arg;
527     unsigned int index = scarg->idx;
528
529     if (!scarg->pshader) {
530         hret = IDirect3DDevice9_SetVertexShaderConstantI(device, index, scdata->int_constant, 1);
531         ok(hret == D3D_OK, "SetVertexShaderConstantI returned %#x.\n", hret);
532         hret = IDirect3DDevice9_SetVertexShaderConstantF(device, index, scdata->float_constant, 1);
533         ok(hret == D3D_OK, "SetVertexShaderConstantF returned %#x.\n", hret);
534         hret = IDirect3DDevice9_SetVertexShaderConstantB(device, index, scdata->bool_constant, 4);
535         ok(hret == D3D_OK, "SetVertexShaderConstantB returned %#x.\n", hret);
536
537     } else {
538         hret = IDirect3DDevice9_SetPixelShaderConstantI(device, index, scdata->int_constant, 1);
539         ok(hret == D3D_OK, "SetPixelShaderConstantI returned %#x.\n", hret);
540         hret = IDirect3DDevice9_SetPixelShaderConstantF(device, index, scdata->float_constant, 1);
541         ok(hret == D3D_OK, "SetPixelShaderConstantF returned %#x.\n", hret);
542         hret = IDirect3DDevice9_SetPixelShaderConstantB(device, index, scdata->bool_constant, 4);
543         ok(hret == D3D_OK, "SetPixelShaderConstantB returned %#x.\n", hret);
544     }
545 }
546
547 static void shader_constant_get_handler(
548     IDirect3DDevice9* device, const state_test* test, void* data) {
549
550     HRESULT hret;
551     shader_constant_data* scdata = data;
552     const shader_constant_arg* scarg = test->test_arg;
553     unsigned int index = scarg->idx;
554
555     if (!scarg->pshader) {
556         hret = IDirect3DDevice9_GetVertexShaderConstantI(device, index, scdata->int_constant, 1);
557         ok(hret == D3D_OK, "GetVertexShaderConstantI returned %#x.\n", hret);
558         hret = IDirect3DDevice9_GetVertexShaderConstantF(device, index, scdata->float_constant, 1);
559         ok(hret == D3D_OK, "GetVertexShaderConstantF returned %#x.\n", hret);
560         hret = IDirect3DDevice9_GetVertexShaderConstantB(device, index, scdata->bool_constant, 4);
561         ok(hret == D3D_OK, "GetVertexShaderConstantB returned %#x.\n", hret);
562
563     } else {
564         hret = IDirect3DDevice9_GetPixelShaderConstantI(device, index, scdata->int_constant, 1);
565         ok(hret == D3D_OK, "GetPixelShaderConstantI returned %#x.\n", hret);
566         hret = IDirect3DDevice9_GetPixelShaderConstantF(device, index, scdata->float_constant, 1);
567         ok(hret == D3D_OK, "GetPixelShaderConstantF returned %#x.\n", hret);
568         hret = IDirect3DDevice9_GetPixelShaderConstantB(device, index, scdata->bool_constant, 4);
569         ok(hret == D3D_OK, "GetPixelShaderConstantB returned %#x.\n", hret);
570     }
571 }
572
573 static const shader_constant_data shader_constant_poison_data = {
574     { 0x1337c0de, 0x1337c0de, 0x1337c0de, 0x1337c0de },
575     { 1.0f, 2.0f, 3.0f, 4.0f },
576     { FALSE, TRUE, FALSE, TRUE }
577 };
578
579 static const shader_constant_data shader_constant_default_data = {
580         { 0, 0, 0, 0 }, { 0.0f, 0.0f, 0.0f, 0.0f }, { 0, 0, 0, 0 }
581 };
582
583 static const shader_constant_data shader_constant_test_data = {
584     { 0xdead0000, 0xdead0001, 0xdead0002, 0xdead0003 },
585     { 5.0f, 6.0f, 7.0f, 8.0f },
586     { TRUE, FALSE, FALSE, TRUE }
587 };
588
589 static HRESULT shader_constant_setup_handler(
590     state_test* test) {
591
592     shader_constant_context *ctx = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(shader_constant_context));
593     if (ctx == NULL) return E_FAIL;
594     test->test_context = ctx;
595      
596     test->return_data = &ctx->return_data_buffer;
597     test->test_data_in = &shader_constant_test_data;
598     test->test_data_out = &shader_constant_test_data;
599     test->default_data = &shader_constant_default_data;
600     test->initial_data = &shader_constant_default_data;
601     test->poison_data = &shader_constant_poison_data;
602
603     test->data_size = sizeof(shader_constant_data);
604     
605     return D3D_OK;
606 }
607
608 static void shader_constant_teardown_handler(
609     state_test* test) {
610     
611     HeapFree(GetProcessHeap(), 0, test->test_context);
612 }
613
614 static void shader_constants_queue_test(
615     state_test* test,
616     const shader_constant_arg* test_arg) {
617
618     test->setup_handler = shader_constant_setup_handler;
619     test->teardown_handler = shader_constant_teardown_handler;
620     test->set_handler = shader_constant_set_handler;
621     test->get_handler = shader_constant_get_handler;
622     test->print_handler = shader_constant_print_handler;
623     test->test_name = test_arg->pshader? "set_get_pshader_constants": "set_get_vshader_constants";
624     test->test_arg = test_arg;
625 }
626
627 /* =================== State test: Lights ===================================== */
628
629 typedef struct light_data {
630     D3DLIGHT9 light;
631     BOOL enabled;
632     HRESULT get_light_result;
633     HRESULT get_enabled_result;
634 } light_data;
635
636 typedef struct light_arg {
637     unsigned int idx;
638 } light_arg;
639
640 typedef struct light_context {
641     light_data return_data_buffer;
642 } light_context;
643
644 static void light_print_handler(
645     const state_test* test,
646     const void* data) {
647
648     const light_data* ldata = data;
649
650     trace("Get Light return value: %#x\n", ldata->get_light_result);
651     trace("Get Light enable return value: %#x\n", ldata->get_enabled_result);
652
653     trace("Light Enabled = %u\n", ldata->enabled);
654     trace("Light Type = %u\n", ldata->light.Type);
655     trace("Light Diffuse = { %f, %f, %f, %f }\n",
656         ldata->light.Diffuse.r, ldata->light.Diffuse.g,
657         ldata->light.Diffuse.b, ldata->light.Diffuse.a);
658     trace("Light Specular = { %f, %f, %f, %f}\n",
659         ldata->light.Specular.r, ldata->light.Specular.g,
660         ldata->light.Specular.b, ldata->light.Specular.a);
661     trace("Light Ambient = { %f, %f, %f, %f }\n",
662         ldata->light.Ambient.r, ldata->light.Ambient.g,
663         ldata->light.Ambient.b, ldata->light.Ambient.a);
664     trace("Light Position = { %f, %f, %f }\n",
665         ldata->light.Position.x, ldata->light.Position.y, ldata->light.Position.z);
666     trace("Light Direction = { %f, %f, %f }\n",
667         ldata->light.Direction.x, ldata->light.Direction.y, ldata->light.Direction.z);
668     trace("Light Range = %f\n", ldata->light.Range);
669     trace("Light Fallof = %f\n", ldata->light.Falloff);
670     trace("Light Attenuation0 = %f\n", ldata->light.Attenuation0);
671     trace("Light Attenuation1 = %f\n", ldata->light.Attenuation1);
672     trace("Light Attenuation2 = %f\n", ldata->light.Attenuation2);
673     trace("Light Theta = %f\n", ldata->light.Theta);
674     trace("Light Phi = %f\n", ldata->light.Phi);
675 }
676
677 static void light_set_handler(
678     IDirect3DDevice9* device, const state_test* test, const void* data) {
679
680     HRESULT hret;
681     const light_data* ldata = data;
682     const light_arg* larg = test->test_arg;
683     unsigned int index = larg->idx;
684
685     hret = IDirect3DDevice9_SetLight(device, index, &ldata->light);
686     ok(hret == D3D_OK, "SetLight returned %#x.\n", hret);
687
688     hret = IDirect3DDevice9_LightEnable(device, index, ldata->enabled);
689     ok(hret == D3D_OK, "SetLightEnable returned %#x.\n", hret);
690 }
691
692 static void light_get_handler(
693     IDirect3DDevice9* device, const state_test* test, void* data) {
694
695     HRESULT hret;
696     light_data* ldata = data;
697     const light_arg* larg = test->test_arg;
698     unsigned int index = larg->idx;
699
700     hret = IDirect3DDevice9_GetLightEnable(device, index, &ldata->enabled);
701     ldata->get_enabled_result = hret;
702
703     hret = IDirect3DDevice9_GetLight(device, index, &ldata->light);
704     ldata->get_light_result = hret;
705 }
706
707 static const light_data light_poison_data = 
708     { { 0x1337c0de,
709         { 7.0, 4.0, 2.0, 1.0 }, { 7.0, 4.0, 2.0, 1.0 }, { 7.0, 4.0, 2.0, 1.0 },
710         { 3.3f, 4.4f, 5.5f },{ 6.6f, 7.7f, 8.8f },
711         12.12f, 13.13f, 14.14f, 15.15f, 16.16f, 17.17f, 18.18f },
712         1, 0x1337c0de, 0x1337c0de };
713
714 static const light_data light_default_data =
715     { { D3DLIGHT_DIRECTIONAL,
716         { 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 },
717         { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0 },
718         0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, 0, D3D_OK, D3D_OK };
719
720 /* This is used for the initial read state (before a write causes side effects)
721  * The proper return status is D3DERR_INVALIDCALL */
722 static const light_data light_initial_data =
723     { { 0x1337c0de,
724         { 7.0, 4.0, 2.0, 1.0 }, { 7.0, 4.0, 2.0, 1.0 }, { 7.0, 4.0, 2.0, 1.0 },
725         { 3.3f, 4.4f, 5.5f }, { 6.6f, 7.7f, 8.8f },
726         12.12f, 13.13f, 14.14f, 15.15f, 16.16f, 17.17f, 18.18f },
727         1, D3DERR_INVALIDCALL, D3DERR_INVALIDCALL };
728
729 static const light_data light_test_data_in =
730     { { 1,
731         { 2.0, 2.0, 2.0, 2.0 }, { 3.0, 3.0, 3.0, 3.0 }, { 4.0, 4.0, 4.0, 4.0 },
732         { 5.0, 5.0, 5.0 }, { 6.0, 6.0, 6.0 },
733         7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 }, 1, D3D_OK, D3D_OK};
734
735 /* SetLight will use 128 as the "enabled" value */
736 static const light_data light_test_data_out = 
737     { { 1,
738         { 2.0, 2.0, 2.0, 2.0 }, { 3.0, 3.0, 3.0, 3.0 }, { 4.0, 4.0, 4.0, 4.0 },
739         { 5.0, 5.0, 5.0 }, { 6.0, 6.0, 6.0 },
740         7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 }, 128, D3D_OK, D3D_OK};
741
742 static HRESULT light_setup_handler(
743     state_test* test) {
744      
745     light_context *ctx = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(light_context));
746     if (ctx == NULL) return E_FAIL;
747     test->test_context = ctx;
748  
749     test->return_data = &ctx->return_data_buffer;
750     test->test_data_in = &light_test_data_in;
751     test->test_data_out = &light_test_data_out;
752     test->default_data = &light_default_data;
753     test->initial_data = &light_initial_data;
754     test->poison_data = &light_poison_data;
755
756     test->data_size = sizeof(light_data);
757     
758     return D3D_OK;
759 }
760
761 static void light_teardown_handler(
762     state_test* test) {
763     
764     HeapFree(GetProcessHeap(), 0, test->test_context);
765 }
766
767 static void lights_queue_test(
768     state_test* test,
769     const light_arg* test_arg) {
770
771     test->setup_handler = light_setup_handler;
772     test->teardown_handler = light_teardown_handler;
773     test->set_handler = light_set_handler;
774     test->get_handler = light_get_handler;
775     test->print_handler = light_print_handler;
776     test->test_name = "set_get_light";
777     test->test_arg = test_arg;
778 }
779
780 /* =================== State test: Transforms ===================================== */
781
782 typedef struct transform_data {
783
784     D3DMATRIX view;
785     D3DMATRIX projection;
786     D3DMATRIX texture0;
787     D3DMATRIX texture7;
788     D3DMATRIX world0;
789     D3DMATRIX world255;
790
791 } transform_data;
792
793 typedef struct transform_context {
794     transform_data return_data_buffer;
795 } transform_context;
796
797 static inline void print_matrix(
798     const char* name, const D3DMATRIX* matrix) {
799
800     trace("%s Matrix = {\n", name);
801     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]);
802     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]);
803     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]);
804     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]);
805     trace("}\n");
806 }
807
808 static void transform_print_handler(
809     const state_test* test,
810     const void* data) {
811
812     const transform_data* tdata = data;
813
814     print_matrix("View", &tdata->view);
815     print_matrix("Projection", &tdata->projection);
816     print_matrix("Texture0", &tdata->texture0);
817     print_matrix("Texture7", &tdata->texture7);
818     print_matrix("World0", &tdata->world0);
819     print_matrix("World255", &tdata->world255);
820 }
821
822 static void transform_set_handler(
823     IDirect3DDevice9* device, const state_test* test, const void* data) {
824
825     HRESULT hret;
826     const transform_data* tdata = data;
827
828     hret = IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, &tdata->view);
829     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
830
831     hret = IDirect3DDevice9_SetTransform(device, D3DTS_PROJECTION, &tdata->projection);
832     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
833
834     hret = IDirect3DDevice9_SetTransform(device, D3DTS_TEXTURE0, &tdata->texture0);
835     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
836
837     hret = IDirect3DDevice9_SetTransform(device, D3DTS_TEXTURE0 + texture_stages - 1, &tdata->texture7);
838     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
839
840     hret = IDirect3DDevice9_SetTransform(device, D3DTS_WORLD, &tdata->world0);
841     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
842
843     hret = IDirect3DDevice9_SetTransform(device, D3DTS_WORLDMATRIX(255), &tdata->world255);
844     ok(hret == D3D_OK, "SetTransform returned %#x.\n", hret);
845 }
846
847 static void transform_get_handler(
848     IDirect3DDevice9* device, const state_test* test, void* data) {
849
850     HRESULT hret;
851     transform_data* tdata = data;
852
853     hret = IDirect3DDevice9_GetTransform(device, D3DTS_VIEW, &tdata->view);
854     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
855
856     hret = IDirect3DDevice9_GetTransform(device, D3DTS_PROJECTION, &tdata->projection);
857     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
858
859     hret = IDirect3DDevice9_GetTransform(device, D3DTS_TEXTURE0, &tdata->texture0);
860     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
861
862     hret = IDirect3DDevice9_GetTransform(device, D3DTS_TEXTURE0 + texture_stages - 1, &tdata->texture7);
863     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
864
865     hret = IDirect3DDevice9_GetTransform(device, D3DTS_WORLD, &tdata->world0);
866     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
867
868     hret = IDirect3DDevice9_GetTransform(device, D3DTS_WORLDMATRIX(255), &tdata->world255);
869     ok(hret == D3D_OK, "GetTransform returned %#x.\n", hret);
870 }
871
872 static const transform_data transform_default_data = {
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       { { { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } } }
879 };
880
881 static const transform_data transform_poison_data = {
882       { { { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
883         9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f } } },
884
885       { { { 17.0f, 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f,
886         25.0f, 26.0f, 27.0f, 28.0f, 29.0f, 30.0f, 31.0f, 32.0f } } },
887
888       { { { 33.0f, 34.0f, 35.0f, 36.0f, 37.0f, 38.0f, 39.0f, 40.0f,
889         41.0f, 42.0f, 43.0f, 44.0f, 45.0f, 46.0f, 47.0f, 48.0f } } },
890
891       { { { 49.0f, 50.0f, 51.0f, 52.0f, 53.0f, 54.0f, 55.0f, 56.0f,
892         57.0f, 58.0f, 59.0f, 60.0f, 61.0f, 62.0f, 63.0f, 64.0f } } },
893
894       { { { 64.0f, 66.0f, 67.0f, 68.0f, 69.0f, 70.0f, 71.0f, 72.0f,
895         73.0f, 74.0f, 75.0f, 76.0f, 77.0f, 78.0f, 79.0f, 80.0f } } },
896
897       { { { 81.0f, 82.0f, 83.0f, 84.0f, 85.0f, 86.0f, 87.0f, 88.0f,
898         89.0f, 90.0f, 91.0f, 92.0f, 93.0f, 94.0f, 95.0f, 96.0f} } },
899 };
900
901 static const transform_data transform_test_data = {
902       { { { 1.2f, 3.4f, -5.6f, 7.2f, 10.11f, -12.13f, 14.15f, -1.5f,
903         23.56f, 12.89f, 44.56f, -1.0f, 2.3f, 0.0f, 4.4f, 5.5f } } },
904
905       { { { 9.2f, 38.7f, -6.6f, 7.2f, 10.11f, -12.13f, 77.15f, -1.5f,
906         23.56f, 12.89f, 14.56f, -1.0f, 12.3f, 0.0f, 4.4f, 5.5f } } },
907
908       { { { 10.2f, 3.4f, 0.6f, 7.2f, 10.11f, -12.13f, 14.15f, -1.5f,
909         23.54f, 12.9f, 44.56f, -1.0f, 2.3f, 0.0f, 4.4f, 5.5f } } },
910
911       { { { 1.2f, 3.4f, -5.6f, 7.2f, 10.11f, -12.13f, -14.5f, -1.5f,
912         2.56f, 12.89f, 23.56f, -1.0f, 112.3f, 0.0f, 4.4f, 2.5f } } },
913
914       { { { 1.2f, 31.41f, 58.6f, 7.2f, 10.11f, -12.13f, -14.5f, -1.5f,
915         2.56f, 12.89f, 11.56f, -1.0f, 112.3f, 0.0f, 44.4f, 2.5f } } },
916
917       { { { 1.20f, 3.4f, -5.6f, 7.0f, 10.11f, -12.156f, -14.5f, -1.5f,
918         2.56f, 1.829f, 23.6f, -1.0f, 112.3f, 0.0f, 41.4f, 2.5f } } },
919 };
920
921 static HRESULT transform_setup_handler(
922     state_test* test) {
923
924     transform_context *ctx = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(transform_context));
925     if (ctx == NULL) return E_FAIL;
926     test->test_context = ctx;
927
928     test->return_data = &ctx->return_data_buffer;
929     test->test_data_in = &transform_test_data;
930     test->test_data_out = &transform_test_data;
931     test->default_data = &transform_default_data;
932     test->initial_data = &transform_default_data;
933     test->poison_data = &transform_poison_data;
934
935     test->data_size = sizeof(transform_data);
936
937     return D3D_OK;
938 }
939
940 static void transform_teardown_handler(
941     state_test* test) {
942     
943     HeapFree(GetProcessHeap(), 0, test->test_context);
944 }
945
946 static void transform_queue_test(
947     state_test* test) {
948
949     test->setup_handler = transform_setup_handler;
950     test->teardown_handler = transform_teardown_handler;
951     test->set_handler = transform_set_handler;
952     test->get_handler = transform_get_handler;
953     test->print_handler = transform_print_handler;
954     test->test_name = "set_get_transforms";
955     test->test_arg = NULL;
956 }
957
958 /* =================== State test: Render States ===================================== */
959
960 #define D3D9_RENDER_STATES 102
961 const D3DRENDERSTATETYPE render_state_indices[] = {
962
963     D3DRS_ZENABLE,
964     D3DRS_FILLMODE,
965     D3DRS_SHADEMODE,
966     D3DRS_ZWRITEENABLE,
967     D3DRS_ALPHATESTENABLE,
968     D3DRS_LASTPIXEL,
969     D3DRS_SRCBLEND,
970     D3DRS_DESTBLEND,
971     D3DRS_CULLMODE,
972     D3DRS_ZFUNC,
973     D3DRS_ALPHAREF,
974     D3DRS_ALPHAFUNC,
975     D3DRS_DITHERENABLE,
976     D3DRS_ALPHABLENDENABLE,
977     D3DRS_FOGENABLE,
978     D3DRS_SPECULARENABLE,
979     D3DRS_FOGCOLOR,
980     D3DRS_FOGTABLEMODE,
981     D3DRS_FOGSTART,
982     D3DRS_FOGEND,
983     D3DRS_FOGDENSITY,
984     D3DRS_RANGEFOGENABLE,
985     D3DRS_STENCILENABLE,
986     D3DRS_STENCILFAIL,
987     D3DRS_STENCILZFAIL,
988     D3DRS_STENCILPASS,
989     D3DRS_STENCILFUNC,
990     D3DRS_STENCILREF,
991     D3DRS_STENCILMASK,
992     D3DRS_STENCILWRITEMASK,
993     D3DRS_TEXTUREFACTOR,
994     D3DRS_WRAP0,
995     D3DRS_WRAP1,
996     D3DRS_WRAP2,
997     D3DRS_WRAP3,
998     D3DRS_WRAP4,
999     D3DRS_WRAP5,
1000     D3DRS_WRAP6,
1001     D3DRS_WRAP7,
1002     D3DRS_CLIPPING,
1003     D3DRS_LIGHTING,
1004     D3DRS_AMBIENT,
1005     D3DRS_FOGVERTEXMODE,
1006     D3DRS_COLORVERTEX,
1007     D3DRS_LOCALVIEWER,
1008     D3DRS_NORMALIZENORMALS,
1009     D3DRS_DIFFUSEMATERIALSOURCE,
1010     D3DRS_SPECULARMATERIALSOURCE,
1011     D3DRS_AMBIENTMATERIALSOURCE,
1012     D3DRS_EMISSIVEMATERIALSOURCE,
1013     D3DRS_VERTEXBLEND,
1014     D3DRS_CLIPPLANEENABLE,
1015 #if 0 /* Driver dependent, increase array size to enable */
1016     D3DRS_POINTSIZE,
1017 #endif
1018     D3DRS_POINTSIZE_MIN,
1019     D3DRS_POINTSPRITEENABLE,
1020     D3DRS_POINTSCALEENABLE,
1021     D3DRS_POINTSCALE_A,
1022     D3DRS_POINTSCALE_B,
1023     D3DRS_POINTSCALE_C,
1024     D3DRS_MULTISAMPLEANTIALIAS,
1025     D3DRS_MULTISAMPLEMASK,
1026     D3DRS_PATCHEDGESTYLE,
1027     D3DRS_DEBUGMONITORTOKEN,
1028     D3DRS_POINTSIZE_MAX,
1029     D3DRS_INDEXEDVERTEXBLENDENABLE,
1030     D3DRS_COLORWRITEENABLE,
1031     D3DRS_TWEENFACTOR,
1032     D3DRS_BLENDOP,
1033     D3DRS_POSITIONDEGREE,
1034     D3DRS_NORMALDEGREE,
1035     D3DRS_SCISSORTESTENABLE,
1036     D3DRS_SLOPESCALEDEPTHBIAS,
1037     D3DRS_ANTIALIASEDLINEENABLE,
1038     D3DRS_MINTESSELLATIONLEVEL,
1039     D3DRS_MAXTESSELLATIONLEVEL,
1040     D3DRS_ADAPTIVETESS_X,
1041     D3DRS_ADAPTIVETESS_Y,
1042     D3DRS_ADAPTIVETESS_Z,
1043     D3DRS_ADAPTIVETESS_W,
1044     D3DRS_ENABLEADAPTIVETESSELLATION,
1045     D3DRS_TWOSIDEDSTENCILMODE,
1046     D3DRS_CCW_STENCILFAIL,
1047     D3DRS_CCW_STENCILZFAIL,
1048     D3DRS_CCW_STENCILPASS,
1049     D3DRS_CCW_STENCILFUNC,
1050     D3DRS_COLORWRITEENABLE1,
1051     D3DRS_COLORWRITEENABLE2,
1052     D3DRS_COLORWRITEENABLE3,
1053     D3DRS_BLENDFACTOR,
1054     D3DRS_SRGBWRITEENABLE,
1055     D3DRS_DEPTHBIAS,
1056     D3DRS_WRAP8,
1057     D3DRS_WRAP9,
1058     D3DRS_WRAP10,
1059     D3DRS_WRAP11,
1060     D3DRS_WRAP12,
1061     D3DRS_WRAP13,
1062     D3DRS_WRAP14,
1063     D3DRS_WRAP15,
1064     D3DRS_SEPARATEALPHABLENDENABLE,
1065     D3DRS_SRCBLENDALPHA,
1066     D3DRS_DESTBLENDALPHA,
1067     D3DRS_BLENDOPALPHA,
1068 };
1069
1070 typedef struct render_state_data {
1071     DWORD states[D3D9_RENDER_STATES];
1072 } render_state_data;
1073
1074 typedef struct render_state_arg {
1075     D3DPRESENT_PARAMETERS* device_pparams;
1076     float pointsize_max;
1077 } render_state_arg;
1078
1079 typedef struct render_state_context {
1080    render_state_data return_data_buffer;
1081    render_state_data default_data_buffer;
1082    render_state_data test_data_buffer;
1083    render_state_data poison_data_buffer;
1084 } render_state_context;
1085
1086 static void render_state_set_handler(
1087     IDirect3DDevice9* device, const state_test* test, const void* data) {
1088
1089     HRESULT hret;
1090     const render_state_data* rsdata = data;
1091     unsigned int i;
1092
1093     for (i = 0; i < D3D9_RENDER_STATES; i++) {
1094         hret = IDirect3DDevice9_SetRenderState(device, render_state_indices[i], rsdata->states[i]);
1095         ok(hret == D3D_OK, "SetRenderState returned %#x.\n", hret);
1096     }
1097 }
1098
1099 static void render_state_get_handler(
1100     IDirect3DDevice9* device, const state_test* test, void* data) {
1101
1102     HRESULT hret;
1103     render_state_data* rsdata = data;
1104     unsigned int i = 0;
1105
1106     for (i = 0; i < D3D9_RENDER_STATES; i++) {
1107         hret = IDirect3DDevice9_GetRenderState(device, render_state_indices[i], &rsdata->states[i]);
1108         ok(hret == D3D_OK, "GetRenderState returned %#x.\n", hret);
1109     }
1110 }
1111
1112 static void render_state_print_handler(
1113     const state_test* test,
1114     const void* data) {
1115
1116     const render_state_data* rsdata = data;
1117
1118     unsigned int i;
1119     for (i = 0; i < D3D9_RENDER_STATES; i++)
1120         trace("Index = %u, Value = %#x\n", i, rsdata->states[i]);
1121 }
1122
1123 static inline DWORD to_dword(float fl) {
1124     return *((DWORD*) &fl);
1125 }
1126
1127 static void render_state_default_data_init(const struct render_state_arg *rsarg, struct render_state_data *data)
1128 {
1129    DWORD zenable = rsarg->device_pparams->EnableAutoDepthStencil ? D3DZB_TRUE : D3DZB_FALSE;
1130    unsigned int idx = 0;
1131
1132    data->states[idx++] = zenable;               /* 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(rsarg->pointsize_max); /* 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, &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_state_arg.pointsize_max = caps.MaxPointSize;
1457     render_states_queue_test(&tests[tcount], &render_state_arg);
1458     tcount++;
1459
1460     execute_test_chain_all(device, tests, tcount);
1461 }
1462
1463 static void test_shader_constant_apply(IDirect3DDevice9 *device)
1464 {
1465     static const float initial[] = {0.0f, 0.0f, 0.0f, 0.0f};
1466     static const float vs_const[] = {1.0f, 2.0f, 3.0f, 4.0f};
1467     static const float ps_const[] = {5.0f, 6.0f, 7.0f, 8.0f};
1468     IDirect3DStateBlock9 *stateblock;
1469     float ret[4];
1470     HRESULT hr;
1471
1472     hr = IDirect3DDevice9_SetVertexShaderConstantF(device, 0, initial, 1);
1473     ok(SUCCEEDED(hr), "SetVertexShaderConstantF returned %#x\n", hr);
1474     hr = IDirect3DDevice9_SetVertexShaderConstantF(device, 1, initial, 1);
1475     ok(SUCCEEDED(hr), "SetVertexShaderConstantF returned %#x\n", hr);
1476     hr = IDirect3DDevice9_SetPixelShaderConstantF(device, 0, initial, 1);
1477     ok(SUCCEEDED(hr), "SetPixelShaderConstantF returned %#x\n", hr);
1478     hr = IDirect3DDevice9_SetPixelShaderConstantF(device, 1, initial, 1);
1479     ok(SUCCEEDED(hr), "SetPixelShaderConstantF returned %#x\n", hr);
1480
1481     hr = IDirect3DDevice9_GetVertexShaderConstantF(device, 0, ret, 1);
1482     ok(SUCCEEDED(hr), "GetVertexShaderConstantF returned %#x\n", hr);
1483     ok(!memcmp(ret, initial, sizeof(initial)),
1484             "GetVertexShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1485             ret[0], ret[1], ret[2], ret[3], initial[0], initial[1], initial[2], initial[3]);
1486     hr = IDirect3DDevice9_GetVertexShaderConstantF(device, 1, ret, 1);
1487     ok(SUCCEEDED(hr), "GetVertexShaderConstantF returned %#x\n", hr);
1488     ok(!memcmp(ret, initial, sizeof(initial)),
1489             "GetVertexShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1490             ret[0], ret[1], ret[2], ret[3], initial[0], initial[1], initial[2], initial[3]);
1491     hr = IDirect3DDevice9_GetPixelShaderConstantF(device, 0, ret, 1);
1492     ok(SUCCEEDED(hr), "GetPixelShaderConstantF returned %#x\n", hr);
1493     ok(!memcmp(ret, initial, sizeof(initial)),
1494             "GetpixelShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1495             ret[0], ret[1], ret[2], ret[3], initial[0], initial[1], initial[2], initial[3]);
1496     hr = IDirect3DDevice9_GetPixelShaderConstantF(device, 1, ret, 1);
1497     ok(SUCCEEDED(hr), "GetPixelShaderConstantF returned %#x\n", hr);
1498     ok(!memcmp(ret, initial, sizeof(initial)),
1499             "GetPixelShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1500             ret[0], ret[1], ret[2], ret[3], initial[0], initial[1], initial[2], initial[3]);
1501
1502     hr = IDirect3DDevice9_SetVertexShaderConstantF(device, 0, vs_const, 1);
1503     ok(SUCCEEDED(hr), "SetVertexShaderConstantF returned %#x\n", hr);
1504     hr = IDirect3DDevice9_SetPixelShaderConstantF(device, 0, ps_const, 1);
1505     ok(SUCCEEDED(hr), "SetPixelShaderConstantF returned %#x\n", hr);
1506
1507     hr = IDirect3DDevice9_BeginStateBlock(device);
1508     ok(SUCCEEDED(hr), "BeginStateBlock returned %#x\n", hr);
1509
1510     hr = IDirect3DDevice9_SetVertexShaderConstantF(device, 1, vs_const, 1);
1511     ok(SUCCEEDED(hr), "SetVertexShaderConstantF returned %#x\n", hr);
1512     hr = IDirect3DDevice9_SetPixelShaderConstantF(device, 1, ps_const, 1);
1513     ok(SUCCEEDED(hr), "SetPixelShaderConstantF returned %#x\n", hr);
1514
1515     hr = IDirect3DDevice9_EndStateBlock(device, &stateblock);
1516     ok(SUCCEEDED(hr), "EndStateBlock returned %#x\n", hr);
1517
1518     hr = IDirect3DDevice9_GetVertexShaderConstantF(device, 0, ret, 1);
1519     ok(SUCCEEDED(hr), "GetVertexShaderConstantF returned %#x\n", hr);
1520     ok(!memcmp(ret, vs_const, sizeof(vs_const)),
1521             "GetVertexShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1522             ret[0], ret[1], ret[2], ret[3], vs_const[0], vs_const[1], vs_const[2], vs_const[3]);
1523     hr = IDirect3DDevice9_GetVertexShaderConstantF(device, 1, ret, 1);
1524     ok(SUCCEEDED(hr), "GetVertexShaderConstantF returned %#x\n", hr);
1525     ok(!memcmp(ret, initial, sizeof(initial)),
1526             "GetVertexShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1527             ret[0], ret[1], ret[2], ret[3], initial[0], initial[1], initial[2], initial[3]);
1528     hr = IDirect3DDevice9_GetPixelShaderConstantF(device, 0, ret, 1);
1529     ok(SUCCEEDED(hr), "GetPixelShaderConstantF returned %#x\n", hr);
1530     ok(!memcmp(ret, ps_const, sizeof(ps_const)),
1531             "GetPixelShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1532             ret[0], ret[1], ret[2], ret[3], ps_const[0], ps_const[1], ps_const[2], ps_const[3]);
1533     hr = IDirect3DDevice9_GetPixelShaderConstantF(device, 1, ret, 1);
1534     ok(SUCCEEDED(hr), "GetPixelShaderConstantF returned %#x\n", hr);
1535     ok(!memcmp(ret, initial, sizeof(initial)),
1536             "GetPixelShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1537             ret[0], ret[1], ret[2], ret[3], initial[0], initial[1], initial[2], initial[3]);
1538
1539     hr = IDirect3DStateBlock9_Apply(stateblock);
1540     ok(SUCCEEDED(hr), "Apply returned %#x\n", hr);
1541
1542     /* Apply doesn't overwrite constants that aren't explicitly set on the source stateblock. */
1543     hr = IDirect3DDevice9_GetVertexShaderConstantF(device, 0, ret, 1);
1544     ok(SUCCEEDED(hr), "GetVertexShaderConstantF returned %#x\n", hr);
1545     ok(!memcmp(ret, vs_const, sizeof(vs_const)),
1546             "GetVertexShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1547             ret[0], ret[1], ret[2], ret[3], vs_const[0], vs_const[1], vs_const[2], vs_const[3]);
1548     hr = IDirect3DDevice9_GetVertexShaderConstantF(device, 1, ret, 1);
1549     ok(SUCCEEDED(hr), "GetVertexShaderConstantF returned %#x\n", hr);
1550     ok(!memcmp(ret, vs_const, sizeof(vs_const)),
1551             "GetVertexShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1552             ret[0], ret[1], ret[2], ret[3], vs_const[0], vs_const[1], vs_const[2], vs_const[3]);
1553     hr = IDirect3DDevice9_GetPixelShaderConstantF(device, 0, ret, 1);
1554     ok(SUCCEEDED(hr), "GetPixelShaderConstantF returned %#x\n", hr);
1555     ok(!memcmp(ret, ps_const, sizeof(ps_const)),
1556             "GetPixelShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1557             ret[0], ret[1], ret[2], ret[3], ps_const[0], ps_const[1], ps_const[2], ps_const[3]);
1558     hr = IDirect3DDevice9_GetPixelShaderConstantF(device, 1, ret, 1);
1559     ok(SUCCEEDED(hr), "GetPixelShaderConstantF returned %#x\n", hr);
1560     ok(!memcmp(ret, ps_const, sizeof(ps_const)),
1561             "GetPixelShaderConstantF got {%f, %f, %f, %f}, expected {%f, %f, %f, %f}\n",
1562             ret[0], ret[1], ret[2], ret[3], ps_const[0], ps_const[1], ps_const[2], ps_const[3]);
1563
1564     IDirect3DStateBlock9_Release(stateblock);
1565 }
1566
1567 START_TEST(stateblock)
1568 {
1569     IDirect3DDevice9 *device_ptr = NULL;
1570     D3DPRESENT_PARAMETERS device_pparams;
1571     HRESULT hret;
1572     ULONG refcount;
1573
1574     d3d9_handle = LoadLibraryA("d3d9.dll");
1575     if (!d3d9_handle)
1576     {
1577         skip("Could not load d3d9.dll\n");
1578         return;
1579     }
1580
1581     hret = init_d3d9(&device_ptr, &device_pparams);
1582     if (hret != D3D_OK) return;
1583
1584     test_begin_end_state_block(device_ptr);
1585     test_state_management(device_ptr, &device_pparams);
1586     test_shader_constant_apply(device_ptr);
1587
1588     refcount = IDirect3DDevice9_Release(device_ptr);
1589     ok(!refcount, "Device has %u references left\n", refcount);
1590 }