d3dx9: Constant table parsing foundations.
[wine] / dlls / d3dx9_36 / tests / shader.c
1 /*
2  * Copyright 2008 Luis Busquets
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 #include "wine/test.h"
20 #include "d3dx9.h"
21
22 static const DWORD simple_vs[] = {
23     0xfffe0101,                                                             /* vs_1_1                       */
24     0x0000001f, 0x80000000, 0x900f0000,                                     /* dcl_position0 v0             */
25     0x00000009, 0xc0010000, 0x90e40000, 0xa0e40000,                         /* dp4 oPos.x, v0, c0           */
26     0x00000009, 0xc0020000, 0x90e40000, 0xa0e40001,                         /* dp4 oPos.y, v0, c1           */
27     0x00000009, 0xc0040000, 0x90e40000, 0xa0e40002,                         /* dp4 oPos.z, v0, c2           */
28     0x00000009, 0xc0080000, 0x90e40000, 0xa0e40003,                         /* dp4 oPos.w, v0, c3           */
29     0x0000ffff};                                                            /* END                          */
30
31 static const DWORD simple_ps[] = {
32     0xffff0101,                                                             /* ps_1_1                       */
33     0x00000051, 0xa00f0001, 0x3f800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
34     0x00000042, 0xb00f0000,                                                 /* tex t0                       */
35     0x00000008, 0x800f0000, 0xa0e40001, 0xa0e40000,                         /* dp3 r0, c1, c0               */
36     0x00000005, 0x800f0000, 0x90e40000, 0x80e40000,                         /* mul r0, v0, r0               */
37     0x00000005, 0x800f0000, 0xb0e40000, 0x80e40000,                         /* mul r0, t0, r0               */
38     0x0000ffff};                                                            /* END                          */
39
40 #define FCC_TEXT MAKEFOURCC('T','E','X','T')
41 #define FCC_CTAB MAKEFOURCC('C','T','A','B')
42
43 static const DWORD shader_with_ctab[] = {
44     0xfffe0300,                                                             /* vs_3_0                       */
45     0x0002fffe, FCC_TEXT,   0x00000000,                                     /* TEXT comment                 */
46     0x0008fffe, FCC_CTAB,   0x0000001c, 0x00000010, 0xfffe0300, 0x00000000, /* CTAB comment                 */
47                 0x00000000, 0x00000000, 0x00000000,
48     0x0004fffe, FCC_TEXT,   0x00000000, 0x00000000, 0x00000000,             /* TEXT comment                 */
49     0x0000ffff};                                                            /* END                          */
50
51 static const DWORD shader_with_invalid_ctab[] = {
52     0xfffe0300,                                                             /* vs_3_0                       */
53     0x0005fffe, FCC_CTAB,                                                   /* CTAB comment                 */
54                 0x0000001c, 0x000000a9, 0xfffe0300,
55                 0x00000000, 0x00000000,
56     0x0000ffff};                                                            /* END                          */
57
58 static const DWORD shader_with_ctab_constants[] = {
59     0xfffe0300,                                                             /* vs_3_0                       */
60     0x002efffe, FCC_CTAB,                                                   /* CTAB comment                 */
61     0x0000001c, 0x000000a4, 0xfffe0300, 0x00000003, 0x0000001c, 0x20008100, /* Header                       */
62     0x0000009c,
63     0x00000058, 0x00070002, 0x00000001, 0x00000064, 0x00000000,             /* Constant 1 desc              */
64     0x00000074, 0x00000002, 0x00000004, 0x00000080, 0x00000000,             /* Constant 2 desc              */
65     0x00000090, 0x00040002, 0x00000003, 0x00000080, 0x00000000,             /* Constant 3 desc              */
66     0x736e6f43, 0x746e6174, 0xabab0031,                                     /* Constant 1 name string       */
67     0x00030001, 0x00040001, 0x00000001, 0x00000000,                         /* Constant 1 type desc         */
68     0x736e6f43, 0x746e6174, 0xabab0032,                                     /* Constant 2 name string       */
69     0x00030003, 0x00040004, 0x00000001, 0x00000000,                         /* Constant 2 & 3 type desc     */
70     0x736e6f43, 0x746e6174, 0xabab0033,                                     /* Constant 3 name string       */
71     0x335f7376, 0xab00305f,                                                 /* Target name string           */
72     0x656e6957, 0x6f727020, 0x7463656a, 0xababab00,                         /* Creator name string          */
73     0x0000ffff};                                                            /* END                          */
74
75 static void test_get_shader_size(void)
76 {
77     UINT shader_size, expected;
78
79     shader_size = D3DXGetShaderSize(simple_vs);
80     expected = sizeof(simple_vs);
81     ok(shader_size == expected, "Got shader size %u, expected %u\n", shader_size, expected);
82
83     shader_size = D3DXGetShaderSize(simple_ps);
84     expected = sizeof(simple_ps);
85     ok(shader_size == expected, "Got shader size %u, expected %u\n", shader_size, expected);
86
87     shader_size = D3DXGetShaderSize(NULL);
88     ok(shader_size == 0, "Got shader size %u, expected 0\n", shader_size);
89 }
90
91 static void test_get_shader_version(void)
92 {
93     DWORD shader_version;
94
95     shader_version = D3DXGetShaderVersion(simple_vs);
96     ok(shader_version == D3DVS_VERSION(1, 1), "Got shader version 0x%08x, expected 0x%08x\n",
97             shader_version, D3DVS_VERSION(1, 1));
98
99     shader_version = D3DXGetShaderVersion(simple_ps);
100     ok(shader_version == D3DPS_VERSION(1, 1), "Got shader version 0x%08x, expected 0x%08x\n",
101             shader_version, D3DPS_VERSION(1, 1));
102
103     shader_version = D3DXGetShaderVersion(NULL);
104     ok(shader_version == 0, "Got shader version 0x%08x, expected 0\n", shader_version);
105 }
106
107 static void test_find_shader_comment(void)
108 {
109     HRESULT hr;
110     LPCVOID data;
111     UINT size;
112
113     hr = D3DXFindShaderComment(NULL, MAKEFOURCC('C','T','A','B'), &data, &size);
114     ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
115
116     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), NULL, &size);
117     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
118
119     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), &data, NULL);
120     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
121
122     hr = D3DXFindShaderComment(shader_with_ctab, 0, &data, &size);
123     ok(hr == S_FALSE, "Got result %x, expected 1 (S_FALSE)\n", hr);
124
125     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('X','X','X','X'), &data, &size);
126     ok(hr == S_FALSE, "Got result %x, expected 1 (S_FALSE)\n", hr);
127
128     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), &data, &size);
129     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
130     ok(data == (LPCVOID)(shader_with_ctab + 6), "Got result %p, expected %p\n", data, shader_with_ctab + 6);
131     ok(size == 28, "Got result %d, expected 28\n", size);
132 }
133
134 static void test_get_shader_constant_table_ex(void)
135 {
136     LPD3DXCONSTANTTABLE constant_table = NULL;
137     HRESULT hr;
138     LPVOID data;
139     DWORD size;
140     D3DXCONSTANTTABLE_DESC desc;
141
142     hr = D3DXGetShaderConstantTableEx(NULL, 0, &constant_table);
143     ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
144
145     /* No CTAB data */
146     hr = D3DXGetShaderConstantTableEx(simple_ps, 0, &constant_table);
147     ok(hr == D3DXERR_INVALIDDATA, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DXERR_INVALIDDATA);
148
149     /* With invalid CTAB data */
150     hr = D3DXGetShaderConstantTableEx(shader_with_invalid_ctab, 0, &constant_table);
151     ok(hr == D3DXERR_INVALIDDATA || broken(hr == D3D_OK), /* winxp 64-bit, w2k3 64-bit */
152        "Got result %x, expected %x (D3DXERR_INVALIDDATA)\n", hr, D3DXERR_INVALIDDATA);
153     if (constant_table) ID3DXConstantTable_Release(constant_table);
154
155     hr = D3DXGetShaderConstantTableEx(shader_with_ctab, 0, &constant_table);
156     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
157
158     if (constant_table)
159     {
160         size = ID3DXConstantTable_GetBufferSize(constant_table);
161         ok(size == 28, "Got result %x, expected 28\n", size);
162
163         data = ID3DXConstantTable_GetBufferPointer(constant_table);
164         ok(!memcmp(data, shader_with_ctab + 6, size), "Retrieved wrong CTAB data\n");
165
166         hr = ID3DXConstantTable_GetDesc(constant_table, NULL);
167         ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
168
169         hr = ID3DXConstantTable_GetDesc(constant_table, &desc);
170         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
171         ok(desc.Creator == (LPCSTR)data + 0x10, "Got result %p, expected %p\n", desc.Creator, (LPCSTR)data + 0x10);
172         ok(desc.Version == D3DVS_VERSION(3, 0), "Got result %x, expected %x\n", desc.Version, D3DVS_VERSION(3, 0));
173         ok(desc.Constants == 0, "Got result %x, expected 0\n", desc.Constants);
174
175         ID3DXConstantTable_Release(constant_table);
176     }
177
178     hr = D3DXGetShaderConstantTableEx(shader_with_ctab_constants, 0, &constant_table);
179     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
180
181     if (SUCCEEDED(hr))
182     {
183         D3DXHANDLE constant;
184         D3DXCONSTANT_DESC constant_desc;
185         D3DXCONSTANT_DESC constant_desc_save;
186         UINT nb;
187
188         /* Test GetDesc */
189         hr = ID3DXConstantTable_GetDesc(constant_table, &desc);
190         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
191         ok(!strcmp(desc.Creator, "Wine project"), "Got result '%s', expected 'Wine project'\n", desc.Creator);
192         ok(desc.Version == D3DVS_VERSION(3, 0), "Got result %x, expected %x\n", desc.Version, D3DVS_VERSION(3, 0));
193         ok(desc.Constants == 3, "Got result %x, expected 3\n", desc.Constants);
194
195         /* Test GetConstant */
196         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 0);
197         ok(constant != NULL, "No constant found\n");
198         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, &nb);
199         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
200         ok(!strcmp(constant_desc.Name, "Constant1"), "Got result '%s', expected 'Constant1'\n",
201             constant_desc.Name);
202         ok(constant_desc.Class == D3DXPC_VECTOR, "Got result %x, expected %u (D3DXPC_VECTOR)\n",
203             constant_desc.Class, D3DXPC_VECTOR);
204         ok(constant_desc.Type == D3DXPT_FLOAT, "Got result %x, expected %u (D3DXPT_FLOAT)\n",
205             constant_desc.Type, D3DXPT_FLOAT);
206         ok(constant_desc.Rows == 1, "Got result %x, expected 1\n", constant_desc.Rows);
207         ok(constant_desc.Columns == 4, "Got result %x, expected 4\n", constant_desc.Columns);
208
209         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 1);
210         ok(constant != NULL, "No constant found\n");
211         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, &nb);
212         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
213         ok(!strcmp(constant_desc.Name, "Constant2"), "Got result '%s', expected 'Constant2'\n",
214             constant_desc.Name);
215         ok(constant_desc.Class == D3DXPC_MATRIX_COLUMNS, "Got result %x, expected %u (D3DXPC_MATRIX_COLUMNS)\n",
216             constant_desc.Class, D3DXPC_MATRIX_COLUMNS);
217         ok(constant_desc.Type == D3DXPT_FLOAT, "Got result %x, expected %u (D3DXPT_FLOAT)\n",
218             constant_desc.Type, D3DXPT_FLOAT);
219         ok(constant_desc.Rows == 4, "Got result %x, expected 1\n", constant_desc.Rows);
220         ok(constant_desc.Columns == 4, "Got result %x, expected 4\n", constant_desc.Columns);
221
222         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 2);
223         ok(constant != NULL, "No constant found\n");
224         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, &nb);
225         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
226         ok(!strcmp(constant_desc.Name, "Constant3"), "Got result '%s', expected 'Constant3'\n",
227             constant_desc.Name);
228         ok(constant_desc.Class == D3DXPC_MATRIX_COLUMNS, "Got result %x, expected %u (D3DXPC_MATRIX_COLUMNS)\n",
229             constant_desc.Class, D3DXPC_MATRIX_COLUMNS);
230         ok(constant_desc.Type == D3DXPT_FLOAT, "Got result %x, expected %u (D3DXPT_FLOAT)\n",
231             constant_desc.Type, D3DXPT_FLOAT);
232         ok(constant_desc.Rows == 4, "Got result %x, expected 1\n", constant_desc.Rows);
233         ok(constant_desc.Columns == 4, "Got result %x, expected 4\n", constant_desc.Columns);
234         constant_desc_save = constant_desc; /* For GetConstantDesc test */
235
236         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 3);
237         ok(constant == NULL, "Got result %p, expected NULL\n", constant);
238
239         /* Test GetConstantByName */
240         constant = ID3DXConstantTable_GetConstantByName(constant_table, NULL, "Constant unknown");
241         ok(constant == NULL, "Got result %p, expected NULL\n", constant);
242         constant = ID3DXConstantTable_GetConstantByName(constant_table, NULL, "Constant3");
243         ok(constant != NULL, "No constant found\n");
244         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, &nb);
245         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
246         ok(!memcmp(&constant_desc, &constant_desc_save, sizeof(D3DXCONSTANT_DESC)), "Got different constant data\n");
247
248         /* Test GetConstantDesc */
249         constant = ID3DXConstantTable_GetConstant(constant_table, NULL, 0);
250         ok(constant != NULL, "No constant found\n");
251         hr = ID3DXConstantTable_GetConstantDesc(constant_table, NULL, &constant_desc, &nb);
252         ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
253         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, NULL, &nb);
254         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
255         hr = ID3DXConstantTable_GetConstantDesc(constant_table, constant, &constant_desc, NULL);
256         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
257         hr = ID3DXConstantTable_GetConstantDesc(constant_table, "Constant unknow", &constant_desc, &nb);
258         ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
259         hr = ID3DXConstantTable_GetConstantDesc(constant_table, "Constant3", &constant_desc, &nb);
260         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
261         ok(!memcmp(&constant_desc, &constant_desc_save, sizeof(D3DXCONSTANT_DESC)), "Got different constant data\n");
262
263         ID3DXConstantTable_Release(constant_table);
264     }
265 }
266
267 START_TEST(shader)
268 {
269     test_get_shader_size();
270     test_get_shader_version();
271     test_find_shader_comment();
272     test_get_shader_constant_table_ex();
273 }