Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/crocus/crocus_program_cache.c
4570 views
1
/*
2
* Copyright © 2017 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_program_cache.c
25
*
26
* The in-memory program cache. This is basically a hash table mapping
27
* API-specified shaders and a state key to a compiled variant. It also
28
* takes care of uploading shader assembly into a BO for use on the GPU.
29
*/
30
31
#include <stdio.h>
32
#include <errno.h>
33
#include "pipe/p_defines.h"
34
#include "pipe/p_state.h"
35
#include "pipe/p_context.h"
36
#include "pipe/p_screen.h"
37
#include "util/u_atomic.h"
38
#include "util/u_upload_mgr.h"
39
#include "compiler/nir/nir.h"
40
#include "compiler/nir/nir_builder.h"
41
#include "intel/compiler/brw_compiler.h"
42
#include "intel/compiler/brw_eu.h"
43
#include "intel/compiler/brw_nir.h"
44
#include "crocus_context.h"
45
#include "crocus_resource.h"
46
47
struct keybox {
48
uint16_t size;
49
enum crocus_program_cache_id cache_id;
50
uint8_t data[0];
51
};
52
53
static struct keybox *
54
make_keybox(void *mem_ctx, enum crocus_program_cache_id cache_id,
55
const void *key, uint32_t key_size)
56
{
57
struct keybox *keybox =
58
ralloc_size(mem_ctx, sizeof(struct keybox) + key_size);
59
60
keybox->cache_id = cache_id;
61
keybox->size = key_size;
62
memcpy(keybox->data, key, key_size);
63
64
return keybox;
65
}
66
67
static uint32_t
68
keybox_hash(const void *void_key)
69
{
70
const struct keybox *key = void_key;
71
return _mesa_hash_data(&key->cache_id, key->size + sizeof(key->cache_id));
72
}
73
74
static bool
75
keybox_equals(const void *void_a, const void *void_b)
76
{
77
const struct keybox *a = void_a, *b = void_b;
78
if (a->size != b->size)
79
return false;
80
81
return memcmp(a->data, b->data, a->size) == 0;
82
}
83
84
struct crocus_compiled_shader *
85
crocus_find_cached_shader(struct crocus_context *ice,
86
enum crocus_program_cache_id cache_id,
87
uint32_t key_size, const void *key)
88
{
89
struct keybox *keybox = make_keybox(NULL, cache_id, key, key_size);
90
struct hash_entry *entry =
91
_mesa_hash_table_search(ice->shaders.cache, keybox);
92
93
ralloc_free(keybox);
94
95
return entry ? entry->data : NULL;
96
}
97
98
const void *
99
crocus_find_previous_compile(const struct crocus_context *ice,
100
enum crocus_program_cache_id cache_id,
101
unsigned program_string_id)
102
{
103
hash_table_foreach(ice->shaders.cache, entry) {
104
const struct keybox *keybox = entry->key;
105
const struct brw_base_prog_key *key = (const void *)keybox->data;
106
if (keybox->cache_id == cache_id &&
107
key->program_string_id == program_string_id) {
108
return keybox->data;
109
}
110
}
111
112
return NULL;
113
}
114
115
/**
116
* Look for an existing entry in the cache that has identical assembly code.
117
*
118
* This is useful for programs generating shaders at runtime, where multiple
119
* distinct shaders (from an API perspective) may compile to the same assembly
120
* in our backend. This saves space in the program cache buffer.
121
*/
122
static const struct crocus_compiled_shader *
123
find_existing_assembly(struct hash_table *cache, void *map,
124
const void *assembly, unsigned assembly_size)
125
{
126
hash_table_foreach (cache, entry) {
127
const struct crocus_compiled_shader *existing = entry->data;
128
129
if (existing->map_size != assembly_size)
130
continue;
131
132
if (memcmp(map + existing->offset, assembly, assembly_size) == 0)
133
return existing;
134
}
135
return NULL;
136
}
137
138
static void
139
crocus_cache_new_bo(struct crocus_context *ice,
140
uint32_t new_size)
141
{
142
struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
143
struct crocus_bo *new_bo;
144
new_bo = crocus_bo_alloc(screen->bufmgr, "program cache", new_size);
145
146
void *map = crocus_bo_map(NULL, new_bo, MAP_READ | MAP_WRITE |
147
MAP_ASYNC | MAP_PERSISTENT);
148
149
if (ice->shaders.cache_next_offset != 0) {
150
memcpy(map, ice->shaders.cache_bo_map, ice->shaders.cache_next_offset);
151
}
152
153
crocus_bo_unmap(ice->shaders.cache_bo);
154
crocus_bo_unreference(ice->shaders.cache_bo);
155
ice->shaders.cache_bo = new_bo;
156
ice->shaders.cache_bo_map = map;
157
158
if (screen->devinfo.ver == 4) {
159
/* reemit all shaders on GEN4 only. */
160
ice->state.dirty |= CROCUS_DIRTY_CLIP | CROCUS_DIRTY_RASTER |
161
CROCUS_DIRTY_WM;
162
}
163
ice->batches[CROCUS_BATCH_RENDER].state_base_address_emitted = false;
164
ice->batches[CROCUS_BATCH_COMPUTE].state_base_address_emitted = false;
165
/* unset state base address */
166
}
167
168
static uint32_t
169
crocus_alloc_item_data(struct crocus_context *ice, uint32_t size)
170
{
171
if (ice->shaders.cache_next_offset + size > ice->shaders.cache_bo->size) {
172
uint32_t new_size = ice->shaders.cache_bo->size * 2;
173
while (ice->shaders.cache_next_offset + size > new_size)
174
new_size *= 2;
175
176
crocus_cache_new_bo(ice, new_size);
177
}
178
uint32_t offset = ice->shaders.cache_next_offset;
179
180
/* Programs are always 64-byte aligned, so set up the next one now */
181
ice->shaders.cache_next_offset = ALIGN(offset + size, 64);
182
return offset;
183
}
184
185
struct crocus_compiled_shader *
186
crocus_upload_shader(struct crocus_context *ice,
187
enum crocus_program_cache_id cache_id, uint32_t key_size,
188
const void *key, const void *assembly, uint32_t asm_size,
189
struct brw_stage_prog_data *prog_data,
190
uint32_t prog_data_size, uint32_t *streamout,
191
enum brw_param_builtin *system_values,
192
unsigned num_system_values, unsigned num_cbufs,
193
const struct crocus_binding_table *bt)
194
{
195
struct hash_table *cache = ice->shaders.cache;
196
struct crocus_compiled_shader *shader =
197
rzalloc_size(cache, sizeof(struct crocus_compiled_shader));
198
const struct crocus_compiled_shader *existing = find_existing_assembly(
199
cache, ice->shaders.cache_bo_map, assembly, asm_size);
200
201
/* If we can find a matching prog in the cache already, then reuse the
202
* existing stuff without creating new copy into the underlying buffer
203
* object. This is notably useful for programs generating shaders at
204
* runtime, where multiple shaders may compile to the same thing in our
205
* backend.
206
*/
207
if (existing) {
208
shader->offset = existing->offset;
209
shader->map_size = existing->map_size;
210
} else {
211
shader->offset = crocus_alloc_item_data(ice, asm_size);
212
shader->map_size = asm_size;
213
214
memcpy(ice->shaders.cache_bo_map + shader->offset, assembly, asm_size);
215
}
216
217
shader->prog_data = prog_data;
218
shader->prog_data_size = prog_data_size;
219
shader->streamout = streamout;
220
shader->system_values = system_values;
221
shader->num_system_values = num_system_values;
222
shader->num_cbufs = num_cbufs;
223
shader->bt = *bt;
224
225
ralloc_steal(shader, shader->prog_data);
226
if (prog_data_size > 16) {
227
ralloc_steal(shader->prog_data, prog_data->param);
228
ralloc_steal(shader->prog_data, prog_data->pull_param);
229
}
230
ralloc_steal(shader, shader->streamout);
231
ralloc_steal(shader, shader->system_values);
232
233
struct keybox *keybox = make_keybox(shader, cache_id, key, key_size);
234
_mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
235
236
return shader;
237
}
238
239
bool
240
crocus_blorp_lookup_shader(struct blorp_batch *blorp_batch, const void *key,
241
uint32_t key_size, uint32_t *kernel_out,
242
void *prog_data_out)
243
{
244
struct blorp_context *blorp = blorp_batch->blorp;
245
struct crocus_context *ice = blorp->driver_ctx;
246
struct crocus_compiled_shader *shader =
247
crocus_find_cached_shader(ice, CROCUS_CACHE_BLORP, key_size, key);
248
249
if (!shader)
250
return false;
251
252
*kernel_out = shader->offset;
253
*((void **)prog_data_out) = shader->prog_data;
254
255
return true;
256
}
257
258
bool
259
crocus_blorp_upload_shader(struct blorp_batch *blorp_batch, uint32_t stage,
260
const void *key, uint32_t key_size,
261
const void *kernel, uint32_t kernel_size,
262
const struct brw_stage_prog_data *prog_data_templ,
263
uint32_t prog_data_size, uint32_t *kernel_out,
264
void *prog_data_out)
265
{
266
struct blorp_context *blorp = blorp_batch->blorp;
267
struct crocus_context *ice = blorp->driver_ctx;
268
269
struct brw_stage_prog_data *prog_data = ralloc_size(NULL, prog_data_size);
270
memcpy(prog_data, prog_data_templ, prog_data_size);
271
272
struct crocus_binding_table bt;
273
memset(&bt, 0, sizeof(bt));
274
275
struct crocus_compiled_shader *shader = crocus_upload_shader(
276
ice, CROCUS_CACHE_BLORP, key_size, key, kernel, kernel_size, prog_data,
277
prog_data_size, NULL, NULL, 0, 0, &bt);
278
279
*kernel_out = shader->offset;
280
*((void **)prog_data_out) = shader->prog_data;
281
282
return true;
283
}
284
285
void
286
crocus_init_program_cache(struct crocus_context *ice)
287
{
288
struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
289
ice->shaders.cache =
290
_mesa_hash_table_create(ice, keybox_hash, keybox_equals);
291
292
ice->shaders.cache_bo =
293
crocus_bo_alloc(screen->bufmgr, "program_cache", 16384);
294
ice->shaders.cache_bo_map =
295
crocus_bo_map(NULL, ice->shaders.cache_bo,
296
MAP_READ | MAP_WRITE | MAP_ASYNC | MAP_PERSISTENT);
297
}
298
299
void
300
crocus_destroy_program_cache(struct crocus_context *ice)
301
{
302
for (int i = 0; i < MESA_SHADER_STAGES; i++) {
303
ice->shaders.prog[i] = NULL;
304
}
305
306
if (ice->shaders.cache_bo) {
307
crocus_bo_unmap(ice->shaders.cache_bo);
308
crocus_bo_unreference(ice->shaders.cache_bo);
309
ice->shaders.cache_bo_map = NULL;
310
ice->shaders.cache_bo = NULL;
311
}
312
313
ralloc_free(ice->shaders.cache);
314
}
315
316
static const char *
317
cache_name(enum crocus_program_cache_id cache_id)
318
{
319
if (cache_id == CROCUS_CACHE_BLORP)
320
return "BLORP";
321
322
if (cache_id == CROCUS_CACHE_SF)
323
return "SF";
324
325
if (cache_id == CROCUS_CACHE_CLIP)
326
return "CLIP";
327
328
if (cache_id == CROCUS_CACHE_FF_GS)
329
return "FF_GS";
330
331
return _mesa_shader_stage_to_string(cache_id);
332
}
333
334
void
335
crocus_print_program_cache(struct crocus_context *ice)
336
{
337
struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
338
const struct intel_device_info *devinfo = &screen->devinfo;
339
340
hash_table_foreach(ice->shaders.cache, entry) {
341
const struct keybox *keybox = entry->key;
342
struct crocus_compiled_shader *shader = entry->data;
343
fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
344
brw_disassemble(devinfo, ice->shaders.cache_bo_map + shader->offset, 0,
345
shader->prog_data->program_size, NULL, stderr);
346
}
347
}
348
349