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
22 #include "wine/port.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
26 #include "d3dcompiler_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler
);
31 #define D3DXERR_INVALIDDATA 0x88760b59
33 #define BUFFER_INITIAL_CAPACITY 256
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
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
=
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
)
83 if(wpp_messages_capacity
== 0)
85 wpp_messages
= HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE
);
86 if(wpp_messages
== NULL
)
88 ERR("Error allocating memory for parser messages\n");
91 wpp_messages_capacity
= MESSAGEBUFFER_INITIAL_SIZE
;
96 rc
= vsnprintf(wpp_messages
+ wpp_messages_size
,
97 wpp_messages_capacity
- wpp_messages_size
, fmt
, args
);
99 if (rc
< 0 || /* C89 */
100 rc
>= wpp_messages_capacity
- wpp_messages_size
) { /* C99 */
101 /* Resize the buffer */
102 newsize
= wpp_messages_capacity
* 2;
103 newbuffer
= HeapReAlloc(GetProcessHeap(), 0, wpp_messages
, newsize
);
104 if(newbuffer
== NULL
)
106 ERR("Error reallocating memory for parser messages\n");
109 wpp_messages
= newbuffer
;
110 wpp_messages_capacity
= newsize
;
114 wpp_messages_size
+= rc
;
120 static void PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt
, ...)
125 wpp_write_message(fmt
, args
);
129 static void wpp_error(const char *file
, int line
, int col
, const char *near
,
130 const char *msg
, va_list ap
)
132 wpp_write_message_var("%s:%d:%d: %s: ", file
? file
: "'main file'",
134 wpp_write_message(msg
, ap
);
135 wpp_write_message_var("\n");
138 static void wpp_warning(const char *file
, int line
, int col
, const char *near
,
139 const char *msg
, va_list ap
)
141 wpp_write_message_var("%s:%d:%d: %s: ", file
? file
: "'main file'",
142 line
, col
, "Warning");
143 wpp_write_message(msg
, ap
);
144 wpp_write_message_var("\n");
147 static char *wpp_lookup_mem(const char *filename
, int type
, const char *parent_name
,
148 char **include_path
, int include_path_count
)
150 /* Here we return always ok. We will maybe fail on the next wpp_open_mem */
154 parent_include
= NULL
;
155 if(parent_name
[0] != '\0')
157 for(i
= 0; i
< includes_size
; i
++)
159 if(!strcmp(parent_name
, includes
[i
].name
))
161 parent_include
= includes
[i
].data
;
165 if(parent_include
== NULL
)
167 ERR("Parent include file missing\n");
172 path
= malloc(strlen(filename
) + 1);
174 memcpy(path
, filename
, strlen(filename
) + 1);
178 static void *wpp_open_mem(const char *filename
, int type
)
180 struct mem_file_desc
*desc
;
183 if(!strcmp(filename
, initial_filename
))
185 current_shader
.pos
= 0;
186 return ¤t_shader
;
189 if(current_include
== NULL
) return NULL
;
190 desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*desc
));
193 ERR("Error allocating memory\n");
196 hr
= ID3DInclude_Open(current_include
,
197 type
? D3D_INCLUDE_LOCAL
: D3D_INCLUDE_SYSTEM
,
198 filename
, parent_include
, (LPCVOID
*)&desc
->buffer
,
202 HeapFree(GetProcessHeap(), 0, desc
);
206 if(includes_capacity
== includes_size
)
208 if(includes_capacity
== 0)
210 includes
= HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY
* sizeof(*includes
));
213 ERR("Error allocating memory for the loaded includes structure\n");
216 includes_capacity
= INCLUDES_INITIAL_CAPACITY
* sizeof(*includes
);
220 int newcapacity
= includes_capacity
* 2;
221 struct loaded_include
*newincludes
=
222 HeapReAlloc(GetProcessHeap(), 0, includes
, newcapacity
);
223 if(newincludes
== NULL
)
225 ERR("Error reallocating memory for the loaded includes structure\n");
228 includes
= newincludes
;
229 includes_capacity
= newcapacity
;
232 includes
[includes_size
].name
= filename
;
233 includes
[includes_size
++].data
= desc
->buffer
;
239 ID3DInclude_Close(current_include
, desc
->buffer
);
240 HeapFree(GetProcessHeap(), 0, desc
);
244 static void wpp_close_mem(void *file
)
246 struct mem_file_desc
*desc
= file
;
248 if(desc
!= ¤t_shader
)
251 ID3DInclude_Close(current_include
, desc
->buffer
);
253 ERR("current_include == NULL, desc == %p, buffer = %s\n",
256 HeapFree(GetProcessHeap(), 0, desc
);
260 static int wpp_read_mem(void *file
, char *buffer
, unsigned int len
)
262 struct mem_file_desc
*desc
= file
;
264 len
= min(len
, desc
->size
- desc
->pos
);
265 memcpy(buffer
, desc
->buffer
+ desc
->pos
, len
);
270 static void wpp_write_mem(const char *buffer
, unsigned int len
)
272 char *new_wpp_output
;
274 if(wpp_output_capacity
== 0)
276 wpp_output
= HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY
);
279 ERR("Error allocating memory\n");
282 wpp_output_capacity
= BUFFER_INITIAL_CAPACITY
;
284 if(len
> wpp_output_capacity
- wpp_output_size
)
286 while(len
> wpp_output_capacity
- wpp_output_size
)
288 wpp_output_capacity
*= 2;
290 new_wpp_output
= HeapReAlloc(GetProcessHeap(), 0, wpp_output
,
291 wpp_output_capacity
);
294 ERR("Error allocating memory\n");
297 wpp_output
= new_wpp_output
;
299 memcpy(wpp_output
+ wpp_output_size
, buffer
, len
);
300 wpp_output_size
+= len
;
303 static int wpp_close_output(void)
305 char *new_wpp_output
= HeapReAlloc(GetProcessHeap(), 0, wpp_output
,
306 wpp_output_size
+ 1);
307 if(!new_wpp_output
) return 0;
308 wpp_output
= new_wpp_output
;
309 wpp_output
[wpp_output_size
]='\0';
314 static HRESULT
preprocess_shader(const void *data
, SIZE_T data_size
, const char *filename
,
315 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
, ID3DBlob
**error_messages
)
319 const D3D_SHADER_MACRO
*def
= defines
;
321 static const struct wpp_callbacks wpp_callbacks
=
334 while (def
->Name
!= NULL
)
336 wpp_add_define(def
->Name
, def
->Definition
);
340 current_include
= include
;
343 wpp_output_size
= wpp_output_capacity
= 0;
346 wpp_set_callbacks(&wpp_callbacks
);
347 wpp_messages_size
= wpp_messages_capacity
= 0;
349 current_shader
.buffer
= data
;
350 current_shader
.size
= data_size
;
351 initial_filename
= filename
? filename
: "";
353 ret
= wpp_parse(initial_filename
, NULL
);
354 if (!wpp_close_output())
358 TRACE("Error during shader preprocessing\n");
364 TRACE("Preprocessor messages:\n%s\n", debugstr_a(wpp_messages
));
368 size
= strlen(wpp_messages
) + 1;
369 hr
= D3DCreateBlob(size
, &buffer
);
372 CopyMemory(ID3D10Blob_GetBufferPointer(buffer
), wpp_messages
, size
);
373 *error_messages
= buffer
;
377 TRACE("Shader source:\n%s\n", debugstr_an(data
, data_size
));
382 /* Remove the previously added defines */
385 while (defines
->Name
!= NULL
)
387 wpp_del_define(defines
->Name
);
391 HeapFree(GetProcessHeap(), 0, wpp_messages
);
395 static HRESULT
assemble_shader(const char *preproc_shader
,
396 ID3DBlob
**shader_blob
, ID3DBlob
**error_messages
)
398 struct bwriter_shader
*shader
;
399 char *messages
= NULL
;
405 shader
= SlAssembleShader(preproc_shader
, &messages
);
409 TRACE("Assembler messages:\n");
410 TRACE("%s\n", debugstr_a(messages
));
412 TRACE("Shader source:\n");
413 TRACE("%s\n", debugstr_a(preproc_shader
));
417 const char *preproc_messages
= *error_messages
? ID3D10Blob_GetBufferPointer(*error_messages
) : NULL
;
419 size
= strlen(messages
) + (preproc_messages
? strlen(preproc_messages
) : 0) + 1;
420 hr
= D3DCreateBlob(size
, &buffer
);
423 HeapFree(GetProcessHeap(), 0, messages
);
424 if (shader
) SlDeleteShader(shader
);
427 pos
= ID3D10Blob_GetBufferPointer(buffer
);
428 if (preproc_messages
)
430 CopyMemory(pos
, preproc_messages
, strlen(preproc_messages
) + 1);
431 pos
+= strlen(preproc_messages
);
433 CopyMemory(pos
, messages
, strlen(messages
) + 1);
435 if (*error_messages
) ID3D10Blob_Release(*error_messages
);
436 *error_messages
= buffer
;
438 HeapFree(GetProcessHeap(), 0, messages
);
443 ERR("Asm reading failed\n");
444 return D3DXERR_INVALIDDATA
;
447 hr
= SlWriteBytecode(shader
, 9, &res
, &size
);
448 SlDeleteShader(shader
);
451 ERR("SlWriteBytecode failed with 0x%08x\n", hr
);
452 return D3DXERR_INVALIDDATA
;
457 hr
= D3DCreateBlob(size
, &buffer
);
460 HeapFree(GetProcessHeap(), 0, res
);
463 CopyMemory(ID3D10Blob_GetBufferPointer(buffer
), res
, size
);
464 *shader_blob
= buffer
;
467 HeapFree(GetProcessHeap(), 0, res
);
472 HRESULT WINAPI
D3DAssemble(const void *data
, SIZE_T datasize
, const char *filename
,
473 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
, UINT flags
,
474 ID3DBlob
**shader
, ID3DBlob
**error_messages
)
478 EnterCriticalSection(&wpp_mutex
);
481 if (flags
) FIXME("flags %x\n", flags
);
483 if (shader
) *shader
= NULL
;
484 if (error_messages
) *error_messages
= NULL
;
486 hr
= preprocess_shader(data
, datasize
, filename
, defines
, include
, error_messages
);
488 hr
= assemble_shader(wpp_output
, shader
, error_messages
);
490 HeapFree(GetProcessHeap(), 0, wpp_output
);
491 LeaveCriticalSection(&wpp_mutex
);
495 static HRESULT
compile_shader(const char *preproc_shader
, const char *target
, const char *entrypoint
,
496 ID3DBlob
**shader_blob
, ID3DBlob
**error_messages
)
498 struct bwriter_shader
*shader
;
499 char *messages
= NULL
;
501 DWORD
*res
, size
, major
, minor
;
504 enum shader_type shader_type
;
506 TRACE("Preprocessed shader source: %s\n", debugstr_a(preproc_shader
));
508 TRACE("Parsing compilation target %s.\n", debugstr_a(target
));
509 if (strlen(target
) != 6 || target
[1] != 's' || target
[2] != '_' || target
[4] != '_')
511 FIXME("Unknown compilation target %s.\n", debugstr_a(target
));
512 return D3DERR_INVALIDCALL
;
515 if (target
[0] == 'v')
516 shader_type
= ST_VERTEX
;
517 else if (target
[0] == 'p')
518 shader_type
= ST_PIXEL
;
521 FIXME("Unsupported shader target type %s.\n", debugstr_a(target
));
522 return D3DERR_INVALIDCALL
;
525 major
= target
[3] - '0';
526 if (major
== 0 || major
> 5)
528 FIXME("Unsupported shader target major version %d.\n", major
);
529 return D3DERR_INVALIDCALL
;
531 minor
= target
[5] - '0';
532 if (minor
> 1 || (minor
== 1 && (shader_type
!= ST_VERTEX
|| major
> 1)))
534 FIXME("Unsupported shader target minor version %d.\n", minor
);
535 return D3DERR_INVALIDCALL
;
537 shader
= parse_hlsl_shader(preproc_shader
, shader_type
, major
, minor
, entrypoint
, &messages
);
541 TRACE("Compiler messages:\n");
542 TRACE("%s\n", debugstr_a(messages
));
544 TRACE("Shader source:\n");
545 TRACE("%s\n", debugstr_a(preproc_shader
));
549 const char *preproc_messages
= *error_messages
? ID3D10Blob_GetBufferPointer(*error_messages
) : NULL
;
551 size
= strlen(messages
) + (preproc_messages
? strlen(preproc_messages
) : 0) + 1;
552 hr
= D3DCreateBlob(size
, &buffer
);
555 HeapFree(GetProcessHeap(), 0, messages
);
556 if (shader
) SlDeleteShader(shader
);
559 pos
= ID3D10Blob_GetBufferPointer(buffer
);
560 if (preproc_messages
)
562 memcpy(pos
, preproc_messages
, strlen(preproc_messages
) + 1);
563 pos
+= strlen(preproc_messages
);
565 memcpy(pos
, messages
, strlen(messages
) + 1);
567 if (*error_messages
) ID3D10Blob_Release(*error_messages
);
568 *error_messages
= buffer
;
570 HeapFree(GetProcessHeap(), 0, messages
);
575 ERR("HLSL shader parsing failed.\n");
576 return D3DXERR_INVALIDDATA
;
579 hr
= SlWriteBytecode(shader
, 9, &res
, &size
);
580 SlDeleteShader(shader
);
583 ERR("SlWriteBytecode failed with error 0x%08x.\n", hr
);
584 return D3DXERR_INVALIDDATA
;
589 hr
= D3DCreateBlob(size
, &buffer
);
592 HeapFree(GetProcessHeap(), 0, res
);
595 memcpy(ID3D10Blob_GetBufferPointer(buffer
), res
, size
);
596 *shader_blob
= buffer
;
599 HeapFree(GetProcessHeap(), 0, res
);
604 HRESULT WINAPI
D3DCompile(const void *data
, SIZE_T data_size
, const char *filename
,
605 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
, const char *entrypoint
,
606 const char *target
, UINT sflags
, UINT eflags
, ID3DBlob
**shader
, ID3DBlob
**error_messages
)
610 FIXME("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s,\n"
611 "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p stub!\n",
612 data
, data_size
, debugstr_a(filename
), defines
, include
, debugstr_a(entrypoint
),
613 debugstr_a(target
), sflags
, eflags
, shader
, error_messages
);
615 if (shader
) *shader
= NULL
;
616 if (error_messages
) *error_messages
= NULL
;
618 EnterCriticalSection(&wpp_mutex
);
620 hr
= preprocess_shader(data
, data_size
, filename
, defines
, include
, error_messages
);
622 hr
= compile_shader(wpp_output
, target
, entrypoint
, shader
, error_messages
);
624 HeapFree(GetProcessHeap(), 0, wpp_output
);
625 LeaveCriticalSection(&wpp_mutex
);
629 HRESULT WINAPI
D3DPreprocess(const void *data
, SIZE_T size
, const char *filename
,
630 const D3D_SHADER_MACRO
*defines
, ID3DInclude
*include
,
631 ID3DBlob
**shader
, ID3DBlob
**error_messages
)
639 EnterCriticalSection(&wpp_mutex
);
641 if (shader
) *shader
= NULL
;
642 if (error_messages
) *error_messages
= NULL
;
644 hr
= preprocess_shader(data
, size
, filename
, defines
, include
, error_messages
);
650 hr
= D3DCreateBlob(wpp_output_size
, &buffer
);
653 CopyMemory(ID3D10Blob_GetBufferPointer(buffer
), wpp_output
, wpp_output_size
);
661 HeapFree(GetProcessHeap(), 0, wpp_output
);
662 LeaveCriticalSection(&wpp_mutex
);