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 static BOOL copy_name(const char *ptr, char **name)
32 if (!ptr) return TRUE;
34 name_len = strlen(ptr) + 1;
40 *name = HeapAlloc(GetProcessHeap(), 0, name_len);
43 ERR("Failed to allocate name memory.\n");
47 memcpy(*name, ptr, name_len);
52 static void free_signature(struct d3dcompiler_shader_signature *sig)
54 TRACE("Free signature %p\n", sig);
56 HeapFree(GetProcessHeap(), 0, sig->elements);
57 HeapFree(GetProcessHeap(), 0, sig->string_data);
60 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
62 TRACE("Cleanup %p\n", ref);
66 free_signature(ref->isgn);
67 HeapFree(GetProcessHeap(), 0, ref->isgn);
72 free_signature(ref->osgn);
73 HeapFree(GetProcessHeap(), 0, ref->osgn);
78 free_signature(ref->pcsg);
79 HeapFree(GetProcessHeap(), 0, ref->pcsg);
82 HeapFree(GetProcessHeap(), 0, ref->creator);
85 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
87 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
90 /* IUnknown methods */
92 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
94 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
96 if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
97 || IsEqualGUID(riid, &IID_IUnknown))
99 IUnknown_AddRef(iface);
104 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
107 return E_NOINTERFACE;
110 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
112 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
113 ULONG refcount = InterlockedIncrement(&This->refcount);
115 TRACE("%p increasing refcount to %u\n", This, refcount);
120 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
122 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
123 ULONG refcount = InterlockedDecrement(&This->refcount);
125 TRACE("%p decreasing refcount to %u\n", This, refcount);
129 reflection_cleanup(This);
130 HeapFree(GetProcessHeap(), 0, This);
136 /* ID3D11ShaderReflection methods */
138 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
140 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
142 FIXME("iface %p, desc %p partial stub!\n", iface, desc);
146 WARN("Invalid argument specified\n");
150 desc->Version = This->version;
151 desc->Creator = This->creator;
152 desc->Flags = This->flags;
153 desc->ConstantBuffers = This->constant_buffer_count;
154 desc->BoundResources = This->bound_resource_count;
155 desc->InputParameters = This->isgn ? This->isgn->element_count : 0;
156 desc->OutputParameters = This->osgn ? This->osgn->element_count : 0;
157 desc->InstructionCount = This->instruction_count;
158 desc->TempRegisterCount = This->temp_register_count;
159 desc->TempArrayCount = This->temp_array_count;
161 desc->DclCount = This->dcl_count;
162 desc->TextureNormalInstructions = This->texture_normal_instructions;
163 desc->TextureLoadInstructions = This->texture_load_instructions;
164 desc->TextureCompInstructions = This->texture_comp_instructions;
165 desc->TextureBiasInstructions = This->texture_bias_instructions;
166 desc->TextureGradientInstructions = This->texture_gradient_instructions;
167 desc->FloatInstructionCount = This->float_instruction_count;
168 desc->IntInstructionCount = This->int_instruction_count;
169 desc->UintInstructionCount = This->uint_instruction_count;
170 desc->StaticFlowControlCount = This->static_flow_control_count;
171 desc->DynamicFlowControlCount = This->dynamic_flow_control_count;
172 desc->MacroInstructionCount = 0;
173 desc->ArrayInstructionCount = This->array_instruction_count;
174 desc->CutInstructionCount = This->cut_instruction_count;
175 desc->EmitInstructionCount = This->emit_instruction_count;
176 desc->GSOutputTopology = This->gs_output_topology;
177 desc->GSMaxOutputVertexCount = This->gs_max_output_vertex_count;
178 desc->InputPrimitive = This->input_primitive;
179 desc->PatchConstantParameters = This->pcsg ? This->pcsg->element_count : 0;
180 desc->cGSInstanceCount = 0;
181 desc->cControlPoints = This->c_control_points;
182 desc->HSOutputPrimitive = This->hs_output_primitive;
183 desc->HSPartitioning = This->hs_prtitioning;
184 desc->TessellatorDomain = This->tessellator_domain;
185 desc->cBarrierInstructions = 0;
186 desc->cInterlockedInstructions = 0;
187 desc->cTextureStoreInstructions = 0;
192 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
193 ID3D11ShaderReflection *iface, UINT index)
195 FIXME("iface %p, index %u stub!\n", iface, index);
200 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
201 ID3D11ShaderReflection *iface, LPCSTR name)
203 FIXME("iface %p, name \"%s\" stub!\n", iface, name);
208 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
209 ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_DESC *desc)
211 FIXME("iface %p, index %u, desc %p stub!\n", iface, index, desc);
216 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
217 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
219 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
221 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
223 if (!desc || !This->isgn || index >= This->isgn->element_count)
225 WARN("Invalid argument specified\n");
229 *desc = This->isgn->elements[index];
234 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
235 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
237 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
239 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
241 if (!desc || !This->osgn || index >= This->osgn->element_count)
243 WARN("Invalid argument specified\n");
247 *desc = This->osgn->elements[index];
252 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
253 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
255 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
257 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
259 if (!desc || !This->pcsg || index >= This->pcsg->element_count)
261 WARN("Invalid argument specified\n");
265 *desc = This->pcsg->elements[index];
270 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
271 ID3D11ShaderReflection *iface, LPCSTR name)
273 FIXME("iface %p, name %s stub!\n", iface, name);
278 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
279 ID3D11ShaderReflection *iface, LPCSTR name, D3D11_SHADER_INPUT_BIND_DESC *desc)
281 FIXME("iface %p, name %s, desc %p stub!\n", iface, name, desc);
286 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
287 ID3D11ShaderReflection *iface)
289 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
291 TRACE("iface %p\n", iface);
293 return This->mov_instruction_count;
296 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
297 ID3D11ShaderReflection *iface)
299 FIXME("iface %p stub!\n", iface);
304 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
305 ID3D11ShaderReflection *iface)
307 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
309 TRACE("iface %p\n", iface);
311 return This->conversion_instruction_count;
314 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
315 ID3D11ShaderReflection *iface)
317 FIXME("iface %p stub!\n", iface);
322 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
323 ID3D11ShaderReflection *iface)
325 FIXME("iface %p stub!\n", iface);
330 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
331 ID3D11ShaderReflection *iface)
333 FIXME("iface %p stub!\n", iface);
338 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
339 ID3D11ShaderReflection *iface)
341 FIXME("iface %p stub!\n", iface);
346 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
347 ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
349 FIXME("iface %p, level %p stub!\n", iface, level);
354 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
355 ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
357 FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
362 const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
364 /* IUnknown methods */
365 d3dcompiler_shader_reflection_QueryInterface,
366 d3dcompiler_shader_reflection_AddRef,
367 d3dcompiler_shader_reflection_Release,
368 /* ID3D11ShaderReflection methods */
369 d3dcompiler_shader_reflection_GetDesc,
370 d3dcompiler_shader_reflection_GetConstantBufferByIndex,
371 d3dcompiler_shader_reflection_GetConstantBufferByName,
372 d3dcompiler_shader_reflection_GetResourceBindingDesc,
373 d3dcompiler_shader_reflection_GetInputParameterDesc,
374 d3dcompiler_shader_reflection_GetOutputParameterDesc,
375 d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
376 d3dcompiler_shader_reflection_GetVariableByName,
377 d3dcompiler_shader_reflection_GetResourceBindingDescByName,
378 d3dcompiler_shader_reflection_GetMovInstructionCount,
379 d3dcompiler_shader_reflection_GetMovcInstructionCount,
380 d3dcompiler_shader_reflection_GetConversionInstructionCount,
381 d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
382 d3dcompiler_shader_reflection_GetGSInputPrimitive,
383 d3dcompiler_shader_reflection_IsSampleFrequencyShader,
384 d3dcompiler_shader_reflection_GetNumInterfaceSlots,
385 d3dcompiler_shader_reflection_GetMinFeatureLevel,
386 d3dcompiler_shader_reflection_GetThreadGroupSize,
389 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
391 const char *ptr = data;
392 DWORD size = data_size >> 2;
394 TRACE("Size %u\n", size);
396 read_dword(&ptr, &r->instruction_count);
397 TRACE("InstructionCount: %u\n", r->instruction_count);
399 read_dword(&ptr, &r->temp_register_count);
400 TRACE("TempRegisterCount: %u\n", r->temp_register_count);
402 skip_dword_unknown(&ptr, 1);
404 read_dword(&ptr, &r->dcl_count);
405 TRACE("DclCount: %u\n", r->dcl_count);
407 read_dword(&ptr, &r->float_instruction_count);
408 TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
410 read_dword(&ptr, &r->int_instruction_count);
411 TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
413 read_dword(&ptr, &r->uint_instruction_count);
414 TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
416 read_dword(&ptr, &r->static_flow_control_count);
417 TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
419 read_dword(&ptr, &r->dynamic_flow_control_count);
420 TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
422 skip_dword_unknown(&ptr, 1);
424 read_dword(&ptr, &r->temp_array_count);
425 TRACE("TempArrayCount: %u\n", r->temp_array_count);
427 read_dword(&ptr, &r->array_instruction_count);
428 TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
430 read_dword(&ptr, &r->cut_instruction_count);
431 TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
433 read_dword(&ptr, &r->emit_instruction_count);
434 TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
436 read_dword(&ptr, &r->texture_normal_instructions);
437 TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
439 read_dword(&ptr, &r->texture_load_instructions);
440 TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
442 read_dword(&ptr, &r->texture_comp_instructions);
443 TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
445 read_dword(&ptr, &r->texture_bias_instructions);
446 TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
448 read_dword(&ptr, &r->texture_gradient_instructions);
449 TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
451 read_dword(&ptr, &r->mov_instruction_count);
452 TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
454 skip_dword_unknown(&ptr, 1);
456 read_dword(&ptr, &r->conversion_instruction_count);
457 TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
459 skip_dword_unknown(&ptr, 1);
461 read_dword(&ptr, &r->input_primitive);
462 TRACE("InputPrimitive: %x\n", r->input_primitive);
464 read_dword(&ptr, &r->gs_output_topology);
465 TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
467 read_dword(&ptr, &r->gs_max_output_vertex_count);
468 TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
470 skip_dword_unknown(&ptr, 3);
473 if (size == 29) return S_OK;
475 skip_dword_unknown(&ptr, 1);
477 read_dword(&ptr, &r->c_control_points);
478 TRACE("cControlPoints: %u\n", r->c_control_points);
480 read_dword(&ptr, &r->hs_output_primitive);
481 TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
483 read_dword(&ptr, &r->hs_prtitioning);
484 TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
486 read_dword(&ptr, &r->tessellator_domain);
487 TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
489 skip_dword_unknown(&ptr, 3);
492 if (size == 37) return S_OK;
494 FIXME("Unhandled size %u\n", size);
499 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
501 const char *ptr = data;
502 DWORD size = data_size >> 2;
505 TRACE("Size %u\n", size);
507 read_dword(&ptr, &r->constant_buffer_count);
508 TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
510 read_dword(&ptr, &offset);
511 TRACE("Constant buffer offset: %x\n", offset);
513 read_dword(&ptr, &r->bound_resource_count);
514 TRACE("Bound resource count: %u\n", r->bound_resource_count);
516 read_dword(&ptr, &offset);
517 TRACE("Bound resource offset: %x\n", offset);
519 skip_dword_unknown(&ptr, 1);
521 read_dword(&ptr, &r->flags);
522 TRACE("Flags: %u\n", r->flags);
524 read_dword(&ptr, &offset);
525 TRACE("Creator at offset %#x.\n", offset);
527 if (!copy_name(data + offset, &r->creator))
529 ERR("Failed to copy name.\n");
530 return E_OUTOFMEMORY;
532 TRACE("Creator: %s.\n", debugstr_a(r->creator));
534 /* todo: Parse D3D11_SHADER_INPUT_BIND_DESC Structure */
536 /* todo: Parse Constant buffers */
541 HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, const char *data, DWORD data_size)
543 D3D11_SIGNATURE_PARAMETER_DESC *d;
544 unsigned int string_data_offset;
545 unsigned int string_data_size;
546 const char *ptr = data;
551 read_dword(&ptr, &count);
552 TRACE("%u elements\n", count);
554 skip_dword_unknown(&ptr, 1);
556 d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
559 ERR("Failed to allocate signature memory.\n");
560 return E_OUTOFMEMORY;
563 /* 2 DWORDs for the header, 6 for each element. */
564 string_data_offset = 2 * sizeof(DWORD) + count * 6 * sizeof(DWORD);
565 string_data_size = data_size - string_data_offset;
566 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
569 ERR("Failed to allocate string data memory.\n");
570 HeapFree(GetProcessHeap(), 0, d);
571 return E_OUTOFMEMORY;
573 memcpy(string_data, data + string_data_offset, string_data_size);
575 for (i = 0; i < count; ++i)
580 /* todo: Parse stream in shaderblobs v5 (dx11) */
583 read_dword(&ptr, &name_offset);
584 d[i].SemanticName = string_data + (name_offset - string_data_offset);
585 read_dword(&ptr, &d[i].SemanticIndex);
586 read_dword(&ptr, &d[i].SystemValueType);
587 read_dword(&ptr, &d[i].ComponentType);
588 read_dword(&ptr, &d[i].Register);
589 read_dword(&ptr, &mask);
590 d[i].ReadWriteMask = (mask >> 8) & 0xff;
591 d[i].Mask = mask & 0xff;
593 TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
594 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
595 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
596 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
600 s->element_count = count;
601 s->string_data = string_data;
606 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
608 const char *ptr = data;
610 read_dword(&ptr, &r->version);
611 TRACE("Shader version: %u\n", r->version);
613 /* todo: Check if anything else is needed from the shdr or shex blob. */
618 HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
619 const void *data, SIZE_T data_size)
621 struct dxbc src_dxbc;
625 reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
626 reflection->refcount = 1;
628 hr = dxbc_parse(data, data_size, &src_dxbc);
631 WARN("Failed to parse reflection\n");
635 for (i = 0; i < src_dxbc.count; ++i)
637 struct dxbc_section *section = &src_dxbc.sections[i];
639 switch (section->tag)
642 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
645 WARN("Failed to parse section STAT.\n");
652 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
655 WARN("Failed to parse SHDR section.\n");
661 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
664 WARN("Failed to parse RDEF section.\n");
670 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
671 if (!reflection->isgn)
673 ERR("Failed to allocate ISGN memory.\n");
678 hr = d3dcompiler_parse_signature(reflection->isgn, section->data, section->data_size);
681 WARN("Failed to parse section ISGN.\n");
687 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
688 if (!reflection->osgn)
690 ERR("Failed to allocate OSGN memory.\n");
695 hr = d3dcompiler_parse_signature(reflection->osgn, section->data, section->data_size);
698 WARN("Failed to parse section OSGN.\n");
704 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
705 if (!reflection->pcsg)
707 ERR("Failed to allocate PCSG memory.\n");
712 hr = d3dcompiler_parse_signature(reflection->pcsg, section->data, section->data_size);
715 WARN("Failed to parse section PCSG.\n");
721 FIXME("Unhandled section %s!\n", debugstr_an((const char *)§ion->tag, 4));
726 dxbc_destroy(&src_dxbc);
731 reflection_cleanup(reflection);
732 dxbc_destroy(&src_dxbc);