2017-06-16 13:38:21 -07:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Józef Kucia for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vkd3d_shader_private.h"
|
|
|
|
|
2019-01-31 02:29:27 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-01-31 02:29:29 -08:00
|
|
|
VKD3D_DEBUG_ENV_NAME("VKD3D_SHADER_DEBUG");
|
2019-01-31 02:29:28 -08:00
|
|
|
|
2018-11-08 08:19:32 -08:00
|
|
|
STATIC_ASSERT(MEMBER_SIZE(struct vkd3d_shader_scan_info, uav_counter_mask) * CHAR_BIT >= VKD3D_SHADER_MAX_UNORDERED_ACCESS_VIEWS);
|
|
|
|
STATIC_ASSERT(MEMBER_SIZE(struct vkd3d_shader_scan_info, uav_read_mask) * CHAR_BIT >= VKD3D_SHADER_MAX_UNORDERED_ACCESS_VIEWS);
|
|
|
|
|
2019-01-31 02:29:27 -08:00
|
|
|
static void vkd3d_shader_dump_blob(const char *path, const char *prefix, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
static int shader_id = 0;
|
|
|
|
char filename[1024];
|
|
|
|
unsigned int id;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
id = InterlockedIncrement(&shader_id) - 1;
|
|
|
|
|
|
|
|
snprintf(filename, ARRAY_SIZE(filename), "%s/vkd3d-shader-%s-%u.dxbc", path, prefix, id);
|
|
|
|
if ((f = fopen(filename, "wb")))
|
|
|
|
{
|
|
|
|
if (fwrite(data, 1, size, f) != size)
|
|
|
|
ERR("Failed to write shader to %s.\n", filename);
|
|
|
|
if (fclose(f))
|
|
|
|
ERR("Failed to close stream %s.\n", filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("Failed to open %s for dumping shader.\n", filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_shader_dump_shader(enum vkd3d_shader_type type, const struct vkd3d_shader_code *shader)
|
|
|
|
{
|
|
|
|
static bool enabled = true;
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
if (!enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(path = getenv("VKD3D_SHADER_DUMP_PATH")))
|
|
|
|
{
|
|
|
|
enabled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vkd3d_shader_dump_blob(path, shader_get_type_prefix(type), shader->code, shader->size);
|
|
|
|
}
|
|
|
|
|
2017-08-21 03:41:07 -07:00
|
|
|
struct vkd3d_shader_parser
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-19 09:05:53 -07:00
|
|
|
struct vkd3d_shader_desc shader_desc;
|
2017-08-21 03:41:07 -07:00
|
|
|
struct vkd3d_shader_version shader_version;
|
|
|
|
void *data;
|
2017-06-19 09:05:53 -07:00
|
|
|
const DWORD *ptr;
|
2017-08-21 03:41:07 -07:00
|
|
|
};
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2018-02-15 06:43:52 -08:00
|
|
|
static int vkd3d_shader_parser_init(struct vkd3d_shader_parser *parser,
|
2017-08-21 03:41:07 -07:00
|
|
|
const struct vkd3d_shader_code *dxbc)
|
|
|
|
{
|
|
|
|
struct vkd3d_shader_desc *shader_desc = &parser->shader_desc;
|
2018-02-15 06:43:52 -08:00
|
|
|
int ret;
|
2017-06-19 09:05:53 -07:00
|
|
|
|
2018-02-15 06:43:52 -08:00
|
|
|
if ((ret = shader_extract_from_dxbc(dxbc->code, dxbc->size, shader_desc)) < 0)
|
2017-06-19 09:05:53 -07:00
|
|
|
{
|
2018-02-15 06:43:52 -08:00
|
|
|
WARN("Failed to extract shader, vkd3d result %d.\n", ret);
|
|
|
|
return ret;
|
2017-06-19 09:05:53 -07:00
|
|
|
}
|
|
|
|
|
2017-08-21 03:41:07 -07:00
|
|
|
if (!(parser->data = shader_sm4_init(shader_desc->byte_code,
|
|
|
|
shader_desc->byte_code_size, &shader_desc->output_signature)))
|
2017-06-19 09:05:53 -07:00
|
|
|
{
|
2018-01-16 05:02:21 -08:00
|
|
|
WARN("Failed to initialize shader parser.\n");
|
2017-08-21 03:41:07 -07:00
|
|
|
free_shader_desc(shader_desc);
|
2018-02-15 06:43:52 -08:00
|
|
|
return VKD3D_ERROR_INVALID_ARGUMENT;
|
2017-06-19 09:05:53 -07:00
|
|
|
}
|
|
|
|
|
2017-08-21 03:41:07 -07:00
|
|
|
shader_sm4_read_header(parser->data, &parser->ptr, &parser->shader_version);
|
2018-02-15 06:43:52 -08:00
|
|
|
return VKD3D_OK;
|
2017-08-21 03:41:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_shader_parser_destroy(struct vkd3d_shader_parser *parser)
|
|
|
|
{
|
|
|
|
shader_sm4_free(parser->data);
|
|
|
|
free_shader_desc(&parser->shader_desc);
|
|
|
|
}
|
2017-06-19 09:05:53 -07:00
|
|
|
|
2020-06-19 04:43:36 -07:00
|
|
|
static int vkd3d_shader_validate_compile_info(const struct vkd3d_shader_compile_info *compile_info)
|
|
|
|
{
|
|
|
|
if (compile_info->type != VKD3D_SHADER_STRUCTURE_TYPE_COMPILE_INFO)
|
|
|
|
{
|
|
|
|
WARN("Invalid structure type %#x.\n", compile_info->type);
|
|
|
|
return VKD3D_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (compile_info->source_type)
|
|
|
|
{
|
|
|
|
case VKD3D_SHADER_SOURCE_DXBC_TPF:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN("Invalid shader source type %#x.\n", compile_info->source_type);
|
|
|
|
return VKD3D_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2020-06-23 02:20:12 -07:00
|
|
|
switch (compile_info->target_type)
|
|
|
|
{
|
|
|
|
case VKD3D_SHADER_TARGET_SPIRV_BINARY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN("Invalid shader target type %#x.\n", compile_info->target_type);
|
|
|
|
return VKD3D_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2020-06-19 04:43:36 -07:00
|
|
|
return VKD3D_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-23 02:20:12 -07:00
|
|
|
int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info, struct vkd3d_shader_code *out)
|
2017-08-21 03:41:07 -07:00
|
|
|
{
|
|
|
|
struct vkd3d_shader_instruction instruction;
|
|
|
|
struct vkd3d_dxbc_compiler *spirv_compiler;
|
2017-08-21 03:41:07 -07:00
|
|
|
struct vkd3d_shader_scan_info scan_info;
|
2017-08-21 03:41:07 -07:00
|
|
|
struct vkd3d_shader_parser parser;
|
2018-04-13 03:37:36 -07:00
|
|
|
int ret;
|
2017-08-21 03:41:07 -07:00
|
|
|
|
2020-06-23 02:20:12 -07:00
|
|
|
TRACE("compile_info %p, out %p.\n", compile_info, out);
|
2020-06-19 04:43:32 -07:00
|
|
|
|
2020-06-19 04:43:36 -07:00
|
|
|
if ((ret = vkd3d_shader_validate_compile_info(compile_info)) < 0)
|
|
|
|
return ret;
|
2017-08-21 03:41:07 -07:00
|
|
|
|
2018-10-30 07:22:53 -07:00
|
|
|
scan_info.type = VKD3D_SHADER_STRUCTURE_TYPE_SCAN_INFO;
|
|
|
|
scan_info.next = NULL;
|
2020-06-19 04:43:32 -07:00
|
|
|
if ((ret = vkd3d_shader_scan_dxbc(&compile_info->source, &scan_info)) < 0)
|
2018-02-15 06:43:52 -08:00
|
|
|
return ret;
|
2017-08-21 03:41:07 -07:00
|
|
|
|
2020-06-19 04:43:32 -07:00
|
|
|
if ((ret = vkd3d_shader_parser_init(&parser, &compile_info->source)) < 0)
|
2018-02-15 06:43:52 -08:00
|
|
|
return ret;
|
2017-08-21 03:41:07 -07:00
|
|
|
|
2020-06-19 04:43:32 -07:00
|
|
|
vkd3d_shader_dump_shader(parser.shader_version.type, &compile_info->source);
|
2019-01-31 02:29:27 -08:00
|
|
|
|
2018-05-29 03:50:32 -07:00
|
|
|
if (TRACE_ON())
|
|
|
|
vkd3d_shader_trace(parser.data);
|
|
|
|
|
2017-08-21 03:41:07 -07:00
|
|
|
if (!(spirv_compiler = vkd3d_dxbc_compiler_create(&parser.shader_version,
|
2020-06-19 04:43:35 -07:00
|
|
|
&parser.shader_desc, compile_info, &scan_info)))
|
2017-06-19 09:05:53 -07:00
|
|
|
{
|
|
|
|
ERR("Failed to create DXBC compiler.\n");
|
2017-08-21 03:41:07 -07:00
|
|
|
vkd3d_shader_parser_destroy(&parser);
|
2018-02-15 06:43:52 -08:00
|
|
|
return VKD3D_ERROR;
|
2017-06-19 09:05:53 -07:00
|
|
|
}
|
|
|
|
|
2017-08-21 03:41:07 -07:00
|
|
|
while (!shader_sm4_is_end(parser.data, &parser.ptr))
|
2017-06-19 09:05:53 -07:00
|
|
|
{
|
2017-08-21 03:41:07 -07:00
|
|
|
shader_sm4_read_instruction(parser.data, &parser.ptr, &instruction);
|
2017-06-19 09:05:53 -07:00
|
|
|
|
2019-02-20 04:42:48 -08:00
|
|
|
if (instruction.handler_idx == VKD3DSIH_INVALID)
|
2017-06-19 09:05:53 -07:00
|
|
|
{
|
|
|
|
WARN("Encountered unrecognized or invalid instruction.\n");
|
|
|
|
vkd3d_dxbc_compiler_destroy(spirv_compiler);
|
2017-08-21 03:41:07 -07:00
|
|
|
vkd3d_shader_parser_destroy(&parser);
|
2018-02-15 06:43:52 -08:00
|
|
|
return VKD3D_ERROR_INVALID_ARGUMENT;
|
2017-06-19 09:05:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-26 06:06:53 -07:00
|
|
|
if ((ret = vkd3d_dxbc_compiler_handle_instruction(spirv_compiler, &instruction)) < 0)
|
|
|
|
break;
|
2017-06-19 09:05:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-26 06:06:53 -07:00
|
|
|
if (ret >= 0)
|
2020-06-23 02:20:12 -07:00
|
|
|
ret = vkd3d_dxbc_compiler_generate_spirv(spirv_compiler, out);
|
2018-10-26 06:06:53 -07:00
|
|
|
|
2017-06-19 09:05:53 -07:00
|
|
|
vkd3d_dxbc_compiler_destroy(spirv_compiler);
|
2017-08-21 03:41:07 -07:00
|
|
|
vkd3d_shader_parser_destroy(&parser);
|
2020-06-24 23:18:24 -07:00
|
|
|
vkd3d_shader_free_scan_info(&scan_info);
|
2018-02-15 06:43:52 -08:00
|
|
|
return ret;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 23:18:24 -07:00
|
|
|
struct vkd3d_shader_scan_context
|
|
|
|
{
|
|
|
|
struct vkd3d_shader_scan_info *scan_info;
|
|
|
|
size_t descriptors_size;
|
|
|
|
};
|
|
|
|
|
2017-08-21 03:41:07 -07:00
|
|
|
static bool vkd3d_shader_instruction_is_uav_read(const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
|
|
|
enum VKD3D_SHADER_INSTRUCTION_HANDLER handler_idx = instruction->handler_idx;
|
|
|
|
return (VKD3DSIH_ATOMIC_AND <= handler_idx && handler_idx <= VKD3DSIH_ATOMIC_XOR)
|
2017-09-07 08:48:43 -07:00
|
|
|
|| (VKD3DSIH_IMM_ATOMIC_ALLOC <= handler_idx && handler_idx <= VKD3DSIH_IMM_ATOMIC_XOR)
|
2017-08-21 03:41:07 -07:00
|
|
|
|| handler_idx == VKD3DSIH_LD_UAV_TYPED
|
|
|
|
|| (handler_idx == VKD3DSIH_LD_RAW && instruction->src[1].reg.type == VKD3DSPR_UAV)
|
|
|
|
|| (handler_idx == VKD3DSIH_LD_STRUCTURED && instruction->src[2].reg.type == VKD3DSPR_UAV);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_shader_scan_record_uav_read(struct vkd3d_shader_scan_info *scan_info,
|
|
|
|
const struct vkd3d_shader_register *reg)
|
|
|
|
{
|
2017-09-01 02:27:58 -07:00
|
|
|
assert(reg->idx[0].offset < VKD3D_SHADER_MAX_UNORDERED_ACCESS_VIEWS);
|
2017-08-21 03:41:07 -07:00
|
|
|
scan_info->uav_read_mask |= 1u << reg->idx[0].offset;
|
|
|
|
}
|
|
|
|
|
2017-09-01 02:27:58 -07:00
|
|
|
static bool vkd3d_shader_instruction_is_uav_counter(const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
|
|
|
enum VKD3D_SHADER_INSTRUCTION_HANDLER handler_idx = instruction->handler_idx;
|
|
|
|
return handler_idx == VKD3DSIH_IMM_ATOMIC_ALLOC
|
|
|
|
|| handler_idx == VKD3DSIH_IMM_ATOMIC_CONSUME;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_shader_scan_record_uav_counter(struct vkd3d_shader_scan_info *scan_info,
|
|
|
|
const struct vkd3d_shader_register *reg)
|
|
|
|
{
|
2017-09-01 02:27:58 -07:00
|
|
|
assert(reg->idx[0].offset < VKD3D_SHADER_MAX_UNORDERED_ACCESS_VIEWS);
|
2017-09-01 02:27:58 -07:00
|
|
|
scan_info->uav_counter_mask |= 1u << reg->idx[0].offset;
|
|
|
|
}
|
|
|
|
|
2020-06-24 23:18:24 -07:00
|
|
|
static bool vkd3d_shader_scan_add_descriptor(struct vkd3d_shader_scan_context *context,
|
|
|
|
enum vkd3d_shader_descriptor_type type, unsigned int register_space, unsigned int register_index)
|
|
|
|
{
|
|
|
|
struct vkd3d_shader_scan_info *scan_info = context->scan_info;
|
|
|
|
struct vkd3d_shader_descriptor_info *d;
|
|
|
|
|
|
|
|
if (!vkd3d_array_reserve((void **)&scan_info->descriptors, &context->descriptors_size,
|
|
|
|
scan_info->descriptor_count + 1, sizeof(*scan_info->descriptors)))
|
|
|
|
{
|
|
|
|
ERR("Failed to allocate descriptor info.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
d = &scan_info->descriptors[scan_info->descriptor_count];
|
|
|
|
d->type = type;
|
|
|
|
d->register_space = register_space;
|
|
|
|
d->register_index = register_index;
|
|
|
|
d->count = 1;
|
|
|
|
++scan_info->descriptor_count;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_shader_scan_constant_buffer_declaration(struct vkd3d_shader_scan_context *context,
|
|
|
|
const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_constant_buffer *cb = &instruction->declaration.cb;
|
|
|
|
|
|
|
|
vkd3d_shader_scan_add_descriptor(context, VKD3D_SHADER_DESCRIPTOR_TYPE_CBV,
|
|
|
|
cb->register_space, cb->register_index);
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:59:18 -08:00
|
|
|
static void vkd3d_shader_scan_input_declaration(struct vkd3d_shader_scan_info *scan_info,
|
|
|
|
const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_dst_param *dst = &instruction->declaration.dst;
|
|
|
|
|
|
|
|
if (dst->reg.type == VKD3DSPR_OUTCONTROLPOINT)
|
|
|
|
scan_info->use_vocp = true;
|
|
|
|
}
|
|
|
|
|
2020-06-24 23:18:24 -07:00
|
|
|
static void vkd3d_shader_scan_sampler_declaration(struct vkd3d_shader_scan_context *context,
|
2018-10-21 16:49:15 -07:00
|
|
|
const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
2020-06-24 23:18:24 -07:00
|
|
|
const struct vkd3d_shader_sampler *sampler = &instruction->declaration.sampler;
|
|
|
|
struct vkd3d_shader_scan_info *scan_info = context->scan_info;
|
|
|
|
unsigned int sampler_index = sampler->register_index;
|
|
|
|
|
2018-10-21 16:49:15 -07:00
|
|
|
if (instruction->flags & VKD3DSI_SAMPLER_COMPARISON_MODE)
|
|
|
|
{
|
|
|
|
assert(sampler_index < CHAR_BIT * sizeof(scan_info->sampler_comparison_mode_mask));
|
|
|
|
scan_info->sampler_comparison_mode_mask |= 1u << sampler_index;
|
|
|
|
}
|
2020-06-24 23:18:24 -07:00
|
|
|
|
|
|
|
vkd3d_shader_scan_add_descriptor(context, VKD3D_SHADER_DESCRIPTOR_TYPE_SAMPLER,
|
|
|
|
sampler->register_space, sampler->register_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_shader_scan_resource_declaration(struct vkd3d_shader_scan_context *context,
|
|
|
|
const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_semantic *semantic = &instruction->declaration.semantic;
|
|
|
|
enum vkd3d_shader_descriptor_type type;
|
|
|
|
|
|
|
|
if (semantic->reg.reg.type == VKD3DSPR_UAV)
|
|
|
|
type = VKD3D_SHADER_DESCRIPTOR_TYPE_UAV;
|
|
|
|
else
|
|
|
|
type = VKD3D_SHADER_DESCRIPTOR_TYPE_SRV;
|
|
|
|
vkd3d_shader_scan_add_descriptor(context, type, semantic->register_space, semantic->register_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_shader_scan_resource_declaration_raw(struct vkd3d_shader_scan_context *context,
|
|
|
|
const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_raw_resource *resource = &instruction->declaration.raw_resource;
|
|
|
|
enum vkd3d_shader_descriptor_type type;
|
|
|
|
|
|
|
|
if (resource->dst.reg.type == VKD3DSPR_UAV)
|
|
|
|
type = VKD3D_SHADER_DESCRIPTOR_TYPE_UAV;
|
|
|
|
else
|
|
|
|
type = VKD3D_SHADER_DESCRIPTOR_TYPE_SRV;
|
|
|
|
vkd3d_shader_scan_add_descriptor(context, type, resource->register_space, resource->register_index);
|
2018-10-21 16:49:15 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 23:18:24 -07:00
|
|
|
static void vkd3d_shader_scan_resource_declaration_structured(struct vkd3d_shader_scan_context *context,
|
|
|
|
const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_structured_resource *resource = &instruction->declaration.structured_resource;
|
|
|
|
enum vkd3d_shader_descriptor_type type;
|
|
|
|
|
|
|
|
if (resource->reg.reg.type == VKD3DSPR_UAV)
|
|
|
|
type = VKD3D_SHADER_DESCRIPTOR_TYPE_UAV;
|
|
|
|
else
|
|
|
|
type = VKD3D_SHADER_DESCRIPTOR_TYPE_SRV;
|
|
|
|
vkd3d_shader_scan_add_descriptor(context, type, resource->register_space, resource->register_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *context,
|
2017-08-21 03:41:07 -07:00
|
|
|
const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2019-02-07 00:59:18 -08:00
|
|
|
switch (instruction->handler_idx)
|
|
|
|
{
|
2020-06-24 23:18:24 -07:00
|
|
|
case VKD3DSIH_DCL_CONSTANT_BUFFER:
|
|
|
|
vkd3d_shader_scan_constant_buffer_declaration(context, instruction);
|
|
|
|
break;
|
2019-02-07 00:59:18 -08:00
|
|
|
case VKD3DSIH_DCL_INPUT:
|
2020-06-24 23:18:24 -07:00
|
|
|
vkd3d_shader_scan_input_declaration(context->scan_info, instruction);
|
2019-02-07 00:59:18 -08:00
|
|
|
break;
|
|
|
|
case VKD3DSIH_DCL_SAMPLER:
|
2020-06-24 23:18:24 -07:00
|
|
|
vkd3d_shader_scan_sampler_declaration(context, instruction);
|
|
|
|
break;
|
|
|
|
case VKD3DSIH_DCL:
|
|
|
|
case VKD3DSIH_DCL_UAV_TYPED:
|
|
|
|
vkd3d_shader_scan_resource_declaration(context, instruction);
|
|
|
|
break;
|
|
|
|
case VKD3DSIH_DCL_RESOURCE_RAW:
|
|
|
|
case VKD3DSIH_DCL_UAV_RAW:
|
|
|
|
vkd3d_shader_scan_resource_declaration_raw(context, instruction);
|
|
|
|
break;
|
|
|
|
case VKD3DSIH_DCL_RESOURCE_STRUCTURED:
|
|
|
|
case VKD3DSIH_DCL_UAV_STRUCTURED:
|
|
|
|
vkd3d_shader_scan_resource_declaration_structured(context, instruction);
|
2019-02-07 00:59:18 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-10-21 16:49:15 -07:00
|
|
|
|
2017-08-21 03:41:07 -07:00
|
|
|
if (vkd3d_shader_instruction_is_uav_read(instruction))
|
|
|
|
{
|
|
|
|
for (i = 0; i < instruction->dst_count; ++i)
|
|
|
|
{
|
|
|
|
if (instruction->dst[i].reg.type == VKD3DSPR_UAV)
|
2020-06-24 23:18:24 -07:00
|
|
|
vkd3d_shader_scan_record_uav_read(context->scan_info, &instruction->dst[i].reg);
|
2017-08-21 03:41:07 -07:00
|
|
|
}
|
|
|
|
for (i = 0; i < instruction->src_count; ++i)
|
|
|
|
{
|
|
|
|
if (instruction->src[i].reg.type == VKD3DSPR_UAV)
|
2020-06-24 23:18:24 -07:00
|
|
|
vkd3d_shader_scan_record_uav_read(context->scan_info, &instruction->src[i].reg);
|
2017-08-21 03:41:07 -07:00
|
|
|
}
|
|
|
|
}
|
2017-09-01 02:27:58 -07:00
|
|
|
|
|
|
|
if (vkd3d_shader_instruction_is_uav_counter(instruction))
|
2020-06-24 23:18:24 -07:00
|
|
|
vkd3d_shader_scan_record_uav_counter(context->scan_info, &instruction->src[0].reg);
|
2017-08-21 03:41:07 -07:00
|
|
|
}
|
|
|
|
|
2018-02-15 06:43:52 -08:00
|
|
|
int vkd3d_shader_scan_dxbc(const struct vkd3d_shader_code *dxbc,
|
2017-08-21 03:41:07 -07:00
|
|
|
struct vkd3d_shader_scan_info *scan_info)
|
|
|
|
{
|
|
|
|
struct vkd3d_shader_instruction instruction;
|
2020-06-24 23:18:24 -07:00
|
|
|
struct vkd3d_shader_scan_context context;
|
2017-08-21 03:41:07 -07:00
|
|
|
struct vkd3d_shader_parser parser;
|
2018-02-15 06:43:52 -08:00
|
|
|
int ret;
|
2017-08-21 03:41:07 -07:00
|
|
|
|
|
|
|
TRACE("dxbc {%p, %zu}, scan_info %p.\n", dxbc->code, dxbc->size, scan_info);
|
|
|
|
|
2018-10-30 07:22:53 -07:00
|
|
|
if (scan_info->type != VKD3D_SHADER_STRUCTURE_TYPE_SCAN_INFO)
|
|
|
|
{
|
|
|
|
WARN("Invalid structure type %#x.\n", scan_info->type);
|
|
|
|
return VKD3D_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2018-02-15 06:43:52 -08:00
|
|
|
if ((ret = vkd3d_shader_parser_init(&parser, dxbc)) < 0)
|
|
|
|
return ret;
|
2017-08-21 03:41:07 -07:00
|
|
|
|
|
|
|
memset(scan_info, 0, sizeof(*scan_info));
|
|
|
|
|
2020-06-24 23:18:24 -07:00
|
|
|
memset(&context, 0, sizeof(context));
|
|
|
|
context.scan_info = scan_info;
|
|
|
|
|
2017-08-21 03:41:07 -07:00
|
|
|
while (!shader_sm4_is_end(parser.data, &parser.ptr))
|
|
|
|
{
|
|
|
|
shader_sm4_read_instruction(parser.data, &parser.ptr, &instruction);
|
|
|
|
|
2019-02-20 04:42:48 -08:00
|
|
|
if (instruction.handler_idx == VKD3DSIH_INVALID)
|
2017-08-21 03:41:07 -07:00
|
|
|
{
|
|
|
|
WARN("Encountered unrecognized or invalid instruction.\n");
|
2020-06-24 23:18:24 -07:00
|
|
|
vkd3d_shader_free_scan_info(scan_info);
|
2017-08-21 03:41:07 -07:00
|
|
|
vkd3d_shader_parser_destroy(&parser);
|
2018-02-15 06:43:52 -08:00
|
|
|
return VKD3D_ERROR_INVALID_ARGUMENT;
|
2017-08-21 03:41:07 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 23:18:24 -07:00
|
|
|
vkd3d_shader_scan_instruction(&context, &instruction);
|
2017-08-21 03:41:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
vkd3d_shader_parser_destroy(&parser);
|
2018-02-15 06:43:52 -08:00
|
|
|
return VKD3D_OK;
|
2017-08-21 03:41:07 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 23:18:24 -07:00
|
|
|
void vkd3d_shader_free_scan_info(struct vkd3d_shader_scan_info *scan_info)
|
|
|
|
{
|
|
|
|
if (!scan_info)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vkd3d_free(scan_info->descriptors);
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
void vkd3d_shader_free_shader_code(struct vkd3d_shader_code *shader_code)
|
|
|
|
{
|
|
|
|
if (!shader_code)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vkd3d_free((void *)shader_code->code);
|
|
|
|
}
|
2017-06-27 04:16:47 -07:00
|
|
|
|
2019-04-24 07:05:48 -07:00
|
|
|
static void vkd3d_shader_free_root_signature_v_1_0(struct vkd3d_root_signature_desc *root_signature)
|
2017-06-27 04:16:47 -07:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2018-02-15 06:43:51 -08:00
|
|
|
for (i = 0; i < root_signature->parameter_count; ++i)
|
2017-06-27 04:16:47 -07:00
|
|
|
{
|
2018-02-15 06:43:51 -08:00
|
|
|
const struct vkd3d_root_parameter *parameter = &root_signature->parameters[i];
|
2017-06-27 04:16:47 -07:00
|
|
|
|
2018-02-15 06:43:51 -08:00
|
|
|
if (parameter->parameter_type == VKD3D_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE)
|
|
|
|
vkd3d_free((void *)parameter->u.descriptor_table.descriptor_ranges);
|
2017-06-27 04:16:47 -07:00
|
|
|
}
|
2018-02-15 06:43:51 -08:00
|
|
|
vkd3d_free((void *)root_signature->parameters);
|
|
|
|
vkd3d_free((void *)root_signature->static_samplers);
|
2017-06-27 04:16:47 -07:00
|
|
|
|
|
|
|
memset(root_signature, 0, sizeof(*root_signature));
|
|
|
|
}
|
2017-09-11 13:35:16 -07:00
|
|
|
|
2019-04-24 07:05:48 -07:00
|
|
|
static void vkd3d_shader_free_root_signature_v_1_1(struct vkd3d_root_signature_desc1 *root_signature)
|
2019-04-19 02:07:31 -07:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < root_signature->parameter_count; ++i)
|
|
|
|
{
|
|
|
|
const struct vkd3d_root_parameter1 *parameter = &root_signature->parameters[i];
|
|
|
|
|
|
|
|
if (parameter->parameter_type == VKD3D_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE)
|
|
|
|
vkd3d_free((void *)parameter->u.descriptor_table.descriptor_ranges);
|
|
|
|
}
|
|
|
|
vkd3d_free((void *)root_signature->parameters);
|
|
|
|
vkd3d_free((void *)root_signature->static_samplers);
|
|
|
|
|
|
|
|
memset(root_signature, 0, sizeof(*root_signature));
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:05:48 -07:00
|
|
|
void vkd3d_shader_free_root_signature(struct vkd3d_versioned_root_signature_desc *desc)
|
|
|
|
{
|
|
|
|
if (desc->version == VKD3D_ROOT_SIGNATURE_VERSION_1_0)
|
|
|
|
{
|
|
|
|
vkd3d_shader_free_root_signature_v_1_0(&desc->u.v_1_0);
|
|
|
|
}
|
|
|
|
else if (desc->version == VKD3D_ROOT_SIGNATURE_VERSION_1_1)
|
|
|
|
{
|
|
|
|
vkd3d_shader_free_root_signature_v_1_1(&desc->u.v_1_1);
|
|
|
|
}
|
|
|
|
else if (desc->version)
|
|
|
|
{
|
|
|
|
FIXME("Unknown version %#x.\n", desc->version);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc->version = 0;
|
|
|
|
}
|
|
|
|
|
2018-02-15 06:43:52 -08:00
|
|
|
int vkd3d_shader_parse_input_signature(const struct vkd3d_shader_code *dxbc,
|
2017-09-11 13:35:16 -07:00
|
|
|
struct vkd3d_shader_signature *signature)
|
|
|
|
{
|
|
|
|
TRACE("dxbc {%p, %zu}, signature %p.\n", dxbc->code, dxbc->size, signature);
|
|
|
|
|
|
|
|
return shader_parse_input_signature(dxbc->code, dxbc->size, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct vkd3d_shader_signature_element *vkd3d_shader_find_signature_element(
|
|
|
|
const struct vkd3d_shader_signature *signature, const char *semantic_name,
|
|
|
|
unsigned int semantic_index, unsigned int stream_index)
|
|
|
|
{
|
2018-04-13 03:37:38 -07:00
|
|
|
struct vkd3d_shader_signature_element *e;
|
2017-09-11 13:35:16 -07:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
TRACE("signature %p, semantic_name %s, semantic_index %u, stream_index %u.\n",
|
|
|
|
signature, debugstr_a(semantic_name), semantic_index, stream_index);
|
|
|
|
|
|
|
|
e = signature->elements;
|
|
|
|
for (i = 0; i < signature->element_count; ++i)
|
|
|
|
{
|
2019-04-30 05:33:48 -07:00
|
|
|
if (!ascii_strcasecmp(e[i].semantic_name, semantic_name)
|
2017-09-11 13:35:16 -07:00
|
|
|
&& e[i].semantic_index == semantic_index
|
|
|
|
&& e[i].stream_index == stream_index)
|
|
|
|
return &e[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vkd3d_shader_free_shader_signature(struct vkd3d_shader_signature *signature)
|
|
|
|
{
|
|
|
|
TRACE("signature %p.\n", signature);
|
|
|
|
|
|
|
|
vkd3d_free(signature->elements);
|
|
|
|
signature->elements = NULL;
|
|
|
|
}
|