2 * Copyright 2009 Matteo Bruni
3 * Copyright 2010 Matteo Bruni for CodeWeavers
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
22 #include "wine/port.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
26 #include "d3dcompiler_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
31 #define D3DXERR_INVALIDDATA 0x88760b59
33 #define BUFFER_INITIAL_CAPACITY 256
42 static struct mem_file_desc current_shader;
43 static ID3DInclude *current_include;
44 static const char *initial_filename;
46 #define INCLUDES_INITIAL_CAPACITY 4
54 static struct loaded_include *includes;
55 static int includes_capacity, includes_size;
56 static const char *parent_include;
58 static char *wpp_output;
59 static int wpp_output_capacity, wpp_output_size;
61 static char *wpp_messages;
62 static int wpp_messages_capacity, wpp_messages_size;
64 /* Mutex used to guarantee a single invocation
65 of the D3DXAssembleShader function (or its variants) at a time.
66 This is needed as wpp isn't thread-safe */
67 static CRITICAL_SECTION wpp_mutex;
68 static CRITICAL_SECTION_DEBUG wpp_mutex_debug =
71 { &wpp_mutex_debug.ProcessLocksList,
72 &wpp_mutex_debug.ProcessLocksList },
73 0, 0, { (DWORD_PTR)(__FILE__ ": wpp_mutex") }
75 static CRITICAL_SECTION wpp_mutex = { &wpp_mutex_debug, -1, 0, 0, 0, 0 };
77 /* Preprocessor error reporting functions */
78 static void wpp_write_message(const char *fmt, va_list args)
83 if(wpp_messages_capacity == 0)
85 wpp_messages = HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE);
86 if(wpp_messages == NULL)
89 wpp_messages_capacity = MESSAGEBUFFER_INITIAL_SIZE;
94 rc = vsnprintf(wpp_messages + wpp_messages_size,
95 wpp_messages_capacity - wpp_messages_size, fmt, args);
97 if (rc < 0 || /* C89 */
98 rc >= wpp_messages_capacity - wpp_messages_size) { /* C99 */
99 /* Resize the buffer */
100 newsize = wpp_messages_capacity * 2;
101 newbuffer = HeapReAlloc(GetProcessHeap(), 0, wpp_messages, newsize);
102 if(newbuffer == NULL)
104 ERR("Error reallocating memory for parser messages\n");
107 wpp_messages = newbuffer;
108 wpp_messages_capacity = newsize;
112 wpp_messages_size += rc;
118 static void PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt, ...)
123 wpp_write_message(fmt, args);
127 static void wpp_error(const char *file, int line, int col, const char *near,
128 const char *msg, va_list ap)
130 wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
132 wpp_write_message(msg, ap);
133 wpp_write_message_var("\n");
136 static void wpp_warning(const char *file, int line, int col, const char *near,
137 const char *msg, va_list ap)
139 wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
140 line, col, "Warning");
141 wpp_write_message(msg, ap);
142 wpp_write_message_var("\n");
145 static char *wpp_lookup_mem(const char *filename, int type, const char *parent_name,
146 char **include_path, int include_path_count)
148 /* Here we return always ok. We will maybe fail on the next wpp_open_mem */
152 parent_include = NULL;
153 if(parent_name[0] != '\0')
155 for(i = 0; i < includes_size; i++)
157 if(!strcmp(parent_name, includes[i].name))
159 parent_include = includes[i].data;
163 if(parent_include == NULL)
165 ERR("Parent include file missing\n");
170 path = malloc(strlen(filename) + 1);
172 memcpy(path, filename, strlen(filename) + 1);
176 static void *wpp_open_mem(const char *filename, int type)
178 struct mem_file_desc *desc;
181 if(!strcmp(filename, initial_filename))
183 current_shader.pos = 0;
184 return ¤t_shader;
187 if(current_include == NULL) return NULL;
188 desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
192 hr = ID3DInclude_Open(current_include,
193 type ? D3D_INCLUDE_LOCAL : D3D_INCLUDE_SYSTEM,
194 filename, parent_include, (LPCVOID *)&desc->buffer,
198 HeapFree(GetProcessHeap(), 0, desc);
202 if(includes_capacity == includes_size)
204 if(includes_capacity == 0)
206 includes = HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY * sizeof(*includes));
209 ERR("Error allocating memory for the loaded includes structure\n");
212 includes_capacity = INCLUDES_INITIAL_CAPACITY * sizeof(*includes);
216 int newcapacity = includes_capacity * 2;
217 struct loaded_include *newincludes =
218 HeapReAlloc(GetProcessHeap(), 0, includes, newcapacity);
219 if(newincludes == NULL)
221 ERR("Error reallocating memory for the loaded includes structure\n");
224 includes = newincludes;
225 includes_capacity = newcapacity;
228 includes[includes_size].name = filename;
229 includes[includes_size++].data = desc->buffer;
235 ID3DInclude_Close(current_include, desc->buffer);
236 HeapFree(GetProcessHeap(), 0, desc);
240 static void wpp_close_mem(void *file)
242 struct mem_file_desc *desc = file;
244 if(desc != ¤t_shader)
247 ID3DInclude_Close(current_include, desc->buffer);
249 ERR("current_include == NULL, desc == %p, buffer = %s\n",
252 HeapFree(GetProcessHeap(), 0, desc);
256 static int wpp_read_mem(void *file, char *buffer, unsigned int len)
258 struct mem_file_desc *desc = file;
260 len = min(len, desc->size - desc->pos);
261 memcpy(buffer, desc->buffer + desc->pos, len);
266 static void wpp_write_mem(const char *buffer, unsigned int len)
268 char *new_wpp_output;
270 if(wpp_output_capacity == 0)
272 wpp_output = HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY);
276 wpp_output_capacity = BUFFER_INITIAL_CAPACITY;
278 if(len > wpp_output_capacity - wpp_output_size)
280 while(len > wpp_output_capacity - wpp_output_size)
282 wpp_output_capacity *= 2;
284 new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
285 wpp_output_capacity);
288 ERR("Error allocating memory\n");
291 wpp_output = new_wpp_output;
293 memcpy(wpp_output + wpp_output_size, buffer, len);
294 wpp_output_size += len;
297 static int wpp_close_output(void)
299 char *new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
300 wpp_output_size + 1);
301 if(!new_wpp_output) return 0;
302 wpp_output = new_wpp_output;
303 wpp_output[wpp_output_size]='\0';
308 static HRESULT preprocess_shader(const void *data, SIZE_T data_size, const char *filename,
309 const D3D_SHADER_MACRO *defines, ID3DInclude *include, ID3DBlob **error_messages)
313 const D3D_SHADER_MACRO *def = defines;
315 static const struct wpp_callbacks wpp_callbacks =
328 while (def->Name != NULL)
330 wpp_add_define(def->Name, def->Definition);
334 current_include = include;
337 wpp_output_size = wpp_output_capacity = 0;
340 wpp_set_callbacks(&wpp_callbacks);
341 wpp_messages_size = wpp_messages_capacity = 0;
343 current_shader.buffer = data;
344 current_shader.size = data_size;
345 initial_filename = filename ? filename : "";
347 ret = wpp_parse(initial_filename, NULL);
348 if (!wpp_close_output())
352 TRACE("Error during shader preprocessing\n");
358 TRACE("Preprocessor messages:\n%s\n", debugstr_a(wpp_messages));
362 size = strlen(wpp_messages) + 1;
363 hr = D3DCreateBlob(size, &buffer);
366 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_messages, size);
367 *error_messages = buffer;
371 TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
376 /* Remove the previously added defines */
379 while (defines->Name != NULL)
381 wpp_del_define(defines->Name);
385 HeapFree(GetProcessHeap(), 0, wpp_messages);
389 static HRESULT assemble_shader(const char *preproc_shader,
390 ID3DBlob **shader_blob, ID3DBlob **error_messages)
392 struct bwriter_shader *shader;
393 char *messages = NULL;
399 shader = SlAssembleShader(preproc_shader, &messages);
403 TRACE("Assembler messages:\n");
404 TRACE("%s\n", debugstr_a(messages));
406 TRACE("Shader source:\n");
407 TRACE("%s\n", debugstr_a(preproc_shader));
411 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
413 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
414 hr = D3DCreateBlob(size, &buffer);
417 HeapFree(GetProcessHeap(), 0, messages);
418 if (shader) SlDeleteShader(shader);
421 pos = ID3D10Blob_GetBufferPointer(buffer);
422 if (preproc_messages)
424 CopyMemory(pos, preproc_messages, strlen(preproc_messages) + 1);
425 pos += strlen(preproc_messages);
427 CopyMemory(pos, messages, strlen(messages) + 1);
429 if (*error_messages) ID3D10Blob_Release(*error_messages);
430 *error_messages = buffer;
432 HeapFree(GetProcessHeap(), 0, messages);
437 ERR("Asm reading failed\n");
438 return D3DXERR_INVALIDDATA;
441 hr = SlWriteBytecode(shader, 9, &res, &size);
442 SlDeleteShader(shader);
445 ERR("SlWriteBytecode failed with 0x%08x\n", hr);
446 return D3DXERR_INVALIDDATA;
451 hr = D3DCreateBlob(size, &buffer);
454 HeapFree(GetProcessHeap(), 0, res);
457 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), res, size);
458 *shader_blob = buffer;
461 HeapFree(GetProcessHeap(), 0, res);
466 HRESULT WINAPI D3DAssemble(const void *data, SIZE_T datasize, const char *filename,
467 const D3D_SHADER_MACRO *defines, ID3DInclude *include, UINT flags,
468 ID3DBlob **shader, ID3DBlob **error_messages)
472 TRACE("data %p, datasize %lu, filename %s, defines %p, include %p, sflags %#x,\n"
473 "shader %p, error_messages %p\n",
474 data, datasize, debugstr_a(filename), defines, include, flags, shader, error_messages);
476 EnterCriticalSection(&wpp_mutex);
479 if (flags) FIXME("flags %x\n", flags);
481 if (shader) *shader = NULL;
482 if (error_messages) *error_messages = NULL;
484 hr = preprocess_shader(data, datasize, filename, defines, include, error_messages);
486 hr = assemble_shader(wpp_output, shader, error_messages);
488 HeapFree(GetProcessHeap(), 0, wpp_output);
489 LeaveCriticalSection(&wpp_mutex);
493 static HRESULT compile_shader(const char *preproc_shader, const char *target, const char *entrypoint,
494 ID3DBlob **shader_blob, ID3DBlob **error_messages)
496 struct bwriter_shader *shader;
497 char *messages = NULL;
499 DWORD *res, size, major, minor;
502 enum shader_type shader_type;
504 TRACE("Preprocessed shader source: %s\n", debugstr_a(preproc_shader));
506 TRACE("Parsing compilation target %s.\n", debugstr_a(target));
507 if (strlen(target) != 6 || target[1] != 's' || target[2] != '_' || target[4] != '_')
509 FIXME("Unknown compilation target %s.\n", debugstr_a(target));
510 return D3DERR_INVALIDCALL;
513 if (target[0] == 'v')
514 shader_type = ST_VERTEX;
515 else if (target[0] == 'p')
516 shader_type = ST_PIXEL;
519 FIXME("Unsupported shader target type %s.\n", debugstr_a(target));
520 return D3DERR_INVALIDCALL;
523 major = target[3] - '0';
524 if (major == 0 || major > 5)
526 FIXME("Unsupported shader target major version %d.\n", major);
527 return D3DERR_INVALIDCALL;
529 minor = target[5] - '0';
530 if (minor > 1 || (minor == 1 && (shader_type != ST_VERTEX || major > 1)))
532 FIXME("Unsupported shader target minor version %d.\n", minor);
533 return D3DERR_INVALIDCALL;
535 shader = parse_hlsl_shader(preproc_shader, shader_type, major, minor, entrypoint, &messages);
539 TRACE("Compiler messages:\n");
540 TRACE("%s\n", debugstr_a(messages));
542 TRACE("Shader source:\n");
543 TRACE("%s\n", debugstr_a(preproc_shader));
547 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
549 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
550 hr = D3DCreateBlob(size, &buffer);
553 HeapFree(GetProcessHeap(), 0, messages);
554 if (shader) SlDeleteShader(shader);
557 pos = ID3D10Blob_GetBufferPointer(buffer);
558 if (preproc_messages)
560 memcpy(pos, preproc_messages, strlen(preproc_messages) + 1);
561 pos += strlen(preproc_messages);
563 memcpy(pos, messages, strlen(messages) + 1);
565 if (*error_messages) ID3D10Blob_Release(*error_messages);
566 *error_messages = buffer;
568 HeapFree(GetProcessHeap(), 0, messages);
573 ERR("HLSL shader parsing failed.\n");
574 return D3DXERR_INVALIDDATA;
577 hr = SlWriteBytecode(shader, 9, &res, &size);
578 SlDeleteShader(shader);
581 ERR("SlWriteBytecode failed with error 0x%08x.\n", hr);
582 return D3DXERR_INVALIDDATA;
587 hr = D3DCreateBlob(size, &buffer);
590 HeapFree(GetProcessHeap(), 0, res);
593 memcpy(ID3D10Blob_GetBufferPointer(buffer), res, size);
594 *shader_blob = buffer;
597 HeapFree(GetProcessHeap(), 0, res);
602 HRESULT WINAPI D3DCompile(const void *data, SIZE_T data_size, const char *filename,
603 const D3D_SHADER_MACRO *defines, ID3DInclude *include, const char *entrypoint,
604 const char *target, UINT sflags, UINT eflags, ID3DBlob **shader, ID3DBlob **error_messages)
608 TRACE("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s,\n"
609 "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p\n",
610 data, data_size, debugstr_a(filename), defines, include, debugstr_a(entrypoint),
611 debugstr_a(target), sflags, eflags, shader, error_messages);
613 if (shader) *shader = NULL;
614 if (error_messages) *error_messages = NULL;
616 EnterCriticalSection(&wpp_mutex);
618 hr = preprocess_shader(data, data_size, filename, defines, include, error_messages);
620 hr = compile_shader(wpp_output, target, entrypoint, shader, error_messages);
622 HeapFree(GetProcessHeap(), 0, wpp_output);
623 LeaveCriticalSection(&wpp_mutex);
627 HRESULT WINAPI D3DPreprocess(const void *data, SIZE_T size, const char *filename,
628 const D3D_SHADER_MACRO *defines, ID3DInclude *include,
629 ID3DBlob **shader, ID3DBlob **error_messages)
637 EnterCriticalSection(&wpp_mutex);
639 if (shader) *shader = NULL;
640 if (error_messages) *error_messages = NULL;
642 hr = preprocess_shader(data, size, filename, defines, include, error_messages);
648 hr = D3DCreateBlob(wpp_output_size, &buffer);
651 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_output, wpp_output_size);
659 HeapFree(GetProcessHeap(), 0, wpp_output);
660 LeaveCriticalSection(&wpp_mutex);
664 HRESULT WINAPI D3DDisassemble(const void *data, SIZE_T size, UINT flags, const char *comments, ID3DBlob **disassembly)
666 FIXME("data %p, size %lu, flags %#x, comments %p, disassembly %p stub!\n",
667 data, size, flags, comments, disassembly);