llvmpipe: overhaul fs/cs variant keys to be simpler.

These currently always had one sampler state embedded, but got messy
when images was 1 and samplers was 0.

This should fix some undefined reads seen

Fixes: e639e311a1 ("llvmpipe/cs: overhaul cs variant key state.")
Reviewed-by: Roland Scheidegger <sroland@vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13012>
This commit is contained in:
Dave Airlie 2021-09-24 13:01:16 +10:00
parent b9262960ff
commit 690cc3bb80
8 changed files with 75 additions and 41 deletions

View file

@ -175,7 +175,7 @@ lp_fs_linear_run(const struct lp_rast_state *state,
if (!lp_linear_init_sampler(&samp[i], if (!lp_linear_init_sampler(&samp[i],
tex_info, tex_info,
&variant->key.samplers[unit], lp_fs_variant_key_sampler_idx(&variant->key, unit),
&state->jit_context.textures[unit], &state->jit_context.textures[unit],
x, y, width, height, x, y, width, height,
a0, dadx, dady)) { a0, dadx, dady)) {
@ -315,7 +315,8 @@ lp_linear_check_variant(struct lp_fragment_shader_variant *variant)
goto fail; goto fail;
} }
if (!lp_linear_check_sampler(&key->samplers[unit], tex_info)) { struct lp_sampler_static_state *samp = lp_fs_variant_key_sampler_idx(key, unit);
if (!lp_linear_check_sampler(samp, tex_info)) {
if (LP_DEBUG & DEBUG_LINEAR) if (LP_DEBUG & DEBUG_LINEAR)
debug_printf(" -- samp[%d]: check_sampler failed\n", i); debug_printf(" -- samp[%d]: check_sampler failed\n", i);
goto fail; goto fail;

View file

@ -199,11 +199,15 @@ lp_linear_purple(const struct lp_rast_state *state,
boolean boolean
lp_linear_check_fastpath(struct lp_fragment_shader_variant *variant) lp_linear_check_fastpath(struct lp_fragment_shader_variant *variant)
{ {
enum pipe_format tex_format = variant->key.samplers[0].texture_state.format; struct lp_sampler_static_state *samp0 = lp_fs_variant_key_sampler_idx(&variant->key, 0);
if (!samp0)
return false;
enum pipe_format tex_format = samp0->texture_state.format;
if (variant->shader->kind == LP_FS_KIND_BLIT_RGBA && if (variant->shader->kind == LP_FS_KIND_BLIT_RGBA &&
tex_format == PIPE_FORMAT_B8G8R8A8_UNORM && tex_format == PIPE_FORMAT_B8G8R8A8_UNORM &&
is_nearest_clamp_sampler(&variant->key.samplers[0]) && is_nearest_clamp_sampler(samp0) &&
variant->opaque) { variant->opaque) {
variant->jit_linear_blit = lp_linear_blit_rgba_blit; variant->jit_linear_blit = lp_linear_blit_rgba_blit;
} }
@ -212,7 +216,7 @@ lp_linear_check_fastpath(struct lp_fragment_shader_variant *variant)
variant->opaque && variant->opaque &&
(tex_format == PIPE_FORMAT_B8G8R8A8_UNORM || (tex_format == PIPE_FORMAT_B8G8R8A8_UNORM ||
tex_format == PIPE_FORMAT_B8G8R8X8_UNORM) && tex_format == PIPE_FORMAT_B8G8R8X8_UNORM) &&
is_nearest_clamp_sampler(&variant->key.samplers[0])) { is_nearest_clamp_sampler(samp0)) {
variant->jit_linear_blit = lp_linear_blit_rgb1_blit; variant->jit_linear_blit = lp_linear_blit_rgb1_blit;
} }

View file

@ -173,8 +173,10 @@ lp_setup_is_blit(const struct lp_setup_context *setup,
* texture filtering. * texture filtering.
*/ */
assert(variant->key.samplers[0].sampler_state.min_img_filter == PIPE_TEX_FILTER_NEAREST); ASSERTED struct lp_sampler_static_state *samp0 = lp_fs_variant_key_sampler_idx(&variant->key, 0);
assert(variant->key.samplers[0].sampler_state.mag_img_filter == PIPE_TEX_FILTER_NEAREST); assert(samp0);
assert(samp0->sampler_state.min_img_filter == PIPE_TEX_FILTER_NEAREST);
assert(samp0->sampler_state.mag_img_filter == PIPE_TEX_FILTER_NEAREST);
/* /*
* Check for 1:1 match of texels to dest pixels * Check for 1:1 match of texels to dest pixels

View file

@ -179,7 +179,7 @@ generate_compute(struct llvmpipe_context *lp,
builder = gallivm->builder; builder = gallivm->builder;
assert(builder); assert(builder);
LLVMPositionBuilderAtEnd(builder, block); LLVMPositionBuilderAtEnd(builder, block);
sampler = lp_llvm_sampler_soa_create(key->samplers, key->nr_samplers); sampler = lp_llvm_sampler_soa_create(lp_cs_variant_key_samplers(key), key->nr_samplers);
image = lp_llvm_image_soa_create(lp_cs_variant_key_images(key), key->nr_images); image = lp_llvm_image_soa_create(lp_cs_variant_key_images(key), key->nr_images);
struct lp_build_loop_state loop_state[4]; struct lp_build_loop_state loop_state[4];
@ -578,7 +578,7 @@ make_variant_key(struct llvmpipe_context *lp,
int i; int i;
struct lp_compute_shader_variant_key *key; struct lp_compute_shader_variant_key *key;
key = (struct lp_compute_shader_variant_key *)store; key = (struct lp_compute_shader_variant_key *)store;
memset(key, 0, offsetof(struct lp_compute_shader_variant_key, samplers[1])); memset(key, 0, sizeof(*key));
/* This value will be the same for all the variants of a given shader: /* This value will be the same for all the variants of a given shader:
*/ */
@ -586,7 +586,9 @@ make_variant_key(struct llvmpipe_context *lp,
struct lp_sampler_static_state *cs_sampler; struct lp_sampler_static_state *cs_sampler;
cs_sampler = key->samplers; cs_sampler = lp_cs_variant_key_samplers(key);
memset(cs_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *cs_sampler);
for(i = 0; i < key->nr_samplers; ++i) { for(i = 0; i < key->nr_samplers; ++i) {
if(shader->info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) { if(shader->info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) {
lp_sampler_static_sampler_state(&cs_sampler[i].sampler_state, lp_sampler_static_sampler_state(&cs_sampler[i].sampler_state,
@ -642,7 +644,8 @@ dump_cs_variant_key(const struct lp_compute_shader_variant_key *key)
debug_printf("cs variant %p:\n", (void *) key); debug_printf("cs variant %p:\n", (void *) key);
for (i = 0; i < key->nr_samplers; ++i) { for (i = 0; i < key->nr_samplers; ++i) {
const struct lp_static_sampler_state *sampler = &key->samplers[i].sampler_state; const struct lp_sampler_static_state *samplers = lp_cs_variant_key_samplers(key);
const struct lp_static_sampler_state *sampler = &samplers[i].sampler_state;
debug_printf("sampler[%u] = \n", i); debug_printf("sampler[%u] = \n", i);
debug_printf(" .wrap = %s %s %s\n", debug_printf(" .wrap = %s %s %s\n",
util_str_tex_wrap(sampler->wrap_s, TRUE), util_str_tex_wrap(sampler->wrap_s, TRUE),
@ -664,7 +667,8 @@ dump_cs_variant_key(const struct lp_compute_shader_variant_key *key)
debug_printf(" .aniso = %u\n", sampler->aniso); debug_printf(" .aniso = %u\n", sampler->aniso);
} }
for (i = 0; i < key->nr_sampler_views; ++i) { for (i = 0; i < key->nr_sampler_views; ++i) {
const struct lp_static_texture_state *texture = &key->samplers[i].texture_state; const struct lp_sampler_static_state *samplers = lp_cs_variant_key_samplers(key);
const struct lp_static_texture_state *texture = &samplers[i].texture_state;
debug_printf("texture[%u] = \n", i); debug_printf("texture[%u] = \n", i);
debug_printf(" .format = %s\n", debug_printf(" .format = %s\n",
util_format_name(texture->format)); util_format_name(texture->format));

View file

@ -42,8 +42,6 @@ struct lp_compute_shader_variant_key
unsigned nr_samplers:8; unsigned nr_samplers:8;
unsigned nr_sampler_views:8; unsigned nr_sampler_views:8;
unsigned nr_images:8; unsigned nr_images:8;
/* followed by variable number of images */
struct lp_sampler_static_state samplers[1];
}; };
#define LP_CS_MAX_VARIANT_KEY_SIZE \ #define LP_CS_MAX_VARIANT_KEY_SIZE \
@ -54,17 +52,22 @@ struct lp_compute_shader_variant_key
static inline size_t static inline size_t
lp_cs_variant_key_size(unsigned nr_samplers, unsigned nr_images) lp_cs_variant_key_size(unsigned nr_samplers, unsigned nr_images)
{ {
unsigned samplers = nr_samplers > 1 ? (nr_samplers - 1) : 0;
return (sizeof(struct lp_compute_shader_variant_key) + return (sizeof(struct lp_compute_shader_variant_key) +
samplers * sizeof(struct lp_sampler_static_state) + nr_samplers * sizeof(struct lp_sampler_static_state) +
nr_images * sizeof(struct lp_image_static_state)); nr_images * sizeof(struct lp_image_static_state));
} }
static inline struct lp_sampler_static_state *
lp_cs_variant_key_samplers(const struct lp_compute_shader_variant_key *key)
{
return (struct lp_sampler_static_state *)&(key[1]);
}
static inline struct lp_image_static_state * static inline struct lp_image_static_state *
lp_cs_variant_key_images(const struct lp_compute_shader_variant_key *key) lp_cs_variant_key_images(const struct lp_compute_shader_variant_key *key)
{ {
return (struct lp_image_static_state *) return (struct lp_image_static_state *)
&key->samplers[key->nr_samplers]; &(lp_cs_variant_key_samplers(key)[key->nr_samplers]);
} }
struct lp_cs_variant_list_item struct lp_cs_variant_list_item

View file

@ -3158,7 +3158,7 @@ generate_fragment(struct llvmpipe_context *lp,
} }
/* code generated texture sampling */ /* code generated texture sampling */
sampler = lp_llvm_sampler_soa_create(key->samplers, key->nr_samplers); sampler = lp_llvm_sampler_soa_create(lp_fs_variant_key_samplers(key), key->nr_samplers);
image = lp_llvm_image_soa_create(lp_fs_variant_key_images(key), key->nr_images); image = lp_llvm_image_soa_create(lp_fs_variant_key_images(key), key->nr_images);
num_fs = 16 / fs_type.length; /* number of loops per 4x4 stamp */ num_fs = 16 / fs_type.length; /* number of loops per 4x4 stamp */
@ -3432,7 +3432,8 @@ dump_fs_variant_key(struct lp_fragment_shader_variant_key *key)
debug_printf("blend.alpha_to_coverage is enabled\n"); debug_printf("blend.alpha_to_coverage is enabled\n");
} }
for (i = 0; i < key->nr_samplers; ++i) { for (i = 0; i < key->nr_samplers; ++i) {
const struct lp_static_sampler_state *sampler = &key->samplers[i].sampler_state; const struct lp_sampler_static_state *samplers = lp_fs_variant_key_samplers(key);
const struct lp_static_sampler_state *sampler = &samplers[i].sampler_state;
debug_printf("sampler[%u] = \n", i); debug_printf("sampler[%u] = \n", i);
debug_printf(" .wrap = %s %s %s\n", debug_printf(" .wrap = %s %s %s\n",
util_str_tex_wrap(sampler->wrap_s, TRUE), util_str_tex_wrap(sampler->wrap_s, TRUE),
@ -3455,7 +3456,8 @@ dump_fs_variant_key(struct lp_fragment_shader_variant_key *key)
debug_printf(" .aniso = %u\n", sampler->aniso); debug_printf(" .aniso = %u\n", sampler->aniso);
} }
for (i = 0; i < key->nr_sampler_views; ++i) { for (i = 0; i < key->nr_sampler_views; ++i) {
const struct lp_static_texture_state *texture = &key->samplers[i].texture_state; const struct lp_sampler_static_state *samplers = lp_fs_variant_key_samplers(key);
const struct lp_static_texture_state *texture = &samplers[i].texture_state;
debug_printf("texture[%u] = \n", i); debug_printf("texture[%u] = \n", i);
debug_printf(" .format = %s\n", debug_printf(" .format = %s\n",
util_format_name(texture->format)); util_format_name(texture->format));
@ -3645,15 +3647,16 @@ generate_variant(struct llvmpipe_context *lp,
shader->kind == LP_FS_KIND_BLIT_RGB1)) { shader->kind == LP_FS_KIND_BLIT_RGB1)) {
unsigned target, min_img_filter, mag_img_filter, min_mip_filter; unsigned target, min_img_filter, mag_img_filter, min_mip_filter;
enum pipe_format texture_format; enum pipe_format texture_format;
struct lp_sampler_static_state *samp0 = lp_fs_variant_key_sampler_idx(key, 0);
texture_format = key->samplers[0].texture_state.format; assert(samp0);
target = key->samplers[0].texture_state.target; texture_format = samp0->texture_state.format;
min_img_filter = key->samplers[0].sampler_state.min_img_filter; target = samp0->texture_state.target;
mag_img_filter = key->samplers[0].sampler_state.mag_img_filter; min_img_filter = samp0->sampler_state.min_img_filter;
if (key->samplers[0].texture_state.level_zero_only) { mag_img_filter = samp0->sampler_state.mag_img_filter;
if (samp0->texture_state.level_zero_only) {
min_mip_filter = PIPE_TEX_MIPFILTER_NONE; min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
} else { } else {
min_mip_filter = key->samplers[0].sampler_state.min_mip_filter; min_mip_filter = samp0->sampler_state.min_mip_filter;
} }
if (target == PIPE_TEXTURE_2D && if (target == PIPE_TEXTURE_2D &&
@ -4138,7 +4141,7 @@ make_variant_key(struct llvmpipe_context *lp,
key = (struct lp_fragment_shader_variant_key *)store; key = (struct lp_fragment_shader_variant_key *)store;
memset(key, 0, offsetof(struct lp_fragment_shader_variant_key, samplers[1])); memset(key, 0, sizeof(*key));
if (lp->framebuffer.zsbuf) { if (lp->framebuffer.zsbuf) {
enum pipe_format zsbuf_format = lp->framebuffer.zsbuf->format; enum pipe_format zsbuf_format = lp->framebuffer.zsbuf->format;
@ -4283,7 +4286,7 @@ make_variant_key(struct llvmpipe_context *lp,
struct lp_sampler_static_state *fs_sampler; struct lp_sampler_static_state *fs_sampler;
fs_sampler = key->samplers; fs_sampler = lp_fs_variant_key_samplers(key);
memset(fs_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *fs_sampler); memset(fs_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *fs_sampler);
@ -4334,8 +4337,10 @@ make_variant_key(struct llvmpipe_context *lp,
} }
if (shader->kind == LP_FS_KIND_AERO_MINIFICATION) { if (shader->kind == LP_FS_KIND_AERO_MINIFICATION) {
key->samplers[0].sampler_state.min_img_filter = PIPE_TEX_FILTER_NEAREST; struct lp_sampler_static_state *samp0 = lp_fs_variant_key_sampler_idx(key, 0);
key->samplers[0].sampler_state.mag_img_filter = PIPE_TEX_FILTER_NEAREST; assert(samp0);
samp0->sampler_state.min_img_filter = PIPE_TEX_FILTER_NEAREST;
samp0->sampler_state.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
} }
return key; return key;

View file

@ -111,9 +111,7 @@ struct lp_fragment_shader_variant_key
uint8_t zsbuf_nr_samples; uint8_t zsbuf_nr_samples;
uint8_t coverage_samples; uint8_t coverage_samples;
uint8_t min_samples; uint8_t min_samples;
/* followed by variable number of samplers + images */
struct lp_sampler_static_state samplers[1];
/* followed by variable number of images */
}; };
#define LP_FS_MAX_VARIANT_KEY_SIZE \ #define LP_FS_MAX_VARIANT_KEY_SIZE \
@ -124,17 +122,30 @@ struct lp_fragment_shader_variant_key
static inline size_t static inline size_t
lp_fs_variant_key_size(unsigned nr_samplers, unsigned nr_images) lp_fs_variant_key_size(unsigned nr_samplers, unsigned nr_images)
{ {
unsigned samplers = nr_samplers > 1 ? (nr_samplers - 1) : 0;
return (sizeof(struct lp_fragment_shader_variant_key) + return (sizeof(struct lp_fragment_shader_variant_key) +
samplers * sizeof(struct lp_sampler_static_state) + nr_samplers * sizeof(struct lp_sampler_static_state) +
nr_images * sizeof(struct lp_image_static_state)); nr_images * sizeof(struct lp_image_static_state));
} }
static inline struct lp_sampler_static_state *
lp_fs_variant_key_samplers(const struct lp_fragment_shader_variant_key *key)
{
return (struct lp_sampler_static_state *)&(key[1]);
}
static inline struct lp_sampler_static_state *
lp_fs_variant_key_sampler_idx(const struct lp_fragment_shader_variant_key *key, int idx)
{
if (idx >= key->nr_samplers)
return NULL;
return &lp_fs_variant_key_samplers(key)[idx];
}
static inline struct lp_image_static_state * static inline struct lp_image_static_state *
lp_fs_variant_key_images(struct lp_fragment_shader_variant_key *key) lp_fs_variant_key_images(struct lp_fragment_shader_variant_key *key)
{ {
return (struct lp_image_static_state *) return (struct lp_image_static_state *)
&key->samplers[key->nr_samplers]; &(lp_fs_variant_key_samplers(key)[key->nr_samplers]);
} }
/** doubly-linked list item */ /** doubly-linked list item */

View file

@ -667,16 +667,20 @@ is_one_inv_src_alpha_blend(const struct lp_fragment_shader_variant *variant)
void void
llvmpipe_fs_variant_linear_fastpath(struct lp_fragment_shader_variant *variant) llvmpipe_fs_variant_linear_fastpath(struct lp_fragment_shader_variant *variant)
{ {
enum pipe_format tex_format = variant->key.samplers[0].texture_state.format; struct lp_sampler_static_state *samp0 = lp_fs_variant_key_sampler_idx(&variant->key, 0);
if (LP_PERF & PERF_NO_SHADE) { if (LP_PERF & PERF_NO_SHADE) {
variant->jit_linear = linear_red; variant->jit_linear = linear_red;
return; return;
} }
if (!samp0)
return;
enum pipe_format tex_format = samp0->texture_state.format;
if (variant->shader->kind == LP_FS_KIND_BLIT_RGBA && if (variant->shader->kind == LP_FS_KIND_BLIT_RGBA &&
tex_format == PIPE_FORMAT_B8G8R8A8_UNORM && tex_format == PIPE_FORMAT_B8G8R8A8_UNORM &&
is_nearest_clamp_sampler(&variant->key.samplers[0])) { is_nearest_clamp_sampler(samp0)) {
if (variant->opaque) { if (variant->opaque) {
variant->jit_linear_blit = blit_rgba_blit; variant->jit_linear_blit = blit_rgba_blit;
variant->jit_linear = blit_rgba; variant->jit_linear = blit_rgba;
@ -692,7 +696,7 @@ llvmpipe_fs_variant_linear_fastpath(struct lp_fragment_shader_variant *variant)
variant->opaque && variant->opaque &&
(tex_format == PIPE_FORMAT_B8G8R8A8_UNORM || (tex_format == PIPE_FORMAT_B8G8R8A8_UNORM ||
tex_format == PIPE_FORMAT_B8G8R8X8_UNORM) && tex_format == PIPE_FORMAT_B8G8R8X8_UNORM) &&
is_nearest_clamp_sampler(&variant->key.samplers[0])) { is_nearest_clamp_sampler(samp0)) {
variant->jit_linear_blit = blit_rgb1_blit; variant->jit_linear_blit = blit_rgb1_blit;
variant->jit_linear = blit_rgb1; variant->jit_linear = blit_rgb1;
return; return;