From cf6a3ad6768a56ea16c6a74b822cc9178b221c45 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Sun, 29 Sep 2024 21:04:57 +0200 Subject: [PATCH] vkd3d-shader/glsl: Implement VKD3DSIH_SAMPLE. --- libs/vkd3d-shader/glsl.c | 84 +++++++++++++++++++ tests/hlsl/cbuffer.shader_test | 2 +- tests/hlsl/combined-samplers.shader_test | 16 ++-- tests/hlsl/conditional.shader_test | 10 +-- tests/hlsl/object-references.shader_test | 2 +- .../register-reservations-numeric.shader_test | 2 +- tests/hlsl/sampler.shader_test | 4 +- tests/hlsl/srv-buffers.shader_test | 2 +- tests/hlsl/static-initializer.shader_test | 4 +- tests/hlsl/texture-ordering.shader_test | 10 +-- 10 files changed, 110 insertions(+), 26 deletions(-) diff --git a/libs/vkd3d-shader/glsl.c b/libs/vkd3d-shader/glsl.c index cf722340..e3942900 100644 --- a/libs/vkd3d-shader/glsl.c +++ b/libs/vkd3d-shader/glsl.c @@ -727,6 +727,87 @@ static void shader_glsl_ld(struct vkd3d_glsl_generator *gen, const struct vkd3d_ glsl_dst_cleanup(&dst, &gen->string_buffers); } +static void shader_glsl_sample(struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_instruction *ins) +{ + const struct glsl_resource_type_info *resource_type_info; + unsigned int resource_id, resource_idx, resource_space; + unsigned int sampler_id, sampler_idx, sampler_space; + const struct vkd3d_shader_descriptor_info1 *d; + enum vkd3d_shader_component_type sampled_type; + enum vkd3d_shader_resource_type resource_type; + struct vkd3d_string_buffer *sample; + enum vkd3d_data_type data_type; + struct glsl_src coord; + struct glsl_dst dst; + uint32_t coord_mask; + + if (vkd3d_shader_instruction_has_texel_offset(ins)) + vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL, + "Internal compiler error: Unhandled texel sample offset."); + + if (ins->src[1].reg.idx[0].rel_addr || ins->src[1].reg.idx[1].rel_addr + || ins->src[2].reg.idx[0].rel_addr || ins->src[2].reg.idx[1].rel_addr) + vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_UNSUPPORTED, + "Descriptor indexing is not supported."); + + resource_id = ins->src[1].reg.idx[0].offset; + resource_idx = ins->src[1].reg.idx[1].offset; + if ((d = shader_glsl_get_descriptor_by_id(gen, VKD3D_SHADER_DESCRIPTOR_TYPE_SRV, resource_id))) + { + resource_type = d->resource_type; + resource_space = d->register_space; + sampled_type = vkd3d_component_type_from_resource_data_type(d->resource_data_type); + data_type = vkd3d_data_type_from_component_type(sampled_type); + } + else + { + vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL, + "Internal compiler error: Undeclared resource descriptor %u.", resource_id); + resource_space = 0; + resource_type = VKD3D_SHADER_RESOURCE_TEXTURE_2D; + data_type = VKD3D_DATA_FLOAT; + } + + if ((resource_type_info = shader_glsl_get_resource_type_info(resource_type))) + { + coord_mask = vkd3d_write_mask_from_component_count(resource_type_info->coord_size); + } + else + { + vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL, + "Internal compiler error: Unhandled resource type %#x.", resource_type); + coord_mask = vkd3d_write_mask_from_component_count(2); + } + + sampler_id = ins->src[2].reg.idx[0].offset; + sampler_idx = ins->src[2].reg.idx[1].offset; + if ((d = shader_glsl_get_descriptor_by_id(gen, VKD3D_SHADER_DESCRIPTOR_TYPE_SAMPLER, sampler_id))) + { + sampler_space = d->register_space; + } + else + { + vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL, + "Internal compiler error: Undeclared sampler descriptor %u.", sampler_id); + sampler_space = 0; + } + + glsl_dst_init(&dst, gen, ins, &ins->dst[0]); + glsl_src_init(&coord, gen, &ins->src[0], coord_mask); + sample = vkd3d_string_buffer_get(&gen->string_buffers); + + vkd3d_string_buffer_printf(sample, "texture("); + shader_glsl_print_combined_sampler_name(sample, gen, resource_idx, resource_space, sampler_idx, sampler_space); + vkd3d_string_buffer_printf(sample, ", %s)", coord.str->buffer); + shader_glsl_print_swizzle(sample, ins->src[1].swizzle, ins->dst[0].write_mask); + + shader_glsl_print_assignment_ext(gen, &dst, data_type, "%s", sample->buffer); + + vkd3d_string_buffer_release(&gen->string_buffers, sample); + glsl_src_cleanup(&coord, &gen->string_buffers); + glsl_dst_cleanup(&dst, &gen->string_buffers); +} + static void shader_glsl_unary_op(struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_instruction *ins, const char *op) { @@ -1054,6 +1135,9 @@ static void vkd3d_glsl_handle_instruction(struct vkd3d_glsl_generator *gen, case VKD3DSIH_RSQ: shader_glsl_intrinsic(gen, ins, "inversesqrt"); break; + case VKD3DSIH_SAMPLE: + shader_glsl_sample(gen, ins); + break; case VKD3DSIH_SQRT: shader_glsl_intrinsic(gen, ins, "sqrt"); break; diff --git a/tests/hlsl/cbuffer.shader_test b/tests/hlsl/cbuffer.shader_test index 1f44a895..725c3679 100644 --- a/tests/hlsl/cbuffer.shader_test +++ b/tests/hlsl/cbuffer.shader_test @@ -577,7 +577,7 @@ float4 main() : sv_target uniform 0 float4 1.0 0.0 0.0 0.0 uniform 4 float4 0.0 2.0 0.0 0.0 uniform 8 float4 0.0 0.0 3.0 0.0 -todo(glsl) draw quad +draw quad probe (0, 0) rgba (1.0, 2.0, 0.0, 4.0) diff --git a/tests/hlsl/combined-samplers.shader_test b/tests/hlsl/combined-samplers.shader_test index 1eb181ca..4d3b507d 100644 --- a/tests/hlsl/combined-samplers.shader_test +++ b/tests/hlsl/combined-samplers.shader_test @@ -45,7 +45,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (0, 0, 0, 1) @@ -61,7 +61,7 @@ float4 main() : sv_target [test] uniform 0 float4 0.5 0.0 0.0 0.0 -todo(glsl) draw quad +draw quad probe (0, 0) rgba (0.5, 0.5, 0.5, 1.5) @@ -77,7 +77,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad if(sm<4) todo probe (0, 0) rgba (1, 1, 1, 11) if(sm>=4) probe (0, 0) rgba (10, 10, 10, 11) @@ -92,7 +92,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad if(sm<4) todo probe (0, 0) rgba (1, 1, 1, 11) if(sm>=4) probe (0, 0) rgba (10, 10, 10, 11) @@ -107,7 +107,7 @@ float4 main() : sv_target } [test] -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (21, 21, 21, 11) @@ -123,7 +123,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad if(sm<4) probe (0, 0) rgba (102, 102, 102, 111) if(sm>=4) probe (0, 0) rgba (12, 12, 12, 111) @@ -139,7 +139,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (104, 104, 104, 111) @@ -163,7 +163,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (1, 1, 1, 11) diff --git a/tests/hlsl/conditional.shader_test b/tests/hlsl/conditional.shader_test index 5404fe02..c199d138 100644 --- a/tests/hlsl/conditional.shader_test +++ b/tests/hlsl/conditional.shader_test @@ -243,10 +243,10 @@ float4 main() : sv_target [test] uniform 0 float -2 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.0, 0.0, 0.0, 4.0) uniform 0 float 4 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (1.0, 2.0, 3.0, 4.0) @@ -267,11 +267,11 @@ float4 main() : sv_target [test] uniform 0 float 2 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.0, 0.0, 0.0, 4.0) uniform 0 float 1 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (1.0, 0.0, 0.0, 4.0) uniform 0 float 0 -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (1.0, 1.0, 0.0, 4.0) diff --git a/tests/hlsl/object-references.shader_test b/tests/hlsl/object-references.shader_test index 0ab10977..a8f0a5d0 100644 --- a/tests/hlsl/object-references.shader_test +++ b/tests/hlsl/object-references.shader_test @@ -46,7 +46,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (77.77, 77.77, 77.77, 77.77) diff --git a/tests/hlsl/register-reservations-numeric.shader_test b/tests/hlsl/register-reservations-numeric.shader_test index 88a7b8e6..4117dac8 100644 --- a/tests/hlsl/register-reservations-numeric.shader_test +++ b/tests/hlsl/register-reservations-numeric.shader_test @@ -527,7 +527,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad if(sm<6) probe (0, 0) rgba(2.0, 2.0, 2.0, 2.0) if(sm>=6) probe (0, 0) rgba(0.5, 0.5, 0.5, 0.5) diff --git a/tests/hlsl/sampler.shader_test b/tests/hlsl/sampler.shader_test index 63063b0c..13e47731 100644 --- a/tests/hlsl/sampler.shader_test +++ b/tests/hlsl/sampler.shader_test @@ -17,7 +17,7 @@ float4 main() : sv_target } [test] -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.25, 0, 0.25, 0) [pixel shader todo(sm<4)] @@ -30,7 +30,7 @@ float4 main() : sv_target } [test] -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (0.25, 0, 0.25, 0) [pixel shader fail] diff --git a/tests/hlsl/srv-buffers.shader_test b/tests/hlsl/srv-buffers.shader_test index f715b492..db87b49c 100644 --- a/tests/hlsl/srv-buffers.shader_test +++ b/tests/hlsl/srv-buffers.shader_test @@ -67,7 +67,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (3.0, 4.0, -1.0, -1.0) diff --git a/tests/hlsl/static-initializer.shader_test b/tests/hlsl/static-initializer.shader_test index 75e88c81..8c3b95eb 100644 --- a/tests/hlsl/static-initializer.shader_test +++ b/tests/hlsl/static-initializer.shader_test @@ -147,7 +147,7 @@ float4 main() : sv_target } [test] -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (1, 2, 3, 4) @@ -163,7 +163,7 @@ float4 main() : sv_target } [test] -todo(sm<4 | glsl) draw quad +todo(sm<4) draw quad probe (0, 0) rgba (1, 2, 3, 4) diff --git a/tests/hlsl/texture-ordering.shader_test b/tests/hlsl/texture-ordering.shader_test index 1bedd3c5..6486bdb1 100644 --- a/tests/hlsl/texture-ordering.shader_test +++ b/tests/hlsl/texture-ordering.shader_test @@ -136,7 +136,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (450, 139, 876, 333) @@ -189,7 +189,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (450, 138, 796, 333) @@ -242,7 +242,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (478, 913, 256, 333) @@ -275,7 +275,7 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (215, 215, 215, 111) @@ -305,5 +305,5 @@ float4 main() : sv_target } [test] -todo(glsl) draw quad +draw quad probe (0, 0) rgba (5, 4, 2, 0)