From cd094f7dbb5823a4dce923eefff4291900730058 Mon Sep 17 00:00:00 2001 From: Iago Toral Quiroga Date: Tue, 7 May 2024 10:51:05 +0200 Subject: [PATCH] broadcom/compiler: fix num_textures for precompiled shaders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some shaders use N textures but not bind these in consecutive slots, so then we find texture accesses to indices that exceed the number of textures and we assert crash. This happens with various shaders from shader-db. Fix that by looking at the largest binding used in the shader and using that as the number of textures used. Reviewed-by: Alejandro PiƱeiro Part-of: --- src/gallium/drivers/v3d/v3d_program.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/gallium/drivers/v3d/v3d_program.c b/src/gallium/drivers/v3d/v3d_program.c index 45cc1c5bb6c..5436411e65e 100644 --- a/src/gallium/drivers/v3d/v3d_program.c +++ b/src/gallium/drivers/v3d/v3d_program.c @@ -602,12 +602,25 @@ v3d_setup_shared_precompile_key(struct v3d_uncompiled_shader *uncompiled, { nir_shader *s = uncompiled->base.ir.nir; + /* The shader may have gaps in the texture bindings, so figure out + * the largest binding in use and setup the number of textures and + * samplers from there instead of just the texture count from shader + * info. + */ + key->num_tex_used = 0; + key->num_samplers_used = 0; + for (int i = V3D_MAX_TEXTURE_SAMPLERS - 1; i >= 0; i--) { + if (s->info.textures_used[0] & (1 << i)) { + key->num_tex_used = i + 1; + key->num_samplers_used = i + 1; + break; + } + } + /* Note that below we access they key's texture and sampler fields * using the same index. On OpenGL they are the same (they are * combined) */ - key->num_tex_used = s->info.num_textures; - key->num_samplers_used = s->info.num_textures; for (int i = 0; i < s->info.num_textures; i++) { key->sampler[i].return_size = 16; key->sampler[i].return_channels = 2;