From 3a0a4b625f533cce9323da0910c41381e6676373 Mon Sep 17 00:00:00 2001 From: Francisco Casas Date: Fri, 5 Apr 2024 16:34:32 -0300 Subject: [PATCH] vkd3d-shader/hlsl: Merge HLSL_OP3_MOVC into HLSL_OP3_TERNARY. --- libs/vkd3d-shader/hlsl.c | 1 - libs/vkd3d-shader/hlsl.h | 5 +--- libs/vkd3d-shader/hlsl_codegen.c | 45 ++++++++++++-------------------- libs/vkd3d-shader/tpf.c | 2 +- tests/hlsl/ternary.shader_test | 2 +- 5 files changed, 20 insertions(+), 35 deletions(-) diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 5638a03a..62467c68 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -2631,7 +2631,6 @@ const char *debug_hlsl_expr_op(enum hlsl_ir_expr_op op) [HLSL_OP3_CMP] = "cmp", [HLSL_OP3_DP2ADD] = "dp2add", - [HLSL_OP3_MOVC] = "movc", [HLSL_OP3_TERNARY] = "ternary", }; diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 3f8eeb57..9882caf7 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -602,11 +602,8 @@ enum hlsl_ir_expr_op * then adds c. */ HLSL_OP3_DP2ADD, /* TERNARY(a, b, c) returns 'b' if 'a' is true and 'c' otherwise. 'a' must always be boolean. - * MOVC(a, b, c) returns 'c' if 'a' is bitwise zero and 'b' otherwise. - * CMP(a, b, c) returns 'b' if 'a' >= 0, and 'c' otherwise. It's used only for SM1-SM3 targets, - while SM4+ is using MOVC in such cases. */ + * CMP(a, b, c) returns 'b' if 'a' >= 0, and 'c' otherwise. It's used only for SM1-SM3 targets. */ HLSL_OP3_CMP, - HLSL_OP3_MOVC, HLSL_OP3_TERNARY, }; diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index ef3ef919..3bb90c98 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -2954,7 +2954,7 @@ static bool lower_logic_not(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, st return true; } -/* Use movc/cmp for the ternary operator. */ +/* Lower TERNARY to CMP for SM1. */ static bool lower_ternary(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct hlsl_block *block) { struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS] = { 0 }, *replacement; @@ -2981,35 +2981,23 @@ static bool lower_ternary(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, stru assert(cond->data_type->base_type == HLSL_TYPE_BOOL); - if (ctx->profile->major_version < 4) - { - type = hlsl_get_numeric_type(ctx, instr->data_type->class, HLSL_TYPE_FLOAT, - instr->data_type->dimx, instr->data_type->dimy); + type = hlsl_get_numeric_type(ctx, instr->data_type->class, HLSL_TYPE_FLOAT, + instr->data_type->dimx, instr->data_type->dimy); - if (!(float_cond = hlsl_new_cast(ctx, cond, type, &instr->loc))) - return false; - hlsl_block_add_instr(block, float_cond); + if (!(float_cond = hlsl_new_cast(ctx, cond, type, &instr->loc))) + return false; + hlsl_block_add_instr(block, float_cond); - if (!(neg = hlsl_new_unary_expr(ctx, HLSL_OP1_NEG, float_cond, &instr->loc))) - return false; - hlsl_block_add_instr(block, neg); + if (!(neg = hlsl_new_unary_expr(ctx, HLSL_OP1_NEG, float_cond, &instr->loc))) + return false; + hlsl_block_add_instr(block, neg); - memset(operands, 0, sizeof(operands)); - operands[0] = neg; - operands[1] = second; - operands[2] = first; - if (!(replacement = hlsl_new_expr(ctx, HLSL_OP3_CMP, operands, first->data_type, &instr->loc))) - return false; - } - else - { - memset(operands, 0, sizeof(operands)); - operands[0] = cond; - operands[1] = first; - operands[2] = second; - if (!(replacement = hlsl_new_expr(ctx, HLSL_OP3_MOVC, operands, first->data_type, &instr->loc))) - return false; - } + memset(operands, 0, sizeof(operands)); + operands[0] = neg; + operands[1] = second; + operands[2] = first; + if (!(replacement = hlsl_new_expr(ctx, HLSL_OP3_CMP, operands, first->data_type, &instr->loc))) + return false; hlsl_block_add_instr(block, replacement); return true; @@ -5429,9 +5417,10 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry hlsl_transform_ir(ctx, track_object_components_usage, body, NULL); sort_synthetic_separated_samplers_first(ctx); - lower_ir(ctx, lower_ternary, body); if (profile->major_version < 4) { + lower_ir(ctx, lower_ternary, body); + lower_ir(ctx, lower_nonfloat_exprs, body); /* Constants casted to float must be folded, and new casts to bool also need to be lowered. */ hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, body, NULL); diff --git a/libs/vkd3d-shader/tpf.c b/libs/vkd3d-shader/tpf.c index 4d065831..23cde7ca 100644 --- a/libs/vkd3d-shader/tpf.c +++ b/libs/vkd3d-shader/tpf.c @@ -5343,7 +5343,7 @@ static void write_sm4_expr(const struct tpf_writer *tpf, const struct hlsl_ir_ex &expr->node, arg1, arg2); break; - case HLSL_OP3_MOVC: + case HLSL_OP3_TERNARY: write_sm4_ternary_op(tpf, VKD3D_SM4_OP_MOVC, &expr->node, arg1, arg2, arg3); break; diff --git a/tests/hlsl/ternary.shader_test b/tests/hlsl/ternary.shader_test index 91802afd..0ac78914 100644 --- a/tests/hlsl/ternary.shader_test +++ b/tests/hlsl/ternary.shader_test @@ -318,7 +318,7 @@ probe all rgba (2.0, 3.0, 3.0, 2.0) % Objects can be used, but their types have to be identical. -[pixel shader todo] +[pixel shader todo(sm<4)] Texture2D t; float4 main() : sv_target