From e330ad2ac727afab0b252c7555966c02f49d4076 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Wed, 3 Sep 2025 14:34:48 +0200 Subject: [PATCH] vkd3d-shader/hlsl: Implement constant folding of 'cos' expressions. As encountered in tests/hlsl/trigonometry.shader_test. --- libs/vkd3d-shader/hlsl_constant_ops.c | 34 +++++++++++++++++++++++++++ tests/hlsl/trigonometry.shader_test | 4 ++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 83a07cfca..1262cfe89 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -250,6 +250,36 @@ static bool fold_ceil(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, return true; } +static bool fold_cos(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, + const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src) +{ + enum hlsl_base_type type = dst_type->e.numeric.type; + unsigned int k; + + VKD3D_ASSERT(type == src->node.data_type->e.numeric.type); + + for (k = 0; k < dst_type->e.numeric.dimx; ++k) + { + switch (type) + { + case HLSL_TYPE_FLOAT: + case HLSL_TYPE_HALF: + dst->u[k].f = cosf(src->value.u[k].f); + break; + + case HLSL_TYPE_DOUBLE: + dst->u[k].d = cos(src->value.u[k].d); + break; + + default: + FIXME("Fold 'cos' for type %s.\n", debug_hlsl_type(ctx, dst_type)); + return false; + } + } + + return true; +} + static bool fold_exp2(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src) { @@ -1331,6 +1361,10 @@ bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, success = fold_ceil(ctx, &res, instr->data_type, arg1); break; + case HLSL_OP1_COS: + success = fold_cos(ctx, &res, instr->data_type, arg1); + break; + case HLSL_OP1_EXP2: success = fold_exp2(ctx, &res, instr->data_type, arg1); break; diff --git a/tests/hlsl/trigonometry.shader_test b/tests/hlsl/trigonometry.shader_test index e7e020305..deb3f1245 100644 --- a/tests/hlsl/trigonometry.shader_test +++ b/tests/hlsl/trigonometry.shader_test @@ -171,5 +171,5 @@ float4 main() : sv_target } [test] -todo(msl & sm<6) draw quad -probe (0, 0) rgba (0, 0, 0, 0); +draw quad +probe (0, 0) f32(0, 0, 0, 0);