From b26a9efc5a661a2cde1f9fa7e67913658c786112 Mon Sep 17 00:00:00 2001 From: Emma Anholt Date: Tue, 16 May 2023 13:37:53 -0700 Subject: [PATCH] nir,mesa: Add helpers for creating uniform state variables. It's one of the weirder parts of our shader interface's interactions with the GL API, so let's try to make it a little cleaner. Reviewed-by: Alyssa Rosenzweig Reviewed-by: Erik Faye-Lund Part-of: --- src/compiler/nir/nir.c | 15 ++++++++++ src/compiler/nir/nir.h | 7 +++++ src/compiler/nir/nir_lower_alpha_test.c | 12 ++------ src/compiler/nir/nir_lower_atomics_to_ssbo.c | 7 +---- src/compiler/nir/nir_lower_clip.c | 13 ++------- src/compiler/nir/nir_lower_drawpixels.c | 29 +++++-------------- src/compiler/nir/nir_lower_patch_vertices.c | 7 ++--- src/compiler/nir/nir_lower_pntc_ytransform.c | 12 +++----- src/compiler/nir/nir_lower_point_size_mov.c | 10 ++----- src/compiler/nir/nir_lower_wpos_ytransform.c | 12 +++----- .../drivers/d3d12/d3d12_lower_point_sprite.c | 11 ++----- src/gallium/drivers/d3d12/d3d12_nir_passes.c | 12 ++------ src/mesa/main/ffvertex_prog.c | 16 ++-------- src/mesa/state_tracker/st_nir.h | 6 ++++ src/mesa/state_tracker/st_nir_lower_builtin.c | 25 ++++++++-------- 15 files changed, 75 insertions(+), 119 deletions(-) diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c index 30174ff965b..510bc8ab3a4 100644 --- a/src/compiler/nir/nir.c +++ b/src/compiler/nir/nir.c @@ -323,6 +323,21 @@ nir_local_variable_create(nir_function_impl *impl, return var; } +nir_variable * +nir_state_variable_create(nir_shader *shader, + const struct glsl_type *type, + const char *name, + const gl_state_index16 tokens[STATE_LENGTH]) +{ + nir_variable *var = nir_variable_create(shader, nir_var_uniform, type, name); + var->num_state_slots = 1; + var->state_slots = rzalloc_array(var, nir_state_slot, 1); + memcpy(var->state_slots[0].tokens, tokens, + sizeof(var->state_slots[0].tokens)); + shader->num_uniforms++; + return var; +} + nir_variable * nir_create_variable_with_location(nir_shader *shader, nir_variable_mode mode, int location, const struct glsl_type *type) diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 207a0a59ebe..f05e1763bee 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -4056,6 +4056,13 @@ nir_variable *nir_local_variable_create(nir_function_impl *impl, const struct glsl_type *type, const char *name); +/** Creates a uniform builtin state variable. */ +nir_variable * +nir_state_variable_create(nir_shader *shader, + const struct glsl_type *type, + const char *name, + const gl_state_index16 tokens[STATE_LENGTH]); + /* Gets the variable for the given mode and location, creating it (with the given * type) if necessary. */ diff --git a/src/compiler/nir/nir_lower_alpha_test.c b/src/compiler/nir/nir_lower_alpha_test.c index 89b6c7366a8..750810f15af 100644 --- a/src/compiler/nir/nir_lower_alpha_test.c +++ b/src/compiler/nir/nir_lower_alpha_test.c @@ -95,15 +95,9 @@ nir_lower_alpha_test(nir_shader *shader, enum compare_func func, 3); } - nir_variable *var = nir_variable_create(shader, - nir_var_uniform, - glsl_float_type(), - "gl_AlphaRefMESA"); - var->num_state_slots = 1; - var->state_slots = rzalloc_array(var, nir_state_slot, 1); - memcpy(var->state_slots[0].tokens, - alpha_ref_state_tokens, - sizeof(var->state_slots[0].tokens)); + nir_variable *var = nir_state_variable_create(shader, glsl_float_type(), + "gl_AlphaRefMESA", + alpha_ref_state_tokens); nir_ssa_def *alpha_ref = nir_load_var(&b, var); nir_ssa_def *condition = diff --git a/src/compiler/nir/nir_lower_atomics_to_ssbo.c b/src/compiler/nir/nir_lower_atomics_to_ssbo.c index 7d709fcc66f..f470c476250 100644 --- a/src/compiler/nir/nir_lower_atomics_to_ssbo.c +++ b/src/compiler/nir/nir_lower_atomics_to_ssbo.c @@ -38,13 +38,8 @@ deref_offset_var(nir_builder *b, unsigned binding, unsigned offset_align_state) gl_state_index16 tokens[STATE_LENGTH] = {offset_align_state, binding}; nir_variable *var = nir_find_state_variable(b->shader, tokens); if (!var) { - var = nir_variable_create(b->shader, nir_var_uniform, glsl_uint_type(), "offset"); - var->state_slots = rzalloc_array(var, nir_state_slot, 1); - var->state_slots[0].tokens[0] = offset_align_state; - var->state_slots[0].tokens[1] = binding; - var->num_state_slots = 1; + var = nir_state_variable_create(b->shader, glsl_uint_type(), "offset", tokens); var->data.how_declared = nir_var_hidden; - b->shader->num_uniforms++; } return nir_build_deref_var(b, var); } diff --git a/src/compiler/nir/nir_lower_clip.c b/src/compiler/nir/nir_lower_clip.c index 5632cbf81a5..755647bd410 100644 --- a/src/compiler/nir/nir_lower_clip.c +++ b/src/compiler/nir/nir_lower_clip.c @@ -227,16 +227,9 @@ get_ucp(nir_builder *b, int plane, if (clipplane_state_tokens) { char tmp[100]; snprintf(tmp, ARRAY_SIZE(tmp), "gl_ClipPlane%dMESA", plane); - nir_variable *var = nir_variable_create(b->shader, - nir_var_uniform, - glsl_vec4_type(), - tmp); - - var->num_state_slots = 1; - var->state_slots = ralloc_array(var, nir_state_slot, 1); - memcpy(var->state_slots[0].tokens, - clipplane_state_tokens[plane], - sizeof(var->state_slots[0].tokens)); + nir_variable *var = nir_state_variable_create(b->shader, + glsl_vec4_type(), + tmp, clipplane_state_tokens[plane]); return nir_load_var(b, var); } else return nir_load_user_clip_plane(b, plane); diff --git a/src/compiler/nir/nir_lower_drawpixels.c b/src/compiler/nir/nir_lower_drawpixels.c index 0198d909113..b9a7e8d54bd 100644 --- a/src/compiler/nir/nir_lower_drawpixels.c +++ b/src/compiler/nir/nir_lower_drawpixels.c @@ -47,27 +47,12 @@ get_texcoord(nir_builder *b, lower_drawpixels_state *state) return nir_load_var(b, state->texcoord); } -static nir_variable * -create_uniform(nir_shader *shader, const char *name, - const gl_state_index16 state_tokens[STATE_LENGTH]) -{ - nir_variable *var = nir_variable_create(shader, - nir_var_uniform, - glsl_vec4_type(), - name); - var->num_state_slots = 1; - var->state_slots = ralloc_array(var, nir_state_slot, 1); - memcpy(var->state_slots[0].tokens, state_tokens, - sizeof(var->state_slots[0].tokens)); - return var; -} - static nir_ssa_def * get_scale(nir_builder *b, lower_drawpixels_state *state) { if (state->scale == NULL) { - state->scale = create_uniform(state->shader, "gl_PTscale", - state->options->scale_state_tokens); + state->scale = nir_state_variable_create(state->shader, glsl_vec4_type(), "gl_PTscale", + state->options->scale_state_tokens); } return nir_load_var(b, state->scale); } @@ -76,8 +61,8 @@ static nir_ssa_def * get_bias(nir_builder *b, lower_drawpixels_state *state) { if (state->bias == NULL) { - state->bias = create_uniform(state->shader, "gl_PTbias", - state->options->bias_state_tokens); + state->bias = nir_state_variable_create(state->shader, glsl_vec4_type(), "gl_PTbias", + state->options->bias_state_tokens); } return nir_load_var(b, state->bias); } @@ -86,9 +71,9 @@ static nir_ssa_def * get_texcoord_const(nir_builder *b, lower_drawpixels_state *state) { if (state->texcoord_const == NULL) { - state->texcoord_const = create_uniform(state->shader, - "gl_MultiTexCoord0", - state->options->texcoord_state_tokens); + state->texcoord_const = nir_state_variable_create(state->shader, glsl_vec4_type(), + "gl_MultiTexCoord0", + state->options->texcoord_state_tokens); } return nir_load_var(b, state->texcoord_const); } diff --git a/src/compiler/nir/nir_lower_patch_vertices.c b/src/compiler/nir/nir_lower_patch_vertices.c index 476f1b00847..b01edd96db2 100644 --- a/src/compiler/nir/nir_lower_patch_vertices.c +++ b/src/compiler/nir/nir_lower_patch_vertices.c @@ -30,11 +30,8 @@ make_uniform(nir_shader *nir, const gl_state_index16 *tokens) * special handling in uniform setup. */ nir_variable *var = - nir_variable_create(nir, nir_var_uniform, glsl_int_type(), - "gl_PatchVerticesIn"); - var->num_state_slots = 1; - var->state_slots = ralloc_array(var, nir_state_slot, var->num_state_slots); - memcpy(var->state_slots[0].tokens, tokens, sizeof(*tokens) * STATE_LENGTH); + nir_state_variable_create(nir, glsl_int_type(), + "gl_PatchVerticesIn", tokens); return var; } diff --git a/src/compiler/nir/nir_lower_pntc_ytransform.c b/src/compiler/nir/nir_lower_pntc_ytransform.c index 18f0f326763..39cb1f3e420 100644 --- a/src/compiler/nir/nir_lower_pntc_ytransform.c +++ b/src/compiler/nir/nir_lower_pntc_ytransform.c @@ -42,15 +42,11 @@ get_pntc_transform(lower_pntc_ytransform_state *state) /* NOTE: name must be prefixed w/ "gl_" to trigger slot based * special handling in uniform setup: */ - nir_variable *var = nir_variable_create(state->shader, - nir_var_uniform, - glsl_vec4_type(), - "gl_PntcYTransform"); + nir_variable *var = nir_state_variable_create(state->shader, + glsl_vec4_type(), + "gl_PntcYTransform", + state->pntc_state_tokens); - var->num_state_slots = 1; - var->state_slots = ralloc_array(var, nir_state_slot, 1); - memcpy(var->state_slots[0].tokens, state->pntc_state_tokens, - sizeof(var->state_slots[0].tokens)); var->data.how_declared = nir_var_hidden; state->pntc_transform = var; } diff --git a/src/compiler/nir/nir_lower_point_size_mov.c b/src/compiler/nir/nir_lower_point_size_mov.c index 23d971d712a..bcd9e635099 100644 --- a/src/compiler/nir/nir_lower_point_size_mov.c +++ b/src/compiler/nir/nir_lower_point_size_mov.c @@ -42,13 +42,9 @@ lower_impl(nir_function_impl *impl, nir_builder_init(&b, impl); - in = nir_variable_create(shader, nir_var_uniform, - glsl_vec4_type(), "gl_PointSizeClampedMESA"); - in->num_state_slots = 1; - in->state_slots = ralloc_array(in, nir_state_slot, 1); - memcpy(in->state_slots[0].tokens, - pointsize_state_tokens, - sizeof(in->state_slots[0].tokens)); + in = nir_state_variable_create(shader, glsl_vec4_type(), + "gl_PointSizeClampedMESA", + pointsize_state_tokens); /* the existing output can't be removed in order to avoid breaking xfb. * drivers must check var->data.explicit_location to find the original output diff --git a/src/compiler/nir/nir_lower_wpos_ytransform.c b/src/compiler/nir/nir_lower_wpos_ytransform.c index 3e70b1d1454..27a7fbb1eb4 100644 --- a/src/compiler/nir/nir_lower_wpos_ytransform.c +++ b/src/compiler/nir/nir_lower_wpos_ytransform.c @@ -48,15 +48,11 @@ get_transform(lower_wpos_ytransform_state *state) /* NOTE: name must be prefixed w/ "gl_" to trigger slot based * special handling in uniform setup: */ - nir_variable *var = nir_variable_create(state->shader, - nir_var_uniform, - glsl_vec4_type(), - "gl_FbWposYTransform"); + nir_variable *var = nir_state_variable_create(state->shader, + glsl_vec4_type(), + "gl_FbWposYTransform", + state->options->state_tokens); - var->num_state_slots = 1; - var->state_slots = ralloc_array(var, nir_state_slot, 1); - memcpy(var->state_slots[0].tokens, state->options->state_tokens, - sizeof(var->state_slots[0].tokens)); var->data.how_declared = nir_var_hidden; state->transform = var; } diff --git a/src/gallium/drivers/d3d12/d3d12_lower_point_sprite.c b/src/gallium/drivers/d3d12/d3d12_lower_point_sprite.c index 27a877c8bb1..83f557872d2 100644 --- a/src/gallium/drivers/d3d12/d3d12_lower_point_sprite.c +++ b/src/gallium/drivers/d3d12/d3d12_lower_point_sprite.c @@ -250,15 +250,8 @@ d3d12_lower_point_sprite(nir_shader *shader, /* Create uniform to retrieve inverse of viewport size and point size: * (1/ViewportWidth, 1/ViewportHeight, PointSize, MaxPointSize) */ - state.uniform = nir_variable_create(shader, - nir_var_uniform, - glsl_vec4_type(), - "d3d12_ViewportSizeRcp"); - state.uniform->num_state_slots = 1; - state.uniform->state_slots = ralloc_array(state.uniform, nir_state_slot, 1); - memcpy(state.uniform->state_slots[0].tokens, tokens, - sizeof(state.uniform->state_slots[0].tokens)); - shader->num_uniforms++; + state.uniform = nir_state_variable_create(shader, glsl_vec4_type(), + "d3d12_ViewportSizeRcp", tokens); /* Create new outputs for point tex coordinates */ unsigned count = 0; diff --git a/src/gallium/drivers/d3d12/d3d12_nir_passes.c b/src/gallium/drivers/d3d12/d3d12_nir_passes.c index 5eb67be4d91..3c5458a1848 100644 --- a/src/gallium/drivers/d3d12/d3d12_nir_passes.c +++ b/src/gallium/drivers/d3d12/d3d12_nir_passes.c @@ -46,17 +46,9 @@ d3d12_get_state_var(nir_builder *b, { const gl_state_index16 tokens[STATE_LENGTH] = { STATE_INTERNAL_DRIVER, var_enum }; if (*out_var == NULL) { - nir_variable *var = nir_variable_create(b->shader, - nir_var_uniform, - var_type, - var_name); - - var->num_state_slots = 1; - var->state_slots = ralloc_array(var, nir_state_slot, 1); - memcpy(var->state_slots[0].tokens, tokens, - sizeof(var->state_slots[0].tokens)); + nir_variable *var = nir_state_variable_create(b->shader, var_type, + var_name, tokens); var->data.how_declared = nir_var_hidden; - b->shader->num_uniforms++; *out_var = var; } return nir_load_var(b, *out_var); diff --git a/src/mesa/main/ffvertex_prog.c b/src/mesa/main/ffvertex_prog.c index fa385d29863..8c30811c7b2 100644 --- a/src/mesa/main/ffvertex_prog.c +++ b/src/mesa/main/ffvertex_prog.c @@ -46,6 +46,7 @@ #include "util/bitscan.h" #include "state_tracker/st_program.h" +#include "state_tracker/st_nir.h" #include "compiler/nir/nir_builder.h" #include "compiler/nir/nir_builtin_builder.h" @@ -310,20 +311,9 @@ register_state_var(struct tnl_program *p, if (var) return var; - int loc = _mesa_add_state_reference(p->state_params, tokens); + var = st_nir_state_variable_create(p->b->shader, type, tokens); + var->data.driver_location = _mesa_add_state_reference(p->state_params, tokens); - char *name = _mesa_program_state_string(tokens); - var = nir_variable_create(p->b->shader, nir_var_uniform, type, - name); - free(name); - - var->num_state_slots = 1; - var->state_slots = ralloc_array(var, nir_state_slot, 1); - var->data.driver_location = loc; - memcpy(var->state_slots[0].tokens, tokens, - sizeof(var->state_slots[0].tokens)); - - p->b->shader->num_uniforms++; return var; } diff --git a/src/mesa/state_tracker/st_nir.h b/src/mesa/state_tracker/st_nir.h index 13628ccf154..c400cc6017b 100644 --- a/src/mesa/state_tracker/st_nir.h +++ b/src/mesa/state_tracker/st_nir.h @@ -33,6 +33,7 @@ extern "C" { #endif struct nir_shader; +struct nir_variable; void st_nir_lower_builtin(struct nir_shader *shader); void st_nir_lower_tex_src_plane(struct nir_shader *shader, unsigned free_slots, @@ -81,6 +82,11 @@ st_nir_add_point_size(struct nir_shader *nir); struct pipe_shader_state * st_nir_make_clearcolor_shader(struct st_context *st); +struct nir_variable * +st_nir_state_variable_create(struct nir_shader *shader, + const struct glsl_type *type, + const gl_state_index16 state[STATE_LENGTH]); + #ifdef __cplusplus } #endif diff --git a/src/mesa/state_tracker/st_nir_lower_builtin.c b/src/mesa/state_tracker/st_nir_lower_builtin.c index b9baf54d16a..9b3dc5c5d0e 100644 --- a/src/mesa/state_tracker/st_nir_lower_builtin.c +++ b/src/mesa/state_tracker/st_nir_lower_builtin.c @@ -64,6 +64,18 @@ #include "uniforms.h" #include "program/prog_instruction.h" +/** Wrapper around nir_state_variable_create to pick the name automatically. */ +nir_variable * +st_nir_state_variable_create(nir_shader *shader, + const struct glsl_type *type, + const gl_state_index16 tokens[STATE_LENGTH]) +{ + char *name = _mesa_program_state_string(tokens); + nir_variable *var = nir_state_variable_create(shader, type, name, tokens); + free(name); + return var; +} + static const struct gl_builtin_uniform_element * get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path) { @@ -137,19 +149,8 @@ get_variable(nir_builder *b, nir_deref_path *path, if (var) return var; - char *name = _mesa_program_state_string(tokens); - /* variable doesn't exist yet, so create it: */ - var = nir_variable_create(shader, nir_var_uniform, glsl_vec4_type(), name); - - var->num_state_slots = 1; - var->state_slots = rzalloc_array(var, nir_state_slot, 1); - memcpy(var->state_slots[0].tokens, tokens, - sizeof(var->state_slots[0].tokens)); - - free(name); - - return var; + return st_nir_state_variable_create(shader, glsl_vec4_type(), tokens); } static bool