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