vkd3d-shader/hlsl: Add a hlsl_block_add_uint_constant() helper.

This commit is contained in:
Elizabeth Figura
2024-12-08 20:01:38 -06:00
committed by Henri Verbeet
parent 79ad8c9354
commit 992d20def3
Notes: Henri Verbeet 2025-02-20 16:07:11 +01:00
Approved-by: Henri Verbeet (@hverbeet)
Approved-by: Francisco Casas (@fcasas)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1370
4 changed files with 40 additions and 70 deletions

View File

@@ -845,13 +845,7 @@ static bool init_deref_from_component_index(struct hlsl_ctx *ctx, struct hlsl_bl
{ {
unsigned int next_index = traverse_path_from_component_index(ctx, &path_type, &path_index); unsigned int next_index = traverse_path_from_component_index(ctx, &path_type, &path_index);
if (!(c = hlsl_new_uint_constant(ctx, next_index, loc))) c = hlsl_block_add_uint_constant(ctx, block, next_index, loc);
{
hlsl_block_cleanup(block);
return false;
}
hlsl_block_add_instr(block, c);
hlsl_src_from_node(&deref->path[deref_path_len++], c); hlsl_src_from_node(&deref->path[deref_path_len++], c);
} }
@@ -1324,6 +1318,18 @@ bool hlsl_scope_add_type(struct hlsl_scope *scope, struct hlsl_type *type)
return true; return true;
} }
static struct hlsl_ir_node *append_new_instr(struct hlsl_ctx *ctx, struct hlsl_block *block, struct hlsl_ir_node *instr)
{
if (!instr)
{
block->value = ctx->error_instr;
return ctx->error_instr;
}
hlsl_block_add_instr(block, instr);
return instr;
}
struct hlsl_ir_node *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type, struct hlsl_ir_node *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type,
const struct vkd3d_shader_location *loc) const struct vkd3d_shader_location *loc)
{ {
@@ -1601,6 +1607,12 @@ struct hlsl_ir_node *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned int n
return hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), &value, loc); return hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), &value, loc);
} }
struct hlsl_ir_node *hlsl_block_add_uint_constant(struct hlsl_ctx *ctx, struct hlsl_block *block,
unsigned int n, const struct vkd3d_shader_location *loc)
{
return append_new_instr(ctx, block, hlsl_new_uint_constant(ctx, n, loc));
}
struct hlsl_ir_node *hlsl_new_string_constant(struct hlsl_ctx *ctx, const char *str, struct hlsl_ir_node *hlsl_new_string_constant(struct hlsl_ctx *ctx, const char *str,
const struct vkd3d_shader_location *loc) const struct vkd3d_shader_location *loc)
{ {

View File

@@ -1505,6 +1505,8 @@ struct hlsl_ir_node *hlsl_add_conditional(struct hlsl_ctx *ctx, struct hlsl_bloc
void hlsl_add_function(struct hlsl_ctx *ctx, char *name, struct hlsl_ir_function_decl *decl); void hlsl_add_function(struct hlsl_ctx *ctx, char *name, struct hlsl_ir_function_decl *decl);
void hlsl_add_var(struct hlsl_ctx *ctx, struct hlsl_ir_var *decl); void hlsl_add_var(struct hlsl_ctx *ctx, struct hlsl_ir_var *decl);
struct hlsl_ir_node *hlsl_block_add_uint_constant(struct hlsl_ctx *ctx, struct hlsl_block *block,
unsigned int n, const struct vkd3d_shader_location *loc);
void hlsl_block_cleanup(struct hlsl_block *block); void hlsl_block_cleanup(struct hlsl_block *block);
bool hlsl_clone_block(struct hlsl_ctx *ctx, struct hlsl_block *dst_block, const struct hlsl_block *src_block); bool hlsl_clone_block(struct hlsl_ctx *ctx, struct hlsl_block *dst_block, const struct hlsl_block *src_block);

View File

@@ -925,9 +925,7 @@ static bool add_record_access(struct hlsl_ctx *ctx, struct hlsl_block *block, st
VKD3D_ASSERT(idx < record->data_type->e.record.field_count); VKD3D_ASSERT(idx < record->data_type->e.record.field_count);
if (!(c = hlsl_new_uint_constant(ctx, idx, loc))) c = hlsl_block_add_uint_constant(ctx, block, idx, loc);
return false;
hlsl_block_add_instr(block, c);
if (!(index = hlsl_new_index(ctx, record, c, loc))) if (!(index = hlsl_new_index(ctx, record, c, loc)))
return false; return false;
@@ -2275,9 +2273,7 @@ static bool add_assignment(struct hlsl_ctx *ctx, struct hlsl_block *block, struc
if (!(writemask & (1 << i))) if (!(writemask & (1 << i)))
continue; continue;
if (!(c = hlsl_new_uint_constant(ctx, i, &lhs->loc))) c = hlsl_block_add_uint_constant(ctx, block, i, &lhs->loc);
return false;
hlsl_block_add_instr(block, c);
if (!(cell = hlsl_new_index(ctx, &row->node, c, &lhs->loc))) if (!(cell = hlsl_new_index(ctx, &row->node, c, &lhs->loc)))
return false; return false;
@@ -2869,12 +2865,7 @@ static struct hlsl_block *initialize_vars(struct hlsl_ctx *ctx, struct list *var
continue; continue;
} }
if (!(zero = hlsl_new_uint_constant(ctx, 0, &var->loc))) zero = hlsl_block_add_uint_constant(ctx, &ctx->static_initializers, 0, &var->loc);
{
free_parse_variable_def(v);
continue;
}
hlsl_block_add_instr(&ctx->static_initializers, zero);
if (!(cast = add_cast(ctx, &ctx->static_initializers, zero, var->data_type, &var->loc))) if (!(cast = add_cast(ctx, &ctx->static_initializers, zero, var->data_type, &var->loc)))
{ {
@@ -6377,8 +6368,8 @@ static bool add_getdimensions_method_call(struct hlsl_ctx *ctx, struct hlsl_bloc
bool uint_resinfo, has_uint_arg, has_float_arg; bool uint_resinfo, has_uint_arg, has_float_arg;
struct hlsl_resource_load_params load_params; struct hlsl_resource_load_params load_params;
struct hlsl_ir_node *sample_info, *res_info; struct hlsl_ir_node *sample_info, *res_info;
struct hlsl_ir_node *zero = NULL, *void_ret;
struct hlsl_type *uint_type, *float_type; struct hlsl_type *uint_type, *float_type;
struct hlsl_ir_node *void_ret;
unsigned int i, j; unsigned int i, j;
enum func_argument enum func_argument
{ {
@@ -6478,12 +6469,7 @@ static bool add_getdimensions_method_call(struct hlsl_ctx *ctx, struct hlsl_bloc
} }
if (!args[ARG_MIP_LEVEL]) if (!args[ARG_MIP_LEVEL])
{ args[ARG_MIP_LEVEL] = hlsl_block_add_uint_constant(ctx, block, 0, loc);
if (!(zero = hlsl_new_uint_constant(ctx, 0, loc)))
return false;
hlsl_block_add_instr(block, zero);
args[ARG_MIP_LEVEL] = zero;
}
memset(&load_params, 0, sizeof(load_params)); memset(&load_params, 0, sizeof(load_params));
load_params.type = HLSL_RESOURCE_RESINFO; load_params.type = HLSL_RESOURCE_RESINFO;
@@ -9177,9 +9163,7 @@ jump_statement:
if (!($$ = make_empty_block(ctx))) if (!($$ = make_empty_block(ctx)))
YYABORT; YYABORT;
if (!(c = hlsl_new_uint_constant(ctx, ~0u, &@1))) c = hlsl_block_add_uint_constant(ctx, $$, ~0u, &@1);
return false;
hlsl_block_add_instr($$, c);
if (!(discard = hlsl_new_jump(ctx, HLSL_IR_JUMP_DISCARD_NZ, c, &@1))) if (!(discard = hlsl_new_jump(ctx, HLSL_IR_JUMP_DISCARD_NZ, c, &@1)))
return false; return false;
@@ -9401,12 +9385,9 @@ primary_expr:
} }
| C_UNSIGNED | C_UNSIGNED
{ {
struct hlsl_ir_node *c; if (!($$ = make_empty_block(ctx)))
if (!(c = hlsl_new_uint_constant(ctx, $1, &@1)))
YYABORT;
if (!($$ = make_block(ctx, c)))
YYABORT; YYABORT;
hlsl_block_add_uint_constant(ctx, $$, $1, &@1);
} }
| boolean | boolean
{ {

View File

@@ -62,9 +62,7 @@ static struct hlsl_ir_node *new_offset_from_path_index(struct hlsl_ctx *ctx, str
size /= 4; size /= 4;
} }
if (!(c = hlsl_new_uint_constant(ctx, size, loc))) c = hlsl_block_add_uint_constant(ctx, block, size, loc);
return NULL;
hlsl_block_add_instr(block, c);
if (!(idx_offset = hlsl_new_binary_expr(ctx, HLSL_OP2_MUL, c, idx))) if (!(idx_offset = hlsl_new_binary_expr(ctx, HLSL_OP2_MUL, c, idx)))
return NULL; return NULL;
@@ -86,12 +84,7 @@ static struct hlsl_ir_node *new_offset_from_path_index(struct hlsl_ctx *ctx, str
field_offset /= 4; field_offset /= 4;
} }
if (!(c = hlsl_new_uint_constant(ctx, field_offset, loc))) idx_offset = hlsl_block_add_uint_constant(ctx, block, field_offset, loc);
return NULL;
hlsl_block_add_instr(block, c);
idx_offset = c;
break; break;
} }
@@ -122,9 +115,7 @@ static struct hlsl_ir_node *new_offset_instr_from_deref(struct hlsl_ctx *ctx, st
hlsl_block_init(block); hlsl_block_init(block);
if (!(offset = hlsl_new_uint_constant(ctx, 0, loc))) offset = hlsl_block_add_uint_constant(ctx, block, 0, loc);
return NULL;
hlsl_block_add_instr(block, offset);
VKD3D_ASSERT(deref->var); VKD3D_ASSERT(deref->var);
type = deref->var->data_type; type = deref->var->data_type;
@@ -448,9 +439,7 @@ static void prepend_input_copy(struct hlsl_ctx *ctx, struct hlsl_ir_function_dec
return; return;
hlsl_init_simple_deref_from_var(&patch_deref, input); hlsl_init_simple_deref_from_var(&patch_deref, input);
if (!(idx = hlsl_new_uint_constant(ctx, patch_index, &var->loc))) idx = hlsl_block_add_uint_constant(ctx, block, patch_index, &var->loc);
return;
hlsl_block_add_instr(block, idx);
if (!(load = hlsl_new_load_index(ctx, &patch_deref, idx, loc))) if (!(load = hlsl_new_load_index(ctx, &patch_deref, idx, loc)))
return; return;
@@ -473,9 +462,7 @@ static void prepend_input_copy(struct hlsl_ctx *ctx, struct hlsl_ir_function_dec
if (type->class == HLSL_CLASS_MATRIX) if (type->class == HLSL_CLASS_MATRIX)
{ {
if (!(c = hlsl_new_uint_constant(ctx, i, &var->loc))) c = hlsl_block_add_uint_constant(ctx, block, i, &var->loc);
return;
hlsl_block_add_instr(block, c);
if (!(store = hlsl_new_store_index(ctx, &lhs->src, c, cast, 0, &var->loc))) if (!(store = hlsl_new_store_index(ctx, &lhs->src, c, cast, 0, &var->loc)))
return; return;
@@ -538,9 +525,7 @@ static void prepend_input_copy_recurse(struct hlsl_ctx *ctx, struct hlsl_ir_func
force_align = (i == 0); force_align = (i == 0);
} }
if (!(c = hlsl_new_uint_constant(ctx, i, &var->loc))) c = hlsl_block_add_uint_constant(ctx, block, i, &var->loc);
return;
hlsl_block_add_instr(block, c);
/* This redundant load is expected to be deleted later by DCE. */ /* This redundant load is expected to be deleted later by DCE. */
if (!(element_load = hlsl_new_load_index(ctx, &lhs->src, c, loc))) if (!(element_load = hlsl_new_load_index(ctx, &lhs->src, c, loc)))
@@ -615,9 +600,7 @@ static void append_output_copy(struct hlsl_ctx *ctx, struct hlsl_ir_function_dec
if (type->class == HLSL_CLASS_MATRIX) if (type->class == HLSL_CLASS_MATRIX)
{ {
if (!(c = hlsl_new_uint_constant(ctx, i, &var->loc))) c = hlsl_block_add_uint_constant(ctx, &func->body, i, &var->loc);
return;
hlsl_block_add_instr(&func->body, c);
if (!(load = hlsl_new_load_index(ctx, &rhs->src, c, &var->loc))) if (!(load = hlsl_new_load_index(ctx, &rhs->src, c, &var->loc)))
return; return;
@@ -678,9 +661,7 @@ static void append_output_copy_recurse(struct hlsl_ctx *ctx,
force_align = (i == 0); force_align = (i == 0);
} }
if (!(c = hlsl_new_uint_constant(ctx, i, &var->loc))) c = hlsl_block_add_uint_constant(ctx, &func->body, i, &var->loc);
return;
hlsl_block_add_instr(&func->body, c);
if (!(element_load = hlsl_new_load_index(ctx, &rhs->src, c, loc))) if (!(element_load = hlsl_new_load_index(ctx, &rhs->src, c, loc)))
return; return;
@@ -1127,9 +1108,7 @@ static struct hlsl_ir_node *add_zero_mipmap_level(struct hlsl_ctx *ctx, struct h
return NULL; return NULL;
hlsl_block_add_instr(block, store); hlsl_block_add_instr(block, store);
if (!(zero = hlsl_new_uint_constant(ctx, 0, loc))) zero = hlsl_block_add_uint_constant(ctx, block, 0, loc);
return NULL;
hlsl_block_add_instr(block, zero);
if (!(store = hlsl_new_store_index(ctx, &coords_deref, NULL, zero, 1u << dim_count, loc))) if (!(store = hlsl_new_store_index(ctx, &coords_deref, NULL, zero, 1u << dim_count, loc)))
return NULL; return NULL;
@@ -1341,9 +1320,7 @@ static bool lower_index_loads(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
{ {
struct hlsl_ir_node *c; struct hlsl_ir_node *c;
if (!(c = hlsl_new_uint_constant(ctx, i, &instr->loc))) c = hlsl_block_add_uint_constant(ctx, block, i, &instr->loc);
return false;
hlsl_block_add_instr(block, c);
if (!(load = hlsl_new_load_index(ctx, &var_deref, c, &instr->loc))) if (!(load = hlsl_new_load_index(ctx, &var_deref, c, &instr->loc)))
return false; return false;
@@ -2977,9 +2954,7 @@ static bool lower_nonconstant_array_loads(struct hlsl_ctx *ctx, struct hlsl_ir_n
struct hlsl_ir_load *var_load, *specific_load; struct hlsl_ir_load *var_load, *specific_load;
struct hlsl_deref deref_copy = {0}; struct hlsl_deref deref_copy = {0};
if (!(const_i = hlsl_new_uint_constant(ctx, i, &cut_index->loc))) const_i = hlsl_block_add_uint_constant(ctx, block, i, &cut_index->loc);
return false;
hlsl_block_add_instr(block, const_i);
operands[0] = cut_index; operands[0] = cut_index;
operands[1] = const_i; operands[1] = const_i;