Path: blob/21.2-virgl/src/gallium/drivers/crocus/crocus_disk_cache.c
4570 views
/*1* Copyright © 2018 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included11* in all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19* DEALINGS IN THE SOFTWARE.20*/2122/**23* @file crocus_disk_cache.c24*25* Functions for interacting with the on-disk shader cache.26*/2728#include <stdio.h>29#include <stdint.h>30#include <assert.h>31#include <string.h>3233#include "compiler/nir/nir.h"34#include "util/blob.h"35#include "util/build_id.h"36#include "util/disk_cache.h"37#include "util/mesa-sha1.h"3839#include "crocus_context.h"4041static bool debug = false;4243/**44* Compute a disk cache key for the given uncompiled shader and NOS key.45*/46static void47crocus_disk_cache_compute_key(struct disk_cache *cache,48const struct crocus_uncompiled_shader *ish,49const void *orig_prog_key,50uint32_t prog_key_size,51cache_key cache_key)52{53/* Create a copy of the program key with program_string_id zeroed out.54* It's essentially random data which we don't want to include in our55* hashing and comparisons. We'll set a proper value on a cache hit.56*/57union brw_any_prog_key prog_key;58memcpy(&prog_key, orig_prog_key, prog_key_size);59prog_key.base.program_string_id = 0;6061uint8_t data[sizeof(prog_key) + sizeof(ish->nir_sha1)];62uint32_t data_size = prog_key_size + sizeof(ish->nir_sha1);6364memcpy(data, ish->nir_sha1, sizeof(ish->nir_sha1));65memcpy(data + sizeof(ish->nir_sha1), &prog_key, prog_key_size);6667disk_cache_compute_key(cache, data, data_size, cache_key);68}6970/**71* Store the given compiled shader in the disk cache.72*73* This should only be called on newly compiled shaders. No checking is74* done to prevent repeated stores of the same shader.75*/76void77crocus_disk_cache_store(struct disk_cache *cache,78const struct crocus_uncompiled_shader *ish,79const struct crocus_compiled_shader *shader,80void *map,81const void *prog_key,82uint32_t prog_key_size)83{84#ifdef ENABLE_SHADER_CACHE85if (!cache)86return;8788gl_shader_stage stage = ish->nir->info.stage;89const struct brw_stage_prog_data *prog_data = shader->prog_data;9091cache_key cache_key;92crocus_disk_cache_compute_key(cache, ish, prog_key, prog_key_size, cache_key);9394if (debug) {95char sha1[41];96_mesa_sha1_format(sha1, cache_key);97fprintf(stderr, "[mesa disk cache] storing %s\n", sha1);98}99100struct blob blob;101blob_init(&blob);102103/* We write the following data to the cache blob:104*105* 1. Prog data (must come first because it has the assembly size)106* 2. Assembly code107* 3. Number of entries in the system value array108* 4. System value array109* 5. Legacy param array (only used for compute workgroup ID)110* 6. Binding table111*/112blob_write_bytes(&blob, shader->prog_data, brw_prog_data_size(stage));113blob_write_bytes(&blob, map + shader->offset, shader->prog_data->program_size);114blob_write_bytes(&blob, &shader->num_system_values, sizeof(unsigned));115blob_write_bytes(&blob, shader->system_values,116shader->num_system_values * sizeof(enum brw_param_builtin));117blob_write_bytes(&blob, prog_data->param,118prog_data->nr_params * sizeof(uint32_t));119blob_write_bytes(&blob, &shader->bt, sizeof(shader->bt));120121disk_cache_put(cache, cache_key, blob.data, blob.size, NULL);122blob_finish(&blob);123#endif124}125126/**127* Search for a compiled shader in the disk cache. If found, upload it128* to the in-memory program cache so we can use it.129*/130struct crocus_compiled_shader *131crocus_disk_cache_retrieve(struct crocus_context *ice,132const struct crocus_uncompiled_shader *ish,133const void *prog_key,134uint32_t key_size)135{136#ifdef ENABLE_SHADER_CACHE137struct crocus_screen *screen = (void *) ice->ctx.screen;138struct disk_cache *cache = screen->disk_cache;139gl_shader_stage stage = ish->nir->info.stage;140141if (!cache)142return NULL;143144cache_key cache_key;145crocus_disk_cache_compute_key(cache, ish, prog_key, key_size, cache_key);146147if (debug) {148char sha1[41];149_mesa_sha1_format(sha1, cache_key);150fprintf(stderr, "[mesa disk cache] retrieving %s: ", sha1);151}152153size_t size;154void *buffer = disk_cache_get(screen->disk_cache, cache_key, &size);155156if (debug)157fprintf(stderr, "%s\n", buffer ? "found" : "missing");158159if (!buffer)160return NULL;161162const uint32_t prog_data_size = brw_prog_data_size(stage);163164struct brw_stage_prog_data *prog_data = ralloc_size(NULL, prog_data_size);165const void *assembly;166uint32_t num_system_values;167uint32_t *system_values = NULL;168uint32_t *so_decls = NULL;169170struct blob_reader blob;171blob_reader_init(&blob, buffer, size);172blob_copy_bytes(&blob, prog_data, prog_data_size);173assembly = blob_read_bytes(&blob, prog_data->program_size);174num_system_values = blob_read_uint32(&blob);175if (num_system_values) {176system_values =177ralloc_array(NULL, enum brw_param_builtin, num_system_values);178blob_copy_bytes(&blob, system_values,179num_system_values * sizeof(enum brw_param_builtin));180}181182prog_data->param = NULL;183prog_data->pull_param = NULL;184assert(prog_data->nr_pull_params == 0);185186if (prog_data->nr_params) {187prog_data->param = ralloc_array(NULL, uint32_t, prog_data->nr_params);188blob_copy_bytes(&blob, prog_data->param,189prog_data->nr_params * sizeof(uint32_t));190}191192struct crocus_binding_table bt;193blob_copy_bytes(&blob, &bt, sizeof(bt));194195if ((stage == MESA_SHADER_VERTEX ||196stage == MESA_SHADER_TESS_EVAL ||197stage == MESA_SHADER_GEOMETRY) && screen->devinfo.ver > 6) {198struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;199so_decls = screen->vtbl.create_so_decl_list(&ish->stream_output,200&vue_prog_data->vue_map);201}202203/* System values and uniforms are stored in constant buffer 0, the204* user-facing UBOs are indexed by one. So if any constant buffer is205* needed, the constant buffer 0 will be needed, so account for it.206*/207unsigned num_cbufs = ish->nir->info.num_ubos;208209if (num_cbufs || ish->nir->num_uniforms)210num_cbufs++;211212if (num_system_values)213num_cbufs++;214215/* Upload our newly read shader to the in-memory program cache and216* return it to the caller.217*/218struct crocus_compiled_shader *shader =219crocus_upload_shader(ice, stage, key_size, prog_key, assembly,220prog_data->program_size,221prog_data, prog_data_size, so_decls, system_values,222num_system_values, num_cbufs, &bt);223224free(buffer);225226return shader;227#else228return NULL;229#endif230}231232/**233* Initialize the on-disk shader cache.234*/235void236crocus_disk_cache_init(struct crocus_screen *screen)237{238#ifdef ENABLE_SHADER_CACHE239if (INTEL_DEBUG & DEBUG_DISK_CACHE_DISABLE_MASK)240return;241242/* array length = print length + nul char + 1 extra to verify it's unused */243char renderer[13];244UNUSED int len =245snprintf(renderer, sizeof(renderer), "crocus_%04x", screen->pci_id);246assert(len == sizeof(renderer) - 2);247248const struct build_id_note *note =249build_id_find_nhdr_for_addr(crocus_disk_cache_init);250assert(note && build_id_length(note) == 20); /* sha1 */251252const uint8_t *id_sha1 = build_id_data(note);253assert(id_sha1);254255char timestamp[41];256_mesa_sha1_format(timestamp, id_sha1);257258const uint64_t driver_flags =259brw_get_compiler_config_value(screen->compiler);260screen->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);261#endif262}263264265