Path: blob/21.2-virgl/src/gallium/drivers/lima/lima_disk_cache.c
4565 views
/*1* Copyright © 2018 Intel Corporation2* Copyright (c) 2021 Lima Project3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sub license,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the12* next paragraph) shall be included in all copies or substantial portions13* of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23*/2425#include "compiler/nir/nir.h"26#include "util/blob.h"27#include "util/build_id.h"28#include "util/disk_cache.h"29#include "util/mesa-sha1.h"3031#include "lima_context.h"32#include "lima_screen.h"33#include "lima_disk_cache.h"3435void36lima_vs_disk_cache_store(struct disk_cache *cache,37const struct lima_vs_key *key,38const struct lima_vs_compiled_shader *shader)39{40if (!cache)41return;4243cache_key cache_key;44disk_cache_compute_key(cache, key, sizeof(*key), cache_key);4546if (lima_debug & LIMA_DEBUG_DISK_CACHE) {47char sha1[41];48_mesa_sha1_format(sha1, cache_key);49fprintf(stderr, "[mesa disk cache] storing %s\n", sha1);50}5152struct blob blob;53blob_init(&blob);5455blob_write_bytes(&blob, &shader->state, sizeof(shader->state));56blob_write_bytes(&blob, shader->shader, shader->state.shader_size);57blob_write_bytes(&blob, shader->constant, shader->state.constant_size);5859disk_cache_put(cache, cache_key, blob.data, blob.size, NULL);60blob_finish(&blob);61}6263void64lima_fs_disk_cache_store(struct disk_cache *cache,65const struct lima_fs_key *key,66const struct lima_fs_compiled_shader *shader)67{68if (!cache)69return;7071cache_key cache_key;72disk_cache_compute_key(cache, key, sizeof(*key), cache_key);7374if (lima_debug & LIMA_DEBUG_DISK_CACHE) {75char sha1[41];76_mesa_sha1_format(sha1, cache_key);77fprintf(stderr, "[mesa disk cache] storing %s\n", sha1);78}7980struct blob blob;81blob_init(&blob);8283blob_write_bytes(&blob, &shader->state, sizeof(shader->state));84blob_write_bytes(&blob, shader->shader, shader->state.shader_size);8586disk_cache_put(cache, cache_key, blob.data, blob.size, NULL);87blob_finish(&blob);88}8990struct lima_vs_compiled_shader *91lima_vs_disk_cache_retrieve(struct disk_cache *cache,92struct lima_vs_key *key)93{94struct lima_vs_compiled_shader *shader = NULL;9596if (!cache)97return NULL;9899cache_key cache_key;100disk_cache_compute_key(cache, key, sizeof(*key), cache_key);101102if (lima_debug & LIMA_DEBUG_DISK_CACHE) {103char sha1[41];104_mesa_sha1_format(sha1, cache_key);105fprintf(stderr, "[mesa disk cache] retrieving %s: ", sha1);106}107108size_t size;109void *buffer = disk_cache_get(cache, cache_key, &size);110111if (lima_debug & LIMA_DEBUG_DISK_CACHE)112fprintf(stderr, "%s\n", buffer ? "found" : "missing");113114if (!buffer)115return NULL;116117shader = rzalloc(NULL, struct lima_vs_compiled_shader);118if (!shader)119goto out;120121struct blob_reader blob;122blob_reader_init(&blob, buffer, size);123blob_copy_bytes(&blob, &shader->state, sizeof(shader->state));124shader->shader = rzalloc_size(shader, shader->state.shader_size);125if (!shader->shader)126goto err;127blob_copy_bytes(&blob, shader->shader, shader->state.shader_size);128shader->constant = rzalloc_size(shader, shader->state.constant_size);129if (!shader->constant)130goto err;131blob_copy_bytes(&blob, shader->constant, shader->state.constant_size);132133out:134free(buffer);135return shader;136137err:138ralloc_free(shader);139return NULL;140}141142struct lima_fs_compiled_shader *143lima_fs_disk_cache_retrieve(struct disk_cache *cache,144struct lima_fs_key *key)145{146struct lima_fs_compiled_shader *shader = NULL;147148if (!cache)149return NULL;150151cache_key cache_key;152disk_cache_compute_key(cache, key, sizeof(*key), cache_key);153154if (lima_debug & LIMA_DEBUG_DISK_CACHE) {155char sha1[41];156_mesa_sha1_format(sha1, cache_key);157fprintf(stderr, "[mesa disk cache] retrieving %s: ", sha1);158}159160size_t size;161void *buffer = disk_cache_get(cache, cache_key, &size);162163if (lima_debug & LIMA_DEBUG_DISK_CACHE)164fprintf(stderr, "%s\n", buffer ? "found" : "missing");165166if (!buffer)167return NULL;168169shader = rzalloc(NULL, struct lima_fs_compiled_shader);170if (!shader)171goto out;172173struct blob_reader blob;174blob_reader_init(&blob, buffer, size);175blob_copy_bytes(&blob, &shader->state, sizeof(shader->state));176shader->shader = rzalloc_size(shader, shader->state.shader_size);177if (!shader->shader)178goto err;179blob_copy_bytes(&blob, shader->shader, shader->state.shader_size);180181out:182free(buffer);183return shader;184185err:186ralloc_free(shader);187return NULL;188}189190void191lima_disk_cache_init(struct lima_screen *screen)192{193const struct build_id_note *note =194build_id_find_nhdr_for_addr(lima_disk_cache_init);195assert(note && build_id_length(note) == 20); /* sha1 */196197const uint8_t *id_sha1 = build_id_data(note);198assert(id_sha1);199200char timestamp[41];201_mesa_sha1_format(timestamp, id_sha1);202203screen->disk_cache = disk_cache_create(screen->base.get_name(&screen->base), timestamp, 0);204}205206207