d3dx9: Use debug_d3dxparameter_class/type in traces.
[wine] / dlls / d3dx9_36 / shader.c
1 /*
2  * Copyright 2008 Luis Busquets
3  * Copyright 2009 Matteo Bruni
4  * Copyright 2011 Travis Athougies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28 #include "windef.h"
29 #include "wingdi.h"
30 #include "objbase.h"
31 #include "d3dcommon.h"
32 #include "d3dcompiler.h"
33 #include "d3dx9_36_private.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
36
37 /* This function is not declared in the SDK headers yet */
38 HRESULT WINAPI D3DAssemble(LPCVOID data, SIZE_T datasize, LPCSTR filename,
39                            const D3D_SHADER_MACRO *defines, ID3DInclude *include,
40                            UINT flags,
41                            ID3DBlob **shader, ID3DBlob **error_messages);
42
43 LPCSTR WINAPI D3DXGetPixelShaderProfile(LPDIRECT3DDEVICE9 device)
44 {
45     D3DCAPS9 caps;
46
47     TRACE("device %p\n", device);
48
49     if (!device) return NULL;
50
51     IDirect3DDevice9_GetDeviceCaps(device,&caps);
52
53     switch (caps.PixelShaderVersion)
54     {
55     case D3DPS_VERSION(1, 1):
56         return "ps_1_1";
57
58     case D3DPS_VERSION(1, 2):
59         return "ps_1_2";
60
61     case D3DPS_VERSION(1, 3):
62         return "ps_1_3";
63
64     case D3DPS_VERSION(1, 4):
65         return "ps_1_4";
66
67     case D3DPS_VERSION(2, 0):
68         if ((caps.PS20Caps.NumTemps>=22)                          &&
69             (caps.PS20Caps.Caps&D3DPS20CAPS_ARBITRARYSWIZZLE)     &&
70             (caps.PS20Caps.Caps&D3DPS20CAPS_GRADIENTINSTRUCTIONS) &&
71             (caps.PS20Caps.Caps&D3DPS20CAPS_PREDICATION)          &&
72             (caps.PS20Caps.Caps&D3DPS20CAPS_NODEPENDENTREADLIMIT) &&
73             (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
74         {
75             return "ps_2_a";
76         }
77         if ((caps.PS20Caps.NumTemps>=32)                          &&
78             (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
79         {
80             return "ps_2_b";
81         }
82         return "ps_2_0";
83
84     case D3DPS_VERSION(3, 0):
85         return "ps_3_0";
86     }
87
88     return NULL;
89 }
90
91 UINT WINAPI D3DXGetShaderSize(const DWORD *byte_code)
92 {
93     const DWORD *ptr = byte_code;
94
95     TRACE("byte_code %p\n", byte_code);
96
97     if (!ptr) return 0;
98
99     /* Look for the END token, skipping the VERSION token */
100     while (*++ptr != D3DSIO_END)
101     {
102         /* Skip comments */
103         if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
104         {
105             ptr += ((*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT);
106         }
107     }
108     ++ptr;
109
110     /* Return the shader size in bytes */
111     return (ptr - byte_code) * sizeof(*ptr);
112 }
113
114 DWORD WINAPI D3DXGetShaderVersion(const DWORD *byte_code)
115 {
116     TRACE("byte_code %p\n", byte_code);
117
118     return byte_code ? *byte_code : 0;
119 }
120
121 LPCSTR WINAPI D3DXGetVertexShaderProfile(LPDIRECT3DDEVICE9 device)
122 {
123     D3DCAPS9 caps;
124
125     TRACE("device %p\n", device);
126
127     if (!device) return NULL;
128
129     IDirect3DDevice9_GetDeviceCaps(device,&caps);
130
131     switch (caps.VertexShaderVersion)
132     {
133     case D3DVS_VERSION(1, 1):
134         return "vs_1_1";
135     case D3DVS_VERSION(2, 0):
136         if ((caps.VS20Caps.NumTemps>=13) &&
137             (caps.VS20Caps.DynamicFlowControlDepth==24) &&
138             (caps.VS20Caps.Caps&D3DPS20CAPS_PREDICATION))
139         {
140             return "vs_2_a";
141         }
142         return "vs_2_0";
143     case D3DVS_VERSION(3, 0):
144         return "vs_3_0";
145     }
146
147     return NULL;
148 }
149
150 HRESULT WINAPI D3DXFindShaderComment(CONST DWORD* byte_code, DWORD fourcc, LPCVOID* data, UINT* size)
151 {
152     CONST DWORD *ptr = byte_code;
153
154     TRACE("(%p, %x, %p, %p)\n", byte_code, fourcc, data, size);
155
156     if (data) *data = NULL;
157     if (size) *size = 0;
158
159     if (!byte_code)
160         return D3DERR_INVALIDCALL;
161
162     while (*++ptr != D3DSIO_END)
163     {
164         /* Check if it is a comment */
165         if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
166         {
167             DWORD comment_size = (*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
168
169             /* Check if this is the comment we are looking for */
170             if (*(ptr + 1) == fourcc)
171             {
172                 UINT ctab_size = (comment_size - 1) * sizeof(DWORD);
173                 LPCVOID ctab_data = ptr + 2;
174                 if (size)
175                     *size = ctab_size;
176                 if (data)
177                     *data = ctab_data;
178                 TRACE("Returning comment data at %p with size %d\n", ctab_data, ctab_size);
179                 return D3D_OK;
180             }
181             ptr += comment_size;
182         }
183     }
184
185     return S_FALSE;
186 }
187
188 HRESULT WINAPI D3DXAssembleShader(LPCSTR data,
189                                   UINT data_len,
190                                   CONST D3DXMACRO* defines,
191                                   LPD3DXINCLUDE include,
192                                   DWORD flags,
193                                   LPD3DXBUFFER* shader,
194                                   LPD3DXBUFFER* error_messages)
195 {
196     /* Forward to d3dcompiler: the parameter types aren't really different,
197        the actual data types are equivalent */
198     HRESULT hr = D3DAssemble(data, data_len, NULL, (D3D_SHADER_MACRO *)defines,
199                              (ID3DInclude *)include, flags, (ID3DBlob **)shader,
200                              (ID3DBlob **)error_messages);
201
202     if(hr == E_FAIL) hr = D3DXERR_INVALIDDATA;
203     return hr;
204 }
205
206 /* D3DXInclude private implementation, used to implement
207    D3DXAssembleShaderFromFile from D3DXAssembleShader */
208 /* To be able to correctly resolve include search paths we have to store
209    the pathname of each include file. We store the pathname pointer right
210    before the file data. */
211 static HRESULT WINAPI d3dincludefromfile_open(ID3DXInclude *iface,
212                                               D3DXINCLUDE_TYPE include_type,
213                                               LPCSTR filename, LPCVOID parent_data,
214                                               LPCVOID *data, UINT *bytes) {
215     const char *p, *parent_name = "";
216     char *pathname = NULL;
217     char **buffer = NULL;
218     HANDLE file;
219     UINT size;
220
221     if(parent_data != NULL)
222         parent_name = *((const char **)parent_data - 1);
223
224     TRACE("Looking up for include file %s, parent %s\n", debugstr_a(filename), debugstr_a(parent_name));
225
226     if ((p = strrchr(parent_name, '\\')) || (p = strrchr(parent_name, '/'))) p++;
227     else p = parent_name;
228     pathname = HeapAlloc(GetProcessHeap(), 0, (p - parent_name) + strlen(filename) + 1);
229     if(!pathname)
230         return HRESULT_FROM_WIN32(GetLastError());
231
232     memcpy(pathname, parent_name, p - parent_name);
233     strcpy(pathname + (p - parent_name), filename);
234
235     file = CreateFileA(pathname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
236     if(file == INVALID_HANDLE_VALUE)
237         goto error;
238
239     TRACE("Include file found at pathname = %s\n", debugstr_a(pathname));
240
241     size = GetFileSize(file, NULL);
242     if(size == INVALID_FILE_SIZE)
243         goto error;
244
245     buffer = HeapAlloc(GetProcessHeap(), 0, size + sizeof(char *));
246     if(!buffer)
247         goto error;
248     *buffer = pathname;
249     if(!ReadFile(file, buffer + 1, size, bytes, NULL))
250         goto error;
251
252     *data = buffer + 1;
253
254     CloseHandle(file);
255     return S_OK;
256
257 error:
258     CloseHandle(file);
259     HeapFree(GetProcessHeap(), 0, pathname);
260     HeapFree(GetProcessHeap(), 0, buffer);
261     return HRESULT_FROM_WIN32(GetLastError());
262 }
263
264 static HRESULT WINAPI d3dincludefromfile_close(ID3DXInclude *iface, LPCVOID data) {
265     HeapFree(GetProcessHeap(), 0, *((char **)data - 1));
266     HeapFree(GetProcessHeap(), 0, (char **)data - 1);
267     return S_OK;
268 }
269
270 static const struct ID3DXIncludeVtbl D3DXInclude_Vtbl = {
271     d3dincludefromfile_open,
272     d3dincludefromfile_close
273 };
274
275 struct D3DXIncludeImpl {
276     ID3DXInclude ID3DXInclude_iface;
277 };
278
279 HRESULT WINAPI D3DXAssembleShaderFromFileA(LPCSTR filename,
280                                            CONST D3DXMACRO* defines,
281                                            LPD3DXINCLUDE include,
282                                            DWORD flags,
283                                            LPD3DXBUFFER* shader,
284                                            LPD3DXBUFFER* error_messages)
285 {
286     LPWSTR filename_w = NULL;
287     DWORD len;
288     HRESULT ret;
289
290     if (!filename) return D3DXERR_INVALIDDATA;
291
292     len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
293     filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
294     if (!filename_w) return E_OUTOFMEMORY;
295     MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
296
297     ret = D3DXAssembleShaderFromFileW(filename_w, defines, include, flags, shader, error_messages);
298
299     HeapFree(GetProcessHeap(), 0, filename_w);
300     return ret;
301 }
302
303 HRESULT WINAPI D3DXAssembleShaderFromFileW(LPCWSTR filename,
304                                            CONST D3DXMACRO* defines,
305                                            LPD3DXINCLUDE include,
306                                            DWORD flags,
307                                            LPD3DXBUFFER* shader,
308                                            LPD3DXBUFFER* error_messages)
309 {
310     void *buffer;
311     DWORD len;
312     HRESULT hr;
313     struct D3DXIncludeImpl includefromfile;
314
315     if(FAILED(map_view_of_file(filename, &buffer, &len)))
316         return D3DXERR_INVALIDDATA;
317
318     if(!include)
319     {
320         includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
321         include = &includefromfile.ID3DXInclude_iface;
322     }
323
324     hr = D3DXAssembleShader(buffer, len, defines, include, flags,
325                             shader, error_messages);
326
327     UnmapViewOfFile(buffer);
328     return hr;
329 }
330
331 HRESULT WINAPI D3DXAssembleShaderFromResourceA(HMODULE module,
332                                                LPCSTR resource,
333                                                CONST D3DXMACRO* defines,
334                                                LPD3DXINCLUDE include,
335                                                DWORD flags,
336                                                LPD3DXBUFFER* shader,
337                                                LPD3DXBUFFER* error_messages)
338 {
339     HRSRC res;
340     LPCSTR buffer;
341     DWORD len;
342
343     if (!(res = FindResourceA(module, resource, (LPCSTR)RT_RCDATA)))
344         return D3DXERR_INVALIDDATA;
345     if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
346         return D3DXERR_INVALIDDATA;
347     return D3DXAssembleShader(buffer, len, defines, include, flags,
348                               shader, error_messages);
349 }
350
351 HRESULT WINAPI D3DXAssembleShaderFromResourceW(HMODULE module,
352                                                LPCWSTR resource,
353                                                CONST D3DXMACRO* defines,
354                                                LPD3DXINCLUDE include,
355                                                DWORD flags,
356                                                LPD3DXBUFFER* shader,
357                                                LPD3DXBUFFER* error_messages)
358 {
359     HRSRC res;
360     LPCSTR buffer;
361     DWORD len;
362
363     if (!(res = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA)))
364         return D3DXERR_INVALIDDATA;
365     if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
366         return D3DXERR_INVALIDDATA;
367     return D3DXAssembleShader(buffer, len, defines, include, flags,
368                               shader, error_messages);
369 }
370
371 HRESULT WINAPI D3DXCompileShader(LPCSTR pSrcData,
372                                  UINT srcDataLen,
373                                  CONST D3DXMACRO* pDefines,
374                                  LPD3DXINCLUDE pInclude,
375                                  LPCSTR pFunctionName,
376                                  LPCSTR pProfile,
377                                  DWORD Flags,
378                                  LPD3DXBUFFER* ppShader,
379                                  LPD3DXBUFFER* ppErrorMsgs,
380                                  LPD3DXCONSTANTTABLE * ppConstantTable)
381 {
382     HRESULT hr = D3DCompile(pSrcData, srcDataLen, NULL,
383                             (D3D_SHADER_MACRO *)pDefines, (ID3DInclude *)pInclude,
384                             pFunctionName, pProfile, Flags, 0,
385                             (ID3DBlob **)ppShader, (ID3DBlob **)ppErrorMsgs);
386
387     if(SUCCEEDED(hr) && ppConstantTable)
388         return D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*ppShader),
389                                           ppConstantTable);
390     return hr;
391 }
392
393 HRESULT WINAPI D3DXCompileShaderFromFileA(LPCSTR filename,
394                                           CONST D3DXMACRO* defines,
395                                           LPD3DXINCLUDE include,
396                                           LPCSTR entrypoint,
397                                           LPCSTR profile,
398                                           DWORD flags,
399                                           LPD3DXBUFFER* shader,
400                                           LPD3DXBUFFER* error_messages,
401                                           LPD3DXCONSTANTTABLE* constant_table)
402 {
403     LPWSTR filename_w = NULL;
404     DWORD len;
405     HRESULT ret;
406
407     if (!filename) return D3DXERR_INVALIDDATA;
408
409     len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
410     filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
411     if (!filename_w) return E_OUTOFMEMORY;
412     MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
413
414     ret = D3DXCompileShaderFromFileW(filename_w, defines, include,
415                                      entrypoint, profile, flags,
416                                      shader, error_messages, constant_table);
417
418     HeapFree(GetProcessHeap(), 0, filename_w);
419     return ret;
420 }
421
422 HRESULT WINAPI D3DXCompileShaderFromFileW(LPCWSTR filename,
423                                           CONST D3DXMACRO* defines,
424                                           LPD3DXINCLUDE include,
425                                           LPCSTR entrypoint,
426                                           LPCSTR profile,
427                                           DWORD flags,
428                                           LPD3DXBUFFER* shader,
429                                           LPD3DXBUFFER* error_messages,
430                                           LPD3DXCONSTANTTABLE* constant_table)
431 {
432     void *buffer;
433     DWORD len, filename_len;
434     HRESULT hr;
435     struct D3DXIncludeImpl includefromfile;
436     char *filename_a;
437
438     if (FAILED(map_view_of_file(filename, &buffer, &len)))
439         return D3DXERR_INVALIDDATA;
440
441     if (!include)
442     {
443         includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
444         include = &includefromfile.ID3DXInclude_iface;
445     }
446
447     filename_len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
448     filename_a = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(char));
449     if (!filename_a)
450     {
451         UnmapViewOfFile(buffer);
452         return E_OUTOFMEMORY;
453     }
454     WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, filename_len, NULL, NULL);
455
456     hr = D3DCompile(buffer, len, filename_a, (const D3D_SHADER_MACRO *)defines,
457                     (ID3DInclude *)include, entrypoint, profile, flags, 0,
458                     (ID3DBlob **)shader, (ID3DBlob **)error_messages);
459
460     if (SUCCEEDED(hr) && constant_table)
461         hr = D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*shader),
462                                         constant_table);
463
464     HeapFree(GetProcessHeap(), 0, filename_a);
465     UnmapViewOfFile(buffer);
466     return hr;
467 }
468
469 HRESULT WINAPI D3DXCompileShaderFromResourceA(HMODULE module,
470                                               LPCSTR resource,
471                                               CONST D3DXMACRO* defines,
472                                               LPD3DXINCLUDE include,
473                                               LPCSTR entrypoint,
474                                               LPCSTR profile,
475                                               DWORD flags,
476                                               LPD3DXBUFFER* shader,
477                                               LPD3DXBUFFER* error_messages,
478                                               LPD3DXCONSTANTTABLE* constant_table)
479 {
480     HRSRC res;
481     LPCSTR buffer;
482     DWORD len;
483
484     if (!(res = FindResourceA(module, resource, (LPCSTR)RT_RCDATA)))
485         return D3DXERR_INVALIDDATA;
486     if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
487         return D3DXERR_INVALIDDATA;
488     return D3DXCompileShader(buffer, len, defines, include, entrypoint, profile,
489                              flags, shader, error_messages, constant_table);
490 }
491
492 HRESULT WINAPI D3DXCompileShaderFromResourceW(HMODULE module,
493                                               LPCWSTR resource,
494                                               CONST D3DXMACRO* defines,
495                                               LPD3DXINCLUDE include,
496                                               LPCSTR entrypoint,
497                                               LPCSTR profile,
498                                               DWORD flags,
499                                               LPD3DXBUFFER* shader,
500                                               LPD3DXBUFFER* error_messages,
501                                               LPD3DXCONSTANTTABLE* constant_table)
502 {
503     HRSRC res;
504     LPCSTR buffer;
505     DWORD len;
506
507     if (!(res = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA)))
508         return D3DXERR_INVALIDDATA;
509     if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
510         return D3DXERR_INVALIDDATA;
511     return D3DXCompileShader(buffer, len, defines, include, entrypoint, profile,
512                              flags, shader, error_messages, constant_table);
513 }
514
515 HRESULT WINAPI D3DXPreprocessShader(LPCSTR data,
516                                     UINT data_len,
517                                     CONST D3DXMACRO* defines,
518                                     LPD3DXINCLUDE include,
519                                     LPD3DXBUFFER* shader,
520                                     LPD3DXBUFFER* error_messages)
521 {
522     TRACE("Forward to D3DPreprocess\n");
523     return D3DPreprocess(data, data_len, NULL,
524                          (const D3D_SHADER_MACRO *)defines, (ID3DInclude *)include,
525                          (ID3DBlob **)shader, (ID3DBlob **)error_messages);
526 }
527
528 HRESULT WINAPI D3DXPreprocessShaderFromFileA(LPCSTR filename,
529                                              CONST D3DXMACRO* defines,
530                                              LPD3DXINCLUDE include,
531                                              LPD3DXBUFFER* shader,
532                                              LPD3DXBUFFER* error_messages)
533 {
534     WCHAR *filename_w = NULL;
535     DWORD len;
536     HRESULT ret;
537
538     if (!filename) return D3DXERR_INVALIDDATA;
539
540     len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
541     filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
542     if (!filename_w) return E_OUTOFMEMORY;
543     MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
544
545     ret = D3DXPreprocessShaderFromFileW(filename_w, defines, include, shader, error_messages);
546
547     HeapFree(GetProcessHeap(), 0, filename_w);
548     return ret;
549 }
550
551 HRESULT WINAPI D3DXPreprocessShaderFromFileW(LPCWSTR filename,
552                                              CONST D3DXMACRO* defines,
553                                              LPD3DXINCLUDE include,
554                                              LPD3DXBUFFER* shader,
555                                              LPD3DXBUFFER* error_messages)
556 {
557     void *buffer;
558     DWORD len;
559     HRESULT hr;
560     struct D3DXIncludeImpl includefromfile;
561
562     if (FAILED(map_view_of_file(filename, &buffer, &len)))
563         return D3DXERR_INVALIDDATA;
564
565     if (!include)
566     {
567         includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
568         include = &includefromfile.ID3DXInclude_iface;
569     }
570
571     hr = D3DPreprocess(buffer, len, NULL,
572                        (const D3D_SHADER_MACRO *)defines,
573                        (ID3DInclude *) include,
574                        (ID3DBlob **)shader, (ID3DBlob **)error_messages);
575
576     UnmapViewOfFile(buffer);
577     return hr;
578 }
579
580 HRESULT WINAPI D3DXPreprocessShaderFromResourceA(HMODULE module,
581                                                  LPCSTR resource,
582                                                  CONST D3DXMACRO* defines,
583                                                  LPD3DXINCLUDE include,
584                                                  LPD3DXBUFFER* shader,
585                                                  LPD3DXBUFFER* error_messages)
586 {
587     HRSRC res;
588     const char *buffer;
589     DWORD len;
590
591     if (!(res = FindResourceA(module, resource, (LPCSTR)RT_RCDATA)))
592         return D3DXERR_INVALIDDATA;
593     if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
594         return D3DXERR_INVALIDDATA;
595     return D3DXPreprocessShader(buffer, len, defines, include,
596                                 shader, error_messages);
597 }
598
599 HRESULT WINAPI D3DXPreprocessShaderFromResourceW(HMODULE module,
600                                                  LPCWSTR resource,
601                                                  CONST D3DXMACRO* defines,
602                                                  LPD3DXINCLUDE include,
603                                                  LPD3DXBUFFER* shader,
604                                                  LPD3DXBUFFER* error_messages)
605 {
606     HRSRC res;
607     const char *buffer;
608     DWORD len;
609
610     if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
611         return D3DXERR_INVALIDDATA;
612     if (FAILED(load_resource_into_memory(module, res, (void **)&buffer, &len)))
613         return D3DXERR_INVALIDDATA;
614     return D3DXPreprocessShader(buffer, len, defines, include,
615                                 shader, error_messages);
616
617 }
618
619 struct ctab_constant {
620     D3DXCONSTANT_DESC desc;
621     struct ctab_constant *constants;
622 };
623
624 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl;
625
626 struct ID3DXConstantTableImpl {
627     ID3DXConstantTable ID3DXConstantTable_iface;
628     LONG ref;
629     char *ctab;
630     DWORD size;
631     D3DXCONSTANTTABLE_DESC desc;
632     struct ctab_constant *constants;
633 };
634
635 static void free_constant(struct ctab_constant *constant)
636 {
637     if (constant->constants)
638     {
639         UINT i, count = constant->desc.Elements > 1 ? constant->desc.Elements : constant->desc.StructMembers;
640
641         for (i = 0; i < count; ++i)
642         {
643             free_constant(&constant->constants[i]);
644         }
645         HeapFree(GetProcessHeap(), 0, constant->constants);
646     }
647 }
648
649 static void free_constant_table(struct ID3DXConstantTableImpl *table)
650 {
651     if (table->constants)
652     {
653         UINT i;
654
655         for (i = 0; i < table->desc.Constants; ++i)
656         {
657             free_constant(&table->constants[i]);
658         }
659         HeapFree(GetProcessHeap(), 0, table->constants);
660     }
661     HeapFree(GetProcessHeap(), 0, table->ctab);
662 }
663
664 static inline struct ID3DXConstantTableImpl *impl_from_ID3DXConstantTable(ID3DXConstantTable *iface)
665 {
666     return CONTAINING_RECORD(iface, struct ID3DXConstantTableImpl, ID3DXConstantTable_iface);
667 }
668
669 static inline int is_vertex_shader(DWORD version)
670 {
671     return (version & 0xFFFF0000) == 0xFFFE0000;
672 }
673
674 static DWORD calc_bytes(D3DXCONSTANT_DESC *desc)
675 {
676     if (desc->RegisterSet != D3DXRS_FLOAT4 && desc->RegisterSet != D3DXRS_SAMPLER)
677         FIXME("Don't know how to calculate Bytes for constants of type %d\n",
678                 desc->RegisterSet);
679
680     return 4 * desc->Elements * desc->Rows * desc->Columns;
681 }
682
683 static inline struct ctab_constant *constant_from_handle(D3DXHANDLE handle)
684 {
685     return (struct ctab_constant *)handle;
686 }
687
688 static inline D3DXHANDLE handle_from_constant(struct ctab_constant *constant)
689 {
690     return (D3DXHANDLE)constant;
691 }
692
693 static struct ctab_constant *get_constant_by_name(struct ID3DXConstantTableImpl *, struct ctab_constant *, LPCSTR);
694
695 static struct ctab_constant *get_constant_element_by_name(struct ctab_constant *constant, LPCSTR name)
696 {
697     UINT element;
698     LPCSTR part;
699
700     TRACE("constant %p, name %s\n", constant, debugstr_a(name));
701
702     if (!name || !*name) return NULL;
703
704     element = atoi(name);
705     part = strchr(name, ']') + 1;
706
707     if (constant->desc.Elements > element)
708     {
709         struct ctab_constant *c = constant->constants ? &constant->constants[element] : constant;
710
711         switch (*part++)
712         {
713             case '.':
714                 return get_constant_by_name(NULL, c, part);
715
716             case '[':
717                 return get_constant_element_by_name(c, part);
718
719             case '\0':
720                 TRACE("Returning parameter %p\n", c);
721                 return c;
722
723             default:
724                 FIXME("Unhandled case \"%c\"\n", *--part);
725                 break;
726         }
727     }
728
729     TRACE("Constant not found\n");
730     return NULL;
731 }
732
733 static struct ctab_constant *get_constant_by_name(struct ID3DXConstantTableImpl *table,
734         struct ctab_constant *constant, LPCSTR name)
735 {
736     UINT i, count, length;
737     struct ctab_constant *handles;
738     LPCSTR part;
739
740     TRACE("table %p, constant %p, name %s\n", table, constant, debugstr_a(name));
741
742     if (!name || !*name) return NULL;
743
744     if (!constant)
745     {
746         count = table->desc.Constants;
747         handles = table->constants;
748     }
749     else
750     {
751         count = constant->desc.StructMembers;
752         handles = constant->constants;
753     }
754
755     length = strcspn( name, "[." );
756     part = name + length;
757
758     for (i = 0; i < count; i++)
759     {
760         if (strlen(handles[i].desc.Name) == length && !strncmp(handles[i].desc.Name, name, length))
761         {
762             switch (*part++)
763             {
764                 case '.':
765                     return get_constant_by_name(NULL, &handles[i], part);
766
767                 case '[':
768                     return get_constant_element_by_name(&handles[i], part);
769
770                 case '\0':
771                     TRACE("Returning parameter %p\n", &handles[i]);
772                     return &handles[i];
773
774                 default:
775                     FIXME("Unhandled case \"%c\"\n", *--part);
776                     break;
777             }
778         }
779     }
780
781     TRACE("Constant not found\n");
782     return NULL;
783 }
784
785 static struct ctab_constant *is_valid_sub_constant(struct ctab_constant *parent, struct ctab_constant *constant)
786 {
787     UINT i, count;
788
789     /* all variable have at least elements = 1, but no elements */
790     if (!parent->constants) return NULL;
791
792     if (parent->desc.Elements > 1) count = parent->desc.Elements;
793     else count = parent->desc.StructMembers;
794
795     for (i = 0; i < count; ++i)
796     {
797         if (&parent->constants[i] == constant)
798             return constant;
799
800         if (is_valid_sub_constant(&parent->constants[i], constant))
801             return constant;
802     }
803
804     return NULL;
805 }
806
807 static inline struct ctab_constant *is_valid_constant(struct ID3DXConstantTableImpl *table, D3DXHANDLE handle)
808 {
809     struct ctab_constant *c = constant_from_handle(handle);
810     UINT i;
811
812     if (!c) return NULL;
813
814     for (i = 0; i < table->desc.Constants; ++i)
815     {
816         if (&table->constants[i] == c)
817             return c;
818
819         if (is_valid_sub_constant(&table->constants[i], c))
820             return c;
821     }
822
823     return NULL;
824 }
825
826 static inline struct ctab_constant *get_valid_constant(struct ID3DXConstantTableImpl *table, D3DXHANDLE handle)
827 {
828     struct ctab_constant *constant = is_valid_constant(table, handle);
829
830     if (!constant) constant = get_constant_by_name(table, NULL, handle);
831
832     return constant;
833 }
834
835 static inline void set_float_shader_constant(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device,
836                                              UINT register_index, const FLOAT *data, UINT count)
837 {
838     if (is_vertex_shader(table->desc.Version))
839         IDirect3DDevice9_SetVertexShaderConstantF(device, register_index, data, count);
840     else
841         IDirect3DDevice9_SetPixelShaderConstantF(device, register_index, data, count);
842 }
843
844 /*** IUnknown methods ***/
845 static HRESULT WINAPI ID3DXConstantTableImpl_QueryInterface(ID3DXConstantTable *iface, REFIID riid, void **out)
846 {
847     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
848
849     if (IsEqualGUID(riid, &IID_IUnknown) ||
850         IsEqualGUID(riid, &IID_ID3DXBuffer) ||
851         IsEqualGUID(riid, &IID_ID3DXConstantTable))
852     {
853         ID3DXConstantTable_AddRef(iface);
854         *out = iface;
855         return S_OK;
856     }
857
858     WARN("Interface %s not found.\n", debugstr_guid(riid));
859
860     return E_NOINTERFACE;
861 }
862
863 static ULONG WINAPI ID3DXConstantTableImpl_AddRef(ID3DXConstantTable *iface)
864 {
865     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
866
867     TRACE("(%p)->(): AddRef from %d\n", This, This->ref);
868
869     return InterlockedIncrement(&This->ref);
870 }
871
872 static ULONG WINAPI ID3DXConstantTableImpl_Release(ID3DXConstantTable *iface)
873 {
874     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
875     ULONG ref = InterlockedDecrement(&This->ref);
876
877     TRACE("(%p)->(): Release from %d\n", This, ref + 1);
878
879     if (!ref)
880     {
881         free_constant_table(This);
882         HeapFree(GetProcessHeap(), 0, This);
883     }
884
885     return ref;
886 }
887
888 /*** ID3DXBuffer methods ***/
889 static LPVOID WINAPI ID3DXConstantTableImpl_GetBufferPointer(ID3DXConstantTable *iface)
890 {
891     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
892
893     TRACE("(%p)->()\n", This);
894
895     return This->ctab;
896 }
897
898 static DWORD WINAPI ID3DXConstantTableImpl_GetBufferSize(ID3DXConstantTable *iface)
899 {
900     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
901
902     TRACE("(%p)->()\n", This);
903
904     return This->size;
905 }
906
907 /*** ID3DXConstantTable methods ***/
908 static HRESULT WINAPI ID3DXConstantTableImpl_GetDesc(ID3DXConstantTable *iface, D3DXCONSTANTTABLE_DESC *desc)
909 {
910     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
911
912     TRACE("(%p)->(%p)\n", This, desc);
913
914     if (!desc)
915         return D3DERR_INVALIDCALL;
916
917     memcpy(desc, &This->desc, sizeof(This->desc));
918
919     return D3D_OK;
920 }
921
922 static HRESULT WINAPI ID3DXConstantTableImpl_GetConstantDesc(ID3DXConstantTable *iface, D3DXHANDLE constant,
923                                                              D3DXCONSTANT_DESC *desc, UINT *count)
924 {
925     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
926     struct ctab_constant *c = get_valid_constant(This, constant);
927
928     TRACE("(%p)->(%p, %p, %p)\n", This, constant, desc, count);
929
930     if (!c)
931     {
932         WARN("Invalid argument specified\n");
933         return D3DERR_INVALIDCALL;
934     }
935
936     if (desc) *desc = c->desc;
937     if (count) *count = 1;
938
939     return D3D_OK;
940 }
941
942 static UINT WINAPI ID3DXConstantTableImpl_GetSamplerIndex(ID3DXConstantTable *iface, D3DXHANDLE constant)
943 {
944     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
945     D3DXCONSTANT_DESC desc;
946     UINT count = 1;
947     HRESULT res;
948
949     TRACE("(%p)->(%p)\n", This, constant);
950
951     res = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &count);
952     if (FAILED(res))
953         return (UINT)-1;
954
955     if (desc.RegisterSet != D3DXRS_SAMPLER)
956         return (UINT)-1;
957
958     return desc.RegisterIndex;
959 }
960
961 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstant(ID3DXConstantTable *iface, D3DXHANDLE constant, UINT index)
962 {
963     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
964     struct ctab_constant *c;
965
966     TRACE("(%p)->(%p, %d)\n", This, constant, index);
967
968     if (constant)
969     {
970         c = get_valid_constant(This, constant);
971         if (c && index < c->desc.StructMembers)
972         {
973             c = &c->constants[index];
974             TRACE("Returning constant %p\n", c);
975             return handle_from_constant(c);
976         }
977     }
978     else
979     {
980         if (index < This->desc.Constants)
981         {
982             c = &This->constants[index];
983             TRACE("Returning constant %p\n", c);
984             return handle_from_constant(c);
985         }
986     }
987
988     WARN("Index out of range\n");
989     return NULL;
990 }
991
992 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantByName(ID3DXConstantTable *iface, D3DXHANDLE constant, LPCSTR name)
993 {
994     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
995     struct ctab_constant *c = get_valid_constant(This, constant);
996
997     TRACE("(%p)->(%p, %s)\n", This, constant, name);
998
999     c = get_constant_by_name(This, c, name);
1000     TRACE("Returning constant %p\n", c);
1001
1002     return handle_from_constant(c);
1003 }
1004
1005 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantElement(ID3DXConstantTable *iface, D3DXHANDLE constant, UINT index)
1006 {
1007     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1008     struct ctab_constant *c = get_valid_constant(This, constant);
1009
1010     TRACE("(%p)->(%p, %d)\n", This, constant, index);
1011
1012     if (c && index < c->desc.Elements)
1013     {
1014         if (c->constants) c = &c->constants[index];
1015         TRACE("Returning constant %p\n", c);
1016         return handle_from_constant(c);
1017     }
1018
1019     WARN("Invalid argument specified\n");
1020     return NULL;
1021 }
1022
1023 static HRESULT set_scalar_array(ID3DXConstantTable *iface, IDirect3DDevice9 *device, D3DXHANDLE constant, const void *data,
1024                                UINT count, D3DXPARAMETER_TYPE type)
1025 {
1026     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1027     D3DXCONSTANT_DESC desc;
1028     HRESULT hr;
1029     UINT i, desc_count = 1;
1030     float row[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1031
1032     hr = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &desc_count);
1033     if (FAILED(hr))
1034     {
1035         TRACE("ID3DXConstantTable_GetConstantDesc failed: %08x\n", hr);
1036         return D3DERR_INVALIDCALL;
1037     }
1038
1039     if (desc.Class != D3DXPC_SCALAR)
1040         return D3D_OK;
1041
1042     switch (desc.RegisterSet)
1043     {
1044         case D3DXRS_FLOAT4:
1045             for (i = 0; i < min(count, desc.RegisterCount); i++)
1046             {
1047                 /* We need the for loop since each IDirect3DDevice9_Set*ShaderConstantF expects a float4 */
1048                 switch(type)
1049                 {
1050                     case D3DXPT_FLOAT:
1051                         row[0] = ((float *)data)[i];
1052                         break;
1053                     case D3DXPT_INT:
1054                         row[0] = (float)((int *)data)[i];
1055                         break;
1056                     case D3DXPT_BOOL:
1057                         row[0] = ((BOOL *)data)[i] ? 1.0f : 0.0f;
1058                         break;
1059                     default:
1060                         FIXME("Unhandled type %s\n", debug_d3dxparameter_type(type));
1061                         return D3DERR_INVALIDCALL;
1062                 }
1063                 set_float_shader_constant(This, device, desc.RegisterIndex + i, row, 1);
1064             }
1065             break;
1066         default:
1067             FIXME("Handle other register sets\n");
1068             return E_NOTIMPL;
1069     }
1070
1071     return D3D_OK;
1072 }
1073
1074 static HRESULT set_vector_array(ID3DXConstantTable *iface, IDirect3DDevice9 *device, D3DXHANDLE constant, const void *data,
1075                                 UINT count, D3DXPARAMETER_TYPE type)
1076 {
1077     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1078     D3DXCONSTANT_DESC desc;
1079     HRESULT hr;
1080     UINT i, j, desc_count = 1;
1081     float vec[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1082
1083     hr = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &desc_count);
1084     if (FAILED(hr))
1085     {
1086         TRACE("ID3DXConstantTable_GetConstantDesc failed: %08x\n", hr);
1087         return D3DERR_INVALIDCALL;
1088     }
1089
1090     if (desc.Class == D3DXPC_MATRIX_ROWS || desc.Class == D3DXPC_MATRIX_COLUMNS)
1091         return D3D_OK;
1092
1093     switch (desc.RegisterSet)
1094     {
1095         case D3DXRS_FLOAT4:
1096             for (i = 0; i < min(count, desc.RegisterCount); i++)
1097             {
1098                 switch (type)
1099                 {
1100                     case D3DXPT_FLOAT:
1101                         memcpy(vec, ((float *)data) + i * desc.Columns, desc.Columns * sizeof(float));
1102                         break;
1103                     case D3DXPT_INT:
1104                         for (j = 0; j < desc.Columns; j++)
1105                             vec[j] = (float)((int *)data)[i * desc.Columns + j];
1106                         break;
1107                     case D3DXPT_BOOL:
1108                         for (j = 0; j < desc.Columns; j++)
1109                             vec[j] = ((BOOL *)data)[i * desc.Columns + j] ? 1.0f : 0.0f;
1110                         break;
1111                     default:
1112                         FIXME("Unhandled type %s\n", debug_d3dxparameter_type(type));
1113                         return D3DERR_INVALIDCALL;
1114                 }
1115
1116                 set_float_shader_constant(This, device, desc.RegisterIndex + i, vec, 1);
1117             }
1118             break;
1119         default:
1120             FIXME("Unhandled register set %#x\n", desc.RegisterSet);
1121             return E_NOTIMPL;
1122     }
1123
1124     return D3D_OK;
1125 }
1126
1127 static HRESULT set_float_matrix(FLOAT *matrix, const D3DXCONSTANT_DESC *desc,
1128                                 UINT row_offset, UINT column_offset, UINT rows, UINT columns,
1129                                 const void *data, D3DXPARAMETER_TYPE type, UINT src_columns)
1130 {
1131     UINT i, j;
1132
1133     switch (type)
1134     {
1135         case D3DXPT_FLOAT:
1136             for (i = 0; i < rows; i++)
1137             {
1138                 for (j = 0; j < columns; j++)
1139                     matrix[i * row_offset + j * column_offset] = ((FLOAT *)data)[i * src_columns + j];
1140             }
1141             break;
1142         case D3DXPT_INT:
1143             for (i = 0; i < rows; i++)
1144             {
1145                 for (j = 0; j < columns; j++)
1146                     matrix[i * row_offset + j * column_offset] = ((INT *)data)[i * src_columns + j];
1147             }
1148             break;
1149         default:
1150             FIXME("Unhandled type %s\n", debug_d3dxparameter_type(type));
1151             return D3DERR_INVALIDCALL;
1152     }
1153
1154     return D3D_OK;
1155 }
1156
1157 static HRESULT set_matrix_array(ID3DXConstantTable *iface, IDirect3DDevice9 *device, D3DXHANDLE constant, const void *data,
1158                                 UINT count, D3DXPARAMETER_CLASS class, D3DXPARAMETER_TYPE type, UINT rows, UINT columns)
1159 {
1160     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1161     D3DXCONSTANT_DESC desc;
1162     HRESULT hr;
1163     UINT registers_per_matrix;
1164     UINT i, desc_count = 1;
1165     UINT num_rows, num_columns;
1166     UINT row_offset, column_offset;
1167     const DWORD *data_ptr;
1168     FLOAT matrix[16] = {0.0f, 0.0f, 0.0f, 0.0f,
1169                         0.0f, 0.0f, 0.0f, 0.0f,
1170                         0.0f, 0.0f, 0.0f, 0.0f,
1171                         0.0f, 0.0f, 0.0f, 0.0f};
1172
1173     hr = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &desc_count);
1174     if (FAILED(hr))
1175     {
1176         TRACE("ID3DXConstantTable_GetConstantDesc failed: %08x\n", hr);
1177         return D3DERR_INVALIDCALL;
1178     }
1179
1180     if (desc.Class == D3DXPC_MATRIX_ROWS || desc.Class == D3DXPC_MATRIX_COLUMNS)
1181     {
1182         if (desc.Class == class)
1183         {
1184             column_offset = 1;
1185             row_offset = 4;
1186         }
1187         else
1188         {
1189             column_offset = 4;
1190             row_offset = 1;
1191         }
1192
1193         if (class == D3DXPC_MATRIX_ROWS)
1194         {
1195             num_rows = min(desc.Rows, rows);
1196             num_columns = min(desc.Columns, columns);
1197         }
1198         else
1199         {
1200             num_rows = min(desc.Columns, columns);
1201             num_columns = min(desc.Rows, rows);
1202         }
1203
1204         registers_per_matrix = (desc.Class == D3DXPC_MATRIX_ROWS) ? desc.Rows : desc.Columns;
1205     }
1206     else if (desc.Class == D3DXPC_SCALAR)
1207     {
1208         column_offset = 1;
1209         row_offset = 1;
1210         num_rows = min(desc.Rows, rows);
1211         num_columns = min(desc.Columns, columns);
1212         registers_per_matrix = 1;
1213     }
1214     else
1215     {
1216         FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc.Class));
1217         return D3D_OK;
1218     }
1219
1220     switch (desc.RegisterSet)
1221     {
1222         case D3DXRS_FLOAT4:
1223             data_ptr = data;
1224             for (i = 0; i < count; i++)
1225             {
1226                 if (registers_per_matrix * (i + 1) > desc.RegisterCount)
1227                     break;
1228
1229                 hr = set_float_matrix(matrix, &desc, row_offset, column_offset, num_rows, num_columns, data_ptr, type, columns);
1230                 if (FAILED(hr)) return hr;
1231
1232                 set_float_shader_constant(This, device, desc.RegisterIndex + i * registers_per_matrix, matrix, registers_per_matrix);
1233
1234                 data_ptr += rows * columns;
1235             }
1236             break;
1237         default:
1238             FIXME("Unhandled register set %#x\n", desc.RegisterSet);
1239             return E_NOTIMPL;
1240     }
1241
1242     return D3D_OK;
1243 }
1244
1245 static HRESULT set_matrix_pointer_array(ID3DXConstantTable *iface, IDirect3DDevice9 *device, D3DXHANDLE constant,
1246                                 const D3DXMATRIX **data, UINT count, D3DXPARAMETER_CLASS class)
1247 {
1248     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1249     D3DXCONSTANT_DESC desc;
1250     HRESULT hr;
1251     UINT registers_per_matrix;
1252     UINT i, desc_count = 1;
1253     UINT num_rows, num_columns;
1254     UINT row_offset, column_offset;
1255     FLOAT matrix[16] = {0.0f, 0.0f, 0.0f, 0.0f,
1256                         0.0f, 0.0f, 0.0f, 0.0f,
1257                         0.0f, 0.0f, 0.0f, 0.0f,
1258                         0.0f, 0.0f, 0.0f, 0.0f};
1259
1260     hr = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &desc_count);
1261     if (FAILED(hr))
1262     {
1263         TRACE("ID3DXConstantTable_GetConstantDesc failed: %08x\n", hr);
1264         return D3DERR_INVALIDCALL;
1265     }
1266
1267     if (desc.Class == D3DXPC_MATRIX_ROWS || desc.Class == D3DXPC_MATRIX_COLUMNS)
1268     {
1269         if (desc.Class == class)
1270         {
1271             column_offset = 1;
1272             row_offset = 4;
1273         }
1274         else
1275         {
1276             column_offset = 4;
1277             row_offset = 1;
1278         }
1279
1280         if (class == D3DXPC_MATRIX_ROWS)
1281         {
1282             num_rows = desc.Rows;
1283             num_columns = desc.Columns;
1284         }
1285         else
1286         {
1287             num_rows = desc.Columns;
1288             num_columns = desc.Rows;
1289         }
1290
1291         registers_per_matrix = (desc.Class == D3DXPC_MATRIX_ROWS) ? desc.Rows : desc.Columns;
1292     }
1293     else if (desc.Class == D3DXPC_SCALAR)
1294     {
1295         registers_per_matrix = 1;
1296         column_offset = 1;
1297         row_offset = 1;
1298         num_rows = desc.Rows;
1299         num_columns = desc.Columns;
1300     }
1301     else if (desc.Class == D3DXPC_VECTOR)
1302     {
1303         registers_per_matrix = 1;
1304
1305         if (class == D3DXPC_MATRIX_ROWS)
1306         {
1307             column_offset = 1;
1308             row_offset = 4;
1309             num_rows = desc.Rows;
1310             num_columns = desc.Columns;
1311         }
1312         else
1313         {
1314             column_offset = 4;
1315             row_offset = 1;
1316             num_rows = desc.Columns;
1317             num_columns = desc.Rows;
1318         }
1319     }
1320     else
1321     {
1322         FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc.Class));
1323         return D3D_OK;
1324     }
1325
1326     switch (desc.RegisterSet)
1327     {
1328         case D3DXRS_FLOAT4:
1329             for (i = 0; i < count; i++)
1330             {
1331                 if (registers_per_matrix * (i + 1) > desc.RegisterCount)
1332                     break;
1333
1334                 hr = set_float_matrix(matrix, &desc, row_offset, column_offset, num_rows, num_columns, *data, D3DXPT_FLOAT, 4);
1335                 if (FAILED(hr)) return hr;
1336
1337                 set_float_shader_constant(This, device, desc.RegisterIndex + i * registers_per_matrix, matrix, registers_per_matrix);
1338
1339                 data++;
1340             }
1341             break;
1342         default:
1343             FIXME("Unhandled register set %#x\n", desc.RegisterSet);
1344             return E_NOTIMPL;
1345     }
1346
1347     return D3D_OK;
1348 }
1349
1350 static HRESULT WINAPI ID3DXConstantTableImpl_SetDefaults(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device)
1351 {
1352     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1353     UINT i;
1354
1355     TRACE("(%p)->(%p)\n", This, device);
1356
1357     if (!device)
1358         return D3DERR_INVALIDCALL;
1359
1360     for (i = 0; i < This->desc.Constants; i++)
1361     {
1362         D3DXCONSTANT_DESC *desc = &This->constants[i].desc;
1363
1364         if (!desc->DefaultValue)
1365             continue;
1366
1367         set_float_shader_constant(This, device, desc->RegisterIndex, desc->DefaultValue, desc->RegisterCount);
1368     }
1369
1370     return D3D_OK;
1371 }
1372
1373 static HRESULT WINAPI ID3DXConstantTableImpl_SetValue(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1374                                                       D3DXHANDLE constant, LPCVOID data, UINT bytes)
1375 {
1376     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1377     HRESULT hr;
1378     UINT elements;
1379     UINT count = 1;
1380     D3DXCONSTANT_DESC desc;
1381
1382     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, data, bytes);
1383
1384     if (!device || !constant || !data)
1385         return D3DERR_INVALIDCALL;
1386
1387     hr = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &count);
1388     if (FAILED(hr))
1389         return hr;
1390
1391     elements = bytes / (desc.Bytes / desc.Elements);
1392
1393     switch (desc.Class)
1394     {
1395         case D3DXPC_SCALAR:
1396             return set_scalar_array(iface, device, constant, data, elements, desc.Type);
1397         case D3DXPC_VECTOR:
1398             return set_vector_array(iface, device, constant, data, elements, desc.Type);
1399         case D3DXPC_MATRIX_ROWS:
1400         case D3DXPC_MATRIX_COLUMNS:
1401             return set_matrix_array(iface, device, constant, data, elements,
1402                     D3DXPC_MATRIX_ROWS, desc.Type, desc.Rows, desc.Columns);
1403         default:
1404             FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(desc.Class));
1405             return D3DERR_INVALIDCALL;
1406     }
1407 }
1408
1409 static HRESULT WINAPI ID3DXConstantTableImpl_SetBool(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1410                                                      D3DXHANDLE constant, BOOL b)
1411 {
1412     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1413
1414     TRACE("(%p)->(%p, %p, %d)\n", This, device, constant, b);
1415
1416     return set_scalar_array(iface, device, constant, &b, 1, D3DXPT_BOOL);
1417 }
1418
1419 static HRESULT WINAPI ID3DXConstantTableImpl_SetBoolArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1420                                                           D3DXHANDLE constant, CONST BOOL* b, UINT count)
1421 {
1422     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1423
1424     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, b, count);
1425
1426     return set_scalar_array(iface, device, constant, b, count, D3DXPT_BOOL);
1427 }
1428
1429 static HRESULT WINAPI ID3DXConstantTableImpl_SetInt(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device, D3DXHANDLE constant, INT n)
1430 {
1431     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1432
1433     TRACE("(%p)->(%p, %p, %d)\n", This, device, constant, n);
1434
1435     return set_scalar_array(iface, device, constant, &n, 1, D3DXPT_INT);
1436 }
1437
1438 static HRESULT WINAPI ID3DXConstantTableImpl_SetIntArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1439                                                          D3DXHANDLE constant, CONST INT* n, UINT count)
1440 {
1441     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1442
1443     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, n, count);
1444
1445     return set_scalar_array(iface, device, constant, n, count, D3DXPT_INT);
1446 }
1447
1448 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloat(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1449                                                       D3DXHANDLE constant, FLOAT f)
1450 {
1451     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1452
1453     TRACE("(%p)->(%p, %p, %f)\n", This, device, constant, f);
1454
1455     return set_scalar_array(iface, device, constant, &f, 1, D3DXPT_FLOAT);
1456 }
1457
1458 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloatArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1459                                                            D3DXHANDLE constant, CONST FLOAT *f, UINT count)
1460 {
1461     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1462
1463     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, f, count);
1464
1465     return set_scalar_array(iface, device, constant, f, count, D3DXPT_FLOAT);
1466 }
1467
1468 static HRESULT WINAPI ID3DXConstantTableImpl_SetVector(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1469                                                        D3DXHANDLE constant, CONST D3DXVECTOR4 *vector)
1470 {
1471     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1472
1473     TRACE("(%p)->(%p, %p, %p)\n", This, device, constant, vector);
1474
1475     return set_vector_array(iface, device, constant, vector, 1, D3DXPT_FLOAT);
1476 }
1477
1478 static HRESULT WINAPI ID3DXConstantTableImpl_SetVectorArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1479                                                             D3DXHANDLE constant, CONST D3DXVECTOR4 *vector, UINT count)
1480 {
1481     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1482
1483     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, vector, count);
1484
1485     return set_vector_array(iface, device, constant, vector, count, D3DXPT_FLOAT);
1486 }
1487
1488 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrix(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1489                                                        D3DXHANDLE constant, CONST D3DXMATRIX *matrix)
1490 {
1491     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1492
1493     TRACE("(%p)->(%p, %p, %p)\n", This, device, constant, matrix);
1494
1495     return set_matrix_array(iface, device, constant, matrix, 1, D3DXPC_MATRIX_ROWS, D3DXPT_FLOAT, 4, 4);
1496 }
1497
1498 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1499                                                             D3DXHANDLE constant, CONST D3DXMATRIX *matrix, UINT count)
1500 {
1501     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1502
1503     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, matrix, count);
1504
1505     return set_matrix_array(iface, device, constant, matrix, count, D3DXPC_MATRIX_ROWS, D3DXPT_FLOAT, 4, 4);
1506 }
1507
1508 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixPointerArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1509                                                                    D3DXHANDLE constant, CONST D3DXMATRIX **matrix, UINT count)
1510 {
1511     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1512
1513     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, matrix, count);
1514
1515     return set_matrix_pointer_array(iface, device, constant, matrix, count, D3DXPC_MATRIX_ROWS);
1516 }
1517
1518 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTranspose(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1519                                                                 D3DXHANDLE constant, CONST D3DXMATRIX *matrix)
1520 {
1521     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1522
1523     TRACE("(%p)->(%p, %p, %p)\n", This, device, constant, matrix);
1524
1525     return set_matrix_array(iface, device, constant, matrix, 1, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 4, 4);
1526 }
1527
1528 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposeArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1529                                                                      D3DXHANDLE constant, CONST D3DXMATRIX *matrix, UINT count)
1530 {
1531     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1532
1533     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, matrix, count);
1534
1535     return set_matrix_array(iface, device, constant, matrix, count, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 4, 4);
1536 }
1537
1538 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposePointerArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1539                                                                             D3DXHANDLE constant, CONST D3DXMATRIX **matrix, UINT count)
1540 {
1541     struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1542
1543     TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, matrix, count);
1544
1545     return set_matrix_pointer_array(iface, device, constant, matrix, count, D3DXPC_MATRIX_COLUMNS);
1546 }
1547
1548 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl =
1549 {
1550     /*** IUnknown methods ***/
1551     ID3DXConstantTableImpl_QueryInterface,
1552     ID3DXConstantTableImpl_AddRef,
1553     ID3DXConstantTableImpl_Release,
1554     /*** ID3DXBuffer methods ***/
1555     ID3DXConstantTableImpl_GetBufferPointer,
1556     ID3DXConstantTableImpl_GetBufferSize,
1557     /*** ID3DXConstantTable methods ***/
1558     ID3DXConstantTableImpl_GetDesc,
1559     ID3DXConstantTableImpl_GetConstantDesc,
1560     ID3DXConstantTableImpl_GetSamplerIndex,
1561     ID3DXConstantTableImpl_GetConstant,
1562     ID3DXConstantTableImpl_GetConstantByName,
1563     ID3DXConstantTableImpl_GetConstantElement,
1564     ID3DXConstantTableImpl_SetDefaults,
1565     ID3DXConstantTableImpl_SetValue,
1566     ID3DXConstantTableImpl_SetBool,
1567     ID3DXConstantTableImpl_SetBoolArray,
1568     ID3DXConstantTableImpl_SetInt,
1569     ID3DXConstantTableImpl_SetIntArray,
1570     ID3DXConstantTableImpl_SetFloat,
1571     ID3DXConstantTableImpl_SetFloatArray,
1572     ID3DXConstantTableImpl_SetVector,
1573     ID3DXConstantTableImpl_SetVectorArray,
1574     ID3DXConstantTableImpl_SetMatrix,
1575     ID3DXConstantTableImpl_SetMatrixArray,
1576     ID3DXConstantTableImpl_SetMatrixPointerArray,
1577     ID3DXConstantTableImpl_SetMatrixTranspose,
1578     ID3DXConstantTableImpl_SetMatrixTransposeArray,
1579     ID3DXConstantTableImpl_SetMatrixTransposePointerArray
1580 };
1581
1582 static HRESULT parse_ctab_constant_type(const char *ctab, DWORD typeoffset, struct ctab_constant *constant,
1583         BOOL is_element, WORD index, WORD max, DWORD *offset, DWORD nameoffset, UINT regset)
1584 {
1585     const D3DXSHADER_TYPEINFO *type = (LPD3DXSHADER_TYPEINFO)(ctab + typeoffset);
1586     const D3DXSHADER_STRUCTMEMBERINFO *memberinfo = NULL;
1587     HRESULT hr = D3D_OK;
1588     UINT i, count = 0;
1589     WORD size = 0;
1590
1591     constant->desc.DefaultValue = offset ? ctab + *offset : NULL;
1592     constant->desc.Class = type->Class;
1593     constant->desc.Type = type->Type;
1594     constant->desc.Rows = type->Rows;
1595     constant->desc.Columns = type->Columns;
1596     constant->desc.Elements = is_element ? 1 : type->Elements;
1597     constant->desc.StructMembers = type->StructMembers;
1598     constant->desc.Name = ctab + nameoffset;
1599     constant->desc.RegisterSet = regset;
1600     constant->desc.RegisterIndex = index;
1601
1602     TRACE("name %s, elements %u, index %u, defaultvalue %p\n", constant->desc.Name,
1603             constant->desc.Elements, index, constant->desc.DefaultValue);
1604     TRACE("class %s, type %s, rows %d, columns %d, elements %d, struct_members %d\n",
1605             debug_d3dxparameter_class(type->Class), debug_d3dxparameter_type(type->Type),
1606             type->Rows, type->Columns, type->Elements, type->StructMembers);
1607
1608     if (type->Elements > 1 && !is_element)
1609     {
1610         count = type->Elements;
1611     }
1612     else if ((type->Class == D3DXPC_STRUCT) && type->StructMembers)
1613     {
1614         memberinfo = (D3DXSHADER_STRUCTMEMBERINFO*)(ctab + type->StructMemberInfo);
1615         count = type->StructMembers;
1616     }
1617
1618     if (count)
1619     {
1620         constant->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*constant->constants) * count);
1621         if (!constant->constants)
1622         {
1623              ERR("Out of memory\n");
1624              hr = E_OUTOFMEMORY;
1625              goto error;
1626         }
1627
1628         for (i = 0; i < count; ++i)
1629         {
1630             hr = parse_ctab_constant_type(ctab, memberinfo ? memberinfo[i].TypeInfo : typeoffset,
1631                     &constant->constants[i], memberinfo == NULL, index + size, max, offset,
1632                     memberinfo ? memberinfo[i].Name : nameoffset, regset);
1633             if (hr != D3D_OK)
1634                 goto error;
1635
1636             size += constant->constants[i].desc.RegisterCount;
1637         }
1638     }
1639     else
1640     {
1641         WORD offsetdiff = 0;
1642
1643         switch (type->Class)
1644         {
1645             case D3DXPC_SCALAR:
1646             case D3DXPC_VECTOR:
1647                 offsetdiff = 1;
1648                 size = 1;
1649                 break;
1650
1651             case D3DXPC_MATRIX_ROWS:
1652                 size = is_element ? type->Rows : max(type->Rows, type->Columns);
1653                 offsetdiff = type->Rows;
1654                 break;
1655
1656             case D3DXPC_MATRIX_COLUMNS:
1657                 size = type->Columns;
1658                 offsetdiff = type->Columns;
1659                 break;
1660
1661             case D3DXPC_OBJECT:
1662                 size = 1;
1663                 break;
1664
1665             default:
1666                 FIXME("Unhandled type class %s\n", debug_d3dxparameter_class(type->Class));
1667                 break;
1668         }
1669
1670         /* offset in bytes => offsetdiff * components(4) * sizeof(DWORD) */
1671         if (offset) *offset += offsetdiff * 4 * 4;
1672     }
1673
1674     constant->desc.RegisterCount = max(0, min(max - index, size));
1675     constant->desc.Bytes = calc_bytes(&constant->desc);
1676
1677     return D3D_OK;
1678
1679 error:
1680     if (constant->constants)
1681     {
1682         for (i = 0; i < count; ++i)
1683         {
1684             free_constant(&constant->constants[i]);
1685         }
1686         HeapFree(GetProcessHeap(), 0, constant->constants);
1687         constant->constants = NULL;
1688     }
1689
1690     return hr;
1691 }
1692
1693 HRESULT WINAPI D3DXGetShaderConstantTableEx(CONST DWORD *byte_code,
1694                                             DWORD flags,
1695                                             LPD3DXCONSTANTTABLE *constant_table)
1696 {
1697     struct ID3DXConstantTableImpl *object = NULL;
1698     HRESULT hr;
1699     LPCVOID data;
1700     UINT size;
1701     const D3DXSHADER_CONSTANTTABLE* ctab_header;
1702     D3DXSHADER_CONSTANTINFO* constant_info;
1703     DWORD i;
1704
1705     TRACE("(%p, %x, %p)\n", byte_code, flags, constant_table);
1706
1707     if (!byte_code || !constant_table)
1708     {
1709         WARN("Invalid argument specified.\n");
1710         return D3DERR_INVALIDCALL;
1711     }
1712
1713     if (flags) FIXME("Flags (%#x) are not handled, yet!\n", flags);
1714
1715     hr = D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), &data, &size);
1716     if (hr != D3D_OK)
1717     {
1718         WARN("CTAB not found.\n");
1719         return D3DXERR_INVALIDDATA;
1720     }
1721
1722     if (size < sizeof(D3DXSHADER_CONSTANTTABLE))
1723     {
1724         WARN("Invalid CTAB size.\n");
1725         return D3DXERR_INVALIDDATA;
1726     }
1727
1728     ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
1729     if (ctab_header->Size != sizeof(D3DXSHADER_CONSTANTTABLE))
1730     {
1731         WARN("Invalid D3DXSHADER_CONSTANTTABLE size.\n");
1732         return D3DXERR_INVALIDDATA;
1733     }
1734
1735     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1736     if (!object)
1737     {
1738         ERR("Out of memory\n");
1739         return E_OUTOFMEMORY;
1740     }
1741
1742     object->ID3DXConstantTable_iface.lpVtbl = &ID3DXConstantTable_Vtbl;
1743     object->ref = 1;
1744
1745     object->ctab = HeapAlloc(GetProcessHeap(), 0, size);
1746     if (!object->ctab)
1747     {
1748         ERR("Out of memory\n");
1749         HeapFree(GetProcessHeap(), 0, object);
1750         return E_OUTOFMEMORY;
1751     }
1752     object->size = size;
1753     memcpy(object->ctab, data, object->size);
1754
1755     object->desc.Creator = ctab_header->Creator ? object->ctab + ctab_header->Creator : NULL;
1756     object->desc.Version = ctab_header->Version;
1757     object->desc.Constants = ctab_header->Constants;
1758     TRACE("Creator %s, Version %x, Constants %u, Target %s\n",
1759             debugstr_a(object->desc.Creator), object->desc.Version, object->desc.Constants,
1760             debugstr_a(ctab_header->Target ? object->ctab + ctab_header->Target : NULL));
1761
1762     if (object->desc.Constants > 65535)
1763     {
1764         FIXME("Too many constants (%u)\n", object->desc.Constants);
1765         hr = E_NOTIMPL;
1766         goto error;
1767     }
1768
1769     object->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1770                                   sizeof(*object->constants) * object->desc.Constants);
1771     if (!object->constants)
1772     {
1773          ERR("Out of memory\n");
1774          hr = E_OUTOFMEMORY;
1775          goto error;
1776     }
1777
1778     constant_info = (LPD3DXSHADER_CONSTANTINFO)(object->ctab + ctab_header->ConstantInfo);
1779     for (i = 0; i < ctab_header->Constants; i++)
1780     {
1781         DWORD offset = constant_info[i].DefaultValue;
1782
1783         hr = parse_ctab_constant_type(object->ctab, constant_info[i].TypeInfo,
1784                 &object->constants[i], FALSE, constant_info[i].RegisterIndex,
1785                 constant_info[i].RegisterIndex + constant_info[i].RegisterCount,
1786                 offset ? &offset : NULL, constant_info[i].Name, constant_info[i].RegisterSet);
1787         if (hr != D3D_OK)
1788             goto error;
1789     }
1790
1791     *constant_table = &object->ID3DXConstantTable_iface;
1792
1793     return D3D_OK;
1794
1795 error:
1796     free_constant_table(object);
1797     HeapFree(GetProcessHeap(), 0, object);
1798
1799     return hr;
1800 }
1801
1802 HRESULT WINAPI D3DXGetShaderConstantTable(CONST DWORD* byte_code,
1803                                           LPD3DXCONSTANTTABLE* constant_table)
1804 {
1805     TRACE("(%p, %p): Forwarded to D3DXGetShaderConstantTableEx\n", byte_code, constant_table);
1806
1807     return D3DXGetShaderConstantTableEx(byte_code, 0, constant_table);
1808 }
1809
1810 HRESULT WINAPI D3DXGetShaderSamplers(CONST DWORD *byte_code, LPCSTR *samplers, UINT *count)
1811 {
1812     HRESULT hr;
1813     UINT i, sampler_count = 0;
1814     UINT size;
1815     LPCSTR data;
1816     const D3DXSHADER_CONSTANTTABLE *ctab_header;
1817     const D3DXSHADER_CONSTANTINFO *constant_info;
1818
1819     TRACE("byte_code %p, samplers %p, count %p\n", byte_code, samplers, count);
1820
1821     if (count) *count = 0;
1822
1823     hr = D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), (LPCVOID *)&data, &size);
1824     if (hr != D3D_OK) return D3D_OK;
1825
1826     if (size < sizeof(D3DXSHADER_CONSTANTTABLE)) return D3D_OK;
1827
1828     ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
1829     if (ctab_header->Size != sizeof(*ctab_header)) return D3D_OK;
1830
1831     constant_info = (D3DXSHADER_CONSTANTINFO *)(data + ctab_header->ConstantInfo);
1832     for (i = 0; i < ctab_header->Constants; i++)
1833     {
1834         const D3DXSHADER_TYPEINFO *type;
1835
1836         TRACE("name = %s\n", data + constant_info[i].Name);
1837
1838         type = (D3DXSHADER_TYPEINFO *)(data + constant_info[i].TypeInfo);
1839
1840         if (type->Type == D3DXPT_SAMPLER
1841                 || type->Type == D3DXPT_SAMPLER1D
1842                 || type->Type == D3DXPT_SAMPLER2D
1843                 || type->Type == D3DXPT_SAMPLER3D
1844                 || type->Type == D3DXPT_SAMPLERCUBE)
1845         {
1846             if (samplers) samplers[sampler_count] = data + constant_info[i].Name;
1847
1848             ++sampler_count;
1849         }
1850     }
1851
1852     TRACE("Found %u samplers\n", sampler_count);
1853
1854     if (count) *count = sampler_count;
1855
1856     return D3D_OK;
1857 }