Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/crocus/crocus_disk_cache.c
4570 views
1
/*
2
* Copyright © 2018 Intel Corporation
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 shall be included
12
* in all copies or substantial portions of the Software.
13
*
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
* DEALINGS IN THE SOFTWARE.
21
*/
22
23
/**
24
* @file crocus_disk_cache.c
25
*
26
* Functions for interacting with the on-disk shader cache.
27
*/
28
29
#include <stdio.h>
30
#include <stdint.h>
31
#include <assert.h>
32
#include <string.h>
33
34
#include "compiler/nir/nir.h"
35
#include "util/blob.h"
36
#include "util/build_id.h"
37
#include "util/disk_cache.h"
38
#include "util/mesa-sha1.h"
39
40
#include "crocus_context.h"
41
42
static bool debug = false;
43
44
/**
45
* Compute a disk cache key for the given uncompiled shader and NOS key.
46
*/
47
static void
48
crocus_disk_cache_compute_key(struct disk_cache *cache,
49
const struct crocus_uncompiled_shader *ish,
50
const void *orig_prog_key,
51
uint32_t prog_key_size,
52
cache_key cache_key)
53
{
54
/* Create a copy of the program key with program_string_id zeroed out.
55
* It's essentially random data which we don't want to include in our
56
* hashing and comparisons. We'll set a proper value on a cache hit.
57
*/
58
union brw_any_prog_key prog_key;
59
memcpy(&prog_key, orig_prog_key, prog_key_size);
60
prog_key.base.program_string_id = 0;
61
62
uint8_t data[sizeof(prog_key) + sizeof(ish->nir_sha1)];
63
uint32_t data_size = prog_key_size + sizeof(ish->nir_sha1);
64
65
memcpy(data, ish->nir_sha1, sizeof(ish->nir_sha1));
66
memcpy(data + sizeof(ish->nir_sha1), &prog_key, prog_key_size);
67
68
disk_cache_compute_key(cache, data, data_size, cache_key);
69
}
70
71
/**
72
* Store the given compiled shader in the disk cache.
73
*
74
* This should only be called on newly compiled shaders. No checking is
75
* done to prevent repeated stores of the same shader.
76
*/
77
void
78
crocus_disk_cache_store(struct disk_cache *cache,
79
const struct crocus_uncompiled_shader *ish,
80
const struct crocus_compiled_shader *shader,
81
void *map,
82
const void *prog_key,
83
uint32_t prog_key_size)
84
{
85
#ifdef ENABLE_SHADER_CACHE
86
if (!cache)
87
return;
88
89
gl_shader_stage stage = ish->nir->info.stage;
90
const struct brw_stage_prog_data *prog_data = shader->prog_data;
91
92
cache_key cache_key;
93
crocus_disk_cache_compute_key(cache, ish, prog_key, prog_key_size, cache_key);
94
95
if (debug) {
96
char sha1[41];
97
_mesa_sha1_format(sha1, cache_key);
98
fprintf(stderr, "[mesa disk cache] storing %s\n", sha1);
99
}
100
101
struct blob blob;
102
blob_init(&blob);
103
104
/* We write the following data to the cache blob:
105
*
106
* 1. Prog data (must come first because it has the assembly size)
107
* 2. Assembly code
108
* 3. Number of entries in the system value array
109
* 4. System value array
110
* 5. Legacy param array (only used for compute workgroup ID)
111
* 6. Binding table
112
*/
113
blob_write_bytes(&blob, shader->prog_data, brw_prog_data_size(stage));
114
blob_write_bytes(&blob, map + shader->offset, shader->prog_data->program_size);
115
blob_write_bytes(&blob, &shader->num_system_values, sizeof(unsigned));
116
blob_write_bytes(&blob, shader->system_values,
117
shader->num_system_values * sizeof(enum brw_param_builtin));
118
blob_write_bytes(&blob, prog_data->param,
119
prog_data->nr_params * sizeof(uint32_t));
120
blob_write_bytes(&blob, &shader->bt, sizeof(shader->bt));
121
122
disk_cache_put(cache, cache_key, blob.data, blob.size, NULL);
123
blob_finish(&blob);
124
#endif
125
}
126
127
/**
128
* Search for a compiled shader in the disk cache. If found, upload it
129
* to the in-memory program cache so we can use it.
130
*/
131
struct crocus_compiled_shader *
132
crocus_disk_cache_retrieve(struct crocus_context *ice,
133
const struct crocus_uncompiled_shader *ish,
134
const void *prog_key,
135
uint32_t key_size)
136
{
137
#ifdef ENABLE_SHADER_CACHE
138
struct crocus_screen *screen = (void *) ice->ctx.screen;
139
struct disk_cache *cache = screen->disk_cache;
140
gl_shader_stage stage = ish->nir->info.stage;
141
142
if (!cache)
143
return NULL;
144
145
cache_key cache_key;
146
crocus_disk_cache_compute_key(cache, ish, prog_key, key_size, cache_key);
147
148
if (debug) {
149
char sha1[41];
150
_mesa_sha1_format(sha1, cache_key);
151
fprintf(stderr, "[mesa disk cache] retrieving %s: ", sha1);
152
}
153
154
size_t size;
155
void *buffer = disk_cache_get(screen->disk_cache, cache_key, &size);
156
157
if (debug)
158
fprintf(stderr, "%s\n", buffer ? "found" : "missing");
159
160
if (!buffer)
161
return NULL;
162
163
const uint32_t prog_data_size = brw_prog_data_size(stage);
164
165
struct brw_stage_prog_data *prog_data = ralloc_size(NULL, prog_data_size);
166
const void *assembly;
167
uint32_t num_system_values;
168
uint32_t *system_values = NULL;
169
uint32_t *so_decls = NULL;
170
171
struct blob_reader blob;
172
blob_reader_init(&blob, buffer, size);
173
blob_copy_bytes(&blob, prog_data, prog_data_size);
174
assembly = blob_read_bytes(&blob, prog_data->program_size);
175
num_system_values = blob_read_uint32(&blob);
176
if (num_system_values) {
177
system_values =
178
ralloc_array(NULL, enum brw_param_builtin, num_system_values);
179
blob_copy_bytes(&blob, system_values,
180
num_system_values * sizeof(enum brw_param_builtin));
181
}
182
183
prog_data->param = NULL;
184
prog_data->pull_param = NULL;
185
assert(prog_data->nr_pull_params == 0);
186
187
if (prog_data->nr_params) {
188
prog_data->param = ralloc_array(NULL, uint32_t, prog_data->nr_params);
189
blob_copy_bytes(&blob, prog_data->param,
190
prog_data->nr_params * sizeof(uint32_t));
191
}
192
193
struct crocus_binding_table bt;
194
blob_copy_bytes(&blob, &bt, sizeof(bt));
195
196
if ((stage == MESA_SHADER_VERTEX ||
197
stage == MESA_SHADER_TESS_EVAL ||
198
stage == MESA_SHADER_GEOMETRY) && screen->devinfo.ver > 6) {
199
struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
200
so_decls = screen->vtbl.create_so_decl_list(&ish->stream_output,
201
&vue_prog_data->vue_map);
202
}
203
204
/* System values and uniforms are stored in constant buffer 0, the
205
* user-facing UBOs are indexed by one. So if any constant buffer is
206
* needed, the constant buffer 0 will be needed, so account for it.
207
*/
208
unsigned num_cbufs = ish->nir->info.num_ubos;
209
210
if (num_cbufs || ish->nir->num_uniforms)
211
num_cbufs++;
212
213
if (num_system_values)
214
num_cbufs++;
215
216
/* Upload our newly read shader to the in-memory program cache and
217
* return it to the caller.
218
*/
219
struct crocus_compiled_shader *shader =
220
crocus_upload_shader(ice, stage, key_size, prog_key, assembly,
221
prog_data->program_size,
222
prog_data, prog_data_size, so_decls, system_values,
223
num_system_values, num_cbufs, &bt);
224
225
free(buffer);
226
227
return shader;
228
#else
229
return NULL;
230
#endif
231
}
232
233
/**
234
* Initialize the on-disk shader cache.
235
*/
236
void
237
crocus_disk_cache_init(struct crocus_screen *screen)
238
{
239
#ifdef ENABLE_SHADER_CACHE
240
if (INTEL_DEBUG & DEBUG_DISK_CACHE_DISABLE_MASK)
241
return;
242
243
/* array length = print length + nul char + 1 extra to verify it's unused */
244
char renderer[13];
245
UNUSED int len =
246
snprintf(renderer, sizeof(renderer), "crocus_%04x", screen->pci_id);
247
assert(len == sizeof(renderer) - 2);
248
249
const struct build_id_note *note =
250
build_id_find_nhdr_for_addr(crocus_disk_cache_init);
251
assert(note && build_id_length(note) == 20); /* sha1 */
252
253
const uint8_t *id_sha1 = build_id_data(note);
254
assert(id_sha1);
255
256
char timestamp[41];
257
_mesa_sha1_format(timestamp, id_sha1);
258
259
const uint64_t driver_flags =
260
brw_get_compiler_config_value(screen->compiler);
261
screen->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);
262
#endif
263
}
264
265