reg/tests: Use a helper function to delete registry values.
[wine.git] / dlls / d3dx9_36 / shader.c
blobe46e0ae04bdd8b2c41302b9c8a647b465d7926dd
1 /*
2 * Copyright 2008 Luis Busquets
3 * Copyright 2009 Matteo Bruni
4 * Copyright 2011 Travis Athougies
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include "d3dx9_private.h"
25 #include "d3dcommon.h"
26 #include "d3dcompiler.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30 /* This function is not declared in the SDK headers yet. */
31 HRESULT WINAPI D3DAssemble(const void *data, SIZE_T datasize, const char *filename,
32 const D3D_SHADER_MACRO *defines, ID3DInclude *include, UINT flags,
33 ID3DBlob **shader, ID3DBlob **error_messages);
35 static inline BOOL is_valid_bytecode(DWORD token)
37 return (token & 0xfffe0000) == 0xfffe0000;
40 const char * WINAPI D3DXGetPixelShaderProfile(struct IDirect3DDevice9 *device)
42 D3DCAPS9 caps;
44 TRACE("device %p\n", device);
46 if (!device) return NULL;
48 IDirect3DDevice9_GetDeviceCaps(device,&caps);
50 switch (caps.PixelShaderVersion)
52 case D3DPS_VERSION(1, 1):
53 return "ps_1_1";
55 case D3DPS_VERSION(1, 2):
56 return "ps_1_2";
58 case D3DPS_VERSION(1, 3):
59 return "ps_1_3";
61 case D3DPS_VERSION(1, 4):
62 return "ps_1_4";
64 case D3DPS_VERSION(2, 0):
65 if ((caps.PS20Caps.NumTemps>=22) &&
66 (caps.PS20Caps.Caps&D3DPS20CAPS_ARBITRARYSWIZZLE) &&
67 (caps.PS20Caps.Caps&D3DPS20CAPS_GRADIENTINSTRUCTIONS) &&
68 (caps.PS20Caps.Caps&D3DPS20CAPS_PREDICATION) &&
69 (caps.PS20Caps.Caps&D3DPS20CAPS_NODEPENDENTREADLIMIT) &&
70 (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
72 return "ps_2_a";
74 if ((caps.PS20Caps.NumTemps>=32) &&
75 (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
77 return "ps_2_b";
79 return "ps_2_0";
81 case D3DPS_VERSION(3, 0):
82 return "ps_3_0";
85 return NULL;
88 UINT WINAPI D3DXGetShaderSize(const DWORD *byte_code)
90 const DWORD *ptr = byte_code;
92 TRACE("byte_code %p\n", byte_code);
94 if (!ptr) return 0;
96 /* Look for the END token, skipping the VERSION token */
97 while (*++ptr != D3DSIO_END)
99 /* Skip comments */
100 if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
102 ptr += ((*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT);
105 ++ptr;
107 /* Return the shader size in bytes */
108 return (ptr - byte_code) * sizeof(*ptr);
111 DWORD WINAPI D3DXGetShaderVersion(const DWORD *byte_code)
113 TRACE("byte_code %p\n", byte_code);
115 return byte_code ? *byte_code : 0;
118 const char * WINAPI D3DXGetVertexShaderProfile(struct IDirect3DDevice9 *device)
120 D3DCAPS9 caps;
122 TRACE("device %p\n", device);
124 if (!device) return NULL;
126 IDirect3DDevice9_GetDeviceCaps(device,&caps);
128 switch (caps.VertexShaderVersion)
130 case D3DVS_VERSION(1, 1):
131 return "vs_1_1";
132 case D3DVS_VERSION(2, 0):
133 if ((caps.VS20Caps.NumTemps>=13) &&
134 (caps.VS20Caps.DynamicFlowControlDepth==24) &&
135 (caps.VS20Caps.Caps&D3DPS20CAPS_PREDICATION))
137 return "vs_2_a";
139 return "vs_2_0";
140 case D3DVS_VERSION(3, 0):
141 return "vs_3_0";
144 return NULL;
147 HRESULT WINAPI D3DXFindShaderComment(const DWORD *byte_code, DWORD fourcc, const void **data, UINT *size)
149 const DWORD *ptr = byte_code;
150 DWORD version;
152 TRACE("byte_code %p, fourcc %x, data %p, size %p\n", byte_code, fourcc, data, size);
154 if (data) *data = NULL;
155 if (size) *size = 0;
157 if (!byte_code) return D3DERR_INVALIDCALL;
159 version = *ptr >> 16;
160 if (version != 0x4658 /* FX */
161 && version != 0x5458 /* TX */
162 && version != 0x7ffe
163 && version != 0x7fff
164 && version != 0xfffe /* VS */
165 && version != 0xffff) /* PS */
167 WARN("Invalid data supplied\n");
168 return D3DXERR_INVALIDDATA;
171 while (*++ptr != D3DSIO_END)
173 /* Check if it is a comment */
174 if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
176 DWORD comment_size = (*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
178 /* Check if this is the comment we are looking for */
179 if (*(ptr + 1) == fourcc)
181 UINT ctab_size = (comment_size - 1) * sizeof(DWORD);
182 const void *ctab_data = ptr + 2;
183 if (size)
184 *size = ctab_size;
185 if (data)
186 *data = ctab_data;
187 TRACE("Returning comment data at %p with size %d\n", ctab_data, ctab_size);
188 return D3D_OK;
190 ptr += comment_size;
194 return S_FALSE;
197 HRESULT WINAPI D3DXAssembleShader(const char *data, UINT data_len, const D3DXMACRO *defines,
198 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
200 HRESULT hr;
202 TRACE("data %p, data_len %u, defines %p, include %p, flags %#x, shader %p, error_messages %p\n",
203 data, data_len, defines, include, flags, shader, error_messages);
205 /* Forward to d3dcompiler: the parameter types aren't really different,
206 the actual data types are equivalent */
207 hr = D3DAssemble(data, data_len, NULL, (D3D_SHADER_MACRO *)defines,
208 (ID3DInclude *)include, flags, (ID3DBlob **)shader,
209 (ID3DBlob **)error_messages);
211 if(hr == E_FAIL) hr = D3DXERR_INVALIDDATA;
212 return hr;
215 static const void *main_file_data;
217 static CRITICAL_SECTION from_file_mutex;
218 static CRITICAL_SECTION_DEBUG from_file_mutex_debug =
220 0, 0, &from_file_mutex,
222 &from_file_mutex_debug.ProcessLocksList,
223 &from_file_mutex_debug.ProcessLocksList
225 0, 0, {(DWORD_PTR)(__FILE__ ": from_file_mutex")}
227 static CRITICAL_SECTION from_file_mutex = {&from_file_mutex_debug, -1, 0, 0, 0, 0};
229 /* D3DXInclude private implementation, used to implement
230 * D3DXAssembleShaderFromFile() from D3DXAssembleShader(). */
231 /* To be able to correctly resolve include search paths we have to store the
232 * pathname of each include file. We store the pathname pointer right before
233 * the file data. */
234 static HRESULT WINAPI d3dincludefromfile_open(ID3DXInclude *iface, D3DXINCLUDE_TYPE include_type,
235 const char *filename, const void *parent_data, const void **data, UINT *bytes)
237 const char *p, *parent_name = "";
238 char *pathname = NULL, *ptr;
239 char **buffer = NULL;
240 HANDLE file;
241 UINT size;
243 if (parent_data)
245 parent_name = *((const char **)parent_data - 1);
247 else
249 if (main_file_data)
250 parent_name = *((const char **)main_file_data - 1);
253 TRACE("Looking up for include file %s, parent %s\n", debugstr_a(filename), debugstr_a(parent_name));
255 if ((p = strrchr(parent_name, '\\')))
256 ++p;
257 else
258 p = parent_name;
259 pathname = HeapAlloc(GetProcessHeap(), 0, (p - parent_name) + strlen(filename) + 1);
260 if(!pathname)
261 return HRESULT_FROM_WIN32(GetLastError());
263 memcpy(pathname, parent_name, p - parent_name);
264 strcpy(pathname + (p - parent_name), filename);
265 ptr = pathname + (p - parent_name);
266 while (*ptr)
268 if (*ptr == '/')
269 *ptr = '\\';
270 ++ptr;
273 file = CreateFileA(pathname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
274 if(file == INVALID_HANDLE_VALUE)
275 goto error;
277 TRACE("Include file found at pathname = %s\n", debugstr_a(pathname));
279 size = GetFileSize(file, NULL);
280 if(size == INVALID_FILE_SIZE)
281 goto error;
283 buffer = HeapAlloc(GetProcessHeap(), 0, size + sizeof(char *));
284 if(!buffer)
285 goto error;
286 *buffer = pathname;
287 if(!ReadFile(file, buffer + 1, size, bytes, NULL))
288 goto error;
290 *data = buffer + 1;
291 if (!main_file_data)
292 main_file_data = *data;
294 CloseHandle(file);
295 return S_OK;
297 error:
298 CloseHandle(file);
299 HeapFree(GetProcessHeap(), 0, pathname);
300 HeapFree(GetProcessHeap(), 0, buffer);
301 return HRESULT_FROM_WIN32(GetLastError());
304 static HRESULT WINAPI d3dincludefromfile_close(ID3DXInclude *iface, const void *data)
306 HeapFree(GetProcessHeap(), 0, *((char **)data - 1));
307 HeapFree(GetProcessHeap(), 0, (char **)data - 1);
308 if (main_file_data == data)
309 main_file_data = NULL;
310 return S_OK;
313 static const struct ID3DXIncludeVtbl D3DXInclude_Vtbl = {
314 d3dincludefromfile_open,
315 d3dincludefromfile_close
318 struct D3DXIncludeImpl {
319 ID3DXInclude ID3DXInclude_iface;
322 HRESULT WINAPI D3DXAssembleShaderFromFileA(const char *filename, const D3DXMACRO *defines,
323 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
325 WCHAR *filename_w;
326 DWORD len;
327 HRESULT ret;
329 TRACE("filename %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
330 debugstr_a(filename), defines, include, flags, shader, error_messages);
332 if (!filename) return D3DXERR_INVALIDDATA;
334 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
335 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
336 if (!filename_w) return E_OUTOFMEMORY;
337 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
339 ret = D3DXAssembleShaderFromFileW(filename_w, defines, include, flags, shader, error_messages);
341 HeapFree(GetProcessHeap(), 0, filename_w);
342 return ret;
345 HRESULT WINAPI D3DXAssembleShaderFromFileW(const WCHAR *filename, const D3DXMACRO *defines,
346 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
348 const void *buffer;
349 DWORD len;
350 HRESULT hr;
351 struct D3DXIncludeImpl includefromfile;
352 char *filename_a;
354 TRACE("filename %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
355 debugstr_w(filename), defines, include, flags, shader, error_messages);
357 if(!include)
359 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
360 include = &includefromfile.ID3DXInclude_iface;
363 len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
364 filename_a = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
365 if (!filename_a)
366 return E_OUTOFMEMORY;
367 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, len, NULL, NULL);
369 EnterCriticalSection(&from_file_mutex);
370 hr = ID3DXInclude_Open(include, D3DXINC_LOCAL, filename_a, NULL, &buffer, &len);
371 if (FAILED(hr))
373 LeaveCriticalSection(&from_file_mutex);
374 HeapFree(GetProcessHeap(), 0, filename_a);
375 return D3DXERR_INVALIDDATA;
378 hr = D3DXAssembleShader(buffer, len, defines, include, flags, shader, error_messages);
380 ID3DXInclude_Close(include, buffer);
381 LeaveCriticalSection(&from_file_mutex);
382 HeapFree(GetProcessHeap(), 0, filename_a);
383 return hr;
386 HRESULT WINAPI D3DXAssembleShaderFromResourceA(HMODULE module, const char *resource, const D3DXMACRO *defines,
387 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
389 void *buffer;
390 HRSRC res;
391 DWORD len;
393 TRACE("module %p, resource %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
394 module, debugstr_a(resource), defines, include, flags, shader, error_messages);
396 if (!(res = FindResourceA(module, resource, (const char *)RT_RCDATA)))
397 return D3DXERR_INVALIDDATA;
398 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
399 return D3DXERR_INVALIDDATA;
400 return D3DXAssembleShader(buffer, len, defines, include, flags,
401 shader, error_messages);
404 HRESULT WINAPI D3DXAssembleShaderFromResourceW(HMODULE module, const WCHAR *resource, const D3DXMACRO *defines,
405 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
407 void *buffer;
408 HRSRC res;
409 DWORD len;
411 TRACE("module %p, resource %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
412 module, debugstr_w(resource), defines, include, flags, shader, error_messages);
414 if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
415 return D3DXERR_INVALIDDATA;
416 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
417 return D3DXERR_INVALIDDATA;
418 return D3DXAssembleShader(buffer, len, defines, include, flags,
419 shader, error_messages);
422 HRESULT WINAPI D3DXCompileShader(const char *data, UINT length, const D3DXMACRO *defines,
423 ID3DXInclude *include, const char *function, const char *profile, DWORD flags,
424 ID3DXBuffer **shader, ID3DXBuffer **error_msgs, ID3DXConstantTable **constant_table)
426 HRESULT hr;
428 TRACE("data %s, length %u, defines %p, include %p, function %s, profile %s, "
429 "flags %#x, shader %p, error_msgs %p, constant_table %p.\n",
430 debugstr_a(data), length, defines, include, debugstr_a(function), debugstr_a(profile),
431 flags, shader, error_msgs, constant_table);
433 hr = D3DCompile(data, length, NULL, (D3D_SHADER_MACRO *)defines, (ID3DInclude *)include,
434 function, profile, flags, 0, (ID3DBlob **)shader, (ID3DBlob **)error_msgs);
436 if (SUCCEEDED(hr) && constant_table)
438 hr = D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*shader), constant_table);
439 if (FAILED(hr))
441 ID3DXBuffer_Release(*shader);
442 *shader = NULL;
446 return hr;
449 HRESULT WINAPI D3DXCompileShaderFromFileA(const char *filename, const D3DXMACRO *defines,
450 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
451 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
453 WCHAR *filename_w;
454 DWORD len;
455 HRESULT ret;
457 TRACE("filename %s, defines %p, include %p, entrypoint %s, profile %s, "
458 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
459 debugstr_a(filename), defines, include, debugstr_a(entrypoint),
460 debugstr_a(profile), flags, shader, error_messages, constant_table);
462 if (!filename) return D3DXERR_INVALIDDATA;
464 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
465 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
466 if (!filename_w) return E_OUTOFMEMORY;
467 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
469 ret = D3DXCompileShaderFromFileW(filename_w, defines, include,
470 entrypoint, profile, flags,
471 shader, error_messages, constant_table);
473 HeapFree(GetProcessHeap(), 0, filename_w);
474 return ret;
477 HRESULT WINAPI D3DXCompileShaderFromFileW(const WCHAR *filename, const D3DXMACRO *defines,
478 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
479 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
481 const void *buffer;
482 DWORD len, filename_len;
483 HRESULT hr;
484 struct D3DXIncludeImpl includefromfile;
485 char *filename_a;
487 TRACE("filename %s, defines %p, include %p, entrypoint %s, profile %s, "
488 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
489 debugstr_w(filename), defines, include, debugstr_a(entrypoint), debugstr_a(profile),
490 flags, shader, error_messages, constant_table);
492 if (!include)
494 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
495 include = &includefromfile.ID3DXInclude_iface;
498 filename_len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
499 filename_a = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(char));
500 if (!filename_a)
501 return E_OUTOFMEMORY;
502 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, filename_len, NULL, NULL);
504 EnterCriticalSection(&from_file_mutex);
505 hr = ID3DXInclude_Open(include, D3DXINC_LOCAL, filename_a, NULL, &buffer, &len);
506 if (FAILED(hr))
508 LeaveCriticalSection(&from_file_mutex);
509 HeapFree(GetProcessHeap(), 0, filename_a);
510 return D3DXERR_INVALIDDATA;
513 hr = D3DCompile(buffer, len, filename_a, (const D3D_SHADER_MACRO *)defines,
514 (ID3DInclude *)include, entrypoint, profile, flags, 0,
515 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
517 if (SUCCEEDED(hr) && constant_table)
518 hr = D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*shader),
519 constant_table);
521 ID3DXInclude_Close(include, buffer);
522 LeaveCriticalSection(&from_file_mutex);
523 HeapFree(GetProcessHeap(), 0, filename_a);
524 return hr;
527 HRESULT WINAPI D3DXCompileShaderFromResourceA(HMODULE module, const char *resource, const D3DXMACRO *defines,
528 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
529 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
531 void *buffer;
532 HRSRC res;
533 DWORD len;
535 TRACE("module %p, resource %s, defines %p, include %p, entrypoint %s, profile %s, "
536 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
537 module, debugstr_a(resource), defines, include, debugstr_a(entrypoint), debugstr_a(profile),
538 flags, shader, error_messages, constant_table);
540 if (!(res = FindResourceA(module, resource, (const char *)RT_RCDATA)))
541 return D3DXERR_INVALIDDATA;
542 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
543 return D3DXERR_INVALIDDATA;
544 return D3DXCompileShader(buffer, len, defines, include, entrypoint, profile,
545 flags, shader, error_messages, constant_table);
548 HRESULT WINAPI D3DXCompileShaderFromResourceW(HMODULE module, const WCHAR *resource, const D3DXMACRO *defines,
549 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
550 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
552 void *buffer;
553 HRSRC res;
554 DWORD len;
556 TRACE("module %p, resource %s, defines %p, include %p, entrypoint %s, profile %s, "
557 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
558 module, debugstr_w(resource), defines, include, debugstr_a(entrypoint), debugstr_a(profile),
559 flags, shader, error_messages, constant_table);
561 if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
562 return D3DXERR_INVALIDDATA;
563 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
564 return D3DXERR_INVALIDDATA;
565 return D3DXCompileShader(buffer, len, defines, include, entrypoint, profile,
566 flags, shader, error_messages, constant_table);
569 HRESULT WINAPI D3DXPreprocessShader(const char *data, UINT data_len, const D3DXMACRO *defines,
570 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
572 TRACE("data %s, data_len %u, defines %p, include %p, shader %p, error_messages %p.\n",
573 debugstr_a(data), data_len, defines, include, shader, error_messages);
575 return D3DPreprocess(data, data_len, NULL,
576 (const D3D_SHADER_MACRO *)defines, (ID3DInclude *)include,
577 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
580 HRESULT WINAPI D3DXPreprocessShaderFromFileA(const char *filename, const D3DXMACRO *defines,
581 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
583 WCHAR *filename_w = NULL;
584 DWORD len;
585 HRESULT ret;
587 TRACE("filename %s, defines %p, include %p, shader %p, error_messages %p.\n",
588 debugstr_a(filename), defines, include, shader, error_messages);
590 if (!filename) return D3DXERR_INVALIDDATA;
592 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
593 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
594 if (!filename_w) return E_OUTOFMEMORY;
595 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
597 ret = D3DXPreprocessShaderFromFileW(filename_w, defines, include, shader, error_messages);
599 HeapFree(GetProcessHeap(), 0, filename_w);
600 return ret;
603 HRESULT WINAPI D3DXPreprocessShaderFromFileW(const WCHAR *filename, const D3DXMACRO *defines,
604 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
606 const void *buffer;
607 DWORD len;
608 HRESULT hr;
609 struct D3DXIncludeImpl includefromfile;
610 char *filename_a;
612 TRACE("filename %s, defines %p, include %p, shader %p, error_messages %p.\n",
613 debugstr_w(filename), defines, include, shader, error_messages);
615 if (!include)
617 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
618 include = &includefromfile.ID3DXInclude_iface;
621 len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
622 filename_a = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
623 if (!filename_a)
624 return E_OUTOFMEMORY;
625 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, len, NULL, NULL);
627 EnterCriticalSection(&from_file_mutex);
628 hr = ID3DXInclude_Open(include, D3DXINC_LOCAL, filename_a, NULL, &buffer, &len);
629 if (FAILED(hr))
631 LeaveCriticalSection(&from_file_mutex);
632 HeapFree(GetProcessHeap(), 0, filename_a);
633 return D3DXERR_INVALIDDATA;
636 hr = D3DPreprocess(buffer, len, NULL,
637 (const D3D_SHADER_MACRO *)defines,
638 (ID3DInclude *) include,
639 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
641 ID3DXInclude_Close(include, buffer);
642 LeaveCriticalSection(&from_file_mutex);
643 HeapFree(GetProcessHeap(), 0, filename_a);
644 return hr;
647 HRESULT WINAPI D3DXPreprocessShaderFromResourceA(HMODULE module, const char *resource, const D3DXMACRO *defines,
648 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
650 void *buffer;
651 HRSRC res;
652 DWORD len;
654 TRACE("module %p, resource %s, defines %p, include %p, shader %p, error_messages %p.\n",
655 module, debugstr_a(resource), defines, include, shader, error_messages);
657 if (!(res = FindResourceA(module, resource, (const char *)RT_RCDATA)))
658 return D3DXERR_INVALIDDATA;
659 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
660 return D3DXERR_INVALIDDATA;
661 return D3DXPreprocessShader(buffer, len, defines, include,
662 shader, error_messages);
665 HRESULT WINAPI D3DXPreprocessShaderFromResourceW(HMODULE module, const WCHAR *resource, const D3DXMACRO *defines,
666 ID3DXInclude *include, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
668 void *buffer;
669 HRSRC res;
670 DWORD len;
672 TRACE("module %p, resource %s, defines %p, include %p, shader %p, error_messages %p.\n",
673 module, debugstr_w(resource), defines, include, shader, error_messages);
675 if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
676 return D3DXERR_INVALIDDATA;
677 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
678 return D3DXERR_INVALIDDATA;
679 return D3DXPreprocessShader(buffer, len, defines, include,
680 shader, error_messages);
684 struct ID3DXConstantTableImpl {
685 ID3DXConstantTable ID3DXConstantTable_iface;
686 LONG ref;
687 char *ctab;
688 DWORD size;
689 D3DXCONSTANTTABLE_DESC desc;
690 struct ctab_constant *constants;
693 static void free_constant(struct ctab_constant *constant)
695 if (constant->constants)
697 UINT i, count = constant->desc.Elements > 1 ? constant->desc.Elements : constant->desc.StructMembers;
699 for (i = 0; i < count; ++i)
701 free_constant(&constant->constants[i]);
703 HeapFree(GetProcessHeap(), 0, constant->constants);
707 static void free_constant_table(struct ID3DXConstantTableImpl *table)
709 if (table->constants)
711 UINT i;
713 for (i = 0; i < table->desc.Constants; ++i)
715 free_constant(&table->constants[i]);
717 HeapFree(GetProcessHeap(), 0, table->constants);
719 HeapFree(GetProcessHeap(), 0, table->ctab);
722 static inline struct ID3DXConstantTableImpl *impl_from_ID3DXConstantTable(ID3DXConstantTable *iface)
724 return CONTAINING_RECORD(iface, struct ID3DXConstantTableImpl, ID3DXConstantTable_iface);
727 static inline BOOL is_vertex_shader(DWORD version)
729 return (version & 0xffff0000) == 0xfffe0000;
732 static inline D3DXHANDLE handle_from_constant(struct ctab_constant *constant)
734 return (D3DXHANDLE)constant;
737 static struct ctab_constant *get_constant_by_name(struct ID3DXConstantTableImpl *table,
738 struct ctab_constant *constant, const char *name);
740 static struct ctab_constant *get_constant_element_by_name(struct ctab_constant *constant, const char *name)
742 const char *part;
743 UINT element;
745 TRACE("constant %p, name %s\n", constant, debugstr_a(name));
747 if (!name || !*name) return NULL;
749 element = atoi(name);
750 part = strchr(name, ']') + 1;
752 if (constant->desc.Elements > element)
754 struct ctab_constant *c = constant->constants ? &constant->constants[element] : constant;
756 switch (*part++)
758 case '.':
759 return get_constant_by_name(NULL, c, part);
761 case '[':
762 return get_constant_element_by_name(c, part);
764 case '\0':
765 TRACE("Returning parameter %p\n", c);
766 return c;
768 default:
769 FIXME("Unhandled case \"%c\"\n", *--part);
770 break;
774 TRACE("Constant not found\n");
775 return NULL;
778 static struct ctab_constant *get_constant_by_name(struct ID3DXConstantTableImpl *table,
779 struct ctab_constant *constant, const char *name)
781 UINT i, count, length;
782 struct ctab_constant *handles;
783 const char *part;
785 TRACE("table %p, constant %p, name %s\n", table, constant, debugstr_a(name));
787 if (!name || !*name) return NULL;
789 if (!constant)
791 count = table->desc.Constants;
792 handles = table->constants;
794 else
796 count = constant->desc.StructMembers;
797 handles = constant->constants;
800 length = strcspn(name, "[.");
801 part = name + length;
803 for (i = 0; i < count; i++)
805 if (strlen(handles[i].desc.Name) == length && !strncmp(handles[i].desc.Name, name, length))
807 switch (*part++)
809 case '.':
810 return get_constant_by_name(NULL, &handles[i], part);
812 case '[':
813 return get_constant_element_by_name(&handles[i], part);
815 default:
816 TRACE("Returning parameter %p\n", &handles[i]);
817 return &handles[i];
822 TRACE("Constant not found\n");
823 return NULL;
826 static struct ctab_constant *is_valid_sub_constant(struct ctab_constant *parent, D3DXHANDLE handle)
828 struct ctab_constant *c;
829 UINT i, count;
831 /* all variable have at least elements = 1, but not always elements */
832 if (!parent->constants) return NULL;
834 count = parent->desc.Elements > 1 ? parent->desc.Elements : parent->desc.StructMembers;
835 for (i = 0; i < count; ++i)
837 if (handle_from_constant(&parent->constants[i]) == handle)
838 return &parent->constants[i];
840 c = is_valid_sub_constant(&parent->constants[i], handle);
841 if (c) return c;
844 return NULL;
847 static inline struct ctab_constant *get_valid_constant(struct ID3DXConstantTableImpl *table, D3DXHANDLE handle)
849 struct ctab_constant *c;
850 UINT i;
852 if (!handle) return NULL;
854 for (i = 0; i < table->desc.Constants; ++i)
856 if (handle_from_constant(&table->constants[i]) == handle)
857 return &table->constants[i];
859 c = is_valid_sub_constant(&table->constants[i], handle);
860 if (c) return c;
863 return get_constant_by_name(table, NULL, handle);
866 /*** IUnknown methods ***/
867 static HRESULT WINAPI ID3DXConstantTableImpl_QueryInterface(ID3DXConstantTable *iface, REFIID riid, void **out)
869 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
871 if (IsEqualGUID(riid, &IID_IUnknown) ||
872 IsEqualGUID(riid, &IID_ID3DXBuffer) ||
873 IsEqualGUID(riid, &IID_ID3DXConstantTable))
875 ID3DXConstantTable_AddRef(iface);
876 *out = iface;
877 return S_OK;
880 WARN("Interface %s not found.\n", debugstr_guid(riid));
882 return E_NOINTERFACE;
885 static ULONG WINAPI ID3DXConstantTableImpl_AddRef(ID3DXConstantTable *iface)
887 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
889 TRACE("(%p)->(): AddRef from %d\n", This, This->ref);
891 return InterlockedIncrement(&This->ref);
894 static ULONG WINAPI ID3DXConstantTableImpl_Release(ID3DXConstantTable *iface)
896 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
897 ULONG ref = InterlockedDecrement(&This->ref);
899 TRACE("(%p)->(): Release from %d\n", This, ref + 1);
901 if (!ref)
903 free_constant_table(This);
904 HeapFree(GetProcessHeap(), 0, This);
907 return ref;
910 /*** ID3DXBuffer methods ***/
911 static void * WINAPI ID3DXConstantTableImpl_GetBufferPointer(ID3DXConstantTable *iface)
913 struct ID3DXConstantTableImpl *table = impl_from_ID3DXConstantTable(iface);
915 TRACE("iface %p.\n", iface);
917 return table->ctab;
920 static DWORD WINAPI ID3DXConstantTableImpl_GetBufferSize(ID3DXConstantTable *iface)
922 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
924 TRACE("(%p)->()\n", This);
926 return This->size;
929 /*** ID3DXConstantTable methods ***/
930 static HRESULT WINAPI ID3DXConstantTableImpl_GetDesc(ID3DXConstantTable *iface, D3DXCONSTANTTABLE_DESC *desc)
932 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
934 TRACE("(%p)->(%p)\n", This, desc);
936 if (!desc)
937 return D3DERR_INVALIDCALL;
939 *desc = This->desc;
941 return D3D_OK;
944 const struct ctab_constant *d3dx_shader_get_ctab_constant(ID3DXConstantTable *iface, D3DXHANDLE constant)
946 struct ID3DXConstantTableImpl *ctab = impl_from_ID3DXConstantTable(iface);
948 return get_valid_constant(ctab, constant);
951 static HRESULT WINAPI ID3DXConstantTableImpl_GetConstantDesc(ID3DXConstantTable *iface, D3DXHANDLE constant,
952 D3DXCONSTANT_DESC *desc, UINT *count)
954 struct ID3DXConstantTableImpl *ctab = impl_from_ID3DXConstantTable(iface);
955 struct ctab_constant *c = get_valid_constant(ctab, constant);
957 TRACE("(%p)->(%p, %p, %p)\n", ctab, constant, desc, count);
959 if (!c)
961 WARN("Invalid argument specified\n");
962 return D3DERR_INVALIDCALL;
965 if (desc) *desc = c->desc;
966 if (count) *count = 1;
968 return D3D_OK;
971 static UINT WINAPI ID3DXConstantTableImpl_GetSamplerIndex(ID3DXConstantTable *iface, D3DXHANDLE constant)
973 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
974 struct ctab_constant *c = get_valid_constant(This, constant);
976 TRACE("(%p)->(%p)\n", This, constant);
978 if (!c || c->desc.RegisterSet != D3DXRS_SAMPLER)
980 WARN("Invalid argument specified\n");
981 return (UINT)-1;
984 TRACE("Returning RegisterIndex %u\n", c->desc.RegisterIndex);
985 return c->desc.RegisterIndex;
988 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstant(ID3DXConstantTable *iface, D3DXHANDLE constant, UINT index)
990 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
991 struct ctab_constant *c;
993 TRACE("(%p)->(%p, %d)\n", This, constant, index);
995 if (constant)
997 c = get_valid_constant(This, constant);
998 if (c && index < c->desc.StructMembers)
1000 c = &c->constants[index];
1001 TRACE("Returning constant %p\n", c);
1002 return handle_from_constant(c);
1005 else
1007 if (index < This->desc.Constants)
1009 c = &This->constants[index];
1010 TRACE("Returning constant %p\n", c);
1011 return handle_from_constant(c);
1015 WARN("Index out of range\n");
1016 return NULL;
1019 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantByName(ID3DXConstantTable *iface,
1020 D3DXHANDLE constant, const char *name)
1022 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1023 struct ctab_constant *c = get_valid_constant(This, constant);
1025 TRACE("iface %p, constant %p, name %s.\n", iface, constant, debugstr_a(name));
1027 c = get_constant_by_name(This, c, name);
1028 TRACE("Returning constant %p\n", c);
1030 return handle_from_constant(c);
1033 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantElement(ID3DXConstantTable *iface, D3DXHANDLE constant, UINT index)
1035 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1036 struct ctab_constant *c = get_valid_constant(This, constant);
1038 TRACE("(%p)->(%p, %d)\n", This, constant, index);
1040 if (c && index < c->desc.Elements)
1042 if (c->desc.Elements > 1) c = &c->constants[index];
1043 TRACE("Returning constant %p\n", c);
1044 return handle_from_constant(c);
1047 WARN("Invalid argument specified\n");
1048 return NULL;
1051 static inline DWORD get_index(const void **indata, UINT index, BOOL is_pointer)
1053 if (!indata)
1054 return 0;
1056 if (is_pointer)
1057 return ((DWORD **)indata)[index / 16][index % 16];
1059 return (*((DWORD **)indata))[index];
1062 static UINT set(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, struct ctab_constant *constant,
1063 const void **indata, D3DXPARAMETER_TYPE intype, UINT *size, UINT incol, D3DXPARAMETER_CLASS inclass, UINT index,
1064 BOOL is_pointer)
1066 D3DXCONSTANT_DESC *desc = &constant->desc;
1067 UINT l, i, regcount = 1, regsize = 1, cin = 1, rin = 1, ret, last = 0;
1068 DWORD tmp;
1070 /* size too small to set anything */
1071 if (*size < desc->Rows * desc->Columns)
1073 *size = 0;
1074 return 0;
1077 /* D3DXPC_STRUCT is somewhat special */
1078 if (desc->Class == D3DXPC_STRUCT)
1081 * Struct array sets the last complete input to the first struct element, all other
1082 * elements are not set.
1083 * E.g.: struct {int i;} s1[2];
1084 * SetValue(device, "s1", [1, 2], 8) => s1 = {2, x};
1086 * struct {int i; int n} s2[2];
1087 * SetValue(device, "s2", [1, 2, 3, 4, 5], 20) => s1 = {{3, 4}, {x, x}};
1089 if (desc->Elements > 1)
1091 UINT offset = *size / (desc->Rows * desc->Columns) - 1;
1093 offset = min(desc->Elements - 1, offset);
1094 last = offset * desc->Rows * desc->Columns;
1096 if ((is_pointer || inclass == D3DXPC_MATRIX_ROWS) && desc->RegisterSet != D3DXRS_BOOL)
1098 set(table, device, &constant->constants[0], NULL, intype, size, incol, inclass, 0, is_pointer);
1100 else
1102 last += set(table, device, &constant->constants[0], indata, intype, size, incol, inclass,
1103 index + last, is_pointer);
1106 else
1109 * D3DXRS_BOOL is always set. As there are only 16 bools and there are
1110 * exactly 16 input values, use matrix transpose.
1112 if (inclass == D3DXPC_MATRIX_ROWS && desc->RegisterSet == D3DXRS_BOOL)
1114 D3DXMATRIX mat, *m, min;
1115 D3DXMatrixTranspose(&mat, &min);
1117 if (is_pointer)
1118 min = *(D3DXMATRIX *)(indata[index / 16]);
1119 else
1120 min = **(D3DXMATRIX **)indata;
1122 D3DXMatrixTranspose(&mat, &min);
1123 m = &mat;
1124 for (i = 0; i < desc->StructMembers; ++i)
1126 last += set(table, device, &constant->constants[i], (const void **)&m, intype, size, incol,
1127 D3DXPC_SCALAR, index + last, is_pointer);
1131 * For pointers or for matrix rows, only the first member is set.
1132 * All other members are set to 0. This is not true for D3DXRS_BOOL.
1133 * E.g.: struct {int i; int n} s;
1134 * SetValue(device, "s", [1, 2], 8) => s = {1, 0};
1136 else if ((is_pointer || inclass == D3DXPC_MATRIX_ROWS) && desc->RegisterSet != D3DXRS_BOOL)
1138 last = set(table, device, &constant->constants[0], indata, intype, size, incol, inclass,
1139 index + last, is_pointer);
1141 for (i = 1; i < desc->StructMembers; ++i)
1143 set(table, device, &constant->constants[i], NULL, intype, size, incol, inclass, 0, is_pointer);
1146 else
1148 for (i = 0; i < desc->StructMembers; ++i)
1150 last += set(table, device, &constant->constants[i], indata, intype, size, incol, D3DXPC_SCALAR,
1151 index + last, is_pointer);
1156 return last;
1159 /* elements */
1160 if (desc->Elements > 1)
1162 for (i = 0; i < desc->Elements && *size > 0; ++i)
1164 last += set(table, device, &constant->constants[i], indata, intype, size, incol, inclass,
1165 index + last, is_pointer);
1167 /* adjust the vector size for matrix rows */
1168 if (inclass == D3DXPC_MATRIX_ROWS && desc->Class == D3DXPC_VECTOR && (i % 4) == 3)
1170 last += 12;
1171 *size = *size < 12 ? 0 : *size - 12;
1175 return last;
1178 switch (desc->Class)
1180 case D3DXPC_SCALAR:
1181 case D3DXPC_VECTOR:
1182 case D3DXPC_MATRIX_ROWS:
1183 regcount = min(desc->RegisterCount, desc->Rows);
1184 if (inclass == D3DXPC_MATRIX_ROWS) cin = incol;
1185 else rin = incol;
1186 regsize = desc->Columns;
1187 break;
1189 case D3DXPC_MATRIX_COLUMNS:
1190 regcount = min(desc->RegisterCount, desc->Columns);
1191 if (inclass == D3DXPC_MATRIX_ROWS) rin = incol;
1192 else cin = incol;
1193 regsize = desc->Rows;
1194 break;
1196 default:
1197 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1198 return 0;
1201 /* specific stuff for different in types */
1202 switch (inclass)
1204 case D3DXPC_SCALAR:
1205 ret = desc->Columns * desc->Rows;
1206 *size -= desc->Columns * desc->Rows;
1207 break;
1209 case D3DXPC_VECTOR:
1210 switch (desc->Class)
1212 case D3DXPC_MATRIX_ROWS:
1213 if (*size < regcount * 4)
1215 *size = 0;
1216 return 0;
1218 ret = 4 * regcount;
1219 *size -= 4 * regcount;
1220 break;
1222 case D3DXPC_MATRIX_COLUMNS:
1223 ret = 4 * regsize;
1224 *size -= 4 * regcount;
1225 break;
1227 case D3DXPC_SCALAR:
1228 ret = 1;
1229 *size -= ret;
1230 break;
1232 case D3DXPC_VECTOR:
1233 ret = 4;
1234 *size -= ret;
1235 break;
1237 default:
1238 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1239 return 0;
1241 break;
1243 case D3DXPC_MATRIX_ROWS:
1244 switch (desc->Class)
1246 case D3DXPC_MATRIX_ROWS:
1247 case D3DXPC_MATRIX_COLUMNS:
1248 if (*size < 16)
1250 *size = 0;
1251 return 0;
1253 ret = 16;
1254 break;
1256 case D3DXPC_SCALAR:
1257 ret = 4;
1258 break;
1260 case D3DXPC_VECTOR:
1261 ret = 1;
1262 break;
1264 default:
1265 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1266 return 0;
1268 *size -= ret;
1269 break;
1271 case D3DXPC_MATRIX_COLUMNS:
1272 switch (desc->Class)
1274 case D3DXPC_MATRIX_ROWS:
1275 case D3DXPC_MATRIX_COLUMNS:
1276 if (*size < 16)
1278 *size = 0;
1279 return 0;
1281 ret = 16;
1282 break;
1284 case D3DXPC_SCALAR:
1285 ret = 1;
1286 break;
1288 case D3DXPC_VECTOR:
1289 ret = 4;
1290 break;
1292 default:
1293 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1294 return 0;
1296 *size -= ret;
1297 break;
1299 default:
1300 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(inclass));
1301 return 0;
1304 /* set the registers */
1305 switch (desc->RegisterSet)
1307 case D3DXRS_BOOL:
1308 regcount = min(desc->RegisterCount, desc->Columns * desc->Rows);
1309 l = 0;
1310 for (i = 0; i < regcount; ++i)
1312 BOOL out;
1313 DWORD t = get_index(indata, index + i / regsize * rin + l * cin, is_pointer);
1315 set_number(&tmp, desc->Type, &t, intype);
1316 set_number(&out, D3DXPT_BOOL, &tmp, desc->Type);
1317 if (is_vertex_shader(table->desc.Version))
1318 IDirect3DDevice9_SetVertexShaderConstantB(device, desc->RegisterIndex + i, &out, 1);
1319 else
1320 IDirect3DDevice9_SetPixelShaderConstantB(device, desc->RegisterIndex + i, &out, 1);
1322 if (++l >= regsize) l = 0;
1324 return ret;
1326 case D3DXRS_INT4:
1327 for (i = 0; i < regcount; ++i)
1329 INT vec[4] = {0, 0, 1, 0};
1331 for (l = 0; l < regsize; ++l)
1333 DWORD t = get_index(indata, index + i * rin + l * cin, is_pointer);
1335 set_number(&tmp, desc->Type, &t, intype);
1336 set_number(&vec[l], D3DXPT_INT, &tmp, desc->Type);
1338 if (is_vertex_shader(table->desc.Version))
1339 IDirect3DDevice9_SetVertexShaderConstantI(device, desc->RegisterIndex + i, vec, 1);
1340 else
1341 IDirect3DDevice9_SetPixelShaderConstantI(device, desc->RegisterIndex + i, vec, 1);
1343 return ret;
1345 case D3DXRS_FLOAT4:
1346 for (i = 0; i < regcount; ++i)
1348 FLOAT vec[4] = {0};
1350 for (l = 0; l < regsize; ++l)
1352 DWORD t = get_index(indata, index + i * rin + l * cin, is_pointer);
1354 set_number(&tmp, desc->Type, &t, intype);
1355 set_number(&vec[l], D3DXPT_FLOAT, &tmp, desc->Type);
1357 if (is_vertex_shader(table->desc.Version))
1358 IDirect3DDevice9_SetVertexShaderConstantF(device, desc->RegisterIndex + i, vec, 1);
1359 else
1360 IDirect3DDevice9_SetPixelShaderConstantF(device, desc->RegisterIndex + i, vec, 1);
1362 return ret;
1364 default:
1365 FIXME("Unhandled register set %s\n", debug_d3dxparameter_registerset(desc->RegisterSet));
1366 return 0;
1370 static HRESULT set_scalar(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1371 const void *indata, D3DXPARAMETER_TYPE intype)
1373 struct ctab_constant *c = get_valid_constant(table, constant);
1374 UINT count = 1;
1376 if (!c)
1378 WARN("Invalid argument specified\n");
1379 return D3DERR_INVALIDCALL;
1382 switch (c->desc.Class)
1384 case D3DXPC_SCALAR:
1385 set(table, device, c, &indata, intype, &count, c->desc.Columns, D3DXPC_SCALAR, 0, FALSE);
1386 return D3D_OK;
1388 case D3DXPC_VECTOR:
1389 case D3DXPC_MATRIX_ROWS:
1390 case D3DXPC_MATRIX_COLUMNS:
1391 case D3DXPC_STRUCT:
1392 return D3D_OK;
1394 default:
1395 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1396 return D3DERR_INVALIDCALL;
1400 static HRESULT set_scalar_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1401 const void *indata, UINT count, D3DXPARAMETER_TYPE intype)
1403 struct ctab_constant *c = get_valid_constant(table, constant);
1405 if (!c)
1407 WARN("Invalid argument specified\n");
1408 return D3DERR_INVALIDCALL;
1411 switch (c->desc.Class)
1413 case D3DXPC_SCALAR:
1414 case D3DXPC_VECTOR:
1415 case D3DXPC_MATRIX_ROWS:
1416 case D3DXPC_MATRIX_COLUMNS:
1417 case D3DXPC_STRUCT:
1418 set(table, device, c, &indata, intype, &count, c->desc.Columns, D3DXPC_SCALAR, 0, FALSE);
1419 return D3D_OK;
1421 default:
1422 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1423 return D3DERR_INVALIDCALL;
1427 static HRESULT set_vector(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1428 const void *indata, D3DXPARAMETER_TYPE intype)
1430 struct ctab_constant *c = get_valid_constant(table, constant);
1431 UINT count = 4;
1433 if (!c)
1435 WARN("Invalid argument specified\n");
1436 return D3DERR_INVALIDCALL;
1439 switch (c->desc.Class)
1441 case D3DXPC_SCALAR:
1442 case D3DXPC_VECTOR:
1443 case D3DXPC_STRUCT:
1444 set(table, device, c, &indata, intype, &count, 4, D3DXPC_VECTOR, 0, FALSE);
1445 return D3D_OK;
1447 case D3DXPC_MATRIX_ROWS:
1448 case D3DXPC_MATRIX_COLUMNS:
1449 return D3D_OK;
1451 default:
1452 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1453 return D3DERR_INVALIDCALL;
1457 static HRESULT set_vector_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1458 const void *indata, UINT count, D3DXPARAMETER_TYPE intype)
1460 struct ctab_constant *c = get_valid_constant(table, constant);
1462 if (!c)
1464 WARN("Invalid argument specified\n");
1465 return D3DERR_INVALIDCALL;
1468 switch (c->desc.Class)
1470 case D3DXPC_SCALAR:
1471 case D3DXPC_VECTOR:
1472 case D3DXPC_MATRIX_ROWS:
1473 case D3DXPC_MATRIX_COLUMNS:
1474 case D3DXPC_STRUCT:
1475 count *= 4;
1476 set(table, device, c, &indata, intype, &count, 4, D3DXPC_VECTOR, 0, FALSE);
1477 return D3D_OK;
1479 default:
1480 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1481 return D3DERR_INVALIDCALL;
1485 static HRESULT set_matrix_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1486 const void *indata, UINT count, BOOL transpose)
1488 struct ctab_constant *c = get_valid_constant(table, constant);
1490 if (!c)
1492 WARN("Invalid argument specified\n");
1493 return D3DERR_INVALIDCALL;
1496 switch (c->desc.Class)
1498 case D3DXPC_SCALAR:
1499 case D3DXPC_VECTOR:
1500 case D3DXPC_MATRIX_ROWS:
1501 case D3DXPC_MATRIX_COLUMNS:
1502 case D3DXPC_STRUCT:
1503 count *= 16;
1504 set(table, device, c, &indata, D3DXPT_FLOAT, &count, 4,
1505 transpose ? D3DXPC_MATRIX_ROWS : D3DXPC_MATRIX_COLUMNS, 0, FALSE);
1506 return D3D_OK;
1508 default:
1509 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1510 return D3DERR_INVALIDCALL;
1514 static HRESULT set_matrix_pointer_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device,
1515 D3DXHANDLE constant, const void **indata, UINT count, BOOL transpose)
1517 struct ctab_constant *c = get_valid_constant(table, constant);
1519 if (!c)
1521 WARN("Invalid argument specified\n");
1522 return D3DERR_INVALIDCALL;
1525 switch (c->desc.Class)
1527 case D3DXPC_SCALAR:
1528 case D3DXPC_VECTOR:
1529 case D3DXPC_MATRIX_ROWS:
1530 case D3DXPC_MATRIX_COLUMNS:
1531 case D3DXPC_STRUCT:
1532 count *= 16;
1533 set(table, device, c, indata, D3DXPT_FLOAT, &count, 4,
1534 transpose ? D3DXPC_MATRIX_ROWS : D3DXPC_MATRIX_COLUMNS, 0, TRUE);
1535 return D3D_OK;
1537 default:
1538 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1539 return D3DERR_INVALIDCALL;
1543 static HRESULT WINAPI ID3DXConstantTableImpl_SetDefaults(struct ID3DXConstantTable *iface,
1544 struct IDirect3DDevice9 *device)
1546 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1547 UINT i;
1549 TRACE("iface %p, device %p\n", iface, device);
1551 if (!device)
1553 WARN("Invalid argument specified\n");
1554 return D3DERR_INVALIDCALL;
1557 for (i = 0; i < This->desc.Constants; i++)
1559 D3DXCONSTANT_DESC *desc = &This->constants[i].desc;
1560 HRESULT hr;
1562 if (!desc->DefaultValue)
1563 continue;
1565 switch (desc->RegisterSet)
1567 case D3DXRS_BOOL:
1568 if (is_vertex_shader(This->desc.Version))
1569 hr = IDirect3DDevice9_SetVertexShaderConstantB(device, desc->RegisterIndex, desc->DefaultValue,
1570 desc->RegisterCount);
1571 else
1572 hr = IDirect3DDevice9_SetPixelShaderConstantB(device, desc->RegisterIndex, desc->DefaultValue,
1573 desc->RegisterCount);
1574 break;
1576 case D3DXRS_INT4:
1577 if (is_vertex_shader(This->desc.Version))
1578 hr = IDirect3DDevice9_SetVertexShaderConstantI(device, desc->RegisterIndex, desc->DefaultValue,
1579 desc->RegisterCount);
1580 else
1581 hr = IDirect3DDevice9_SetPixelShaderConstantI(device, desc->RegisterIndex, desc->DefaultValue,
1582 desc->RegisterCount);
1583 break;
1585 case D3DXRS_FLOAT4:
1586 if (is_vertex_shader(This->desc.Version))
1587 hr = IDirect3DDevice9_SetVertexShaderConstantF(device, desc->RegisterIndex, desc->DefaultValue,
1588 desc->RegisterCount);
1589 else
1590 hr = IDirect3DDevice9_SetPixelShaderConstantF(device, desc->RegisterIndex, desc->DefaultValue,
1591 desc->RegisterCount);
1592 break;
1594 default:
1595 FIXME("Unhandled register set %s\n", debug_d3dxparameter_registerset(desc->RegisterSet));
1596 hr = E_NOTIMPL;
1597 break;
1600 if (hr != D3D_OK)
1601 return hr;
1604 return D3D_OK;
1607 static HRESULT WINAPI ID3DXConstantTableImpl_SetValue(struct ID3DXConstantTable *iface,
1608 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const void *data, unsigned int bytes)
1610 struct ID3DXConstantTableImpl *table = impl_from_ID3DXConstantTable(iface);
1611 struct ctab_constant *c = get_valid_constant(table, constant);
1612 D3DXCONSTANT_DESC *desc;
1614 TRACE("iface %p, device %p, constant %p, data %p, bytes %u\n", iface, device, constant, data, bytes);
1616 if (!device || !c || !data)
1618 WARN("Invalid argument specified\n");
1619 return D3DERR_INVALIDCALL;
1622 desc = &c->desc;
1624 switch (desc->Class)
1626 case D3DXPC_SCALAR:
1627 case D3DXPC_VECTOR:
1628 case D3DXPC_MATRIX_ROWS:
1629 case D3DXPC_MATRIX_COLUMNS:
1630 case D3DXPC_STRUCT:
1631 bytes /= 4;
1632 set(table, device, c, &data, desc->Type, &bytes, desc->Columns, D3DXPC_SCALAR, 0, FALSE);
1633 return D3D_OK;
1635 default:
1636 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(desc->Class));
1637 return D3DERR_INVALIDCALL;
1641 static HRESULT WINAPI ID3DXConstantTableImpl_SetBool(struct ID3DXConstantTable *iface,
1642 struct IDirect3DDevice9 *device, D3DXHANDLE constant, BOOL b)
1644 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1646 TRACE("iface %p, device %p, constant %p, b %d\n", iface, device, constant, b);
1648 return set_scalar(This, device, constant, &b, D3DXPT_BOOL);
1651 static HRESULT WINAPI ID3DXConstantTableImpl_SetBoolArray(struct ID3DXConstantTable *iface,
1652 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const BOOL *b, UINT count)
1654 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1656 TRACE("iface %p, device %p, constant %p, b %p, count %d\n", iface, device, constant, b, count);
1658 return set_scalar_array(This, device, constant, b, count, D3DXPT_BOOL);
1661 static HRESULT WINAPI ID3DXConstantTableImpl_SetInt(struct ID3DXConstantTable *iface,
1662 struct IDirect3DDevice9 *device, D3DXHANDLE constant, INT n)
1664 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1666 TRACE("iface %p, device %p, constant %p, n %d\n", iface, device, constant, n);
1668 return set_scalar(This, device, constant, &n, D3DXPT_INT);
1671 static HRESULT WINAPI ID3DXConstantTableImpl_SetIntArray(struct ID3DXConstantTable *iface,
1672 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const INT *n, UINT count)
1674 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1676 TRACE("iface %p, device %p, constant %p, n %p, count %d\n", iface, device, constant, n, count);
1678 return set_scalar_array(This, device, constant, n, count, D3DXPT_INT);
1681 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloat(struct ID3DXConstantTable *iface,
1682 struct IDirect3DDevice9 *device, D3DXHANDLE constant, float f)
1684 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1686 TRACE("iface %p, device %p, constant %p, f %f\n", iface, device, constant, f);
1688 return set_scalar(This, device, constant, &f, D3DXPT_FLOAT);
1691 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloatArray(struct ID3DXConstantTable *iface,
1692 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const float *f, UINT count)
1694 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1696 TRACE("iface %p, device %p, constant %p, f %p, count %d\n", iface, device, constant, f, count);
1698 return set_scalar_array(This, device, constant, f, count, D3DXPT_FLOAT);
1701 static HRESULT WINAPI ID3DXConstantTableImpl_SetVector(struct ID3DXConstantTable *iface,
1702 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXVECTOR4 *vector)
1704 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1706 TRACE("iface %p, device %p, constant %p, vector %p\n", iface, device, constant, vector);
1708 return set_vector(This, device, constant, vector, D3DXPT_FLOAT);
1711 static HRESULT WINAPI ID3DXConstantTableImpl_SetVectorArray(struct ID3DXConstantTable *iface,
1712 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXVECTOR4 *vector, UINT count)
1714 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1716 TRACE("iface %p, device %p, constant %p, vector %p, count %u\n", iface, device, constant, vector, count);
1718 return set_vector_array(This, device, constant, vector, count, D3DXPT_FLOAT);
1721 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrix(struct ID3DXConstantTable *iface,
1722 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix)
1724 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1726 TRACE("iface %p, device %p, constant %p, matrix %p\n", iface, device, constant, matrix);
1728 return set_matrix_array(This, device, constant, matrix, 1, FALSE);
1731 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixArray(struct ID3DXConstantTable *iface,
1732 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
1734 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1736 TRACE("iface %p, device %p, constant %p, matrix %p, count %u\n", iface, device, constant, matrix, count);
1738 return set_matrix_array(This, device, constant, matrix, count, FALSE);
1741 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixPointerArray(struct ID3DXConstantTable *iface,
1742 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
1744 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1746 TRACE("iface %p, device %p, constant %p, matrix %p, count %u)\n", iface, device, constant, matrix, count);
1748 return set_matrix_pointer_array(This, device, constant, (const void **)matrix, count, FALSE);
1751 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTranspose(struct ID3DXConstantTable *iface,
1752 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix)
1754 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1756 TRACE("iface %p, device %p, constant %p, matrix %p\n", iface, device, constant, matrix);
1758 return set_matrix_array(This, device, constant, matrix, 1, TRUE);
1761 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposeArray(struct ID3DXConstantTable *iface,
1762 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
1764 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1766 TRACE("iface %p, device %p, constant %p, matrix %p, count %u\n", iface, device, constant, matrix, count);
1768 return set_matrix_array(This, device, constant, matrix, count, TRUE);
1771 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposePointerArray(struct ID3DXConstantTable *iface,
1772 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
1774 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1776 TRACE("iface %p, device %p, constant %p, matrix %p, count %u)\n", iface, device, constant, matrix, count);
1778 return set_matrix_pointer_array(This, device, constant, (const void **)matrix, count, TRUE);
1781 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl =
1783 /*** IUnknown methods ***/
1784 ID3DXConstantTableImpl_QueryInterface,
1785 ID3DXConstantTableImpl_AddRef,
1786 ID3DXConstantTableImpl_Release,
1787 /*** ID3DXBuffer methods ***/
1788 ID3DXConstantTableImpl_GetBufferPointer,
1789 ID3DXConstantTableImpl_GetBufferSize,
1790 /*** ID3DXConstantTable methods ***/
1791 ID3DXConstantTableImpl_GetDesc,
1792 ID3DXConstantTableImpl_GetConstantDesc,
1793 ID3DXConstantTableImpl_GetSamplerIndex,
1794 ID3DXConstantTableImpl_GetConstant,
1795 ID3DXConstantTableImpl_GetConstantByName,
1796 ID3DXConstantTableImpl_GetConstantElement,
1797 ID3DXConstantTableImpl_SetDefaults,
1798 ID3DXConstantTableImpl_SetValue,
1799 ID3DXConstantTableImpl_SetBool,
1800 ID3DXConstantTableImpl_SetBoolArray,
1801 ID3DXConstantTableImpl_SetInt,
1802 ID3DXConstantTableImpl_SetIntArray,
1803 ID3DXConstantTableImpl_SetFloat,
1804 ID3DXConstantTableImpl_SetFloatArray,
1805 ID3DXConstantTableImpl_SetVector,
1806 ID3DXConstantTableImpl_SetVectorArray,
1807 ID3DXConstantTableImpl_SetMatrix,
1808 ID3DXConstantTableImpl_SetMatrixArray,
1809 ID3DXConstantTableImpl_SetMatrixPointerArray,
1810 ID3DXConstantTableImpl_SetMatrixTranspose,
1811 ID3DXConstantTableImpl_SetMatrixTransposeArray,
1812 ID3DXConstantTableImpl_SetMatrixTransposePointerArray
1815 static HRESULT parse_ctab_constant_type(const char *ctab, DWORD typeoffset, struct ctab_constant *constant,
1816 BOOL is_element, WORD index, WORD max_index, DWORD *offset, DWORD nameoffset, UINT regset)
1818 const D3DXSHADER_TYPEINFO *type = (LPD3DXSHADER_TYPEINFO)(ctab + typeoffset);
1819 const D3DXSHADER_STRUCTMEMBERINFO *memberinfo = NULL;
1820 HRESULT hr = D3D_OK;
1821 UINT i, count = 0;
1822 WORD size = 0;
1824 constant->desc.DefaultValue = offset ? ctab + *offset : NULL;
1825 constant->desc.Class = type->Class;
1826 constant->desc.Type = type->Type;
1827 constant->desc.Rows = type->Rows;
1828 constant->desc.Columns = type->Columns;
1829 constant->desc.Elements = is_element ? 1 : type->Elements;
1830 constant->desc.StructMembers = type->StructMembers;
1831 constant->desc.Name = ctab + nameoffset;
1832 constant->desc.RegisterSet = regset;
1833 constant->desc.RegisterIndex = index;
1835 TRACE("name %s, elements %u, index %u, defaultvalue %p, regset %s\n", constant->desc.Name,
1836 constant->desc.Elements, index, constant->desc.DefaultValue,
1837 debug_d3dxparameter_registerset(regset));
1838 TRACE("class %s, type %s, rows %d, columns %d, elements %d, struct_members %d\n",
1839 debug_d3dxparameter_class(type->Class), debug_d3dxparameter_type(type->Type),
1840 type->Rows, type->Columns, type->Elements, type->StructMembers);
1842 if (type->Elements > 1 && !is_element)
1844 count = type->Elements;
1846 else if ((type->Class == D3DXPC_STRUCT) && type->StructMembers)
1848 memberinfo = (D3DXSHADER_STRUCTMEMBERINFO*)(ctab + type->StructMemberInfo);
1849 count = type->StructMembers;
1852 if (count)
1854 constant->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*constant->constants) * count);
1855 if (!constant->constants)
1857 ERR("Out of memory\n");
1858 hr = E_OUTOFMEMORY;
1859 goto error;
1862 for (i = 0; i < count; ++i)
1864 hr = parse_ctab_constant_type(ctab, memberinfo ? memberinfo[i].TypeInfo : typeoffset,
1865 &constant->constants[i], memberinfo == NULL, index + size, max_index, offset,
1866 memberinfo ? memberinfo[i].Name : nameoffset, regset);
1867 if (hr != D3D_OK)
1868 goto error;
1870 size += constant->constants[i].desc.RegisterCount;
1873 else
1875 WORD offsetdiff = type->Columns * type->Rows;
1876 BOOL fail = FALSE;
1878 size = type->Columns * type->Rows;
1880 switch (regset)
1882 case D3DXRS_BOOL:
1883 fail = type->Class != D3DXPC_SCALAR && type->Class != D3DXPC_VECTOR
1884 && type->Class != D3DXPC_MATRIX_ROWS && type->Class != D3DXPC_MATRIX_COLUMNS;
1885 break;
1887 case D3DXRS_FLOAT4:
1888 case D3DXRS_INT4:
1889 switch (type->Class)
1891 case D3DXPC_VECTOR:
1892 size = 1;
1893 /* fall through */
1894 case D3DXPC_SCALAR:
1895 offsetdiff = type->Rows * 4;
1896 break;
1898 case D3DXPC_MATRIX_ROWS:
1899 offsetdiff = type->Rows * 4;
1900 size = type->Rows;
1901 break;
1903 case D3DXPC_MATRIX_COLUMNS:
1904 offsetdiff = type->Columns * 4;
1905 size = type->Columns;
1906 break;
1908 default:
1909 fail = TRUE;
1910 break;
1912 break;
1914 case D3DXRS_SAMPLER:
1915 size = 1;
1916 fail = type->Class != D3DXPC_OBJECT;
1917 break;
1919 default:
1920 fail = TRUE;
1921 break;
1924 if (fail)
1926 FIXME("Unhandled register set %s, type class %s\n", debug_d3dxparameter_registerset(regset),
1927 debug_d3dxparameter_class(type->Class));
1930 /* offset in bytes => offsetdiff * sizeof(DWORD) */
1931 if (offset) *offset += offsetdiff * 4;
1934 constant->desc.RegisterCount = max(0, min(max_index - index, size));
1935 constant->desc.Bytes = 4 * constant->desc.Elements * type->Rows * type->Columns;
1937 return D3D_OK;
1939 error:
1940 if (constant->constants)
1942 for (i = 0; i < count; ++i)
1944 free_constant(&constant->constants[i]);
1946 HeapFree(GetProcessHeap(), 0, constant->constants);
1947 constant->constants = NULL;
1950 return hr;
1953 HRESULT WINAPI D3DXGetShaderConstantTableEx(const DWORD *byte_code, DWORD flags, ID3DXConstantTable **constant_table)
1955 struct ID3DXConstantTableImpl *object = NULL;
1956 const void *data;
1957 HRESULT hr;
1958 UINT size;
1959 const D3DXSHADER_CONSTANTTABLE *ctab_header;
1960 const D3DXSHADER_CONSTANTINFO *constant_info;
1961 DWORD i;
1963 TRACE("byte_code %p, flags %x, constant_table %p\n", byte_code, flags, constant_table);
1965 if (constant_table) *constant_table = NULL;
1967 if (!byte_code || !constant_table)
1969 WARN("Invalid argument specified.\n");
1970 return D3DERR_INVALIDCALL;
1973 if (!is_valid_bytecode(*byte_code))
1975 WARN("Invalid byte_code specified.\n");
1976 return D3D_OK;
1979 if (flags) FIXME("Flags (%#x) are not handled, yet!\n", flags);
1981 hr = D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), &data, &size);
1982 if (hr != D3D_OK)
1984 WARN("CTAB not found.\n");
1985 return D3DXERR_INVALIDDATA;
1988 if (size < sizeof(*ctab_header))
1990 WARN("Invalid CTAB size.\n");
1991 return D3DXERR_INVALIDDATA;
1994 ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
1995 if (ctab_header->Size != sizeof(*ctab_header))
1997 WARN("Invalid D3DXSHADER_CONSTANTTABLE size.\n");
1998 return D3DXERR_INVALIDDATA;
2001 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2002 if (!object)
2003 return E_OUTOFMEMORY;
2005 object->ID3DXConstantTable_iface.lpVtbl = &ID3DXConstantTable_Vtbl;
2006 object->ref = 1;
2008 object->ctab = HeapAlloc(GetProcessHeap(), 0, size);
2009 if (!object->ctab)
2011 ERR("Out of memory\n");
2012 HeapFree(GetProcessHeap(), 0, object);
2013 return E_OUTOFMEMORY;
2015 object->size = size;
2016 memcpy(object->ctab, data, object->size);
2018 object->desc.Creator = ctab_header->Creator ? object->ctab + ctab_header->Creator : NULL;
2019 object->desc.Version = ctab_header->Version;
2020 object->desc.Constants = ctab_header->Constants;
2021 TRACE("Creator %s, Version %x, Constants %u, Target %s\n",
2022 debugstr_a(object->desc.Creator), object->desc.Version, object->desc.Constants,
2023 debugstr_a(ctab_header->Target ? object->ctab + ctab_header->Target : NULL));
2025 object->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2026 sizeof(*object->constants) * object->desc.Constants);
2027 if (!object->constants)
2029 ERR("Out of memory\n");
2030 hr = E_OUTOFMEMORY;
2031 goto error;
2034 constant_info = (const D3DXSHADER_CONSTANTINFO *)(object->ctab + ctab_header->ConstantInfo);
2035 for (i = 0; i < ctab_header->Constants; i++)
2037 DWORD offset = constant_info[i].DefaultValue;
2039 hr = parse_ctab_constant_type(object->ctab, constant_info[i].TypeInfo,
2040 &object->constants[i], FALSE, constant_info[i].RegisterIndex,
2041 constant_info[i].RegisterIndex + constant_info[i].RegisterCount,
2042 offset ? &offset : NULL, constant_info[i].Name, constant_info[i].RegisterSet);
2043 if (hr != D3D_OK)
2044 goto error;
2047 * Set the register count, it may differ for D3DXRS_INT4, because somehow
2048 * it makes the assumption that the register size is 1 instead of 4, so the
2049 * count is 4 times bigger. This holds true only for toplevel shader
2050 * constants. The count of elements and members is always based on a
2051 * register size of 4.
2053 if (object->constants[i].desc.RegisterSet == D3DXRS_INT4)
2055 object->constants[i].desc.RegisterCount = constant_info[i].RegisterCount;
2057 object->constants[i].constantinfo_reserved = constant_info[i].Reserved;
2060 *constant_table = &object->ID3DXConstantTable_iface;
2062 return D3D_OK;
2064 error:
2065 free_constant_table(object);
2066 HeapFree(GetProcessHeap(), 0, object);
2068 return hr;
2071 HRESULT WINAPI D3DXGetShaderConstantTable(const DWORD *byte_code, ID3DXConstantTable **constant_table)
2073 TRACE("(%p, %p): Forwarded to D3DXGetShaderConstantTableEx\n", byte_code, constant_table);
2075 return D3DXGetShaderConstantTableEx(byte_code, 0, constant_table);
2078 HRESULT WINAPI D3DXCreateFragmentLinker(IDirect3DDevice9 *device, UINT size, ID3DXFragmentLinker **linker)
2080 FIXME("device %p, size %u, linker %p: stub.\n", device, size, linker);
2082 if (linker)
2083 *linker = NULL;
2086 return E_NOTIMPL;
2089 HRESULT WINAPI D3DXCreateFragmentLinkerEx(IDirect3DDevice9 *device, UINT size, DWORD flags, ID3DXFragmentLinker **linker)
2091 FIXME("device %p, size %u, flags %#x, linker %p: stub.\n", device, size, flags, linker);
2093 if (linker)
2094 *linker = NULL;
2096 return E_NOTIMPL;
2099 HRESULT WINAPI D3DXGetShaderSamplers(const DWORD *byte_code, const char **samplers, UINT *count)
2101 UINT i, sampler_count = 0;
2102 UINT size;
2103 const char *data;
2104 const D3DXSHADER_CONSTANTTABLE *ctab_header;
2105 const D3DXSHADER_CONSTANTINFO *constant_info;
2107 TRACE("byte_code %p, samplers %p, count %p\n", byte_code, samplers, count);
2109 if (count) *count = 0;
2111 if (D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), (const void **)&data, &size) != D3D_OK)
2112 return D3D_OK;
2114 if (size < sizeof(*ctab_header)) return D3D_OK;
2116 ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
2117 if (ctab_header->Size != sizeof(*ctab_header)) return D3D_OK;
2119 constant_info = (const D3DXSHADER_CONSTANTINFO *)(data + ctab_header->ConstantInfo);
2120 for (i = 0; i < ctab_header->Constants; i++)
2122 const D3DXSHADER_TYPEINFO *type;
2124 TRACE("name = %s\n", data + constant_info[i].Name);
2126 type = (const D3DXSHADER_TYPEINFO *)(data + constant_info[i].TypeInfo);
2128 if (type->Type == D3DXPT_SAMPLER
2129 || type->Type == D3DXPT_SAMPLER1D
2130 || type->Type == D3DXPT_SAMPLER2D
2131 || type->Type == D3DXPT_SAMPLER3D
2132 || type->Type == D3DXPT_SAMPLERCUBE)
2134 if (samplers) samplers[sampler_count] = data + constant_info[i].Name;
2136 ++sampler_count;
2140 TRACE("Found %u samplers\n", sampler_count);
2142 if (count) *count = sampler_count;
2144 return D3D_OK;
2147 HRESULT WINAPI D3DXDisassembleShader(const DWORD *shader, BOOL colorcode, const char *comments, ID3DXBuffer **disassembly)
2149 FIXME("%p %d %s %p: stub\n", shader, colorcode, debugstr_a(comments), disassembly);
2150 return E_OUTOFMEMORY;
2153 struct d3dx9_texture_shader
2155 ID3DXTextureShader ID3DXTextureShader_iface;
2156 LONG ref;
2159 static inline struct d3dx9_texture_shader *impl_from_ID3DXTextureShader(ID3DXTextureShader *iface)
2161 return CONTAINING_RECORD(iface, struct d3dx9_texture_shader, ID3DXTextureShader_iface);
2164 static HRESULT WINAPI d3dx9_texture_shader_QueryInterface(ID3DXTextureShader *iface, REFIID riid, void **out)
2166 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
2168 if (IsEqualGUID(riid, &IID_IUnknown) ||
2169 IsEqualGUID(riid, &IID_ID3DXTextureShader))
2171 iface->lpVtbl->AddRef(iface);
2172 *out = iface;
2173 return D3D_OK;
2176 WARN("Interface %s not found.\n", debugstr_guid(riid));
2177 *out = NULL;
2178 return E_NOINTERFACE;
2181 static ULONG WINAPI d3dx9_texture_shader_AddRef(ID3DXTextureShader *iface)
2183 struct d3dx9_texture_shader *texture_shader = impl_from_ID3DXTextureShader(iface);
2184 ULONG refcount = InterlockedIncrement(&texture_shader->ref);
2186 TRACE("%p increasing refcount to %u.\n", texture_shader, refcount);
2188 return refcount;
2191 static ULONG WINAPI d3dx9_texture_shader_Release(ID3DXTextureShader *iface)
2193 struct d3dx9_texture_shader *texture_shader = impl_from_ID3DXTextureShader(iface);
2194 ULONG refcount = InterlockedDecrement(&texture_shader->ref);
2196 TRACE("%p decreasing refcount to %u.\n", texture_shader, refcount);
2198 if (!refcount)
2200 HeapFree(GetProcessHeap(), 0, texture_shader);
2203 return refcount;
2206 static HRESULT WINAPI d3dx9_texture_shader_GetFunction(ID3DXTextureShader *iface, struct ID3DXBuffer **function)
2208 FIXME("iface %p, function %p stub.\n", iface, function);
2210 return E_NOTIMPL;
2213 static HRESULT WINAPI d3dx9_texture_shader_GetConstantBuffer(ID3DXTextureShader *iface, struct ID3DXBuffer **constant_buffer)
2215 FIXME("iface %p, constant_buffer %p stub.\n", iface, constant_buffer);
2217 return E_NOTIMPL;
2220 static HRESULT WINAPI d3dx9_texture_shader_GetDesc(ID3DXTextureShader *iface, D3DXCONSTANTTABLE_DESC *desc)
2222 FIXME("iface %p, desc %p stub.\n", iface, desc);
2224 return E_NOTIMPL;
2227 static HRESULT WINAPI d3dx9_texture_shader_GetConstantDesc(ID3DXTextureShader *iface, D3DXHANDLE constant, D3DXCONSTANT_DESC *constant_desc, UINT *count)
2229 FIXME("iface %p, constant %p, constant_desc %p, count %p stub.\n", iface, constant, constant_desc, count);
2231 return E_NOTIMPL;
2234 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstant(ID3DXTextureShader *iface, D3DXHANDLE constant, UINT index)
2236 FIXME("iface %p, constant %p, index %u stub.\n", iface, constant, index);
2238 return NULL;
2241 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstantByName(ID3DXTextureShader *iface, D3DXHANDLE constant, const char *name)
2243 FIXME("iface %p, constant %p, name %s stub.\n", iface, constant, debugstr_a(name));
2245 return NULL;
2248 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstantElement(ID3DXTextureShader *iface, D3DXHANDLE constant, UINT index)
2250 FIXME("iface %p, constant %p, index %u stub.\n", iface, constant, index);
2252 return NULL;
2255 static HRESULT WINAPI d3dx9_texture_shader_SetDefaults(ID3DXTextureShader *iface)
2257 FIXME("iface %p stub.\n", iface);
2259 return E_NOTIMPL;
2262 static HRESULT WINAPI d3dx9_texture_shader_SetValue(ID3DXTextureShader *iface, D3DXHANDLE constant, const void *data, UINT bytes)
2264 FIXME("iface %p, constant %p, data %p, bytes %u stub.\n", iface, constant, data, bytes);
2266 return E_NOTIMPL;
2269 static HRESULT WINAPI d3dx9_texture_shader_SetBool(ID3DXTextureShader *iface, D3DXHANDLE constant, BOOL b)
2271 FIXME("iface %p, constant %p, b %u stub.\n", iface, constant, b);
2273 return E_NOTIMPL;
2276 static HRESULT WINAPI d3dx9_texture_shader_SetBoolArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const BOOL *b, UINT count)
2278 FIXME("iface %p, constant %p, b %p, count %u stub.\n", iface, constant, b, count);
2280 return E_NOTIMPL;
2283 static HRESULT WINAPI d3dx9_texture_shader_SetInt(ID3DXTextureShader *iface, D3DXHANDLE constant, INT n)
2285 FIXME("iface %p, constant %p, n %d stub.\n", iface, constant, n);
2287 return E_NOTIMPL;
2290 static HRESULT WINAPI d3dx9_texture_shader_SetIntArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const INT *n, UINT count)
2292 FIXME("iface %p, constant %p, n %p, count %u stub.\n", iface, constant, n, count);
2294 return E_NOTIMPL;
2297 static HRESULT WINAPI d3dx9_texture_shader_SetFloat(ID3DXTextureShader *iface, D3DXHANDLE constant, FLOAT f)
2299 FIXME("iface %p, constant %p, f %f stub.\n", iface, constant, f);
2301 return E_NOTIMPL;
2304 static HRESULT WINAPI d3dx9_texture_shader_SetFloatArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const FLOAT *f, UINT count)
2306 FIXME("iface %p, constant %p, f %p, count %u stub.\n", iface, constant, f, count);
2308 return E_NOTIMPL;
2311 static HRESULT WINAPI d3dx9_texture_shader_SetVector(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXVECTOR4 *vector)
2313 FIXME("iface %p, constant %p, vector %p stub.\n", iface, constant, vector);
2315 return E_NOTIMPL;
2318 static HRESULT WINAPI d3dx9_texture_shader_SetVectorArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXVECTOR4 *vector, UINT count)
2320 FIXME("iface %p, constant %p, vector %p, count %u stub.\n", iface, constant, vector, count);
2322 return E_NOTIMPL;
2325 static HRESULT WINAPI d3dx9_texture_shader_SetMatrix(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix)
2327 FIXME("iface %p, constant %p, matrix %p stub.\n", iface, constant, matrix);
2329 return E_NOTIMPL;
2332 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
2334 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2336 return E_NOTIMPL;
2339 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixPointerArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
2341 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2343 return E_NOTIMPL;
2346 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTranspose(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix)
2348 FIXME("iface %p, constant %p, matrix %p stub.\n", iface, constant, matrix);
2350 return E_NOTIMPL;
2353 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTransposeArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
2355 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2357 return E_NOTIMPL;
2360 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTransposePointerArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
2362 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2364 return E_NOTIMPL;
2367 static const struct ID3DXTextureShaderVtbl d3dx9_texture_shader_vtbl =
2369 /*** IUnknown methods ***/
2370 d3dx9_texture_shader_QueryInterface,
2371 d3dx9_texture_shader_AddRef,
2372 d3dx9_texture_shader_Release,
2373 /*** ID3DXTextureShader methods ***/
2374 d3dx9_texture_shader_GetFunction,
2375 d3dx9_texture_shader_GetConstantBuffer,
2376 d3dx9_texture_shader_GetDesc,
2377 d3dx9_texture_shader_GetConstantDesc,
2378 d3dx9_texture_shader_GetConstant,
2379 d3dx9_texture_shader_GetConstantByName,
2380 d3dx9_texture_shader_GetConstantElement,
2381 d3dx9_texture_shader_SetDefaults,
2382 d3dx9_texture_shader_SetValue,
2383 d3dx9_texture_shader_SetBool,
2384 d3dx9_texture_shader_SetBoolArray,
2385 d3dx9_texture_shader_SetInt,
2386 d3dx9_texture_shader_SetIntArray,
2387 d3dx9_texture_shader_SetFloat,
2388 d3dx9_texture_shader_SetFloatArray,
2389 d3dx9_texture_shader_SetVector,
2390 d3dx9_texture_shader_SetVectorArray,
2391 d3dx9_texture_shader_SetMatrix,
2392 d3dx9_texture_shader_SetMatrixArray,
2393 d3dx9_texture_shader_SetMatrixPointerArray,
2394 d3dx9_texture_shader_SetMatrixTranspose,
2395 d3dx9_texture_shader_SetMatrixTransposeArray,
2396 d3dx9_texture_shader_SetMatrixTransposePointerArray
2399 HRESULT WINAPI D3DXCreateTextureShader(const DWORD *function, ID3DXTextureShader **texture_shader)
2401 struct d3dx9_texture_shader *object;
2403 TRACE("function %p, texture_shader %p.\n", function, texture_shader);
2405 if (!function || !texture_shader)
2406 return D3DERR_INVALIDCALL;
2408 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
2409 if (!object)
2410 return E_OUTOFMEMORY;
2412 object->ID3DXTextureShader_iface.lpVtbl = &d3dx9_texture_shader_vtbl;
2413 object->ref = 1;
2415 *texture_shader = &object->ID3DXTextureShader_iface;
2417 return D3D_OK;