mshtml: Implement IHTMLAnchorElement_put_href.
[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 static BOOL copy_name(const char *ptr, char **name)
35 {
36     size_t name_len;
37
38     if (!ptr) return TRUE;
39
40     name_len = strlen(ptr) + 1;
41     if (name_len == 1)
42     {
43         return TRUE;
44     }
45
46     *name = HeapAlloc(GetProcessHeap(), 0, name_len);
47     if (!*name)
48     {
49         ERR("Failed to allocate name memory.\n");
50         return FALSE;
51     }
52
53     memcpy(*name, ptr, name_len);
54
55     return TRUE;
56 }
57
58 static void free_signature(struct d3dcompiler_shader_signature *sig)
59 {
60     TRACE("Free signature %p\n", sig);
61
62     HeapFree(GetProcessHeap(), 0, sig->elements);
63     HeapFree(GetProcessHeap(), 0, sig->string_data);
64 }
65
66 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
67 {
68     TRACE("Cleanup %p\n", ref);
69
70     if (ref->isgn)
71     {
72         free_signature(ref->isgn);
73         HeapFree(GetProcessHeap(), 0, ref->isgn);
74     }
75
76     if (ref->osgn)
77     {
78         free_signature(ref->osgn);
79         HeapFree(GetProcessHeap(), 0, ref->osgn);
80     }
81
82     if (ref->pcsg)
83     {
84         free_signature(ref->pcsg);
85         HeapFree(GetProcessHeap(), 0, ref->pcsg);
86     }
87
88     HeapFree(GetProcessHeap(), 0, ref->bound_resources);
89     HeapFree(GetProcessHeap(), 0, ref->resource_string);
90     HeapFree(GetProcessHeap(), 0, ref->creator);
91 }
92
93 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
94 {
95     return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
96 }
97
98 /* IUnknown methods */
99
100 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
101 {
102     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
103
104     if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
105             || IsEqualGUID(riid, &IID_IUnknown))
106     {
107         IUnknown_AddRef(iface);
108         *object = iface;
109         return S_OK;
110     }
111
112     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
113
114     *object = NULL;
115     return E_NOINTERFACE;
116 }
117
118 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
119 {
120     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
121     ULONG refcount = InterlockedIncrement(&This->refcount);
122
123     TRACE("%p increasing refcount to %u\n", This, refcount);
124
125     return refcount;
126 }
127
128 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
129 {
130     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
131     ULONG refcount = InterlockedDecrement(&This->refcount);
132
133     TRACE("%p decreasing refcount to %u\n", This, refcount);
134
135     if (!refcount)
136     {
137         reflection_cleanup(This);
138         HeapFree(GetProcessHeap(), 0, This);
139     }
140
141     return refcount;
142 }
143
144 /* ID3D11ShaderReflection methods */
145
146 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
147 {
148     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
149
150     FIXME("iface %p, desc %p partial stub!\n", iface, desc);
151
152     if (!desc)
153     {
154         WARN("Invalid argument specified\n");
155         return E_FAIL;
156     }
157
158     desc->Version = This->version;
159     desc->Creator = This->creator;
160     desc->Flags = This->flags;
161     desc->ConstantBuffers = This->constant_buffer_count;
162     desc->BoundResources = This->bound_resource_count;
163     desc->InputParameters = This->isgn ? This->isgn->element_count : 0;
164     desc->OutputParameters = This->osgn ? This->osgn->element_count : 0;
165     desc->InstructionCount = This->instruction_count;
166     desc->TempRegisterCount = This->temp_register_count;
167     desc->TempArrayCount = This->temp_array_count;
168     desc->DefCount = 0;
169     desc->DclCount = This->dcl_count;
170     desc->TextureNormalInstructions = This->texture_normal_instructions;
171     desc->TextureLoadInstructions = This->texture_load_instructions;
172     desc->TextureCompInstructions = This->texture_comp_instructions;
173     desc->TextureBiasInstructions = This->texture_bias_instructions;
174     desc->TextureGradientInstructions = This->texture_gradient_instructions;
175     desc->FloatInstructionCount = This->float_instruction_count;
176     desc->IntInstructionCount = This->int_instruction_count;
177     desc->UintInstructionCount = This->uint_instruction_count;
178     desc->StaticFlowControlCount = This->static_flow_control_count;
179     desc->DynamicFlowControlCount = This->dynamic_flow_control_count;
180     desc->MacroInstructionCount = 0;
181     desc->ArrayInstructionCount = This->array_instruction_count;
182     desc->CutInstructionCount = This->cut_instruction_count;
183     desc->EmitInstructionCount = This->emit_instruction_count;
184     desc->GSOutputTopology = This->gs_output_topology;
185     desc->GSMaxOutputVertexCount = This->gs_max_output_vertex_count;
186     desc->InputPrimitive = This->input_primitive;
187     desc->PatchConstantParameters = This->pcsg ? This->pcsg->element_count : 0;
188     desc->cGSInstanceCount = 0;
189     desc->cControlPoints = This->c_control_points;
190     desc->HSOutputPrimitive = This->hs_output_primitive;
191     desc->HSPartitioning = This->hs_prtitioning;
192     desc->TessellatorDomain = This->tessellator_domain;
193     desc->cBarrierInstructions = 0;
194     desc->cInterlockedInstructions = 0;
195     desc->cTextureStoreInstructions = 0;
196
197     return S_OK;
198 }
199
200 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
201         ID3D11ShaderReflection *iface, UINT index)
202 {
203     FIXME("iface %p, index %u stub!\n", iface, index);
204
205     return NULL;
206 }
207
208 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
209         ID3D11ShaderReflection *iface, LPCSTR name)
210 {
211     FIXME("iface %p, name \"%s\" stub!\n", iface, name);
212
213     return NULL;
214 }
215
216 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
217         ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_DESC *desc)
218 {
219     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
220
221     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
222
223     if (!desc || index >= This->bound_resource_count)
224     {
225         WARN("Invalid argument specified\n");
226         return E_INVALIDARG;
227     }
228
229     *desc = This->bound_resources[index];
230
231     return S_OK;
232 }
233
234 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
235         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
236 {
237     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
238
239     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
240
241     if (!desc || !This->isgn || index >= This->isgn->element_count)
242     {
243         WARN("Invalid argument specified\n");
244         return E_INVALIDARG;
245     }
246
247     *desc = This->isgn->elements[index];
248
249     return S_OK;
250 }
251
252 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
253         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
254 {
255     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
256
257     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
258
259     if (!desc || !This->osgn || index >= This->osgn->element_count)
260     {
261         WARN("Invalid argument specified\n");
262         return E_INVALIDARG;
263     }
264
265     *desc = This->osgn->elements[index];
266
267     return S_OK;
268 }
269
270 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
271         ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
272 {
273     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
274
275     TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
276
277     if (!desc || !This->pcsg || index >= This->pcsg->element_count)
278     {
279         WARN("Invalid argument specified\n");
280         return E_INVALIDARG;
281     }
282
283     *desc = This->pcsg->elements[index];
284
285     return S_OK;
286 }
287
288 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
289         ID3D11ShaderReflection *iface, LPCSTR name)
290 {
291     FIXME("iface %p, name %s stub!\n", iface, name);
292
293     return NULL;
294 }
295
296 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
297         ID3D11ShaderReflection *iface, LPCSTR name, D3D11_SHADER_INPUT_BIND_DESC *desc)
298 {
299     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
300     unsigned int i;
301
302     TRACE("iface %p, name %s, desc %p\n", iface, debugstr_a(name), desc);
303
304     if (!desc || !name)
305     {
306         WARN("Invalid argument specified\n");
307         return E_INVALIDARG;
308     }
309
310     for (i = 0; i < This->bound_resource_count; ++i)
311     {
312         D3D11_SHADER_INPUT_BIND_DESC *d = &This->bound_resources[i];
313
314         if (!strcmp(d->Name, name))
315         {
316             TRACE("Returning D3D11_SHADER_INPUT_BIND_DESC %p.\n", d);
317             *desc = *d;
318             return S_OK;
319         }
320     }
321
322     WARN("Invalid name specified\n");
323
324     return E_INVALIDARG;
325 }
326
327 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
328         ID3D11ShaderReflection *iface)
329 {
330     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
331
332     TRACE("iface %p\n", iface);
333
334     return This->mov_instruction_count;
335 }
336
337 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
338         ID3D11ShaderReflection *iface)
339 {
340     FIXME("iface %p stub!\n", iface);
341
342     return 0;
343 }
344
345 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
346         ID3D11ShaderReflection *iface)
347 {
348     struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
349
350     TRACE("iface %p\n", iface);
351
352     return This->conversion_instruction_count;
353 }
354
355 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
356         ID3D11ShaderReflection *iface)
357 {
358     FIXME("iface %p stub!\n", iface);
359
360     return 0;
361 }
362
363 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
364         ID3D11ShaderReflection *iface)
365 {
366     FIXME("iface %p stub!\n", iface);
367
368     return 0;
369 }
370
371 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
372         ID3D11ShaderReflection *iface)
373 {
374     FIXME("iface %p stub!\n", iface);
375
376     return 0;
377 }
378
379 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
380         ID3D11ShaderReflection *iface)
381 {
382     FIXME("iface %p stub!\n", iface);
383
384     return 0;
385 }
386
387 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
388         ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
389 {
390     FIXME("iface %p, level %p stub!\n", iface, level);
391
392     return E_NOTIMPL;
393 }
394
395 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
396         ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
397 {
398     FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
399
400     return 0;
401 }
402
403 const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
404 {
405     /* IUnknown methods */
406     d3dcompiler_shader_reflection_QueryInterface,
407     d3dcompiler_shader_reflection_AddRef,
408     d3dcompiler_shader_reflection_Release,
409     /* ID3D11ShaderReflection methods */
410     d3dcompiler_shader_reflection_GetDesc,
411     d3dcompiler_shader_reflection_GetConstantBufferByIndex,
412     d3dcompiler_shader_reflection_GetConstantBufferByName,
413     d3dcompiler_shader_reflection_GetResourceBindingDesc,
414     d3dcompiler_shader_reflection_GetInputParameterDesc,
415     d3dcompiler_shader_reflection_GetOutputParameterDesc,
416     d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
417     d3dcompiler_shader_reflection_GetVariableByName,
418     d3dcompiler_shader_reflection_GetResourceBindingDescByName,
419     d3dcompiler_shader_reflection_GetMovInstructionCount,
420     d3dcompiler_shader_reflection_GetMovcInstructionCount,
421     d3dcompiler_shader_reflection_GetConversionInstructionCount,
422     d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
423     d3dcompiler_shader_reflection_GetGSInputPrimitive,
424     d3dcompiler_shader_reflection_IsSampleFrequencyShader,
425     d3dcompiler_shader_reflection_GetNumInterfaceSlots,
426     d3dcompiler_shader_reflection_GetMinFeatureLevel,
427     d3dcompiler_shader_reflection_GetThreadGroupSize,
428 };
429
430 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
431 {
432     const char *ptr = data;
433     DWORD size = data_size >> 2;
434
435     TRACE("Size %u\n", size);
436
437     read_dword(&ptr, &r->instruction_count);
438     TRACE("InstructionCount: %u\n", r->instruction_count);
439
440     read_dword(&ptr, &r->temp_register_count);
441     TRACE("TempRegisterCount: %u\n", r->temp_register_count);
442
443     skip_dword_unknown(&ptr, 1);
444
445     read_dword(&ptr, &r->dcl_count);
446     TRACE("DclCount: %u\n", r->dcl_count);
447
448     read_dword(&ptr, &r->float_instruction_count);
449     TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
450
451     read_dword(&ptr, &r->int_instruction_count);
452     TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
453
454     read_dword(&ptr, &r->uint_instruction_count);
455     TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
456
457     read_dword(&ptr, &r->static_flow_control_count);
458     TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
459
460     read_dword(&ptr, &r->dynamic_flow_control_count);
461     TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
462
463     skip_dword_unknown(&ptr, 1);
464
465     read_dword(&ptr, &r->temp_array_count);
466     TRACE("TempArrayCount: %u\n", r->temp_array_count);
467
468     read_dword(&ptr, &r->array_instruction_count);
469     TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
470
471     read_dword(&ptr, &r->cut_instruction_count);
472     TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
473
474     read_dword(&ptr, &r->emit_instruction_count);
475     TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
476
477     read_dword(&ptr, &r->texture_normal_instructions);
478     TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
479
480     read_dword(&ptr, &r->texture_load_instructions);
481     TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
482
483     read_dword(&ptr, &r->texture_comp_instructions);
484     TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
485
486     read_dword(&ptr, &r->texture_bias_instructions);
487     TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
488
489     read_dword(&ptr, &r->texture_gradient_instructions);
490     TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
491
492     read_dword(&ptr, &r->mov_instruction_count);
493     TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
494
495     skip_dword_unknown(&ptr, 1);
496
497     read_dword(&ptr, &r->conversion_instruction_count);
498     TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
499
500     skip_dword_unknown(&ptr, 1);
501
502     read_dword(&ptr, &r->input_primitive);
503     TRACE("InputPrimitive: %x\n", r->input_primitive);
504
505     read_dword(&ptr, &r->gs_output_topology);
506     TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
507
508     read_dword(&ptr, &r->gs_max_output_vertex_count);
509     TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
510
511     skip_dword_unknown(&ptr, 3);
512
513     /* dx10 stat size */
514     if (size == 29) return S_OK;
515
516     skip_dword_unknown(&ptr, 1);
517
518     read_dword(&ptr, &r->c_control_points);
519     TRACE("cControlPoints: %u\n", r->c_control_points);
520
521     read_dword(&ptr, &r->hs_output_primitive);
522     TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
523
524     read_dword(&ptr, &r->hs_prtitioning);
525     TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
526
527     read_dword(&ptr, &r->tessellator_domain);
528     TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
529
530     skip_dword_unknown(&ptr, 3);
531
532     /* dx11 stat size */
533     if (size == 37) return S_OK;
534
535     FIXME("Unhandled size %u\n", size);
536
537     return E_FAIL;
538 }
539
540 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
541 {
542     const char *ptr = data;
543     DWORD size = data_size >> 2;
544     DWORD offset, cbuffer_offset, resource_offset, creator_offset;
545     unsigned int i;
546     unsigned int string_data_offset;
547     unsigned int string_data_size;
548     char *string_data = NULL, *creator = NULL;
549     D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
550     HRESULT hr;
551
552     TRACE("Size %u\n", size);
553
554     read_dword(&ptr, &r->constant_buffer_count);
555     TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
556
557     read_dword(&ptr, &cbuffer_offset);
558     TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
559
560     read_dword(&ptr, &r->bound_resource_count);
561     TRACE("Bound resource count: %u\n", r->bound_resource_count);
562
563     read_dword(&ptr, &resource_offset);
564     TRACE("Bound resource offset: %#x\n", resource_offset);
565
566     read_dword(&ptr, &r->target);
567     TRACE("Target: %#x\n", r->target);
568
569     read_dword(&ptr, &r->flags);
570     TRACE("Flags: %u\n", r->flags);
571
572     read_dword(&ptr, &creator_offset);
573     TRACE("Creator at offset %#x.\n", creator_offset);
574
575     if (!copy_name(data + creator_offset, &creator))
576     {
577         ERR("Failed to copy name.\n");
578         return E_OUTOFMEMORY;
579     }
580     TRACE("Creator: %s.\n", debugstr_a(creator));
581
582     /* todo: Parse RD11 */
583     if ((r->target & 0x0000ffff) >= 0x500)
584     {
585         skip_dword_unknown(&ptr, 8);
586     }
587
588     if (r->bound_resource_count)
589     {
590         /* 8 for each bind desc */
591         string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
592         string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
593
594         string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
595         if (!string_data)
596         {
597             ERR("Failed to allocate string data memory.\n");
598             hr = E_OUTOFMEMORY;
599             goto err_out;
600         }
601         memcpy(string_data, data + string_data_offset, string_data_size);
602
603         bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
604         if (!bound_resources)
605         {
606             ERR("Failed to allocate resources memory.\n");
607             hr = E_OUTOFMEMORY;
608             goto err_out;
609         }
610
611         ptr = data + resource_offset;
612         for (i = 0; i < r->bound_resource_count; i++)
613         {
614             D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
615
616             read_dword(&ptr, &offset);
617             desc->Name = string_data + (offset - string_data_offset);
618             TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
619
620             read_dword(&ptr, &desc->Type);
621             TRACE("Input bind Type: %#x\n", desc->Type);
622
623             read_dword(&ptr, &desc->ReturnType);
624             TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
625
626             read_dword(&ptr, &desc->Dimension);
627             TRACE("Input bind Dimension: %#x\n", desc->Dimension);
628
629             read_dword(&ptr, &desc->NumSamples);
630             TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
631
632             read_dword(&ptr, &desc->BindPoint);
633             TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
634
635             read_dword(&ptr, &desc->BindCount);
636             TRACE("Input bind BindCount: %u\n", desc->BindCount);
637
638             read_dword(&ptr, &desc->uFlags);
639             TRACE("Input bind uFlags: %u\n", desc->uFlags);
640         }
641     }
642
643     /* todo: Parse Constant buffers */
644
645     r->creator = creator;
646     r->resource_string = string_data;
647     r->bound_resources = bound_resources;
648
649     return S_OK;
650
651 err_out:
652     HeapFree(GetProcessHeap(), 0, bound_resources);
653     HeapFree(GetProcessHeap(), 0, string_data);
654     HeapFree(GetProcessHeap(), 0, creator);
655
656     return hr;
657 }
658
659 HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section)
660 {
661     D3D11_SIGNATURE_PARAMETER_DESC *d;
662     unsigned int string_data_offset;
663     unsigned int string_data_size;
664     const char *ptr = section->data;
665     char *string_data;
666     unsigned int i;
667     DWORD count;
668     enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
669
670     switch (section->tag)
671     {
672         case TAG_OSG5:
673             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
674             break;
675
676         case TAG_ISGN:
677         case TAG_OSGN:
678         case TAG_PCSG:
679             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
680             break;
681
682         default:
683             FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
684             element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
685             break;
686     }
687
688     read_dword(&ptr, &count);
689     TRACE("%u elements\n", count);
690
691     skip_dword_unknown(&ptr, 1);
692
693     d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
694     if (!d)
695     {
696         ERR("Failed to allocate signature memory.\n");
697         return E_OUTOFMEMORY;
698     }
699
700     /* 2 DWORDs for the header, element_size for each element. */
701     string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
702     string_data_size = section->data_size - string_data_offset;
703
704     string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
705     if (!string_data)
706     {
707         ERR("Failed to allocate string data memory.\n");
708         HeapFree(GetProcessHeap(), 0, d);
709         return E_OUTOFMEMORY;
710     }
711     memcpy(string_data, section->data + string_data_offset, string_data_size);
712
713     for (i = 0; i < count; ++i)
714     {
715         UINT name_offset;
716         DWORD mask;
717
718         if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
719         {
720             read_dword(&ptr, &d[i].Stream);
721         }
722         else
723         {
724             d[i].Stream = 0;
725         }
726
727         read_dword(&ptr, &name_offset);
728         d[i].SemanticName = string_data + (name_offset - string_data_offset);
729         read_dword(&ptr, &d[i].SemanticIndex);
730         read_dword(&ptr, &d[i].SystemValueType);
731         read_dword(&ptr, &d[i].ComponentType);
732         read_dword(&ptr, &d[i].Register);
733         read_dword(&ptr, &mask);
734         d[i].ReadWriteMask = (mask >> 8) & 0xff;
735         d[i].Mask = mask & 0xff;
736
737         TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
738                 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
739                 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
740                 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
741     }
742
743     s->elements = d;
744     s->element_count = count;
745     s->string_data = string_data;
746
747     return S_OK;
748 }
749
750 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
751 {
752     const char *ptr = data;
753
754     read_dword(&ptr, &r->version);
755     TRACE("Shader version: %u\n", r->version);
756
757     /* todo: Check if anything else is needed from the shdr or shex blob. */
758
759     return S_OK;
760 }
761
762 HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
763         const void *data, SIZE_T data_size)
764 {
765     struct dxbc src_dxbc;
766     HRESULT hr;
767     unsigned int i;
768
769     reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
770     reflection->refcount = 1;
771
772     hr = dxbc_parse(data, data_size, &src_dxbc);
773     if (FAILED(hr))
774     {
775         WARN("Failed to parse reflection\n");
776         return hr;
777     }
778
779     for (i = 0; i < src_dxbc.count; ++i)
780     {
781         struct dxbc_section *section = &src_dxbc.sections[i];
782
783         switch (section->tag)
784         {
785             case TAG_STAT:
786                 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
787                 if (FAILED(hr))
788                 {
789                     WARN("Failed to parse section STAT.\n");
790                     goto err_out;
791                 }
792                 break;
793
794             case TAG_SHEX:
795             case TAG_SHDR:
796                 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
797                 if (FAILED(hr))
798                 {
799                     WARN("Failed to parse SHDR section.\n");
800                     goto err_out;
801                 }
802                 break;
803
804             case TAG_RDEF:
805                 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
806                 if (FAILED(hr))
807                 {
808                     WARN("Failed to parse RDEF section.\n");
809                     goto err_out;
810                 }
811                 break;
812
813             case TAG_ISGN:
814                 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
815                 if (!reflection->isgn)
816                 {
817                     ERR("Failed to allocate ISGN memory.\n");
818                     hr = E_OUTOFMEMORY;
819                     goto err_out;
820                 }
821
822                 hr = d3dcompiler_parse_signature(reflection->isgn, section);
823                 if (FAILED(hr))
824                 {
825                     WARN("Failed to parse section ISGN.\n");
826                     goto err_out;
827                 }
828                 break;
829
830             case TAG_OSG5:
831             case TAG_OSGN:
832                 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
833                 if (!reflection->osgn)
834                 {
835                     ERR("Failed to allocate OSGN memory.\n");
836                     hr = E_OUTOFMEMORY;
837                     goto err_out;
838                 }
839
840                 hr = d3dcompiler_parse_signature(reflection->osgn, section);
841                 if (FAILED(hr))
842                 {
843                     WARN("Failed to parse section OSGN.\n");
844                     goto err_out;
845                 }
846                 break;
847
848             case TAG_PCSG:
849                 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
850                 if (!reflection->pcsg)
851                 {
852                     ERR("Failed to allocate PCSG memory.\n");
853                     hr = E_OUTOFMEMORY;
854                     goto err_out;
855                 }
856
857                 hr = d3dcompiler_parse_signature(reflection->pcsg, section);
858                 if (FAILED(hr))
859                 {
860                     WARN("Failed to parse section PCSG.\n");
861                     goto err_out;
862                 }
863                 break;
864
865             default:
866                 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
867                 break;
868         }
869     }
870
871     dxbc_destroy(&src_dxbc);
872
873     return hr;
874
875 err_out:
876     reflection_cleanup(reflection);
877     dxbc_destroy(&src_dxbc);
878
879     return hr;
880 }