-- v6: vkd3d-shader/hlsl: Support log() intrinsic. vkd3d-shader/hlsl: Support log10() intrinsic. vkd3d-shader/hlsl: Support log2() intrinsic. vkd3d-shader/hlsl: Write 'log' instruction for SM1.
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 606ecdd9..be665981 100644 --- a/libs/vkd3d-shader/hlsl_sm1.c +++ b/libs/vkd3d-shader/hlsl_sm1.c @@ -723,6 +723,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 | 12 ++++++++++++ tests/log.shader_test | 12 ++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/log.shader_test
diff --git a/Makefile.am b/Makefile.am index 48991982..a7abd56a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -122,6 +122,7 @@ vkd3d_shader_tests = \ tests/hlsl-vector-indexing.shader_test \ tests/hlsl-vector-indexing-uniform.shader_test \ tests/lit.shader_test \ + tests/log.shader_test \ tests/logic-operations.shader_test \ tests/majority-syntax.shader_test \ tests/math.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 3873a939..f2990e6f 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2843,6 +2843,17 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx, return true; }
+static bool intrinsic_log2(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_LOG2, arg, loc); +} + static bool intrinsic_max(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -3300,6 +3311,7 @@ intrinsic_functions[] = {"length", 1, true, intrinsic_length}, {"lerp", 3, true, intrinsic_lerp}, {"lit", 3, true, intrinsic_lit}, + {"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
Signed-off-by: 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 f2990e6f..e487902e 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2843,6 +2843,25 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx, return true; }
+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, *arg; + + if (!(arg = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) + return false; + + if (!(log = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_LOG2, arg, 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) { @@ -3311,6 +3330,7 @@ intrinsic_functions[] = {"length", 1, true, intrinsic_length}, {"lerp", 3, true, intrinsic_lerp}, {"lit", 3, true, intrinsic_lit}, + {"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 ++++++++++++++++++++ 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 e487902e..ce20c00e 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2843,6 +2843,25 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx, return true; }
+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, *arg; + + if (!(arg = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) + return false; + + if (!(log = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_LOG2, arg, 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) { @@ -3330,6 +3349,7 @@ intrinsic_functions[] = {"length", 1, true, intrinsic_length}, {"lerp", 3, true, intrinsic_lerp}, {"lit", 3, true, intrinsic_lit}, + {"log", 1, true, intrinsic_log}, {"log10", 1, true, intrinsic_log10}, {"log2", 1, true, intrinsic_log2}, {"max", 2, true, intrinsic_max}, diff --git a/tests/log.shader_test b/tests/log.shader_test index 0a99904a..b0f405d1 100644 --- a/tests/log.shader_test +++ b/tests/log.shader_test @@ -23,3 +23,16 @@ float4 main() : sv_target 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 + +[pixel shader] +uniform float4 x; + +float4 main() : sv_target +{ + return log(x); +} + +[test] +uniform 0 float4 3.0 10.0 1.0 0.1 +draw quad +probe all rgba (1.0986123, 2.302585, 0.0, -2.302585) 2
Anything else I should do here? Question from https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/89#note_23902 I believe was answered.
As far as I can tell we're not yet taking the logarithm of a negative number, and it's not completely clear to me why we cannot do that in a test constrained to SM4+. But I am not sure it's a good idea to waste more time on this, so I'm approving.
This merge request was approved by Giovanni Mascellani.
On Wed Apr 12 11:20:44 2023 +0000, Giovanni Mascellani wrote:
As far as I can tell we're not yet taking the logarithm of a negative number, and it's not completely clear to me why we cannot do that in a test constrained to SM4+. But I am not sure it's a good idea to waste more time on this, so I'm approving.
For SM4+ it will results in nans, for older versions it will abs() input. None of that behavior is compiled in as a part of intrinsic.
For SM4+ it will results in nans, for older versions it will abs() input.
Yes, but that is part of what makes this useful to test.
Somewhat similarly, there are other known (and likely some unknown) differences in behaviour between the different shader models. It's helpful for shader_runner to be able to handle those for the cases we care about.
This merge request was approved by Henri Verbeet.