ws2_32: name is never NULL as array (Coverity).
[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 const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl;
35 const struct ID3D11ShaderReflectionVariableVtbl d3dcompiler_shader_reflection_variable_vtbl;
36 const struct ID3D11ShaderReflectionTypeVtbl d3dcompiler_shader_reflection_type_vtbl;
37
38 /* null objects - needed for invalid calls */
39 static struct d3dcompiler_shader_reflection_constant_buffer null_constant_buffer = {{&d3dcompiler_shader_reflection_constant_buffer_vtbl}};
40 static struct d3dcompiler_shader_reflection_type null_type = {{&d3dcompiler_shader_reflection_type_vtbl}};
41 static struct d3dcompiler_shader_reflection_variable null_variable = {{&d3dcompiler_shader_reflection_variable_vtbl},
42     &null_constant_buffer, &null_type};
43
44 static BOOL copy_name(const char *ptr, char **name)
45 {
46     size_t name_len;
47
48     if (!ptr) return TRUE;
49
50     name_len = strlen(ptr) + 1;
51     if (name_len == 1)
52     {
53         return TRUE;
54     }
55
56     *name = HeapAlloc(GetProcessHeap(), 0, name_len);
57     if (!*name)
58     {
59         ERR("Failed to allocate name memory.\n");
60         return FALSE;
61     }
62
63     memcpy(*name, ptr, name_len);
64
65     return TRUE;
66 }
67
68 static BOOL copy_value(const char *ptr, void **value, DWORD size)
69 {
70     if (!ptr || !size) return TRUE;
71
72     *value = HeapAlloc(GetProcessHeap(), 0, size);
73     if (!*value)
74     {
75         ERR("Failed to allocate vlaue memory.\n");
76         return FALSE;
77     }
78
79     memcpy(*value, ptr, size);
80
81     return TRUE;
82 }
83
84 void *d3dcompiler_rb_alloc(size_t size)
85 {
86     return HeapAlloc(GetProcessHeap(), 0, size);
87 }
88
89 void *d3dcompiler_rb_realloc(void *ptr, size_t size)
90 {
91     return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
92 }
93
94 void d3dcompiler_rb_free(void *ptr)
95 {
96     HeapFree(GetProcessHeap(), 0, ptr);
97 }
98
99 static int d3dcompiler_shader_reflection_type_compare(const void *key, const struct wine_rb_entry *entry)
100 {
101     const struct d3dcompiler_shader_reflection_type *t = WINE_RB_ENTRY_VALUE(entry, const struct d3dcompiler_shader_reflection_type, entry);
102     const DWORD *id = key;
103
104     return *id - t->id;
105 }
106
107 static void d3dcompiler_shader_reflection_type_destroy(struct wine_rb_entry *entry, void *context)
108 {
109     struct d3dcompiler_shader_reflection_type *t = WINE_RB_ENTRY_VALUE(entry, struct d3dcompiler_shader_reflection_type, entry);
110
111     TRACE("reflection type %p.\n", t);
112
113     HeapFree(GetProcessHeap(), 0, t);
114 }
115
116 static const struct wine_rb_functions d3dcompiler_shader_reflection_type_rb_functions =
117 {
118     d3dcompiler_rb_alloc,
119     d3dcompiler_rb_realloc,
120     d3dcompiler_rb_free,
121     d3dcompiler_shader_reflection_type_compare,
122 };
123
124 static void free_signature(struct d3dcompiler_shader_signature *sig)
125 {
126     TRACE("Free signature %p\n", sig);
127
128     HeapFree(GetProcessHeap(), 0, sig->elements);
129     HeapFree(GetProcessHeap(), 0, sig->string_data);
130 }
131
132 static void free_variable(struct d3dcompiler_shader_reflection_variable *var)
133 {
134     if (var)
135     {
136         HeapFree(GetProcessHeap(), 0, var->name);
137         HeapFree(GetProcessHeap(), 0, var->default_value);
138     }
139 }
140
141 static void free_constant_buffer(struct d3dcompiler_shader_reflection_constant_buffer *cb)
142 {
143     if (cb->variables)
144     {
145         unsigned int i;
146
147         for (i = 0; i < cb->variable_count; ++i)
148         {
149             free_variable(&cb->variables[i]);
150         }
151         HeapFree(GetProcessHeap(), 0, cb->variables);
152     }
153
154     HeapFree(GetProcessHeap(), 0, cb->name);
155 }
156
157 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
158 {
159     TRACE("Cleanup %p\n", ref);
160
161     if (ref->isgn)
162     {
163         free_signature(ref->isgn);
164         HeapFree(GetProcessHeap(), 0, ref->isgn);
165     }
166
167     if (ref->osgn)
168     {
169         free_signature(ref->osgn);
170         HeapFree(GetProcessHeap(), 0, ref->osgn);
171     }
172
173     if (ref->pcsg)
174     {
175         free_signature(ref->pcsg);
176         HeapFree(GetProcessHeap(), 0, ref->pcsg);
177     }
178
179     if (ref->constant_buffers)
180     {
181         unsigned int i;
182
183         for (i = 0; i < ref->constant_buffer_count; ++i)
184         {
185             free_constant_buffer(&ref->constant_buffers[i]);
186         }
187     }
188
189     wine_rb_destroy(&ref->types, d3dcompiler_shader_reflection_type_destroy, NULL);
190     HeapFree(GetProcessHeap(), 0, ref->constant_buffers);
191     HeapFree(GetProcessHeap(), 0, ref->bound_resources);
192     HeapFree(GetProcessHeap(), 0, ref->resource_string);
193     HeapFree(GetProcessHeap(), 0, ref->creator);
194 }
195
196 /* IUnknown methods */
197
198 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
199 {
200     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
201 }
202
203 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
204 {
205     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
206
207     if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
208             || IsEqualGUID(riid, &IID_IUnknown))
209     {
210         IUnknown_AddRef(iface);
211         *object = iface;
212         return S_OK;
213     }
214
215     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
216
217     *object = NULL;
218     return E_NOINTERFACE;
219 }
220
221 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
222 {
223     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
224     ULONG refcount = InterlockedIncrement(&This->refcount);
225
226     TRACE("%p increasing refcount to %u\n", This, refcount);
227
228     return refcount;
229 }
230
231 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
232 {
233     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
234     ULONG refcount = InterlockedDecrement(&This->refcount);
235
236     TRACE("%p decreasing refcount to %u\n", This, refcount);
237
238     if (!refcount)
239     {
240         reflection_cleanup(This);
241         HeapFree(GetProcessHeap(), 0, This);
242     }
243
244     return refcount;
245 }
246
247 /* ID3D11ShaderReflection methods */
248
249 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
250 {
251     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
252
253     FIXME("iface %p, desc %p partial stub!\n", iface, desc);
254
255     if (!desc)
256     {
257         WARN("Invalid argument specified\n");
258         return E_FAIL;
259     }
260
261     desc->Version = This->version;
262     desc->Creator = This->creator;
263     desc->Flags = This->flags;
264     desc->ConstantBuffers = This->constant_buffer_count;
265     desc->BoundResources = This->bound_resource_count;
266     desc->InputParameters = This->isgn ? This->isgn->element_count : 0;
267     desc->OutputParameters = This->osgn ? This->osgn->element_count : 0;
268     desc->InstructionCount = This->instruction_count;
269     desc->TempRegisterCount = This->temp_register_count;
270     desc->TempArrayCount = This->temp_array_count;
271     desc->DefCount = 0;
272     desc->DclCount = This->dcl_count;
273     desc->TextureNormalInstructions = This->texture_normal_instructions;
274     desc->TextureLoadInstructions = This->texture_load_instructions;
275     desc->TextureCompInstructions = This->texture_comp_instructions;
276     desc->TextureBiasInstructions = This->texture_bias_instructions;
277     desc->TextureGradientInstructions = This->texture_gradient_instructions;
278     desc->FloatInstructionCount = This->float_instruction_count;
279     desc->IntInstructionCount = This->int_instruction_count;
280     desc->UintInstructionCount = This->uint_instruction_count;
281     desc->StaticFlowControlCount = This->static_flow_control_count;
282     desc->DynamicFlowControlCount = This->dynamic_flow_control_count;
283     desc->MacroInstructionCount = 0;
284     desc->ArrayInstructionCount = This->array_instruction_count;
285     desc->CutInstructionCount = This->cut_instruction_count;
286     desc->EmitInstructionCount = This->emit_instruction_count;
287     desc->GSOutputTopology = This->gs_output_topology;
288     desc->GSMaxOutputVertexCount = This->gs_max_output_vertex_count;
289     desc->InputPrimitive = This->input_primitive;
290     desc->PatchConstantParameters = This->pcsg ? This->pcsg->element_count : 0;
291     desc->cGSInstanceCount = 0;
292     desc->cControlPoints = This->c_control_points;
293     desc->HSOutputPrimitive = This->hs_output_primitive;
294     desc->HSPartitioning = This->hs_prtitioning;
295     desc->TessellatorDomain = This->tessellator_domain;
296     desc->cBarrierInstructions = 0;
297     desc->cInterlockedInstructions = 0;
298     desc->cTextureStoreInstructions = 0;
299
300     return S_OK;
301 }
302
303 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
304         ID3D11ShaderReflection *iface, UINT index)
305 {
306     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
307
308     TRACE("iface %p, index %u\n", iface, index);
309
310     if (index >= This->constant_buffer_count)
311     {
312         WARN("Invalid argument specified\n");
313         return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
314     }
315
316     return &This->constant_buffers[index].ID3D11ShaderReflectionConstantBuffer_iface;
317 }
318
319 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
320         ID3D11ShaderReflection *iface, LPCSTR name)
321 {
322     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
323     unsigned int i;
324
325     TRACE("iface %p, name %s\n", iface, debugstr_a(name));
326
327     if (!name)
328     {
329         WARN("Invalid argument specified\n");
330         return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
331     }
332
333     for (i = 0; i < This->constant_buffer_count; ++i)
334     {
335         struct d3dcompiler_shader_reflection_constant_buffer *d = &This->constant_buffers[i];
336
337         if (!strcmp(d->name, name))
338         {
339             TRACE("Returning ID3D11ShaderReflectionConstantBuffer %p.\n", d);
340             return &d->ID3D11ShaderReflectionConstantBuffer_iface;
341         }
342     }
343
344     WARN("Invalid name specified\n");
345
346     return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
347 }
348
349 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
350         ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_DESC *desc)
351 {
352     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
353
354     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
355
356     if (!desc || index >= This->bound_resource_count)
357     {
358         WARN("Invalid argument specified\n");
359         return E_INVALIDARG;
360     }
361
362     *desc = This->bound_resources[index];
363
364     return S_OK;
365 }
366
367 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
368         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
369 {
370     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
371
372     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
373
374     if (!desc || !This->isgn || index >= This->isgn->element_count)
375     {
376         WARN("Invalid argument specified\n");
377         return E_INVALIDARG;
378     }
379
380     *desc = This->isgn->elements[index];
381
382     return S_OK;
383 }
384
385 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
386         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
387 {
388     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
389
390     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
391
392     if (!desc || !This->osgn || index >= This->osgn->element_count)
393     {
394         WARN("Invalid argument specified\n");
395         return E_INVALIDARG;
396     }
397
398     *desc = This->osgn->elements[index];
399
400     return S_OK;
401 }
402
403 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
404         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
405 {
406     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
407
408     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
409
410     if (!desc || !This->pcsg || index >= This->pcsg->element_count)
411     {
412         WARN("Invalid argument specified\n");
413         return E_INVALIDARG;
414     }
415
416     *desc = This->pcsg->elements[index];
417
418     return S_OK;
419 }
420
421 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
422         ID3D11ShaderReflection *iface, LPCSTR name)
423 {
424     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
425     unsigned int i, k;
426
427     TRACE("iface %p, name %s\n", iface, debugstr_a(name));
428
429     if (!name)
430     {
431         WARN("Invalid name specified\n");
432         return &null_variable.ID3D11ShaderReflectionVariable_iface;
433     }
434
435     for (i = 0; i < This->constant_buffer_count; ++i)
436     {
437         struct d3dcompiler_shader_reflection_constant_buffer *cb = &This->constant_buffers[i];
438
439         for (k = 0; k < cb->variable_count; ++k)
440         {
441             struct d3dcompiler_shader_reflection_variable *v = &cb->variables[k];
442
443             if (!strcmp(v->name, name))
444             {
445                 TRACE("Returning ID3D11ShaderReflectionVariable %p.\n", v);
446                 return &v->ID3D11ShaderReflectionVariable_iface;
447             }
448         }
449     }
450
451     WARN("Invalid name specified\n");
452
453     return &null_variable.ID3D11ShaderReflectionVariable_iface;
454 }
455
456 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
457         ID3D11ShaderReflection *iface, LPCSTR name, D3D11_SHADER_INPUT_BIND_DESC *desc)
458 {
459     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
460     unsigned int i;
461
462     TRACE("iface %p, name %s, desc %p\n", iface, debugstr_a(name), desc);
463
464     if (!desc || !name)
465     {
466         WARN("Invalid argument specified\n");
467         return E_INVALIDARG;
468     }
469
470     for (i = 0; i < This->bound_resource_count; ++i)
471     {
472         D3D11_SHADER_INPUT_BIND_DESC *d = &This->bound_resources[i];
473
474         if (!strcmp(d->Name, name))
475         {
476             TRACE("Returning D3D11_SHADER_INPUT_BIND_DESC %p.\n", d);
477             *desc = *d;
478             return S_OK;
479         }
480     }
481
482     WARN("Invalid name specified\n");
483
484     return E_INVALIDARG;
485 }
486
487 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
488         ID3D11ShaderReflection *iface)
489 {
490     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
491
492     TRACE("iface %p\n", iface);
493
494     return This->mov_instruction_count;
495 }
496
497 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
498         ID3D11ShaderReflection *iface)
499 {
500     FIXME("iface %p stub!\n", iface);
501
502     return 0;
503 }
504
505 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
506         ID3D11ShaderReflection *iface)
507 {
508     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
509
510     TRACE("iface %p\n", iface);
511
512     return This->conversion_instruction_count;
513 }
514
515 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
516         ID3D11ShaderReflection *iface)
517 {
518     FIXME("iface %p stub!\n", iface);
519
520     return 0;
521 }
522
523 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
524         ID3D11ShaderReflection *iface)
525 {
526     FIXME("iface %p stub!\n", iface);
527
528     return 0;
529 }
530
531 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
532         ID3D11ShaderReflection *iface)
533 {
534     FIXME("iface %p stub!\n", iface);
535
536     return 0;
537 }
538
539 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
540         ID3D11ShaderReflection *iface)
541 {
542     FIXME("iface %p stub!\n", iface);
543
544     return 0;
545 }
546
547 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
548         ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
549 {
550     FIXME("iface %p, level %p stub!\n", iface, level);
551
552     return E_NOTIMPL;
553 }
554
555 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
556         ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
557 {
558     FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
559
560     return 0;
561 }
562
563 const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
564 {
565     /* IUnknown methods */
566     d3dcompiler_shader_reflection_QueryInterface,
567     d3dcompiler_shader_reflection_AddRef,
568     d3dcompiler_shader_reflection_Release,
569     /* ID3D11ShaderReflection methods */
570     d3dcompiler_shader_reflection_GetDesc,
571     d3dcompiler_shader_reflection_GetConstantBufferByIndex,
572     d3dcompiler_shader_reflection_GetConstantBufferByName,
573     d3dcompiler_shader_reflection_GetResourceBindingDesc,
574     d3dcompiler_shader_reflection_GetInputParameterDesc,
575     d3dcompiler_shader_reflection_GetOutputParameterDesc,
576     d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
577     d3dcompiler_shader_reflection_GetVariableByName,
578     d3dcompiler_shader_reflection_GetResourceBindingDescByName,
579     d3dcompiler_shader_reflection_GetMovInstructionCount,
580     d3dcompiler_shader_reflection_GetMovcInstructionCount,
581     d3dcompiler_shader_reflection_GetConversionInstructionCount,
582     d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
583     d3dcompiler_shader_reflection_GetGSInputPrimitive,
584     d3dcompiler_shader_reflection_IsSampleFrequencyShader,
585     d3dcompiler_shader_reflection_GetNumInterfaceSlots,
586     d3dcompiler_shader_reflection_GetMinFeatureLevel,
587     d3dcompiler_shader_reflection_GetThreadGroupSize,
588 };
589
590 /* ID3D11ShaderReflectionConstantBuffer methods */
591
592 static inline struct d3dcompiler_shader_reflection_constant_buffer *impl_from_ID3D11ShaderReflectionConstantBuffer(ID3D11ShaderReflectionConstantBuffer *iface)
593 {
594     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_constant_buffer, ID3D11ShaderReflectionConstantBuffer_iface);
595 }
596
597 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetDesc(
598         ID3D11ShaderReflectionConstantBuffer *iface, D3D11_SHADER_BUFFER_DESC *desc)
599 {
600     struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
601
602     TRACE("iface %p, desc %p\n", iface, desc);
603
604     if (This == &null_constant_buffer)
605     {
606         WARN("Null constant buffer specified\n");
607         return E_FAIL;
608     }
609
610     if (!desc)
611     {
612         WARN("Invalid argument specified\n");
613         return E_FAIL;
614     }
615
616     desc->Name = This->name;
617     desc->Type = This->type;
618     desc->Variables = This->variable_count;
619     desc->Size = This->size;
620     desc->uFlags = This->flags;
621
622     return S_OK;
623 }
624
625 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex(
626         ID3D11ShaderReflectionConstantBuffer *iface, UINT index)
627 {
628     struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
629
630     TRACE("iface %p, index %u\n", iface, index);
631
632     if (index >= This->variable_count)
633     {
634         WARN("Invalid index specified\n");
635         return &null_variable.ID3D11ShaderReflectionVariable_iface;
636     }
637
638     return &This->variables[index].ID3D11ShaderReflectionVariable_iface;
639 }
640
641 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByName(
642         ID3D11ShaderReflectionConstantBuffer *iface, LPCSTR name)
643 {
644     struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
645     unsigned int i;
646
647     TRACE("iface %p, name %s\n", iface, debugstr_a(name));
648
649     if (!name)
650     {
651         WARN("Invalid argument specified\n");
652         return &null_variable.ID3D11ShaderReflectionVariable_iface;
653     }
654
655     for (i = 0; i < This->variable_count; ++i)
656     {
657         struct d3dcompiler_shader_reflection_variable *v = &This->variables[i];
658
659         if (!strcmp(v->name, name))
660         {
661             TRACE("Returning ID3D11ShaderReflectionVariable %p.\n", v);
662             return &v->ID3D11ShaderReflectionVariable_iface;
663         }
664     }
665
666     WARN("Invalid name specified\n");
667
668     return &null_variable.ID3D11ShaderReflectionVariable_iface;
669 }
670
671 const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl =
672 {
673     /* ID3D11ShaderReflectionConstantBuffer methods */
674     d3dcompiler_shader_reflection_constant_buffer_GetDesc,
675     d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex,
676     d3dcompiler_shader_reflection_constant_buffer_GetVariableByName,
677 };
678
679 /* ID3D11ShaderReflectionVariable methods */
680
681 static inline struct d3dcompiler_shader_reflection_variable *impl_from_ID3D11ShaderReflectionVariable(ID3D11ShaderReflectionVariable *iface)
682 {
683     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_variable, ID3D11ShaderReflectionVariable_iface);
684 }
685
686 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetDesc(
687         ID3D11ShaderReflectionVariable *iface, D3D11_SHADER_VARIABLE_DESC *desc)
688 {
689     struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
690
691     TRACE("iface %p, desc %p\n", iface, desc);
692
693     if (This == &null_variable)
694     {
695         WARN("Null variable specified\n");
696         return E_FAIL;
697     }
698
699     if (!desc)
700     {
701         WARN("Invalid argument specified\n");
702         return E_FAIL;
703     }
704
705     desc->Name = This->name;
706     desc->StartOffset = This->start_offset;
707     desc->Size = This->size;
708     desc->uFlags = This->flags;
709     desc->DefaultValue = This->default_value;
710
711     return S_OK;
712 }
713
714 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetType(
715         ID3D11ShaderReflectionVariable *iface)
716 {
717     struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
718
719     TRACE("iface %p\n", iface);
720
721     return &This->type->ID3D11ShaderReflectionType_iface;
722 }
723
724 static ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetBuffer(
725         ID3D11ShaderReflectionVariable *iface)
726 {
727     struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
728
729     TRACE("iface %p\n", iface);
730
731     return &This->constant_buffer->ID3D11ShaderReflectionConstantBuffer_iface;
732 }
733
734 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetInterfaceSlot(
735         ID3D11ShaderReflectionVariable *iface, UINT index)
736 {
737     FIXME("iface %p, index %u stub!\n", iface, index);
738
739     return 0;
740 }
741
742 const struct ID3D11ShaderReflectionVariableVtbl d3dcompiler_shader_reflection_variable_vtbl =
743 {
744     /* ID3D11ShaderReflectionVariable methods */
745     d3dcompiler_shader_reflection_variable_GetDesc,
746     d3dcompiler_shader_reflection_variable_GetType,
747     d3dcompiler_shader_reflection_variable_GetBuffer,
748     d3dcompiler_shader_reflection_variable_GetInterfaceSlot,
749 };
750
751 /* ID3D11ShaderReflectionType methods */
752
753 static inline struct d3dcompiler_shader_reflection_type *impl_from_ID3D11ShaderReflectionType(ID3D11ShaderReflectionType *iface)
754 {
755     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_type, ID3D11ShaderReflectionType_iface);
756 }
757
758 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetDesc(
759         ID3D11ShaderReflectionType *iface, D3D11_SHADER_TYPE_DESC *desc)
760 {
761     struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
762
763     TRACE("iface %p, desc %p\n", iface, desc);
764
765     if (This == &null_type)
766     {
767         WARN("Null type specified\n");
768         return E_FAIL;
769     }
770
771     if (!desc)
772     {
773         WARN("Invalid argument specified\n");
774         return E_FAIL;
775     }
776
777     *desc = This->desc;
778
779     return S_OK;
780 }
781
782 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeByIndex(
783         ID3D11ShaderReflectionType *iface, UINT index)
784 {
785     FIXME("iface %p, index %u stub!\n", iface, index);
786
787     return NULL;
788 }
789
790 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeByName(
791         ID3D11ShaderReflectionType *iface, LPCSTR name)
792 {
793     FIXME("iface %p, name %s stub!\n", iface, name);
794
795     return NULL;
796 }
797
798 static LPCSTR STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeName(
799         ID3D11ShaderReflectionType *iface, UINT index)
800 {
801     FIXME("iface %p, index %u stub!\n", iface, index);
802
803     return NULL;
804 }
805
806 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_IsEqual(
807         ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *type)
808 {
809     FIXME("iface %p, type %p stub!\n", iface, type);
810
811     return E_NOTIMPL;
812 }
813
814 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetSubType(
815         ID3D11ShaderReflectionType *iface)
816 {
817     FIXME("iface %p stub!\n", iface);
818
819     return NULL;
820 }
821
822 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetBaseClass(
823         ID3D11ShaderReflectionType *iface)
824 {
825     FIXME("iface %p stub!\n", iface);
826
827     return NULL;
828 }
829
830 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetNumInterfaces(
831         ID3D11ShaderReflectionType *iface)
832 {
833     FIXME("iface %p stub!\n", iface);
834
835     return 0;
836 }
837
838 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetInterfaceByIndex(
839         ID3D11ShaderReflectionType *iface, UINT index)
840 {
841     FIXME("iface %p, index %u stub!\n", iface, index);
842
843     return NULL;
844 }
845
846 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_IsOfType(
847         ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *type)
848 {
849     FIXME("iface %p, type %p stub!\n", iface, type);
850
851     return E_NOTIMPL;
852 }
853
854 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_ImplementsInterface(
855         ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *base)
856 {
857     FIXME("iface %p, base %p stub!\n", iface, base);
858
859     return E_NOTIMPL;
860 }
861
862 const struct ID3D11ShaderReflectionTypeVtbl d3dcompiler_shader_reflection_type_vtbl =
863 {
864     /* ID3D11ShaderReflectionType methods */
865     d3dcompiler_shader_reflection_type_GetDesc,
866     d3dcompiler_shader_reflection_type_GetMemberTypeByIndex,
867     d3dcompiler_shader_reflection_type_GetMemberTypeByName,
868     d3dcompiler_shader_reflection_type_GetMemberTypeName,
869     d3dcompiler_shader_reflection_type_IsEqual,
870     d3dcompiler_shader_reflection_type_GetSubType,
871     d3dcompiler_shader_reflection_type_GetBaseClass,
872     d3dcompiler_shader_reflection_type_GetNumInterfaces,
873     d3dcompiler_shader_reflection_type_GetInterfaceByIndex,
874     d3dcompiler_shader_reflection_type_IsOfType,
875     d3dcompiler_shader_reflection_type_ImplementsInterface,
876 };
877
878 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
879 {
880     const char *ptr = data;
881     DWORD size = data_size >> 2;
882
883     TRACE("Size %u\n", size);
884
885     read_dword(&ptr, &r->instruction_count);
886     TRACE("InstructionCount: %u\n", r->instruction_count);
887
888     read_dword(&ptr, &r->temp_register_count);
889     TRACE("TempRegisterCount: %u\n", r->temp_register_count);
890
891     skip_dword_unknown(&ptr, 1);
892
893     read_dword(&ptr, &r->dcl_count);
894     TRACE("DclCount: %u\n", r->dcl_count);
895
896     read_dword(&ptr, &r->float_instruction_count);
897     TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
898
899     read_dword(&ptr, &r->int_instruction_count);
900     TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
901
902     read_dword(&ptr, &r->uint_instruction_count);
903     TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
904
905     read_dword(&ptr, &r->static_flow_control_count);
906     TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
907
908     read_dword(&ptr, &r->dynamic_flow_control_count);
909     TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
910
911     skip_dword_unknown(&ptr, 1);
912
913     read_dword(&ptr, &r->temp_array_count);
914     TRACE("TempArrayCount: %u\n", r->temp_array_count);
915
916     read_dword(&ptr, &r->array_instruction_count);
917     TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
918
919     read_dword(&ptr, &r->cut_instruction_count);
920     TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
921
922     read_dword(&ptr, &r->emit_instruction_count);
923     TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
924
925     read_dword(&ptr, &r->texture_normal_instructions);
926     TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
927
928     read_dword(&ptr, &r->texture_load_instructions);
929     TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
930
931     read_dword(&ptr, &r->texture_comp_instructions);
932     TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
933
934     read_dword(&ptr, &r->texture_bias_instructions);
935     TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
936
937     read_dword(&ptr, &r->texture_gradient_instructions);
938     TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
939
940     read_dword(&ptr, &r->mov_instruction_count);
941     TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
942
943     skip_dword_unknown(&ptr, 1);
944
945     read_dword(&ptr, &r->conversion_instruction_count);
946     TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
947
948     skip_dword_unknown(&ptr, 1);
949
950     read_dword(&ptr, &r->input_primitive);
951     TRACE("InputPrimitive: %x\n", r->input_primitive);
952
953     read_dword(&ptr, &r->gs_output_topology);
954     TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
955
956     read_dword(&ptr, &r->gs_max_output_vertex_count);
957     TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
958
959     skip_dword_unknown(&ptr, 3);
960
961     /* dx10 stat size */
962     if (size == 29) return S_OK;
963
964     skip_dword_unknown(&ptr, 1);
965
966     read_dword(&ptr, &r->c_control_points);
967     TRACE("cControlPoints: %u\n", r->c_control_points);
968
969     read_dword(&ptr, &r->hs_output_primitive);
970     TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
971
972     read_dword(&ptr, &r->hs_prtitioning);
973     TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
974
975     read_dword(&ptr, &r->tessellator_domain);
976     TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
977
978     skip_dword_unknown(&ptr, 3);
979
980     /* dx11 stat size */
981     if (size == 37) return S_OK;
982
983     FIXME("Unhandled size %u\n", size);
984
985     return E_FAIL;
986 }
987
988 static HRESULT d3dcompiler_parse_type(struct d3dcompiler_shader_reflection_type *type, const char *data, DWORD offset)
989 {
990     const char *ptr = data + offset;
991     DWORD temp;
992     D3D11_SHADER_TYPE_DESC *desc;
993     unsigned int i;
994
995     desc = &type->desc;
996
997     read_dword(&ptr, &temp);
998     desc->Class = temp & 0xffff;
999     desc->Type = temp >> 16;
1000     TRACE("Class %x, Type %x\n", desc->Class, desc->Type);
1001
1002     read_dword(&ptr, &temp);
1003     desc->Rows = temp & 0xffff;
1004     desc->Columns = temp >> 16;
1005     TRACE("Rows %u, Columns %u\n", desc->Rows, desc->Columns);
1006
1007     read_dword(&ptr, &temp);
1008     desc->Elements = temp & 0xffff;
1009     desc->Members = temp >> 16;
1010     TRACE("Elements %u, Members %u\n", desc->Elements, desc->Members);
1011
1012     read_dword(&ptr, &temp);
1013     TRACE("Member Offset %u\n", temp);
1014
1015     if ((type->reflection->target & 0xffff) >= 0x500)
1016         skip_dword_unknown(&ptr, 4);
1017
1018     /* todo: Parse type members */
1019     for (i = 0; i < desc->Members; ++i)
1020     {
1021         skip_dword_unknown(&ptr, 3);
1022     }
1023
1024     return S_OK;
1025 }
1026
1027 static struct d3dcompiler_shader_reflection_type *get_reflection_type(struct d3dcompiler_shader_reflection *reflection, const char *data, DWORD offset)
1028 {
1029     struct d3dcompiler_shader_reflection_type *type;
1030     struct wine_rb_entry *entry;
1031     HRESULT hr;
1032
1033     entry = wine_rb_get(&reflection->types, &offset);
1034     if (entry)
1035     {
1036         TRACE("Returning existing type.\n");
1037         return WINE_RB_ENTRY_VALUE(entry, struct d3dcompiler_shader_reflection_type, entry);
1038     }
1039
1040     type = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*type));
1041     if (!type)
1042     {
1043         ERR("Failed to allocate type memory.\n");
1044         return NULL;
1045     }
1046
1047     type->ID3D11ShaderReflectionType_iface.lpVtbl = &d3dcompiler_shader_reflection_type_vtbl;
1048     type->id = offset;
1049     type->reflection = reflection;
1050
1051     hr = d3dcompiler_parse_type(type, data, offset);
1052     if (FAILED(hr))
1053     {
1054         ERR("Failed to parse type info, hr %#x.\n", hr);
1055         HeapFree(GetProcessHeap(), 0, type);
1056         return NULL;
1057     }
1058
1059     if (wine_rb_put(&reflection->types, &offset, &type->entry) == -1)
1060     {
1061         ERR("Failed to insert type entry.\n");
1062         HeapFree(GetProcessHeap(), 0, type);
1063         return NULL;
1064     }
1065
1066     return type;
1067 }
1068
1069 static HRESULT d3dcompiler_parse_variables(struct d3dcompiler_shader_reflection_constant_buffer *cb,
1070         const char *data, DWORD data_size, const char *ptr)
1071 {
1072     struct d3dcompiler_shader_reflection_variable *variables;
1073     unsigned int i;
1074     HRESULT hr;
1075
1076     variables = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb->variable_count * sizeof(*variables));
1077     if (!variables)
1078     {
1079         ERR("Failed to allocate variables memory.\n");
1080         return E_OUTOFMEMORY;
1081     }
1082
1083     for (i = 0; i < cb->variable_count; i++)
1084     {
1085         struct d3dcompiler_shader_reflection_variable *v = &variables[i];
1086         DWORD offset;
1087
1088         v->ID3D11ShaderReflectionVariable_iface.lpVtbl = &d3dcompiler_shader_reflection_variable_vtbl;
1089         v->constant_buffer = cb;
1090
1091         read_dword(&ptr, &offset);
1092         if (!copy_name(data + offset, &v->name))
1093         {
1094             ERR("Failed to copy name.\n");
1095             hr = E_OUTOFMEMORY;
1096             goto err_out;
1097         }
1098         TRACE("Variable name: %s.\n", debugstr_a(v->name));
1099
1100         read_dword(&ptr, &v->start_offset);
1101         TRACE("Variable offset: %u\n", v->start_offset);
1102
1103         read_dword(&ptr, &v->size);
1104         TRACE("Variable size: %u\n", v->size);
1105
1106         read_dword(&ptr, &v->flags);
1107         TRACE("Variable flags: %u\n", v->flags);
1108
1109         read_dword(&ptr, &offset);
1110         TRACE("Variable type offset: %x\n", offset);
1111         v->type = get_reflection_type(cb->reflection, data, offset);
1112         if (!v->type)
1113         {
1114             ERR("Failed to get type.\n");
1115             hr = E_FAIL;
1116             goto err_out;
1117         }
1118
1119         read_dword(&ptr, &offset);
1120         TRACE("Variable default value offset: %x\n", offset);
1121         if (!copy_value(data + offset, &v->default_value, offset ? v->size : 0))
1122         {
1123             ERR("Failed to copy name.\n");
1124             hr = E_OUTOFMEMORY;
1125             goto err_out;
1126         }
1127
1128         if ((cb->reflection->target & 0xffff) >= 0x500)
1129             skip_dword_unknown(&ptr, 4);
1130     }
1131
1132     cb->variables = variables;
1133
1134     return S_OK;
1135
1136 err_out:
1137     for (i = 0; i < cb->variable_count; i++)
1138     {
1139         free_variable(&variables[i]);
1140     }
1141     HeapFree(GetProcessHeap(), 0, variables);
1142     return hr;
1143 }
1144
1145 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1146 {
1147     const char *ptr = data;
1148     DWORD size = data_size >> 2;
1149     DWORD offset, cbuffer_offset, resource_offset, creator_offset;
1150     unsigned int i, string_data_offset, string_data_size;
1151     char *string_data = NULL, *creator = NULL;
1152     D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
1153     struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers = NULL;
1154     HRESULT hr;
1155
1156     TRACE("Size %u\n", size);
1157
1158     read_dword(&ptr, &r->constant_buffer_count);
1159     TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
1160
1161     read_dword(&ptr, &cbuffer_offset);
1162     TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
1163
1164     read_dword(&ptr, &r->bound_resource_count);
1165     TRACE("Bound resource count: %u\n", r->bound_resource_count);
1166
1167     read_dword(&ptr, &resource_offset);
1168     TRACE("Bound resource offset: %#x\n", resource_offset);
1169
1170     read_dword(&ptr, &r->target);
1171     TRACE("Target: %#x\n", r->target);
1172
1173     read_dword(&ptr, &r->flags);
1174     TRACE("Flags: %u\n", r->flags);
1175
1176     read_dword(&ptr, &creator_offset);
1177     TRACE("Creator at offset %#x.\n", creator_offset);
1178
1179     if (!copy_name(data + creator_offset, &creator))
1180     {
1181         ERR("Failed to copy name.\n");
1182         return E_OUTOFMEMORY;
1183     }
1184     TRACE("Creator: %s.\n", debugstr_a(creator));
1185
1186     /* todo: Parse RD11 */
1187     if ((r->target & 0x0000ffff) >= 0x500)
1188     {
1189         skip_dword_unknown(&ptr, 8);
1190     }
1191
1192     if (r->bound_resource_count)
1193     {
1194         /* 8 for each bind desc */
1195         string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
1196         string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
1197
1198         string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1199         if (!string_data)
1200         {
1201             ERR("Failed to allocate string data memory.\n");
1202             hr = E_OUTOFMEMORY;
1203             goto err_out;
1204         }
1205         memcpy(string_data, data + string_data_offset, string_data_size);
1206
1207         bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
1208         if (!bound_resources)
1209         {
1210             ERR("Failed to allocate resources memory.\n");
1211             hr = E_OUTOFMEMORY;
1212             goto err_out;
1213         }
1214
1215         ptr = data + resource_offset;
1216         for (i = 0; i < r->bound_resource_count; i++)
1217         {
1218             D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
1219
1220             read_dword(&ptr, &offset);
1221             desc->Name = string_data + (offset - string_data_offset);
1222             TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
1223
1224             read_dword(&ptr, &desc->Type);
1225             TRACE("Input bind Type: %#x\n", desc->Type);
1226
1227             read_dword(&ptr, &desc->ReturnType);
1228             TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
1229
1230             read_dword(&ptr, &desc->Dimension);
1231             TRACE("Input bind Dimension: %#x\n", desc->Dimension);
1232
1233             read_dword(&ptr, &desc->NumSamples);
1234             TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
1235
1236             read_dword(&ptr, &desc->BindPoint);
1237             TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
1238
1239             read_dword(&ptr, &desc->BindCount);
1240             TRACE("Input bind BindCount: %u\n", desc->BindCount);
1241
1242             read_dword(&ptr, &desc->uFlags);
1243             TRACE("Input bind uFlags: %u\n", desc->uFlags);
1244         }
1245     }
1246
1247     if (r->constant_buffer_count)
1248     {
1249         constant_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, r->constant_buffer_count * sizeof(*constant_buffers));
1250         if (!constant_buffers)
1251         {
1252             ERR("Failed to allocate constant buffer memory.\n");
1253             hr = E_OUTOFMEMORY;
1254             goto err_out;
1255         }
1256
1257         ptr = data + cbuffer_offset;
1258         for (i = 0; i < r->constant_buffer_count; i++)
1259         {
1260             struct d3dcompiler_shader_reflection_constant_buffer *cb = &constant_buffers[i];
1261
1262             cb->ID3D11ShaderReflectionConstantBuffer_iface.lpVtbl = &d3dcompiler_shader_reflection_constant_buffer_vtbl;
1263             cb->reflection = r;
1264
1265             read_dword(&ptr, &offset);
1266             if (!copy_name(data + offset, &cb->name))
1267             {
1268                 ERR("Failed to copy name.\n");
1269                 hr = E_OUTOFMEMORY;
1270                 goto err_out;
1271             }
1272             TRACE("Name: %s.\n", debugstr_a(cb->name));
1273
1274             read_dword(&ptr, &cb->variable_count);
1275             TRACE("Variable count: %u\n", cb->variable_count);
1276
1277             read_dword(&ptr, &offset);
1278             TRACE("Variable offset: %x\n", offset);
1279
1280             hr = d3dcompiler_parse_variables(cb, data, data_size, data + offset);
1281             if (hr != S_OK)
1282             {
1283                 FIXME("Failed to parse variables.");
1284                 goto err_out;
1285             }
1286
1287             read_dword(&ptr, &cb->size);
1288             TRACE("Cbuffer size: %u\n", cb->size);
1289
1290             read_dword(&ptr, &cb->flags);
1291             TRACE("Cbuffer flags: %u\n", cb->flags);
1292
1293             read_dword(&ptr, &cb->type);
1294             TRACE("Cbuffer type: %#x\n", cb->type);
1295         }
1296     }
1297
1298     r->creator = creator;
1299     r->resource_string = string_data;
1300     r->bound_resources = bound_resources;
1301     r->constant_buffers = constant_buffers;
1302
1303     return S_OK;
1304
1305 err_out:
1306     for (i = 0; i < r->constant_buffer_count; ++i)
1307     {
1308         free_constant_buffer(&constant_buffers[i]);
1309     }
1310     HeapFree(GetProcessHeap(), 0, constant_buffers);
1311     HeapFree(GetProcessHeap(), 0, bound_resources);
1312     HeapFree(GetProcessHeap(), 0, string_data);
1313     HeapFree(GetProcessHeap(), 0, creator);
1314
1315     return hr;
1316 }
1317
1318 HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section)
1319 {
1320     D3D11_SIGNATURE_PARAMETER_DESC *d;
1321     unsigned int string_data_offset;
1322     unsigned int string_data_size;
1323     const char *ptr = section->data;
1324     char *string_data;
1325     unsigned int i;
1326     DWORD count;
1327     enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
1328
1329     switch (section->tag)
1330     {
1331         case TAG_OSG5:
1332             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
1333             break;
1334
1335         case TAG_ISGN:
1336         case TAG_OSGN:
1337         case TAG_PCSG:
1338             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1339             break;
1340
1341         default:
1342             FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1343             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1344             break;
1345     }
1346
1347     read_dword(&ptr, &count);
1348     TRACE("%u elements\n", count);
1349
1350     skip_dword_unknown(&ptr, 1);
1351
1352     d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
1353     if (!d)
1354     {
1355         ERR("Failed to allocate signature memory.\n");
1356         return E_OUTOFMEMORY;
1357     }
1358
1359     /* 2 DWORDs for the header, element_size for each element. */
1360     string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
1361     string_data_size = section->data_size - string_data_offset;
1362
1363     string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1364     if (!string_data)
1365     {
1366         ERR("Failed to allocate string data memory.\n");
1367         HeapFree(GetProcessHeap(), 0, d);
1368         return E_OUTOFMEMORY;
1369     }
1370     memcpy(string_data, section->data + string_data_offset, string_data_size);
1371
1372     for (i = 0; i < count; ++i)
1373     {
1374         UINT name_offset;
1375         DWORD mask;
1376
1377         if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
1378         {
1379             read_dword(&ptr, &d[i].Stream);
1380         }
1381         else
1382         {
1383             d[i].Stream = 0;
1384         }
1385
1386         read_dword(&ptr, &name_offset);
1387         d[i].SemanticName = string_data + (name_offset - string_data_offset);
1388         read_dword(&ptr, &d[i].SemanticIndex);
1389         read_dword(&ptr, &d[i].SystemValueType);
1390         read_dword(&ptr, &d[i].ComponentType);
1391         read_dword(&ptr, &d[i].Register);
1392         read_dword(&ptr, &mask);
1393         d[i].ReadWriteMask = (mask >> 8) & 0xff;
1394         d[i].Mask = mask & 0xff;
1395
1396         TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
1397                 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
1398                 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
1399                 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
1400     }
1401
1402     s->elements = d;
1403     s->element_count = count;
1404     s->string_data = string_data;
1405
1406     return S_OK;
1407 }
1408
1409 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1410 {
1411     const char *ptr = data;
1412
1413     read_dword(&ptr, &r->version);
1414     TRACE("Shader version: %u\n", r->version);
1415
1416     /* todo: Check if anything else is needed from the shdr or shex blob. */
1417
1418     return S_OK;
1419 }
1420
1421 HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
1422         const void *data, SIZE_T data_size)
1423 {
1424     struct dxbc src_dxbc;
1425     HRESULT hr;
1426     unsigned int i;
1427
1428     reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
1429     reflection->refcount = 1;
1430
1431     if (wine_rb_init(&reflection->types, &d3dcompiler_shader_reflection_type_rb_functions) == -1)
1432     {
1433         ERR("Failed to initialize type rbtree.\n");
1434         return E_FAIL;
1435     }
1436
1437     hr = dxbc_parse(data, data_size, &src_dxbc);
1438     if (FAILED(hr))
1439     {
1440         WARN("Failed to parse reflection\n");
1441         return hr;
1442     }
1443
1444     for (i = 0; i < src_dxbc.count; ++i)
1445     {
1446         struct dxbc_section *section = &src_dxbc.sections[i];
1447
1448         switch (section->tag)
1449         {
1450             case TAG_STAT:
1451                 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
1452                 if (FAILED(hr))
1453                 {
1454                     WARN("Failed to parse section STAT.\n");
1455                     goto err_out;
1456                 }
1457                 break;
1458
1459             case TAG_SHEX:
1460             case TAG_SHDR:
1461                 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
1462                 if (FAILED(hr))
1463                 {
1464                     WARN("Failed to parse SHDR section.\n");
1465                     goto err_out;
1466                 }
1467                 break;
1468
1469             case TAG_RDEF:
1470                 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
1471                 if (FAILED(hr))
1472                 {
1473                     WARN("Failed to parse RDEF section.\n");
1474                     goto err_out;
1475                 }
1476                 break;
1477
1478             case TAG_ISGN:
1479                 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
1480                 if (!reflection->isgn)
1481                 {
1482                     ERR("Failed to allocate ISGN memory.\n");
1483                     hr = E_OUTOFMEMORY;
1484                     goto err_out;
1485                 }
1486
1487                 hr = d3dcompiler_parse_signature(reflection->isgn, section);
1488                 if (FAILED(hr))
1489                 {
1490                     WARN("Failed to parse section ISGN.\n");
1491                     goto err_out;
1492                 }
1493                 break;
1494
1495             case TAG_OSG5:
1496             case TAG_OSGN:
1497                 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
1498                 if (!reflection->osgn)
1499                 {
1500                     ERR("Failed to allocate OSGN memory.\n");
1501                     hr = E_OUTOFMEMORY;
1502                     goto err_out;
1503                 }
1504
1505                 hr = d3dcompiler_parse_signature(reflection->osgn, section);
1506                 if (FAILED(hr))
1507                 {
1508                     WARN("Failed to parse section OSGN.\n");
1509                     goto err_out;
1510                 }
1511                 break;
1512
1513             case TAG_PCSG:
1514                 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
1515                 if (!reflection->pcsg)
1516                 {
1517                     ERR("Failed to allocate PCSG memory.\n");
1518                     hr = E_OUTOFMEMORY;
1519                     goto err_out;
1520                 }
1521
1522                 hr = d3dcompiler_parse_signature(reflection->pcsg, section);
1523                 if (FAILED(hr))
1524                 {
1525                     WARN("Failed to parse section PCSG.\n");
1526                     goto err_out;
1527                 }
1528                 break;
1529
1530             default:
1531                 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1532                 break;
1533         }
1534     }
1535
1536     dxbc_destroy(&src_dxbc);
1537
1538     return hr;
1539
1540 err_out:
1541     reflection_cleanup(reflection);
1542     dxbc_destroy(&src_dxbc);
1543
1544     return hr;
1545 }