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