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 "wine/port.h"
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
31 #include "d3dcommon.h"
32 #include "d3dcompiler.h"
33 #include "d3dx9_36_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
37 /* This function is not declared in the SDK headers yet */
38 HRESULT WINAPI D3DAssemble(LPCVOID data, SIZE_T datasize, LPCSTR filename,
39 const D3D_SHADER_MACRO *defines, ID3DInclude *include,
41 ID3DBlob **shader, ID3DBlob **error_messages);
43 LPCSTR WINAPI D3DXGetPixelShaderProfile(LPDIRECT3DDEVICE9 device)
47 TRACE("device %p\n", device);
49 if (!device) return NULL;
51 IDirect3DDevice9_GetDeviceCaps(device,&caps);
53 switch (caps.PixelShaderVersion)
55 case D3DPS_VERSION(1, 1):
58 case D3DPS_VERSION(1, 2):
61 case D3DPS_VERSION(1, 3):
64 case D3DPS_VERSION(1, 4):
67 case D3DPS_VERSION(2, 0):
68 if ((caps.PS20Caps.NumTemps>=22) &&
69 (caps.PS20Caps.Caps&D3DPS20CAPS_ARBITRARYSWIZZLE) &&
70 (caps.PS20Caps.Caps&D3DPS20CAPS_GRADIENTINSTRUCTIONS) &&
71 (caps.PS20Caps.Caps&D3DPS20CAPS_PREDICATION) &&
72 (caps.PS20Caps.Caps&D3DPS20CAPS_NODEPENDENTREADLIMIT) &&
73 (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
77 if ((caps.PS20Caps.NumTemps>=32) &&
78 (caps.PS20Caps.Caps&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT))
84 case D3DPS_VERSION(3, 0):
91 UINT WINAPI D3DXGetShaderSize(const DWORD *byte_code)
93 const DWORD *ptr = byte_code;
95 TRACE("byte_code %p\n", byte_code);
99 /* Look for the END token, skipping the VERSION token */
100 while (*++ptr != D3DSIO_END)
103 if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
105 ptr += ((*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT);
110 /* Return the shader size in bytes */
111 return (ptr - byte_code) * sizeof(*ptr);
114 DWORD WINAPI D3DXGetShaderVersion(const DWORD *byte_code)
116 TRACE("byte_code %p\n", byte_code);
118 return byte_code ? *byte_code : 0;
121 LPCSTR WINAPI D3DXGetVertexShaderProfile(LPDIRECT3DDEVICE9 device)
125 TRACE("device %p\n", device);
127 if (!device) return NULL;
129 IDirect3DDevice9_GetDeviceCaps(device,&caps);
131 switch (caps.VertexShaderVersion)
133 case D3DVS_VERSION(1, 1):
135 case D3DVS_VERSION(2, 0):
136 if ((caps.VS20Caps.NumTemps>=13) &&
137 (caps.VS20Caps.DynamicFlowControlDepth==24) &&
138 (caps.VS20Caps.Caps&D3DPS20CAPS_PREDICATION))
143 case D3DVS_VERSION(3, 0):
150 HRESULT WINAPI D3DXFindShaderComment(CONST DWORD* byte_code, DWORD fourcc, LPCVOID* data, UINT* size)
152 CONST DWORD *ptr = byte_code;
154 TRACE("(%p, %x, %p, %p)\n", byte_code, fourcc, data, size);
156 if (data) *data = NULL;
160 return D3DERR_INVALIDCALL;
162 while (*++ptr != D3DSIO_END)
164 /* Check if it is a comment */
165 if ((*ptr & D3DSI_OPCODE_MASK) == D3DSIO_COMMENT)
167 DWORD comment_size = (*ptr & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
169 /* Check if this is the comment we are looking for */
170 if (*(ptr + 1) == fourcc)
172 UINT ctab_size = (comment_size - 1) * sizeof(DWORD);
173 LPCVOID ctab_data = ptr + 2;
178 TRACE("Returning comment data at %p with size %d\n", ctab_data, ctab_size);
188 HRESULT WINAPI D3DXAssembleShader(LPCSTR data,
190 CONST D3DXMACRO* defines,
191 LPD3DXINCLUDE include,
193 LPD3DXBUFFER* shader,
194 LPD3DXBUFFER* error_messages)
196 /* Forward to d3dcompiler: the parameter types aren't really different,
197 the actual data types are equivalent */
198 HRESULT hr = D3DAssemble(data, data_len, NULL, (D3D_SHADER_MACRO *)defines,
199 (ID3DInclude *)include, flags, (ID3DBlob **)shader,
200 (ID3DBlob **)error_messages);
202 if(hr == E_FAIL) hr = D3DXERR_INVALIDDATA;
206 /* D3DXInclude private implementation, used to implement
207 D3DXAssembleShaderFromFile from D3DXAssembleShader */
208 /* To be able to correctly resolve include search paths we have to store
209 the pathname of each include file. We store the pathname pointer right
210 before the file data. */
211 static HRESULT WINAPI d3dincludefromfile_open(ID3DXInclude *iface,
212 D3DXINCLUDE_TYPE include_type,
213 LPCSTR filename, LPCVOID parent_data,
214 LPCVOID *data, UINT *bytes) {
215 const char *p, *parent_name = "";
216 char *pathname = NULL;
217 char **buffer = NULL;
221 if(parent_data != NULL)
222 parent_name = *((const char **)parent_data - 1);
224 TRACE("Looking up for include file %s, parent %s\n", debugstr_a(filename), debugstr_a(parent_name));
226 if ((p = strrchr(parent_name, '\\')) || (p = strrchr(parent_name, '/'))) p++;
227 else p = parent_name;
228 pathname = HeapAlloc(GetProcessHeap(), 0, (p - parent_name) + strlen(filename) + 1);
230 return HRESULT_FROM_WIN32(GetLastError());
232 memcpy(pathname, parent_name, p - parent_name);
233 strcpy(pathname + (p - parent_name), filename);
235 file = CreateFileA(pathname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
236 if(file == INVALID_HANDLE_VALUE)
239 TRACE("Include file found at pathname = %s\n", debugstr_a(pathname));
241 size = GetFileSize(file, NULL);
242 if(size == INVALID_FILE_SIZE)
245 buffer = HeapAlloc(GetProcessHeap(), 0, size + sizeof(char *));
249 if(!ReadFile(file, buffer + 1, size, bytes, NULL))
259 HeapFree(GetProcessHeap(), 0, pathname);
260 HeapFree(GetProcessHeap(), 0, buffer);
261 return HRESULT_FROM_WIN32(GetLastError());
264 static HRESULT WINAPI d3dincludefromfile_close(ID3DXInclude *iface, LPCVOID data) {
265 HeapFree(GetProcessHeap(), 0, *((char **)data - 1));
266 HeapFree(GetProcessHeap(), 0, (char **)data - 1);
270 static const struct ID3DXIncludeVtbl D3DXInclude_Vtbl = {
271 d3dincludefromfile_open,
272 d3dincludefromfile_close
275 struct D3DXIncludeImpl {
276 ID3DXInclude ID3DXInclude_iface;
279 HRESULT WINAPI D3DXAssembleShaderFromFileA(LPCSTR filename,
280 CONST D3DXMACRO* defines,
281 LPD3DXINCLUDE include,
283 LPD3DXBUFFER* shader,
284 LPD3DXBUFFER* error_messages)
286 LPWSTR filename_w = NULL;
290 if (!filename) return D3DXERR_INVALIDDATA;
292 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
293 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
294 if (!filename_w) return E_OUTOFMEMORY;
295 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
297 ret = D3DXAssembleShaderFromFileW(filename_w, defines, include, flags, shader, error_messages);
299 HeapFree(GetProcessHeap(), 0, filename_w);
303 HRESULT WINAPI D3DXAssembleShaderFromFileW(LPCWSTR filename,
304 CONST D3DXMACRO* defines,
305 LPD3DXINCLUDE include,
307 LPD3DXBUFFER* shader,
308 LPD3DXBUFFER* error_messages)
313 struct D3DXIncludeImpl includefromfile;
315 if(FAILED(map_view_of_file(filename, &buffer, &len)))
316 return D3DXERR_INVALIDDATA;
320 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
321 include = &includefromfile.ID3DXInclude_iface;
324 hr = D3DXAssembleShader(buffer, len, defines, include, flags,
325 shader, error_messages);
327 UnmapViewOfFile(buffer);
331 HRESULT WINAPI D3DXAssembleShaderFromResourceA(HMODULE module,
333 CONST D3DXMACRO* defines,
334 LPD3DXINCLUDE include,
336 LPD3DXBUFFER* shader,
337 LPD3DXBUFFER* error_messages)
343 if (!(res = FindResourceA(module, resource, (LPCSTR)RT_RCDATA)))
344 return D3DXERR_INVALIDDATA;
345 if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
346 return D3DXERR_INVALIDDATA;
347 return D3DXAssembleShader(buffer, len, defines, include, flags,
348 shader, error_messages);
351 HRESULT WINAPI D3DXAssembleShaderFromResourceW(HMODULE module,
353 CONST D3DXMACRO* defines,
354 LPD3DXINCLUDE include,
356 LPD3DXBUFFER* shader,
357 LPD3DXBUFFER* error_messages)
363 if (!(res = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA)))
364 return D3DXERR_INVALIDDATA;
365 if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
366 return D3DXERR_INVALIDDATA;
367 return D3DXAssembleShader(buffer, len, defines, include, flags,
368 shader, error_messages);
371 HRESULT WINAPI D3DXCompileShader(LPCSTR pSrcData,
373 CONST D3DXMACRO* pDefines,
374 LPD3DXINCLUDE pInclude,
375 LPCSTR pFunctionName,
378 LPD3DXBUFFER* ppShader,
379 LPD3DXBUFFER* ppErrorMsgs,
380 LPD3DXCONSTANTTABLE * ppConstantTable)
382 HRESULT hr = D3DCompile(pSrcData, srcDataLen, NULL,
383 (D3D_SHADER_MACRO *)pDefines, (ID3DInclude *)pInclude,
384 pFunctionName, pProfile, Flags, 0,
385 (ID3DBlob **)ppShader, (ID3DBlob **)ppErrorMsgs);
387 if(SUCCEEDED(hr) && ppConstantTable)
388 return D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*ppShader),
393 HRESULT WINAPI D3DXCompileShaderFromFileA(LPCSTR filename,
394 CONST D3DXMACRO* defines,
395 LPD3DXINCLUDE include,
399 LPD3DXBUFFER* shader,
400 LPD3DXBUFFER* error_messages,
401 LPD3DXCONSTANTTABLE* constant_table)
403 LPWSTR filename_w = NULL;
407 if (!filename) return D3DXERR_INVALIDDATA;
409 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
410 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
411 if (!filename_w) return E_OUTOFMEMORY;
412 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
414 ret = D3DXCompileShaderFromFileW(filename_w, defines, include,
415 entrypoint, profile, flags,
416 shader, error_messages, constant_table);
418 HeapFree(GetProcessHeap(), 0, filename_w);
422 HRESULT WINAPI D3DXCompileShaderFromFileW(LPCWSTR filename,
423 CONST D3DXMACRO* defines,
424 LPD3DXINCLUDE include,
428 LPD3DXBUFFER* shader,
429 LPD3DXBUFFER* error_messages,
430 LPD3DXCONSTANTTABLE* constant_table)
433 DWORD len, filename_len;
435 struct D3DXIncludeImpl includefromfile;
438 if (FAILED(map_view_of_file(filename, &buffer, &len)))
439 return D3DXERR_INVALIDDATA;
443 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
444 include = &includefromfile.ID3DXInclude_iface;
447 filename_len = WideCharToMultiByte(CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL);
448 filename_a = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(char));
451 UnmapViewOfFile(buffer);
452 return E_OUTOFMEMORY;
454 WideCharToMultiByte(CP_ACP, 0, filename, -1, filename_a, filename_len, NULL, NULL);
456 hr = D3DCompile(buffer, len, filename_a, (const D3D_SHADER_MACRO *)defines,
457 (ID3DInclude *)include, entrypoint, profile, flags, 0,
458 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
460 if (SUCCEEDED(hr) && constant_table)
461 hr = D3DXGetShaderConstantTable(ID3DXBuffer_GetBufferPointer(*shader),
464 HeapFree(GetProcessHeap(), 0, filename_a);
465 UnmapViewOfFile(buffer);
469 HRESULT WINAPI D3DXCompileShaderFromResourceA(HMODULE module,
471 CONST D3DXMACRO* defines,
472 LPD3DXINCLUDE include,
476 LPD3DXBUFFER* shader,
477 LPD3DXBUFFER* error_messages,
478 LPD3DXCONSTANTTABLE* constant_table)
484 if (!(res = FindResourceA(module, resource, (LPCSTR)RT_RCDATA)))
485 return D3DXERR_INVALIDDATA;
486 if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
487 return D3DXERR_INVALIDDATA;
488 return D3DXCompileShader(buffer, len, defines, include, entrypoint, profile,
489 flags, shader, error_messages, constant_table);
492 HRESULT WINAPI D3DXCompileShaderFromResourceW(HMODULE module,
494 CONST D3DXMACRO* defines,
495 LPD3DXINCLUDE include,
499 LPD3DXBUFFER* shader,
500 LPD3DXBUFFER* error_messages,
501 LPD3DXCONSTANTTABLE* constant_table)
507 if (!(res = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA)))
508 return D3DXERR_INVALIDDATA;
509 if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
510 return D3DXERR_INVALIDDATA;
511 return D3DXCompileShader(buffer, len, defines, include, entrypoint, profile,
512 flags, shader, error_messages, constant_table);
515 HRESULT WINAPI D3DXPreprocessShader(LPCSTR data,
517 CONST D3DXMACRO* defines,
518 LPD3DXINCLUDE include,
519 LPD3DXBUFFER* shader,
520 LPD3DXBUFFER* error_messages)
522 TRACE("Forward to D3DPreprocess\n");
523 return D3DPreprocess(data, data_len, NULL,
524 (const D3D_SHADER_MACRO *)defines, (ID3DInclude *)include,
525 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
528 HRESULT WINAPI D3DXPreprocessShaderFromFileA(LPCSTR filename,
529 CONST D3DXMACRO* defines,
530 LPD3DXINCLUDE include,
531 LPD3DXBUFFER* shader,
532 LPD3DXBUFFER* error_messages)
534 WCHAR *filename_w = NULL;
538 if (!filename) return D3DXERR_INVALIDDATA;
540 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
541 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
542 if (!filename_w) return E_OUTOFMEMORY;
543 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
545 ret = D3DXPreprocessShaderFromFileW(filename_w, defines, include, shader, error_messages);
547 HeapFree(GetProcessHeap(), 0, filename_w);
551 HRESULT WINAPI D3DXPreprocessShaderFromFileW(LPCWSTR filename,
552 CONST D3DXMACRO* defines,
553 LPD3DXINCLUDE include,
554 LPD3DXBUFFER* shader,
555 LPD3DXBUFFER* error_messages)
560 struct D3DXIncludeImpl includefromfile;
562 if (FAILED(map_view_of_file(filename, &buffer, &len)))
563 return D3DXERR_INVALIDDATA;
567 includefromfile.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
568 include = &includefromfile.ID3DXInclude_iface;
571 hr = D3DPreprocess(buffer, len, NULL,
572 (const D3D_SHADER_MACRO *)defines,
573 (ID3DInclude *) include,
574 (ID3DBlob **)shader, (ID3DBlob **)error_messages);
576 UnmapViewOfFile(buffer);
580 HRESULT WINAPI D3DXPreprocessShaderFromResourceA(HMODULE module,
582 CONST D3DXMACRO* defines,
583 LPD3DXINCLUDE include,
584 LPD3DXBUFFER* shader,
585 LPD3DXBUFFER* error_messages)
591 if (!(res = FindResourceA(module, resource, (LPCSTR)RT_RCDATA)))
592 return D3DXERR_INVALIDDATA;
593 if (FAILED(load_resource_into_memory(module, res, (LPVOID *)&buffer, &len)))
594 return D3DXERR_INVALIDDATA;
595 return D3DXPreprocessShader(buffer, len, defines, include,
596 shader, error_messages);
599 HRESULT WINAPI D3DXPreprocessShaderFromResourceW(HMODULE module,
601 CONST D3DXMACRO* defines,
602 LPD3DXINCLUDE include,
603 LPD3DXBUFFER* shader,
604 LPD3DXBUFFER* error_messages)
610 if (!(res = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
611 return D3DXERR_INVALIDDATA;
612 if (FAILED(load_resource_into_memory(module, res, (void **)&buffer, &len)))
613 return D3DXERR_INVALIDDATA;
614 return D3DXPreprocessShader(buffer, len, defines, include,
615 shader, error_messages);
619 typedef struct ctab_constant {
620 D3DXCONSTANT_DESC desc;
621 struct ctab_constant *members;
624 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl;
626 typedef struct ID3DXConstantTableImpl {
627 ID3DXConstantTable ID3DXConstantTable_iface;
631 D3DXCONSTANTTABLE_DESC desc;
632 ctab_constant *constants;
633 } ID3DXConstantTableImpl;
635 static inline ID3DXConstantTableImpl *impl_from_ID3DXConstantTable(ID3DXConstantTable *iface)
637 return CONTAINING_RECORD(iface, ID3DXConstantTableImpl, ID3DXConstantTable_iface);
640 static inline int is_vertex_shader(DWORD version)
642 return (version & 0xFFFF0000) == 0xFFFE0000;
645 static DWORD calc_bytes(D3DXCONSTANT_DESC *desc)
647 if (desc->RegisterSet != D3DXRS_FLOAT4 && desc->RegisterSet != D3DXRS_SAMPLER)
648 FIXME("Don't know how to calculate Bytes for constants of type %d\n",
651 return 4 * desc->Elements * desc->Rows * desc->Columns;
654 /*** IUnknown methods ***/
655 static HRESULT WINAPI ID3DXConstantTableImpl_QueryInterface(ID3DXConstantTable* iface, REFIID riid, void** ppvObject)
657 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
659 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObject);
661 if (IsEqualGUID(riid, &IID_IUnknown) ||
662 IsEqualGUID(riid, &IID_ID3DXBuffer) ||
663 IsEqualGUID(riid, &IID_ID3DXConstantTable))
665 ID3DXConstantTable_AddRef(iface);
670 WARN("Interface %s not found.\n", debugstr_guid(riid));
672 return E_NOINTERFACE;
675 static ULONG WINAPI ID3DXConstantTableImpl_AddRef(ID3DXConstantTable* iface)
677 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
679 TRACE("(%p)->(): AddRef from %d\n", This, This->ref);
681 return InterlockedIncrement(&This->ref);
684 static ULONG WINAPI ID3DXConstantTableImpl_Release(ID3DXConstantTable* iface)
686 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
687 ULONG ref = InterlockedDecrement(&This->ref);
689 TRACE("(%p)->(): Release from %d\n", This, ref + 1);
693 HeapFree(GetProcessHeap(), 0, This->constants);
694 HeapFree(GetProcessHeap(), 0, This->ctab);
695 HeapFree(GetProcessHeap(), 0, This);
701 /*** ID3DXBuffer methods ***/
702 static LPVOID WINAPI ID3DXConstantTableImpl_GetBufferPointer(ID3DXConstantTable* iface)
704 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
706 TRACE("(%p)->()\n", This);
711 static DWORD WINAPI ID3DXConstantTableImpl_GetBufferSize(ID3DXConstantTable* iface)
713 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
715 TRACE("(%p)->()\n", This);
720 /*** ID3DXConstantTable methods ***/
721 static HRESULT WINAPI ID3DXConstantTableImpl_GetDesc(ID3DXConstantTable* iface, D3DXCONSTANTTABLE_DESC *desc)
723 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
725 TRACE("(%p)->(%p)\n", This, desc);
728 return D3DERR_INVALIDCALL;
730 memcpy(desc, &This->desc, sizeof(This->desc));
735 static HRESULT WINAPI ID3DXConstantTableImpl_GetConstantDesc(ID3DXConstantTable* iface, D3DXHANDLE constant,
736 D3DXCONSTANT_DESC *desc, UINT *count)
738 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
739 ctab_constant *constant_info;
741 TRACE("(%p)->(%p, %p, %p)\n", This, constant, desc, count);
744 return D3DERR_INVALIDCALL;
746 /* Applications can pass the name of the constant in place of the handle */
747 if (!((UINT_PTR)constant >> 16))
748 constant_info = &This->constants[(UINT_PTR)constant - 1];
751 D3DXHANDLE c = ID3DXConstantTable_GetConstantByName(iface, NULL, constant);
753 return D3DERR_INVALIDCALL;
755 constant_info = &This->constants[(UINT_PTR)c - 1];
759 *desc = constant_info->desc;
766 static UINT WINAPI ID3DXConstantTableImpl_GetSamplerIndex(ID3DXConstantTable *iface, D3DXHANDLE constant)
768 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
769 D3DXCONSTANT_DESC desc;
773 TRACE("(%p)->(%p)\n", This, constant);
775 res = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &count);
779 if (desc.RegisterSet != D3DXRS_SAMPLER)
782 return desc.RegisterIndex;
785 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstant(ID3DXConstantTable* iface, D3DXHANDLE constant, UINT index)
787 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
789 TRACE("(%p)->(%p, %d)\n", This, constant, index);
793 FIXME("Only top level constants supported\n");
797 if (index >= This->desc.Constants)
800 return (D3DXHANDLE)(DWORD_PTR)(index + 1);
803 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantByName(ID3DXConstantTable* iface, D3DXHANDLE constant, LPCSTR name)
805 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
808 TRACE("(%p)->(%p, %s)\n", This, constant, name);
815 FIXME("Only top level constants supported\n");
819 for (i = 0; i < This->desc.Constants; i++)
820 if (!strcmp(This->constants[i].desc.Name, name))
821 return (D3DXHANDLE)(DWORD_PTR)(i + 1);
826 static D3DXHANDLE WINAPI ID3DXConstantTableImpl_GetConstantElement(ID3DXConstantTable* iface, D3DXHANDLE constant, UINT index)
828 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
830 FIXME("(%p)->(%p, %d): stub\n", This, constant, index);
835 static HRESULT set_float_array(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device, D3DXHANDLE constant, const void *data,
836 UINT count, D3DXPARAMETER_TYPE type)
838 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
840 D3DXCONSTANT_DESC desc;
842 UINT i, desc_count = 1;
843 float row[4] = {0.0f, 0.0f, 0.0f, 0.0f};
845 hr = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &desc_count);
848 TRACE("ID3DXConstantTable_GetConstantDesc failed: %08x\n", hr);
849 return D3DERR_INVALIDCALL;
852 switch (desc.RegisterSet)
855 for (i = 0; i < count && i < desc.RegisterCount; i++)
857 /* We need the for loop since each IDirect3DDevice9_Set*ShaderConstantF expects a float4 */
861 row[0] = ((float *)data)[i];
864 row[0] = (float)((int *)data)[i];
867 FIXME("Unhandled type passed to set_float_array\n");
868 return D3DERR_INVALIDCALL;
870 if (is_vertex_shader(This->desc.Version))
871 IDirect3DDevice9_SetVertexShaderConstantF(device, desc.RegisterIndex + i, row, 1);
873 IDirect3DDevice9_SetPixelShaderConstantF(device, desc.RegisterIndex + i, row, 1);
877 FIXME("Handle other register sets\n");
884 static HRESULT WINAPI ID3DXConstantTableImpl_SetDefaults(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device)
886 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
888 FIXME("(%p)->(%p): stub\n", This, device);
893 static HRESULT WINAPI ID3DXConstantTableImpl_SetValue(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
894 D3DXHANDLE constant, LPCVOID data, UINT bytes)
896 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
898 FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, data, bytes);
903 static HRESULT WINAPI ID3DXConstantTableImpl_SetBool(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
904 D3DXHANDLE constant, BOOL b)
906 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
908 FIXME("(%p)->(%p, %p, %d): stub\n", This, device, constant, b);
913 static HRESULT WINAPI ID3DXConstantTableImpl_SetBoolArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
914 D3DXHANDLE constant, CONST BOOL* b, UINT count)
916 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
918 FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, b, count);
923 static HRESULT WINAPI ID3DXConstantTableImpl_SetInt(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device, D3DXHANDLE constant, INT n)
925 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
927 TRACE("(%p)->(%p, %p, %d)\n", This, device, constant, n);
929 return ID3DXConstantTable_SetIntArray(iface, device, constant, &n, 1);
932 static HRESULT WINAPI ID3DXConstantTableImpl_SetIntArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
933 D3DXHANDLE constant, CONST INT* n, UINT count)
935 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
937 TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, n, count);
939 return set_float_array(iface, device, constant, n, count, D3DXPT_INT);
942 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloat(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
943 D3DXHANDLE constant, FLOAT f)
945 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
947 TRACE("(%p)->(%p, %p, %f)\n", This, device, constant, f);
949 return ID3DXConstantTable_SetFloatArray(iface, device, constant, &f, 1);
952 static HRESULT WINAPI ID3DXConstantTableImpl_SetFloatArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
953 D3DXHANDLE constant, CONST FLOAT *f, UINT count)
955 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
957 TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, f, count);
959 return set_float_array(iface, device, constant, f, count, D3DXPT_FLOAT);
962 static HRESULT WINAPI ID3DXConstantTableImpl_SetVector(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
963 D3DXHANDLE constant, CONST D3DXVECTOR4 *vector)
965 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
967 TRACE("(%p)->(%p, %p, %p)\n", This, device, constant, vector);
969 return ID3DXConstantTable_SetVectorArray(iface, device, constant, vector, 1);
972 static HRESULT WINAPI ID3DXConstantTableImpl_SetVectorArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
973 D3DXHANDLE constant, CONST D3DXVECTOR4 *vector, UINT count)
975 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
977 D3DXCONSTANT_DESC desc;
981 TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, vector, count);
983 hr = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &desc_count);
986 TRACE("ID3DXConstantTable_GetConstantDesc failed: %08x\n", hr);
987 return D3DERR_INVALIDCALL;
990 switch (desc.RegisterSet)
993 if (is_vertex_shader(This->desc.Version))
994 IDirect3DDevice9_SetVertexShaderConstantF(device, desc.RegisterIndex, (float *)vector,
995 min(desc.RegisterCount, count));
997 IDirect3DDevice9_SetPixelShaderConstantF(device, desc.RegisterIndex, (float *)vector,
998 min(desc.RegisterCount, count));
1001 FIXME("Handle other register sets\n");
1008 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrix(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1009 D3DXHANDLE constant, CONST D3DXMATRIX *matrix)
1011 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1013 TRACE("(%p)->(%p, %p, %p)\n", This, device, constant, matrix);
1015 return ID3DXConstantTable_SetMatrixArray(iface, device, constant, matrix, 1);
1018 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixArray(ID3DXConstantTable *iface, LPDIRECT3DDEVICE9 device,
1019 D3DXHANDLE constant, CONST D3DXMATRIX *matrix, UINT count)
1021 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1023 D3DXCONSTANT_DESC desc;
1025 UINT i, desc_count = 1;
1028 TRACE("(%p)->(%p, %p, %p, %d)\n", This, device, constant, matrix, count);
1030 hr = ID3DXConstantTable_GetConstantDesc(iface, constant, &desc, &desc_count);
1033 TRACE("ID3DXConstantTable_GetConstantDesc failed: %08x\n", hr);
1034 return D3DERR_INVALIDCALL;
1037 switch (desc.RegisterSet)
1040 /* i * 4 + 3 is the last register we set. The conditional makes sure that we don't access
1041 registers we're not supposed to */
1042 for (i = 0; i < count && i * 4 + 3 < desc.RegisterCount; i++)
1044 if (desc.Class == D3DXPC_MATRIX_ROWS)
1047 D3DXMatrixTranspose(&temp, &matrix[i]);
1049 if (is_vertex_shader(This->desc.Version))
1050 IDirect3DDevice9_SetVertexShaderConstantF(device, desc.RegisterIndex + i * 4, &temp.u.s._11, 4);
1052 IDirect3DDevice9_SetPixelShaderConstantF(device, desc.RegisterIndex + i * 4, &temp.u.s._11, 4);
1056 FIXME("Handle other register sets\n");
1063 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixPointerArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
1064 D3DXHANDLE constant, CONST D3DXMATRIX** matrix, UINT count)
1066 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1068 FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, matrix, count);
1073 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTranspose(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
1074 D3DXHANDLE constant, CONST D3DXMATRIX* matrix)
1076 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1078 FIXME("(%p)->(%p, %p, %p): stub\n", This, device, constant, matrix);
1083 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposeArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
1084 D3DXHANDLE constant, CONST D3DXMATRIX* matrix, UINT count)
1086 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1088 FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, matrix, count);
1093 static HRESULT WINAPI ID3DXConstantTableImpl_SetMatrixTransposePointerArray(ID3DXConstantTable* iface, LPDIRECT3DDEVICE9 device,
1094 D3DXHANDLE constant, CONST D3DXMATRIX** matrix, UINT count)
1096 ID3DXConstantTableImpl *This = impl_from_ID3DXConstantTable(iface);
1098 FIXME("(%p)->(%p, %p, %p, %d): stub\n", This, device, constant, matrix, count);
1103 static const struct ID3DXConstantTableVtbl ID3DXConstantTable_Vtbl =
1105 /*** IUnknown methods ***/
1106 ID3DXConstantTableImpl_QueryInterface,
1107 ID3DXConstantTableImpl_AddRef,
1108 ID3DXConstantTableImpl_Release,
1109 /*** ID3DXBuffer methods ***/
1110 ID3DXConstantTableImpl_GetBufferPointer,
1111 ID3DXConstantTableImpl_GetBufferSize,
1112 /*** ID3DXConstantTable methods ***/
1113 ID3DXConstantTableImpl_GetDesc,
1114 ID3DXConstantTableImpl_GetConstantDesc,
1115 ID3DXConstantTableImpl_GetSamplerIndex,
1116 ID3DXConstantTableImpl_GetConstant,
1117 ID3DXConstantTableImpl_GetConstantByName,
1118 ID3DXConstantTableImpl_GetConstantElement,
1119 ID3DXConstantTableImpl_SetDefaults,
1120 ID3DXConstantTableImpl_SetValue,
1121 ID3DXConstantTableImpl_SetBool,
1122 ID3DXConstantTableImpl_SetBoolArray,
1123 ID3DXConstantTableImpl_SetInt,
1124 ID3DXConstantTableImpl_SetIntArray,
1125 ID3DXConstantTableImpl_SetFloat,
1126 ID3DXConstantTableImpl_SetFloatArray,
1127 ID3DXConstantTableImpl_SetVector,
1128 ID3DXConstantTableImpl_SetVectorArray,
1129 ID3DXConstantTableImpl_SetMatrix,
1130 ID3DXConstantTableImpl_SetMatrixArray,
1131 ID3DXConstantTableImpl_SetMatrixPointerArray,
1132 ID3DXConstantTableImpl_SetMatrixTranspose,
1133 ID3DXConstantTableImpl_SetMatrixTransposeArray,
1134 ID3DXConstantTableImpl_SetMatrixTransposePointerArray
1137 static HRESULT parse_ctab_constant_type(const D3DXSHADER_TYPEINFO *type, ctab_constant *constant)
1139 constant->desc.Class = type->Class;
1140 constant->desc.Type = type->Type;
1141 constant->desc.Rows = type->Rows;
1142 constant->desc.Columns = type->Columns;
1143 constant->desc.Elements = type->Elements;
1144 constant->desc.StructMembers = type->StructMembers;
1145 constant->desc.Bytes = calc_bytes(&constant->desc);
1147 TRACE("class = %d, type = %d, rows = %d, columns = %d, elements = %d, struct_members = %d\n",
1148 constant->desc.Class, constant->desc.Type, constant->desc.Elements,
1149 constant->desc.Rows, constant->desc.Columns, constant->desc.StructMembers);
1151 if ((constant->desc.Class == D3DXPC_STRUCT) && constant->desc.StructMembers)
1153 FIXME("Struct not supported yet\n");
1160 HRESULT WINAPI D3DXGetShaderConstantTableEx(CONST DWORD* byte_code,
1162 LPD3DXCONSTANTTABLE* constant_table)
1164 ID3DXConstantTableImpl* object = NULL;
1168 const D3DXSHADER_CONSTANTTABLE* ctab_header;
1169 D3DXSHADER_CONSTANTINFO* constant_info;
1172 TRACE("(%p, %x, %p)\n", byte_code, flags, constant_table);
1174 if (!byte_code || !constant_table)
1175 return D3DERR_INVALIDCALL;
1177 hr = D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), &data, &size);
1179 return D3DXERR_INVALIDDATA;
1181 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1184 ERR("Out of memory\n");
1185 return E_OUTOFMEMORY;
1188 object->ID3DXConstantTable_iface.lpVtbl = &ID3DXConstantTable_Vtbl;
1191 if (size < sizeof(D3DXSHADER_CONSTANTTABLE))
1193 hr = D3DXERR_INVALIDDATA;
1197 object->ctab = HeapAlloc(GetProcessHeap(), 0, size);
1200 ERR("Out of memory\n");
1204 object->size = size;
1205 memcpy(object->ctab, data, object->size);
1207 ctab_header = (const D3DXSHADER_CONSTANTTABLE*)data;
1208 if (ctab_header->Size != sizeof(D3DXSHADER_CONSTANTTABLE))
1210 hr = D3DXERR_INVALIDDATA;
1213 object->desc.Creator = ctab_header->Creator ? object->ctab + ctab_header->Creator : NULL;
1214 object->desc.Version = ctab_header->Version;
1215 object->desc.Constants = ctab_header->Constants;
1216 if (object->desc.Creator)
1217 TRACE("Creator = %s\n", object->desc.Creator);
1218 TRACE("Version = %x\n", object->desc.Version);
1219 TRACE("Constants = %d\n", ctab_header->Constants);
1220 if (ctab_header->Target)
1221 TRACE("Target = %s\n", object->ctab + ctab_header->Target);
1223 if (object->desc.Constants > 65535)
1225 FIXME("Too many constants (%u)\n", object->desc.Constants);
1230 object->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1231 sizeof(*object->constants) * object->desc.Constants);
1232 if (!object->constants)
1234 ERR("Out of memory\n");
1239 constant_info = (LPD3DXSHADER_CONSTANTINFO)(object->ctab + ctab_header->ConstantInfo);
1240 for (i = 0; i < ctab_header->Constants; i++)
1242 TRACE("name = %s\n", object->ctab + constant_info[i].Name);
1243 object->constants[i].desc.Name = object->ctab + constant_info[i].Name;
1244 object->constants[i].desc.RegisterSet = constant_info[i].RegisterSet;
1245 object->constants[i].desc.RegisterIndex = constant_info[i].RegisterIndex;
1246 object->constants[i].desc.RegisterCount = constant_info[i].RegisterCount;
1247 object->constants[i].desc.DefaultValue = object->ctab + constant_info[i].DefaultValue;
1249 hr = parse_ctab_constant_type((LPD3DXSHADER_TYPEINFO)(object->ctab + constant_info[i].TypeInfo),
1250 &object->constants[i]);
1255 *constant_table = &object->ID3DXConstantTable_iface;
1261 HeapFree(GetProcessHeap(), 0, object->constants);
1262 HeapFree(GetProcessHeap(), 0, object->ctab);
1263 HeapFree(GetProcessHeap(), 0, object);
1268 HRESULT WINAPI D3DXGetShaderConstantTable(CONST DWORD* byte_code,
1269 LPD3DXCONSTANTTABLE* constant_table)
1271 TRACE("(%p, %p): Forwarded to D3DXGetShaderConstantTableEx\n", byte_code, constant_table);
1273 return D3DXGetShaderConstantTableEx(byte_code, 0, constant_table);
1276 HRESULT WINAPI D3DXGetShaderSamplers(CONST DWORD *byte_code, LPCSTR *samplers, UINT *count)
1279 UINT i, sampler_count = 0;
1282 const D3DXSHADER_CONSTANTTABLE *ctab_header;
1283 const D3DXSHADER_CONSTANTINFO *constant_info;
1285 TRACE("byte_code %p, samplers %p, count %p\n", byte_code, samplers, count);
1287 if (count) *count = 0;
1289 hr = D3DXFindShaderComment(byte_code, MAKEFOURCC('C','T','A','B'), (LPCVOID *)&data, &size);
1290 if (hr != D3D_OK) return D3D_OK;
1292 if (size < sizeof(D3DXSHADER_CONSTANTTABLE)) return D3D_OK;
1294 ctab_header = (const D3DXSHADER_CONSTANTTABLE *)data;
1295 if (ctab_header->Size != sizeof(*ctab_header)) return D3D_OK;
1297 constant_info = (D3DXSHADER_CONSTANTINFO *)(data + ctab_header->ConstantInfo);
1298 for (i = 0; i < ctab_header->Constants; i++)
1300 const D3DXSHADER_TYPEINFO *type;
1302 TRACE("name = %s\n", data + constant_info[i].Name);
1304 type = (D3DXSHADER_TYPEINFO *)(data + constant_info[i].TypeInfo);
1306 if (type->Type == D3DXPT_SAMPLER
1307 || type->Type == D3DXPT_SAMPLER1D
1308 || type->Type == D3DXPT_SAMPLER2D
1309 || type->Type == D3DXPT_SAMPLER3D
1310 || type->Type == D3DXPT_SAMPLERCUBE)
1312 if (samplers) samplers[sampler_count] = data + constant_info[i].Name;
1318 TRACE("Found %u samplers\n", sampler_count);
1320 if (count) *count = sampler_count;