d3dcompiler: D3DPreprocess implementation.
[wine] / dlls / 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 #define COBJMACROS
21 #include "config.h"
22 #include "wine/port.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
25
26 #include "d3dcompiler_private.h"
27 #include "wine/wpp.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
30
31 #define D3DXERR_INVALIDDATA                      0x88760b59
32
33 #define BUFFER_INITIAL_CAPACITY 256
34
35 struct mem_file_desc
36 {
37     const char *buffer;
38     unsigned int size;
39     unsigned int pos;
40 };
41
42 struct mem_file_desc current_shader;
43 LPD3DINCLUDE current_include;
44
45 #define INCLUDES_INITIAL_CAPACITY 4
46
47 struct loaded_include
48 {
49     const char *name;
50     const char *data;
51 };
52
53 struct loaded_include *includes;
54 int includes_capacity, includes_size;
55 const char *parent_include;
56
57 char *wpp_output;
58 int wpp_output_capacity, wpp_output_size;
59
60 char *wpp_messages;
61 int wpp_messages_capacity, wpp_messages_size;
62
63 /* Mutex used to guarantee a single invocation
64    of the D3DXAssembleShader function (or its variants) at a time.
65    This is needed as wpp isn't thread-safe */
66 static CRITICAL_SECTION wpp_mutex;
67 static CRITICAL_SECTION_DEBUG wpp_mutex_debug =
68 {
69     0, 0, &wpp_mutex,
70     { &wpp_mutex_debug.ProcessLocksList,
71       &wpp_mutex_debug.ProcessLocksList },
72       0, 0, { (DWORD_PTR)(__FILE__ ": wpp_mutex") }
73 };
74 static CRITICAL_SECTION wpp_mutex = { &wpp_mutex_debug, -1, 0, 0, 0, 0 };
75
76 /* Preprocessor error reporting functions */
77 static void wpp_write_message(const char *fmt, va_list args)
78 {
79     char* newbuffer;
80     int rc, newsize;
81
82     if(wpp_messages_capacity == 0)
83     {
84         wpp_messages = HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE);
85         if(wpp_messages == NULL)
86         {
87             ERR("Error allocating memory for parser messages\n");
88             return;
89         }
90         wpp_messages_capacity = MESSAGEBUFFER_INITIAL_SIZE;
91     }
92
93     while(1)
94     {
95         rc = vsnprintf(wpp_messages + wpp_messages_size,
96                        wpp_messages_capacity - wpp_messages_size, fmt, args);
97
98         if (rc < 0 ||                                           /* C89 */
99             rc >= wpp_messages_capacity - wpp_messages_size) {  /* C99 */
100             /* Resize the buffer */
101             newsize = wpp_messages_capacity * 2;
102             newbuffer = HeapReAlloc(GetProcessHeap(), 0, wpp_messages, newsize);
103             if(newbuffer == NULL)
104             {
105                 ERR("Error reallocating memory for parser messages\n");
106                 return;
107             }
108             wpp_messages = newbuffer;
109             wpp_messages_capacity = newsize;
110         }
111         else
112         {
113             wpp_messages_size += rc;
114             return;
115         }
116     }
117 }
118
119 static void PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt, ...)
120 {
121     va_list args;
122
123     va_start(args, fmt);
124     wpp_write_message(fmt, args);
125     va_end(args);
126 }
127
128 static void wpp_error(const char *file, int line, int col, const char *near,
129                       const char *msg, va_list ap)
130 {
131     wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
132                           line, col, "Error");
133     wpp_write_message(msg, ap);
134     wpp_write_message_var("\n");
135 }
136
137 static void wpp_warning(const char *file, int line, int col, const char *near,
138                         const char *msg, va_list ap)
139 {
140     wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
141                           line, col, "Warning");
142     wpp_write_message(msg, ap);
143     wpp_write_message_var("\n");
144 }
145
146 static char *wpp_lookup_mem(const char *filename, const char *parent_name,
147                             char **include_path, int include_path_count)
148 {
149     /* Here we return always ok. We will maybe fail on the next wpp_open_mem */
150     char *path;
151     int i;
152
153     parent_include = NULL;
154     if(parent_name[0] != '\0')
155     {
156         for(i = 0; i < includes_size; i++)
157         {
158             if(!strcmp(parent_name, includes[i].name))
159             {
160                 parent_include = includes[i].data;
161                 break;
162             }
163         }
164         if(parent_include == NULL)
165         {
166             ERR("Parent include file missing\n");
167             return NULL;
168         }
169     }
170
171     path = malloc(strlen(filename) + 1);
172     if(path)
173         memcpy(path, filename, strlen(filename) + 1);
174     return path;
175 }
176
177 static void *wpp_open_mem(const char *filename, int type)
178 {
179     struct mem_file_desc *desc;
180     HRESULT hr;
181
182     if(filename[0] == '\0') /* "" means to load the initial shader */
183     {
184         current_shader.pos = 0;
185         return &current_shader;
186     }
187
188     if(current_include == NULL) return NULL;
189     desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
190     if(!desc)
191     {
192         ERR("Error allocating memory\n");
193         return NULL;
194     }
195     hr = ID3DInclude_Open(current_include,
196                           type ? D3D_INCLUDE_SYSTEM : D3D_INCLUDE_LOCAL,
197                           filename, parent_include, (LPCVOID *)&desc->buffer,
198                           &desc->size);
199     if(FAILED(hr))
200     {
201         HeapFree(GetProcessHeap(), 0, desc);
202         return NULL;
203     }
204
205     if(includes_capacity == includes_size)
206     {
207         if(includes_capacity == 0)
208         {
209             includes = HeapAlloc(GetProcessHeap(), 0, INCLUDES_INITIAL_CAPACITY);
210             if(includes == NULL)
211             {
212                 ERR("Error allocating memory for the loaded includes structure\n");
213                 goto error;
214             }
215             includes_capacity = INCLUDES_INITIAL_CAPACITY;
216         }
217         else
218         {
219             int newcapacity = includes_capacity * 2;
220             struct loaded_include *newincludes =
221                 HeapReAlloc(GetProcessHeap(), 0, includes, newcapacity);
222             if(newincludes == NULL)
223             {
224                 ERR("Error reallocating memory for the loaded includes structure\n");
225                 goto error;
226             }
227             includes = newincludes;
228             includes_capacity = newcapacity;
229         }
230     }
231     includes[includes_size].name = filename;
232     includes[includes_size++].data = desc->buffer;
233
234     desc->pos = 0;
235     return desc;
236
237 error:
238     ID3DInclude_Close(current_include, desc->buffer);
239     HeapFree(GetProcessHeap(), 0, desc);
240     return NULL;
241 }
242
243 static void wpp_close_mem(void *file)
244 {
245     struct mem_file_desc *desc = file;
246
247     if(desc != &current_shader)
248     {
249         if(current_include)
250             ID3DInclude_Close(current_include, desc->buffer);
251         else
252             ERR("current_include == NULL, desc == %p, buffer = %s\n",
253                 desc, desc->buffer);
254
255         HeapFree(GetProcessHeap(), 0, desc);
256     }
257 }
258
259 static int wpp_read_mem(void *file, char *buffer, unsigned int len)
260 {
261     struct mem_file_desc *desc = file;
262
263     len = min(len, desc->size - desc->pos);
264     memcpy(buffer, desc->buffer + desc->pos, len);
265     desc->pos += len;
266     return len;
267 }
268
269 static void wpp_write_mem(const char *buffer, unsigned int len)
270 {
271     char *new_wpp_output;
272
273     if(wpp_output_capacity == 0)
274     {
275         wpp_output = HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY);
276         if(!wpp_output)
277         {
278             ERR("Error allocating memory\n");
279             return;
280         }
281         wpp_output_capacity = BUFFER_INITIAL_CAPACITY;
282     }
283     if(len > wpp_output_capacity - wpp_output_size)
284     {
285         while(len > wpp_output_capacity - wpp_output_size)
286         {
287             wpp_output_capacity *= 2;
288         }
289         new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
290                                      wpp_output_capacity);
291         if(!new_wpp_output)
292         {
293             ERR("Error allocating memory\n");
294             return;
295         }
296         wpp_output = new_wpp_output;
297     }
298     memcpy(wpp_output + wpp_output_size, buffer, len);
299     wpp_output_size += len;
300 }
301
302 static int wpp_close_output(void)
303 {
304     char *new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
305                                        wpp_output_size + 1);
306     if(!new_wpp_output) return 0;
307     wpp_output = new_wpp_output;
308     wpp_output[wpp_output_size]='\0';
309     return 1;
310 }
311
312 static HRESULT preprocess_shader(const void *data, SIZE_T data_size,
313         const D3D_SHADER_MACRO *defines, ID3DInclude *include, ID3DBlob **error_messages)
314 {
315     int ret;
316     HRESULT hr = S_OK;
317     const D3D_SHADER_MACRO *def = defines;
318
319     static const struct wpp_callbacks wpp_callbacks =
320     {
321         wpp_lookup_mem,
322         wpp_open_mem,
323         wpp_close_mem,
324         wpp_read_mem,
325         wpp_write_mem,
326         wpp_error,
327         wpp_warning,
328     };
329
330     if (def != NULL)
331     {
332         while (def->Name != NULL)
333         {
334             wpp_add_define(def->Name, def->Definition);
335             def++;
336         }
337     }
338     current_include = include;
339     includes_size = 0;
340
341     wpp_output_size = wpp_output_capacity = 0;
342     wpp_output = NULL;
343
344     wpp_set_callbacks(&wpp_callbacks);
345     wpp_messages_size = wpp_messages_capacity = 0;
346     wpp_messages = NULL;
347     current_shader.buffer = data;
348     current_shader.size = data_size;
349
350     ret = wpp_parse("", NULL);
351     if (!wpp_close_output())
352         ret = 1;
353     if (ret)
354     {
355         TRACE("Error during shader preprocessing\n");
356         if (wpp_messages)
357         {
358             int size;
359             ID3DBlob *buffer;
360
361             TRACE("Preprocessor messages:\n%s", wpp_messages);
362
363             if (error_messages)
364             {
365                 size = strlen(wpp_messages) + 1;
366                 hr = D3DCreateBlob(size, &buffer);
367                 if (FAILED(hr))
368                     goto cleanup;
369                 CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_messages, size);
370                 *error_messages = buffer;
371             }
372         }
373         if (data)
374             TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
375         hr = E_FAIL;
376     }
377
378 cleanup:
379     /* Remove the previously added defines */
380     if (defines != NULL)
381     {
382         while (defines->Name != NULL)
383         {
384             wpp_del_define(defines->Name);
385             defines++;
386         }
387     }
388     HeapFree(GetProcessHeap(), 0, wpp_messages);
389     return hr;
390 }
391
392 static HRESULT assemble_shader(const char *preprocShader, const char *preprocMessages,
393                                LPD3DBLOB* ppShader, LPD3DBLOB* ppErrorMsgs)
394 {
395     struct bwriter_shader *shader;
396     char *messages = NULL;
397     HRESULT hr;
398     DWORD *res;
399     LPD3DBLOB buffer;
400     int size;
401     char *pos;
402
403     shader = SlAssembleShader(preprocShader, &messages);
404
405     if(messages || preprocMessages)
406     {
407         if(preprocMessages)
408         {
409             TRACE("Preprocessor messages:\n");
410             TRACE("%s", preprocMessages);
411         }
412         if(messages)
413         {
414             TRACE("Assembler messages:\n");
415             TRACE("%s", messages);
416         }
417
418         TRACE("Shader source:\n");
419         TRACE("%s\n", debugstr_a(preprocShader));
420
421         if(ppErrorMsgs)
422         {
423             size = (messages ? strlen(messages) : 0) +
424                 (preprocMessages ? strlen(preprocMessages) : 0) + 1;
425             hr = D3DCreateBlob(size, &buffer);
426             if(FAILED(hr))
427             {
428                 HeapFree(GetProcessHeap(), 0, messages);
429                 if(shader) SlDeleteShader(shader);
430                 return hr;
431             }
432             pos = ID3D10Blob_GetBufferPointer(buffer);
433             if(preprocMessages)
434             {
435                 CopyMemory(pos, preprocMessages, strlen(preprocMessages) + 1);
436                 pos += strlen(preprocMessages);
437             }
438             if(messages)
439                 CopyMemory(pos, messages, strlen(messages) + 1);
440
441             *ppErrorMsgs = buffer;
442         }
443
444         HeapFree(GetProcessHeap(), 0, messages);
445     }
446
447     if(shader == NULL)
448     {
449         ERR("Asm reading failed\n");
450         return D3DXERR_INVALIDDATA;
451     }
452
453     hr = SlWriteBytecode(shader, 9, &res);
454     SlDeleteShader(shader);
455     if(FAILED(hr))
456     {
457         ERR("SlWriteBytecode failed with 0x%08x\n", hr);
458         return D3DXERR_INVALIDDATA;
459     }
460
461     if(ppShader)
462     {
463         size = HeapSize(GetProcessHeap(), 0, res);
464         hr = D3DCreateBlob(size, &buffer);
465         if(FAILED(hr))
466         {
467             HeapFree(GetProcessHeap(), 0, res);
468             return hr;
469         }
470         CopyMemory(ID3D10Blob_GetBufferPointer(buffer), res, size);
471         *ppShader = buffer;
472     }
473
474     HeapFree(GetProcessHeap(), 0, res);
475
476     return S_OK;
477 }
478
479 HRESULT WINAPI D3DAssemble(const void *data, SIZE_T datasize, const char *filename,
480         const D3D_SHADER_MACRO *defines, ID3DInclude *include, UINT flags,
481         ID3DBlob **shader, ID3DBlob **error_messages)
482 {
483     HRESULT hr;
484
485     EnterCriticalSection(&wpp_mutex);
486
487     /* TODO: flags */
488     if (flags) FIXME("flags %x\n", flags);
489
490     if (shader) *shader = NULL;
491     if (error_messages) *error_messages = NULL;
492
493     hr = preprocess_shader(data, datasize, defines, include, error_messages);
494     if (SUCCEEDED(hr))
495         hr = assemble_shader(wpp_output, wpp_messages, shader, error_messages);
496
497     HeapFree(GetProcessHeap(), 0, wpp_output);
498     LeaveCriticalSection(&wpp_mutex);
499     return hr;
500 }
501
502 HRESULT WINAPI D3DCompile(const void *data, SIZE_T data_size, const char *filename,
503         const D3D_SHADER_MACRO *defines, ID3DInclude *include, const char *entrypoint,
504         const char *target, UINT sflags, UINT eflags, ID3DBlob **shader, ID3DBlob **error_messages)
505 {
506     FIXME("data %p, data_size %lu, filename %s, defines %p, include %p, entrypoint %s,\n"
507             "target %s, sflags %#x, eflags %#x, shader %p, error_messages %p stub!\n",
508             data, data_size, debugstr_a(filename), defines, include, debugstr_a(entrypoint),
509             debugstr_a(target), sflags, eflags, shader, error_messages);
510
511     TRACE("Shader source:\n%s\n", debugstr_an(data, data_size));
512
513     if (error_messages)
514         D3DCreateBlob(1, error_messages); /* zero fill used as string end */
515
516     return D3DERR_INVALIDCALL;
517 }
518
519 HRESULT WINAPI D3DPreprocess(const void *data, SIZE_T size, const char *filename,
520         const D3D_SHADER_MACRO *defines, ID3DInclude *include,
521         ID3DBlob **shader, ID3DBlob **error_messages)
522 {
523     HRESULT hr;
524     ID3DBlob *buffer;
525
526     if (!data)
527         return E_INVALIDARG;
528
529     EnterCriticalSection(&wpp_mutex);
530
531     if (shader) *shader = NULL;
532     if (error_messages) *error_messages = NULL;
533
534     hr = preprocess_shader(data, size, defines, include, error_messages);
535
536     if (SUCCEEDED(hr))
537     {
538         if (shader)
539         {
540             hr = D3DCreateBlob(wpp_output_size, &buffer);
541             if (FAILED(hr))
542                 goto cleanup;
543             CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_output, wpp_output_size);
544             *shader = buffer;
545         }
546         else
547             hr = E_INVALIDARG;
548     }
549
550 cleanup:
551     HeapFree(GetProcessHeap(), 0, wpp_output);
552     LeaveCriticalSection(&wpp_mutex);
553     return hr;
554 }