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
21 #include "wine/test.h"
25 #include "resources.h"
27 static char temp_path
[MAX_PATH
];
29 static BOOL
create_file(const char *filename
, const char *data
, const unsigned int size
, char *out_path
)
36 GetTempPathA(sizeof(temp_path
), temp_path
);
38 strcpy(path
, temp_path
);
39 strcat(path
, filename
);
40 hfile
= CreateFileA(path
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
41 if (hfile
== INVALID_HANDLE_VALUE
)
44 if (WriteFile(hfile
, data
, size
, &written
, NULL
))
49 strcpy(out_path
, path
);
57 static void delete_file(const char *filename
)
61 strcpy(path
, temp_path
);
62 strcat(path
, filename
);
66 static BOOL
create_directory(const char *name
)
70 strcpy(path
, temp_path
);
72 return CreateDirectoryA(path
, NULL
);
75 static void delete_directory(const char *name
)
79 strcpy(path
, temp_path
);
81 RemoveDirectoryA(path
);
84 static HRESULT WINAPI
testD3DXInclude_open(ID3DXInclude
*iface
, D3DXINCLUDE_TYPE include_type
,
85 const char *filename
, const void *parent_data
, const void **data
, UINT
*bytes
)
88 static const char shader
[] =
89 "#include \"incl.vsh\"\n"
91 static const char include
[] = "#define REGISTER r0\nvs.1.1\n";
92 static const char include2
[] = "#include \"incl3.vsh\"\n";
93 static const char include3
[] = "vs.1.1\n";
95 trace("filename %s.\n", filename
);
96 trace("parent_data %p: %s.\n", parent_data
, parent_data
? (char *)parent_data
: "(null)");
98 if (!strcmp(filename
, "shader.vsh"))
100 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(shader
));
101 memcpy(buffer
, shader
, sizeof(shader
));
102 *bytes
= sizeof(shader
);
104 else if (!strcmp(filename
, "incl.vsh"))
106 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(include
));
107 memcpy(buffer
, include
, sizeof(include
));
108 *bytes
= sizeof(include
);
109 /* This is included from the first D3DXAssembleShader with non-null ID3DXInclude test
110 * (parent_data == NULL) and from shader.vsh / shader[] (with matching parent_data).
111 * Allow both cases. */
112 ok(parent_data
== NULL
|| !strncmp(shader
, parent_data
, strlen(shader
)), "wrong parent_data value\n");
114 else if (!strcmp(filename
, "incl2.vsh"))
116 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(include2
));
117 memcpy(buffer
, include2
, sizeof(include2
));
118 *bytes
= sizeof(include2
);
120 else if (!strcmp(filename
, "incl3.vsh"))
122 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(include3
));
123 memcpy(buffer
, include3
, sizeof(include3
));
124 *bytes
= sizeof(include3
);
125 /* Also check for the correct parent_data content */
126 ok(parent_data
!= NULL
&& !strncmp(include2
, parent_data
, strlen(include2
)), "wrong parent_data value\n");
128 else if (!strcmp(filename
, "include/incl3.vsh"))
130 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(include
));
131 memcpy(buffer
, include
, sizeof(include
));
132 *bytes
= sizeof(include
);
133 ok(!parent_data
, "wrong parent_data value\n");
137 ok(0, "Unexpected #include for file %s.\n", filename
);
138 return D3DERR_INVALIDCALL
;
144 static HRESULT WINAPI
testD3DXInclude_close(ID3DXInclude
*iface
, const void *data
)
146 HeapFree(GetProcessHeap(), 0, (void *)data
);
150 static const struct ID3DXIncludeVtbl D3DXInclude_Vtbl
= {
151 testD3DXInclude_open
,
152 testD3DXInclude_close
155 struct D3DXIncludeImpl
{
156 ID3DXInclude ID3DXInclude_iface
;
159 static void assembleshader_test(void)
161 static const char test1
[] =
164 static const char testincl
[] =
165 "#define REGISTER r0\n"
167 static const char testshader
[] =
168 "#include \"incl.vsh\"\n"
169 "mov REGISTER, v0\n";
170 static const char testshader2
[] =
171 "#include \"incl2.vsh\"\n"
172 "mov REGISTER, v0\n";
173 static const char testshader3
[] =
174 "#include \"include/incl3.vsh\"\n"
175 "mov REGISTER, v0\n";
176 static const char testincl3
[] =
177 "#include \"incl4.vsh\"\n";
178 static const char testincl4_ok
[] =
179 "#define REGISTER r0\n"
181 static const char testincl4_wrong
[] =
182 "#error \"wrong include\"\n";
184 ID3DXBuffer
*shader
, *messages
;
185 static const D3DXMACRO defines
[] = {
196 struct D3DXIncludeImpl include
;
197 char shader_vsh_path
[MAX_PATH
], shader3_vsh_path
[MAX_PATH
];
202 hr
= D3DXAssembleShader(test1
, strlen(test1
),
203 defines
, NULL
, D3DXSHADER_SKIPVALIDATION
,
205 ok(hr
== D3D_OK
, "pDefines test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
207 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
208 ID3DXBuffer_Release(messages
);
210 if(shader
) ID3DXBuffer_Release(shader
);
212 /* NULL messages test */
214 hr
= D3DXAssembleShader(test1
, strlen(test1
),
215 defines
, NULL
, D3DXSHADER_SKIPVALIDATION
,
217 ok(hr
== D3D_OK
, "NULL messages test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
218 if(shader
) ID3DXBuffer_Release(shader
);
220 /* NULL shader test */
222 hr
= D3DXAssembleShader(test1
, strlen(test1
),
223 defines
, NULL
, D3DXSHADER_SKIPVALIDATION
,
225 ok(hr
== D3D_OK
, "NULL shader test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
227 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
228 ID3DXBuffer_Release(messages
);
234 include
.ID3DXInclude_iface
.lpVtbl
= &D3DXInclude_Vtbl
;
235 hr
= D3DXAssembleShader(testshader
, strlen(testshader
), NULL
, &include
.ID3DXInclude_iface
,
236 D3DXSHADER_SKIPVALIDATION
, &shader
, &messages
);
237 ok(hr
== D3D_OK
, "pInclude test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
239 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
240 ID3DXBuffer_Release(messages
);
242 if(shader
) ID3DXBuffer_Release(shader
);
244 /* "unexpected #include file from memory" test */
247 hr
= D3DXAssembleShader(testshader
, strlen(testshader
),
248 NULL
, NULL
, D3DXSHADER_SKIPVALIDATION
,
250 ok(hr
== D3DXERR_INVALIDDATA
, "D3DXAssembleShader test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
252 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
253 ID3DXBuffer_Release(messages
);
255 if(shader
) ID3DXBuffer_Release(shader
);
257 /* recursive #include test */
260 hr
= D3DXAssembleShader(testshader2
, strlen(testshader2
), NULL
, &include
.ID3DXInclude_iface
,
261 D3DXSHADER_SKIPVALIDATION
, &shader
, &messages
);
262 ok(hr
== D3D_OK
, "D3DXAssembleShader test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
264 trace("recursive D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
265 ID3DXBuffer_Release(messages
);
267 if(shader
) ID3DXBuffer_Release(shader
);
269 /* #include with a path. */
272 hr
= D3DXAssembleShader(testshader3
, strlen(testshader3
), NULL
, &include
.ID3DXInclude_iface
,
273 D3DXSHADER_SKIPVALIDATION
, &shader
, &messages
);
274 ok(hr
== D3D_OK
, "D3DXAssembleShader test failed with error 0x%x - %d\n", hr
, hr
& 0x0000ffff);
277 trace("Path search D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
278 ID3DXBuffer_Release(messages
);
281 ID3DXBuffer_Release(shader
);
283 if (create_file("shader.vsh", testshader
, sizeof(testshader
) - 1, shader_vsh_path
))
285 create_file("incl.vsh", testincl
, sizeof(testincl
) - 1, NULL
);
287 /* D3DXAssembleShaderFromFile + #include test */
290 hr
= D3DXAssembleShaderFromFileA(shader_vsh_path
,
291 NULL
, NULL
, D3DXSHADER_SKIPVALIDATION
,
293 ok(hr
== D3D_OK
, "D3DXAssembleShaderFromFile test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
295 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
296 ID3DXBuffer_Release(messages
);
298 if(shader
) ID3DXBuffer_Release(shader
);
300 /* D3DXAssembleShaderFromFile + pInclude test */
303 hr
= D3DXAssembleShaderFromFileA("shader.vsh", NULL
, &include
.ID3DXInclude_iface
,
304 D3DXSHADER_SKIPVALIDATION
, &shader
, &messages
);
305 ok(hr
== D3D_OK
, "D3DXAssembleShaderFromFile + pInclude test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
307 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
308 ID3DXBuffer_Release(messages
);
310 if(shader
) ID3DXBuffer_Release(shader
);
312 create_file("shader3.vsh", testshader3
, sizeof(testshader3
) - 1, shader3_vsh_path
);
313 create_file("incl4.vsh", testincl4_wrong
, sizeof(testincl4_wrong
) - 1, NULL
);
314 if (create_directory("include"))
316 create_file("include\\incl3.vsh", testincl3
, sizeof(testincl3
) - 1, NULL
);
317 create_file("include\\incl4.vsh", testincl4_ok
, sizeof(testincl4_ok
) - 1, NULL
);
319 /* path search #include test */
322 hr
= D3DXAssembleShaderFromFileA(shader3_vsh_path
, NULL
, NULL
,
323 D3DXSHADER_SKIPVALIDATION
,
325 ok(hr
== D3D_OK
, "D3DXAssembleShaderFromFile path search test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
327 trace("D3DXAssembleShaderFromFile path search messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
328 ID3DXBuffer_Release(messages
);
330 if(shader
) ID3DXBuffer_Release(shader
);
331 } else skip("Couldn't create \"include\" directory\n");
333 delete_file("shader.vsh");
334 delete_file("incl.vsh");
335 delete_file("shader3.vsh");
336 delete_file("incl4.vsh");
337 delete_file("include\\incl3.vsh");
338 delete_file("include\\incl4.vsh");
339 delete_directory("include");
341 /* The main shader is also to be loaded through the ID3DXInclude object. */
344 hr
= D3DXAssembleShaderFromFileA("shader.vsh", NULL
, &include
.ID3DXInclude_iface
,
345 D3DXSHADER_SKIPVALIDATION
, &shader
, &messages
);
346 ok(hr
== D3D_OK
, "D3DXAssembleShaderFromFile + pInclude main shader test failed with error 0x%x - %d\n",
347 hr
, hr
& 0x0000ffff);
350 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
351 ID3DXBuffer_Release(messages
);
354 ID3DXBuffer_Release(shader
);
358 hr
= D3DXAssembleShaderFromFileW(L
"shader.vsh", NULL
, &include
.ID3DXInclude_iface
,
359 D3DXSHADER_SKIPVALIDATION
, &shader
, &messages
);
360 ok(hr
== D3D_OK
, "D3DXAssembleShaderFromFile + pInclude main shader test failed with error 0x%x - %d\n",
361 hr
, hr
& 0x0000ffff);
364 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
365 ID3DXBuffer_Release(messages
);
368 ID3DXBuffer_Release(shader
);
369 } else skip("Couldn't create \"shader.vsh\"\n");
371 /* NULL shader tests */
374 hr
= D3DXAssembleShader(NULL
, 0,
375 NULL
, NULL
, D3DXSHADER_SKIPVALIDATION
,
377 ok(hr
== D3DXERR_INVALIDDATA
, "NULL shader test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
379 trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
380 ID3DXBuffer_Release(messages
);
382 if(shader
) ID3DXBuffer_Release(shader
);
386 hr
= D3DXAssembleShaderFromFileA("nonexistent.vsh",
387 NULL
, NULL
, D3DXSHADER_SKIPVALIDATION
,
389 ok(hr
== D3DXERR_INVALIDDATA
|| hr
== E_FAIL
, /* I get this on WinXP */
390 "D3DXAssembleShaderFromFile nonexistent file test failed with error 0x%x - %d\n",
391 hr
, hr
& 0x0000FFFF);
393 trace("D3DXAssembleShaderFromFile messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
394 ID3DXBuffer_Release(messages
);
396 if(shader
) ID3DXBuffer_Release(shader
);
398 /* D3DXAssembleShaderFromResource test */
401 hr
= D3DXAssembleShaderFromResourceA(NULL
, MAKEINTRESOURCEA(IDB_ASMSHADER
),
402 NULL
, NULL
, D3DXSHADER_SKIPVALIDATION
,
404 ok(hr
== D3D_OK
, "D3DXAssembleShaderFromResource test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
406 trace("D3DXAssembleShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
407 ID3DXBuffer_Release(messages
);
409 if(shader
) ID3DXBuffer_Release(shader
);
411 /* D3DXAssembleShaderFromResource with missing shader resource test */
414 hr
= D3DXAssembleShaderFromResourceA(NULL
, "notexisting",
415 NULL
, NULL
, D3DXSHADER_SKIPVALIDATION
,
417 ok(hr
== D3DXERR_INVALIDDATA
, "D3DXAssembleShaderFromResource NULL shader test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
419 trace("D3DXAssembleShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
420 ID3DXBuffer_Release(messages
);
422 if(shader
) ID3DXBuffer_Release(shader
);
425 static void d3dxpreprocess_test(void)
427 static const char testincl
[] =
428 "#define REGISTER r0\n"
430 static const char testshader
[] =
431 "#include \"incl.vsh\"\n"
432 "mov REGISTER, v0\n";
433 static const char testshader3
[] =
434 "#include \"include/incl3.vsh\"\n"
435 "mov REGISTER, v0\n";
436 static const char testincl3
[] =
437 "#include \"incl4.vsh\"\n";
438 static const char testincl4_ok
[] =
439 "#define REGISTER r0\n"
441 static const char testincl4_wrong
[] =
442 "#error \"wrong include\"\n";
444 ID3DXBuffer
*shader
, *messages
;
445 char shader_vsh_path
[MAX_PATH
], shader3_vsh_path
[MAX_PATH
];
446 static struct D3DXIncludeImpl include
= {{&D3DXInclude_Vtbl
}};
448 if (create_file("shader.vsh", testshader
, sizeof(testshader
) - 1, shader_vsh_path
))
450 create_file("incl.vsh", testincl
, sizeof(testincl
) - 1, NULL
);
451 create_file("shader3.vsh", testshader3
, sizeof(testshader3
) - 1, shader3_vsh_path
);
452 create_file("incl4.vsh", testincl4_wrong
, sizeof(testincl4_wrong
) - 1, NULL
);
453 if (create_directory("include"))
455 create_file("include\\incl3.vsh", testincl3
, sizeof(testincl3
) - 1, NULL
);
456 create_file("include\\incl4.vsh", testincl4_ok
, sizeof(testincl4_ok
) - 1, NULL
);
458 /* path search #include test */
461 hr
= D3DXPreprocessShaderFromFileA(shader3_vsh_path
, NULL
, NULL
,
463 ok(hr
== D3D_OK
, "D3DXPreprocessShaderFromFile path search test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
465 trace("D3DXPreprocessShaderFromFile path search messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
466 ID3DXBuffer_Release(messages
);
468 if(shader
) ID3DXBuffer_Release(shader
);
469 } else skip("Couldn't create \"include\" directory\n");
471 /* D3DXPreprocessShaderFromFile + #include test */
474 hr
= D3DXPreprocessShaderFromFileA(shader_vsh_path
,
477 ok(hr
== D3D_OK
, "D3DXPreprocessShaderFromFile test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
479 trace("D3DXPreprocessShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
480 ID3DXBuffer_Release(messages
);
482 if(shader
) ID3DXBuffer_Release(shader
);
484 /* D3DXPreprocessShaderFromFile + pInclude test */
487 hr
= D3DXPreprocessShaderFromFileA("shader.vsh", NULL
, &include
.ID3DXInclude_iface
,
489 ok(hr
== D3D_OK
, "D3DXPreprocessShaderFromFile + pInclude test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
491 trace("D3DXPreprocessShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
492 ID3DXBuffer_Release(messages
);
494 if(shader
) ID3DXBuffer_Release(shader
);
496 delete_file("shader.vsh");
497 delete_file("incl.vsh");
498 delete_file("shader3.vsh");
499 delete_file("incl4.vsh");
500 delete_file("include\\incl3.vsh");
501 delete_file("include\\incl4.vsh");
502 delete_directory("include");
504 /* The main shader is also to be loaded through the ID3DXInclude object. */
507 hr
= D3DXPreprocessShaderFromFileA("shader.vsh", NULL
, &include
.ID3DXInclude_iface
,
509 ok(hr
== D3D_OK
, "D3DXPreprocessShaderFromFile + pInclude main shader test failed with error 0x%x - %d\n",
510 hr
, hr
& 0x0000ffff);
513 trace("D3DXPreprocessShaderFromFile messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
514 ID3DXBuffer_Release(messages
);
517 ID3DXBuffer_Release(shader
);
521 hr
= D3DXPreprocessShaderFromFileW(L
"shader.vsh", NULL
, &include
.ID3DXInclude_iface
,
523 ok(hr
== D3D_OK
, "D3DXPreprocessShaderFromFile + pInclude main shader test failed with error 0x%x - %d\n",
524 hr
, hr
& 0x0000ffff);
527 trace("D3DXPreprocessShaderFromFile messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
528 ID3DXBuffer_Release(messages
);
531 ID3DXBuffer_Release(shader
);
532 } else skip("Couldn't create \"shader.vsh\"\n");
534 /* NULL shader tests */
537 hr
= D3DXPreprocessShaderFromFileA("nonexistent.vsh",
540 ok(hr
== D3DXERR_INVALIDDATA
|| hr
== E_FAIL
, /* I get this on WinXP */
541 "D3DXPreprocessShaderFromFile nonexistent file test failed with error 0x%x - %d\n",
542 hr
, hr
& 0x0000FFFF);
544 trace("D3DXPreprocessShaderFromFile messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
545 ID3DXBuffer_Release(messages
);
547 if(shader
) ID3DXBuffer_Release(shader
);
549 /* D3DXPreprocessShaderFromResource test */
552 hr
= D3DXPreprocessShaderFromResourceA(NULL
, MAKEINTRESOURCEA(IDB_ASMSHADER
),
555 ok(hr
== D3D_OK
, "D3DXPreprocessShaderFromResource test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
557 trace("D3DXPreprocessShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
558 ID3DXBuffer_Release(messages
);
560 if(shader
) ID3DXBuffer_Release(shader
);
562 /* D3DXPreprocessShaderFromResource with missing shader resource test */
565 hr
= D3DXPreprocessShaderFromResourceA(NULL
, "notexisting",
568 ok(hr
== D3DXERR_INVALIDDATA
, "D3DXPreprocessShaderFromResource NULL shader test failed with error 0x%x - %d\n", hr
, hr
& 0x0000FFFF);
570 trace("D3DXPreprocessShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages
));
571 ID3DXBuffer_Release(messages
);
573 if(shader
) ID3DXBuffer_Release(shader
);
578 assembleshader_test();
580 d3dxpreprocess_test();