propsys: Forward corresponding methods to exports.
[wine.git] / dlls / d3dcompiler_43 / compiler.c
blobd09b5f650ba7c582d9ea9924d71ef675d891d3a8
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 TRACE("Looking for include %s.\n", debugstr_a(filename));
154 parent_include = NULL;
155 if (strcmp(parent_name, initial_filename))
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 %s missing.\n", debugstr_a(parent_name));
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 TRACE("Opening include %s.\n", debugstr_a(filename));
185 if(!strcmp(filename, initial_filename))
187 current_shader.pos = 0;
188 return &current_shader;
191 if(current_include == NULL) return NULL;
192 desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
193 if(!desc)
194 return NULL;
196 if (FAILED(hr = ID3DInclude_Open(current_include, type ? D3D_INCLUDE_LOCAL : D3D_INCLUDE_SYSTEM,
197 filename, parent_include, (const void **)&desc->buffer, &desc->size)))
199 HeapFree(GetProcessHeap(), 0, desc);
200 return NULL;
203 if(includes_capacity == includes_size)
205 if(includes_capacity == 0)
207 includes = HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY * sizeof(*includes));
208 if(includes == NULL)
210 ERR("Error allocating memory for the loaded includes structure\n");
211 goto error;
213 includes_capacity = INCLUDES_INITIAL_CAPACITY * sizeof(*includes);
215 else
217 int newcapacity = includes_capacity * 2;
218 struct loaded_include *newincludes =
219 HeapReAlloc(GetProcessHeap(), 0, includes, newcapacity);
220 if(newincludes == NULL)
222 ERR("Error reallocating memory for the loaded includes structure\n");
223 goto error;
225 includes = newincludes;
226 includes_capacity = newcapacity;
229 includes[includes_size].name = filename;
230 includes[includes_size++].data = desc->buffer;
232 desc->pos = 0;
233 return desc;
235 error:
236 ID3DInclude_Close(current_include, desc->buffer);
237 HeapFree(GetProcessHeap(), 0, desc);
238 return NULL;
241 static void wpp_close_mem(void *file)
243 struct mem_file_desc *desc = file;
245 if(desc != &current_shader)
247 if(current_include)
248 ID3DInclude_Close(current_include, desc->buffer);
249 else
250 ERR("current_include == NULL, desc == %p, buffer = %s\n",
251 desc, desc->buffer);
253 HeapFree(GetProcessHeap(), 0, desc);
257 static int wpp_read_mem(void *file, char *buffer, unsigned int len)
259 struct mem_file_desc *desc = file;
261 len = min(len, desc->size - desc->pos);
262 memcpy(buffer, desc->buffer + desc->pos, len);
263 desc->pos += len;
264 return len;
267 static void wpp_write_mem(const char *buffer, unsigned int len)
269 char *new_wpp_output;
271 if(wpp_output_capacity == 0)
273 wpp_output = HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY);
274 if(!wpp_output)
275 return;
277 wpp_output_capacity = BUFFER_INITIAL_CAPACITY;
279 if(len > wpp_output_capacity - wpp_output_size)
281 while(len > wpp_output_capacity - wpp_output_size)
283 wpp_output_capacity *= 2;
285 new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
286 wpp_output_capacity);
287 if(!new_wpp_output)
289 ERR("Error allocating memory\n");
290 return;
292 wpp_output = new_wpp_output;
294 memcpy(wpp_output + wpp_output_size, buffer, len);
295 wpp_output_size += len;
298 static int wpp_close_output(void)
300 char *new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
301 wpp_output_size + 1);
302 if(!new_wpp_output) return 0;
303 wpp_output = new_wpp_output;
304 wpp_output[wpp_output_size]='\0';
305 wpp_output_size++;
306 return 1;
309 static HRESULT preprocess_shader(const void *data, SIZE_T data_size, const char *filename,
310 const D3D_SHADER_MACRO *defines, ID3DInclude *include, ID3DBlob **error_messages)
312 int ret;
313 HRESULT hr = S_OK;
314 const D3D_SHADER_MACRO *def = defines;
316 static const struct wpp_callbacks wpp_callbacks =
318 wpp_lookup_mem,
319 wpp_open_mem,
320 wpp_close_mem,
321 wpp_read_mem,
322 wpp_write_mem,
323 wpp_error,
324 wpp_warning,
327 if (def != NULL)
329 while (def->Name != NULL)
331 wpp_add_define(def->Name, def->Definition);
332 def++;
335 current_include = include;
336 includes_size = 0;
338 wpp_output_size = wpp_output_capacity = 0;
339 wpp_output = NULL;
341 wpp_set_callbacks(&wpp_callbacks);
342 wpp_messages_size = wpp_messages_capacity = 0;
343 wpp_messages = NULL;
344 current_shader.buffer = data;
345 current_shader.size = data_size;
346 initial_filename = filename ? filename : "";
348 ret = wpp_parse(initial_filename, NULL);
349 if (!wpp_close_output())
350 ret = 1;
351 if (ret)
353 TRACE("Error during shader preprocessing\n");
354 if (wpp_messages)
356 int size;
357 ID3DBlob *buffer;
359 TRACE("Preprocessor messages:\n%s\n", debugstr_a(wpp_messages));
361 if (error_messages)
363 size = strlen(wpp_messages) + 1;
364 hr = D3DCreateBlob(size, &buffer);
365 if (FAILED(hr))
366 goto cleanup;
367 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_messages, size);
368 *error_messages = buffer;
371 if (data)
372 TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
373 hr = E_FAIL;
376 cleanup:
377 /* Remove the previously added defines */
378 if (defines != NULL)
380 while (defines->Name != NULL)
382 wpp_del_define(defines->Name);
383 defines++;
386 HeapFree(GetProcessHeap(), 0, wpp_messages);
387 return hr;
390 static HRESULT assemble_shader(const char *preproc_shader,
391 ID3DBlob **shader_blob, ID3DBlob **error_messages)
393 struct bwriter_shader *shader;
394 char *messages = NULL;
395 HRESULT hr;
396 DWORD *res, size;
397 ID3DBlob *buffer;
398 char *pos;
400 shader = SlAssembleShader(preproc_shader, &messages);
402 if (messages)
404 TRACE("Assembler messages:\n");
405 TRACE("%s\n", debugstr_a(messages));
407 TRACE("Shader source:\n");
408 TRACE("%s\n", debugstr_a(preproc_shader));
410 if (error_messages)
412 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
414 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
415 hr = D3DCreateBlob(size, &buffer);
416 if (FAILED(hr))
418 HeapFree(GetProcessHeap(), 0, messages);
419 if (shader) SlDeleteShader(shader);
420 return hr;
422 pos = ID3D10Blob_GetBufferPointer(buffer);
423 if (preproc_messages)
425 CopyMemory(pos, preproc_messages, strlen(preproc_messages) + 1);
426 pos += strlen(preproc_messages);
428 CopyMemory(pos, messages, strlen(messages) + 1);
430 if (*error_messages) ID3D10Blob_Release(*error_messages);
431 *error_messages = buffer;
433 HeapFree(GetProcessHeap(), 0, messages);
436 if (shader == NULL)
438 ERR("Asm reading failed\n");
439 return D3DXERR_INVALIDDATA;
442 hr = SlWriteBytecode(shader, 9, &res, &size);
443 SlDeleteShader(shader);
444 if (FAILED(hr))
446 ERR("SlWriteBytecode failed with 0x%08x\n", hr);
447 return D3DXERR_INVALIDDATA;
450 if (shader_blob)
452 hr = D3DCreateBlob(size, &buffer);
453 if (FAILED(hr))
455 HeapFree(GetProcessHeap(), 0, res);
456 return hr;
458 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), res, size);
459 *shader_blob = buffer;
462 HeapFree(GetProcessHeap(), 0, res);
464 return S_OK;
467 HRESULT WINAPI D3DAssemble(const void *data, SIZE_T datasize, const char *filename,
468 const D3D_SHADER_MACRO *defines, ID3DInclude *include, UINT flags,
469 ID3DBlob **shader, ID3DBlob **error_messages)
471 HRESULT hr;
473 TRACE("data %p, datasize %lu, filename %s, defines %p, include %p, sflags %#x,\n"
474 "shader %p, error_messages %p\n",
475 data, datasize, debugstr_a(filename), defines, include, flags, shader, error_messages);
477 EnterCriticalSection(&wpp_mutex);
479 /* TODO: flags */
480 if (flags) FIXME("flags %x\n", flags);
482 if (shader) *shader = NULL;
483 if (error_messages) *error_messages = NULL;
485 hr = preprocess_shader(data, datasize, filename, defines, include, error_messages);
486 if (SUCCEEDED(hr))
487 hr = assemble_shader(wpp_output, shader, error_messages);
489 HeapFree(GetProcessHeap(), 0, wpp_output);
490 LeaveCriticalSection(&wpp_mutex);
491 return hr;
494 struct target_info {
495 const char *name;
496 enum shader_type type;
497 DWORD sm_major;
498 DWORD sm_minor;
499 DWORD level_major;
500 DWORD level_minor;
501 BOOL sw;
502 BOOL support;
505 /* Must be kept sorted for binary search */
506 static const struct target_info targets_info[] = {
507 { "cs_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
508 { "cs_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
509 { "cs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
510 { "ds_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
511 { "fx_2_0", ST_UNKNOWN, 2, 0, 0, 0, FALSE, FALSE },
512 { "fx_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
513 { "fx_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
514 { "fx_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
515 { "gs_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
516 { "gs_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
517 { "gs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
518 { "hs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
519 { "ps.1.0", ST_PIXEL, 1, 0, 0, 0, FALSE, TRUE },
520 { "ps.1.1", ST_PIXEL, 1, 1, 0, 0, FALSE, FALSE },
521 { "ps.1.2", ST_PIXEL, 1, 2, 0, 0, FALSE, FALSE },
522 { "ps.1.3", ST_PIXEL, 1, 3, 0, 0, FALSE, FALSE },
523 { "ps.1.4", ST_PIXEL, 1, 4, 0, 0, FALSE, FALSE },
524 { "ps.2.0", ST_PIXEL, 2, 0, 0, 0, FALSE, TRUE },
525 { "ps.2.a", ST_PIXEL, 2, 1, 0, 0, FALSE, FALSE },
526 { "ps.2.b", ST_PIXEL, 2, 2, 0, 0, FALSE, FALSE },
527 { "ps.2.sw", ST_PIXEL, 2, 0, 0, 0, TRUE, FALSE },
528 { "ps.3.0", ST_PIXEL, 3, 0, 0, 0, FALSE, TRUE },
529 { "ps_1_0", ST_PIXEL, 1, 0, 0, 0, FALSE, TRUE },
530 { "ps_1_1", ST_PIXEL, 1, 1, 0, 0, FALSE, FALSE },
531 { "ps_1_2", ST_PIXEL, 1, 2, 0, 0, FALSE, FALSE },
532 { "ps_1_3", ST_PIXEL, 1, 3, 0, 0, FALSE, FALSE },
533 { "ps_1_4", ST_PIXEL, 1, 4, 0, 0, FALSE, FALSE },
534 { "ps_2_0", ST_PIXEL, 2, 0, 0, 0, FALSE, TRUE },
535 { "ps_2_a", ST_PIXEL, 2, 1, 0, 0, FALSE, FALSE },
536 { "ps_2_b", ST_PIXEL, 2, 2, 0, 0, FALSE, FALSE },
537 { "ps_2_sw", ST_PIXEL, 2, 0, 0, 0, TRUE, FALSE },
538 { "ps_3_0", ST_PIXEL, 3, 0, 0, 0, FALSE, TRUE },
539 { "ps_3_sw", ST_PIXEL, 3, 0, 0, 0, TRUE, FALSE },
540 { "ps_4_0", ST_PIXEL, 4, 0, 0, 0, FALSE, TRUE },
541 { "ps_4_0_level_9_0", ST_PIXEL, 4, 0, 9, 0, FALSE, FALSE },
542 { "ps_4_0_level_9_1", ST_PIXEL, 4, 0, 9, 1, FALSE, FALSE },
543 { "ps_4_0_level_9_3", ST_PIXEL, 4, 0, 9, 3, FALSE, FALSE },
544 { "ps_4_1", ST_PIXEL, 4, 1, 0, 0, FALSE, TRUE },
545 { "ps_5_0", ST_PIXEL, 5, 0, 0, 0, FALSE, TRUE },
546 { "tx_1_0", ST_UNKNOWN, 1, 0, 0, 0, FALSE, FALSE },
547 { "vs.1.0", ST_VERTEX, 1, 0, 0, 0, FALSE, TRUE },
548 { "vs.1.1", ST_VERTEX, 1, 1, 0, 0, FALSE, TRUE },
549 { "vs.2.0", ST_VERTEX, 2, 0, 0, 0, FALSE, TRUE },
550 { "vs.2.a", ST_VERTEX, 2, 1, 0, 0, FALSE, FALSE },
551 { "vs.2.sw", ST_VERTEX, 2, 0, 0, 0, TRUE, FALSE },
552 { "vs.3.0", ST_VERTEX, 3, 0, 0, 0, FALSE, TRUE },
553 { "vs.3.sw", ST_VERTEX, 3, 0, 0, 0, TRUE, FALSE },
554 { "vs_1_0", ST_VERTEX, 1, 0, 0, 0, FALSE, TRUE },
555 { "vs_1_1", ST_VERTEX, 1, 1, 0, 0, FALSE, TRUE },
556 { "vs_2_0", ST_VERTEX, 2, 0, 0, 0, FALSE, TRUE },
557 { "vs_2_a", ST_VERTEX, 2, 1, 0, 0, FALSE, FALSE },
558 { "vs_2_sw", ST_VERTEX, 2, 0, 0, 0, TRUE, FALSE },
559 { "vs_3_0", ST_VERTEX, 3, 0, 0, 0, FALSE, TRUE },
560 { "vs_3_sw", ST_VERTEX, 3, 0, 0, 0, TRUE, FALSE },
561 { "vs_4_0", ST_VERTEX, 4, 0, 0, 0, FALSE, TRUE },
562 { "vs_4_0_level_9_0", ST_VERTEX, 4, 0, 9, 0, FALSE, FALSE },
563 { "vs_4_0_level_9_1", ST_VERTEX, 4, 0, 9, 1, FALSE, FALSE },
564 { "vs_4_0_level_9_3", ST_VERTEX, 4, 0, 9, 3, FALSE, FALSE },
565 { "vs_4_1", ST_VERTEX, 4, 1, 0, 0, FALSE, TRUE },
566 { "vs_5_0", ST_VERTEX, 5, 0, 0, 0, FALSE, TRUE },
569 static const struct target_info * get_target_info(const char *target)
571 LONG min = 0;
572 LONG max = sizeof(targets_info) / sizeof(targets_info[0]) - 1;
573 LONG cur;
574 int res;
576 while (min <= max)
578 cur = (min + max) / 2;
579 res = strcmp(target, targets_info[cur].name);
580 if (res < 0)
581 max = cur - 1;
582 else if (res > 0)
583 min = cur + 1;
584 else
585 return &targets_info[cur];
588 return NULL;
591 static HRESULT compile_shader(const char *preproc_shader, const char *target, const char *entrypoint,
592 ID3DBlob **shader_blob, ID3DBlob **error_messages)
594 struct bwriter_shader *shader;
595 char *messages = NULL;
596 HRESULT hr;
597 DWORD *res, size, major, minor;
598 ID3DBlob *buffer;
599 char *pos;
600 enum shader_type shader_type;
601 const struct target_info *info;
603 TRACE("Preprocessed shader source: %s\n", debugstr_a(preproc_shader));
605 TRACE("Checking compilation target %s\n", debugstr_a(target));
606 info = get_target_info(target);
607 if (!info)
609 FIXME("Unknown compilation target %s\n", debugstr_a(target));
610 return D3DERR_INVALIDCALL;
612 else
614 if (!info->support)
616 FIXME("Compilation target %s not yet supported\n", debugstr_a(target));
617 return D3DERR_INVALIDCALL;
619 else
621 shader_type = info->type;
622 major = info->sm_major;
623 minor = info->sm_minor;
627 shader = parse_hlsl_shader(preproc_shader, shader_type, major, minor, entrypoint, &messages);
629 if (messages)
631 TRACE("Compiler messages:\n");
632 TRACE("%s\n", debugstr_a(messages));
634 TRACE("Shader source:\n");
635 TRACE("%s\n", debugstr_a(preproc_shader));
637 if (error_messages)
639 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
641 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
642 hr = D3DCreateBlob(size, &buffer);
643 if (FAILED(hr))
645 HeapFree(GetProcessHeap(), 0, messages);
646 if (shader) SlDeleteShader(shader);
647 return hr;
649 pos = ID3D10Blob_GetBufferPointer(buffer);
650 if (preproc_messages)
652 memcpy(pos, preproc_messages, strlen(preproc_messages) + 1);
653 pos += strlen(preproc_messages);
655 memcpy(pos, messages, strlen(messages) + 1);
657 if (*error_messages) ID3D10Blob_Release(*error_messages);
658 *error_messages = buffer;
660 HeapFree(GetProcessHeap(), 0, messages);
663 if (!shader)
665 ERR("HLSL shader parsing failed.\n");
666 return D3DXERR_INVALIDDATA;
669 hr = SlWriteBytecode(shader, 9, &res, &size);
670 SlDeleteShader(shader);
671 if (FAILED(hr))
673 ERR("SlWriteBytecode failed with error 0x%08x.\n", hr);
674 return D3DXERR_INVALIDDATA;
677 if (shader_blob)
679 hr = D3DCreateBlob(size, &buffer);
680 if (FAILED(hr))
682 HeapFree(GetProcessHeap(), 0, res);
683 return hr;
685 memcpy(ID3D10Blob_GetBufferPointer(buffer), res, size);
686 *shader_blob = buffer;
689 HeapFree(GetProcessHeap(), 0, res);
691 return S_OK;
694 HRESULT WINAPI D3DCompile(const void *data, SIZE_T data_size, const char *filename,
695 const D3D_SHADER_MACRO *defines, ID3DInclude *include, const char *entrypoint,
696 const char *target, UINT sflags, UINT eflags, ID3DBlob **shader, ID3DBlob **error_messages)
698 HRESULT hr;
700 TRACE("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s,\n"
701 "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p\n",
702 data, data_size, debugstr_a(filename), defines, include, debugstr_a(entrypoint),
703 debugstr_a(target), sflags, eflags, shader, error_messages);
705 if (shader) *shader = NULL;
706 if (error_messages) *error_messages = NULL;
708 EnterCriticalSection(&wpp_mutex);
710 hr = preprocess_shader(data, data_size, filename, defines, include, error_messages);
711 if (SUCCEEDED(hr))
712 hr = compile_shader(wpp_output, target, entrypoint, shader, error_messages);
714 HeapFree(GetProcessHeap(), 0, wpp_output);
715 LeaveCriticalSection(&wpp_mutex);
716 return hr;
719 HRESULT WINAPI D3DPreprocess(const void *data, SIZE_T size, const char *filename,
720 const D3D_SHADER_MACRO *defines, ID3DInclude *include,
721 ID3DBlob **shader, ID3DBlob **error_messages)
723 HRESULT hr;
724 ID3DBlob *buffer;
726 TRACE("data %p, size %lu, filename %s, defines %p, include %p, shader %p, error_messages %p\n",
727 data, size, debugstr_a(filename), defines, include, shader, error_messages);
729 if (!data)
730 return E_INVALIDARG;
732 EnterCriticalSection(&wpp_mutex);
734 if (shader) *shader = NULL;
735 if (error_messages) *error_messages = NULL;
737 hr = preprocess_shader(data, size, filename, defines, include, error_messages);
739 if (SUCCEEDED(hr))
741 if (shader)
743 hr = D3DCreateBlob(wpp_output_size, &buffer);
744 if (FAILED(hr))
745 goto cleanup;
746 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_output, wpp_output_size);
747 *shader = buffer;
749 else
750 hr = E_INVALIDARG;
753 cleanup:
754 HeapFree(GetProcessHeap(), 0, wpp_output);
755 LeaveCriticalSection(&wpp_mutex);
756 return hr;
759 HRESULT WINAPI D3DDisassemble(const void *data, SIZE_T size, UINT flags, const char *comments, ID3DBlob **disassembly)
761 FIXME("data %p, size %lu, flags %#x, comments %p, disassembly %p stub!\n",
762 data, size, flags, comments, disassembly);
763 return E_NOTIMPL;