From 3239ea5ff1c8c40a597f42cea93fb19877895534 Mon Sep 17 00:00:00 2001 From: Francisco Casas Date: Fri, 13 Jan 2023 15:22:13 -0300 Subject: [PATCH] vkd3d-shader/hlsl: Support sin() intrinsic. --- libs/vkd3d-shader/hlsl.y | 12 ++++++++++++ libs/vkd3d-shader/hlsl_sm4.c | 28 ++++++++++++++++++++++++++++ tests/trigonometry.shader_test | 6 +++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 7d3b23cf..d096856d 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2736,6 +2736,17 @@ static bool intrinsic_saturate(struct hlsl_ctx *ctx, return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_SAT, arg, loc); } +static bool intrinsic_sin(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_node *arg; + + if (!(arg = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) + return false; + + return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_SIN, arg, loc); +} + /* smoothstep(a, b, x) = p^2 (3 - 2p), where p = saturate((x - a)/(b - a)) */ static bool intrinsic_smoothstep(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) @@ -2880,6 +2891,7 @@ intrinsic_functions[] = {"pow", 2, true, intrinsic_pow}, {"round", 1, true, intrinsic_round}, {"saturate", 1, true, intrinsic_saturate}, + {"sin", 1, true, intrinsic_sin}, {"smoothstep", 3, true, intrinsic_smoothstep}, {"transpose", 1, true, intrinsic_transpose}, }; diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index d9d05e04..c7231826 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -1319,6 +1319,29 @@ static void write_sm4_unary_op(struct vkd3d_bytecode_buffer *buffer, enum vkd3d_ write_sm4_instruction(buffer, &instr); } +static void write_sm4_unary_op_with_two_destinations(struct vkd3d_bytecode_buffer *buffer, + enum vkd3d_sm4_opcode opcode, const struct hlsl_ir_node *dst, unsigned dst_idx, + const struct hlsl_ir_node *src) +{ + struct sm4_instruction instr; + + memset(&instr, 0, sizeof(instr)); + instr.opcode = opcode; + + assert(dst_idx < ARRAY_SIZE(instr.dsts)); + sm4_dst_from_node(&instr.dsts[dst_idx], dst); + assert(1 - dst_idx >= 0); + instr.dsts[1 - dst_idx].reg.type = VKD3D_SM4_RT_NULL; + instr.dsts[1 - dst_idx].reg.dim = VKD3D_SM4_DIMENSION_NONE; + instr.dsts[1 - dst_idx].reg.idx_count = 0; + instr.dst_count = 2; + + sm4_src_from_node(&instr.srcs[0], src, instr.dsts[dst_idx].writemask); + instr.src_count = 1; + + write_sm4_instruction(buffer, &instr); +} + static void write_sm4_binary_op(struct vkd3d_bytecode_buffer *buffer, enum vkd3d_sm4_opcode opcode, const struct hlsl_ir_node *dst, const struct hlsl_ir_node *src1, const struct hlsl_ir_node *src2) { @@ -1738,6 +1761,11 @@ static void write_sm4_expr(struct hlsl_ctx *ctx, &expr->node, arg1, 0); break; + case HLSL_OP1_SIN: + assert(type_is_float(dst_type)); + write_sm4_unary_op_with_two_destinations(buffer, VKD3D_SM4_OP_SINCOS, &expr->node, 0, arg1); + break; + case HLSL_OP1_SQRT: assert(type_is_float(dst_type)); write_sm4_unary_op(buffer, VKD3D_SM4_OP_SQRT, &expr->node, arg1, 0); diff --git a/tests/trigonometry.shader_test b/tests/trigonometry.shader_test index fef85ca0..919eea20 100644 --- a/tests/trigonometry.shader_test +++ b/tests/trigonometry.shader_test @@ -31,7 +31,7 @@ probe (14, 0) rgba ( 0.99060736, 0.13673722, 0.0, 0.0) 1024 probe (15, 0) rgba ( 0.65028784, -0.75968791, 0.0, 0.0) 1024 -[pixel shader todo] +[pixel shader] uniform float4 a; float4 main() : sv_target @@ -41,8 +41,8 @@ float4 main() : sv_target [test] uniform 0 float4 0.0 0.52359877 2.61799387 3.14159265 -todo draw quad -todo probe all rgba (0.0, 500.0, 500.0, 0.0) +draw quad +probe all rgba (0.0, 500.0, 500.0, 0.0) [pixel shader todo]