Release 1.5.29.
[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         return NULL;
1286
1287     type->ID3D11ShaderReflectionType_iface.lpVtbl = &d3dcompiler_shader_reflection_type_vtbl;
1288     type->id = offset;
1289     type->reflection = reflection;
1290
1291     hr = d3dcompiler_parse_type(type, data, offset);
1292     if (FAILED(hr))
1293     {
1294         ERR("Failed to parse type info, hr %#x.\n", hr);
1295         HeapFree(GetProcessHeap(), 0, type);
1296         return NULL;
1297     }
1298
1299     if (wine_rb_put(&reflection->types, &offset, &type->entry) == -1)
1300     {
1301         ERR("Failed to insert type entry.\n");
1302         HeapFree(GetProcessHeap(), 0, type);
1303         return NULL;
1304     }
1305
1306     return type;
1307 }
1308
1309 static HRESULT d3dcompiler_parse_variables(struct d3dcompiler_shader_reflection_constant_buffer *cb,
1310         const char *data, DWORD data_size, const char *ptr)
1311 {
1312     struct d3dcompiler_shader_reflection_variable *variables;
1313     unsigned int i;
1314     HRESULT hr;
1315
1316     variables = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb->variable_count * sizeof(*variables));
1317     if (!variables)
1318     {
1319         ERR("Failed to allocate variables memory.\n");
1320         return E_OUTOFMEMORY;
1321     }
1322
1323     for (i = 0; i < cb->variable_count; i++)
1324     {
1325         struct d3dcompiler_shader_reflection_variable *v = &variables[i];
1326         DWORD offset;
1327
1328         v->ID3D11ShaderReflectionVariable_iface.lpVtbl = &d3dcompiler_shader_reflection_variable_vtbl;
1329         v->constant_buffer = cb;
1330
1331         read_dword(&ptr, &offset);
1332         if (!copy_name(data + offset, &v->name))
1333         {
1334             ERR("Failed to copy name.\n");
1335             hr = E_OUTOFMEMORY;
1336             goto err_out;
1337         }
1338         TRACE("Variable name: %s.\n", debugstr_a(v->name));
1339
1340         read_dword(&ptr, &v->start_offset);
1341         TRACE("Variable offset: %u\n", v->start_offset);
1342
1343         read_dword(&ptr, &v->size);
1344         TRACE("Variable size: %u\n", v->size);
1345
1346         read_dword(&ptr, &v->flags);
1347         TRACE("Variable flags: %u\n", v->flags);
1348
1349         read_dword(&ptr, &offset);
1350         TRACE("Variable type offset: %x\n", offset);
1351         v->type = get_reflection_type(cb->reflection, data, offset);
1352         if (!v->type)
1353         {
1354             ERR("Failed to get type.\n");
1355             hr = E_FAIL;
1356             goto err_out;
1357         }
1358
1359         read_dword(&ptr, &offset);
1360         TRACE("Variable default value offset: %x\n", offset);
1361         if (!copy_value(data + offset, &v->default_value, offset ? v->size : 0))
1362         {
1363             ERR("Failed to copy name.\n");
1364             hr = E_OUTOFMEMORY;
1365             goto err_out;
1366         }
1367
1368         if ((cb->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1369             skip_dword_unknown(&ptr, 4);
1370     }
1371
1372     cb->variables = variables;
1373
1374     return S_OK;
1375
1376 err_out:
1377     for (i = 0; i < cb->variable_count; i++)
1378     {
1379         free_variable(&variables[i]);
1380     }
1381     HeapFree(GetProcessHeap(), 0, variables);
1382     return hr;
1383 }
1384
1385 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1386 {
1387     const char *ptr = data;
1388     DWORD size = data_size >> 2;
1389     DWORD offset, cbuffer_offset, resource_offset, creator_offset;
1390     unsigned int i, string_data_offset, string_data_size;
1391     char *string_data = NULL, *creator = NULL;
1392     D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
1393     struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers = NULL;
1394     HRESULT hr;
1395
1396     TRACE("Size %u\n", size);
1397
1398     read_dword(&ptr, &r->constant_buffer_count);
1399     TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
1400
1401     read_dword(&ptr, &cbuffer_offset);
1402     TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
1403
1404     read_dword(&ptr, &r->bound_resource_count);
1405     TRACE("Bound resource count: %u\n", r->bound_resource_count);
1406
1407     read_dword(&ptr, &resource_offset);
1408     TRACE("Bound resource offset: %#x\n", resource_offset);
1409
1410     read_dword(&ptr, &r->target);
1411     TRACE("Target: %#x\n", r->target);
1412
1413     read_dword(&ptr, &r->flags);
1414     TRACE("Flags: %u\n", r->flags);
1415
1416     read_dword(&ptr, &creator_offset);
1417     TRACE("Creator at offset %#x.\n", creator_offset);
1418
1419     if (!copy_name(data + creator_offset, &creator))
1420     {
1421         ERR("Failed to copy name.\n");
1422         return E_OUTOFMEMORY;
1423     }
1424     TRACE("Creator: %s.\n", debugstr_a(creator));
1425
1426     /* todo: Parse RD11 */
1427     if ((r->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1428     {
1429         skip_dword_unknown(&ptr, 8);
1430     }
1431
1432     if (r->bound_resource_count)
1433     {
1434         /* 8 for each bind desc */
1435         string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
1436         string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
1437
1438         string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1439         if (!string_data)
1440         {
1441             ERR("Failed to allocate string data memory.\n");
1442             hr = E_OUTOFMEMORY;
1443             goto err_out;
1444         }
1445         memcpy(string_data, data + string_data_offset, string_data_size);
1446
1447         bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
1448         if (!bound_resources)
1449         {
1450             ERR("Failed to allocate resources memory.\n");
1451             hr = E_OUTOFMEMORY;
1452             goto err_out;
1453         }
1454
1455         ptr = data + resource_offset;
1456         for (i = 0; i < r->bound_resource_count; i++)
1457         {
1458             D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
1459
1460             read_dword(&ptr, &offset);
1461             desc->Name = string_data + (offset - string_data_offset);
1462             TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
1463
1464             read_dword(&ptr, &desc->Type);
1465             TRACE("Input bind Type: %#x\n", desc->Type);
1466
1467             read_dword(&ptr, &desc->ReturnType);
1468             TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
1469
1470             read_dword(&ptr, &desc->Dimension);
1471             TRACE("Input bind Dimension: %#x\n", desc->Dimension);
1472
1473             read_dword(&ptr, &desc->NumSamples);
1474             TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
1475
1476             read_dword(&ptr, &desc->BindPoint);
1477             TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
1478
1479             read_dword(&ptr, &desc->BindCount);
1480             TRACE("Input bind BindCount: %u\n", desc->BindCount);
1481
1482             read_dword(&ptr, &desc->uFlags);
1483             TRACE("Input bind uFlags: %u\n", desc->uFlags);
1484         }
1485     }
1486
1487     if (r->constant_buffer_count)
1488     {
1489         constant_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, r->constant_buffer_count * sizeof(*constant_buffers));
1490         if (!constant_buffers)
1491         {
1492             ERR("Failed to allocate constant buffer memory.\n");
1493             hr = E_OUTOFMEMORY;
1494             goto err_out;
1495         }
1496
1497         ptr = data + cbuffer_offset;
1498         for (i = 0; i < r->constant_buffer_count; i++)
1499         {
1500             struct d3dcompiler_shader_reflection_constant_buffer *cb = &constant_buffers[i];
1501
1502             cb->ID3D11ShaderReflectionConstantBuffer_iface.lpVtbl = &d3dcompiler_shader_reflection_constant_buffer_vtbl;
1503             cb->reflection = r;
1504
1505             read_dword(&ptr, &offset);
1506             if (!copy_name(data + offset, &cb->name))
1507             {
1508                 ERR("Failed to copy name.\n");
1509                 hr = E_OUTOFMEMORY;
1510                 goto err_out;
1511             }
1512             TRACE("Name: %s.\n", debugstr_a(cb->name));
1513
1514             read_dword(&ptr, &cb->variable_count);
1515             TRACE("Variable count: %u\n", cb->variable_count);
1516
1517             read_dword(&ptr, &offset);
1518             TRACE("Variable offset: %x\n", offset);
1519
1520             hr = d3dcompiler_parse_variables(cb, data, data_size, data + offset);
1521             if (hr != S_OK)
1522             {
1523                 FIXME("Failed to parse variables.\n");
1524                 goto err_out;
1525             }
1526
1527             read_dword(&ptr, &cb->size);
1528             TRACE("Cbuffer size: %u\n", cb->size);
1529
1530             read_dword(&ptr, &cb->flags);
1531             TRACE("Cbuffer flags: %u\n", cb->flags);
1532
1533             read_dword(&ptr, &cb->type);
1534             TRACE("Cbuffer type: %#x\n", cb->type);
1535         }
1536     }
1537
1538     r->creator = creator;
1539     r->resource_string = string_data;
1540     r->bound_resources = bound_resources;
1541     r->constant_buffers = constant_buffers;
1542
1543     return S_OK;
1544
1545 err_out:
1546     for (i = 0; i < r->constant_buffer_count; ++i)
1547     {
1548         free_constant_buffer(&constant_buffers[i]);
1549     }
1550     HeapFree(GetProcessHeap(), 0, constant_buffers);
1551     HeapFree(GetProcessHeap(), 0, bound_resources);
1552     HeapFree(GetProcessHeap(), 0, string_data);
1553     HeapFree(GetProcessHeap(), 0, creator);
1554
1555     return hr;
1556 }
1557
1558 static HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section, DWORD target)
1559 {
1560     D3D11_SIGNATURE_PARAMETER_DESC *d;
1561     unsigned int string_data_offset;
1562     unsigned int string_data_size;
1563     const char *ptr = section->data;
1564     char *string_data;
1565     unsigned int i;
1566     DWORD count;
1567     enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
1568
1569     switch (section->tag)
1570     {
1571         case TAG_OSG5:
1572             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
1573             break;
1574
1575         case TAG_ISGN:
1576         case TAG_OSGN:
1577         case TAG_PCSG:
1578             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1579             break;
1580
1581         default:
1582             FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1583             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1584             break;
1585     }
1586
1587     read_dword(&ptr, &count);
1588     TRACE("%u elements\n", count);
1589
1590     skip_dword_unknown(&ptr, 1);
1591
1592     d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
1593     if (!d)
1594     {
1595         ERR("Failed to allocate signature memory.\n");
1596         return E_OUTOFMEMORY;
1597     }
1598
1599     /* 2 DWORDs for the header, element_size for each element. */
1600     string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
1601     string_data_size = section->data_size - string_data_offset;
1602
1603     string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1604     if (!string_data)
1605     {
1606         ERR("Failed to allocate string data memory.\n");
1607         HeapFree(GetProcessHeap(), 0, d);
1608         return E_OUTOFMEMORY;
1609     }
1610     memcpy(string_data, section->data + string_data_offset, string_data_size);
1611
1612     for (i = 0; i < count; ++i)
1613     {
1614         UINT name_offset;
1615         DWORD mask;
1616
1617         if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
1618         {
1619             read_dword(&ptr, &d[i].Stream);
1620         }
1621         else
1622         {
1623             d[i].Stream = 0;
1624         }
1625
1626         read_dword(&ptr, &name_offset);
1627         d[i].SemanticName = string_data + (name_offset - string_data_offset);
1628         read_dword(&ptr, &d[i].SemanticIndex);
1629         read_dword(&ptr, &d[i].SystemValueType);
1630         read_dword(&ptr, &d[i].ComponentType);
1631         read_dword(&ptr, &d[i].Register);
1632         read_dword(&ptr, &mask);
1633         d[i].ReadWriteMask = (mask >> 8) & 0xff;
1634         d[i].Mask = mask & 0xff;
1635
1636         /* pixel shaders have a special handling for SystemValueType in the output signature */
1637         if (((target & D3DCOMPILER_SHADER_TARGET_SHADERTYPE_MASK) == 0xffff0000) && (section->tag == TAG_OSG5 || section->tag == TAG_OSGN))
1638         {
1639             TRACE("Pixelshader output signature fixup.\n");
1640
1641             if (d[i].Register == 0xffffffff)
1642             {
1643                 if (!strcasecmp(d[i].SemanticName, "sv_depth")) d[i].SystemValueType = D3D_NAME_DEPTH;
1644                 if (!strcasecmp(d[i].SemanticName, "sv_coverage")) d[i].SystemValueType = D3D_NAME_COVERAGE;
1645                 if (!strcasecmp(d[i].SemanticName, "sv_depthgreaterequal")) d[i].SystemValueType = D3D_NAME_DEPTH_GREATER_EQUAL;
1646                 if (!strcasecmp(d[i].SemanticName, "sv_depthlessequal")) d[i].SystemValueType = D3D_NAME_DEPTH_LESS_EQUAL;
1647             }
1648             else
1649             {
1650                 d[i].SystemValueType = D3D_NAME_TARGET;
1651             }
1652         }
1653
1654         TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
1655                 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
1656                 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
1657                 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
1658     }
1659
1660     s->elements = d;
1661     s->element_count = count;
1662     s->string_data = string_data;
1663
1664     return S_OK;
1665 }
1666
1667 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1668 {
1669     const char *ptr = data;
1670
1671     read_dword(&ptr, &r->version);
1672     TRACE("Shader version: %u\n", r->version);
1673
1674     /* todo: Check if anything else is needed from the shdr or shex blob. */
1675
1676     return S_OK;
1677 }
1678
1679 static HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
1680         const void *data, SIZE_T data_size)
1681 {
1682     struct dxbc src_dxbc;
1683     HRESULT hr;
1684     unsigned int i;
1685
1686     reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
1687     reflection->refcount = 1;
1688
1689     if (wine_rb_init(&reflection->types, &d3dcompiler_shader_reflection_type_rb_functions) == -1)
1690     {
1691         ERR("Failed to initialize type rbtree.\n");
1692         return E_FAIL;
1693     }
1694
1695     hr = dxbc_parse(data, data_size, &src_dxbc);
1696     if (FAILED(hr))
1697     {
1698         WARN("Failed to parse reflection\n");
1699         return hr;
1700     }
1701
1702     for (i = 0; i < src_dxbc.count; ++i)
1703     {
1704         struct dxbc_section *section = &src_dxbc.sections[i];
1705
1706         switch (section->tag)
1707         {
1708             case TAG_RDEF:
1709                 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
1710                 if (FAILED(hr))
1711                 {
1712                     WARN("Failed to parse RDEF section.\n");
1713                     goto err_out;
1714                 }
1715                 break;
1716
1717             case TAG_ISGN:
1718                 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
1719                 if (!reflection->isgn)
1720                 {
1721                     ERR("Failed to allocate ISGN memory.\n");
1722                     hr = E_OUTOFMEMORY;
1723                     goto err_out;
1724                 }
1725
1726                 hr = d3dcompiler_parse_signature(reflection->isgn, section, reflection->target);
1727                 if (FAILED(hr))
1728                 {
1729                     WARN("Failed to parse section ISGN.\n");
1730                     goto err_out;
1731                 }
1732                 break;
1733
1734             case TAG_OSG5:
1735             case TAG_OSGN:
1736                 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
1737                 if (!reflection->osgn)
1738                 {
1739                     ERR("Failed to allocate OSGN memory.\n");
1740                     hr = E_OUTOFMEMORY;
1741                     goto err_out;
1742                 }
1743
1744                 hr = d3dcompiler_parse_signature(reflection->osgn, section, reflection->target);
1745                 if (FAILED(hr))
1746                 {
1747                     WARN("Failed to parse section OSGN.\n");
1748                     goto err_out;
1749                 }
1750                 break;
1751
1752             case TAG_PCSG:
1753                 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
1754                 if (!reflection->pcsg)
1755                 {
1756                     ERR("Failed to allocate PCSG memory.\n");
1757                     hr = E_OUTOFMEMORY;
1758                     goto err_out;
1759                 }
1760
1761                 hr = d3dcompiler_parse_signature(reflection->pcsg, section, reflection->target);
1762                 if (FAILED(hr))
1763                 {
1764                     WARN("Failed to parse section PCSG.\n");
1765                     goto err_out;
1766                 }
1767                 break;
1768
1769             case TAG_SHEX:
1770             case TAG_SHDR:
1771                 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
1772                 if (FAILED(hr))
1773                 {
1774                     WARN("Failed to parse SHDR section.\n");
1775                     goto err_out;
1776                 }
1777                 break;
1778
1779             case TAG_STAT:
1780                 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
1781                 if (FAILED(hr))
1782                 {
1783                     WARN("Failed to parse section STAT.\n");
1784                     goto err_out;
1785                 }
1786                 break;
1787
1788             default:
1789                 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1790                 break;
1791         }
1792     }
1793
1794     dxbc_destroy(&src_dxbc);
1795
1796     return hr;
1797
1798 err_out:
1799     reflection_cleanup(reflection);
1800     dxbc_destroy(&src_dxbc);
1801
1802     return hr;
1803 }
1804
1805 HRESULT WINAPI D3DReflect(const void *data, SIZE_T data_size, REFIID riid, void **reflector)
1806 {
1807     struct d3dcompiler_shader_reflection *object;
1808     HRESULT hr;
1809     const DWORD *temp = data;
1810
1811     TRACE("data %p, data_size %lu, riid %s, blob %p\n", data, data_size, debugstr_guid(riid), reflector);
1812
1813     if (!data || data_size < 32)
1814     {
1815         WARN("Invalid argument supplied.\n");
1816         return D3DERR_INVALIDCALL;
1817     }
1818
1819     if (temp[6] != data_size)
1820     {
1821         WARN("Wrong size supplied.\n");
1822         return E_FAIL;
1823     }
1824
1825     if (!IsEqualGUID(riid, &IID_ID3D11ShaderReflection))
1826     {
1827         WARN("Wrong riid %s, accept only %s!\n", debugstr_guid(riid), debugstr_guid(&IID_ID3D11ShaderReflection));
1828         return E_NOINTERFACE;
1829     }
1830
1831     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1832     if (!object)
1833         return E_OUTOFMEMORY;
1834
1835     hr = d3dcompiler_shader_reflection_init(object, data, data_size);
1836     if (FAILED(hr))
1837     {
1838         WARN("Failed to initialize shader reflection\n");
1839         HeapFree(GetProcessHeap(), 0, object);
1840         return hr;
1841     }
1842
1843     *reflector = object;
1844
1845     TRACE("Created ID3D11ShaderReflection %p\n", object);
1846
1847     return S_OK;
1848 }