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