wined3d: Don't normalize R8G8B8A8_UINT attributes.
[wine/multimedia.git] / dlls / d3dcompiler_43 / reflection.c
blobe2955f048b31a8ae73a7db1164d322618b571f8b
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 void *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 value 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, const char *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, const char *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, const char *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 FALSE;
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, const char *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, const char *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 const char * 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)
1285 return NULL;
1287 type->ID3D11ShaderReflectionType_iface.lpVtbl = &d3dcompiler_shader_reflection_type_vtbl;
1288 type->id = offset;
1289 type->reflection = reflection;
1291 hr = d3dcompiler_parse_type(type, data, offset);
1292 if (FAILED(hr))
1294 ERR("Failed to parse type info, hr %#x.\n", hr);
1295 HeapFree(GetProcessHeap(), 0, type);
1296 return NULL;
1299 if (wine_rb_put(&reflection->types, &offset, &type->entry) == -1)
1301 ERR("Failed to insert type entry.\n");
1302 HeapFree(GetProcessHeap(), 0, type);
1303 return NULL;
1306 return type;
1309 static HRESULT d3dcompiler_parse_variables(struct d3dcompiler_shader_reflection_constant_buffer *cb,
1310 const char *data, DWORD data_size, const char *ptr)
1312 struct d3dcompiler_shader_reflection_variable *variables;
1313 unsigned int i;
1314 HRESULT hr;
1316 variables = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb->variable_count * sizeof(*variables));
1317 if (!variables)
1319 ERR("Failed to allocate variables memory.\n");
1320 return E_OUTOFMEMORY;
1323 for (i = 0; i < cb->variable_count; i++)
1325 struct d3dcompiler_shader_reflection_variable *v = &variables[i];
1326 DWORD offset;
1328 v->ID3D11ShaderReflectionVariable_iface.lpVtbl = &d3dcompiler_shader_reflection_variable_vtbl;
1329 v->constant_buffer = cb;
1331 read_dword(&ptr, &offset);
1332 if (!copy_name(data + offset, &v->name))
1334 ERR("Failed to copy name.\n");
1335 hr = E_OUTOFMEMORY;
1336 goto err_out;
1338 TRACE("Variable name: %s.\n", debugstr_a(v->name));
1340 read_dword(&ptr, &v->start_offset);
1341 TRACE("Variable offset: %u\n", v->start_offset);
1343 read_dword(&ptr, &v->size);
1344 TRACE("Variable size: %u\n", v->size);
1346 read_dword(&ptr, &v->flags);
1347 TRACE("Variable flags: %u\n", v->flags);
1349 read_dword(&ptr, &offset);
1350 TRACE("Variable type offset: %x\n", offset);
1351 v->type = get_reflection_type(cb->reflection, data, offset);
1352 if (!v->type)
1354 ERR("Failed to get type.\n");
1355 hr = E_FAIL;
1356 goto err_out;
1359 read_dword(&ptr, &offset);
1360 TRACE("Variable default value offset: %x\n", offset);
1361 if (!copy_value(data + offset, &v->default_value, offset ? v->size : 0))
1363 ERR("Failed to copy name.\n");
1364 hr = E_OUTOFMEMORY;
1365 goto err_out;
1368 if ((cb->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1369 skip_dword_unknown(&ptr, 4);
1372 cb->variables = variables;
1374 return S_OK;
1376 err_out:
1377 for (i = 0; i < cb->variable_count; i++)
1379 free_variable(&variables[i]);
1381 HeapFree(GetProcessHeap(), 0, variables);
1382 return hr;
1385 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1387 const char *ptr = data;
1388 DWORD size = data_size >> 2;
1389 DWORD offset, cbuffer_offset, resource_offset, creator_offset;
1390 unsigned int i, string_data_offset, string_data_size;
1391 char *string_data = NULL, *creator = NULL;
1392 D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
1393 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers = NULL;
1394 HRESULT hr;
1396 TRACE("Size %u\n", size);
1398 read_dword(&ptr, &r->constant_buffer_count);
1399 TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
1401 read_dword(&ptr, &cbuffer_offset);
1402 TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
1404 read_dword(&ptr, &r->bound_resource_count);
1405 TRACE("Bound resource count: %u\n", r->bound_resource_count);
1407 read_dword(&ptr, &resource_offset);
1408 TRACE("Bound resource offset: %#x\n", resource_offset);
1410 read_dword(&ptr, &r->target);
1411 TRACE("Target: %#x\n", r->target);
1413 read_dword(&ptr, &r->flags);
1414 TRACE("Flags: %u\n", r->flags);
1416 read_dword(&ptr, &creator_offset);
1417 TRACE("Creator at offset %#x.\n", creator_offset);
1419 if (!copy_name(data + creator_offset, &creator))
1421 ERR("Failed to copy name.\n");
1422 return E_OUTOFMEMORY;
1424 TRACE("Creator: %s.\n", debugstr_a(creator));
1426 /* todo: Parse RD11 */
1427 if ((r->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1429 skip_dword_unknown(&ptr, 8);
1432 if (r->bound_resource_count)
1434 /* 8 for each bind desc */
1435 string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
1436 string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
1438 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1439 if (!string_data)
1441 ERR("Failed to allocate string data memory.\n");
1442 hr = E_OUTOFMEMORY;
1443 goto err_out;
1445 memcpy(string_data, data + string_data_offset, string_data_size);
1447 bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
1448 if (!bound_resources)
1450 ERR("Failed to allocate resources memory.\n");
1451 hr = E_OUTOFMEMORY;
1452 goto err_out;
1455 ptr = data + resource_offset;
1456 for (i = 0; i < r->bound_resource_count; i++)
1458 D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
1460 read_dword(&ptr, &offset);
1461 desc->Name = string_data + (offset - string_data_offset);
1462 TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
1464 read_dword(&ptr, &desc->Type);
1465 TRACE("Input bind Type: %#x\n", desc->Type);
1467 read_dword(&ptr, &desc->ReturnType);
1468 TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
1470 read_dword(&ptr, &desc->Dimension);
1471 TRACE("Input bind Dimension: %#x\n", desc->Dimension);
1473 read_dword(&ptr, &desc->NumSamples);
1474 TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
1476 read_dword(&ptr, &desc->BindPoint);
1477 TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
1479 read_dword(&ptr, &desc->BindCount);
1480 TRACE("Input bind BindCount: %u\n", desc->BindCount);
1482 read_dword(&ptr, &desc->uFlags);
1483 TRACE("Input bind uFlags: %u\n", desc->uFlags);
1487 if (r->constant_buffer_count)
1489 constant_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, r->constant_buffer_count * sizeof(*constant_buffers));
1490 if (!constant_buffers)
1492 ERR("Failed to allocate constant buffer memory.\n");
1493 hr = E_OUTOFMEMORY;
1494 goto err_out;
1497 ptr = data + cbuffer_offset;
1498 for (i = 0; i < r->constant_buffer_count; i++)
1500 struct d3dcompiler_shader_reflection_constant_buffer *cb = &constant_buffers[i];
1502 cb->ID3D11ShaderReflectionConstantBuffer_iface.lpVtbl = &d3dcompiler_shader_reflection_constant_buffer_vtbl;
1503 cb->reflection = r;
1505 read_dword(&ptr, &offset);
1506 if (!copy_name(data + offset, &cb->name))
1508 ERR("Failed to copy name.\n");
1509 hr = E_OUTOFMEMORY;
1510 goto err_out;
1512 TRACE("Name: %s.\n", debugstr_a(cb->name));
1514 read_dword(&ptr, &cb->variable_count);
1515 TRACE("Variable count: %u\n", cb->variable_count);
1517 read_dword(&ptr, &offset);
1518 TRACE("Variable offset: %x\n", offset);
1520 hr = d3dcompiler_parse_variables(cb, data, data_size, data + offset);
1521 if (hr != S_OK)
1523 FIXME("Failed to parse variables.\n");
1524 goto err_out;
1527 read_dword(&ptr, &cb->size);
1528 TRACE("Cbuffer size: %u\n", cb->size);
1530 read_dword(&ptr, &cb->flags);
1531 TRACE("Cbuffer flags: %u\n", cb->flags);
1533 read_dword(&ptr, &cb->type);
1534 TRACE("Cbuffer type: %#x\n", cb->type);
1538 r->creator = creator;
1539 r->resource_string = string_data;
1540 r->bound_resources = bound_resources;
1541 r->constant_buffers = constant_buffers;
1543 return S_OK;
1545 err_out:
1546 for (i = 0; i < r->constant_buffer_count; ++i)
1548 free_constant_buffer(&constant_buffers[i]);
1550 HeapFree(GetProcessHeap(), 0, constant_buffers);
1551 HeapFree(GetProcessHeap(), 0, bound_resources);
1552 HeapFree(GetProcessHeap(), 0, string_data);
1553 HeapFree(GetProcessHeap(), 0, creator);
1555 return hr;
1558 static HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section, DWORD target)
1560 D3D11_SIGNATURE_PARAMETER_DESC *d;
1561 unsigned int string_data_offset;
1562 unsigned int string_data_size;
1563 const char *ptr = section->data;
1564 char *string_data;
1565 unsigned int i;
1566 DWORD count;
1567 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
1569 switch (section->tag)
1571 case TAG_OSG5:
1572 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
1573 break;
1575 case TAG_ISGN:
1576 case TAG_OSGN:
1577 case TAG_PCSG:
1578 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1579 break;
1581 default:
1582 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1583 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1584 break;
1587 read_dword(&ptr, &count);
1588 TRACE("%u elements\n", count);
1590 skip_dword_unknown(&ptr, 1);
1592 d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
1593 if (!d)
1595 ERR("Failed to allocate signature memory.\n");
1596 return E_OUTOFMEMORY;
1599 /* 2 DWORDs for the header, element_size for each element. */
1600 string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
1601 string_data_size = section->data_size - string_data_offset;
1603 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1604 if (!string_data)
1606 ERR("Failed to allocate string data memory.\n");
1607 HeapFree(GetProcessHeap(), 0, d);
1608 return E_OUTOFMEMORY;
1610 memcpy(string_data, section->data + string_data_offset, string_data_size);
1612 for (i = 0; i < count; ++i)
1614 UINT name_offset;
1615 DWORD mask;
1617 if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
1619 read_dword(&ptr, &d[i].Stream);
1621 else
1623 d[i].Stream = 0;
1626 read_dword(&ptr, &name_offset);
1627 d[i].SemanticName = string_data + (name_offset - string_data_offset);
1628 read_dword(&ptr, &d[i].SemanticIndex);
1629 read_dword(&ptr, &d[i].SystemValueType);
1630 read_dword(&ptr, &d[i].ComponentType);
1631 read_dword(&ptr, &d[i].Register);
1632 read_dword(&ptr, &mask);
1633 d[i].ReadWriteMask = (mask >> 8) & 0xff;
1634 d[i].Mask = mask & 0xff;
1636 /* pixel shaders have a special handling for SystemValueType in the output signature */
1637 if (((target & D3DCOMPILER_SHADER_TARGET_SHADERTYPE_MASK) == 0xffff0000) && (section->tag == TAG_OSG5 || section->tag == TAG_OSGN))
1639 TRACE("Pixelshader output signature fixup.\n");
1641 if (d[i].Register == 0xffffffff)
1643 if (!strcasecmp(d[i].SemanticName, "sv_depth")) d[i].SystemValueType = D3D_NAME_DEPTH;
1644 if (!strcasecmp(d[i].SemanticName, "sv_coverage")) d[i].SystemValueType = D3D_NAME_COVERAGE;
1645 if (!strcasecmp(d[i].SemanticName, "sv_depthgreaterequal")) d[i].SystemValueType = D3D_NAME_DEPTH_GREATER_EQUAL;
1646 if (!strcasecmp(d[i].SemanticName, "sv_depthlessequal")) d[i].SystemValueType = D3D_NAME_DEPTH_LESS_EQUAL;
1648 else
1650 d[i].SystemValueType = D3D_NAME_TARGET;
1654 TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
1655 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
1656 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
1657 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
1660 s->elements = d;
1661 s->element_count = count;
1662 s->string_data = string_data;
1664 return S_OK;
1667 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1669 const char *ptr = data;
1671 read_dword(&ptr, &r->version);
1672 TRACE("Shader version: %u\n", r->version);
1674 /* todo: Check if anything else is needed from the shdr or shex blob. */
1676 return S_OK;
1679 static HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
1680 const void *data, SIZE_T data_size)
1682 struct dxbc src_dxbc;
1683 HRESULT hr;
1684 unsigned int i;
1686 reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
1687 reflection->refcount = 1;
1689 if (wine_rb_init(&reflection->types, &d3dcompiler_shader_reflection_type_rb_functions) == -1)
1691 ERR("Failed to initialize type rbtree.\n");
1692 return E_FAIL;
1695 hr = dxbc_parse(data, data_size, &src_dxbc);
1696 if (FAILED(hr))
1698 WARN("Failed to parse reflection\n");
1699 return hr;
1702 for (i = 0; i < src_dxbc.count; ++i)
1704 struct dxbc_section *section = &src_dxbc.sections[i];
1706 switch (section->tag)
1708 case TAG_RDEF:
1709 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
1710 if (FAILED(hr))
1712 WARN("Failed to parse RDEF section.\n");
1713 goto err_out;
1715 break;
1717 case TAG_ISGN:
1718 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
1719 if (!reflection->isgn)
1721 ERR("Failed to allocate ISGN memory.\n");
1722 hr = E_OUTOFMEMORY;
1723 goto err_out;
1726 hr = d3dcompiler_parse_signature(reflection->isgn, section, reflection->target);
1727 if (FAILED(hr))
1729 WARN("Failed to parse section ISGN.\n");
1730 goto err_out;
1732 break;
1734 case TAG_OSG5:
1735 case TAG_OSGN:
1736 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
1737 if (!reflection->osgn)
1739 ERR("Failed to allocate OSGN memory.\n");
1740 hr = E_OUTOFMEMORY;
1741 goto err_out;
1744 hr = d3dcompiler_parse_signature(reflection->osgn, section, reflection->target);
1745 if (FAILED(hr))
1747 WARN("Failed to parse section OSGN.\n");
1748 goto err_out;
1750 break;
1752 case TAG_PCSG:
1753 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
1754 if (!reflection->pcsg)
1756 ERR("Failed to allocate PCSG memory.\n");
1757 hr = E_OUTOFMEMORY;
1758 goto err_out;
1761 hr = d3dcompiler_parse_signature(reflection->pcsg, section, reflection->target);
1762 if (FAILED(hr))
1764 WARN("Failed to parse section PCSG.\n");
1765 goto err_out;
1767 break;
1769 case TAG_SHEX:
1770 case TAG_SHDR:
1771 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
1772 if (FAILED(hr))
1774 WARN("Failed to parse SHDR section.\n");
1775 goto err_out;
1777 break;
1779 case TAG_STAT:
1780 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
1781 if (FAILED(hr))
1783 WARN("Failed to parse section STAT.\n");
1784 goto err_out;
1786 break;
1788 default:
1789 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1790 break;
1794 dxbc_destroy(&src_dxbc);
1796 return hr;
1798 err_out:
1799 reflection_cleanup(reflection);
1800 dxbc_destroy(&src_dxbc);
1802 return hr;
1805 HRESULT WINAPI D3DReflect(const void *data, SIZE_T data_size, REFIID riid, void **reflector)
1807 struct d3dcompiler_shader_reflection *object;
1808 HRESULT hr;
1809 const DWORD *temp = data;
1811 TRACE("data %p, data_size %lu, riid %s, blob %p\n", data, data_size, debugstr_guid(riid), reflector);
1813 if (!data || data_size < 32)
1815 WARN("Invalid argument supplied.\n");
1816 return D3DERR_INVALIDCALL;
1819 if (temp[6] != data_size)
1821 WARN("Wrong size supplied.\n");
1822 return E_FAIL;
1825 if (!IsEqualGUID(riid, &IID_ID3D11ShaderReflection))
1827 WARN("Wrong riid %s, accept only %s!\n", debugstr_guid(riid), debugstr_guid(&IID_ID3D11ShaderReflection));
1828 return E_NOINTERFACE;
1831 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1832 if (!object)
1833 return E_OUTOFMEMORY;
1835 hr = d3dcompiler_shader_reflection_init(object, data, data_size);
1836 if (FAILED(hr))
1838 WARN("Failed to initialize shader reflection\n");
1839 HeapFree(GetProcessHeap(), 0, object);
1840 return hr;
1843 *reflector = object;
1845 TRACE("Created ID3D11ShaderReflection %p\n", object);
1847 return S_OK;