wined3d: Avoid a compiler warning on old gcc.
[wine] / dlls / d3dx9_36 / shader.c
1 /*
2  * Copyright 2008 Luis Busquets
3  * Copyright 2009 Matteo Bruni
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 "config.h"
21 #include "wine/port.h"
22 #include "wine/debug.h"
23 #include "wine/unicode.h"
24 #include "windef.h"
25 #include "wingdi.h"
26 #include "wine/wpp.h"
27 #include "d3dx9_36_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30
31 LPCSTR WINAPI D3DXGetPixelShaderProfile(LPDIRECT3DDEVICE9 device)
32 {
33     D3DCAPS9 caps;
34
35     TRACE("device %p\n", device);
36
37     if (!device) return NULL;
38
39     IDirect3DDevice9_GetDeviceCaps(device,&caps);
40
41     switch (caps.PixelShaderVersion)
42     {
43     case D3DPS_VERSION(1, 1):
44         return "ps_1_1";
45
46     case D3DPS_VERSION(1, 2):
47         return "ps_1_2";
48
49     case D3DPS_VERSION(1, 3):
50         return "ps_1_3";
51
52     case D3DPS_VERSION(1, 4):
53         return "ps_1_4";
54
55     case D3DPS_VERSION(2, 0):
56         if ((caps.PS20Caps.NumTemps>=22)                          &&
57             (caps.PS20Caps.Caps&D3DPS20CAPS_ARBITRARYSWIZZLE)     &&
58             (caps.PS20Caps.Caps&D3DPS20CAPS_GRADIENTINSTRUCTIONS) &&
59             (caps.PS20Caps.Caps&D3DPS20CAPS_PREDICATION)          &&
60             (caps.PS20Caps.Caps&D3DPS20CAPS_NODEPENDENTREADLIMIT) &&
61             (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
62         {
63             return "ps_2_a";
64         }
65         if ((caps.PS20Caps.NumTemps>=32)                          &&
66             (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
67         {
68             return "ps_2_b";
69         }
70         return "ps_2_0";
71
72     case D3DPS_VERSION(3, 0):
73         return "ps_3_0";
74     }
75
76     return NULL;
77 }
78
79 UINT WINAPI D3DXGetShaderSize(const DWORD *byte_code)
80 {
81     const DWORD *ptr = byte_code;
82
83     TRACE("byte_code %p\n", byte_code);
84
85     if (!ptr) return 0;
86
87     /* Look for the END token, skipping the VERSION token */
88     while (*++ptr != D3DSIO_END)
89     {
90         /* Skip comments */
91         if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
92         {
93             ptr += ((*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT);
94         }
95     }
96     ++ptr;
97
98     /* Return the shader size in bytes */
99     return (ptr - byte_code) * sizeof(*ptr);
100 }
101
102 DWORD WINAPI D3DXGetShaderVersion(const DWORD *byte_code)
103 {
104     TRACE("byte_code %p\n", byte_code);
105
106     return byte_code ? *byte_code : 0;
107 }
108
109 LPCSTR WINAPI D3DXGetVertexShaderProfile(LPDIRECT3DDEVICE9 device)
110 {
111     D3DCAPS9 caps;
112
113     TRACE("device %p\n", device);
114
115     if (!device) return NULL;
116
117     IDirect3DDevice9_GetDeviceCaps(device,&caps);
118
119     switch (caps.VertexShaderVersion)
120     {
121     case D3DVS_VERSION(1, 1):
122         return "vs_1_1";
123     case D3DVS_VERSION(2, 0):
124         if ((caps.VS20Caps.NumTemps>=13) &&
125             (caps.VS20Caps.DynamicFlowControlDepth==24) &&
126             (caps.VS20Caps.Caps&D3DPS20CAPS_PREDICATION))
127         {
128             return "vs_2_a";
129         }
130         return "vs_2_0";
131     case D3DVS_VERSION(3, 0):
132         return "vs_3_0";
133     }
134
135     return NULL;
136 }
137
138 HRESULT WINAPI D3DXFindShaderComment(CONST DWORD* byte_code, DWORD fourcc, LPCVOID* data, UINT* size)
139 {
140     CONST DWORD *ptr = byte_code;
141
142     TRACE("(%p, %x, %p, %p)\n", byte_code, fourcc, data, size);
143
144     if (!byte_code)
145         return D3DERR_INVALIDCALL;
146
147     while (*++ptr != D3DSIO_END)
148     {
149         /* Check if it is a comment */
150         if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
151         {
152             DWORD comment_size = (*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
153
154             /* Check if this is the comment we are looking for */
155             if (*(ptr + 1) == fourcc)
156             {
157                 UINT ctab_size = (comment_size - 1) * sizeof(DWORD);
158                 LPCVOID ctab_data = ptr + 2;
159                 if (size)
160                     *size = ctab_size;
161                 if (data)
162                     *data = ctab_data;
163                 TRACE("Returning comment data at %p with size %d\n", ctab_data, ctab_size);
164                 return D3D_OK;
165             }
166             ptr += comment_size;
167         }
168     }
169
170     return S_FALSE;
171 }
172
173 #define BUFFER_INITIAL_CAPACITY 256
174
175 struct mem_file_desc
176 {
177     const char *buffer;
178     unsigned int size;
179     unsigned int pos;
180 };
181
182 struct mem_file_desc current_shader;
183 LPD3DXINCLUDE current_include;
184 char *wpp_output;
185 int wpp_output_capacity, wpp_output_size;
186
187 char *wpp_messages;
188 int wpp_messages_capacity, wpp_messages_size;
189
190 /* Mutex used to guarantee a single invocation
191    of the D3DXAssembleShader function (or its variants) at a time.
192    This is needed as wpp isn't thread-safe */
193 static CRITICAL_SECTION wpp_mutex;
194 static CRITICAL_SECTION_DEBUG wpp_mutex_debug =
195 {
196     0, 0, &wpp_mutex,
197     { &wpp_mutex_debug.ProcessLocksList,
198       &wpp_mutex_debug.ProcessLocksList },
199       0, 0, { (DWORD_PTR)(__FILE__ ": wpp_mutex") }
200 };
201 static CRITICAL_SECTION wpp_mutex = { &wpp_mutex_debug, -1, 0, 0, 0, 0 };
202
203 /* Preprocessor error reporting functions */
204 static void wpp_write_message(const char *fmt, va_list args)
205 {
206     char* newbuffer;
207     int rc, newsize;
208
209     if(wpp_messages_capacity == 0)
210     {
211         wpp_messages = HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE);
212         if(wpp_messages == NULL)
213         {
214             ERR("Error allocating memory for parser messages\n");
215             return;
216         }
217         wpp_messages_capacity = MESSAGEBUFFER_INITIAL_SIZE;
218     }
219
220     while(1)
221     {
222         rc = vsnprintf(wpp_messages + wpp_messages_size,
223                        wpp_messages_capacity - wpp_messages_size, fmt, args);
224
225         if (rc < 0 ||                                           /* C89 */
226             rc >= wpp_messages_capacity - wpp_messages_size) {  /* C99 */
227             /* Resize the buffer */
228             newsize = wpp_messages_capacity * 2;
229             newbuffer = HeapReAlloc(GetProcessHeap(), 0, wpp_messages, newsize);
230             if(newbuffer == NULL)
231             {
232                 ERR("Error reallocating memory for parser messages\n");
233                 return;
234             }
235             wpp_messages = newbuffer;
236             wpp_messages_capacity = newsize;
237         }
238         else
239         {
240             wpp_messages_size += rc;
241             return;
242         }
243     }
244 }
245
246 static void PRINTF_ATTR(1,2) wpp_write_message_var(const char *fmt, ...)
247 {
248     va_list args;
249
250     va_start(args, fmt);
251     wpp_write_message(fmt, args);
252     va_end(args);
253 }
254
255 static void wpp_error(const char *file, int line, int col, const char *near,
256                       const char *msg, va_list ap)
257 {
258     wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
259                           line, col, "Error");
260     wpp_write_message(msg, ap);
261     wpp_write_message_var("\n");
262 }
263
264 static void wpp_warning(const char *file, int line, int col, const char *near,
265                         const char *msg, va_list ap)
266 {
267     wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
268                           line, col, "Warning");
269     wpp_write_message(msg, ap);
270     wpp_write_message_var("\n");
271 }
272
273 static char *wpp_lookup_mem(const char *filename, const char *parent_name,
274                             char **include_path, int include_path_count)
275 {
276     /* Here we return always ok. We will maybe fail on the next wpp_open_mem */
277     char *path;
278
279     path = malloc(strlen(filename) + 1);
280     if(!path) return NULL;
281     memcpy(path, filename, strlen(filename) + 1);
282     return path;
283 }
284
285 static void *wpp_open_mem(const char *filename, int type)
286 {
287     struct mem_file_desc *desc;
288     HRESULT hr;
289
290     if(filename[0] == '\0') /* "" means to load the initial shader */
291     {
292         current_shader.pos = 0;
293         return &current_shader;
294     }
295
296     if(current_include == NULL) return NULL;
297     desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
298     if(!desc)
299     {
300         ERR("Error allocating memory\n");
301         return NULL;
302     }
303     hr = ID3DXInclude_Open(current_include,
304                            type ? D3DXINC_SYSTEM : D3DXINC_LOCAL,
305                            filename, NULL, (LPCVOID *)&desc->buffer,
306                            &desc->size);
307     if(FAILED(hr))
308     {
309         HeapFree(GetProcessHeap(), 0, desc);
310         return NULL;
311     }
312     desc->pos = 0;
313     return desc;
314 }
315
316 static void wpp_close_mem(void *file)
317 {
318     struct mem_file_desc *desc = file;
319
320     if(desc != &current_shader)
321     {
322         if(current_include)
323             ID3DXInclude_Close(current_include, desc->buffer);
324         else
325             ERR("current_include == NULL, desc == %p, buffer = %s\n",
326                 desc, desc->buffer);
327
328         HeapFree(GetProcessHeap(), 0, desc);
329     }
330 }
331
332 static int wpp_read_mem(void *file, char *buffer, unsigned int len)
333 {
334     struct mem_file_desc *desc = file;
335
336     len = min(len, desc->size - desc->pos);
337     memcpy(buffer, desc->buffer + desc->pos, len);
338     desc->pos += len;
339     return len;
340 }
341
342 static void wpp_write_mem(const char *buffer, unsigned int len)
343 {
344     char *new_wpp_output;
345
346     if(wpp_output_capacity == 0)
347     {
348         wpp_output = HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY);
349         if(!wpp_output)
350         {
351             ERR("Error allocating memory\n");
352             return;
353         }
354         wpp_output_capacity = BUFFER_INITIAL_CAPACITY;
355     }
356     if(len > wpp_output_capacity - wpp_output_size)
357     {
358         while(len > wpp_output_capacity - wpp_output_size)
359         {
360             wpp_output_capacity *= 2;
361         }
362         new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
363                                      wpp_output_capacity);
364         if(!new_wpp_output)
365         {
366             ERR("Error allocating memory\n");
367             return;
368         }
369         wpp_output = new_wpp_output;
370     }
371     memcpy(wpp_output + wpp_output_size, buffer, len);
372     wpp_output_size += len;
373 }
374
375 static int wpp_close_output(void)
376 {
377     char *new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
378                                        wpp_output_size + 1);
379     if(!new_wpp_output) return 0;
380     wpp_output = new_wpp_output;
381     wpp_output[wpp_output_size]='\0';
382     return 1;
383 }
384
385 HRESULT assemble_shader(const char *preprocShader, const char *preprocMessages,
386                         LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs)
387 {
388     struct bwriter_shader *shader;
389     char *messages = NULL;
390     HRESULT hr;
391     DWORD *res;
392     LPD3DXBUFFER buffer;
393     int size;
394     char *pos;
395
396     shader = SlAssembleShader(preprocShader, &messages);
397
398     if(messages || preprocMessages)
399     {
400         if(preprocMessages)
401         {
402             TRACE("Preprocessor messages:\n");
403             TRACE("%s", preprocMessages);
404         }
405         if(messages)
406         {
407             TRACE("Assembler messages:\n");
408             TRACE("%s", messages);
409         }
410
411         TRACE("Shader source:\n");
412         TRACE("%s\n", debugstr_a(preprocShader));
413
414         size = (messages ? strlen(messages) : 0) +
415             (preprocMessages ? strlen(preprocMessages) : 0) + 1;
416         hr = D3DXCreateBuffer(size, &buffer);
417         if(FAILED(hr))
418         {
419             HeapFree(GetProcessHeap(), 0, messages);
420             if(shader) SlDeleteShader(shader);
421             return hr;
422         }
423         pos = ID3DXBuffer_GetBufferPointer(buffer);
424         if(preprocMessages)
425         {
426             CopyMemory(pos, preprocMessages, strlen(preprocMessages) + 1);
427             pos += strlen(preprocMessages);
428         }
429         if(messages)
430             CopyMemory(pos, messages, strlen(messages) + 1);
431
432         *ppErrorMsgs = buffer;
433
434         HeapFree(GetProcessHeap(), 0, messages);
435     }
436
437     if(shader == NULL)
438     {
439         ERR("Asm reading failed\n");
440         return D3DXERR_INVALIDDATA;
441     }
442
443     hr = SlWriteBytecode(shader, 9, &res);
444     SlDeleteShader(shader);
445     if(FAILED(hr))
446     {
447         ERR("SlWriteBytecode failed with 0x%08x\n", hr);
448         return D3DXERR_INVALIDDATA;
449     }
450
451     size = HeapSize(GetProcessHeap(), 0, res);
452     hr = D3DXCreateBuffer(size, &buffer);
453     if(FAILED(hr))
454     {
455         HeapFree(GetProcessHeap(), 0, res);
456         return hr;
457     }
458     CopyMemory(ID3DXBuffer_GetBufferPointer(buffer), res, size);
459     *ppShader = buffer;
460
461     HeapFree(GetProcessHeap(), 0, res);
462
463     return D3D_OK;
464 }
465
466 HRESULT WINAPI D3DXAssembleShader(LPCSTR data,
467                                   UINT data_len,
468                                   CONST D3DXMACRO* defines,
469                                   LPD3DXINCLUDE include,
470                                   DWORD flags,
471                                   LPD3DXBUFFER* shader,
472                                   LPD3DXBUFFER* error_messages)
473 {
474     int ret;
475     HRESULT hr;
476     CONST D3DXMACRO* def = defines;
477
478     static const struct wpp_callbacks wpp_callbacks = {
479         wpp_lookup_mem,
480         wpp_open_mem,
481         wpp_close_mem,
482         wpp_read_mem,
483         wpp_write_mem,
484         wpp_error,
485         wpp_warning,
486     };
487
488     EnterCriticalSection(&wpp_mutex);
489
490     /* TODO: flags */
491     if(flags) FIXME("flags: %x\n", flags);
492
493     if(def != NULL)
494     {
495         while(def->Name != NULL)
496         {
497             wpp_add_define(def->Name, def->Definition);
498             def++;
499         }
500     }
501     current_include = include;
502
503     *shader = *error_messages = NULL;
504     wpp_output_size = wpp_output_capacity = 0;
505     wpp_output = NULL;
506
507     /* Preprocess shader */
508     wpp_set_callbacks(&wpp_callbacks);
509     wpp_messages_size = wpp_messages_capacity = 0;
510     wpp_messages = NULL;
511     current_shader.buffer = data;
512     current_shader.size = data_len;
513
514     ret = wpp_parse("", NULL);
515     if(!wpp_close_output())
516         ret = 1;
517     if(ret)
518     {
519         TRACE("Error during shader preprocessing\n");
520         if(wpp_messages)
521         {
522             int size;
523             LPD3DXBUFFER buffer;
524
525             TRACE("Preprocessor messages:\n");
526             TRACE("%s", wpp_messages);
527
528             size = strlen(wpp_messages) + 1;
529             hr = D3DXCreateBuffer(size, &buffer);
530             if(FAILED(hr)) goto cleanup;
531             CopyMemory(ID3DXBuffer_GetBufferPointer(buffer), wpp_messages, size);
532             *error_messages = buffer;
533         }
534         if(data)
535         {
536             TRACE("Shader source:\n");
537             TRACE("%s\n", debugstr_an(data, data_len));
538         }
539         hr = D3DXERR_INVALIDDATA;
540         goto cleanup;
541     }
542
543     hr = assemble_shader(wpp_output, wpp_messages, shader, error_messages);
544
545 cleanup:
546     /* Remove the previously added defines */
547     if(defines != NULL)
548     {
549         while(defines->Name != NULL)
550         {
551             wpp_del_define(defines->Name);
552             defines++;
553         }
554     }
555     HeapFree(GetProcessHeap(), 0, wpp_messages);
556     HeapFree(GetProcessHeap(), 0, wpp_output);
557     LeaveCriticalSection(&wpp_mutex);
558     return hr;
559 }
560
561 HRESULT WINAPI D3DXAssembleShaderFromFileA(LPCSTR filename,
562                                            CONST D3DXMACRO* defines,
563                                            LPD3DXINCLUDE include,
564                                            DWORD flags,
565                                            LPD3DXBUFFER* shader,
566                                            LPD3DXBUFFER* error_messages)
567 {
568     LPWSTR filename_w = NULL;
569     DWORD len;
570     HRESULT ret;
571
572     if (!filename) return D3DXERR_INVALIDDATA;
573
574     len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
575     filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
576     if (!filename_w) return E_OUTOFMEMORY;
577     MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
578
579     ret = D3DXAssembleShaderFromFileW(filename_w, defines, include, flags, shader, error_messages);
580
581     HeapFree(GetProcessHeap(), 0, filename_w);
582     return ret;
583 }
584
585 HRESULT WINAPI D3DXAssembleShaderFromFileW(LPCWSTR filename,
586                                            CONST D3DXMACRO* defines,
587                                            LPD3DXINCLUDE include,
588                                            DWORD flags,
589                                            LPD3DXBUFFER* shader,
590                                            LPD3DXBUFFER* error_messages)
591 {
592     FIXME("(%s, %p, %p, %x, %p, %p): stub\n", debugstr_w(filename), defines, include, flags, shader, error_messages);
593     return D3DERR_INVALIDCALL;
594 }
595
596 HRESULT WINAPI D3DXAssembleShaderFromResourceA(HMODULE module,
597                                                LPCSTR resource,
598                                                CONST D3DXMACRO* defines,
599                                                LPD3DXINCLUDE include,
600                                                DWORD flags,
601                                                LPD3DXBUFFER* shader,
602                                                LPD3DXBUFFER* error_messages)
603 {
604     HRSRC res;
605     LPCSTR buffer;
606     DWORD len;
607
608     if (!(res = FindResourceA(module, resource, (LPCSTR)RT_RCDATA)))
609         return D3DXERR_INVALIDDATA;
610     if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
611         return D3DXERR_INVALIDDATA;
612     return D3DXAssembleShader(buffer, len, defines, include, flags,
613                               shader, error_messages);
614 }
615
616 HRESULT WINAPI D3DXAssembleShaderFromResourceW(HMODULE module,
617                                                LPCWSTR resource,
618                                                CONST D3DXMACRO* defines,
619                                                LPD3DXINCLUDE include,
620                                                DWORD flags,
621                                                LPD3DXBUFFER* shader,
622                                                LPD3DXBUFFER* error_messages)
623 {
624     HRSRC res;
625     LPCSTR buffer;
626     DWORD len;
627
628     if (!(res = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA)))
629         return D3DXERR_INVALIDDATA;
630     if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
631         return D3DXERR_INVALIDDATA;
632     return D3DXAssembleShader(buffer, len, defines, include, flags,
633                               shader, error_messages);
634 }
635
636 HRESULT WINAPI D3DXCompileShader(LPCSTR pSrcData,
637                                  UINT srcDataLen,
638                                  CONST D3DXMACRO* pDefines,
639                                  LPD3DXINCLUDE pInclude,
640                                  LPCSTR pFunctionName,
641                                  LPCSTR pProfile,
642                                  DWORD Flags,
643                                  LPD3DXBUFFER* ppShader,
644                                  LPD3DXBUFFER* ppErrorMsgs,
645                                  LPD3DXCONSTANTTABLE * ppConstantTable)
646 {
647     FIXME("(%p, %d, %p, %p, %p, %p, %d, %p, %p, %p): stub\n",
648           pSrcData, srcDataLen, pDefines, pInclude, pFunctionName,
649           pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
650     return D3DERR_INVALIDCALL;
651 }
652
653 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl;
654
655 typedef struct ID3DXConstantTableImpl {
656     const ID3DXConstantTableVtbl *lpVtbl;
657     LONG ref;
658     LPVOID ctab;
659     DWORD size;
660     D3DXCONSTANTTABLE_DESC desc;
661 } ID3DXConstantTableImpl;
662
663 /*** IUnknown methods ***/
664 static HRESULT WINAPI ID3DXConstantTableImpl_QueryInterface(ID3DXConstantTable* iface, REFIID riid, void** ppvObject)
665 {
666     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
667
668     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObject);
669
670     if (IsEqualGUID(riid, &IID_IUnknown) ||
671         IsEqualGUID(riid, &IID_ID3DXBuffer) ||
672         IsEqualGUID(riid, &IID_ID3DXConstantTable))
673     {
674         ID3DXConstantTable_AddRef(iface);
675         *ppvObject = This;
676         return S_OK;
677     }
678
679     WARN("Interface %s not found.\n", debugstr_guid(riid));
680
681     return E_NOINTERFACE;
682 }
683
684 static ULONG WINAPI ID3DXConstantTableImpl_AddRef(ID3DXConstantTable* iface)
685 {
686     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
687
688     TRACE("(%p)->(): AddRef from %d\n", This, This->ref);
689
690     return InterlockedIncrement(&This->ref);
691 }
692
693 static ULONG WINAPI ID3DXConstantTableImpl_Release(ID3DXConstantTable* iface)
694 {
695     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
696     ULONG ref = InterlockedDecrement(&This->ref);
697
698     TRACE("(%p)->(): Release from %d\n", This, ref + 1);
699
700     if (!ref)
701     {
702         HeapFree(GetProcessHeap(), 0, This->ctab);
703         HeapFree(GetProcessHeap(), 0, This);
704     }
705
706     return ref;
707 }
708
709 /*** ID3DXBuffer methods ***/
710 static LPVOID WINAPI ID3DXConstantTableImpl_GetBufferPointer(ID3DXConstantTable* iface)
711 {
712     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
713
714     TRACE("(%p)->()\n", This);
715
716     return This->ctab;
717 }
718
719 static DWORD WINAPI ID3DXConstantTableImpl_GetBufferSize(ID3DXConstantTable* iface)
720 {
721     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
722
723     TRACE("(%p)->()\n", This);
724
725     return This->size;
726 }
727
728 /*** ID3DXConstantTable methods ***/
729 static HRESULT WINAPI ID3DXConstantTableImpl_GetDesc(ID3DXConstantTable* iface, D3DXCONSTANTTABLE_DESC *desc)
730 {
731     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
732
733     TRACE("(%p)->(%p)\n", This, desc);
734
735     if (!desc)
736         return D3DERR_INVALIDCALL;
737
738     memcpy(desc, &This->desc, sizeof(This->desc));
739
740     return D3D_OK;
741 }
742
743 static HRESULT WINAPI ID3DXConstantTableImpl_GetConstantDesc(ID3DXConstantTable* iface, D3DXHANDLE constant,
744                                                              D3DXCONSTANT_DESC *desc, UINT *count)
745 {
746     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
747
748     FIXME("(%p)->(%p, %p, %p): stub\n", This, constant, desc, count);
749
750     return E_NOTIMPL;
751 }
752
753 static UINT WINAPI ID3DXConstantTableImpl_GetSamplerIndex(LPD3DXCONSTANTTABLE iface, D3DXHANDLE constant)
754 {
755     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
756
757     FIXME("(%p)->(%p): stub\n", This, constant);
758
759     return (UINT)-1;
760 }
761
762 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstant(ID3DXConstantTable* iface, D3DXHANDLE constant, UINT index)
763 {
764     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
765
766     FIXME("(%p)->(%p, %d): stub\n", This, constant, index);
767
768     return NULL;
769 }
770
771 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantByName(ID3DXConstantTable* iface, D3DXHANDLE constant, LPCSTR name)
772 {
773     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
774
775     FIXME("(%p)->(%p, %s): stub\n", This, constant, name);
776
777     return NULL;
778 }
779
780 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantElement(ID3DXConstantTable* iface, D3DXHANDLE constant, UINT index)
781 {
782     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
783
784     FIXME("(%p)->(%p, %d): stub\n", This, constant, index);
785
786     return NULL;
787 }
788
789 static HRESULT WINAPI ID3DXConstantTableImpl_SetDefaults(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device)
790 {
791     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
792
793     FIXME("(%p)->(%p): stub\n", This, device);
794
795     return E_NOTIMPL;
796 }
797
798 static HRESULT WINAPI ID3DXConstantTableImpl_SetValue(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
799                                                       D3DXHANDLE constant, LPCVOID data, UINT bytes)
800 {
801     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
802
803     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, data, bytes);
804
805     return E_NOTIMPL;
806 }
807
808 static HRESULT WINAPI ID3DXConstantTableImpl_SetBool(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
809                                                      D3DXHANDLE constant, BOOL b)
810 {
811     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
812
813     FIXME("(%p)->(%p, %p, %d): stub\n", This, device, constant, b);
814
815     return E_NOTIMPL;
816 }
817
818 static HRESULT WINAPI ID3DXConstantTableImpl_SetBoolArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
819                                                           D3DXHANDLE constant, CONST BOOL* b, UINT count)
820 {
821     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
822
823     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, b, count);
824
825     return E_NOTIMPL;
826 }
827
828 static HRESULT WINAPI ID3DXConstantTableImpl_SetInt(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device, D3DXHANDLE constant, INT n)
829 {
830     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
831
832     FIXME("(%p)->(%p, %p, %d): stub\n", This, device, constant, n);
833
834     return E_NOTIMPL;
835 }
836
837 static HRESULT WINAPI ID3DXConstantTableImpl_SetIntArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
838                                                          D3DXHANDLE constant, CONST INT* n, UINT count)
839 {
840     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
841
842     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, n, count);
843
844     return E_NOTIMPL;
845 }
846
847 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloat(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
848                                                       D3DXHANDLE constant, FLOAT f)
849 {
850     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
851
852     FIXME("(%p)->(%p, %p, %f): stub\n", This, device, constant, f);
853
854     return E_NOTIMPL;
855 }
856
857 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloatArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
858                                                            D3DXHANDLE constant, CONST FLOAT* f, UINT count)
859 {
860     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
861
862     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, f, count);
863
864     return E_NOTIMPL;
865 }
866
867 static HRESULT WINAPI ID3DXConstantTableImpl_SetVector(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
868                                                        D3DXHANDLE constant, CONST D3DXVECTOR4* vector)
869 {
870     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
871
872     FIXME("(%p)->(%p, %p, %p): stub\n", This, device, constant, vector);
873
874     return E_NOTIMPL;
875 }
876
877 static HRESULT WINAPI ID3DXConstantTableImpl_SetVectorArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
878                                                             D3DXHANDLE constant, CONST D3DXVECTOR4* vector, UINT count)
879 {
880     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
881
882     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, vector, count);
883
884     return E_NOTIMPL;
885 }
886
887 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrix(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
888                                                        D3DXHANDLE constant, CONST D3DXMATRIX* matrix)
889 {
890     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
891
892     FIXME("(%p)->(%p, %p, %p): stub\n", This, device, constant, matrix);
893
894     return E_NOTIMPL;
895 }
896
897 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
898                                                             D3DXHANDLE constant, CONST D3DXMATRIX* matrix, UINT count)
899 {
900     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
901
902     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, matrix, count);
903
904     return E_NOTIMPL;
905 }
906
907 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixPointerArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
908                                                                    D3DXHANDLE constant, CONST D3DXMATRIX** matrix, UINT count)
909 {
910     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
911
912     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, matrix, count);
913
914     return E_NOTIMPL;
915 }
916
917 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTranspose(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
918                                                                 D3DXHANDLE constant, CONST D3DXMATRIX* matrix)
919 {
920     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
921
922     FIXME("(%p)->(%p, %p, %p): stub\n", This, device, constant, matrix);
923
924     return E_NOTIMPL;
925 }
926
927 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposeArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
928                                                                      D3DXHANDLE constant, CONST D3DXMATRIX* matrix, UINT count)
929 {
930     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
931
932     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, matrix, count);
933
934     return E_NOTIMPL;
935 }
936
937 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposePointerArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
938                                                                             D3DXHANDLE constant, CONST D3DXMATRIX** matrix, UINT count)
939 {
940     ID3DXConstantTableImpl *This = (ID3DXConstantTableImpl *)iface;
941
942     FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, matrix, count);
943
944     return E_NOTIMPL;
945 }
946
947 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl =
948 {
949     /*** IUnknown methods ***/
950     ID3DXConstantTableImpl_QueryInterface,
951     ID3DXConstantTableImpl_AddRef,
952     ID3DXConstantTableImpl_Release,
953     /*** ID3DXBuffer methods ***/
954     ID3DXConstantTableImpl_GetBufferPointer,
955     ID3DXConstantTableImpl_GetBufferSize,
956     /*** ID3DXConstantTable methods ***/
957     ID3DXConstantTableImpl_GetDesc,
958     ID3DXConstantTableImpl_GetConstantDesc,
959     ID3DXConstantTableImpl_GetSamplerIndex,
960     ID3DXConstantTableImpl_GetConstant,
961     ID3DXConstantTableImpl_GetConstantByName,
962     ID3DXConstantTableImpl_GetConstantElement,
963     ID3DXConstantTableImpl_SetDefaults,
964     ID3DXConstantTableImpl_SetValue,
965     ID3DXConstantTableImpl_SetBool,
966     ID3DXConstantTableImpl_SetBoolArray,
967     ID3DXConstantTableImpl_SetInt,
968     ID3DXConstantTableImpl_SetIntArray,
969     ID3DXConstantTableImpl_SetFloat,
970     ID3DXConstantTableImpl_SetFloatArray,
971     ID3DXConstantTableImpl_SetVector,
972     ID3DXConstantTableImpl_SetVectorArray,
973     ID3DXConstantTableImpl_SetMatrix,
974     ID3DXConstantTableImpl_SetMatrixArray,
975     ID3DXConstantTableImpl_SetMatrixPointerArray,
976     ID3DXConstantTableImpl_SetMatrixTranspose,
977     ID3DXConstantTableImpl_SetMatrixTransposeArray,
978     ID3DXConstantTableImpl_SetMatrixTransposePointerArray
979 };
980
981 HRESULT WINAPI D3DXGetShaderConstantTableEx(CONST DWORD* byte_code,
982                                             DWORD flags,
983                                             LPD3DXCONSTANTTABLE* constant_table)
984 {
985     ID3DXConstantTableImpl* object = NULL;
986     HRESULT hr;
987     LPCVOID data;
988     UINT size;
989     const D3DXSHADER_CONSTANTTABLE* ctab_header;
990
991     FIXME("(%p, %x, %p): semi-stub\n", byte_code, flags, constant_table);
992
993     if (!byte_code || !constant_table)
994         return D3DERR_INVALIDCALL;
995
996     hr = D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), &data, &size);
997     if (hr != D3D_OK)
998         return D3DXERR_INVALIDDATA;
999
1000     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXConstantTableImpl));
1001     if (!object)
1002     {
1003         ERR("Out of memory\n");
1004         return E_OUTOFMEMORY;
1005     }
1006
1007     object->lpVtbl = &ID3DXConstantTable_Vtbl;
1008     object->ref = 1;
1009
1010     if (size < sizeof(D3DXSHADER_CONSTANTTABLE))
1011         goto error;
1012
1013     object->ctab = HeapAlloc(GetProcessHeap(), 0, size);
1014     if (!object->ctab)
1015     {
1016         HeapFree(GetProcessHeap(), 0, object);
1017         ERR("Out of memory\n");
1018         return E_OUTOFMEMORY;
1019     }
1020     object->size = size;
1021     memcpy(object->ctab, data, object->size);
1022
1023     ctab_header = (const D3DXSHADER_CONSTANTTABLE*)data;
1024     if (ctab_header->Size != sizeof(D3DXSHADER_CONSTANTTABLE))
1025         goto error;
1026     object->desc.Creator = ctab_header->Creator ? (LPCSTR)object->ctab + ctab_header->Creator : NULL;
1027     object->desc.Version = ctab_header->Version;
1028     object->desc.Constants = ctab_header->Constants;
1029
1030     *constant_table = (LPD3DXCONSTANTTABLE)object;
1031
1032     return D3D_OK;
1033
1034 error:
1035
1036     HeapFree(GetProcessHeap(), 0, object->ctab);
1037     HeapFree(GetProcessHeap(), 0, object);
1038
1039     return D3DXERR_INVALIDDATA;
1040 }
1041
1042 HRESULT WINAPI D3DXGetShaderConstantTable(CONST DWORD* byte_code,
1043                                           LPD3DXCONSTANTTABLE* constant_table)
1044 {
1045     TRACE("(%p, %p): Forwarded to D3DXGetShaderConstantTableEx\n", byte_code, constant_table);
1046
1047     return D3DXGetShaderConstantTableEx(byte_code, 0, constant_table);
1048 }