From: Francisco Casas Subject: [PATCH vkd3d 3/8] vkd3d-shader/hlsl: Move fold_constants() to a separate file. Message-Id: <20220106173949.82958-3-fcasas@codeweavers.com> Date: Thu, 6 Jan 2022 14:39:44 -0300 Signed-off-by: Francisco Casas --- Makefile.am | 1 + libs/vkd3d-shader/hlsl.h | 2 + libs/vkd3d-shader/hlsl_codegen.c | 106 --------------------- libs/vkd3d-shader/hlsl_constant_ops.c | 127 ++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 106 deletions(-) create mode 100644 libs/vkd3d-shader/hlsl_constant_ops.c diff --git a/Makefile.am b/Makefile.am index 20fee06d..d3dfc0a7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -194,6 +194,7 @@ libvkd3d_shader_la_SOURCES = \ libs/vkd3d-shader/hlsl.c \ libs/vkd3d-shader/hlsl.h \ libs/vkd3d-shader/hlsl_codegen.c \ + libs/vkd3d-shader/hlsl_constant_ops.c \ libs/vkd3d-shader/hlsl_sm1.c \ libs/vkd3d-shader/hlsl_sm4.c \ libs/vkd3d-shader/preproc.h \ diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 530d12ad..f2cb0802 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -762,6 +762,8 @@ unsigned int hlsl_offset_from_deref_safe(struct hlsl_ctx *ctx, const struct hlsl struct hlsl_reg hlsl_reg_from_deref(struct hlsl_ctx *ctx, const struct hlsl_deref *deref, const struct hlsl_type *type); +bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context); + bool hlsl_sm1_register_from_semantic(struct hlsl_ctx *ctx, const struct hlsl_semantic *semantic, bool output, D3DSHADER_PARAM_REGISTER_TYPE *type, unsigned int *reg); bool hlsl_sm1_usage_from_semantic(const struct hlsl_semantic *semantic, D3DDECLUSAGE *usage, uint32_t *usage_idx); diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 6cb4df8b..5f93c9a9 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -639,112 +639,6 @@ static bool lower_narrowing_casts(struct hlsl_ctx *ctx, struct hlsl_ir_node *ins return false; } -static bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) -{ - struct hlsl_ir_constant *arg1, *arg2 = NULL, *res; - struct hlsl_ir_expr *expr; - unsigned int i, dimx; - - if (instr->type != HLSL_IR_EXPR) - return false; - expr = hlsl_ir_expr(instr); - - for (i = 0; i < ARRAY_SIZE(expr->operands); ++i) - { - if (expr->operands[i].node && expr->operands[i].node->type != HLSL_IR_CONSTANT) - return false; - } - arg1 = hlsl_ir_constant(expr->operands[0].node); - if (expr->operands[1].node) - arg2 = hlsl_ir_constant(expr->operands[1].node); - dimx = instr->data_type->dimx; - - 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) - { - case HLSL_TYPE_FLOAT: - { - switch (expr->op) - { - case HLSL_OP1_CAST: - if (instr->data_type->dimx != arg1->node.data_type->dimx - || instr->data_type->dimy != arg1->node.data_type->dimy) - { - FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type), - debug_hlsl_type(ctx, instr->data_type)); - vkd3d_free(res); - return false; - } - - switch (arg1->node.data_type->base_type) - { - case HLSL_TYPE_INT: - for (i = 0; i < dimx; ++i) - res->value[i].f = arg1->value[i].i; - break; - - case HLSL_TYPE_UINT: - for (i = 0; i < dimx; ++i) - res->value[i].f = arg1->value[i].u; - break; - - default: - FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type), - debug_hlsl_type(ctx, instr->data_type)); - vkd3d_free(res); - return false; - } - break; - - default: - FIXME("Fold float op %#x.\n", expr->op); - vkd3d_free(res); - return false; - } - break; - } - - case HLSL_TYPE_UINT: - { - switch (expr->op) - { - case HLSL_OP1_NEG: - for (i = 0; i < instr->data_type->dimx; ++i) - res->value[i].u = -arg1->value[i].u; - break; - - case HLSL_OP2_ADD: - for (i = 0; i < instr->data_type->dimx; ++i) - res->value[i].u = arg1->value[i].u + arg2->value[i].u; - break; - - case HLSL_OP2_MUL: - for (i = 0; i < instr->data_type->dimx; ++i) - res->value[i].u = arg1->value[i].u * arg2->value[i].u; - break; - - default: - FIXME("Fold uint op %#x.\n", expr->op); - vkd3d_free(res); - return false; - } - break; - } - - default: - FIXME("Fold type %#x op %#x.\n", instr->data_type->base_type, expr->op); - vkd3d_free(res); - return false; - } - - list_add_before(&expr->node.entry, &res->node.entry); - hlsl_replace_node(&expr->node, &res->node); - return true; -} - static bool remove_trivial_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { struct hlsl_ir_swizzle *swizzle; diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c new file mode 100644 index 00000000..924e0380 --- /dev/null +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -0,0 +1,127 @@ +/* + * HLSL constant value operations for constant folding + * + * Copyright 2022 Francisco Casas for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "hlsl.h" + +bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) +{ + struct hlsl_ir_constant *arg1, *arg2 = NULL, *res; + struct hlsl_ir_expr *expr; + unsigned int i, dimx; + + if (instr->type != HLSL_IR_EXPR) + return false; + expr = hlsl_ir_expr(instr); + + for (i = 0; i < ARRAY_SIZE(expr->operands); ++i) + { + if (expr->operands[i].node && expr->operands[i].node->type != HLSL_IR_CONSTANT) + return false; + } + arg1 = hlsl_ir_constant(expr->operands[0].node); + if (expr->operands[1].node) + arg2 = hlsl_ir_constant(expr->operands[1].node); + dimx = instr->data_type->dimx; + + 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) + { + case HLSL_TYPE_FLOAT: + { + switch (expr->op) + { + case HLSL_OP1_CAST: + if (instr->data_type->dimx != arg1->node.data_type->dimx + || instr->data_type->dimy != arg1->node.data_type->dimy) + { + FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type), + debug_hlsl_type(ctx, instr->data_type)); + vkd3d_free(res); + return false; + } + + switch (arg1->node.data_type->base_type) + { + case HLSL_TYPE_INT: + for (i = 0; i < dimx; ++i) + res->value[i].f = arg1->value[i].i; + break; + + case HLSL_TYPE_UINT: + for (i = 0; i < dimx; ++i) + res->value[i].f = arg1->value[i].u; + break; + + default: + FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type), + debug_hlsl_type(ctx, instr->data_type)); + vkd3d_free(res); + return false; + } + break; + + default: + FIXME("Fold float op %#x.\n", expr->op); + vkd3d_free(res); + return false; + } + break; + } + + case HLSL_TYPE_UINT: + { + switch (expr->op) + { + case HLSL_OP1_NEG: + for (i = 0; i < instr->data_type->dimx; ++i) + res->value[i].u = -arg1->value[i].u; + break; + + case HLSL_OP2_ADD: + for (i = 0; i < instr->data_type->dimx; ++i) + res->value[i].u = arg1->value[i].u + arg2->value[i].u; + break; + + case HLSL_OP2_MUL: + for (i = 0; i < instr->data_type->dimx; ++i) + res->value[i].u = arg1->value[i].u * arg2->value[i].u; + break; + + default: + FIXME("Fold uint op %#x.\n", expr->op); + vkd3d_free(res); + return false; + } + break; + } + + default: + FIXME("Fold type %#x op %#x.\n", instr->data_type->base_type, expr->op); + vkd3d_free(res); + return false; + } + + list_add_before(&expr->node.entry, &res->node.entry); + hlsl_replace_node(&expr->node, &res->node); + return true; +} \ No newline at end of file -- 2.25.1