X-Git-Url: https://git.libre-soc.org/?p=mesa.git;a=blobdiff_plain;f=src%2Fgallium%2Fdrivers%2Fllvmpipe%2Flp_state_cs.c;h=a3daf6a503bd620ad22b2d18f1073e66be469d08;hp=92e156b411123c1d6251465c67a63b39bd9126d1;hb=30c5cbbcd206aa55f8dad4cdc96407642d0e98a8;hpb=6e035c01fb95686b9c48f2930104b90c7d12f0f7 diff --git a/src/gallium/drivers/llvmpipe/lp_state_cs.c b/src/gallium/drivers/llvmpipe/lp_state_cs.c index 92e156b4111..a3daf6a503b 100644 --- a/src/gallium/drivers/llvmpipe/lp_state_cs.c +++ b/src/gallium/drivers/llvmpipe/lp_state_cs.c @@ -44,9 +44,14 @@ #include "lp_screen.h" #include "lp_memory.h" #include "lp_cs_tpool.h" -#include "state_tracker/sw_winsys.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) */ +static unsigned cs_no = 0; + struct lp_cs_job_info { unsigned grid_size[3]; unsigned block_size[3]; @@ -92,11 +97,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 */ @@ -136,6 +139,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); @@ -166,8 +174,8 @@ generate_compute(struct llvmpipe_context *lp, builder = gallivm->builder; assert(builder); LLVMPositionBuilderAtEnd(builder, block); - sampler = lp_llvm_sampler_soa_create(key->state); - image = lp_llvm_image_soa_create(key->image_state); + sampler = lp_llvm_sampler_soa_create(key->samplers, key->nr_samplers); + image = lp_llvm_image_soa_create(lp_cs_variant_key_images(key)); struct lp_build_loop_state loop_state[4]; LLVMValueRef num_x_loop; @@ -421,10 +429,13 @@ llvmpipe_create_compute_state(struct pipe_context *pipe, { struct lp_compute_shader *shader; int nr_samplers, nr_sampler_views; + shader = CALLOC_STRUCT(lp_compute_shader); if (!shader) return NULL; + shader->no = cs_no++; + shader->base.type = templ->ir_type; if (templ->ir_type == PIPE_SHADER_IR_NIR_SERIALIZED) { struct blob_reader reader; @@ -453,8 +464,9 @@ llvmpipe_create_compute_state(struct pipe_context *pipe, nr_samplers = shader->info.base.file_max[TGSI_FILE_SAMPLER] + 1; nr_sampler_views = shader->info.base.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; - shader->variant_key_size = Offset(struct lp_compute_shader_variant_key, - state[MAX2(nr_samplers, nr_sampler_views)]); + int nr_images = shader->info.base.file_max[TGSI_FILE_IMAGE] + 1; + shader->variant_key_size = lp_cs_variant_key_size(MAX2(nr_samplers, nr_sampler_views), nr_images); + return shader; } @@ -523,26 +535,32 @@ llvmpipe_delete_compute_state(struct pipe_context *pipe, llvmpipe_remove_cs_shader_variant(llvmpipe, li->base); li = next; } + if (shader->base.ir.nir) + ralloc_free(shader->base.ir.nir); tgsi_free_tokens(shader->base.tokens); FREE(shader); } -static void +static struct lp_compute_shader_variant_key * make_variant_key(struct llvmpipe_context *lp, struct lp_compute_shader *shader, - struct lp_compute_shader_variant_key *key) + char *store) { int i; - - memset(key, 0, shader->variant_key_size); + struct lp_compute_shader_variant_key *key; + key = (struct lp_compute_shader_variant_key *)store; + memset(key, 0, offsetof(struct lp_compute_shader_variant_key, samplers[1])); /* This value will be the same for all the variants of a given shader: */ key->nr_samplers = shader->info.base.file_max[TGSI_FILE_SAMPLER] + 1; + struct lp_sampler_static_state *cs_sampler; + + cs_sampler = key->samplers; for(i = 0; i < key->nr_samplers; ++i) { if(shader->info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) { - lp_sampler_static_sampler_state(&key->state[i].sampler_state, + lp_sampler_static_sampler_state(&cs_sampler[i].sampler_state, lp->samplers[PIPE_SHADER_COMPUTE][i]); } } @@ -561,7 +579,7 @@ make_variant_key(struct llvmpipe_context *lp, * used views may be included in the shader key. */ if(shader->info.base.file_mask[TGSI_FILE_SAMPLER_VIEW] & (1u << (i & 31))) { - lp_sampler_static_texture_state(&key->state[i].texture_state, + lp_sampler_static_texture_state(&cs_sampler[i].texture_state, lp->sampler_views[PIPE_SHADER_COMPUTE][i]); } } @@ -570,19 +588,22 @@ make_variant_key(struct llvmpipe_context *lp, key->nr_sampler_views = key->nr_samplers; for(i = 0; i < key->nr_sampler_views; ++i) { if(shader->info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) { - lp_sampler_static_texture_state(&key->state[i].texture_state, + lp_sampler_static_texture_state(&cs_sampler[i].texture_state, lp->sampler_views[PIPE_SHADER_COMPUTE][i]); } } } + struct lp_image_static_state *lp_image; + lp_image = lp_cs_variant_key_images(key); key->nr_images = shader->info.base.file_max[TGSI_FILE_IMAGE] + 1; for (i = 0; i < key->nr_images; ++i) { if (shader->info.base.file_mask[TGSI_FILE_IMAGE] & (1 << i)) { - lp_sampler_static_texture_state_image(&key->image_state[i].image_state, + lp_sampler_static_texture_state_image(&lp_image[i].image_state, &lp->images[PIPE_SHADER_COMPUTE][i]); } } + return key; } static void @@ -592,7 +613,7 @@ dump_cs_variant_key(const struct lp_compute_shader_variant_key *key) debug_printf("cs variant %p:\n", (void *) key); for (i = 0; i < key->nr_samplers; ++i) { - const struct lp_static_sampler_state *sampler = &key->state[i].sampler_state; + const struct lp_static_sampler_state *sampler = &key->samplers[i].sampler_state; debug_printf("sampler[%u] = \n", i); debug_printf(" .wrap = %s %s %s\n", util_str_tex_wrap(sampler->wrap_s, TRUE), @@ -613,7 +634,7 @@ dump_cs_variant_key(const struct lp_compute_shader_variant_key *key) debug_printf(" .apply_max_lod = %u\n", sampler->apply_max_lod); } for (i = 0; i < key->nr_sampler_views; ++i) { - const struct lp_static_texture_state *texture = &key->state[i].texture_state; + const struct lp_static_texture_state *texture = &key->samplers[i].texture_state; debug_printf("texture[%u] = \n", i); debug_printf(" .format = %s\n", util_format_name(texture->format)); @@ -626,8 +647,9 @@ dump_cs_variant_key(const struct lp_compute_shader_variant_key *key) texture->pot_height, texture->pot_depth); } + struct lp_image_static_state *images = lp_cs_variant_key_images(key); for (i = 0; i < key->nr_images; ++i) { - const struct lp_static_texture_state *image = &key->image_state[i].image_state; + const struct lp_static_texture_state *image = &images[i].image_state; debug_printf("image[%u] = \n", i); debug_printf(" .format = %s\n", util_format_name(image->format)); @@ -655,33 +677,68 @@ 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]; - - variant = CALLOC_STRUCT(lp_compute_shader_variant); + 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; + memset(variant, 0, sizeof(*variant)); 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); @@ -693,10 +750,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; } @@ -713,16 +774,17 @@ llvmpipe_update_cs(struct llvmpipe_context *lp) { struct lp_compute_shader *shader = lp->cs; - struct lp_compute_shader_variant_key key; + struct lp_compute_shader_variant_key *key; struct lp_compute_shader_variant *variant = NULL; struct lp_cs_variant_list_item *li; + char store[LP_CS_MAX_VARIANT_KEY_SIZE]; - make_variant_key(lp, shader, &key); + key = make_variant_key(lp, shader, store); /* Search the variants for one which matches the key */ li = first_elem(&shader->variants); while(!at_end(&shader->variants, li)) { - if(memcmp(&li->base->key, &key, shader->variant_key_size) == 0) { + if(memcmp(&li->base->key, key, shader->variant_key_size) == 0) { variant = li->base; break; } @@ -784,7 +846,7 @@ llvmpipe_update_cs(struct llvmpipe_context *lp) * Generate the new variant. */ t0 = os_time_get(); - variant = generate_variant(lp, shader, &key); + variant = generate_variant(lp, shader, key); t1 = os_time_get(); dt = t1 - t0; LP_COUNT_ADD(llvm_compile_time, dt); @@ -860,6 +922,8 @@ lp_csctx_set_sampler_views(struct lp_cs_context *csctx, jit_tex->mip_offsets[0] = 0; jit_tex->row_stride[0] = 0; jit_tex->img_stride[0] = 0; + jit_tex->num_samples = 0; + jit_tex->sample_stride = 0; } else { jit_tex->width = res->width0; @@ -867,6 +931,8 @@ lp_csctx_set_sampler_views(struct lp_cs_context *csctx, jit_tex->depth = res->depth0; jit_tex->first_level = first_level; jit_tex->last_level = last_level; + jit_tex->num_samples = res->nr_samples; + jit_tex->sample_stride = 0; if (llvmpipe_resource_is_texture(res)) { for (j = first_level; j <= last_level; j++) { @@ -874,6 +940,7 @@ lp_csctx_set_sampler_views(struct lp_cs_context *csctx, jit_tex->row_stride[j] = lp_tex->row_stride[j]; jit_tex->img_stride[j] = lp_tex->img_stride[j]; } + jit_tex->sample_stride = lp_tex->sample_stride; if (res->target == PIPE_TEXTURE_1D_ARRAY || res->target == PIPE_TEXTURE_2D_ARRAY || @@ -933,6 +1000,8 @@ lp_csctx_set_sampler_views(struct lp_cs_context *csctx, jit_tex->height = res->height0; jit_tex->depth = res->depth0; jit_tex->first_level = jit_tex->last_level = 0; + jit_tex->num_samples = res->nr_samples; + jit_tex->sample_stride = 0; assert(jit_tex->base); } } @@ -1042,6 +1111,7 @@ lp_csctx_set_cs_images(struct lp_cs_context *csctx, jit_image->width = res->width0; jit_image->height = res->height0; jit_image->depth = res->depth0; + jit_image->num_samples = res->nr_samples; if (llvmpipe_resource_is_texture(res)) { uint32_t mip_offset = lp_res->mip_offsets[image->u.tex.level]; @@ -1067,6 +1137,7 @@ lp_csctx_set_cs_images(struct lp_cs_context *csctx, jit_image->row_stride = lp_res->row_stride[image->u.tex.level]; jit_image->img_stride = lp_res->img_stride[image->u.tex.level]; + jit_image->sample_stride = lp_res->sample_stride; jit_image->base = (uint8_t *)jit_image->base + mip_offset; } else { unsigned view_blocksize = util_format_get_blocksize(image->format); @@ -1304,12 +1375,12 @@ llvmpipe_set_global_binding(struct pipe_context *pipe, } for (i = 0; i < count; i++) { - uint64_t va; + uintptr_t va; uint32_t offset; pipe_resource_reference(&cs->global_buffers[first + i], resources[i]); struct llvmpipe_resource *lp_res = llvmpipe_resource(resources[i]); offset = *handles[i]; - va = (uint64_t)((char *)lp_res->data + offset); + va = (uintptr_t)((char *)lp_res->data + offset); memcpy(handles[i], &va, sizeof(va)); } }