2 * Copyright © 2017 Intel Corporation
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
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
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_atomic.h"
30 #include "util/u_upload_mgr.h"
31 #include "compiler/nir/nir.h"
32 #include "compiler/nir/nir_builder.h"
33 #include "intel/compiler/brw_compiler.h"
34 #include "intel/compiler/brw_eu.h"
35 #include "intel/compiler/brw_nir.h"
36 #include "iris_context.h"
37 #include "iris_resource.h"
41 enum iris_program_cache_id cache_id
;
46 key_size_for_cache(enum iris_program_cache_id cache_id
)
48 static const unsigned key_sizes
[] = {
49 [IRIS_CACHE_VS
] = sizeof(struct brw_vs_prog_key
),
50 [IRIS_CACHE_TCS
] = sizeof(struct brw_tcs_prog_key
),
51 [IRIS_CACHE_TES
] = sizeof(struct brw_tes_prog_key
),
52 [IRIS_CACHE_GS
] = sizeof(struct brw_gs_prog_key
),
53 [IRIS_CACHE_FS
] = sizeof(struct brw_wm_prog_key
),
54 [IRIS_CACHE_CS
] = sizeof(struct brw_cs_prog_key
),
57 /* BLORP keys aren't all the same size. */
58 assert(cache_id
!= IRIS_CACHE_BLORP
);
60 return key_sizes
[cache_id
];
63 static struct keybox
*
64 make_keybox(void *mem_ctx
,
65 enum iris_program_cache_id cache_id
,
69 struct keybox
*keybox
=
70 ralloc_size(mem_ctx
, sizeof(struct keybox
) + key_size
);
72 keybox
->cache_id
= cache_id
;
73 keybox
->size
= key_size
;
74 memcpy(keybox
->data
, key
, key_size
);
80 keybox_hash(const void *void_key
)
82 const struct keybox
*key
= void_key
;
83 return _mesa_hash_data(&key
->cache_id
, key
->size
+ sizeof(key
->cache_id
));
87 keybox_equals(const void *void_a
, const void *void_b
)
89 const struct keybox
*a
= void_a
, *b
= void_b
;
90 if (a
->size
!= b
->size
)
93 return memcmp(a
->data
, b
->data
, a
->size
) == 0;
97 dirty_flag_for_cache(enum iris_program_cache_id cache_id
)
99 assert(cache_id
<= MESA_SHADER_STAGES
);
102 // XXX: move this flagging out to a higher level, allow comparison of
103 // XXX: new and old programs to decide what bits to twiddle
104 // XXX: CLIP: toggle if barycentric modes has any NONPERSPECTIVE or not
105 if (cache_id
== IRIS_CACHE_FS
)
106 return IRIS_DIRTY_WM
| IRIS_DIRTY_FS
| IRIS_DIRTY_CLIP
;
107 if (cache_id
== IRIS_CACHE_VS
)
108 return IRIS_DIRTY_VS
| IRIS_DIRTY_VF_SGVS
;
110 return IRIS_DIRTY_VS
<< cache_id
;
114 get_program_string_id(enum iris_program_cache_id cache_id
, const void *key
)
118 return ((struct brw_vs_prog_key
*) key
)->program_string_id
;
120 return ((struct brw_tcs_prog_key
*) key
)->program_string_id
;
122 return ((struct brw_tes_prog_key
*) key
)->program_string_id
;
124 return ((struct brw_gs_prog_key
*) key
)->program_string_id
;
126 return ((struct brw_cs_prog_key
*) key
)->program_string_id
;
128 return ((struct brw_wm_prog_key
*) key
)->program_string_id
;
130 unreachable("no program string id for this kind of program");
134 static struct iris_compiled_shader
*
135 iris_find_cached_shader(struct iris_context
*ice
,
136 enum iris_program_cache_id cache_id
,
140 struct keybox
*keybox
=
141 make_keybox(ice
->shaders
.cache
, cache_id
, key
, key_size
);
142 struct hash_entry
*entry
=
143 _mesa_hash_table_search(ice
->shaders
.cache
, keybox
);
147 return entry
? entry
->data
: NULL
;
151 * Looks for a program in the cache and binds it.
153 * If no program was found, returns false and leaves the binding alone.
156 iris_bind_cached_shader(struct iris_context
*ice
,
157 enum iris_program_cache_id cache_id
,
160 unsigned key_size
= key_size_for_cache(cache_id
);
161 struct iris_compiled_shader
*shader
=
162 iris_find_cached_shader(ice
, cache_id
, key
, key_size
);
167 if (memcmp(shader
, ice
->shaders
.prog
[cache_id
], sizeof(*shader
)) != 0) {
168 ice
->shaders
.prog
[cache_id
] = shader
;
169 ice
->state
.dirty
|= dirty_flag_for_cache(cache_id
);
176 iris_find_previous_compile(const struct iris_context
*ice
,
177 enum iris_program_cache_id cache_id
,
178 unsigned program_string_id
)
180 hash_table_foreach(ice
->shaders
.cache
, entry
) {
181 const struct keybox
*keybox
= entry
->key
;
182 if (keybox
->cache_id
== cache_id
&&
183 get_program_string_id(cache_id
, keybox
->data
) == program_string_id
) {
192 * Look for an existing entry in the cache that has identical assembly code.
194 * This is useful for programs generating shaders at runtime, where multiple
195 * distinct shaders (from an API perspective) may compile to the same assembly
196 * in our backend. This saves space in the program cache buffer.
198 static const struct iris_compiled_shader
*
199 find_existing_assembly(struct hash_table
*cache
,
200 const void *assembly
,
201 unsigned assembly_size
)
203 hash_table_foreach(cache
, entry
) {
204 const struct iris_compiled_shader
*existing
= entry
->data
;
205 if (existing
->prog_data
->program_size
== assembly_size
&&
206 memcmp(existing
->map
, assembly
, assembly_size
) == 0)
212 static struct iris_compiled_shader
*
213 iris_upload_shader(struct iris_context
*ice
,
214 enum iris_program_cache_id cache_id
,
217 const void *assembly
,
218 struct brw_stage_prog_data
*prog_data
,
221 struct iris_screen
*screen
= (void *) ice
->ctx
.screen
;
222 struct gen_device_info
*devinfo
= &screen
->devinfo
;
223 struct hash_table
*cache
= ice
->shaders
.cache
;
224 struct iris_compiled_shader
*shader
=
225 rzalloc_size(cache
, sizeof(struct iris_compiled_shader
) +
226 ice
->vtbl
.derived_program_state_size(cache_id
));
227 const struct iris_compiled_shader
*existing
=
228 find_existing_assembly(cache
, assembly
, prog_data
->program_size
);
230 /* If we can find a matching prog in the cache already, then reuse the
231 * existing stuff without creating new copy into the underlying buffer
232 * object. This is notably useful for programs generating shaders at
233 * runtime, where multiple shaders may compile to the same thing in our
237 pipe_resource_reference(&shader
->assembly
.res
, existing
->assembly
.res
);
238 shader
->assembly
.offset
= existing
->assembly
.offset
;
239 shader
->map
= existing
->map
;
241 shader
->assembly
.res
= NULL
;
242 u_upload_alloc(ice
->shaders
.uploader
, 0, prog_data
->program_size
, 64,
243 &shader
->assembly
.offset
, &shader
->assembly
.res
,
245 memcpy(shader
->map
, assembly
, prog_data
->program_size
);
248 shader
->prog_data
= prog_data
;
249 shader
->streamout
= streamout
;
251 ralloc_steal(shader
, shader
->prog_data
);
252 ralloc_steal(shader
->prog_data
, prog_data
->param
);
253 ralloc_steal(shader
->prog_data
, prog_data
->pull_param
);
254 ralloc_steal(shader
, shader
->streamout
);
256 /* Store the 3DSTATE shader packets and other derived state. */
257 ice
->vtbl
.store_derived_program_state(devinfo
, cache_id
, shader
);
259 struct keybox
*keybox
= make_keybox(cache
, cache_id
, key
, key_size
);
260 _mesa_hash_table_insert(ice
->shaders
.cache
, keybox
, shader
);
266 * Upload a new shader to the program cache, and bind it for use.
268 * \param prog_data must be ralloc'd and will be stolen.
271 iris_upload_and_bind_shader(struct iris_context
*ice
,
272 enum iris_program_cache_id cache_id
,
274 const void *assembly
,
275 struct brw_stage_prog_data
*prog_data
,
278 assert(cache_id
!= IRIS_CACHE_BLORP
);
280 struct iris_compiled_shader
*shader
=
281 iris_upload_shader(ice
, cache_id
, key_size_for_cache(cache_id
), key
,
282 assembly
, prog_data
, streamout
);
284 ice
->shaders
.prog
[cache_id
] = shader
;
285 ice
->state
.dirty
|= dirty_flag_for_cache(cache_id
);
289 iris_blorp_lookup_shader(struct blorp_batch
*blorp_batch
,
290 const void *key
, uint32_t key_size
,
291 uint32_t *kernel_out
, void *prog_data_out
)
293 struct blorp_context
*blorp
= blorp_batch
->blorp
;
294 struct iris_context
*ice
= blorp
->driver_ctx
;
295 struct iris_batch
*batch
= blorp_batch
->driver_batch
;
296 struct iris_compiled_shader
*shader
=
297 iris_find_cached_shader(ice
, IRIS_CACHE_BLORP
, key
, key_size
);
302 struct iris_bo
*bo
= iris_resource_bo(shader
->assembly
.res
);
304 iris_bo_offset_from_base_address(bo
) + shader
->assembly
.offset
;
305 *((void **) prog_data_out
) = shader
->prog_data
;
307 iris_use_pinned_bo(batch
, bo
, false);
313 iris_blorp_upload_shader(struct blorp_batch
*blorp_batch
,
314 const void *key
, uint32_t key_size
,
315 const void *kernel
, UNUSED
uint32_t kernel_size
,
316 const struct brw_stage_prog_data
*prog_data_templ
,
317 UNUSED
uint32_t prog_data_size
,
318 uint32_t *kernel_out
, void *prog_data_out
)
320 struct blorp_context
*blorp
= blorp_batch
->blorp
;
321 struct iris_context
*ice
= blorp
->driver_ctx
;
322 struct iris_batch
*batch
= blorp_batch
->driver_batch
;
324 void *prog_data
= ralloc_size(NULL
, prog_data_size
);
325 memcpy(prog_data
, prog_data_templ
, prog_data_size
);
327 struct iris_compiled_shader
*shader
=
328 iris_upload_shader(ice
, IRIS_CACHE_BLORP
, key_size
, key
, kernel
,
331 struct iris_bo
*bo
= iris_resource_bo(shader
->assembly
.res
);
333 iris_bo_offset_from_base_address(bo
) + shader
->assembly
.offset
;
334 *((void **) prog_data_out
) = shader
->prog_data
;
336 iris_use_pinned_bo(batch
, bo
, false);
342 iris_init_program_cache(struct iris_context
*ice
)
345 _mesa_hash_table_create(ice
, keybox_hash
, keybox_equals
);
347 ice
->shaders
.uploader
=
348 u_upload_create(&ice
->ctx
, 16384, PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
,
349 IRIS_RESOURCE_FLAG_SHADER_MEMZONE
);
353 iris_destroy_program_cache(struct iris_context
*ice
)
355 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
356 ice
->shaders
.prog
[i
] = NULL
;
359 hash_table_foreach(ice
->shaders
.cache
, entry
) {
360 struct iris_compiled_shader
*shader
= entry
->data
;
361 pipe_resource_reference(&shader
->assembly
.res
, NULL
);
364 u_upload_destroy(ice
->shaders
.uploader
);
366 ralloc_free(ice
->shaders
.cache
);
370 cache_name(enum iris_program_cache_id cache_id
)
372 if (cache_id
== IRIS_CACHE_BLORP
)
375 return _mesa_shader_stage_to_string(cache_id
);
379 iris_print_program_cache(struct iris_context
*ice
)
381 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
382 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
384 hash_table_foreach(ice
->shaders
.cache
, entry
) {
385 const struct keybox
*keybox
= entry
->key
;
386 struct iris_compiled_shader
*shader
= entry
->data
;
387 fprintf(stderr
, "%s:\n", cache_name(keybox
->cache_id
));
388 brw_disassemble(devinfo
, shader
->map
, 0,
389 shader
->prog_data
->program_size
, stderr
);