d3dx9_36/tests: Fixed constant table sizes in shader.c.
[wine] / dlls / d3dx9_36 / tests / shader.c
1 /*
2  * Copyright 2008 Luis Busquets
3  * Copyright 2011 Travis Athougies
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 #include "wine/test.h"
21 #include "d3dx9.h"
22
23 static const DWORD simple_vs[] = {
24     0xfffe0101,                                                             /* vs_1_1                       */
25     0x0000001f, 0x80000000, 0x900f0000,                                     /* dcl_position0 v0             */
26     0x00000009, 0xc0010000, 0x90e40000, 0xa0e40000,                         /* dp4 oPos.x, v0, c0           */
27     0x00000009, 0xc0020000, 0x90e40000, 0xa0e40001,                         /* dp4 oPos.y, v0, c1           */
28     0x00000009, 0xc0040000, 0x90e40000, 0xa0e40002,                         /* dp4 oPos.z, v0, c2           */
29     0x00000009, 0xc0080000, 0x90e40000, 0xa0e40003,                         /* dp4 oPos.w, v0, c3           */
30     0x0000ffff};                                                            /* END                          */
31
32 static const DWORD simple_ps[] = {
33     0xffff0101,                                                             /* ps_1_1                       */
34     0x00000051, 0xa00f0001, 0x3f800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
35     0x00000042, 0xb00f0000,                                                 /* tex t0                       */
36     0x00000008, 0x800f0000, 0xa0e40001, 0xa0e40000,                         /* dp3 r0, c1, c0               */
37     0x00000005, 0x800f0000, 0x90e40000, 0x80e40000,                         /* mul r0, v0, r0               */
38     0x00000005, 0x800f0000, 0xb0e40000, 0x80e40000,                         /* mul r0, t0, r0               */
39     0x0000ffff};                                                            /* END                          */
40
41 #define FCC_TEXT MAKEFOURCC('T','E','X','T')
42 #define FCC_CTAB MAKEFOURCC('C','T','A','B')
43
44 static const DWORD shader_with_ctab[] = {
45     0xfffe0300,                                                             /* vs_3_0                       */
46     0x0002fffe, FCC_TEXT,   0x00000000,                                     /* TEXT comment                 */
47     0x0008fffe, FCC_CTAB,   0x0000001c, 0x00000010, 0xfffe0300, 0x00000000, /* CTAB comment                 */
48                 0x00000000, 0x00000000, 0x00000000,
49     0x0004fffe, FCC_TEXT,   0x00000000, 0x00000000, 0x00000000,             /* TEXT comment                 */
50     0x0000ffff};                                                            /* END                          */
51
52 static const DWORD shader_with_invalid_ctab[] = {
53     0xfffe0300,                                                             /* vs_3_0                       */
54     0x0005fffe, FCC_CTAB,                                                   /* CTAB comment                 */
55                 0x0000001c, 0x000000a9, 0xfffe0300,
56                 0x00000000, 0x00000000,
57     0x0000ffff};                                                            /* END                          */
58
59 static const DWORD shader_with_ctab_constants[] = {
60     0xfffe0300,                                                             /* vs_3_0                       */
61     0x002efffe, FCC_CTAB,                                                   /* CTAB comment                 */
62     0x0000001c, 0x000000a4, 0xfffe0300, 0x00000003, 0x0000001c, 0x20008100, /* Header                       */
63     0x0000009c,
64     0x00000058, 0x00070002, 0x00000001, 0x00000064, 0x00000000,             /* Constant 1 desc              */
65     0x00000074, 0x00000002, 0x00000004, 0x00000080, 0x00000000,             /* Constant 2 desc              */
66     0x00000090, 0x00040002, 0x00000003, 0x00000080, 0x00000000,             /* Constant 3 desc              */
67     0x736e6f43, 0x746e6174, 0xabab0031,                                     /* Constant 1 name string       */
68     0x00030001, 0x00040001, 0x00000001, 0x00000000,                         /* Constant 1 type desc         */
69     0x736e6f43, 0x746e6174, 0xabab0032,                                     /* Constant 2 name string       */
70     0x00030003, 0x00040004, 0x00000001, 0x00000000,                         /* Constant 2 & 3 type desc     */
71     0x736e6f43, 0x746e6174, 0xabab0033,                                     /* Constant 3 name string       */
72     0x335f7376, 0xab00305f,                                                 /* Target name string           */
73     0x656e6957, 0x6f727020, 0x7463656a, 0xababab00,                         /* Creator name string          */
74     0x0000ffff};                                                            /* END                          */
75
76 static const DWORD ctab_basic[] = {
77     0xfffe0300,                                                             /* vs_3_0                       */
78     0x0040fffe, FCC_CTAB,                                                   /* CTAB comment                 */
79     0x0000001c, 0x000000ec, 0xfffe0300, 0x00000005, 0x0000001c, 0x20008100, /* Header                       */
80     0x000000e4,
81     0x00000080, 0x00060002, 0x00000001, 0x00000084, 0x00000000,             /* Constant 1 desc (f)          */
82     0x00000094, 0x00070002, 0x00000001, 0x00000098, 0x00000000,             /* Constant 2 desc (f4)         */
83     0x000000A8, 0x00040002, 0x00000001, 0x000000AC, 0x00000000,             /* Constant 3 desc (i)          */
84     0x000000BC, 0x00050002, 0x00000001, 0x000000C0, 0x00000000,             /* Constant 4 desc (i4)         */
85     0x000000D0, 0x00000002, 0x00000004, 0x000000D4, 0x00000000,             /* Constant 5 desc (mvp)        */
86     0xabab0066, 0x00030000, 0x00010001, 0x00000001, 0x00000000,             /* Constant 1 name/type desc    */
87     0xab003466, 0x00030001, 0x00040001, 0x00000001, 0x00000000,             /* Constant 2 name/type desc    */
88     0xabab0069, 0x00020000, 0x00010001, 0x00000001, 0x00000000,             /* Constant 3 name/type desc    */
89     0xab003469, 0x00020001, 0x00040001, 0x00000001, 0x00000000,             /* Constant 4 name/type desc    */
90     0x0070766d, 0x00030003, 0x00040004, 0x00000001, 0x00000000,             /* Constant 5 name/type desc    */
91     0x335f7376, 0xab00305f,                                                 /* Target name string           */
92     0x656e6957, 0x6f727020, 0x7463656a, 0xababab00,                         /* Creator name string          */
93     0x0000ffff};                                                            /* END                          */
94
95 static const D3DXCONSTANT_DESC ctab_basic_expected[] = {
96     {"mvp", D3DXRS_FLOAT4, 0, 4, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 4, 4, 1, 0, 64, 0},
97     {"i",   D3DXRS_FLOAT4, 4, 1, D3DXPC_SCALAR,         D3DXPT_INT,   1, 1, 1, 0,  4, 0},
98     {"i4",  D3DXRS_FLOAT4, 5, 1, D3DXPC_VECTOR,         D3DXPT_INT,   1, 4, 1, 0, 16, 0},
99     {"f",   D3DXRS_FLOAT4, 6, 1, D3DXPC_SCALAR,         D3DXPT_FLOAT, 1, 1, 1, 0,  4, 0},
100     {"f4",  D3DXRS_FLOAT4, 7, 1, D3DXPC_VECTOR,         D3DXPT_FLOAT, 1, 4, 1, 0, 16, 0}};
101
102 static const DWORD ctab_matrices[] = {
103     0xfffe0300,                                                             /* vs_3_0                       */
104     0x0032fffe, FCC_CTAB,                                                   /* CTAB comment                 */
105     0x0000001c, 0x000000b0, 0xfffe0300, 0x00000003, 0x0000001c, 0x20008100, /* Header                       */
106     0x000000a8,
107     0x00000058, 0x00070002, 0x00000001, 0x00000064, 0x00000000,             /* Constant 1 desc (fmatrix3x1) */
108     0x00000074, 0x00000002, 0x00000004, 0x00000080, 0x00000000,             /* Constant 2 desc (fmatrix4x4) */
109     0x00000090, 0x00040002, 0x00000003, 0x0000009c, 0x00000000,             /* Constant 3 desc (imatrix2x3) */
110     0x74616D66, 0x33786972, 0xab003178,                                     /* Constant 1 name              */
111     0x00030003, 0x00010003, 0x00000001, 0x00000000,                         /* Constant 1 type desc         */
112     0x74616D66, 0x34786972, 0xab003478,                                     /* Constant 2 name              */
113     0x00030003, 0x00040004, 0x00000001, 0x00000000,                         /* Constant 2 type desc         */
114     0x74616D69, 0x32786972, 0xab003378,                                     /* Constant 3 name              */
115     0x00020002, 0x00030002, 0x00000001, 0x00000000,                         /* Constant 3 type desc         */
116     0x335f7376, 0xab00305f,                                                 /* Target name string           */
117     0x656e6957, 0x6f727020, 0x7463656a, 0xababab00,                         /* Creator name string          */
118     0x0000ffff};                                                            /* END                          */
119
120 static const D3DXCONSTANT_DESC ctab_matrices_expected[] = {
121     {"fmatrix4x4", D3DXRS_FLOAT4, 0, 4, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 4, 4, 1, 0, 64, 0},
122     {"imatrix2x3", D3DXRS_FLOAT4, 4, 3, D3DXPC_MATRIX_ROWS,    D3DXPT_INT,   2, 3, 1, 0, 24, 0},
123     {"fmatrix3x1", D3DXRS_FLOAT4, 7, 1, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 3, 1, 1, 0, 12, 0}};
124
125 static const DWORD ctab_arrays[] = {
126     0xfffe0300,                                                             /* vs_3_0                       */
127     0x0052fffe, FCC_CTAB,                                                   /* CTAB comment                 */
128     0x0000001c, 0x0000013c, 0xfffe0300, 0x00000006, 0x0000001c, 0x20008100, /* Header                       */
129     0x00000134,
130     0x00000094, 0x000E0002, 0x00000002, 0x0000009c, 0x00000000,             /* Constant 1 desc (barray)     */
131     0x000000ac, 0x00100002, 0x00000002, 0x000000b8, 0x00000000,             /* Constant 2 desc (bvecarray)  */
132     0x000000c8, 0x00080002, 0x00000004, 0x000000d0, 0x00000000,             /* Constant 3 desc (farray)     */
133     0x000000e0, 0x00000002, 0x00000008, 0x000000ec, 0x00000000,             /* Constant 4 desc (fmtxarray)  */
134     0x000000fc, 0x000C0002, 0x00000002, 0x00000108, 0x00000000,             /* Constant 5 desc (fvecarray)  */
135     0x00000118, 0x00120002, 0x00000001, 0x00000124, 0x00000000,             /* Constant 6 desc (ivecarray)  */
136     0x72726162, 0xab007961,                                                 /* Constant 1 name              */
137     0x00010000, 0x00010001, 0x00000002, 0x00000000,                         /* Constant 1 type desc         */
138     0x63657662, 0x61727261, 0xabab0079,                                     /* Constant 2 name              */
139     0x00010001, 0x00030001, 0x00000003, 0x00000000,                         /* Constant 2 type desc         */
140     0x72726166, 0xab007961,                                                 /* Constant 3 name              */
141     0x00030000, 0x00010001, 0x00000004, 0x00000000,                         /* constant 3 type desc         */
142     0x78746d66, 0x61727261, 0xabab0079,                                     /* Constant 4 name              */
143     0x00030002, 0x00040004, 0x00000002, 0x00000000,                         /* Constant 4 type desc         */
144     0x63657666, 0x61727261, 0xabab0079,                                     /* Constant 5 name              */
145     0x00030001, 0x00040001, 0x00000002, 0x00000000,                         /* Constant 5 type desc         */
146     0x63657669, 0x61727261, 0xabab0079,                                     /* Constant 6 name              */
147     0x00020001, 0x00040001, 0x00000001, 0x00000000,                         /* Constant 6 type desc         */
148     0x335f7376, 0xab00305f,                                                 /* Target name string           */
149     0x656e6957, 0x6f727020, 0x7463656a, 0xababab00,                         /* Creator name string          */
150     0x0000ffff};                                                            /* END                          */
151
152 static const D3DXCONSTANT_DESC ctab_arrays_expected[] = {
153     {"fmtxarray", D3DXRS_FLOAT4,  0, 8, D3DXPC_MATRIX_ROWS, D3DXPT_FLOAT, 4, 4, 2, 0, 128, 0},
154     {"farray",    D3DXRS_FLOAT4,  8, 4, D3DXPC_SCALAR,      D3DXPT_FLOAT, 1, 1, 4, 0,  16, 0},
155     {"fvecarray", D3DXRS_FLOAT4, 12, 2, D3DXPC_VECTOR,      D3DXPT_FLOAT, 1, 4, 2, 0,  32, 0},
156     {"barray",    D3DXRS_FLOAT4, 14, 2, D3DXPC_SCALAR,      D3DXPT_BOOL,  1, 1, 2, 0,   8, 0},
157     {"bvecarray", D3DXRS_FLOAT4, 16, 2, D3DXPC_VECTOR,      D3DXPT_BOOL,  1, 3, 3, 0,  36, 0},
158     {"ivecarray", D3DXRS_FLOAT4, 18, 1, D3DXPC_VECTOR,      D3DXPT_INT,   1, 4, 1, 0,  16, 0}};
159
160 static void test_get_shader_size(void)
161 {
162     UINT shader_size, expected;
163
164     shader_size = D3DXGetShaderSize(simple_vs);
165     expected = sizeof(simple_vs);
166     ok(shader_size == expected, "Got shader size %u, expected %u\n", shader_size, expected);
167
168     shader_size = D3DXGetShaderSize(simple_ps);
169     expected = sizeof(simple_ps);
170     ok(shader_size == expected, "Got shader size %u, expected %u\n", shader_size, expected);
171
172     shader_size = D3DXGetShaderSize(NULL);
173     ok(shader_size == 0, "Got shader size %u, expected 0\n", shader_size);
174 }
175
176 static void test_get_shader_version(void)
177 {
178     DWORD shader_version;
179
180     shader_version = D3DXGetShaderVersion(simple_vs);
181     ok(shader_version == D3DVS_VERSION(1, 1), "Got shader version 0x%08x, expected 0x%08x\n",
182             shader_version, D3DVS_VERSION(1, 1));
183
184     shader_version = D3DXGetShaderVersion(simple_ps);
185     ok(shader_version == D3DPS_VERSION(1, 1), "Got shader version 0x%08x, expected 0x%08x\n",
186             shader_version, D3DPS_VERSION(1, 1));
187
188     shader_version = D3DXGetShaderVersion(NULL);
189     ok(shader_version == 0, "Got shader version 0x%08x, expected 0\n", shader_version);
190 }
191
192 static void test_find_shader_comment(void)
193 {
194     HRESULT hr;
195     LPCVOID data;
196     UINT size;
197
198     hr = D3DXFindShaderComment(NULL, MAKEFOURCC('C','T','A','B'), &data, &size);
199     ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
200
201     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), NULL, &size);
202     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
203
204     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), &data, NULL);
205     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
206
207     hr = D3DXFindShaderComment(shader_with_ctab, 0, &data, &size);
208     ok(hr == S_FALSE, "Got result %x, expected 1 (S_FALSE)\n", hr);
209
210     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('X','X','X','X'), &data, &size);
211     ok(hr == S_FALSE, "Got result %x, expected 1 (S_FALSE)\n", hr);
212
213     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), &data, &size);
214     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
215     ok(data == (LPCVOID)(shader_with_ctab + 6), "Got result %p, expected %p\n", data, shader_with_ctab + 6);
216     ok(size == 28, "Got result %d, expected 28\n", size);
217 }
218
219 static void test_get_shader_constant_table_ex(void)
220 {
221     LPD3DXCONSTANTTABLE constant_table = NULL;
222     HRESULT hr;
223     LPVOID data;
224     DWORD size;
225     D3DXCONSTANTTABLE_DESC desc;
226
227     hr = D3DXGetShaderConstantTableEx(NULL, 0, &constant_table);
228     ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
229
230     /* No CTAB data */
231     hr = D3DXGetShaderConstantTableEx(simple_ps, 0, &constant_table);
232     ok(hr == D3DXERR_INVALIDDATA, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DXERR_INVALIDDATA);
233
234     /* With invalid CTAB data */
235     hr = D3DXGetShaderConstantTableEx(shader_with_invalid_ctab, 0, &constant_table);
236     ok(hr == D3DXERR_INVALIDDATA || broken(hr == D3D_OK), /* winxp 64-bit, w2k3 64-bit */
237        "Got result %x, expected %x (D3DXERR_INVALIDDATA)\n", hr, D3DXERR_INVALIDDATA);
238     if (constant_table) ID3DXConstantTable_Release(constant_table);
239
240     hr = D3DXGetShaderConstantTableEx(shader_with_ctab, 0, &constant_table);
241     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
242
243     if (constant_table)
244     {
245         size = ID3DXConstantTable_GetBufferSize(constant_table);
246         ok(size == 28, "Got result %x, expected 28\n", size);
247
248         data = ID3DXConstantTable_GetBufferPointer(constant_table);
249         ok(!memcmp(data, shader_with_ctab + 6, size), "Retrieved wrong CTAB data\n");
250
251         hr = ID3DXConstantTable_GetDesc(constant_table, NULL);
252         ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
253
254         hr = ID3DXConstantTable_GetDesc(constant_table, &desc);
255         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
256         ok(desc.Creator == (LPCSTR)data + 0x10, "Got result %p, expected %p\n", desc.Creator, (LPCSTR)data + 0x10);
257         ok(desc.Version == D3DVS_VERSION(3, 0), "Got result %x, expected %x\n", desc.Version, D3DVS_VERSION(3, 0));
258         ok(desc.Constants == 0, "Got result %x, expected 0\n", desc.Constants);
259
260         ID3DXConstantTable_Release(constant_table);
261     }
262
263     hr = D3DXGetShaderConstantTableEx(shader_with_ctab_constants, 0, &constant_table);
264     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
265
266     if (SUCCEEDED(hr))
267     {
268         D3DXHANDLE constant;
269         D3DXCONSTANT_DESC constant_desc;
270         D3DXCONSTANT_DESC constant_desc_save;
271         UINT nb;
272
273         /* Test GetDesc */
274         hr = ID3DXConstantTable_GetDesc(constant_table, &desc);
275         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
276         ok(!strcmp(desc.Creator, "Wine project"), "Got result '%s', expected 'Wine project'\n", desc.Creator);
277         ok(desc.Version == D3DVS_VERSION(3, 0), "Got result %x, expected %x\n", desc.Version, D3DVS_VERSION(3, 0));
278         ok(desc.Constants == 3, "Got result %x, expected 3\n", desc.Constants);
279
280         /* Test GetConstant */
281         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 0);
282         ok(constant != NULL, "No constant found\n");
283         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, &nb);
284         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
285         ok(!strcmp(constant_desc.Name, "Constant1"), "Got result '%s', expected 'Constant1'\n",
286             constant_desc.Name);
287         ok(constant_desc.Class == D3DXPC_VECTOR, "Got result %x, expected %u (D3DXPC_VECTOR)\n",
288             constant_desc.Class, D3DXPC_VECTOR);
289         ok(constant_desc.Type == D3DXPT_FLOAT, "Got result %x, expected %u (D3DXPT_FLOAT)\n",
290             constant_desc.Type, D3DXPT_FLOAT);
291         ok(constant_desc.Rows == 1, "Got result %x, expected 1\n", constant_desc.Rows);
292         ok(constant_desc.Columns == 4, "Got result %x, expected 4\n", constant_desc.Columns);
293
294         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 1);
295         ok(constant != NULL, "No constant found\n");
296         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, &nb);
297         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
298         ok(!strcmp(constant_desc.Name, "Constant2"), "Got result '%s', expected 'Constant2'\n",
299             constant_desc.Name);
300         ok(constant_desc.Class == D3DXPC_MATRIX_COLUMNS, "Got result %x, expected %u (D3DXPC_MATRIX_COLUMNS)\n",
301             constant_desc.Class, D3DXPC_MATRIX_COLUMNS);
302         ok(constant_desc.Type == D3DXPT_FLOAT, "Got result %x, expected %u (D3DXPT_FLOAT)\n",
303             constant_desc.Type, D3DXPT_FLOAT);
304         ok(constant_desc.Rows == 4, "Got result %x, expected 1\n", constant_desc.Rows);
305         ok(constant_desc.Columns == 4, "Got result %x, expected 4\n", constant_desc.Columns);
306
307         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 2);
308         ok(constant != NULL, "No constant found\n");
309         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, &nb);
310         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
311         ok(!strcmp(constant_desc.Name, "Constant3"), "Got result '%s', expected 'Constant3'\n",
312             constant_desc.Name);
313         ok(constant_desc.Class == D3DXPC_MATRIX_COLUMNS, "Got result %x, expected %u (D3DXPC_MATRIX_COLUMNS)\n",
314             constant_desc.Class, D3DXPC_MATRIX_COLUMNS);
315         ok(constant_desc.Type == D3DXPT_FLOAT, "Got result %x, expected %u (D3DXPT_FLOAT)\n",
316             constant_desc.Type, D3DXPT_FLOAT);
317         ok(constant_desc.Rows == 4, "Got result %x, expected 1\n", constant_desc.Rows);
318         ok(constant_desc.Columns == 4, "Got result %x, expected 4\n", constant_desc.Columns);
319         constant_desc_save = constant_desc; /* For GetConstantDesc test */
320
321         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 3);
322         ok(constant == NULL, "Got result %p, expected NULL\n", constant);
323
324         /* Test GetConstantByName */
325         constant = ID3DXConstantTable_GetConstantByName(constant_table, NULL, "Constant unknown");
326         ok(constant == NULL, "Got result %p, expected NULL\n", constant);
327         constant = ID3DXConstantTable_GetConstantByName(constant_table, NULL, "Constant3");
328         ok(constant != NULL, "No constant found\n");
329         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, &nb);
330         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
331         ok(!memcmp(&constant_desc, &constant_desc_save, sizeof(D3DXCONSTANT_DESC)), "Got different constant data\n");
332
333         /* Test GetConstantDesc */
334         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 0);
335         ok(constant != NULL, "No constant found\n");
336         hr = ID3DXConstantTable_GetConstantDesc(constant_table, NULL, &constant_desc, &nb);
337         ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
338         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, NULL, &nb);
339         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
340         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, NULL);
341         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
342         hr = ID3DXConstantTable_GetConstantDesc(constant_table, "Constant unknow", &constant_desc, &nb);
343         ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
344         hr = ID3DXConstantTable_GetConstantDesc(constant_table, "Constant3", &constant_desc, &nb);
345         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
346         ok(!memcmp(&constant_desc, &constant_desc_save, sizeof(D3DXCONSTANT_DESC)), "Got different constant data\n");
347
348         ID3DXConstantTable_Release(constant_table);
349     }
350 }
351
352 static void test_constant_table(const char *test_name, const DWORD *ctable_fn,
353         const D3DXCONSTANT_DESC *expecteds, UINT count)
354 {
355     UINT i;
356     ID3DXConstantTable *ctable;
357
358     HRESULT res;
359
360     /* Get the constant table from the shader itself */
361     res = D3DXGetShaderConstantTable(ctable_fn, &ctable);
362     ok(res == D3D_OK, "D3DXGetShaderConstantTable failed on %s: got %08x\n", test_name, res);
363
364     for (i = 0; i < count; i++)
365     {
366         const D3DXCONSTANT_DESC *expected = &expecteds[i];
367         D3DXHANDLE const_handle;
368         D3DXCONSTANT_DESC actual;
369         UINT pCount = 1;
370
371         const_handle = ID3DXConstantTable_GetConstantByName(ctable, NULL, expected->Name);
372
373         ID3DXConstantTable_GetConstantDesc(ctable, const_handle, &actual, &pCount);
374         ok(pCount == 1, "Got more or less descriptions: %d\n", pCount);
375
376         ok(strcmp(actual.Name, expected->Name) == 0,
377            "%s in %s: Got different names: Got %s, expected %s\n", expected->Name,
378            test_name, actual.Name, expected->Name);
379         ok(actual.RegisterSet == expected->RegisterSet,
380            "%s in %s: Got different register sets: Got %d, expected %d\n",
381            expected->Name, test_name, actual.RegisterSet, expected->RegisterSet);
382         ok(actual.RegisterIndex == expected->RegisterIndex,
383            "%s in %s: Got different register indices: Got %d, expected %d\n",
384            expected->Name, test_name, actual.RegisterIndex, expected->RegisterIndex);
385         ok(actual.RegisterCount == expected->RegisterCount,
386            "%s in %s: Got different register counts: Got %d, expected %d\n",
387            expected->Name, test_name, actual.RegisterCount, expected->RegisterCount);
388         ok(actual.Class == expected->Class,
389            "%s in %s: Got different classes: Got %d, expected %d\n", expected->Name,
390            test_name, actual.Class, expected->Class);
391         ok(actual.Type == expected->Type,
392            "%s in %s: Got different types: Got %d, expected %d\n", expected->Name,
393            test_name, actual.Type, expected->Type);
394         ok(actual.Rows == expected->Rows && actual.Columns == expected->Columns,
395            "%s in %s: Got different dimensions: Got (%d, %d), expected (%d, %d)\n",
396            expected->Name, test_name, actual.Rows, actual.Columns, expected->Rows,
397            expected->Columns);
398         ok(actual.Elements == expected->Elements,
399            "%s in %s: Got different element count: Got %d, expected %d\n",
400            expected->Name, test_name, actual.Elements, expected->Elements);
401         ok(actual.StructMembers == expected->StructMembers,
402            "%s in %s: Got different struct member count: Got %d, expected %d\n",
403            expected->Name, test_name, actual.StructMembers, expected->StructMembers);
404         ok(actual.Bytes == expected->Bytes,
405            "%s in %s: Got different byte count: Got %d, expected %d\n",
406            expected->Name, test_name, actual.Bytes, expected->Bytes);
407     }
408
409     /* Finally, release the constant table */
410     ID3DXConstantTable_Release(ctable);
411 }
412
413 static void test_constant_tables(void)
414 {
415     test_constant_table("test_basic", ctab_basic, ctab_basic_expected,
416             sizeof(ctab_basic_expected)/sizeof(*ctab_basic_expected));
417     test_constant_table("test_matrices", ctab_matrices, ctab_matrices_expected,
418             sizeof(ctab_matrices_expected)/sizeof(*ctab_matrices_expected));
419     test_constant_table("test_arrays", ctab_arrays, ctab_arrays_expected,
420             sizeof(ctab_arrays_expected)/sizeof(*ctab_arrays_expected));
421 }
422
423 static void test_setting_basic_table(IDirect3DDevice9 *device)
424 {
425     static const D3DXMATRIX mvp = {{{
426         0.514f, 0.626f, 0.804f, 0.786f,
427         0.238f, 0.956f, 0.374f, 0.483f,
428         0.109f, 0.586f, 0.900f, 0.255f,
429         0.898f, 0.411f, 0.932f, 0.275f}}};
430     static const D3DXVECTOR4 f4 = {0.350f, 0.526f, 0.925f, 0.021f};
431     static const float f = 0.12543f;
432     static const int i = 321;
433
434     ID3DXConstantTable *ctable;
435
436     HRESULT res;
437     float out[16];
438     ULONG refcnt;
439
440     /* Get the constant table from the shader itself */
441     res = D3DXGetShaderConstantTable(ctab_basic, &ctable);
442     ok(res == D3D_OK, "D3DXGetShaderConstantTable failed: got 0x%08x\n", res);
443
444     /* Set constants */
445     res = ID3DXConstantTable_SetMatrix(ctable, device, "mvp", &mvp);
446     ok(res == D3D_OK, "ID3DXConstantTable_SetMatrix failed on variable mvp: got 0x%08x\n", res);
447
448     ID3DXConstantTable_SetInt(ctable, device, "i", i + 1);
449     ok(res == D3D_OK, "ID3DXConstantTable_SetInt failed on variable i: got 0x%08x\n", res);
450
451     /* Check that setting i again will overwrite the previous value */
452     res = ID3DXConstantTable_SetInt(ctable, device, "i", i);
453     ok(res == D3D_OK, "ID3DXConstantTable_SetInt failed on variable i: got 0x%08x\n", res);
454
455     res = ID3DXConstantTable_SetFloat(ctable, device, "f", f);
456     ok(res == D3D_OK, "ID3DXConstantTable_SetFloat failed on variable f: got 0x%08x\n", res);
457
458     res = ID3DXConstantTable_SetVector(ctable, device, "f4", &f4);
459     ok(res == D3D_OK, "ID3DXConstantTable_SetVector failed on variable f4: got 0x%08x\n", res);
460
461     /* Get constants back and validate */
462     IDirect3DDevice9_GetVertexShaderConstantF(device, 0, out, 4);
463     ok(out[0] == mvp._11 && out[4] == mvp._12 && out[8] == mvp._13 && out[12] == mvp._14,
464             "The first row of mvp was not set correctly, got {%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
465             out[0], out[4], out[8], out[12], mvp._11, mvp._12, mvp._13, mvp._14);
466     ok(out[1] == mvp._21 && out[5] == mvp._22 && out[9] == mvp._23 && out[13] == mvp._24,
467             "The second row of mvp was not set correctly, got {%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
468             out[1], out[5], out[9], out[13], mvp._21, mvp._22, mvp._23, mvp._24);
469     ok(out[2] == mvp._31 && out[6] == mvp._32 && out[10] == mvp._33 && out[14] == mvp._34,
470             "The third row of mvp was not set correctly, got {%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
471             out[2], out[6], out[10], out[14], mvp._31, mvp._32, mvp._33, mvp._34);
472     ok(out[3] == mvp._41 && out[7] == mvp._42 && out[11] == mvp._43 && out[15] == mvp._44,
473             "The fourth row of mvp was not set correctly, got {%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
474             out[3], out[7], out[11], out[15], mvp._41, mvp._42, mvp._43, mvp._44);
475
476     IDirect3DDevice9_GetVertexShaderConstantF(device, 4, out, 1);
477     ok(out[0] == (float)i && out[1] == 0.0f && out[2] == 0.0f && out[3] == 0.0f,
478             "The variable i was not set correctly, out={%f, %f, %f, %f}, should be {%d, 0.0, 0.0, 0.0}\n",
479             out[0], out[1], out[2], out[3], i);
480
481     IDirect3DDevice9_GetVertexShaderConstantF(device, 6, out, 1);
482     ok(out[0] == f && out[1] == 0.0f && out[2] == 0.0f && out[3] == 0.0f,
483             "The variable f was not set correctly, out={%f, %f, %f, %f}, should be {%f, 0.0, 0.0, 0.0}\n",
484             out[0], out[1], out[2], out[3], f);
485
486     IDirect3DDevice9_GetVertexShaderConstantF(device, 7, out, 1);
487     ok(memcmp(out, (void*)&f4, sizeof(f4)) == 0,
488             "The variable f4 was not set correctly, out={%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
489             out[0], out[1], out[2], out[3], f4.x, f4.y, f4.z, f4.w);
490
491     /* Finally test using a set* function for one type to set a variable of another type (should succeed) */
492     res = ID3DXConstantTable_SetVector(ctable, device, "f", &f4);
493     ok(res == D3D_OK, "ID3DXConstantTable_SetVector failed on variable f: 0x%08x\n", res);
494
495     IDirect3DDevice9_GetVertexShaderConstantF(device, 6, out, 1);
496     ok(out[0] == f4.x, "The variable f was not set correctly by ID3DXConstantTable_SetVector, got %f, should be %f\n",
497             out[0], f4.x);
498
499     refcnt = ID3DXConstantTable_Release(ctable);
500     ok(refcnt == 0, "The constant table reference count was %u, should be 0\n", refcnt);
501 }
502
503 static void test_setting_arrays_table(IDirect3DDevice9 *device)
504 {
505     static const float farray[8] = {
506         0.005f, 0.745f, 0.973f, 0.264f,
507         0.010f, 0.020f, 0.030f, 0.040f};
508     static const D3DXMATRIX fmtxarray[2] = {
509         {{{0.001f, 0.002f, 0.003f, 0.004f,
510            0.005f, 0.006f, 0.007f, 0.008f,
511            0.009f, 0.010f, 0.011f, 0.012f,
512            0.013f, 0.014f, 0.015f, 0.016f}}},
513         {{{0.010f, 0.020f, 0.030f, 0.040f,
514            0.050f, 0.060f, 0.070f, 0.080f,
515            0.090f, 0.100f, 0.110f, 0.120f,
516            0.130f, 0.140f, 0.150f, 0.160f}}}};
517     static const int iarray[4] = {1, 2, 3, 4};
518     static const D3DXVECTOR4 fvecarray[2] = {
519         {0.745f, 0.997f, 0.353f, 0.237f},
520         {0.060f, 0.455f, 0.333f, 0.983f}};
521
522     ID3DXConstantTable *ctable;
523
524     HRESULT res;
525     float out[32];
526     ULONG refcnt;
527
528     /* Get the constant table from the shader */
529     res = D3DXGetShaderConstantTable(ctab_arrays, &ctable);
530     ok(res == D3D_OK, "D3DXGetShaderConstantTable failed: got 0x%08x\n", res);
531
532     /* Set constants */
533
534     /* Make sure that we cannot set registers that do not belong to this constant */
535     res = ID3DXConstantTable_SetFloatArray(ctable, device, "farray", farray, 8);
536     ok(res == D3D_OK, "ID3DXConstantTable_SetFloatArray failed: got 0x%08x\n", res);
537
538     IDirect3DDevice9_GetVertexShaderConstantF(device, 8, out, 8);
539     ok(out[0] == farray[0] && out[4] == farray[1] && out[8] == farray[2] && out[12] == farray[3],
540             "The in-bounds elements of the array were not set, out={%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
541             out[0], out[4], out[8], out[12], farray[0], farray[1], farray[2], farray[3]);
542     ok(out[16] == 0.0f && out[20] == 0.0f && out[24] == 0.0f && out[28] == 0.0f,
543             "The excess elements of the array were set, out={%f, %f, %f, %f}, should be all 0.0f",
544             out[16], out[20], out[24], out[28]);
545
546     /* ivecarray takes up only 1 register, but a matrix takes up 4, so no elements should be set */
547     res = ID3DXConstantTable_SetMatrix(ctable, device, "ivecarray", &fmtxarray[0]);
548     ok(res == D3D_OK, "ID3DXConstantTable_SetMatrix failed: got 0x%08x\n", res);
549
550     IDirect3DDevice9_GetVertexShaderConstantF(device, 18, out, 4);
551     ok(out[0] == 0.0f && out[1] == 0.0f && out[2] == 0.0f && out[3] == 0.0f,
552        "The array was set, out={%f, %f, %f, %f}, should be all 0.0f\n", out[0], out[1], out[2], out[3]);
553
554     /* Try setting an integer array to an array declared as a float array */
555     res = ID3DXConstantTable_SetIntArray(ctable, device, "farray", iarray, 4);
556     ok(res == D3D_OK, "ID3DXConstantTable_SetIntArray failed: got 0x%08x\n", res);
557
558     IDirect3DDevice9_GetVertexShaderConstantF(device, 8, out, 4);
559     ok(out[0] == iarray[0] && out[4] == iarray[1] && out[8] == iarray[2] && out[12] == iarray[3],
560            "SetIntArray did not properly set a float array: out={%f, %f, %f, %f}, should be {%d, %d, %d, %d}\n",
561             out[0], out[4], out[8], out[12], iarray[0], iarray[1], iarray[2], iarray[3]);
562
563     res = ID3DXConstantTable_SetFloatArray(ctable, device, "farray", farray, 4);
564     ok(res == D3D_OK, "ID3DXConstantTable_SetFloatArray failed: got x0%08x\n", res);
565
566     res = ID3DXConstantTable_SetVectorArray(ctable, device, "fvecarray", fvecarray, 2);
567     ok(res == D3D_OK, "ID3DXConstantTable_SetVectorArray failed: got 0x%08x\n", res);
568
569     res = ID3DXConstantTable_SetMatrixArray(ctable, device, "fmtxarray", fmtxarray, 2);
570     ok(res == D3D_OK, "ID3DXConstantTable_SetMatrixArray failed: got 0x%08x\n", res);
571
572     /* Read back constants */
573     IDirect3DDevice9_GetVertexShaderConstantF(device, 8, out, 4);
574     ok(out[0] == farray[0] && out[4] == farray[1] && out[8] == farray[2] && out[12] == farray[3],
575             "The variable farray was not set correctly, out={%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
576             out[0], out[4], out[8], out[12], farray[0], farray[1], farray[2], farray[3]);
577
578     IDirect3DDevice9_GetVertexShaderConstantF(device, 12, out, 2);
579     ok(out[0] == fvecarray[0].x && out[1] == fvecarray[0].y && out[2] == fvecarray[0].z && out[3] == fvecarray[0].w &&
580             out[4] == fvecarray[1].x && out[5] == fvecarray[1].y && out[6] == fvecarray[1].z && out[7] == fvecarray[1].w,
581             "The variable fvecarray was not set correctly, out={{%f, %f, %f, %f}, {%f, %f, %f, %f}}, should be "
582             "{{%f, %f, %f, %f}, {%f, %f, %f, %f}}\n", out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
583             fvecarray[0].x, fvecarray[0].y, fvecarray[0].z, fvecarray[0].w, fvecarray[1].x, fvecarray[1].y,
584             fvecarray[1].z, fvecarray[1].w);
585
586     IDirect3DDevice9_GetVertexShaderConstantF(device, 0, out, 8);
587     /* Just check a few elements in each matrix to make sure fmtxarray was set row-major */
588     ok(out[0] == fmtxarray[0]._11 && out[1] == fmtxarray[0]._12 && out[2] == fmtxarray[0]._13 && out[3] == fmtxarray[0]._14,
589            "The variable fmtxarray was not set row-major, out={%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
590            out[0], out[1], out[2], out[3], fmtxarray[0]._11, fmtxarray[0]._12, fmtxarray[0]._13, fmtxarray[0]._14);
591     ok(out[16] == fmtxarray[1]._11 && out[17] == fmtxarray[1]._12 && out[18] == fmtxarray[1]._13 && out[19] == fmtxarray[1]._14,
592            "The variable fmtxarray was not set row-major, out={%f, %f, %f, %f}, should be {%f, %f, %f, %f}\n",
593            out[16], out[17], out[18], out[19], fmtxarray[1]._11, fmtxarray[1]._12, fmtxarray[1]._13, fmtxarray[1]._14);
594
595     refcnt = ID3DXConstantTable_Release(ctable);
596     ok(refcnt == 0, "The constant table reference count was %u, should be 0\n", refcnt);
597 }
598
599 static void test_setting_constants(void)
600 {
601     HWND wnd;
602     IDirect3D9 *d3d;
603     IDirect3DDevice9 *device;
604     D3DPRESENT_PARAMETERS d3dpp;
605     HRESULT hr;
606     ULONG refcnt;
607
608     /* Create the device to use for our tests */
609     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
610     d3d = Direct3DCreate9(D3D_SDK_VERSION);
611     if (!wnd)
612     {
613         skip("Couldn't create application window\n");
614         return;
615     }
616     if (!d3d)
617     {
618         skip("Couldn't create IDirect3D9 object\n");
619         DestroyWindow(wnd);
620         return;
621     }
622
623     ZeroMemory(&d3dpp, sizeof(d3dpp));
624     d3dpp.Windowed   = TRUE;
625     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
626     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
627     if (FAILED(hr))
628     {
629         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
630         IDirect3D9_Release(d3d);
631         DestroyWindow(wnd);
632         return;
633     }
634
635     test_setting_basic_table(device);
636     test_setting_arrays_table(device);
637
638     /* Release resources */
639     refcnt = IDirect3DDevice9_Release(device);
640     ok(refcnt == 0, "The Direct3D device reference count was %u, should be 0\n", refcnt);
641
642     refcnt = IDirect3D9_Release(d3d);
643     ok(refcnt == 0, "The Direct3D object referenct count was %u, should be 0\n", refcnt);
644
645     if (wnd) DestroyWindow(wnd);
646 }
647
648 START_TEST(shader)
649 {
650     test_get_shader_size();
651     test_get_shader_version();
652     test_find_shader_comment();
653     test_get_shader_constant_table_ex();
654     test_constant_tables();
655     test_setting_constants();
656 }