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 #include "v3d_screen.h"
30
31 #include "util/blob.h"
32 #include "util/u_upload_mgr.h"
33
34 #ifdef ENABLE_SHADER_CACHE
35
36 static uint32_t
v3d_key_size(gl_shader_stage stage)37 v3d_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
v3d_disk_cache_init(struct v3d_screen * screen)53 void v3d_disk_cache_init(struct v3d_screen *screen)
54 {
55 const struct build_id_note *note =
56 build_id_find_nhdr_for_addr(v3d_disk_cache_init);
57 assert(note && build_id_length(note) == 20);
58
59 const uint8_t *id_sha1 = build_id_data(note);
60 assert(id_sha1);
61
62 char timestamp[41];
63 _mesa_sha1_format(timestamp, id_sha1);
64
65 screen->disk_cache =
66 disk_cache_create(v3d_screen_get_name((struct pipe_screen *) screen),
67 timestamp, v3d_mesa_debug);
68 }
69
70 static void
v3d_disk_cache_compute_key(struct disk_cache * cache,const struct v3d_key * key,cache_key cache_key,const struct v3d_uncompiled_shader * uncompiled)71 v3d_disk_cache_compute_key(struct disk_cache *cache,
72 const struct v3d_key *key,
73 cache_key cache_key,
74 const struct v3d_uncompiled_shader *uncompiled)
75 {
76 assert(cache);
77
78 assert(uncompiled->base.type == PIPE_SHADER_IR_NIR);
79 nir_shader *nir = uncompiled->base.ir.nir;
80
81 uint32_t ckey_size = v3d_key_size(nir->info.stage);
82 struct v3d_key *ckey = malloc(ckey_size);
83 memcpy(ckey, key, ckey_size);
84
85 struct blob blob;
86 blob_init(&blob);
87 blob_write_bytes(&blob, ckey, ckey_size);
88 blob_write_bytes(&blob, uncompiled->sha1, 20);
89
90 disk_cache_compute_key(cache, blob.data, blob.size, cache_key);
91
92 blob_finish(&blob);
93 free(ckey);
94 }
95
96 struct v3d_compiled_shader *
v3d_disk_cache_retrieve(struct v3d_context * v3d,const struct v3d_key * key,const struct v3d_uncompiled_shader * uncompiled)97 v3d_disk_cache_retrieve(struct v3d_context *v3d,
98 const struct v3d_key *key,
99 const struct v3d_uncompiled_shader *uncompiled)
100 {
101 struct v3d_screen *screen = v3d->screen;
102 struct disk_cache *cache = screen->disk_cache;
103
104 if (!cache)
105 return NULL;
106
107 assert(uncompiled->base.type == PIPE_SHADER_IR_NIR);
108 nir_shader *nir = uncompiled->base.ir.nir;
109
110 cache_key cache_key;
111 v3d_disk_cache_compute_key(cache, key, cache_key, uncompiled);
112
113 size_t buffer_size;
114 void *buffer = disk_cache_get(cache, cache_key, &buffer_size);
115
116 if (V3D_DBG(CACHE)) {
117 char sha1[41];
118 _mesa_sha1_format(sha1, cache_key);
119 fprintf(stderr, "[v3d on-disk cache] %s %s\n",
120 buffer ? "hit" : "miss",
121 sha1);
122 }
123
124 if (!buffer)
125 return NULL;
126
127 /* Load data */
128 struct blob_reader blob;
129 blob_reader_init(&blob, buffer, buffer_size);
130
131 uint32_t prog_data_size = v3d_prog_data_size(nir->info.stage);
132 const void *prog_data = blob_read_bytes(&blob, prog_data_size);
133 if (blob.overrun)
134 return NULL;
135
136 uint32_t ulist_count = blob_read_uint32(&blob);
137 uint32_t ulist_contents_size = ulist_count * sizeof(enum quniform_contents);
138 const void *ulist_contents = blob_read_bytes(&blob, ulist_contents_size);
139 if (blob.overrun)
140 return NULL;
141
142 uint32_t ulist_data_size = ulist_count * sizeof(uint32_t);
143 const void *ulist_data = blob_read_bytes(&blob, ulist_data_size);
144 if (blob.overrun)
145 return NULL;
146
147 uint32_t qpu_size = blob_read_uint32(&blob);
148 const void *qpu_insts =
149 blob_read_bytes(&blob, qpu_size);
150 if (blob.overrun)
151 return NULL;
152
153 /* Assemble data */
154 struct v3d_compiled_shader *shader = rzalloc(NULL, struct v3d_compiled_shader);
155
156 shader->prog_data.base = rzalloc_size(shader, prog_data_size);
157 memcpy(shader->prog_data.base, prog_data, prog_data_size);
158
159 shader->prog_data.base->uniforms.count = ulist_count;
160
161 shader->prog_data.base->uniforms.contents =
162 ralloc_array(shader->prog_data.base, enum quniform_contents, ulist_count);
163 memcpy(shader->prog_data.base->uniforms.contents, ulist_contents, ulist_contents_size);
164
165 shader->prog_data.base->uniforms.data =
166 ralloc_array(shader->prog_data.base, uint32_t, ulist_count);
167 memcpy(shader->prog_data.base->uniforms.data, ulist_data, ulist_data_size);
168
169 u_upload_data(v3d->state_uploader, 0, qpu_size, 8,
170 qpu_insts, &shader->offset, &shader->resource);
171
172 free(buffer);
173
174 return shader;
175 }
176
177 void
v3d_disk_cache_store(struct v3d_context * v3d,const struct v3d_key * key,const struct v3d_uncompiled_shader * uncompiled,const struct v3d_compiled_shader * shader,uint64_t * qpu_insts,uint32_t qpu_size)178 v3d_disk_cache_store(struct v3d_context *v3d,
179 const struct v3d_key *key,
180 const struct v3d_uncompiled_shader *uncompiled,
181 const struct v3d_compiled_shader *shader,
182 uint64_t *qpu_insts,
183 uint32_t qpu_size)
184 {
185 struct v3d_screen *screen = v3d->screen;
186 struct disk_cache *cache = screen->disk_cache;
187
188 if (!cache)
189 return;
190
191 assert(uncompiled->base.type == PIPE_SHADER_IR_NIR);
192 nir_shader *nir = uncompiled->base.ir.nir;
193
194 cache_key cache_key;
195 v3d_disk_cache_compute_key(cache, key, cache_key, uncompiled);
196
197 if (V3D_DBG(CACHE)) {
198 char sha1[41];
199 _mesa_sha1_format(sha1, cache_key);
200 fprintf(stderr, "[v3d on-disk cache] storing %s\n", sha1);
201 }
202
203 struct blob blob;
204 blob_init(&blob);
205
206 blob_write_bytes(&blob, shader->prog_data.base, v3d_prog_data_size(nir->info.stage));
207 uint32_t ulist_count = shader->prog_data.base->uniforms.count;
208 blob_write_uint32(&blob, ulist_count);
209 blob_write_bytes(&blob,
210 shader->prog_data.base->uniforms.contents,
211 ulist_count * sizeof(enum quniform_contents));
212 blob_write_bytes(&blob,
213 shader->prog_data.base->uniforms.data,
214 ulist_count * sizeof(uint32_t));
215
216 blob_write_uint32(&blob, qpu_size);
217 blob_write_bytes(&blob, qpu_insts, qpu_size);
218
219 disk_cache_put(cache, cache_key, blob.data, blob.size, NULL);
220
221 blob_finish(&blob);
222 }
223
224 #endif /* ENABLE_SHADER_CACHE */
225
226