* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / directx / wine / d3dcompiler_43 / reflection.c
1 /*
2 * Copyright 2009 Henri Verbeet for CodeWeavers
3 * Copyright 2010 Rico Schüller
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 */
20
21 #include "d3dcompiler_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
24
25 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE
26 {
27 D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6 = 6,
28 D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7 = 7,
29 };
30
31 #define D3DCOMPILER_SHADER_TARGET_VERSION_MASK 0xffff
32 #define D3DCOMPILER_SHADER_TARGET_SHADERTYPE_MASK 0xffff0000
33
34 struct d3dcompiler_shader_signature
35 {
36 D3D11_SIGNATURE_PARAMETER_DESC *elements;
37 UINT element_count;
38 char *string_data;
39 };
40
41 struct d3dcompiler_shader_reflection_type
42 {
43 ID3D11ShaderReflectionType ID3D11ShaderReflectionType_iface;
44
45 DWORD id;
46 struct wine_rb_entry entry;
47
48 struct d3dcompiler_shader_reflection *reflection;
49
50 D3D11_SHADER_TYPE_DESC desc;
51 struct d3dcompiler_shader_reflection_type_member *members;
52 };
53
54 struct d3dcompiler_shader_reflection_type_member
55 {
56 char *name;
57 DWORD offset;
58 struct d3dcompiler_shader_reflection_type *type;
59 };
60
61 struct d3dcompiler_shader_reflection_variable
62 {
63 ID3D11ShaderReflectionVariable ID3D11ShaderReflectionVariable_iface;
64
65 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffer;
66 struct d3dcompiler_shader_reflection_type *type;
67
68 char *name;
69 UINT start_offset;
70 UINT size;
71 UINT flags;
72 void *default_value;
73 };
74
75 struct d3dcompiler_shader_reflection_constant_buffer
76 {
77 ID3D11ShaderReflectionConstantBuffer ID3D11ShaderReflectionConstantBuffer_iface;
78
79 struct d3dcompiler_shader_reflection *reflection;
80
81 char *name;
82 D3D_CBUFFER_TYPE type;
83 UINT variable_count;
84 UINT size;
85 UINT flags;
86
87 struct d3dcompiler_shader_reflection_variable *variables;
88 };
89
90 /* ID3D11ShaderReflection */
91 struct d3dcompiler_shader_reflection
92 {
93 ID3D11ShaderReflection ID3D11ShaderReflection_iface;
94 LONG refcount;
95
96 DWORD target;
97 char *creator;
98 UINT flags;
99 UINT version;
100 UINT bound_resource_count;
101 UINT constant_buffer_count;
102
103 UINT mov_instruction_count;
104 UINT conversion_instruction_count;
105 UINT instruction_count;
106 UINT emit_instruction_count;
107 D3D_PRIMITIVE_TOPOLOGY gs_output_topology;
108 UINT gs_max_output_vertex_count;
109 D3D_PRIMITIVE input_primitive;
110 UINT cut_instruction_count;
111 UINT dcl_count;
112 UINT static_flow_control_count;
113 UINT float_instruction_count;
114 UINT temp_register_count;
115 UINT int_instruction_count;
116 UINT uint_instruction_count;
117 UINT temp_array_count;
118 UINT array_instruction_count;
119 UINT texture_normal_instructions;
120 UINT texture_load_instructions;
121 UINT texture_comp_instructions;
122 UINT texture_bias_instructions;
123 UINT texture_gradient_instructions;
124 UINT dynamic_flow_control_count;
125 UINT c_control_points;
126 D3D_TESSELLATOR_OUTPUT_PRIMITIVE hs_output_primitive;
127 D3D_TESSELLATOR_PARTITIONING hs_prtitioning;
128 D3D_TESSELLATOR_DOMAIN tessellator_domain;
129
130 struct d3dcompiler_shader_signature *isgn;
131 struct d3dcompiler_shader_signature *osgn;
132 struct d3dcompiler_shader_signature *pcsg;
133 char *resource_string;
134 D3D11_SHADER_INPUT_BIND_DESC *bound_resources;
135 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers;
136 struct wine_rb_tree types;
137 };
138
139 static struct d3dcompiler_shader_reflection_type *get_reflection_type(struct d3dcompiler_shader_reflection *reflection, const char *data, DWORD offset);
140
141 static const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl;
142 static const struct ID3D11ShaderReflectionVariableVtbl d3dcompiler_shader_reflection_variable_vtbl;
143 static const struct ID3D11ShaderReflectionTypeVtbl d3dcompiler_shader_reflection_type_vtbl;
144
145 /* null objects - needed for invalid calls */
146 static struct d3dcompiler_shader_reflection_constant_buffer null_constant_buffer = {{&d3dcompiler_shader_reflection_constant_buffer_vtbl}};
147 static struct d3dcompiler_shader_reflection_type null_type = {{&d3dcompiler_shader_reflection_type_vtbl}};
148 static struct d3dcompiler_shader_reflection_variable null_variable = {{&d3dcompiler_shader_reflection_variable_vtbl},
149 &null_constant_buffer, &null_type};
150
151 static BOOL copy_name(const char *ptr, char **name)
152 {
153 size_t name_len;
154
155 if (!ptr) return TRUE;
156
157 name_len = strlen(ptr) + 1;
158 if (name_len == 1)
159 {
160 return TRUE;
161 }
162
163 *name = HeapAlloc(GetProcessHeap(), 0, name_len);
164 if (!*name)
165 {
166 ERR("Failed to allocate name memory.\n");
167 return FALSE;
168 }
169
170 memcpy(*name, ptr, name_len);
171
172 return TRUE;
173 }
174
175 static BOOL copy_value(const char *ptr, void **value, DWORD size)
176 {
177 if (!ptr || !size) return TRUE;
178
179 *value = HeapAlloc(GetProcessHeap(), 0, size);
180 if (!*value)
181 {
182 ERR("Failed to allocate value memory.\n");
183 return FALSE;
184 }
185
186 memcpy(*value, ptr, size);
187
188 return TRUE;
189 }
190
191 static void *d3dcompiler_rb_alloc(size_t size)
192 {
193 return HeapAlloc(GetProcessHeap(), 0, size);
194 }
195
196 static void *d3dcompiler_rb_realloc(void *ptr, size_t size)
197 {
198 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
199 }
200
201 static void d3dcompiler_rb_free(void *ptr)
202 {
203 HeapFree(GetProcessHeap(), 0, ptr);
204 }
205
206 static int d3dcompiler_shader_reflection_type_compare(const void *key, const struct wine_rb_entry *entry)
207 {
208 const struct d3dcompiler_shader_reflection_type *t = WINE_RB_ENTRY_VALUE(entry, const struct d3dcompiler_shader_reflection_type, entry);
209 const DWORD *id = key;
210
211 return *id - t->id;
212 }
213
214 static void free_type_member(struct d3dcompiler_shader_reflection_type_member *member)
215 {
216 if (member)
217 {
218 HeapFree(GetProcessHeap(), 0, member->name);
219 }
220 }
221
222 static void d3dcompiler_shader_reflection_type_destroy(struct wine_rb_entry *entry, void *context)
223 {
224 struct d3dcompiler_shader_reflection_type *t = WINE_RB_ENTRY_VALUE(entry, struct d3dcompiler_shader_reflection_type, entry);
225 unsigned int i;
226
227 TRACE("reflection type %p.\n", t);
228
229 if (t->members)
230 {
231 for (i = 0; i < t->desc.Members; ++i)
232 {
233 free_type_member(&t->members[i]);
234 }
235 HeapFree(GetProcessHeap(), 0, t->members);
236 }
237
238 HeapFree(GetProcessHeap(), 0, t);
239 }
240
241 static const struct wine_rb_functions d3dcompiler_shader_reflection_type_rb_functions =
242 {
243 d3dcompiler_rb_alloc,
244 d3dcompiler_rb_realloc,
245 d3dcompiler_rb_free,
246 d3dcompiler_shader_reflection_type_compare,
247 };
248
249 static void free_signature(struct d3dcompiler_shader_signature *sig)
250 {
251 TRACE("Free signature %p\n", sig);
252
253 HeapFree(GetProcessHeap(), 0, sig->elements);
254 HeapFree(GetProcessHeap(), 0, sig->string_data);
255 }
256
257 static void free_variable(struct d3dcompiler_shader_reflection_variable *var)
258 {
259 if (var)
260 {
261 HeapFree(GetProcessHeap(), 0, var->name);
262 HeapFree(GetProcessHeap(), 0, var->default_value);
263 }
264 }
265
266 static void free_constant_buffer(struct d3dcompiler_shader_reflection_constant_buffer *cb)
267 {
268 if (cb->variables)
269 {
270 unsigned int i;
271
272 for (i = 0; i < cb->variable_count; ++i)
273 {
274 free_variable(&cb->variables[i]);
275 }
276 HeapFree(GetProcessHeap(), 0, cb->variables);
277 }
278
279 HeapFree(GetProcessHeap(), 0, cb->name);
280 }
281
282 static void reflection_cleanup(struct d3dcompiler_shader_reflection *ref)
283 {
284 TRACE("Cleanup %p\n", ref);
285
286 if (ref->isgn)
287 {
288 free_signature(ref->isgn);
289 HeapFree(GetProcessHeap(), 0, ref->isgn);
290 }
291
292 if (ref->osgn)
293 {
294 free_signature(ref->osgn);
295 HeapFree(GetProcessHeap(), 0, ref->osgn);
296 }
297
298 if (ref->pcsg)
299 {
300 free_signature(ref->pcsg);
301 HeapFree(GetProcessHeap(), 0, ref->pcsg);
302 }
303
304 if (ref->constant_buffers)
305 {
306 unsigned int i;
307
308 for (i = 0; i < ref->constant_buffer_count; ++i)
309 {
310 free_constant_buffer(&ref->constant_buffers[i]);
311 }
312 }
313
314 wine_rb_destroy(&ref->types, d3dcompiler_shader_reflection_type_destroy, NULL);
315 HeapFree(GetProcessHeap(), 0, ref->constant_buffers);
316 HeapFree(GetProcessHeap(), 0, ref->bound_resources);
317 HeapFree(GetProcessHeap(), 0, ref->resource_string);
318 HeapFree(GetProcessHeap(), 0, ref->creator);
319 }
320
321 /* IUnknown methods */
322
323 static inline struct d3dcompiler_shader_reflection *impl_from_ID3D11ShaderReflection(ID3D11ShaderReflection *iface)
324 {
325 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection, ID3D11ShaderReflection_iface);
326 }
327
328 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_QueryInterface(ID3D11ShaderReflection *iface, REFIID riid, void **object)
329 {
330 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
331
332 if (IsEqualGUID(riid, &IID_ID3D11ShaderReflection)
333 || IsEqualGUID(riid, &IID_IUnknown))
334 {
335 IUnknown_AddRef(iface);
336 *object = iface;
337 return S_OK;
338 }
339
340 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
341
342 *object = NULL;
343 return E_NOINTERFACE;
344 }
345
346 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_AddRef(ID3D11ShaderReflection *iface)
347 {
348 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
349 ULONG refcount = InterlockedIncrement(&This->refcount);
350
351 TRACE("%p increasing refcount to %u\n", This, refcount);
352
353 return refcount;
354 }
355
356 static ULONG STDMETHODCALLTYPE d3dcompiler_shader_reflection_Release(ID3D11ShaderReflection *iface)
357 {
358 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
359 ULONG refcount = InterlockedDecrement(&This->refcount);
360
361 TRACE("%p decreasing refcount to %u\n", This, refcount);
362
363 if (!refcount)
364 {
365 reflection_cleanup(This);
366 HeapFree(GetProcessHeap(), 0, This);
367 }
368
369 return refcount;
370 }
371
372 /* ID3D11ShaderReflection methods */
373
374 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetDesc(ID3D11ShaderReflection *iface, D3D11_SHADER_DESC *desc)
375 {
376 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
377
378 FIXME("iface %p, desc %p partial stub!\n", iface, desc);
379
380 if (!desc)
381 {
382 WARN("Invalid argument specified\n");
383 return E_FAIL;
384 }
385
386 desc->Version = This->version;
387 desc->Creator = This->creator;
388 desc->Flags = This->flags;
389 desc->ConstantBuffers = This->constant_buffer_count;
390 desc->BoundResources = This->bound_resource_count;
391 desc->InputParameters = This->isgn ? This->isgn->element_count : 0;
392 desc->OutputParameters = This->osgn ? This->osgn->element_count : 0;
393 desc->InstructionCount = This->instruction_count;
394 desc->TempRegisterCount = This->temp_register_count;
395 desc->TempArrayCount = This->temp_array_count;
396 desc->DefCount = 0;
397 desc->DclCount = This->dcl_count;
398 desc->TextureNormalInstructions = This->texture_normal_instructions;
399 desc->TextureLoadInstructions = This->texture_load_instructions;
400 desc->TextureCompInstructions = This->texture_comp_instructions;
401 desc->TextureBiasInstructions = This->texture_bias_instructions;
402 desc->TextureGradientInstructions = This->texture_gradient_instructions;
403 desc->FloatInstructionCount = This->float_instruction_count;
404 desc->IntInstructionCount = This->int_instruction_count;
405 desc->UintInstructionCount = This->uint_instruction_count;
406 desc->StaticFlowControlCount = This->static_flow_control_count;
407 desc->DynamicFlowControlCount = This->dynamic_flow_control_count;
408 desc->MacroInstructionCount = 0;
409 desc->ArrayInstructionCount = This->array_instruction_count;
410 desc->CutInstructionCount = This->cut_instruction_count;
411 desc->EmitInstructionCount = This->emit_instruction_count;
412 desc->GSOutputTopology = This->gs_output_topology;
413 desc->GSMaxOutputVertexCount = This->gs_max_output_vertex_count;
414 desc->InputPrimitive = This->input_primitive;
415 desc->PatchConstantParameters = This->pcsg ? This->pcsg->element_count : 0;
416 desc->cGSInstanceCount = 0;
417 desc->cControlPoints = This->c_control_points;
418 desc->HSOutputPrimitive = This->hs_output_primitive;
419 desc->HSPartitioning = This->hs_prtitioning;
420 desc->TessellatorDomain = This->tessellator_domain;
421 desc->cBarrierInstructions = 0;
422 desc->cInterlockedInstructions = 0;
423 desc->cTextureStoreInstructions = 0;
424
425 return S_OK;
426 }
427
428 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByIndex(
429 ID3D11ShaderReflection *iface, UINT index)
430 {
431 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
432
433 TRACE("iface %p, index %u\n", iface, index);
434
435 if (index >= This->constant_buffer_count)
436 {
437 WARN("Invalid argument specified\n");
438 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
439 }
440
441 return &This->constant_buffers[index].ID3D11ShaderReflectionConstantBuffer_iface;
442 }
443
444 static struct ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConstantBufferByName(
445 ID3D11ShaderReflection *iface, const char *name)
446 {
447 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
448 unsigned int i;
449
450 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
451
452 if (!name)
453 {
454 WARN("Invalid argument specified\n");
455 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
456 }
457
458 for (i = 0; i < This->constant_buffer_count; ++i)
459 {
460 struct d3dcompiler_shader_reflection_constant_buffer *d = &This->constant_buffers[i];
461
462 if (!strcmp(d->name, name))
463 {
464 TRACE("Returning ID3D11ShaderReflectionConstantBuffer %p.\n", d);
465 return &d->ID3D11ShaderReflectionConstantBuffer_iface;
466 }
467 }
468
469 WARN("Invalid name specified\n");
470
471 return &null_constant_buffer.ID3D11ShaderReflectionConstantBuffer_iface;
472 }
473
474 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDesc(
475 ID3D11ShaderReflection *iface, UINT index, D3D11_SHADER_INPUT_BIND_DESC *desc)
476 {
477 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
478
479 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
480
481 if (!desc || index >= This->bound_resource_count)
482 {
483 WARN("Invalid argument specified\n");
484 return E_INVALIDARG;
485 }
486
487 *desc = This->bound_resources[index];
488
489 return S_OK;
490 }
491
492 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetInputParameterDesc(
493 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
494 {
495 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
496
497 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
498
499 if (!desc || !This->isgn || index >= This->isgn->element_count)
500 {
501 WARN("Invalid argument specified\n");
502 return E_INVALIDARG;
503 }
504
505 *desc = This->isgn->elements[index];
506
507 return S_OK;
508 }
509
510 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetOutputParameterDesc(
511 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
512 {
513 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
514
515 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
516
517 if (!desc || !This->osgn || index >= This->osgn->element_count)
518 {
519 WARN("Invalid argument specified\n");
520 return E_INVALIDARG;
521 }
522
523 *desc = This->osgn->elements[index];
524
525 return S_OK;
526 }
527
528 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetPatchConstantParameterDesc(
529 ID3D11ShaderReflection *iface, UINT index, D3D11_SIGNATURE_PARAMETER_DESC *desc)
530 {
531 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
532
533 TRACE("iface %p, index %u, desc %p\n", iface, index, desc);
534
535 if (!desc || !This->pcsg || index >= This->pcsg->element_count)
536 {
537 WARN("Invalid argument specified\n");
538 return E_INVALIDARG;
539 }
540
541 *desc = This->pcsg->elements[index];
542
543 return S_OK;
544 }
545
546 static struct ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetVariableByName(
547 ID3D11ShaderReflection *iface, const char *name)
548 {
549 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
550 unsigned int i, k;
551
552 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
553
554 if (!name)
555 {
556 WARN("Invalid name specified\n");
557 return &null_variable.ID3D11ShaderReflectionVariable_iface;
558 }
559
560 for (i = 0; i < This->constant_buffer_count; ++i)
561 {
562 struct d3dcompiler_shader_reflection_constant_buffer *cb = &This->constant_buffers[i];
563
564 for (k = 0; k < cb->variable_count; ++k)
565 {
566 struct d3dcompiler_shader_reflection_variable *v = &cb->variables[k];
567
568 if (!strcmp(v->name, name))
569 {
570 TRACE("Returning ID3D11ShaderReflectionVariable %p.\n", v);
571 return &v->ID3D11ShaderReflectionVariable_iface;
572 }
573 }
574 }
575
576 WARN("Invalid name specified\n");
577
578 return &null_variable.ID3D11ShaderReflectionVariable_iface;
579 }
580
581 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetResourceBindingDescByName(
582 ID3D11ShaderReflection *iface, const char *name, D3D11_SHADER_INPUT_BIND_DESC *desc)
583 {
584 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
585 unsigned int i;
586
587 TRACE("iface %p, name %s, desc %p\n", iface, debugstr_a(name), desc);
588
589 if (!desc || !name)
590 {
591 WARN("Invalid argument specified\n");
592 return E_INVALIDARG;
593 }
594
595 for (i = 0; i < This->bound_resource_count; ++i)
596 {
597 D3D11_SHADER_INPUT_BIND_DESC *d = &This->bound_resources[i];
598
599 if (!strcmp(d->Name, name))
600 {
601 TRACE("Returning D3D11_SHADER_INPUT_BIND_DESC %p.\n", d);
602 *desc = *d;
603 return S_OK;
604 }
605 }
606
607 WARN("Invalid name specified\n");
608
609 return E_INVALIDARG;
610 }
611
612 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovInstructionCount(
613 ID3D11ShaderReflection *iface)
614 {
615 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
616
617 TRACE("iface %p\n", iface);
618
619 return This->mov_instruction_count;
620 }
621
622 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMovcInstructionCount(
623 ID3D11ShaderReflection *iface)
624 {
625 FIXME("iface %p stub!\n", iface);
626
627 return 0;
628 }
629
630 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetConversionInstructionCount(
631 ID3D11ShaderReflection *iface)
632 {
633 struct d3dcompiler_shader_reflection *This = impl_from_ID3D11ShaderReflection(iface);
634
635 TRACE("iface %p\n", iface);
636
637 return This->conversion_instruction_count;
638 }
639
640 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetBitwiseInstructionCount(
641 ID3D11ShaderReflection *iface)
642 {
643 FIXME("iface %p stub!\n", iface);
644
645 return 0;
646 }
647
648 static D3D_PRIMITIVE STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetGSInputPrimitive(
649 ID3D11ShaderReflection *iface)
650 {
651 FIXME("iface %p stub!\n", iface);
652
653 return 0;
654 }
655
656 static BOOL STDMETHODCALLTYPE d3dcompiler_shader_reflection_IsSampleFrequencyShader(
657 ID3D11ShaderReflection *iface)
658 {
659 FIXME("iface %p stub!\n", iface);
660
661 return FALSE;
662 }
663
664 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetNumInterfaceSlots(
665 ID3D11ShaderReflection *iface)
666 {
667 FIXME("iface %p stub!\n", iface);
668
669 return 0;
670 }
671
672 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetMinFeatureLevel(
673 ID3D11ShaderReflection *iface, D3D_FEATURE_LEVEL *level)
674 {
675 FIXME("iface %p, level %p stub!\n", iface, level);
676
677 return E_NOTIMPL;
678 }
679
680 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_GetThreadGroupSize(
681 ID3D11ShaderReflection *iface, UINT *sizex, UINT *sizey, UINT *sizez)
682 {
683 FIXME("iface %p, sizex %p, sizey %p, sizez %p stub!\n", iface, sizex, sizey, sizez);
684
685 return 0;
686 }
687
688 static const struct ID3D11ShaderReflectionVtbl d3dcompiler_shader_reflection_vtbl =
689 {
690 /* IUnknown methods */
691 d3dcompiler_shader_reflection_QueryInterface,
692 d3dcompiler_shader_reflection_AddRef,
693 d3dcompiler_shader_reflection_Release,
694 /* ID3D11ShaderReflection methods */
695 d3dcompiler_shader_reflection_GetDesc,
696 d3dcompiler_shader_reflection_GetConstantBufferByIndex,
697 d3dcompiler_shader_reflection_GetConstantBufferByName,
698 d3dcompiler_shader_reflection_GetResourceBindingDesc,
699 d3dcompiler_shader_reflection_GetInputParameterDesc,
700 d3dcompiler_shader_reflection_GetOutputParameterDesc,
701 d3dcompiler_shader_reflection_GetPatchConstantParameterDesc,
702 d3dcompiler_shader_reflection_GetVariableByName,
703 d3dcompiler_shader_reflection_GetResourceBindingDescByName,
704 d3dcompiler_shader_reflection_GetMovInstructionCount,
705 d3dcompiler_shader_reflection_GetMovcInstructionCount,
706 d3dcompiler_shader_reflection_GetConversionInstructionCount,
707 d3dcompiler_shader_reflection_GetBitwiseInstructionCount,
708 d3dcompiler_shader_reflection_GetGSInputPrimitive,
709 d3dcompiler_shader_reflection_IsSampleFrequencyShader,
710 d3dcompiler_shader_reflection_GetNumInterfaceSlots,
711 d3dcompiler_shader_reflection_GetMinFeatureLevel,
712 d3dcompiler_shader_reflection_GetThreadGroupSize,
713 };
714
715 /* ID3D11ShaderReflectionConstantBuffer methods */
716
717 static inline struct d3dcompiler_shader_reflection_constant_buffer *impl_from_ID3D11ShaderReflectionConstantBuffer(ID3D11ShaderReflectionConstantBuffer *iface)
718 {
719 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_constant_buffer, ID3D11ShaderReflectionConstantBuffer_iface);
720 }
721
722 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetDesc(
723 ID3D11ShaderReflectionConstantBuffer *iface, D3D11_SHADER_BUFFER_DESC *desc)
724 {
725 struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
726
727 TRACE("iface %p, desc %p\n", iface, desc);
728
729 if (This == &null_constant_buffer)
730 {
731 WARN("Null constant buffer specified\n");
732 return E_FAIL;
733 }
734
735 if (!desc)
736 {
737 WARN("Invalid argument specified\n");
738 return E_FAIL;
739 }
740
741 desc->Name = This->name;
742 desc->Type = This->type;
743 desc->Variables = This->variable_count;
744 desc->Size = This->size;
745 desc->uFlags = This->flags;
746
747 return S_OK;
748 }
749
750 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex(
751 ID3D11ShaderReflectionConstantBuffer *iface, UINT index)
752 {
753 struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
754
755 TRACE("iface %p, index %u\n", iface, index);
756
757 if (index >= This->variable_count)
758 {
759 WARN("Invalid index specified\n");
760 return &null_variable.ID3D11ShaderReflectionVariable_iface;
761 }
762
763 return &This->variables[index].ID3D11ShaderReflectionVariable_iface;
764 }
765
766 static ID3D11ShaderReflectionVariable * STDMETHODCALLTYPE d3dcompiler_shader_reflection_constant_buffer_GetVariableByName(
767 ID3D11ShaderReflectionConstantBuffer *iface, const char *name)
768 {
769 struct d3dcompiler_shader_reflection_constant_buffer *This = impl_from_ID3D11ShaderReflectionConstantBuffer(iface);
770 unsigned int i;
771
772 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
773
774 if (!name)
775 {
776 WARN("Invalid argument specified\n");
777 return &null_variable.ID3D11ShaderReflectionVariable_iface;
778 }
779
780 for (i = 0; i < This->variable_count; ++i)
781 {
782 struct d3dcompiler_shader_reflection_variable *v = &This->variables[i];
783
784 if (!strcmp(v->name, name))
785 {
786 TRACE("Returning ID3D11ShaderReflectionVariable %p.\n", v);
787 return &v->ID3D11ShaderReflectionVariable_iface;
788 }
789 }
790
791 WARN("Invalid name specified\n");
792
793 return &null_variable.ID3D11ShaderReflectionVariable_iface;
794 }
795
796 static const struct ID3D11ShaderReflectionConstantBufferVtbl d3dcompiler_shader_reflection_constant_buffer_vtbl =
797 {
798 /* ID3D11ShaderReflectionConstantBuffer methods */
799 d3dcompiler_shader_reflection_constant_buffer_GetDesc,
800 d3dcompiler_shader_reflection_constant_buffer_GetVariableByIndex,
801 d3dcompiler_shader_reflection_constant_buffer_GetVariableByName,
802 };
803
804 /* ID3D11ShaderReflectionVariable methods */
805
806 static inline struct d3dcompiler_shader_reflection_variable *impl_from_ID3D11ShaderReflectionVariable(ID3D11ShaderReflectionVariable *iface)
807 {
808 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_variable, ID3D11ShaderReflectionVariable_iface);
809 }
810
811 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetDesc(
812 ID3D11ShaderReflectionVariable *iface, D3D11_SHADER_VARIABLE_DESC *desc)
813 {
814 struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
815
816 TRACE("iface %p, desc %p\n", iface, desc);
817
818 if (This == &null_variable)
819 {
820 WARN("Null variable specified\n");
821 return E_FAIL;
822 }
823
824 if (!desc)
825 {
826 WARN("Invalid argument specified\n");
827 return E_FAIL;
828 }
829
830 desc->Name = This->name;
831 desc->StartOffset = This->start_offset;
832 desc->Size = This->size;
833 desc->uFlags = This->flags;
834 desc->DefaultValue = This->default_value;
835
836 return S_OK;
837 }
838
839 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetType(
840 ID3D11ShaderReflectionVariable *iface)
841 {
842 struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
843
844 TRACE("iface %p\n", iface);
845
846 return &This->type->ID3D11ShaderReflectionType_iface;
847 }
848
849 static ID3D11ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetBuffer(
850 ID3D11ShaderReflectionVariable *iface)
851 {
852 struct d3dcompiler_shader_reflection_variable *This = impl_from_ID3D11ShaderReflectionVariable(iface);
853
854 TRACE("iface %p\n", iface);
855
856 return &This->constant_buffer->ID3D11ShaderReflectionConstantBuffer_iface;
857 }
858
859 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_variable_GetInterfaceSlot(
860 ID3D11ShaderReflectionVariable *iface, UINT index)
861 {
862 FIXME("iface %p, index %u stub!\n", iface, index);
863
864 return 0;
865 }
866
867 static const struct ID3D11ShaderReflectionVariableVtbl d3dcompiler_shader_reflection_variable_vtbl =
868 {
869 /* ID3D11ShaderReflectionVariable methods */
870 d3dcompiler_shader_reflection_variable_GetDesc,
871 d3dcompiler_shader_reflection_variable_GetType,
872 d3dcompiler_shader_reflection_variable_GetBuffer,
873 d3dcompiler_shader_reflection_variable_GetInterfaceSlot,
874 };
875
876 /* ID3D11ShaderReflectionType methods */
877
878 static inline struct d3dcompiler_shader_reflection_type *impl_from_ID3D11ShaderReflectionType(ID3D11ShaderReflectionType *iface)
879 {
880 return CONTAINING_RECORD(iface, struct d3dcompiler_shader_reflection_type, ID3D11ShaderReflectionType_iface);
881 }
882
883 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetDesc(
884 ID3D11ShaderReflectionType *iface, D3D11_SHADER_TYPE_DESC *desc)
885 {
886 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
887
888 TRACE("iface %p, desc %p\n", iface, desc);
889
890 if (This == &null_type)
891 {
892 WARN("Null type specified\n");
893 return E_FAIL;
894 }
895
896 if (!desc)
897 {
898 WARN("Invalid argument specified\n");
899 return E_FAIL;
900 }
901
902 *desc = This->desc;
903
904 return S_OK;
905 }
906
907 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeByIndex(
908 ID3D11ShaderReflectionType *iface, UINT index)
909 {
910 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
911
912 TRACE("iface %p, index %u\n", iface, index);
913
914 if (index >= This->desc.Members)
915 {
916 WARN("Invalid index specified\n");
917 return &null_type.ID3D11ShaderReflectionType_iface;
918 }
919
920 return &This->members[index].type->ID3D11ShaderReflectionType_iface;
921 }
922
923 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeByName(
924 ID3D11ShaderReflectionType *iface, const char *name)
925 {
926 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
927 unsigned int i;
928
929 TRACE("iface %p, name %s\n", iface, debugstr_a(name));
930
931 if (!name)
932 {
933 WARN("Invalid argument specified\n");
934 return &null_type.ID3D11ShaderReflectionType_iface;
935 }
936
937 for (i = 0; i < This->desc.Members; ++i)
938 {
939 struct d3dcompiler_shader_reflection_type_member *member = &This->members[i];
940
941 if (!strcmp(member->name, name))
942 {
943 TRACE("Returning ID3D11ShaderReflectionType %p.\n", member->type);
944 return &member->type->ID3D11ShaderReflectionType_iface;
945 }
946 }
947
948 WARN("Invalid name specified\n");
949
950 return &null_type.ID3D11ShaderReflectionType_iface;
951 }
952
953 static const char * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetMemberTypeName(
954 ID3D11ShaderReflectionType *iface, UINT index)
955 {
956 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
957
958 TRACE("iface %p, index %u\n", iface, index);
959
960 if (This == &null_type)
961 {
962 WARN("Null type specified\n");
963 return "$Invalid";
964 }
965
966 if (index >= This->desc.Members)
967 {
968 WARN("Invalid index specified\n");
969 return NULL;
970 }
971
972 return This->members[index].name;
973 }
974
975 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_IsEqual(
976 ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *type)
977 {
978 struct d3dcompiler_shader_reflection_type *This = impl_from_ID3D11ShaderReflectionType(iface);
979
980 TRACE("iface %p, type %p\n", iface, type);
981
982 if (This == &null_type)
983 {
984 WARN("Null type specified\n");
985 return E_FAIL;
986 }
987
988 if (iface == type)
989 return S_OK;
990
991 return S_FALSE;
992 }
993
994 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetSubType(
995 ID3D11ShaderReflectionType *iface)
996 {
997 FIXME("iface %p stub!\n", iface);
998
999 return NULL;
1000 }
1001
1002 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetBaseClass(
1003 ID3D11ShaderReflectionType *iface)
1004 {
1005 FIXME("iface %p stub!\n", iface);
1006
1007 return NULL;
1008 }
1009
1010 static UINT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetNumInterfaces(
1011 ID3D11ShaderReflectionType *iface)
1012 {
1013 FIXME("iface %p stub!\n", iface);
1014
1015 return 0;
1016 }
1017
1018 static ID3D11ShaderReflectionType * STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_GetInterfaceByIndex(
1019 ID3D11ShaderReflectionType *iface, UINT index)
1020 {
1021 FIXME("iface %p, index %u stub!\n", iface, index);
1022
1023 return NULL;
1024 }
1025
1026 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_IsOfType(
1027 ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *type)
1028 {
1029 FIXME("iface %p, type %p stub!\n", iface, type);
1030
1031 return E_NOTIMPL;
1032 }
1033
1034 static HRESULT STDMETHODCALLTYPE d3dcompiler_shader_reflection_type_ImplementsInterface(
1035 ID3D11ShaderReflectionType *iface, ID3D11ShaderReflectionType *base)
1036 {
1037 FIXME("iface %p, base %p stub!\n", iface, base);
1038
1039 return E_NOTIMPL;
1040 }
1041
1042 static const struct ID3D11ShaderReflectionTypeVtbl d3dcompiler_shader_reflection_type_vtbl =
1043 {
1044 /* ID3D11ShaderReflectionType methods */
1045 d3dcompiler_shader_reflection_type_GetDesc,
1046 d3dcompiler_shader_reflection_type_GetMemberTypeByIndex,
1047 d3dcompiler_shader_reflection_type_GetMemberTypeByName,
1048 d3dcompiler_shader_reflection_type_GetMemberTypeName,
1049 d3dcompiler_shader_reflection_type_IsEqual,
1050 d3dcompiler_shader_reflection_type_GetSubType,
1051 d3dcompiler_shader_reflection_type_GetBaseClass,
1052 d3dcompiler_shader_reflection_type_GetNumInterfaces,
1053 d3dcompiler_shader_reflection_type_GetInterfaceByIndex,
1054 d3dcompiler_shader_reflection_type_IsOfType,
1055 d3dcompiler_shader_reflection_type_ImplementsInterface,
1056 };
1057
1058 static HRESULT d3dcompiler_parse_stat(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1059 {
1060 const char *ptr = data;
1061 DWORD size = data_size >> 2;
1062
1063 TRACE("Size %u\n", size);
1064
1065 read_dword(&ptr, &r->instruction_count);
1066 TRACE("InstructionCount: %u\n", r->instruction_count);
1067
1068 read_dword(&ptr, &r->temp_register_count);
1069 TRACE("TempRegisterCount: %u\n", r->temp_register_count);
1070
1071 skip_dword_unknown(&ptr, 1);
1072
1073 read_dword(&ptr, &r->dcl_count);
1074 TRACE("DclCount: %u\n", r->dcl_count);
1075
1076 read_dword(&ptr, &r->float_instruction_count);
1077 TRACE("FloatInstructionCount: %u\n", r->float_instruction_count);
1078
1079 read_dword(&ptr, &r->int_instruction_count);
1080 TRACE("IntInstructionCount: %u\n", r->int_instruction_count);
1081
1082 read_dword(&ptr, &r->uint_instruction_count);
1083 TRACE("UintInstructionCount: %u\n", r->uint_instruction_count);
1084
1085 read_dword(&ptr, &r->static_flow_control_count);
1086 TRACE("StaticFlowControlCount: %u\n", r->static_flow_control_count);
1087
1088 read_dword(&ptr, &r->dynamic_flow_control_count);
1089 TRACE("DynamicFlowControlCount: %u\n", r->dynamic_flow_control_count);
1090
1091 skip_dword_unknown(&ptr, 1);
1092
1093 read_dword(&ptr, &r->temp_array_count);
1094 TRACE("TempArrayCount: %u\n", r->temp_array_count);
1095
1096 read_dword(&ptr, &r->array_instruction_count);
1097 TRACE("ArrayInstructionCount: %u\n", r->array_instruction_count);
1098
1099 read_dword(&ptr, &r->cut_instruction_count);
1100 TRACE("CutInstructionCount: %u\n", r->cut_instruction_count);
1101
1102 read_dword(&ptr, &r->emit_instruction_count);
1103 TRACE("EmitInstructionCount: %u\n", r->emit_instruction_count);
1104
1105 read_dword(&ptr, &r->texture_normal_instructions);
1106 TRACE("TextureNormalInstructions: %u\n", r->texture_normal_instructions);
1107
1108 read_dword(&ptr, &r->texture_load_instructions);
1109 TRACE("TextureLoadInstructions: %u\n", r->texture_load_instructions);
1110
1111 read_dword(&ptr, &r->texture_comp_instructions);
1112 TRACE("TextureCompInstructions: %u\n", r->texture_comp_instructions);
1113
1114 read_dword(&ptr, &r->texture_bias_instructions);
1115 TRACE("TextureBiasInstructions: %u\n", r->texture_bias_instructions);
1116
1117 read_dword(&ptr, &r->texture_gradient_instructions);
1118 TRACE("TextureGradientInstructions: %u\n", r->texture_gradient_instructions);
1119
1120 read_dword(&ptr, &r->mov_instruction_count);
1121 TRACE("MovInstructionCount: %u\n", r->mov_instruction_count);
1122
1123 skip_dword_unknown(&ptr, 1);
1124
1125 read_dword(&ptr, &r->conversion_instruction_count);
1126 TRACE("ConversionInstructionCount: %u\n", r->conversion_instruction_count);
1127
1128 skip_dword_unknown(&ptr, 1);
1129
1130 read_dword(&ptr, &r->input_primitive);
1131 TRACE("InputPrimitive: %x\n", r->input_primitive);
1132
1133 read_dword(&ptr, &r->gs_output_topology);
1134 TRACE("GSOutputTopology: %x\n", r->gs_output_topology);
1135
1136 read_dword(&ptr, &r->gs_max_output_vertex_count);
1137 TRACE("GSMaxOutputVertexCount: %u\n", r->gs_max_output_vertex_count);
1138
1139 skip_dword_unknown(&ptr, 3);
1140
1141 /* dx10 stat size */
1142 if (size == 29) return S_OK;
1143
1144 skip_dword_unknown(&ptr, 1);
1145
1146 read_dword(&ptr, &r->c_control_points);
1147 TRACE("cControlPoints: %u\n", r->c_control_points);
1148
1149 read_dword(&ptr, &r->hs_output_primitive);
1150 TRACE("HSOutputPrimitive: %x\n", r->hs_output_primitive);
1151
1152 read_dword(&ptr, &r->hs_prtitioning);
1153 TRACE("HSPartitioning: %x\n", r->hs_prtitioning);
1154
1155 read_dword(&ptr, &r->tessellator_domain);
1156 TRACE("TessellatorDomain: %x\n", r->tessellator_domain);
1157
1158 skip_dword_unknown(&ptr, 3);
1159
1160 /* dx11 stat size */
1161 if (size == 37) return S_OK;
1162
1163 FIXME("Unhandled size %u\n", size);
1164
1165 return E_FAIL;
1166 }
1167
1168 static HRESULT d3dcompiler_parse_type_members(struct d3dcompiler_shader_reflection *ref,
1169 struct d3dcompiler_shader_reflection_type_member *member, const char *data, const char **ptr)
1170 {
1171 DWORD offset;
1172
1173 read_dword(ptr, &offset);
1174 if (!copy_name(data + offset, &member->name))
1175 {
1176 ERR("Failed to copy name.\n");
1177 return E_OUTOFMEMORY;
1178 }
1179 TRACE("Member name: %s.\n", debugstr_a(member->name));
1180
1181 read_dword(ptr, &offset);
1182 TRACE("Member type offset: %x\n", offset);
1183
1184 member->type = get_reflection_type(ref, data, offset);
1185 if (!member->type)
1186 {
1187 ERR("Failed to get member type\n");
1188 HeapFree(GetProcessHeap(), 0, member->name);
1189 return E_FAIL;
1190 }
1191
1192 read_dword(ptr, &member->offset);
1193 TRACE("Member offset %x\n", member->offset);
1194
1195 return S_OK;
1196 }
1197
1198 static HRESULT d3dcompiler_parse_type(struct d3dcompiler_shader_reflection_type *type, const char *data, DWORD offset)
1199 {
1200 const char *ptr = data + offset;
1201 DWORD temp;
1202 D3D11_SHADER_TYPE_DESC *desc;
1203 unsigned int i;
1204 struct d3dcompiler_shader_reflection_type_member *members = NULL;
1205 HRESULT hr;
1206 DWORD member_offset;
1207
1208 desc = &type->desc;
1209
1210 read_dword(&ptr, &temp);
1211 desc->Class = temp & 0xffff;
1212 desc->Type = temp >> 16;
1213 TRACE("Class %s, Type %s\n", debug_d3dcompiler_shader_variable_class(desc->Class),
1214 debug_d3dcompiler_shader_variable_type(desc->Type));
1215
1216 read_dword(&ptr, &temp);
1217 desc->Rows = temp & 0xffff;
1218 desc->Columns = temp >> 16;
1219 TRACE("Rows %u, Columns %u\n", desc->Rows, desc->Columns);
1220
1221 read_dword(&ptr, &temp);
1222 desc->Elements = temp & 0xffff;
1223 desc->Members = temp >> 16;
1224 TRACE("Elements %u, Members %u\n", desc->Elements, desc->Members);
1225
1226 read_dword(&ptr, &member_offset);
1227 TRACE("Member Offset %u\n", member_offset);
1228
1229 if ((type->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1230 skip_dword_unknown(&ptr, 4);
1231
1232 if (desc->Members)
1233 {
1234 const char *ptr2 = data + member_offset;
1235
1236 members = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*members) * desc->Members);
1237 if (!members)
1238 {
1239 ERR("Failed to allocate type memory.\n");
1240 return E_OUTOFMEMORY;
1241 }
1242
1243 for (i = 0; i < desc->Members; ++i)
1244 {
1245 hr = d3dcompiler_parse_type_members(type->reflection, &members[i], data, &ptr2);
1246 if (hr != S_OK)
1247 {
1248 FIXME("Failed to parse type members.\n");
1249 goto err_out;
1250 }
1251 }
1252 }
1253
1254 type->members = members;
1255
1256 return S_OK;
1257
1258 err_out:
1259 for (i = 0; i < desc->Members; ++i)
1260 {
1261 free_type_member(&members[i]);
1262 }
1263 HeapFree(GetProcessHeap(), 0, members);
1264 return hr;
1265 }
1266
1267 static struct d3dcompiler_shader_reflection_type *get_reflection_type(struct d3dcompiler_shader_reflection *reflection, const char *data, DWORD offset)
1268 {
1269 struct d3dcompiler_shader_reflection_type *type;
1270 struct wine_rb_entry *entry;
1271 HRESULT hr;
1272
1273 entry = wine_rb_get(&reflection->types, &offset);
1274 if (entry)
1275 {
1276 TRACE("Returning existing type.\n");
1277 return WINE_RB_ENTRY_VALUE(entry, struct d3dcompiler_shader_reflection_type, entry);
1278 }
1279
1280 type = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*type));
1281 if (!type)
1282 return NULL;
1283
1284 type->ID3D11ShaderReflectionType_iface.lpVtbl = &d3dcompiler_shader_reflection_type_vtbl;
1285 type->id = offset;
1286 type->reflection = reflection;
1287
1288 hr = d3dcompiler_parse_type(type, data, offset);
1289 if (FAILED(hr))
1290 {
1291 ERR("Failed to parse type info, hr %#x.\n", hr);
1292 HeapFree(GetProcessHeap(), 0, type);
1293 return NULL;
1294 }
1295
1296 if (wine_rb_put(&reflection->types, &offset, &type->entry) == -1)
1297 {
1298 ERR("Failed to insert type entry.\n");
1299 HeapFree(GetProcessHeap(), 0, type);
1300 return NULL;
1301 }
1302
1303 return type;
1304 }
1305
1306 static HRESULT d3dcompiler_parse_variables(struct d3dcompiler_shader_reflection_constant_buffer *cb,
1307 const char *data, DWORD data_size, const char *ptr)
1308 {
1309 struct d3dcompiler_shader_reflection_variable *variables;
1310 unsigned int i;
1311 HRESULT hr;
1312
1313 variables = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb->variable_count * sizeof(*variables));
1314 if (!variables)
1315 {
1316 ERR("Failed to allocate variables memory.\n");
1317 return E_OUTOFMEMORY;
1318 }
1319
1320 for (i = 0; i < cb->variable_count; i++)
1321 {
1322 struct d3dcompiler_shader_reflection_variable *v = &variables[i];
1323 DWORD offset;
1324
1325 v->ID3D11ShaderReflectionVariable_iface.lpVtbl = &d3dcompiler_shader_reflection_variable_vtbl;
1326 v->constant_buffer = cb;
1327
1328 read_dword(&ptr, &offset);
1329 if (!copy_name(data + offset, &v->name))
1330 {
1331 ERR("Failed to copy name.\n");
1332 hr = E_OUTOFMEMORY;
1333 goto err_out;
1334 }
1335 TRACE("Variable name: %s.\n", debugstr_a(v->name));
1336
1337 read_dword(&ptr, &v->start_offset);
1338 TRACE("Variable offset: %u\n", v->start_offset);
1339
1340 read_dword(&ptr, &v->size);
1341 TRACE("Variable size: %u\n", v->size);
1342
1343 read_dword(&ptr, &v->flags);
1344 TRACE("Variable flags: %u\n", v->flags);
1345
1346 read_dword(&ptr, &offset);
1347 TRACE("Variable type offset: %x\n", offset);
1348 v->type = get_reflection_type(cb->reflection, data, offset);
1349 if (!v->type)
1350 {
1351 ERR("Failed to get type.\n");
1352 hr = E_FAIL;
1353 goto err_out;
1354 }
1355
1356 read_dword(&ptr, &offset);
1357 TRACE("Variable default value offset: %x\n", offset);
1358 if (!copy_value(data + offset, &v->default_value, offset ? v->size : 0))
1359 {
1360 ERR("Failed to copy name.\n");
1361 hr = E_OUTOFMEMORY;
1362 goto err_out;
1363 }
1364
1365 if ((cb->reflection->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1366 skip_dword_unknown(&ptr, 4);
1367 }
1368
1369 cb->variables = variables;
1370
1371 return S_OK;
1372
1373 err_out:
1374 for (i = 0; i < cb->variable_count; i++)
1375 {
1376 free_variable(&variables[i]);
1377 }
1378 HeapFree(GetProcessHeap(), 0, variables);
1379 return hr;
1380 }
1381
1382 static HRESULT d3dcompiler_parse_rdef(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1383 {
1384 const char *ptr = data;
1385 DWORD size = data_size >> 2;
1386 DWORD offset, cbuffer_offset, resource_offset, creator_offset;
1387 unsigned int i, string_data_offset, string_data_size;
1388 char *string_data = NULL, *creator = NULL;
1389 D3D11_SHADER_INPUT_BIND_DESC *bound_resources = NULL;
1390 struct d3dcompiler_shader_reflection_constant_buffer *constant_buffers = NULL;
1391 HRESULT hr;
1392
1393 TRACE("Size %u\n", size);
1394
1395 read_dword(&ptr, &r->constant_buffer_count);
1396 TRACE("Constant buffer count: %u\n", r->constant_buffer_count);
1397
1398 read_dword(&ptr, &cbuffer_offset);
1399 TRACE("Constant buffer offset: %#x\n", cbuffer_offset);
1400
1401 read_dword(&ptr, &r->bound_resource_count);
1402 TRACE("Bound resource count: %u\n", r->bound_resource_count);
1403
1404 read_dword(&ptr, &resource_offset);
1405 TRACE("Bound resource offset: %#x\n", resource_offset);
1406
1407 read_dword(&ptr, &r->target);
1408 TRACE("Target: %#x\n", r->target);
1409
1410 read_dword(&ptr, &r->flags);
1411 TRACE("Flags: %u\n", r->flags);
1412
1413 read_dword(&ptr, &creator_offset);
1414 TRACE("Creator at offset %#x.\n", creator_offset);
1415
1416 if (!copy_name(data + creator_offset, &creator))
1417 {
1418 ERR("Failed to copy name.\n");
1419 return E_OUTOFMEMORY;
1420 }
1421 TRACE("Creator: %s.\n", debugstr_a(creator));
1422
1423 /* todo: Parse RD11 */
1424 if ((r->target & D3DCOMPILER_SHADER_TARGET_VERSION_MASK) >= 0x500)
1425 {
1426 skip_dword_unknown(&ptr, 8);
1427 }
1428
1429 if (r->bound_resource_count)
1430 {
1431 /* 8 for each bind desc */
1432 string_data_offset = resource_offset + r->bound_resource_count * 8 * sizeof(DWORD);
1433 string_data_size = (cbuffer_offset ? cbuffer_offset : creator_offset) - string_data_offset;
1434
1435 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1436 if (!string_data)
1437 {
1438 ERR("Failed to allocate string data memory.\n");
1439 hr = E_OUTOFMEMORY;
1440 goto err_out;
1441 }
1442 memcpy(string_data, data + string_data_offset, string_data_size);
1443
1444 bound_resources = HeapAlloc(GetProcessHeap(), 0, r->bound_resource_count * sizeof(*bound_resources));
1445 if (!bound_resources)
1446 {
1447 ERR("Failed to allocate resources memory.\n");
1448 hr = E_OUTOFMEMORY;
1449 goto err_out;
1450 }
1451
1452 ptr = data + resource_offset;
1453 for (i = 0; i < r->bound_resource_count; i++)
1454 {
1455 D3D11_SHADER_INPUT_BIND_DESC *desc = &bound_resources[i];
1456
1457 read_dword(&ptr, &offset);
1458 desc->Name = string_data + (offset - string_data_offset);
1459 TRACE("Input bind Name: %s\n", debugstr_a(desc->Name));
1460
1461 read_dword(&ptr, &desc->Type);
1462 TRACE("Input bind Type: %#x\n", desc->Type);
1463
1464 read_dword(&ptr, &desc->ReturnType);
1465 TRACE("Input bind ReturnType: %#x\n", desc->ReturnType);
1466
1467 read_dword(&ptr, &desc->Dimension);
1468 TRACE("Input bind Dimension: %#x\n", desc->Dimension);
1469
1470 read_dword(&ptr, &desc->NumSamples);
1471 TRACE("Input bind NumSamples: %u\n", desc->NumSamples);
1472
1473 read_dword(&ptr, &desc->BindPoint);
1474 TRACE("Input bind BindPoint: %u\n", desc->BindPoint);
1475
1476 read_dword(&ptr, &desc->BindCount);
1477 TRACE("Input bind BindCount: %u\n", desc->BindCount);
1478
1479 read_dword(&ptr, &desc->uFlags);
1480 TRACE("Input bind uFlags: %u\n", desc->uFlags);
1481 }
1482 }
1483
1484 if (r->constant_buffer_count)
1485 {
1486 constant_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, r->constant_buffer_count * sizeof(*constant_buffers));
1487 if (!constant_buffers)
1488 {
1489 ERR("Failed to allocate constant buffer memory.\n");
1490 hr = E_OUTOFMEMORY;
1491 goto err_out;
1492 }
1493
1494 ptr = data + cbuffer_offset;
1495 for (i = 0; i < r->constant_buffer_count; i++)
1496 {
1497 struct d3dcompiler_shader_reflection_constant_buffer *cb = &constant_buffers[i];
1498
1499 cb->ID3D11ShaderReflectionConstantBuffer_iface.lpVtbl = &d3dcompiler_shader_reflection_constant_buffer_vtbl;
1500 cb->reflection = r;
1501
1502 read_dword(&ptr, &offset);
1503 if (!copy_name(data + offset, &cb->name))
1504 {
1505 ERR("Failed to copy name.\n");
1506 hr = E_OUTOFMEMORY;
1507 goto err_out;
1508 }
1509 TRACE("Name: %s.\n", debugstr_a(cb->name));
1510
1511 read_dword(&ptr, &cb->variable_count);
1512 TRACE("Variable count: %u\n", cb->variable_count);
1513
1514 read_dword(&ptr, &offset);
1515 TRACE("Variable offset: %x\n", offset);
1516
1517 hr = d3dcompiler_parse_variables(cb, data, data_size, data + offset);
1518 if (hr != S_OK)
1519 {
1520 FIXME("Failed to parse variables.\n");
1521 goto err_out;
1522 }
1523
1524 read_dword(&ptr, &cb->size);
1525 TRACE("Cbuffer size: %u\n", cb->size);
1526
1527 read_dword(&ptr, &cb->flags);
1528 TRACE("Cbuffer flags: %u\n", cb->flags);
1529
1530 read_dword(&ptr, &cb->type);
1531 TRACE("Cbuffer type: %#x\n", cb->type);
1532 }
1533 }
1534
1535 r->creator = creator;
1536 r->resource_string = string_data;
1537 r->bound_resources = bound_resources;
1538 r->constant_buffers = constant_buffers;
1539
1540 return S_OK;
1541
1542 err_out:
1543 for (i = 0; i < r->constant_buffer_count; ++i)
1544 {
1545 free_constant_buffer(&constant_buffers[i]);
1546 }
1547 HeapFree(GetProcessHeap(), 0, constant_buffers);
1548 HeapFree(GetProcessHeap(), 0, bound_resources);
1549 HeapFree(GetProcessHeap(), 0, string_data);
1550 HeapFree(GetProcessHeap(), 0, creator);
1551
1552 return hr;
1553 }
1554
1555 static HRESULT d3dcompiler_parse_signature(struct d3dcompiler_shader_signature *s, struct dxbc_section *section, DWORD target)
1556 {
1557 D3D11_SIGNATURE_PARAMETER_DESC *d;
1558 unsigned int string_data_offset;
1559 unsigned int string_data_size;
1560 const char *ptr = section->data;
1561 char *string_data;
1562 unsigned int i;
1563 DWORD count;
1564 enum D3DCOMPILER_SIGNATURE_ELEMENT_SIZE element_size;
1565
1566 switch (section->tag)
1567 {
1568 case TAG_OSG5:
1569 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7;
1570 break;
1571
1572 case TAG_ISGN:
1573 case TAG_OSGN:
1574 case TAG_PCSG:
1575 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1576 break;
1577
1578 default:
1579 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1580 element_size = D3DCOMPILER_SIGNATURE_ELEMENT_SIZE6;
1581 break;
1582 }
1583
1584 read_dword(&ptr, &count);
1585 TRACE("%u elements\n", count);
1586
1587 skip_dword_unknown(&ptr, 1);
1588
1589 d = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*d));
1590 if (!d)
1591 {
1592 ERR("Failed to allocate signature memory.\n");
1593 return E_OUTOFMEMORY;
1594 }
1595
1596 /* 2 DWORDs for the header, element_size for each element. */
1597 string_data_offset = 2 * sizeof(DWORD) + count * element_size * sizeof(DWORD);
1598 string_data_size = section->data_size - string_data_offset;
1599
1600 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
1601 if (!string_data)
1602 {
1603 ERR("Failed to allocate string data memory.\n");
1604 HeapFree(GetProcessHeap(), 0, d);
1605 return E_OUTOFMEMORY;
1606 }
1607 memcpy(string_data, section->data + string_data_offset, string_data_size);
1608
1609 for (i = 0; i < count; ++i)
1610 {
1611 UINT name_offset;
1612 DWORD mask;
1613
1614 if (element_size == D3DCOMPILER_SIGNATURE_ELEMENT_SIZE7)
1615 {
1616 read_dword(&ptr, &d[i].Stream);
1617 }
1618 else
1619 {
1620 d[i].Stream = 0;
1621 }
1622
1623 read_dword(&ptr, &name_offset);
1624 d[i].SemanticName = string_data + (name_offset - string_data_offset);
1625 read_dword(&ptr, &d[i].SemanticIndex);
1626 read_dword(&ptr, &d[i].SystemValueType);
1627 read_dword(&ptr, &d[i].ComponentType);
1628 read_dword(&ptr, &d[i].Register);
1629 read_dword(&ptr, &mask);
1630 d[i].ReadWriteMask = (mask >> 8) & 0xff;
1631 d[i].Mask = mask & 0xff;
1632
1633 /* pixel shaders have a special handling for SystemValueType in the output signature */
1634 if (((target & D3DCOMPILER_SHADER_TARGET_SHADERTYPE_MASK) == 0xffff0000) && (section->tag == TAG_OSG5 || section->tag == TAG_OSGN))
1635 {
1636 TRACE("Pixelshader output signature fixup.\n");
1637
1638 if (d[i].Register == 0xffffffff)
1639 {
1640 if (!strcasecmp(d[i].SemanticName, "sv_depth")) d[i].SystemValueType = D3D_NAME_DEPTH;
1641 if (!strcasecmp(d[i].SemanticName, "sv_coverage")) d[i].SystemValueType = D3D_NAME_COVERAGE;
1642 if (!strcasecmp(d[i].SemanticName, "sv_depthgreaterequal")) d[i].SystemValueType = D3D_NAME_DEPTH_GREATER_EQUAL;
1643 if (!strcasecmp(d[i].SemanticName, "sv_depthlessequal")) d[i].SystemValueType = D3D_NAME_DEPTH_LESS_EQUAL;
1644 }
1645 else
1646 {
1647 d[i].SystemValueType = D3D_NAME_TARGET;
1648 }
1649 }
1650
1651 TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
1652 "type %u, register idx: %u, use_mask %#x, input_mask %#x, stream %u\n",
1653 debugstr_a(d[i].SemanticName), d[i].SemanticIndex, d[i].SystemValueType,
1654 d[i].ComponentType, d[i].Register, d[i].Mask, d[i].ReadWriteMask, d[i].Stream);
1655 }
1656
1657 s->elements = d;
1658 s->element_count = count;
1659 s->string_data = string_data;
1660
1661 return S_OK;
1662 }
1663
1664 static HRESULT d3dcompiler_parse_shdr(struct d3dcompiler_shader_reflection *r, const char *data, DWORD data_size)
1665 {
1666 const char *ptr = data;
1667
1668 read_dword(&ptr, &r->version);
1669 TRACE("Shader version: %u\n", r->version);
1670
1671 /* todo: Check if anything else is needed from the shdr or shex blob. */
1672
1673 return S_OK;
1674 }
1675
1676 static HRESULT d3dcompiler_shader_reflection_init(struct d3dcompiler_shader_reflection *reflection,
1677 const void *data, SIZE_T data_size)
1678 {
1679 struct dxbc src_dxbc;
1680 HRESULT hr;
1681 unsigned int i;
1682
1683 reflection->ID3D11ShaderReflection_iface.lpVtbl = &d3dcompiler_shader_reflection_vtbl;
1684 reflection->refcount = 1;
1685
1686 if (wine_rb_init(&reflection->types, &d3dcompiler_shader_reflection_type_rb_functions) == -1)
1687 {
1688 ERR("Failed to initialize type rbtree.\n");
1689 return E_FAIL;
1690 }
1691
1692 hr = dxbc_parse(data, data_size, &src_dxbc);
1693 if (FAILED(hr))
1694 {
1695 WARN("Failed to parse reflection\n");
1696 return hr;
1697 }
1698
1699 for (i = 0; i < src_dxbc.count; ++i)
1700 {
1701 struct dxbc_section *section = &src_dxbc.sections[i];
1702
1703 switch (section->tag)
1704 {
1705 case TAG_RDEF:
1706 hr = d3dcompiler_parse_rdef(reflection, section->data, section->data_size);
1707 if (FAILED(hr))
1708 {
1709 WARN("Failed to parse RDEF section.\n");
1710 goto err_out;
1711 }
1712 break;
1713
1714 case TAG_ISGN:
1715 reflection->isgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->isgn));
1716 if (!reflection->isgn)
1717 {
1718 ERR("Failed to allocate ISGN memory.\n");
1719 hr = E_OUTOFMEMORY;
1720 goto err_out;
1721 }
1722
1723 hr = d3dcompiler_parse_signature(reflection->isgn, section, reflection->target);
1724 if (FAILED(hr))
1725 {
1726 WARN("Failed to parse section ISGN.\n");
1727 goto err_out;
1728 }
1729 break;
1730
1731 case TAG_OSG5:
1732 case TAG_OSGN:
1733 reflection->osgn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->osgn));
1734 if (!reflection->osgn)
1735 {
1736 ERR("Failed to allocate OSGN memory.\n");
1737 hr = E_OUTOFMEMORY;
1738 goto err_out;
1739 }
1740
1741 hr = d3dcompiler_parse_signature(reflection->osgn, section, reflection->target);
1742 if (FAILED(hr))
1743 {
1744 WARN("Failed to parse section OSGN.\n");
1745 goto err_out;
1746 }
1747 break;
1748
1749 case TAG_PCSG:
1750 reflection->pcsg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*reflection->pcsg));
1751 if (!reflection->pcsg)
1752 {
1753 ERR("Failed to allocate PCSG memory.\n");
1754 hr = E_OUTOFMEMORY;
1755 goto err_out;
1756 }
1757
1758 hr = d3dcompiler_parse_signature(reflection->pcsg, section, reflection->target);
1759 if (FAILED(hr))
1760 {
1761 WARN("Failed to parse section PCSG.\n");
1762 goto err_out;
1763 }
1764 break;
1765
1766 case TAG_SHEX:
1767 case TAG_SHDR:
1768 hr = d3dcompiler_parse_shdr(reflection, section->data, section->data_size);
1769 if (FAILED(hr))
1770 {
1771 WARN("Failed to parse SHDR section.\n");
1772 goto err_out;
1773 }
1774 break;
1775
1776 case TAG_STAT:
1777 hr = d3dcompiler_parse_stat(reflection, section->data, section->data_size);
1778 if (FAILED(hr))
1779 {
1780 WARN("Failed to parse section STAT.\n");
1781 goto err_out;
1782 }
1783 break;
1784
1785 default:
1786 FIXME("Unhandled section %s!\n", debugstr_an((const char *)&section->tag, 4));
1787 break;
1788 }
1789 }
1790
1791 dxbc_destroy(&src_dxbc);
1792
1793 return hr;
1794
1795 err_out:
1796 reflection_cleanup(reflection);
1797 dxbc_destroy(&src_dxbc);
1798
1799 return hr;
1800 }
1801
1802 HRESULT WINAPI D3DReflect(const void *data, SIZE_T data_size, REFIID riid, void **reflector)
1803 {
1804 struct d3dcompiler_shader_reflection *object;
1805 HRESULT hr;
1806 const DWORD *temp = data;
1807
1808 TRACE("data %p, data_size %lu, riid %s, blob %p\n", data, data_size, debugstr_guid(riid), reflector);
1809
1810 if (!data || data_size < 32)
1811 {
1812 WARN("Invalid argument supplied.\n");
1813 return D3DERR_INVALIDCALL;
1814 }
1815
1816 if (temp[6] != data_size)
1817 {
1818 WARN("Wrong size supplied.\n");
1819 return E_FAIL;
1820 }
1821
1822 if (!IsEqualGUID(riid, &IID_ID3D11ShaderReflection))
1823 {
1824 WARN("Wrong riid %s, accept only %s!\n", debugstr_guid(riid), debugstr_guid(&IID_ID3D11ShaderReflection));
1825 return E_NOINTERFACE;
1826 }
1827
1828 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1829 if (!object)
1830 return E_OUTOFMEMORY;
1831
1832 hr = d3dcompiler_shader_reflection_init(object, data, data_size);
1833 if (FAILED(hr))
1834 {
1835 WARN("Failed to initialize shader reflection\n");
1836 HeapFree(GetProcessHeap(), 0, object);
1837 return hr;
1838 }
1839
1840 *reflector = object;
1841
1842 TRACE("Created ID3D11ShaderReflection %p\n", object);
1843
1844 return S_OK;
1845 }