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