msi: Don't defer custom actions in the UI sequence if they match the currently runnin...
[wine/multimedia.git] / dlls / d3dcompiler_43 / compiler.c
blobac8fdc57f2b298d91f09e1c07ea94fb203b0fd5e
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)
87 return;
89 wpp_messages_capacity = MESSAGEBUFFER_INITIAL_SIZE;
92 while(1)
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");
105 return;
107 wpp_messages = newbuffer;
108 wpp_messages_capacity = newsize;
110 else
112 wpp_messages_size += rc;
113 return;
118 static void PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt, ...)
120 va_list args;
122 va_start(args, fmt);
123 wpp_write_message(fmt, args);
124 va_end(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'",
131 line, col, "Error");
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 */
149 char *path;
150 int i;
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;
160 break;
163 if(parent_include == NULL)
165 ERR("Parent include file missing\n");
166 return NULL;
170 path = malloc(strlen(filename) + 1);
171 if(path)
172 memcpy(path, filename, strlen(filename) + 1);
173 return path;
176 static void *wpp_open_mem(const char *filename, int type)
178 struct mem_file_desc *desc;
179 HRESULT hr;
181 if(!strcmp(filename, initial_filename))
183 current_shader.pos = 0;
184 return &current_shader;
187 if(current_include == NULL) return NULL;
188 desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
189 if(!desc)
190 return NULL;
192 hr = ID3DInclude_Open(current_include,
193 type ? D3D_INCLUDE_LOCAL : D3D_INCLUDE_SYSTEM,
194 filename, parent_include, (LPCVOID *)&desc->buffer,
195 &desc->size);
196 if(FAILED(hr))
198 HeapFree(GetProcessHeap(), 0, desc);
199 return NULL;
202 if(includes_capacity == includes_size)
204 if(includes_capacity == 0)
206 includes = HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY * sizeof(*includes));
207 if(includes == NULL)
209 ERR("Error allocating memory for the loaded includes structure\n");
210 goto error;
212 includes_capacity = INCLUDES_INITIAL_CAPACITY * sizeof(*includes);
214 else
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");
222 goto error;
224 includes = newincludes;
225 includes_capacity = newcapacity;
228 includes[includes_size].name = filename;
229 includes[includes_size++].data = desc->buffer;
231 desc->pos = 0;
232 return desc;
234 error:
235 ID3DInclude_Close(current_include, desc->buffer);
236 HeapFree(GetProcessHeap(), 0, desc);
237 return NULL;
240 static void wpp_close_mem(void *file)
242 struct mem_file_desc *desc = file;
244 if(desc != &current_shader)
246 if(current_include)
247 ID3DInclude_Close(current_include, desc->buffer);
248 else
249 ERR("current_include == NULL, desc == %p, buffer = %s\n",
250 desc, desc->buffer);
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);
262 desc->pos += len;
263 return 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);
273 if(!wpp_output)
274 return;
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);
286 if(!new_wpp_output)
288 ERR("Error allocating memory\n");
289 return;
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';
304 wpp_output_size++;
305 return 1;
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)
311 int ret;
312 HRESULT hr = S_OK;
313 const D3D_SHADER_MACRO *def = defines;
315 static const struct wpp_callbacks wpp_callbacks =
317 wpp_lookup_mem,
318 wpp_open_mem,
319 wpp_close_mem,
320 wpp_read_mem,
321 wpp_write_mem,
322 wpp_error,
323 wpp_warning,
326 if (def != NULL)
328 while (def->Name != NULL)
330 wpp_add_define(def->Name, def->Definition);
331 def++;
334 current_include = include;
335 includes_size = 0;
337 wpp_output_size = wpp_output_capacity = 0;
338 wpp_output = NULL;
340 wpp_set_callbacks(&wpp_callbacks);
341 wpp_messages_size = wpp_messages_capacity = 0;
342 wpp_messages = NULL;
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())
349 ret = 1;
350 if (ret)
352 TRACE("Error during shader preprocessing\n");
353 if (wpp_messages)
355 int size;
356 ID3DBlob *buffer;
358 TRACE("Preprocessor messages:\n%s\n", debugstr_a(wpp_messages));
360 if (error_messages)
362 size = strlen(wpp_messages) + 1;
363 hr = D3DCreateBlob(size, &buffer);
364 if (FAILED(hr))
365 goto cleanup;
366 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_messages, size);
367 *error_messages = buffer;
370 if (data)
371 TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
372 hr = E_FAIL;
375 cleanup:
376 /* Remove the previously added defines */
377 if (defines != NULL)
379 while (defines->Name != NULL)
381 wpp_del_define(defines->Name);
382 defines++;
385 HeapFree(GetProcessHeap(), 0, wpp_messages);
386 return hr;
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;
394 HRESULT hr;
395 DWORD *res, size;
396 ID3DBlob *buffer;
397 char *pos;
399 shader = SlAssembleShader(preproc_shader, &messages);
401 if (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));
409 if (error_messages)
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);
415 if (FAILED(hr))
417 HeapFree(GetProcessHeap(), 0, messages);
418 if (shader) SlDeleteShader(shader);
419 return hr;
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);
435 if (shader == NULL)
437 ERR("Asm reading failed\n");
438 return D3DXERR_INVALIDDATA;
441 hr = SlWriteBytecode(shader, 9, &res, &size);
442 SlDeleteShader(shader);
443 if (FAILED(hr))
445 ERR("SlWriteBytecode failed with 0x%08x\n", hr);
446 return D3DXERR_INVALIDDATA;
449 if (shader_blob)
451 hr = D3DCreateBlob(size, &buffer);
452 if (FAILED(hr))
454 HeapFree(GetProcessHeap(), 0, res);
455 return hr;
457 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), res, size);
458 *shader_blob = buffer;
461 HeapFree(GetProcessHeap(), 0, res);
463 return S_OK;
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)
470 HRESULT hr;
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);
478 /* TODO: flags */
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);
485 if (SUCCEEDED(hr))
486 hr = assemble_shader(wpp_output, shader, error_messages);
488 HeapFree(GetProcessHeap(), 0, wpp_output);
489 LeaveCriticalSection(&wpp_mutex);
490 return hr;
493 struct target_info {
494 const char *name;
495 enum shader_type type;
496 DWORD sm_major;
497 DWORD sm_minor;
498 DWORD level_major;
499 DWORD level_minor;
500 BOOL sw;
501 BOOL support;
504 /* Must be kept sorted for binary search */
505 static const struct target_info targets_info[] = {
506 { "cs_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
507 { "cs_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
508 { "cs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
509 { "ds_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
510 { "fx_2_0", ST_UNKNOWN, 2, 0, 0, 0, FALSE, FALSE },
511 { "fx_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
512 { "fx_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
513 { "fx_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
514 { "gs_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
515 { "gs_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
516 { "gs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
517 { "hs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
518 { "ps.1.0", ST_PIXEL, 1, 0, 0, 0, FALSE, TRUE },
519 { "ps.1.1", ST_PIXEL, 1, 1, 0, 0, FALSE, FALSE },
520 { "ps.1.2", ST_PIXEL, 1, 2, 0, 0, FALSE, FALSE },
521 { "ps.1.3", ST_PIXEL, 1, 3, 0, 0, FALSE, FALSE },
522 { "ps.1.4", ST_PIXEL, 1, 4, 0, 0, FALSE, FALSE },
523 { "ps.2.0", ST_PIXEL, 2, 0, 0, 0, FALSE, TRUE },
524 { "ps.2.a", ST_PIXEL, 2, 1, 0, 0, FALSE, FALSE },
525 { "ps.2.b", ST_PIXEL, 2, 2, 0, 0, FALSE, FALSE },
526 { "ps.2.sw", ST_PIXEL, 2, 0, 0, 0, TRUE, FALSE },
527 { "ps.3.0", ST_PIXEL, 3, 0, 0, 0, FALSE, TRUE },
528 { "ps_1_0", ST_PIXEL, 1, 0, 0, 0, FALSE, TRUE },
529 { "ps_1_1", ST_PIXEL, 1, 1, 0, 0, FALSE, FALSE },
530 { "ps_1_2", ST_PIXEL, 1, 2, 0, 0, FALSE, FALSE },
531 { "ps_1_3", ST_PIXEL, 1, 3, 0, 0, FALSE, FALSE },
532 { "ps_1_4", ST_PIXEL, 1, 4, 0, 0, FALSE, FALSE },
533 { "ps_2_0", ST_PIXEL, 2, 0, 0, 0, FALSE, TRUE },
534 { "ps_2_a", ST_PIXEL, 2, 1, 0, 0, FALSE, FALSE },
535 { "ps_2_b", ST_PIXEL, 2, 2, 0, 0, FALSE, FALSE },
536 { "ps_2_sw", ST_PIXEL, 2, 0, 0, 0, TRUE, FALSE },
537 { "ps_3_0", ST_PIXEL, 3, 0, 0, 0, FALSE, TRUE },
538 { "ps_3_sw", ST_PIXEL, 3, 0, 0, 0, TRUE, FALSE },
539 { "ps_4_0", ST_PIXEL, 4, 0, 0, 0, FALSE, TRUE },
540 { "ps_4_0_level_9_0", ST_PIXEL, 4, 0, 9, 0, FALSE, FALSE },
541 { "ps_4_0_level_9_1", ST_PIXEL, 4, 0, 9, 1, FALSE, FALSE },
542 { "ps_4_0_level_9_3", ST_PIXEL, 4, 0, 9, 3, FALSE, FALSE },
543 { "ps_4_1", ST_PIXEL, 4, 1, 0, 0, FALSE, TRUE },
544 { "ps_5_0", ST_PIXEL, 5, 0, 0, 0, FALSE, TRUE },
545 { "tx_1_0", ST_UNKNOWN, 1, 0, 0, 0, FALSE, FALSE },
546 { "vs.1.0", ST_VERTEX, 1, 0, 0, 0, FALSE, TRUE },
547 { "vs.1.1", ST_VERTEX, 1, 1, 0, 0, FALSE, TRUE },
548 { "vs.2.0", ST_VERTEX, 2, 0, 0, 0, FALSE, TRUE },
549 { "vs.2.a", ST_VERTEX, 2, 1, 0, 0, FALSE, FALSE },
550 { "vs.2.sw", ST_VERTEX, 2, 0, 0, 0, TRUE, FALSE },
551 { "vs.3.0", ST_VERTEX, 3, 0, 0, 0, FALSE, TRUE },
552 { "vs.3.sw", ST_VERTEX, 3, 0, 0, 0, TRUE, FALSE },
553 { "vs_1_0", ST_VERTEX, 1, 0, 0, 0, FALSE, TRUE },
554 { "vs_1_1", ST_VERTEX, 1, 1, 0, 0, FALSE, TRUE },
555 { "vs_2_0", ST_VERTEX, 2, 0, 0, 0, FALSE, TRUE },
556 { "vs_2_a", ST_VERTEX, 2, 1, 0, 0, FALSE, FALSE },
557 { "vs_2_sw", ST_VERTEX, 2, 0, 0, 0, TRUE, FALSE },
558 { "vs_3_0", ST_VERTEX, 3, 0, 0, 0, FALSE, TRUE },
559 { "vs_3_sw", ST_VERTEX, 3, 0, 0, 0, TRUE, FALSE },
560 { "vs_4_0", ST_VERTEX, 4, 0, 0, 0, FALSE, TRUE },
561 { "vs_4_0_level_9_0", ST_VERTEX, 4, 0, 9, 0, FALSE, FALSE },
562 { "vs_4_0_level_9_1", ST_VERTEX, 4, 0, 9, 1, FALSE, FALSE },
563 { "vs_4_0_level_9_3", ST_VERTEX, 4, 0, 9, 3, FALSE, FALSE },
564 { "vs_4_1", ST_VERTEX, 4, 1, 0, 0, FALSE, TRUE },
565 { "vs_5_0", ST_VERTEX, 5, 0, 0, 0, FALSE, TRUE },
568 static const struct target_info * get_target_info(const char *target)
570 LONG min = 0;
571 LONG max = sizeof(targets_info) / sizeof(targets_info[0]) - 1;
572 LONG cur;
573 int res;
575 while (min <= max)
577 cur = (min + max) / 2;
578 res = strcmp(target, targets_info[cur].name);
579 if (res < 0)
580 max = cur - 1;
581 else if (res > 0)
582 min = cur + 1;
583 else
584 return &targets_info[cur];
587 return NULL;
590 static HRESULT compile_shader(const char *preproc_shader, const char *target, const char *entrypoint,
591 ID3DBlob **shader_blob, ID3DBlob **error_messages)
593 struct bwriter_shader *shader;
594 char *messages = NULL;
595 HRESULT hr;
596 DWORD *res, size, major, minor;
597 ID3DBlob *buffer;
598 char *pos;
599 enum shader_type shader_type;
600 const struct target_info *info;
602 TRACE("Preprocessed shader source: %s\n", debugstr_a(preproc_shader));
604 TRACE("Checking compilation target %s\n", debugstr_a(target));
605 info = get_target_info(target);
606 if (!info)
608 FIXME("Unknown compilation target %s\n", debugstr_a(target));
609 return D3DERR_INVALIDCALL;
611 else
613 if (!info->support)
615 FIXME("Compilation target %s not yet supported\n", debugstr_a(target));
616 return D3DERR_INVALIDCALL;
618 else
620 shader_type = info->type;
621 major = info->sm_major;
622 minor = info->sm_minor;
626 shader = parse_hlsl_shader(preproc_shader, shader_type, major, minor, entrypoint, &messages);
628 if (messages)
630 TRACE("Compiler messages:\n");
631 TRACE("%s\n", debugstr_a(messages));
633 TRACE("Shader source:\n");
634 TRACE("%s\n", debugstr_a(preproc_shader));
636 if (error_messages)
638 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
640 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
641 hr = D3DCreateBlob(size, &buffer);
642 if (FAILED(hr))
644 HeapFree(GetProcessHeap(), 0, messages);
645 if (shader) SlDeleteShader(shader);
646 return hr;
648 pos = ID3D10Blob_GetBufferPointer(buffer);
649 if (preproc_messages)
651 memcpy(pos, preproc_messages, strlen(preproc_messages) + 1);
652 pos += strlen(preproc_messages);
654 memcpy(pos, messages, strlen(messages) + 1);
656 if (*error_messages) ID3D10Blob_Release(*error_messages);
657 *error_messages = buffer;
659 HeapFree(GetProcessHeap(), 0, messages);
662 if (!shader)
664 ERR("HLSL shader parsing failed.\n");
665 return D3DXERR_INVALIDDATA;
668 hr = SlWriteBytecode(shader, 9, &res, &size);
669 SlDeleteShader(shader);
670 if (FAILED(hr))
672 ERR("SlWriteBytecode failed with error 0x%08x.\n", hr);
673 return D3DXERR_INVALIDDATA;
676 if (shader_blob)
678 hr = D3DCreateBlob(size, &buffer);
679 if (FAILED(hr))
681 HeapFree(GetProcessHeap(), 0, res);
682 return hr;
684 memcpy(ID3D10Blob_GetBufferPointer(buffer), res, size);
685 *shader_blob = buffer;
688 HeapFree(GetProcessHeap(), 0, res);
690 return S_OK;
693 HRESULT WINAPI D3DCompile(const void *data, SIZE_T data_size, const char *filename,
694 const D3D_SHADER_MACRO *defines, ID3DInclude *include, const char *entrypoint,
695 const char *target, UINT sflags, UINT eflags, ID3DBlob **shader, ID3DBlob **error_messages)
697 HRESULT hr;
699 TRACE("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s,\n"
700 "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p\n",
701 data, data_size, debugstr_a(filename), defines, include, debugstr_a(entrypoint),
702 debugstr_a(target), sflags, eflags, shader, error_messages);
704 if (shader) *shader = NULL;
705 if (error_messages) *error_messages = NULL;
707 EnterCriticalSection(&wpp_mutex);
709 hr = preprocess_shader(data, data_size, filename, defines, include, error_messages);
710 if (SUCCEEDED(hr))
711 hr = compile_shader(wpp_output, target, entrypoint, shader, error_messages);
713 HeapFree(GetProcessHeap(), 0, wpp_output);
714 LeaveCriticalSection(&wpp_mutex);
715 return hr;
718 HRESULT WINAPI D3DPreprocess(const void *data, SIZE_T size, const char *filename,
719 const D3D_SHADER_MACRO *defines, ID3DInclude *include,
720 ID3DBlob **shader, ID3DBlob **error_messages)
722 HRESULT hr;
723 ID3DBlob *buffer;
725 TRACE("data %p, size %lu, filename %s, defines %p, include %p, shader %p, error_messages %p\n",
726 data, size, debugstr_a(filename), defines, include, shader, error_messages);
728 if (!data)
729 return E_INVALIDARG;
731 EnterCriticalSection(&wpp_mutex);
733 if (shader) *shader = NULL;
734 if (error_messages) *error_messages = NULL;
736 hr = preprocess_shader(data, size, filename, defines, include, error_messages);
738 if (SUCCEEDED(hr))
740 if (shader)
742 hr = D3DCreateBlob(wpp_output_size, &buffer);
743 if (FAILED(hr))
744 goto cleanup;
745 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_output, wpp_output_size);
746 *shader = buffer;
748 else
749 hr = E_INVALIDARG;
752 cleanup:
753 HeapFree(GetProcessHeap(), 0, wpp_output);
754 LeaveCriticalSection(&wpp_mutex);
755 return hr;
758 HRESULT WINAPI D3DDisassemble(const void *data, SIZE_T size, UINT flags, const char *comments, ID3DBlob **disassembly)
760 FIXME("data %p, size %lu, flags %#x, comments %p, disassembly %p stub!\n",
761 data, size, flags, comments, disassembly);
762 return E_NOTIMPL;