Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / d3dx9_36 / tests / asm.c
blobf6a0cb2d73d2b1dfaeb2e5ce77ce4c9d7d60cefc
1 /*
2 * Copyright (C) 2008 Stefan Dösinger
3 * Copyright (C) 2009 Matteo Bruni
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.
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.
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
19 #define COBJMACROS
20 #include "wine/test.h"
22 #include <d3dx9.h>
24 #include "resources.h"
26 static HRESULT create_file(const char *filename, const char *data, const unsigned int size)
28 DWORD received;
29 HANDLE hfile;
31 hfile = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
32 if(hfile == INVALID_HANDLE_VALUE) return HRESULT_FROM_WIN32(GetLastError());
34 if(WriteFile(hfile, data, size, &received, NULL))
36 CloseHandle(hfile);
37 return D3D_OK;
40 CloseHandle(hfile);
41 return D3DERR_INVALIDCALL;
44 static HRESULT WINAPI testD3DXInclude_open(ID3DXInclude *iface,
45 D3DXINCLUDE_TYPE include_type,
46 LPCSTR filename, LPCVOID parent_data,
47 LPCVOID *data, UINT *bytes) {
48 char *buffer;
49 const char include[] = "#define REGISTER r0\nvs.1.1\n";
50 const char include2[] = "#include \"incl3.vsh\"\n";
51 const char include3[] = "vs.1.1\n";
53 trace("filename = %s\n", filename);
54 trace("parent_data = (%p) -> %s\n", parent_data, (char *)parent_data);
56 if(!strcmp(filename,"incl.vsh")) {
57 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(include));
58 CopyMemory(buffer, include, sizeof(include));
59 *bytes = sizeof(include);
60 /* Also check for the correct parent_data content */
61 ok(parent_data == NULL, "wrong parent_data value\n");
63 else if(!strcmp(filename,"incl3.vsh")) {
64 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(include3));
65 CopyMemory(buffer, include3, sizeof(include3));
66 *bytes = sizeof(include3);
67 /* Also check for the correct parent_data content */
68 ok(parent_data != NULL && !strncmp(include2, parent_data, strlen(include2)), "wrong parent_data value\n");
70 else {
71 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(include2));
72 CopyMemory(buffer, include2, sizeof(include2));
73 *bytes = sizeof(include2);
75 *data = buffer;
76 return S_OK;
79 static HRESULT WINAPI testD3DXInclude_close(ID3DXInclude *iface, LPCVOID data) {
80 HeapFree(GetProcessHeap(), 0, (LPVOID)data);
81 return S_OK;
84 static const struct ID3DXIncludeVtbl D3DXInclude_Vtbl = {
85 testD3DXInclude_open,
86 testD3DXInclude_close
89 struct D3DXIncludeImpl {
90 const ID3DXIncludeVtbl *lpVtbl;
93 static void assembleshader_test(void) {
94 const char test1[] = {
95 "vs.1.1\n"
96 "mov DEF2, v0\n"
98 const char testincl[] = {
99 "#define REGISTER r0\n"
100 "vs.1.1\n"
102 const char testshader[] = {
103 "#include \"incl.vsh\"\n"
104 "mov REGISTER, v0\n"
106 const char testshader2[] = {
107 "#include \"incl2.vsh\"\n"
108 "mov REGISTER, v0\n"
110 const char testshader3[] = {
111 "#include \"include/incl3.vsh\"\n"
112 "mov REGISTER, v0\n"
114 const char testincl3[] = {
115 "#include \"incl4.vsh\"\n"
117 const char testincl4_ok[] = {
118 "#define REGISTER r0\n"
119 "vs.1.1\n"
121 const char testincl4_wrong[] = {
122 "#error \"wrong include\"\n"
124 HRESULT hr;
125 LPD3DXBUFFER shader, messages;
126 D3DXMACRO defines[] = {
128 "DEF1", "10 + 15"
131 "DEF2", "r0"
134 NULL, NULL
137 struct D3DXIncludeImpl include;
138 HRESULT shader_vsh_res;
140 /* pDefines test */
141 shader = NULL;
142 messages = NULL;
143 hr = D3DXAssembleShader(test1, strlen(test1),
144 defines, NULL, D3DXSHADER_SKIPVALIDATION,
145 &shader, &messages);
146 ok(hr == D3D_OK, "pDefines test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
147 if(messages) {
148 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
149 ID3DXBuffer_Release(messages);
151 if(shader) ID3DXBuffer_Release(shader);
153 /* NULL messages test */
154 shader = NULL;
155 hr = D3DXAssembleShader(test1, strlen(test1),
156 defines, NULL, D3DXSHADER_SKIPVALIDATION,
157 &shader, NULL);
158 ok(hr == D3D_OK, "NULL messages test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
159 if(shader) ID3DXBuffer_Release(shader);
161 /* NULL shader test */
162 messages = NULL;
163 hr = D3DXAssembleShader(test1, strlen(test1),
164 defines, NULL, D3DXSHADER_SKIPVALIDATION,
165 NULL, &messages);
166 ok(hr == D3D_OK, "NULL shader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
167 if(messages) {
168 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
169 ID3DXBuffer_Release(messages);
172 /* pInclude test */
173 shader = NULL;
174 messages = NULL;
175 include.lpVtbl = &D3DXInclude_Vtbl;
176 hr = D3DXAssembleShader(testshader, strlen(testshader),
177 NULL, (LPD3DXINCLUDE)&include, D3DXSHADER_SKIPVALIDATION,
178 &shader, &messages);
179 ok(hr == D3D_OK, "pInclude test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
180 if(messages) {
181 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
182 ID3DXBuffer_Release(messages);
184 if(shader) ID3DXBuffer_Release(shader);
186 /* "unexpected #include file from memory" test */
187 shader = NULL;
188 messages = NULL;
189 hr = D3DXAssembleShader(testshader, strlen(testshader),
190 NULL, NULL, D3DXSHADER_SKIPVALIDATION,
191 &shader, &messages);
192 ok(hr == D3DXERR_INVALIDDATA, "D3DXAssembleShader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
193 if(messages) {
194 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
195 ID3DXBuffer_Release(messages);
197 if(shader) ID3DXBuffer_Release(shader);
199 /* recursive #include test */
200 shader = NULL;
201 messages = NULL;
202 hr = D3DXAssembleShader(testshader2, strlen(testshader2),
203 NULL, (LPD3DXINCLUDE)&include, D3DXSHADER_SKIPVALIDATION,
204 &shader, &messages);
205 ok(hr == D3D_OK, "D3DXAssembleShader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
206 if(messages) {
207 trace("recursive D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
208 ID3DXBuffer_Release(messages);
210 if(shader) ID3DXBuffer_Release(shader);
212 shader_vsh_res = create_file("shader.vsh", testshader, sizeof(testshader) - 1);
213 if(SUCCEEDED(shader_vsh_res)) {
214 create_file("incl.vsh", testincl, sizeof(testincl) - 1);
216 /* D3DXAssembleShaderFromFile + #include test */
217 shader = NULL;
218 messages = NULL;
219 hr = D3DXAssembleShaderFromFileA("shader.vsh",
220 NULL, NULL, D3DXSHADER_SKIPVALIDATION,
221 &shader, &messages);
222 ok(hr == D3D_OK, "D3DXAssembleShaderFromFile test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
223 if(messages) {
224 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
225 ID3DXBuffer_Release(messages);
227 if(shader) ID3DXBuffer_Release(shader);
229 /* D3DXAssembleShaderFromFile + pInclude test */
230 shader = NULL;
231 messages = NULL;
232 hr = D3DXAssembleShaderFromFileA("shader.vsh",
233 NULL, (LPD3DXINCLUDE)&include, D3DXSHADER_SKIPVALIDATION,
234 &shader, &messages);
235 ok(hr == D3D_OK, "D3DXAssembleShaderFromFile + pInclude test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
236 if(messages) {
237 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
238 ID3DXBuffer_Release(messages);
240 if(shader) ID3DXBuffer_Release(shader);
242 create_file("shader3.vsh", testshader3, sizeof(testshader3) - 1);
243 create_file("incl4.vsh", testincl4_wrong, sizeof(testincl4_wrong) - 1);
244 if(CreateDirectoryA("include", NULL)) {
245 create_file("include/incl3.vsh", testincl3, sizeof(testincl3) - 1);
246 create_file("include/incl4.vsh", testincl4_ok, sizeof(testincl4_ok) - 1);
248 /* path search #include test */
249 shader = NULL;
250 messages = NULL;
251 hr = D3DXAssembleShaderFromFileA("shader3.vsh", NULL, NULL,
252 D3DXSHADER_SKIPVALIDATION,
253 &shader, &messages);
254 ok(hr == D3D_OK, "D3DXAssembleShaderFromFile path search test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
255 if(messages) {
256 trace("D3DXAssembleShaderFromFile path search messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
257 ID3DXBuffer_Release(messages);
259 if(shader) ID3DXBuffer_Release(shader);
260 } else skip("Couldn't create \"include\" directory\n");
261 } else skip("Couldn't create \"shader.vsh\"\n");
263 /* NULL shader tests */
264 shader = NULL;
265 messages = NULL;
266 hr = D3DXAssembleShader(NULL, 0,
267 NULL, NULL, D3DXSHADER_SKIPVALIDATION,
268 &shader, &messages);
269 ok(hr == D3DXERR_INVALIDDATA, "NULL shader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
270 if(messages) {
271 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
272 ID3DXBuffer_Release(messages);
274 if(shader) ID3DXBuffer_Release(shader);
276 shader = NULL;
277 messages = NULL;
278 hr = D3DXAssembleShaderFromFileA("nonexistent.vsh",
279 NULL, NULL, D3DXSHADER_SKIPVALIDATION,
280 &shader, &messages);
281 ok(hr == D3DXERR_INVALIDDATA || hr == E_FAIL, /* I get this on WinXP */
282 "D3DXAssembleShaderFromFile nonexistent file test failed with error 0x%x - %d\n",
283 hr, hr & 0x0000FFFF);
284 if(messages) {
285 trace("D3DXAssembleShaderFromFile messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
286 ID3DXBuffer_Release(messages);
288 if(shader) ID3DXBuffer_Release(shader);
290 /* D3DXAssembleShaderFromResource test */
291 shader = NULL;
292 messages = NULL;
293 hr = D3DXAssembleShaderFromResourceA(NULL, MAKEINTRESOURCEA(IDB_ASMSHADER),
294 NULL, NULL, D3DXSHADER_SKIPVALIDATION,
295 &shader, &messages);
296 ok(hr == D3D_OK, "D3DXAssembleShaderFromResource test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
297 if(messages) {
298 trace("D3DXAssembleShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
299 ID3DXBuffer_Release(messages);
301 if(shader) ID3DXBuffer_Release(shader);
303 /* D3DXAssembleShaderFromResource with missing shader resource test */
304 shader = NULL;
305 messages = NULL;
306 hr = D3DXAssembleShaderFromResourceA(NULL, "notexisting",
307 NULL, NULL, D3DXSHADER_SKIPVALIDATION,
308 &shader, &messages);
309 ok(hr == D3DXERR_INVALIDDATA, "D3DXAssembleShaderFromResource NULL shader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
310 if(messages) {
311 trace("D3DXAssembleShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
312 ID3DXBuffer_Release(messages);
314 if(shader) ID3DXBuffer_Release(shader);
316 /* cleanup */
317 if(SUCCEEDED(shader_vsh_res)) {
318 DeleteFileA("shader.vsh");
319 DeleteFileA("incl.vsh");
320 DeleteFileA("shader3.vsh");
321 DeleteFileA("incl4.vsh");
322 DeleteFileA("include/incl3.vsh");
323 DeleteFileA("include/incl4.vsh");
324 RemoveDirectoryA("include");
328 static void d3dxpreprocess_test(void) {
329 const char testincl[] = {
330 "#define REGISTER r0\n"
331 "vs.1.1\n"
333 const char testshader[] = {
334 "#include \"incl.vsh\"\n"
335 "mov REGISTER, v0\n"
337 const char testshader3[] = {
338 "#include \"include/incl3.vsh\"\n"
339 "mov REGISTER, v0\n"
341 const char testincl3[] = {
342 "#include \"incl4.vsh\"\n"
344 const char testincl4_ok[] = {
345 "#define REGISTER r0\n"
346 "vs.1.1\n"
348 const char testincl4_wrong[] = {
349 "#error \"wrong include\"\n"
351 HRESULT hr;
352 LPD3DXBUFFER shader, messages;
353 HRESULT shader_vsh_res;
354 struct D3DXIncludeImpl include = {&D3DXInclude_Vtbl};
356 shader_vsh_res = create_file("shader.vsh", testshader, sizeof(testshader) - 1);
357 if(SUCCEEDED(shader_vsh_res)) {
358 create_file("incl.vsh", testincl, sizeof(testincl) - 1);
359 create_file("shader3.vsh", testshader3, sizeof(testshader3) - 1);
360 create_file("incl4.vsh", testincl4_wrong, sizeof(testincl4_wrong) - 1);
361 if(CreateDirectoryA("include", NULL)) {
362 create_file("include/incl3.vsh", testincl3, sizeof(testincl3) - 1);
363 create_file("include/incl4.vsh", testincl4_ok, sizeof(testincl4_ok) - 1);
365 /* path search #include test */
366 shader = NULL;
367 messages = NULL;
368 hr = D3DXPreprocessShaderFromFileA("shader3.vsh", NULL, NULL,
369 &shader, &messages);
370 ok(hr == D3D_OK, "D3DXPreprocessShaderFromFile path search test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
371 if(messages) {
372 trace("D3DXPreprocessShaderFromFile path search messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
373 ID3DXBuffer_Release(messages);
375 if(shader) ID3DXBuffer_Release(shader);
376 } else skip("Couldn't create \"include\" directory\n");
378 /* D3DXPreprocessShaderFromFile + #include test */
379 shader = NULL;
380 messages = NULL;
381 hr = D3DXPreprocessShaderFromFileA("shader.vsh",
382 NULL, NULL,
383 &shader, &messages);
384 ok(hr == D3D_OK, "D3DXPreprocessShaderFromFile test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
385 if(messages) {
386 trace("D3DXPreprocessShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
387 ID3DXBuffer_Release(messages);
389 if(shader) ID3DXBuffer_Release(shader);
391 /* D3DXPreprocessShaderFromFile + pInclude test */
392 shader = NULL;
393 messages = NULL;
394 hr = D3DXPreprocessShaderFromFileA("shader.vsh",
395 NULL, (LPD3DXINCLUDE)&include,
396 &shader, &messages);
397 ok(hr == D3D_OK, "D3DXPreprocessShaderFromFile + pInclude test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
398 if(messages) {
399 trace("D3DXPreprocessShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
400 ID3DXBuffer_Release(messages);
402 if(shader) ID3DXBuffer_Release(shader);
403 } else skip("Couldn't create \"shader.vsh\"\n");
405 /* NULL shader tests */
406 shader = NULL;
407 messages = NULL;
408 hr = D3DXPreprocessShaderFromFileA("nonexistent.vsh",
409 NULL, NULL,
410 &shader, &messages);
411 ok(hr == D3DXERR_INVALIDDATA || hr == E_FAIL, /* I get this on WinXP */
412 "D3DXPreprocessShaderFromFile nonexistent file test failed with error 0x%x - %d\n",
413 hr, hr & 0x0000FFFF);
414 if(messages) {
415 trace("D3DXPreprocessShaderFromFile messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
416 ID3DXBuffer_Release(messages);
418 if(shader) ID3DXBuffer_Release(shader);
420 /* D3DXPreprocessShaderFromResource test */
421 shader = NULL;
422 messages = NULL;
423 hr = D3DXPreprocessShaderFromResourceA(NULL, MAKEINTRESOURCEA(IDB_ASMSHADER),
424 NULL, NULL,
425 &shader, &messages);
426 ok(hr == D3D_OK, "D3DXPreprocessShaderFromResource test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
427 if(messages) {
428 trace("D3DXPreprocessShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
429 ID3DXBuffer_Release(messages);
431 if(shader) ID3DXBuffer_Release(shader);
433 /* D3DXPreprocessShaderFromResource with missing shader resource test */
434 shader = NULL;
435 messages = NULL;
436 hr = D3DXPreprocessShaderFromResourceA(NULL, "notexisting",
437 NULL, NULL,
438 &shader, &messages);
439 ok(hr == D3DXERR_INVALIDDATA, "D3DXPreprocessShaderFromResource NULL shader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
440 if(messages) {
441 trace("D3DXPreprocessShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
442 ID3DXBuffer_Release(messages);
444 if(shader) ID3DXBuffer_Release(shader);
446 /* cleanup */
447 if(SUCCEEDED(shader_vsh_res)) {
448 DeleteFileA("shader.vsh");
449 DeleteFileA("incl.vsh");
450 DeleteFileA("shader3.vsh");
451 DeleteFileA("incl4.vsh");
452 DeleteFileA("include/incl3.vsh");
453 DeleteFileA("include/incl4.vsh");
454 RemoveDirectoryA("include");
458 START_TEST(asm)
460 assembleshader_test();
462 d3dxpreprocess_test();