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
23 #include "wine/debug.h"
25 #include "d3dcompiler_private.h"
26 #include "wpp_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler
);
30 #define D3DXERR_INVALIDDATA 0x88760b59
32 #define BUFFER_INITIAL_CAPACITY 256
41 static struct mem_file_desc current_shader
;
42 static ID3DInclude
*current_include
;
43 static const char *initial_filename
;
45 #define INCLUDES_INITIAL_CAPACITY 4
53 static struct loaded_include
*includes
;
54 static int includes_capacity
, includes_size
;
55 static const char *parent_include
;
57 static char *wpp_output
;
58 static int wpp_output_capacity
, wpp_output_size
;
60 static char *wpp_messages
;
61 static int wpp_messages_capacity
, wpp_messages_size
;
70 static struct define
*cmdline_defines
;
72 /* Mutex used to guarantee a single invocation
73 of the D3DXAssembleShader function (or its variants) at a time.
74 This is needed as wpp isn't thread-safe */
75 static CRITICAL_SECTION wpp_mutex
;
76 static CRITICAL_SECTION_DEBUG wpp_mutex_debug
=
79 { &wpp_mutex_debug
.ProcessLocksList
,
80 &wpp_mutex_debug
.ProcessLocksList
},
81 0, 0, { (DWORD_PTR
)(__FILE__
": wpp_mutex") }
83 static CRITICAL_SECTION wpp_mutex
= { &wpp_mutex_debug
, -1, 0, 0, 0, 0 };
85 /* Preprocessor error reporting functions */
86 static void wpp_write_message(const char *fmt
, __ms_va_list args
)
91 if(wpp_messages_capacity
== 0)
93 wpp_messages
= HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE
);
94 if(wpp_messages
== NULL
)
97 wpp_messages_capacity
= MESSAGEBUFFER_INITIAL_SIZE
;
102 rc
= vsnprintf(wpp_messages
+ wpp_messages_size
,
103 wpp_messages_capacity
- wpp_messages_size
, fmt
, args
);
105 if (rc
< 0 || /* C89 */
106 rc
>= wpp_messages_capacity
- wpp_messages_size
) { /* C99 */
107 /* Resize the buffer */
108 newsize
= wpp_messages_capacity
* 2;
109 newbuffer
= HeapReAlloc(GetProcessHeap(), 0, wpp_messages
, newsize
);
110 if(newbuffer
== NULL
)
112 ERR("Error reallocating memory for parser messages\n");
115 wpp_messages
= newbuffer
;
116 wpp_messages_capacity
= newsize
;
120 wpp_messages_size
+= rc
;
126 static void WINAPIV
PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt
, ...)
130 __ms_va_start(args
, fmt
);
131 wpp_write_message(fmt
, args
);
135 int WINAPIV
ppy_error(const char *msg
, ...)
138 __ms_va_start(ap
, msg
);
139 wpp_write_message_var("%s:%d:%d: %s: ",
140 pp_status
.input
? pp_status
.input
: "'main file'",
141 pp_status
.line_number
, pp_status
.char_number
, "Error");
142 wpp_write_message(msg
, ap
);
143 wpp_write_message_var("\n");
149 int WINAPIV
ppy_warning(const char *msg
, ...)
152 __ms_va_start(ap
, msg
);
153 wpp_write_message_var("%s:%d:%d: %s: ",
154 pp_status
.input
? pp_status
.input
: "'main file'",
155 pp_status
.line_number
, pp_status
.char_number
, "Warning");
156 wpp_write_message(msg
, ap
);
157 wpp_write_message_var("\n");
162 char *wpp_lookup(const char *filename
, int type
, const char *parent_name
)
164 /* We don't check for file existence here. We will potentially fail on
165 * the following wpp_open_mem(). */
169 TRACE("Looking for include %s, parent %s.\n", debugstr_a(filename
), debugstr_a(parent_name
));
171 parent_include
= NULL
;
172 if (strcmp(parent_name
, initial_filename
))
174 for(i
= 0; i
< includes_size
; i
++)
176 if(!strcmp(parent_name
, includes
[i
].name
))
178 parent_include
= includes
[i
].data
;
182 if(parent_include
== NULL
)
184 ERR("Parent include %s missing.\n", debugstr_a(parent_name
));
189 path
= malloc(strlen(filename
) + 1);
191 memcpy(path
, filename
, strlen(filename
) + 1);
195 void *wpp_open(const char *filename
, int type
)
197 struct mem_file_desc
*desc
;
200 TRACE("Opening include %s.\n", debugstr_a(filename
));
202 if(!strcmp(filename
, initial_filename
))
204 current_shader
.pos
= 0;
205 return ¤t_shader
;
208 if(current_include
== NULL
) return NULL
;
209 desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*desc
));
213 if (FAILED(hr
= ID3DInclude_Open(current_include
, type
? D3D_INCLUDE_LOCAL
: D3D_INCLUDE_SYSTEM
,
214 filename
, parent_include
, (const void **)&desc
->buffer
, &desc
->size
)))
216 HeapFree(GetProcessHeap(), 0, desc
);
220 if(includes_capacity
== includes_size
)
222 if(includes_capacity
== 0)
224 includes
= HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY
* sizeof(*includes
));
227 ERR("Error allocating memory for the loaded includes structure\n");
230 includes_capacity
= INCLUDES_INITIAL_CAPACITY
* sizeof(*includes
);
234 int newcapacity
= includes_capacity
* 2;
235 struct loaded_include
*newincludes
=
236 HeapReAlloc(GetProcessHeap(), 0, includes
, newcapacity
);
237 if(newincludes
== NULL
)
239 ERR("Error reallocating memory for the loaded includes structure\n");
242 includes
= newincludes
;
243 includes_capacity
= newcapacity
;
246 includes
[includes_size
].name
= filename
;
247 includes
[includes_size
++].data
= desc
->buffer
;
253 ID3DInclude_Close(current_include
, desc
->buffer
);
254 HeapFree(GetProcessHeap(), 0, desc
);
258 void wpp_close(void *file
)
260 struct mem_file_desc
*desc
= file
;
262 if(desc
!= ¤t_shader
)
265 ID3DInclude_Close(current_include
, desc
->buffer
);
267 ERR("current_include == NULL, desc == %p, buffer = %s\n",
270 HeapFree(GetProcessHeap(), 0, desc
);
274 int wpp_read(void *file
, char *buffer
, unsigned int len
)
276 struct mem_file_desc
*desc
= file
;
278 len
= min(len
, desc
->size
- desc
->pos
);
279 memcpy(buffer
, desc
->buffer
+ desc
->pos
, len
);
284 void wpp_write(const char *buffer
, unsigned int len
)
286 char *new_wpp_output
;
288 if(wpp_output_capacity
== 0)
290 wpp_output
= HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY
);
294 wpp_output_capacity
= BUFFER_INITIAL_CAPACITY
;
296 if(len
> wpp_output_capacity
- wpp_output_size
)
298 while(len
> wpp_output_capacity
- wpp_output_size
)
300 wpp_output_capacity
*= 2;
302 new_wpp_output
= HeapReAlloc(GetProcessHeap(), 0, wpp_output
,
303 wpp_output_capacity
);
306 ERR("Error allocating memory\n");
309 wpp_output
= new_wpp_output
;
311 memcpy(wpp_output
+ wpp_output_size
, buffer
, len
);
312 wpp_output_size
+= len
;
315 static int wpp_close_output(void)
317 char *new_wpp_output
= HeapReAlloc(GetProcessHeap(), 0, wpp_output
,
318 wpp_output_size
+ 1);
319 if(!new_wpp_output
) return 0;
320 wpp_output
= new_wpp_output
;
321 wpp_output
[wpp_output_size
]='\0';
326 static void add_cmdline_defines(void)
330 for (def
= cmdline_defines
; def
; def
= def
->next
)
332 if (def
->value
) pp_add_define( def
->name
, def
->value
);
336 static void del_cmdline_defines(void)
340 for (def
= cmdline_defines
; def
; def
= def
->next
)
342 if (def
->value
) pp_del_define( def
->name
);
346 static void add_special_defines(void)
348 time_t now
= time(NULL
);
352 strftime(buf
, sizeof(buf
), "\"%b %d %Y\"", localtime(&now
));
353 pp_add_define( "__DATE__", buf
);
355 strftime(buf
, sizeof(buf
), "\"%H:%M:%S\"", localtime(&now
));
356 pp_add_define( "__TIME__", buf
);
358 ppp
= pp_add_define( "__FILE__", "" );
360 ppp
->type
= def_special
;
362 ppp
= pp_add_define( "__LINE__", "" );
364 ppp
->type
= def_special
;
367 static void del_special_defines(void)
369 pp_del_define( "__DATE__" );
370 pp_del_define( "__TIME__" );
371 pp_del_define( "__FILE__" );
372 pp_del_define( "__LINE__" );
376 /* add a define to the preprocessor list */
377 int wpp_add_define( const char *name
, const char *value
)
381 if (!value
) value
= "";
383 for (def
= cmdline_defines
; def
; def
= def
->next
)
385 if (!strcmp( def
->name
, name
))
387 char *new_value
= pp_xstrdup(value
);
391 def
->value
= new_value
;
397 def
= pp_xmalloc( sizeof(*def
) );
400 def
->next
= cmdline_defines
;
401 def
->name
= pp_xstrdup(name
);
407 def
->value
= pp_xstrdup(value
);
414 cmdline_defines
= def
;
419 /* undefine a previously added definition */
420 void wpp_del_define( const char *name
)
424 for (def
= cmdline_defines
; def
; def
= def
->next
)
426 if (!strcmp( def
->name
, name
))
436 /* the main preprocessor parsing loop */
437 int wpp_parse( const char *input
, FILE *output
)
441 pp_status
.input
= NULL
;
442 pp_status
.line_number
= 1;
443 pp_status
.char_number
= 1;
446 ret
= pp_push_define_state();
449 add_cmdline_defines();
450 add_special_defines();
452 if (!input
) pp_status
.file
= stdin
;
453 else if (!(pp_status
.file
= wpp_open(input
, 1)))
455 ppy_error("Could not open %s\n", input
);
456 del_special_defines();
457 del_cmdline_defines();
458 pp_pop_define_state();
462 pp_status
.input
= input
? pp_xstrdup(input
) : NULL
;
465 pp_writestring("# 1 \"%s\" 1\n", input
? input
: "");
468 /* If there were errors during processing, return an error code */
469 if (!ret
&& pp_status
.state
) ret
= pp_status
.state
;
473 wpp_close(pp_status
.file
);
474 free(pp_status
.input
);
476 /* Clean if_stack, it could remain dirty on errors */
477 while (pp_get_if_depth()) pp_pop_if();
478 del_special_defines();
479 del_cmdline_defines();
480 pp_pop_define_state();
484 static HRESULT
preprocess_shader(const void *data
, SIZE_T data_size
, const char *filename
,
485 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
, ID3DBlob
**error_messages
)
489 const D3D_SHADER_MACRO
*def
= defines
;
493 while (def
->Name
!= NULL
)
495 wpp_add_define(def
->Name
, def
->Definition
);
499 current_include
= include
;
502 wpp_output_size
= wpp_output_capacity
= 0;
505 wpp_messages_size
= wpp_messages_capacity
= 0;
507 current_shader
.buffer
= data
;
508 current_shader
.size
= data_size
;
509 initial_filename
= filename
? filename
: "";
511 ret
= wpp_parse(initial_filename
, NULL
);
512 if (!wpp_close_output())
516 TRACE("Error during shader preprocessing\n");
522 TRACE("Preprocessor messages:\n%s\n", debugstr_a(wpp_messages
));
526 size
= strlen(wpp_messages
) + 1;
527 hr
= D3DCreateBlob(size
, &buffer
);
530 CopyMemory(ID3D10Blob_GetBufferPointer(buffer
), wpp_messages
, size
);
531 *error_messages
= buffer
;
535 TRACE("Shader source:\n%s\n", debugstr_an(data
, data_size
));
540 /* Remove the previously added defines */
543 while (defines
->Name
!= NULL
)
545 wpp_del_define(defines
->Name
);
549 HeapFree(GetProcessHeap(), 0, wpp_messages
);
553 static HRESULT
assemble_shader(const char *preproc_shader
,
554 ID3DBlob
**shader_blob
, ID3DBlob
**error_messages
)
556 struct bwriter_shader
*shader
;
557 char *messages
= NULL
;
563 shader
= SlAssembleShader(preproc_shader
, &messages
);
567 TRACE("Assembler messages:\n");
568 TRACE("%s\n", debugstr_a(messages
));
570 TRACE("Shader source:\n");
571 TRACE("%s\n", debugstr_a(preproc_shader
));
575 const char *preproc_messages
= *error_messages
? ID3D10Blob_GetBufferPointer(*error_messages
) : NULL
;
577 size
= strlen(messages
) + (preproc_messages
? strlen(preproc_messages
) : 0) + 1;
578 hr
= D3DCreateBlob(size
, &buffer
);
581 HeapFree(GetProcessHeap(), 0, messages
);
582 if (shader
) SlDeleteShader(shader
);
585 pos
= ID3D10Blob_GetBufferPointer(buffer
);
586 if (preproc_messages
)
588 CopyMemory(pos
, preproc_messages
, strlen(preproc_messages
) + 1);
589 pos
+= strlen(preproc_messages
);
591 CopyMemory(pos
, messages
, strlen(messages
) + 1);
593 if (*error_messages
) ID3D10Blob_Release(*error_messages
);
594 *error_messages
= buffer
;
596 HeapFree(GetProcessHeap(), 0, messages
);
601 ERR("Asm reading failed\n");
602 return D3DXERR_INVALIDDATA
;
605 hr
= shader_write_bytecode(shader
, &res
, &size
);
606 SlDeleteShader(shader
);
609 ERR("Failed to write bytecode, hr %#x.\n", hr
);
610 return D3DXERR_INVALIDDATA
;
615 hr
= D3DCreateBlob(size
, &buffer
);
618 HeapFree(GetProcessHeap(), 0, res
);
621 CopyMemory(ID3D10Blob_GetBufferPointer(buffer
), res
, size
);
622 *shader_blob
= buffer
;
625 HeapFree(GetProcessHeap(), 0, res
);
630 HRESULT WINAPI
D3DAssemble(const void *data
, SIZE_T datasize
, const char *filename
,
631 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
, UINT flags
,
632 ID3DBlob
**shader
, ID3DBlob
**error_messages
)
636 TRACE("data %p, datasize %lu, filename %s, defines %p, include %p, sflags %#x, "
637 "shader %p, error_messages %p.\n",
638 data
, datasize
, debugstr_a(filename
), defines
, include
, flags
, shader
, error_messages
);
640 EnterCriticalSection(&wpp_mutex
);
643 if (flags
) FIXME("flags %x\n", flags
);
645 if (shader
) *shader
= NULL
;
646 if (error_messages
) *error_messages
= NULL
;
648 hr
= preprocess_shader(data
, datasize
, filename
, defines
, include
, error_messages
);
650 hr
= assemble_shader(wpp_output
, shader
, error_messages
);
652 HeapFree(GetProcessHeap(), 0, wpp_output
);
653 LeaveCriticalSection(&wpp_mutex
);
659 enum shader_type type
;
668 /* Must be kept sorted for binary search */
669 static const struct target_info targets_info
[] = {
670 { "cs_4_0", ST_UNKNOWN
, 4, 0, 0, 0, FALSE
, FALSE
},
671 { "cs_4_1", ST_UNKNOWN
, 4, 1, 0, 0, FALSE
, FALSE
},
672 { "cs_5_0", ST_UNKNOWN
, 5, 0, 0, 0, FALSE
, FALSE
},
673 { "ds_5_0", ST_UNKNOWN
, 5, 0, 0, 0, FALSE
, FALSE
},
674 { "fx_2_0", ST_UNKNOWN
, 2, 0, 0, 0, FALSE
, FALSE
},
675 { "fx_4_0", ST_UNKNOWN
, 4, 0, 0, 0, FALSE
, FALSE
},
676 { "fx_4_1", ST_UNKNOWN
, 4, 1, 0, 0, FALSE
, FALSE
},
677 { "fx_5_0", ST_UNKNOWN
, 5, 0, 0, 0, FALSE
, FALSE
},
678 { "gs_4_0", ST_UNKNOWN
, 4, 0, 0, 0, FALSE
, FALSE
},
679 { "gs_4_1", ST_UNKNOWN
, 4, 1, 0, 0, FALSE
, FALSE
},
680 { "gs_5_0", ST_UNKNOWN
, 5, 0, 0, 0, FALSE
, FALSE
},
681 { "hs_5_0", ST_UNKNOWN
, 5, 0, 0, 0, FALSE
, FALSE
},
682 { "ps.1.0", ST_PIXEL
, 1, 0, 0, 0, FALSE
, TRUE
},
683 { "ps.1.1", ST_PIXEL
, 1, 1, 0, 0, FALSE
, FALSE
},
684 { "ps.1.2", ST_PIXEL
, 1, 2, 0, 0, FALSE
, FALSE
},
685 { "ps.1.3", ST_PIXEL
, 1, 3, 0, 0, FALSE
, FALSE
},
686 { "ps.1.4", ST_PIXEL
, 1, 4, 0, 0, FALSE
, FALSE
},
687 { "ps.2.0", ST_PIXEL
, 2, 0, 0, 0, FALSE
, TRUE
},
688 { "ps.2.a", ST_PIXEL
, 2, 1, 0, 0, FALSE
, FALSE
},
689 { "ps.2.b", ST_PIXEL
, 2, 2, 0, 0, FALSE
, FALSE
},
690 { "ps.2.sw", ST_PIXEL
, 2, 0, 0, 0, TRUE
, FALSE
},
691 { "ps.3.0", ST_PIXEL
, 3, 0, 0, 0, FALSE
, TRUE
},
692 { "ps_1_0", ST_PIXEL
, 1, 0, 0, 0, FALSE
, TRUE
},
693 { "ps_1_1", ST_PIXEL
, 1, 1, 0, 0, FALSE
, FALSE
},
694 { "ps_1_2", ST_PIXEL
, 1, 2, 0, 0, FALSE
, FALSE
},
695 { "ps_1_3", ST_PIXEL
, 1, 3, 0, 0, FALSE
, FALSE
},
696 { "ps_1_4", ST_PIXEL
, 1, 4, 0, 0, FALSE
, FALSE
},
697 { "ps_2_0", ST_PIXEL
, 2, 0, 0, 0, FALSE
, TRUE
},
698 { "ps_2_a", ST_PIXEL
, 2, 1, 0, 0, FALSE
, FALSE
},
699 { "ps_2_b", ST_PIXEL
, 2, 2, 0, 0, FALSE
, FALSE
},
700 { "ps_2_sw", ST_PIXEL
, 2, 0, 0, 0, TRUE
, FALSE
},
701 { "ps_3_0", ST_PIXEL
, 3, 0, 0, 0, FALSE
, TRUE
},
702 { "ps_3_sw", ST_PIXEL
, 3, 0, 0, 0, TRUE
, FALSE
},
703 { "ps_4_0", ST_PIXEL
, 4, 0, 0, 0, FALSE
, TRUE
},
704 { "ps_4_0_level_9_0", ST_PIXEL
, 4, 0, 9, 0, FALSE
, FALSE
},
705 { "ps_4_0_level_9_1", ST_PIXEL
, 4, 0, 9, 1, FALSE
, FALSE
},
706 { "ps_4_0_level_9_3", ST_PIXEL
, 4, 0, 9, 3, FALSE
, FALSE
},
707 { "ps_4_1", ST_PIXEL
, 4, 1, 0, 0, FALSE
, TRUE
},
708 { "ps_5_0", ST_PIXEL
, 5, 0, 0, 0, FALSE
, TRUE
},
709 { "tx_1_0", ST_UNKNOWN
, 1, 0, 0, 0, FALSE
, FALSE
},
710 { "vs.1.0", ST_VERTEX
, 1, 0, 0, 0, FALSE
, TRUE
},
711 { "vs.1.1", ST_VERTEX
, 1, 1, 0, 0, FALSE
, TRUE
},
712 { "vs.2.0", ST_VERTEX
, 2, 0, 0, 0, FALSE
, TRUE
},
713 { "vs.2.a", ST_VERTEX
, 2, 1, 0, 0, FALSE
, FALSE
},
714 { "vs.2.sw", ST_VERTEX
, 2, 0, 0, 0, TRUE
, FALSE
},
715 { "vs.3.0", ST_VERTEX
, 3, 0, 0, 0, FALSE
, TRUE
},
716 { "vs.3.sw", ST_VERTEX
, 3, 0, 0, 0, TRUE
, FALSE
},
717 { "vs_1_0", ST_VERTEX
, 1, 0, 0, 0, FALSE
, TRUE
},
718 { "vs_1_1", ST_VERTEX
, 1, 1, 0, 0, FALSE
, TRUE
},
719 { "vs_2_0", ST_VERTEX
, 2, 0, 0, 0, FALSE
, TRUE
},
720 { "vs_2_a", ST_VERTEX
, 2, 1, 0, 0, FALSE
, FALSE
},
721 { "vs_2_sw", ST_VERTEX
, 2, 0, 0, 0, TRUE
, FALSE
},
722 { "vs_3_0", ST_VERTEX
, 3, 0, 0, 0, FALSE
, TRUE
},
723 { "vs_3_sw", ST_VERTEX
, 3, 0, 0, 0, TRUE
, FALSE
},
724 { "vs_4_0", ST_VERTEX
, 4, 0, 0, 0, FALSE
, TRUE
},
725 { "vs_4_0_level_9_0", ST_VERTEX
, 4, 0, 9, 0, FALSE
, FALSE
},
726 { "vs_4_0_level_9_1", ST_VERTEX
, 4, 0, 9, 1, FALSE
, FALSE
},
727 { "vs_4_0_level_9_3", ST_VERTEX
, 4, 0, 9, 3, FALSE
, FALSE
},
728 { "vs_4_1", ST_VERTEX
, 4, 1, 0, 0, FALSE
, TRUE
},
729 { "vs_5_0", ST_VERTEX
, 5, 0, 0, 0, FALSE
, TRUE
},
732 static const struct target_info
* get_target_info(const char *target
)
735 LONG max
= ARRAY_SIZE(targets_info
) - 1;
741 cur
= (min
+ max
) / 2;
742 res
= strcmp(target
, targets_info
[cur
].name
);
748 return &targets_info
[cur
];
754 static HRESULT
compile_shader(const char *preproc_shader
, const char *target
, const char *entrypoint
,
755 ID3DBlob
**shader
, ID3DBlob
**error_messages
)
757 DWORD size
, major
, minor
;
758 char *messages
= NULL
;
762 enum shader_type shader_type
;
763 const struct target_info
*info
;
765 TRACE("Preprocessed shader source: %s\n", debugstr_a(preproc_shader
));
767 TRACE("Checking compilation target %s\n", debugstr_a(target
));
768 info
= get_target_info(target
);
771 FIXME("Unknown compilation target %s\n", debugstr_a(target
));
772 return D3DERR_INVALIDCALL
;
778 FIXME("Compilation target %s not yet supported\n", debugstr_a(target
));
779 return D3DERR_INVALIDCALL
;
783 shader_type
= info
->type
;
784 major
= info
->sm_major
;
785 minor
= info
->sm_minor
;
789 hr
= parse_hlsl_shader(preproc_shader
, shader_type
, major
, minor
, entrypoint
, shader
, &messages
);
793 TRACE("Compiler messages:\n");
794 TRACE("%s\n", debugstr_a(messages
));
796 TRACE("Shader source:\n");
797 TRACE("%s\n", debugstr_a(preproc_shader
));
801 const char *preproc_messages
= *error_messages
? ID3D10Blob_GetBufferPointer(*error_messages
) : NULL
;
804 size
= strlen(messages
) + (preproc_messages
? strlen(preproc_messages
) : 0) + 1;
805 if (FAILED(blob_hr
= D3DCreateBlob(size
, &buffer
)))
807 HeapFree(GetProcessHeap(), 0, messages
);
808 if (shader
&& *shader
)
810 ID3D10Blob_Release(*shader
);
815 pos
= ID3D10Blob_GetBufferPointer(buffer
);
816 if (preproc_messages
)
818 memcpy(pos
, preproc_messages
, strlen(preproc_messages
) + 1);
819 pos
+= strlen(preproc_messages
);
821 memcpy(pos
, messages
, strlen(messages
) + 1);
823 if (*error_messages
) ID3D10Blob_Release(*error_messages
);
824 *error_messages
= buffer
;
826 HeapFree(GetProcessHeap(), 0, messages
);
832 static HRESULT WINAPI
d3dcompiler_include_from_file_open(ID3DInclude
*iface
, D3D_INCLUDE_TYPE include_type
,
833 const char *filename
, const void *parent_data
, const void **data
, UINT
*bytes
)
835 char *fullpath
, *buffer
= NULL
, current_dir
[MAX_PATH
+ 1];
836 const char *initial_dir
;
842 if ((initial_dir
= strrchr(initial_filename
, '\\')))
844 len
= initial_dir
- initial_filename
+ 1;
845 initial_dir
= initial_filename
;
849 len
= GetCurrentDirectoryA(MAX_PATH
, current_dir
);
850 current_dir
[len
] = '\\';
852 initial_dir
= current_dir
;
854 fullpath
= heap_alloc(len
+ strlen(filename
) + 1);
856 return E_OUTOFMEMORY
;
857 memcpy(fullpath
, initial_dir
, len
);
858 strcpy(fullpath
+ len
, filename
);
860 file
= CreateFileA(fullpath
, GENERIC_READ
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
861 if (file
== INVALID_HANDLE_VALUE
)
864 TRACE("Include file found at %s.\n", debugstr_a(fullpath
));
866 size
= GetFileSize(file
, NULL
);
867 if (size
== INVALID_FILE_SIZE
)
869 buffer
= heap_alloc(size
);
872 if (!ReadFile(file
, buffer
, size
, &read
, NULL
) || read
!= size
)
886 WARN("Returning E_FAIL.\n");
890 static HRESULT WINAPI
d3dcompiler_include_from_file_close(ID3DInclude
*iface
, const void *data
)
892 heap_free((void *)data
);
896 const struct ID3DIncludeVtbl d3dcompiler_include_from_file_vtbl
=
898 d3dcompiler_include_from_file_open
,
899 d3dcompiler_include_from_file_close
902 struct d3dcompiler_include_from_file
904 ID3DInclude ID3DInclude_iface
;
907 HRESULT WINAPI
D3DCompile2(const void *data
, SIZE_T data_size
, const char *filename
,
908 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
, const char *entrypoint
,
909 const char *target
, UINT sflags
, UINT eflags
, UINT secondary_flags
,
910 const void *secondary_data
, SIZE_T secondary_data_size
, ID3DBlob
**shader
,
911 ID3DBlob
**error_messages
)
913 struct d3dcompiler_include_from_file include_from_file
;
916 TRACE("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s, "
917 "target %s, sflags %#x, eflags %#x, secondary_flags %#x, secondary_data %p, "
918 "secondary_data_size %lu, shader %p, error_messages %p.\n",
919 data
, data_size
, debugstr_a(filename
), defines
, include
, debugstr_a(entrypoint
),
920 debugstr_a(target
), sflags
, eflags
, secondary_flags
, secondary_data
,
921 secondary_data_size
, shader
, error_messages
);
924 FIXME("secondary data not implemented yet\n");
926 if (shader
) *shader
= NULL
;
927 if (error_messages
) *error_messages
= NULL
;
929 if (include
== D3D_COMPILE_STANDARD_FILE_INCLUDE
)
931 include_from_file
.ID3DInclude_iface
.lpVtbl
= &d3dcompiler_include_from_file_vtbl
;
932 include
= &include_from_file
.ID3DInclude_iface
;
935 EnterCriticalSection(&wpp_mutex
);
937 hr
= preprocess_shader(data
, data_size
, filename
, defines
, include
, error_messages
);
939 hr
= compile_shader(wpp_output
, target
, entrypoint
, shader
, error_messages
);
941 HeapFree(GetProcessHeap(), 0, wpp_output
);
942 LeaveCriticalSection(&wpp_mutex
);
946 HRESULT WINAPI
D3DCompile(const void *data
, SIZE_T data_size
, const char *filename
,
947 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
, const char *entrypoint
,
948 const char *target
, UINT sflags
, UINT eflags
, ID3DBlob
**shader
, ID3DBlob
**error_messages
)
950 TRACE("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s, "
951 "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p.\n",
952 data
, data_size
, debugstr_a(filename
), defines
, include
, debugstr_a(entrypoint
),
953 debugstr_a(target
), sflags
, eflags
, shader
, error_messages
);
955 return D3DCompile2(data
, data_size
, filename
, defines
, include
, entrypoint
, target
, sflags
,
956 eflags
, 0, NULL
, 0, shader
, error_messages
);
959 HRESULT WINAPI
D3DPreprocess(const void *data
, SIZE_T size
, const char *filename
,
960 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
,
961 ID3DBlob
**shader
, ID3DBlob
**error_messages
)
966 TRACE("data %p, size %lu, filename %s, defines %p, include %p, shader %p, error_messages %p\n",
967 data
, size
, debugstr_a(filename
), defines
, include
, shader
, error_messages
);
972 EnterCriticalSection(&wpp_mutex
);
974 if (shader
) *shader
= NULL
;
975 if (error_messages
) *error_messages
= NULL
;
977 hr
= preprocess_shader(data
, size
, filename
, defines
, include
, error_messages
);
983 hr
= D3DCreateBlob(wpp_output_size
, &buffer
);
986 CopyMemory(ID3D10Blob_GetBufferPointer(buffer
), wpp_output
, wpp_output_size
);
994 HeapFree(GetProcessHeap(), 0, wpp_output
);
995 LeaveCriticalSection(&wpp_mutex
);
999 HRESULT WINAPI
D3DDisassemble(const void *data
, SIZE_T size
, UINT flags
, const char *comments
, ID3DBlob
**disassembly
)
1001 FIXME("data %p, size %lu, flags %#x, comments %p, disassembly %p stub!\n",
1002 data
, size
, flags
, comments
, disassembly
);
1006 HRESULT WINAPI
D3DCompileFromFile(const WCHAR
*filename
, const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
,
1007 const char *entrypoint
, const char *target
, UINT flags1
, UINT flags2
, ID3DBlob
**code
, ID3DBlob
**errors
)
1009 char filename_a
[MAX_PATH
], *source
= NULL
;
1010 DWORD source_size
, read_size
;
1014 TRACE("filename %s, defines %p, include %p, entrypoint %s, target %s, flags1 %#x, flags2 %#x, "
1015 "code %p, errors %p.\n", debugstr_w(filename
), defines
, include
, debugstr_a(entrypoint
),
1016 debugstr_a(target
), flags1
, flags2
, code
, errors
);
1018 file
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1019 if (file
== INVALID_HANDLE_VALUE
)
1020 return HRESULT_FROM_WIN32(GetLastError());
1022 source_size
= GetFileSize(file
, NULL
);
1023 if (source_size
== INVALID_FILE_SIZE
)
1025 hr
= HRESULT_FROM_WIN32(GetLastError());
1029 if (!(source
= heap_alloc(source_size
)))
1035 if (!ReadFile(file
, source
, source_size
, &read_size
, NULL
) || read_size
!= source_size
)
1037 WARN("Failed to read file contents.\n");
1042 WideCharToMultiByte(CP_ACP
, 0, filename
, -1, filename_a
, sizeof(filename_a
), NULL
, NULL
);
1044 hr
= D3DCompile(source
, source_size
, filename_a
, defines
, include
, entrypoint
, target
,
1045 flags1
, flags2
, code
, errors
);
1053 HRESULT WINAPI
D3DLoadModule(const void *data
, SIZE_T size
, ID3D11Module
**module
)
1055 FIXME("data %p, size %lu, module %p stub!\n", data
, size
, module
);