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