cso: fix broken optimization for sampler state lookups
authorMarek Olšák <marek.olsak@amd.com>
Sun, 7 Aug 2022 23:59:48 +0000 (19:59 -0400)
committerMarge Bot <emma+marge@anholt.net>
Wed, 19 Oct 2022 04:56:55 +0000 (04:56 +0000)
Since the key size wasn't a constant expression, all the function inlining
didn't do much. This makes it a constant expression.

Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Fixes: c4e18cd4dd1 - mesa/st: add PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_FREEDRENO
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19129>

src/gallium/auxiliary/cso_cache/cso_context.c

index 0d276fb..4c0502a 100644 (file)
@@ -1323,10 +1323,19 @@ void
 cso_single_sampler(struct cso_context *ctx, enum pipe_shader_type shader_stage,
                    unsigned idx, const struct pipe_sampler_state *templ)
 {
-   size_t size = ctx->sampler_format ? sizeof(struct pipe_sampler_state) :
-             offsetof(struct pipe_sampler_state, border_color_format);
-   if (cso_set_sampler(ctx, shader_stage, idx, templ, size))
-      ctx->max_sampler_seen = MAX2(ctx->max_sampler_seen, (int)idx);
+   /* The reasons both blocks are duplicated is that we want the size parameter
+    * to be a constant expression to inline and unroll memcmp and hash key
+    * computations.
+    */
+   if (ctx->sampler_format) {
+      if (cso_set_sampler(ctx, shader_stage, idx, templ,
+                          sizeof(struct pipe_sampler_state)))
+         ctx->max_sampler_seen = MAX2(ctx->max_sampler_seen, (int)idx);
+   } else {
+      if (cso_set_sampler(ctx, shader_stage, idx, templ,
+                          offsetof(struct pipe_sampler_state, border_color_format)))
+         ctx->max_sampler_seen = MAX2(ctx->max_sampler_seen, (int)idx);
+   }
 }
 
 
@@ -1403,10 +1412,16 @@ cso_set_samplers(struct cso_context *ctx,
                  unsigned nr,
                  const struct pipe_sampler_state **templates)
 {
+   int last;
+
    /* ensure sampler size is a constant for memcmp */
-   size_t size = ctx->sampler_format ? sizeof(struct pipe_sampler_state) :
-                   offsetof(struct pipe_sampler_state, border_color_format);
-   int last = set_samplers(ctx, shader_stage, nr, templates, size);
+   if (ctx->sampler_format) {
+      last = set_samplers(ctx, shader_stage, nr, templates,
+                          sizeof(struct pipe_sampler_state));
+   } else {
+      last = set_samplers(ctx, shader_stage, nr, templates,
+                          offsetof(struct pipe_sampler_state, border_color_format));
+   }
 
    ctx->max_sampler_seen = MAX2(ctx->max_sampler_seen, last);
    cso_single_sampler_done(ctx, shader_stage);