[D3DX9_24=>43] Sync with Wine 3.0. CORE-14225
[reactos.git] / 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
23 #include <stdio.h>
24
25 #include "d3dcompiler.h"
26
27 /* This function is not declared in the SDK headers yet. */
28 HRESULT WINAPI D3DAssemble(const void *data, SIZE_T datasize, const char *filename,
29 const D3D_SHADER_MACRO *defines, ID3DInclude *include, UINT flags,
30 ID3DBlob **shader, ID3DBlob **error_messages);
31
32 static inline BOOL is_valid_bytecode(DWORD token)
33 {
34 return (token & 0xfffe0000) == 0xfffe0000;
35 }
36
37 const char * WINAPI D3DXGetPixelShaderProfile(struct IDirect3DDevice9 *device)
38 {
39 D3DCAPS9 caps;
40
41 TRACE("device %p\n", device);
42
43 if (!device) return NULL;
44
45 IDirect3DDevice9_GetDeviceCaps(device,&caps);
46
47 switch (caps.PixelShaderVersion)
48 {
49 case D3DPS_VERSION(1, 1):
50 return "ps_1_1";
51
52 case D3DPS_VERSION(1, 2):
53 return "ps_1_2";
54
55 case D3DPS_VERSION(1, 3):
56 return "ps_1_3";
57
58 case D3DPS_VERSION(1, 4):
59 return "ps_1_4";
60
61 case D3DPS_VERSION(2, 0):
62 if ((caps.PS20Caps.NumTemps>=22) &&
63 (caps.PS20Caps.Caps&D3DPS20CAPS_ARBITRARYSWIZZLE) &&
64 (caps.PS20Caps.Caps&D3DPS20CAPS_GRADIENTINSTRUCTIONS) &&
65 (caps.PS20Caps.Caps&D3DPS20CAPS_PREDICATION) &&
66 (caps.PS20Caps.Caps&D3DPS20CAPS_NODEPENDENTREADLIMIT) &&
67 (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
68 {
69 return "ps_2_a";
70 }
71 if ((caps.PS20Caps.NumTemps>=32) &&
72 (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
73 {
74 return "ps_2_b";
75 }
76 return "ps_2_0";
77
78 case D3DPS_VERSION(3, 0):
79 return "ps_3_0";
80 }
81
82 return NULL;
83 }
84
85 UINT WINAPI D3DXGetShaderSize(const DWORD *byte_code)
86 {
87 const DWORD *ptr = byte_code;
88
89 TRACE("byte_code %p\n", byte_code);
90
91 if (!ptr) return 0;
92
93 /* Look for the END token, skipping the VERSION token */
94 while (*++ptr != D3DSIO_END)
95 {
96 /* Skip comments */
97 if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
98 {
99 ptr += ((*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT);
100 }
101 }
102 ++ptr;
103
104 /* Return the shader size in bytes */
105 return (ptr - byte_code) * sizeof(*ptr);
106 }
107
108 DWORD WINAPI D3DXGetShaderVersion(const DWORD *byte_code)
109 {
110 TRACE("byte_code %p\n", byte_code);
111
112 return byte_code ? *byte_code : 0;
113 }
114
115 const char * WINAPI D3DXGetVertexShaderProfile(struct IDirect3DDevice9 *device)
116 {
117 D3DCAPS9 caps;
118
119 TRACE("device %p\n", device);
120
121 if (!device) return NULL;
122
123 IDirect3DDevice9_GetDeviceCaps(device,&caps);
124
125 switch (caps.VertexShaderVersion)
126 {
127 case D3DVS_VERSION(1, 1):
128 return "vs_1_1";
129 case D3DVS_VERSION(2, 0):
130 if ((caps.VS20Caps.NumTemps>=13) &&
131 (caps.VS20Caps.DynamicFlowControlDepth==24) &&
132 (caps.VS20Caps.Caps&D3DPS20CAPS_PREDICATION))
133 {
134 return "vs_2_a";
135 }
136 return "vs_2_0";
137 case D3DVS_VERSION(3, 0):
138 return "vs_3_0";
139 }
140
141 return NULL;
142 }
143
144 HRESULT WINAPI D3DXFindShaderComment(const DWORD *byte_code, DWORD fourcc, const void **data, UINT *size)
145 {
146 const DWORD *ptr = byte_code;
147 DWORD version;
148
149 TRACE("byte_code %p, fourcc %x, data %p, size %p\n", byte_code, fourcc, data, size);
150
151 if (data) *data = NULL;
152 if (size) *size = 0;
153
154 if (!byte_code) return D3DERR_INVALIDCALL;
155
156 version = *ptr >> 16;
157 if (version != 0x4658 /* FX */
158 && version != 0x5458 /* TX */
159 && version != 0x7ffe
160 && version != 0x7fff
161 && version != 0xfffe /* VS */
162 && version != 0xffff) /* PS */
163 {
164 WARN("Invalid data supplied\n");
165 return D3DXERR_INVALIDDATA;
166 }
167
168 while (*++ptr != D3DSIO_END)
169 {
170 /* Check if it is a comment */
171 if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
172 {
173 DWORD comment_size = (*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
174
175 /* Check if this is the comment we are looking for */
176 if (*(ptr + 1) == fourcc)
177 {
178 UINT ctab_size = (comment_size - 1) * sizeof(DWORD);
179 const void *ctab_data = ptr + 2;
180 if (size)
181 *size = ctab_size;
182 if (data)
183 *data = ctab_data;
184 TRACE("Returning comment data at %p with size %d\n", ctab_data, ctab_size);
185 return D3D_OK;
186 }
187 ptr += comment_size;
188 }
189 }
190
191 return S_FALSE;
192 }
193
194 HRESULT WINAPI D3DXAssembleShader(const char *data, UINT data_len, const D3DXMACRO *defines,
195 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
196 {
197 HRESULT hr;
198
199 TRACE("data %p, data_len %u, defines %p, include %p, flags %#x, shader %p, error_messages %p\n",
200 data, data_len, defines, include, flags, shader, error_messages);
201
202 /* Forward to d3dcompiler: the parameter types aren't really different,
203 the actual data types are equivalent */
204 hr = D3DAssemble(data, data_len, NULL, (D3D_SHADER_MACRO *)defines,
205 (ID3DInclude *)include, flags, (ID3DBlob **)shader,
206 (ID3DBlob **)error_messages);
207
208 if(hr == E_FAIL) hr = D3DXERR_INVALIDDATA;
209 return hr;
210 }
211
212 static const void *main_file_data;
213
214 static CRITICAL_SECTION from_file_mutex;
215 static CRITICAL_SECTION_DEBUG from_file_mutex_debug =
216 {
217 0, 0, &from_file_mutex,
218 {
219 &from_file_mutex_debug.ProcessLocksList,
220 &from_file_mutex_debug.ProcessLocksList
221 },
222 0, 0, {(DWORD_PTR)(__FILE__ ": from_file_mutex")}
223 };
224 static CRITICAL_SECTION from_file_mutex = {&from_file_mutex_debug, -1, 0, 0, 0, 0};
225
226 /* D3DXInclude private implementation, used to implement
227 * D3DXAssembleShaderFromFile() from D3DXAssembleShader(). */
228 /* To be able to correctly resolve include search paths we have to store the
229 * pathname of each include file. We store the pathname pointer right before
230 * the file data. */
231 static HRESULT WINAPI d3dincludefromfile_open(ID3DXInclude *iface, D3DXINCLUDE_TYPE include_type,
232 const char *filename, const void *parent_data, const void **data, UINT *bytes)
233 {
234 const char *p, *parent_name = "";
235 char *pathname = NULL, *ptr;
236 char **buffer = NULL;
237 HANDLE file;
238 UINT size;
239
240 if (parent_data)
241 {
242 parent_name = *((const char **)parent_data - 1);
243 }
244 else
245 {
246 if (main_file_data)
247 parent_name = *((const char **)main_file_data - 1);
248 }
249
250 TRACE("Looking up for include file %s, parent %s\n", debugstr_a(filename), debugstr_a(parent_name));
251
252 if ((p = strrchr(parent_name, '\\')))
253 ++p;
254 else
255 p = parent_name;
256 pathname = HeapAlloc(GetProcessHeap(), 0, (p - parent_name) + strlen(filename) + 1);
257 if(!pathname)
258 return HRESULT_FROM_WIN32(GetLastError());
259
260 memcpy(pathname, parent_name, p - parent_name);
261 strcpy(pathname + (p - parent_name), filename);
262 ptr = pathname + (p - parent_name);
263 while (*ptr)
264 {
265 if (*ptr == '/')
266 *ptr = '\\';
267 ++ptr;
268 }
269
270 file = CreateFileA(pathname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
271 if(file == INVALID_HANDLE_VALUE)
272 goto error;
273
274 TRACE("Include file found at pathname = %s\n", debugstr_a(pathname));
275
276 size = GetFileSize(file, NULL);
277 if(size == INVALID_FILE_SIZE)
278 goto error;
279
280 buffer = HeapAlloc(GetProcessHeap(), 0, size + sizeof(char *));
281 if(!buffer)
282 goto error;
283 *buffer = pathname;
284 if(!ReadFile(file, buffer + 1, size, bytes, NULL))
285 goto error;
286
287 *data = buffer + 1;
288 if (!main_file_data)
289 main_file_data = *data;
290
291 CloseHandle(file);
292 return S_OK;
293
294 error:
295 CloseHandle(file);
296 HeapFree(GetProcessHeap(), 0, pathname);
297 HeapFree(GetProcessHeap(), 0, buffer);
298 return HRESULT_FROM_WIN32(GetLastError());
299 }
300
301 static HRESULT WINAPI d3dincludefromfile_close(ID3DXInclude *iface, const void *data)
302 {
303 HeapFree(GetProcessHeap(), 0, *((char **)data - 1));
304 HeapFree(GetProcessHeap(), 0, (char **)data - 1);
305 if (main_file_data == data)
306 main_file_data = NULL;
307 return S_OK;
308 }
309
310 static const struct ID3DXIncludeVtbl D3DXInclude_Vtbl = {
311 d3dincludefromfile_open,
312 d3dincludefromfile_close
313 };
314
315 struct D3DXIncludeImpl {
316 ID3DXInclude ID3DXInclude_iface;
317 };
318
319 HRESULT WINAPI D3DXAssembleShaderFromFileA(const char *filename, const D3DXMACRO *defines,
320 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
321 {
322 WCHAR *filename_w;
323 DWORD len;
324 HRESULT ret;
325
326 TRACE("filename %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
327 debugstr_a(filename), defines, include, flags, shader, error_messages);
328
329 if (!filename) return D3DXERR_INVALIDDATA;
330
331 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
332 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
333 if (!filename_w) return E_OUTOFMEMORY;
334 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
335
336 ret = D3DXAssembleShaderFromFileW(filename_w, defines, include, flags, shader, error_messages);
337
338 HeapFree(GetProcessHeap(), 0, filename_w);
339 return ret;
340 }
341
342 HRESULT WINAPI D3DXAssembleShaderFromFileW(const WCHAR *filename, const D3DXMACRO *defines,
343 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
344 {
345 const void *buffer;
346 DWORD len;
347 HRESULT hr;
348 struct D3DXIncludeImpl includefromfile;
349 char *filename_a;
350
351 TRACE("filename %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
352 debugstr_w(filename), defines, include, flags, shader, error_messages);
353
354 if(!include)
355 {
356 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
357 include = &includefromfile.ID3DXInclude_iface;
358 }
359
360 len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
361 filename_a = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
362 if (!filename_a)
363 return E_OUTOFMEMORY;
364 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, len, NULL, NULL);
365
366 EnterCriticalSection(&from_file_mutex);
367 hr = ID3DXInclude_Open(include, D3DXINC_LOCAL, filename_a, NULL, &buffer, &len);
368 if (FAILED(hr))
369 {
370 LeaveCriticalSection(&from_file_mutex);
371 HeapFree(GetProcessHeap(), 0, filename_a);
372 return D3DXERR_INVALIDDATA;
373 }
374
375 hr = D3DXAssembleShader(buffer, len, defines, include, flags, shader, error_messages);
376
377 ID3DXInclude_Close(include, buffer);
378 LeaveCriticalSection(&from_file_mutex);
379 HeapFree(GetProcessHeap(), 0, filename_a);
380 return hr;
381 }
382
383 HRESULT WINAPI D3DXAssembleShaderFromResourceA(HMODULE module, const char *resource, const D3DXMACRO *defines,
384 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
385 {
386 void *buffer;
387 HRSRC res;
388 DWORD len;
389
390 TRACE("module %p, resource %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
391 module, debugstr_a(resource), defines, include, flags, shader, error_messages);
392
393 if (!(res = FindResourceA(module, resource, (const char *)RT_RCDATA)))
394 return D3DXERR_INVALIDDATA;
395 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
396 return D3DXERR_INVALIDDATA;
397 return D3DXAssembleShader(buffer, len, defines, include, flags,
398 shader, error_messages);
399 }
400
401 HRESULT WINAPI D3DXAssembleShaderFromResourceW(HMODULE module, const WCHAR *resource, const D3DXMACRO *defines,
402 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
403 {
404 void *buffer;
405 HRSRC res;
406 DWORD len;
407
408 TRACE("module %p, resource %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
409 module, debugstr_w(resource), defines, include, flags, shader, error_messages);
410
411 if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
412 return D3DXERR_INVALIDDATA;
413 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
414 return D3DXERR_INVALIDDATA;
415 return D3DXAssembleShader(buffer, len, defines, include, flags,
416 shader, error_messages);
417 }
418
419 HRESULT WINAPI D3DXCompileShader(const char *data, UINT length, const D3DXMACRO *defines,
420 ID3DXInclude *include, const char *function, const char *profile, DWORD flags,
421 ID3DXBuffer **shader, ID3DXBuffer **error_msgs, ID3DXConstantTable **constant_table)
422 {
423 HRESULT hr;
424
425 TRACE("data %s, length %u, defines %p, include %p, function %s, profile %s, "
426 "flags %#x, shader %p, error_msgs %p, constant_table %p.\n",
427 debugstr_a(data), length, defines, include, debugstr_a(function), debugstr_a(profile),
428 flags, shader, error_msgs, constant_table);
429
430 hr = D3DCompile(data, length, NULL, (D3D_SHADER_MACRO *)defines, (ID3DInclude *)include,
431 function, profile, flags, 0, (ID3DBlob **)shader, (ID3DBlob **)error_msgs);
432
433 if (SUCCEEDED(hr) && constant_table)
434 {
435 hr = D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*shader), constant_table);
436 if (FAILED(hr))
437 {
438 ID3DXBuffer_Release(*shader);
439 *shader = NULL;
440 }
441 }
442
443 return hr;
444 }
445
446 HRESULT WINAPI D3DXCompileShaderFromFileA(const char *filename, const D3DXMACRO *defines,
447 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
448 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
449 {
450 WCHAR *filename_w;
451 DWORD len;
452 HRESULT ret;
453
454 TRACE("filename %s, defines %p, include %p, entrypoint %s, profile %s, "
455 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
456 debugstr_a(filename), defines, include, debugstr_a(entrypoint),
457 debugstr_a(profile), flags, shader, error_messages, constant_table);
458
459 if (!filename) return D3DXERR_INVALIDDATA;
460
461 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
462 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
463 if (!filename_w) return E_OUTOFMEMORY;
464 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
465
466 ret = D3DXCompileShaderFromFileW(filename_w, defines, include,
467 entrypoint, profile, flags,
468 shader, error_messages, constant_table);
469
470 HeapFree(GetProcessHeap(), 0, filename_w);
471 return ret;
472 }
473
474 HRESULT WINAPI D3DXCompileShaderFromFileW(const WCHAR *filename, const D3DXMACRO *defines,
475 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
476 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
477 {
478 const void *buffer;
479 DWORD len, filename_len;
480 HRESULT hr;
481 struct D3DXIncludeImpl includefromfile;
482 char *filename_a;
483
484 TRACE("filename %s, defines %p, include %p, entrypoint %s, profile %s, "
485 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
486 debugstr_w(filename), defines, include, debugstr_a(entrypoint), debugstr_a(profile),
487 flags, shader, error_messages, constant_table);
488
489 if (!include)
490 {
491 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
492 include = &includefromfile.ID3DXInclude_iface;
493 }
494
495 filename_len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
496 filename_a = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(char));
497 if (!filename_a)
498 return E_OUTOFMEMORY;
499 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, filename_len, NULL, NULL);
500
501 EnterCriticalSection(&from_file_mutex);
502 hr = ID3DXInclude_Open(include, D3DXINC_LOCAL, filename_a, NULL, &buffer, &len);
503 if (FAILED(hr))
504 {
505 LeaveCriticalSection(&from_file_mutex);
506 HeapFree(GetProcessHeap(), 0, filename_a);
507 return D3DXERR_INVALIDDATA;
508 }
509
510 hr = D3DCompile(buffer, len, filename_a, (const D3D_SHADER_MACRO *)defines,
511 (ID3DInclude *)include, entrypoint, profile, flags, 0,
512 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
513
514 if (SUCCEEDED(hr) && constant_table)
515 hr = D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*shader),
516 constant_table);
517
518 ID3DXInclude_Close(include, buffer);
519 LeaveCriticalSection(&from_file_mutex);
520 HeapFree(GetProcessHeap(), 0, filename_a);
521 return hr;
522 }
523
524 HRESULT WINAPI D3DXCompileShaderFromResourceA(HMODULE module, const char *resource, const D3DXMACRO *defines,
525 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
526 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
527 {
528 void *buffer;
529 HRSRC res;
530 DWORD len;
531
532 TRACE("module %p, resource %s, defines %p, include %p, entrypoint %s, profile %s, "
533 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
534 module, debugstr_a(resource), defines, include, debugstr_a(entrypoint), debugstr_a(profile),
535 flags, shader, error_messages, constant_table);
536
537 if (!(res = FindResourceA(module, resource, (const char *)RT_RCDATA)))
538 return D3DXERR_INVALIDDATA;
539 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
540 return D3DXERR_INVALIDDATA;
541 return D3DXCompileShader(buffer, len, defines, include, entrypoint, profile,
542 flags, shader, error_messages, constant_table);
543 }
544
545 HRESULT WINAPI D3DXCompileShaderFromResourceW(HMODULE module, const WCHAR *resource, const D3DXMACRO *defines,
546 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
547 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
548 {
549 void *buffer;
550 HRSRC res;
551 DWORD len;
552
553 TRACE("module %p, resource %s, defines %p, include %p, entrypoint %s, profile %s, "
554 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
555 module, debugstr_w(resource), defines, include, debugstr_a(entrypoint), debugstr_a(profile),
556 flags, shader, error_messages, constant_table);
557
558 if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
559 return D3DXERR_INVALIDDATA;
560 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
561 return D3DXERR_INVALIDDATA;
562 return D3DXCompileShader(buffer, len, defines, include, entrypoint, profile,
563 flags, shader, error_messages, constant_table);
564 }
565
566 HRESULT WINAPI D3DXPreprocessShader(const char *data, UINT data_len, const D3DXMACRO *defines,
567 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
568 {
569 TRACE("data %s, data_len %u, defines %p, include %p, shader %p, error_messages %p.\n",
570 debugstr_a(data), data_len, defines, include, shader, error_messages);
571
572 return D3DPreprocess(data, data_len, NULL,
573 (const D3D_SHADER_MACRO *)defines, (ID3DInclude *)include,
574 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
575 }
576
577 HRESULT WINAPI D3DXPreprocessShaderFromFileA(const char *filename, const D3DXMACRO *defines,
578 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
579 {
580 WCHAR *filename_w = NULL;
581 DWORD len;
582 HRESULT ret;
583
584 TRACE("filename %s, defines %p, include %p, shader %p, error_messages %p.\n",
585 debugstr_a(filename), defines, include, shader, error_messages);
586
587 if (!filename) return D3DXERR_INVALIDDATA;
588
589 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
590 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
591 if (!filename_w) return E_OUTOFMEMORY;
592 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
593
594 ret = D3DXPreprocessShaderFromFileW(filename_w, defines, include, shader, error_messages);
595
596 HeapFree(GetProcessHeap(), 0, filename_w);
597 return ret;
598 }
599
600 HRESULT WINAPI D3DXPreprocessShaderFromFileW(const WCHAR *filename, const D3DXMACRO *defines,
601 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
602 {
603 const void *buffer;
604 DWORD len;
605 HRESULT hr;
606 struct D3DXIncludeImpl includefromfile;
607 char *filename_a;
608
609 TRACE("filename %s, defines %p, include %p, shader %p, error_messages %p.\n",
610 debugstr_w(filename), defines, include, shader, error_messages);
611
612 if (!include)
613 {
614 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
615 include = &includefromfile.ID3DXInclude_iface;
616 }
617
618 len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
619 filename_a = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
620 if (!filename_a)
621 return E_OUTOFMEMORY;
622 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, len, NULL, NULL);
623
624 EnterCriticalSection(&from_file_mutex);
625 hr = ID3DXInclude_Open(include, D3DXINC_LOCAL, filename_a, NULL, &buffer, &len);
626 if (FAILED(hr))
627 {
628 LeaveCriticalSection(&from_file_mutex);
629 HeapFree(GetProcessHeap(), 0, filename_a);
630 return D3DXERR_INVALIDDATA;
631 }
632
633 hr = D3DPreprocess(buffer, len, NULL,
634 (const D3D_SHADER_MACRO *)defines,
635 (ID3DInclude *) include,
636 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
637
638 ID3DXInclude_Close(include, buffer);
639 LeaveCriticalSection(&from_file_mutex);
640 HeapFree(GetProcessHeap(), 0, filename_a);
641 return hr;
642 }
643
644 HRESULT WINAPI D3DXPreprocessShaderFromResourceA(HMODULE module, const char *resource, const D3DXMACRO *defines,
645 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
646 {
647 void *buffer;
648 HRSRC res;
649 DWORD len;
650
651 TRACE("module %p, resource %s, defines %p, include %p, shader %p, error_messages %p.\n",
652 module, debugstr_a(resource), defines, include, shader, error_messages);
653
654 if (!(res = FindResourceA(module, resource, (const char *)RT_RCDATA)))
655 return D3DXERR_INVALIDDATA;
656 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
657 return D3DXERR_INVALIDDATA;
658 return D3DXPreprocessShader(buffer, len, defines, include,
659 shader, error_messages);
660 }
661
662 HRESULT WINAPI D3DXPreprocessShaderFromResourceW(HMODULE module, const WCHAR *resource, const D3DXMACRO *defines,
663 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
664 {
665 void *buffer;
666 HRSRC res;
667 DWORD len;
668
669 TRACE("module %p, resource %s, defines %p, include %p, shader %p, error_messages %p.\n",
670 module, debugstr_w(resource), defines, include, shader, error_messages);
671
672 if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
673 return D3DXERR_INVALIDDATA;
674 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
675 return D3DXERR_INVALIDDATA;
676 return D3DXPreprocessShader(buffer, len, defines, include,
677 shader, error_messages);
678
679 }
680
681 struct ID3DXConstantTableImpl {
682 ID3DXConstantTable ID3DXConstantTable_iface;
683 LONG ref;
684 char *ctab;
685 DWORD size;
686 D3DXCONSTANTTABLE_DESC desc;
687 struct ctab_constant *constants;
688 };
689
690 static void free_constant(struct ctab_constant *constant)
691 {
692 if (constant->constants)
693 {
694 UINT i, count = constant->desc.Elements > 1 ? constant->desc.Elements : constant->desc.StructMembers;
695
696 for (i = 0; i < count; ++i)
697 {
698 free_constant(&constant->constants[i]);
699 }
700 HeapFree(GetProcessHeap(), 0, constant->constants);
701 }
702 }
703
704 static void free_constant_table(struct ID3DXConstantTableImpl *table)
705 {
706 if (table->constants)
707 {
708 UINT i;
709
710 for (i = 0; i < table->desc.Constants; ++i)
711 {
712 free_constant(&table->constants[i]);
713 }
714 HeapFree(GetProcessHeap(), 0, table->constants);
715 }
716 HeapFree(GetProcessHeap(), 0, table->ctab);
717 }
718
719 static inline struct ID3DXConstantTableImpl *impl_from_ID3DXConstantTable(ID3DXConstantTable *iface)
720 {
721 return CONTAINING_RECORD(iface, struct ID3DXConstantTableImpl, ID3DXConstantTable_iface);
722 }
723
724 static inline BOOL is_vertex_shader(DWORD version)
725 {
726 return (version & 0xffff0000) == 0xfffe0000;
727 }
728
729 static inline D3DXHANDLE handle_from_constant(struct ctab_constant *constant)
730 {
731 return (D3DXHANDLE)constant;
732 }
733
734 static struct ctab_constant *get_constant_by_name(struct ID3DXConstantTableImpl *table,
735 struct ctab_constant *constant, const char *name);
736
737 static struct ctab_constant *get_constant_element_by_name(struct ctab_constant *constant, const char *name)
738 {
739 const char *part;
740 UINT element;
741
742 TRACE("constant %p, name %s\n", constant, debugstr_a(name));
743
744 if (!name || !*name) return NULL;
745
746 element = atoi(name);
747 part = strchr(name, ']') + 1;
748
749 if (constant->desc.Elements > element)
750 {
751 struct ctab_constant *c = constant->constants ? &constant->constants[element] : constant;
752
753 switch (*part++)
754 {
755 case '.':
756 return get_constant_by_name(NULL, c, part);
757
758 case '[':
759 return get_constant_element_by_name(c, part);
760
761 case '\0':
762 TRACE("Returning parameter %p\n", c);
763 return c;
764
765 default:
766 FIXME("Unhandled case \"%c\"\n", *--part);
767 break;
768 }
769 }
770
771 TRACE("Constant not found\n");
772 return NULL;
773 }
774
775 static struct ctab_constant *get_constant_by_name(struct ID3DXConstantTableImpl *table,
776 struct ctab_constant *constant, const char *name)
777 {
778 UINT i, count, length;
779 struct ctab_constant *handles;
780 const char *part;
781
782 TRACE("table %p, constant %p, name %s\n", table, constant, debugstr_a(name));
783
784 if (!name || !*name) return NULL;
785
786 if (!constant)
787 {
788 count = table->desc.Constants;
789 handles = table->constants;
790 }
791 else
792 {
793 count = constant->desc.StructMembers;
794 handles = constant->constants;
795 }
796
797 length = strcspn(name, "[.");
798 part = name + length;
799
800 for (i = 0; i < count; i++)
801 {
802 if (strlen(handles[i].desc.Name) == length && !strncmp(handles[i].desc.Name, name, length))
803 {
804 switch (*part++)
805 {
806 case '.':
807 return get_constant_by_name(NULL, &handles[i], part);
808
809 case '[':
810 return get_constant_element_by_name(&handles[i], part);
811
812 default:
813 TRACE("Returning parameter %p\n", &handles[i]);
814 return &handles[i];
815 }
816 }
817 }
818
819 TRACE("Constant not found\n");
820 return NULL;
821 }
822
823 static struct ctab_constant *is_valid_sub_constant(struct ctab_constant *parent, D3DXHANDLE handle)
824 {
825 struct ctab_constant *c;
826 UINT i, count;
827
828 /* all variable have at least elements = 1, but not always elements */
829 if (!parent->constants) return NULL;
830
831 count = parent->desc.Elements > 1 ? parent->desc.Elements : parent->desc.StructMembers;
832 for (i = 0; i < count; ++i)
833 {
834 if (handle_from_constant(&parent->constants[i]) == handle)
835 return &parent->constants[i];
836
837 c = is_valid_sub_constant(&parent->constants[i], handle);
838 if (c) return c;
839 }
840
841 return NULL;
842 }
843
844 static inline struct ctab_constant *get_valid_constant(struct ID3DXConstantTableImpl *table, D3DXHANDLE handle)
845 {
846 struct ctab_constant *c;
847 UINT i;
848
849 if (!handle) return NULL;
850
851 for (i = 0; i < table->desc.Constants; ++i)
852 {
853 if (handle_from_constant(&table->constants[i]) == handle)
854 return &table->constants[i];
855
856 c = is_valid_sub_constant(&table->constants[i], handle);
857 if (c) return c;
858 }
859
860 return get_constant_by_name(table, NULL, handle);
861 }
862
863 /*** IUnknown methods ***/
864 static HRESULT WINAPI ID3DXConstantTableImpl_QueryInterface(ID3DXConstantTable *iface, REFIID riid, void **out)
865 {
866 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
867
868 if (IsEqualGUID(riid, &IID_IUnknown) ||
869 IsEqualGUID(riid, &IID_ID3DXBuffer) ||
870 IsEqualGUID(riid, &IID_ID3DXConstantTable))
871 {
872 ID3DXConstantTable_AddRef(iface);
873 *out = iface;
874 return S_OK;
875 }
876
877 WARN("Interface %s not found.\n", debugstr_guid(riid));
878
879 return E_NOINTERFACE;
880 }
881
882 static ULONG WINAPI ID3DXConstantTableImpl_AddRef(ID3DXConstantTable *iface)
883 {
884 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
885
886 TRACE("(%p)->(): AddRef from %d\n", This, This->ref);
887
888 return InterlockedIncrement(&This->ref);
889 }
890
891 static ULONG WINAPI ID3DXConstantTableImpl_Release(ID3DXConstantTable *iface)
892 {
893 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
894 ULONG ref = InterlockedDecrement(&This->ref);
895
896 TRACE("(%p)->(): Release from %d\n", This, ref + 1);
897
898 if (!ref)
899 {
900 free_constant_table(This);
901 HeapFree(GetProcessHeap(), 0, This);
902 }
903
904 return ref;
905 }
906
907 /*** ID3DXBuffer methods ***/
908 static void * WINAPI ID3DXConstantTableImpl_GetBufferPointer(ID3DXConstantTable *iface)
909 {
910 struct ID3DXConstantTableImpl *table = impl_from_ID3DXConstantTable(iface);
911
912 TRACE("iface %p.\n", iface);
913
914 return table->ctab;
915 }
916
917 static DWORD WINAPI ID3DXConstantTableImpl_GetBufferSize(ID3DXConstantTable *iface)
918 {
919 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
920
921 TRACE("(%p)->()\n", This);
922
923 return This->size;
924 }
925
926 /*** ID3DXConstantTable methods ***/
927 static HRESULT WINAPI ID3DXConstantTableImpl_GetDesc(ID3DXConstantTable *iface, D3DXCONSTANTTABLE_DESC *desc)
928 {
929 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
930
931 TRACE("(%p)->(%p)\n", This, desc);
932
933 if (!desc)
934 return D3DERR_INVALIDCALL;
935
936 *desc = This->desc;
937
938 return D3D_OK;
939 }
940
941 const struct ctab_constant *d3dx_shader_get_ctab_constant(ID3DXConstantTable *iface, D3DXHANDLE constant)
942 {
943 struct ID3DXConstantTableImpl *ctab = impl_from_ID3DXConstantTable(iface);
944
945 return get_valid_constant(ctab, constant);
946 }
947
948 static HRESULT WINAPI ID3DXConstantTableImpl_GetConstantDesc(ID3DXConstantTable *iface, D3DXHANDLE constant,
949 D3DXCONSTANT_DESC *desc, UINT *count)
950 {
951 struct ID3DXConstantTableImpl *ctab = impl_from_ID3DXConstantTable(iface);
952 struct ctab_constant *c = get_valid_constant(ctab, constant);
953
954 TRACE("(%p)->(%p, %p, %p)\n", ctab, constant, desc, count);
955
956 if (!c)
957 {
958 WARN("Invalid argument specified\n");
959 return D3DERR_INVALIDCALL;
960 }
961
962 if (desc) *desc = c->desc;
963 if (count) *count = 1;
964
965 return D3D_OK;
966 }
967
968 static UINT WINAPI ID3DXConstantTableImpl_GetSamplerIndex(ID3DXConstantTable *iface, D3DXHANDLE constant)
969 {
970 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
971 struct ctab_constant *c = get_valid_constant(This, constant);
972
973 TRACE("(%p)->(%p)\n", This, constant);
974
975 if (!c || c->desc.RegisterSet != D3DXRS_SAMPLER)
976 {
977 WARN("Invalid argument specified\n");
978 return (UINT)-1;
979 }
980
981 TRACE("Returning RegisterIndex %u\n", c->desc.RegisterIndex);
982 return c->desc.RegisterIndex;
983 }
984
985 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstant(ID3DXConstantTable *iface, D3DXHANDLE constant, UINT index)
986 {
987 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
988 struct ctab_constant *c;
989
990 TRACE("(%p)->(%p, %d)\n", This, constant, index);
991
992 if (constant)
993 {
994 c = get_valid_constant(This, constant);
995 if (c && index < c->desc.StructMembers)
996 {
997 c = &c->constants[index];
998 TRACE("Returning constant %p\n", c);
999 return handle_from_constant(c);
1000 }
1001 }
1002 else
1003 {
1004 if (index < This->desc.Constants)
1005 {
1006 c = &This->constants[index];
1007 TRACE("Returning constant %p\n", c);
1008 return handle_from_constant(c);
1009 }
1010 }
1011
1012 WARN("Index out of range\n");
1013 return NULL;
1014 }
1015
1016 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantByName(ID3DXConstantTable *iface,
1017 D3DXHANDLE constant, const char *name)
1018 {
1019 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1020 struct ctab_constant *c = get_valid_constant(This, constant);
1021
1022 TRACE("iface %p, constant %p, name %s.\n", iface, constant, debugstr_a(name));
1023
1024 c = get_constant_by_name(This, c, name);
1025 TRACE("Returning constant %p\n", c);
1026
1027 return handle_from_constant(c);
1028 }
1029
1030 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantElement(ID3DXConstantTable *iface, D3DXHANDLE constant, UINT index)
1031 {
1032 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1033 struct ctab_constant *c = get_valid_constant(This, constant);
1034
1035 TRACE("(%p)->(%p, %d)\n", This, constant, index);
1036
1037 if (c && index < c->desc.Elements)
1038 {
1039 if (c->desc.Elements > 1) c = &c->constants[index];
1040 TRACE("Returning constant %p\n", c);
1041 return handle_from_constant(c);
1042 }
1043
1044 WARN("Invalid argument specified\n");
1045 return NULL;
1046 }
1047
1048 static inline DWORD get_index(const void **indata, UINT index, BOOL is_pointer)
1049 {
1050 if (!indata)
1051 return 0;
1052
1053 if (is_pointer)
1054 return ((DWORD **)indata)[index / 16][index % 16];
1055
1056 return (*((DWORD **)indata))[index];
1057 }
1058
1059 static UINT set(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, struct ctab_constant *constant,
1060 const void **indata, D3DXPARAMETER_TYPE intype, UINT *size, UINT incol, D3DXPARAMETER_CLASS inclass, UINT index,
1061 BOOL is_pointer)
1062 {
1063 D3DXCONSTANT_DESC *desc = &constant->desc;
1064 UINT l, i, regcount = 1, regsize = 1, cin = 1, rin = 1, ret, last = 0;
1065 DWORD tmp;
1066
1067 /* size too small to set anything */
1068 if (*size < desc->Rows * desc->Columns)
1069 {
1070 *size = 0;
1071 return 0;
1072 }
1073
1074 /* D3DXPC_STRUCT is somewhat special */
1075 if (desc->Class == D3DXPC_STRUCT)
1076 {
1077 /*
1078 * Struct array sets the last complete input to the first struct element, all other
1079 * elements are not set.
1080 * E.g.: struct {int i;} s1[2];
1081 * SetValue(device, "s1", [1, 2], 8) => s1 = {2, x};
1082 *
1083 * struct {int i; int n} s2[2];
1084 * SetValue(device, "s2", [1, 2, 3, 4, 5], 20) => s1 = {{3, 4}, {x, x}};
1085 */
1086 if (desc->Elements > 1)
1087 {
1088 UINT offset = *size / (desc->Rows * desc->Columns) - 1;
1089
1090 offset = min(desc->Elements - 1, offset);
1091 last = offset * desc->Rows * desc->Columns;
1092
1093 if ((is_pointer || inclass == D3DXPC_MATRIX_ROWS) && desc->RegisterSet != D3DXRS_BOOL)
1094 {
1095 set(table, device, &constant->constants[0], NULL, intype, size, incol, inclass, 0, is_pointer);
1096 }
1097 else
1098 {
1099 last += set(table, device, &constant->constants[0], indata, intype, size, incol, inclass,
1100 index + last, is_pointer);
1101 }
1102 }
1103 else
1104 {
1105 /*
1106 * D3DXRS_BOOL is always set. As there are only 16 bools and there are
1107 * exactly 16 input values, use matrix transpose.
1108 */
1109 if (inclass == D3DXPC_MATRIX_ROWS && desc->RegisterSet == D3DXRS_BOOL)
1110 {
1111 D3DXMATRIX mat, *m, min;
1112 D3DXMatrixTranspose(&mat, &min);
1113
1114 if (is_pointer)
1115 min = *(D3DXMATRIX *)(indata[index / 16]);
1116 else
1117 min = **(D3DXMATRIX **)indata;
1118
1119 D3DXMatrixTranspose(&mat, &min);
1120 m = &mat;
1121 for (i = 0; i < desc->StructMembers; ++i)
1122 {
1123 last += set(table, device, &constant->constants[i], (const void **)&m, intype, size, incol,
1124 D3DXPC_SCALAR, index + last, is_pointer);
1125 }
1126 }
1127 /*
1128 * For pointers or for matrix rows, only the first member is set.
1129 * All other members are set to 0. This is not true for D3DXRS_BOOL.
1130 * E.g.: struct {int i; int n} s;
1131 * SetValue(device, "s", [1, 2], 8) => s = {1, 0};
1132 */
1133 else if ((is_pointer || inclass == D3DXPC_MATRIX_ROWS) && desc->RegisterSet != D3DXRS_BOOL)
1134 {
1135 last = set(table, device, &constant->constants[0], indata, intype, size, incol, inclass,
1136 index + last, is_pointer);
1137
1138 for (i = 1; i < desc->StructMembers; ++i)
1139 {
1140 set(table, device, &constant->constants[i], NULL, intype, size, incol, inclass, 0, is_pointer);
1141 }
1142 }
1143 else
1144 {
1145 for (i = 0; i < desc->StructMembers; ++i)
1146 {
1147 last += set(table, device, &constant->constants[i], indata, intype, size, incol, D3DXPC_SCALAR,
1148 index + last, is_pointer);
1149 }
1150 }
1151 }
1152
1153 return last;
1154 }
1155
1156 /* elements */
1157 if (desc->Elements > 1)
1158 {
1159 for (i = 0; i < desc->Elements && *size > 0; ++i)
1160 {
1161 last += set(table, device, &constant->constants[i], indata, intype, size, incol, inclass,
1162 index + last, is_pointer);
1163
1164 /* adjust the vector size for matrix rows */
1165 if (inclass == D3DXPC_MATRIX_ROWS && desc->Class == D3DXPC_VECTOR && (i % 4) == 3)
1166 {
1167 last += 12;
1168 *size = *size < 12 ? 0 : *size - 12;
1169 }
1170 }
1171
1172 return last;
1173 }
1174
1175 switch (desc->Class)
1176 {
1177 case D3DXPC_SCALAR:
1178 case D3DXPC_VECTOR:
1179 case D3DXPC_MATRIX_ROWS:
1180 regcount = min(desc->RegisterCount, desc->Rows);
1181 if (inclass == D3DXPC_MATRIX_ROWS) cin = incol;
1182 else rin = incol;
1183 regsize = desc->Columns;
1184 break;
1185
1186 case D3DXPC_MATRIX_COLUMNS:
1187 regcount = min(desc->RegisterCount, desc->Columns);
1188 if (inclass == D3DXPC_MATRIX_ROWS) rin = incol;
1189 else cin = incol;
1190 regsize = desc->Rows;
1191 break;
1192
1193 default:
1194 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1195 return 0;
1196 }
1197
1198 /* specific stuff for different in types */
1199 switch (inclass)
1200 {
1201 case D3DXPC_SCALAR:
1202 ret = desc->Columns * desc->Rows;
1203 *size -= desc->Columns * desc->Rows;
1204 break;
1205
1206 case D3DXPC_VECTOR:
1207 switch (desc->Class)
1208 {
1209 case D3DXPC_MATRIX_ROWS:
1210 if (*size < regcount * 4)
1211 {
1212 *size = 0;
1213 return 0;
1214 }
1215 ret = 4 * regcount;
1216 *size -= 4 * regcount;
1217 break;
1218
1219 case D3DXPC_MATRIX_COLUMNS:
1220 ret = 4 * regsize;
1221 *size -= 4 * regcount;
1222 break;
1223
1224 case D3DXPC_SCALAR:
1225 ret = 1;
1226 *size -= ret;
1227 break;
1228
1229 case D3DXPC_VECTOR:
1230 ret = 4;
1231 *size -= ret;
1232 break;
1233
1234 default:
1235 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1236 return 0;
1237 }
1238 break;
1239
1240 case D3DXPC_MATRIX_ROWS:
1241 switch (desc->Class)
1242 {
1243 case D3DXPC_MATRIX_ROWS:
1244 case D3DXPC_MATRIX_COLUMNS:
1245 if (*size < 16)
1246 {
1247 *size = 0;
1248 return 0;
1249 }
1250 ret = 16;
1251 break;
1252
1253 case D3DXPC_SCALAR:
1254 ret = 4;
1255 break;
1256
1257 case D3DXPC_VECTOR:
1258 ret = 1;
1259 break;
1260
1261 default:
1262 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1263 return 0;
1264 }
1265 *size -= ret;
1266 break;
1267
1268 case D3DXPC_MATRIX_COLUMNS:
1269 switch (desc->Class)
1270 {
1271 case D3DXPC_MATRIX_ROWS:
1272 case D3DXPC_MATRIX_COLUMNS:
1273 if (*size < 16)
1274 {
1275 *size = 0;
1276 return 0;
1277 }
1278 ret = 16;
1279 break;
1280
1281 case D3DXPC_SCALAR:
1282 ret = 1;
1283 break;
1284
1285 case D3DXPC_VECTOR:
1286 ret = 4;
1287 break;
1288
1289 default:
1290 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1291 return 0;
1292 }
1293 *size -= ret;
1294 break;
1295
1296 default:
1297 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(inclass));
1298 return 0;
1299 }
1300
1301 /* set the registers */
1302 switch (desc->RegisterSet)
1303 {
1304 case D3DXRS_BOOL:
1305 regcount = min(desc->RegisterCount, desc->Columns * desc->Rows);
1306 l = 0;
1307 for (i = 0; i < regcount; ++i)
1308 {
1309 BOOL out;
1310 DWORD t = get_index(indata, index + i / regsize * rin + l * cin, is_pointer);
1311
1312 set_number(&tmp, desc->Type, &t, intype);
1313 set_number(&out, D3DXPT_BOOL, &tmp, desc->Type);
1314 if (is_vertex_shader(table->desc.Version))
1315 IDirect3DDevice9_SetVertexShaderConstantB(device, desc->RegisterIndex + i, &out, 1);
1316 else
1317 IDirect3DDevice9_SetPixelShaderConstantB(device, desc->RegisterIndex + i, &out, 1);
1318
1319 if (++l >= regsize) l = 0;
1320 }
1321 return ret;
1322
1323 case D3DXRS_INT4:
1324 for (i = 0; i < regcount; ++i)
1325 {
1326 INT vec[4] = {0, 0, 1, 0};
1327
1328 for (l = 0; l < regsize; ++l)
1329 {
1330 DWORD t = get_index(indata, index + i * rin + l * cin, is_pointer);
1331
1332 set_number(&tmp, desc->Type, &t, intype);
1333 set_number(&vec[l], D3DXPT_INT, &tmp, desc->Type);
1334 }
1335 if (is_vertex_shader(table->desc.Version))
1336 IDirect3DDevice9_SetVertexShaderConstantI(device, desc->RegisterIndex + i, vec, 1);
1337 else
1338 IDirect3DDevice9_SetPixelShaderConstantI(device, desc->RegisterIndex + i, vec, 1);
1339 }
1340 return ret;
1341
1342 case D3DXRS_FLOAT4:
1343 for (i = 0; i < regcount; ++i)
1344 {
1345 FLOAT vec[4] = {0};
1346
1347 for (l = 0; l < regsize; ++l)
1348 {
1349 DWORD t = get_index(indata, index + i * rin + l * cin, is_pointer);
1350
1351 set_number(&tmp, desc->Type, &t, intype);
1352 set_number(&vec[l], D3DXPT_FLOAT, &tmp, desc->Type);
1353 }
1354 if (is_vertex_shader(table->desc.Version))
1355 IDirect3DDevice9_SetVertexShaderConstantF(device, desc->RegisterIndex + i, vec, 1);
1356 else
1357 IDirect3DDevice9_SetPixelShaderConstantF(device, desc->RegisterIndex + i, vec, 1);
1358 }
1359 return ret;
1360
1361 default:
1362 FIXME("Unhandled register set %s\n", debug_d3dxparameter_registerset(desc->RegisterSet));
1363 return 0;
1364 }
1365 }
1366
1367 static HRESULT set_scalar(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1368 const void *indata, D3DXPARAMETER_TYPE intype)
1369 {
1370 struct ctab_constant *c = get_valid_constant(table, constant);
1371 UINT count = 1;
1372
1373 if (!c)
1374 {
1375 WARN("Invalid argument specified\n");
1376 return D3DERR_INVALIDCALL;
1377 }
1378
1379 switch (c->desc.Class)
1380 {
1381 case D3DXPC_SCALAR:
1382 set(table, device, c, &indata, intype, &count, c->desc.Columns, D3DXPC_SCALAR, 0, FALSE);
1383 return D3D_OK;
1384
1385 case D3DXPC_VECTOR:
1386 case D3DXPC_MATRIX_ROWS:
1387 case D3DXPC_MATRIX_COLUMNS:
1388 case D3DXPC_STRUCT:
1389 return D3D_OK;
1390
1391 default:
1392 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1393 return D3DERR_INVALIDCALL;
1394 }
1395 }
1396
1397 static HRESULT set_scalar_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1398 const void *indata, UINT count, D3DXPARAMETER_TYPE intype)
1399 {
1400 struct ctab_constant *c = get_valid_constant(table, constant);
1401
1402 if (!c)
1403 {
1404 WARN("Invalid argument specified\n");
1405 return D3DERR_INVALIDCALL;
1406 }
1407
1408 switch (c->desc.Class)
1409 {
1410 case D3DXPC_SCALAR:
1411 case D3DXPC_VECTOR:
1412 case D3DXPC_MATRIX_ROWS:
1413 case D3DXPC_MATRIX_COLUMNS:
1414 case D3DXPC_STRUCT:
1415 set(table, device, c, &indata, intype, &count, c->desc.Columns, D3DXPC_SCALAR, 0, FALSE);
1416 return D3D_OK;
1417
1418 default:
1419 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1420 return D3DERR_INVALIDCALL;
1421 }
1422 }
1423
1424 static HRESULT set_vector(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1425 const void *indata, D3DXPARAMETER_TYPE intype)
1426 {
1427 struct ctab_constant *c = get_valid_constant(table, constant);
1428 UINT count = 4;
1429
1430 if (!c)
1431 {
1432 WARN("Invalid argument specified\n");
1433 return D3DERR_INVALIDCALL;
1434 }
1435
1436 switch (c->desc.Class)
1437 {
1438 case D3DXPC_SCALAR:
1439 case D3DXPC_VECTOR:
1440 case D3DXPC_STRUCT:
1441 set(table, device, c, &indata, intype, &count, 4, D3DXPC_VECTOR, 0, FALSE);
1442 return D3D_OK;
1443
1444 case D3DXPC_MATRIX_ROWS:
1445 case D3DXPC_MATRIX_COLUMNS:
1446 return D3D_OK;
1447
1448 default:
1449 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1450 return D3DERR_INVALIDCALL;
1451 }
1452 }
1453
1454 static HRESULT set_vector_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1455 const void *indata, UINT count, D3DXPARAMETER_TYPE intype)
1456 {
1457 struct ctab_constant *c = get_valid_constant(table, constant);
1458
1459 if (!c)
1460 {
1461 WARN("Invalid argument specified\n");
1462 return D3DERR_INVALIDCALL;
1463 }
1464
1465 switch (c->desc.Class)
1466 {
1467 case D3DXPC_SCALAR:
1468 case D3DXPC_VECTOR:
1469 case D3DXPC_MATRIX_ROWS:
1470 case D3DXPC_MATRIX_COLUMNS:
1471 case D3DXPC_STRUCT:
1472 count *= 4;
1473 set(table, device, c, &indata, intype, &count, 4, D3DXPC_VECTOR, 0, FALSE);
1474 return D3D_OK;
1475
1476 default:
1477 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1478 return D3DERR_INVALIDCALL;
1479 }
1480 }
1481
1482 static HRESULT set_matrix_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1483 const void *indata, UINT count, BOOL transpose)
1484 {
1485 struct ctab_constant *c = get_valid_constant(table, constant);
1486
1487 if (!c)
1488 {
1489 WARN("Invalid argument specified\n");
1490 return D3DERR_INVALIDCALL;
1491 }
1492
1493 switch (c->desc.Class)
1494 {
1495 case D3DXPC_SCALAR:
1496 case D3DXPC_VECTOR:
1497 case D3DXPC_MATRIX_ROWS:
1498 case D3DXPC_MATRIX_COLUMNS:
1499 case D3DXPC_STRUCT:
1500 count *= 16;
1501 set(table, device, c, &indata, D3DXPT_FLOAT, &count, 4,
1502 transpose ? D3DXPC_MATRIX_ROWS : D3DXPC_MATRIX_COLUMNS, 0, FALSE);
1503 return D3D_OK;
1504
1505 default:
1506 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1507 return D3DERR_INVALIDCALL;
1508 }
1509 }
1510
1511 static HRESULT set_matrix_pointer_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device,
1512 D3DXHANDLE constant, const void **indata, UINT count, BOOL transpose)
1513 {
1514 struct ctab_constant *c = get_valid_constant(table, constant);
1515
1516 if (!c)
1517 {
1518 WARN("Invalid argument specified\n");
1519 return D3DERR_INVALIDCALL;
1520 }
1521
1522 switch (c->desc.Class)
1523 {
1524 case D3DXPC_SCALAR:
1525 case D3DXPC_VECTOR:
1526 case D3DXPC_MATRIX_ROWS:
1527 case D3DXPC_MATRIX_COLUMNS:
1528 case D3DXPC_STRUCT:
1529 count *= 16;
1530 set(table, device, c, indata, D3DXPT_FLOAT, &count, 4,
1531 transpose ? D3DXPC_MATRIX_ROWS : D3DXPC_MATRIX_COLUMNS, 0, TRUE);
1532 return D3D_OK;
1533
1534 default:
1535 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1536 return D3DERR_INVALIDCALL;
1537 }
1538 }
1539
1540 static HRESULT WINAPI ID3DXConstantTableImpl_SetDefaults(struct ID3DXConstantTable *iface,
1541 struct IDirect3DDevice9 *device)
1542 {
1543 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1544 UINT i;
1545
1546 TRACE("iface %p, device %p\n", iface, device);
1547
1548 if (!device)
1549 {
1550 WARN("Invalid argument specified\n");
1551 return D3DERR_INVALIDCALL;
1552 }
1553
1554 for (i = 0; i < This->desc.Constants; i++)
1555 {
1556 D3DXCONSTANT_DESC *desc = &This->constants[i].desc;
1557 HRESULT hr;
1558
1559 if (!desc->DefaultValue)
1560 continue;
1561
1562 switch (desc->RegisterSet)
1563 {
1564 case D3DXRS_BOOL:
1565 if (is_vertex_shader(This->desc.Version))
1566 hr = IDirect3DDevice9_SetVertexShaderConstantB(device, desc->RegisterIndex, desc->DefaultValue,
1567 desc->RegisterCount);
1568 else
1569 hr = IDirect3DDevice9_SetPixelShaderConstantB(device, desc->RegisterIndex, desc->DefaultValue,
1570 desc->RegisterCount);
1571 break;
1572
1573 case D3DXRS_INT4:
1574 if (is_vertex_shader(This->desc.Version))
1575 hr = IDirect3DDevice9_SetVertexShaderConstantI(device, desc->RegisterIndex, desc->DefaultValue,
1576 desc->RegisterCount);
1577 else
1578 hr = IDirect3DDevice9_SetPixelShaderConstantI(device, desc->RegisterIndex, desc->DefaultValue,
1579 desc->RegisterCount);
1580 break;
1581
1582 case D3DXRS_FLOAT4:
1583 if (is_vertex_shader(This->desc.Version))
1584 hr = IDirect3DDevice9_SetVertexShaderConstantF(device, desc->RegisterIndex, desc->DefaultValue,
1585 desc->RegisterCount);
1586 else
1587 hr = IDirect3DDevice9_SetPixelShaderConstantF(device, desc->RegisterIndex, desc->DefaultValue,
1588 desc->RegisterCount);
1589 break;
1590
1591 default:
1592 FIXME("Unhandled register set %s\n", debug_d3dxparameter_registerset(desc->RegisterSet));
1593 hr = E_NOTIMPL;
1594 break;
1595 }
1596
1597 if (hr != D3D_OK)
1598 return hr;
1599 }
1600
1601 return D3D_OK;
1602 }
1603
1604 static HRESULT WINAPI ID3DXConstantTableImpl_SetValue(struct ID3DXConstantTable *iface,
1605 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const void *data, unsigned int bytes)
1606 {
1607 struct ID3DXConstantTableImpl *table = impl_from_ID3DXConstantTable(iface);
1608 struct ctab_constant *c = get_valid_constant(table, constant);
1609 D3DXCONSTANT_DESC *desc;
1610
1611 TRACE("iface %p, device %p, constant %p, data %p, bytes %u\n", iface, device, constant, data, bytes);
1612
1613 if (!device || !c || !data)
1614 {
1615 WARN("Invalid argument specified\n");
1616 return D3DERR_INVALIDCALL;
1617 }
1618
1619 desc = &c->desc;
1620
1621 switch (desc->Class)
1622 {
1623 case D3DXPC_SCALAR:
1624 case D3DXPC_VECTOR:
1625 case D3DXPC_MATRIX_ROWS:
1626 case D3DXPC_MATRIX_COLUMNS:
1627 case D3DXPC_STRUCT:
1628 bytes /= 4;
1629 set(table, device, c, &data, desc->Type, &bytes, desc->Columns, D3DXPC_SCALAR, 0, FALSE);
1630 return D3D_OK;
1631
1632 default:
1633 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(desc->Class));
1634 return D3DERR_INVALIDCALL;
1635 }
1636 }
1637
1638 static HRESULT WINAPI ID3DXConstantTableImpl_SetBool(struct ID3DXConstantTable *iface,
1639 struct IDirect3DDevice9 *device, D3DXHANDLE constant, BOOL b)
1640 {
1641 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1642
1643 TRACE("iface %p, device %p, constant %p, b %d\n", iface, device, constant, b);
1644
1645 return set_scalar(This, device, constant, &b, D3DXPT_BOOL);
1646 }
1647
1648 static HRESULT WINAPI ID3DXConstantTableImpl_SetBoolArray(struct ID3DXConstantTable *iface,
1649 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const BOOL *b, UINT count)
1650 {
1651 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1652
1653 TRACE("iface %p, device %p, constant %p, b %p, count %d\n", iface, device, constant, b, count);
1654
1655 return set_scalar_array(This, device, constant, b, count, D3DXPT_BOOL);
1656 }
1657
1658 static HRESULT WINAPI ID3DXConstantTableImpl_SetInt(struct ID3DXConstantTable *iface,
1659 struct IDirect3DDevice9 *device, D3DXHANDLE constant, INT n)
1660 {
1661 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1662
1663 TRACE("iface %p, device %p, constant %p, n %d\n", iface, device, constant, n);
1664
1665 return set_scalar(This, device, constant, &n, D3DXPT_INT);
1666 }
1667
1668 static HRESULT WINAPI ID3DXConstantTableImpl_SetIntArray(struct ID3DXConstantTable *iface,
1669 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const INT *n, UINT count)
1670 {
1671 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1672
1673 TRACE("iface %p, device %p, constant %p, n %p, count %d\n", iface, device, constant, n, count);
1674
1675 return set_scalar_array(This, device, constant, n, count, D3DXPT_INT);
1676 }
1677
1678 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloat(struct ID3DXConstantTable *iface,
1679 struct IDirect3DDevice9 *device, D3DXHANDLE constant, float f)
1680 {
1681 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1682
1683 TRACE("iface %p, device %p, constant %p, f %f\n", iface, device, constant, f);
1684
1685 return set_scalar(This, device, constant, &f, D3DXPT_FLOAT);
1686 }
1687
1688 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloatArray(struct ID3DXConstantTable *iface,
1689 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const float *f, UINT count)
1690 {
1691 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1692
1693 TRACE("iface %p, device %p, constant %p, f %p, count %d\n", iface, device, constant, f, count);
1694
1695 return set_scalar_array(This, device, constant, f, count, D3DXPT_FLOAT);
1696 }
1697
1698 static HRESULT WINAPI ID3DXConstantTableImpl_SetVector(struct ID3DXConstantTable *iface,
1699 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXVECTOR4 *vector)
1700 {
1701 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1702
1703 TRACE("iface %p, device %p, constant %p, vector %p\n", iface, device, constant, vector);
1704
1705 return set_vector(This, device, constant, vector, D3DXPT_FLOAT);
1706 }
1707
1708 static HRESULT WINAPI ID3DXConstantTableImpl_SetVectorArray(struct ID3DXConstantTable *iface,
1709 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXVECTOR4 *vector, UINT count)
1710 {
1711 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1712
1713 TRACE("iface %p, device %p, constant %p, vector %p, count %u\n", iface, device, constant, vector, count);
1714
1715 return set_vector_array(This, device, constant, vector, count, D3DXPT_FLOAT);
1716 }
1717
1718 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrix(struct ID3DXConstantTable *iface,
1719 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix)
1720 {
1721 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1722
1723 TRACE("iface %p, device %p, constant %p, matrix %p\n", iface, device, constant, matrix);
1724
1725 return set_matrix_array(This, device, constant, matrix, 1, FALSE);
1726 }
1727
1728 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixArray(struct ID3DXConstantTable *iface,
1729 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
1730 {
1731 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1732
1733 TRACE("iface %p, device %p, constant %p, matrix %p, count %u\n", iface, device, constant, matrix, count);
1734
1735 return set_matrix_array(This, device, constant, matrix, count, FALSE);
1736 }
1737
1738 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixPointerArray(struct ID3DXConstantTable *iface,
1739 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
1740 {
1741 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1742
1743 TRACE("iface %p, device %p, constant %p, matrix %p, count %u)\n", iface, device, constant, matrix, count);
1744
1745 return set_matrix_pointer_array(This, device, constant, (const void **)matrix, count, FALSE);
1746 }
1747
1748 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTranspose(struct ID3DXConstantTable *iface,
1749 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix)
1750 {
1751 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1752
1753 TRACE("iface %p, device %p, constant %p, matrix %p\n", iface, device, constant, matrix);
1754
1755 return set_matrix_array(This, device, constant, matrix, 1, TRUE);
1756 }
1757
1758 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposeArray(struct ID3DXConstantTable *iface,
1759 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
1760 {
1761 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1762
1763 TRACE("iface %p, device %p, constant %p, matrix %p, count %u\n", iface, device, constant, matrix, count);
1764
1765 return set_matrix_array(This, device, constant, matrix, count, TRUE);
1766 }
1767
1768 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposePointerArray(struct ID3DXConstantTable *iface,
1769 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
1770 {
1771 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1772
1773 TRACE("iface %p, device %p, constant %p, matrix %p, count %u)\n", iface, device, constant, matrix, count);
1774
1775 return set_matrix_pointer_array(This, device, constant, (const void **)matrix, count, TRUE);
1776 }
1777
1778 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl =
1779 {
1780 /*** IUnknown methods ***/
1781 ID3DXConstantTableImpl_QueryInterface,
1782 ID3DXConstantTableImpl_AddRef,
1783 ID3DXConstantTableImpl_Release,
1784 /*** ID3DXBuffer methods ***/
1785 ID3DXConstantTableImpl_GetBufferPointer,
1786 ID3DXConstantTableImpl_GetBufferSize,
1787 /*** ID3DXConstantTable methods ***/
1788 ID3DXConstantTableImpl_GetDesc,
1789 ID3DXConstantTableImpl_GetConstantDesc,
1790 ID3DXConstantTableImpl_GetSamplerIndex,
1791 ID3DXConstantTableImpl_GetConstant,
1792 ID3DXConstantTableImpl_GetConstantByName,
1793 ID3DXConstantTableImpl_GetConstantElement,
1794 ID3DXConstantTableImpl_SetDefaults,
1795 ID3DXConstantTableImpl_SetValue,
1796 ID3DXConstantTableImpl_SetBool,
1797 ID3DXConstantTableImpl_SetBoolArray,
1798 ID3DXConstantTableImpl_SetInt,
1799 ID3DXConstantTableImpl_SetIntArray,
1800 ID3DXConstantTableImpl_SetFloat,
1801 ID3DXConstantTableImpl_SetFloatArray,
1802 ID3DXConstantTableImpl_SetVector,
1803 ID3DXConstantTableImpl_SetVectorArray,
1804 ID3DXConstantTableImpl_SetMatrix,
1805 ID3DXConstantTableImpl_SetMatrixArray,
1806 ID3DXConstantTableImpl_SetMatrixPointerArray,
1807 ID3DXConstantTableImpl_SetMatrixTranspose,
1808 ID3DXConstantTableImpl_SetMatrixTransposeArray,
1809 ID3DXConstantTableImpl_SetMatrixTransposePointerArray
1810 };
1811
1812 static HRESULT parse_ctab_constant_type(const char *ctab, DWORD typeoffset, struct ctab_constant *constant,
1813 BOOL is_element, WORD index, WORD max_index, DWORD *offset, DWORD nameoffset, UINT regset)
1814 {
1815 const D3DXSHADER_TYPEINFO *type = (LPD3DXSHADER_TYPEINFO)(ctab + typeoffset);
1816 const D3DXSHADER_STRUCTMEMBERINFO *memberinfo = NULL;
1817 HRESULT hr = D3D_OK;
1818 UINT i, count = 0;
1819 WORD size = 0;
1820
1821 constant->desc.DefaultValue = offset ? ctab + *offset : NULL;
1822 constant->desc.Class = type->Class;
1823 constant->desc.Type = type->Type;
1824 constant->desc.Rows = type->Rows;
1825 constant->desc.Columns = type->Columns;
1826 constant->desc.Elements = is_element ? 1 : type->Elements;
1827 constant->desc.StructMembers = type->StructMembers;
1828 constant->desc.Name = ctab + nameoffset;
1829 constant->desc.RegisterSet = regset;
1830 constant->desc.RegisterIndex = index;
1831
1832 TRACE("name %s, elements %u, index %u, defaultvalue %p, regset %s\n", constant->desc.Name,
1833 constant->desc.Elements, index, constant->desc.DefaultValue,
1834 debug_d3dxparameter_registerset(regset));
1835 TRACE("class %s, type %s, rows %d, columns %d, elements %d, struct_members %d\n",
1836 debug_d3dxparameter_class(type->Class), debug_d3dxparameter_type(type->Type),
1837 type->Rows, type->Columns, type->Elements, type->StructMembers);
1838
1839 if (type->Elements > 1 && !is_element)
1840 {
1841 count = type->Elements;
1842 }
1843 else if ((type->Class == D3DXPC_STRUCT) && type->StructMembers)
1844 {
1845 memberinfo = (D3DXSHADER_STRUCTMEMBERINFO*)(ctab + type->StructMemberInfo);
1846 count = type->StructMembers;
1847 }
1848
1849 if (count)
1850 {
1851 constant->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*constant->constants) * count);
1852 if (!constant->constants)
1853 {
1854 ERR("Out of memory\n");
1855 hr = E_OUTOFMEMORY;
1856 goto error;
1857 }
1858
1859 for (i = 0; i < count; ++i)
1860 {
1861 hr = parse_ctab_constant_type(ctab, memberinfo ? memberinfo[i].TypeInfo : typeoffset,
1862 &constant->constants[i], memberinfo == NULL, index + size, max_index, offset,
1863 memberinfo ? memberinfo[i].Name : nameoffset, regset);
1864 if (hr != D3D_OK)
1865 goto error;
1866
1867 size += constant->constants[i].desc.RegisterCount;
1868 }
1869 }
1870 else
1871 {
1872 WORD offsetdiff = type->Columns * type->Rows;
1873 BOOL fail = FALSE;
1874
1875 size = type->Columns * type->Rows;
1876
1877 switch (regset)
1878 {
1879 case D3DXRS_BOOL:
1880 fail = type->Class != D3DXPC_SCALAR && type->Class != D3DXPC_VECTOR
1881 && type->Class != D3DXPC_MATRIX_ROWS && type->Class != D3DXPC_MATRIX_COLUMNS;
1882 break;
1883
1884 case D3DXRS_FLOAT4:
1885 case D3DXRS_INT4:
1886 switch (type->Class)
1887 {
1888 case D3DXPC_VECTOR:
1889 size = 1;
1890 /* fall through */
1891 case D3DXPC_SCALAR:
1892 offsetdiff = type->Rows * 4;
1893 break;
1894
1895 case D3DXPC_MATRIX_ROWS:
1896 offsetdiff = type->Rows * 4;
1897 size = type->Rows;
1898 break;
1899
1900 case D3DXPC_MATRIX_COLUMNS:
1901 offsetdiff = type->Columns * 4;
1902 size = type->Columns;
1903 break;
1904
1905 default:
1906 fail = TRUE;
1907 break;
1908 }
1909 break;
1910
1911 case D3DXRS_SAMPLER:
1912 size = 1;
1913 fail = type->Class != D3DXPC_OBJECT;
1914 break;
1915
1916 default:
1917 fail = TRUE;
1918 break;
1919 }
1920
1921 if (fail)
1922 {
1923 FIXME("Unhandled register set %s, type class %s\n", debug_d3dxparameter_registerset(regset),
1924 debug_d3dxparameter_class(type->Class));
1925 }
1926
1927 /* offset in bytes => offsetdiff * sizeof(DWORD) */
1928 if (offset) *offset += offsetdiff * 4;
1929 }
1930
1931 constant->desc.RegisterCount = max(0, min(max_index - index, size));
1932 constant->desc.Bytes = 4 * constant->desc.Elements * type->Rows * type->Columns;
1933
1934 return D3D_OK;
1935
1936 error:
1937 if (constant->constants)
1938 {
1939 for (i = 0; i < count; ++i)
1940 {
1941 free_constant(&constant->constants[i]);
1942 }
1943 HeapFree(GetProcessHeap(), 0, constant->constants);
1944 constant->constants = NULL;
1945 }
1946
1947 return hr;
1948 }
1949
1950 HRESULT WINAPI D3DXGetShaderConstantTableEx(const DWORD *byte_code, DWORD flags, ID3DXConstantTable **constant_table)
1951 {
1952 struct ID3DXConstantTableImpl *object = NULL;
1953 const void *data;
1954 HRESULT hr;
1955 UINT size;
1956 const D3DXSHADER_CONSTANTTABLE *ctab_header;
1957 const D3DXSHADER_CONSTANTINFO *constant_info;
1958 DWORD i;
1959
1960 TRACE("byte_code %p, flags %x, constant_table %p\n", byte_code, flags, constant_table);
1961
1962 if (constant_table) *constant_table = NULL;
1963
1964 if (!byte_code || !constant_table)
1965 {
1966 WARN("Invalid argument specified.\n");
1967 return D3DERR_INVALIDCALL;
1968 }
1969
1970 if (!is_valid_bytecode(*byte_code))
1971 {
1972 WARN("Invalid byte_code specified.\n");
1973 return D3D_OK;
1974 }
1975
1976 if (flags) FIXME("Flags (%#x) are not handled, yet!\n", flags);
1977
1978 hr = D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), &data, &size);
1979 if (hr != D3D_OK)
1980 {
1981 WARN("CTAB not found.\n");
1982 return D3DXERR_INVALIDDATA;
1983 }
1984
1985 if (size < sizeof(*ctab_header))
1986 {
1987 WARN("Invalid CTAB size.\n");
1988 return D3DXERR_INVALIDDATA;
1989 }
1990
1991 ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
1992 if (ctab_header->Size != sizeof(*ctab_header))
1993 {
1994 WARN("Invalid D3DXSHADER_CONSTANTTABLE size.\n");
1995 return D3DXERR_INVALIDDATA;
1996 }
1997
1998 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1999 if (!object)
2000 return E_OUTOFMEMORY;
2001
2002 object->ID3DXConstantTable_iface.lpVtbl = &ID3DXConstantTable_Vtbl;
2003 object->ref = 1;
2004
2005 object->ctab = HeapAlloc(GetProcessHeap(), 0, size);
2006 if (!object->ctab)
2007 {
2008 ERR("Out of memory\n");
2009 HeapFree(GetProcessHeap(), 0, object);
2010 return E_OUTOFMEMORY;
2011 }
2012 object->size = size;
2013 memcpy(object->ctab, data, object->size);
2014
2015 object->desc.Creator = ctab_header->Creator ? object->ctab + ctab_header->Creator : NULL;
2016 object->desc.Version = ctab_header->Version;
2017 object->desc.Constants = ctab_header->Constants;
2018 TRACE("Creator %s, Version %x, Constants %u, Target %s\n",
2019 debugstr_a(object->desc.Creator), object->desc.Version, object->desc.Constants,
2020 debugstr_a(ctab_header->Target ? object->ctab + ctab_header->Target : NULL));
2021
2022 object->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2023 sizeof(*object->constants) * object->desc.Constants);
2024 if (!object->constants)
2025 {
2026 ERR("Out of memory\n");
2027 hr = E_OUTOFMEMORY;
2028 goto error;
2029 }
2030
2031 constant_info = (const D3DXSHADER_CONSTANTINFO *)(object->ctab + ctab_header->ConstantInfo);
2032 for (i = 0; i < ctab_header->Constants; i++)
2033 {
2034 DWORD offset = constant_info[i].DefaultValue;
2035
2036 hr = parse_ctab_constant_type(object->ctab, constant_info[i].TypeInfo,
2037 &object->constants[i], FALSE, constant_info[i].RegisterIndex,
2038 constant_info[i].RegisterIndex + constant_info[i].RegisterCount,
2039 offset ? &offset : NULL, constant_info[i].Name, constant_info[i].RegisterSet);
2040 if (hr != D3D_OK)
2041 goto error;
2042
2043 /*
2044 * Set the register count, it may differ for D3DXRS_INT4, because somehow
2045 * it makes the assumption that the register size is 1 instead of 4, so the
2046 * count is 4 times bigger. This holds true only for toplevel shader
2047 * constants. The count of elements and members is always based on a
2048 * register size of 4.
2049 */
2050 if (object->constants[i].desc.RegisterSet == D3DXRS_INT4)
2051 {
2052 object->constants[i].desc.RegisterCount = constant_info[i].RegisterCount;
2053 }
2054 object->constants[i].constantinfo_reserved = constant_info[i].Reserved;
2055 }
2056
2057 *constant_table = &object->ID3DXConstantTable_iface;
2058
2059 return D3D_OK;
2060
2061 error:
2062 free_constant_table(object);
2063 HeapFree(GetProcessHeap(), 0, object);
2064
2065 return hr;
2066 }
2067
2068 HRESULT WINAPI D3DXGetShaderConstantTable(const DWORD *byte_code, ID3DXConstantTable **constant_table)
2069 {
2070 TRACE("(%p, %p): Forwarded to D3DXGetShaderConstantTableEx\n", byte_code, constant_table);
2071
2072 return D3DXGetShaderConstantTableEx(byte_code, 0, constant_table);
2073 }
2074
2075 HRESULT WINAPI D3DXCreateFragmentLinker(IDirect3DDevice9 *device, UINT size, ID3DXFragmentLinker **linker)
2076 {
2077 FIXME("device %p, size %u, linker %p: stub.\n", device, size, linker);
2078
2079 if (linker)
2080 *linker = NULL;
2081
2082
2083 return E_NOTIMPL;
2084 }
2085
2086 HRESULT WINAPI D3DXCreateFragmentLinkerEx(IDirect3DDevice9 *device, UINT size, DWORD flags, ID3DXFragmentLinker **linker)
2087 {
2088 FIXME("device %p, size %u, flags %#x, linker %p: stub.\n", device, size, flags, linker);
2089
2090 if (linker)
2091 *linker = NULL;
2092
2093 return E_NOTIMPL;
2094 }
2095
2096 HRESULT WINAPI D3DXGetShaderSamplers(const DWORD *byte_code, const char **samplers, UINT *count)
2097 {
2098 UINT i, sampler_count = 0;
2099 UINT size;
2100 const char *data;
2101 const D3DXSHADER_CONSTANTTABLE *ctab_header;
2102 const D3DXSHADER_CONSTANTINFO *constant_info;
2103
2104 TRACE("byte_code %p, samplers %p, count %p\n", byte_code, samplers, count);
2105
2106 if (count) *count = 0;
2107
2108 if (D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), (const void **)&data, &size) != D3D_OK)
2109 return D3D_OK;
2110
2111 if (size < sizeof(*ctab_header)) return D3D_OK;
2112
2113 ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
2114 if (ctab_header->Size != sizeof(*ctab_header)) return D3D_OK;
2115
2116 constant_info = (const D3DXSHADER_CONSTANTINFO *)(data + ctab_header->ConstantInfo);
2117 for (i = 0; i < ctab_header->Constants; i++)
2118 {
2119 const D3DXSHADER_TYPEINFO *type;
2120
2121 TRACE("name = %s\n", data + constant_info[i].Name);
2122
2123 type = (const D3DXSHADER_TYPEINFO *)(data + constant_info[i].TypeInfo);
2124
2125 if (type->Type == D3DXPT_SAMPLER
2126 || type->Type == D3DXPT_SAMPLER1D
2127 || type->Type == D3DXPT_SAMPLER2D
2128 || type->Type == D3DXPT_SAMPLER3D
2129 || type->Type == D3DXPT_SAMPLERCUBE)
2130 {
2131 if (samplers) samplers[sampler_count] = data + constant_info[i].Name;
2132
2133 ++sampler_count;
2134 }
2135 }
2136
2137 TRACE("Found %u samplers\n", sampler_count);
2138
2139 if (count) *count = sampler_count;
2140
2141 return D3D_OK;
2142 }
2143
2144 HRESULT WINAPI D3DXDisassembleShader(const DWORD *shader, BOOL colorcode, const char *comments, ID3DXBuffer **disassembly)
2145 {
2146 FIXME("%p %d %s %p: stub\n", shader, colorcode, debugstr_a(comments), disassembly);
2147 return E_OUTOFMEMORY;
2148 }
2149
2150 struct d3dx9_texture_shader
2151 {
2152 ID3DXTextureShader ID3DXTextureShader_iface;
2153 LONG ref;
2154 };
2155
2156 static inline struct d3dx9_texture_shader *impl_from_ID3DXTextureShader(ID3DXTextureShader *iface)
2157 {
2158 return CONTAINING_RECORD(iface, struct d3dx9_texture_shader, ID3DXTextureShader_iface);
2159 }
2160
2161 static HRESULT WINAPI d3dx9_texture_shader_QueryInterface(ID3DXTextureShader *iface, REFIID riid, void **out)
2162 {
2163 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
2164
2165 if (IsEqualGUID(riid, &IID_IUnknown) ||
2166 IsEqualGUID(riid, &IID_ID3DXTextureShader))
2167 {
2168 iface->lpVtbl->AddRef(iface);
2169 *out = iface;
2170 return D3D_OK;
2171 }
2172
2173 WARN("Interface %s not found.\n", debugstr_guid(riid));
2174 *out = NULL;
2175 return E_NOINTERFACE;
2176 }
2177
2178 static ULONG WINAPI d3dx9_texture_shader_AddRef(ID3DXTextureShader *iface)
2179 {
2180 struct d3dx9_texture_shader *texture_shader = impl_from_ID3DXTextureShader(iface);
2181 ULONG refcount = InterlockedIncrement(&texture_shader->ref);
2182
2183 TRACE("%p increasing refcount to %u.\n", texture_shader, refcount);
2184
2185 return refcount;
2186 }
2187
2188 static ULONG WINAPI d3dx9_texture_shader_Release(ID3DXTextureShader *iface)
2189 {
2190 struct d3dx9_texture_shader *texture_shader = impl_from_ID3DXTextureShader(iface);
2191 ULONG refcount = InterlockedDecrement(&texture_shader->ref);
2192
2193 TRACE("%p decreasing refcount to %u.\n", texture_shader, refcount);
2194
2195 if (!refcount)
2196 {
2197 HeapFree(GetProcessHeap(), 0, texture_shader);
2198 }
2199
2200 return refcount;
2201 }
2202
2203 static HRESULT WINAPI d3dx9_texture_shader_GetFunction(ID3DXTextureShader *iface, struct ID3DXBuffer **function)
2204 {
2205 FIXME("iface %p, function %p stub.\n", iface, function);
2206
2207 return E_NOTIMPL;
2208 }
2209
2210 static HRESULT WINAPI d3dx9_texture_shader_GetConstantBuffer(ID3DXTextureShader *iface, struct ID3DXBuffer **constant_buffer)
2211 {
2212 FIXME("iface %p, constant_buffer %p stub.\n", iface, constant_buffer);
2213
2214 return E_NOTIMPL;
2215 }
2216
2217 static HRESULT WINAPI d3dx9_texture_shader_GetDesc(ID3DXTextureShader *iface, D3DXCONSTANTTABLE_DESC *desc)
2218 {
2219 FIXME("iface %p, desc %p stub.\n", iface, desc);
2220
2221 return E_NOTIMPL;
2222 }
2223
2224 static HRESULT WINAPI d3dx9_texture_shader_GetConstantDesc(ID3DXTextureShader *iface, D3DXHANDLE constant, D3DXCONSTANT_DESC *constant_desc, UINT *count)
2225 {
2226 FIXME("iface %p, constant %p, constant_desc %p, count %p stub.\n", iface, constant, constant_desc, count);
2227
2228 return E_NOTIMPL;
2229 }
2230
2231 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstant(ID3DXTextureShader *iface, D3DXHANDLE constant, UINT index)
2232 {
2233 FIXME("iface %p, constant %p, index %u stub.\n", iface, constant, index);
2234
2235 return NULL;
2236 }
2237
2238 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstantByName(ID3DXTextureShader *iface, D3DXHANDLE constant, const char *name)
2239 {
2240 FIXME("iface %p, constant %p, name %s stub.\n", iface, constant, debugstr_a(name));
2241
2242 return NULL;
2243 }
2244
2245 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstantElement(ID3DXTextureShader *iface, D3DXHANDLE constant, UINT index)
2246 {
2247 FIXME("iface %p, constant %p, index %u stub.\n", iface, constant, index);
2248
2249 return NULL;
2250 }
2251
2252 static HRESULT WINAPI d3dx9_texture_shader_SetDefaults(ID3DXTextureShader *iface)
2253 {
2254 FIXME("iface %p stub.\n", iface);
2255
2256 return E_NOTIMPL;
2257 }
2258
2259 static HRESULT WINAPI d3dx9_texture_shader_SetValue(ID3DXTextureShader *iface, D3DXHANDLE constant, const void *data, UINT bytes)
2260 {
2261 FIXME("iface %p, constant %p, data %p, bytes %u stub.\n", iface, constant, data, bytes);
2262
2263 return E_NOTIMPL;
2264 }
2265
2266 static HRESULT WINAPI d3dx9_texture_shader_SetBool(ID3DXTextureShader *iface, D3DXHANDLE constant, BOOL b)
2267 {
2268 FIXME("iface %p, constant %p, b %u stub.\n", iface, constant, b);
2269
2270 return E_NOTIMPL;
2271 }
2272
2273 static HRESULT WINAPI d3dx9_texture_shader_SetBoolArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const BOOL *b, UINT count)
2274 {
2275 FIXME("iface %p, constant %p, b %p, count %u stub.\n", iface, constant, b, count);
2276
2277 return E_NOTIMPL;
2278 }
2279
2280 static HRESULT WINAPI d3dx9_texture_shader_SetInt(ID3DXTextureShader *iface, D3DXHANDLE constant, INT n)
2281 {
2282 FIXME("iface %p, constant %p, n %d stub.\n", iface, constant, n);
2283
2284 return E_NOTIMPL;
2285 }
2286
2287 static HRESULT WINAPI d3dx9_texture_shader_SetIntArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const INT *n, UINT count)
2288 {
2289 FIXME("iface %p, constant %p, n %p, count %u stub.\n", iface, constant, n, count);
2290
2291 return E_NOTIMPL;
2292 }
2293
2294 static HRESULT WINAPI d3dx9_texture_shader_SetFloat(ID3DXTextureShader *iface, D3DXHANDLE constant, FLOAT f)
2295 {
2296 FIXME("iface %p, constant %p, f %f stub.\n", iface, constant, f);
2297
2298 return E_NOTIMPL;
2299 }
2300
2301 static HRESULT WINAPI d3dx9_texture_shader_SetFloatArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const FLOAT *f, UINT count)
2302 {
2303 FIXME("iface %p, constant %p, f %p, count %u stub.\n", iface, constant, f, count);
2304
2305 return E_NOTIMPL;
2306 }
2307
2308 static HRESULT WINAPI d3dx9_texture_shader_SetVector(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXVECTOR4 *vector)
2309 {
2310 FIXME("iface %p, constant %p, vector %p stub.\n", iface, constant, vector);
2311
2312 return E_NOTIMPL;
2313 }
2314
2315 static HRESULT WINAPI d3dx9_texture_shader_SetVectorArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXVECTOR4 *vector, UINT count)
2316 {
2317 FIXME("iface %p, constant %p, vector %p, count %u stub.\n", iface, constant, vector, count);
2318
2319 return E_NOTIMPL;
2320 }
2321
2322 static HRESULT WINAPI d3dx9_texture_shader_SetMatrix(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix)
2323 {
2324 FIXME("iface %p, constant %p, matrix %p stub.\n", iface, constant, matrix);
2325
2326 return E_NOTIMPL;
2327 }
2328
2329 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
2330 {
2331 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2332
2333 return E_NOTIMPL;
2334 }
2335
2336 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixPointerArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
2337 {
2338 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2339
2340 return E_NOTIMPL;
2341 }
2342
2343 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTranspose(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix)
2344 {
2345 FIXME("iface %p, constant %p, matrix %p stub.\n", iface, constant, matrix);
2346
2347 return E_NOTIMPL;
2348 }
2349
2350 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTransposeArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
2351 {
2352 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2353
2354 return E_NOTIMPL;
2355 }
2356
2357 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTransposePointerArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
2358 {
2359 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2360
2361 return E_NOTIMPL;
2362 }
2363
2364 static const struct ID3DXTextureShaderVtbl d3dx9_texture_shader_vtbl =
2365 {
2366 /*** IUnknown methods ***/
2367 d3dx9_texture_shader_QueryInterface,
2368 d3dx9_texture_shader_AddRef,
2369 d3dx9_texture_shader_Release,
2370 /*** ID3DXTextureShader methods ***/
2371 d3dx9_texture_shader_GetFunction,
2372 d3dx9_texture_shader_GetConstantBuffer,
2373 d3dx9_texture_shader_GetDesc,
2374 d3dx9_texture_shader_GetConstantDesc,
2375 d3dx9_texture_shader_GetConstant,
2376 d3dx9_texture_shader_GetConstantByName,
2377 d3dx9_texture_shader_GetConstantElement,
2378 d3dx9_texture_shader_SetDefaults,
2379 d3dx9_texture_shader_SetValue,
2380 d3dx9_texture_shader_SetBool,
2381 d3dx9_texture_shader_SetBoolArray,
2382 d3dx9_texture_shader_SetInt,
2383 d3dx9_texture_shader_SetIntArray,
2384 d3dx9_texture_shader_SetFloat,
2385 d3dx9_texture_shader_SetFloatArray,
2386 d3dx9_texture_shader_SetVector,
2387 d3dx9_texture_shader_SetVectorArray,
2388 d3dx9_texture_shader_SetMatrix,
2389 d3dx9_texture_shader_SetMatrixArray,
2390 d3dx9_texture_shader_SetMatrixPointerArray,
2391 d3dx9_texture_shader_SetMatrixTranspose,
2392 d3dx9_texture_shader_SetMatrixTransposeArray,
2393 d3dx9_texture_shader_SetMatrixTransposePointerArray
2394 };
2395
2396 HRESULT WINAPI D3DXCreateTextureShader(const DWORD *function, ID3DXTextureShader **texture_shader)
2397 {
2398 struct d3dx9_texture_shader *object;
2399
2400 TRACE("function %p, texture_shader %p.\n", function, texture_shader);
2401
2402 if (!function || !texture_shader)
2403 return D3DERR_INVALIDCALL;
2404
2405 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
2406 if (!object)
2407 return E_OUTOFMEMORY;
2408
2409 object->ID3DXTextureShader_iface.lpVtbl = &d3dx9_texture_shader_vtbl;
2410 object->ref = 1;
2411
2412 *texture_shader = &object->ID3DXTextureShader_iface;
2413
2414 return D3D_OK;
2415 }
2416
2417 static unsigned int get_instr_length(const DWORD *byte_code, unsigned int major, unsigned int minor)
2418 {
2419 unsigned int len = 0;
2420
2421 if (major > 1)
2422 return (*byte_code & D3DSI_INSTLENGTH_MASK) >> D3DSI_INSTLENGTH_SHIFT;
2423
2424 switch (*byte_code & 0xffff)
2425 {
2426 case D3DSIO_END:
2427 ERR("Unexpected END token.\n");
2428 return 0;
2429 case D3DSIO_COMMENT:
2430 return (*byte_code & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
2431 case D3DSIO_DEF:
2432 case D3DSIO_DEFI:
2433 return 5;
2434 case D3DSIO_DEFB:
2435 return 2;
2436 default:
2437 ++byte_code;
2438 while (*byte_code & 0x80000000)
2439 {
2440 ++byte_code;
2441 ++len;
2442 }
2443 }
2444
2445 return len;
2446 }
2447
2448 static HRESULT get_shader_semantics(const DWORD *byte_code, D3DXSEMANTIC *semantics, UINT *count, BOOL output)
2449 {
2450 static const D3DDECLUSAGE regtype_usage[] =
2451 {
2452 D3DDECLUSAGE_COLOR,
2453 D3DDECLUSAGE_COLOR,
2454 0,
2455 D3DDECLUSAGE_TEXCOORD,
2456 0,
2457 D3DDECLUSAGE_COLOR,
2458 D3DDECLUSAGE_TEXCOORD,
2459 0,
2460 0,
2461 D3DDECLUSAGE_DEPTH
2462 };
2463 static const D3DDECLUSAGE rast_usage[] =
2464 {
2465 D3DDECLUSAGE_POSITION,
2466 D3DDECLUSAGE_FOG,
2467 D3DDECLUSAGE_PSIZE
2468 };
2469 DWORD reg_type, usage, index, version_token = *byte_code;
2470 BOOL is_ps = version_token >> 16 == 0xffff;
2471 unsigned int major, minor, i = 0, j;
2472 BYTE colors = 0, rastout = 0;
2473 BOOL has_dcl, depth = 0;
2474 WORD texcoords = 0;
2475
2476 if ((version_token & 0xffff0000) != 0xfffe0000 && (version_token & 0xffff0000) != 0xffff0000)
2477 return D3DXERR_INVALIDDATA;
2478
2479 major = version_token >> 8 & 0xff;
2480 minor = version_token & 0xff;
2481
2482 TRACE("%s shader, version %u.%u.\n", is_ps ? "Pixel" : "Vertex", major, minor);
2483 ++byte_code;
2484
2485 has_dcl = (!is_ps && (!output || major == 3)) || (is_ps && !output && major >= 2);
2486
2487 while (*byte_code != D3DSIO_END)
2488 {
2489 if (has_dcl && (*byte_code & 0xffff) == D3DSIO_DCL)
2490 {
2491 DWORD usage_token = byte_code[1];
2492 DWORD reg = byte_code[2];
2493
2494 reg_type = ((reg & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT)
2495 | ((reg & D3DSP_REGTYPE_MASK2) >> D3DSP_REGTYPE_SHIFT2);
2496
2497 if (is_ps && !output && major == 2)
2498 {
2499 /* dcl with no explicit usage, look at the register. */
2500 reg_type = ((reg & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT)
2501 | ((reg & D3DSP_REGTYPE_MASK2) >> D3DSP_REGTYPE_SHIFT2);
2502 index = reg & D3DSP_REGNUM_MASK;
2503 if (reg_type >= ARRAY_SIZE(regtype_usage))
2504 {
2505 WARN("Invalid register type %u.\n", reg_type);
2506 reg_type = 0;
2507 }
2508 usage = regtype_usage[reg_type];
2509 if (semantics)
2510 {
2511 semantics[i].Usage = usage;
2512 semantics[i].UsageIndex = index;
2513 }
2514 ++i;
2515 }
2516 else if ((!output && reg_type == D3DSPR_INPUT) || (output && reg_type == D3DSPR_OUTPUT))
2517 {
2518 if (semantics)
2519 {
2520 semantics[i].Usage =
2521 (usage_token & D3DSP_DCL_USAGE_MASK) >> D3DSP_DCL_USAGE_SHIFT;
2522 semantics[i].UsageIndex =
2523 (usage_token & D3DSP_DCL_USAGEINDEX_MASK) >> D3DSP_DCL_USAGEINDEX_SHIFT;
2524 }
2525 ++i;
2526 }
2527 byte_code += 3;
2528 }
2529 else if (!has_dcl)
2530 {
2531 unsigned int len = get_instr_length(byte_code, major, minor) + 1;
2532
2533 switch (*byte_code & 0xffff)
2534 {
2535 case D3DSIO_COMMENT:
2536 case D3DSIO_DEF:
2537 case D3DSIO_DEFB:
2538 case D3DSIO_DEFI:
2539 byte_code += len;
2540 break;
2541 default:
2542 ++byte_code;
2543 while (*byte_code & 0x80000000)
2544 {
2545 reg_type = ((*byte_code & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT)
2546 | ((*byte_code & D3DSP_REGTYPE_MASK2) >> D3DSP_REGTYPE_SHIFT2);
2547 index = *byte_code & D3DSP_REGNUM_MASK;
2548
2549 if ((reg_type == D3DSPR_TEMP && is_ps && major == 1)
2550 || (reg_type == D3DSPR_INPUT && is_ps)
2551 || (reg_type == D3DSPR_TEXTURE && is_ps && !output)
2552 || reg_type == D3DSPR_RASTOUT
2553 || reg_type == D3DSPR_ATTROUT
2554 || reg_type == D3DSPR_OUTPUT
2555 || reg_type == D3DSPR_DEPTHOUT)
2556 {
2557 if (reg_type == D3DSPR_RASTOUT)
2558 rastout |= 1u << index;
2559 else if (reg_type == D3DSPR_DEPTHOUT)
2560 depth = TRUE;
2561 else if (reg_type == D3DSPR_TEXTURE || reg_type == D3DSPR_OUTPUT)
2562 texcoords |= 1u << index;
2563 else
2564 colors |= 1u << index;
2565 }
2566 ++byte_code;
2567 }
2568 }
2569 }
2570 else
2571 {
2572 byte_code += get_instr_length(byte_code, major, minor) + 1;
2573 }
2574 }
2575
2576 if (!has_dcl)
2577 {
2578 i = j = 0;
2579 while (texcoords)
2580 {
2581 if (texcoords & 1)
2582 {
2583 if (semantics)
2584 {
2585 semantics[i].Usage = D3DDECLUSAGE_TEXCOORD;
2586 semantics[i].UsageIndex = j;
2587 }
2588 ++i;
2589 }
2590 texcoords >>= 1;
2591 ++j;
2592 }
2593 j = 0;
2594 while (colors)
2595 {
2596 if (colors & 1)
2597 {
2598 if (semantics)
2599 {
2600 semantics[i].Usage = D3DDECLUSAGE_COLOR;
2601 semantics[i].UsageIndex = j;
2602 }
2603 ++i;
2604 }
2605 colors >>= 1;
2606 ++j;
2607 }
2608 j = 0;
2609 while (rastout)
2610 {
2611 if (rastout & 1)
2612 {
2613 if (j >= ARRAY_SIZE(rast_usage))
2614 {
2615 WARN("Invalid RASTOUT register index.\n");
2616 usage = 0;
2617 }
2618 else
2619 {
2620 usage = rast_usage[j];
2621 }
2622 if (semantics)
2623 {
2624 semantics[i].Usage = usage;
2625 semantics[i].UsageIndex = 0;
2626 }
2627 ++i;
2628 }
2629 rastout >>= 1;
2630 ++j;
2631 }
2632 if (depth)
2633 {
2634 if (semantics)
2635 {
2636 semantics[i].Usage = D3DDECLUSAGE_DEPTH;
2637 semantics[i].UsageIndex = 0;
2638 }
2639 ++i;
2640 }
2641 }
2642
2643 if (count)
2644 *count = i;
2645
2646 return D3D_OK;
2647 }
2648
2649 HRESULT WINAPI D3DXGetShaderInputSemantics(const DWORD *byte_code, D3DXSEMANTIC *semantics, UINT *count)
2650 {
2651 TRACE("byte_code %p, semantics %p, count %p.\n", byte_code, semantics, count);
2652
2653 return get_shader_semantics(byte_code, semantics, count, FALSE);
2654 }
2655
2656 HRESULT WINAPI D3DXGetShaderOutputSemantics(const DWORD *byte_code, D3DXSEMANTIC *semantics, UINT *count)
2657 {
2658 TRACE("byte_code %p, semantics %p, count %p.\n", byte_code, semantics, count);
2659
2660 return get_shader_semantics(byte_code, semantics, count, TRUE);
2661 }