2 * Copyright 2009 Henri Verbeet for CodeWeavers
3 * Copyright 2010 Rico Schüller
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.
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.
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
22 #include "wine/port.h"
24 #include "d3dcompiler_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
28 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE
30 D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6 = 6,
31 D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7 = 7,
34 const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl;
36 /* null objects - needed for invalid calls */
37 static struct d3dcompiler_shader_reflection_constant_buffer null_constant_buffer = {{&d3dcompiler_shader_reflection_constant_buffer_vtbl}};
39 static BOOL copy_name(const char *ptr, char **name)
43 if (!ptr) return TRUE;
45 name_len = strlen(ptr) + 1;
51 *name = HeapAlloc(GetProcessHeap(), 0, name_len);
54 ERR("Failed to allocate name memory.\n");
58 memcpy(*name, ptr, name_len);
63 static void free_signature(struct d3dcompiler_shader_signature *sig)
65 TRACE("Free signature %p\n", sig);
67 HeapFree(GetProcessHeap(), 0, sig->elements);
68 HeapFree(GetProcessHeap(), 0, sig->string_data);
71 static void free_constant_buffer(struct d3dcompiler_shader_reflection_constant_buffer *cb)
73 HeapFree(GetProcessHeap(), 0, cb->name);
76 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
78 TRACE("Cleanup %p\n", ref);
82 free_signature(ref->isgn);
83 HeapFree(GetProcessHeap(), 0, ref->isgn);
88 free_signature(ref->osgn);
89 HeapFree(GetProcessHeap(), 0, ref->osgn);
94 free_signature(ref->pcsg);
95 HeapFree(GetProcessHeap(), 0, ref->pcsg);
98 if (ref->constant_buffers)
102 for (i = 0; i < ref->constant_buffer_count; ++i)
104 free_constant_buffer(&ref->constant_buffers[i]);
108 HeapFree(GetProcessHeap(), 0, ref->constant_buffers);
109 HeapFree(GetProcessHeap(), 0, ref->bound_resources);
110 HeapFree(GetProcessHeap(), 0, ref->resource_string);
111 HeapFree(GetProcessHeap(), 0, ref->creator);
114 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
116 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
119 /* IUnknown methods */
121 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
123 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
125 if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
126 || IsEqualGUID(riid, &IID_IUnknown))
128 IUnknown_AddRef(iface);
133 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
136 return E_NOINTERFACE;
139 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
141 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
142 ULONG refcount = InterlockedIncrement(&This->refcount);
144 TRACE("%p increasing refcount to %u\n", This, refcount);
149 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
151 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
152 ULONG refcount = InterlockedDecrement(&This->refcount);
154 TRACE("%p decreasing refcount to %u\n", This, refcount);
158 reflection_cleanup(This);
159 HeapFree(GetProcessHeap(), 0, This);
165 /* ID3D11ShaderReflection methods */
167 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
169 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
171 FIXME("iface %p, desc %p partial stub!\n", iface, desc);
175 WARN("Invalid argument specified\n");
179 desc->Version = This->version;
180 desc->Creator = This->creator;
181 desc->Flags = This->flags;
182 desc->ConstantBuffers = This->constant_buffer_count;
183 desc->BoundResources = This->bound_resource_count;
184 desc->InputParameters = This->isgn ? This->isgn->element_count : 0;
185 desc->OutputParameters = This->osgn ? This->osgn->element_count : 0;
186 desc->InstructionCount = This->instruction_count;
187 desc->TempRegisterCount = This->temp_register_count;
188 desc->TempArrayCount = This->temp_array_count;
190 desc->DclCount = This->dcl_count;
191 desc->TextureNormalInstructions = This->texture_normal_instructions;
192 desc->TextureLoadInstructions = This->texture_load_instructions;
193 desc->TextureCompInstructions = This->texture_comp_instructions;
194 desc->TextureBiasInstructions = This->texture_bias_instructions;
195 desc->TextureGradientInstructions = This->texture_gradient_instructions;
196 desc->FloatInstructionCount = This->float_instruction_count;
197 desc->IntInstructionCount = This->int_instruction_count;
198 desc->UintInstructionCount = This->uint_instruction_count;
199 desc->StaticFlowControlCount = This->static_flow_control_count;
200 desc->DynamicFlowControlCount = This->dynamic_flow_control_count;
201 desc->MacroInstructionCount = 0;
202 desc->ArrayInstructionCount = This->array_instruction_count;
203 desc->CutInstructionCount = This->cut_instruction_count;
204 desc->EmitInstructionCount = This->emit_instruction_count;
205 desc->GSOutputTopology = This->gs_output_topology;
206 desc->GSMaxOutputVertexCount = This->gs_max_output_vertex_count;
207 desc->InputPrimitive = This->input_primitive;
208 desc->PatchConstantParameters = This->pcsg ? This->pcsg->element_count : 0;
209 desc->cGSInstanceCount = 0;
210 desc->cControlPoints = This->c_control_points;
211 desc->HSOutputPrimitive = This->hs_output_primitive;
212 desc->HSPartitioning = This->hs_prtitioning;
213 desc->TessellatorDomain = This->tessellator_domain;
214 desc->cBarrierInstructions = 0;
215 desc->cInterlockedInstructions = 0;
216 desc->cTextureStoreInstructions = 0;
221 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
222 ID3D11ShaderReflection *iface, UINT index)
224 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
226 TRACE("iface %p, index %u\n", iface, index);
228 if (index >= This->constant_buffer_count)
230 WARN("Invalid argument specified\n");
231 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
234 return &This->constant_buffers[index].ID3D11ShaderReflectionConstantBuffer_iface;
237 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
238 ID3D11ShaderReflection *iface, LPCSTR name)
240 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
243 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
247 WARN("Invalid argument specified\n");
248 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
251 for (i = 0; i < This->constant_buffer_count; ++i)
253 struct d3dcompiler_shader_reflection_constant_buffer *d = &This->constant_buffers[i];
255 if (!strcmp(d->name, name))
257 TRACE("Returning ID3D11ShaderReflectionConstantBuffer %p.\n", d);
258 return &d->ID3D11ShaderReflectionConstantBuffer_iface;
262 WARN("Invalid name specified\n");
264 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
267 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
268 ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_DESC *desc)
270 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
272 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
274 if (!desc || index >= This->bound_resource_count)
276 WARN("Invalid argument specified\n");
280 *desc = This->bound_resources[index];
285 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
286 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
288 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
290 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
292 if (!desc || !This->isgn || index >= This->isgn->element_count)
294 WARN("Invalid argument specified\n");
298 *desc = This->isgn->elements[index];
303 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
304 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
306 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
308 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
310 if (!desc || !This->osgn || index >= This->osgn->element_count)
312 WARN("Invalid argument specified\n");
316 *desc = This->osgn->elements[index];
321 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
322 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
324 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
326 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
328 if (!desc || !This->pcsg || index >= This->pcsg->element_count)
330 WARN("Invalid argument specified\n");
334 *desc = This->pcsg->elements[index];
339 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
340 ID3D11ShaderReflection *iface, LPCSTR name)
342 FIXME("iface %p, name %s stub!\n", iface, name);
347 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
348 ID3D11ShaderReflection *iface, LPCSTR name, D3D11_SHADER_INPUT_BIND_DESC *desc)
350 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
353 TRACE("iface %p, name %s, desc %p\n", iface, debugstr_a(name), desc);
357 WARN("Invalid argument specified\n");
361 for (i = 0; i < This->bound_resource_count; ++i)
363 D3D11_SHADER_INPUT_BIND_DESC *d = &This->bound_resources[i];
365 if (!strcmp(d->Name, name))
367 TRACE("Returning D3D11_SHADER_INPUT_BIND_DESC %p.\n", d);
373 WARN("Invalid name specified\n");
378 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
379 ID3D11ShaderReflection *iface)
381 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
383 TRACE("iface %p\n", iface);
385 return This->mov_instruction_count;
388 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
389 ID3D11ShaderReflection *iface)
391 FIXME("iface %p stub!\n", iface);
396 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
397 ID3D11ShaderReflection *iface)
399 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
401 TRACE("iface %p\n", iface);
403 return This->conversion_instruction_count;
406 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
407 ID3D11ShaderReflection *iface)
409 FIXME("iface %p stub!\n", iface);
414 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
415 ID3D11ShaderReflection *iface)
417 FIXME("iface %p stub!\n", iface);
422 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
423 ID3D11ShaderReflection *iface)
425 FIXME("iface %p stub!\n", iface);
430 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
431 ID3D11ShaderReflection *iface)
433 FIXME("iface %p stub!\n", iface);
438 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
439 ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
441 FIXME("iface %p, level %p stub!\n", iface, level);
446 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
447 ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
449 FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
454 const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
456 /* IUnknown methods */
457 d3dcompiler_shader_reflection_QueryInterface,
458 d3dcompiler_shader_reflection_AddRef,
459 d3dcompiler_shader_reflection_Release,
460 /* ID3D11ShaderReflection methods */
461 d3dcompiler_shader_reflection_GetDesc,
462 d3dcompiler_shader_reflection_GetConstantBufferByIndex,
463 d3dcompiler_shader_reflection_GetConstantBufferByName,
464 d3dcompiler_shader_reflection_GetResourceBindingDesc,
465 d3dcompiler_shader_reflection_GetInputParameterDesc,
466 d3dcompiler_shader_reflection_GetOutputParameterDesc,
467 d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
468 d3dcompiler_shader_reflection_GetVariableByName,
469 d3dcompiler_shader_reflection_GetResourceBindingDescByName,
470 d3dcompiler_shader_reflection_GetMovInstructionCount,
471 d3dcompiler_shader_reflection_GetMovcInstructionCount,
472 d3dcompiler_shader_reflection_GetConversionInstructionCount,
473 d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
474 d3dcompiler_shader_reflection_GetGSInputPrimitive,
475 d3dcompiler_shader_reflection_IsSampleFrequencyShader,
476 d3dcompiler_shader_reflection_GetNumInterfaceSlots,
477 d3dcompiler_shader_reflection_GetMinFeatureLevel,
478 d3dcompiler_shader_reflection_GetThreadGroupSize,
481 /* ID3D11ShaderReflectionConstantBuffer methods */
483 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetDesc(
484 ID3D11ShaderReflectionConstantBuffer *iface, D3D11_SHADER_BUFFER_DESC *desc)
486 FIXME("iface %p, desc %p stub!\n", iface, desc);
491 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex(
492 ID3D11ShaderReflectionConstantBuffer *iface, UINT index)
494 FIXME("iface %p, index %u stub!\n", iface, index);
499 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByName(
500 ID3D11ShaderReflectionConstantBuffer *iface, LPCSTR name)
502 FIXME("iface %p, name %s stub!\n", iface, name);
507 const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl =
509 /* ID3D11ShaderReflectionConstantBuffer methods */
510 d3dcompiler_shader_reflection_constant_buffer_GetDesc,
511 d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex,
512 d3dcompiler_shader_reflection_constant_buffer_GetVariableByName,
515 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
517 const char *ptr = data;
518 DWORD size = data_size >> 2;
520 TRACE("Size %u\n", size);
522 read_dword(&ptr, &r->instruction_count);
523 TRACE("InstructionCount: %u\n", r->instruction_count);
525 read_dword(&ptr, &r->temp_register_count);
526 TRACE("TempRegisterCount: %u\n", r->temp_register_count);
528 skip_dword_unknown(&ptr, 1);
530 read_dword(&ptr, &r->dcl_count);
531 TRACE("DclCount: %u\n", r->dcl_count);
533 read_dword(&ptr, &r->float_instruction_count);
534 TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
536 read_dword(&ptr, &r->int_instruction_count);
537 TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
539 read_dword(&ptr, &r->uint_instruction_count);
540 TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
542 read_dword(&ptr, &r->static_flow_control_count);
543 TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
545 read_dword(&ptr, &r->dynamic_flow_control_count);
546 TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
548 skip_dword_unknown(&ptr, 1);
550 read_dword(&ptr, &r->temp_array_count);
551 TRACE("TempArrayCount: %u\n", r->temp_array_count);
553 read_dword(&ptr, &r->array_instruction_count);
554 TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
556 read_dword(&ptr, &r->cut_instruction_count);
557 TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
559 read_dword(&ptr, &r->emit_instruction_count);
560 TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
562 read_dword(&ptr, &r->texture_normal_instructions);
563 TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
565 read_dword(&ptr, &r->texture_load_instructions);
566 TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
568 read_dword(&ptr, &r->texture_comp_instructions);
569 TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
571 read_dword(&ptr, &r->texture_bias_instructions);
572 TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
574 read_dword(&ptr, &r->texture_gradient_instructions);
575 TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
577 read_dword(&ptr, &r->mov_instruction_count);
578 TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
580 skip_dword_unknown(&ptr, 1);
582 read_dword(&ptr, &r->conversion_instruction_count);
583 TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
585 skip_dword_unknown(&ptr, 1);
587 read_dword(&ptr, &r->input_primitive);
588 TRACE("InputPrimitive: %x\n", r->input_primitive);
590 read_dword(&ptr, &r->gs_output_topology);
591 TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
593 read_dword(&ptr, &r->gs_max_output_vertex_count);
594 TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
596 skip_dword_unknown(&ptr, 3);
599 if (size == 29) return S_OK;
601 skip_dword_unknown(&ptr, 1);
603 read_dword(&ptr, &r->c_control_points);
604 TRACE("cControlPoints: %u\n", r->c_control_points);
606 read_dword(&ptr, &r->hs_output_primitive);
607 TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
609 read_dword(&ptr, &r->hs_prtitioning);
610 TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
612 read_dword(&ptr, &r->tessellator_domain);
613 TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
615 skip_dword_unknown(&ptr, 3);
618 if (size == 37) return S_OK;
620 FIXME("Unhandled size %u\n", size);
625 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
627 const char *ptr = data;
628 DWORD size = data_size >> 2;
629 DWORD offset, cbuffer_offset, resource_offset, creator_offset;
630 unsigned int i, string_data_offset, string_data_size;
631 char *string_data = NULL, *creator = NULL;
632 D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
633 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers = NULL;
636 TRACE("Size %u\n", size);
638 read_dword(&ptr, &r->constant_buffer_count);
639 TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
641 read_dword(&ptr, &cbuffer_offset);
642 TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
644 read_dword(&ptr, &r->bound_resource_count);
645 TRACE("Bound resource count: %u\n", r->bound_resource_count);
647 read_dword(&ptr, &resource_offset);
648 TRACE("Bound resource offset: %#x\n", resource_offset);
650 read_dword(&ptr, &r->target);
651 TRACE("Target: %#x\n", r->target);
653 read_dword(&ptr, &r->flags);
654 TRACE("Flags: %u\n", r->flags);
656 read_dword(&ptr, &creator_offset);
657 TRACE("Creator at offset %#x.\n", creator_offset);
659 if (!copy_name(data + creator_offset, &creator))
661 ERR("Failed to copy name.\n");
662 return E_OUTOFMEMORY;
664 TRACE("Creator: %s.\n", debugstr_a(creator));
666 /* todo: Parse RD11 */
667 if ((r->target & 0x0000ffff) >= 0x500)
669 skip_dword_unknown(&ptr, 8);
672 if (r->bound_resource_count)
674 /* 8 for each bind desc */
675 string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
676 string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
678 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
681 ERR("Failed to allocate string data memory.\n");
685 memcpy(string_data, data + string_data_offset, string_data_size);
687 bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
688 if (!bound_resources)
690 ERR("Failed to allocate resources memory.\n");
695 ptr = data + resource_offset;
696 for (i = 0; i < r->bound_resource_count; i++)
698 D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
700 read_dword(&ptr, &offset);
701 desc->Name = string_data + (offset - string_data_offset);
702 TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
704 read_dword(&ptr, &desc->Type);
705 TRACE("Input bind Type: %#x\n", desc->Type);
707 read_dword(&ptr, &desc->ReturnType);
708 TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
710 read_dword(&ptr, &desc->Dimension);
711 TRACE("Input bind Dimension: %#x\n", desc->Dimension);
713 read_dword(&ptr, &desc->NumSamples);
714 TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
716 read_dword(&ptr, &desc->BindPoint);
717 TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
719 read_dword(&ptr, &desc->BindCount);
720 TRACE("Input bind BindCount: %u\n", desc->BindCount);
722 read_dword(&ptr, &desc->uFlags);
723 TRACE("Input bind uFlags: %u\n", desc->uFlags);
727 if (r->constant_buffer_count)
729 constant_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, r->constant_buffer_count * sizeof(*constant_buffers));
730 if (!constant_buffers)
732 ERR("Failed to allocate constant buffer memory.\n");
737 ptr = data + cbuffer_offset;
738 for (i = 0; i < r->constant_buffer_count; i++)
740 struct d3dcompiler_shader_reflection_constant_buffer *cb = &constant_buffers[i];
742 cb->ID3D11ShaderReflectionConstantBuffer_iface.lpVtbl = &d3dcompiler_shader_reflection_constant_buffer_vtbl;
745 read_dword(&ptr, &offset);
746 if (!copy_name(data + offset, &cb->name))
748 ERR("Failed to copy name.\n");
752 TRACE("Name: %s.\n", debugstr_a(cb->name));
754 read_dword(&ptr, &cb->variable_count);
755 TRACE("Variable count: %u\n", cb->variable_count);
757 /* todo: Parse variables */
758 read_dword(&ptr, &offset);
759 FIXME("Variable offset: %x\n", offset);
761 read_dword(&ptr, &cb->size);
762 TRACE("Cbuffer size: %u\n", cb->size);
764 read_dword(&ptr, &cb->flags);
765 TRACE("Cbuffer flags: %u\n", cb->flags);
767 read_dword(&ptr, &cb->type);
768 TRACE("Cbuffer type: %#x\n", cb->type);
772 r->creator = creator;
773 r->resource_string = string_data;
774 r->bound_resources = bound_resources;
775 r->constant_buffers = constant_buffers;
780 for (i = 0; i < r->constant_buffer_count; ++i)
782 free_constant_buffer(&constant_buffers[i]);
784 HeapFree(GetProcessHeap(), 0, constant_buffers);
785 HeapFree(GetProcessHeap(), 0, bound_resources);
786 HeapFree(GetProcessHeap(), 0, string_data);
787 HeapFree(GetProcessHeap(), 0, creator);
792 HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section)
794 D3D11_SIGNATURE_PARAMETER_DESC *d;
795 unsigned int string_data_offset;
796 unsigned int string_data_size;
797 const char *ptr = section->data;
801 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
803 switch (section->tag)
806 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
812 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
816 FIXME("Unhandled section %s!\n", debugstr_an((const char *)§ion->tag, 4));
817 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
821 read_dword(&ptr, &count);
822 TRACE("%u elements\n", count);
824 skip_dword_unknown(&ptr, 1);
826 d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
829 ERR("Failed to allocate signature memory.\n");
830 return E_OUTOFMEMORY;
833 /* 2 DWORDs for the header, element_size for each element. */
834 string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
835 string_data_size = section->data_size - string_data_offset;
837 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
840 ERR("Failed to allocate string data memory.\n");
841 HeapFree(GetProcessHeap(), 0, d);
842 return E_OUTOFMEMORY;
844 memcpy(string_data, section->data + string_data_offset, string_data_size);
846 for (i = 0; i < count; ++i)
851 if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
853 read_dword(&ptr, &d[i].Stream);
860 read_dword(&ptr, &name_offset);
861 d[i].SemanticName = string_data + (name_offset - string_data_offset);
862 read_dword(&ptr, &d[i].SemanticIndex);
863 read_dword(&ptr, &d[i].SystemValueType);
864 read_dword(&ptr, &d[i].ComponentType);
865 read_dword(&ptr, &d[i].Register);
866 read_dword(&ptr, &mask);
867 d[i].ReadWriteMask = (mask >> 8) & 0xff;
868 d[i].Mask = mask & 0xff;
870 TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
871 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
872 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
873 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
877 s->element_count = count;
878 s->string_data = string_data;
883 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
885 const char *ptr = data;
887 read_dword(&ptr, &r->version);
888 TRACE("Shader version: %u\n", r->version);
890 /* todo: Check if anything else is needed from the shdr or shex blob. */
895 HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
896 const void *data, SIZE_T data_size)
898 struct dxbc src_dxbc;
902 reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
903 reflection->refcount = 1;
905 hr = dxbc_parse(data, data_size, &src_dxbc);
908 WARN("Failed to parse reflection\n");
912 for (i = 0; i < src_dxbc.count; ++i)
914 struct dxbc_section *section = &src_dxbc.sections[i];
916 switch (section->tag)
919 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
922 WARN("Failed to parse section STAT.\n");
929 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
932 WARN("Failed to parse SHDR section.\n");
938 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
941 WARN("Failed to parse RDEF section.\n");
947 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
948 if (!reflection->isgn)
950 ERR("Failed to allocate ISGN memory.\n");
955 hr = d3dcompiler_parse_signature(reflection->isgn, section);
958 WARN("Failed to parse section ISGN.\n");
965 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
966 if (!reflection->osgn)
968 ERR("Failed to allocate OSGN memory.\n");
973 hr = d3dcompiler_parse_signature(reflection->osgn, section);
976 WARN("Failed to parse section OSGN.\n");
982 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
983 if (!reflection->pcsg)
985 ERR("Failed to allocate PCSG memory.\n");
990 hr = d3dcompiler_parse_signature(reflection->pcsg, section);
993 WARN("Failed to parse section PCSG.\n");
999 FIXME("Unhandled section %s!\n", debugstr_an((const char *)§ion->tag, 4));
1004 dxbc_destroy(&src_dxbc);
1009 reflection_cleanup(reflection);
1010 dxbc_destroy(&src_dxbc);