llvmpipe/draw: handle constant buffer limits and robustness (v1.1)
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_cs.c
index c9c03b6db20582854b2bf4b04fb832d04115b0ce..be6d648cd537a830259a5aadc54343812d5af96c 100644 (file)
 #include "lp_perf.h"
 #include "lp_screen.h"
 #include "lp_memory.h"
+#include "lp_query.h"
 #include "lp_cs_tpool.h"
 #include "frontend/sw_winsys.h"
 #include "nir/nir_to_tgsi_info.h"
+#include "util/mesa-sha1.h"
 #include "nir_serialize.h"
 
 /** Fragment shader number (for debugging) */
@@ -96,11 +98,9 @@ generate_compute(struct llvmpipe_context *lp,
    cs_type.norm = FALSE;         /* values are not limited to [0,1] or [-1,1] */
    cs_type.width = 32;           /* 32-bit float */
    cs_type.length = MIN2(lp_native_vector_width / 32, 16); /* n*4 elements per vector */
-   snprintf(func_name, sizeof(func_name), "cs%u_variant%u",
-            shader->no, variant->no);
+   snprintf(func_name, sizeof(func_name), "cs_variant");
 
-   snprintf(func_name_coro, sizeof(func_name), "cs_co_%u_variant%u",
-            shader->no, variant->no);
+   snprintf(func_name_coro, sizeof(func_name), "cs_co_variant");
 
    arg_types[0] = variant->jit_cs_context_ptr_type;       /* context */
    arg_types[1] = int32_type;                          /* block_x_size */
@@ -140,6 +140,11 @@ generate_compute(struct llvmpipe_context *lp,
       }
    }
 
+   lp_build_coro_declare_malloc_hooks(gallivm);
+
+   if (variant->gallivm->cache->data_size)
+      return;
+
    context_ptr  = LLVMGetParam(function, 0);
    x_size_arg = LLVMGetParam(function, 1);
    y_size_arg = LLVMGetParam(function, 2);
@@ -170,8 +175,8 @@ generate_compute(struct llvmpipe_context *lp,
    builder = gallivm->builder;
    assert(builder);
    LLVMPositionBuilderAtEnd(builder, block);
-   sampler = lp_llvm_sampler_soa_create(key->samplers);
-   image = lp_llvm_image_soa_create(lp_cs_variant_key_images(key));
+   sampler = lp_llvm_sampler_soa_create(key->samplers, key->nr_samplers);
+   image = lp_llvm_image_soa_create(lp_cs_variant_key_images(key), key->nr_images);
 
    struct lp_build_loop_state loop_state[4];
    LLVMValueRef num_x_loop;
@@ -673,13 +678,39 @@ lp_debug_cs_variant(const struct lp_compute_shader_variant *variant)
    debug_printf("\n");
 }
 
+static void
+lp_cs_get_ir_cache_key(struct lp_compute_shader_variant *variant,
+                       unsigned char ir_sha1_cache_key[20])
+{
+   struct blob blob = { 0 };
+   unsigned ir_size;
+   void *ir_binary;
+
+   blob_init(&blob);
+   nir_serialize(&blob, variant->shader->base.ir.nir, true);
+   ir_binary = blob.data;
+   ir_size = blob.size;
+
+   struct mesa_sha1 ctx;
+   _mesa_sha1_init(&ctx);
+   _mesa_sha1_update(&ctx, &variant->key, variant->shader->variant_key_size);
+   _mesa_sha1_update(&ctx, ir_binary, ir_size);
+   _mesa_sha1_final(&ctx, ir_sha1_cache_key);
+
+   blob_finish(&blob);
+}
+
 static struct lp_compute_shader_variant *
 generate_variant(struct llvmpipe_context *lp,
                  struct lp_compute_shader *shader,
                  const struct lp_compute_shader_variant_key *key)
 {
+   struct llvmpipe_screen *screen = llvmpipe_screen(lp->pipe.screen);
    struct lp_compute_shader_variant *variant;
    char module_name[64];
+   unsigned char ir_sha1_cache_key[20];
+   struct lp_cached_code cached = { 0 };
+   bool needs_caching = false;
    variant = MALLOC(sizeof *variant + shader->variant_key_size - sizeof variant->key);
    if (!variant)
       return NULL;
@@ -688,18 +719,27 @@ generate_variant(struct llvmpipe_context *lp,
    snprintf(module_name, sizeof(module_name), "cs%u_variant%u",
             shader->no, shader->variants_created);
 
-   variant->gallivm = gallivm_create(module_name, lp->context);
+   variant->shader = shader;
+   memcpy(&variant->key, key, shader->variant_key_size);
+
+   if (shader->base.ir.nir) {
+      lp_cs_get_ir_cache_key(variant, ir_sha1_cache_key);
+
+      lp_disk_cache_find_shader(screen, &cached, ir_sha1_cache_key);
+      if (!cached.data_size)
+         needs_caching = true;
+   }
+   variant->gallivm = gallivm_create(module_name, lp->context, &cached);
    if (!variant->gallivm) {
       FREE(variant);
       return NULL;
    }
 
-   variant->shader = shader;
    variant->list_item_global.base = variant;
    variant->list_item_local.base = variant;
    variant->no = shader->variants_created++;
 
-   memcpy(&variant->key, key, shader->variant_key_size);
+
 
    if ((LP_DEBUG & DEBUG_CS) || (gallivm_debug & GALLIVM_DEBUG_IR)) {
       lp_debug_cs_variant(variant);
@@ -711,10 +751,14 @@ generate_variant(struct llvmpipe_context *lp,
 
    gallivm_compile_module(variant->gallivm);
 
+   lp_build_coro_add_malloc_hooks(variant->gallivm);
    variant->nr_instrs += lp_build_count_ir_module(variant->gallivm->module);
 
    variant->jit_function = (lp_jit_cs_func)gallivm_jit_function(variant->gallivm, variant->function);
 
+   if (needs_caching) {
+      lp_disk_cache_insert_shader(screen, &cached, ir_sha1_cache_key);
+   }
    gallivm_free_ir(variant->gallivm);
    return variant;
 }
@@ -1117,7 +1161,7 @@ update_csctx_consts(struct llvmpipe_context *llvmpipe)
    for (i = 0; i < ARRAY_SIZE(csctx->constants); ++i) {
       struct pipe_resource *buffer = csctx->constants[i].current.buffer;
       const ubyte *current_data = NULL;
-
+      unsigned current_size = csctx->constants[i].current.buffer_size;
       if (buffer) {
          /* resource buffer */
          current_data = (ubyte *) llvmpipe_resource_data(buffer);
@@ -1127,13 +1171,15 @@ update_csctx_consts(struct llvmpipe_context *llvmpipe)
          current_data = (ubyte *) csctx->constants[i].current.user_buffer;
       }
 
-      if (current_data) {
+      if (current_data && current_size >= sizeof(float)) {
          current_data += csctx->constants[i].current.buffer_offset;
-
          csctx->cs.current.jit_context.constants[i] = (const float *)current_data;
-         csctx->cs.current.jit_context.num_constants[i] = csctx->constants[i].current.buffer_size;
+         csctx->cs.current.jit_context.num_constants[i] =
+            DIV_ROUND_UP(csctx->constants[i].current.buffer_size,
+                         lp_get_constant_buffer_stride(llvmpipe->pipe.screen));
       } else {
-         csctx->cs.current.jit_context.constants[i] = NULL;
+         static const float fake_const_buf[4];
+         csctx->cs.current.jit_context.constants[i] = fake_const_buf;
          csctx->cs.current.jit_context.num_constants[i] = 0;
       }
    }
@@ -1167,9 +1213,6 @@ update_csctx_ssbo(struct llvmpipe_context *llvmpipe)
 static void
 llvmpipe_cs_update_derived(struct llvmpipe_context *llvmpipe, void *input)
 {
-   if (llvmpipe->cs_dirty & (LP_CSNEW_CS))
-      llvmpipe_update_cs(llvmpipe);
-
    if (llvmpipe->cs_dirty & LP_CSNEW_CONSTANTS) {
       lp_csctx_set_cs_constants(llvmpipe->csctx,
                                 ARRAY_SIZE(llvmpipe->constants[PIPE_SHADER_COMPUTE]),
@@ -1205,6 +1248,13 @@ llvmpipe_cs_update_derived(struct llvmpipe_context *llvmpipe, void *input)
       csctx->cs.current.jit_context.kernel_args = input;
    }
 
+   if (llvmpipe->cs_dirty & (LP_CSNEW_CS |
+                             LP_CSNEW_IMAGES |
+                             LP_CSNEW_SAMPLER_VIEW |
+                             LP_CSNEW_SAMPLER))
+      llvmpipe_update_cs(llvmpipe);
+
+
    llvmpipe->cs_dirty = 0;
 }
 
@@ -1269,6 +1319,9 @@ static void llvmpipe_launch_grid(struct pipe_context *pipe,
    struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
    struct lp_cs_job_info job_info;
 
+   if (!llvmpipe_check_render_cond(llvmpipe))
+      return;
+
    memset(&job_info, 0, sizeof(job_info));
 
    llvmpipe_cs_update_derived(llvmpipe, info->input);