dinput/tests: Improve controller debug information.
[wine.git] / dlls / d3dx9_36 / shader.c
blob4dbecb15a44b9505ca2b65a80da78715ab6c1169
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 ctab_constant {
685 D3DXCONSTANT_DESC desc;
686 struct ctab_constant *constants;
689 struct ID3DXConstantTableImpl {
690 ID3DXConstantTable ID3DXConstantTable_iface;
691 LONG ref;
692 char *ctab;
693 DWORD size;
694 D3DXCONSTANTTABLE_DESC desc;
695 struct ctab_constant *constants;
698 static void free_constant(struct ctab_constant *constant)
700 if (constant->constants)
702 UINT i, count = constant->desc.Elements > 1 ? constant->desc.Elements : constant->desc.StructMembers;
704 for (i = 0; i < count; ++i)
706 free_constant(&constant->constants[i]);
708 HeapFree(GetProcessHeap(), 0, constant->constants);
712 static void free_constant_table(struct ID3DXConstantTableImpl *table)
714 if (table->constants)
716 UINT i;
718 for (i = 0; i < table->desc.Constants; ++i)
720 free_constant(&table->constants[i]);
722 HeapFree(GetProcessHeap(), 0, table->constants);
724 HeapFree(GetProcessHeap(), 0, table->ctab);
727 static inline struct ID3DXConstantTableImpl *impl_from_ID3DXConstantTable(ID3DXConstantTable *iface)
729 return CONTAINING_RECORD(iface, struct ID3DXConstantTableImpl, ID3DXConstantTable_iface);
732 static inline BOOL is_vertex_shader(DWORD version)
734 return (version & 0xffff0000) == 0xfffe0000;
737 static inline D3DXHANDLE handle_from_constant(struct ctab_constant *constant)
739 return (D3DXHANDLE)constant;
742 static struct ctab_constant *get_constant_by_name(struct ID3DXConstantTableImpl *table,
743 struct ctab_constant *constant, const char *name);
745 static struct ctab_constant *get_constant_element_by_name(struct ctab_constant *constant, const char *name)
747 const char *part;
748 UINT element;
750 TRACE("constant %p, name %s\n", constant, debugstr_a(name));
752 if (!name || !*name) return NULL;
754 element = atoi(name);
755 part = strchr(name, ']') + 1;
757 if (constant->desc.Elements > element)
759 struct ctab_constant *c = constant->constants ? &constant->constants[element] : constant;
761 switch (*part++)
763 case '.':
764 return get_constant_by_name(NULL, c, part);
766 case '[':
767 return get_constant_element_by_name(c, part);
769 case '\0':
770 TRACE("Returning parameter %p\n", c);
771 return c;
773 default:
774 FIXME("Unhandled case \"%c\"\n", *--part);
775 break;
779 TRACE("Constant not found\n");
780 return NULL;
783 static struct ctab_constant *get_constant_by_name(struct ID3DXConstantTableImpl *table,
784 struct ctab_constant *constant, const char *name)
786 UINT i, count, length;
787 struct ctab_constant *handles;
788 const char *part;
790 TRACE("table %p, constant %p, name %s\n", table, constant, debugstr_a(name));
792 if (!name || !*name) return NULL;
794 if (!constant)
796 count = table->desc.Constants;
797 handles = table->constants;
799 else
801 count = constant->desc.StructMembers;
802 handles = constant->constants;
805 length = strcspn(name, "[.");
806 part = name + length;
808 for (i = 0; i < count; i++)
810 if (strlen(handles[i].desc.Name) == length && !strncmp(handles[i].desc.Name, name, length))
812 switch (*part++)
814 case '.':
815 return get_constant_by_name(NULL, &handles[i], part);
817 case '[':
818 return get_constant_element_by_name(&handles[i], part);
820 default:
821 TRACE("Returning parameter %p\n", &handles[i]);
822 return &handles[i];
827 TRACE("Constant not found\n");
828 return NULL;
831 static struct ctab_constant *is_valid_sub_constant(struct ctab_constant *parent, D3DXHANDLE handle)
833 struct ctab_constant *c;
834 UINT i, count;
836 /* all variable have at least elements = 1, but not always elements */
837 if (!parent->constants) return NULL;
839 count = parent->desc.Elements > 1 ? parent->desc.Elements : parent->desc.StructMembers;
840 for (i = 0; i < count; ++i)
842 if (handle_from_constant(&parent->constants[i]) == handle)
843 return &parent->constants[i];
845 c = is_valid_sub_constant(&parent->constants[i], handle);
846 if (c) return c;
849 return NULL;
852 static inline struct ctab_constant *get_valid_constant(struct ID3DXConstantTableImpl *table, D3DXHANDLE handle)
854 struct ctab_constant *c;
855 UINT i;
857 if (!handle) return NULL;
859 for (i = 0; i < table->desc.Constants; ++i)
861 if (handle_from_constant(&table->constants[i]) == handle)
862 return &table->constants[i];
864 c = is_valid_sub_constant(&table->constants[i], handle);
865 if (c) return c;
868 return get_constant_by_name(table, NULL, handle);
871 /*** IUnknown methods ***/
872 static HRESULT WINAPI ID3DXConstantTableImpl_QueryInterface(ID3DXConstantTable *iface, REFIID riid, void **out)
874 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
876 if (IsEqualGUID(riid, &IID_IUnknown) ||
877 IsEqualGUID(riid, &IID_ID3DXBuffer) ||
878 IsEqualGUID(riid, &IID_ID3DXConstantTable))
880 ID3DXConstantTable_AddRef(iface);
881 *out = iface;
882 return S_OK;
885 WARN("Interface %s not found.\n", debugstr_guid(riid));
887 return E_NOINTERFACE;
890 static ULONG WINAPI ID3DXConstantTableImpl_AddRef(ID3DXConstantTable *iface)
892 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
894 TRACE("(%p)->(): AddRef from %d\n", This, This->ref);
896 return InterlockedIncrement(&This->ref);
899 static ULONG WINAPI ID3DXConstantTableImpl_Release(ID3DXConstantTable *iface)
901 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
902 ULONG ref = InterlockedDecrement(&This->ref);
904 TRACE("(%p)->(): Release from %d\n", This, ref + 1);
906 if (!ref)
908 free_constant_table(This);
909 HeapFree(GetProcessHeap(), 0, This);
912 return ref;
915 /*** ID3DXBuffer methods ***/
916 static void * WINAPI ID3DXConstantTableImpl_GetBufferPointer(ID3DXConstantTable *iface)
918 struct ID3DXConstantTableImpl *table = impl_from_ID3DXConstantTable(iface);
920 TRACE("iface %p.\n", iface);
922 return table->ctab;
925 static DWORD WINAPI ID3DXConstantTableImpl_GetBufferSize(ID3DXConstantTable *iface)
927 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
929 TRACE("(%p)->()\n", This);
931 return This->size;
934 /*** ID3DXConstantTable methods ***/
935 static HRESULT WINAPI ID3DXConstantTableImpl_GetDesc(ID3DXConstantTable *iface, D3DXCONSTANTTABLE_DESC *desc)
937 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
939 TRACE("(%p)->(%p)\n", This, desc);
941 if (!desc)
942 return D3DERR_INVALIDCALL;
944 *desc = This->desc;
946 return D3D_OK;
949 static HRESULT WINAPI ID3DXConstantTableImpl_GetConstantDesc(ID3DXConstantTable *iface, D3DXHANDLE constant,
950 D3DXCONSTANT_DESC *desc, UINT *count)
952 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
953 struct ctab_constant *c = get_valid_constant(This, constant);
955 TRACE("(%p)->(%p, %p, %p)\n", This, constant, desc, count);
957 if (!c)
959 WARN("Invalid argument specified\n");
960 return D3DERR_INVALIDCALL;
963 if (desc) *desc = c->desc;
964 if (count) *count = 1;
966 return D3D_OK;
969 static UINT WINAPI ID3DXConstantTableImpl_GetSamplerIndex(ID3DXConstantTable *iface, D3DXHANDLE constant)
971 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
972 struct ctab_constant *c = get_valid_constant(This, constant);
974 TRACE("(%p)->(%p)\n", This, constant);
976 if (!c || c->desc.RegisterSet != D3DXRS_SAMPLER)
978 WARN("Invalid argument specified\n");
979 return (UINT)-1;
982 TRACE("Returning RegisterIndex %u\n", c->desc.RegisterIndex);
983 return c->desc.RegisterIndex;
986 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstant(ID3DXConstantTable *iface, D3DXHANDLE constant, UINT index)
988 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
989 struct ctab_constant *c;
991 TRACE("(%p)->(%p, %d)\n", This, constant, index);
993 if (constant)
995 c = get_valid_constant(This, constant);
996 if (c && index < c->desc.StructMembers)
998 c = &c->constants[index];
999 TRACE("Returning constant %p\n", c);
1000 return handle_from_constant(c);
1003 else
1005 if (index < This->desc.Constants)
1007 c = &This->constants[index];
1008 TRACE("Returning constant %p\n", c);
1009 return handle_from_constant(c);
1013 WARN("Index out of range\n");
1014 return NULL;
1017 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantByName(ID3DXConstantTable *iface,
1018 D3DXHANDLE constant, const char *name)
1020 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1021 struct ctab_constant *c = get_valid_constant(This, constant);
1023 TRACE("iface %p, constant %p, name %s.\n", iface, constant, debugstr_a(name));
1025 c = get_constant_by_name(This, c, name);
1026 TRACE("Returning constant %p\n", c);
1028 return handle_from_constant(c);
1031 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantElement(ID3DXConstantTable *iface, D3DXHANDLE constant, UINT index)
1033 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1034 struct ctab_constant *c = get_valid_constant(This, constant);
1036 TRACE("(%p)->(%p, %d)\n", This, constant, index);
1038 if (c && index < c->desc.Elements)
1040 if (c->desc.Elements > 1) c = &c->constants[index];
1041 TRACE("Returning constant %p\n", c);
1042 return handle_from_constant(c);
1045 WARN("Invalid argument specified\n");
1046 return NULL;
1049 static inline DWORD get_index(const void **indata, UINT index, BOOL is_pointer)
1051 if (!indata)
1052 return 0;
1054 if (is_pointer)
1055 return ((DWORD **)indata)[index / 16][index % 16];
1057 return (*((DWORD **)indata))[index];
1060 static UINT set(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, struct ctab_constant *constant,
1061 const void **indata, D3DXPARAMETER_TYPE intype, UINT *size, UINT incol, D3DXPARAMETER_CLASS inclass, UINT index,
1062 BOOL is_pointer)
1064 D3DXCONSTANT_DESC *desc = &constant->desc;
1065 UINT l, i, regcount = 1, regsize = 1, cin = 1, rin = 1, ret, last = 0;
1066 DWORD tmp;
1068 /* size too small to set anything */
1069 if (*size < desc->Rows * desc->Columns)
1071 *size = 0;
1072 return 0;
1075 /* D3DXPC_STRUCT is somewhat special */
1076 if (desc->Class == D3DXPC_STRUCT)
1079 * Struct array sets the last complete input to the first struct element, all other
1080 * elements are not set.
1081 * E.g.: struct {int i;} s1[2];
1082 * SetValue(device, "s1", [1, 2], 8) => s1 = {2, x};
1084 * struct {int i; int n} s2[2];
1085 * SetValue(device, "s2", [1, 2, 3, 4, 5], 20) => s1 = {{3, 4}, {x, x}};
1087 if (desc->Elements > 1)
1089 UINT offset = *size / (desc->Rows * desc->Columns) - 1;
1091 offset = min(desc->Elements - 1, offset);
1092 last = offset * desc->Rows * desc->Columns;
1094 if ((is_pointer || inclass == D3DXPC_MATRIX_ROWS) && desc->RegisterSet != D3DXRS_BOOL)
1096 set(table, device, &constant->constants[0], NULL, intype, size, incol, inclass, 0, is_pointer);
1098 else
1100 last += set(table, device, &constant->constants[0], indata, intype, size, incol, inclass,
1101 index + last, is_pointer);
1104 else
1107 * D3DXRS_BOOL is always set. As there are only 16 bools and there are
1108 * exactly 16 input values, use matrix transpose.
1110 if (inclass == D3DXPC_MATRIX_ROWS && desc->RegisterSet == D3DXRS_BOOL)
1112 D3DXMATRIX mat, *m, min;
1113 D3DXMatrixTranspose(&mat, &min);
1115 if (is_pointer)
1116 min = *(D3DXMATRIX *)(indata[index / 16]);
1117 else
1118 min = **(D3DXMATRIX **)indata;
1120 D3DXMatrixTranspose(&mat, &min);
1121 m = &mat;
1122 for (i = 0; i < desc->StructMembers; ++i)
1124 last += set(table, device, &constant->constants[i], (const void **)&m, intype, size, incol,
1125 D3DXPC_SCALAR, index + last, is_pointer);
1129 * For pointers or for matrix rows, only the first member is set.
1130 * All other members are set to 0. This is not true for D3DXRS_BOOL.
1131 * E.g.: struct {int i; int n} s;
1132 * SetValue(device, "s", [1, 2], 8) => s = {1, 0};
1134 else if ((is_pointer || inclass == D3DXPC_MATRIX_ROWS) && desc->RegisterSet != D3DXRS_BOOL)
1136 last = set(table, device, &constant->constants[0], indata, intype, size, incol, inclass,
1137 index + last, is_pointer);
1139 for (i = 1; i < desc->StructMembers; ++i)
1141 set(table, device, &constant->constants[i], NULL, intype, size, incol, inclass, 0, is_pointer);
1144 else
1146 for (i = 0; i < desc->StructMembers; ++i)
1148 last += set(table, device, &constant->constants[i], indata, intype, size, incol, D3DXPC_SCALAR,
1149 index + last, is_pointer);
1154 return last;
1157 /* elements */
1158 if (desc->Elements > 1)
1160 for (i = 0; i < desc->Elements && *size > 0; ++i)
1162 last += set(table, device, &constant->constants[i], indata, intype, size, incol, inclass,
1163 index + last, is_pointer);
1165 /* adjust the vector size for matrix rows */
1166 if (inclass == D3DXPC_MATRIX_ROWS && desc->Class == D3DXPC_VECTOR && (i % 4) == 3)
1168 last += 12;
1169 *size = *size < 12 ? 0 : *size - 12;
1173 return last;
1176 switch (desc->Class)
1178 case D3DXPC_SCALAR:
1179 case D3DXPC_VECTOR:
1180 case D3DXPC_MATRIX_ROWS:
1181 regcount = min(desc->RegisterCount, desc->Rows);
1182 if (inclass == D3DXPC_MATRIX_ROWS) cin = incol;
1183 else rin = incol;
1184 regsize = desc->Columns;
1185 break;
1187 case D3DXPC_MATRIX_COLUMNS:
1188 regcount = min(desc->RegisterCount, desc->Columns);
1189 if (inclass == D3DXPC_MATRIX_ROWS) rin = incol;
1190 else cin = incol;
1191 regsize = desc->Rows;
1192 break;
1194 default:
1195 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1196 return 0;
1199 /* specific stuff for different in types */
1200 switch (inclass)
1202 case D3DXPC_SCALAR:
1203 ret = desc->Columns * desc->Rows;
1204 *size -= desc->Columns * desc->Rows;
1205 break;
1207 case D3DXPC_VECTOR:
1208 switch (desc->Class)
1210 case D3DXPC_MATRIX_ROWS:
1211 if (*size < regcount * 4)
1213 *size = 0;
1214 return 0;
1216 ret = 4 * regcount;
1217 *size -= 4 * regcount;
1218 break;
1220 case D3DXPC_MATRIX_COLUMNS:
1221 ret = 4 * regsize;
1222 *size -= 4 * regcount;
1223 break;
1225 case D3DXPC_SCALAR:
1226 ret = 1;
1227 *size -= ret;
1228 break;
1230 case D3DXPC_VECTOR:
1231 ret = 4;
1232 *size -= ret;
1233 break;
1235 default:
1236 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1237 return 0;
1239 break;
1241 case D3DXPC_MATRIX_ROWS:
1242 switch (desc->Class)
1244 case D3DXPC_MATRIX_ROWS:
1245 case D3DXPC_MATRIX_COLUMNS:
1246 if (*size < 16)
1248 *size = 0;
1249 return 0;
1251 ret = 16;
1252 break;
1254 case D3DXPC_SCALAR:
1255 ret = 4;
1256 break;
1258 case D3DXPC_VECTOR:
1259 ret = 1;
1260 break;
1262 default:
1263 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1264 return 0;
1266 *size -= ret;
1267 break;
1269 case D3DXPC_MATRIX_COLUMNS:
1270 switch (desc->Class)
1272 case D3DXPC_MATRIX_ROWS:
1273 case D3DXPC_MATRIX_COLUMNS:
1274 if (*size < 16)
1276 *size = 0;
1277 return 0;
1279 ret = 16;
1280 break;
1282 case D3DXPC_SCALAR:
1283 ret = 1;
1284 break;
1286 case D3DXPC_VECTOR:
1287 ret = 4;
1288 break;
1290 default:
1291 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc->Class));
1292 return 0;
1294 *size -= ret;
1295 break;
1297 default:
1298 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(inclass));
1299 return 0;
1302 /* set the registers */
1303 switch (desc->RegisterSet)
1305 case D3DXRS_BOOL:
1306 regcount = min(desc->RegisterCount, desc->Columns * desc->Rows);
1307 l = 0;
1308 for (i = 0; i < regcount; ++i)
1310 BOOL out;
1311 DWORD t = get_index(indata, index + i / regsize * rin + l * cin, is_pointer);
1313 set_number(&tmp, desc->Type, &t, intype);
1314 set_number(&out, D3DXPT_BOOL, &tmp, desc->Type);
1315 if (is_vertex_shader(table->desc.Version))
1316 IDirect3DDevice9_SetVertexShaderConstantB(device, desc->RegisterIndex + i, &out, 1);
1317 else
1318 IDirect3DDevice9_SetPixelShaderConstantB(device, desc->RegisterIndex + i, &out, 1);
1320 if (++l >= regsize) l = 0;
1322 return ret;
1324 case D3DXRS_INT4:
1325 for (i = 0; i < regcount; ++i)
1327 INT vec[4] = {0, 0, 1, 0};
1329 for (l = 0; l < regsize; ++l)
1331 DWORD t = get_index(indata, index + i * rin + l * cin, is_pointer);
1333 set_number(&tmp, desc->Type, &t, intype);
1334 set_number(&vec[l], D3DXPT_INT, &tmp, desc->Type);
1336 if (is_vertex_shader(table->desc.Version))
1337 IDirect3DDevice9_SetVertexShaderConstantI(device, desc->RegisterIndex + i, vec, 1);
1338 else
1339 IDirect3DDevice9_SetPixelShaderConstantI(device, desc->RegisterIndex + i, vec, 1);
1341 return ret;
1343 case D3DXRS_FLOAT4:
1344 for (i = 0; i < regcount; ++i)
1346 FLOAT vec[4] = {0};
1348 for (l = 0; l < regsize; ++l)
1350 DWORD t = get_index(indata, index + i * rin + l * cin, is_pointer);
1352 set_number(&tmp, desc->Type, &t, intype);
1353 set_number(&vec[l], D3DXPT_FLOAT, &tmp, desc->Type);
1355 if (is_vertex_shader(table->desc.Version))
1356 IDirect3DDevice9_SetVertexShaderConstantF(device, desc->RegisterIndex + i, vec, 1);
1357 else
1358 IDirect3DDevice9_SetPixelShaderConstantF(device, desc->RegisterIndex + i, vec, 1);
1360 return ret;
1362 default:
1363 FIXME("Unhandled register set %s\n", debug_d3dxparameter_registerset(desc->RegisterSet));
1364 return 0;
1368 static HRESULT set_scalar(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1369 const void *indata, D3DXPARAMETER_TYPE intype)
1371 struct ctab_constant *c = get_valid_constant(table, constant);
1372 UINT count = 1;
1374 if (!c)
1376 WARN("Invalid argument specified\n");
1377 return D3DERR_INVALIDCALL;
1380 switch (c->desc.Class)
1382 case D3DXPC_SCALAR:
1383 set(table, device, c, &indata, intype, &count, c->desc.Columns, D3DXPC_SCALAR, 0, FALSE);
1384 return D3D_OK;
1386 case D3DXPC_VECTOR:
1387 case D3DXPC_MATRIX_ROWS:
1388 case D3DXPC_MATRIX_COLUMNS:
1389 case D3DXPC_STRUCT:
1390 return D3D_OK;
1392 default:
1393 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1394 return D3DERR_INVALIDCALL;
1398 static HRESULT set_scalar_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1399 const void *indata, UINT count, D3DXPARAMETER_TYPE intype)
1401 struct ctab_constant *c = get_valid_constant(table, constant);
1403 if (!c)
1405 WARN("Invalid argument specified\n");
1406 return D3DERR_INVALIDCALL;
1409 switch (c->desc.Class)
1411 case D3DXPC_SCALAR:
1412 case D3DXPC_VECTOR:
1413 case D3DXPC_MATRIX_ROWS:
1414 case D3DXPC_MATRIX_COLUMNS:
1415 case D3DXPC_STRUCT:
1416 set(table, device, c, &indata, intype, &count, c->desc.Columns, D3DXPC_SCALAR, 0, FALSE);
1417 return D3D_OK;
1419 default:
1420 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1421 return D3DERR_INVALIDCALL;
1425 static HRESULT set_vector(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1426 const void *indata, D3DXPARAMETER_TYPE intype)
1428 struct ctab_constant *c = get_valid_constant(table, constant);
1429 UINT count = 4;
1431 if (!c)
1433 WARN("Invalid argument specified\n");
1434 return D3DERR_INVALIDCALL;
1437 switch (c->desc.Class)
1439 case D3DXPC_SCALAR:
1440 case D3DXPC_VECTOR:
1441 case D3DXPC_STRUCT:
1442 set(table, device, c, &indata, intype, &count, 4, D3DXPC_VECTOR, 0, FALSE);
1443 return D3D_OK;
1445 case D3DXPC_MATRIX_ROWS:
1446 case D3DXPC_MATRIX_COLUMNS:
1447 return D3D_OK;
1449 default:
1450 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1451 return D3DERR_INVALIDCALL;
1455 static HRESULT set_vector_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1456 const void *indata, UINT count, D3DXPARAMETER_TYPE intype)
1458 struct ctab_constant *c = get_valid_constant(table, constant);
1460 if (!c)
1462 WARN("Invalid argument specified\n");
1463 return D3DERR_INVALIDCALL;
1466 switch (c->desc.Class)
1468 case D3DXPC_SCALAR:
1469 case D3DXPC_VECTOR:
1470 case D3DXPC_MATRIX_ROWS:
1471 case D3DXPC_MATRIX_COLUMNS:
1472 case D3DXPC_STRUCT:
1473 count *= 4;
1474 set(table, device, c, &indata, intype, &count, 4, D3DXPC_VECTOR, 0, FALSE);
1475 return D3D_OK;
1477 default:
1478 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1479 return D3DERR_INVALIDCALL;
1483 static HRESULT set_matrix_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device, D3DXHANDLE constant,
1484 const void *indata, UINT count, BOOL transpose)
1486 struct ctab_constant *c = get_valid_constant(table, constant);
1488 if (!c)
1490 WARN("Invalid argument specified\n");
1491 return D3DERR_INVALIDCALL;
1494 switch (c->desc.Class)
1496 case D3DXPC_SCALAR:
1497 case D3DXPC_VECTOR:
1498 case D3DXPC_MATRIX_ROWS:
1499 case D3DXPC_MATRIX_COLUMNS:
1500 case D3DXPC_STRUCT:
1501 count *= 16;
1502 set(table, device, c, &indata, D3DXPT_FLOAT, &count, 4,
1503 transpose ? D3DXPC_MATRIX_ROWS : D3DXPC_MATRIX_COLUMNS, 0, FALSE);
1504 return D3D_OK;
1506 default:
1507 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1508 return D3DERR_INVALIDCALL;
1512 static HRESULT set_matrix_pointer_array(struct ID3DXConstantTableImpl *table, IDirect3DDevice9 *device,
1513 D3DXHANDLE constant, const void **indata, UINT count, BOOL transpose)
1515 struct ctab_constant *c = get_valid_constant(table, constant);
1517 if (!c)
1519 WARN("Invalid argument specified\n");
1520 return D3DERR_INVALIDCALL;
1523 switch (c->desc.Class)
1525 case D3DXPC_SCALAR:
1526 case D3DXPC_VECTOR:
1527 case D3DXPC_MATRIX_ROWS:
1528 case D3DXPC_MATRIX_COLUMNS:
1529 case D3DXPC_STRUCT:
1530 count *= 16;
1531 set(table, device, c, indata, D3DXPT_FLOAT, &count, 4,
1532 transpose ? D3DXPC_MATRIX_ROWS : D3DXPC_MATRIX_COLUMNS, 0, TRUE);
1533 return D3D_OK;
1535 default:
1536 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(c->desc.Class));
1537 return D3DERR_INVALIDCALL;
1541 static HRESULT WINAPI ID3DXConstantTableImpl_SetDefaults(struct ID3DXConstantTable *iface,
1542 struct IDirect3DDevice9 *device)
1544 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1545 UINT i;
1547 TRACE("iface %p, device %p\n", iface, device);
1549 if (!device)
1551 WARN("Invalid argument specified\n");
1552 return D3DERR_INVALIDCALL;
1555 for (i = 0; i < This->desc.Constants; i++)
1557 D3DXCONSTANT_DESC *desc = &This->constants[i].desc;
1558 HRESULT hr;
1560 if (!desc->DefaultValue)
1561 continue;
1563 switch (desc->RegisterSet)
1565 case D3DXRS_BOOL:
1566 if (is_vertex_shader(This->desc.Version))
1567 hr = IDirect3DDevice9_SetVertexShaderConstantB(device, desc->RegisterIndex, desc->DefaultValue,
1568 desc->RegisterCount);
1569 else
1570 hr = IDirect3DDevice9_SetPixelShaderConstantB(device, desc->RegisterIndex, desc->DefaultValue,
1571 desc->RegisterCount);
1572 break;
1574 case D3DXRS_INT4:
1575 if (is_vertex_shader(This->desc.Version))
1576 hr = IDirect3DDevice9_SetVertexShaderConstantI(device, desc->RegisterIndex, desc->DefaultValue,
1577 desc->RegisterCount);
1578 else
1579 hr = IDirect3DDevice9_SetPixelShaderConstantI(device, desc->RegisterIndex, desc->DefaultValue,
1580 desc->RegisterCount);
1581 break;
1583 case D3DXRS_FLOAT4:
1584 if (is_vertex_shader(This->desc.Version))
1585 hr = IDirect3DDevice9_SetVertexShaderConstantF(device, desc->RegisterIndex, desc->DefaultValue,
1586 desc->RegisterCount);
1587 else
1588 hr = IDirect3DDevice9_SetPixelShaderConstantF(device, desc->RegisterIndex, desc->DefaultValue,
1589 desc->RegisterCount);
1590 break;
1592 default:
1593 FIXME("Unhandled register set %s\n", debug_d3dxparameter_registerset(desc->RegisterSet));
1594 hr = E_NOTIMPL;
1595 break;
1598 if (hr != D3D_OK)
1599 return hr;
1602 return D3D_OK;
1605 static HRESULT WINAPI ID3DXConstantTableImpl_SetValue(struct ID3DXConstantTable *iface,
1606 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const void *data, unsigned int bytes)
1608 struct ID3DXConstantTableImpl *table = impl_from_ID3DXConstantTable(iface);
1609 struct ctab_constant *c = get_valid_constant(table, constant);
1610 D3DXCONSTANT_DESC *desc;
1612 TRACE("iface %p, device %p, constant %p, data %p, bytes %u\n", iface, device, constant, data, bytes);
1614 if (!device || !c || !data)
1616 WARN("Invalid argument specified\n");
1617 return D3DERR_INVALIDCALL;
1620 desc = &c->desc;
1622 switch (desc->Class)
1624 case D3DXPC_SCALAR:
1625 case D3DXPC_VECTOR:
1626 case D3DXPC_MATRIX_ROWS:
1627 case D3DXPC_MATRIX_COLUMNS:
1628 case D3DXPC_STRUCT:
1629 bytes /= 4;
1630 set(table, device, c, &data, desc->Type, &bytes, desc->Columns, D3DXPC_SCALAR, 0, FALSE);
1631 return D3D_OK;
1633 default:
1634 FIXME("Unhandled parameter class %s\n", debug_d3dxparameter_class(desc->Class));
1635 return D3DERR_INVALIDCALL;
1639 static HRESULT WINAPI ID3DXConstantTableImpl_SetBool(struct ID3DXConstantTable *iface,
1640 struct IDirect3DDevice9 *device, D3DXHANDLE constant, BOOL b)
1642 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1644 TRACE("iface %p, device %p, constant %p, b %d\n", iface, device, constant, b);
1646 return set_scalar(This, device, constant, &b, D3DXPT_BOOL);
1649 static HRESULT WINAPI ID3DXConstantTableImpl_SetBoolArray(struct ID3DXConstantTable *iface,
1650 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const BOOL *b, UINT count)
1652 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1654 TRACE("iface %p, device %p, constant %p, b %p, count %d\n", iface, device, constant, b, count);
1656 return set_scalar_array(This, device, constant, b, count, D3DXPT_BOOL);
1659 static HRESULT WINAPI ID3DXConstantTableImpl_SetInt(struct ID3DXConstantTable *iface,
1660 struct IDirect3DDevice9 *device, D3DXHANDLE constant, INT n)
1662 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1664 TRACE("iface %p, device %p, constant %p, n %d\n", iface, device, constant, n);
1666 return set_scalar(This, device, constant, &n, D3DXPT_INT);
1669 static HRESULT WINAPI ID3DXConstantTableImpl_SetIntArray(struct ID3DXConstantTable *iface,
1670 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const INT *n, UINT count)
1672 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1674 TRACE("iface %p, device %p, constant %p, n %p, count %d\n", iface, device, constant, n, count);
1676 return set_scalar_array(This, device, constant, n, count, D3DXPT_INT);
1679 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloat(struct ID3DXConstantTable *iface,
1680 struct IDirect3DDevice9 *device, D3DXHANDLE constant, float f)
1682 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1684 TRACE("iface %p, device %p, constant %p, f %f\n", iface, device, constant, f);
1686 return set_scalar(This, device, constant, &f, D3DXPT_FLOAT);
1689 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloatArray(struct ID3DXConstantTable *iface,
1690 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const float *f, UINT count)
1692 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1694 TRACE("iface %p, device %p, constant %p, f %p, count %d\n", iface, device, constant, f, count);
1696 return set_scalar_array(This, device, constant, f, count, D3DXPT_FLOAT);
1699 static HRESULT WINAPI ID3DXConstantTableImpl_SetVector(struct ID3DXConstantTable *iface,
1700 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXVECTOR4 *vector)
1702 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1704 TRACE("iface %p, device %p, constant %p, vector %p\n", iface, device, constant, vector);
1706 return set_vector(This, device, constant, vector, D3DXPT_FLOAT);
1709 static HRESULT WINAPI ID3DXConstantTableImpl_SetVectorArray(struct ID3DXConstantTable *iface,
1710 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXVECTOR4 *vector, UINT count)
1712 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1714 TRACE("iface %p, device %p, constant %p, vector %p, count %u\n", iface, device, constant, vector, count);
1716 return set_vector_array(This, device, constant, vector, count, D3DXPT_FLOAT);
1719 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrix(struct ID3DXConstantTable *iface,
1720 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix)
1722 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1724 TRACE("iface %p, device %p, constant %p, matrix %p\n", iface, device, constant, matrix);
1726 return set_matrix_array(This, device, constant, matrix, 1, FALSE);
1729 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixArray(struct ID3DXConstantTable *iface,
1730 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
1732 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1734 TRACE("iface %p, device %p, constant %p, matrix %p, count %u\n", iface, device, constant, matrix, count);
1736 return set_matrix_array(This, device, constant, matrix, count, FALSE);
1739 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixPointerArray(struct ID3DXConstantTable *iface,
1740 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
1742 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1744 TRACE("iface %p, device %p, constant %p, matrix %p, count %u)\n", iface, device, constant, matrix, count);
1746 return set_matrix_pointer_array(This, device, constant, (const void **)matrix, count, FALSE);
1749 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTranspose(struct ID3DXConstantTable *iface,
1750 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix)
1752 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1754 TRACE("iface %p, device %p, constant %p, matrix %p\n", iface, device, constant, matrix);
1756 return set_matrix_array(This, device, constant, matrix, 1, TRUE);
1759 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposeArray(struct ID3DXConstantTable *iface,
1760 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
1762 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1764 TRACE("iface %p, device %p, constant %p, matrix %p, count %u\n", iface, device, constant, matrix, count);
1766 return set_matrix_array(This, device, constant, matrix, count, TRUE);
1769 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposePointerArray(struct ID3DXConstantTable *iface,
1770 struct IDirect3DDevice9 *device, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
1772 struct ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1774 TRACE("iface %p, device %p, constant %p, matrix %p, count %u)\n", iface, device, constant, matrix, count);
1776 return set_matrix_pointer_array(This, device, constant, (const void **)matrix, count, TRUE);
1779 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl =
1781 /*** IUnknown methods ***/
1782 ID3DXConstantTableImpl_QueryInterface,
1783 ID3DXConstantTableImpl_AddRef,
1784 ID3DXConstantTableImpl_Release,
1785 /*** ID3DXBuffer methods ***/
1786 ID3DXConstantTableImpl_GetBufferPointer,
1787 ID3DXConstantTableImpl_GetBufferSize,
1788 /*** ID3DXConstantTable methods ***/
1789 ID3DXConstantTableImpl_GetDesc,
1790 ID3DXConstantTableImpl_GetConstantDesc,
1791 ID3DXConstantTableImpl_GetSamplerIndex,
1792 ID3DXConstantTableImpl_GetConstant,
1793 ID3DXConstantTableImpl_GetConstantByName,
1794 ID3DXConstantTableImpl_GetConstantElement,
1795 ID3DXConstantTableImpl_SetDefaults,
1796 ID3DXConstantTableImpl_SetValue,
1797 ID3DXConstantTableImpl_SetBool,
1798 ID3DXConstantTableImpl_SetBoolArray,
1799 ID3DXConstantTableImpl_SetInt,
1800 ID3DXConstantTableImpl_SetIntArray,
1801 ID3DXConstantTableImpl_SetFloat,
1802 ID3DXConstantTableImpl_SetFloatArray,
1803 ID3DXConstantTableImpl_SetVector,
1804 ID3DXConstantTableImpl_SetVectorArray,
1805 ID3DXConstantTableImpl_SetMatrix,
1806 ID3DXConstantTableImpl_SetMatrixArray,
1807 ID3DXConstantTableImpl_SetMatrixPointerArray,
1808 ID3DXConstantTableImpl_SetMatrixTranspose,
1809 ID3DXConstantTableImpl_SetMatrixTransposeArray,
1810 ID3DXConstantTableImpl_SetMatrixTransposePointerArray
1813 static HRESULT parse_ctab_constant_type(const char *ctab, DWORD typeoffset, struct ctab_constant *constant,
1814 BOOL is_element, WORD index, WORD max_index, DWORD *offset, DWORD nameoffset, UINT regset)
1816 const D3DXSHADER_TYPEINFO *type = (LPD3DXSHADER_TYPEINFO)(ctab + typeoffset);
1817 const D3DXSHADER_STRUCTMEMBERINFO *memberinfo = NULL;
1818 HRESULT hr = D3D_OK;
1819 UINT i, count = 0;
1820 WORD size = 0;
1822 constant->desc.DefaultValue = offset ? ctab + *offset : NULL;
1823 constant->desc.Class = type->Class;
1824 constant->desc.Type = type->Type;
1825 constant->desc.Rows = type->Rows;
1826 constant->desc.Columns = type->Columns;
1827 constant->desc.Elements = is_element ? 1 : type->Elements;
1828 constant->desc.StructMembers = type->StructMembers;
1829 constant->desc.Name = ctab + nameoffset;
1830 constant->desc.RegisterSet = regset;
1831 constant->desc.RegisterIndex = index;
1833 TRACE("name %s, elements %u, index %u, defaultvalue %p, regset %s\n", constant->desc.Name,
1834 constant->desc.Elements, index, constant->desc.DefaultValue,
1835 debug_d3dxparameter_registerset(regset));
1836 TRACE("class %s, type %s, rows %d, columns %d, elements %d, struct_members %d\n",
1837 debug_d3dxparameter_class(type->Class), debug_d3dxparameter_type(type->Type),
1838 type->Rows, type->Columns, type->Elements, type->StructMembers);
1840 if (type->Elements > 1 && !is_element)
1842 count = type->Elements;
1844 else if ((type->Class == D3DXPC_STRUCT) && type->StructMembers)
1846 memberinfo = (D3DXSHADER_STRUCTMEMBERINFO*)(ctab + type->StructMemberInfo);
1847 count = type->StructMembers;
1850 if (count)
1852 constant->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*constant->constants) * count);
1853 if (!constant->constants)
1855 ERR("Out of memory\n");
1856 hr = E_OUTOFMEMORY;
1857 goto error;
1860 for (i = 0; i < count; ++i)
1862 hr = parse_ctab_constant_type(ctab, memberinfo ? memberinfo[i].TypeInfo : typeoffset,
1863 &constant->constants[i], memberinfo == NULL, index + size, max_index, offset,
1864 memberinfo ? memberinfo[i].Name : nameoffset, regset);
1865 if (hr != D3D_OK)
1866 goto error;
1868 size += constant->constants[i].desc.RegisterCount;
1871 else
1873 WORD offsetdiff = type->Columns * type->Rows;
1874 BOOL fail = FALSE;
1876 size = type->Columns * type->Rows;
1878 switch (regset)
1880 case D3DXRS_BOOL:
1881 fail = type->Class != D3DXPC_SCALAR && type->Class != D3DXPC_VECTOR
1882 && type->Class != D3DXPC_MATRIX_ROWS && type->Class != D3DXPC_MATRIX_COLUMNS;
1883 break;
1885 case D3DXRS_FLOAT4:
1886 case D3DXRS_INT4:
1887 switch (type->Class)
1889 case D3DXPC_VECTOR:
1890 size = 1;
1891 /* fall through */
1892 case D3DXPC_SCALAR:
1893 offsetdiff = type->Rows * 4;
1894 break;
1896 case D3DXPC_MATRIX_ROWS:
1897 offsetdiff = type->Rows * 4;
1898 size = type->Rows;
1899 break;
1901 case D3DXPC_MATRIX_COLUMNS:
1902 offsetdiff = type->Columns * 4;
1903 size = type->Columns;
1904 break;
1906 default:
1907 fail = TRUE;
1908 break;
1910 break;
1912 case D3DXRS_SAMPLER:
1913 size = 1;
1914 fail = type->Class != D3DXPC_OBJECT;
1915 break;
1917 default:
1918 fail = TRUE;
1919 break;
1922 if (fail)
1924 FIXME("Unhandled register set %s, type class %s\n", debug_d3dxparameter_registerset(regset),
1925 debug_d3dxparameter_class(type->Class));
1928 /* offset in bytes => offsetdiff * sizeof(DWORD) */
1929 if (offset) *offset += offsetdiff * 4;
1932 constant->desc.RegisterCount = max(0, min(max_index - index, size));
1933 constant->desc.Bytes = 4 * constant->desc.Elements * type->Rows * type->Columns;
1935 return D3D_OK;
1937 error:
1938 if (constant->constants)
1940 for (i = 0; i < count; ++i)
1942 free_constant(&constant->constants[i]);
1944 HeapFree(GetProcessHeap(), 0, constant->constants);
1945 constant->constants = NULL;
1948 return hr;
1951 HRESULT WINAPI D3DXGetShaderConstantTableEx(const DWORD *byte_code, DWORD flags, ID3DXConstantTable **constant_table)
1953 struct ID3DXConstantTableImpl *object = NULL;
1954 const void *data;
1955 HRESULT hr;
1956 UINT size;
1957 const D3DXSHADER_CONSTANTTABLE *ctab_header;
1958 const D3DXSHADER_CONSTANTINFO *constant_info;
1959 DWORD i;
1961 TRACE("byte_code %p, flags %x, constant_table %p\n", byte_code, flags, constant_table);
1963 if (constant_table) *constant_table = NULL;
1965 if (!byte_code || !constant_table)
1967 WARN("Invalid argument specified.\n");
1968 return D3DERR_INVALIDCALL;
1971 if (!is_valid_bytecode(*byte_code))
1973 WARN("Invalid byte_code specified.\n");
1974 return D3D_OK;
1977 if (flags) FIXME("Flags (%#x) are not handled, yet!\n", flags);
1979 hr = D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), &data, &size);
1980 if (hr != D3D_OK)
1982 WARN("CTAB not found.\n");
1983 return D3DXERR_INVALIDDATA;
1986 if (size < sizeof(*ctab_header))
1988 WARN("Invalid CTAB size.\n");
1989 return D3DXERR_INVALIDDATA;
1992 ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
1993 if (ctab_header->Size != sizeof(*ctab_header))
1995 WARN("Invalid D3DXSHADER_CONSTANTTABLE size.\n");
1996 return D3DXERR_INVALIDDATA;
1999 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2000 if (!object)
2001 return E_OUTOFMEMORY;
2003 object->ID3DXConstantTable_iface.lpVtbl = &ID3DXConstantTable_Vtbl;
2004 object->ref = 1;
2006 object->ctab = HeapAlloc(GetProcessHeap(), 0, size);
2007 if (!object->ctab)
2009 ERR("Out of memory\n");
2010 HeapFree(GetProcessHeap(), 0, object);
2011 return E_OUTOFMEMORY;
2013 object->size = size;
2014 memcpy(object->ctab, data, object->size);
2016 object->desc.Creator = ctab_header->Creator ? object->ctab + ctab_header->Creator : NULL;
2017 object->desc.Version = ctab_header->Version;
2018 object->desc.Constants = ctab_header->Constants;
2019 TRACE("Creator %s, Version %x, Constants %u, Target %s\n",
2020 debugstr_a(object->desc.Creator), object->desc.Version, object->desc.Constants,
2021 debugstr_a(ctab_header->Target ? object->ctab + ctab_header->Target : NULL));
2023 object->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2024 sizeof(*object->constants) * object->desc.Constants);
2025 if (!object->constants)
2027 ERR("Out of memory\n");
2028 hr = E_OUTOFMEMORY;
2029 goto error;
2032 constant_info = (const D3DXSHADER_CONSTANTINFO *)(object->ctab + ctab_header->ConstantInfo);
2033 for (i = 0; i < ctab_header->Constants; i++)
2035 DWORD offset = constant_info[i].DefaultValue;
2037 hr = parse_ctab_constant_type(object->ctab, constant_info[i].TypeInfo,
2038 &object->constants[i], FALSE, constant_info[i].RegisterIndex,
2039 constant_info[i].RegisterIndex + constant_info[i].RegisterCount,
2040 offset ? &offset : NULL, constant_info[i].Name, constant_info[i].RegisterSet);
2041 if (hr != D3D_OK)
2042 goto error;
2045 * Set the register count, it may differ for D3DXRS_INT4, because somehow
2046 * it makes the assumption that the register size is 1 instead of 4, so the
2047 * count is 4 times bigger. This holds true only for toplevel shader
2048 * constants. The count of elements and members is always based on a
2049 * register size of 4.
2051 if (object->constants[i].desc.RegisterSet == D3DXRS_INT4)
2053 object->constants[i].desc.RegisterCount = constant_info[i].RegisterCount;
2057 *constant_table = &object->ID3DXConstantTable_iface;
2059 return D3D_OK;
2061 error:
2062 free_constant_table(object);
2063 HeapFree(GetProcessHeap(), 0, object);
2065 return hr;
2068 HRESULT WINAPI D3DXGetShaderConstantTable(const DWORD *byte_code, ID3DXConstantTable **constant_table)
2070 TRACE("(%p, %p): Forwarded to D3DXGetShaderConstantTableEx\n", byte_code, constant_table);
2072 return D3DXGetShaderConstantTableEx(byte_code, 0, constant_table);
2075 HRESULT WINAPI D3DXCreateFragmentLinker(IDirect3DDevice9 *device, UINT size, ID3DXFragmentLinker **linker)
2077 FIXME("device %p, size %u, linker %p: stub.\n", device, size, linker);
2079 if (linker)
2080 *linker = NULL;
2083 return E_NOTIMPL;
2086 HRESULT WINAPI D3DXCreateFragmentLinkerEx(IDirect3DDevice9 *device, UINT size, DWORD flags, ID3DXFragmentLinker **linker)
2088 FIXME("device %p, size %u, flags %#x, linker %p: stub.\n", device, size, flags, linker);
2090 if (linker)
2091 *linker = NULL;
2093 return E_NOTIMPL;
2096 HRESULT WINAPI D3DXGetShaderSamplers(const DWORD *byte_code, const char **samplers, UINT *count)
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;
2104 TRACE("byte_code %p, samplers %p, count %p\n", byte_code, samplers, count);
2106 if (count) *count = 0;
2108 if (D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), (const void **)&data, &size) != D3D_OK)
2109 return D3D_OK;
2111 if (size < sizeof(*ctab_header)) return D3D_OK;
2113 ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
2114 if (ctab_header->Size != sizeof(*ctab_header)) return D3D_OK;
2116 constant_info = (const D3DXSHADER_CONSTANTINFO *)(data + ctab_header->ConstantInfo);
2117 for (i = 0; i < ctab_header->Constants; i++)
2119 const D3DXSHADER_TYPEINFO *type;
2121 TRACE("name = %s\n", data + constant_info[i].Name);
2123 type = (const D3DXSHADER_TYPEINFO *)(data + constant_info[i].TypeInfo);
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)
2131 if (samplers) samplers[sampler_count] = data + constant_info[i].Name;
2133 ++sampler_count;
2137 TRACE("Found %u samplers\n", sampler_count);
2139 if (count) *count = sampler_count;
2141 return D3D_OK;
2144 HRESULT WINAPI D3DXDisassembleShader(const DWORD *shader, BOOL colorcode, const char *comments, ID3DXBuffer **disassembly)
2146 FIXME("%p %d %s %p: stub\n", shader, colorcode, debugstr_a(comments), disassembly);
2147 return E_OUTOFMEMORY;
2150 struct d3dx9_texture_shader
2152 ID3DXTextureShader ID3DXTextureShader_iface;
2153 LONG ref;
2156 static inline struct d3dx9_texture_shader *impl_from_ID3DXTextureShader(ID3DXTextureShader *iface)
2158 return CONTAINING_RECORD(iface, struct d3dx9_texture_shader, ID3DXTextureShader_iface);
2161 static HRESULT WINAPI d3dx9_texture_shader_QueryInterface(ID3DXTextureShader *iface, REFIID riid, void **out)
2163 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
2165 if (IsEqualGUID(riid, &IID_IUnknown) ||
2166 IsEqualGUID(riid, &IID_ID3DXTextureShader))
2168 iface->lpVtbl->AddRef(iface);
2169 *out = iface;
2170 return D3D_OK;
2173 WARN("Interface %s not found.\n", debugstr_guid(riid));
2174 *out = NULL;
2175 return E_NOINTERFACE;
2178 static ULONG WINAPI d3dx9_texture_shader_AddRef(ID3DXTextureShader *iface)
2180 struct d3dx9_texture_shader *texture_shader = impl_from_ID3DXTextureShader(iface);
2181 ULONG refcount = InterlockedIncrement(&texture_shader->ref);
2183 TRACE("%p increasing refcount to %u.\n", texture_shader, refcount);
2185 return refcount;
2188 static ULONG WINAPI d3dx9_texture_shader_Release(ID3DXTextureShader *iface)
2190 struct d3dx9_texture_shader *texture_shader = impl_from_ID3DXTextureShader(iface);
2191 ULONG refcount = InterlockedDecrement(&texture_shader->ref);
2193 TRACE("%p decreasing refcount to %u.\n", texture_shader, refcount);
2195 if (!refcount)
2197 HeapFree(GetProcessHeap(), 0, texture_shader);
2200 return refcount;
2203 static HRESULT WINAPI d3dx9_texture_shader_GetFunction(ID3DXTextureShader *iface, struct ID3DXBuffer **function)
2205 FIXME("iface %p, function %p stub.\n", iface, function);
2207 return E_NOTIMPL;
2210 static HRESULT WINAPI d3dx9_texture_shader_GetConstantBuffer(ID3DXTextureShader *iface, struct ID3DXBuffer **constant_buffer)
2212 FIXME("iface %p, constant_buffer %p stub.\n", iface, constant_buffer);
2214 return E_NOTIMPL;
2217 static HRESULT WINAPI d3dx9_texture_shader_GetDesc(ID3DXTextureShader *iface, D3DXCONSTANTTABLE_DESC *desc)
2219 FIXME("iface %p, desc %p stub.\n", iface, desc);
2221 return E_NOTIMPL;
2224 static HRESULT WINAPI d3dx9_texture_shader_GetConstantDesc(ID3DXTextureShader *iface, D3DXHANDLE constant, D3DXCONSTANT_DESC *constant_desc, UINT *count)
2226 FIXME("iface %p, constant %p, constant_desc %p, count %p stub.\n", iface, constant, constant_desc, count);
2228 return E_NOTIMPL;
2231 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstant(ID3DXTextureShader *iface, D3DXHANDLE constant, UINT index)
2233 FIXME("iface %p, constant %p, index %u stub.\n", iface, constant, index);
2235 return NULL;
2238 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstantByName(ID3DXTextureShader *iface, D3DXHANDLE constant, const char *name)
2240 FIXME("iface %p, constant %p, name %s stub.\n", iface, constant, debugstr_a(name));
2242 return NULL;
2245 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstantElement(ID3DXTextureShader *iface, D3DXHANDLE constant, UINT index)
2247 FIXME("iface %p, constant %p, index %u stub.\n", iface, constant, index);
2249 return NULL;
2252 static HRESULT WINAPI d3dx9_texture_shader_SetDefaults(ID3DXTextureShader *iface)
2254 FIXME("iface %p stub.\n", iface);
2256 return E_NOTIMPL;
2259 static HRESULT WINAPI d3dx9_texture_shader_SetValue(ID3DXTextureShader *iface, D3DXHANDLE constant, const void *data, UINT bytes)
2261 FIXME("iface %p, constant %p, data %p, bytes %u stub.\n", iface, constant, data, bytes);
2263 return E_NOTIMPL;
2266 static HRESULT WINAPI d3dx9_texture_shader_SetBool(ID3DXTextureShader *iface, D3DXHANDLE constant, BOOL b)
2268 FIXME("iface %p, constant %p, b %u stub.\n", iface, constant, b);
2270 return E_NOTIMPL;
2273 static HRESULT WINAPI d3dx9_texture_shader_SetBoolArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const BOOL *b, UINT count)
2275 FIXME("iface %p, constant %p, b %p, count %u stub.\n", iface, constant, b, count);
2277 return E_NOTIMPL;
2280 static HRESULT WINAPI d3dx9_texture_shader_SetInt(ID3DXTextureShader *iface, D3DXHANDLE constant, INT n)
2282 FIXME("iface %p, constant %p, n %d stub.\n", iface, constant, n);
2284 return E_NOTIMPL;
2287 static HRESULT WINAPI d3dx9_texture_shader_SetIntArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const INT *n, UINT count)
2289 FIXME("iface %p, constant %p, n %p, count %u stub.\n", iface, constant, n, count);
2291 return E_NOTIMPL;
2294 static HRESULT WINAPI d3dx9_texture_shader_SetFloat(ID3DXTextureShader *iface, D3DXHANDLE constant, FLOAT f)
2296 FIXME("iface %p, constant %p, f %f stub.\n", iface, constant, f);
2298 return E_NOTIMPL;
2301 static HRESULT WINAPI d3dx9_texture_shader_SetFloatArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const FLOAT *f, UINT count)
2303 FIXME("iface %p, constant %p, f %p, count %u stub.\n", iface, constant, f, count);
2305 return E_NOTIMPL;
2308 static HRESULT WINAPI d3dx9_texture_shader_SetVector(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXVECTOR4 *vector)
2310 FIXME("iface %p, constant %p, vector %p stub.\n", iface, constant, vector);
2312 return E_NOTIMPL;
2315 static HRESULT WINAPI d3dx9_texture_shader_SetVectorArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXVECTOR4 *vector, UINT count)
2317 FIXME("iface %p, constant %p, vector %p, count %u stub.\n", iface, constant, vector, count);
2319 return E_NOTIMPL;
2322 static HRESULT WINAPI d3dx9_texture_shader_SetMatrix(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix)
2324 FIXME("iface %p, constant %p, matrix %p stub.\n", iface, constant, matrix);
2326 return E_NOTIMPL;
2329 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
2331 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2333 return E_NOTIMPL;
2336 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixPointerArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
2338 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2340 return E_NOTIMPL;
2343 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTranspose(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix)
2345 FIXME("iface %p, constant %p, matrix %p stub.\n", iface, constant, matrix);
2347 return E_NOTIMPL;
2350 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTransposeArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
2352 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2354 return E_NOTIMPL;
2357 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTransposePointerArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
2359 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2361 return E_NOTIMPL;
2364 static const struct ID3DXTextureShaderVtbl d3dx9_texture_shader_vtbl =
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
2396 HRESULT WINAPI D3DXCreateTextureShader(const DWORD *function, ID3DXTextureShader **texture_shader)
2398 struct d3dx9_texture_shader *object;
2400 TRACE("function %p, texture_shader %p.\n", function, texture_shader);
2402 if (!function || !texture_shader)
2403 return D3DERR_INVALIDCALL;
2405 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
2406 if (!object)
2407 return E_OUTOFMEMORY;
2409 object->ID3DXTextureShader_iface.lpVtbl = &d3dx9_texture_shader_vtbl;
2410 object->ref = 1;
2412 *texture_shader = &object->ID3DXTextureShader_iface;
2414 return D3D_OK;