To automatically put the compilation context in a failed state.
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- libs/vkd3d-shader/hlsl.c | 102 ++++++++++----------- libs/vkd3d-shader/hlsl.h | 57 +++++++++--- libs/vkd3d-shader/hlsl.l | 5 +- libs/vkd3d-shader/hlsl.y | 137 ++++++++++++++-------------- libs/vkd3d-shader/hlsl_codegen.c | 147 +++++++++++-------------------- 5 files changed, 216 insertions(+), 232 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index e81efa2d..86ef1247 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -103,7 +103,7 @@ struct hlsl_type *hlsl_new_type(struct hlsl_ctx *ctx, const char *name, enum hls { struct hlsl_type *type;
- if (!(type = vkd3d_calloc(1, sizeof(*type)))) + if (!(type = hlsl_alloc(ctx, sizeof(*type)))) return NULL; type->name = name; type->type = type_class; @@ -149,7 +149,7 @@ struct hlsl_type *hlsl_new_struct_type(struct hlsl_ctx *ctx, const char *name, s unsigned int reg_size = 0; struct hlsl_type *type;
- if (!(type = vkd3d_calloc(1, sizeof(*type)))) + if (!(type = hlsl_alloc(ctx, sizeof(*type)))) return NULL; type->type = HLSL_CLASS_STRUCT; type->base_type = HLSL_TYPE_VOID; @@ -281,12 +281,12 @@ struct hlsl_type *hlsl_type_clone(struct hlsl_ctx *ctx, struct hlsl_type *old, u struct hlsl_struct_field *old_field, *field; struct hlsl_type *type;
- if (!(type = vkd3d_calloc(1, sizeof(*type)))) + if (!(type = hlsl_alloc(ctx, sizeof(*type)))) return NULL;
if (old->name) { - type->name = vkd3d_strdup(old->name); + type->name = hlsl_strdup(ctx, old->name); if (!type->name) { vkd3d_free(type); @@ -313,7 +313,7 @@ struct hlsl_type *hlsl_type_clone(struct hlsl_ctx *ctx, struct hlsl_type *old, u { unsigned int reg_size = 0;
- if (!(type->e.elements = vkd3d_malloc(sizeof(*type->e.elements)))) + if (!(type->e.elements = hlsl_alloc(ctx, sizeof(*type->e.elements)))) { vkd3d_free((void *)type->name); vkd3d_free(type); @@ -322,7 +322,7 @@ struct hlsl_type *hlsl_type_clone(struct hlsl_ctx *ctx, struct hlsl_type *old, u list_init(type->e.elements); LIST_FOR_EACH_ENTRY(old_field, old->e.elements, struct hlsl_struct_field, entry) { - if (!(field = vkd3d_calloc(1, sizeof(*field)))) + if (!(field = hlsl_alloc(ctx, sizeof(*field)))) { LIST_FOR_EACH_ENTRY_SAFE(field, old_field, type->e.elements, struct hlsl_struct_field, entry) { @@ -337,10 +337,10 @@ struct hlsl_type *hlsl_type_clone(struct hlsl_ctx *ctx, struct hlsl_type *old, u } field->loc = old_field->loc; field->type = hlsl_type_clone(ctx, old_field->type, default_majority); - field->name = vkd3d_strdup(old_field->name); + field->name = hlsl_strdup(ctx, old_field->name); if (old_field->semantic.name) { - field->semantic.name = vkd3d_strdup(old_field->semantic.name); + field->semantic.name = hlsl_strdup(ctx, old_field->semantic.name); field->semantic.index = old_field->semantic.index; } field->reg_offset = reg_size; @@ -373,30 +373,30 @@ bool hlsl_scope_add_type(struct hlsl_scope *scope, struct hlsl_type *type) return true; }
-struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ir_node *node, struct hlsl_type *type, +struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type, struct vkd3d_shader_location *loc) { struct hlsl_ir_node *cast;
- cast = hlsl_new_unary_expr(HLSL_IR_UNOP_CAST, node, *loc); + cast = hlsl_new_unary_expr(ctx, HLSL_IR_UNOP_CAST, node, *loc); if (cast) cast->data_type = type; return hlsl_ir_expr(cast); }
-struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ir_node *node) +struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node) { /* Use a cast to the same type as a makeshift identity expression. */ - return hlsl_new_cast(node, node->data_type, &node->loc); + return hlsl_new_cast(ctx, node, node->data_type, &node->loc); }
-struct hlsl_ir_var *hlsl_new_var(const char *name, struct hlsl_type *type, +struct hlsl_ir_var *hlsl_new_var(struct hlsl_ctx *ctx, const char *name, struct hlsl_type *type, const struct vkd3d_shader_location loc, const struct hlsl_semantic *semantic, unsigned int modifiers, const struct hlsl_reg_reservation *reg_reservation) { struct hlsl_ir_var *var;
- if (!(var = vkd3d_calloc(1, sizeof(*var)))) + if (!(var = hlsl_alloc(ctx, sizeof(*var)))) return NULL;
var->name = name; @@ -412,7 +412,7 @@ struct hlsl_ir_var *hlsl_new_var(const char *name, struct hlsl_type *type, struct hlsl_ir_var *hlsl_new_synthetic_var(struct hlsl_ctx *ctx, const char *name, struct hlsl_type *type, const struct vkd3d_shader_location loc) { - struct hlsl_ir_var *var = hlsl_new_var(vkd3d_strdup(name), type, loc, NULL, 0, NULL); + struct hlsl_ir_var *var = hlsl_new_var(ctx, hlsl_strdup(ctx, name), type, loc, NULL, 0, NULL);
if (var) list_add_tail(&ctx->globals->vars, &var->scope_entry); @@ -424,7 +424,7 @@ static bool type_is_single_reg(const struct hlsl_type *type) return type->type == HLSL_CLASS_SCALAR || type->type == HLSL_CLASS_VECTOR; }
-struct hlsl_ir_store *hlsl_new_store(struct hlsl_ir_var *var, struct hlsl_ir_node *offset, +struct hlsl_ir_store *hlsl_new_store(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, struct hlsl_ir_node *offset, struct hlsl_ir_node *rhs, unsigned int writemask, struct vkd3d_shader_location loc) { struct hlsl_ir_store *store; @@ -432,7 +432,7 @@ struct hlsl_ir_store *hlsl_new_store(struct hlsl_ir_var *var, struct hlsl_ir_nod if (!writemask && type_is_single_reg(rhs->data_type)) writemask = (1 << rhs->data_type->dimx) - 1;
- if (!(store = vkd3d_malloc(sizeof(*store)))) + if (!(store = hlsl_alloc(ctx, sizeof(*store)))) return NULL;
init_node(&store->node, HLSL_IR_STORE, NULL, loc); @@ -443,9 +443,9 @@ struct hlsl_ir_store *hlsl_new_store(struct hlsl_ir_var *var, struct hlsl_ir_nod return store; }
-struct hlsl_ir_store *hlsl_new_simple_store(struct hlsl_ir_var *lhs, struct hlsl_ir_node *rhs) +struct hlsl_ir_store *hlsl_new_simple_store(struct hlsl_ctx *ctx, struct hlsl_ir_var *lhs, struct hlsl_ir_node *rhs) { - return hlsl_new_store(lhs, NULL, rhs, 0, rhs->loc); + return hlsl_new_store(ctx, lhs, NULL, rhs, 0, rhs->loc); }
struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned int n, @@ -453,19 +453,19 @@ struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned i { struct hlsl_ir_constant *c;
- if (!(c = vkd3d_malloc(sizeof(*c)))) + if (!(c = hlsl_alloc(ctx, sizeof(*c)))) return NULL; init_node(&c->node, HLSL_IR_CONSTANT, ctx->builtin_types.scalar[HLSL_TYPE_UINT], loc); c->value.u[0] = n; return c; }
-struct hlsl_ir_node *hlsl_new_unary_expr(enum hlsl_ir_expr_op op, +struct hlsl_ir_node *hlsl_new_unary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg, struct vkd3d_shader_location loc) { struct hlsl_ir_expr *expr;
- if (!(expr = vkd3d_calloc(1, sizeof(*expr)))) + if (!(expr = hlsl_alloc(ctx, sizeof(*expr)))) return NULL; init_node(&expr->node, HLSL_IR_EXPR, arg->data_type, loc); expr->op = op; @@ -473,13 +473,14 @@ struct hlsl_ir_node *hlsl_new_unary_expr(enum hlsl_ir_expr_op op, return &expr->node; }
-struct hlsl_ir_node *hlsl_new_binary_expr(enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg1, struct hlsl_ir_node *arg2) +struct hlsl_ir_node *hlsl_new_binary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, + struct hlsl_ir_node *arg1, struct hlsl_ir_node *arg2) { struct hlsl_ir_expr *expr;
assert(hlsl_types_are_equal(arg1->data_type, arg2->data_type));
- if (!(expr = vkd3d_calloc(1, sizeof(*expr)))) + if (!(expr = hlsl_alloc(ctx, sizeof(*expr)))) return NULL; init_node(&expr->node, HLSL_IR_EXPR, arg1->data_type, arg1->loc); expr->op = op; @@ -488,11 +489,11 @@ struct hlsl_ir_node *hlsl_new_binary_expr(enum hlsl_ir_expr_op op, struct hlsl_i return &expr->node; }
-struct hlsl_ir_if *hlsl_new_if(struct hlsl_ir_node *condition, struct vkd3d_shader_location loc) +struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, struct vkd3d_shader_location loc) { struct hlsl_ir_if *iff;
- if (!(iff = vkd3d_malloc(sizeof(*iff)))) + if (!(iff = hlsl_alloc(ctx, sizeof(*iff)))) return NULL; init_node(&iff->node, HLSL_IR_IF, NULL, loc); hlsl_src_from_node(&iff->condition, condition); @@ -501,12 +502,12 @@ struct hlsl_ir_if *hlsl_new_if(struct hlsl_ir_node *condition, struct vkd3d_shad return iff; }
-struct hlsl_ir_load *hlsl_new_load(struct hlsl_ir_var *var, struct hlsl_ir_node *offset, +struct hlsl_ir_load *hlsl_new_load(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, struct hlsl_ir_node *offset, struct hlsl_type *type, const struct vkd3d_shader_location loc) { struct hlsl_ir_load *load;
- if (!(load = vkd3d_calloc(1, sizeof(*load)))) + if (!(load = hlsl_alloc(ctx, sizeof(*load)))) return NULL; init_node(&load->node, HLSL_IR_LOAD, type, loc); load->src.var = var; @@ -514,9 +515,10 @@ struct hlsl_ir_load *hlsl_new_load(struct hlsl_ir_var *var, struct hlsl_ir_node return load; }
-struct hlsl_ir_load *hlsl_new_var_load(struct hlsl_ir_var *var, const struct vkd3d_shader_location loc) +struct hlsl_ir_load *hlsl_new_var_load(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, + const struct vkd3d_shader_location loc) { - return hlsl_new_load(var, NULL, var->data_type, loc); + return hlsl_new_load(ctx, var, NULL, var->data_type, loc); }
struct hlsl_ir_swizzle *hlsl_new_swizzle(struct hlsl_ctx *ctx, DWORD s, unsigned int components, @@ -524,7 +526,7 @@ struct hlsl_ir_swizzle *hlsl_new_swizzle(struct hlsl_ctx *ctx, DWORD s, unsigned { struct hlsl_ir_swizzle *swizzle;
- if (!(swizzle = vkd3d_malloc(sizeof(*swizzle)))) + if (!(swizzle = hlsl_alloc(ctx, sizeof(*swizzle)))) return NULL; init_node(&swizzle->node, HLSL_IR_SWIZZLE, hlsl_new_type(ctx, NULL, HLSL_CLASS_VECTOR, val->data_type->base_type, components, 1), *loc); @@ -533,22 +535,22 @@ struct hlsl_ir_swizzle *hlsl_new_swizzle(struct hlsl_ctx *ctx, DWORD s, unsigned return swizzle; }
-struct hlsl_ir_jump *hlsl_new_jump(enum hlsl_ir_jump_type type, struct vkd3d_shader_location loc) +struct hlsl_ir_jump *hlsl_new_jump(struct hlsl_ctx *ctx, enum hlsl_ir_jump_type type, struct vkd3d_shader_location loc) { struct hlsl_ir_jump *jump;
- if (!(jump = vkd3d_malloc(sizeof(*jump)))) + if (!(jump = hlsl_alloc(ctx, sizeof(*jump)))) return NULL; init_node(&jump->node, HLSL_IR_JUMP, NULL, loc); jump->type = type; return jump; }
-struct hlsl_ir_loop *hlsl_new_loop(struct vkd3d_shader_location loc) +struct hlsl_ir_loop *hlsl_new_loop(struct hlsl_ctx *ctx, struct vkd3d_shader_location loc) { struct hlsl_ir_loop *loop;
- if (!(loop = vkd3d_calloc(1, sizeof(*loop)))) + if (!(loop = hlsl_alloc(ctx, sizeof(*loop)))) return NULL; init_node(&loop->node, HLSL_IR_LOOP, NULL, loc); list_init(&loop->body); @@ -565,7 +567,7 @@ struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hl { struct hlsl_ir_function_decl *decl;
- if (!(decl = vkd3d_calloc(1, sizeof(*decl)))) + if (!(decl = hlsl_alloc(ctx, sizeof(*decl)))) return NULL; decl->return_type = return_type; decl->parameters = parameters; @@ -577,7 +579,7 @@ struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hl char name[28];
sprintf(name, "<retval-%p>", decl); - if (!(return_var = hlsl_new_var(vkd3d_strdup(name), return_type, loc, semantic, 0, NULL))) + if (!(return_var = hlsl_new_var(ctx, hlsl_strdup(ctx, name), return_type, loc, semantic, 0, NULL))) { vkd3d_free(decl); return NULL; @@ -609,7 +611,7 @@ void hlsl_push_scope(struct hlsl_ctx *ctx) { struct hlsl_scope *new_scope;
- if (!(new_scope = vkd3d_malloc(sizeof(*new_scope)))) + if (!(new_scope = hlsl_alloc(ctx, sizeof(*new_scope)))) return; TRACE("Pushing a new scope.\n"); list_init(&new_scope->vars); @@ -1333,12 +1335,12 @@ static void free_function_rb(struct rb_entry *entry, void *context) free_function(RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry)); }
-void hlsl_add_function(struct rb_tree *funcs, char *name, struct hlsl_ir_function_decl *decl, bool intrinsic) +void hlsl_add_function(struct hlsl_ctx *ctx, char *name, struct hlsl_ir_function_decl *decl, bool intrinsic) { struct hlsl_ir_function *func; struct rb_entry *func_entry, *old_entry;
- func_entry = rb_get(funcs, name); + func_entry = rb_get(&ctx->functions, name); if (func_entry) { func = RB_ENTRY_VALUE(func_entry, struct hlsl_ir_function, entry); @@ -1372,13 +1374,13 @@ void hlsl_add_function(struct rb_tree *funcs, char *name, struct hlsl_ir_functio vkd3d_free(name); return; } - func = vkd3d_malloc(sizeof(*func)); + func = hlsl_alloc(ctx, sizeof(*func)); func->name = name; rb_init(&func->overloads, compare_function_decl_rb); decl->func = func; rb_put(&func->overloads, decl->parameters, &decl->entry); func->intrinsic = intrinsic; - rb_put(funcs, func->name, &func->entry); + rb_put(&ctx->functions, func->name, &func->entry); }
static const struct hlsl_profile_info *get_target_info(const char *target) @@ -1517,20 +1519,20 @@ static void declare_predefined_types(struct hlsl_ctx *ctx) for (x = 1; x <= 4; ++x) { sprintf(name, "%s%ux%u", names[bt], y, x); - type = hlsl_new_type(ctx, vkd3d_strdup(name), HLSL_CLASS_MATRIX, bt, x, y); + type = hlsl_new_type(ctx, hlsl_strdup(ctx, name), HLSL_CLASS_MATRIX, bt, x, y); hlsl_scope_add_type(ctx->globals, type);
if (y == 1) { sprintf(name, "%s%u", names[bt], x); - type = hlsl_new_type(ctx, vkd3d_strdup(name), HLSL_CLASS_VECTOR, bt, x, y); + type = hlsl_new_type(ctx, hlsl_strdup(ctx, name), HLSL_CLASS_VECTOR, bt, x, y); hlsl_scope_add_type(ctx->globals, type); ctx->builtin_types.vector[bt][x - 1] = type;
if (x == 1) { sprintf(name, "%s", names[bt]); - type = hlsl_new_type(ctx, vkd3d_strdup(name), HLSL_CLASS_SCALAR, bt, x, y); + type = hlsl_new_type(ctx, hlsl_strdup(ctx, name), HLSL_CLASS_SCALAR, bt, x, y); hlsl_scope_add_type(ctx->globals, type); ctx->builtin_types.scalar[bt] = type; } @@ -1541,16 +1543,16 @@ static void declare_predefined_types(struct hlsl_ctx *ctx)
for (bt = 0; bt <= HLSL_SAMPLER_DIM_MAX; ++bt) { - type = hlsl_new_type(ctx, vkd3d_strdup(sampler_names[bt]), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1); + type = hlsl_new_type(ctx, hlsl_strdup(ctx, sampler_names[bt]), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1); type->sampler_dim = bt; ctx->builtin_types.sampler[bt] = type; }
- ctx->builtin_types.Void = hlsl_new_type(ctx, vkd3d_strdup("void"), HLSL_CLASS_OBJECT, HLSL_TYPE_VOID, 1, 1); + ctx->builtin_types.Void = hlsl_new_type(ctx, hlsl_strdup(ctx, "void"), HLSL_CLASS_OBJECT, HLSL_TYPE_VOID, 1, 1);
for (i = 0; i < ARRAY_SIZE(effect_types); ++i) { - type = hlsl_new_type(ctx, vkd3d_strdup(effect_types[i].name), effect_types[i].class, + type = hlsl_new_type(ctx, hlsl_strdup(ctx, effect_types[i].name), effect_types[i].class, effect_types[i].base_type, effect_types[i].dimx, effect_types[i].dimy); hlsl_scope_add_type(ctx->globals, type); } @@ -1565,9 +1567,9 @@ static bool hlsl_ctx_init(struct hlsl_ctx *ctx, const struct hlsl_profile_info *
ctx->message_context = message_context;
- if (!(ctx->source_files = vkd3d_malloc(sizeof(*ctx->source_files)))) + if (!(ctx->source_files = hlsl_alloc(ctx, sizeof(*ctx->source_files)))) return false; - if (!(ctx->source_files[0] = vkd3d_strdup(""))) + if (!(ctx->source_files[0] = hlsl_strdup(ctx, ""))) { vkd3d_free(ctx->source_files); return false; diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 9081ef37..3e7d2445 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -539,6 +539,34 @@ static inline void hlsl_src_remove(struct hlsl_src *src) src->node = NULL; }
+static inline void *hlsl_alloc(struct hlsl_ctx *ctx, size_t size) +{ + void *ptr = vkd3d_calloc(1, size); + + if (!ptr) + ctx->failed = true; + return ptr; +} + +static inline char *hlsl_strdup(struct hlsl_ctx *ctx, const char *string) +{ + char *ptr = vkd3d_strdup(string); + + if (!ptr) + ctx->failed = true; + return ptr; +} + +static inline bool hlsl_array_reserve(struct hlsl_ctx *ctx, void **elements, + size_t *capacity, size_t element_count, size_t element_size) +{ + bool ret = vkd3d_array_reserve(elements, capacity, element_count, element_size); + + if (!ret) + ctx->failed = true; + return ret; +} + const char *debug_hlsl_type(const struct hlsl_type *type) DECLSPEC_HIDDEN; const char *debug_hlsl_writemask(unsigned int writemask) DECLSPEC_HIDDEN;
@@ -548,7 +576,7 @@ struct vkd3d_string_buffer *hlsl_modifiers_to_string(struct vkd3d_string_buffer_ unsigned int modifiers) DECLSPEC_HIDDEN; const char *hlsl_node_type_to_string(enum hlsl_ir_node_type type) DECLSPEC_HIDDEN;
-void hlsl_add_function(struct rb_tree *funcs, char *name, struct hlsl_ir_function_decl *decl, +void hlsl_add_function(struct hlsl_ctx *ctx, char *name, struct hlsl_ir_function_decl *decl, bool intrinsic) DECLSPEC_HIDDEN; bool hlsl_add_var(struct hlsl_ctx *ctx, struct hlsl_ir_var *decl, bool local_var) DECLSPEC_HIDDEN;
@@ -569,22 +597,24 @@ struct hlsl_ir_var *hlsl_get_var(struct hlsl_scope *scope, const char *name) DEC
struct hlsl_type *hlsl_new_array_type(struct hlsl_ctx *ctx, struct hlsl_type *basic_type, unsigned int array_size) DECLSPEC_HIDDEN; -struct hlsl_ir_node *hlsl_new_binary_expr(enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg1, +struct hlsl_ir_node *hlsl_new_binary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg1, struct hlsl_ir_node *arg2) DECLSPEC_HIDDEN; -struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ir_node *node, struct hlsl_type *type, +struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type, struct vkd3d_shader_location *loc) DECLSPEC_HIDDEN; -struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ir_node *node) DECLSPEC_HIDDEN; +struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node) DECLSPEC_HIDDEN; struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hlsl_type *return_type, struct list *parameters, const struct hlsl_semantic *semantic, struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; -struct hlsl_ir_if *hlsl_new_if(struct hlsl_ir_node *condition, struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; -struct hlsl_ir_jump *hlsl_new_jump(enum hlsl_ir_jump_type type, struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; -struct hlsl_ir_load *hlsl_new_load(struct hlsl_ir_var *var, struct hlsl_ir_node *offset, struct hlsl_type *type, +struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, + struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; +struct hlsl_ir_jump *hlsl_new_jump(struct hlsl_ctx *ctx, enum hlsl_ir_jump_type type, struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; -struct hlsl_ir_loop *hlsl_new_loop(struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; -struct hlsl_ir_store *hlsl_new_simple_store(struct hlsl_ir_var *lhs, +struct hlsl_ir_load *hlsl_new_load(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, struct hlsl_ir_node *offset, + struct hlsl_type *type, struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; +struct hlsl_ir_loop *hlsl_new_loop(struct hlsl_ctx *ctx, struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; +struct hlsl_ir_store *hlsl_new_simple_store(struct hlsl_ctx *ctx, struct hlsl_ir_var *lhs, struct hlsl_ir_node *rhs) DECLSPEC_HIDDEN; -struct hlsl_ir_store *hlsl_new_store(struct hlsl_ir_var *var, struct hlsl_ir_node *offset, +struct hlsl_ir_store *hlsl_new_store(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, struct hlsl_ir_node *offset, struct hlsl_ir_node *rhs, unsigned int writemask, struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; struct hlsl_type *hlsl_new_struct_type(struct hlsl_ctx *ctx, const char *name, struct list *fields) DECLSPEC_HIDDEN; struct hlsl_ir_swizzle *hlsl_new_swizzle(struct hlsl_ctx *ctx, DWORD s, unsigned int components, @@ -595,12 +625,13 @@ struct hlsl_type *hlsl_new_type(struct hlsl_ctx *ctx, const char *name, enum hls enum hlsl_base_type base_type, unsigned dimx, unsigned dimy) DECLSPEC_HIDDEN; struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned int n, const struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; -struct hlsl_ir_node *hlsl_new_unary_expr(enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg, +struct hlsl_ir_node *hlsl_new_unary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg, struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; -struct hlsl_ir_var *hlsl_new_var(const char *name, struct hlsl_type *type, +struct hlsl_ir_var *hlsl_new_var(struct hlsl_ctx *ctx, const char *name, struct hlsl_type *type, const struct vkd3d_shader_location loc, const struct hlsl_semantic *semantic, unsigned int modifiers, const struct hlsl_reg_reservation *reg_reservation) DECLSPEC_HIDDEN; -struct hlsl_ir_load *hlsl_new_var_load(struct hlsl_ir_var *var, const struct vkd3d_shader_location loc) DECLSPEC_HIDDEN; +struct hlsl_ir_load *hlsl_new_var_load(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, + const struct vkd3d_shader_location loc) DECLSPEC_HIDDEN;
void hlsl_error(struct hlsl_ctx *ctx, const struct vkd3d_shader_location loc, enum vkd3d_shader_error error, const char *fmt, ...) VKD3D_PRINTF_FUNC(4, 5) DECLSPEC_HIDDEN; diff --git a/libs/vkd3d-shader/hlsl.l b/libs/vkd3d-shader/hlsl.l index cee98ea5..f8ff2830 100644 --- a/libs/vkd3d-shader/hlsl.l +++ b/libs/vkd3d-shader/hlsl.l @@ -170,7 +170,7 @@ row_major {return KW_ROW_MAJOR; } {IDENTIFIER} { struct hlsl_ctx *ctx = yyget_extra(yyscanner);
- yylval->name = vkd3d_strdup(yytext); + yylval->name = hlsl_strdup(ctx, yytext); if (hlsl_get_var(ctx->cur_scope, yytext) || hlsl_get_function(ctx, yytext)) return VAR_IDENTIFIER; else if (hlsl_get_type(ctx->cur_scope, yytext, true)) @@ -248,7 +248,8 @@ row_major {return KW_ROW_MAJOR; } return PRE_LINE; } <pp_line>{STRING} { - char *string = vkd3d_strdup(yytext + 1); + struct hlsl_ctx *ctx = yyget_extra(yyscanner); + char *string = hlsl_strdup(ctx, yytext + 1);
BEGIN(pp_ignore); string[strlen(string) - 1] = 0; diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index bcb1c207..52544c43 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -127,11 +127,11 @@ static struct hlsl_ir_node *node_from_list(struct list *list) return LIST_ENTRY(list_tail(list), struct hlsl_ir_node, entry); }
-static struct list *make_empty_list(void) +static struct list *make_empty_list(struct hlsl_ctx *ctx) { struct list *list;
- if ((list = vkd3d_malloc(sizeof(*list)))) + if ((list = hlsl_alloc(ctx, sizeof(*list)))) list_init(list); return list; } @@ -286,7 +286,7 @@ static struct hlsl_ir_node *add_implicit_conversion(struct hlsl_ctx *ctx, struct hlsl_warning(ctx, *loc, VKD3D_SHADER_WARNING_HLSL_IMPLICIT_TRUNCATION, "Implicit truncation of %s type.", src_type->type == HLSL_CLASS_VECTOR ? "vector" : "matrix");
- if (!(cast = hlsl_new_cast(node, dst_type, loc))) + if (!(cast = hlsl_new_cast(ctx, node, dst_type, loc))) return NULL; list_add_tail(instrs, &cast->node.entry); return &cast->node; @@ -313,7 +313,7 @@ static DWORD add_modifiers(struct hlsl_ctx *ctx, DWORD modifiers, DWORD mod, con return modifiers | mod; }
-static bool append_conditional_break(struct list *cond_list) +static bool append_conditional_break(struct hlsl_ctx *ctx, struct list *cond_list) { struct hlsl_ir_node *condition, *not; struct hlsl_ir_jump *jump; @@ -324,15 +324,15 @@ static bool append_conditional_break(struct list *cond_list) return true;
condition = node_from_list(cond_list); - if (!(not = hlsl_new_unary_expr(HLSL_IR_UNOP_LOGIC_NOT, condition, condition->loc))) + if (!(not = hlsl_new_unary_expr(ctx, HLSL_IR_UNOP_LOGIC_NOT, condition, condition->loc))) return false; list_add_tail(cond_list, ¬->entry);
- if (!(iff = hlsl_new_if(not, condition->loc))) + if (!(iff = hlsl_new_if(ctx, not, condition->loc))) return false; list_add_tail(cond_list, &iff->node.entry);
- if (!(jump = hlsl_new_jump(HLSL_IR_JUMP_BREAK, condition->loc))) + if (!(jump = hlsl_new_jump(ctx, HLSL_IR_JUMP_BREAK, condition->loc))) return false; list_add_head(&iff->then_instrs, &jump->node.entry); return true; @@ -345,24 +345,24 @@ enum loop_type LOOP_DO_WHILE };
-static struct list *create_loop(enum loop_type type, struct list *init, struct list *cond, +static struct list *create_loop(struct hlsl_ctx *ctx, enum loop_type type, struct list *init, struct list *cond, struct list *iter, struct list *body, struct vkd3d_shader_location loc) { struct list *list = NULL; struct hlsl_ir_loop *loop = NULL; struct hlsl_ir_if *cond_jump = NULL;
- if (!(list = make_empty_list())) + if (!(list = make_empty_list(ctx))) goto oom;
if (init) list_move_head(list, init);
- if (!(loop = hlsl_new_loop(loc))) + if (!(loop = hlsl_new_loop(ctx, loc))) goto oom; list_add_tail(list, &loop->node.entry);
- if (!append_conditional_break(cond)) + if (!append_conditional_break(ctx, cond)) goto oom;
if (type != LOOP_DO_WHILE) @@ -504,7 +504,7 @@ static struct hlsl_ir_jump *add_return(struct hlsl_ctx *ctx, struct list *instrs if (!(return_value = add_implicit_conversion(ctx, instrs, return_value, return_type, &loc))) return NULL;
- if (!(store = hlsl_new_simple_store(ctx->cur_function->return_var, return_value))) + if (!(store = hlsl_new_simple_store(ctx, ctx->cur_function->return_var, return_value))) return NULL; list_add_after(&return_value->entry, &store->node.entry); } @@ -514,7 +514,7 @@ static struct hlsl_ir_jump *add_return(struct hlsl_ctx *ctx, struct list *instrs return NULL; }
- if (!(jump = hlsl_new_jump(HLSL_IR_JUMP_RETURN, loc))) + if (!(jump = hlsl_new_jump(ctx, HLSL_IR_JUMP_RETURN, loc))) return NULL; list_add_tail(instrs, &jump->node.entry);
@@ -535,7 +535,7 @@ static struct hlsl_ir_load *add_load(struct hlsl_ctx *ctx, struct list *instrs, var = src->var; if (src->offset.node) { - if (!(add = hlsl_new_binary_expr(HLSL_IR_BINOP_ADD, src->offset.node, offset))) + if (!(add = hlsl_new_binary_expr(ctx, HLSL_IR_BINOP_ADD, src->offset.node, offset))) return NULL; list_add_tail(instrs, &add->entry); offset = add; @@ -550,13 +550,13 @@ static struct hlsl_ir_load *add_load(struct hlsl_ctx *ctx, struct list *instrs, if (!(var = hlsl_new_synthetic_var(ctx, name, var_node->data_type, var_node->loc))) return NULL;
- if (!(store = hlsl_new_simple_store(var, var_node))) + if (!(store = hlsl_new_simple_store(ctx, var, var_node))) return NULL;
list_add_tail(instrs, &store->node.entry); }
- if (!(load = hlsl_new_load(var, offset, data_type, loc))) + if (!(load = hlsl_new_load(ctx, var, offset, data_type, loc))) return NULL; list_add_tail(instrs, &load->node.entry); return load; @@ -604,7 +604,7 @@ static struct hlsl_ir_load *add_array_load(struct hlsl_ctx *ctx, struct list *in if (!(c = hlsl_new_uint_constant(ctx, data_type->reg_size * 4, loc))) return NULL; list_add_tail(instrs, &c->node.entry); - if (!(mul = hlsl_new_binary_expr(HLSL_IR_BINOP_MUL, index, &c->node))) + if (!(mul = hlsl_new_binary_expr(ctx, HLSL_IR_BINOP_MUL, index, &c->node))) return NULL; list_add_tail(instrs, &mul->entry); index = mul; @@ -676,13 +676,13 @@ static struct list *gen_struct_fields(struct hlsl_ctx *ctx, struct hlsl_type *ty if (type->type == HLSL_CLASS_MATRIX) assert(type->modifiers & HLSL_MODIFIERS_MAJORITY_MASK);
- if (!(list = make_empty_list())) + if (!(list = make_empty_list(ctx))) return NULL; LIST_FOR_EACH_ENTRY_SAFE(v, v_next, fields, struct parse_variable_def, entry) { unsigned int i;
- if (!(field = vkd3d_calloc(1, sizeof(*field)))) + if (!(field = hlsl_alloc(ctx, sizeof(*field)))) { vkd3d_free(v); return list; @@ -767,7 +767,7 @@ static bool add_func_parameter(struct hlsl_ctx *ctx, struct list *list, hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_INVALID_MODIFIER, "Parameter '%s' is declared as both "out" and "uniform".", param->name);
- if (!(var = hlsl_new_var(param->name, param->type, loc, ¶m->semantic, param->modifiers, param->reg_reservation))) + if (!(var = hlsl_new_var(ctx, param->name, param->type, loc, ¶m->semantic, param->modifiers, param->reg_reservation))) return false; var->is_param = 1;
@@ -780,7 +780,7 @@ static bool add_func_parameter(struct hlsl_ctx *ctx, struct list *list, return true; }
-static struct hlsl_reg_reservation *parse_reg_reservation(const char *reg_string) +static struct hlsl_reg_reservation *parse_reg_reservation(struct hlsl_ctx *ctx, const char *reg_string) { enum vkd3d_shader_register_type type; struct hlsl_reg_reservation *reg_res; @@ -811,7 +811,7 @@ static struct hlsl_reg_reservation *parse_reg_reservation(const char *reg_string return NULL; }
- if (!(reg_res = vkd3d_malloc(sizeof(*reg_res)))) + if (!(reg_res = hlsl_alloc(ctx, sizeof(*reg_res)))) return NULL; reg_res->type = type; reg_res->regnum = regnum; @@ -841,11 +841,11 @@ static const struct hlsl_ir_function_decl *get_overloaded_func(struct rb_tree *f return NULL; }
-static struct list *make_list(struct hlsl_ir_node *node) +static struct list *make_list(struct hlsl_ctx *ctx, struct hlsl_ir_node *node) { struct list *list;
- if (!(list = make_empty_list())) + if (!(list = make_empty_list(ctx))) { hlsl_free_instr(node); return NULL; @@ -1112,13 +1112,13 @@ static struct hlsl_ir_expr *add_expr(struct hlsl_ctx *ctx, struct list *instrs, "Implicit truncation of %s type.", operands[i]->data_type->type == HLSL_CLASS_VECTOR ? "vector" : "matrix");
- if (!(cast = hlsl_new_cast(operands[i], type, &operands[i]->loc))) + if (!(cast = hlsl_new_cast(ctx, operands[i], type, &operands[i]->loc))) return NULL; list_add_after(&operands[i]->entry, &cast->node.entry); operands[i] = &cast->node; }
- if (!(expr = vkd3d_calloc(1, sizeof(*expr)))) + if (!(expr = hlsl_alloc(ctx, sizeof(*expr)))) return NULL; init_node(&expr->node, HLSL_IR_EXPR, type, *loc); expr->op = op; @@ -1229,7 +1229,7 @@ static struct hlsl_ir_node *add_assignment(struct hlsl_ctx *ctx, struct list *in return NULL; }
- if (!(store = vkd3d_malloc(sizeof(*store)))) + if (!(store = hlsl_alloc(ctx, sizeof(*store)))) return NULL;
while (lhs->type != HLSL_IR_LOAD) @@ -1283,7 +1283,7 @@ static struct hlsl_ir_node *add_assignment(struct hlsl_ctx *ctx, struct list *in /* Don't use the instruction itself as a source, as this makes structure * splitting easier. Instead copy it here. Since we retrieve sources from * the last instruction in the list, we do need to copy. */ - if (!(copy = hlsl_new_copy(rhs))) + if (!(copy = hlsl_new_copy(ctx, rhs))) return NULL; list_add_tail(instrs, ©->node.entry); return ©->node; @@ -1310,7 +1310,7 @@ static bool add_increment(struct hlsl_ctx *ctx, struct list *instrs, bool decrem { struct hlsl_ir_expr *copy;
- if (!(copy = hlsl_new_copy(lhs))) + if (!(copy = hlsl_new_copy(ctx, lhs))) return false; list_add_tail(instrs, ©->node.entry);
@@ -1357,7 +1357,7 @@ static void struct_var_initializer(struct hlsl_ctx *ctx, struct list *list, stru break; list_add_tail(list, &c->node.entry);
- if (!(store = hlsl_new_store(var, &c->node, node, 0, node->loc))) + if (!(store = hlsl_new_store(ctx, var, &c->node, node, 0, node->loc))) break; list_add_tail(list, &store->node.entry); } @@ -1391,7 +1391,7 @@ static struct list *declare_vars(struct hlsl_ctx *ctx, struct hlsl_type *basic_t if (basic_type->type == HLSL_CLASS_MATRIX) assert(basic_type->modifiers & HLSL_MODIFIERS_MAJORITY_MASK);
- if (!(statements_list = make_empty_list())) + if (!(statements_list = make_empty_list(ctx))) { LIST_FOR_EACH_ENTRY_SAFE(v, v_next, var_list, struct parse_variable_def, entry) free_parse_variable_def(v); @@ -1413,7 +1413,7 @@ static struct list *declare_vars(struct hlsl_ctx *ctx, struct hlsl_type *basic_t if (type->type != HLSL_CLASS_MATRIX) check_invalid_matrix_modifiers(ctx, modifiers, v->loc);
- if (!(var = hlsl_new_var(v->name, type, v->loc, &v->semantic, modifiers, v->reg_reservation))) + if (!(var = hlsl_new_var(ctx, v->name, type, v->loc, &v->semantic, modifiers, v->reg_reservation))) { free_parse_variable_def(v); continue; @@ -1538,7 +1538,7 @@ static struct list *declare_vars(struct hlsl_ctx *ctx, struct hlsl_type *basic_t continue; }
- load = hlsl_new_var_load(var, var->loc); + load = hlsl_new_var_load(ctx, var, var->loc); list_add_tail(v->initializer.instrs, &load->node.entry); add_assignment(ctx, v->initializer.instrs, &load->node, ASSIGN_OP_ASSIGN, v->initializer.args[0]); vkd3d_free(v->initializer.args); @@ -1800,7 +1800,7 @@ hlsl_prog: } }
- hlsl_add_function(&ctx->functions, $2.name, $2.decl, false); + hlsl_add_function(ctx, $2.name, $2.decl, false); } | hlsl_prog declaration_statement { @@ -1893,7 +1893,7 @@ any_identifier: fields_list: %empty { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; } | fields_list field @@ -1996,7 +1996,7 @@ func_prototype: compound_statement: '{' '}' { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; } | '{' scope_start statement_list '}' @@ -2048,7 +2048,7 @@ semantic: register_opt: ':' KW_REGISTER '(' any_identifier ')' { - $$ = parse_reg_reservation($4); + $$ = parse_reg_reservation(ctx, $4); vkd3d_free($4); } | ':' KW_REGISTER '(' any_identifier ',' any_identifier ')' @@ -2056,14 +2056,14 @@ register_opt: FIXME("Ignoring shader target %s in a register reservation.\n", debugstr_a($4)); vkd3d_free($4);
- $$ = parse_reg_reservation($6); + $$ = parse_reg_reservation(ctx, $6); vkd3d_free($6); }
parameters: scope_start { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; } | scope_start param_list @@ -2074,12 +2074,11 @@ parameters: param_list: parameter { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; if (!add_func_parameter(ctx, $$, &$1, @1)) { ERR("Error adding function parameter %s.\n", $1.name); - ctx->failed = true; YYABORT; } } @@ -2244,7 +2243,7 @@ declaration_statement: | struct_declaration | typedef { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; }
@@ -2272,7 +2271,7 @@ typedef: type_specs: type_spec { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; list_add_head($$, &$1->entry); } @@ -2285,7 +2284,7 @@ type_specs: type_spec: any_identifier arrays { - $$ = vkd3d_calloc(1, sizeof(*$$)); + $$ = hlsl_alloc(ctx, sizeof(*$$)); $$->loc = @1; $$->name = $1; $$->arrays = $2; @@ -2312,7 +2311,7 @@ variables_def_optional: variables_def: variable_def { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; list_add_head($$, &$1->entry); } @@ -2325,7 +2324,7 @@ variables_def: variable_def: any_identifier arrays colon_attribute { - $$ = vkd3d_calloc(1, sizeof(*$$)); + $$ = hlsl_alloc(ctx, sizeof(*$$)); $$->loc = @1; $$->name = $1; $$->arrays = $2; @@ -2334,7 +2333,7 @@ variable_def: } | any_identifier arrays colon_attribute '=' complex_initializer { - $$ = vkd3d_calloc(1, sizeof(*$$)); + $$ = hlsl_alloc(ctx, sizeof(*$$)); $$->loc = @1; $$->name = $1; $$->arrays = $2; @@ -2437,7 +2436,7 @@ complex_initializer: initializer_expr { $$.args_count = 1; - if (!($$.args = vkd3d_malloc(sizeof(*$$.args)))) + if (!($$.args = hlsl_alloc(ctx, sizeof(*$$.args)))) YYABORT; $$.args[0] = node_from_list($1); $$.instrs = $1; @@ -2458,7 +2457,7 @@ initializer_expr_list: initializer_expr { $$.args_count = 1; - if (!($$.args = vkd3d_malloc(sizeof(*$$.args)))) + if (!($$.args = hlsl_alloc(ctx, sizeof(*$$.args)))) YYABORT; $$.args[0] = node_from_list($1); $$.instrs = $1; @@ -2509,7 +2508,7 @@ jump_statement: } | KW_RETURN ';' { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; if (!add_return(ctx, $$, NULL, @1)) YYABORT; @@ -2521,7 +2520,7 @@ selection_statement: struct hlsl_ir_node *condition = node_from_list($3); struct hlsl_ir_if *instr;
- if (!(instr = hlsl_new_if(condition, @1))) + if (!(instr = hlsl_new_if(ctx, condition, @1))) YYABORT; list_move_tail(&instr->then_instrs, $5.then_instrs); list_move_tail(&instr->else_instrs, $5.else_instrs); @@ -2555,27 +2554,27 @@ if_body: loop_statement: KW_WHILE '(' expr ')' statement { - $$ = create_loop(LOOP_WHILE, NULL, $3, NULL, $5, @1); + $$ = create_loop(ctx, LOOP_WHILE, NULL, $3, NULL, $5, @1); } | KW_DO statement KW_WHILE '(' expr ')' ';' { - $$ = create_loop(LOOP_DO_WHILE, NULL, $5, NULL, $2, @1); + $$ = create_loop(ctx, LOOP_DO_WHILE, NULL, $5, NULL, $2, @1); } | KW_FOR '(' scope_start expr_statement expr_statement expr ')' statement { - $$ = create_loop(LOOP_FOR, $4, $5, $6, $8, @1); + $$ = create_loop(ctx, LOOP_FOR, $4, $5, $6, $8, @1); hlsl_pop_scope(ctx); } | KW_FOR '(' scope_start declaration expr_statement expr ')' statement { - $$ = create_loop(LOOP_FOR, $4, $5, $6, $8, @1); + $$ = create_loop(ctx, LOOP_FOR, $4, $5, $6, $8, @1); hlsl_pop_scope(ctx); }
expr_statement: ';' { - if (!($$ = make_empty_list())) + if (!($$ = make_empty_list(ctx))) YYABORT; } | expr ';' @@ -2588,33 +2587,33 @@ primary_expr: { struct hlsl_ir_constant *c;
- if (!(c = vkd3d_malloc(sizeof(*c)))) + if (!(c = hlsl_alloc(ctx, sizeof(*c)))) YYABORT; init_node(&c->node, HLSL_IR_CONSTANT, ctx->builtin_types.scalar[HLSL_TYPE_FLOAT], @1); c->value.f[0] = $1; - if (!($$ = make_list(&c->node))) + if (!($$ = make_list(ctx, &c->node))) YYABORT; } | C_INTEGER { struct hlsl_ir_constant *c;
- if (!(c = vkd3d_malloc(sizeof(*c)))) + if (!(c = hlsl_alloc(ctx, sizeof(*c)))) YYABORT; init_node(&c->node, HLSL_IR_CONSTANT, ctx->builtin_types.scalar[HLSL_TYPE_INT], @1); c->value.i[0] = $1; - if (!($$ = make_list(&c->node))) + if (!($$ = make_list(ctx, &c->node))) YYABORT; } | boolean { struct hlsl_ir_constant *c;
- if (!(c = vkd3d_malloc(sizeof(*c)))) + if (!(c = hlsl_alloc(ctx, sizeof(*c)))) YYABORT; init_node(&c->node, HLSL_IR_CONSTANT, ctx->builtin_types.scalar[HLSL_TYPE_BOOL], @1); c->value.b[0] = $1; - if (!($$ = make_list(&c->node))) + if (!($$ = make_list(ctx, &c->node))) YYABORT; } | VAR_IDENTIFIER @@ -2627,9 +2626,9 @@ primary_expr: hlsl_error(ctx, @1, VKD3D_SHADER_ERROR_HLSL_NOT_DEFINED, "Variable "%s" is not defined.", $1); YYABORT; } - if ((load = hlsl_new_var_load(var, @1))) + if ((load = hlsl_new_var_load(ctx, var, @1))) { - if (!($$ = make_list(&load->node))) + if (!($$ = make_list(ctx, &load->node))) YYABORT; } else @@ -2711,7 +2710,7 @@ postfix_expr: YYABORT; }
- if (!(cast = hlsl_new_cast(index, ctx->builtin_types.scalar[HLSL_TYPE_UINT], &index->loc))) + if (!(cast = hlsl_new_cast(ctx, index, ctx->builtin_types.scalar[HLSL_TYPE_UINT], &index->loc))) { hlsl_free_instr_list($1); YYABORT; @@ -2793,14 +2792,14 @@ postfix_expr: ctx->builtin_types.vector[$2->base_type][width - 1], &arg->loc))) continue;
- if (!(store = hlsl_new_store(var, NULL, arg, + if (!(store = hlsl_new_store(ctx, var, NULL, arg, ((1 << width) - 1) << writemask_offset, arg->loc))) YYABORT; writemask_offset += width; list_add_tail($4.instrs, &store->node.entry); } vkd3d_free($4.args); - if (!(load = hlsl_new_var_load(var, @2))) + if (!(load = hlsl_new_var_load(ctx, var, @2))) YYABORT; $$ = append_unop($4.instrs, &load->node); } @@ -2833,7 +2832,7 @@ unary_expr: if ($1 == UNARY_OP_PLUS) $$ = $2; else - $$ = append_unop($2, hlsl_new_unary_expr(ops[$1], node_from_list($2), @1)); + $$ = append_unop($2, hlsl_new_unary_expr(ctx, ops[$1], node_from_list($2), @1)); }
/* var_modifiers is necessary to avoid shift/reduce conflicts. */ @@ -2868,7 +2867,7 @@ unary_expr: YYABORT; }
- $$ = append_unop($6, &hlsl_new_cast(node_from_list($6), dst_type, &@3)->node); + $$ = append_unop($6, &hlsl_new_cast(ctx, node_from_list($6), dst_type, &@3)->node); }
unary_op: diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index c2cedd2a..1705529e 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -35,11 +35,8 @@ static void prepend_uniform_copy(struct hlsl_ctx *ctx, struct list *instrs, stru /* Use the synthetic name for the temp, rather than the uniform, so that we * can write the uniform name into the shader reflection data. */
- if (!(uniform = hlsl_new_var(temp->name, temp->data_type, temp->loc, NULL, 0, temp->reg_reservation))) - { - ctx->failed = true; + if (!(uniform = hlsl_new_var(ctx, temp->name, temp->data_type, temp->loc, NULL, 0, temp->reg_reservation))) return; - } list_add_before(&temp->scope_entry, &uniform->scope_entry); list_add_tail(&ctx->extern_vars, &uniform->extern_entry); uniform->is_uniform = 1; @@ -51,21 +48,15 @@ static void prepend_uniform_copy(struct hlsl_ctx *ctx, struct list *instrs, stru return; } vkd3d_string_buffer_printf(name, "<temp-%s>", temp->name); - temp->name = vkd3d_strdup(name->buffer); + temp->name = hlsl_strdup(ctx, name->buffer); vkd3d_string_buffer_release(&ctx->string_buffers, name);
- if (!(load = hlsl_new_var_load(uniform, temp->loc))) - { - ctx->failed = true; + if (!(load = hlsl_new_var_load(ctx, uniform, temp->loc))) return; - } list_add_head(instrs, &load->node.entry);
- if (!(store = hlsl_new_simple_store(temp, &load->node))) - { - ctx->failed = true; + if (!(store = hlsl_new_simple_store(ctx, temp, &load->node))) return; - } list_add_after(&load->node.entry, &store->node.entry); }
@@ -85,18 +76,16 @@ static void prepend_input_copy(struct hlsl_ctx *ctx, struct list *instrs, struct return; } vkd3d_string_buffer_printf(name, "<input-%s%u>", semantic->name, semantic->index); - if (!(new_semantic.name = vkd3d_strdup(semantic->name))) + if (!(new_semantic.name = hlsl_strdup(ctx, semantic->name))) { vkd3d_string_buffer_release(&ctx->string_buffers, name); - ctx->failed = true; return; } new_semantic.index = semantic->index; - if (!(input = hlsl_new_var(vkd3d_strdup(name->buffer), type, var->loc, &new_semantic, 0, NULL))) + if (!(input = hlsl_new_var(ctx, hlsl_strdup(ctx, name->buffer), type, var->loc, &new_semantic, 0, NULL))) { vkd3d_string_buffer_release(&ctx->string_buffers, name); vkd3d_free((void *)new_semantic.name); - ctx->failed = true; return; } vkd3d_string_buffer_release(&ctx->string_buffers, name); @@ -105,25 +94,16 @@ static void prepend_input_copy(struct hlsl_ctx *ctx, struct list *instrs, struct list_add_before(&var->scope_entry, &input->scope_entry); list_add_tail(&ctx->extern_vars, &input->extern_entry);
- if (!(load = hlsl_new_var_load(input, var->loc))) - { - ctx->failed = true; + if (!(load = hlsl_new_var_load(ctx, input, var->loc))) return; - } list_add_head(instrs, &load->node.entry);
if (!(offset = hlsl_new_uint_constant(ctx, field_offset * 4, var->loc))) - { - ctx->failed = true; return; - } list_add_after(&load->node.entry, &offset->node.entry);
- if (!(store = hlsl_new_store(var, &offset->node, &load->node, 0, var->loc))) - { - ctx->failed = true; + if (!(store = hlsl_new_store(ctx, var, &offset->node, &load->node, 0, var->loc))) return; - } list_add_after(&offset->node.entry, &store->node.entry); }
@@ -170,18 +150,16 @@ static void append_output_copy(struct hlsl_ctx *ctx, struct list *instrs, struct return; } vkd3d_string_buffer_printf(name, "<output-%s%u>", semantic->name, semantic->index); - if (!(new_semantic.name = vkd3d_strdup(semantic->name))) + if (!(new_semantic.name = hlsl_strdup(ctx, semantic->name))) { vkd3d_string_buffer_release(&ctx->string_buffers, name); - ctx->failed = true; return; } new_semantic.index = semantic->index; - if (!(output = hlsl_new_var(vkd3d_strdup(name->buffer), type, var->loc, &new_semantic, 0, NULL))) + if (!(output = hlsl_new_var(ctx, hlsl_strdup(ctx, name->buffer), type, var->loc, &new_semantic, 0, NULL))) { vkd3d_free((void *)new_semantic.name); vkd3d_string_buffer_release(&ctx->string_buffers, name); - ctx->failed = true; return; } vkd3d_string_buffer_release(&ctx->string_buffers, name); @@ -191,24 +169,15 @@ static void append_output_copy(struct hlsl_ctx *ctx, struct list *instrs, struct list_add_tail(&ctx->extern_vars, &output->extern_entry);
if (!(offset = hlsl_new_uint_constant(ctx, field_offset * 4, var->loc))) - { - ctx->failed = true; return; - } list_add_tail(instrs, &offset->node.entry);
- if (!(load = hlsl_new_load(var, &offset->node, type, var->loc))) - { - ctx->failed = true; + if (!(load = hlsl_new_load(ctx, var, &offset->node, type, var->loc))) return; - } list_add_after(&offset->node.entry, &load->node.entry);
- if (!(store = hlsl_new_store(output, NULL, &load->node, 0, var->loc))) - { - ctx->failed = true; + if (!(store = hlsl_new_store(ctx, output, NULL, &load->node, 0, var->loc))) return; - } list_add_after(&load->node.entry, &store->node.entry); }
@@ -331,47 +300,32 @@ static bool split_struct_copies(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr struct hlsl_ir_constant *c;
if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset * 4, instr->loc))) - { - ctx->failed = true; 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(HLSL_IR_BINOP_ADD, rhs_load->src.offset.node, &c->node))) - { - ctx->failed = true; + if (!(add = hlsl_new_binary_expr(ctx, HLSL_IR_BINOP_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(rhs_load->src.var, offset, field->type, instr->loc))) - { - ctx->failed = true; + 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);
offset = &c->node; if (store->lhs.offset.node) { - if (!(add = hlsl_new_binary_expr(HLSL_IR_BINOP_ADD, store->lhs.offset.node, &c->node))) - { - ctx->failed = true; + if (!(add = hlsl_new_binary_expr(ctx, HLSL_IR_BINOP_ADD, store->lhs.offset.node, &c->node))) return false; - } list_add_before(&instr->entry, &add->entry); offset = add; }
- if (!(field_store = hlsl_new_store(store->lhs.var, offset, &field_load->node, 0, instr->loc))) - { - ctx->failed = true; + if (!(field_store = hlsl_new_store(ctx, store->lhs.var, offset, &field_load->node, 0, instr->loc))) return false; - } list_add_before(&instr->entry, &field_store->node.entry); }
@@ -402,11 +356,8 @@ static bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi if (expr->operands[1].node) arg2 = hlsl_ir_constant(expr->operands[1].node);
- if (!(res = vkd3d_calloc(1, sizeof(*res)))) - { - ctx->failed = true; + if (!(res = hlsl_alloc(ctx, sizeof(*res)))) return false; - } init_node(&res->node, HLSL_IR_CONSTANT, instr->data_type, instr->loc);
switch (instr->data_type->base_type) @@ -657,11 +608,11 @@ static unsigned int get_available_writemask(struct liveness *liveness, return 0; }
-static bool resize_liveness(struct liveness *liveness, size_t new_count) +static bool resize_liveness(struct hlsl_ctx *ctx, struct liveness *liveness, size_t new_count) { size_t old_capacity = liveness->size;
- if (!vkd3d_array_reserve((void **)&liveness->regs, &liveness->size, new_count, sizeof(*liveness->regs))) + if (!hlsl_array_reserve(ctx, (void **)&liveness->regs, &liveness->size, new_count, sizeof(*liveness->regs))) return false;
if (liveness->size > old_capacity) @@ -669,7 +620,7 @@ static bool resize_liveness(struct liveness *liveness, size_t new_count) return true; }
-static struct hlsl_reg allocate_register(struct liveness *liveness, +static struct hlsl_reg allocate_register(struct hlsl_ctx *ctx, struct liveness *liveness, unsigned int first_write, unsigned int last_read, unsigned int component_count) { unsigned int component_idx, writemask, i; @@ -682,7 +633,7 @@ static struct hlsl_reg allocate_register(struct liveness *liveness, } if (component_idx == liveness->size) { - if (!resize_liveness(liveness, component_idx + 4)) + if (!resize_liveness(ctx, liveness, component_idx + 4)) return ret; writemask = (1u << component_count) - 1; } @@ -710,7 +661,7 @@ static bool is_range_available(struct liveness *liveness, unsigned int first_wri return true; }
-static struct hlsl_reg allocate_range(struct liveness *liveness, +static struct hlsl_reg allocate_range(struct hlsl_ctx *ctx, struct liveness *liveness, unsigned int first_write, unsigned int last_read, unsigned int reg_count) { const unsigned int component_count = reg_count * 4; @@ -723,7 +674,7 @@ static struct hlsl_reg allocate_range(struct liveness *liveness, min(component_count, liveness->size - component_idx))) break; } - if (!resize_liveness(liveness, component_idx + component_count)) + if (!resize_liveness(ctx, liveness, component_idx + component_count)) return ret;
for (i = 0; i < component_count; ++i) @@ -741,7 +692,7 @@ static const char *debug_register(char class, struct hlsl_reg reg, const struct return vkd3d_dbg_sprintf("%c%u%s", class, reg.id, debug_hlsl_writemask(reg.writemask)); }
-static void allocate_variable_temp_register(struct hlsl_ir_var *var, struct liveness *liveness) +static void allocate_variable_temp_register(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, struct liveness *liveness) { if (var->is_input_semantic || var->is_output_semantic || var->is_uniform) return; @@ -749,17 +700,17 @@ static void allocate_variable_temp_register(struct hlsl_ir_var *var, struct live if (!var->reg.allocated && var->last_read) { if (var->data_type->reg_size > 1) - var->reg = allocate_range(liveness, var->first_write, + var->reg = allocate_range(ctx, liveness, var->first_write, var->last_read, var->data_type->reg_size); else - var->reg = allocate_register(liveness, var->first_write, + var->reg = allocate_register(ctx, liveness, var->first_write, var->last_read, var->data_type->dimx); TRACE("Allocated %s to %s (liveness %u-%u).\n", var->name, debug_register('r', var->reg, var->data_type), var->first_write, var->last_read); } }
-static void allocate_temp_registers_recurse(struct list *instrs, struct liveness *liveness) +static void allocate_temp_registers_recurse(struct hlsl_ctx *ctx, struct list *instrs, struct liveness *liveness) { struct hlsl_ir_node *instr;
@@ -768,10 +719,10 @@ static void allocate_temp_registers_recurse(struct list *instrs, struct liveness if (!instr->reg.allocated && instr->last_read) { if (instr->data_type->reg_size > 1) - instr->reg = allocate_range(liveness, instr->index, + instr->reg = allocate_range(ctx, liveness, instr->index, instr->last_read, instr->data_type->reg_size); else - instr->reg = allocate_register(liveness, instr->index, + instr->reg = allocate_register(ctx, liveness, instr->index, instr->last_read, instr->data_type->dimx); TRACE("Allocated anonymous expression @%u to %s (liveness %u-%u).\n", instr->index, debug_register('r', instr->reg, instr->data_type), instr->index, instr->last_read); @@ -782,8 +733,8 @@ static void allocate_temp_registers_recurse(struct list *instrs, struct liveness case HLSL_IR_IF: { struct hlsl_ir_if *iff = hlsl_ir_if(instr); - allocate_temp_registers_recurse(&iff->then_instrs, liveness); - allocate_temp_registers_recurse(&iff->else_instrs, liveness); + allocate_temp_registers_recurse(ctx, &iff->then_instrs, liveness); + allocate_temp_registers_recurse(ctx, &iff->else_instrs, liveness); break; }
@@ -792,21 +743,21 @@ static void allocate_temp_registers_recurse(struct list *instrs, struct liveness struct hlsl_ir_load *load = hlsl_ir_load(instr); /* We need to at least allocate a variable for undefs. * FIXME: We should probably find a way to remove them instead. */ - allocate_variable_temp_register(load->src.var, liveness); + allocate_variable_temp_register(ctx, load->src.var, liveness); break; }
case HLSL_IR_LOOP: { struct hlsl_ir_loop *loop = hlsl_ir_loop(instr); - allocate_temp_registers_recurse(&loop->body, liveness); + allocate_temp_registers_recurse(ctx, &loop->body, liveness); break; }
case HLSL_IR_STORE: { struct hlsl_ir_store *store = hlsl_ir_store(instr); - allocate_variable_temp_register(store->lhs.var, liveness); + allocate_variable_temp_register(ctx, store->lhs.var, liveness); break; }
@@ -833,17 +784,14 @@ static void allocate_const_registers_recurse(struct hlsl_ctx *ctx, struct list * unsigned int x, y, i, writemask;
if (reg_size > 1) - constant->reg = allocate_range(liveness, 1, UINT_MAX, reg_size); + constant->reg = allocate_range(ctx, liveness, 1, UINT_MAX, reg_size); else - constant->reg = allocate_register(liveness, 1, UINT_MAX, type->dimx); + constant->reg = allocate_register(ctx, liveness, 1, UINT_MAX, type->dimx); TRACE("Allocated constant @%u to %s.\n", instr->index, debug_register('c', constant->reg, type));
- if (!vkd3d_array_reserve((void **)&defs->values, &defs->size, + if (!hlsl_array_reserve(ctx, (void **)&defs->values, &defs->size, constant->reg.id + reg_size, sizeof(*defs->values))) - { - ctx->failed = true; return; - } defs->count = max(defs->count, constant->reg.id + reg_size);
assert(type->type <= HLSL_CLASS_LAST_NUMERIC); @@ -927,10 +875,10 @@ static void allocate_const_registers(struct hlsl_ctx *ctx, struct hlsl_ir_functi if (var->is_uniform && var->last_read) { if (var->data_type->reg_size > 1) - var->reg = allocate_range(&liveness, 1, UINT_MAX, var->data_type->reg_size); + var->reg = allocate_range(ctx, &liveness, 1, UINT_MAX, var->data_type->reg_size); else { - var->reg = allocate_register(&liveness, 1, UINT_MAX, 4); + var->reg = allocate_register(ctx, &liveness, 1, UINT_MAX, 4); var->reg.writemask = (1u << var->data_type->dimx) - 1; } TRACE("Allocated %s to %s.\n", var->name, debug_register('c', var->reg, var->data_type)); @@ -942,10 +890,10 @@ static void allocate_const_registers(struct hlsl_ctx *ctx, struct hlsl_ir_functi * index to all (simultaneously live) variables or intermediate values. Agnostic * as to how many registers are actually available for the current backend, and * does not handle constants. */ -static void allocate_temp_registers(struct hlsl_ir_function_decl *entry_func) +static void allocate_temp_registers(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_func) { struct liveness liveness = {0}; - allocate_temp_registers_recurse(entry_func->body, &liveness); + allocate_temp_registers_recurse(ctx, entry_func->body, &liveness); }
static bool sm1_register_from_semantic(struct hlsl_ctx *ctx, const struct hlsl_semantic *semantic, bool output, @@ -1216,6 +1164,7 @@ static struct hlsl_reg hlsl_reg_from_deref(const struct hlsl_deref *deref, const
struct bytecode_buffer { + struct hlsl_ctx *ctx; uint32_t *data; size_t count, size; int status; @@ -1229,7 +1178,8 @@ static unsigned int put_dword(struct bytecode_buffer *buffer, uint32_t value) if (buffer->status) return index;
- if (!vkd3d_array_reserve((void **)&buffer->data, &buffer->size, buffer->count + 1, sizeof(*buffer->data))) + if (!hlsl_array_reserve(buffer->ctx, (void **)&buffer->data, &buffer->size, + buffer->count + 1, sizeof(*buffer->data))) { buffer->status = VKD3D_ERROR_OUT_OF_MEMORY; return index; @@ -1270,7 +1220,8 @@ static unsigned int put_string(struct bytecode_buffer *buffer, const char *str) if (buffer->status) return index;
- if (!vkd3d_array_reserve((void **)&buffer->data, &buffer->size, buffer->count + token_count, sizeof(*buffer->data))) + if (!hlsl_array_reserve(buffer->ctx, (void **)&buffer->data, &buffer->size, + buffer->count + token_count, sizeof(*buffer->data))) { buffer->status = E_OUTOFMEMORY; return index; @@ -1478,7 +1429,7 @@ static void write_sm1_uniforms(struct hlsl_ctx *ctx, struct bytecode_buffer *buf } vkd3d_string_buffer_printf(name, "$%s", var->name); vkd3d_free((char *)var->name); - var->name = vkd3d_strdup(name->buffer); + var->name = hlsl_strdup(ctx, name->buffer); vkd3d_string_buffer_release(&ctx->string_buffers, name); } } @@ -1936,7 +1887,7 @@ static void write_sm1_instructions(struct hlsl_ctx *ctx, struct bytecode_buffer static int write_sm1_shader(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_func, struct vkd3d_shader_code *out) { - struct bytecode_buffer buffer = {0}; + struct bytecode_buffer buffer = {.ctx = ctx}; int ret;
put_dword(&buffer, sm1_version(ctx->profile->type, ctx->profile->major_version, ctx->profile->minor_version)); @@ -2018,7 +1969,7 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun if (TRACE_ON()) rb_for_each_entry(&ctx->functions, dump_function, NULL);
- allocate_temp_registers(entry_func); + allocate_temp_registers(ctx, entry_func); if (ctx->profile->major_version < 4) allocate_const_registers(ctx, entry_func); allocate_semantic_registers(ctx);
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- libs/vkd3d-shader/hlsl.h | 14 ++++++++++++++ libs/vkd3d-shader/hlsl_codegen.c | 33 ++++++++++++-------------------- 2 files changed, 26 insertions(+), 21 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 3e7d2445..3e6d1c3b 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -567,6 +567,20 @@ static inline bool hlsl_array_reserve(struct hlsl_ctx *ctx, void **elements, return ret; }
+static inline struct vkd3d_string_buffer *hlsl_get_string_buffer(struct hlsl_ctx *ctx) +{ + struct vkd3d_string_buffer *ret = vkd3d_string_buffer_get(&ctx->string_buffers); + + if (!ret) + ctx->failed = true; + return ret; +} + +static inline void hlsl_release_string_buffer(struct hlsl_ctx *ctx, struct vkd3d_string_buffer *buffer) +{ + vkd3d_string_buffer_release(&ctx->string_buffers, buffer); +} + const char *debug_hlsl_type(const struct hlsl_type *type) DECLSPEC_HIDDEN; const char *debug_hlsl_writemask(unsigned int writemask) DECLSPEC_HIDDEN;
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 1705529e..84a47eda 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -42,14 +42,11 @@ static void prepend_uniform_copy(struct hlsl_ctx *ctx, struct list *instrs, stru uniform->is_uniform = 1; uniform->is_param = temp->is_param;
- if (!(name = vkd3d_string_buffer_get(&ctx->string_buffers))) - { - ctx->failed = true; + if (!(name = hlsl_get_string_buffer(ctx))) return; - } vkd3d_string_buffer_printf(name, "<temp-%s>", temp->name); temp->name = hlsl_strdup(ctx, name->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, name); + hlsl_release_string_buffer(ctx, name);
if (!(load = hlsl_new_var_load(ctx, uniform, temp->loc))) return; @@ -70,25 +67,22 @@ static void prepend_input_copy(struct hlsl_ctx *ctx, struct list *instrs, struct struct hlsl_ir_load *load; struct hlsl_ir_var *input;
- if (!(name = vkd3d_string_buffer_get(&ctx->string_buffers))) - { - ctx->failed = true; + if (!(name = hlsl_get_string_buffer(ctx))) return; - } vkd3d_string_buffer_printf(name, "<input-%s%u>", semantic->name, semantic->index); if (!(new_semantic.name = hlsl_strdup(ctx, semantic->name))) { - vkd3d_string_buffer_release(&ctx->string_buffers, name); + hlsl_release_string_buffer(ctx, name); return; } new_semantic.index = semantic->index; if (!(input = hlsl_new_var(ctx, hlsl_strdup(ctx, name->buffer), type, var->loc, &new_semantic, 0, NULL))) { - vkd3d_string_buffer_release(&ctx->string_buffers, name); + hlsl_release_string_buffer(ctx, name); vkd3d_free((void *)new_semantic.name); return; } - vkd3d_string_buffer_release(&ctx->string_buffers, name); + hlsl_release_string_buffer(ctx, name); input->is_input_semantic = 1; input->is_param = var->is_param; list_add_before(&var->scope_entry, &input->scope_entry); @@ -144,25 +138,22 @@ static void append_output_copy(struct hlsl_ctx *ctx, struct list *instrs, struct struct hlsl_ir_var *output; struct hlsl_ir_load *load;
- if (!(name = vkd3d_string_buffer_get(&ctx->string_buffers))) - { - ctx->failed = true; + if (!(name = hlsl_get_string_buffer(ctx))) return; - } vkd3d_string_buffer_printf(name, "<output-%s%u>", semantic->name, semantic->index); if (!(new_semantic.name = hlsl_strdup(ctx, semantic->name))) { - vkd3d_string_buffer_release(&ctx->string_buffers, name); + hlsl_release_string_buffer(ctx, name); return; } new_semantic.index = semantic->index; if (!(output = hlsl_new_var(ctx, hlsl_strdup(ctx, name->buffer), type, var->loc, &new_semantic, 0, NULL))) { vkd3d_free((void *)new_semantic.name); - vkd3d_string_buffer_release(&ctx->string_buffers, name); + hlsl_release_string_buffer(ctx, name); return; } - vkd3d_string_buffer_release(&ctx->string_buffers, name); + hlsl_release_string_buffer(ctx, name); output->is_output_semantic = 1; output->is_param = var->is_param; list_add_before(&var->scope_entry, &output->scope_entry); @@ -1422,7 +1413,7 @@ static void write_sm1_uniforms(struct hlsl_ctx *ctx, struct bytecode_buffer *buf { struct vkd3d_string_buffer *name;
- if (!(name = vkd3d_string_buffer_get(&ctx->string_buffers))) + if (!(name = hlsl_get_string_buffer(ctx))) { buffer->status = VKD3D_ERROR_OUT_OF_MEMORY; return; @@ -1430,7 +1421,7 @@ static void write_sm1_uniforms(struct hlsl_ctx *ctx, struct bytecode_buffer *buf vkd3d_string_buffer_printf(name, "$%s", var->name); vkd3d_free((char *)var->name); var->name = hlsl_strdup(ctx, name->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, name); + hlsl_release_string_buffer(ctx, name); } } }
Signed-off-by: Matteo Bruni mbruni@codeweavers.com
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- libs/vkd3d-shader/hlsl.c | 66 ++++++++++++++----------------- libs/vkd3d-shader/hlsl.h | 10 ++--- libs/vkd3d-shader/hlsl.y | 68 ++++++++++++++++---------------- libs/vkd3d-shader/hlsl_codegen.c | 9 +++-- 4 files changed, 73 insertions(+), 80 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 86ef1247..f7e2ad5c 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -219,7 +219,7 @@ unsigned int hlsl_type_component_count(struct hlsl_type *type) } if (type->type != HLSL_CLASS_STRUCT) { - ERR("Unexpected data type %s.\n", debug_hlsl_type(type)); + ERR("Unexpected data type %#x.\n", type->type); return 0; }
@@ -706,8 +706,7 @@ static int compare_function_decl_rb(const void *key, const struct rb_entry *entr return 0; }
-struct vkd3d_string_buffer *hlsl_type_to_string(struct vkd3d_string_buffer_cache *string_buffers, - const struct hlsl_type *type) +struct vkd3d_string_buffer *hlsl_type_to_string(struct hlsl_ctx *ctx, const struct hlsl_type *type) { struct vkd3d_string_buffer *string;
@@ -721,7 +720,7 @@ struct vkd3d_string_buffer *hlsl_type_to_string(struct vkd3d_string_buffer_cache "bool", };
- if (!(string = vkd3d_string_buffer_get(string_buffers))) + if (!(string = hlsl_get_string_buffer(ctx))) return NULL;
if (type->name) @@ -755,10 +754,10 @@ struct vkd3d_string_buffer *hlsl_type_to_string(struct vkd3d_string_buffer_cache for (t = type; t->type == HLSL_CLASS_ARRAY; t = t->e.array.type) ;
- if ((inner_string = hlsl_type_to_string(string_buffers, t))) + if ((inner_string = hlsl_type_to_string(ctx, t))) { vkd3d_string_buffer_printf(string, "%s", inner_string->buffer); - vkd3d_string_buffer_release(string_buffers, inner_string); + hlsl_release_string_buffer(ctx, inner_string); }
for (t = type; t->type == HLSL_CLASS_ARRAY; t = t->e.array.type) @@ -776,27 +775,23 @@ struct vkd3d_string_buffer *hlsl_type_to_string(struct vkd3d_string_buffer_cache } }
-const char *debug_hlsl_type(const struct hlsl_type *type) +const char *debug_hlsl_type(struct hlsl_ctx *ctx, const struct hlsl_type *type) { - struct vkd3d_string_buffer_cache string_buffers; struct vkd3d_string_buffer *string; const char *ret;
- vkd3d_string_buffer_cache_init(&string_buffers); - if (!(string = hlsl_type_to_string(&string_buffers, type))) + if (!(string = hlsl_type_to_string(ctx, type))) return NULL; ret = vkd3d_dbg_sprintf("%s", string->buffer); - vkd3d_string_buffer_release(&string_buffers, string); - vkd3d_string_buffer_cache_cleanup(&string_buffers); + hlsl_release_string_buffer(ctx, string); return ret; }
-struct vkd3d_string_buffer *hlsl_modifiers_to_string(struct vkd3d_string_buffer_cache *string_buffers, - unsigned int modifiers) +struct vkd3d_string_buffer *hlsl_modifiers_to_string(struct hlsl_ctx *ctx, unsigned int modifiers) { struct vkd3d_string_buffer *string;
- if (!(string = vkd3d_string_buffer_get(string_buffers))) + if (!(string = hlsl_get_string_buffer(ctx))) return NULL;
if (modifiers & HLSL_STORAGE_EXTERN) @@ -853,15 +848,15 @@ const char *hlsl_node_type_to_string(enum hlsl_ir_node_type type) return names[type]; }
-static void dump_instr(struct vkd3d_string_buffer *buffer, const struct hlsl_ir_node *instr); +static void dump_instr(struct hlsl_ctx *ctx, struct vkd3d_string_buffer *buffer, const struct hlsl_ir_node *instr);
-static void dump_instr_list(struct vkd3d_string_buffer *buffer, const struct list *list) +static void dump_instr_list(struct hlsl_ctx *ctx, struct vkd3d_string_buffer *buffer, const struct list *list) { struct hlsl_ir_node *instr;
LIST_FOR_EACH_ENTRY(instr, list, struct hlsl_ir_node, entry) { - dump_instr(buffer, instr); + dump_instr(ctx, buffer, instr); vkd3d_string_buffer_printf(buffer, "\n"); } } @@ -874,20 +869,17 @@ static void dump_src(struct vkd3d_string_buffer *buffer, const struct hlsl_src * vkd3d_string_buffer_printf(buffer, "%p", src->node); }
-static void dump_ir_var(struct vkd3d_string_buffer *buffer, const struct hlsl_ir_var *var) +static void dump_ir_var(struct hlsl_ctx *ctx, struct vkd3d_string_buffer *buffer, const struct hlsl_ir_var *var) { if (var->modifiers) { - struct vkd3d_string_buffer_cache string_buffers; struct vkd3d_string_buffer *string;
- vkd3d_string_buffer_cache_init(&string_buffers); - if ((string = hlsl_modifiers_to_string(&string_buffers, var->modifiers))) + if ((string = hlsl_modifiers_to_string(ctx, var->modifiers))) vkd3d_string_buffer_printf(buffer, "%s ", string->buffer); - vkd3d_string_buffer_release(&string_buffers, string); - vkd3d_string_buffer_cache_cleanup(&string_buffers); + hlsl_release_string_buffer(ctx, string); } - vkd3d_string_buffer_printf(buffer, "%s %s", debug_hlsl_type(var->data_type), var->name); + vkd3d_string_buffer_printf(buffer, "%s %s", debug_hlsl_type(ctx, var->data_type), var->name); if (var->semantic.name) vkd3d_string_buffer_printf(buffer, " : %s%u", var->semantic.name, var->semantic.index); } @@ -1042,14 +1034,14 @@ static void dump_ir_expr(struct vkd3d_string_buffer *buffer, const struct hlsl_i vkd3d_string_buffer_printf(buffer, ")"); }
-static void dump_ir_if(struct vkd3d_string_buffer *buffer, const struct hlsl_ir_if *if_node) +static void dump_ir_if(struct hlsl_ctx *ctx, struct vkd3d_string_buffer *buffer, const struct hlsl_ir_if *if_node) { vkd3d_string_buffer_printf(buffer, "if ("); dump_src(buffer, &if_node->condition); vkd3d_string_buffer_printf(buffer, ")\n{\n"); - dump_instr_list(buffer, &if_node->then_instrs); + dump_instr_list(ctx, buffer, &if_node->then_instrs); vkd3d_string_buffer_printf(buffer, "}\nelse\n{\n"); - dump_instr_list(buffer, &if_node->else_instrs); + dump_instr_list(ctx, buffer, &if_node->else_instrs); vkd3d_string_buffer_printf(buffer, "}\n"); }
@@ -1075,10 +1067,10 @@ static void dump_ir_jump(struct vkd3d_string_buffer *buffer, const struct hlsl_i } }
-static void dump_ir_loop(struct vkd3d_string_buffer *buffer, const struct hlsl_ir_loop *loop) +static void dump_ir_loop(struct hlsl_ctx *ctx, struct vkd3d_string_buffer *buffer, const struct hlsl_ir_loop *loop) { vkd3d_string_buffer_printf(buffer, "for (;;)\n{\n"); - dump_instr_list(buffer, &loop->body); + dump_instr_list(ctx, buffer, &loop->body); vkd3d_string_buffer_printf(buffer, "}\n"); }
@@ -1113,14 +1105,14 @@ static void dump_ir_swizzle(struct vkd3d_string_buffer *buffer, const struct hls } }
-static void dump_instr(struct vkd3d_string_buffer *buffer, const struct hlsl_ir_node *instr) +static void dump_instr(struct hlsl_ctx *ctx, struct vkd3d_string_buffer *buffer, const struct hlsl_ir_node *instr) { if (instr->index) vkd3d_string_buffer_printf(buffer, "%4u: ", instr->index); else vkd3d_string_buffer_printf(buffer, "%p: ", instr);
- vkd3d_string_buffer_printf(buffer, "%10s | ", instr->data_type ? debug_hlsl_type(instr->data_type) : ""); + vkd3d_string_buffer_printf(buffer, "%10s | ", instr->data_type ? debug_hlsl_type(ctx, instr->data_type) : "");
switch (instr->type) { @@ -1133,7 +1125,7 @@ static void dump_instr(struct vkd3d_string_buffer *buffer, const struct hlsl_ir_ break;
case HLSL_IR_IF: - dump_ir_if(buffer, hlsl_ir_if(instr)); + dump_ir_if(ctx, buffer, hlsl_ir_if(instr)); break;
case HLSL_IR_JUMP: @@ -1145,7 +1137,7 @@ static void dump_instr(struct vkd3d_string_buffer *buffer, const struct hlsl_ir_ break;
case HLSL_IR_LOOP: - dump_ir_loop(buffer, hlsl_ir_loop(instr)); + dump_ir_loop(ctx, buffer, hlsl_ir_loop(instr)); break;
case HLSL_IR_STORE: @@ -1161,7 +1153,7 @@ static void dump_instr(struct vkd3d_string_buffer *buffer, const struct hlsl_ir_ } }
-void hlsl_dump_function(const struct hlsl_ir_function_decl *func) +void hlsl_dump_function(struct hlsl_ctx *ctx, const struct hlsl_ir_function_decl *func) { struct vkd3d_string_buffer buffer; struct hlsl_ir_var *param; @@ -1171,11 +1163,11 @@ void hlsl_dump_function(const struct hlsl_ir_function_decl *func) vkd3d_string_buffer_printf(&buffer, "Function parameters:\n"); LIST_FOR_EACH_ENTRY(param, func->parameters, struct hlsl_ir_var, param_entry) { - dump_ir_var(&buffer, param); + dump_ir_var(ctx, &buffer, param); vkd3d_string_buffer_printf(&buffer, "\n"); } if (func->body) - dump_instr_list(&buffer, func->body); + dump_instr_list(ctx, &buffer, func->body);
vkd3d_string_buffer_trace(&buffer); vkd3d_string_buffer_cleanup(&buffer); diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 3e6d1c3b..55665c19 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -581,20 +581,18 @@ static inline void hlsl_release_string_buffer(struct hlsl_ctx *ctx, struct vkd3d vkd3d_string_buffer_release(&ctx->string_buffers, buffer); }
-const char *debug_hlsl_type(const struct hlsl_type *type) DECLSPEC_HIDDEN; +const char *debug_hlsl_type(struct hlsl_ctx *ctx, const struct hlsl_type *type) DECLSPEC_HIDDEN; const char *debug_hlsl_writemask(unsigned int writemask) DECLSPEC_HIDDEN;
-struct vkd3d_string_buffer *hlsl_type_to_string(struct vkd3d_string_buffer_cache *string_buffers, - const struct hlsl_type *type) DECLSPEC_HIDDEN; -struct vkd3d_string_buffer *hlsl_modifiers_to_string(struct vkd3d_string_buffer_cache *string_buffers, - unsigned int modifiers) DECLSPEC_HIDDEN; +struct vkd3d_string_buffer *hlsl_type_to_string(struct hlsl_ctx *ctx, const struct hlsl_type *type) DECLSPEC_HIDDEN; +struct vkd3d_string_buffer *hlsl_modifiers_to_string(struct hlsl_ctx *ctx, unsigned int modifiers) DECLSPEC_HIDDEN; const char *hlsl_node_type_to_string(enum hlsl_ir_node_type type) DECLSPEC_HIDDEN;
void hlsl_add_function(struct hlsl_ctx *ctx, char *name, struct hlsl_ir_function_decl *decl, bool intrinsic) DECLSPEC_HIDDEN; bool hlsl_add_var(struct hlsl_ctx *ctx, struct hlsl_ir_var *decl, bool local_var) DECLSPEC_HIDDEN;
-void hlsl_dump_function(const struct hlsl_ir_function_decl *func) DECLSPEC_HIDDEN; +void hlsl_dump_function(struct hlsl_ctx *ctx, const struct hlsl_ir_function_decl *func) DECLSPEC_HIDDEN;
int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_func, struct vkd3d_shader_code *out) DECLSPEC_HIDDEN; diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 52544c43..cab46aee 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -272,13 +272,13 @@ static struct hlsl_ir_node *add_implicit_conversion(struct hlsl_ctx *ctx, struct { struct vkd3d_string_buffer *src_string, *dst_string;
- src_string = hlsl_type_to_string(&ctx->string_buffers, src_type); - dst_string = hlsl_type_to_string(&ctx->string_buffers, dst_type); + src_string = hlsl_type_to_string(ctx, src_type); + dst_string = hlsl_type_to_string(ctx, dst_type); if (src_string && dst_string) hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Can't implicitly convert from %s to %s.", src_string->buffer, dst_string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, src_string); - vkd3d_string_buffer_release(&ctx->string_buffers, dst_string); + hlsl_release_string_buffer(ctx, src_string); + hlsl_release_string_buffer(ctx, dst_string); return NULL; }
@@ -298,10 +298,10 @@ static DWORD add_modifiers(struct hlsl_ctx *ctx, DWORD modifiers, DWORD mod, con { struct vkd3d_string_buffer *string;
- if ((string = hlsl_modifiers_to_string(&ctx->string_buffers, mod))) + if ((string = hlsl_modifiers_to_string(ctx, mod))) hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_INVALID_MODIFIER, "Modifier '%s' was already specified.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); return modifiers; } if ((mod & HLSL_MODIFIERS_MAJORITY_MASK) && (modifiers & HLSL_MODIFIERS_MAJORITY_MASK)) @@ -974,10 +974,10 @@ static struct hlsl_type *expr_common_type(struct hlsl_ctx *ctx, struct hlsl_type { struct vkd3d_string_buffer *string;
- if ((string = hlsl_type_to_string(&ctx->string_buffers, t1))) + if ((string = hlsl_type_to_string(ctx, t1))) hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Expression of type "%s" cannot be used in a numeric expression.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); return NULL; }
@@ -985,10 +985,10 @@ static struct hlsl_type *expr_common_type(struct hlsl_ctx *ctx, struct hlsl_type { struct vkd3d_string_buffer *string;
- if ((string = hlsl_type_to_string(&ctx->string_buffers, t2))) + if ((string = hlsl_type_to_string(ctx, t2))) hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Expression of type "%s" cannot be used in a numeric expression.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); return NULL; }
@@ -997,15 +997,15 @@ static struct hlsl_type *expr_common_type(struct hlsl_ctx *ctx, struct hlsl_type
if (!expr_compatible_data_types(t1, t2)) { - struct vkd3d_string_buffer *t1_string = hlsl_type_to_string(&ctx->string_buffers, t1); - struct vkd3d_string_buffer *t2_string = hlsl_type_to_string(&ctx->string_buffers, t2); + struct vkd3d_string_buffer *t1_string = hlsl_type_to_string(ctx, t1); + struct vkd3d_string_buffer *t2_string = hlsl_type_to_string(ctx, t2);
if (t1_string && t2_string) hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Expression data types "%s" and "%s" are incompatible.", t1_string->buffer, t2_string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, t1_string); - vkd3d_string_buffer_release(&ctx->string_buffers, t2_string); + hlsl_release_string_buffer(ctx, t1_string); + hlsl_release_string_buffer(ctx, t2_string); return NULL; }
@@ -1450,10 +1450,10 @@ static struct list *declare_vars(struct hlsl_ctx *ctx, struct hlsl_type *basic_t { struct vkd3d_string_buffer *string;
- if ((string = hlsl_modifiers_to_string(&ctx->string_buffers, modifiers & invalid))) + if ((string = hlsl_modifiers_to_string(ctx, modifiers & invalid))) hlsl_error(ctx, var->loc, VKD3D_SHADER_ERROR_HLSL_INVALID_MODIFIER, "Modifiers '%s' are not allowed on local variables.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); } if (var->semantic.name) hlsl_error(ctx, var->loc, VKD3D_SHADER_ERROR_HLSL_INVALID_SEMANTIC, @@ -1935,10 +1935,10 @@ field: { struct vkd3d_string_buffer *string;
- if ((string = hlsl_modifiers_to_string(&ctx->string_buffers, modifiers))) + if ((string = hlsl_modifiers_to_string(ctx, modifiers))) hlsl_error(ctx, @1, VKD3D_SHADER_ERROR_HLSL_INVALID_MODIFIER, "Modifiers '%s' are not allowed on struct fields.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); } $$ = gen_struct_fields(ctx, type, $3); } @@ -2121,10 +2121,10 @@ input_mods: { struct vkd3d_string_buffer *string;
- if ((string = hlsl_modifiers_to_string(&ctx->string_buffers, $2))) + if ((string = hlsl_modifiers_to_string(ctx, $2))) hlsl_error(ctx, @2, VKD3D_SHADER_ERROR_HLSL_INVALID_MODIFIER, "Modifier "%s" was already specified.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); YYABORT; } $$ = $1 | $2; @@ -2155,11 +2155,11 @@ type: { struct vkd3d_string_buffer *string;
- string = hlsl_type_to_string(&ctx->string_buffers, $3); + string = hlsl_type_to_string(ctx, $3); if (string) hlsl_error(ctx, @3, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Vector base type %s is not scalar.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); YYABORT; } if ($5 < 1 || $5 > 4) @@ -2177,11 +2177,11 @@ type: { struct vkd3d_string_buffer *string;
- string = hlsl_type_to_string(&ctx->string_buffers, $3); + string = hlsl_type_to_string(ctx, $3); if (string) hlsl_error(ctx, @3, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Matrix base type %s is not scalar.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); YYABORT; } if ($5 < 1 || $5 > 4) @@ -2530,10 +2530,10 @@ selection_statement: { struct vkd3d_string_buffer *string;
- if ((string = hlsl_type_to_string(&ctx->string_buffers, condition->data_type))) + if ((string = hlsl_type_to_string(ctx, condition->data_type))) hlsl_error(ctx, instr->node.loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "if condition type %s is not scalar.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); } $$ = $3; list_add_tail($$, &instr->node.entry); @@ -2745,10 +2745,10 @@ postfix_expr: { struct vkd3d_string_buffer *string;
- if ((string = hlsl_type_to_string(&ctx->string_buffers, $2))) + if ((string = hlsl_type_to_string(ctx, $2))) hlsl_error(ctx, @2, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Constructor data type %s is not numeric.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); YYABORT; } if ($2->dimx * $2->dimy != initializer_size(&$4)) @@ -2774,10 +2774,10 @@ postfix_expr: { struct vkd3d_string_buffer *string;
- if ((string = hlsl_type_to_string(&ctx->string_buffers, arg->data_type))) + if ((string = hlsl_type_to_string(ctx, arg->data_type))) hlsl_error(ctx, arg->loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Invalid type %s for constructor argument.", string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, string); + hlsl_release_string_buffer(ctx, string); continue; } width = hlsl_type_component_count(arg->data_type); @@ -2857,13 +2857,13 @@ unary_expr: { struct vkd3d_string_buffer *src_string, *dst_string;
- src_string = hlsl_type_to_string(&ctx->string_buffers, src_type); - dst_string = hlsl_type_to_string(&ctx->string_buffers, dst_type); + src_string = hlsl_type_to_string(ctx, src_type); + dst_string = hlsl_type_to_string(ctx, dst_type); if (src_string && dst_string) hlsl_error(ctx, @3, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Can't cast from %s to %s.", src_string->buffer, dst_string->buffer); - vkd3d_string_buffer_release(&ctx->string_buffers, src_string); - vkd3d_string_buffer_release(&ctx->string_buffers, dst_string); + hlsl_release_string_buffer(ctx, src_string); + hlsl_release_string_buffer(ctx, dst_string); YYABORT; }
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 84a47eda..243deec8 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -456,15 +456,18 @@ static unsigned int index_instructions(struct list *instrs, unsigned int index) static void dump_function_decl(struct rb_entry *entry, void *context) { struct hlsl_ir_function_decl *func = RB_ENTRY_VALUE(entry, struct hlsl_ir_function_decl, entry); + struct hlsl_ctx *ctx = context;
if (func->body) - hlsl_dump_function(func); + hlsl_dump_function(ctx, func); }
static void dump_function(struct rb_entry *entry, void *context) { struct hlsl_ir_function *func = RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry); - rb_for_each_entry(&func->overloads, dump_function_decl, NULL); + struct hlsl_ctx *ctx = context; + + rb_for_each_entry(&func->overloads, dump_function_decl, ctx); }
/* Compute the earliest and latest liveness for each variable. In the case that @@ -1958,7 +1961,7 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun compute_liveness(ctx, entry_func);
if (TRACE_ON()) - rb_for_each_entry(&ctx->functions, dump_function, NULL); + rb_for_each_entry(&ctx->functions, dump_function, ctx);
allocate_temp_registers(ctx, entry_func); if (ctx->profile->major_version < 4)
With the subject changed to "vkd3d-shader: Use the HLSL string buffer cache in debug_hlsl_type() and dump_ir_var()." (or something to that effect) this is:
Signed-off-by: Matteo Bruni mbruni@codeweavers.com
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- libs/vkd3d-shader/hlsl.c | 7 ++++--- libs/vkd3d-shader/hlsl.h | 10 +++++----- libs/vkd3d-shader/hlsl_codegen.c | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index f7e2ad5c..441fd6c2 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -39,7 +39,8 @@ void hlsl_error(struct hlsl_ctx *ctx, const struct vkd3d_shader_location loc, vkd3d_shader_verror(ctx->message_context, &loc, error, fmt, args); va_end(args);
- ctx->failed = true; + if (!ctx->result) + ctx->result = VKD3D_ERROR_INVALID_SHADER; }
void hlsl_warning(struct hlsl_ctx *ctx, const struct vkd3d_shader_location loc, @@ -1648,10 +1649,10 @@ int hlsl_compile_shader(const struct vkd3d_shader_code *hlsl, const struct vkd3d return VKD3D_ERROR_OUT_OF_MEMORY; }
- if (ctx.failed) + if (ctx.result) { hlsl_ctx_cleanup(&ctx); - return VKD3D_ERROR_INVALID_SHADER; + return ctx.result; }
if (!(entry_func = hlsl_get_func_decl(&ctx, entry_point))) diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 55665c19..4fe90a91 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -428,7 +428,7 @@ struct hlsl_ctx struct vkd3d_shader_location location; struct vkd3d_shader_message_context *message_context; struct vkd3d_string_buffer_cache string_buffers; - bool failed; + int result;
void *scanner;
@@ -544,7 +544,7 @@ static inline void *hlsl_alloc(struct hlsl_ctx *ctx, size_t size) void *ptr = vkd3d_calloc(1, size);
if (!ptr) - ctx->failed = true; + ctx->result = VKD3D_ERROR_OUT_OF_MEMORY; return ptr; }
@@ -553,7 +553,7 @@ static inline char *hlsl_strdup(struct hlsl_ctx *ctx, const char *string) char *ptr = vkd3d_strdup(string);
if (!ptr) - ctx->failed = true; + ctx->result = VKD3D_ERROR_OUT_OF_MEMORY; return ptr; }
@@ -563,7 +563,7 @@ static inline bool hlsl_array_reserve(struct hlsl_ctx *ctx, void **elements, bool ret = vkd3d_array_reserve(elements, capacity, element_count, element_size);
if (!ret) - ctx->failed = true; + ctx->result = VKD3D_ERROR_OUT_OF_MEMORY; return ret; }
@@ -572,7 +572,7 @@ static inline struct vkd3d_string_buffer *hlsl_get_string_buffer(struct hlsl_ctx struct vkd3d_string_buffer *ret = vkd3d_string_buffer_get(&ctx->string_buffers);
if (!ret) - ctx->failed = true; + ctx->result = VKD3D_ERROR_OUT_OF_MEMORY; return ret; }
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 243deec8..b4ea8396 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -1968,8 +1968,8 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun allocate_const_registers(ctx, entry_func); allocate_semantic_registers(ctx);
- if (ctx->failed) - return VKD3D_ERROR_INVALID_SHADER; + if (ctx->result) + return ctx->result;
if (ctx->profile->major_version < 4) return write_sm1_shader(ctx, entry_func, out);
Signed-off-by: Matteo Bruni mbruni@codeweavers.com
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- libs/vkd3d-shader/hlsl_codegen.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index b4ea8396..afa096b0 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -388,6 +388,27 @@ static bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi return true; }
+/* Lower DIV to RCP + MUL. */ +static bool lower_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) +{ + struct hlsl_ir_expr *expr; + struct hlsl_ir_node *rcp; + + if (instr->type != HLSL_IR_EXPR) + return false; + expr = hlsl_ir_expr(instr); + if (expr->op != HLSL_IR_BINOP_DIV) + return false; + + if (!(rcp = hlsl_new_unary_expr(ctx, HLSL_IR_UNOP_RCP, expr->operands[1].node, instr->loc))) + return false; + list_add_before(&expr->node.entry, &rcp->entry); + expr->op = HLSL_IR_BINOP_MUL; + hlsl_src_remove(&expr->operands[1]); + hlsl_src_from_node(&expr->operands[1], rcp); + return true; +} + static bool dce(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { switch (instr->type) @@ -1954,6 +1975,9 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun while (transform_ir(ctx, split_struct_copies, entry_func->body, NULL)); while (transform_ir(ctx, fold_constants, entry_func->body, NULL));
+ if (ctx->profile->major_version < 4) + transform_ir(ctx, lower_division, entry_func->body, NULL); + do compute_liveness(ctx, entry_func); while (transform_ir(ctx, dce, entry_func->body, NULL));
Signed-off-by: Matteo Bruni mbruni@codeweavers.com
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com
Signed-off-by: Matteo Bruni mbruni@codeweavers.com --- I'm sure this is no surprise but I find it a bit awkward that currently there is a struct hlsl_ctx * inside struct bytecode_buffer while it's also passed separately to a bunch of helpers. Just another thing for potential followups...
On 5/28/21 7:07 AM, Matteo Bruni wrote:
Signed-off-by: Matteo Bruni mbruni@codeweavers.com
I'm sure this is no surprise but I find it a bit awkward that currently there is a struct hlsl_ctx * inside struct bytecode_buffer while it's also passed separately to a bunch of helpers. Just another thing for potential followups...
It's a bit awkward, it came of this patch being written much later than anything else. I didn't really want to have to pass ctx to put_dword() everywhere, but probably I should just do so anyway...