1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2022 Raspberry Pi Ltd 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/** 25bf215546Sopenharmony_ci * V3D on-disk shader cache. 26bf215546Sopenharmony_ci */ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include "v3d_context.h" 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include "compiler/nir/nir_serialize.h" 31bf215546Sopenharmony_ci#include "util/blob.h" 32bf215546Sopenharmony_ci#include "util/u_upload_mgr.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#ifdef ENABLE_SHADER_CACHE 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_cistatic uint32_t 37bf215546Sopenharmony_civ3d_key_size(gl_shader_stage stage) 38bf215546Sopenharmony_ci{ 39bf215546Sopenharmony_ci static const int key_size[] = { 40bf215546Sopenharmony_ci [MESA_SHADER_VERTEX] = sizeof(struct v3d_vs_key), 41bf215546Sopenharmony_ci [MESA_SHADER_GEOMETRY] = sizeof(struct v3d_gs_key), 42bf215546Sopenharmony_ci [MESA_SHADER_FRAGMENT] = sizeof(struct v3d_fs_key), 43bf215546Sopenharmony_ci [MESA_SHADER_COMPUTE] = sizeof(struct v3d_key), 44bf215546Sopenharmony_ci }; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci assert(stage >= 0 && 47bf215546Sopenharmony_ci stage < ARRAY_SIZE(key_size) && 48bf215546Sopenharmony_ci key_size[stage]); 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci return key_size[stage]; 51bf215546Sopenharmony_ci} 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_civoid v3d_disk_cache_init(struct v3d_screen *screen) 54bf215546Sopenharmony_ci{ 55bf215546Sopenharmony_ci char *renderer; 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci ASSERTED int len = 58bf215546Sopenharmony_ci asprintf(&renderer, "V3D %d.%d", 59bf215546Sopenharmony_ci screen->devinfo.ver / 10, 60bf215546Sopenharmony_ci screen->devinfo.ver % 10); 61bf215546Sopenharmony_ci assert(len > 0); 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci const struct build_id_note *note = 64bf215546Sopenharmony_ci build_id_find_nhdr_for_addr(v3d_disk_cache_init); 65bf215546Sopenharmony_ci assert(note && build_id_length(note) == 20); 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci const uint8_t *id_sha1 = build_id_data(note); 68bf215546Sopenharmony_ci assert(id_sha1); 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci char timestamp[41]; 71bf215546Sopenharmony_ci _mesa_sha1_format(timestamp, id_sha1); 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci screen->disk_cache = disk_cache_create(renderer, timestamp, 0); 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci free(renderer); 76bf215546Sopenharmony_ci} 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_cistatic void 79bf215546Sopenharmony_civ3d_disk_cache_compute_key(struct disk_cache *cache, 80bf215546Sopenharmony_ci const struct v3d_key *key, 81bf215546Sopenharmony_ci cache_key cache_key) 82bf215546Sopenharmony_ci{ 83bf215546Sopenharmony_ci assert(cache); 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci struct v3d_uncompiled_shader *uncompiled = key->shader_state; 86bf215546Sopenharmony_ci assert(uncompiled->base.type == PIPE_SHADER_IR_NIR); 87bf215546Sopenharmony_ci nir_shader *nir = uncompiled->base.ir.nir; 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci struct blob blob; 90bf215546Sopenharmony_ci blob_init(&blob); 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci uint32_t ckey_size = v3d_key_size(nir->info.stage); 93bf215546Sopenharmony_ci struct v3d_key *ckey = malloc(ckey_size); 94bf215546Sopenharmony_ci memcpy(ckey, key, ckey_size); 95bf215546Sopenharmony_ci ckey->shader_state = NULL; 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ci blob_write_bytes(&blob, ckey, ckey_size); 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci nir_serialize(&blob, nir, true); 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci disk_cache_compute_key(cache, blob.data, blob.size, cache_key); 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci blob_finish(&blob); 104bf215546Sopenharmony_ci free(ckey); 105bf215546Sopenharmony_ci} 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_cistruct v3d_compiled_shader * 108bf215546Sopenharmony_civ3d_disk_cache_retrieve(struct v3d_context *v3d, 109bf215546Sopenharmony_ci const struct v3d_key *key) 110bf215546Sopenharmony_ci{ 111bf215546Sopenharmony_ci struct v3d_screen *screen = v3d->screen; 112bf215546Sopenharmony_ci struct disk_cache *cache = screen->disk_cache; 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci if (!cache) 115bf215546Sopenharmony_ci return NULL; 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci struct v3d_uncompiled_shader *uncompiled = key->shader_state; 118bf215546Sopenharmony_ci assert(uncompiled->base.type == PIPE_SHADER_IR_NIR); 119bf215546Sopenharmony_ci nir_shader *nir = uncompiled->base.ir.nir; 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci cache_key cache_key; 122bf215546Sopenharmony_ci v3d_disk_cache_compute_key(cache, key, cache_key); 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci size_t buffer_size; 125bf215546Sopenharmony_ci void *buffer = disk_cache_get(cache, cache_key, &buffer_size); 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci if (unlikely(V3D_DEBUG & V3D_DEBUG_CACHE)) { 128bf215546Sopenharmony_ci char sha1[41]; 129bf215546Sopenharmony_ci _mesa_sha1_format(sha1, cache_key); 130bf215546Sopenharmony_ci fprintf(stderr, "[v3d on-disk cache] %s %s\n", 131bf215546Sopenharmony_ci buffer ? "hit" : "miss", 132bf215546Sopenharmony_ci sha1); 133bf215546Sopenharmony_ci } 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci if (!buffer) 136bf215546Sopenharmony_ci return NULL; 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci /* Load data */ 139bf215546Sopenharmony_ci struct blob_reader blob; 140bf215546Sopenharmony_ci blob_reader_init(&blob, buffer, buffer_size); 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci uint32_t prog_data_size = v3d_prog_data_size(nir->info.stage); 143bf215546Sopenharmony_ci const void *prog_data = blob_read_bytes(&blob, prog_data_size); 144bf215546Sopenharmony_ci if (blob.overrun) 145bf215546Sopenharmony_ci return NULL; 146bf215546Sopenharmony_ci 147bf215546Sopenharmony_ci uint32_t ulist_count = blob_read_uint32(&blob); 148bf215546Sopenharmony_ci uint32_t ulist_contents_size = ulist_count * sizeof(enum quniform_contents); 149bf215546Sopenharmony_ci const void *ulist_contents = blob_read_bytes(&blob, ulist_contents_size); 150bf215546Sopenharmony_ci if (blob.overrun) 151bf215546Sopenharmony_ci return NULL; 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci uint32_t ulist_data_size = ulist_count * sizeof(uint32_t); 154bf215546Sopenharmony_ci const void *ulist_data = blob_read_bytes(&blob, ulist_data_size); 155bf215546Sopenharmony_ci if (blob.overrun) 156bf215546Sopenharmony_ci return NULL; 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci uint32_t qpu_size = blob_read_uint32(&blob); 159bf215546Sopenharmony_ci const void *qpu_insts = 160bf215546Sopenharmony_ci blob_read_bytes(&blob, qpu_size); 161bf215546Sopenharmony_ci if (blob.overrun) 162bf215546Sopenharmony_ci return NULL; 163bf215546Sopenharmony_ci 164bf215546Sopenharmony_ci /* Assemble data */ 165bf215546Sopenharmony_ci struct v3d_compiled_shader *shader = rzalloc(NULL, struct v3d_compiled_shader); 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci shader->prog_data.base = rzalloc_size(shader, prog_data_size); 168bf215546Sopenharmony_ci memcpy(shader->prog_data.base, prog_data, prog_data_size); 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci shader->prog_data.base->uniforms.count = ulist_count; 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci shader->prog_data.base->uniforms.contents = 173bf215546Sopenharmony_ci ralloc_array(shader->prog_data.base, enum quniform_contents, ulist_count); 174bf215546Sopenharmony_ci memcpy(shader->prog_data.base->uniforms.contents, ulist_contents, ulist_contents_size); 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_ci shader->prog_data.base->uniforms.data = 177bf215546Sopenharmony_ci ralloc_array(shader->prog_data.base, uint32_t, ulist_count); 178bf215546Sopenharmony_ci memcpy(shader->prog_data.base->uniforms.data, ulist_data, ulist_data_size); 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci u_upload_data(v3d->state_uploader, 0, qpu_size, 8, 181bf215546Sopenharmony_ci qpu_insts, &shader->offset, &shader->resource); 182bf215546Sopenharmony_ci 183bf215546Sopenharmony_ci free(buffer); 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_ci return shader; 186bf215546Sopenharmony_ci} 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_civoid 189bf215546Sopenharmony_civ3d_disk_cache_store(struct v3d_context *v3d, 190bf215546Sopenharmony_ci const struct v3d_key *key, 191bf215546Sopenharmony_ci const struct v3d_compiled_shader *shader, 192bf215546Sopenharmony_ci uint64_t *qpu_insts, 193bf215546Sopenharmony_ci uint32_t qpu_size) 194bf215546Sopenharmony_ci{ 195bf215546Sopenharmony_ci struct v3d_screen *screen = v3d->screen; 196bf215546Sopenharmony_ci struct disk_cache *cache = screen->disk_cache; 197bf215546Sopenharmony_ci 198bf215546Sopenharmony_ci if (!cache) 199bf215546Sopenharmony_ci return; 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_ci struct v3d_uncompiled_shader *uncompiled = key->shader_state; 202bf215546Sopenharmony_ci assert(uncompiled->base.type == PIPE_SHADER_IR_NIR); 203bf215546Sopenharmony_ci nir_shader *nir = uncompiled->base.ir.nir; 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci cache_key cache_key; 206bf215546Sopenharmony_ci v3d_disk_cache_compute_key(cache, key, cache_key); 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci if (unlikely(V3D_DEBUG & V3D_DEBUG_CACHE)) { 209bf215546Sopenharmony_ci char sha1[41]; 210bf215546Sopenharmony_ci _mesa_sha1_format(sha1, cache_key); 211bf215546Sopenharmony_ci fprintf(stderr, "[v3d on-disk cache] storing %s\n", sha1); 212bf215546Sopenharmony_ci } 213bf215546Sopenharmony_ci 214bf215546Sopenharmony_ci struct blob blob; 215bf215546Sopenharmony_ci blob_init(&blob); 216bf215546Sopenharmony_ci 217bf215546Sopenharmony_ci blob_write_bytes(&blob, shader->prog_data.base, v3d_prog_data_size(nir->info.stage)); 218bf215546Sopenharmony_ci uint32_t ulist_count = shader->prog_data.base->uniforms.count; 219bf215546Sopenharmony_ci blob_write_uint32(&blob, ulist_count); 220bf215546Sopenharmony_ci blob_write_bytes(&blob, 221bf215546Sopenharmony_ci shader->prog_data.base->uniforms.contents, 222bf215546Sopenharmony_ci ulist_count * sizeof(enum quniform_contents)); 223bf215546Sopenharmony_ci blob_write_bytes(&blob, 224bf215546Sopenharmony_ci shader->prog_data.base->uniforms.data, 225bf215546Sopenharmony_ci ulist_count * sizeof(uint32_t)); 226bf215546Sopenharmony_ci 227bf215546Sopenharmony_ci blob_write_uint32(&blob, qpu_size); 228bf215546Sopenharmony_ci blob_write_bytes(&blob, qpu_insts, qpu_size); 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci disk_cache_put(cache, cache_key, blob.data, blob.size, NULL); 231bf215546Sopenharmony_ci 232bf215546Sopenharmony_ci blob_finish(&blob); 233bf215546Sopenharmony_ci} 234bf215546Sopenharmony_ci 235bf215546Sopenharmony_ci#endif /* ENABLE_SHADER_CACHE */ 236bf215546Sopenharmony_ci 237