winaspi.dll16: Simplify the "pointer to start of array" idiom.
[wine/multimedia.git] / dlls / d3dcompiler_43 / compiler.c
blob9f1ed86aae65ef1ab65058693a7efb8a52b6d3b7
1 /*
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
20 #define COBJMACROS
21 #include "config.h"
22 #include "wine/port.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
26 #include "d3dcompiler_private.h"
27 #include "wine/wpp.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
31 #define D3DXERR_INVALIDDATA 0x88760b59
33 #define BUFFER_INITIAL_CAPACITY 256
35 struct mem_file_desc
37 const char *buffer;
38 unsigned int size;
39 unsigned int pos;
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
48 struct loaded_include
50 const char *name;
51 const char *data;
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 =
70 0, 0, &wpp_mutex,
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)
80 char* newbuffer;
81 int rc, newsize;
83 if(wpp_messages_capacity == 0)
85 wpp_messages = HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE);
86 if(wpp_messages == NULL)
88 ERR("Error allocating memory for parser messages\n");
89 return;
91 wpp_messages_capacity = MESSAGEBUFFER_INITIAL_SIZE;
94 while(1)
96 rc = vsnprintf(wpp_messages + wpp_messages_size,
97 wpp_messages_capacity - wpp_messages_size, fmt, args);
99 if (rc < 0 || /* C89 */
100 rc >= wpp_messages_capacity - wpp_messages_size) { /* C99 */
101 /* Resize the buffer */
102 newsize = wpp_messages_capacity * 2;
103 newbuffer = HeapReAlloc(GetProcessHeap(), 0, wpp_messages, newsize);
104 if(newbuffer == NULL)
106 ERR("Error reallocating memory for parser messages\n");
107 return;
109 wpp_messages = newbuffer;
110 wpp_messages_capacity = newsize;
112 else
114 wpp_messages_size += rc;
115 return;
120 static void PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt, ...)
122 va_list args;
124 va_start(args, fmt);
125 wpp_write_message(fmt, args);
126 va_end(args);
129 static void wpp_error(const char *file, int line, int col, const char *near,
130 const char *msg, va_list ap)
132 wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
133 line, col, "Error");
134 wpp_write_message(msg, ap);
135 wpp_write_message_var("\n");
138 static void wpp_warning(const char *file, int line, int col, const char *near,
139 const char *msg, va_list ap)
141 wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
142 line, col, "Warning");
143 wpp_write_message(msg, ap);
144 wpp_write_message_var("\n");
147 static char *wpp_lookup_mem(const char *filename, int type, const char *parent_name,
148 char **include_path, int include_path_count)
150 /* Here we return always ok. We will maybe fail on the next wpp_open_mem */
151 char *path;
152 int i;
154 parent_include = NULL;
155 if(parent_name[0] != '\0')
157 for(i = 0; i < includes_size; i++)
159 if(!strcmp(parent_name, includes[i].name))
161 parent_include = includes[i].data;
162 break;
165 if(parent_include == NULL)
167 ERR("Parent include file missing\n");
168 return NULL;
172 path = malloc(strlen(filename) + 1);
173 if(path)
174 memcpy(path, filename, strlen(filename) + 1);
175 return path;
178 static void *wpp_open_mem(const char *filename, int type)
180 struct mem_file_desc *desc;
181 HRESULT hr;
183 if(!strcmp(filename, initial_filename))
185 current_shader.pos = 0;
186 return &current_shader;
189 if(current_include == NULL) return NULL;
190 desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
191 if(!desc)
193 ERR("Error allocating memory\n");
194 return NULL;
196 hr = ID3DInclude_Open(current_include,
197 type ? D3D_INCLUDE_LOCAL : D3D_INCLUDE_SYSTEM,
198 filename, parent_include, (LPCVOID *)&desc->buffer,
199 &desc->size);
200 if(FAILED(hr))
202 HeapFree(GetProcessHeap(), 0, desc);
203 return NULL;
206 if(includes_capacity == includes_size)
208 if(includes_capacity == 0)
210 includes = HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY * sizeof(*includes));
211 if(includes == NULL)
213 ERR("Error allocating memory for the loaded includes structure\n");
214 goto error;
216 includes_capacity = INCLUDES_INITIAL_CAPACITY * sizeof(*includes);
218 else
220 int newcapacity = includes_capacity * 2;
221 struct loaded_include *newincludes =
222 HeapReAlloc(GetProcessHeap(), 0, includes, newcapacity);
223 if(newincludes == NULL)
225 ERR("Error reallocating memory for the loaded includes structure\n");
226 goto error;
228 includes = newincludes;
229 includes_capacity = newcapacity;
232 includes[includes_size].name = filename;
233 includes[includes_size++].data = desc->buffer;
235 desc->pos = 0;
236 return desc;
238 error:
239 ID3DInclude_Close(current_include, desc->buffer);
240 HeapFree(GetProcessHeap(), 0, desc);
241 return NULL;
244 static void wpp_close_mem(void *file)
246 struct mem_file_desc *desc = file;
248 if(desc != &current_shader)
250 if(current_include)
251 ID3DInclude_Close(current_include, desc->buffer);
252 else
253 ERR("current_include == NULL, desc == %p, buffer = %s\n",
254 desc, desc->buffer);
256 HeapFree(GetProcessHeap(), 0, desc);
260 static int wpp_read_mem(void *file, char *buffer, unsigned int len)
262 struct mem_file_desc *desc = file;
264 len = min(len, desc->size - desc->pos);
265 memcpy(buffer, desc->buffer + desc->pos, len);
266 desc->pos += len;
267 return len;
270 static void wpp_write_mem(const char *buffer, unsigned int len)
272 char *new_wpp_output;
274 if(wpp_output_capacity == 0)
276 wpp_output = HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY);
277 if(!wpp_output)
279 ERR("Error allocating memory\n");
280 return;
282 wpp_output_capacity = BUFFER_INITIAL_CAPACITY;
284 if(len > wpp_output_capacity - wpp_output_size)
286 while(len > wpp_output_capacity - wpp_output_size)
288 wpp_output_capacity *= 2;
290 new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
291 wpp_output_capacity);
292 if(!new_wpp_output)
294 ERR("Error allocating memory\n");
295 return;
297 wpp_output = new_wpp_output;
299 memcpy(wpp_output + wpp_output_size, buffer, len);
300 wpp_output_size += len;
303 static int wpp_close_output(void)
305 char *new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
306 wpp_output_size + 1);
307 if(!new_wpp_output) return 0;
308 wpp_output = new_wpp_output;
309 wpp_output[wpp_output_size]='\0';
310 wpp_output_size++;
311 return 1;
314 static HRESULT preprocess_shader(const void *data, SIZE_T data_size, const char *filename,
315 const D3D_SHADER_MACRO *defines, ID3DInclude *include, ID3DBlob **error_messages)
317 int ret;
318 HRESULT hr = S_OK;
319 const D3D_SHADER_MACRO *def = defines;
321 static const struct wpp_callbacks wpp_callbacks =
323 wpp_lookup_mem,
324 wpp_open_mem,
325 wpp_close_mem,
326 wpp_read_mem,
327 wpp_write_mem,
328 wpp_error,
329 wpp_warning,
332 if (def != NULL)
334 while (def->Name != NULL)
336 wpp_add_define(def->Name, def->Definition);
337 def++;
340 current_include = include;
341 includes_size = 0;
343 wpp_output_size = wpp_output_capacity = 0;
344 wpp_output = NULL;
346 wpp_set_callbacks(&wpp_callbacks);
347 wpp_messages_size = wpp_messages_capacity = 0;
348 wpp_messages = NULL;
349 current_shader.buffer = data;
350 current_shader.size = data_size;
351 initial_filename = filename ? filename : "";
353 ret = wpp_parse(initial_filename, NULL);
354 if (!wpp_close_output())
355 ret = 1;
356 if (ret)
358 TRACE("Error during shader preprocessing\n");
359 if (wpp_messages)
361 int size;
362 ID3DBlob *buffer;
364 TRACE("Preprocessor messages:\n%s\n", debugstr_a(wpp_messages));
366 if (error_messages)
368 size = strlen(wpp_messages) + 1;
369 hr = D3DCreateBlob(size, &buffer);
370 if (FAILED(hr))
371 goto cleanup;
372 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_messages, size);
373 *error_messages = buffer;
376 if (data)
377 TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
378 hr = E_FAIL;
381 cleanup:
382 /* Remove the previously added defines */
383 if (defines != NULL)
385 while (defines->Name != NULL)
387 wpp_del_define(defines->Name);
388 defines++;
391 HeapFree(GetProcessHeap(), 0, wpp_messages);
392 return hr;
395 static HRESULT assemble_shader(const char *preproc_shader,
396 ID3DBlob **shader_blob, ID3DBlob **error_messages)
398 struct bwriter_shader *shader;
399 char *messages = NULL;
400 HRESULT hr;
401 DWORD *res, size;
402 ID3DBlob *buffer;
403 char *pos;
405 shader = SlAssembleShader(preproc_shader, &messages);
407 if (messages)
409 TRACE("Assembler messages:\n");
410 TRACE("%s\n", debugstr_a(messages));
412 TRACE("Shader source:\n");
413 TRACE("%s\n", debugstr_a(preproc_shader));
415 if (error_messages)
417 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
419 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
420 hr = D3DCreateBlob(size, &buffer);
421 if (FAILED(hr))
423 HeapFree(GetProcessHeap(), 0, messages);
424 if (shader) SlDeleteShader(shader);
425 return hr;
427 pos = ID3D10Blob_GetBufferPointer(buffer);
428 if (preproc_messages)
430 CopyMemory(pos, preproc_messages, strlen(preproc_messages) + 1);
431 pos += strlen(preproc_messages);
433 CopyMemory(pos, messages, strlen(messages) + 1);
435 if (*error_messages) ID3D10Blob_Release(*error_messages);
436 *error_messages = buffer;
438 HeapFree(GetProcessHeap(), 0, messages);
441 if (shader == NULL)
443 ERR("Asm reading failed\n");
444 return D3DXERR_INVALIDDATA;
447 hr = SlWriteBytecode(shader, 9, &res, &size);
448 SlDeleteShader(shader);
449 if (FAILED(hr))
451 ERR("SlWriteBytecode failed with 0x%08x\n", hr);
452 return D3DXERR_INVALIDDATA;
455 if (shader_blob)
457 hr = D3DCreateBlob(size, &buffer);
458 if (FAILED(hr))
460 HeapFree(GetProcessHeap(), 0, res);
461 return hr;
463 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), res, size);
464 *shader_blob = buffer;
467 HeapFree(GetProcessHeap(), 0, res);
469 return S_OK;
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)
476 HRESULT hr;
478 EnterCriticalSection(&wpp_mutex);
480 /* TODO: flags */
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, filename, defines, include, error_messages);
487 if (SUCCEEDED(hr))
488 hr = assemble_shader(wpp_output, shader, error_messages);
490 HeapFree(GetProcessHeap(), 0, wpp_output);
491 LeaveCriticalSection(&wpp_mutex);
492 return hr;
495 static HRESULT compile_shader(const char *preproc_shader, const char *target, const char *entrypoint,
496 ID3DBlob **shader_blob, ID3DBlob **error_messages)
498 struct bwriter_shader *shader;
499 char *messages = NULL;
500 HRESULT hr;
501 DWORD *res, size, major, minor;
502 ID3DBlob *buffer;
503 char *pos;
504 enum shader_type shader_type;
506 TRACE("Preprocessed shader source: %s\n", debugstr_a(preproc_shader));
508 TRACE("Parsing compilation target %s.\n", debugstr_a(target));
509 if (strlen(target) != 6 || target[1] != 's' || target[2] != '_' || target[4] != '_')
511 FIXME("Unknown compilation target %s.\n", debugstr_a(target));
512 return D3DERR_INVALIDCALL;
515 if (target[0] == 'v')
516 shader_type = ST_VERTEX;
517 else if (target[0] == 'p')
518 shader_type = ST_PIXEL;
519 else
521 FIXME("Unsupported shader target type %s.\n", debugstr_a(target));
522 return D3DERR_INVALIDCALL;
525 major = target[3] - '0';
526 if (major == 0 || major > 5)
528 FIXME("Unsupported shader target major version %d.\n", major);
529 return D3DERR_INVALIDCALL;
531 minor = target[5] - '0';
532 if (minor > 1 || (minor == 1 && (shader_type != ST_VERTEX || major > 1)))
534 FIXME("Unsupported shader target minor version %d.\n", minor);
535 return D3DERR_INVALIDCALL;
537 shader = parse_hlsl_shader(preproc_shader, shader_type, major, minor, entrypoint, &messages);
539 if (messages)
541 TRACE("Compiler messages:\n");
542 TRACE("%s\n", debugstr_a(messages));
544 TRACE("Shader source:\n");
545 TRACE("%s\n", debugstr_a(preproc_shader));
547 if (error_messages)
549 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
551 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
552 hr = D3DCreateBlob(size, &buffer);
553 if (FAILED(hr))
555 HeapFree(GetProcessHeap(), 0, messages);
556 if (shader) SlDeleteShader(shader);
557 return hr;
559 pos = ID3D10Blob_GetBufferPointer(buffer);
560 if (preproc_messages)
562 memcpy(pos, preproc_messages, strlen(preproc_messages) + 1);
563 pos += strlen(preproc_messages);
565 memcpy(pos, messages, strlen(messages) + 1);
567 if (*error_messages) ID3D10Blob_Release(*error_messages);
568 *error_messages = buffer;
570 HeapFree(GetProcessHeap(), 0, messages);
573 if (!shader)
575 ERR("HLSL shader parsing failed.\n");
576 return D3DXERR_INVALIDDATA;
579 hr = SlWriteBytecode(shader, 9, &res, &size);
580 SlDeleteShader(shader);
581 if (FAILED(hr))
583 ERR("SlWriteBytecode failed with error 0x%08x.\n", hr);
584 return D3DXERR_INVALIDDATA;
587 if (shader_blob)
589 hr = D3DCreateBlob(size, &buffer);
590 if (FAILED(hr))
592 HeapFree(GetProcessHeap(), 0, res);
593 return hr;
595 memcpy(ID3D10Blob_GetBufferPointer(buffer), res, size);
596 *shader_blob = buffer;
599 HeapFree(GetProcessHeap(), 0, res);
601 return S_OK;
604 HRESULT WINAPI D3DCompile(const void *data, SIZE_T data_size, const char *filename,
605 const D3D_SHADER_MACRO *defines, ID3DInclude *include, const char *entrypoint,
606 const char *target, UINT sflags, UINT eflags, ID3DBlob **shader, ID3DBlob **error_messages)
608 HRESULT hr;
610 FIXME("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s,\n"
611 "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p stub!\n",
612 data, data_size, debugstr_a(filename), defines, include, debugstr_a(entrypoint),
613 debugstr_a(target), sflags, eflags, shader, error_messages);
615 if (shader) *shader = NULL;
616 if (error_messages) *error_messages = NULL;
618 EnterCriticalSection(&wpp_mutex);
620 hr = preprocess_shader(data, data_size, filename, defines, include, error_messages);
621 if (SUCCEEDED(hr))
622 hr = compile_shader(wpp_output, target, entrypoint, shader, error_messages);
624 HeapFree(GetProcessHeap(), 0, wpp_output);
625 LeaveCriticalSection(&wpp_mutex);
626 return hr;
629 HRESULT WINAPI D3DPreprocess(const void *data, SIZE_T size, const char *filename,
630 const D3D_SHADER_MACRO *defines, ID3DInclude *include,
631 ID3DBlob **shader, ID3DBlob **error_messages)
633 HRESULT hr;
634 ID3DBlob *buffer;
636 if (!data)
637 return E_INVALIDARG;
639 EnterCriticalSection(&wpp_mutex);
641 if (shader) *shader = NULL;
642 if (error_messages) *error_messages = NULL;
644 hr = preprocess_shader(data, size, filename, defines, include, error_messages);
646 if (SUCCEEDED(hr))
648 if (shader)
650 hr = D3DCreateBlob(wpp_output_size, &buffer);
651 if (FAILED(hr))
652 goto cleanup;
653 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_output, wpp_output_size);
654 *shader = buffer;
656 else
657 hr = E_INVALIDARG;
660 cleanup:
661 HeapFree(GetProcessHeap(), 0, wpp_output);
662 LeaveCriticalSection(&wpp_mutex);
663 return hr;