From: Conor McCarthy cmccarthy@codeweavers.com
--- libs/vkd3d-shader/dxil.c | 88 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 7 deletions(-)
diff --git a/libs/vkd3d-shader/dxil.c b/libs/vkd3d-shader/dxil.c index 65971a62..3aaff0de 100644 --- a/libs/vkd3d-shader/dxil.c +++ b/libs/vkd3d-shader/dxil.c @@ -101,6 +101,10 @@ struct sm6_parser struct sm6_block root_block; struct sm6_block *current_block;
+ struct sm6_global_abbrev **abbrevs; + size_t abbrev_capacity; + size_t abbrev_count; + struct vkd3d_shader_parser p; };
@@ -117,6 +121,12 @@ struct sm6_abbrev struct sm6_abbrev_operand operands[]; };
+struct sm6_global_abbrev +{ + unsigned int block_id; + struct sm6_abbrev abbrev; +}; + static struct sm6_parser *sm6_parser(struct vkd3d_shader_parser *parser) { return CONTAINING_RECORD(parser, struct sm6_parser, p); @@ -396,6 +406,35 @@ static enum vkd3d_result sm6_abbrev_init(struct sm6_abbrev *abbrev, unsigned int return sm6->p.failed ? VKD3D_ERROR_INVALID_SHADER : VKD3D_OK; }
+static enum vkd3d_result sm6_add_global_abbrev(struct sm6_parser *sm6) +{ + struct sm6_block *block = sm6->current_block; + unsigned int count = sm6_read_vbr(sm6, 5); + struct sm6_global_abbrev *global_abbrev; + enum vkd3d_result ret; + + assert(block->id == BLOCKINFO_BLOCK); + + if (!vkd3d_array_reserve((void **)&sm6->abbrevs, &sm6->abbrev_capacity, sm6->abbrev_count + 1, sizeof(*sm6->abbrevs)) + || !(global_abbrev = vkd3d_malloc(sizeof(*global_abbrev) + count * sizeof(global_abbrev->abbrev.operands[0])))) + { + ERR("Failed to allocate global abbreviation.\n"); + return VKD3D_ERROR_OUT_OF_MEMORY; + } + + if ((ret = sm6_abbrev_init(&global_abbrev->abbrev, count, sm6)) < 0) + { + vkd3d_free(global_abbrev); + return ret; + } + + global_abbrev->block_id = block->blockinfo_bid; + + sm6->abbrevs[sm6->abbrev_count++] = global_abbrev; + + return VKD3D_OK; +} + static enum vkd3d_result sm6_add_block_abbrev(struct sm6_parser *sm6) { struct sm6_block *block = sm6->current_block; @@ -404,10 +443,7 @@ static enum vkd3d_result sm6_add_block_abbrev(struct sm6_parser *sm6) unsigned int count;
if (block->id == BLOCKINFO_BLOCK) - { - FIXME("Unhandled global abbreviation.\n"); - return VKD3D_ERROR_INVALID_SHADER; - } + return sm6_add_global_abbrev(sm6);
count = sm6_read_vbr(sm6, 5); if (!vkd3d_array_reserve((void **)&block->abbrevs, &block->abbrev_capacity, block->abbrev_count + 1, sizeof(*block->abbrevs)) @@ -567,11 +603,22 @@ static enum vkd3d_result sm6_block_read(struct sm6_block *parent, struct sm6_par return VKD3D_ERROR_INVALID_SHADER; }
+static unsigned int sm6_compute_global_abbrev_count_for_block_id(struct sm6_parser *sm6, + unsigned int block_id) +{ + unsigned int i, count; + + for (i = 0, count = 0; i < sm6->abbrev_count; ++i) + count += sm6->abbrevs[i]->block_id == block_id; + + return count; +} + static enum vkd3d_result sm6_block_init(struct sm6_block *block, const struct sm6_block *parent, struct sm6_parser *sm6) { + unsigned int i, abbrev_count = 0; enum vkd3d_result ret; - unsigned int i;
block->parent = parent; block->level = parent ? parent->level + 1 : 0; @@ -582,11 +629,26 @@ static enum vkd3d_result sm6_block_init(struct sm6_block *block, const struct sm block->start = sm6->ptr - sm6->start; block->abbrevs = NULL; block->abbrev_capacity = 0; - block->abbrev_count = 0;
if (sm6->p.failed) return VKD3D_ERROR_INVALID_SHADER;
+ if ((block->abbrev_count = sm6_compute_global_abbrev_count_for_block_id(sm6, block->id))) + { + if (!vkd3d_array_reserve((void **)&block->abbrevs, &block->abbrev_capacity, + block->abbrev_count, sizeof(*block->abbrevs))) + { + ERR("Failed to allocate block abbreviations.\n"); + return VKD3D_ERROR_OUT_OF_MEMORY; + } + + for (i = 0; i < sm6->abbrev_count; ++i) + if (sm6->abbrevs[i]->block_id == block->id) + block->abbrevs[abbrev_count++] = &sm6->abbrevs[i]->abbrev; + + assert(abbrev_count == block->abbrev_count); + } + block->child_blocks = NULL; block->child_block_capacity = 0; block->child_block_count = 0; @@ -596,7 +658,7 @@ static enum vkd3d_result sm6_block_init(struct sm6_block *block, const struct sm
ret = sm6_block_read(block, sm6);
- for (i = 0; i < block->abbrev_count; ++i) + for (i = abbrev_count; i < block->abbrev_count; ++i) vkd3d_free(block->abbrevs[i]); vkd3d_free(block->abbrevs); block->abbrevs = NULL; @@ -605,6 +667,15 @@ static enum vkd3d_result sm6_block_init(struct sm6_block *block, const struct sm return ret; }
+static void sm6_global_abbrevs_cleanup(struct sm6_global_abbrev **abbrevs, unsigned int count) +{ + unsigned int i; + + for (i = 0; i < count; ++i) + vkd3d_free(abbrevs[i]); + vkd3d_free(abbrevs); +} + static void sm6_block_destroy(struct sm6_block *block) { unsigned int i; @@ -714,6 +785,7 @@ static enum vkd3d_result sm6_init(struct sm6_parser *sm6, const uint32_t *byte_c return VKD3D_ERROR_INVALID_SHADER; }
+ /* Estimate instruction count to avoid reallocation in most shaders. */ count = max(token_count, 400) - 400; vkd3d_shader_parser_init(&sm6->p, message_context, source_name, &version, &sm6_parser_ops, (count + (count >> 2)) / 2u + 10); @@ -734,6 +806,8 @@ static enum vkd3d_result sm6_init(struct sm6_parser *sm6, const uint32_t *byte_c return ret; }
+ sm6_global_abbrevs_cleanup(sm6->abbrevs, sm6->abbrev_count); + length = sm6->ptr - sm6->start - block->start; if (length != block->length) {