[rshell]
[reactos.git] / dll / directx / wine / d3dcompiler_43 / compiler.c
1 /*
2 * Copyright 2009 Matteo Bruni
3 * Copyright 2010 Matteo Bruni for CodeWeavers
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include "d3dcompiler_private.h"
21 #include "wine/unicode.h"
22 #include "wine/wpp.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
25
26 #define D3DXERR_INVALIDDATA 0x88760b59
27
28 #define BUFFER_INITIAL_CAPACITY 256
29
30 struct mem_file_desc
31 {
32 const char *buffer;
33 unsigned int size;
34 unsigned int pos;
35 };
36
37 static struct mem_file_desc current_shader;
38 static ID3DInclude *current_include;
39 static const char *initial_filename;
40
41 #define INCLUDES_INITIAL_CAPACITY 4
42
43 struct loaded_include
44 {
45 const char *name;
46 const char *data;
47 };
48
49 static struct loaded_include *includes;
50 static int includes_capacity, includes_size;
51 static const char *parent_include;
52
53 static char *wpp_output;
54 static int wpp_output_capacity, wpp_output_size;
55
56 static char *wpp_messages;
57 static int wpp_messages_capacity, wpp_messages_size;
58
59 /* Mutex used to guarantee a single invocation
60 of the D3DXAssembleShader function (or its variants) at a time.
61 This is needed as wpp isn't thread-safe */
62 static CRITICAL_SECTION wpp_mutex;
63 static CRITICAL_SECTION_DEBUG wpp_mutex_debug =
64 {
65 0, 0, &wpp_mutex,
66 { &wpp_mutex_debug.ProcessLocksList,
67 &wpp_mutex_debug.ProcessLocksList },
68 0, 0, { (DWORD_PTR)(__FILE__ ": wpp_mutex") }
69 };
70 static CRITICAL_SECTION wpp_mutex = { &wpp_mutex_debug, -1, 0, 0, 0, 0 };
71
72 /* Preprocessor error reporting functions */
73 static void wpp_write_message(const char *fmt, va_list args)
74 {
75 char* newbuffer;
76 int rc, newsize;
77
78 if(wpp_messages_capacity == 0)
79 {
80 wpp_messages = HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE);
81 if(wpp_messages == NULL)
82 return;
83
84 wpp_messages_capacity = MESSAGEBUFFER_INITIAL_SIZE;
85 }
86
87 while(1)
88 {
89 rc = vsnprintf(wpp_messages + wpp_messages_size,
90 wpp_messages_capacity - wpp_messages_size, fmt, args);
91
92 if (rc < 0 || /* C89 */
93 rc >= wpp_messages_capacity - wpp_messages_size) { /* C99 */
94 /* Resize the buffer */
95 newsize = wpp_messages_capacity * 2;
96 newbuffer = HeapReAlloc(GetProcessHeap(), 0, wpp_messages, newsize);
97 if(newbuffer == NULL)
98 {
99 ERR("Error reallocating memory for parser messages\n");
100 return;
101 }
102 wpp_messages = newbuffer;
103 wpp_messages_capacity = newsize;
104 }
105 else
106 {
107 wpp_messages_size += rc;
108 return;
109 }
110 }
111 }
112
113 static void PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt, ...)
114 {
115 va_list args;
116
117 va_start(args, fmt);
118 wpp_write_message(fmt, args);
119 va_end(args);
120 }
121
122 static void wpp_error(const char *file, int line, int col, const char *_near,
123 const char *msg, va_list ap)
124 {
125 wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
126 line, col, "Error");
127 wpp_write_message(msg, ap);
128 wpp_write_message_var("\n");
129 }
130
131 static void wpp_warning(const char *file, int line, int col, const char *_near,
132 const char *msg, va_list ap)
133 {
134 wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
135 line, col, "Warning");
136 wpp_write_message(msg, ap);
137 wpp_write_message_var("\n");
138 }
139
140 static char *wpp_lookup_mem(const char *filename, int type, const char *parent_name,
141 char **include_path, int include_path_count)
142 {
143 /* Here we return always ok. We will maybe fail on the next wpp_open_mem */
144 char *path;
145 int i;
146
147 parent_include = NULL;
148 if(parent_name[0] != '\0')
149 {
150 for(i = 0; i < includes_size; i++)
151 {
152 if(!strcmp(parent_name, includes[i].name))
153 {
154 parent_include = includes[i].data;
155 break;
156 }
157 }
158 if(parent_include == NULL)
159 {
160 ERR("Parent include file missing\n");
161 return NULL;
162 }
163 }
164
165 path = malloc(strlen(filename) + 1);
166 if(path)
167 memcpy(path, filename, strlen(filename) + 1);
168 return path;
169 }
170
171 static void *wpp_open_mem(const char *filename, int type)
172 {
173 struct mem_file_desc *desc;
174 HRESULT hr;
175
176 if(!strcmp(filename, initial_filename))
177 {
178 current_shader.pos = 0;
179 return &current_shader;
180 }
181
182 if(current_include == NULL) return NULL;
183 desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
184 if(!desc)
185 return NULL;
186
187 hr = ID3DInclude_Open(current_include,
188 type ? D3D_INCLUDE_LOCAL : D3D_INCLUDE_SYSTEM,
189 filename, parent_include, (LPCVOID *)&desc->buffer,
190 &desc->size);
191 if(FAILED(hr))
192 {
193 HeapFree(GetProcessHeap(), 0, desc);
194 return NULL;
195 }
196
197 if(includes_capacity == includes_size)
198 {
199 if(includes_capacity == 0)
200 {
201 includes = HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY * sizeof(*includes));
202 if(includes == NULL)
203 {
204 ERR("Error allocating memory for the loaded includes structure\n");
205 goto error;
206 }
207 includes_capacity = INCLUDES_INITIAL_CAPACITY * sizeof(*includes);
208 }
209 else
210 {
211 int newcapacity = includes_capacity * 2;
212 struct loaded_include *newincludes =
213 HeapReAlloc(GetProcessHeap(), 0, includes, newcapacity);
214 if(newincludes == NULL)
215 {
216 ERR("Error reallocating memory for the loaded includes structure\n");
217 goto error;
218 }
219 includes = newincludes;
220 includes_capacity = newcapacity;
221 }
222 }
223 includes[includes_size].name = filename;
224 includes[includes_size++].data = desc->buffer;
225
226 desc->pos = 0;
227 return desc;
228
229 error:
230 ID3DInclude_Close(current_include, desc->buffer);
231 HeapFree(GetProcessHeap(), 0, desc);
232 return NULL;
233 }
234
235 static void wpp_close_mem(void *file)
236 {
237 struct mem_file_desc *desc = file;
238
239 if(desc != &current_shader)
240 {
241 if(current_include)
242 ID3DInclude_Close(current_include, desc->buffer);
243 else
244 ERR("current_include == NULL, desc == %p, buffer = %s\n",
245 desc, desc->buffer);
246
247 HeapFree(GetProcessHeap(), 0, desc);
248 }
249 }
250
251 static int wpp_read_mem(void *file, char *buffer, unsigned int len)
252 {
253 struct mem_file_desc *desc = file;
254
255 len = min(len, desc->size - desc->pos);
256 memcpy(buffer, desc->buffer + desc->pos, len);
257 desc->pos += len;
258 return len;
259 }
260
261 static void wpp_write_mem(const char *buffer, unsigned int len)
262 {
263 char *new_wpp_output;
264
265 if(wpp_output_capacity == 0)
266 {
267 wpp_output = HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY);
268 if(!wpp_output)
269 return;
270
271 wpp_output_capacity = BUFFER_INITIAL_CAPACITY;
272 }
273 if(len > wpp_output_capacity - wpp_output_size)
274 {
275 while(len > wpp_output_capacity - wpp_output_size)
276 {
277 wpp_output_capacity *= 2;
278 }
279 new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
280 wpp_output_capacity);
281 if(!new_wpp_output)
282 {
283 ERR("Error allocating memory\n");
284 return;
285 }
286 wpp_output = new_wpp_output;
287 }
288 memcpy(wpp_output + wpp_output_size, buffer, len);
289 wpp_output_size += len;
290 }
291
292 static int wpp_close_output(void)
293 {
294 char *new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
295 wpp_output_size + 1);
296 if(!new_wpp_output) return 0;
297 wpp_output = new_wpp_output;
298 wpp_output[wpp_output_size]='\0';
299 wpp_output_size++;
300 return 1;
301 }
302
303 static HRESULT preprocess_shader(const void *data, SIZE_T data_size, const char *filename,
304 const D3D_SHADER_MACRO *defines, ID3DInclude *include, ID3DBlob **error_messages)
305 {
306 int ret;
307 HRESULT hr = S_OK;
308 const D3D_SHADER_MACRO *def = defines;
309
310 static const struct wpp_callbacks wpp_callbacks =
311 {
312 wpp_lookup_mem,
313 wpp_open_mem,
314 wpp_close_mem,
315 wpp_read_mem,
316 wpp_write_mem,
317 wpp_error,
318 wpp_warning,
319 };
320
321 if (def != NULL)
322 {
323 while (def->Name != NULL)
324 {
325 wpp_add_define(def->Name, def->Definition);
326 def++;
327 }
328 }
329 current_include = include;
330 includes_size = 0;
331
332 wpp_output_size = wpp_output_capacity = 0;
333 wpp_output = NULL;
334
335 wpp_set_callbacks(&wpp_callbacks);
336 wpp_messages_size = wpp_messages_capacity = 0;
337 wpp_messages = NULL;
338 current_shader.buffer = data;
339 current_shader.size = data_size;
340 initial_filename = filename ? filename : "";
341
342 ret = wpp_parse(initial_filename, NULL);
343 if (!wpp_close_output())
344 ret = 1;
345 if (ret)
346 {
347 TRACE("Error during shader preprocessing\n");
348 if (wpp_messages)
349 {
350 int size;
351 ID3DBlob *buffer;
352
353 TRACE("Preprocessor messages:\n%s\n", debugstr_a(wpp_messages));
354
355 if (error_messages)
356 {
357 size = strlen(wpp_messages) + 1;
358 hr = D3DCreateBlob(size, &buffer);
359 if (FAILED(hr))
360 goto cleanup;
361 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_messages, size);
362 *error_messages = buffer;
363 }
364 }
365 if (data)
366 TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
367 hr = E_FAIL;
368 }
369
370 cleanup:
371 /* Remove the previously added defines */
372 if (defines != NULL)
373 {
374 while (defines->Name != NULL)
375 {
376 wpp_del_define(defines->Name);
377 defines++;
378 }
379 }
380 HeapFree(GetProcessHeap(), 0, wpp_messages);
381 return hr;
382 }
383
384 static HRESULT assemble_shader(const char *preproc_shader,
385 ID3DBlob **shader_blob, ID3DBlob **error_messages)
386 {
387 struct bwriter_shader *shader;
388 char *messages = NULL;
389 HRESULT hr;
390 DWORD *res, size;
391 ID3DBlob *buffer;
392 char *pos;
393
394 shader = SlAssembleShader(preproc_shader, &messages);
395
396 if (messages)
397 {
398 TRACE("Assembler messages:\n");
399 TRACE("%s\n", debugstr_a(messages));
400
401 TRACE("Shader source:\n");
402 TRACE("%s\n", debugstr_a(preproc_shader));
403
404 if (error_messages)
405 {
406 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
407
408 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
409 hr = D3DCreateBlob(size, &buffer);
410 if (FAILED(hr))
411 {
412 HeapFree(GetProcessHeap(), 0, messages);
413 if (shader) SlDeleteShader(shader);
414 return hr;
415 }
416 pos = ID3D10Blob_GetBufferPointer(buffer);
417 if (preproc_messages)
418 {
419 CopyMemory(pos, preproc_messages, strlen(preproc_messages) + 1);
420 pos += strlen(preproc_messages);
421 }
422 CopyMemory(pos, messages, strlen(messages) + 1);
423
424 if (*error_messages) ID3D10Blob_Release(*error_messages);
425 *error_messages = buffer;
426 }
427 HeapFree(GetProcessHeap(), 0, messages);
428 }
429
430 if (shader == NULL)
431 {
432 ERR("Asm reading failed\n");
433 return D3DXERR_INVALIDDATA;
434 }
435
436 hr = SlWriteBytecode(shader, 9, &res, &size);
437 SlDeleteShader(shader);
438 if (FAILED(hr))
439 {
440 ERR("SlWriteBytecode failed with 0x%08x\n", hr);
441 return D3DXERR_INVALIDDATA;
442 }
443
444 if (shader_blob)
445 {
446 hr = D3DCreateBlob(size, &buffer);
447 if (FAILED(hr))
448 {
449 HeapFree(GetProcessHeap(), 0, res);
450 return hr;
451 }
452 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), res, size);
453 *shader_blob = buffer;
454 }
455
456 HeapFree(GetProcessHeap(), 0, res);
457
458 return S_OK;
459 }
460
461 HRESULT WINAPI D3DAssemble(const void *data, SIZE_T datasize, const char *filename,
462 const D3D_SHADER_MACRO *defines, ID3DInclude *include, UINT flags,
463 ID3DBlob **shader, ID3DBlob **error_messages)
464 {
465 HRESULT hr;
466
467 TRACE("data %p, datasize %lu, filename %s, defines %p, include %p, sflags %#x,\n"
468 "shader %p, error_messages %p\n",
469 data, datasize, debugstr_a(filename), defines, include, flags, shader, error_messages);
470
471 EnterCriticalSection(&wpp_mutex);
472
473 /* TODO: flags */
474 if (flags) FIXME("flags %x\n", flags);
475
476 if (shader) *shader = NULL;
477 if (error_messages) *error_messages = NULL;
478
479 hr = preprocess_shader(data, datasize, filename, defines, include, error_messages);
480 if (SUCCEEDED(hr))
481 hr = assemble_shader(wpp_output, shader, error_messages);
482
483 HeapFree(GetProcessHeap(), 0, wpp_output);
484 LeaveCriticalSection(&wpp_mutex);
485 return hr;
486 }
487
488 struct target_info {
489 const char *name;
490 enum shader_type type;
491 DWORD sm_major;
492 DWORD sm_minor;
493 DWORD level_major;
494 DWORD level_minor;
495 BOOL sw;
496 BOOL support;
497 };
498
499 /* Must be kept sorted for binary search */
500 static const struct target_info targets_info[] = {
501 { "cs_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
502 { "cs_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
503 { "cs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
504 { "ds_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
505 { "fx_2_0", ST_UNKNOWN, 2, 0, 0, 0, FALSE, FALSE },
506 { "fx_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
507 { "fx_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
508 { "fx_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
509 { "gs_4_0", ST_UNKNOWN, 4, 0, 0, 0, FALSE, FALSE },
510 { "gs_4_1", ST_UNKNOWN, 4, 1, 0, 0, FALSE, FALSE },
511 { "gs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
512 { "hs_5_0", ST_UNKNOWN, 5, 0, 0, 0, FALSE, FALSE },
513 { "ps.1.0", ST_PIXEL, 1, 0, 0, 0, FALSE, TRUE },
514 { "ps.1.1", ST_PIXEL, 1, 1, 0, 0, FALSE, FALSE },
515 { "ps.1.2", ST_PIXEL, 1, 2, 0, 0, FALSE, FALSE },
516 { "ps.1.3", ST_PIXEL, 1, 3, 0, 0, FALSE, FALSE },
517 { "ps.1.4", ST_PIXEL, 1, 4, 0, 0, FALSE, FALSE },
518 { "ps.2.0", ST_PIXEL, 2, 0, 0, 0, FALSE, TRUE },
519 { "ps.2.a", ST_PIXEL, 2, 1, 0, 0, FALSE, FALSE },
520 { "ps.2.b", ST_PIXEL, 2, 2, 0, 0, FALSE, FALSE },
521 { "ps.2.sw", ST_PIXEL, 2, 0, 0, 0, TRUE, FALSE },
522 { "ps.3.0", ST_PIXEL, 3, 0, 0, 0, FALSE, TRUE },
523 { "ps_1_0", ST_PIXEL, 1, 0, 0, 0, FALSE, TRUE },
524 { "ps_1_1", ST_PIXEL, 1, 1, 0, 0, FALSE, FALSE },
525 { "ps_1_2", ST_PIXEL, 1, 2, 0, 0, FALSE, FALSE },
526 { "ps_1_3", ST_PIXEL, 1, 3, 0, 0, FALSE, FALSE },
527 { "ps_1_4", ST_PIXEL, 1, 4, 0, 0, FALSE, FALSE },
528 { "ps_2_0", ST_PIXEL, 2, 0, 0, 0, FALSE, TRUE },
529 { "ps_2_a", ST_PIXEL, 2, 1, 0, 0, FALSE, FALSE },
530 { "ps_2_b", ST_PIXEL, 2, 2, 0, 0, FALSE, FALSE },
531 { "ps_2_sw", ST_PIXEL, 2, 0, 0, 0, TRUE, FALSE },
532 { "ps_3_0", ST_PIXEL, 3, 0, 0, 0, FALSE, TRUE },
533 { "ps_3_sw", ST_PIXEL, 3, 0, 0, 0, TRUE, FALSE },
534 { "ps_4_0", ST_PIXEL, 4, 0, 0, 0, FALSE, TRUE },
535 { "ps_4_0_level_9_0", ST_PIXEL, 4, 0, 9, 0, FALSE, FALSE },
536 { "ps_4_0_level_9_1", ST_PIXEL, 4, 0, 9, 1, FALSE, FALSE },
537 { "ps_4_0_level_9_3", ST_PIXEL, 4, 0, 9, 3, FALSE, FALSE },
538 { "ps_4_1", ST_PIXEL, 4, 1, 0, 0, FALSE, TRUE },
539 { "ps_5_0", ST_PIXEL, 5, 0, 0, 0, FALSE, TRUE },
540 { "tx_1_0", ST_UNKNOWN, 1, 0, 0, 0, FALSE, FALSE },
541 { "vs.1.0", ST_VERTEX, 1, 0, 0, 0, FALSE, TRUE },
542 { "vs.1.1", ST_VERTEX, 1, 1, 0, 0, FALSE, TRUE },
543 { "vs.2.0", ST_VERTEX, 2, 0, 0, 0, FALSE, TRUE },
544 { "vs.2.a", ST_VERTEX, 2, 1, 0, 0, FALSE, FALSE },
545 { "vs.2.sw", ST_VERTEX, 2, 0, 0, 0, TRUE, FALSE },
546 { "vs.3.0", ST_VERTEX, 3, 0, 0, 0, FALSE, TRUE },
547 { "vs.3.sw", ST_VERTEX, 3, 0, 0, 0, TRUE, FALSE },
548 { "vs_1_0", ST_VERTEX, 1, 0, 0, 0, FALSE, TRUE },
549 { "vs_1_1", ST_VERTEX, 1, 1, 0, 0, FALSE, TRUE },
550 { "vs_2_0", ST_VERTEX, 2, 0, 0, 0, FALSE, TRUE },
551 { "vs_2_a", ST_VERTEX, 2, 1, 0, 0, FALSE, FALSE },
552 { "vs_2_sw", ST_VERTEX, 2, 0, 0, 0, TRUE, FALSE },
553 { "vs_3_0", ST_VERTEX, 3, 0, 0, 0, FALSE, TRUE },
554 { "vs_3_sw", ST_VERTEX, 3, 0, 0, 0, TRUE, FALSE },
555 { "vs_4_0", ST_VERTEX, 4, 0, 0, 0, FALSE, TRUE },
556 { "vs_4_0_level_9_0", ST_VERTEX, 4, 0, 9, 0, FALSE, FALSE },
557 { "vs_4_0_level_9_1", ST_VERTEX, 4, 0, 9, 1, FALSE, FALSE },
558 { "vs_4_0_level_9_3", ST_VERTEX, 4, 0, 9, 3, FALSE, FALSE },
559 { "vs_4_1", ST_VERTEX, 4, 1, 0, 0, FALSE, TRUE },
560 { "vs_5_0", ST_VERTEX, 5, 0, 0, 0, FALSE, TRUE },
561 };
562
563 static const struct target_info * get_target_info(const char *target)
564 {
565 LONG min = 0;
566 LONG max = sizeof(targets_info) / sizeof(targets_info[0]) - 1;
567 LONG cur;
568 int res;
569
570 while (min <= max)
571 {
572 cur = (min + max) / 2;
573 res = strcmp(target, targets_info[cur].name);
574 if (res < 0)
575 max = cur - 1;
576 else if (res > 0)
577 min = cur + 1;
578 else
579 return &targets_info[cur];
580 }
581
582 return NULL;
583 }
584
585 static HRESULT compile_shader(const char *preproc_shader, const char *target, const char *entrypoint,
586 ID3DBlob **shader_blob, ID3DBlob **error_messages)
587 {
588 struct bwriter_shader *shader;
589 char *messages = NULL;
590 HRESULT hr;
591 DWORD *res, size, major, minor;
592 ID3DBlob *buffer;
593 char *pos;
594 enum shader_type shader_type;
595 const struct target_info *info;
596
597 TRACE("Preprocessed shader source: %s\n", debugstr_a(preproc_shader));
598
599 TRACE("Checking compilation target %s\n", debugstr_a(target));
600 info = get_target_info(target);
601 if (!info)
602 {
603 FIXME("Unknown compilation target %s\n", debugstr_a(target));
604 return D3DERR_INVALIDCALL;
605 }
606 else
607 {
608 if (!info->support)
609 {
610 FIXME("Compilation target %s not yet supported\n", debugstr_a(target));
611 return D3DERR_INVALIDCALL;
612 }
613 else
614 {
615 shader_type = info->type;
616 major = info->sm_major;
617 minor = info->sm_minor;
618 }
619 }
620
621 shader = parse_hlsl_shader(preproc_shader, shader_type, major, minor, entrypoint, &messages);
622
623 if (messages)
624 {
625 TRACE("Compiler messages:\n");
626 TRACE("%s\n", debugstr_a(messages));
627
628 TRACE("Shader source:\n");
629 TRACE("%s\n", debugstr_a(preproc_shader));
630
631 if (error_messages)
632 {
633 const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL;
634
635 size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1;
636 hr = D3DCreateBlob(size, &buffer);
637 if (FAILED(hr))
638 {
639 HeapFree(GetProcessHeap(), 0, messages);
640 if (shader) SlDeleteShader(shader);
641 return hr;
642 }
643 pos = ID3D10Blob_GetBufferPointer(buffer);
644 if (preproc_messages)
645 {
646 memcpy(pos, preproc_messages, strlen(preproc_messages) + 1);
647 pos += strlen(preproc_messages);
648 }
649 memcpy(pos, messages, strlen(messages) + 1);
650
651 if (*error_messages) ID3D10Blob_Release(*error_messages);
652 *error_messages = buffer;
653 }
654 HeapFree(GetProcessHeap(), 0, messages);
655 }
656
657 if (!shader)
658 {
659 ERR("HLSL shader parsing failed.\n");
660 return D3DXERR_INVALIDDATA;
661 }
662
663 hr = SlWriteBytecode(shader, 9, &res, &size);
664 SlDeleteShader(shader);
665 if (FAILED(hr))
666 {
667 ERR("SlWriteBytecode failed with error 0x%08x.\n", hr);
668 return D3DXERR_INVALIDDATA;
669 }
670
671 if (shader_blob)
672 {
673 hr = D3DCreateBlob(size, &buffer);
674 if (FAILED(hr))
675 {
676 HeapFree(GetProcessHeap(), 0, res);
677 return hr;
678 }
679 memcpy(ID3D10Blob_GetBufferPointer(buffer), res, size);
680 *shader_blob = buffer;
681 }
682
683 HeapFree(GetProcessHeap(), 0, res);
684
685 return S_OK;
686 }
687
688 HRESULT WINAPI D3DCompile(const void *data, SIZE_T data_size, const char *filename,
689 const D3D_SHADER_MACRO *defines, ID3DInclude *include, const char *entrypoint,
690 const char *target, UINT sflags, UINT eflags, ID3DBlob **shader, ID3DBlob **error_messages)
691 {
692 HRESULT hr;
693
694 TRACE("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s,\n"
695 "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p\n",
696 data, data_size, debugstr_a(filename), defines, include, debugstr_a(entrypoint),
697 debugstr_a(target), sflags, eflags, shader, error_messages);
698
699 if (shader) *shader = NULL;
700 if (error_messages) *error_messages = NULL;
701
702 EnterCriticalSection(&wpp_mutex);
703
704 hr = preprocess_shader(data, data_size, filename, defines, include, error_messages);
705 if (SUCCEEDED(hr))
706 hr = compile_shader(wpp_output, target, entrypoint, shader, error_messages);
707
708 HeapFree(GetProcessHeap(), 0, wpp_output);
709 LeaveCriticalSection(&wpp_mutex);
710 return hr;
711 }
712
713 HRESULT WINAPI D3DPreprocess(const void *data, SIZE_T size, const char *filename,
714 const D3D_SHADER_MACRO *defines, ID3DInclude *include,
715 ID3DBlob **shader, ID3DBlob **error_messages)
716 {
717 HRESULT hr;
718 ID3DBlob *buffer;
719
720 TRACE("data %p, size %lu, filename %s, defines %p, include %p, shader %p, error_messages %p\n",
721 data, size, debugstr_a(filename), defines, include, shader, error_messages);
722
723 if (!data)
724 return E_INVALIDARG;
725
726 EnterCriticalSection(&wpp_mutex);
727
728 if (shader) *shader = NULL;
729 if (error_messages) *error_messages = NULL;
730
731 hr = preprocess_shader(data, size, filename, defines, include, error_messages);
732
733 if (SUCCEEDED(hr))
734 {
735 if (shader)
736 {
737 hr = D3DCreateBlob(wpp_output_size, &buffer);
738 if (FAILED(hr))
739 goto cleanup;
740 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_output, wpp_output_size);
741 *shader = buffer;
742 }
743 else
744 hr = E_INVALIDARG;
745 }
746
747 cleanup:
748 HeapFree(GetProcessHeap(), 0, wpp_output);
749 LeaveCriticalSection(&wpp_mutex);
750 return hr;
751 }
752
753 HRESULT WINAPI D3DDisassemble(const void *data, SIZE_T size, UINT flags, const char *comments, ID3DBlob **disassembly)
754 {
755 FIXME("data %p, size %lu, flags %#x, comments %p, disassembly %p stub!\n",
756 data, size, flags, comments, disassembly);
757 return E_NOTIMPL;
758 }