This commit includes work by Francisco Casas.
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com --- libs/vkd3d-shader/hlsl_constant_ops.c | 67 ++++++++++++++++++++++++ libs/vkd3d-shader/vkd3d_shader_private.h | 2 + tests/arithmetic-float.shader_test | 16 ++++++ tests/arithmetic-int.shader_test | 11 +++- tests/arithmetic-uint.shader_test | 9 ++++ 5 files changed, 104 insertions(+), 1 deletion(-)
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 5cac4bde..9a8bc6b0 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -255,6 +255,69 @@ static bool fold_nequal(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, return true; }
+static bool fold_div(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, + struct hlsl_ir_constant *src1, struct hlsl_ir_constant *src2) +{ + enum hlsl_base_type type = dst->node.data_type->base_type; + unsigned int k; + + assert(type == src1->node.data_type->base_type); + assert(type == src2->node.data_type->base_type); + + for (k = 0; k < dst->node.data_type->dimx; ++k) + { + switch (type) + { + case HLSL_TYPE_FLOAT: + case HLSL_TYPE_HALF: + if (src2->value[k].f == 0) + { + hlsl_warning(ctx, &dst->node.loc, VKD3D_SHADER_WARNING_HLSL_DIVISION_BY_ZERO, + "Floating point division by zero"); + } + dst->value[k].f = src1->value[k].f / src2->value[k].f; + break; + + case HLSL_TYPE_DOUBLE: + if (src2->value[k].d == 0) + { + hlsl_warning(ctx, &dst->node.loc, VKD3D_SHADER_WARNING_HLSL_DIVISION_BY_ZERO, + "Floating point division by zero"); + } + dst->value[k].d = src1->value[k].d / src2->value[k].d; + break; + + case HLSL_TYPE_INT: + if (src2->value[k].i == 0) + { + hlsl_error(ctx, &dst->node.loc, VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO, + "Division by zero."); + return false; + } + if (src1->value[k].i == INT_MIN && src2->value[k].i == -1) + dst->value[k].i = INT_MIN; + else + dst->value[k].i = src1->value[k].i / src2->value[k].i; + break; + + case HLSL_TYPE_UINT: + if (src2->value[k].u == 0) + { + hlsl_error(ctx, &dst->node.loc, VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO, + "Division by zero."); + return false; + } + dst->value[k].u = src1->value[k].u / src2->value[k].u; + break; + + default: + FIXME("Fold division for type %s.\n", debug_hlsl_type(ctx, dst->node.data_type)); + return false; + } + } + return true; +} + bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { struct hlsl_ir_constant *arg1, *arg2 = NULL, *res; @@ -308,6 +371,10 @@ bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void success = fold_nequal(ctx, res, arg1, arg2); break;
+ case HLSL_OP2_DIV: + success = fold_div(ctx, res, arg1, arg2); + break; + default: FIXME("Fold "%s" expression.\n", debug_hlsl_expr_op(expr->op)); success = false; diff --git a/libs/vkd3d-shader/vkd3d_shader_private.h b/libs/vkd3d-shader/vkd3d_shader_private.h index 21f23709..29e178cc 100644 --- a/libs/vkd3d-shader/vkd3d_shader_private.h +++ b/libs/vkd3d-shader/vkd3d_shader_private.h @@ -117,8 +117,10 @@ enum vkd3d_shader_error VKD3D_SHADER_ERROR_HLSL_INVALID_TEXEL_OFFSET = 5018, VKD3D_SHADER_ERROR_HLSL_OFFSET_OUT_OF_BOUNDS = 5019, VKD3D_SHADER_ERROR_HLSL_INCOMPATIBLE_PROFILE = 5020, + VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO = 5021,
VKD3D_SHADER_WARNING_HLSL_IMPLICIT_TRUNCATION = 5300, + VKD3D_SHADER_WARNING_HLSL_DIVISION_BY_ZERO = 5301,
VKD3D_SHADER_ERROR_GLSL_INTERNAL = 6000,
diff --git a/tests/arithmetic-float.shader_test b/tests/arithmetic-float.shader_test index d55dc9d4..f99b9728 100644 --- a/tests/arithmetic-float.shader_test +++ b/tests/arithmetic-float.shader_test @@ -23,3 +23,19 @@ float4 main() : SV_TARGET [test] todo draw quad probe all rgba (5.0, 5.0, -5.0, 3.0) + +[require] +shader model >= 4.0 + +[pixel shader] +float4 main() : SV_TARGET +{ + float x = 1; + float y = 0; + + return x / y; +} + +[test] +draw quad +probe all rgba (1e99, 1e99, 1e99, 1e99) diff --git a/tests/arithmetic-int.shader_test b/tests/arithmetic-int.shader_test index 5d9cc029..c85b9a3a 100644 --- a/tests/arithmetic-int.shader_test +++ b/tests/arithmetic-int.shader_test @@ -8,7 +8,7 @@ float4 main() : SV_TARGET }
[test] -todo draw quad +draw quad probe all rgba (20.0, -10.0, 75.0, 0.0)
[pixel shader] @@ -23,3 +23,12 @@ float4 main() : SV_TARGET [test] todo draw quad probe all rgba (5.0, 5.0, -5.0, 3.0) + +[pixel shader fail] +float4 main() : SV_TARGET +{ + int x = 1; + int y = 0; + + return x / y; +} diff --git a/tests/arithmetic-uint.shader_test b/tests/arithmetic-uint.shader_test index b016dbb6..f37c881e 100644 --- a/tests/arithmetic-uint.shader_test +++ b/tests/arithmetic-uint.shader_test @@ -26,3 +26,12 @@ float4 main() : SV_TARGET [test] draw quad probe all rgba (5.0, 5.0, 4294967296.0, 3.0) + +[pixel shader fail] +float4 main() : SV_TARGET +{ + uint x = 1; + uint y = 0; + + return x / y; +}