Zebediah Figura : vkd3d-shader/hlsl: Parse the max() intrinsic.

Alexandre Julliard julliard at winehq.org
Wed Sep 8 15:11:20 CDT 2021


Module: vkd3d
Branch: master
Commit: 99674ab60f417efd7a40f8e5d012ec400b97f2a6
URL:    https://source.winehq.org/git/vkd3d.git/?a=commit;h=99674ab60f417efd7a40f8e5d012ec400b97f2a6

Author: Zebediah Figura <zfigura at codeweavers.com>
Date:   Wed Sep  1 17:20:54 2021 -0500

vkd3d-shader/hlsl: Parse the max() intrinsic.

Signed-off-by: Zebediah Figura <zfigura at codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet at codeweavers.com>
Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>
Signed-off-by: Matteo Bruni <mbruni at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 Makefile.am                 |  2 ++
 libs/vkd3d-shader/hlsl.y    | 67 +++++++++++++++++++++++++++++++++++++++++++++
 tests/max.shader_test       | 23 ++++++++++++++++
 tests/shader_runner_d3d12.c |  2 ++
 4 files changed, 94 insertions(+)

diff --git a/Makefile.am b/Makefile.am
index 721a6b6..09d3755 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -72,6 +72,7 @@ vkd3d_shader_tests = \
 	tests/hlsl-vector-indexing.shader_test \
 	tests/hlsl-vector-indexing-uniform.shader_test \
 	tests/math.shader_test \
+	tests/max.shader_test \
 	tests/preproc-if.shader_test \
 	tests/preproc-ifdef.shader_test \
 	tests/preproc-if-expr.shader_test \
@@ -278,6 +279,7 @@ XFAIL_TESTS = \
 	tests/hlsl-vector-indexing.shader_test \
 	tests/hlsl-vector-indexing-uniform.shader_test \
 	tests/math.shader_test \
+	tests/max.shader_test \
 	tests/trigonometry.shader_test \
 	tests/writemask-assignop-1.shader_test
 endif
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index 2512bcc..d374f64 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -1555,10 +1555,38 @@ static const struct hlsl_ir_function_decl *find_function_call(struct hlsl_ctx *c
     return args.decl;
 }
 
+static bool intrinsic_max(struct hlsl_ctx *ctx,
+        const struct parse_initializer *params, struct vkd3d_shader_location loc)
+{
+    struct hlsl_ir_node *args[3] = {params->args[0], params->args[1]};
+
+    return !!add_expr(ctx, params->instrs, HLSL_OP2_MAX, args, &loc);
+}
+
+static const struct intrinsic_function
+{
+    const char *name;
+    int param_count;
+    bool check_numeric;
+    bool (*handler)(struct hlsl_ctx *ctx, const struct parse_initializer *params, struct vkd3d_shader_location loc);
+}
+intrinsic_functions[] =
+{
+    {"max",                                 2, true,  intrinsic_max},
+};
+
+static int intrinsic_function_name_compare(const void *a, const void *b)
+{
+    const struct intrinsic_function *func = b;
+
+    return strcmp(a, func->name);
+}
+
 static struct list *add_call(struct hlsl_ctx *ctx, const char *name,
         struct parse_initializer *params, struct vkd3d_shader_location loc)
 {
     const struct hlsl_ir_function_decl *decl;
+    struct intrinsic_function *intrinsic;
 
     if ((decl = find_function_call(ctx, name, params)))
     {
@@ -1566,6 +1594,45 @@ static struct list *add_call(struct hlsl_ctx *ctx, const char *name,
         free_parse_initializer(params);
         return NULL;
     }
+    else if ((intrinsic = bsearch(name, intrinsic_functions, ARRAY_SIZE(intrinsic_functions),
+            sizeof(*intrinsic_functions), intrinsic_function_name_compare)))
+    {
+        if (intrinsic->param_count >= 0 && params->args_count != intrinsic->param_count)
+        {
+            hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_WRONG_PARAMETER_COUNT,
+                    "Wrong number of arguments to function '%s': expected %u, but got %u.\n",
+                    name, intrinsic->param_count, params->args_count);
+            free_parse_initializer(params);
+            return NULL;
+        }
+
+        if (intrinsic->check_numeric)
+        {
+            unsigned int i;
+
+            for (i = 0; i < params->args_count; ++i)
+            {
+                if (params->args[i]->data_type->type > HLSL_CLASS_LAST_NUMERIC)
+                {
+                    struct vkd3d_string_buffer *string;
+
+                    if ((string = hlsl_type_to_string(ctx, params->args[i]->data_type)))
+                        hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE,
+                                "Wrong type for argument %u of '%s': expected a numeric type, but got '%s'.\n",
+                                i + 1, name, string->buffer);
+                    hlsl_release_string_buffer(ctx, string);
+                    free_parse_initializer(params);
+                    return NULL;
+                }
+            }
+        }
+
+        if (!intrinsic->handler(ctx, params, loc))
+        {
+            free_parse_initializer(params);
+            return NULL;
+        }
+    }
     else
     {
         hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_NOT_DEFINED, "Function \"%s\" is not defined.", name);
diff --git a/tests/max.shader_test b/tests/max.shader_test
new file mode 100644
index 0000000..56c9a84
--- /dev/null
+++ b/tests/max.shader_test
@@ -0,0 +1,23 @@
+[pixel shader]
+float4 main(uniform float u, uniform float v) : sv_target
+{
+    return float4(max(u, v), max(2, 2.1), max(true, 2), max(-1, -1));
+}
+
+[test]
+uniform 0 float4 0.7 -0.1 0.0 0.0
+draw quad
+probe all rgba (0.7, 2.1, 2.0, -1.0)
+
+[pixel shader]
+float4 main(uniform float2 u, uniform float2 v) : sv_target
+{
+    float3 a = float3(-0.1, 0.2, 0.3);
+
+    return float4(max(u, v), max(a, u));
+}
+
+[test]
+uniform 0 float4 0.7 -0.1 0.4 0.8
+draw quad
+probe all rgba (0.7, 0.8, 0.7, 0.2)
diff --git a/tests/shader_runner_d3d12.c b/tests/shader_runner_d3d12.c
index 3cc859a..2221c0f 100644
--- a/tests/shader_runner_d3d12.c
+++ b/tests/shader_runner_d3d12.c
@@ -136,6 +136,8 @@ static void parse_test_directive(struct shader_context *context, const char *lin
         static const float clear_color[4];
         ID3D12PipelineState *pso;
 
+        if (context->c.root_signature)
+            ID3D12RootSignature_Release(context->c.root_signature);
         context->c.root_signature = create_32bit_constants_root_signature(context->c.device,
                 0, context->uniform_count, D3D12_SHADER_VISIBILITY_ALL);
 




More information about the wine-cvs mailing list