-- v2: vkd3d-shader/hlsl: Support log() intrinsic. vkd3d-shader/hlsl: Support log10() intrinsic.
From: Nikolay Sivov nsivov@codeweavers.com
--- libs/vkd3d-shader/hlsl_sm1.c | 4 ++++ 1 file changed, 4 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl_sm1.c b/libs/vkd3d-shader/hlsl_sm1.c index 41b111a5..0efea851 100644 --- a/libs/vkd3d-shader/hlsl_sm1.c +++ b/libs/vkd3d-shader/hlsl_sm1.c @@ -690,6 +690,10 @@ static void write_sm1_expr(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *b write_sm1_per_component_unary_op(ctx, buffer, instr, D3DSIO_EXP); break;
+ case HLSL_OP1_LOG2: + write_sm1_per_component_unary_op(ctx, buffer, instr, D3DSIO_LOG); + break; + case HLSL_OP1_NEG: write_sm1_unary_op(ctx, buffer, D3DSIO_MOV, &instr->reg, &arg1->reg, D3DSPSM_NEG, 0); break;
From: Nikolay Sivov nsivov@codeweavers.com
--- Makefile.am | 1 + libs/vkd3d-shader/hlsl.y | 10 ++++++++++ tests/log.shader_test | 12 ++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 tests/log.shader_test
diff --git a/Makefile.am b/Makefile.am index f9199472..8ed816f6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -115,6 +115,7 @@ vkd3d_shader_tests = \ tests/hlsl-transpose.shader_test \ tests/hlsl-vector-indexing.shader_test \ tests/hlsl-vector-indexing-uniform.shader_test \ + tests/log.shader_test \ tests/logic-operations.shader_test \ tests/math.shader_test \ tests/matrix-semantics.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index d5e2b2a9..ea8af221 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2610,6 +2610,15 @@ static bool intrinsic_lerp(struct hlsl_ctx *ctx, return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, params->args[0], mul, loc); }
+static bool intrinsic_log2(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + if (!elementwise_intrinsic_float_convert_args(ctx, params, loc)) + return false; + + return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_LOG2, params->args[0], loc); +} + static bool intrinsic_max(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -2977,6 +2986,7 @@ intrinsic_functions[] = {"ldexp", 2, true, intrinsic_ldexp}, {"length", 1, true, intrinsic_length}, {"lerp", 3, true, intrinsic_lerp}, + {"log2", 1, true, intrinsic_log2}, {"max", 2, true, intrinsic_max}, {"min", 2, true, intrinsic_min}, {"mul", 2, true, intrinsic_mul}, diff --git a/tests/log.shader_test b/tests/log.shader_test new file mode 100644 index 00000000..9ba0af76 --- /dev/null +++ b/tests/log.shader_test @@ -0,0 +1,12 @@ +[pixel shader] +uniform float4 x; + +float4 main() : sv_target +{ + return log2(x); +} + +[test] +uniform 0 float4 2.0 4.0 5.0 0.4 +draw quad +probe all rgba (1.0, 2.0, 2.32192802, -1.32192802) 1
From: Nikolay Sivov nsivov@codeweavers.com
--- libs/vkd3d-shader/hlsl.y | 20 ++++++++++++++++++++ tests/log.shader_test | 13 +++++++++++++ 2 files changed, 33 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index ea8af221..e1cde339 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2610,6 +2610,25 @@ static bool intrinsic_lerp(struct hlsl_ctx *ctx, return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, params->args[0], mul, loc); }
+static bool intrinsic_log10(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_constant *coeff; + struct hlsl_ir_node *log; + + if (!elementwise_intrinsic_float_convert_args(ctx, params, loc)) + return false; + + if (!(log = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_LOG2, params->args[0], loc))) + return false; + + /* 1 / log2(10) */ + if (!(coeff = hlsl_new_float_constant(ctx, 0.301029996f, loc))) + return false; + + return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, log, &coeff->node, loc); +} + static bool intrinsic_log2(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -2986,6 +3005,7 @@ intrinsic_functions[] = {"ldexp", 2, true, intrinsic_ldexp}, {"length", 1, true, intrinsic_length}, {"lerp", 3, true, intrinsic_lerp}, + {"log10", 1, true, intrinsic_log10}, {"log2", 1, true, intrinsic_log2}, {"max", 2, true, intrinsic_max}, {"min", 2, true, intrinsic_min}, diff --git a/tests/log.shader_test b/tests/log.shader_test index 9ba0af76..0a99904a 100644 --- a/tests/log.shader_test +++ b/tests/log.shader_test @@ -10,3 +10,16 @@ float4 main() : sv_target uniform 0 float4 2.0 4.0 5.0 0.4 draw quad probe all rgba (1.0, 2.0, 2.32192802, -1.32192802) 1 + +[pixel shader] +uniform float4 x; + +float4 main() : sv_target +{ + return log10(x); +} + +[test] +uniform 0 float4 10.0 100.0 1.0 0.1 +draw quad +probe all rgba (1.0, 2.0, 0.0, -1.0) 1
From: Nikolay Sivov nsivov@codeweavers.com
Signed-off-by: Nikolay Sivov nsivov@codeweavers.com --- libs/vkd3d-shader/hlsl.y | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index e1cde339..c6539762 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2610,6 +2610,25 @@ static bool intrinsic_lerp(struct hlsl_ctx *ctx, return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, params->args[0], mul, loc); }
+static bool intrinsic_log(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_constant *coeff; + struct hlsl_ir_node *log; + + if (!elementwise_intrinsic_float_convert_args(ctx, params, loc)) + return false; + + if (!(log = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_LOG2, params->args[0], loc))) + return false; + + /* ln(2) */ + if (!(coeff = hlsl_new_float_constant(ctx, 0.69314718055f, loc))) + return false; + + return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, log, &coeff->node, loc); +} + static bool intrinsic_log10(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -3005,6 +3024,7 @@ intrinsic_functions[] = {"ldexp", 2, true, intrinsic_ldexp}, {"length", 1, true, intrinsic_length}, {"lerp", 3, true, intrinsic_lerp}, + {"log", 1, true, intrinsic_log}, {"log10", 1, true, intrinsic_log10}, {"log2", 1, true, intrinsic_log2}, {"max", 2, true, intrinsic_max},
On Mon Feb 13 13:27:07 2023 +0000, Henri Verbeet wrote:
+ /* ln(10) / ln(2) */ + if (!(coeff = hlsl_new_float_constant(ctx, 0.301029995664f, loc))) + return false;
That's ln(2) / ln(10), right? (Or 1 / log2(10), if you prefer.) I think the constant is a few digits beyond what a float can actually represent, but I suppose that's not a problem.
You're right, I pushed something for that.
This merge request was approved by Zebediah Figura.
On Mon Feb 13 11:24:01 2023 +0000, Giovanni Mascellani wrote:
Please, also test outside of the domain of definition (i.e., zero and negative numbers).
This is another occasion when d3d9 differs by abs()-ing input, while d3d10+ does not.
On Sat Feb 18 07:54:11 2023 +0000, Nikolay Sivov wrote:
This is another occasion when d3d9 differs by abs()-ing input, while d3d10+ does not.
We could probably get around that by subtracting from the input before taking a logarithm.
On Sat Feb 18 17:37:39 2023 +0000, Zebediah Figura wrote:
We could probably get around that by subtracting from the input before taking a logarithm.
Subtracting where, and what does it achieve?
On Sat Feb 18 19:47:17 2023 +0000, Nikolay Sivov wrote:
Subtracting where, and what does it achieve?
Sorry, never mind, what I said makes no sense.