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 BOOL copy_value(const char *ptr, void **value, DWORD size)
65 if (!ptr || !size) return TRUE;
67 *value = HeapAlloc(GetProcessHeap(), 0, size);
70 ERR("Failed to allocate vlaue memory.\n");
74 memcpy(*value, ptr, size);
79 static void free_signature(struct d3dcompiler_shader_signature *sig)
81 TRACE("Free signature %p\n", sig);
83 HeapFree(GetProcessHeap(), 0, sig->elements);
84 HeapFree(GetProcessHeap(), 0, sig->string_data);
87 static void free_variable(struct d3dcompiler_shader_reflection_variable *var)
91 HeapFree(GetProcessHeap(), 0, var->name);
92 HeapFree(GetProcessHeap(), 0, var->default_value);
96 static void free_constant_buffer(struct d3dcompiler_shader_reflection_constant_buffer *cb)
102 for (i = 0; i < cb->variable_count; ++i)
104 free_variable(&cb->variables[i]);
106 HeapFree(GetProcessHeap(), 0, cb->variables);
109 HeapFree(GetProcessHeap(), 0, cb->name);
112 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
114 TRACE("Cleanup %p\n", ref);
118 free_signature(ref->isgn);
119 HeapFree(GetProcessHeap(), 0, ref->isgn);
124 free_signature(ref->osgn);
125 HeapFree(GetProcessHeap(), 0, ref->osgn);
130 free_signature(ref->pcsg);
131 HeapFree(GetProcessHeap(), 0, ref->pcsg);
134 if (ref->constant_buffers)
138 for (i = 0; i < ref->constant_buffer_count; ++i)
140 free_constant_buffer(&ref->constant_buffers[i]);
144 HeapFree(GetProcessHeap(), 0, ref->constant_buffers);
145 HeapFree(GetProcessHeap(), 0, ref->bound_resources);
146 HeapFree(GetProcessHeap(), 0, ref->resource_string);
147 HeapFree(GetProcessHeap(), 0, ref->creator);
150 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
152 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
155 /* IUnknown methods */
157 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
159 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
161 if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
162 || IsEqualGUID(riid, &IID_IUnknown))
164 IUnknown_AddRef(iface);
169 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
172 return E_NOINTERFACE;
175 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
177 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
178 ULONG refcount = InterlockedIncrement(&This->refcount);
180 TRACE("%p increasing refcount to %u\n", This, refcount);
185 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
187 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
188 ULONG refcount = InterlockedDecrement(&This->refcount);
190 TRACE("%p decreasing refcount to %u\n", This, refcount);
194 reflection_cleanup(This);
195 HeapFree(GetProcessHeap(), 0, This);
201 /* ID3D11ShaderReflection methods */
203 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
205 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
207 FIXME("iface %p, desc %p partial stub!\n", iface, desc);
211 WARN("Invalid argument specified\n");
215 desc->Version = This->version;
216 desc->Creator = This->creator;
217 desc->Flags = This->flags;
218 desc->ConstantBuffers = This->constant_buffer_count;
219 desc->BoundResources = This->bound_resource_count;
220 desc->InputParameters = This->isgn ? This->isgn->element_count : 0;
221 desc->OutputParameters = This->osgn ? This->osgn->element_count : 0;
222 desc->InstructionCount = This->instruction_count;
223 desc->TempRegisterCount = This->temp_register_count;
224 desc->TempArrayCount = This->temp_array_count;
226 desc->DclCount = This->dcl_count;
227 desc->TextureNormalInstructions = This->texture_normal_instructions;
228 desc->TextureLoadInstructions = This->texture_load_instructions;
229 desc->TextureCompInstructions = This->texture_comp_instructions;
230 desc->TextureBiasInstructions = This->texture_bias_instructions;
231 desc->TextureGradientInstructions = This->texture_gradient_instructions;
232 desc->FloatInstructionCount = This->float_instruction_count;
233 desc->IntInstructionCount = This->int_instruction_count;
234 desc->UintInstructionCount = This->uint_instruction_count;
235 desc->StaticFlowControlCount = This->static_flow_control_count;
236 desc->DynamicFlowControlCount = This->dynamic_flow_control_count;
237 desc->MacroInstructionCount = 0;
238 desc->ArrayInstructionCount = This->array_instruction_count;
239 desc->CutInstructionCount = This->cut_instruction_count;
240 desc->EmitInstructionCount = This->emit_instruction_count;
241 desc->GSOutputTopology = This->gs_output_topology;
242 desc->GSMaxOutputVertexCount = This->gs_max_output_vertex_count;
243 desc->InputPrimitive = This->input_primitive;
244 desc->PatchConstantParameters = This->pcsg ? This->pcsg->element_count : 0;
245 desc->cGSInstanceCount = 0;
246 desc->cControlPoints = This->c_control_points;
247 desc->HSOutputPrimitive = This->hs_output_primitive;
248 desc->HSPartitioning = This->hs_prtitioning;
249 desc->TessellatorDomain = This->tessellator_domain;
250 desc->cBarrierInstructions = 0;
251 desc->cInterlockedInstructions = 0;
252 desc->cTextureStoreInstructions = 0;
257 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
258 ID3D11ShaderReflection *iface, UINT index)
260 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
262 TRACE("iface %p, index %u\n", iface, index);
264 if (index >= This->constant_buffer_count)
266 WARN("Invalid argument specified\n");
267 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
270 return &This->constant_buffers[index].ID3D11ShaderReflectionConstantBuffer_iface;
273 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
274 ID3D11ShaderReflection *iface, LPCSTR name)
276 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
279 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
283 WARN("Invalid argument specified\n");
284 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
287 for (i = 0; i < This->constant_buffer_count; ++i)
289 struct d3dcompiler_shader_reflection_constant_buffer *d = &This->constant_buffers[i];
291 if (!strcmp(d->name, name))
293 TRACE("Returning ID3D11ShaderReflectionConstantBuffer %p.\n", d);
294 return &d->ID3D11ShaderReflectionConstantBuffer_iface;
298 WARN("Invalid name specified\n");
300 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
303 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
304 ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_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 || index >= This->bound_resource_count)
312 WARN("Invalid argument specified\n");
316 *desc = This->bound_resources[index];
321 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
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->isgn || index >= This->isgn->element_count)
330 WARN("Invalid argument specified\n");
334 *desc = This->isgn->elements[index];
339 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
340 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
342 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
344 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
346 if (!desc || !This->osgn || index >= This->osgn->element_count)
348 WARN("Invalid argument specified\n");
352 *desc = This->osgn->elements[index];
357 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
358 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
360 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
362 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
364 if (!desc || !This->pcsg || index >= This->pcsg->element_count)
366 WARN("Invalid argument specified\n");
370 *desc = This->pcsg->elements[index];
375 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
376 ID3D11ShaderReflection *iface, LPCSTR name)
378 FIXME("iface %p, name %s stub!\n", iface, name);
383 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
384 ID3D11ShaderReflection *iface, LPCSTR name, D3D11_SHADER_INPUT_BIND_DESC *desc)
386 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
389 TRACE("iface %p, name %s, desc %p\n", iface, debugstr_a(name), desc);
393 WARN("Invalid argument specified\n");
397 for (i = 0; i < This->bound_resource_count; ++i)
399 D3D11_SHADER_INPUT_BIND_DESC *d = &This->bound_resources[i];
401 if (!strcmp(d->Name, name))
403 TRACE("Returning D3D11_SHADER_INPUT_BIND_DESC %p.\n", d);
409 WARN("Invalid name specified\n");
414 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
415 ID3D11ShaderReflection *iface)
417 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
419 TRACE("iface %p\n", iface);
421 return This->mov_instruction_count;
424 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
425 ID3D11ShaderReflection *iface)
427 FIXME("iface %p stub!\n", iface);
432 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
433 ID3D11ShaderReflection *iface)
435 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
437 TRACE("iface %p\n", iface);
439 return This->conversion_instruction_count;
442 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
443 ID3D11ShaderReflection *iface)
445 FIXME("iface %p stub!\n", iface);
450 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
451 ID3D11ShaderReflection *iface)
453 FIXME("iface %p stub!\n", iface);
458 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
459 ID3D11ShaderReflection *iface)
461 FIXME("iface %p stub!\n", iface);
466 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
467 ID3D11ShaderReflection *iface)
469 FIXME("iface %p stub!\n", iface);
474 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
475 ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
477 FIXME("iface %p, level %p stub!\n", iface, level);
482 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
483 ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
485 FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
490 const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
492 /* IUnknown methods */
493 d3dcompiler_shader_reflection_QueryInterface,
494 d3dcompiler_shader_reflection_AddRef,
495 d3dcompiler_shader_reflection_Release,
496 /* ID3D11ShaderReflection methods */
497 d3dcompiler_shader_reflection_GetDesc,
498 d3dcompiler_shader_reflection_GetConstantBufferByIndex,
499 d3dcompiler_shader_reflection_GetConstantBufferByName,
500 d3dcompiler_shader_reflection_GetResourceBindingDesc,
501 d3dcompiler_shader_reflection_GetInputParameterDesc,
502 d3dcompiler_shader_reflection_GetOutputParameterDesc,
503 d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
504 d3dcompiler_shader_reflection_GetVariableByName,
505 d3dcompiler_shader_reflection_GetResourceBindingDescByName,
506 d3dcompiler_shader_reflection_GetMovInstructionCount,
507 d3dcompiler_shader_reflection_GetMovcInstructionCount,
508 d3dcompiler_shader_reflection_GetConversionInstructionCount,
509 d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
510 d3dcompiler_shader_reflection_GetGSInputPrimitive,
511 d3dcompiler_shader_reflection_IsSampleFrequencyShader,
512 d3dcompiler_shader_reflection_GetNumInterfaceSlots,
513 d3dcompiler_shader_reflection_GetMinFeatureLevel,
514 d3dcompiler_shader_reflection_GetThreadGroupSize,
517 /* ID3D11ShaderReflectionConstantBuffer methods */
519 static inline struct d3dcompiler_shader_reflection_constant_buffer *impl_from_ID3D11ShaderReflectionConstantBuffer(ID3D11ShaderReflectionConstantBuffer *iface)
521 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_constant_buffer, ID3D11ShaderReflectionConstantBuffer_iface);
524 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetDesc(
525 ID3D11ShaderReflectionConstantBuffer *iface, D3D11_SHADER_BUFFER_DESC *desc)
527 struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
529 TRACE("iface %p, desc %p\n", iface, desc);
531 if (This == &null_constant_buffer)
533 WARN("Null constant buffer specified\n");
539 WARN("Invalid argument specified\n");
543 desc->Name = This->name;
544 desc->Type = This->type;
545 desc->Variables = This->variable_count;
546 desc->Size = This->size;
547 desc->uFlags = This->flags;
552 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex(
553 ID3D11ShaderReflectionConstantBuffer *iface, UINT index)
555 FIXME("iface %p, index %u stub!\n", iface, index);
560 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByName(
561 ID3D11ShaderReflectionConstantBuffer *iface, LPCSTR name)
563 FIXME("iface %p, name %s stub!\n", iface, name);
568 const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl =
570 /* ID3D11ShaderReflectionConstantBuffer methods */
571 d3dcompiler_shader_reflection_constant_buffer_GetDesc,
572 d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex,
573 d3dcompiler_shader_reflection_constant_buffer_GetVariableByName,
576 /* ID3D11ShaderReflectionVariable methods */
578 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetDesc(
579 ID3D11ShaderReflectionVariable *iface, D3D11_SHADER_VARIABLE_DESC *desc)
581 FIXME("iface %p, desc %p stub!\n", iface, desc);
586 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetType(
587 ID3D11ShaderReflectionVariable *iface)
589 FIXME("iface %p stub!\n", iface);
594 static ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetBuffer(
595 ID3D11ShaderReflectionVariable *iface)
597 FIXME("iface %p stub!\n", iface);
602 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetInterfaceSlot(
603 ID3D11ShaderReflectionVariable *iface, UINT index)
605 FIXME("iface %p, index %u stub!\n", iface, index);
610 const struct ID3D11ShaderReflectionVariableVtbl d3dcompiler_shader_reflection_variable_vtbl =
612 /* ID3D11ShaderReflectionVariable methods */
613 d3dcompiler_shader_reflection_variable_GetDesc,
614 d3dcompiler_shader_reflection_variable_GetType,
615 d3dcompiler_shader_reflection_variable_GetBuffer,
616 d3dcompiler_shader_reflection_variable_GetInterfaceSlot,
619 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
621 const char *ptr = data;
622 DWORD size = data_size >> 2;
624 TRACE("Size %u\n", size);
626 read_dword(&ptr, &r->instruction_count);
627 TRACE("InstructionCount: %u\n", r->instruction_count);
629 read_dword(&ptr, &r->temp_register_count);
630 TRACE("TempRegisterCount: %u\n", r->temp_register_count);
632 skip_dword_unknown(&ptr, 1);
634 read_dword(&ptr, &r->dcl_count);
635 TRACE("DclCount: %u\n", r->dcl_count);
637 read_dword(&ptr, &r->float_instruction_count);
638 TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
640 read_dword(&ptr, &r->int_instruction_count);
641 TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
643 read_dword(&ptr, &r->uint_instruction_count);
644 TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
646 read_dword(&ptr, &r->static_flow_control_count);
647 TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
649 read_dword(&ptr, &r->dynamic_flow_control_count);
650 TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
652 skip_dword_unknown(&ptr, 1);
654 read_dword(&ptr, &r->temp_array_count);
655 TRACE("TempArrayCount: %u\n", r->temp_array_count);
657 read_dword(&ptr, &r->array_instruction_count);
658 TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
660 read_dword(&ptr, &r->cut_instruction_count);
661 TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
663 read_dword(&ptr, &r->emit_instruction_count);
664 TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
666 read_dword(&ptr, &r->texture_normal_instructions);
667 TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
669 read_dword(&ptr, &r->texture_load_instructions);
670 TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
672 read_dword(&ptr, &r->texture_comp_instructions);
673 TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
675 read_dword(&ptr, &r->texture_bias_instructions);
676 TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
678 read_dword(&ptr, &r->texture_gradient_instructions);
679 TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
681 read_dword(&ptr, &r->mov_instruction_count);
682 TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
684 skip_dword_unknown(&ptr, 1);
686 read_dword(&ptr, &r->conversion_instruction_count);
687 TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
689 skip_dword_unknown(&ptr, 1);
691 read_dword(&ptr, &r->input_primitive);
692 TRACE("InputPrimitive: %x\n", r->input_primitive);
694 read_dword(&ptr, &r->gs_output_topology);
695 TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
697 read_dword(&ptr, &r->gs_max_output_vertex_count);
698 TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
700 skip_dword_unknown(&ptr, 3);
703 if (size == 29) return S_OK;
705 skip_dword_unknown(&ptr, 1);
707 read_dword(&ptr, &r->c_control_points);
708 TRACE("cControlPoints: %u\n", r->c_control_points);
710 read_dword(&ptr, &r->hs_output_primitive);
711 TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
713 read_dword(&ptr, &r->hs_prtitioning);
714 TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
716 read_dword(&ptr, &r->tessellator_domain);
717 TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
719 skip_dword_unknown(&ptr, 3);
722 if (size == 37) return S_OK;
724 FIXME("Unhandled size %u\n", size);
729 static HRESULT d3dcompiler_parse_variables(struct d3dcompiler_shader_reflection_constant_buffer *cb,
730 const char *data, DWORD data_size, const char *ptr)
732 struct d3dcompiler_shader_reflection_variable *variables;
736 variables = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb->variable_count * sizeof(*variables));
739 ERR("Failed to allocate variables memory.\n");
740 return E_OUTOFMEMORY;
743 for (i = 0; i < cb->variable_count; i++)
745 struct d3dcompiler_shader_reflection_variable *v = &variables[i];
748 v->ID3D11ShaderReflectionVariable_iface.lpVtbl = &d3dcompiler_shader_reflection_variable_vtbl;
750 read_dword(&ptr, &offset);
751 if (!copy_name(data + offset, &v->name))
753 ERR("Failed to copy name.\n");
757 TRACE("Variable name: %s.\n", debugstr_a(v->name));
759 read_dword(&ptr, &v->start_offset);
760 TRACE("Variable offset: %u\n", v->start_offset);
762 read_dword(&ptr, &v->size);
763 TRACE("Variable size: %u\n", v->size);
765 read_dword(&ptr, &v->flags);
766 TRACE("Variable flags: %u\n", v->flags);
768 /* todo: Parse types */
769 read_dword(&ptr, &offset);
770 FIXME("Variable type offset: %x\n", offset);
772 read_dword(&ptr, &offset);
773 TRACE("Variable default value offset: %x\n", offset);
774 if (!copy_value(data + offset, &v->default_value, offset ? v->size : 0))
776 ERR("Failed to copy name.\n");
781 if ((cb->reflection->target & 0xffff) >= 0x500)
782 skip_dword_unknown(&ptr, 4);
785 cb->variables = variables;
790 for (i = 0; i < cb->variable_count; i++)
792 free_variable(&variables[i]);
794 HeapFree(GetProcessHeap(), 0, variables);
798 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
800 const char *ptr = data;
801 DWORD size = data_size >> 2;
802 DWORD offset, cbuffer_offset, resource_offset, creator_offset;
803 unsigned int i, string_data_offset, string_data_size;
804 char *string_data = NULL, *creator = NULL;
805 D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
806 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers = NULL;
809 TRACE("Size %u\n", size);
811 read_dword(&ptr, &r->constant_buffer_count);
812 TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
814 read_dword(&ptr, &cbuffer_offset);
815 TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
817 read_dword(&ptr, &r->bound_resource_count);
818 TRACE("Bound resource count: %u\n", r->bound_resource_count);
820 read_dword(&ptr, &resource_offset);
821 TRACE("Bound resource offset: %#x\n", resource_offset);
823 read_dword(&ptr, &r->target);
824 TRACE("Target: %#x\n", r->target);
826 read_dword(&ptr, &r->flags);
827 TRACE("Flags: %u\n", r->flags);
829 read_dword(&ptr, &creator_offset);
830 TRACE("Creator at offset %#x.\n", creator_offset);
832 if (!copy_name(data + creator_offset, &creator))
834 ERR("Failed to copy name.\n");
835 return E_OUTOFMEMORY;
837 TRACE("Creator: %s.\n", debugstr_a(creator));
839 /* todo: Parse RD11 */
840 if ((r->target & 0x0000ffff) >= 0x500)
842 skip_dword_unknown(&ptr, 8);
845 if (r->bound_resource_count)
847 /* 8 for each bind desc */
848 string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
849 string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
851 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
854 ERR("Failed to allocate string data memory.\n");
858 memcpy(string_data, data + string_data_offset, string_data_size);
860 bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
861 if (!bound_resources)
863 ERR("Failed to allocate resources memory.\n");
868 ptr = data + resource_offset;
869 for (i = 0; i < r->bound_resource_count; i++)
871 D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
873 read_dword(&ptr, &offset);
874 desc->Name = string_data + (offset - string_data_offset);
875 TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
877 read_dword(&ptr, &desc->Type);
878 TRACE("Input bind Type: %#x\n", desc->Type);
880 read_dword(&ptr, &desc->ReturnType);
881 TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
883 read_dword(&ptr, &desc->Dimension);
884 TRACE("Input bind Dimension: %#x\n", desc->Dimension);
886 read_dword(&ptr, &desc->NumSamples);
887 TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
889 read_dword(&ptr, &desc->BindPoint);
890 TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
892 read_dword(&ptr, &desc->BindCount);
893 TRACE("Input bind BindCount: %u\n", desc->BindCount);
895 read_dword(&ptr, &desc->uFlags);
896 TRACE("Input bind uFlags: %u\n", desc->uFlags);
900 if (r->constant_buffer_count)
902 constant_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, r->constant_buffer_count * sizeof(*constant_buffers));
903 if (!constant_buffers)
905 ERR("Failed to allocate constant buffer memory.\n");
910 ptr = data + cbuffer_offset;
911 for (i = 0; i < r->constant_buffer_count; i++)
913 struct d3dcompiler_shader_reflection_constant_buffer *cb = &constant_buffers[i];
915 cb->ID3D11ShaderReflectionConstantBuffer_iface.lpVtbl = &d3dcompiler_shader_reflection_constant_buffer_vtbl;
918 read_dword(&ptr, &offset);
919 if (!copy_name(data + offset, &cb->name))
921 ERR("Failed to copy name.\n");
925 TRACE("Name: %s.\n", debugstr_a(cb->name));
927 read_dword(&ptr, &cb->variable_count);
928 TRACE("Variable count: %u\n", cb->variable_count);
930 read_dword(&ptr, &offset);
931 TRACE("Variable offset: %x\n", offset);
933 hr = d3dcompiler_parse_variables(cb, data, data_size, data + offset);
936 FIXME("Failed to parse variables.");
940 read_dword(&ptr, &cb->size);
941 TRACE("Cbuffer size: %u\n", cb->size);
943 read_dword(&ptr, &cb->flags);
944 TRACE("Cbuffer flags: %u\n", cb->flags);
946 read_dword(&ptr, &cb->type);
947 TRACE("Cbuffer type: %#x\n", cb->type);
951 r->creator = creator;
952 r->resource_string = string_data;
953 r->bound_resources = bound_resources;
954 r->constant_buffers = constant_buffers;
959 for (i = 0; i < r->constant_buffer_count; ++i)
961 free_constant_buffer(&constant_buffers[i]);
963 HeapFree(GetProcessHeap(), 0, constant_buffers);
964 HeapFree(GetProcessHeap(), 0, bound_resources);
965 HeapFree(GetProcessHeap(), 0, string_data);
966 HeapFree(GetProcessHeap(), 0, creator);
971 HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section)
973 D3D11_SIGNATURE_PARAMETER_DESC *d;
974 unsigned int string_data_offset;
975 unsigned int string_data_size;
976 const char *ptr = section->data;
980 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
982 switch (section->tag)
985 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
991 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
995 FIXME("Unhandled section %s!\n", debugstr_an((const char *)§ion->tag, 4));
996 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1000 read_dword(&ptr, &count);
1001 TRACE("%u elements\n", count);
1003 skip_dword_unknown(&ptr, 1);
1005 d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
1008 ERR("Failed to allocate signature memory.\n");
1009 return E_OUTOFMEMORY;
1012 /* 2 DWORDs for the header, element_size for each element. */
1013 string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
1014 string_data_size = section->data_size - string_data_offset;
1016 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1019 ERR("Failed to allocate string data memory.\n");
1020 HeapFree(GetProcessHeap(), 0, d);
1021 return E_OUTOFMEMORY;
1023 memcpy(string_data, section->data + string_data_offset, string_data_size);
1025 for (i = 0; i < count; ++i)
1030 if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
1032 read_dword(&ptr, &d[i].Stream);
1039 read_dword(&ptr, &name_offset);
1040 d[i].SemanticName = string_data + (name_offset - string_data_offset);
1041 read_dword(&ptr, &d[i].SemanticIndex);
1042 read_dword(&ptr, &d[i].SystemValueType);
1043 read_dword(&ptr, &d[i].ComponentType);
1044 read_dword(&ptr, &d[i].Register);
1045 read_dword(&ptr, &mask);
1046 d[i].ReadWriteMask = (mask >> 8) & 0xff;
1047 d[i].Mask = mask & 0xff;
1049 TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
1050 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
1051 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
1052 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
1056 s->element_count = count;
1057 s->string_data = string_data;
1062 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1064 const char *ptr = data;
1066 read_dword(&ptr, &r->version);
1067 TRACE("Shader version: %u\n", r->version);
1069 /* todo: Check if anything else is needed from the shdr or shex blob. */
1074 HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
1075 const void *data, SIZE_T data_size)
1077 struct dxbc src_dxbc;
1081 reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
1082 reflection->refcount = 1;
1084 hr = dxbc_parse(data, data_size, &src_dxbc);
1087 WARN("Failed to parse reflection\n");
1091 for (i = 0; i < src_dxbc.count; ++i)
1093 struct dxbc_section *section = &src_dxbc.sections[i];
1095 switch (section->tag)
1098 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
1101 WARN("Failed to parse section STAT.\n");
1108 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
1111 WARN("Failed to parse SHDR section.\n");
1117 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
1120 WARN("Failed to parse RDEF section.\n");
1126 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
1127 if (!reflection->isgn)
1129 ERR("Failed to allocate ISGN memory.\n");
1134 hr = d3dcompiler_parse_signature(reflection->isgn, section);
1137 WARN("Failed to parse section ISGN.\n");
1144 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
1145 if (!reflection->osgn)
1147 ERR("Failed to allocate OSGN memory.\n");
1152 hr = d3dcompiler_parse_signature(reflection->osgn, section);
1155 WARN("Failed to parse section OSGN.\n");
1161 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
1162 if (!reflection->pcsg)
1164 ERR("Failed to allocate PCSG memory.\n");
1169 hr = d3dcompiler_parse_signature(reflection->pcsg, section);
1172 WARN("Failed to parse section PCSG.\n");
1178 FIXME("Unhandled section %s!\n", debugstr_an((const char *)§ion->tag, 4));
1183 dxbc_destroy(&src_dxbc);
1188 reflection_cleanup(reflection);
1189 dxbc_destroy(&src_dxbc);