vkd3d-utils: Implement ID3D12ShaderReflection::GetConstantBufferByIndex().

This commit is contained in:
Zebediah Figura 2024-02-01 17:13:30 -06:00 committed by Alexandre Julliard
parent 166dc24b2f
commit 90cd57404b
Notes: Alexandre Julliard 2024-03-27 23:06:24 +01:00
Approved-by: Giovanni Mascellani (@giomasce)
Approved-by: Henri Verbeet (@hverbeet)
Approved-by: Alexandre Julliard (@julliard)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/626
2 changed files with 246 additions and 6 deletions

View File

@ -65,6 +65,15 @@ static inline char *vkd3d_strdup(const char *string)
return ptr;
}
static inline void *vkd3d_memdup(const void *mem, size_t size)
{
void *ptr;
if ((ptr = vkd3d_malloc(size)))
memcpy(ptr, mem, size);
return ptr;
}
bool vkd3d_array_reserve(void **elements, size_t *capacity, size_t element_count, size_t element_size);
#endif /* __VKD3D_MEMORY_H */

View File

@ -21,14 +21,79 @@
#include <vkd3d_d3dcommon.h>
#include <vkd3d_d3d12shader.h>
struct d3d12_buffer
{
ID3D12ShaderReflectionConstantBuffer ID3D12ShaderReflectionConstantBuffer_iface;
D3D12_SHADER_BUFFER_DESC desc;
};
struct d3d12_reflection
{
ID3D12ShaderReflection ID3D12ShaderReflection_iface;
unsigned int refcount;
struct vkd3d_shader_scan_signature_info signature_info;
D3D12_SHADER_DESC desc;
struct d3d12_buffer *buffers;
};
static struct d3d12_buffer null_buffer;
static struct d3d12_buffer *impl_from_ID3D12ShaderReflectionConstantBuffer(ID3D12ShaderReflectionConstantBuffer *iface)
{
return CONTAINING_RECORD(iface, struct d3d12_buffer, ID3D12ShaderReflectionConstantBuffer_iface);
}
static HRESULT STDMETHODCALLTYPE d3d12_buffer_GetDesc(
ID3D12ShaderReflectionConstantBuffer *iface, D3D12_SHADER_BUFFER_DESC *desc)
{
struct d3d12_buffer *buffer = impl_from_ID3D12ShaderReflectionConstantBuffer(iface);
TRACE("iface %p, desc %p.\n", iface, desc);
if (buffer == &null_buffer)
{
WARN("Null constant buffer, returning E_FAIL.\n");
return E_FAIL;
}
if (!desc)
{
WARN("NULL pointer, returning E_FAIL.\n");
return E_FAIL;
}
*desc = buffer->desc;
return S_OK;
}
static ID3D12ShaderReflectionVariable * STDMETHODCALLTYPE d3d12_buffer_GetVariableByIndex(
ID3D12ShaderReflectionConstantBuffer *iface, UINT index)
{
FIXME("iface %p, index %u, stub!\n", iface, index);
return NULL;
}
static ID3D12ShaderReflectionVariable * STDMETHODCALLTYPE d3d12_buffer_GetVariableByName(
ID3D12ShaderReflectionConstantBuffer *iface, const char *name)
{
FIXME("iface %p, name %s, stub!\n", iface, debugstr_a(name));
return NULL;
}
static const struct ID3D12ShaderReflectionConstantBufferVtbl d3d12_buffer_vtbl =
{
d3d12_buffer_GetDesc,
d3d12_buffer_GetVariableByIndex,
d3d12_buffer_GetVariableByName,
};
static struct d3d12_buffer null_buffer = {{&d3d12_buffer_vtbl}};
static struct d3d12_reflection *impl_from_ID3D12ShaderReflection(ID3D12ShaderReflection *iface)
{
return CONTAINING_RECORD(iface, struct d3d12_reflection, ID3D12ShaderReflection_iface);
@ -72,6 +137,10 @@ static ULONG STDMETHODCALLTYPE d3d12_reflection_Release(ID3D12ShaderReflection *
if (!refcount)
{
for (UINT i = 0; i < reflection->desc.ConstantBuffers; ++i)
vkd3d_free((void *)reflection->buffers[i].desc.Name);
vkd3d_free(reflection->buffers);
vkd3d_shader_free_scan_signature_info(&reflection->signature_info);
free(reflection);
}
@ -87,9 +156,7 @@ static HRESULT STDMETHODCALLTYPE d3d12_reflection_GetDesc(ID3D12ShaderReflection
FIXME("iface %p, desc %p partial stub!\n", iface, desc);
desc->InputParameters = reflection->signature_info.input.element_count;
desc->OutputParameters = reflection->signature_info.output.element_count;
desc->PatchConstantParameters = reflection->signature_info.patch_constant.element_count;
*desc = reflection->desc;
return S_OK;
}
@ -97,9 +164,17 @@ static HRESULT STDMETHODCALLTYPE d3d12_reflection_GetDesc(ID3D12ShaderReflection
static struct ID3D12ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3d12_reflection_GetConstantBufferByIndex(
ID3D12ShaderReflection *iface, UINT index)
{
FIXME("iface %p, index %u stub!\n", iface, index);
struct d3d12_reflection *reflection = impl_from_ID3D12ShaderReflection(iface);
return NULL;
TRACE("iface %p, index %u.\n", iface, index);
if (index > reflection->desc.ConstantBuffers)
{
WARN("Invalid index %u.\n", index);
return &null_buffer.ID3D12ShaderReflectionConstantBuffer_iface;
}
return &reflection->buffers[index].ID3D12ShaderReflectionConstantBuffer_iface;
}
static struct ID3D12ShaderReflectionConstantBuffer * STDMETHODCALLTYPE d3d12_reflection_GetConstantBufferByName(
@ -287,9 +362,128 @@ static const struct ID3D12ShaderReflectionVtbl d3d12_reflection_vtbl =
d3d12_reflection_GetRequiresFlags,
};
static bool require_space(size_t offset, size_t count, size_t size, size_t data_size)
{
return !count || (data_size - offset) / count >= size;
}
/* Return a pointer to data in a code blob, with bounds checking. */
static const void *get_data_ptr(const struct vkd3d_shader_code *code,
uint32_t offset, uint32_t count, uint32_t size)
{
if (!require_space(offset, count, size, code->size))
{
WARN("Offset %#x and size %#x exceeds section size %#zx.\n", offset, size, code->size);
return NULL;
}
return (const uint8_t *)code->code + offset;
}
static HRESULT get_string(const struct vkd3d_shader_code *code, uint32_t offset, char **ret)
{
const char *str;
char *end;
if (offset >= code->size)
{
WARN("Offset %#x exceeds size %#zx.\n", offset, code->size);
return E_INVALIDARG;
}
str = (const char *)code->code + offset;
if (!(end = memchr(str, 0, code->size - offset)))
{
WARN("String at %#x is not properly zero-terminated.\n", offset);
return E_INVALIDARG;
}
if (!(*ret = vkd3d_memdup(str, end + 1 - str)))
return E_OUTOFMEMORY;
return S_OK;
}
struct rdef_header
{
uint32_t buffer_count;
uint32_t buffers_offset;
uint32_t binding_count;
uint32_t bindings_offset;
uint8_t minor_version;
uint8_t major_version;
uint16_t type;
uint32_t compile_flags;
uint32_t creator_offset;
};
struct rdef_buffer
{
uint32_t name_offset;
uint32_t var_count;
uint32_t vars_offset;
uint32_t size;
uint32_t flags;
uint32_t type;
};
static HRESULT d3d12_buffer_init(struct d3d12_buffer *buffer, const struct rdef_buffer *rdef_buffer,
const struct vkd3d_shader_code *section)
{
HRESULT hr;
char *name;
if ((FAILED(hr = get_string(section, rdef_buffer->name_offset, &name))))
return hr;
buffer->ID3D12ShaderReflectionConstantBuffer_iface.lpVtbl = &d3d12_buffer_vtbl;
buffer->desc.Type = rdef_buffer->type;
buffer->desc.Variables = rdef_buffer->var_count;
buffer->desc.Size = rdef_buffer->size;
buffer->desc.uFlags = rdef_buffer->flags;
buffer->desc.Name = name;
return S_OK;
}
static HRESULT parse_rdef(struct d3d12_reflection *reflection, const struct vkd3d_shader_code *section)
{
const struct rdef_header *header;
HRESULT hr;
if (!(header = get_data_ptr(section, 0, 1, sizeof(*header))))
return E_INVALIDARG;
reflection->desc.ConstantBuffers = header->buffer_count;
if (header->buffer_count)
{
const struct rdef_buffer *rdef_buffers;
if (!(rdef_buffers = get_data_ptr(section, header->buffers_offset,
header->buffer_count, sizeof(*rdef_buffers))))
return E_INVALIDARG;
if (!(reflection->buffers = vkd3d_calloc(header->buffer_count, sizeof(*reflection->buffers))))
return E_OUTOFMEMORY;
for (uint32_t i = 0; i < header->buffer_count; ++i)
{
if ((hr = d3d12_buffer_init(&reflection->buffers[i], &rdef_buffers[i], section)))
return hr;
}
}
return S_OK;
}
static HRESULT d3d12_reflection_init(struct d3d12_reflection *reflection, const void *data, size_t data_size)
{
struct vkd3d_shader_compile_info compile_info = {.type = VKD3D_SHADER_STRUCTURE_TYPE_COMPILE_INFO};
struct vkd3d_shader_dxbc_desc dxbc_desc;
bool found_rdef = false;
enum vkd3d_result ret;
HRESULT hr;
reflection->ID3D12ShaderReflection_iface.lpVtbl = &d3d12_reflection_vtbl;
reflection->refcount = 1;
@ -301,7 +495,44 @@ static HRESULT d3d12_reflection_init(struct d3d12_reflection *reflection, const
compile_info.next = &reflection->signature_info;
reflection->signature_info.type = VKD3D_SHADER_STRUCTURE_TYPE_SCAN_SIGNATURE_INFO;
return hresult_from_vkd3d_result(vkd3d_shader_scan(&compile_info, NULL));
if (FAILED(hr = hresult_from_vkd3d_result(vkd3d_shader_scan(&compile_info, NULL))))
return hr;
if ((ret = vkd3d_shader_parse_dxbc(&compile_info.source, 0, &dxbc_desc, NULL)))
{
vkd3d_shader_free_scan_signature_info(&reflection->signature_info);
return hresult_from_vkd3d_result(ret);
}
for (unsigned int i = 0; i < dxbc_desc.section_count; ++i)
{
const struct vkd3d_shader_dxbc_section_desc *section = &dxbc_desc.sections[i];
if (section->tag == TAG_RDEF)
{
if (found_rdef)
{
FIXME("Multiple RDEF chunks.\n");
continue;
}
if (FAILED(hr = parse_rdef(reflection, &section->data)))
{
vkd3d_shader_free_scan_signature_info(&reflection->signature_info);
vkd3d_shader_free_dxbc(&dxbc_desc);
return hr;
}
found_rdef = true;
}
}
reflection->desc.InputParameters = reflection->signature_info.input.element_count;
reflection->desc.OutputParameters = reflection->signature_info.output.element_count;
reflection->desc.PatchConstantParameters = reflection->signature_info.patch_constant.element_count;
vkd3d_shader_free_dxbc(&dxbc_desc);
return S_OK;
}
HRESULT WINAPI D3DReflect(const void *data, SIZE_T data_size, REFIID iid, void **reflection)