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;
45 #define INCLUDES_INITIAL_CAPACITY 4
53 static struct loaded_include *includes;
54 static int includes_capacity, includes_size;
55 static const char *parent_include;
57 static char *wpp_output;
58 static int wpp_output_capacity, wpp_output_size;
60 static char *wpp_messages;
61 static int wpp_messages_capacity, wpp_messages_size;
63 /* Mutex used to guarantee a single invocation
64 of the D3DXAssembleShader function (or its variants) at a time.
65 This is needed as wpp isn't thread-safe */
66 static CRITICAL_SECTION wpp_mutex;
67 static CRITICAL_SECTION_DEBUG wpp_mutex_debug =
70 { &wpp_mutex_debug.ProcessLocksList,
71 &wpp_mutex_debug.ProcessLocksList },
72 0, 0, { (DWORD_PTR)(__FILE__ ": wpp_mutex") }
74 static CRITICAL_SECTION wpp_mutex = { &wpp_mutex_debug, -1, 0, 0, 0, 0 };
76 /* Preprocessor error reporting functions */
77 static void wpp_write_message(const char *fmt, va_list args)
82 if(wpp_messages_capacity == 0)
84 wpp_messages = HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE);
85 if(wpp_messages == NULL)
87 ERR("Error allocating memory for parser messages\n");
90 wpp_messages_capacity = MESSAGEBUFFER_INITIAL_SIZE;
95 rc = vsnprintf(wpp_messages + wpp_messages_size,
96 wpp_messages_capacity - wpp_messages_size, fmt, args);
98 if (rc < 0 || /* C89 */
99 rc >= wpp_messages_capacity - wpp_messages_size) { /* C99 */
100 /* Resize the buffer */
101 newsize = wpp_messages_capacity * 2;
102 newbuffer = HeapReAlloc(GetProcessHeap(), 0, wpp_messages, newsize);
103 if(newbuffer == NULL)
105 ERR("Error reallocating memory for parser messages\n");
108 wpp_messages = newbuffer;
109 wpp_messages_capacity = newsize;
113 wpp_messages_size += rc;
119 static void PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt, ...)
124 wpp_write_message(fmt, args);
128 static void wpp_error(const char *file, int line, int col, const char *near,
129 const char *msg, va_list ap)
131 wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
133 wpp_write_message(msg, ap);
134 wpp_write_message_var("\n");
137 static void wpp_warning(const char *file, int line, int col, const char *near,
138 const char *msg, va_list ap)
140 wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
141 line, col, "Warning");
142 wpp_write_message(msg, ap);
143 wpp_write_message_var("\n");
146 static char *wpp_lookup_mem(const char *filename, const char *parent_name,
147 char **include_path, int include_path_count)
149 /* Here we return always ok. We will maybe fail on the next wpp_open_mem */
153 parent_include = NULL;
154 if(parent_name[0] != '\0')
156 for(i = 0; i < includes_size; i++)
158 if(!strcmp(parent_name, includes[i].name))
160 parent_include = includes[i].data;
164 if(parent_include == NULL)
166 ERR("Parent include file missing\n");
171 path = malloc(strlen(filename) + 1);
173 memcpy(path, filename, strlen(filename) + 1);
177 static void *wpp_open_mem(const char *filename, int type)
179 struct mem_file_desc *desc;
182 if(filename[0] == '\0') /* "" means to load the initial shader */
184 current_shader.pos = 0;
185 return ¤t_shader;
188 if(current_include == NULL) return NULL;
189 desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
192 ERR("Error allocating memory\n");
195 hr = ID3DInclude_Open(current_include,
196 type ? D3D_INCLUDE_SYSTEM : D3D_INCLUDE_LOCAL,
197 filename, parent_include, (LPCVOID *)&desc->buffer,
201 HeapFree(GetProcessHeap(), 0, desc);
205 if(includes_capacity == includes_size)
207 if(includes_capacity == 0)
209 includes = HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY * sizeof(*includes));
212 ERR("Error allocating memory for the loaded includes structure\n");
215 includes_capacity = INCLUDES_INITIAL_CAPACITY * sizeof(*includes);
219 int newcapacity = includes_capacity * 2;
220 struct loaded_include *newincludes =
221 HeapReAlloc(GetProcessHeap(), 0, includes, newcapacity);
222 if(newincludes == NULL)
224 ERR("Error reallocating memory for the loaded includes structure\n");
227 includes = newincludes;
228 includes_capacity = newcapacity;
231 includes[includes_size].name = filename;
232 includes[includes_size++].data = desc->buffer;
238 ID3DInclude_Close(current_include, desc->buffer);
239 HeapFree(GetProcessHeap(), 0, desc);
243 static void wpp_close_mem(void *file)
245 struct mem_file_desc *desc = file;
247 if(desc != ¤t_shader)
250 ID3DInclude_Close(current_include, desc->buffer);
252 ERR("current_include == NULL, desc == %p, buffer = %s\n",
255 HeapFree(GetProcessHeap(), 0, desc);
259 static int wpp_read_mem(void *file, char *buffer, unsigned int len)
261 struct mem_file_desc *desc = file;
263 len = min(len, desc->size - desc->pos);
264 memcpy(buffer, desc->buffer + desc->pos, len);
269 static void wpp_write_mem(const char *buffer, unsigned int len)
271 char *new_wpp_output;
273 if(wpp_output_capacity == 0)
275 wpp_output = HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY);
278 ERR("Error allocating memory\n");
281 wpp_output_capacity = BUFFER_INITIAL_CAPACITY;
283 if(len > wpp_output_capacity - wpp_output_size)
285 while(len > wpp_output_capacity - wpp_output_size)
287 wpp_output_capacity *= 2;
289 new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
290 wpp_output_capacity);
293 ERR("Error allocating memory\n");
296 wpp_output = new_wpp_output;
298 memcpy(wpp_output + wpp_output_size, buffer, len);
299 wpp_output_size += len;
302 static int wpp_close_output(void)
304 char *new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
305 wpp_output_size + 1);
306 if(!new_wpp_output) return 0;
307 wpp_output = new_wpp_output;
308 wpp_output[wpp_output_size]='\0';
313 static HRESULT preprocess_shader(const void *data, SIZE_T data_size,
314 const D3D_SHADER_MACRO *defines, ID3DInclude *include, ID3DBlob **error_messages)
318 const D3D_SHADER_MACRO *def = defines;
320 static const struct wpp_callbacks wpp_callbacks =
333 while (def->Name != NULL)
335 wpp_add_define(def->Name, def->Definition);
339 current_include = include;
342 wpp_output_size = wpp_output_capacity = 0;
345 wpp_set_callbacks(&wpp_callbacks);
346 wpp_messages_size = wpp_messages_capacity = 0;
348 current_shader.buffer = data;
349 current_shader.size = data_size;
351 ret = wpp_parse("", NULL);
352 if (!wpp_close_output())
356 TRACE("Error during shader preprocessing\n");
362 TRACE("Preprocessor messages:\n%s", debugstr_a(wpp_messages));
366 size = strlen(wpp_messages) + 1;
367 hr = D3DCreateBlob(size, &buffer);
370 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_messages, size);
371 *error_messages = buffer;
375 TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
380 /* Remove the previously added defines */
383 while (defines->Name != NULL)
385 wpp_del_define(defines->Name);
389 HeapFree(GetProcessHeap(), 0, wpp_messages);
393 static HRESULT assemble_shader(const char *preproc_shader,
394 ID3DBlob **shader_blob, ID3DBlob **error_messages)
396 struct bwriter_shader *shader;
397 char *messages = NULL;
404 shader = SlAssembleShader(preproc_shader, &messages);
408 TRACE("Assembler messages:\n");
409 TRACE("%s", messages);
411 TRACE("Shader source:\n");
412 TRACE("%s\n", debugstr_a(preproc_shader));
416 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
418 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
419 hr = D3DCreateBlob(size, &buffer);
422 HeapFree(GetProcessHeap(), 0, messages);
423 if (shader) SlDeleteShader(shader);
426 pos = ID3D10Blob_GetBufferPointer(buffer);
427 if (preproc_messages)
429 CopyMemory(pos, preproc_messages, strlen(preproc_messages) + 1);
430 pos += strlen(preproc_messages);
432 CopyMemory(pos, messages, strlen(messages) + 1);
434 if (*error_messages) ID3D10Blob_Release(*error_messages);
435 *error_messages = buffer;
437 HeapFree(GetProcessHeap(), 0, messages);
442 ERR("Asm reading failed\n");
443 return D3DXERR_INVALIDDATA;
446 hr = SlWriteBytecode(shader, 9, &res);
447 SlDeleteShader(shader);
450 ERR("SlWriteBytecode failed with 0x%08x\n", hr);
451 return D3DXERR_INVALIDDATA;
456 size = HeapSize(GetProcessHeap(), 0, res);
457 hr = D3DCreateBlob(size, &buffer);
460 HeapFree(GetProcessHeap(), 0, res);
463 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), res, size);
464 *shader_blob = buffer;
467 HeapFree(GetProcessHeap(), 0, res);
472 HRESULT WINAPI D3DAssemble(const void *data, SIZE_T datasize, const char *filename,
473 const D3D_SHADER_MACRO *defines, ID3DInclude *include, UINT flags,
474 ID3DBlob **shader, ID3DBlob **error_messages)
478 EnterCriticalSection(&wpp_mutex);
481 if (flags) FIXME("flags %x\n", flags);
483 if (shader) *shader = NULL;
484 if (error_messages) *error_messages = NULL;
486 hr = preprocess_shader(data, datasize, defines, include, error_messages);
488 hr = assemble_shader(wpp_output, shader, error_messages);
490 HeapFree(GetProcessHeap(), 0, wpp_output);
491 LeaveCriticalSection(&wpp_mutex);
495 HRESULT WINAPI D3DCompile(const void *data, SIZE_T data_size, const char *filename,
496 const D3D_SHADER_MACRO *defines, ID3DInclude *include, const char *entrypoint,
497 const char *target, UINT sflags, UINT eflags, ID3DBlob **shader, ID3DBlob **error_messages)
499 FIXME("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s,\n"
500 "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p stub!\n",
501 data, data_size, debugstr_a(filename), defines, include, debugstr_a(entrypoint),
502 debugstr_a(target), sflags, eflags, shader, error_messages);
504 TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
507 D3DCreateBlob(1, error_messages); /* zero fill used as string end */
509 return D3DERR_INVALIDCALL;
512 HRESULT WINAPI D3DPreprocess(const void *data, SIZE_T size, const char *filename,
513 const D3D_SHADER_MACRO *defines, ID3DInclude *include,
514 ID3DBlob **shader, ID3DBlob **error_messages)
522 EnterCriticalSection(&wpp_mutex);
524 if (shader) *shader = NULL;
525 if (error_messages) *error_messages = NULL;
527 hr = preprocess_shader(data, size, defines, include, error_messages);
533 hr = D3DCreateBlob(wpp_output_size, &buffer);
536 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_output, wpp_output_size);
544 HeapFree(GetProcessHeap(), 0, wpp_output);
545 LeaveCriticalSection(&wpp_mutex);