iris: Flag constants dirty on program changes
[mesa.git] / src / gallium / drivers / iris / iris_program_cache.c
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 iris_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 "iris_context.h"
45 #include "iris_resource.h"
46
47 struct keybox {
48 uint8_t size;
49 enum iris_program_cache_id cache_id;
50 uint8_t data[0];
51 };
52
53 static uint32_t
54 key_size_for_cache(enum iris_program_cache_id cache_id)
55 {
56 static const unsigned key_sizes[] = {
57 [IRIS_CACHE_VS] = sizeof(struct brw_vs_prog_key),
58 [IRIS_CACHE_TCS] = sizeof(struct brw_tcs_prog_key),
59 [IRIS_CACHE_TES] = sizeof(struct brw_tes_prog_key),
60 [IRIS_CACHE_GS] = sizeof(struct brw_gs_prog_key),
61 [IRIS_CACHE_FS] = sizeof(struct brw_wm_prog_key),
62 [IRIS_CACHE_CS] = sizeof(struct brw_cs_prog_key),
63 };
64
65 /* BLORP keys aren't all the same size. */
66 assert(cache_id != IRIS_CACHE_BLORP);
67
68 return key_sizes[cache_id];
69 }
70
71 static struct keybox *
72 make_keybox(void *mem_ctx,
73 enum iris_program_cache_id cache_id,
74 const void *key,
75 uint32_t key_size)
76 {
77 struct keybox *keybox =
78 ralloc_size(mem_ctx, sizeof(struct keybox) + key_size);
79
80 keybox->cache_id = cache_id;
81 keybox->size = key_size;
82 memcpy(keybox->data, key, key_size);
83
84 return keybox;
85 }
86
87 static uint32_t
88 keybox_hash(const void *void_key)
89 {
90 const struct keybox *key = void_key;
91 return _mesa_hash_data(&key->cache_id, key->size + sizeof(key->cache_id));
92 }
93
94 static bool
95 keybox_equals(const void *void_a, const void *void_b)
96 {
97 const struct keybox *a = void_a, *b = void_b;
98 if (a->size != b->size)
99 return false;
100
101 return memcmp(a->data, b->data, a->size) == 0;
102 }
103
104 static uint64_t
105 dirty_flag_for_cache(enum iris_program_cache_id cache_id)
106 {
107 assert(cache_id <= MESA_SHADER_STAGES);
108
109 uint64_t flags = (IRIS_DIRTY_VS |
110 IRIS_DIRTY_BINDINGS_VS |
111 IRIS_DIRTY_CONSTANTS_VS) << cache_id;
112 // XXX: ugly...
113 // XXX: move this flagging out to a higher level, allow comparison of
114 // XXX: new and old programs to decide what bits to twiddle
115 // XXX: CLIP: toggle if barycentric modes has any NONPERSPECTIVE or not
116 if (cache_id == IRIS_CACHE_FS)
117 flags |= IRIS_DIRTY_WM | IRIS_DIRTY_CLIP | IRIS_DIRTY_SBE;
118 if (cache_id == IRIS_CACHE_VS)
119 flags |= IRIS_DIRTY_VF_SGVS;
120
121 return flags;
122 }
123
124 static unsigned
125 get_program_string_id(enum iris_program_cache_id cache_id, const void *key)
126 {
127 switch (cache_id) {
128 case IRIS_CACHE_VS:
129 return ((struct brw_vs_prog_key *) key)->program_string_id;
130 case IRIS_CACHE_TCS:
131 return ((struct brw_tcs_prog_key *) key)->program_string_id;
132 case IRIS_CACHE_TES:
133 return ((struct brw_tes_prog_key *) key)->program_string_id;
134 case IRIS_CACHE_GS:
135 return ((struct brw_gs_prog_key *) key)->program_string_id;
136 case IRIS_CACHE_CS:
137 return ((struct brw_cs_prog_key *) key)->program_string_id;
138 case IRIS_CACHE_FS:
139 return ((struct brw_wm_prog_key *) key)->program_string_id;
140 default:
141 unreachable("no program string id for this kind of program");
142 }
143 }
144
145 static struct iris_compiled_shader *
146 iris_find_cached_shader(struct iris_context *ice,
147 enum iris_program_cache_id cache_id,
148 const void *key,
149 uint32_t key_size)
150 {
151 struct keybox *keybox =
152 make_keybox(ice->shaders.cache, cache_id, key, key_size);
153 struct hash_entry *entry =
154 _mesa_hash_table_search(ice->shaders.cache, keybox);
155
156 ralloc_free(keybox);
157
158 return entry ? entry->data : NULL;
159 }
160
161 /**
162 * Looks for a program in the cache and binds it.
163 *
164 * If no program was found, returns false and leaves the binding alone.
165 */
166 bool
167 iris_bind_cached_shader(struct iris_context *ice,
168 enum iris_program_cache_id cache_id,
169 const void *key)
170 {
171 unsigned key_size = key_size_for_cache(cache_id);
172 struct iris_compiled_shader *shader =
173 iris_find_cached_shader(ice, cache_id, key, key_size);
174
175 if (!shader)
176 return false;
177
178 // XXX: why memcmp?
179 if (!ice->shaders.prog[cache_id] ||
180 memcmp(shader, ice->shaders.prog[cache_id], sizeof(*shader)) != 0) {
181 ice->shaders.prog[cache_id] = shader;
182 ice->state.dirty |= dirty_flag_for_cache(cache_id);
183 }
184
185 return true;
186 }
187
188 void
189 iris_unbind_shader(struct iris_context *ice,
190 enum iris_program_cache_id cache_id)
191 {
192 if (ice->shaders.prog[cache_id]) {
193 ice->shaders.prog[cache_id] = NULL;
194 ice->state.dirty |= dirty_flag_for_cache(cache_id);
195 }
196 }
197
198 const void *
199 iris_find_previous_compile(const struct iris_context *ice,
200 enum iris_program_cache_id cache_id,
201 unsigned program_string_id)
202 {
203 hash_table_foreach(ice->shaders.cache, entry) {
204 const struct keybox *keybox = entry->key;
205 if (keybox->cache_id == cache_id &&
206 get_program_string_id(cache_id, keybox->data) == program_string_id) {
207 return keybox->data;
208 }
209 }
210
211 return NULL;
212 }
213
214 /**
215 * Look for an existing entry in the cache that has identical assembly code.
216 *
217 * This is useful for programs generating shaders at runtime, where multiple
218 * distinct shaders (from an API perspective) may compile to the same assembly
219 * in our backend. This saves space in the program cache buffer.
220 */
221 static const struct iris_compiled_shader *
222 find_existing_assembly(struct hash_table *cache,
223 const void *assembly,
224 unsigned assembly_size)
225 {
226 hash_table_foreach(cache, entry) {
227 const struct iris_compiled_shader *existing = entry->data;
228 if (existing->prog_data->program_size == assembly_size &&
229 memcmp(existing->map, assembly, assembly_size) == 0)
230 return existing;
231 }
232 return NULL;
233 }
234
235 static struct iris_compiled_shader *
236 iris_upload_shader(struct iris_context *ice,
237 enum iris_program_cache_id cache_id,
238 uint32_t key_size,
239 const void *key,
240 const void *assembly,
241 struct brw_stage_prog_data *prog_data,
242 uint32_t *streamout)
243 {
244 struct iris_screen *screen = (void *) ice->ctx.screen;
245 struct gen_device_info *devinfo = &screen->devinfo;
246 struct hash_table *cache = ice->shaders.cache;
247 struct iris_compiled_shader *shader =
248 rzalloc_size(cache, sizeof(struct iris_compiled_shader) +
249 ice->vtbl.derived_program_state_size(cache_id));
250 const struct iris_compiled_shader *existing =
251 find_existing_assembly(cache, assembly, prog_data->program_size);
252
253 /* If we can find a matching prog in the cache already, then reuse the
254 * existing stuff without creating new copy into the underlying buffer
255 * object. This is notably useful for programs generating shaders at
256 * runtime, where multiple shaders may compile to the same thing in our
257 * backend.
258 */
259 if (existing) {
260 pipe_resource_reference(&shader->assembly.res, existing->assembly.res);
261 shader->assembly.offset = existing->assembly.offset;
262 shader->map = existing->map;
263 } else {
264 shader->assembly.res = NULL;
265 u_upload_alloc(ice->shaders.uploader, 0, prog_data->program_size, 64,
266 &shader->assembly.offset, &shader->assembly.res,
267 &shader->map);
268 memcpy(shader->map, assembly, prog_data->program_size);
269 }
270
271 shader->prog_data = prog_data;
272 shader->streamout = streamout;
273
274 ralloc_steal(shader, shader->prog_data);
275 ralloc_steal(shader->prog_data, prog_data->param);
276 ralloc_steal(shader->prog_data, prog_data->pull_param);
277 ralloc_steal(shader, shader->streamout);
278
279 /* Store the 3DSTATE shader packets and other derived state. */
280 ice->vtbl.store_derived_program_state(devinfo, cache_id, shader);
281
282 struct keybox *keybox = make_keybox(cache, cache_id, key, key_size);
283 _mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
284
285 return shader;
286 }
287
288 /**
289 * Upload a new shader to the program cache, and bind it for use.
290 *
291 * \param prog_data must be ralloc'd and will be stolen.
292 */
293 void
294 iris_upload_and_bind_shader(struct iris_context *ice,
295 enum iris_program_cache_id cache_id,
296 const void *key,
297 const void *assembly,
298 struct brw_stage_prog_data *prog_data,
299 uint32_t *streamout)
300 {
301 assert(cache_id != IRIS_CACHE_BLORP);
302
303 struct iris_compiled_shader *shader =
304 iris_upload_shader(ice, cache_id, key_size_for_cache(cache_id), key,
305 assembly, prog_data, streamout);
306
307 ice->shaders.prog[cache_id] = shader;
308 ice->state.dirty |= dirty_flag_for_cache(cache_id);
309 }
310
311 bool
312 iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
313 const void *key, uint32_t key_size,
314 uint32_t *kernel_out, void *prog_data_out)
315 {
316 struct blorp_context *blorp = blorp_batch->blorp;
317 struct iris_context *ice = blorp->driver_ctx;
318 struct iris_batch *batch = blorp_batch->driver_batch;
319 struct iris_compiled_shader *shader =
320 iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key, key_size);
321
322 if (!shader)
323 return false;
324
325 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
326 *kernel_out =
327 iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
328 *((void **) prog_data_out) = shader->prog_data;
329
330 iris_use_pinned_bo(batch, bo, false);
331
332 return true;
333 }
334
335 bool
336 iris_blorp_upload_shader(struct blorp_batch *blorp_batch,
337 const void *key, uint32_t key_size,
338 const void *kernel, UNUSED uint32_t kernel_size,
339 const struct brw_stage_prog_data *prog_data_templ,
340 UNUSED uint32_t prog_data_size,
341 uint32_t *kernel_out, void *prog_data_out)
342 {
343 struct blorp_context *blorp = blorp_batch->blorp;
344 struct iris_context *ice = blorp->driver_ctx;
345 struct iris_batch *batch = blorp_batch->driver_batch;
346
347 void *prog_data = ralloc_size(NULL, prog_data_size);
348 memcpy(prog_data, prog_data_templ, prog_data_size);
349
350 struct iris_compiled_shader *shader =
351 iris_upload_shader(ice, IRIS_CACHE_BLORP, key_size, key, kernel,
352 prog_data, NULL);
353
354 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
355 *kernel_out =
356 iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
357 *((void **) prog_data_out) = shader->prog_data;
358
359 iris_use_pinned_bo(batch, bo, false);
360
361 return true;
362 }
363
364 void
365 iris_init_program_cache(struct iris_context *ice)
366 {
367 ice->shaders.cache =
368 _mesa_hash_table_create(ice, keybox_hash, keybox_equals);
369
370 ice->shaders.uploader =
371 u_upload_create(&ice->ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
372 IRIS_RESOURCE_FLAG_SHADER_MEMZONE);
373 }
374
375 void
376 iris_destroy_program_cache(struct iris_context *ice)
377 {
378 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
379 ice->shaders.prog[i] = NULL;
380 }
381
382 hash_table_foreach(ice->shaders.cache, entry) {
383 struct iris_compiled_shader *shader = entry->data;
384 pipe_resource_reference(&shader->assembly.res, NULL);
385 }
386
387 u_upload_destroy(ice->shaders.uploader);
388
389 ralloc_free(ice->shaders.cache);
390 }
391
392 static const char *
393 cache_name(enum iris_program_cache_id cache_id)
394 {
395 if (cache_id == IRIS_CACHE_BLORP)
396 return "BLORP";
397
398 return _mesa_shader_stage_to_string(cache_id);
399 }
400
401 void
402 iris_print_program_cache(struct iris_context *ice)
403 {
404 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
405 const struct gen_device_info *devinfo = &screen->devinfo;
406
407 hash_table_foreach(ice->shaders.cache, entry) {
408 const struct keybox *keybox = entry->key;
409 struct iris_compiled_shader *shader = entry->data;
410 fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
411 brw_disassemble(devinfo, shader->map, 0,
412 shader->prog_data->program_size, stderr);
413 }
414 }