regedit: An English (United States) spelling fix.
[wine/multimedia.git] / dlls / d3dcompiler_43 / reflection.c
blob5dd7c381e07cce32c00c140db264f41609ff9c67
1 /*
2 * Copyright 2009 Henri Verbeet for CodeWeavers
3 * Copyright 2010 Rico Schüller
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include "d3dcompiler_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
28 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE
30 D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6 = 6,
31 D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7 = 7,
34 #define D3DCOMPILER_SHADER_TARGET_VERSION_MASK 0xffff
35 #define D3DCOMPILER_SHADER_TARGET_SHADERTYPE_MASK 0xffff0000
37 struct d3dcompiler_shader_signature
39 D3D11_SIGNATURE_PARAMETER_DESC *elements;
40 UINT element_count;
41 char *string_data;
44 struct d3dcompiler_shader_reflection_type
46 ID3D11ShaderReflectionType ID3D11ShaderReflectionType_iface;
48 DWORD id;
49 struct wine_rb_entry entry;
51 struct d3dcompiler_shader_reflection *reflection;
53 D3D11_SHADER_TYPE_DESC desc;
54 struct d3dcompiler_shader_reflection_type_member *members;
57 struct d3dcompiler_shader_reflection_type_member
59 char *name;
60 DWORD offset;
61 struct d3dcompiler_shader_reflection_type *type;
64 struct d3dcompiler_shader_reflection_variable
66 ID3D11ShaderReflectionVariable ID3D11ShaderReflectionVariable_iface;
68 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffer;
69 struct d3dcompiler_shader_reflection_type *type;
71 char *name;
72 UINT start_offset;
73 UINT size;
74 UINT flags;
75 LPVOID default_value;
78 struct d3dcompiler_shader_reflection_constant_buffer
80 ID3D11ShaderReflectionConstantBuffer ID3D11ShaderReflectionConstantBuffer_iface;
82 struct d3dcompiler_shader_reflection *reflection;
84 char *name;
85 D3D_CBUFFER_TYPE type;
86 UINT variable_count;
87 UINT size;
88 UINT flags;
90 struct d3dcompiler_shader_reflection_variable *variables;
93 /* ID3D11ShaderReflection */
94 struct d3dcompiler_shader_reflection
96 ID3D11ShaderReflection ID3D11ShaderReflection_iface;
97 LONG refcount;
99 DWORD target;
100 char *creator;
101 UINT flags;
102 UINT version;
103 UINT bound_resource_count;
104 UINT constant_buffer_count;
106 UINT mov_instruction_count;
107 UINT conversion_instruction_count;
108 UINT instruction_count;
109 UINT emit_instruction_count;
110 D3D_PRIMITIVE_TOPOLOGY gs_output_topology;
111 UINT gs_max_output_vertex_count;
112 D3D_PRIMITIVE input_primitive;
113 UINT cut_instruction_count;
114 UINT dcl_count;
115 UINT static_flow_control_count;
116 UINT float_instruction_count;
117 UINT temp_register_count;
118 UINT int_instruction_count;
119 UINT uint_instruction_count;
120 UINT temp_array_count;
121 UINT array_instruction_count;
122 UINT texture_normal_instructions;
123 UINT texture_load_instructions;
124 UINT texture_comp_instructions;
125 UINT texture_bias_instructions;
126 UINT texture_gradient_instructions;
127 UINT dynamic_flow_control_count;
128 UINT c_control_points;
129 D3D_TESSELLATOR_OUTPUT_PRIMITIVE hs_output_primitive;
130 D3D_TESSELLATOR_PARTITIONING hs_prtitioning;
131 D3D_TESSELLATOR_DOMAIN tessellator_domain;
133 struct d3dcompiler_shader_signature *isgn;
134 struct d3dcompiler_shader_signature *osgn;
135 struct d3dcompiler_shader_signature *pcsg;
136 char *resource_string;
137 D3D11_SHADER_INPUT_BIND_DESC *bound_resources;
138 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers;
139 struct wine_rb_tree types;
142 static struct d3dcompiler_shader_reflection_type *get_reflection_type(struct d3dcompiler_shader_reflection *reflection, const char *data, DWORD offset);
144 static const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl;
145 static const struct ID3D11ShaderReflectionVariableVtbl d3dcompiler_shader_reflection_variable_vtbl;
146 static const struct ID3D11ShaderReflectionTypeVtbl d3dcompiler_shader_reflection_type_vtbl;
148 /* null objects - needed for invalid calls */
149 static struct d3dcompiler_shader_reflection_constant_buffer null_constant_buffer = {{&d3dcompiler_shader_reflection_constant_buffer_vtbl}};
150 static struct d3dcompiler_shader_reflection_type null_type = {{&d3dcompiler_shader_reflection_type_vtbl}};
151 static struct d3dcompiler_shader_reflection_variable null_variable = {{&d3dcompiler_shader_reflection_variable_vtbl},
152 &null_constant_buffer, &null_type};
154 static BOOL copy_name(const char *ptr, char **name)
156 size_t name_len;
158 if (!ptr) return TRUE;
160 name_len = strlen(ptr) + 1;
161 if (name_len == 1)
163 return TRUE;
166 *name = HeapAlloc(GetProcessHeap(), 0, name_len);
167 if (!*name)
169 ERR("Failed to allocate name memory.\n");
170 return FALSE;
173 memcpy(*name, ptr, name_len);
175 return TRUE;
178 static BOOL copy_value(const char *ptr, void **value, DWORD size)
180 if (!ptr || !size) return TRUE;
182 *value = HeapAlloc(GetProcessHeap(), 0, size);
183 if (!*value)
185 ERR("Failed to allocate vlaue memory.\n");
186 return FALSE;
189 memcpy(*value, ptr, size);
191 return TRUE;
194 static void *d3dcompiler_rb_alloc(size_t size)
196 return HeapAlloc(GetProcessHeap(), 0, size);
199 static void *d3dcompiler_rb_realloc(void *ptr, size_t size)
201 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
204 static void d3dcompiler_rb_free(void *ptr)
206 HeapFree(GetProcessHeap(), 0, ptr);
209 static int d3dcompiler_shader_reflection_type_compare(const void *key, const struct wine_rb_entry *entry)
211 const struct d3dcompiler_shader_reflection_type *t = WINE_RB_ENTRY_VALUE(entry, const struct d3dcompiler_shader_reflection_type, entry);
212 const DWORD *id = key;
214 return *id - t->id;
217 static void free_type_member(struct d3dcompiler_shader_reflection_type_member *member)
219 if (member)
221 HeapFree(GetProcessHeap(), 0, member->name);
225 static void d3dcompiler_shader_reflection_type_destroy(struct wine_rb_entry *entry, void *context)
227 struct d3dcompiler_shader_reflection_type *t = WINE_RB_ENTRY_VALUE(entry, struct d3dcompiler_shader_reflection_type, entry);
228 unsigned int i;
230 TRACE("reflection type %p.\n", t);
232 if (t->members)
234 for (i = 0; i < t->desc.Members; ++i)
236 free_type_member(&t->members[i]);
238 HeapFree(GetProcessHeap(), 0, t->members);
241 HeapFree(GetProcessHeap(), 0, t);
244 static const struct wine_rb_functions d3dcompiler_shader_reflection_type_rb_functions =
246 d3dcompiler_rb_alloc,
247 d3dcompiler_rb_realloc,
248 d3dcompiler_rb_free,
249 d3dcompiler_shader_reflection_type_compare,
252 static void free_signature(struct d3dcompiler_shader_signature *sig)
254 TRACE("Free signature %p\n", sig);
256 HeapFree(GetProcessHeap(), 0, sig->elements);
257 HeapFree(GetProcessHeap(), 0, sig->string_data);
260 static void free_variable(struct d3dcompiler_shader_reflection_variable *var)
262 if (var)
264 HeapFree(GetProcessHeap(), 0, var->name);
265 HeapFree(GetProcessHeap(), 0, var->default_value);
269 static void free_constant_buffer(struct d3dcompiler_shader_reflection_constant_buffer *cb)
271 if (cb->variables)
273 unsigned int i;
275 for (i = 0; i < cb->variable_count; ++i)
277 free_variable(&cb->variables[i]);
279 HeapFree(GetProcessHeap(), 0, cb->variables);
282 HeapFree(GetProcessHeap(), 0, cb->name);
285 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
287 TRACE("Cleanup %p\n", ref);
289 if (ref->isgn)
291 free_signature(ref->isgn);
292 HeapFree(GetProcessHeap(), 0, ref->isgn);
295 if (ref->osgn)
297 free_signature(ref->osgn);
298 HeapFree(GetProcessHeap(), 0, ref->osgn);
301 if (ref->pcsg)
303 free_signature(ref->pcsg);
304 HeapFree(GetProcessHeap(), 0, ref->pcsg);
307 if (ref->constant_buffers)
309 unsigned int i;
311 for (i = 0; i < ref->constant_buffer_count; ++i)
313 free_constant_buffer(&ref->constant_buffers[i]);
317 wine_rb_destroy(&ref->types, d3dcompiler_shader_reflection_type_destroy, NULL);
318 HeapFree(GetProcessHeap(), 0, ref->constant_buffers);
319 HeapFree(GetProcessHeap(), 0, ref->bound_resources);
320 HeapFree(GetProcessHeap(), 0, ref->resource_string);
321 HeapFree(GetProcessHeap(), 0, ref->creator);
324 /* IUnknown methods */
326 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
328 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
331 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
333 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
335 if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
336 || IsEqualGUID(riid, &IID_IUnknown))
338 IUnknown_AddRef(iface);
339 *object = iface;
340 return S_OK;
343 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
345 *object = NULL;
346 return E_NOINTERFACE;
349 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
351 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
352 ULONG refcount = InterlockedIncrement(&This->refcount);
354 TRACE("%p increasing refcount to %u\n", This, refcount);
356 return refcount;
359 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
361 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
362 ULONG refcount = InterlockedDecrement(&This->refcount);
364 TRACE("%p decreasing refcount to %u\n", This, refcount);
366 if (!refcount)
368 reflection_cleanup(This);
369 HeapFree(GetProcessHeap(), 0, This);
372 return refcount;
375 /* ID3D11ShaderReflection methods */
377 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
379 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
381 FIXME("iface %p, desc %p partial stub!\n", iface, desc);
383 if (!desc)
385 WARN("Invalid argument specified\n");
386 return E_FAIL;
389 desc->Version = This->version;
390 desc->Creator = This->creator;
391 desc->Flags = This->flags;
392 desc->ConstantBuffers = This->constant_buffer_count;
393 desc->BoundResources = This->bound_resource_count;
394 desc->InputParameters = This->isgn ? This->isgn->element_count : 0;
395 desc->OutputParameters = This->osgn ? This->osgn->element_count : 0;
396 desc->InstructionCount = This->instruction_count;
397 desc->TempRegisterCount = This->temp_register_count;
398 desc->TempArrayCount = This->temp_array_count;
399 desc->DefCount = 0;
400 desc->DclCount = This->dcl_count;
401 desc->TextureNormalInstructions = This->texture_normal_instructions;
402 desc->TextureLoadInstructions = This->texture_load_instructions;
403 desc->TextureCompInstructions = This->texture_comp_instructions;
404 desc->TextureBiasInstructions = This->texture_bias_instructions;
405 desc->TextureGradientInstructions = This->texture_gradient_instructions;
406 desc->FloatInstructionCount = This->float_instruction_count;
407 desc->IntInstructionCount = This->int_instruction_count;
408 desc->UintInstructionCount = This->uint_instruction_count;
409 desc->StaticFlowControlCount = This->static_flow_control_count;
410 desc->DynamicFlowControlCount = This->dynamic_flow_control_count;
411 desc->MacroInstructionCount = 0;
412 desc->ArrayInstructionCount = This->array_instruction_count;
413 desc->CutInstructionCount = This->cut_instruction_count;
414 desc->EmitInstructionCount = This->emit_instruction_count;
415 desc->GSOutputTopology = This->gs_output_topology;
416 desc->GSMaxOutputVertexCount = This->gs_max_output_vertex_count;
417 desc->InputPrimitive = This->input_primitive;
418 desc->PatchConstantParameters = This->pcsg ? This->pcsg->element_count : 0;
419 desc->cGSInstanceCount = 0;
420 desc->cControlPoints = This->c_control_points;
421 desc->HSOutputPrimitive = This->hs_output_primitive;
422 desc->HSPartitioning = This->hs_prtitioning;
423 desc->TessellatorDomain = This->tessellator_domain;
424 desc->cBarrierInstructions = 0;
425 desc->cInterlockedInstructions = 0;
426 desc->cTextureStoreInstructions = 0;
428 return S_OK;
431 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
432 ID3D11ShaderReflection *iface, UINT index)
434 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
436 TRACE("iface %p, index %u\n", iface, index);
438 if (index >= This->constant_buffer_count)
440 WARN("Invalid argument specified\n");
441 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
444 return &This->constant_buffers[index].ID3D11ShaderReflectionConstantBuffer_iface;
447 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
448 ID3D11ShaderReflection *iface, LPCSTR name)
450 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
451 unsigned int i;
453 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
455 if (!name)
457 WARN("Invalid argument specified\n");
458 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
461 for (i = 0; i < This->constant_buffer_count; ++i)
463 struct d3dcompiler_shader_reflection_constant_buffer *d = &This->constant_buffers[i];
465 if (!strcmp(d->name, name))
467 TRACE("Returning ID3D11ShaderReflectionConstantBuffer %p.\n", d);
468 return &d->ID3D11ShaderReflectionConstantBuffer_iface;
472 WARN("Invalid name specified\n");
474 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
477 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
478 ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_DESC *desc)
480 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
482 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
484 if (!desc || index >= This->bound_resource_count)
486 WARN("Invalid argument specified\n");
487 return E_INVALIDARG;
490 *desc = This->bound_resources[index];
492 return S_OK;
495 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
496 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
498 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
500 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
502 if (!desc || !This->isgn || index >= This->isgn->element_count)
504 WARN("Invalid argument specified\n");
505 return E_INVALIDARG;
508 *desc = This->isgn->elements[index];
510 return S_OK;
513 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
514 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
516 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
518 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
520 if (!desc || !This->osgn || index >= This->osgn->element_count)
522 WARN("Invalid argument specified\n");
523 return E_INVALIDARG;
526 *desc = This->osgn->elements[index];
528 return S_OK;
531 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
532 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
534 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
536 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
538 if (!desc || !This->pcsg || index >= This->pcsg->element_count)
540 WARN("Invalid argument specified\n");
541 return E_INVALIDARG;
544 *desc = This->pcsg->elements[index];
546 return S_OK;
549 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
550 ID3D11ShaderReflection *iface, LPCSTR name)
552 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
553 unsigned int i, k;
555 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
557 if (!name)
559 WARN("Invalid name specified\n");
560 return &null_variable.ID3D11ShaderReflectionVariable_iface;
563 for (i = 0; i < This->constant_buffer_count; ++i)
565 struct d3dcompiler_shader_reflection_constant_buffer *cb = &This->constant_buffers[i];
567 for (k = 0; k < cb->variable_count; ++k)
569 struct d3dcompiler_shader_reflection_variable *v = &cb->variables[k];
571 if (!strcmp(v->name, name))
573 TRACE("Returning ID3D11ShaderReflectionVariable %p.\n", v);
574 return &v->ID3D11ShaderReflectionVariable_iface;
579 WARN("Invalid name specified\n");
581 return &null_variable.ID3D11ShaderReflectionVariable_iface;
584 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
585 ID3D11ShaderReflection *iface, LPCSTR name, D3D11_SHADER_INPUT_BIND_DESC *desc)
587 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
588 unsigned int i;
590 TRACE("iface %p, name %s, desc %p\n", iface, debugstr_a(name), desc);
592 if (!desc || !name)
594 WARN("Invalid argument specified\n");
595 return E_INVALIDARG;
598 for (i = 0; i < This->bound_resource_count; ++i)
600 D3D11_SHADER_INPUT_BIND_DESC *d = &This->bound_resources[i];
602 if (!strcmp(d->Name, name))
604 TRACE("Returning D3D11_SHADER_INPUT_BIND_DESC %p.\n", d);
605 *desc = *d;
606 return S_OK;
610 WARN("Invalid name specified\n");
612 return E_INVALIDARG;
615 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
616 ID3D11ShaderReflection *iface)
618 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
620 TRACE("iface %p\n", iface);
622 return This->mov_instruction_count;
625 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
626 ID3D11ShaderReflection *iface)
628 FIXME("iface %p stub!\n", iface);
630 return 0;
633 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
634 ID3D11ShaderReflection *iface)
636 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
638 TRACE("iface %p\n", iface);
640 return This->conversion_instruction_count;
643 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
644 ID3D11ShaderReflection *iface)
646 FIXME("iface %p stub!\n", iface);
648 return 0;
651 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
652 ID3D11ShaderReflection *iface)
654 FIXME("iface %p stub!\n", iface);
656 return 0;
659 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
660 ID3D11ShaderReflection *iface)
662 FIXME("iface %p stub!\n", iface);
664 return 0;
667 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
668 ID3D11ShaderReflection *iface)
670 FIXME("iface %p stub!\n", iface);
672 return 0;
675 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
676 ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
678 FIXME("iface %p, level %p stub!\n", iface, level);
680 return E_NOTIMPL;
683 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
684 ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
686 FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
688 return 0;
691 static const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
693 /* IUnknown methods */
694 d3dcompiler_shader_reflection_QueryInterface,
695 d3dcompiler_shader_reflection_AddRef,
696 d3dcompiler_shader_reflection_Release,
697 /* ID3D11ShaderReflection methods */
698 d3dcompiler_shader_reflection_GetDesc,
699 d3dcompiler_shader_reflection_GetConstantBufferByIndex,
700 d3dcompiler_shader_reflection_GetConstantBufferByName,
701 d3dcompiler_shader_reflection_GetResourceBindingDesc,
702 d3dcompiler_shader_reflection_GetInputParameterDesc,
703 d3dcompiler_shader_reflection_GetOutputParameterDesc,
704 d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
705 d3dcompiler_shader_reflection_GetVariableByName,
706 d3dcompiler_shader_reflection_GetResourceBindingDescByName,
707 d3dcompiler_shader_reflection_GetMovInstructionCount,
708 d3dcompiler_shader_reflection_GetMovcInstructionCount,
709 d3dcompiler_shader_reflection_GetConversionInstructionCount,
710 d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
711 d3dcompiler_shader_reflection_GetGSInputPrimitive,
712 d3dcompiler_shader_reflection_IsSampleFrequencyShader,
713 d3dcompiler_shader_reflection_GetNumInterfaceSlots,
714 d3dcompiler_shader_reflection_GetMinFeatureLevel,
715 d3dcompiler_shader_reflection_GetThreadGroupSize,
718 /* ID3D11ShaderReflectionConstantBuffer methods */
720 static inline struct d3dcompiler_shader_reflection_constant_buffer *impl_from_ID3D11ShaderReflectionConstantBuffer(ID3D11ShaderReflectionConstantBuffer *iface)
722 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_constant_buffer, ID3D11ShaderReflectionConstantBuffer_iface);
725 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetDesc(
726 ID3D11ShaderReflectionConstantBuffer *iface, D3D11_SHADER_BUFFER_DESC *desc)
728 struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
730 TRACE("iface %p, desc %p\n", iface, desc);
732 if (This == &null_constant_buffer)
734 WARN("Null constant buffer specified\n");
735 return E_FAIL;
738 if (!desc)
740 WARN("Invalid argument specified\n");
741 return E_FAIL;
744 desc->Name = This->name;
745 desc->Type = This->type;
746 desc->Variables = This->variable_count;
747 desc->Size = This->size;
748 desc->uFlags = This->flags;
750 return S_OK;
753 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex(
754 ID3D11ShaderReflectionConstantBuffer *iface, UINT index)
756 struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
758 TRACE("iface %p, index %u\n", iface, index);
760 if (index >= This->variable_count)
762 WARN("Invalid index specified\n");
763 return &null_variable.ID3D11ShaderReflectionVariable_iface;
766 return &This->variables[index].ID3D11ShaderReflectionVariable_iface;
769 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByName(
770 ID3D11ShaderReflectionConstantBuffer *iface, LPCSTR name)
772 struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
773 unsigned int i;
775 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
777 if (!name)
779 WARN("Invalid argument specified\n");
780 return &null_variable.ID3D11ShaderReflectionVariable_iface;
783 for (i = 0; i < This->variable_count; ++i)
785 struct d3dcompiler_shader_reflection_variable *v = &This->variables[i];
787 if (!strcmp(v->name, name))
789 TRACE("Returning ID3D11ShaderReflectionVariable %p.\n", v);
790 return &v->ID3D11ShaderReflectionVariable_iface;
794 WARN("Invalid name specified\n");
796 return &null_variable.ID3D11ShaderReflectionVariable_iface;
799 static const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl =
801 /* ID3D11ShaderReflectionConstantBuffer methods */
802 d3dcompiler_shader_reflection_constant_buffer_GetDesc,
803 d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex,
804 d3dcompiler_shader_reflection_constant_buffer_GetVariableByName,
807 /* ID3D11ShaderReflectionVariable methods */
809 static inline struct d3dcompiler_shader_reflection_variable *impl_from_ID3D11ShaderReflectionVariable(ID3D11ShaderReflectionVariable *iface)
811 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_variable, ID3D11ShaderReflectionVariable_iface);
814 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetDesc(
815 ID3D11ShaderReflectionVariable *iface, D3D11_SHADER_VARIABLE_DESC *desc)
817 struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
819 TRACE("iface %p, desc %p\n", iface, desc);
821 if (This == &null_variable)
823 WARN("Null variable specified\n");
824 return E_FAIL;
827 if (!desc)
829 WARN("Invalid argument specified\n");
830 return E_FAIL;
833 desc->Name = This->name;
834 desc->StartOffset = This->start_offset;
835 desc->Size = This->size;
836 desc->uFlags = This->flags;
837 desc->DefaultValue = This->default_value;
839 return S_OK;
842 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetType(
843 ID3D11ShaderReflectionVariable *iface)
845 struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
847 TRACE("iface %p\n", iface);
849 return &This->type->ID3D11ShaderReflectionType_iface;
852 static ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetBuffer(
853 ID3D11ShaderReflectionVariable *iface)
855 struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
857 TRACE("iface %p\n", iface);
859 return &This->constant_buffer->ID3D11ShaderReflectionConstantBuffer_iface;
862 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetInterfaceSlot(
863 ID3D11ShaderReflectionVariable *iface, UINT index)
865 FIXME("iface %p, index %u stub!\n", iface, index);
867 return 0;
870 static const struct ID3D11ShaderReflectionVariableVtbl d3dcompiler_shader_reflection_variable_vtbl =
872 /* ID3D11ShaderReflectionVariable methods */
873 d3dcompiler_shader_reflection_variable_GetDesc,
874 d3dcompiler_shader_reflection_variable_GetType,
875 d3dcompiler_shader_reflection_variable_GetBuffer,
876 d3dcompiler_shader_reflection_variable_GetInterfaceSlot,
879 /* ID3D11ShaderReflectionType methods */
881 static inline struct d3dcompiler_shader_reflection_type *impl_from_ID3D11ShaderReflectionType(ID3D11ShaderReflectionType *iface)
883 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_type, ID3D11ShaderReflectionType_iface);
886 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetDesc(
887 ID3D11ShaderReflectionType *iface, D3D11_SHADER_TYPE_DESC *desc)
889 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
891 TRACE("iface %p, desc %p\n", iface, desc);
893 if (This == &null_type)
895 WARN("Null type specified\n");
896 return E_FAIL;
899 if (!desc)
901 WARN("Invalid argument specified\n");
902 return E_FAIL;
905 *desc = This->desc;
907 return S_OK;
910 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeByIndex(
911 ID3D11ShaderReflectionType *iface, UINT index)
913 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
915 TRACE("iface %p, index %u\n", iface, index);
917 if (index >= This->desc.Members)
919 WARN("Invalid index specified\n");
920 return &null_type.ID3D11ShaderReflectionType_iface;
923 return &This->members[index].type->ID3D11ShaderReflectionType_iface;
926 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeByName(
927 ID3D11ShaderReflectionType *iface, LPCSTR name)
929 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
930 unsigned int i;
932 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
934 if (!name)
936 WARN("Invalid argument specified\n");
937 return &null_type.ID3D11ShaderReflectionType_iface;
940 for (i = 0; i < This->desc.Members; ++i)
942 struct d3dcompiler_shader_reflection_type_member *member = &This->members[i];
944 if (!strcmp(member->name, name))
946 TRACE("Returning ID3D11ShaderReflectionType %p.\n", member->type);
947 return &member->type->ID3D11ShaderReflectionType_iface;
951 WARN("Invalid name specified\n");
953 return &null_type.ID3D11ShaderReflectionType_iface;
956 static LPCSTR STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeName(
957 ID3D11ShaderReflectionType *iface, UINT index)
959 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
961 TRACE("iface %p, index %u\n", iface, index);
963 if (This == &null_type)
965 WARN("Null type specified\n");
966 return "$Invalid";
969 if (index >= This->desc.Members)
971 WARN("Invalid index specified\n");
972 return NULL;
975 return This->members[index].name;
978 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_IsEqual(
979 ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *type)
981 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
983 TRACE("iface %p, type %p\n", iface, type);
985 if (This == &null_type)
987 WARN("Null type specified\n");
988 return E_FAIL;
991 if (iface == type)
992 return S_OK;
994 return S_FALSE;
997 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetSubType(
998 ID3D11ShaderReflectionType *iface)
1000 FIXME("iface %p stub!\n", iface);
1002 return NULL;
1005 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetBaseClass(
1006 ID3D11ShaderReflectionType *iface)
1008 FIXME("iface %p stub!\n", iface);
1010 return NULL;
1013 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetNumInterfaces(
1014 ID3D11ShaderReflectionType *iface)
1016 FIXME("iface %p stub!\n", iface);
1018 return 0;
1021 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetInterfaceByIndex(
1022 ID3D11ShaderReflectionType *iface, UINT index)
1024 FIXME("iface %p, index %u stub!\n", iface, index);
1026 return NULL;
1029 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_IsOfType(
1030 ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *type)
1032 FIXME("iface %p, type %p stub!\n", iface, type);
1034 return E_NOTIMPL;
1037 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_ImplementsInterface(
1038 ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *base)
1040 FIXME("iface %p, base %p stub!\n", iface, base);
1042 return E_NOTIMPL;
1045 static const struct ID3D11ShaderReflectionTypeVtbl d3dcompiler_shader_reflection_type_vtbl =
1047 /* ID3D11ShaderReflectionType methods */
1048 d3dcompiler_shader_reflection_type_GetDesc,
1049 d3dcompiler_shader_reflection_type_GetMemberTypeByIndex,
1050 d3dcompiler_shader_reflection_type_GetMemberTypeByName,
1051 d3dcompiler_shader_reflection_type_GetMemberTypeName,
1052 d3dcompiler_shader_reflection_type_IsEqual,
1053 d3dcompiler_shader_reflection_type_GetSubType,
1054 d3dcompiler_shader_reflection_type_GetBaseClass,
1055 d3dcompiler_shader_reflection_type_GetNumInterfaces,
1056 d3dcompiler_shader_reflection_type_GetInterfaceByIndex,
1057 d3dcompiler_shader_reflection_type_IsOfType,
1058 d3dcompiler_shader_reflection_type_ImplementsInterface,
1061 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1063 const char *ptr = data;
1064 DWORD size = data_size >> 2;
1066 TRACE("Size %u\n", size);
1068 read_dword(&ptr, &r->instruction_count);
1069 TRACE("InstructionCount: %u\n", r->instruction_count);
1071 read_dword(&ptr, &r->temp_register_count);
1072 TRACE("TempRegisterCount: %u\n", r->temp_register_count);
1074 skip_dword_unknown(&ptr, 1);
1076 read_dword(&ptr, &r->dcl_count);
1077 TRACE("DclCount: %u\n", r->dcl_count);
1079 read_dword(&ptr, &r->float_instruction_count);
1080 TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
1082 read_dword(&ptr, &r->int_instruction_count);
1083 TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
1085 read_dword(&ptr, &r->uint_instruction_count);
1086 TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
1088 read_dword(&ptr, &r->static_flow_control_count);
1089 TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
1091 read_dword(&ptr, &r->dynamic_flow_control_count);
1092 TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
1094 skip_dword_unknown(&ptr, 1);
1096 read_dword(&ptr, &r->temp_array_count);
1097 TRACE("TempArrayCount: %u\n", r->temp_array_count);
1099 read_dword(&ptr, &r->array_instruction_count);
1100 TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
1102 read_dword(&ptr, &r->cut_instruction_count);
1103 TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
1105 read_dword(&ptr, &r->emit_instruction_count);
1106 TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
1108 read_dword(&ptr, &r->texture_normal_instructions);
1109 TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
1111 read_dword(&ptr, &r->texture_load_instructions);
1112 TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
1114 read_dword(&ptr, &r->texture_comp_instructions);
1115 TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
1117 read_dword(&ptr, &r->texture_bias_instructions);
1118 TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
1120 read_dword(&ptr, &r->texture_gradient_instructions);
1121 TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
1123 read_dword(&ptr, &r->mov_instruction_count);
1124 TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
1126 skip_dword_unknown(&ptr, 1);
1128 read_dword(&ptr, &r->conversion_instruction_count);
1129 TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
1131 skip_dword_unknown(&ptr, 1);
1133 read_dword(&ptr, &r->input_primitive);
1134 TRACE("InputPrimitive: %x\n", r->input_primitive);
1136 read_dword(&ptr, &r->gs_output_topology);
1137 TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
1139 read_dword(&ptr, &r->gs_max_output_vertex_count);
1140 TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
1142 skip_dword_unknown(&ptr, 3);
1144 /* dx10 stat size */
1145 if (size == 29) return S_OK;
1147 skip_dword_unknown(&ptr, 1);
1149 read_dword(&ptr, &r->c_control_points);
1150 TRACE("cControlPoints: %u\n", r->c_control_points);
1152 read_dword(&ptr, &r->hs_output_primitive);
1153 TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
1155 read_dword(&ptr, &r->hs_prtitioning);
1156 TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
1158 read_dword(&ptr, &r->tessellator_domain);
1159 TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
1161 skip_dword_unknown(&ptr, 3);
1163 /* dx11 stat size */
1164 if (size == 37) return S_OK;
1166 FIXME("Unhandled size %u\n", size);
1168 return E_FAIL;
1171 static HRESULT d3dcompiler_parse_type_members(struct d3dcompiler_shader_reflection *ref,
1172 struct d3dcompiler_shader_reflection_type_member *member, const char *data, const char **ptr)
1174 DWORD offset;
1176 read_dword(ptr, &offset);
1177 if (!copy_name(data + offset, &member->name))
1179 ERR("Failed to copy name.\n");
1180 return E_OUTOFMEMORY;
1182 TRACE("Member name: %s.\n", debugstr_a(member->name));
1184 read_dword(ptr, &offset);
1185 TRACE("Member type offset: %x\n", offset);
1187 member->type = get_reflection_type(ref, data, offset);
1188 if (!member->type)
1190 ERR("Failed to get member type\n");
1191 HeapFree(GetProcessHeap(), 0, member->name);
1192 return E_FAIL;
1195 read_dword(ptr, &member->offset);
1196 TRACE("Member offset %x\n", member->offset);
1198 return S_OK;
1201 static HRESULT d3dcompiler_parse_type(struct d3dcompiler_shader_reflection_type *type, const char *data, DWORD offset)
1203 const char *ptr = data + offset;
1204 DWORD temp;
1205 D3D11_SHADER_TYPE_DESC *desc;
1206 unsigned int i;
1207 struct d3dcompiler_shader_reflection_type_member *members = NULL;
1208 HRESULT hr;
1209 DWORD member_offset;
1211 desc = &type->desc;
1213 read_dword(&ptr, &temp);
1214 desc->Class = temp & 0xffff;
1215 desc->Type = temp >> 16;
1216 TRACE("Class %s, Type %s\n", debug_d3dcompiler_shader_variable_class(desc->Class),
1217 debug_d3dcompiler_shader_variable_type(desc->Type));
1219 read_dword(&ptr, &temp);
1220 desc->Rows = temp & 0xffff;
1221 desc->Columns = temp >> 16;
1222 TRACE("Rows %u, Columns %u\n", desc->Rows, desc->Columns);
1224 read_dword(&ptr, &temp);
1225 desc->Elements = temp & 0xffff;
1226 desc->Members = temp >> 16;
1227 TRACE("Elements %u, Members %u\n", desc->Elements, desc->Members);
1229 read_dword(&ptr, &member_offset);
1230 TRACE("Member Offset %u\n", member_offset);
1232 if ((type->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1233 skip_dword_unknown(&ptr, 4);
1235 if (desc->Members)
1237 const char *ptr2 = data + member_offset;
1239 members = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*members) * desc->Members);
1240 if (!members)
1242 ERR("Failed to allocate type memory.\n");
1243 return E_OUTOFMEMORY;
1246 for (i = 0; i < desc->Members; ++i)
1248 hr = d3dcompiler_parse_type_members(type->reflection, &members[i], data, &ptr2);
1249 if (hr != S_OK)
1251 FIXME("Failed to parse type members.\n");
1252 goto err_out;
1257 type->members = members;
1259 return S_OK;
1261 err_out:
1262 for (i = 0; i < desc->Members; ++i)
1264 free_type_member(&members[i]);
1266 HeapFree(GetProcessHeap(), 0, members);
1267 return hr;
1270 static struct d3dcompiler_shader_reflection_type *get_reflection_type(struct d3dcompiler_shader_reflection *reflection, const char *data, DWORD offset)
1272 struct d3dcompiler_shader_reflection_type *type;
1273 struct wine_rb_entry *entry;
1274 HRESULT hr;
1276 entry = wine_rb_get(&reflection->types, &offset);
1277 if (entry)
1279 TRACE("Returning existing type.\n");
1280 return WINE_RB_ENTRY_VALUE(entry, struct d3dcompiler_shader_reflection_type, entry);
1283 type = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*type));
1284 if (!type)
1286 ERR("Failed to allocate type memory.\n");
1287 return NULL;
1290 type->ID3D11ShaderReflectionType_iface.lpVtbl = &d3dcompiler_shader_reflection_type_vtbl;
1291 type->id = offset;
1292 type->reflection = reflection;
1294 hr = d3dcompiler_parse_type(type, data, offset);
1295 if (FAILED(hr))
1297 ERR("Failed to parse type info, hr %#x.\n", hr);
1298 HeapFree(GetProcessHeap(), 0, type);
1299 return NULL;
1302 if (wine_rb_put(&reflection->types, &offset, &type->entry) == -1)
1304 ERR("Failed to insert type entry.\n");
1305 HeapFree(GetProcessHeap(), 0, type);
1306 return NULL;
1309 return type;
1312 static HRESULT d3dcompiler_parse_variables(struct d3dcompiler_shader_reflection_constant_buffer *cb,
1313 const char *data, DWORD data_size, const char *ptr)
1315 struct d3dcompiler_shader_reflection_variable *variables;
1316 unsigned int i;
1317 HRESULT hr;
1319 variables = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb->variable_count * sizeof(*variables));
1320 if (!variables)
1322 ERR("Failed to allocate variables memory.\n");
1323 return E_OUTOFMEMORY;
1326 for (i = 0; i < cb->variable_count; i++)
1328 struct d3dcompiler_shader_reflection_variable *v = &variables[i];
1329 DWORD offset;
1331 v->ID3D11ShaderReflectionVariable_iface.lpVtbl = &d3dcompiler_shader_reflection_variable_vtbl;
1332 v->constant_buffer = cb;
1334 read_dword(&ptr, &offset);
1335 if (!copy_name(data + offset, &v->name))
1337 ERR("Failed to copy name.\n");
1338 hr = E_OUTOFMEMORY;
1339 goto err_out;
1341 TRACE("Variable name: %s.\n", debugstr_a(v->name));
1343 read_dword(&ptr, &v->start_offset);
1344 TRACE("Variable offset: %u\n", v->start_offset);
1346 read_dword(&ptr, &v->size);
1347 TRACE("Variable size: %u\n", v->size);
1349 read_dword(&ptr, &v->flags);
1350 TRACE("Variable flags: %u\n", v->flags);
1352 read_dword(&ptr, &offset);
1353 TRACE("Variable type offset: %x\n", offset);
1354 v->type = get_reflection_type(cb->reflection, data, offset);
1355 if (!v->type)
1357 ERR("Failed to get type.\n");
1358 hr = E_FAIL;
1359 goto err_out;
1362 read_dword(&ptr, &offset);
1363 TRACE("Variable default value offset: %x\n", offset);
1364 if (!copy_value(data + offset, &v->default_value, offset ? v->size : 0))
1366 ERR("Failed to copy name.\n");
1367 hr = E_OUTOFMEMORY;
1368 goto err_out;
1371 if ((cb->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1372 skip_dword_unknown(&ptr, 4);
1375 cb->variables = variables;
1377 return S_OK;
1379 err_out:
1380 for (i = 0; i < cb->variable_count; i++)
1382 free_variable(&variables[i]);
1384 HeapFree(GetProcessHeap(), 0, variables);
1385 return hr;
1388 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1390 const char *ptr = data;
1391 DWORD size = data_size >> 2;
1392 DWORD offset, cbuffer_offset, resource_offset, creator_offset;
1393 unsigned int i, string_data_offset, string_data_size;
1394 char *string_data = NULL, *creator = NULL;
1395 D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
1396 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers = NULL;
1397 HRESULT hr;
1399 TRACE("Size %u\n", size);
1401 read_dword(&ptr, &r->constant_buffer_count);
1402 TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
1404 read_dword(&ptr, &cbuffer_offset);
1405 TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
1407 read_dword(&ptr, &r->bound_resource_count);
1408 TRACE("Bound resource count: %u\n", r->bound_resource_count);
1410 read_dword(&ptr, &resource_offset);
1411 TRACE("Bound resource offset: %#x\n", resource_offset);
1413 read_dword(&ptr, &r->target);
1414 TRACE("Target: %#x\n", r->target);
1416 read_dword(&ptr, &r->flags);
1417 TRACE("Flags: %u\n", r->flags);
1419 read_dword(&ptr, &creator_offset);
1420 TRACE("Creator at offset %#x.\n", creator_offset);
1422 if (!copy_name(data + creator_offset, &creator))
1424 ERR("Failed to copy name.\n");
1425 return E_OUTOFMEMORY;
1427 TRACE("Creator: %s.\n", debugstr_a(creator));
1429 /* todo: Parse RD11 */
1430 if ((r->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1432 skip_dword_unknown(&ptr, 8);
1435 if (r->bound_resource_count)
1437 /* 8 for each bind desc */
1438 string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
1439 string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
1441 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1442 if (!string_data)
1444 ERR("Failed to allocate string data memory.\n");
1445 hr = E_OUTOFMEMORY;
1446 goto err_out;
1448 memcpy(string_data, data + string_data_offset, string_data_size);
1450 bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
1451 if (!bound_resources)
1453 ERR("Failed to allocate resources memory.\n");
1454 hr = E_OUTOFMEMORY;
1455 goto err_out;
1458 ptr = data + resource_offset;
1459 for (i = 0; i < r->bound_resource_count; i++)
1461 D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
1463 read_dword(&ptr, &offset);
1464 desc->Name = string_data + (offset - string_data_offset);
1465 TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
1467 read_dword(&ptr, &desc->Type);
1468 TRACE("Input bind Type: %#x\n", desc->Type);
1470 read_dword(&ptr, &desc->ReturnType);
1471 TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
1473 read_dword(&ptr, &desc->Dimension);
1474 TRACE("Input bind Dimension: %#x\n", desc->Dimension);
1476 read_dword(&ptr, &desc->NumSamples);
1477 TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
1479 read_dword(&ptr, &desc->BindPoint);
1480 TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
1482 read_dword(&ptr, &desc->BindCount);
1483 TRACE("Input bind BindCount: %u\n", desc->BindCount);
1485 read_dword(&ptr, &desc->uFlags);
1486 TRACE("Input bind uFlags: %u\n", desc->uFlags);
1490 if (r->constant_buffer_count)
1492 constant_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, r->constant_buffer_count * sizeof(*constant_buffers));
1493 if (!constant_buffers)
1495 ERR("Failed to allocate constant buffer memory.\n");
1496 hr = E_OUTOFMEMORY;
1497 goto err_out;
1500 ptr = data + cbuffer_offset;
1501 for (i = 0; i < r->constant_buffer_count; i++)
1503 struct d3dcompiler_shader_reflection_constant_buffer *cb = &constant_buffers[i];
1505 cb->ID3D11ShaderReflectionConstantBuffer_iface.lpVtbl = &d3dcompiler_shader_reflection_constant_buffer_vtbl;
1506 cb->reflection = r;
1508 read_dword(&ptr, &offset);
1509 if (!copy_name(data + offset, &cb->name))
1511 ERR("Failed to copy name.\n");
1512 hr = E_OUTOFMEMORY;
1513 goto err_out;
1515 TRACE("Name: %s.\n", debugstr_a(cb->name));
1517 read_dword(&ptr, &cb->variable_count);
1518 TRACE("Variable count: %u\n", cb->variable_count);
1520 read_dword(&ptr, &offset);
1521 TRACE("Variable offset: %x\n", offset);
1523 hr = d3dcompiler_parse_variables(cb, data, data_size, data + offset);
1524 if (hr != S_OK)
1526 FIXME("Failed to parse variables.\n");
1527 goto err_out;
1530 read_dword(&ptr, &cb->size);
1531 TRACE("Cbuffer size: %u\n", cb->size);
1533 read_dword(&ptr, &cb->flags);
1534 TRACE("Cbuffer flags: %u\n", cb->flags);
1536 read_dword(&ptr, &cb->type);
1537 TRACE("Cbuffer type: %#x\n", cb->type);
1541 r->creator = creator;
1542 r->resource_string = string_data;
1543 r->bound_resources = bound_resources;
1544 r->constant_buffers = constant_buffers;
1546 return S_OK;
1548 err_out:
1549 for (i = 0; i < r->constant_buffer_count; ++i)
1551 free_constant_buffer(&constant_buffers[i]);
1553 HeapFree(GetProcessHeap(), 0, constant_buffers);
1554 HeapFree(GetProcessHeap(), 0, bound_resources);
1555 HeapFree(GetProcessHeap(), 0, string_data);
1556 HeapFree(GetProcessHeap(), 0, creator);
1558 return hr;
1561 static HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section, DWORD target)
1563 D3D11_SIGNATURE_PARAMETER_DESC *d;
1564 unsigned int string_data_offset;
1565 unsigned int string_data_size;
1566 const char *ptr = section->data;
1567 char *string_data;
1568 unsigned int i;
1569 DWORD count;
1570 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
1572 switch (section->tag)
1574 case TAG_OSG5:
1575 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
1576 break;
1578 case TAG_ISGN:
1579 case TAG_OSGN:
1580 case TAG_PCSG:
1581 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1582 break;
1584 default:
1585 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1586 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1587 break;
1590 read_dword(&ptr, &count);
1591 TRACE("%u elements\n", count);
1593 skip_dword_unknown(&ptr, 1);
1595 d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
1596 if (!d)
1598 ERR("Failed to allocate signature memory.\n");
1599 return E_OUTOFMEMORY;
1602 /* 2 DWORDs for the header, element_size for each element. */
1603 string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
1604 string_data_size = section->data_size - string_data_offset;
1606 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1607 if (!string_data)
1609 ERR("Failed to allocate string data memory.\n");
1610 HeapFree(GetProcessHeap(), 0, d);
1611 return E_OUTOFMEMORY;
1613 memcpy(string_data, section->data + string_data_offset, string_data_size);
1615 for (i = 0; i < count; ++i)
1617 UINT name_offset;
1618 DWORD mask;
1620 if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
1622 read_dword(&ptr, &d[i].Stream);
1624 else
1626 d[i].Stream = 0;
1629 read_dword(&ptr, &name_offset);
1630 d[i].SemanticName = string_data + (name_offset - string_data_offset);
1631 read_dword(&ptr, &d[i].SemanticIndex);
1632 read_dword(&ptr, &d[i].SystemValueType);
1633 read_dword(&ptr, &d[i].ComponentType);
1634 read_dword(&ptr, &d[i].Register);
1635 read_dword(&ptr, &mask);
1636 d[i].ReadWriteMask = (mask >> 8) & 0xff;
1637 d[i].Mask = mask & 0xff;
1639 /* pixel shaders have a special handling for SystemValueType in the output signature */
1640 if (((target & D3DCOMPILER_SHADER_TARGET_SHADERTYPE_MASK) == 0xffff0000) && (section->tag == TAG_OSG5 || section->tag == TAG_OSGN))
1642 TRACE("Pixelshader output signature fixup.\n");
1644 if (d[i].Register == 0xffffffff)
1646 if (!strcasecmp(d[i].SemanticName, "sv_depth")) d[i].SystemValueType = D3D_NAME_DEPTH;
1647 if (!strcasecmp(d[i].SemanticName, "sv_coverage")) d[i].SystemValueType = D3D_NAME_COVERAGE;
1648 if (!strcasecmp(d[i].SemanticName, "sv_depthgreaterequal")) d[i].SystemValueType = D3D_NAME_DEPTH_GREATER_EQUAL;
1649 if (!strcasecmp(d[i].SemanticName, "sv_depthlessequal")) d[i].SystemValueType = D3D_NAME_DEPTH_LESS_EQUAL;
1651 else
1653 d[i].SystemValueType = D3D_NAME_TARGET;
1657 TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
1658 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
1659 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
1660 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
1663 s->elements = d;
1664 s->element_count = count;
1665 s->string_data = string_data;
1667 return S_OK;
1670 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1672 const char *ptr = data;
1674 read_dword(&ptr, &r->version);
1675 TRACE("Shader version: %u\n", r->version);
1677 /* todo: Check if anything else is needed from the shdr or shex blob. */
1679 return S_OK;
1682 static HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
1683 const void *data, SIZE_T data_size)
1685 struct dxbc src_dxbc;
1686 HRESULT hr;
1687 unsigned int i;
1689 reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
1690 reflection->refcount = 1;
1692 if (wine_rb_init(&reflection->types, &d3dcompiler_shader_reflection_type_rb_functions) == -1)
1694 ERR("Failed to initialize type rbtree.\n");
1695 return E_FAIL;
1698 hr = dxbc_parse(data, data_size, &src_dxbc);
1699 if (FAILED(hr))
1701 WARN("Failed to parse reflection\n");
1702 return hr;
1705 for (i = 0; i < src_dxbc.count; ++i)
1707 struct dxbc_section *section = &src_dxbc.sections[i];
1709 switch (section->tag)
1711 case TAG_RDEF:
1712 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
1713 if (FAILED(hr))
1715 WARN("Failed to parse RDEF section.\n");
1716 goto err_out;
1718 break;
1720 case TAG_ISGN:
1721 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
1722 if (!reflection->isgn)
1724 ERR("Failed to allocate ISGN memory.\n");
1725 hr = E_OUTOFMEMORY;
1726 goto err_out;
1729 hr = d3dcompiler_parse_signature(reflection->isgn, section, reflection->target);
1730 if (FAILED(hr))
1732 WARN("Failed to parse section ISGN.\n");
1733 goto err_out;
1735 break;
1737 case TAG_OSG5:
1738 case TAG_OSGN:
1739 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
1740 if (!reflection->osgn)
1742 ERR("Failed to allocate OSGN memory.\n");
1743 hr = E_OUTOFMEMORY;
1744 goto err_out;
1747 hr = d3dcompiler_parse_signature(reflection->osgn, section, reflection->target);
1748 if (FAILED(hr))
1750 WARN("Failed to parse section OSGN.\n");
1751 goto err_out;
1753 break;
1755 case TAG_PCSG:
1756 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
1757 if (!reflection->pcsg)
1759 ERR("Failed to allocate PCSG memory.\n");
1760 hr = E_OUTOFMEMORY;
1761 goto err_out;
1764 hr = d3dcompiler_parse_signature(reflection->pcsg, section, reflection->target);
1765 if (FAILED(hr))
1767 WARN("Failed to parse section PCSG.\n");
1768 goto err_out;
1770 break;
1772 case TAG_SHEX:
1773 case TAG_SHDR:
1774 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
1775 if (FAILED(hr))
1777 WARN("Failed to parse SHDR section.\n");
1778 goto err_out;
1780 break;
1782 case TAG_STAT:
1783 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
1784 if (FAILED(hr))
1786 WARN("Failed to parse section STAT.\n");
1787 goto err_out;
1789 break;
1791 default:
1792 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1793 break;
1797 dxbc_destroy(&src_dxbc);
1799 return hr;
1801 err_out:
1802 reflection_cleanup(reflection);
1803 dxbc_destroy(&src_dxbc);
1805 return hr;
1808 HRESULT WINAPI D3DReflect(const void *data, SIZE_T data_size, REFIID riid, void **reflector)
1810 struct d3dcompiler_shader_reflection *object;
1811 HRESULT hr;
1812 const DWORD *temp = data;
1814 TRACE("data %p, data_size %lu, riid %s, blob %p\n", data, data_size, debugstr_guid(riid), reflector);
1816 if (!data || data_size < 32)
1818 WARN("Invalid argument supplied.\n");
1819 return D3DERR_INVALIDCALL;
1822 if (temp[6] != data_size)
1824 WARN("Wrong size supplied.\n");
1825 return E_FAIL;
1828 if (!IsEqualGUID(riid, &IID_ID3D11ShaderReflection))
1830 WARN("Wrong riid %s, accept only %s!\n", debugstr_guid(riid), debugstr_guid(&IID_ID3D11ShaderReflection));
1831 return E_NOINTERFACE;
1834 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1835 if (!object)
1837 ERR("Failed to allocate D3D compiler shader reflection object memory\n");
1838 return E_OUTOFMEMORY;
1841 hr = d3dcompiler_shader_reflection_init(object, data, data_size);
1842 if (FAILED(hr))
1844 WARN("Failed to initialize shader reflection\n");
1845 HeapFree(GetProcessHeap(), 0, object);
1846 return hr;
1849 *reflector = object;
1851 TRACE("Created ID3D11ShaderReflection %p\n", object);
1853 return S_OK;