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 void free_signature(struct d3dcompiler_shader_signature *sig)
30 TRACE("Free signature %p\n", sig);
32 HeapFree(GetProcessHeap(), 0, sig->elements);
33 HeapFree(GetProcessHeap(), 0, sig->string_data);
36 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
38 TRACE("Cleanup %p\n", ref);
42 free_signature(ref->isgn);
43 HeapFree(GetProcessHeap(), 0, ref->isgn);
47 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
49 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
52 /* IUnknown methods */
54 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
56 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
58 if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
59 || IsEqualGUID(riid, &IID_IUnknown))
61 IUnknown_AddRef(iface);
66 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
72 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
74 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
75 ULONG refcount = InterlockedIncrement(&This->refcount);
77 TRACE("%p increasing refcount to %u\n", This, refcount);
82 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
84 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
85 ULONG refcount = InterlockedDecrement(&This->refcount);
87 TRACE("%p decreasing refcount to %u\n", This, refcount);
91 reflection_cleanup(This);
92 HeapFree(GetProcessHeap(), 0, This);
98 /* ID3D11ShaderReflection methods */
100 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
102 FIXME("iface %p, desc %p stub!\n", iface, desc);
107 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
108 ID3D11ShaderReflection *iface, UINT index)
110 FIXME("iface %p, index %u stub!\n", iface, index);
115 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
116 ID3D11ShaderReflection *iface, LPCSTR name)
118 FIXME("iface %p, name \"%s\" stub!\n", iface, name);
123 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
124 ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_DESC *desc)
126 FIXME("iface %p, index %u, desc %p stub!\n", iface, index, desc);
131 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
132 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
134 FIXME("iface %p, index %u, desc %p stub!\n", iface, index, desc);
139 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
140 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
142 FIXME("iface %p, index %u, desc %p stub!\n", iface, index, desc);
147 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
148 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
150 FIXME("iface %p, index %u, desc %p stub!\n", iface, index, desc);
155 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
156 ID3D11ShaderReflection *iface, LPCSTR name)
158 FIXME("iface %p, name %s stub!\n", iface, name);
163 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
164 ID3D11ShaderReflection *iface, LPCSTR name, D3D11_SHADER_INPUT_BIND_DESC *desc)
166 FIXME("iface %p, name %s, desc %p stub!\n", iface, name, desc);
171 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
172 ID3D11ShaderReflection *iface)
174 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
176 TRACE("iface %p\n", iface);
178 return This->mov_instruction_count;
181 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
182 ID3D11ShaderReflection *iface)
184 FIXME("iface %p stub!\n", iface);
189 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
190 ID3D11ShaderReflection *iface)
192 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
194 TRACE("iface %p\n", iface);
196 return This->conversion_instruction_count;
199 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
200 ID3D11ShaderReflection *iface)
202 FIXME("iface %p stub!\n", iface);
207 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
208 ID3D11ShaderReflection *iface)
210 FIXME("iface %p stub!\n", iface);
215 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
216 ID3D11ShaderReflection *iface)
218 FIXME("iface %p stub!\n", iface);
223 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
224 ID3D11ShaderReflection *iface)
226 FIXME("iface %p stub!\n", iface);
231 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
232 ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
234 FIXME("iface %p, level %p stub!\n", iface, level);
239 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
240 ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
242 FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
247 const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
249 /* IUnknown methods */
250 d3dcompiler_shader_reflection_QueryInterface,
251 d3dcompiler_shader_reflection_AddRef,
252 d3dcompiler_shader_reflection_Release,
253 /* ID3D11ShaderReflection methods */
254 d3dcompiler_shader_reflection_GetDesc,
255 d3dcompiler_shader_reflection_GetConstantBufferByIndex,
256 d3dcompiler_shader_reflection_GetConstantBufferByName,
257 d3dcompiler_shader_reflection_GetResourceBindingDesc,
258 d3dcompiler_shader_reflection_GetInputParameterDesc,
259 d3dcompiler_shader_reflection_GetOutputParameterDesc,
260 d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
261 d3dcompiler_shader_reflection_GetVariableByName,
262 d3dcompiler_shader_reflection_GetResourceBindingDescByName,
263 d3dcompiler_shader_reflection_GetMovInstructionCount,
264 d3dcompiler_shader_reflection_GetMovcInstructionCount,
265 d3dcompiler_shader_reflection_GetConversionInstructionCount,
266 d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
267 d3dcompiler_shader_reflection_GetGSInputPrimitive,
268 d3dcompiler_shader_reflection_IsSampleFrequencyShader,
269 d3dcompiler_shader_reflection_GetNumInterfaceSlots,
270 d3dcompiler_shader_reflection_GetMinFeatureLevel,
271 d3dcompiler_shader_reflection_GetThreadGroupSize,
274 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
276 const char *ptr = data;
277 DWORD size = data_size >> 2;
279 TRACE("Size %u\n", size);
281 read_dword(&ptr, &r->instruction_count);
282 TRACE("InstructionCount: %u\n", r->instruction_count);
284 read_dword(&ptr, &r->temp_register_count);
285 TRACE("TempRegisterCount: %u\n", r->temp_register_count);
287 skip_dword_unknown(&ptr, 1);
289 read_dword(&ptr, &r->dcl_count);
290 TRACE("DclCount: %u\n", r->dcl_count);
292 read_dword(&ptr, &r->float_instruction_count);
293 TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
295 read_dword(&ptr, &r->int_instruction_count);
296 TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
298 read_dword(&ptr, &r->uint_instruction_count);
299 TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
301 read_dword(&ptr, &r->static_flow_control_count);
302 TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
304 read_dword(&ptr, &r->dynamic_flow_control_count);
305 TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
307 skip_dword_unknown(&ptr, 1);
309 read_dword(&ptr, &r->temp_array_count);
310 TRACE("TempArrayCount: %u\n", r->temp_array_count);
312 read_dword(&ptr, &r->array_instruction_count);
313 TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
315 read_dword(&ptr, &r->cut_instruction_count);
316 TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
318 read_dword(&ptr, &r->emit_instruction_count);
319 TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
321 read_dword(&ptr, &r->texture_normal_instructions);
322 TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
324 read_dword(&ptr, &r->texture_load_instructions);
325 TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
327 read_dword(&ptr, &r->texture_comp_instructions);
328 TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
330 read_dword(&ptr, &r->texture_bias_instructions);
331 TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
333 read_dword(&ptr, &r->texture_gradient_instructions);
334 TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
336 read_dword(&ptr, &r->mov_instruction_count);
337 TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
339 skip_dword_unknown(&ptr, 1);
341 read_dword(&ptr, &r->conversion_instruction_count);
342 TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
344 skip_dword_unknown(&ptr, 1);
346 read_dword(&ptr, &r->input_primitive);
347 TRACE("InputPrimitive: %x\n", r->input_primitive);
349 read_dword(&ptr, &r->gs_output_topology);
350 TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
352 read_dword(&ptr, &r->gs_max_output_vertex_count);
353 TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
355 skip_dword_unknown(&ptr, 3);
358 if (size == 29) return S_OK;
360 skip_dword_unknown(&ptr, 1);
362 read_dword(&ptr, &r->c_control_points);
363 TRACE("cControlPoints: %u\n", r->c_control_points);
365 read_dword(&ptr, &r->hs_output_primitive);
366 TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
368 read_dword(&ptr, &r->hs_prtitioning);
369 TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
371 read_dword(&ptr, &r->tessellator_domain);
372 TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
374 skip_dword_unknown(&ptr, 3);
377 if (size == 37) return S_OK;
379 FIXME("Unhandled size %u\n", size);
384 HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, const char *data, DWORD data_size)
386 D3D11_SIGNATURE_PARAMETER_DESC *d;
387 unsigned int string_data_offset;
388 unsigned int string_data_size;
389 const char *ptr = data;
394 read_dword(&ptr, &count);
395 TRACE("%u elements\n", count);
397 skip_dword_unknown(&ptr, 1);
399 d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
402 ERR("Failed to allocate signature memory.\n");
403 return E_OUTOFMEMORY;
406 /* 2 DWORDs for the header, 6 for each element. */
407 string_data_offset = 2 * sizeof(DWORD) + count * 6 * sizeof(DWORD);
408 string_data_size = data_size - string_data_offset;
409 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
412 ERR("Failed to allocate string data memory.\n");
413 HeapFree(GetProcessHeap(), 0, d);
414 return E_OUTOFMEMORY;
416 memcpy(string_data, data + string_data_offset, string_data_size);
418 for (i = 0; i < count; ++i)
423 read_dword(&ptr, &name_offset);
424 d[i].SemanticName = string_data + (name_offset - string_data_offset);
425 read_dword(&ptr, &d[i].SemanticIndex);
426 read_dword(&ptr, &d[i].SystemValueType);
427 read_dword(&ptr, &d[i].ComponentType);
428 read_dword(&ptr, &d[i].Register);
429 read_dword(&ptr, &mask);
430 d[i].ReadWriteMask = (mask >> 8) & 0xff;
431 d[i].Mask = mask & 0xff;
433 TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
434 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
435 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
436 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
440 s->element_count = count;
441 s->string_data = string_data;
446 HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
447 const void *data, SIZE_T data_size)
449 struct dxbc src_dxbc;
453 reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
454 reflection->refcount = 1;
456 hr = dxbc_parse(data, data_size, &src_dxbc);
459 WARN("Failed to parse reflection\n");
463 for (i = 0; i < src_dxbc.count; ++i)
465 struct dxbc_section *section = &src_dxbc.sections[i];
467 switch (section->tag)
470 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
473 WARN("Failed to parse section STAT.\n");
479 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
480 if (!reflection->isgn)
482 ERR("Failed to allocate ISGN memory.\n");
487 hr = d3dcompiler_parse_signature(reflection->isgn, section->data, section->data_size);
490 WARN("Failed to parse section ISGN.\n");
496 FIXME("Unhandled section %s!\n", debugstr_an((const char *)§ion->tag, 4));
501 dxbc_destroy(&src_dxbc);
506 reflection_cleanup(reflection);
507 dxbc_destroy(&src_dxbc);