wined3d: Implement more GLSL instructions and a little cleanup.
[wine] / dlls / d3d9 / tests / stateblock.c
1 /*
2  * Copyright (C) 2005 Henri Verbeet
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #include <d3d9.h>
21 #include "wine/test.h"
22
23 static HMODULE d3d9_handle = 0;
24
25 static HWND create_window(void)
26 {
27     WNDCLASS wc = {0};
28     wc.lpfnWndProc = &DefWindowProc;
29     wc.lpszClassName = "d3d9_test_wc";
30     RegisterClass(&wc);
31
32     return CreateWindow("d3d9_test_wc", "d3d9_test",
33             0, 0, 0, 0, 0, 0, 0, 0, 0);
34 }
35
36 static IDirect3DDevice9 *init_d3d9(void)
37 {
38     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
39     IDirect3D9 *d3d9_ptr = 0;
40     IDirect3DDevice9 *device_ptr = 0;
41     D3DPRESENT_PARAMETERS present_parameters;
42     HRESULT hres;
43
44     d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
45     ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
46     if (!d3d9_create) return NULL;
47     
48     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
49     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
50     if (!d3d9_ptr) return NULL;
51
52     ZeroMemory(&present_parameters, sizeof(present_parameters));
53     present_parameters.Windowed = TRUE;
54     present_parameters.hDeviceWindow = create_window();
55     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
56
57     hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, NULL,
58         D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
59     ok(hres == D3D_OK, "IDirect3D_CreateDevice returned: 0x%lx\n", hres);
60
61     return device_ptr;
62 }
63
64 static void test_begin_end_state_block(IDirect3DDevice9 *device_ptr)
65 {
66     HRESULT hret = 0;
67     IDirect3DStateBlock9 *state_block_ptr = 0;
68
69     /* Should succeed */
70     hret = IDirect3DDevice9_BeginStateBlock(device_ptr);
71     ok(hret == D3D_OK, "BeginStateBlock returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
72     if (hret != D3D_OK) return;
73
74     /* Calling BeginStateBlock while recording should return D3DERR_INVALIDCALL */
75     hret = IDirect3DDevice9_BeginStateBlock(device_ptr);
76     ok(hret == D3DERR_INVALIDCALL, "BeginStateBlock returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3DERR_INVALIDCALL);
77     if (hret != D3DERR_INVALIDCALL) return;
78
79     /* Should succeed */
80     state_block_ptr = (IDirect3DStateBlock9 *)0xdeadbeef;
81     hret = IDirect3DDevice9_EndStateBlock(device_ptr, &state_block_ptr);
82     ok(hret == D3D_OK && state_block_ptr != 0 && state_block_ptr != (IDirect3DStateBlock9 *)0xdeadbeef, 
83         "EndStateBlock returned: hret 0x%lx, state_block_ptr %p. "
84         "Expected hret 0x%lx, state_block_ptr != %p, state_block_ptr != 0xdeadbeef.\n", hret, state_block_ptr, D3D_OK, NULL);
85
86     /* Calling EndStateBlock while not recording should return D3DERR_INVALIDCALL. state_block_ptr should not be touched. */
87     state_block_ptr = (IDirect3DStateBlock9 *)0xdeadbeef;
88     hret = IDirect3DDevice9_EndStateBlock(device_ptr, &state_block_ptr);
89     ok(hret == D3DERR_INVALIDCALL && state_block_ptr == (IDirect3DStateBlock9 *)0xdeadbeef, 
90         "EndStateBlock returned: hret 0x%lx, state_block_ptr %p. "
91         "Expected hret 0x%lx, state_block_ptr 0xdeadbeef.\n", hret, state_block_ptr, D3DERR_INVALIDCALL);
92 }
93
94 static void test_missing_light_recording(IDirect3DDevice9 *device_ptr)
95 {
96
97     D3DLIGHT9 garbage_light = { 1, 
98                                 { 2.0, 2.0, 2.0, 2.0 }, { 3.0, 3.0, 3.0, 3.0 }, { 4.0, 4.0, 4.0, 4.0 }, 
99                                 { 5.0, 5.0, 5.0 }, { 6.0, 6.0, 6.0 }, 
100                                 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 };
101
102     D3DLIGHT9 expected_light = { D3DLIGHT_DIRECTIONAL, 
103                                 { 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 },
104                                 { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0 }, 
105                                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
106     D3DLIGHT9 result_light;
107     BOOL result_light_enable;
108
109     HRESULT hret = 0;
110     IDirect3DStateBlock9 *state_block_ptr = 0;
111
112     /* Before we make any changes, GetLightEnable should fail, and GetLight should fail */
113     hret = IDirect3DDevice9_GetLightEnable(device_ptr, 0, &result_light_enable);
114     ok(hret == D3DERR_INVALIDCALL, "GetLightEnable returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", 
115         hret, D3DERR_INVALIDCALL);
116     if (hret != D3DERR_INVALIDCALL) goto cleanup;
117
118     hret = IDirect3DDevice9_GetLight(device_ptr, 0, &result_light);
119     ok(hret == D3DERR_INVALIDCALL, "GetLight returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", 
120         hret, D3DERR_INVALIDCALL);
121     if (hret != D3DERR_INVALIDCALL) goto cleanup;
122
123     /* Set states to record with garbage data (light, light_enable) */
124     hret = IDirect3DDevice9_BeginStateBlock(device_ptr);
125     ok(hret == D3D_OK, "BeginStateBlock returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
126     if (hret != D3D_OK) goto cleanup;
127
128     hret = IDirect3DDevice9_SetLight(device_ptr, 0, &garbage_light);
129     ok(hret == D3D_OK, "SetLight returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
130     if (hret != D3D_OK) goto cleanup;
131
132     hret = IDirect3DDevice9_LightEnable(device_ptr, 0, TRUE);
133     ok(hret == D3D_OK, "SetLightEnable returned: hret 0x%lx, Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
134     if (hret != D3D_OK) goto cleanup;
135
136     state_block_ptr = (IDirect3DStateBlock9 *)0xdeadbeef;
137     hret = IDirect3DDevice9_EndStateBlock(device_ptr, &state_block_ptr);
138     ok(hret == D3D_OK, "EndStateBlock returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
139     if (hret != D3D_OK) goto cleanup; 
140
141     /* Now capture light data */
142     hret = IDirect3DStateBlock9_Capture(state_block_ptr);
143     ok(hret == D3D_OK, "Capture returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
144     if (hret != D3D_OK) goto cleanup;
145
146     /* Now apply the resultant stateblock */
147     hret = IDirect3DStateBlock9_Apply(state_block_ptr);
148     ok(hret == D3D_OK, "Apply returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
149     if (hret != D3D_OK) goto cleanup;
150
151     /* Now try to fetch the light set by the stateblock.
152      * Light enable should have been set to 0, and a default light should have been
153      * created as a side effect of that */
154     hret = IDirect3DDevice9_GetLightEnable(device_ptr, 0, &result_light_enable);
155     ok(hret == D3D_OK, "GetLightEnable returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
156     if (hret != D3D_OK) goto cleanup;
157     ok(result_light_enable == 0, "Light enabled status was %u, instead of 0\n", result_light_enable);
158
159     hret = IDirect3DDevice9_GetLight(device_ptr, 0, &result_light);
160     ok(hret == D3D_OK, "GetLight returned: hret 0x%lx. Expected hret 0x%lx. Aborting.\n", hret, D3D_OK);
161     if (hret != D3D_OK) goto cleanup;
162
163     ok (!memcmp(&expected_light, &result_light, sizeof(D3DLIGHT9)), 
164         "Light returned = { %u, { %f, %f, %f, %f }, { %f, %f, %f, %f}, { %f, %f, %f, %f }, "
165                          "{ %f, %f, %f }, { %f, %f, %f }, %f, %f, %f, %f, %f, %f, %f }\n",
166
167          result_light.Type, 
168          result_light.Diffuse.r, result_light.Diffuse.g, result_light.Diffuse.b, result_light.Diffuse.a,
169          result_light.Specular.r, result_light.Specular.g, result_light.Specular.b, result_light.Specular.a,
170          result_light.Ambient.r, result_light.Ambient.g, result_light.Ambient.b, result_light.Ambient.a,
171          result_light.Position.x, result_light.Position.y, result_light.Position.z,
172          result_light.Direction.x, result_light.Direction.y, result_light.Direction.z, 
173          result_light.Range, result_light.Falloff,
174          result_light.Attenuation0, result_light.Attenuation1, result_light.Attenuation2,
175          result_light.Theta, result_light.Phi);
176
177     cleanup:
178     if (state_block_ptr) IUnknown_Release( state_block_ptr );
179 }
180
181 START_TEST(stateblock)
182 {
183     IDirect3DDevice9 *device_ptr;
184
185     d3d9_handle = LoadLibraryA("d3d9.dll");
186     if (!d3d9_handle)
187     {
188         trace("Could not load d3d9.dll, skipping tests\n");
189         return;
190     }
191
192     device_ptr = init_d3d9();
193     if (!device_ptr) return;
194
195     test_begin_end_state_block(device_ptr);
196     test_missing_light_recording(device_ptr);
197 }