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
)
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):
53 case D3DPS_VERSION(1, 2):
56 case D3DPS_VERSION(1, 3):
59 case D3DPS_VERSION(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
))
72 if ((caps
.PS20Caps
.NumTemps
>=32) &&
73 (caps
.PS20Caps
.Caps
&D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT
))
79 case D3DPS_VERSION(3, 0):
86 UINT WINAPI
D3DXGetShaderSize(const DWORD
*byte_code
)
88 const DWORD
*ptr
= byte_code
;
90 TRACE("byte_code %p\n", byte_code
);
94 /* Look for the END token, skipping the VERSION token */
95 while (*++ptr
!= D3DSIO_END
)
98 if ((*ptr
& D3DSI_OPCODE_MASK
) == D3DSIO_COMMENT
)
100 ptr
+= ((*ptr
& D3DSI_COMMENTSIZE_MASK
) >> D3DSI_COMMENTSIZE_SHIFT
);
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
)
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):
130 case D3DVS_VERSION(2, 0):
131 if ((caps
.VS20Caps
.NumTemps
>=13) &&
132 (caps
.VS20Caps
.DynamicFlowControlDepth
==24) &&
133 (caps
.VS20Caps
.Caps
&D3DPS20CAPS_PREDICATION
))
138 case D3DVS_VERSION(3, 0):
145 HRESULT WINAPI
D3DXFindShaderComment(const DWORD
*byte_code
, DWORD fourcc
, const void **data
, UINT
*size
)
147 const DWORD
*ptr
= byte_code
;
150 TRACE("byte_code %p, fourcc %x, data %p, size %p\n", byte_code
, fourcc
, data
, size
);
152 if (data
) *data
= NULL
;
155 if (!byte_code
) return D3DERR_INVALIDCALL
;
157 version
= *ptr
>> 16;
158 if (version
!= 0x4658 /* FX */
159 && version
!= 0x5458 /* TX */
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;
185 TRACE("Returning comment data at %p with size %d\n", ctab_data
, ctab_size
);
195 HRESULT WINAPI
D3DXAssembleShader(const char *data
, UINT data_len
, const D3DXMACRO
*defines
,
196 ID3DXInclude
*include
, DWORD flags
, ID3DXBuffer
**shader
, ID3DXBuffer
**error_messages
)
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
;
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
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
;
242 parent_name
= *((const char **)parent_data
- 1);
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
, '\\')))
256 pathname
= HeapAlloc(GetProcessHeap(), 0, (p
- parent_name
) + strlen(filename
) + 1);
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
);
270 file
= CreateFileA(pathname
, GENERIC_READ
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
271 if(file
== INVALID_HANDLE_VALUE
)
274 TRACE("Include file found at pathname = %s\n", debugstr_a(pathname
));
276 size
= GetFileSize(file
, NULL
);
277 if(size
== INVALID_FILE_SIZE
)
280 buffer
= HeapAlloc(GetProcessHeap(), 0, size
+ sizeof(char *));
284 if(!ReadFile(file
, buffer
+ 1, size
, bytes
, NULL
))
289 main_file_data
= *data
;
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
;
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
)
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
);
339 HRESULT WINAPI
D3DXAssembleShaderFromFileW(const WCHAR
*filename
, const D3DXMACRO
*defines
,
340 ID3DXInclude
*include
, DWORD flags
, ID3DXBuffer
**shader
, ID3DXBuffer
**error_messages
)
345 struct d3dx_include_from_file include_from_file
;
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
);
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));
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
);
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
);
380 HRESULT WINAPI
D3DXAssembleShaderFromResourceA(HMODULE module
, const char *resource
, const D3DXMACRO
*defines
,
381 ID3DXInclude
*include
, DWORD flags
, ID3DXBuffer
**shader
, ID3DXBuffer
**error_messages
)
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
)
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
)
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
);
438 ID3DXBuffer_Release(*shader
);
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
)
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
);
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
)
479 DWORD len
, filename_len
;
481 struct d3dx_include_from_file include_from_file
;
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
);
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));
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
);
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
),
521 ID3DXInclude_Close(include
, buffer
);
522 LeaveCriticalSection(&from_file_mutex
);
523 HeapFree(GetProcessHeap(), 0, filename_a
);
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
)
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
)
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
;
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
);
603 HRESULT WINAPI
D3DXPreprocessShaderFromFileW(const WCHAR
*filename
, const D3DXMACRO
*defines
,
604 ID3DXInclude
*include
, ID3DXBuffer
**shader
, ID3DXBuffer
**error_messages
)
609 struct d3dx_include_from_file include_from_file
;
612 TRACE("filename %s, defines %p, include %p, shader %p, error_messages %p.\n",
613 debugstr_w(filename
), defines
, include
, shader
, error_messages
);
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));
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
);
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
);
647 HRESULT WINAPI
D3DXPreprocessShaderFromResourceA(HMODULE module
, const char *resource
, const D3DXMACRO
*defines
,
648 ID3DXInclude
*include
, ID3DXBuffer
**shader
, ID3DXBuffer
**error_messages
)
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
)
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
;
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
)
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
)
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
;
759 return get_constant_by_name(NULL
, c
, part
);
762 return get_constant_element_by_name(c
, part
);
765 TRACE("Returning parameter %p\n", c
);
769 FIXME("Unhandled case \"%c\"\n", *--part
);
774 TRACE("Constant not found\n");
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
;
785 TRACE("table %p, constant %p, name %s\n", table
, constant
, debugstr_a(name
));
787 if (!name
|| !*name
) return NULL
;
791 count
= table
->desc
.Constants
;
792 handles
= table
->constants
;
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
))
810 return get_constant_by_name(NULL
, &handles
[i
], part
);
813 return get_constant_element_by_name(&handles
[i
], part
);
816 TRACE("Returning parameter %p\n", &handles
[i
]);
822 TRACE("Constant not found\n");
826 static struct ctab_constant
*is_valid_sub_constant(struct ctab_constant
*parent
, D3DXHANDLE handle
)
828 struct ctab_constant
*c
;
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
);
847 static inline struct ctab_constant
*get_valid_constant(struct ID3DXConstantTableImpl
*table
, D3DXHANDLE handle
)
849 struct ctab_constant
*c
;
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
);
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
);
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);
903 free_constant_table(This
);
904 HeapFree(GetProcessHeap(), 0, This
);
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
);
920 static DWORD WINAPI
ID3DXConstantTableImpl_GetBufferSize(ID3DXConstantTable
*iface
)
922 struct ID3DXConstantTableImpl
*This
= impl_from_ID3DXConstantTable(iface
);
924 TRACE("(%p)->()\n", This
);
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
);
937 return D3DERR_INVALIDCALL
;
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
);
961 WARN("Invalid argument specified\n");
962 return D3DERR_INVALIDCALL
;
965 if (desc
) *desc
= c
->desc
;
966 if (count
) *count
= 1;
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");
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
);
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
);
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");
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");
1051 static inline DWORD
get_index(const void **indata
, UINT index
, BOOL 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
,
1066 D3DXCONSTANT_DESC
*desc
= &constant
->desc
;
1067 UINT l
, i
, regcount
= 1, regsize
= 1, cin
= 1, rin
= 1, ret
, last
= 0;
1070 /* size too small to set anything */
1071 if (*size
< desc
->Rows
* desc
->Columns
)
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
);
1102 last
+= set(table
, device
, &constant
->constants
[0], indata
, intype
, size
, incol
, inclass
,
1103 index
+ last
, is_pointer
);
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
);
1118 min
= *(D3DXMATRIX
*)(indata
[index
/ 16]);
1120 min
= **(D3DXMATRIX
**)indata
;
1122 D3DXMatrixTranspose(&mat
, &min
);
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
);
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
);
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)
1171 *size
= *size
< 12 ? 0 : *size
- 12;
1178 switch (desc
->Class
)
1182 case D3DXPC_MATRIX_ROWS
:
1183 regcount
= min(desc
->RegisterCount
, desc
->Rows
);
1184 if (inclass
== D3DXPC_MATRIX_ROWS
) cin
= incol
;
1186 regsize
= desc
->Columns
;
1189 case D3DXPC_MATRIX_COLUMNS
:
1190 regcount
= min(desc
->RegisterCount
, desc
->Columns
);
1191 if (inclass
== D3DXPC_MATRIX_ROWS
) rin
= incol
;
1193 regsize
= desc
->Rows
;
1197 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc
->Class
));
1201 /* specific stuff for different in types */
1205 ret
= desc
->Columns
* desc
->Rows
;
1206 *size
-= desc
->Columns
* desc
->Rows
;
1210 switch (desc
->Class
)
1212 case D3DXPC_MATRIX_ROWS
:
1213 if (*size
< regcount
* 4)
1219 *size
-= 4 * regcount
;
1222 case D3DXPC_MATRIX_COLUMNS
:
1224 *size
-= 4 * regcount
;
1238 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc
->Class
));
1243 case D3DXPC_MATRIX_ROWS
:
1244 switch (desc
->Class
)
1246 case D3DXPC_MATRIX_ROWS
:
1247 case D3DXPC_MATRIX_COLUMNS
:
1265 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc
->Class
));
1271 case D3DXPC_MATRIX_COLUMNS
:
1272 switch (desc
->Class
)
1274 case D3DXPC_MATRIX_ROWS
:
1275 case D3DXPC_MATRIX_COLUMNS
:
1293 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(desc
->Class
));
1300 FIXME("Unhandled variable class %s\n", debug_d3dxparameter_class(inclass
));
1304 /* set the registers */
1305 switch (desc
->RegisterSet
)
1308 regcount
= min(desc
->RegisterCount
, desc
->Columns
* desc
->Rows
);
1310 for (i
= 0; i
< regcount
; ++i
)
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);
1320 IDirect3DDevice9_SetPixelShaderConstantB(device
, desc
->RegisterIndex
+ i
, &out
, 1);
1322 if (++l
>= regsize
) l
= 0;
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);
1341 IDirect3DDevice9_SetPixelShaderConstantI(device
, desc
->RegisterIndex
+ i
, vec
, 1);
1346 for (i
= 0; i
< regcount
; ++i
)
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);
1360 IDirect3DDevice9_SetPixelShaderConstantF(device
, desc
->RegisterIndex
+ i
, vec
, 1);
1365 FIXME("Unhandled register set %s\n", debug_d3dxparameter_registerset(desc
->RegisterSet
));
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
);
1378 WARN("Invalid argument specified\n");
1379 return D3DERR_INVALIDCALL
;
1382 switch (c
->desc
.Class
)
1385 set(table
, device
, c
, &indata
, intype
, &count
, c
->desc
.Columns
, D3DXPC_SCALAR
, 0, FALSE
);
1389 case D3DXPC_MATRIX_ROWS
:
1390 case D3DXPC_MATRIX_COLUMNS
:
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
);
1407 WARN("Invalid argument specified\n");
1408 return D3DERR_INVALIDCALL
;
1411 switch (c
->desc
.Class
)
1415 case D3DXPC_MATRIX_ROWS
:
1416 case D3DXPC_MATRIX_COLUMNS
:
1418 set(table
, device
, c
, &indata
, intype
, &count
, c
->desc
.Columns
, D3DXPC_SCALAR
, 0, FALSE
);
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
);
1435 WARN("Invalid argument specified\n");
1436 return D3DERR_INVALIDCALL
;
1439 switch (c
->desc
.Class
)
1444 set(table
, device
, c
, &indata
, intype
, &count
, 4, D3DXPC_VECTOR
, 0, FALSE
);
1447 case D3DXPC_MATRIX_ROWS
:
1448 case D3DXPC_MATRIX_COLUMNS
:
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
);
1464 WARN("Invalid argument specified\n");
1465 return D3DERR_INVALIDCALL
;
1468 switch (c
->desc
.Class
)
1472 case D3DXPC_MATRIX_ROWS
:
1473 case D3DXPC_MATRIX_COLUMNS
:
1476 set(table
, device
, c
, &indata
, intype
, &count
, 4, D3DXPC_VECTOR
, 0, FALSE
);
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
);
1492 WARN("Invalid argument specified\n");
1493 return D3DERR_INVALIDCALL
;
1496 switch (c
->desc
.Class
)
1500 case D3DXPC_MATRIX_ROWS
:
1501 case D3DXPC_MATRIX_COLUMNS
:
1504 set(table
, device
, c
, &indata
, D3DXPT_FLOAT
, &count
, 4,
1505 transpose
? D3DXPC_MATRIX_ROWS
: D3DXPC_MATRIX_COLUMNS
, 0, FALSE
);
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
);
1521 WARN("Invalid argument specified\n");
1522 return D3DERR_INVALIDCALL
;
1525 switch (c
->desc
.Class
)
1529 case D3DXPC_MATRIX_ROWS
:
1530 case D3DXPC_MATRIX_COLUMNS
:
1533 set(table
, device
, c
, indata
, D3DXPT_FLOAT
, &count
, 4,
1534 transpose
? D3DXPC_MATRIX_ROWS
: D3DXPC_MATRIX_COLUMNS
, 0, TRUE
);
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
);
1549 TRACE("iface %p, device %p\n", iface
, 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
;
1562 if (!desc
->DefaultValue
)
1565 switch (desc
->RegisterSet
)
1568 if (is_vertex_shader(This
->desc
.Version
))
1569 hr
= IDirect3DDevice9_SetVertexShaderConstantB(device
, desc
->RegisterIndex
, desc
->DefaultValue
,
1570 desc
->RegisterCount
);
1572 hr
= IDirect3DDevice9_SetPixelShaderConstantB(device
, desc
->RegisterIndex
, desc
->DefaultValue
,
1573 desc
->RegisterCount
);
1577 if (is_vertex_shader(This
->desc
.Version
))
1578 hr
= IDirect3DDevice9_SetVertexShaderConstantI(device
, desc
->RegisterIndex
, desc
->DefaultValue
,
1579 desc
->RegisterCount
);
1581 hr
= IDirect3DDevice9_SetPixelShaderConstantI(device
, desc
->RegisterIndex
, desc
->DefaultValue
,
1582 desc
->RegisterCount
);
1586 if (is_vertex_shader(This
->desc
.Version
))
1587 hr
= IDirect3DDevice9_SetVertexShaderConstantF(device
, desc
->RegisterIndex
, desc
->DefaultValue
,
1588 desc
->RegisterCount
);
1590 hr
= IDirect3DDevice9_SetPixelShaderConstantF(device
, desc
->RegisterIndex
, desc
->DefaultValue
,
1591 desc
->RegisterCount
);
1595 FIXME("Unhandled register set %s\n", debug_d3dxparameter_registerset(desc
->RegisterSet
));
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
;
1624 switch (desc
->Class
)
1628 case D3DXPC_MATRIX_ROWS
:
1629 case D3DXPC_MATRIX_COLUMNS
:
1632 set(table
, device
, c
, &data
, desc
->Type
, &bytes
, desc
->Columns
, D3DXPC_SCALAR
, 0, FALSE
);
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
;
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
;
1854 constant
->constants
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*constant
->constants
) * count
);
1855 if (!constant
->constants
)
1857 ERR("Out of memory\n");
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
);
1870 size
+= constant
->constants
[i
].desc
.RegisterCount
;
1875 WORD offsetdiff
= type
->Columns
* type
->Rows
;
1878 size
= type
->Columns
* type
->Rows
;
1883 fail
= type
->Class
!= D3DXPC_SCALAR
&& type
->Class
!= D3DXPC_VECTOR
1884 && type
->Class
!= D3DXPC_MATRIX_ROWS
&& type
->Class
!= D3DXPC_MATRIX_COLUMNS
;
1889 switch (type
->Class
)
1895 offsetdiff
= type
->Rows
* 4;
1898 case D3DXPC_MATRIX_ROWS
:
1899 offsetdiff
= type
->Rows
* 4;
1903 case D3DXPC_MATRIX_COLUMNS
:
1904 offsetdiff
= type
->Columns
* 4;
1905 size
= type
->Columns
;
1914 case D3DXRS_SAMPLER
:
1916 fail
= type
->Class
!= D3DXPC_OBJECT
;
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
;
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
;
1953 HRESULT WINAPI
D3DXGetShaderConstantTableEx(const DWORD
*byte_code
, DWORD flags
, ID3DXConstantTable
**constant_table
)
1955 struct ID3DXConstantTableImpl
*object
= NULL
;
1959 const D3DXSHADER_CONSTANTTABLE
*ctab_header
;
1960 const D3DXSHADER_CONSTANTINFO
*constant_info
;
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");
1979 if (flags
) FIXME("Flags (%#x) are not handled, yet!\n", flags
);
1981 hr
= D3DXFindShaderComment(byte_code
, MAKEFOURCC('C','T','A','B'), &data
, &size
);
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
));
2003 return E_OUTOFMEMORY
;
2005 object
->ID3DXConstantTable_iface
.lpVtbl
= &ID3DXConstantTable_Vtbl
;
2008 object
->ctab
= HeapAlloc(GetProcessHeap(), 0, size
);
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");
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
);
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
;
2065 free_constant_table(object
);
2066 HeapFree(GetProcessHeap(), 0, object
);
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
;
2083 struct IDirect3DDevice9
*device
;
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
);
2104 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
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
);
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
);
2128 IDirect3DDevice9_Release(linker
->device
);
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
);
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
);
2154 static UINT WINAPI
d3dx9_fragment_linker_GetNumberOfFragments(ID3DXFragmentLinker
*iface
)
2156 FIXME("iface %p: stub.\n", iface
);
2161 static D3DXHANDLE WINAPI
d3dx9_fragment_linker_GetFragmentHandleByIndex(ID3DXFragmentLinker
*iface
, UINT index
)
2163 FIXME("iface %p, index %u: stub.\n", iface
, index
);
2168 static D3DXHANDLE WINAPI
d3dx9_fragment_linker_GetFragmentHandleByName(ID3DXFragmentLinker
*iface
,
2171 FIXME("iface %p, name %s: stub.\n", iface
, debugstr_a(name
));
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
);
2184 static HRESULT WINAPI
d3dx9_fragment_linker_AddFragments(ID3DXFragmentLinker
*iface
, const DWORD
*fragments
)
2186 FIXME("iface %p, fragments %p: stub.\n", iface
, fragments
);
2191 static HRESULT WINAPI
d3dx9_fragment_linker_GetAllFragments(ID3DXFragmentLinker
*iface
, ID3DXBuffer
**buffer
)
2193 FIXME("iface %p, buffer %p: stub.\n", iface
, buffer
);
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
);
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
);
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
);
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
);
2236 static HRESULT WINAPI
d3dx9_fragment_linker_ClearCache(ID3DXFragmentLinker
*iface
)
2238 FIXME("iface %p: stub.\n", iface
);
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
));
2271 return E_OUTOFMEMORY
;
2273 object
->ID3DXFragmentLinker_iface
.lpVtbl
= &d3dx9_fragment_linker_vtbl
;
2276 IDirect3DDevice9_AddRef(device
);
2277 object
->device
= device
;
2278 object
->flags
= flags
;
2280 *linker
= &object
->ID3DXFragmentLinker_iface
;
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;
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
)
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
;
2333 TRACE("Found %u samplers\n", sampler_count
);
2335 if (count
) *count
= sampler_count
;
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
;
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
);
2369 WARN("Interface %s not found.\n", debugstr_guid(riid
));
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
);
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
);
2393 HeapFree(GetProcessHeap(), 0, texture_shader
);
2399 static HRESULT WINAPI
d3dx9_texture_shader_GetFunction(ID3DXTextureShader
*iface
, struct ID3DXBuffer
**function
)
2401 FIXME("iface %p, function %p stub.\n", iface
, function
);
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
);
2413 static HRESULT WINAPI
d3dx9_texture_shader_GetDesc(ID3DXTextureShader
*iface
, D3DXCONSTANTTABLE_DESC
*desc
)
2415 FIXME("iface %p, desc %p stub.\n", iface
, desc
);
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
);
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
);
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
));
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
);
2448 static HRESULT WINAPI
d3dx9_texture_shader_SetDefaults(ID3DXTextureShader
*iface
)
2450 FIXME("iface %p stub.\n", iface
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
));
2603 return E_OUTOFMEMORY
;
2605 object
->ID3DXTextureShader_iface
.lpVtbl
= &d3dx9_texture_shader_vtbl
;
2608 *texture_shader
= &object
->ID3DXTextureShader_iface
;
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
;
2622 return (*byte_code
& D3DSI_INSTLENGTH_MASK
) >> D3DSI_INSTLENGTH_SHIFT
;
2627 ERR("Unexpected END token.\n");
2636 while (*byte_code
& 0x80000000)
2646 static HRESULT
get_shader_semantics(const DWORD
*byte_code
, D3DXSEMANTIC
*semantics
, UINT
*count
, BOOL output
)
2648 static const D3DDECLUSAGE regtype_usage
[] =
2653 D3DDECLUSAGE_TEXCOORD
,
2656 D3DDECLUSAGE_TEXCOORD
,
2661 static const D3DDECLUSAGE rast_usage
[] =
2663 D3DDECLUSAGE_POSITION
,
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;
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
);
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
);
2706 usage
= regtype_usage
[reg_type
];
2709 semantics
[i
].Usage
= usage
;
2710 semantics
[i
].UsageIndex
= index
;
2714 else if ((!output
&& reg_type
== D3DSPR_INPUT
) || (output
&& reg_type
== D3DSPR_OUTPUT
))
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
;
2729 unsigned int len
= get_instr_length(byte_code
, major
, minor
) + 1;
2731 switch (*byte_code
& 0xffff)
2733 case D3DSIO_COMMENT
:
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
)
2759 else if (reg_type
== D3DSPR_TEXTURE
|| reg_type
== D3DSPR_OUTPUT
)
2760 texcoords
|= 1u << index
;
2762 colors
|= 1u << index
;
2770 byte_code
+= get_instr_length(byte_code
, major
, minor
) + 1;
2783 semantics
[i
].Usage
= D3DDECLUSAGE_TEXCOORD
;
2784 semantics
[i
].UsageIndex
= j
;
2798 semantics
[i
].Usage
= D3DDECLUSAGE_COLOR
;
2799 semantics
[i
].UsageIndex
= j
;
2811 if (j
>= ARRAY_SIZE(rast_usage
))
2813 WARN("Invalid RASTOUT register index.\n");
2818 usage
= rast_usage
[j
];
2822 semantics
[i
].Usage
= usage
;
2823 semantics
[i
].UsageIndex
= 0;
2834 semantics
[i
].Usage
= D3DDECLUSAGE_DEPTH
;
2835 semantics
[i
].UsageIndex
= 0;
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
);