From 93f1af721f16eae390b531bf0f441f5909f7e7a3 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Thu, 3 Oct 2024 15:07:16 +0200 Subject: [PATCH] vkd3d-shader/glsl: Implement loops. --- libs/vkd3d-shader/glsl.c | 46 ++++++++++++++++++++------ tests/hlsl/function-return.shader_test | 20 +++++------ tests/hlsl/loop.shader_test | 6 ++-- tests/hlsl/return.shader_test | 20 +++++------ 4 files changed, 59 insertions(+), 33 deletions(-) diff --git a/libs/vkd3d-shader/glsl.c b/libs/vkd3d-shader/glsl.c index ce81e922..848b9879 100644 --- a/libs/vkd3d-shader/glsl.c +++ b/libs/vkd3d-shader/glsl.c @@ -651,6 +651,20 @@ static void shader_glsl_cast(struct vkd3d_glsl_generator *gen, const struct vkd3 glsl_dst_cleanup(&dst, &gen->string_buffers); } +static void shader_glsl_end_block(struct vkd3d_glsl_generator *gen) +{ + --gen->indent; + shader_glsl_print_indent(gen->buffer, gen->indent); + vkd3d_string_buffer_printf(gen->buffer, "}\n"); +} + +static void shader_glsl_begin_block(struct vkd3d_glsl_generator *gen) +{ + shader_glsl_print_indent(gen->buffer, gen->indent); + vkd3d_string_buffer_printf(gen->buffer, "{\n"); + ++gen->indent; +} + static void shader_glsl_if(struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_instruction *ins) { const char *condition; @@ -664,23 +678,28 @@ static void shader_glsl_if(struct vkd3d_glsl_generator *gen, const struct vkd3d_ glsl_src_cleanup(&src, &gen->string_buffers); - shader_glsl_print_indent(gen->buffer, gen->indent); - vkd3d_string_buffer_printf(gen->buffer, "{\n"); - ++gen->indent; + shader_glsl_begin_block(gen); } static void shader_glsl_else(struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_instruction *ins) { - unsigned int i = 4 * (gen->indent - 1); - - vkd3d_string_buffer_printf(gen->buffer, "%*s}\n%*selse\n%*s{\n", i, "", i, "", i, ""); + shader_glsl_end_block(gen); + shader_glsl_print_indent(gen->buffer, gen->indent); + vkd3d_string_buffer_printf(gen->buffer, "else\n"); + shader_glsl_begin_block(gen); } -static void shader_glsl_endif(struct vkd3d_glsl_generator *gen) +static void shader_glsl_loop(struct vkd3d_glsl_generator *gen) { - --gen->indent; shader_glsl_print_indent(gen->buffer, gen->indent); - vkd3d_string_buffer_printf(gen->buffer, "}\n"); + vkd3d_string_buffer_printf(gen->buffer, "for (;;)\n"); + shader_glsl_begin_block(gen); +} + +static void shader_glsl_break(struct vkd3d_glsl_generator *gen) +{ + shader_glsl_print_indent(gen->buffer, gen->indent); + vkd3d_string_buffer_printf(gen->buffer, "break;\n"); } static void shader_glsl_ld(struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_instruction *ins) @@ -1130,6 +1149,9 @@ static void vkd3d_glsl_handle_instruction(struct vkd3d_glsl_generator *gen, case VKD3DSIH_AND: shader_glsl_binop(gen, ins, "&"); break; + case VKD3DSIH_BREAK: + shader_glsl_break(gen); + break; case VKD3DSIH_DCL_INDEXABLE_TEMP: shader_glsl_dcl_indexable_temp(gen, ins); break; @@ -1157,7 +1179,8 @@ static void vkd3d_glsl_handle_instruction(struct vkd3d_glsl_generator *gen, shader_glsl_else(gen, ins); break; case VKD3DSIH_ENDIF: - shader_glsl_endif(gen); + case VKD3DSIH_ENDLOOP: + shader_glsl_end_block(gen); break; case VKD3DSIH_EQO: case VKD3DSIH_IEQ: @@ -1223,6 +1246,9 @@ static void vkd3d_glsl_handle_instruction(struct vkd3d_glsl_generator *gen, case VKD3DSIH_LOG: shader_glsl_intrinsic(gen, ins, "log2"); break; + case VKD3DSIH_LOOP: + shader_glsl_loop(gen); + break; case VKD3DSIH_MOV: shader_glsl_mov(gen, ins); break; diff --git a/tests/hlsl/function-return.shader_test b/tests/hlsl/function-return.shader_test index 995953b0..b70ae706 100644 --- a/tests/hlsl/function-return.shader_test +++ b/tests/hlsl/function-return.shader_test @@ -235,23 +235,23 @@ float4 main() : sv_target [test] uniform 0 float 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.3, 0.2, 0.3, 0.3) 1 uniform 0 float 0.1 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.3, 0.3, 0.3, 0.3) 1 uniform 0 float 0.3 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.3, 0.5, 0.3, 0.3) 1 uniform 0 float 0.7 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.3, 0.9, 0.7, 0.6) 1 uniform 0 float 0.9 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.4, 0.1, 0.7, 0.6) 1 [pixel shader todo(sm<4)] @@ -291,21 +291,21 @@ float4 main() : sv_target uniform 0 float4 0.3 0.0 0.0 0.0 uniform 4 float4 0.0 0.0 0.0 0.0 uniform 8 float4 0.1 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.3, 0.2, 0.6, 0.6) 1 uniform 4 float4 0.35 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.3, 0.3, 0.6, 0.6) 1 uniform 8 float4 0.5 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.3, 0.5, 0.6, 0.6) 1 uniform 0 float4 1.0 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.3, 0.5, 0.6, 0.6) 1 uniform 4 float4 2.0 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.4, 0.1, 0.6, 0.6) 1 diff --git a/tests/hlsl/loop.shader_test b/tests/hlsl/loop.shader_test index d59a6463..906c768f 100644 --- a/tests/hlsl/loop.shader_test +++ b/tests/hlsl/loop.shader_test @@ -118,7 +118,7 @@ float4 main() : sv_target } [test] -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (10.0, 10.0, 10.0, 10.0) [pixel shader todo(sm<4)] @@ -137,7 +137,7 @@ float4 main() : sv_target } [test] -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (10.0, 10.0, 10.0, 10.0) [pixel shader todo(sm<4)] @@ -156,7 +156,7 @@ float4 main() : sv_target } [test] -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (10.0, 10.0, 10.0, 10.0) % unroll can't be used with fastopt or loop diff --git a/tests/hlsl/return.shader_test b/tests/hlsl/return.shader_test index c31f1111..6c6ae843 100644 --- a/tests/hlsl/return.shader_test +++ b/tests/hlsl/return.shader_test @@ -160,23 +160,23 @@ void main(out float4 ret : sv_target) [test] uniform 0 float 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.1, 0.1, 0.1, 0.1) 1 uniform 0 float 0.1 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.2, 0.2, 0.2, 0.2) 1 uniform 0 float 0.3 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.4, 0.4, 0.4, 0.4) 1 uniform 0 float 0.7 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.8, 0.8, 0.8, 0.8) 1 uniform 0 float 0.9 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.9, 0.9, 0.9, 0.9) 1 [pixel shader todo(sm<4)] @@ -236,21 +236,21 @@ void main(out float4 ret : sv_target) uniform 0 float4 0.3 0.0 0.0 0.0 uniform 4 float4 0.0 0.0 0.0 0.0 uniform 8 float4 0.1 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.1, 0.1, 0.1, 0.1) 1 uniform 4 float4 0.35 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.2, 0.2, 0.2, 0.2) 1 uniform 8 float4 0.5 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.4, 0.4, 0.4, 0.4) 1 uniform 0 float4 1.0 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.4, 0.4, 0.4, 0.4) 1 uniform 4 float4 2.0 0.0 0.0 0.0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.9, 0.9, 0.9, 0.9) 1