d3dcompiler_43/tests: Fix the spelling of a couple of ok() messages.
[wine/zf.git] / dlls / d3dx9_36 / shader.c
blob376b97f5ea6d4e84e3d17807ba2d850e35c5bb90
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
22 #include "d3dx9_private.h"
23 #include "d3dcommon.h"
24 #include "d3dcompiler.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
28 /* This function is not declared in the SDK headers yet. */
29 HRESULT WINAPI D3DAssemble(const void *data, SIZE_T datasize, const char *filename,
30 const D3D_SHADER_MACRO *defines, ID3DInclude *include, UINT flags,
31 ID3DBlob **shader, ID3DBlob **error_messages);
33 static inline BOOL is_valid_bytecode(DWORD token)
35 return (token & 0xfffe0000) == 0xfffe0000;
38 const char * WINAPI D3DXGetPixelShaderProfile(struct IDirect3DDevice9 *device)
40 D3DCAPS9 caps;
42 TRACE("device %p\n", device);
44 if (!device) return NULL;
46 IDirect3DDevice9_GetDeviceCaps(device,&caps);
48 switch (caps.PixelShaderVersion)
50 case D3DPS_VERSION(1, 1):
51 return "ps_1_1";
53 case D3DPS_VERSION(1, 2):
54 return "ps_1_2";
56 case D3DPS_VERSION(1, 3):
57 return "ps_1_3";
59 case D3DPS_VERSION(1, 4):
60 return "ps_1_4";
62 case D3DPS_VERSION(2, 0):
63 if ((caps.PS20Caps.NumTemps>=22) &&
64 (caps.PS20Caps.Caps&D3DPS20CAPS_ARBITRARYSWIZZLE) &&
65 (caps.PS20Caps.Caps&D3DPS20CAPS_GRADIENTINSTRUCTIONS) &&
66 (caps.PS20Caps.Caps&D3DPS20CAPS_PREDICATION) &&
67 (caps.PS20Caps.Caps&D3DPS20CAPS_NODEPENDENTREADLIMIT) &&
68 (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
70 return "ps_2_a";
72 if ((caps.PS20Caps.NumTemps>=32) &&
73 (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
75 return "ps_2_b";
77 return "ps_2_0";
79 case D3DPS_VERSION(3, 0):
80 return "ps_3_0";
83 return NULL;
86 UINT WINAPI D3DXGetShaderSize(const DWORD *byte_code)
88 const DWORD *ptr = byte_code;
90 TRACE("byte_code %p\n", byte_code);
92 if (!ptr) return 0;
94 /* Look for the END token, skipping the VERSION token */
95 while (*++ptr != D3DSIO_END)
97 /* Skip comments */
98 if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
100 ptr += ((*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT);
103 ++ptr;
105 /* Return the shader size in bytes */
106 return (ptr - byte_code) * sizeof(*ptr);
109 DWORD WINAPI D3DXGetShaderVersion(const DWORD *byte_code)
111 TRACE("byte_code %p\n", byte_code);
113 return byte_code ? *byte_code : 0;
116 const char * WINAPI D3DXGetVertexShaderProfile(struct IDirect3DDevice9 *device)
118 D3DCAPS9 caps;
120 TRACE("device %p\n", device);
122 if (!device) return NULL;
124 IDirect3DDevice9_GetDeviceCaps(device,&caps);
126 switch (caps.VertexShaderVersion)
128 case D3DVS_VERSION(1, 1):
129 return "vs_1_1";
130 case D3DVS_VERSION(2, 0):
131 if ((caps.VS20Caps.NumTemps>=13) &&
132 (caps.VS20Caps.DynamicFlowControlDepth==24) &&
133 (caps.VS20Caps.Caps&D3DPS20CAPS_PREDICATION))
135 return "vs_2_a";
137 return "vs_2_0";
138 case D3DVS_VERSION(3, 0):
139 return "vs_3_0";
142 return NULL;
145 HRESULT WINAPI D3DXFindShaderComment(const DWORD *byte_code, DWORD fourcc, const void **data, UINT *size)
147 const DWORD *ptr = byte_code;
148 DWORD version;
150 TRACE("byte_code %p, fourcc %x, data %p, size %p\n", byte_code, fourcc, data, size);
152 if (data) *data = NULL;
153 if (size) *size = 0;
155 if (!byte_code) return D3DERR_INVALIDCALL;
157 version = *ptr >> 16;
158 if (version != 0x4658 /* FX */
159 && version != 0x5458 /* TX */
160 && version != 0x7ffe
161 && version != 0x7fff
162 && version != 0xfffe /* VS */
163 && version != 0xffff) /* PS */
165 WARN("Invalid data supplied\n");
166 return D3DXERR_INVALIDDATA;
169 while (*++ptr != D3DSIO_END)
171 /* Check if it is a comment */
172 if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
174 DWORD comment_size = (*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
176 /* Check if this is the comment we are looking for */
177 if (*(ptr + 1) == fourcc)
179 UINT ctab_size = (comment_size - 1) * sizeof(DWORD);
180 const void *ctab_data = ptr + 2;
181 if (size)
182 *size = ctab_size;
183 if (data)
184 *data = ctab_data;
185 TRACE("Returning comment data at %p with size %d\n", ctab_data, ctab_size);
186 return D3D_OK;
188 ptr += comment_size;
192 return S_FALSE;
195 HRESULT WINAPI D3DXAssembleShader(const char *data, UINT data_len, const D3DXMACRO *defines,
196 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
198 HRESULT hr;
200 TRACE("data %p, data_len %u, defines %p, include %p, flags %#x, shader %p, error_messages %p\n",
201 data, data_len, defines, include, flags, shader, error_messages);
203 /* Forward to d3dcompiler: the parameter types aren't really different,
204 the actual data types are equivalent */
205 hr = D3DAssemble(data, data_len, NULL, (D3D_SHADER_MACRO *)defines,
206 (ID3DInclude *)include, flags, (ID3DBlob **)shader,
207 (ID3DBlob **)error_messages);
209 if(hr == E_FAIL) hr = D3DXERR_INVALIDDATA;
210 return hr;
213 static const void *main_file_data;
215 static CRITICAL_SECTION_DEBUG from_file_mutex_debug =
217 0, 0, &from_file_mutex,
219 &from_file_mutex_debug.ProcessLocksList,
220 &from_file_mutex_debug.ProcessLocksList
222 0, 0, {(DWORD_PTR)(__FILE__ ": from_file_mutex")}
224 CRITICAL_SECTION from_file_mutex = {&from_file_mutex_debug, -1, 0, 0, 0, 0};
226 /* D3DXInclude private implementation, used to implement
227 * D3DXAssembleShaderFromFile() from D3DXAssembleShader(). */
228 /* To be able to correctly resolve include search paths we have to store the
229 * pathname of each include file. We store the pathname pointer right before
230 * the file data. */
231 static HRESULT WINAPI d3dx_include_from_file_open(ID3DXInclude *iface, D3DXINCLUDE_TYPE include_type,
232 const char *filename, const void *parent_data, const void **data, UINT *bytes)
234 const char *p, *parent_name = "";
235 char *pathname = NULL, *ptr;
236 char **buffer = NULL;
237 HANDLE file;
238 UINT size;
240 if (parent_data)
242 parent_name = *((const char **)parent_data - 1);
244 else
246 if (main_file_data)
247 parent_name = *((const char **)main_file_data - 1);
250 TRACE("Looking up include file %s, parent %s.\n", debugstr_a(filename), debugstr_a(parent_name));
252 if ((p = strrchr(parent_name, '\\')))
253 ++p;
254 else
255 p = parent_name;
256 pathname = HeapAlloc(GetProcessHeap(), 0, (p - parent_name) + strlen(filename) + 1);
257 if(!pathname)
258 return HRESULT_FROM_WIN32(GetLastError());
260 memcpy(pathname, parent_name, p - parent_name);
261 strcpy(pathname + (p - parent_name), filename);
262 ptr = pathname + (p - parent_name);
263 while (*ptr)
265 if (*ptr == '/')
266 *ptr = '\\';
267 ++ptr;
270 file = CreateFileA(pathname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
271 if(file == INVALID_HANDLE_VALUE)
272 goto error;
274 TRACE("Include file found at pathname = %s\n", debugstr_a(pathname));
276 size = GetFileSize(file, NULL);
277 if(size == INVALID_FILE_SIZE)
278 goto error;
280 buffer = HeapAlloc(GetProcessHeap(), 0, size + sizeof(char *));
281 if(!buffer)
282 goto error;
283 *buffer = pathname;
284 if(!ReadFile(file, buffer + 1, size, bytes, NULL))
285 goto error;
287 *data = buffer + 1;
288 if (!main_file_data)
289 main_file_data = *data;
291 CloseHandle(file);
292 return S_OK;
294 error:
295 CloseHandle(file);
296 HeapFree(GetProcessHeap(), 0, pathname);
297 HeapFree(GetProcessHeap(), 0, buffer);
298 return HRESULT_FROM_WIN32(GetLastError());
301 static HRESULT WINAPI d3dx_include_from_file_close(ID3DXInclude *iface, const void *data)
303 HeapFree(GetProcessHeap(), 0, *((char **)data - 1));
304 HeapFree(GetProcessHeap(), 0, (char **)data - 1);
305 if (main_file_data == data)
306 main_file_data = NULL;
307 return S_OK;
310 const struct ID3DXIncludeVtbl d3dx_include_from_file_vtbl =
312 d3dx_include_from_file_open,
313 d3dx_include_from_file_close
316 HRESULT WINAPI D3DXAssembleShaderFromFileA(const char *filename, const D3DXMACRO *defines,
317 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
319 WCHAR *filename_w;
320 DWORD len;
321 HRESULT ret;
323 TRACE("filename %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
324 debugstr_a(filename), defines, include, flags, shader, error_messages);
326 if (!filename) return D3DXERR_INVALIDDATA;
328 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
329 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
330 if (!filename_w) return E_OUTOFMEMORY;
331 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
333 ret = D3DXAssembleShaderFromFileW(filename_w, defines, include, flags, shader, error_messages);
335 HeapFree(GetProcessHeap(), 0, filename_w);
336 return ret;
339 HRESULT WINAPI D3DXAssembleShaderFromFileW(const WCHAR *filename, const D3DXMACRO *defines,
340 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
342 const void *buffer;
343 DWORD len;
344 HRESULT hr;
345 struct d3dx_include_from_file include_from_file;
346 char *filename_a;
348 TRACE("filename %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
349 debugstr_w(filename), defines, include, flags, shader, error_messages);
351 if(!include)
353 include_from_file.ID3DXInclude_iface.lpVtbl = &d3dx_include_from_file_vtbl;
354 include = &include_from_file.ID3DXInclude_iface;
357 len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
358 filename_a = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
359 if (!filename_a)
360 return E_OUTOFMEMORY;
361 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, len, NULL, NULL);
363 EnterCriticalSection(&from_file_mutex);
364 hr = ID3DXInclude_Open(include, D3DXINC_LOCAL, filename_a, NULL, &buffer, &len);
365 if (FAILED(hr))
367 LeaveCriticalSection(&from_file_mutex);
368 HeapFree(GetProcessHeap(), 0, filename_a);
369 return D3DXERR_INVALIDDATA;
372 hr = D3DXAssembleShader(buffer, len, defines, include, flags, shader, error_messages);
374 ID3DXInclude_Close(include, buffer);
375 LeaveCriticalSection(&from_file_mutex);
376 HeapFree(GetProcessHeap(), 0, filename_a);
377 return hr;
380 HRESULT WINAPI D3DXAssembleShaderFromResourceA(HMODULE module, const char *resource, const D3DXMACRO *defines,
381 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
383 void *buffer;
384 HRSRC res;
385 DWORD len;
387 TRACE("module %p, resource %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
388 module, debugstr_a(resource), defines, include, flags, shader, error_messages);
390 if (!(res = FindResourceA(module, resource, (const char *)RT_RCDATA)))
391 return D3DXERR_INVALIDDATA;
392 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
393 return D3DXERR_INVALIDDATA;
394 return D3DXAssembleShader(buffer, len, defines, include, flags,
395 shader, error_messages);
398 HRESULT WINAPI D3DXAssembleShaderFromResourceW(HMODULE module, const WCHAR *resource, const D3DXMACRO *defines,
399 ID3DXInclude *include, DWORD flags, ID3DXBuffer **shader, ID3DXBuffer **error_messages)
401 void *buffer;
402 HRSRC res;
403 DWORD len;
405 TRACE("module %p, resource %s, defines %p, include %p, flags %#x, shader %p, error_messages %p.\n",
406 module, debugstr_w(resource), defines, include, flags, shader, error_messages);
408 if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
409 return D3DXERR_INVALIDDATA;
410 if (FAILED(load_resource_into_memory(module, res, &buffer, &len)))
411 return D3DXERR_INVALIDDATA;
412 return D3DXAssembleShader(buffer, len, defines, include, flags,
413 shader, error_messages);
416 HRESULT WINAPI D3DXCompileShader(const char *data, UINT length, const D3DXMACRO *defines,
417 ID3DXInclude *include, const char *function, const char *profile, DWORD flags,
418 ID3DXBuffer **shader, ID3DXBuffer **error_msgs, ID3DXConstantTable **constant_table)
420 HRESULT hr;
422 TRACE("data %s, length %u, defines %p, include %p, function %s, profile %s, "
423 "flags %#x, shader %p, error_msgs %p, constant_table %p.\n",
424 debugstr_a(data), length, defines, include, debugstr_a(function), debugstr_a(profile),
425 flags, shader, error_msgs, constant_table);
427 if (D3DX_SDK_VERSION <= 36)
428 flags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
430 hr = D3DCompile(data, length, NULL, (D3D_SHADER_MACRO *)defines, (ID3DInclude *)include,
431 function, profile, flags, 0, (ID3DBlob **)shader, (ID3DBlob **)error_msgs);
433 if (SUCCEEDED(hr) && constant_table)
435 hr = D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*shader), constant_table);
436 if (FAILED(hr))
438 ID3DXBuffer_Release(*shader);
439 *shader = NULL;
443 return hr;
446 HRESULT WINAPI D3DXCompileShaderFromFileA(const char *filename, const D3DXMACRO *defines,
447 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
448 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
450 WCHAR *filename_w;
451 DWORD len;
452 HRESULT ret;
454 TRACE("filename %s, defines %p, include %p, entrypoint %s, profile %s, "
455 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
456 debugstr_a(filename), defines, include, debugstr_a(entrypoint),
457 debugstr_a(profile), flags, shader, error_messages, constant_table);
459 if (!filename) return D3DXERR_INVALIDDATA;
461 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
462 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
463 if (!filename_w) return E_OUTOFMEMORY;
464 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
466 ret = D3DXCompileShaderFromFileW(filename_w, defines, include,
467 entrypoint, profile, flags,
468 shader, error_messages, constant_table);
470 HeapFree(GetProcessHeap(), 0, filename_w);
471 return ret;
474 HRESULT WINAPI D3DXCompileShaderFromFileW(const WCHAR *filename, const D3DXMACRO *defines,
475 ID3DXInclude *include, const char *entrypoint, const char *profile, DWORD flags,
476 ID3DXBuffer **shader, ID3DXBuffer **error_messages, ID3DXConstantTable **constant_table)
478 const void *buffer;
479 DWORD len, filename_len;
480 HRESULT hr;
481 struct d3dx_include_from_file include_from_file;
482 char *filename_a;
484 TRACE("filename %s, defines %p, include %p, entrypoint %s, profile %s, "
485 "flags %#x, shader %p, error_messages %p, constant_table %p.\n",
486 debugstr_w(filename), defines, include, debugstr_a(entrypoint), debugstr_a(profile),
487 flags, shader, error_messages, constant_table);
489 if (!include)
491 include_from_file.ID3DXInclude_iface.lpVtbl = &d3dx_include_from_file_vtbl;
492 include = &include_from_file.ID3DXInclude_iface;
495 filename_len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
496 filename_a = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(char));
497 if (!filename_a)
498 return E_OUTOFMEMORY;
499 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, filename_len, NULL, NULL);
501 EnterCriticalSection(&from_file_mutex);
502 hr = ID3DXInclude_Open(include, D3DXINC_LOCAL, filename_a, NULL, &buffer, &len);
503 if (FAILED(hr))
505 LeaveCriticalSection(&from_file_mutex);
506 HeapFree(GetProcessHeap(), 0, filename_a);
507 return D3DXERR_INVALIDDATA;
510 if (D3DX_SDK_VERSION <= 36)
511 flags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
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 d3dx_include_from_file include_from_file;
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 include_from_file.ID3DXInclude_iface.lpVtbl = &d3dx_include_from_file_vtbl;
618 include = &include_from_file.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 struct d3dx9_fragment_linker
2080 ID3DXFragmentLinker ID3DXFragmentLinker_iface;
2081 LONG ref;
2083 struct IDirect3DDevice9 *device;
2084 DWORD flags;
2087 static inline struct d3dx9_fragment_linker *impl_from_ID3DXFragmentLinker(ID3DXFragmentLinker *iface)
2089 return CONTAINING_RECORD(iface, struct d3dx9_fragment_linker, ID3DXFragmentLinker_iface);
2092 static HRESULT WINAPI d3dx9_fragment_linker_QueryInterface(ID3DXFragmentLinker *iface, REFIID riid, void **out)
2094 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
2096 if (IsEqualGUID(riid, &IID_IUnknown)
2097 || IsEqualGUID(riid, &IID_ID3DXFragmentLinker))
2099 iface->lpVtbl->AddRef(iface);
2100 *out = iface;
2101 return D3D_OK;
2104 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
2105 *out = NULL;
2106 return E_NOINTERFACE;
2109 static ULONG WINAPI d3dx9_fragment_linker_AddRef(ID3DXFragmentLinker *iface)
2111 struct d3dx9_fragment_linker *linker = impl_from_ID3DXFragmentLinker(iface);
2112 ULONG refcount = InterlockedIncrement(&linker->ref);
2114 TRACE("%p increasing refcount to %u.\n", linker, refcount);
2116 return refcount;
2119 static ULONG WINAPI d3dx9_fragment_linker_Release(ID3DXFragmentLinker *iface)
2121 struct d3dx9_fragment_linker *linker = impl_from_ID3DXFragmentLinker(iface);
2122 ULONG refcount = InterlockedDecrement(&linker->ref);
2124 TRACE("%p decreasing refcount to %u.\n", linker, refcount);
2126 if (!refcount)
2128 IDirect3DDevice9_Release(linker->device);
2129 heap_free(linker);
2132 return refcount;
2135 static HRESULT WINAPI d3dx9_fragment_linker_GetDevice(ID3DXFragmentLinker *iface, struct IDirect3DDevice9 **device)
2137 struct d3dx9_fragment_linker *linker = impl_from_ID3DXFragmentLinker(iface);
2139 TRACE("iface %p, device %p.\n", linker, device);
2141 if (!device)
2143 WARN("Invalid argument supplied.\n");
2144 return D3DERR_INVALIDCALL;
2147 IDirect3DDevice9_AddRef(linker->device);
2148 *device = linker->device;
2149 TRACE("Returning device %p.\n", *device);
2151 return S_OK;
2154 static UINT WINAPI d3dx9_fragment_linker_GetNumberOfFragments(ID3DXFragmentLinker *iface)
2156 FIXME("iface %p: stub.\n", iface);
2158 return E_NOTIMPL;
2161 static D3DXHANDLE WINAPI d3dx9_fragment_linker_GetFragmentHandleByIndex(ID3DXFragmentLinker *iface, UINT index)
2163 FIXME("iface %p, index %u: stub.\n", iface, index);
2165 return NULL;
2168 static D3DXHANDLE WINAPI d3dx9_fragment_linker_GetFragmentHandleByName(ID3DXFragmentLinker *iface,
2169 const char *name)
2171 FIXME("iface %p, name %s: stub.\n", iface, debugstr_a(name));
2173 return NULL;
2176 static HRESULT WINAPI d3dx9_fragment_linker_GetFragmentDesc(ID3DXFragmentLinker *iface, D3DXHANDLE name,
2177 D3DXFRAGMENT_DESC *desc)
2179 FIXME("iface %p, name %p, desc %p: stub.\n", iface, name, desc);
2181 return E_NOTIMPL;
2184 static HRESULT WINAPI d3dx9_fragment_linker_AddFragments(ID3DXFragmentLinker *iface, const DWORD *fragments)
2186 FIXME("iface %p, fragments %p: stub.\n", iface, fragments);
2188 return E_NOTIMPL;
2191 static HRESULT WINAPI d3dx9_fragment_linker_GetAllFragments(ID3DXFragmentLinker *iface, ID3DXBuffer **buffer)
2193 FIXME("iface %p, buffer %p: stub.\n", iface, buffer);
2195 return E_NOTIMPL;
2198 static HRESULT WINAPI d3dx9_fragment_linker_GetFragment(ID3DXFragmentLinker *iface, D3DXHANDLE name,
2199 ID3DXBuffer **buffer)
2201 FIXME("iface %p, name %p, buffer %p: stub.\n", iface, name, buffer);
2203 return E_NOTIMPL;
2206 static HRESULT WINAPI d3dx9_fragment_linker_LinkShader(ID3DXFragmentLinker *iface, const char *profile,
2207 DWORD flags, const D3DXHANDLE *handles, UINT fragment_count, ID3DXBuffer **buffer,
2208 ID3DXBuffer **errors)
2210 FIXME("iface %p, profile %s, flags %#x, handles %p, fragment_count %u, buffer %p, errors %p: stub.\n",
2211 iface, debugstr_a(profile), flags, handles, fragment_count, buffer, errors);
2213 return E_NOTIMPL;
2216 static HRESULT WINAPI d3dx9_fragment_linker_LinkVertexShader(ID3DXFragmentLinker *iface, const char *profile,
2217 DWORD flags, const D3DXHANDLE *handles, UINT fragment_count, IDirect3DVertexShader9 **shader,
2218 ID3DXBuffer **errors)
2220 FIXME("iface %p, profile %s, flags %#x, handles %p, fragment_count %u, shader %p, errors %p: stub.\n",
2221 iface, debugstr_a(profile), flags, handles, fragment_count, shader, errors);
2223 return E_NOTIMPL;
2226 static HRESULT WINAPI d3dx9_fragment_linker_LinkPixelShader(ID3DXFragmentLinker *iface, const char *profile,
2227 DWORD flags, const D3DXHANDLE *handles, UINT fragment_count, IDirect3DPixelShader9 **shader,
2228 ID3DXBuffer **errors)
2230 FIXME("iface %p, profile %s, flags %#x, handles %p, fragment_count %u, shader %p, errors %p: stub.\n",
2231 iface, debugstr_a(profile), flags, handles, fragment_count, shader, errors);
2233 return E_NOTIMPL;
2236 static HRESULT WINAPI d3dx9_fragment_linker_ClearCache(ID3DXFragmentLinker *iface)
2238 FIXME("iface %p: stub.\n", iface);
2240 return E_NOTIMPL;
2243 static const struct ID3DXFragmentLinkerVtbl d3dx9_fragment_linker_vtbl =
2245 d3dx9_fragment_linker_QueryInterface,
2246 d3dx9_fragment_linker_AddRef,
2247 d3dx9_fragment_linker_Release,
2248 d3dx9_fragment_linker_GetDevice,
2249 d3dx9_fragment_linker_GetNumberOfFragments,
2250 d3dx9_fragment_linker_GetFragmentHandleByIndex,
2251 d3dx9_fragment_linker_GetFragmentHandleByName,
2252 d3dx9_fragment_linker_GetFragmentDesc,
2253 d3dx9_fragment_linker_AddFragments,
2254 d3dx9_fragment_linker_GetAllFragments,
2255 d3dx9_fragment_linker_GetFragment,
2256 d3dx9_fragment_linker_LinkShader,
2257 d3dx9_fragment_linker_LinkVertexShader,
2258 d3dx9_fragment_linker_LinkPixelShader,
2259 d3dx9_fragment_linker_ClearCache
2262 HRESULT WINAPI D3DXCreateFragmentLinkerEx(IDirect3DDevice9 *device, UINT size, DWORD flags,
2263 ID3DXFragmentLinker **linker)
2265 struct d3dx9_fragment_linker *object;
2267 TRACE("device %p, size %u, flags %#x, linker %p.\n", device, size, flags, linker);
2269 object = heap_alloc(sizeof(*object));
2270 if (!object)
2271 return E_OUTOFMEMORY;
2273 object->ID3DXFragmentLinker_iface.lpVtbl = &d3dx9_fragment_linker_vtbl;
2274 object->ref = 1;
2276 IDirect3DDevice9_AddRef(device);
2277 object->device = device;
2278 object->flags = flags;
2280 *linker = &object->ID3DXFragmentLinker_iface;
2282 return S_OK;
2285 HRESULT WINAPI D3DXCreateFragmentLinker(IDirect3DDevice9 *device, UINT size, ID3DXFragmentLinker **linker)
2287 TRACE("device %p, size %u, linker %p.\n", device, size, linker);
2289 return D3DXCreateFragmentLinkerEx(device, size, 0, linker);
2292 HRESULT WINAPI D3DXGetShaderSamplers(const DWORD *byte_code, const char **samplers, UINT *count)
2294 UINT i, sampler_count = 0;
2295 UINT size;
2296 const char *data;
2297 const D3DXSHADER_CONSTANTTABLE *ctab_header;
2298 const D3DXSHADER_CONSTANTINFO *constant_info;
2300 TRACE("byte_code %p, samplers %p, count %p\n", byte_code, samplers, count);
2302 if (count) *count = 0;
2304 if (D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), (const void **)&data, &size) != D3D_OK)
2305 return D3D_OK;
2307 if (size < sizeof(*ctab_header)) return D3D_OK;
2309 ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
2310 if (ctab_header->Size != sizeof(*ctab_header)) return D3D_OK;
2312 constant_info = (const D3DXSHADER_CONSTANTINFO *)(data + ctab_header->ConstantInfo);
2313 for (i = 0; i < ctab_header->Constants; i++)
2315 const D3DXSHADER_TYPEINFO *type;
2317 TRACE("name = %s\n", data + constant_info[i].Name);
2319 type = (const D3DXSHADER_TYPEINFO *)(data + constant_info[i].TypeInfo);
2321 if (type->Type == D3DXPT_SAMPLER
2322 || type->Type == D3DXPT_SAMPLER1D
2323 || type->Type == D3DXPT_SAMPLER2D
2324 || type->Type == D3DXPT_SAMPLER3D
2325 || type->Type == D3DXPT_SAMPLERCUBE)
2327 if (samplers) samplers[sampler_count] = data + constant_info[i].Name;
2329 ++sampler_count;
2333 TRACE("Found %u samplers\n", sampler_count);
2335 if (count) *count = sampler_count;
2337 return D3D_OK;
2340 HRESULT WINAPI D3DXDisassembleShader(const DWORD *shader, BOOL colorcode, const char *comments, ID3DXBuffer **disassembly)
2342 FIXME("%p %d %s %p: stub\n", shader, colorcode, debugstr_a(comments), disassembly);
2343 return E_OUTOFMEMORY;
2346 struct d3dx9_texture_shader
2348 ID3DXTextureShader ID3DXTextureShader_iface;
2349 LONG ref;
2352 static inline struct d3dx9_texture_shader *impl_from_ID3DXTextureShader(ID3DXTextureShader *iface)
2354 return CONTAINING_RECORD(iface, struct d3dx9_texture_shader, ID3DXTextureShader_iface);
2357 static HRESULT WINAPI d3dx9_texture_shader_QueryInterface(ID3DXTextureShader *iface, REFIID riid, void **out)
2359 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
2361 if (IsEqualGUID(riid, &IID_IUnknown) ||
2362 IsEqualGUID(riid, &IID_ID3DXTextureShader))
2364 iface->lpVtbl->AddRef(iface);
2365 *out = iface;
2366 return D3D_OK;
2369 WARN("Interface %s not found.\n", debugstr_guid(riid));
2370 *out = NULL;
2371 return E_NOINTERFACE;
2374 static ULONG WINAPI d3dx9_texture_shader_AddRef(ID3DXTextureShader *iface)
2376 struct d3dx9_texture_shader *texture_shader = impl_from_ID3DXTextureShader(iface);
2377 ULONG refcount = InterlockedIncrement(&texture_shader->ref);
2379 TRACE("%p increasing refcount to %u.\n", texture_shader, refcount);
2381 return refcount;
2384 static ULONG WINAPI d3dx9_texture_shader_Release(ID3DXTextureShader *iface)
2386 struct d3dx9_texture_shader *texture_shader = impl_from_ID3DXTextureShader(iface);
2387 ULONG refcount = InterlockedDecrement(&texture_shader->ref);
2389 TRACE("%p decreasing refcount to %u.\n", texture_shader, refcount);
2391 if (!refcount)
2393 HeapFree(GetProcessHeap(), 0, texture_shader);
2396 return refcount;
2399 static HRESULT WINAPI d3dx9_texture_shader_GetFunction(ID3DXTextureShader *iface, struct ID3DXBuffer **function)
2401 FIXME("iface %p, function %p stub.\n", iface, function);
2403 return E_NOTIMPL;
2406 static HRESULT WINAPI d3dx9_texture_shader_GetConstantBuffer(ID3DXTextureShader *iface, struct ID3DXBuffer **constant_buffer)
2408 FIXME("iface %p, constant_buffer %p stub.\n", iface, constant_buffer);
2410 return E_NOTIMPL;
2413 static HRESULT WINAPI d3dx9_texture_shader_GetDesc(ID3DXTextureShader *iface, D3DXCONSTANTTABLE_DESC *desc)
2415 FIXME("iface %p, desc %p stub.\n", iface, desc);
2417 return E_NOTIMPL;
2420 static HRESULT WINAPI d3dx9_texture_shader_GetConstantDesc(ID3DXTextureShader *iface, D3DXHANDLE constant, D3DXCONSTANT_DESC *constant_desc, UINT *count)
2422 FIXME("iface %p, constant %p, constant_desc %p, count %p stub.\n", iface, constant, constant_desc, count);
2424 return E_NOTIMPL;
2427 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstant(ID3DXTextureShader *iface, D3DXHANDLE constant, UINT index)
2429 FIXME("iface %p, constant %p, index %u stub.\n", iface, constant, index);
2431 return NULL;
2434 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstantByName(ID3DXTextureShader *iface, D3DXHANDLE constant, const char *name)
2436 FIXME("iface %p, constant %p, name %s stub.\n", iface, constant, debugstr_a(name));
2438 return NULL;
2441 static D3DXHANDLE WINAPI d3dx9_texture_shader_GetConstantElement(ID3DXTextureShader *iface, D3DXHANDLE constant, UINT index)
2443 FIXME("iface %p, constant %p, index %u stub.\n", iface, constant, index);
2445 return NULL;
2448 static HRESULT WINAPI d3dx9_texture_shader_SetDefaults(ID3DXTextureShader *iface)
2450 FIXME("iface %p stub.\n", iface);
2452 return E_NOTIMPL;
2455 static HRESULT WINAPI d3dx9_texture_shader_SetValue(ID3DXTextureShader *iface, D3DXHANDLE constant, const void *data, UINT bytes)
2457 FIXME("iface %p, constant %p, data %p, bytes %u stub.\n", iface, constant, data, bytes);
2459 return E_NOTIMPL;
2462 static HRESULT WINAPI d3dx9_texture_shader_SetBool(ID3DXTextureShader *iface, D3DXHANDLE constant, BOOL b)
2464 FIXME("iface %p, constant %p, b %u stub.\n", iface, constant, b);
2466 return E_NOTIMPL;
2469 static HRESULT WINAPI d3dx9_texture_shader_SetBoolArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const BOOL *b, UINT count)
2471 FIXME("iface %p, constant %p, b %p, count %u stub.\n", iface, constant, b, count);
2473 return E_NOTIMPL;
2476 static HRESULT WINAPI d3dx9_texture_shader_SetInt(ID3DXTextureShader *iface, D3DXHANDLE constant, INT n)
2478 FIXME("iface %p, constant %p, n %d stub.\n", iface, constant, n);
2480 return E_NOTIMPL;
2483 static HRESULT WINAPI d3dx9_texture_shader_SetIntArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const INT *n, UINT count)
2485 FIXME("iface %p, constant %p, n %p, count %u stub.\n", iface, constant, n, count);
2487 return E_NOTIMPL;
2490 static HRESULT WINAPI d3dx9_texture_shader_SetFloat(ID3DXTextureShader *iface, D3DXHANDLE constant, FLOAT f)
2492 FIXME("iface %p, constant %p, f %f stub.\n", iface, constant, f);
2494 return E_NOTIMPL;
2497 static HRESULT WINAPI d3dx9_texture_shader_SetFloatArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const FLOAT *f, UINT count)
2499 FIXME("iface %p, constant %p, f %p, count %u stub.\n", iface, constant, f, count);
2501 return E_NOTIMPL;
2504 static HRESULT WINAPI d3dx9_texture_shader_SetVector(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXVECTOR4 *vector)
2506 FIXME("iface %p, constant %p, vector %p stub.\n", iface, constant, vector);
2508 return E_NOTIMPL;
2511 static HRESULT WINAPI d3dx9_texture_shader_SetVectorArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXVECTOR4 *vector, UINT count)
2513 FIXME("iface %p, constant %p, vector %p, count %u stub.\n", iface, constant, vector, count);
2515 return E_NOTIMPL;
2518 static HRESULT WINAPI d3dx9_texture_shader_SetMatrix(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix)
2520 FIXME("iface %p, constant %p, matrix %p stub.\n", iface, constant, matrix);
2522 return E_NOTIMPL;
2525 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
2527 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2529 return E_NOTIMPL;
2532 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixPointerArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
2534 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2536 return E_NOTIMPL;
2539 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTranspose(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix)
2541 FIXME("iface %p, constant %p, matrix %p stub.\n", iface, constant, matrix);
2543 return E_NOTIMPL;
2546 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTransposeArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX *matrix, UINT count)
2548 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2550 return E_NOTIMPL;
2553 static HRESULT WINAPI d3dx9_texture_shader_SetMatrixTransposePointerArray(ID3DXTextureShader *iface, D3DXHANDLE constant, const D3DXMATRIX **matrix, UINT count)
2555 FIXME("iface %p, constant %p, matrix %p, count %u stub.\n", iface, constant, matrix, count);
2557 return E_NOTIMPL;
2560 static const struct ID3DXTextureShaderVtbl d3dx9_texture_shader_vtbl =
2562 /*** IUnknown methods ***/
2563 d3dx9_texture_shader_QueryInterface,
2564 d3dx9_texture_shader_AddRef,
2565 d3dx9_texture_shader_Release,
2566 /*** ID3DXTextureShader methods ***/
2567 d3dx9_texture_shader_GetFunction,
2568 d3dx9_texture_shader_GetConstantBuffer,
2569 d3dx9_texture_shader_GetDesc,
2570 d3dx9_texture_shader_GetConstantDesc,
2571 d3dx9_texture_shader_GetConstant,
2572 d3dx9_texture_shader_GetConstantByName,
2573 d3dx9_texture_shader_GetConstantElement,
2574 d3dx9_texture_shader_SetDefaults,
2575 d3dx9_texture_shader_SetValue,
2576 d3dx9_texture_shader_SetBool,
2577 d3dx9_texture_shader_SetBoolArray,
2578 d3dx9_texture_shader_SetInt,
2579 d3dx9_texture_shader_SetIntArray,
2580 d3dx9_texture_shader_SetFloat,
2581 d3dx9_texture_shader_SetFloatArray,
2582 d3dx9_texture_shader_SetVector,
2583 d3dx9_texture_shader_SetVectorArray,
2584 d3dx9_texture_shader_SetMatrix,
2585 d3dx9_texture_shader_SetMatrixArray,
2586 d3dx9_texture_shader_SetMatrixPointerArray,
2587 d3dx9_texture_shader_SetMatrixTranspose,
2588 d3dx9_texture_shader_SetMatrixTransposeArray,
2589 d3dx9_texture_shader_SetMatrixTransposePointerArray
2592 HRESULT WINAPI D3DXCreateTextureShader(const DWORD *function, ID3DXTextureShader **texture_shader)
2594 struct d3dx9_texture_shader *object;
2596 TRACE("function %p, texture_shader %p.\n", function, texture_shader);
2598 if (!function || !texture_shader)
2599 return D3DERR_INVALIDCALL;
2601 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
2602 if (!object)
2603 return E_OUTOFMEMORY;
2605 object->ID3DXTextureShader_iface.lpVtbl = &d3dx9_texture_shader_vtbl;
2606 object->ref = 1;
2608 *texture_shader = &object->ID3DXTextureShader_iface;
2610 return D3D_OK;
2613 static unsigned int get_instr_length(const DWORD *byte_code, unsigned int major, unsigned int minor)
2615 DWORD opcode = *byte_code & 0xffff;
2616 unsigned int len = 0;
2618 if (opcode == D3DSIO_COMMENT)
2619 return (*byte_code & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
2621 if (major > 1)
2622 return (*byte_code & D3DSI_INSTLENGTH_MASK) >> D3DSI_INSTLENGTH_SHIFT;
2624 switch (opcode)
2626 case D3DSIO_END:
2627 ERR("Unexpected END token.\n");
2628 return 0;
2629 case D3DSIO_DEF:
2630 case D3DSIO_DEFI:
2631 return 5;
2632 case D3DSIO_DEFB:
2633 return 2;
2634 default:
2635 ++byte_code;
2636 while (*byte_code & 0x80000000)
2638 ++byte_code;
2639 ++len;
2643 return len;
2646 static HRESULT get_shader_semantics(const DWORD *byte_code, D3DXSEMANTIC *semantics, UINT *count, BOOL output)
2648 static const D3DDECLUSAGE regtype_usage[] =
2650 D3DDECLUSAGE_COLOR,
2651 D3DDECLUSAGE_COLOR,
2653 D3DDECLUSAGE_TEXCOORD,
2655 D3DDECLUSAGE_COLOR,
2656 D3DDECLUSAGE_TEXCOORD,
2659 D3DDECLUSAGE_DEPTH
2661 static const D3DDECLUSAGE rast_usage[] =
2663 D3DDECLUSAGE_POSITION,
2664 D3DDECLUSAGE_FOG,
2665 D3DDECLUSAGE_PSIZE
2667 DWORD reg_type, usage, index, version_token = *byte_code;
2668 BOOL is_ps = version_token >> 16 == 0xffff;
2669 unsigned int major, minor, i = 0, j;
2670 BYTE colors = 0, rastout = 0;
2671 BOOL has_dcl, depth = 0;
2672 WORD texcoords = 0;
2674 if ((version_token & 0xffff0000) != 0xfffe0000 && (version_token & 0xffff0000) != 0xffff0000)
2675 return D3DXERR_INVALIDDATA;
2677 major = version_token >> 8 & 0xff;
2678 minor = version_token & 0xff;
2680 TRACE("%s shader, version %u.%u.\n", is_ps ? "Pixel" : "Vertex", major, minor);
2681 ++byte_code;
2683 has_dcl = (!is_ps && (!output || major == 3)) || (is_ps && !output && major >= 2);
2685 while (*byte_code != D3DSIO_END)
2687 if (has_dcl && (*byte_code & 0xffff) == D3DSIO_DCL)
2689 DWORD usage_token = byte_code[1];
2690 DWORD reg = byte_code[2];
2692 reg_type = ((reg & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT)
2693 | ((reg & D3DSP_REGTYPE_MASK2) >> D3DSP_REGTYPE_SHIFT2);
2695 if (is_ps && !output && major == 2)
2697 /* dcl with no explicit usage, look at the register. */
2698 reg_type = ((reg & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT)
2699 | ((reg & D3DSP_REGTYPE_MASK2) >> D3DSP_REGTYPE_SHIFT2);
2700 index = reg & D3DSP_REGNUM_MASK;
2701 if (reg_type >= ARRAY_SIZE(regtype_usage))
2703 WARN("Invalid register type %u.\n", reg_type);
2704 reg_type = 0;
2706 usage = regtype_usage[reg_type];
2707 if (semantics)
2709 semantics[i].Usage = usage;
2710 semantics[i].UsageIndex = index;
2712 ++i;
2714 else if ((!output && reg_type == D3DSPR_INPUT) || (output && reg_type == D3DSPR_OUTPUT))
2716 if (semantics)
2718 semantics[i].Usage =
2719 (usage_token & D3DSP_DCL_USAGE_MASK) >> D3DSP_DCL_USAGE_SHIFT;
2720 semantics[i].UsageIndex =
2721 (usage_token & D3DSP_DCL_USAGEINDEX_MASK) >> D3DSP_DCL_USAGEINDEX_SHIFT;
2723 ++i;
2725 byte_code += 3;
2727 else if (!has_dcl)
2729 unsigned int len = get_instr_length(byte_code, major, minor) + 1;
2731 switch (*byte_code & 0xffff)
2733 case D3DSIO_COMMENT:
2734 case D3DSIO_DEF:
2735 case D3DSIO_DEFB:
2736 case D3DSIO_DEFI:
2737 byte_code += len;
2738 break;
2739 default:
2740 ++byte_code;
2741 while (*byte_code & 0x80000000)
2743 reg_type = ((*byte_code & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT)
2744 | ((*byte_code & D3DSP_REGTYPE_MASK2) >> D3DSP_REGTYPE_SHIFT2);
2745 index = *byte_code & D3DSP_REGNUM_MASK;
2747 if ((reg_type == D3DSPR_TEMP && is_ps && major == 1)
2748 || (reg_type == D3DSPR_INPUT && is_ps)
2749 || (reg_type == D3DSPR_TEXTURE && is_ps && !output)
2750 || reg_type == D3DSPR_RASTOUT
2751 || reg_type == D3DSPR_ATTROUT
2752 || reg_type == D3DSPR_OUTPUT
2753 || reg_type == D3DSPR_DEPTHOUT)
2755 if (reg_type == D3DSPR_RASTOUT)
2756 rastout |= 1u << index;
2757 else if (reg_type == D3DSPR_DEPTHOUT)
2758 depth = TRUE;
2759 else if (reg_type == D3DSPR_TEXTURE || reg_type == D3DSPR_OUTPUT)
2760 texcoords |= 1u << index;
2761 else
2762 colors |= 1u << index;
2764 ++byte_code;
2768 else
2770 byte_code += get_instr_length(byte_code, major, minor) + 1;
2774 if (!has_dcl)
2776 i = j = 0;
2777 while (texcoords)
2779 if (texcoords & 1)
2781 if (semantics)
2783 semantics[i].Usage = D3DDECLUSAGE_TEXCOORD;
2784 semantics[i].UsageIndex = j;
2786 ++i;
2788 texcoords >>= 1;
2789 ++j;
2791 j = 0;
2792 while (colors)
2794 if (colors & 1)
2796 if (semantics)
2798 semantics[i].Usage = D3DDECLUSAGE_COLOR;
2799 semantics[i].UsageIndex = j;
2801 ++i;
2803 colors >>= 1;
2804 ++j;
2806 j = 0;
2807 while (rastout)
2809 if (rastout & 1)
2811 if (j >= ARRAY_SIZE(rast_usage))
2813 WARN("Invalid RASTOUT register index.\n");
2814 usage = 0;
2816 else
2818 usage = rast_usage[j];
2820 if (semantics)
2822 semantics[i].Usage = usage;
2823 semantics[i].UsageIndex = 0;
2825 ++i;
2827 rastout >>= 1;
2828 ++j;
2830 if (depth)
2832 if (semantics)
2834 semantics[i].Usage = D3DDECLUSAGE_DEPTH;
2835 semantics[i].UsageIndex = 0;
2837 ++i;
2841 if (count)
2842 *count = i;
2844 return D3D_OK;
2847 HRESULT WINAPI D3DXGetShaderInputSemantics(const DWORD *byte_code, D3DXSEMANTIC *semantics, UINT *count)
2849 TRACE("byte_code %p, semantics %p, count %p.\n", byte_code, semantics, count);
2851 return get_shader_semantics(byte_code, semantics, count, FALSE);
2854 HRESULT WINAPI D3DXGetShaderOutputSemantics(const DWORD *byte_code, D3DXSEMANTIC *semantics, UINT *count)
2856 TRACE("byte_code %p, semantics %p, count %p.\n", byte_code, semantics, count);
2858 return get_shader_semantics(byte_code, semantics, count, TRUE);