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