iris: rework program cache interface

This exposes iris_upload_shader() without having to bind it, which will
be useful for precompiles.  It also lets us examine the old programs and
flag dirty bits at a higher level, rather than cramming all that
knowledge into the cache layer.
This commit is contained in:
Kenneth Graunke 2018-11-21 18:15:28 -08:00
parent 701a6b6006
commit ed4ffb9715
3 changed files with 131 additions and 154 deletions

View file

@ -581,11 +581,10 @@ uint32_t iris_get_scratch_space(struct iris_context *ice,
void iris_init_program_cache(struct iris_context *ice); void iris_init_program_cache(struct iris_context *ice);
void iris_destroy_program_cache(struct iris_context *ice); void iris_destroy_program_cache(struct iris_context *ice);
void iris_print_program_cache(struct iris_context *ice); void iris_print_program_cache(struct iris_context *ice);
bool iris_bind_cached_shader(struct iris_context *ice, struct iris_compiled_shader *iris_find_cached_shader(struct iris_context *ice,
enum iris_program_cache_id cache_id, enum iris_program_cache_id,
const void *key); uint32_t key_size,
void iris_unbind_shader(struct iris_context *ice, const void *key);
enum iris_program_cache_id cache_id);
struct iris_compiled_shader *iris_upload_shader(struct iris_context *ice, struct iris_compiled_shader *iris_upload_shader(struct iris_context *ice,
enum iris_program_cache_id, enum iris_program_cache_id,
uint32_t key_size, uint32_t key_size,
@ -595,14 +594,6 @@ struct iris_compiled_shader *iris_upload_shader(struct iris_context *ice,
uint32_t *streamout, uint32_t *streamout,
enum brw_param_builtin *sysv, enum brw_param_builtin *sysv,
unsigned num_system_values); unsigned num_system_values);
void iris_upload_and_bind_shader(struct iris_context *ice,
enum iris_program_cache_id cache_id,
const void *key,
const void *assembly,
struct brw_stage_prog_data *prog_data,
uint32_t *streamout,
enum brw_param_builtin *system_values,
unsigned num_system_values);
const void *iris_find_previous_compile(const struct iris_context *ice, const void *iris_find_previous_compile(const struct iris_context *ice,
enum iris_program_cache_id cache_id, enum iris_program_cache_id cache_id,
unsigned program_string_id); unsigned program_string_id);

View file

@ -605,7 +605,7 @@ iris_setup_uniforms(const struct brw_compiler *compiler,
/** /**
* Compile a vertex shader, and upload the assembly. * Compile a vertex shader, and upload the assembly.
*/ */
static bool static struct iris_compiled_shader *
iris_compile_vs(struct iris_context *ice, iris_compile_vs(struct iris_context *ice,
struct iris_uncompiled_shader *ish, struct iris_uncompiled_shader *ish,
const struct brw_vs_prog_key *key) const struct brw_vs_prog_key *key)
@ -664,11 +664,12 @@ iris_compile_vs(struct iris_context *ice,
ice->vtbl.create_so_decl_list(&ish->stream_output, ice->vtbl.create_so_decl_list(&ish->stream_output,
&vue_prog_data->vue_map); &vue_prog_data->vue_map);
iris_upload_and_bind_shader(ice, IRIS_CACHE_VS, key, program, prog_data, struct iris_compiled_shader *shader =
so_decls, system_values, num_system_values); iris_upload_shader(ice, IRIS_CACHE_VS, sizeof(*key), key, program,
prog_data, so_decls, system_values, num_system_values);
ralloc_free(mem_ctx); ralloc_free(mem_ctx);
return true; return shader;
} }
/** /**
@ -685,10 +686,20 @@ iris_update_compiled_vs(struct iris_context *ice)
struct brw_vs_prog_key key = { .program_string_id = ish->program_id }; struct brw_vs_prog_key key = { .program_string_id = ish->program_id };
ice->vtbl.populate_vs_key(ice, &ish->nir->info, &key); ice->vtbl.populate_vs_key(ice, &ish->nir->info, &key);
if (iris_bind_cached_shader(ice, IRIS_CACHE_VS, &key)) struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_VS];
return; struct iris_compiled_shader *shader =
iris_find_cached_shader(ice, IRIS_CACHE_VS, sizeof(key), &key);
UNUSED bool success = iris_compile_vs(ice, ish, &key); if (!shader)
shader = iris_compile_vs(ice, ish, &key);
if (old != shader) {
ice->shaders.prog[IRIS_CACHE_VS] = shader;
ice->state.dirty |= IRIS_DIRTY_VS |
IRIS_DIRTY_BINDINGS_VS |
IRIS_DIRTY_CONSTANTS_VS |
IRIS_DIRTY_VF_SGVS;
}
} }
/** /**
@ -756,7 +767,7 @@ get_unified_tess_slots(const struct iris_context *ice,
/** /**
* Compile a tessellation control shader, and upload the assembly. * Compile a tessellation control shader, and upload the assembly.
*/ */
static bool static struct iris_compiled_shader *
iris_compile_tcs(struct iris_context *ice, iris_compile_tcs(struct iris_context *ice,
struct iris_uncompiled_shader *ish, struct iris_uncompiled_shader *ish,
const struct brw_tcs_prog_key *key) const struct brw_tcs_prog_key *key)
@ -802,11 +813,12 @@ iris_compile_tcs(struct iris_context *ice,
return false; return false;
} }
iris_upload_and_bind_shader(ice, IRIS_CACHE_TCS, key, program, prog_data, struct iris_compiled_shader *shader =
NULL, system_values, num_system_values); iris_upload_shader(ice, IRIS_CACHE_TCS, sizeof(*key), key, program,
prog_data, NULL, system_values, num_system_values);
ralloc_free(mem_ctx); ralloc_free(mem_ctx);
return true; return shader;
} }
/** /**
@ -831,16 +843,25 @@ iris_update_compiled_tcs(struct iris_context *ice)
&key.patch_outputs_written); &key.patch_outputs_written);
ice->vtbl.populate_tcs_key(ice, &key); ice->vtbl.populate_tcs_key(ice, &key);
if (iris_bind_cached_shader(ice, IRIS_CACHE_TCS, &key)) struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_TCS];
return; struct iris_compiled_shader *shader =
iris_find_cached_shader(ice, IRIS_CACHE_TCS, sizeof(key), &key);
UNUSED bool success = iris_compile_tcs(ice, tcs, &key); if (!shader)
shader = iris_compile_tcs(ice, tcs, &key);
if (old != shader) {
ice->shaders.prog[IRIS_CACHE_TCS] = shader;
ice->state.dirty |= IRIS_DIRTY_TCS |
IRIS_DIRTY_BINDINGS_TCS |
IRIS_DIRTY_CONSTANTS_TCS;
}
} }
/** /**
* Compile a tessellation evaluation shader, and upload the assembly. * Compile a tessellation evaluation shader, and upload the assembly.
*/ */
static bool static struct iris_compiled_shader *
iris_compile_tes(struct iris_context *ice, iris_compile_tes(struct iris_context *ice,
struct iris_uncompiled_shader *ish, struct iris_uncompiled_shader *ish,
const struct brw_tes_prog_key *key) const struct brw_tes_prog_key *key)
@ -882,11 +903,13 @@ iris_compile_tes(struct iris_context *ice,
ice->vtbl.create_so_decl_list(&ish->stream_output, ice->vtbl.create_so_decl_list(&ish->stream_output,
&vue_prog_data->vue_map); &vue_prog_data->vue_map);
iris_upload_and_bind_shader(ice, IRIS_CACHE_TES, key, program, prog_data,
so_decls, system_values, num_system_values); struct iris_compiled_shader *shader =
iris_upload_shader(ice, IRIS_CACHE_TES, sizeof(*key), key, program,
prog_data, so_decls, system_values, num_system_values);
ralloc_free(mem_ctx); ralloc_free(mem_ctx);
return true; return shader;
} }
/** /**
@ -904,16 +927,25 @@ iris_update_compiled_tes(struct iris_context *ice)
get_unified_tess_slots(ice, &key.inputs_read, &key.patch_inputs_read); get_unified_tess_slots(ice, &key.inputs_read, &key.patch_inputs_read);
ice->vtbl.populate_tes_key(ice, &key); ice->vtbl.populate_tes_key(ice, &key);
if (iris_bind_cached_shader(ice, IRIS_CACHE_TES, &key)) struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_TES];
return; struct iris_compiled_shader *shader =
iris_find_cached_shader(ice, IRIS_CACHE_TES, sizeof(key), &key);
UNUSED bool success = iris_compile_tes(ice, ish, &key); if (!shader)
shader = iris_compile_tes(ice, ish, &key);
if (old != shader) {
ice->shaders.prog[IRIS_CACHE_TES] = shader;
ice->state.dirty |= IRIS_DIRTY_TES |
IRIS_DIRTY_BINDINGS_TES |
IRIS_DIRTY_CONSTANTS_TES;
}
} }
/** /**
* Compile a geometry shader, and upload the assembly. * Compile a geometry shader, and upload the assembly.
*/ */
static bool static struct iris_compiled_shader *
iris_compile_gs(struct iris_context *ice, iris_compile_gs(struct iris_context *ice,
struct iris_uncompiled_shader *ish, struct iris_uncompiled_shader *ish,
const struct brw_gs_prog_key *key) const struct brw_gs_prog_key *key)
@ -955,11 +987,12 @@ iris_compile_gs(struct iris_context *ice,
ice->vtbl.create_so_decl_list(&ish->stream_output, ice->vtbl.create_so_decl_list(&ish->stream_output,
&vue_prog_data->vue_map); &vue_prog_data->vue_map);
iris_upload_and_bind_shader(ice, IRIS_CACHE_GS, key, program, prog_data, struct iris_compiled_shader *shader =
so_decls, system_values, num_system_values); iris_upload_shader(ice, IRIS_CACHE_GS, sizeof(*key), key, program,
prog_data, so_decls, system_values, num_system_values);
ralloc_free(mem_ctx); ralloc_free(mem_ctx);
return true; return shader;
} }
/** /**
@ -972,25 +1005,32 @@ iris_update_compiled_gs(struct iris_context *ice)
{ {
struct iris_uncompiled_shader *ish = struct iris_uncompiled_shader *ish =
ice->shaders.uncompiled[MESA_SHADER_GEOMETRY]; ice->shaders.uncompiled[MESA_SHADER_GEOMETRY];
struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_GS];
struct iris_compiled_shader *shader = NULL;
if (!ish) { if (ish) {
iris_unbind_shader(ice, IRIS_CACHE_GS); struct brw_gs_prog_key key = { .program_string_id = ish->program_id };
return; ice->vtbl.populate_gs_key(ice, &key);
shader =
iris_find_cached_shader(ice, IRIS_CACHE_GS, sizeof(key), &key);
if (!shader)
shader = iris_compile_gs(ice, ish, &key);
} }
struct brw_gs_prog_key key = { .program_string_id = ish->program_id }; if (old != shader) {
ice->vtbl.populate_gs_key(ice, &key); ice->shaders.prog[IRIS_CACHE_GS] = shader;
ice->state.dirty |= IRIS_DIRTY_GS |
if (iris_bind_cached_shader(ice, IRIS_CACHE_GS, &key)) IRIS_DIRTY_BINDINGS_GS |
return; IRIS_DIRTY_CONSTANTS_GS;
}
UNUSED bool success = iris_compile_gs(ice, ish, &key);
} }
/** /**
* Compile a fragment (pixel) shader, and upload the assembly. * Compile a fragment (pixel) shader, and upload the assembly.
*/ */
static bool static struct iris_compiled_shader *
iris_compile_fs(struct iris_context *ice, iris_compile_fs(struct iris_context *ice,
struct iris_uncompiled_shader *ish, struct iris_uncompiled_shader *ish,
const struct brw_wm_prog_key *key, const struct brw_wm_prog_key *key,
@ -1026,13 +1066,12 @@ iris_compile_fs(struct iris_context *ice,
return false; return false;
} }
//brw_alloc_stage_scratch(brw, &brw->wm.base, prog_data.base.total_scratch); struct iris_compiled_shader *shader =
iris_upload_shader(ice, IRIS_CACHE_FS, sizeof(*key), key, program,
iris_upload_and_bind_shader(ice, IRIS_CACHE_FS, key, program, prog_data, prog_data, NULL, system_values, num_system_values);
NULL, system_values, num_system_values);
ralloc_free(mem_ctx); ralloc_free(mem_ctx);
return true; return shader;
} }
/** /**
@ -1051,11 +1090,24 @@ iris_update_compiled_fs(struct iris_context *ice)
if (ish->nos & IRIS_NOS_LAST_VUE_MAP) if (ish->nos & IRIS_NOS_LAST_VUE_MAP)
key.input_slots_valid = ice->shaders.last_vue_map->slots_valid; key.input_slots_valid = ice->shaders.last_vue_map->slots_valid;
if (iris_bind_cached_shader(ice, IRIS_CACHE_FS, &key)) struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_FS];
return; struct iris_compiled_shader *shader =
iris_find_cached_shader(ice, IRIS_CACHE_FS, sizeof(key), &key);
UNUSED bool success = if (!shader)
iris_compile_fs(ice, ish, &key, ice->shaders.last_vue_map); shader = iris_compile_fs(ice, ish, &key, ice->shaders.last_vue_map);
if (old != shader) {
// XXX: only need to flag CLIP if barycentric has NONPERSPECTIVE
// toggles. might be able to avoid flagging SBE too.
ice->shaders.prog[IRIS_CACHE_FS] = shader;
ice->state.dirty |= IRIS_DIRTY_FS |
IRIS_DIRTY_BINDINGS_FS |
IRIS_DIRTY_CONSTANTS_FS |
IRIS_DIRTY_WM |
IRIS_DIRTY_CLIP |
IRIS_DIRTY_SBE;
}
} }
/** /**
@ -1123,6 +1175,9 @@ get_vue_prog_data(struct iris_context *ice, gl_shader_stage stage)
return (void *) ice->shaders.prog[stage]->prog_data; return (void *) ice->shaders.prog[stage]->prog_data;
} }
// XXX: iris_compiled_shaders are space-leaking :(
// XXX: do remember to unbind them if deleting them.
/** /**
* Update the current shader variants for the given state. * Update the current shader variants for the given state.
* *
@ -1148,8 +1203,12 @@ iris_update_compiled_shaders(struct iris_context *ice)
iris_update_compiled_tcs(ice); iris_update_compiled_tcs(ice);
iris_update_compiled_tes(ice); iris_update_compiled_tes(ice);
} else { } else {
iris_unbind_shader(ice, IRIS_CACHE_TCS); ice->shaders.prog[IRIS_CACHE_TCS] = NULL;
iris_unbind_shader(ice, IRIS_CACHE_TES); ice->shaders.prog[IRIS_CACHE_TES] = NULL;
ice->state.dirty |=
IRIS_DIRTY_TCS | IRIS_DIRTY_TES |
IRIS_DIRTY_BINDINGS_TCS | IRIS_DIRTY_BINDINGS_TES |
IRIS_DIRTY_CONSTANTS_TCS | IRIS_DIRTY_CONSTANTS_TES;
} }
} }
@ -1183,7 +1242,7 @@ iris_update_compiled_shaders(struct iris_context *ice)
} }
} }
static bool static struct iris_compiled_shader *
iris_compile_cs(struct iris_context *ice, iris_compile_cs(struct iris_context *ice,
struct iris_uncompiled_shader *ish, struct iris_uncompiled_shader *ish,
const struct brw_cs_prog_key *key) const struct brw_cs_prog_key *key)
@ -1220,11 +1279,12 @@ iris_compile_cs(struct iris_context *ice,
return false; return false;
} }
iris_upload_and_bind_shader(ice, IRIS_CACHE_CS, key, program, prog_data, struct iris_compiled_shader *shader =
NULL, system_values, num_system_values); iris_upload_shader(ice, IRIS_CACHE_CS, sizeof(*key), key, program,
prog_data, NULL, system_values, num_system_values);
ralloc_free(mem_ctx); ralloc_free(mem_ctx);
return true; return shader;
} }
void void
@ -1236,10 +1296,19 @@ iris_update_compiled_compute_shader(struct iris_context *ice)
struct brw_cs_prog_key key = { .program_string_id = ish->program_id }; struct brw_cs_prog_key key = { .program_string_id = ish->program_id };
ice->vtbl.populate_cs_key(ice, &key); ice->vtbl.populate_cs_key(ice, &key);
if (iris_bind_cached_shader(ice, IRIS_CACHE_CS, &key)) struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_CS];
return; struct iris_compiled_shader *shader =
iris_find_cached_shader(ice, IRIS_CACHE_CS, sizeof(key), &key);
UNUSED bool success = iris_compile_cs(ice, ish, &key); if (!shader)
shader = iris_compile_cs(ice, ish, &key);
if (old != shader) {
ice->shaders.prog[IRIS_CACHE_CS] = shader;
ice->state.dirty |= IRIS_DIRTY_CS |
IRIS_DIRTY_BINDINGS_CS |
IRIS_DIRTY_CONSTANTS_CS;
}
} }
void void

View file

@ -101,26 +101,6 @@ keybox_equals(const void *void_a, const void *void_b)
return memcmp(a->data, b->data, a->size) == 0; return memcmp(a->data, b->data, a->size) == 0;
} }
static uint64_t
dirty_flag_for_cache(enum iris_program_cache_id cache_id)
{
assert(cache_id <= MESA_SHADER_STAGES);
uint64_t flags = (IRIS_DIRTY_VS |
IRIS_DIRTY_BINDINGS_VS |
IRIS_DIRTY_CONSTANTS_VS) << cache_id;
// XXX: ugly...
// XXX: move this flagging out to a higher level, allow comparison of
// XXX: new and old programs to decide what bits to twiddle
// XXX: CLIP: toggle if barycentric modes has any NONPERSPECTIVE or not
if (cache_id == IRIS_CACHE_FS)
flags |= IRIS_DIRTY_WM | IRIS_DIRTY_CLIP | IRIS_DIRTY_SBE;
if (cache_id == IRIS_CACHE_VS)
flags |= IRIS_DIRTY_VF_SGVS;
return flags;
}
static unsigned static unsigned
get_program_string_id(enum iris_program_cache_id cache_id, const void *key) get_program_string_id(enum iris_program_cache_id cache_id, const void *key)
{ {
@ -142,11 +122,11 @@ get_program_string_id(enum iris_program_cache_id cache_id, const void *key)
} }
} }
static struct iris_compiled_shader * struct iris_compiled_shader *
iris_find_cached_shader(struct iris_context *ice, iris_find_cached_shader(struct iris_context *ice,
enum iris_program_cache_id cache_id, enum iris_program_cache_id cache_id,
const void *key, uint32_t key_size,
uint32_t key_size) const void *key)
{ {
struct keybox *keybox = struct keybox *keybox =
make_keybox(ice->shaders.cache, cache_id, key, key_size); make_keybox(ice->shaders.cache, cache_id, key, key_size);
@ -158,43 +138,6 @@ iris_find_cached_shader(struct iris_context *ice,
return entry ? entry->data : NULL; return entry ? entry->data : NULL;
} }
/**
* Looks for a program in the cache and binds it.
*
* If no program was found, returns false and leaves the binding alone.
*/
bool
iris_bind_cached_shader(struct iris_context *ice,
enum iris_program_cache_id cache_id,
const void *key)
{
unsigned key_size = key_size_for_cache(cache_id);
struct iris_compiled_shader *shader =
iris_find_cached_shader(ice, cache_id, key, key_size);
if (!shader)
return false;
// XXX: why memcmp?
if (!ice->shaders.prog[cache_id] ||
memcmp(shader, ice->shaders.prog[cache_id], sizeof(*shader)) != 0) {
ice->shaders.prog[cache_id] = shader;
ice->state.dirty |= dirty_flag_for_cache(cache_id);
}
return true;
}
void
iris_unbind_shader(struct iris_context *ice,
enum iris_program_cache_id cache_id)
{
if (ice->shaders.prog[cache_id]) {
ice->shaders.prog[cache_id] = NULL;
ice->state.dirty |= dirty_flag_for_cache(cache_id);
}
}
const void * const void *
iris_find_previous_compile(const struct iris_context *ice, iris_find_previous_compile(const struct iris_context *ice,
enum iris_program_cache_id cache_id, enum iris_program_cache_id cache_id,
@ -288,32 +231,6 @@ iris_upload_shader(struct iris_context *ice,
return shader; return shader;
} }
/**
* Upload a new shader to the program cache, and bind it for use.
*
* \param prog_data must be ralloc'd and will be stolen.
*/
void
iris_upload_and_bind_shader(struct iris_context *ice,
enum iris_program_cache_id cache_id,
const void *key,
const void *assembly,
struct brw_stage_prog_data *prog_data,
uint32_t *streamout,
enum brw_param_builtin *system_values,
unsigned num_system_values)
{
assert(cache_id != IRIS_CACHE_BLORP);
struct iris_compiled_shader *shader =
iris_upload_shader(ice, cache_id, key_size_for_cache(cache_id), key,
assembly, prog_data, streamout, system_values,
num_system_values);
ice->shaders.prog[cache_id] = shader;
ice->state.dirty |= dirty_flag_for_cache(cache_id);
}
bool bool
iris_blorp_lookup_shader(struct blorp_batch *blorp_batch, iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
const void *key, uint32_t key_size, const void *key, uint32_t key_size,
@ -323,7 +240,7 @@ iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
struct iris_context *ice = blorp->driver_ctx; struct iris_context *ice = blorp->driver_ctx;
struct iris_batch *batch = blorp_batch->driver_batch; struct iris_batch *batch = blorp_batch->driver_batch;
struct iris_compiled_shader *shader = struct iris_compiled_shader *shader =
iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key, key_size); iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key_size, key);
if (!shader) if (!shader)
return false; return false;