Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- libs/vkd3d-shader/hlsl_codegen.c | 119 +++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 36 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 5f23f345..db340a78 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -264,12 +264,54 @@ static bool fold_redundant_casts(struct hlsl_ctx *ctx, struct hlsl_ir_node *inst return false; }
-static bool split_struct_copies(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) +/* Helper for split_array_copies() and split_struct_copies(). Inserts new + * instructions right before "store". */ +static bool split_copy(struct hlsl_ctx *ctx, struct hlsl_ir_store *store, + const struct hlsl_ir_load *load, const unsigned int offset, struct hlsl_type *type) +{ + struct hlsl_ir_node *offset_instr, *add; + struct hlsl_ir_store *split_store; + struct hlsl_ir_load *split_load; + struct hlsl_ir_constant *c; + + if (!(c = hlsl_new_uint_constant(ctx, offset, store->node.loc))) + return false; + list_add_before(&store->node.entry, &c->node.entry); + + offset_instr = &c->node; + if (load->src.offset.node) + { + if (!(add = hlsl_new_binary_expr(ctx, HLSL_OP2_ADD, load->src.offset.node, &c->node))) + return false; + list_add_before(&store->node.entry, &add->entry); + offset_instr = add; + } + if (!(split_load = hlsl_new_load(ctx, load->src.var, offset_instr, type, store->node.loc))) + return false; + list_add_before(&store->node.entry, &split_load->node.entry); + + offset_instr = &c->node; + if (store->lhs.offset.node) + { + if (!(add = hlsl_new_binary_expr(ctx, HLSL_OP2_ADD, store->lhs.offset.node, &c->node))) + return false; + list_add_before(&store->node.entry, &add->entry); + offset_instr = add; + } + + if (!(split_store = hlsl_new_store(ctx, store->lhs.var, offset_instr, &split_load->node, 0, store->node.loc))) + return false; + list_add_before(&store->node.entry, &split_store->node.entry); + + return true; +} + +static bool split_array_copies(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { - const struct hlsl_struct_field *field; - const struct hlsl_ir_load *rhs_load; const struct hlsl_ir_node *rhs; + struct hlsl_type *element_type; const struct hlsl_type *type; + unsigned int element_size, i; struct hlsl_ir_store *store;
if (instr->type != HLSL_IR_STORE) @@ -278,46 +320,45 @@ static bool split_struct_copies(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr store = hlsl_ir_store(instr); rhs = store->rhs.node; type = rhs->data_type; - if (type->type != HLSL_CLASS_STRUCT) + if (type->type != HLSL_CLASS_ARRAY) return false; + element_type = type->e.array.type; + element_size = element_type->reg_size;
- rhs_load = hlsl_ir_load(rhs); - - LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry) + for (i = 0; i < type->e.array.elements_count; ++i) { - struct hlsl_ir_store *field_store; - struct hlsl_ir_node *offset, *add; - struct hlsl_ir_load *field_load; - struct hlsl_ir_constant *c; - - if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, instr->loc))) + if (!split_copy(ctx, store, hlsl_ir_load(rhs), i * element_size, element_type)) return false; - list_add_before(&instr->entry, &c->node.entry); + }
- offset = &c->node; - if (rhs_load->src.offset.node) - { - if (!(add = hlsl_new_binary_expr(ctx, HLSL_OP2_ADD, rhs_load->src.offset.node, &c->node))) - return false; - list_add_before(&instr->entry, &add->entry); - offset = add; - } - if (!(field_load = hlsl_new_load(ctx, rhs_load->src.var, offset, field->type, instr->loc))) - return false; - list_add_before(&instr->entry, &field_load->node.entry); + /* Remove the store instruction, so that we can split structs which contain + * other structs. Although assignments produce a value, we don't allow + * HLSL_IR_STORE to be used as a source. */ + list_remove(&store->node.entry); + hlsl_free_instr(&store->node); + return true; +}
- offset = &c->node; - if (store->lhs.offset.node) - { - if (!(add = hlsl_new_binary_expr(ctx, HLSL_OP2_ADD, store->lhs.offset.node, &c->node))) - return false; - list_add_before(&instr->entry, &add->entry); - offset = add; - } +static bool split_struct_copies(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) +{ + const struct hlsl_struct_field *field; + const struct hlsl_ir_node *rhs; + const struct hlsl_type *type; + struct hlsl_ir_store *store;
- if (!(field_store = hlsl_new_store(ctx, store->lhs.var, offset, &field_load->node, 0, instr->loc))) + if (instr->type != HLSL_IR_STORE) + return false; + + store = hlsl_ir_store(instr); + rhs = store->rhs.node; + type = rhs->data_type; + if (type->type != HLSL_CLASS_STRUCT) + return false; + + LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry) + { + if (!split_copy(ctx, store, hlsl_ir_load(rhs), field->reg_offset, field->type)) return false; - list_add_before(&instr->entry, &field_store->node.entry); }
/* Remove the store instruction, so that we can split structs which contain @@ -1151,6 +1192,7 @@ struct hlsl_reg hlsl_reg_from_deref(const struct hlsl_deref *deref, const struct int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_func, struct vkd3d_shader_code *out) { struct hlsl_ir_var *var; + bool progress;
list_move_head(entry_func->body, &ctx->static_initializers);
@@ -1197,7 +1239,12 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun }
while (transform_ir(ctx, fold_redundant_casts, entry_func->body, NULL)); - while (transform_ir(ctx, split_struct_copies, entry_func->body, NULL)); + do + { + progress = transform_ir(ctx, split_array_copies, entry_func->body, NULL); + progress |= transform_ir(ctx, split_struct_copies, entry_func->body, NULL); + } + while (progress); while (transform_ir(ctx, fold_constants, entry_func->body, NULL));
if (ctx->profile->major_version < 4)