mesa/st: Use nir_shader_instructions_pass for st_nir_lower_builtin
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16482>
This commit is contained in:
parent
e16197c46e
commit
627d58099c
|
@ -64,12 +64,6 @@
|
|||
#include "uniforms.h"
|
||||
#include "program/prog_instruction.h"
|
||||
|
||||
typedef struct {
|
||||
nir_shader *shader;
|
||||
nir_builder builder;
|
||||
void *mem_ctx;
|
||||
} lower_builtin_state;
|
||||
|
||||
static const struct gl_builtin_uniform_element *
|
||||
get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
|
||||
{
|
||||
|
@ -97,10 +91,10 @@ get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
|
|||
}
|
||||
|
||||
static nir_variable *
|
||||
get_variable(lower_builtin_state *state, nir_deref_path *path,
|
||||
get_variable(nir_builder *b, nir_deref_path *path,
|
||||
const struct gl_builtin_uniform_element *element)
|
||||
{
|
||||
nir_shader *shader = state->shader;
|
||||
nir_shader *shader = b->shader;
|
||||
gl_state_index16 tokens[STATE_LENGTH];
|
||||
int idx = 1;
|
||||
|
||||
|
@ -163,111 +157,83 @@ get_variable(lower_builtin_state *state, nir_deref_path *path,
|
|||
}
|
||||
|
||||
static bool
|
||||
lower_builtin_block(lower_builtin_state *state, nir_block *block)
|
||||
lower_builtin_instr(nir_builder *b, nir_instr *instr, UNUSED void *_data)
|
||||
{
|
||||
nir_builder *b = &state->builder;
|
||||
bool progress = false;
|
||||
if (instr->type != nir_instr_type_intrinsic)
|
||||
return false;
|
||||
|
||||
nir_foreach_instr_safe(instr, block) {
|
||||
if (instr->type != nir_instr_type_intrinsic)
|
||||
continue;
|
||||
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
|
||||
if (intrin->intrinsic != nir_intrinsic_load_deref)
|
||||
return false;
|
||||
|
||||
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
|
||||
nir_variable *var =
|
||||
nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
|
||||
if (var->data.mode != nir_var_uniform)
|
||||
return false;
|
||||
|
||||
if (intrin->intrinsic != nir_intrinsic_load_deref)
|
||||
continue;
|
||||
/* built-in's will always start with "gl_" */
|
||||
if (strncmp(var->name, "gl_", 3) != 0)
|
||||
return false;
|
||||
|
||||
nir_variable *var =
|
||||
nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
|
||||
if (var->data.mode != nir_var_uniform)
|
||||
continue;
|
||||
const struct gl_builtin_uniform_desc *desc =
|
||||
_mesa_glsl_get_builtin_uniform_desc(var->name);
|
||||
|
||||
/* built-in's will always start with "gl_" */
|
||||
if (strncmp(var->name, "gl_", 3) != 0)
|
||||
continue;
|
||||
/* if no descriptor, it isn't something we need to handle specially: */
|
||||
if (!desc)
|
||||
return false;
|
||||
|
||||
const struct gl_builtin_uniform_desc *desc =
|
||||
_mesa_glsl_get_builtin_uniform_desc(var->name);
|
||||
nir_deref_path path;
|
||||
nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
|
||||
|
||||
/* if no descriptor, it isn't something we need to handle specially: */
|
||||
if (!desc)
|
||||
continue;
|
||||
const struct gl_builtin_uniform_element *element = get_element(desc, &path);
|
||||
|
||||
nir_deref_path path;
|
||||
nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
|
||||
|
||||
const struct gl_builtin_uniform_element *element = get_element(desc, &path);
|
||||
|
||||
/* matrix elements (array_deref) do not need special handling: */
|
||||
if (!element) {
|
||||
nir_deref_path_finish(&path);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* remove existing var from uniform list: */
|
||||
exec_node_remove(&var->node);
|
||||
/* the _self_link() ensures we can remove multiple times, rather than
|
||||
* trying to keep track of what we have already removed:
|
||||
*/
|
||||
exec_node_self_link(&var->node);
|
||||
|
||||
nir_variable *new_var = get_variable(state, &path, element);
|
||||
/* matrix elements (array_deref) do not need special handling: */
|
||||
if (!element) {
|
||||
nir_deref_path_finish(&path);
|
||||
|
||||
b->cursor = nir_before_instr(instr);
|
||||
|
||||
nir_ssa_def *def = nir_load_var(b, new_var);
|
||||
|
||||
/* swizzle the result: */
|
||||
unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0};
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
swiz[i] = GET_SWZ(element->swizzle, i);
|
||||
assert(swiz[i] <= SWIZZLE_W);
|
||||
}
|
||||
def = nir_swizzle(b, def, swiz, intrin->num_components);
|
||||
|
||||
/* and rewrite uses of original instruction: */
|
||||
assert(intrin->dest.is_ssa);
|
||||
nir_ssa_def_rewrite_uses(&intrin->dest.ssa, def);
|
||||
|
||||
/* at this point intrin should be unused. We need to remove it
|
||||
* (rather than waiting for DCE pass) to avoid dangling reference
|
||||
* to remove'd var. And we have to remove the original uniform
|
||||
* var since we don't want it to get uniform space allocated.
|
||||
*/
|
||||
nir_instr_remove(&intrin->instr);
|
||||
|
||||
progress = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return progress;
|
||||
}
|
||||
/* remove existing var from uniform list: */
|
||||
exec_node_remove(&var->node);
|
||||
/* the _self_link() ensures we can remove multiple times, rather than
|
||||
* trying to keep track of what we have already removed:
|
||||
*/
|
||||
exec_node_self_link(&var->node);
|
||||
|
||||
static void
|
||||
lower_builtin_impl(lower_builtin_state *state, nir_function_impl *impl)
|
||||
{
|
||||
nir_builder_init(&state->builder, impl);
|
||||
state->mem_ctx = ralloc_parent(impl);
|
||||
nir_variable *new_var = get_variable(b, &path, element);
|
||||
nir_deref_path_finish(&path);
|
||||
|
||||
bool progress = false;
|
||||
nir_foreach_block(block, impl) {
|
||||
progress |= lower_builtin_block(state, block);
|
||||
b->cursor = nir_before_instr(instr);
|
||||
|
||||
nir_ssa_def *def = nir_load_var(b, new_var);
|
||||
|
||||
/* swizzle the result: */
|
||||
unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0};
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
swiz[i] = GET_SWZ(element->swizzle, i);
|
||||
assert(swiz[i] <= SWIZZLE_W);
|
||||
}
|
||||
def = nir_swizzle(b, def, swiz, intrin->num_components);
|
||||
|
||||
if (progress)
|
||||
nir_remove_dead_derefs_impl(impl);
|
||||
/* and rewrite uses of original instruction: */
|
||||
assert(intrin->dest.is_ssa);
|
||||
nir_ssa_def_rewrite_uses(&intrin->dest.ssa, def);
|
||||
|
||||
nir_metadata_preserve(impl, nir_metadata_block_index |
|
||||
nir_metadata_dominance);
|
||||
/* at this point intrin should be unused. We need to remove it
|
||||
* (rather than waiting for DCE pass) to avoid dangling reference
|
||||
* to remove'd var. And we have to remove the original uniform
|
||||
* var since we don't want it to get uniform space allocated.
|
||||
*/
|
||||
nir_instr_remove(&intrin->instr);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
st_nir_lower_builtin(nir_shader *shader)
|
||||
{
|
||||
lower_builtin_state state;
|
||||
state.shader = shader;
|
||||
nir_foreach_function(function, shader) {
|
||||
if (function->impl)
|
||||
lower_builtin_impl(&state, function->impl);
|
||||
}
|
||||
if (nir_shader_instructions_pass(shader, lower_builtin_instr,
|
||||
nir_metadata_block_index |
|
||||
nir_metadata_dominance, NULL))
|
||||
nir_remove_dead_derefs(shader);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue