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