1 /*
2  * Copyright 2009 Henri Verbeet for CodeWeavers
3  * Copyright 2010 Rico Schüller
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20 
21 #include "initguid.h"
22 #include "d3dcompiler_private.h"
23 #include "wine/winternl.h"
24 
25 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
26 
27 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE
28 {
29     D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6 = 6,
30     D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7 = 7,
31 };
32 
33 #define D3DCOMPILER_SHADER_TARGET_VERSION_MASK 0xffff
34 #define D3DCOMPILER_SHADER_TARGET_SHADERTYPE_MASK 0xffff0000
35 
36 struct d3dcompiler_shader_signature
37 {
38     D3D11_SIGNATURE_PARAMETER_DESC *elements;
39     UINT element_count;
40     char *string_data;
41 };
42 
43 struct d3dcompiler_shader_reflection_type
44 {
45     ID3D11ShaderReflectionType ID3D11ShaderReflectionType_iface;
46 
47     DWORD id;
48     struct wine_rb_entry entry;
49 
50     struct d3dcompiler_shader_reflection *reflection;
51 
52     D3D11_SHADER_TYPE_DESC desc;
53     struct d3dcompiler_shader_reflection_type_member *members;
54     char *name;
55 };
56 
57 struct d3dcompiler_shader_reflection_type_member
58 {
59     char *name;
60     DWORD offset;
61     struct d3dcompiler_shader_reflection_type *type;
62 };
63 
64 struct d3dcompiler_shader_reflection_variable
65 {
66     ID3D11ShaderReflectionVariable ID3D11ShaderReflectionVariable_iface;
67 
68     struct d3dcompiler_shader_reflection_constant_buffer *constant_buffer;
69     struct d3dcompiler_shader_reflection_type *type;
70 
71     char *name;
72     UINT start_offset;
73     UINT size;
74     UINT flags;
75     void *default_value;
76 };
77 
78 struct d3dcompiler_shader_reflection_constant_buffer
79 {
80     ID3D11ShaderReflectionConstantBuffer ID3D11ShaderReflectionConstantBuffer_iface;
81 
82     struct d3dcompiler_shader_reflection *reflection;
83 
84     char *name;
85     D3D_CBUFFER_TYPE type;
86     UINT variable_count;
87     UINT size;
88     UINT flags;
89 
90     struct d3dcompiler_shader_reflection_variable *variables;
91 };
92 
93 /* ID3D11ShaderReflection */
94 struct d3dcompiler_shader_reflection
95 {
96     ID3D11ShaderReflection ID3D11ShaderReflection_iface;
97     LONG refcount;
98 
99     DWORD target;
100     char *creator;
101     UINT flags;
102     UINT version;
103     UINT bound_resource_count;
104     UINT constant_buffer_count;
105 
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;
132 
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;
140 };
141 
142 static struct d3dcompiler_shader_reflection_type *get_reflection_type(struct d3dcompiler_shader_reflection *reflection, const char *data, DWORD offset);
143 
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;
147 
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};
153 
154 static BOOL copy_name(const char *ptr, char **name)
155 {
156     size_t name_len;
157 
158     if (!ptr) return TRUE;
159 
160     name_len = strlen(ptr) + 1;
161     if (name_len == 1)
162     {
163         return TRUE;
164     }
165 
166     *name = HeapAlloc(GetProcessHeap(), 0, name_len);
167     if (!*name)
168     {
169         ERR("Failed to allocate name memory.\n");
170         return FALSE;
171     }
172 
173     memcpy(*name, ptr, name_len);
174 
175     return TRUE;
176 }
177 
178 static BOOL copy_value(const char *ptr, void **value, DWORD size)
179 {
180     if (!ptr || !size) return TRUE;
181 
182     *value = HeapAlloc(GetProcessHeap(), 0, size);
183     if (!*value)
184     {
185         ERR("Failed to allocate value memory.\n");
186         return FALSE;
187     }
188 
189     memcpy(*value, ptr, size);
190 
191     return TRUE;
192 }
193 
194 static int d3dcompiler_shader_reflection_type_compare(const void *key, const struct wine_rb_entry *entry)
195 {
196     const struct d3dcompiler_shader_reflection_type *t = WINE_RB_ENTRY_VALUE(entry, const struct d3dcompiler_shader_reflection_type, entry);
197     const DWORD *id = key;
198 
199     return *id - t->id;
200 }
201 
202 static void free_type_member(struct d3dcompiler_shader_reflection_type_member *member)
203 {
204     if (member)
205     {
206         HeapFree(GetProcessHeap(), 0, member->name);
207     }
208 }
209 
210 static void d3dcompiler_shader_reflection_type_destroy(struct wine_rb_entry *entry, void *context)
211 {
212     struct d3dcompiler_shader_reflection_type *t = WINE_RB_ENTRY_VALUE(entry, struct d3dcompiler_shader_reflection_type, entry);
213     unsigned int i;
214 
215     TRACE("reflection type %p.\n", t);
216 
217     if (t->members)
218     {
219         for (i = 0; i < t->desc.Members; ++i)
220         {
221             free_type_member(&t->members[i]);
222         }
223         HeapFree(GetProcessHeap(), 0, t->members);
224     }
225 
226     heap_free(t->name);
227     HeapFree(GetProcessHeap(), 0, t);
228 }
229 
230 static void free_signature(struct d3dcompiler_shader_signature *sig)
231 {
232     TRACE("Free signature %p\n", sig);
233 
234     HeapFree(GetProcessHeap(), 0, sig->elements);
235     HeapFree(GetProcessHeap(), 0, sig->string_data);
236 }
237 
238 static void free_variable(struct d3dcompiler_shader_reflection_variable *var)
239 {
240     if (var)
241     {
242         HeapFree(GetProcessHeap(), 0, var->name);
243         HeapFree(GetProcessHeap(), 0, var->default_value);
244     }
245 }
246 
247 static void free_constant_buffer(struct d3dcompiler_shader_reflection_constant_buffer *cb)
248 {
249     if (cb->variables)
250     {
251         unsigned int i;
252 
253         for (i = 0; i < cb->variable_count; ++i)
254         {
255             free_variable(&cb->variables[i]);
256         }
257         HeapFree(GetProcessHeap(), 0, cb->variables);
258     }
259 
260     HeapFree(GetProcessHeap(), 0, cb->name);
261 }
262 
263 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
264 {
265     TRACE("Cleanup %p\n", ref);
266 
267     if (ref->isgn)
268     {
269         free_signature(ref->isgn);
270         HeapFree(GetProcessHeap(), 0, ref->isgn);
271     }
272 
273     if (ref->osgn)
274     {
275         free_signature(ref->osgn);
276         HeapFree(GetProcessHeap(), 0, ref->osgn);
277     }
278 
279     if (ref->pcsg)
280     {
281         free_signature(ref->pcsg);
282         HeapFree(GetProcessHeap(), 0, ref->pcsg);
283     }
284 
285     if (ref->constant_buffers)
286     {
287         unsigned int i;
288 
289         for (i = 0; i < ref->constant_buffer_count; ++i)
290         {
291             free_constant_buffer(&ref->constant_buffers[i]);
292         }
293     }
294 
295     wine_rb_destroy(&ref->types, d3dcompiler_shader_reflection_type_destroy, NULL);
296     HeapFree(GetProcessHeap(), 0, ref->constant_buffers);
297     HeapFree(GetProcessHeap(), 0, ref->bound_resources);
298     HeapFree(GetProcessHeap(), 0, ref->resource_string);
299     HeapFree(GetProcessHeap(), 0, ref->creator);
300 }
301 
302 /* IUnknown methods */
303 
304 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
305 {
306     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
307 }
308 
309 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
310 {
311     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
312 
313     if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
314             || IsEqualGUID(riid, &IID_IUnknown))
315     {
316         IUnknown_AddRef(iface);
317         *object = iface;
318         return S_OK;
319     }
320 
321     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
322 
323     *object = NULL;
324     return E_NOINTERFACE;
325 }
326 
327 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
328 {
329     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
330     ULONG refcount = InterlockedIncrement(&This->refcount);
331 
332     TRACE("%p increasing refcount to %u\n", This, refcount);
333 
334     return refcount;
335 }
336 
337 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
338 {
339     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
340     ULONG refcount = InterlockedDecrement(&This->refcount);
341 
342     TRACE("%p decreasing refcount to %u\n", This, refcount);
343 
344     if (!refcount)
345     {
346         reflection_cleanup(This);
347         HeapFree(GetProcessHeap(), 0, This);
348     }
349 
350     return refcount;
351 }
352 
353 /* ID3D11ShaderReflection methods */
354 
355 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
356 {
357     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
358 
359     FIXME("iface %p, desc %p partial stub!\n", iface, desc);
360 
361     if (!desc)
362     {
363         WARN("Invalid argument specified\n");
364         return E_FAIL;
365     }
366 
367     desc->Version = This->version;
368     desc->Creator = This->creator;
369     desc->Flags = This->flags;
370     desc->ConstantBuffers = This->constant_buffer_count;
371     desc->BoundResources = This->bound_resource_count;
372     desc->InputParameters = This->isgn ? This->isgn->element_count : 0;
373     desc->OutputParameters = This->osgn ? This->osgn->element_count : 0;
374     desc->InstructionCount = This->instruction_count;
375     desc->TempRegisterCount = This->temp_register_count;
376     desc->TempArrayCount = This->temp_array_count;
377     desc->DefCount = 0;
378     desc->DclCount = This->dcl_count;
379     desc->TextureNormalInstructions = This->texture_normal_instructions;
380     desc->TextureLoadInstructions = This->texture_load_instructions;
381     desc->TextureCompInstructions = This->texture_comp_instructions;
382     desc->TextureBiasInstructions = This->texture_bias_instructions;
383     desc->TextureGradientInstructions = This->texture_gradient_instructions;
384     desc->FloatInstructionCount = This->float_instruction_count;
385     desc->IntInstructionCount = This->int_instruction_count;
386     desc->UintInstructionCount = This->uint_instruction_count;
387     desc->StaticFlowControlCount = This->static_flow_control_count;
388     desc->DynamicFlowControlCount = This->dynamic_flow_control_count;
389     desc->MacroInstructionCount = 0;
390     desc->ArrayInstructionCount = This->array_instruction_count;
391     desc->CutInstructionCount = This->cut_instruction_count;
392     desc->EmitInstructionCount = This->emit_instruction_count;
393     desc->GSOutputTopology = This->gs_output_topology;
394     desc->GSMaxOutputVertexCount = This->gs_max_output_vertex_count;
395     desc->InputPrimitive = This->input_primitive;
396     desc->PatchConstantParameters = This->pcsg ? This->pcsg->element_count : 0;
397     desc->cGSInstanceCount = 0;
398     desc->cControlPoints = This->c_control_points;
399     desc->HSOutputPrimitive = This->hs_output_primitive;
400     desc->HSPartitioning = This->hs_prtitioning;
401     desc->TessellatorDomain = This->tessellator_domain;
402     desc->cBarrierInstructions = 0;
403     desc->cInterlockedInstructions = 0;
404     desc->cTextureStoreInstructions = 0;
405 
406     return S_OK;
407 }
408 
409 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
410         ID3D11ShaderReflection *iface, UINT index)
411 {
412     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
413 
414     TRACE("iface %p, index %u\n", iface, index);
415 
416     if (index >= This->constant_buffer_count)
417     {
418         WARN("Invalid argument specified\n");
419         return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
420     }
421 
422     return &This->constant_buffers[index].ID3D11ShaderReflectionConstantBuffer_iface;
423 }
424 
425 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
426         ID3D11ShaderReflection *iface, const char *name)
427 {
428     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
429     unsigned int i;
430 
431     TRACE("iface %p, name %s\n", iface, debugstr_a(name));
432 
433     if (!name)
434     {
435         WARN("Invalid argument specified\n");
436         return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
437     }
438 
439     for (i = 0; i < This->constant_buffer_count; ++i)
440     {
441         struct d3dcompiler_shader_reflection_constant_buffer *d = &This->constant_buffers[i];
442 
443         if (!strcmp(d->name, name))
444         {
445             TRACE("Returning ID3D11ShaderReflectionConstantBuffer %p.\n", d);
446             return &d->ID3D11ShaderReflectionConstantBuffer_iface;
447         }
448     }
449 
450     WARN("Invalid name specified\n");
451 
452     return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
453 }
454 
455 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
456         ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_DESC *desc)
457 {
458     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
459 
460     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
461 
462     if (!desc || index >= This->bound_resource_count)
463     {
464         WARN("Invalid argument specified\n");
465         return E_INVALIDARG;
466     }
467 
468     *desc = This->bound_resources[index];
469 
470     return S_OK;
471 }
472 
473 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
474         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
475 {
476     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
477 
478     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
479 
480     if (!desc || !This->isgn || index >= This->isgn->element_count)
481     {
482         WARN("Invalid argument specified\n");
483         return E_INVALIDARG;
484     }
485 
486     *desc = This->isgn->elements[index];
487 
488     return S_OK;
489 }
490 
491 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
492         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
493 {
494     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
495 
496     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
497 
498     if (!desc || !This->osgn || index >= This->osgn->element_count)
499     {
500         WARN("Invalid argument specified\n");
501         return E_INVALIDARG;
502     }
503 
504     *desc = This->osgn->elements[index];
505 
506     return S_OK;
507 }
508 
509 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
510         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
511 {
512     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
513 
514     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
515 
516     if (!desc || !This->pcsg || index >= This->pcsg->element_count)
517     {
518         WARN("Invalid argument specified\n");
519         return E_INVALIDARG;
520     }
521 
522     *desc = This->pcsg->elements[index];
523 
524     return S_OK;
525 }
526 
527 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
528         ID3D11ShaderReflection *iface, const char *name)
529 {
530     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
531     unsigned int i, k;
532 
533     TRACE("iface %p, name %s\n", iface, debugstr_a(name));
534 
535     if (!name)
536     {
537         WARN("Invalid name specified\n");
538         return &null_variable.ID3D11ShaderReflectionVariable_iface;
539     }
540 
541     for (i = 0; i < This->constant_buffer_count; ++i)
542     {
543         struct d3dcompiler_shader_reflection_constant_buffer *cb = &This->constant_buffers[i];
544 
545         for (k = 0; k < cb->variable_count; ++k)
546         {
547             struct d3dcompiler_shader_reflection_variable *v = &cb->variables[k];
548 
549             if (!strcmp(v->name, name))
550             {
551                 TRACE("Returning ID3D11ShaderReflectionVariable %p.\n", v);
552                 return &v->ID3D11ShaderReflectionVariable_iface;
553             }
554         }
555     }
556 
557     WARN("Invalid name specified\n");
558 
559     return &null_variable.ID3D11ShaderReflectionVariable_iface;
560 }
561 
562 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
563         ID3D11ShaderReflection *iface, const char *name, D3D11_SHADER_INPUT_BIND_DESC *desc)
564 {
565     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
566     unsigned int i;
567 
568     TRACE("iface %p, name %s, desc %p\n", iface, debugstr_a(name), desc);
569 
570     if (!desc || !name)
571     {
572         WARN("Invalid argument specified\n");
573         return E_INVALIDARG;
574     }
575 
576     for (i = 0; i < This->bound_resource_count; ++i)
577     {
578         D3D11_SHADER_INPUT_BIND_DESC *d = &This->bound_resources[i];
579 
580         if (!strcmp(d->Name, name))
581         {
582             TRACE("Returning D3D11_SHADER_INPUT_BIND_DESC %p.\n", d);
583             *desc = *d;
584             return S_OK;
585         }
586     }
587 
588     WARN("Invalid name specified\n");
589 
590     return E_INVALIDARG;
591 }
592 
593 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
594         ID3D11ShaderReflection *iface)
595 {
596     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
597 
598     TRACE("iface %p\n", iface);
599 
600     return This->mov_instruction_count;
601 }
602 
603 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
604         ID3D11ShaderReflection *iface)
605 {
606     FIXME("iface %p stub!\n", iface);
607 
608     return 0;
609 }
610 
611 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
612         ID3D11ShaderReflection *iface)
613 {
614     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
615 
616     TRACE("iface %p\n", iface);
617 
618     return This->conversion_instruction_count;
619 }
620 
621 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
622         ID3D11ShaderReflection *iface)
623 {
624     FIXME("iface %p stub!\n", iface);
625 
626     return 0;
627 }
628 
629 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
630         ID3D11ShaderReflection *iface)
631 {
632     FIXME("iface %p stub!\n", iface);
633 
634     return 0;
635 }
636 
637 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
638         ID3D11ShaderReflection *iface)
639 {
640     FIXME("iface %p stub!\n", iface);
641 
642     return FALSE;
643 }
644 
645 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
646         ID3D11ShaderReflection *iface)
647 {
648     FIXME("iface %p stub!\n", iface);
649 
650     return 0;
651 }
652 
653 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
654         ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
655 {
656     FIXME("iface %p, level %p stub!\n", iface, level);
657 
658     return E_NOTIMPL;
659 }
660 
661 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
662         ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
663 {
664     FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
665 
666     return 0;
667 }
668 
669 static UINT64 STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetRequiresFlags(
670         ID3D11ShaderReflection *iface)
671 {
672     FIXME("iface %p stub!\n", iface);
673 
674     return 0;
675 }
676 
677 static const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
678 {
679     /* IUnknown methods */
680     d3dcompiler_shader_reflection_QueryInterface,
681     d3dcompiler_shader_reflection_AddRef,
682     d3dcompiler_shader_reflection_Release,
683     /* ID3D11ShaderReflection methods */
684     d3dcompiler_shader_reflection_GetDesc,
685     d3dcompiler_shader_reflection_GetConstantBufferByIndex,
686     d3dcompiler_shader_reflection_GetConstantBufferByName,
687     d3dcompiler_shader_reflection_GetResourceBindingDesc,
688     d3dcompiler_shader_reflection_GetInputParameterDesc,
689     d3dcompiler_shader_reflection_GetOutputParameterDesc,
690     d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
691     d3dcompiler_shader_reflection_GetVariableByName,
692     d3dcompiler_shader_reflection_GetResourceBindingDescByName,
693     d3dcompiler_shader_reflection_GetMovInstructionCount,
694     d3dcompiler_shader_reflection_GetMovcInstructionCount,
695     d3dcompiler_shader_reflection_GetConversionInstructionCount,
696     d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
697     d3dcompiler_shader_reflection_GetGSInputPrimitive,
698     d3dcompiler_shader_reflection_IsSampleFrequencyShader,
699     d3dcompiler_shader_reflection_GetNumInterfaceSlots,
700     d3dcompiler_shader_reflection_GetMinFeatureLevel,
701     d3dcompiler_shader_reflection_GetThreadGroupSize,
702     d3dcompiler_shader_reflection_GetRequiresFlags,
703 };
704 
705 /* ID3D11ShaderReflectionConstantBuffer methods */
706 
707 static inline struct d3dcompiler_shader_reflection_constant_buffer *impl_from_ID3D11ShaderReflectionConstantBuffer(ID3D11ShaderReflectionConstantBuffer *iface)
708 {
709     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_constant_buffer, ID3D11ShaderReflectionConstantBuffer_iface);
710 }
711 
712 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetDesc(
713         ID3D11ShaderReflectionConstantBuffer *iface, D3D11_SHADER_BUFFER_DESC *desc)
714 {
715     struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
716 
717     TRACE("iface %p, desc %p\n", iface, desc);
718 
719     if (This == &null_constant_buffer)
720     {
721         WARN("Null constant buffer specified\n");
722         return E_FAIL;
723     }
724 
725     if (!desc)
726     {
727         WARN("Invalid argument specified\n");
728         return E_FAIL;
729     }
730 
731     desc->Name = This->name;
732     desc->Type = This->type;
733     desc->Variables = This->variable_count;
734     desc->Size = This->size;
735     desc->uFlags = This->flags;
736 
737     return S_OK;
738 }
739 
740 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex(
741         ID3D11ShaderReflectionConstantBuffer *iface, UINT index)
742 {
743     struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
744 
745     TRACE("iface %p, index %u\n", iface, index);
746 
747     if (index >= This->variable_count)
748     {
749         WARN("Invalid index specified\n");
750         return &null_variable.ID3D11ShaderReflectionVariable_iface;
751     }
752 
753     return &This->variables[index].ID3D11ShaderReflectionVariable_iface;
754 }
755 
756 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByName(
757         ID3D11ShaderReflectionConstantBuffer *iface, const char *name)
758 {
759     struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
760     unsigned int i;
761 
762     TRACE("iface %p, name %s\n", iface, debugstr_a(name));
763 
764     if (!name)
765     {
766         WARN("Invalid argument specified\n");
767         return &null_variable.ID3D11ShaderReflectionVariable_iface;
768     }
769 
770     for (i = 0; i < This->variable_count; ++i)
771     {
772         struct d3dcompiler_shader_reflection_variable *v = &This->variables[i];
773 
774         if (!strcmp(v->name, name))
775         {
776             TRACE("Returning ID3D11ShaderReflectionVariable %p.\n", v);
777             return &v->ID3D11ShaderReflectionVariable_iface;
778         }
779     }
780 
781     WARN("Invalid name specified\n");
782 
783     return &null_variable.ID3D11ShaderReflectionVariable_iface;
784 }
785 
786 static const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl =
787 {
788     /* ID3D11ShaderReflectionConstantBuffer methods */
789     d3dcompiler_shader_reflection_constant_buffer_GetDesc,
790     d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex,
791     d3dcompiler_shader_reflection_constant_buffer_GetVariableByName,
792 };
793 
794 /* ID3D11ShaderReflectionVariable methods */
795 
796 static inline struct d3dcompiler_shader_reflection_variable *impl_from_ID3D11ShaderReflectionVariable(ID3D11ShaderReflectionVariable *iface)
797 {
798     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_variable, ID3D11ShaderReflectionVariable_iface);
799 }
800 
801 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetDesc(
802         ID3D11ShaderReflectionVariable *iface, D3D11_SHADER_VARIABLE_DESC *desc)
803 {
804     struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
805 
806     TRACE("iface %p, desc %p\n", iface, desc);
807 
808     if (This == &null_variable)
809     {
810         WARN("Null variable specified\n");
811         return E_FAIL;
812     }
813 
814     if (!desc)
815     {
816         WARN("Invalid argument specified\n");
817         return E_FAIL;
818     }
819 
820     desc->Name = This->name;
821     desc->StartOffset = This->start_offset;
822     desc->Size = This->size;
823     desc->uFlags = This->flags;
824     desc->DefaultValue = This->default_value;
825 
826     return S_OK;
827 }
828 
829 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetType(
830         ID3D11ShaderReflectionVariable *iface)
831 {
832     struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
833 
834     TRACE("iface %p\n", iface);
835 
836     return &This->type->ID3D11ShaderReflectionType_iface;
837 }
838 
839 static ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetBuffer(
840         ID3D11ShaderReflectionVariable *iface)
841 {
842     struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
843 
844     TRACE("iface %p\n", iface);
845 
846     return &This->constant_buffer->ID3D11ShaderReflectionConstantBuffer_iface;
847 }
848 
849 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetInterfaceSlot(
850         ID3D11ShaderReflectionVariable *iface, UINT index)
851 {
852     FIXME("iface %p, index %u stub!\n", iface, index);
853 
854     return 0;
855 }
856 
857 static const struct ID3D11ShaderReflectionVariableVtbl d3dcompiler_shader_reflection_variable_vtbl =
858 {
859     /* ID3D11ShaderReflectionVariable methods */
860     d3dcompiler_shader_reflection_variable_GetDesc,
861     d3dcompiler_shader_reflection_variable_GetType,
862     d3dcompiler_shader_reflection_variable_GetBuffer,
863     d3dcompiler_shader_reflection_variable_GetInterfaceSlot,
864 };
865 
866 /* ID3D11ShaderReflectionType methods */
867 
868 static inline struct d3dcompiler_shader_reflection_type *impl_from_ID3D11ShaderReflectionType(ID3D11ShaderReflectionType *iface)
869 {
870     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_type, ID3D11ShaderReflectionType_iface);
871 }
872 
873 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetDesc(
874         ID3D11ShaderReflectionType *iface, D3D11_SHADER_TYPE_DESC *desc)
875 {
876     struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
877 
878     TRACE("iface %p, desc %p\n", iface, desc);
879 
880     if (This == &null_type)
881     {
882         WARN("Null type specified\n");
883         return E_FAIL;
884     }
885 
886     if (!desc)
887     {
888         WARN("Invalid argument specified\n");
889         return E_FAIL;
890     }
891 
892     *desc = This->desc;
893 
894     return S_OK;
895 }
896 
897 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeByIndex(
898         ID3D11ShaderReflectionType *iface, UINT index)
899 {
900     struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
901 
902     TRACE("iface %p, index %u\n", iface, index);
903 
904     if (index >= This->desc.Members)
905     {
906         WARN("Invalid index specified\n");
907         return &null_type.ID3D11ShaderReflectionType_iface;
908     }
909 
910     return &This->members[index].type->ID3D11ShaderReflectionType_iface;
911 }
912 
913 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeByName(
914         ID3D11ShaderReflectionType *iface, const char *name)
915 {
916     struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
917     unsigned int i;
918 
919     TRACE("iface %p, name %s\n", iface, debugstr_a(name));
920 
921     if (!name)
922     {
923         WARN("Invalid argument specified\n");
924         return &null_type.ID3D11ShaderReflectionType_iface;
925     }
926 
927     for (i = 0; i < This->desc.Members; ++i)
928     {
929         struct d3dcompiler_shader_reflection_type_member *member = &This->members[i];
930 
931         if (!strcmp(member->name, name))
932         {
933             TRACE("Returning ID3D11ShaderReflectionType %p.\n", member->type);
934             return &member->type->ID3D11ShaderReflectionType_iface;
935         }
936     }
937 
938     WARN("Invalid name specified\n");
939 
940     return &null_type.ID3D11ShaderReflectionType_iface;
941 }
942 
943 static const char * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeName(
944         ID3D11ShaderReflectionType *iface, UINT index)
945 {
946     struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
947 
948     TRACE("iface %p, index %u\n", iface, index);
949 
950     if (This == &null_type)
951     {
952         WARN("Null type specified\n");
953         return "$Invalid";
954     }
955 
956     if (index >= This->desc.Members)
957     {
958         WARN("Invalid index specified\n");
959         return NULL;
960     }
961 
962     return This->members[index].name;
963 }
964 
965 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_IsEqual(
966         ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *type)
967 {
968     struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
969 
970     TRACE("iface %p, type %p\n", iface, type);
971 
972     if (This == &null_type)
973     {
974         WARN("Null type specified\n");
975         return E_FAIL;
976     }
977 
978     if (iface == type)
979         return S_OK;
980 
981     return S_FALSE;
982 }
983 
984 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetSubType(
985         ID3D11ShaderReflectionType *iface)
986 {
987     FIXME("iface %p stub!\n", iface);
988 
989     return NULL;
990 }
991 
992 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetBaseClass(
993         ID3D11ShaderReflectionType *iface)
994 {
995     FIXME("iface %p stub!\n", iface);
996 
997     return NULL;
998 }
999 
1000 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetNumInterfaces(
1001         ID3D11ShaderReflectionType *iface)
1002 {
1003     FIXME("iface %p stub!\n", iface);
1004 
1005     return 0;
1006 }
1007 
1008 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetInterfaceByIndex(
1009         ID3D11ShaderReflectionType *iface, UINT index)
1010 {
1011     FIXME("iface %p, index %u stub!\n", iface, index);
1012 
1013     return NULL;
1014 }
1015 
1016 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_IsOfType(
1017         ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *type)
1018 {
1019     FIXME("iface %p, type %p stub!\n", iface, type);
1020 
1021     return E_NOTIMPL;
1022 }
1023 
1024 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_ImplementsInterface(
1025         ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *base)
1026 {
1027     FIXME("iface %p, base %p stub!\n", iface, base);
1028 
1029     return E_NOTIMPL;
1030 }
1031 
1032 static const struct ID3D11ShaderReflectionTypeVtbl d3dcompiler_shader_reflection_type_vtbl =
1033 {
1034     /* ID3D11ShaderReflectionType methods */
1035     d3dcompiler_shader_reflection_type_GetDesc,
1036     d3dcompiler_shader_reflection_type_GetMemberTypeByIndex,
1037     d3dcompiler_shader_reflection_type_GetMemberTypeByName,
1038     d3dcompiler_shader_reflection_type_GetMemberTypeName,
1039     d3dcompiler_shader_reflection_type_IsEqual,
1040     d3dcompiler_shader_reflection_type_GetSubType,
1041     d3dcompiler_shader_reflection_type_GetBaseClass,
1042     d3dcompiler_shader_reflection_type_GetNumInterfaces,
1043     d3dcompiler_shader_reflection_type_GetInterfaceByIndex,
1044     d3dcompiler_shader_reflection_type_IsOfType,
1045     d3dcompiler_shader_reflection_type_ImplementsInterface,
1046 };
1047 
1048 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1049 {
1050     const char *ptr = data;
1051     DWORD size = data_size >> 2;
1052 
1053     TRACE("Size %u\n", size);
1054 
1055     read_dword(&ptr, &r->instruction_count);
1056     TRACE("InstructionCount: %u\n", r->instruction_count);
1057 
1058     read_dword(&ptr, &r->temp_register_count);
1059     TRACE("TempRegisterCount: %u\n", r->temp_register_count);
1060 
1061     skip_dword_unknown(&ptr, 1);
1062 
1063     read_dword(&ptr, &r->dcl_count);
1064     TRACE("DclCount: %u\n", r->dcl_count);
1065 
1066     read_dword(&ptr, &r->float_instruction_count);
1067     TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
1068 
1069     read_dword(&ptr, &r->int_instruction_count);
1070     TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
1071 
1072     read_dword(&ptr, &r->uint_instruction_count);
1073     TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
1074 
1075     read_dword(&ptr, &r->static_flow_control_count);
1076     TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
1077 
1078     read_dword(&ptr, &r->dynamic_flow_control_count);
1079     TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
1080 
1081     skip_dword_unknown(&ptr, 1);
1082 
1083     read_dword(&ptr, &r->temp_array_count);
1084     TRACE("TempArrayCount: %u\n", r->temp_array_count);
1085 
1086     read_dword(&ptr, &r->array_instruction_count);
1087     TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
1088 
1089     read_dword(&ptr, &r->cut_instruction_count);
1090     TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
1091 
1092     read_dword(&ptr, &r->emit_instruction_count);
1093     TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
1094 
1095     read_dword(&ptr, &r->texture_normal_instructions);
1096     TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
1097 
1098     read_dword(&ptr, &r->texture_load_instructions);
1099     TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
1100 
1101     read_dword(&ptr, &r->texture_comp_instructions);
1102     TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
1103 
1104     read_dword(&ptr, &r->texture_bias_instructions);
1105     TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
1106 
1107     read_dword(&ptr, &r->texture_gradient_instructions);
1108     TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
1109 
1110     read_dword(&ptr, &r->mov_instruction_count);
1111     TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
1112 
1113     skip_dword_unknown(&ptr, 1);
1114 
1115     read_dword(&ptr, &r->conversion_instruction_count);
1116     TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
1117 
1118     skip_dword_unknown(&ptr, 1);
1119 
1120 #ifdef __REACTOS__ /* DWORD* cast added */
1121     read_dword(&ptr, (DWORD*)&r->input_primitive);
1122 #else
1123     read_dword(&ptr, &r->input_primitive);
1124 #endif
1125     TRACE("InputPrimitive: %x\n", r->input_primitive);
1126 
1127 #ifdef __REACTOS__ /* DWORD* cast added */
1128     read_dword(&ptr, (DWORD*)&r->gs_output_topology);
1129 #else
1130     read_dword(&ptr, &r->gs_output_topology);
1131 #endif
1132     TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
1133 
1134     read_dword(&ptr, &r->gs_max_output_vertex_count);
1135     TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
1136 
1137     skip_dword_unknown(&ptr, 2);
1138 
1139     /* old dx10 stat size */
1140     if (size == 28) return S_OK;
1141 
1142     skip_dword_unknown(&ptr, 1);
1143 
1144     /* dx10 stat size */
1145     if (size == 29) return S_OK;
1146 
1147     skip_dword_unknown(&ptr, 1);
1148 
1149     read_dword(&ptr, &r->c_control_points);
1150     TRACE("cControlPoints: %u\n", r->c_control_points);
1151 
1152 #ifdef __REACTOS__ /* DWORD* cast added */
1153     read_dword(&ptr, (DWORD*)&r->hs_output_primitive);
1154 #else
1155     read_dword(&ptr, &r->hs_output_primitive);
1156 #endif
1157     TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
1158 
1159 #ifdef __REACTOS__ /* DWORD* cast added */
1160     read_dword(&ptr, (DWORD*)&r->hs_prtitioning);
1161 #else
1162     read_dword(&ptr, &r->hs_prtitioning);
1163 #endif
1164     TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
1165 
1166 #ifdef __REACTOS__ /* DWORD* cast added */
1167     read_dword(&ptr, (DWORD*)&r->tessellator_domain);
1168 #else
1169     read_dword(&ptr, &r->tessellator_domain);
1170 #endif
1171     TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
1172 
1173     skip_dword_unknown(&ptr, 3);
1174 
1175     /* dx11 stat size */
1176     if (size == 37) return S_OK;
1177 
1178     FIXME("Unhandled size %u\n", size);
1179 
1180     return E_FAIL;
1181 }
1182 
1183 static HRESULT d3dcompiler_parse_type_members(struct d3dcompiler_shader_reflection *ref,
1184         struct d3dcompiler_shader_reflection_type_member *member, const char *data, const char **ptr)
1185 {
1186     DWORD offset;
1187 
1188     read_dword(ptr, &offset);
1189     if (!copy_name(data + offset, &member->name))
1190     {
1191         ERR("Failed to copy name.\n");
1192         return E_OUTOFMEMORY;
1193     }
1194     TRACE("Member name: %s.\n", debugstr_a(member->name));
1195 
1196     read_dword(ptr, &offset);
1197     TRACE("Member type offset: %x\n", offset);
1198 
1199     member->type = get_reflection_type(ref, data, offset);
1200     if (!member->type)
1201     {
1202         ERR("Failed to get member type\n");
1203         HeapFree(GetProcessHeap(), 0, member->name);
1204         return E_FAIL;
1205     }
1206 
1207     read_dword(ptr, &member->offset);
1208     TRACE("Member offset %x\n", member->offset);
1209 
1210     return S_OK;
1211 }
1212 
1213 static HRESULT d3dcompiler_parse_type(struct d3dcompiler_shader_reflection_type *type, const char *data, DWORD offset)
1214 {
1215     const char *ptr = data + offset;
1216     DWORD temp;
1217     D3D11_SHADER_TYPE_DESC *desc;
1218     unsigned int i;
1219     struct d3dcompiler_shader_reflection_type_member *members = NULL;
1220     HRESULT hr;
1221     DWORD member_offset;
1222 
1223     desc = &type->desc;
1224 
1225     read_dword(&ptr, &temp);
1226     desc->Class = temp & 0xffff;
1227     desc->Type = temp >> 16;
1228     TRACE("Class %s, Type %s\n", debug_d3dcompiler_shader_variable_class(desc->Class),
1229             debug_d3dcompiler_shader_variable_type(desc->Type));
1230 
1231     read_dword(&ptr, &temp);
1232     desc->Rows = temp & 0xffff;
1233     desc->Columns = temp >> 16;
1234     TRACE("Rows %u, Columns %u\n", desc->Rows, desc->Columns);
1235 
1236     read_dword(&ptr, &temp);
1237     desc->Elements = temp & 0xffff;
1238     desc->Members = temp >> 16;
1239     TRACE("Elements %u, Members %u\n", desc->Elements, desc->Members);
1240 
1241     read_dword(&ptr, &member_offset);
1242     TRACE("Member Offset %u\n", member_offset);
1243 
1244     if ((type->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1245         skip_dword_unknown(&ptr, 4);
1246 
1247     if (desc->Members)
1248     {
1249         const char *ptr2 = data + member_offset;
1250 
1251         members = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*members) * desc->Members);
1252         if (!members)
1253         {
1254             ERR("Failed to allocate type memory.\n");
1255             return E_OUTOFMEMORY;
1256         }
1257 
1258         for (i = 0; i < desc->Members; ++i)
1259         {
1260             hr = d3dcompiler_parse_type_members(type->reflection, &members[i], data, &ptr2);
1261             if (hr != S_OK)
1262             {
1263                 FIXME("Failed to parse type members.\n");
1264                 goto err_out;
1265             }
1266         }
1267     }
1268 
1269     if ((type->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1270     {
1271         read_dword(&ptr, &offset);
1272         if (!copy_name(data + offset, &type->name))
1273         {
1274             ERR("Failed to copy name.\n");
1275             heap_free(members);
1276             return E_OUTOFMEMORY;
1277         }
1278         desc->Name = type->name;
1279         TRACE("Type name: %s.\n", debugstr_a(type->name));
1280     }
1281 
1282     type->members = members;
1283 
1284     return S_OK;
1285 
1286 err_out:
1287     for (i = 0; i < desc->Members; ++i)
1288     {
1289         free_type_member(&members[i]);
1290     }
1291     HeapFree(GetProcessHeap(), 0, members);
1292     return hr;
1293 }
1294 
1295 static struct d3dcompiler_shader_reflection_type *get_reflection_type(struct d3dcompiler_shader_reflection *reflection, const char *data, DWORD offset)
1296 {
1297     struct d3dcompiler_shader_reflection_type *type;
1298     struct wine_rb_entry *entry;
1299     HRESULT hr;
1300 
1301     entry = wine_rb_get(&reflection->types, &offset);
1302     if (entry)
1303     {
1304         TRACE("Returning existing type.\n");
1305         return WINE_RB_ENTRY_VALUE(entry, struct d3dcompiler_shader_reflection_type, entry);
1306     }
1307 
1308     type = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*type));
1309     if (!type)
1310         return NULL;
1311 
1312     type->ID3D11ShaderReflectionType_iface.lpVtbl = &d3dcompiler_shader_reflection_type_vtbl;
1313     type->id = offset;
1314     type->reflection = reflection;
1315 
1316     hr = d3dcompiler_parse_type(type, data, offset);
1317     if (FAILED(hr))
1318     {
1319         ERR("Failed to parse type info, hr %#x.\n", hr);
1320         HeapFree(GetProcessHeap(), 0, type);
1321         return NULL;
1322     }
1323 
1324     if (wine_rb_put(&reflection->types, &offset, &type->entry) == -1)
1325     {
1326         ERR("Failed to insert type entry.\n");
1327         HeapFree(GetProcessHeap(), 0, type);
1328         return NULL;
1329     }
1330 
1331     return type;
1332 }
1333 
1334 static HRESULT d3dcompiler_parse_variables(struct d3dcompiler_shader_reflection_constant_buffer *cb,
1335         const char *data, DWORD data_size, const char *ptr)
1336 {
1337     struct d3dcompiler_shader_reflection_variable *variables;
1338     unsigned int i;
1339     HRESULT hr;
1340 
1341     variables = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb->variable_count * sizeof(*variables));
1342     if (!variables)
1343     {
1344         ERR("Failed to allocate variables memory.\n");
1345         return E_OUTOFMEMORY;
1346     }
1347 
1348     for (i = 0; i < cb->variable_count; i++)
1349     {
1350         struct d3dcompiler_shader_reflection_variable *v = &variables[i];
1351         DWORD offset;
1352 
1353         v->ID3D11ShaderReflectionVariable_iface.lpVtbl = &d3dcompiler_shader_reflection_variable_vtbl;
1354         v->constant_buffer = cb;
1355 
1356         read_dword(&ptr, &offset);
1357         if (!copy_name(data + offset, &v->name))
1358         {
1359             ERR("Failed to copy name.\n");
1360             hr = E_OUTOFMEMORY;
1361             goto err_out;
1362         }
1363         TRACE("Variable name: %s.\n", debugstr_a(v->name));
1364 
1365         read_dword(&ptr, &v->start_offset);
1366         TRACE("Variable offset: %u\n", v->start_offset);
1367 
1368         read_dword(&ptr, &v->size);
1369         TRACE("Variable size: %u\n", v->size);
1370 
1371         read_dword(&ptr, &v->flags);
1372         TRACE("Variable flags: %u\n", v->flags);
1373 
1374         read_dword(&ptr, &offset);
1375         TRACE("Variable type offset: %x\n", offset);
1376         v->type = get_reflection_type(cb->reflection, data, offset);
1377         if (!v->type)
1378         {
1379             ERR("Failed to get type.\n");
1380             hr = E_FAIL;
1381             goto err_out;
1382         }
1383 
1384         read_dword(&ptr, &offset);
1385         TRACE("Variable default value offset: %x\n", offset);
1386         if (!copy_value(data + offset, &v->default_value, offset ? v->size : 0))
1387         {
1388             ERR("Failed to copy name.\n");
1389             hr = E_OUTOFMEMORY;
1390             goto err_out;
1391         }
1392 
1393         if ((cb->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1394             skip_dword_unknown(&ptr, 4);
1395     }
1396 
1397     cb->variables = variables;
1398 
1399     return S_OK;
1400 
1401 err_out:
1402     for (i = 0; i < cb->variable_count; i++)
1403     {
1404         free_variable(&variables[i]);
1405     }
1406     HeapFree(GetProcessHeap(), 0, variables);
1407     return hr;
1408 }
1409 
1410 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1411 {
1412     const char *ptr = data;
1413     DWORD size = data_size >> 2;
1414     DWORD offset, cbuffer_offset, resource_offset, creator_offset;
1415     unsigned int i, string_data_offset, string_data_size;
1416     char *string_data = NULL, *creator = NULL;
1417     D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
1418     struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers = NULL;
1419     HRESULT hr;
1420 
1421     TRACE("Size %u\n", size);
1422 
1423     read_dword(&ptr, &r->constant_buffer_count);
1424     TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
1425 
1426     read_dword(&ptr, &cbuffer_offset);
1427     TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
1428 
1429     read_dword(&ptr, &r->bound_resource_count);
1430     TRACE("Bound resource count: %u\n", r->bound_resource_count);
1431 
1432     read_dword(&ptr, &resource_offset);
1433     TRACE("Bound resource offset: %#x\n", resource_offset);
1434 
1435     read_dword(&ptr, &r->target);
1436     TRACE("Target: %#x\n", r->target);
1437 
1438     read_dword(&ptr, &r->flags);
1439     TRACE("Flags: %u\n", r->flags);
1440 
1441     read_dword(&ptr, &creator_offset);
1442     TRACE("Creator at offset %#x.\n", creator_offset);
1443 
1444     if (!copy_name(data + creator_offset, &creator))
1445     {
1446         ERR("Failed to copy name.\n");
1447         return E_OUTOFMEMORY;
1448     }
1449     TRACE("Creator: %s.\n", debugstr_a(creator));
1450 
1451     /* todo: Parse RD11 */
1452     if ((r->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1453     {
1454         skip_dword_unknown(&ptr, 8);
1455     }
1456 
1457     if (r->bound_resource_count)
1458     {
1459         /* 8 for each bind desc */
1460         string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
1461         string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
1462 
1463         string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1464         if (!string_data)
1465         {
1466             ERR("Failed to allocate string data memory.\n");
1467             hr = E_OUTOFMEMORY;
1468             goto err_out;
1469         }
1470         memcpy(string_data, data + string_data_offset, string_data_size);
1471 
1472         bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
1473         if (!bound_resources)
1474         {
1475             ERR("Failed to allocate resources memory.\n");
1476             hr = E_OUTOFMEMORY;
1477             goto err_out;
1478         }
1479 
1480         ptr = data + resource_offset;
1481         for (i = 0; i < r->bound_resource_count; i++)
1482         {
1483             D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
1484 
1485             read_dword(&ptr, &offset);
1486             desc->Name = string_data + (offset - string_data_offset);
1487             TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
1488 
1489 #ifdef __REACTOS__ /* DWORD* cast added */
1490             read_dword(&ptr, (DWORD*)&desc->Type);
1491 #else
1492             read_dword(&ptr, &desc->Type);
1493 #endif
1494             TRACE("Input bind Type: %#x\n", desc->Type);
1495 
1496 #ifdef __REACTOS__ /* DWORD* cast added */
1497             read_dword(&ptr, (DWORD*)&desc->ReturnType);
1498 #else
1499             read_dword(&ptr, &desc->ReturnType);
1500 #endif
1501             TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
1502 
1503 #ifdef __REACTOS__ /* DWORD* cast added */
1504             read_dword(&ptr, (DWORD*)&desc->Dimension);
1505 #else
1506             read_dword(&ptr, &desc->Dimension);
1507 #endif
1508             TRACE("Input bind Dimension: %#x\n", desc->Dimension);
1509 
1510             read_dword(&ptr, &desc->NumSamples);
1511             TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
1512 
1513             read_dword(&ptr, &desc->BindPoint);
1514             TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
1515 
1516             read_dword(&ptr, &desc->BindCount);
1517             TRACE("Input bind BindCount: %u\n", desc->BindCount);
1518 
1519             read_dword(&ptr, &desc->uFlags);
1520             TRACE("Input bind uFlags: %u\n", desc->uFlags);
1521         }
1522     }
1523 
1524     if (r->constant_buffer_count)
1525     {
1526         constant_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, r->constant_buffer_count * sizeof(*constant_buffers));
1527         if (!constant_buffers)
1528         {
1529             ERR("Failed to allocate constant buffer memory.\n");
1530             hr = E_OUTOFMEMORY;
1531             goto err_out;
1532         }
1533 
1534         ptr = data + cbuffer_offset;
1535         for (i = 0; i < r->constant_buffer_count; i++)
1536         {
1537             struct d3dcompiler_shader_reflection_constant_buffer *cb = &constant_buffers[i];
1538 
1539             cb->ID3D11ShaderReflectionConstantBuffer_iface.lpVtbl = &d3dcompiler_shader_reflection_constant_buffer_vtbl;
1540             cb->reflection = r;
1541 
1542             read_dword(&ptr, &offset);
1543             if (!copy_name(data + offset, &cb->name))
1544             {
1545                 ERR("Failed to copy name.\n");
1546                 hr = E_OUTOFMEMORY;
1547                 goto err_out;
1548             }
1549             TRACE("Name: %s.\n", debugstr_a(cb->name));
1550 
1551             read_dword(&ptr, &cb->variable_count);
1552             TRACE("Variable count: %u\n", cb->variable_count);
1553 
1554             read_dword(&ptr, &offset);
1555             TRACE("Variable offset: %x\n", offset);
1556 
1557             hr = d3dcompiler_parse_variables(cb, data, data_size, data + offset);
1558             if (hr != S_OK)
1559             {
1560                 FIXME("Failed to parse variables.\n");
1561                 goto err_out;
1562             }
1563 
1564             read_dword(&ptr, &cb->size);
1565             TRACE("Cbuffer size: %u\n", cb->size);
1566 
1567             read_dword(&ptr, &cb->flags);
1568             TRACE("Cbuffer flags: %u\n", cb->flags);
1569 
1570 #ifdef __REACTOS__ /* DWORD* cast added */
1571             read_dword(&ptr, (DWORD*)&cb->type);
1572 #else
1573             read_dword(&ptr, &cb->type);
1574 #endif
1575             TRACE("Cbuffer type: %#x\n", cb->type);
1576         }
1577     }
1578 
1579     r->creator = creator;
1580     r->resource_string = string_data;
1581     r->bound_resources = bound_resources;
1582     r->constant_buffers = constant_buffers;
1583 
1584     return S_OK;
1585 
1586 err_out:
1587     for (i = 0; i < r->constant_buffer_count; ++i)
1588     {
1589         free_constant_buffer(&constant_buffers[i]);
1590     }
1591     HeapFree(GetProcessHeap(), 0, constant_buffers);
1592     HeapFree(GetProcessHeap(), 0, bound_resources);
1593     HeapFree(GetProcessHeap(), 0, string_data);
1594     HeapFree(GetProcessHeap(), 0, creator);
1595 
1596     return hr;
1597 }
1598 
1599 static HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section, DWORD target)
1600 {
1601     D3D11_SIGNATURE_PARAMETER_DESC *d;
1602     unsigned int string_data_offset;
1603     unsigned int string_data_size;
1604     const char *ptr = section->data;
1605     char *string_data;
1606     unsigned int i;
1607     DWORD count;
1608     enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
1609 
1610     switch (section->tag)
1611     {
1612         case TAG_OSG5:
1613             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
1614             break;
1615 
1616         case TAG_ISGN:
1617         case TAG_OSGN:
1618         case TAG_PCSG:
1619             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1620             break;
1621 
1622         default:
1623             FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1624             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1625             break;
1626     }
1627 
1628     read_dword(&ptr, &count);
1629     TRACE("%u elements\n", count);
1630 
1631     skip_dword_unknown(&ptr, 1);
1632 
1633     d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
1634     if (!d)
1635     {
1636         ERR("Failed to allocate signature memory.\n");
1637         return E_OUTOFMEMORY;
1638     }
1639 
1640     /* 2 DWORDs for the header, element_size for each element. */
1641     string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
1642     string_data_size = section->data_size - string_data_offset;
1643 
1644     string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1645     if (!string_data)
1646     {
1647         ERR("Failed to allocate string data memory.\n");
1648         HeapFree(GetProcessHeap(), 0, d);
1649         return E_OUTOFMEMORY;
1650     }
1651     memcpy(string_data, section->data + string_data_offset, string_data_size);
1652 
1653     for (i = 0; i < count; ++i)
1654     {
1655         UINT name_offset;
1656         DWORD mask;
1657 
1658         if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
1659         {
1660             read_dword(&ptr, &d[i].Stream);
1661         }
1662         else
1663         {
1664             d[i].Stream = 0;
1665         }
1666 
1667         read_dword(&ptr, &name_offset);
1668         d[i].SemanticName = string_data + (name_offset - string_data_offset);
1669         read_dword(&ptr, &d[i].SemanticIndex);
1670 #ifdef __REACTOS__ /* DWORD* casts added */
1671         read_dword(&ptr, (DWORD*)&d[i].SystemValueType);
1672         read_dword(&ptr, (DWORD*)&d[i].ComponentType);
1673 #else
1674         read_dword(&ptr, &d[i].SystemValueType);
1675         read_dword(&ptr, &d[i].ComponentType);
1676 #endif
1677         read_dword(&ptr, &d[i].Register);
1678         read_dword(&ptr, &mask);
1679         d[i].ReadWriteMask = (mask >> 8) & 0xff;
1680         d[i].Mask = mask & 0xff;
1681 
1682         /* pixel shaders have a special handling for SystemValueType in the output signature */
1683         if (((target & D3DCOMPILER_SHADER_TARGET_SHADERTYPE_MASK) == 0xffff0000) && (section->tag == TAG_OSG5 || section->tag == TAG_OSGN))
1684         {
1685             TRACE("Pixelshader output signature fixup.\n");
1686 
1687             if (d[i].Register == 0xffffffff)
1688             {
1689                 if (!_strnicmp(d[i].SemanticName, "sv_depth", -1))
1690                     d[i].SystemValueType = D3D_NAME_DEPTH;
1691                 else if (!_strnicmp(d[i].SemanticName, "sv_coverage", -1))
1692                     d[i].SystemValueType = D3D_NAME_COVERAGE;
1693                 else if (!_strnicmp(d[i].SemanticName, "sv_depthgreaterequal", -1))
1694                     d[i].SystemValueType = D3D_NAME_DEPTH_GREATER_EQUAL;
1695                 else if (!_strnicmp(d[i].SemanticName, "sv_depthlessequal", -1))
1696                     d[i].SystemValueType = D3D_NAME_DEPTH_LESS_EQUAL;
1697             }
1698             else
1699             {
1700                 d[i].SystemValueType = D3D_NAME_TARGET;
1701             }
1702         }
1703 
1704         TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
1705                 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
1706                 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
1707                 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
1708     }
1709 
1710     s->elements = d;
1711     s->element_count = count;
1712     s->string_data = string_data;
1713 
1714     return S_OK;
1715 }
1716 
1717 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1718 {
1719     const char *ptr = data;
1720 
1721     read_dword(&ptr, &r->version);
1722     TRACE("Shader version: %u\n", r->version);
1723 
1724     /* todo: Check if anything else is needed from the shdr or shex blob. */
1725 
1726     return S_OK;
1727 }
1728 
1729 static HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
1730         const void *data, SIZE_T data_size)
1731 {
1732     struct dxbc src_dxbc;
1733     HRESULT hr;
1734     unsigned int i;
1735 
1736     reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
1737     reflection->refcount = 1;
1738 
1739     wine_rb_init(&reflection->types, d3dcompiler_shader_reflection_type_compare);
1740 
1741     hr = dxbc_parse(data, data_size, &src_dxbc);
1742     if (FAILED(hr))
1743     {
1744         WARN("Failed to parse reflection\n");
1745         return hr;
1746     }
1747 
1748     for (i = 0; i < src_dxbc.count; ++i)
1749     {
1750         struct dxbc_section *section = &src_dxbc.sections[i];
1751 
1752         switch (section->tag)
1753         {
1754             case TAG_RDEF:
1755                 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
1756                 if (FAILED(hr))
1757                 {
1758                     WARN("Failed to parse RDEF section.\n");
1759                     goto err_out;
1760                 }
1761                 break;
1762 
1763             case TAG_ISGN:
1764                 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
1765                 if (!reflection->isgn)
1766                 {
1767                     ERR("Failed to allocate ISGN memory.\n");
1768                     hr = E_OUTOFMEMORY;
1769                     goto err_out;
1770                 }
1771 
1772                 hr = d3dcompiler_parse_signature(reflection->isgn, section, reflection->target);
1773                 if (FAILED(hr))
1774                 {
1775                     WARN("Failed to parse section ISGN.\n");
1776                     goto err_out;
1777                 }
1778                 break;
1779 
1780             case TAG_OSG5:
1781             case TAG_OSGN:
1782                 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
1783                 if (!reflection->osgn)
1784                 {
1785                     ERR("Failed to allocate OSGN memory.\n");
1786                     hr = E_OUTOFMEMORY;
1787                     goto err_out;
1788                 }
1789 
1790                 hr = d3dcompiler_parse_signature(reflection->osgn, section, reflection->target);
1791                 if (FAILED(hr))
1792                 {
1793                     WARN("Failed to parse section OSGN.\n");
1794                     goto err_out;
1795                 }
1796                 break;
1797 
1798             case TAG_PCSG:
1799                 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
1800                 if (!reflection->pcsg)
1801                 {
1802                     ERR("Failed to allocate PCSG memory.\n");
1803                     hr = E_OUTOFMEMORY;
1804                     goto err_out;
1805                 }
1806 
1807                 hr = d3dcompiler_parse_signature(reflection->pcsg, section, reflection->target);
1808                 if (FAILED(hr))
1809                 {
1810                     WARN("Failed to parse section PCSG.\n");
1811                     goto err_out;
1812                 }
1813                 break;
1814 
1815             case TAG_SHEX:
1816             case TAG_SHDR:
1817                 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
1818                 if (FAILED(hr))
1819                 {
1820                     WARN("Failed to parse SHDR section.\n");
1821                     goto err_out;
1822                 }
1823                 break;
1824 
1825             case TAG_STAT:
1826                 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
1827                 if (FAILED(hr))
1828                 {
1829                     WARN("Failed to parse section STAT.\n");
1830                     goto err_out;
1831                 }
1832                 break;
1833 
1834             default:
1835                 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1836                 break;
1837         }
1838     }
1839 
1840     dxbc_destroy(&src_dxbc);
1841 
1842     return hr;
1843 
1844 err_out:
1845     reflection_cleanup(reflection);
1846     dxbc_destroy(&src_dxbc);
1847 
1848     return hr;
1849 }
1850 
1851 HRESULT WINAPI D3DReflect(const void *data, SIZE_T data_size, REFIID riid, void **reflector)
1852 {
1853     struct d3dcompiler_shader_reflection *object;
1854     HRESULT hr;
1855     const DWORD *temp = data;
1856 
1857     TRACE("data %p, data_size %lu, riid %s, blob %p\n", data, data_size, debugstr_guid(riid), reflector);
1858 
1859     if (!data || data_size < 32)
1860     {
1861         WARN("Invalid argument supplied.\n");
1862         return D3DERR_INVALIDCALL;
1863     }
1864 
1865     if (temp[6] != data_size)
1866     {
1867         WARN("Wrong size supplied.\n");
1868         return E_FAIL;
1869     }
1870 
1871     if (!IsEqualGUID(riid, &IID_ID3D11ShaderReflection))
1872     {
1873         WARN("Wrong riid %s, accept only %s!\n", debugstr_guid(riid), debugstr_guid(&IID_ID3D11ShaderReflection));
1874         return E_NOINTERFACE;
1875     }
1876 
1877     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1878     if (!object)
1879         return E_OUTOFMEMORY;
1880 
1881     hr = d3dcompiler_shader_reflection_init(object, data, data_size);
1882     if (FAILED(hr))
1883     {
1884         WARN("Failed to initialize shader reflection\n");
1885         HeapFree(GetProcessHeap(), 0, object);
1886         return hr;
1887     }
1888 
1889     *reflector = object;
1890 
1891     TRACE("Created ID3D11ShaderReflection %p\n", object);
1892 
1893     return S_OK;
1894 }
1895