From 0f25bb8283b7f1354549d4e74d7189ceb719bdbe Mon Sep 17 00:00:00 2001 From: Emma Anholt Date: Tue, 2 May 2023 15:40:51 -0700 Subject: nir: Add helpers for lazy var creation. This should make writing some lowering/meta code easier. It also keeps the num_inputs/outputs updated, when sometimes passes forgot to do so (for example, nir_lower_input_attachments updated for one of the two vars it creates). The names of the variables change in many cases, but it's probably nicer to see "VERT_ATTRIB_POS" than "in_0" or whatever. I've only converted mesa core (compiler and GL), not all the driver meta code. Reviewed-by: Alyssa Rosenzweig Reviewed-by: Erik Faye-Lund Part-of: --- src/compiler/nir/nir.c | 72 ++++++++++++++++++++++ src/compiler/nir/nir.h | 13 ++++ src/compiler/nir/nir_lower_bitmap.c | 21 +------ src/compiler/nir/nir_lower_drawpixels.c | 22 +------ src/compiler/nir/nir_lower_input_attachments.c | 24 ++------ src/compiler/nir/nir_lower_passthrough_edgeflags.c | 12 ++-- src/compiler/nir/nir_lower_point_size_mov.c | 5 +- src/compiler/nir/nir_lower_texcoord_replace.c | 14 +---- src/compiler/nir/nir_lower_two_sided_color.c | 39 +++--------- src/compiler/nir/nir_passthrough_tcs.c | 41 ++++-------- src/gallium/auxiliary/nir/nir_draw_helpers.c | 11 +--- src/gallium/auxiliary/nir/nir_to_tgsi.c | 15 +---- src/mesa/main/ffvertex_prog.c | 34 ++-------- src/mesa/program/prog_to_nir.c | 21 ++----- src/mesa/state_tracker/st_atifs_to_nir.c | 15 ++--- src/mesa/state_tracker/st_cb_drawpixels.c | 40 +++++------- src/mesa/state_tracker/st_glsl_to_nir.cpp | 8 +-- src/mesa/state_tracker/st_nir_builtins.c | 24 +++----- src/mesa/state_tracker/st_pbo.c | 47 ++++++-------- 19 files changed, 192 insertions(+), 286 deletions(-) diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c index 7ed93e1f409..1352d4cd3b4 100644 --- a/src/compiler/nir/nir.c +++ b/src/compiler/nir/nir.c @@ -323,6 +323,78 @@ nir_local_variable_create(nir_function_impl *impl, return var; } +nir_variable * +nir_create_variable_with_location(nir_shader *shader, nir_variable_mode mode, int location, + const struct glsl_type *type) +{ + /* Only supporting non-array, or arrayed-io types, because otherwise we don't + * know how much to increment num_inputs/outputs + */ + assert(glsl_get_length(type) <= 1); + + const char *name; + switch (mode) { + case nir_var_shader_in: + if (shader->info.stage == MESA_SHADER_VERTEX) + name = gl_vert_attrib_name(location); + else + name = gl_varying_slot_name_for_stage(location, shader->info.stage); + break; + + case nir_var_shader_out: + if (shader->info.stage == MESA_SHADER_FRAGMENT) + name = gl_frag_result_name(location); + else + name = gl_varying_slot_name_for_stage(location, shader->info.stage); + break; + + case nir_var_system_value: + name = gl_system_value_name(location); + break; + + default: + unreachable("Unsupported variable mode"); + } + + nir_variable *var = nir_variable_create(shader, mode, type, name); + var->data.location = location; + + switch (mode) { + case nir_var_shader_in: + var->data.driver_location = shader->num_inputs++; + break; + + case nir_var_shader_out: + var->data.driver_location = shader->num_outputs++; + break; + + case nir_var_system_value: + break; + + default: + unreachable("Unsupported variable mode"); + } + + return var; +} + +nir_variable * +nir_get_variable_with_location(nir_shader *shader, nir_variable_mode mode, int location, + const struct glsl_type *type) +{ + nir_variable *var = nir_find_variable_with_location(shader, mode, location); + if (var) { + /* If this shader has location_fracs, this builder function is not suitable. */ + assert(var->data.location_frac == 0); + + /* The variable for the slot should match what we expected. */ + assert(type == var->type); + return var; + } + + return nir_create_variable_with_location(shader, mode, location, type); +} + nir_variable * nir_find_variable_with_location(nir_shader *shader, nir_variable_mode mode, diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 05efa36aec1..1e35b6ae2fc 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -4044,6 +4044,19 @@ nir_variable *nir_local_variable_create(nir_function_impl *impl, const struct glsl_type *type, const char *name); +/* Gets the variable for the given mode and location, creating it (with the given + * type) if necessary. + */ +nir_variable * +nir_get_variable_with_location(nir_shader *shader, nir_variable_mode mode, int location, + const struct glsl_type *type); + +/* Creates a variable for the given mode and location. + */ +nir_variable * +nir_create_variable_with_location(nir_shader *shader, nir_variable_mode mode, int location, + const struct glsl_type *type); + nir_variable *nir_find_variable_with_location(nir_shader *shader, nir_variable_mode mode, unsigned location); diff --git a/src/compiler/nir/nir_lower_bitmap.c b/src/compiler/nir/nir_lower_bitmap.c index 713048714cb..51e3f98f47b 100644 --- a/src/compiler/nir/nir_lower_bitmap.c +++ b/src/compiler/nir/nir_lower_bitmap.c @@ -52,24 +52,6 @@ * Run before nir_lower_io. */ -static nir_variable * -get_texcoord(nir_shader *shader) -{ - nir_variable *texcoord = - nir_find_variable_with_location(shader, nir_var_shader_in, - VARYING_SLOT_TEX0); - /* otherwise create it: */ - if (texcoord == NULL) { - texcoord = nir_variable_create(shader, - nir_var_shader_in, - glsl_vec4_type(), - "gl_TexCoord"); - texcoord->data.location = VARYING_SLOT_TEX0; - } - - return texcoord; -} - static void lower_bitmap(nir_shader *shader, nir_builder *b, const nir_lower_bitmap_options *options) @@ -78,7 +60,8 @@ lower_bitmap(nir_shader *shader, nir_builder *b, nir_tex_instr *tex; nir_ssa_def *cond; - texcoord = nir_load_var(b, get_texcoord(shader)); + texcoord = nir_load_var(b, nir_get_variable_with_location(shader, nir_var_shader_in, + VARYING_SLOT_TEX0, glsl_vec4_type())); const struct glsl_type *sampler2D = glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT); diff --git a/src/compiler/nir/nir_lower_drawpixels.c b/src/compiler/nir/nir_lower_drawpixels.c index 59120ee92cc..ac5490cc9c3 100644 --- a/src/compiler/nir/nir_lower_drawpixels.c +++ b/src/compiler/nir/nir_lower_drawpixels.c @@ -41,26 +41,8 @@ static nir_ssa_def * get_texcoord(nir_builder *b, lower_drawpixels_state *state) { if (state->texcoord == NULL) { - nir_variable *texcoord = NULL; - - /* find gl_TexCoord, if it exists: */ - nir_foreach_shader_in_variable(var, state->shader) { - if (var->data.location == VARYING_SLOT_TEX0) { - texcoord = var; - break; - } - } - - /* otherwise create it: */ - if (texcoord == NULL) { - texcoord = nir_variable_create(state->shader, - nir_var_shader_in, - glsl_vec4_type(), - "gl_TexCoord"); - texcoord->data.location = VARYING_SLOT_TEX0; - } - - state->texcoord = texcoord; + state->texcoord = nir_get_variable_with_location(state->shader, nir_var_shader_in, + VARYING_SLOT_TEX0, glsl_vec4_type()); } return nir_load_var(b, state->texcoord); } diff --git a/src/compiler/nir/nir_lower_input_attachments.c b/src/compiler/nir/nir_lower_input_attachments.c index 40a0ecf885c..869b1d99345 100644 --- a/src/compiler/nir/nir_lower_input_attachments.c +++ b/src/compiler/nir/nir_lower_input_attachments.c @@ -50,14 +50,9 @@ load_frag_coord(nir_builder *b, nir_deref_instr *deref, return frag_coord; } - nir_variable *pos = - nir_find_variable_with_location(b->shader, nir_var_shader_in, - VARYING_SLOT_POS); - if (pos == NULL) { - pos = nir_variable_create(b->shader, nir_var_shader_in, - glsl_vec4_type(), NULL); - pos->data.location = VARYING_SLOT_POS; - } + nir_variable *pos = nir_get_variable_with_location(b->shader, nir_var_shader_in, + VARYING_SLOT_POS, glsl_vec4_type()); + /** * From Vulkan spec: * "The OriginLowerLeft execution mode must not be used; fragment entry @@ -82,16 +77,9 @@ load_layer_id(nir_builder *b, const nir_input_attachment_options *options) gl_varying_slot slot = options->use_view_id_for_layer ? VARYING_SLOT_VIEW_INDEX : VARYING_SLOT_LAYER; - nir_variable *layer_id = - nir_find_variable_with_location(b->shader, nir_var_shader_in, slot); - - if (layer_id == NULL) { - layer_id = nir_variable_create(b->shader, nir_var_shader_in, - glsl_int_type(), NULL); - layer_id->data.location = slot; - layer_id->data.interpolation = INTERP_MODE_FLAT; - layer_id->data.driver_location = b->shader->num_inputs++; - } + nir_variable *layer_id = nir_get_variable_with_location(b->shader, nir_var_shader_in, + slot, glsl_int_type()); + layer_id->data.interpolation = INTERP_MODE_FLAT; return nir_load_var(b, layer_id); } diff --git a/src/compiler/nir/nir_lower_passthrough_edgeflags.c b/src/compiler/nir/nir_lower_passthrough_edgeflags.c index e6d0b14f26b..0a7362288c9 100644 --- a/src/compiler/nir/nir_lower_passthrough_edgeflags.c +++ b/src/compiler/nir/nir_lower_passthrough_edgeflags.c @@ -75,16 +75,12 @@ lower_impl(nir_function_impl *impl) return; } - in = nir_variable_create(shader, nir_var_shader_in, - glsl_vec4_type(), "edgeflag_in"); - in->data.location = VERT_ATTRIB_EDGEFLAG; - - in->data.driver_location = shader->num_inputs++; + in = nir_create_variable_with_location(b.shader, nir_var_shader_in, + VERT_ATTRIB_EDGEFLAG, glsl_vec4_type()); shader->info.inputs_read |= VERT_BIT_EDGEFLAG; - out = nir_variable_create(shader, nir_var_shader_out, - glsl_vec4_type(), "edgeflag_out"); - out->data.location = VARYING_SLOT_EDGE; + out = nir_create_variable_with_location(b.shader, nir_var_shader_out, + VARYING_SLOT_EDGE, glsl_vec4_type()); shader->info.outputs_written |= VARYING_BIT_EDGE; def = nir_load_var(&b, in); diff --git a/src/compiler/nir/nir_lower_point_size_mov.c b/src/compiler/nir/nir_lower_point_size_mov.c index 6c1c16c749a..23d971d712a 100644 --- a/src/compiler/nir/nir_lower_point_size_mov.c +++ b/src/compiler/nir/nir_lower_point_size_mov.c @@ -55,9 +55,8 @@ lower_impl(nir_function_impl *impl, * and only emit that one for xfb */ if (!out || out->data.explicit_location) { - new_out = nir_variable_create(shader, nir_var_shader_out, - glsl_float_type(), "gl_PointSizeMESA"); - new_out->data.location = VARYING_SLOT_PSIZ; + new_out = nir_create_variable_with_location(shader, nir_var_shader_out, + VARYING_SLOT_PSIZ, glsl_float_type()); } diff --git a/src/compiler/nir/nir_lower_texcoord_replace.c b/src/compiler/nir/nir_lower_texcoord_replace.c index f9de2c890a3..a1c77bf4154 100644 --- a/src/compiler/nir/nir_lower_texcoord_replace.c +++ b/src/compiler/nir/nir_lower_texcoord_replace.c @@ -74,17 +74,9 @@ nir_lower_texcoord_replace_impl(nir_function_impl *impl, 0, 2, 32); } else { /* find or create pntc */ - nir_variable *pntc = nir_find_variable_with_location(b.shader, - nir_var_shader_in, - VARYING_SLOT_PNTC); - if (!pntc) { - pntc = nir_variable_create(b.shader, nir_var_shader_in, - glsl_vec_type(2), "gl_PointCoord"); - pntc->data.location = VARYING_SLOT_PNTC; - pntc->data.driver_location = b.shader->num_inputs++; - b.shader->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_PNTC); - } - + nir_variable *pntc = nir_get_variable_with_location(b.shader, nir_var_shader_in, + VARYING_SLOT_PNTC, glsl_vec_type(2)); + b.shader->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_PNTC); new_coord = nir_load_var(&b, pntc); } diff --git a/src/compiler/nir/nir_lower_two_sided_color.c b/src/compiler/nir/nir_lower_two_sided_color.c index d3d7f5be24d..962d223134b 100644 --- a/src/compiler/nir/nir_lower_two_sided_color.c +++ b/src/compiler/nir/nir_lower_two_sided_color.c @@ -37,7 +37,6 @@ typedef struct { nir_variable *front; /* COLn */ nir_variable *back; /* BFCn */ } colors[MAX_COLORS]; - nir_variable *face; int colors_count; } lower_2side_state; @@ -51,38 +50,15 @@ static nir_variable * create_input(nir_shader *shader, gl_varying_slot slot, enum glsl_interp_mode interpolation) { - nir_variable *var = nir_variable_create(shader, nir_var_shader_in, - glsl_vec4_type(), NULL); + nir_variable *var = nir_create_variable_with_location(shader, nir_var_shader_in, + slot, glsl_vec4_type()); - var->data.driver_location = shader->num_inputs++; - var->name = ralloc_asprintf(var, "in_%d", var->data.driver_location); var->data.index = 0; - var->data.location = slot; var->data.interpolation = interpolation; return var; } -static nir_variable * -create_face_input(nir_shader *shader) -{ - nir_variable *var = - nir_find_variable_with_location(shader, nir_var_shader_in, - VARYING_SLOT_FACE); - - if (var == NULL) { - var = nir_variable_create(shader, nir_var_shader_in, - glsl_bool_type(), "gl_FrontFacing"); - - var->data.driver_location = shader->num_inputs++; - var->data.index = 0; - var->data.location = VARYING_SLOT_FACE; - var->data.interpolation = INTERP_MODE_FLAT; - } - - return var; -} - static nir_ssa_def * load_input(nir_builder *b, nir_variable *in) { @@ -123,9 +99,6 @@ setup_inputs(lower_2side_state *state) state->colors[i].front->data.interpolation); } - if (!state->face_sysval) - state->face = create_face_input(state->shader); - return 0; } @@ -174,8 +147,12 @@ nir_lower_two_sided_color_instr(nir_builder *b, nir_instr *instr, void *data) nir_ssa_def *face; if (state->face_sysval) face = nir_load_front_face(b, 1); - else - face = nir_load_var(b, state->face); + else { + nir_variable *var = nir_get_variable_with_location(b->shader, nir_var_shader_in, + VARYING_SLOT_FACE, glsl_bool_type()); + var->data.interpolation = INTERP_MODE_FLAT; + face = nir_load_var(b, var); + } nir_ssa_def *front, *back; if (intr->intrinsic == nir_intrinsic_load_deref) { diff --git a/src/compiler/nir/nir_passthrough_tcs.c b/src/compiler/nir/nir_passthrough_tcs.c index 0d2edbccd60..2abaf6e5ce9 100644 --- a/src/compiler/nir/nir_passthrough_tcs.c +++ b/src/compiler/nir/nir_passthrough_tcs.c @@ -42,33 +42,24 @@ nir_create_passthrough_tcs_impl(const nir_shader_compiler_options *options, nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_TESS_CTRL, options, "tcs passthrough"); - unsigned num_inputs = 0; - unsigned num_outputs = 0; - nir_variable *in_inner = - nir_variable_create(b.shader, nir_var_system_value, glsl_vec_type(2), - "tess inner default"); - in_inner->data.location = SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT; + nir_create_variable_with_location(b.shader, nir_var_system_value, + SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT, glsl_vec_type(2)); nir_variable *out_inner = - nir_variable_create(b.shader, nir_var_shader_out, glsl_vec_type(2), - "tess inner"); - out_inner->data.location = VARYING_SLOT_TESS_LEVEL_INNER; - out_inner->data.driver_location = num_outputs++; + nir_create_variable_with_location(b.shader, nir_var_shader_out, + VARYING_SLOT_TESS_LEVEL_INNER, glsl_vec_type(2)); nir_ssa_def *inner = nir_load_var(&b, in_inner); nir_store_var(&b, out_inner, inner, 0x3); nir_variable *in_outer = - nir_variable_create(b.shader, nir_var_system_value, glsl_vec4_type(), - "tess outer default"); - in_outer->data.location = SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT; + nir_create_variable_with_location(b.shader, nir_var_system_value, + SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT, glsl_vec4_type()); nir_variable *out_outer = - nir_variable_create(b.shader, nir_var_shader_out, glsl_vec4_type(), - "tess outer"); - out_outer->data.location = VARYING_SLOT_TESS_LEVEL_OUTER; - out_outer->data.driver_location = num_outputs++; + nir_create_variable_with_location(b.shader, nir_var_shader_out, + VARYING_SLOT_TESS_LEVEL_OUTER, glsl_vec4_type()); nir_ssa_def *outer = nir_load_var(&b, in_outer); nir_store_var(&b, out_outer, outer, 0xf); @@ -83,25 +74,17 @@ nir_create_passthrough_tcs_impl(const nir_shader_compiler_options *options, else continue; - char name[10]; - snprintf(name, sizeof(name), "in_%d", i); - nir_variable *in = nir_variable_create(b.shader, nir_var_shader_in, type, name); - in->data.location = semantic; - in->data.driver_location = num_inputs++; + nir_variable *in = nir_create_variable_with_location(b.shader, nir_var_shader_in, + semantic, type); - snprintf(name, sizeof(name), "out_%d", i); - nir_variable *out = nir_variable_create(b.shader, nir_var_shader_out, type, name); - out->data.location = semantic; - out->data.driver_location = num_outputs++; + nir_variable *out = nir_create_variable_with_location(b.shader, nir_var_shader_out, + semantic, type); /* no need to use copy_var to save a lower pass */ nir_ssa_def *value = nir_load_array_var(&b, in, id); nir_store_array_var(&b, out, id, value, 0xf); } - b.shader->num_inputs = num_inputs; - b.shader->num_outputs = num_outputs; - b.shader->info.tess.tcs_vertices_out = patch_vertices; nir_validate_shader(b.shader, "in nir_create_passthrough_tcs"); diff --git a/src/gallium/auxiliary/nir/nir_draw_helpers.c b/src/gallium/auxiliary/nir/nir_draw_helpers.c index ad0d6d15ea1..2b362433f77 100644 --- a/src/gallium/auxiliary/nir/nir_draw_helpers.c +++ b/src/gallium/auxiliary/nir/nir_draw_helpers.c @@ -50,16 +50,9 @@ typedef struct { static nir_ssa_def * load_frag_coord(nir_builder *b) { - nir_foreach_shader_in_variable(var, b->shader) { - if (var->data.location == VARYING_SLOT_POS) - return nir_load_var(b, var); - } - - nir_variable *pos = nir_variable_create(b->shader, nir_var_shader_in, - glsl_vec4_type(), NULL); - pos->data.location = VARYING_SLOT_POS; + nir_variable *pos = nir_get_variable_with_location(b->shader, nir_var_shader_in, + VARYING_SLOT_POS, glsl_vec4_type()); pos->data.interpolation = INTERP_MODE_NOPERSPECTIVE; - pos->data.driver_location = b->shader->num_inputs++; return nir_load_var(b, pos); } diff --git a/src/gallium/auxiliary/nir/nir_to_tgsi.c b/src/gallium/auxiliary/nir/nir_to_tgsi.c index 5d60915392f..cb95afa5383 100644 --- a/src/gallium/auxiliary/nir/nir_to_tgsi.c +++ b/src/gallium/auxiliary/nir/nir_to_tgsi.c @@ -3673,15 +3673,8 @@ nir_lower_primid_sysval_to_input_filter(const nir_instr *instr, const void *_dat static nir_ssa_def * nir_lower_primid_sysval_to_input_lower(nir_builder *b, nir_instr *instr, void *data) { - nir_variable *var = *(nir_variable **)data; - if (!var) { - var = nir_variable_create(b->shader, nir_var_shader_in, glsl_uint_type(), "gl_PrimitiveID"); - var->data.location = VARYING_SLOT_PRIMITIVE_ID; - b->shader->info.inputs_read |= VARYING_BIT_PRIMITIVE_ID; - var->data.driver_location = b->shader->num_inputs++; - - *(nir_variable **)data = var; - } + nir_variable *var = nir_get_variable_with_location(b->shader, nir_var_shader_in, + VARYING_SLOT_PRIMITIVE_ID, glsl_uint_type()); nir_io_semantics semantics = { .location = var->data.location, @@ -3695,11 +3688,9 @@ nir_lower_primid_sysval_to_input_lower(nir_builder *b, nir_instr *instr, void *d static bool nir_lower_primid_sysval_to_input(nir_shader *s) { - nir_variable *input = NULL; - return nir_shader_lower_instructions(s, nir_lower_primid_sysval_to_input_filter, - nir_lower_primid_sysval_to_input_lower, &input); + nir_lower_primid_sysval_to_input_lower, NULL); } const void * diff --git a/src/mesa/main/ffvertex_prog.c b/src/mesa/main/ffvertex_prog.c index a5d8bb1a3af..31fea0cbf7f 100644 --- a/src/mesa/main/ffvertex_prog.c +++ b/src/mesa/main/ffvertex_prog.c @@ -375,21 +375,9 @@ load_input(struct tnl_program *p, gl_vert_attrib attr, const struct glsl_type *type) { if (p->state->varying_vp_inputs & VERT_BIT(attr)) { - nir_variable *var = - nir_find_variable_with_location(p->b->shader, - nir_var_shader_in, - attr); - if (!var) { - var = nir_variable_create(p->b->shader, - nir_var_shader_in, - type, - gl_vert_attrib_name(attr)); - - var->data.location = attr; - var->data.driver_location = p->b->shader->num_inputs++; - - p->b->shader->info.inputs_read |= (uint64_t)VERT_BIT(attr); - } + nir_variable *var = nir_get_variable_with_location(p->b->shader, nir_var_shader_in, + attr, type); + p->b->shader->info.inputs_read |= (uint64_t)VERT_BIT(attr); return nir_load_var(p->b, var); } else return load_state_var(p, STATE_CURRENT_ATTRIB, attr, 0, 0, type); @@ -405,20 +393,8 @@ static nir_variable * register_output(struct tnl_program *p, gl_varying_slot slot, const struct glsl_type *type) { - nir_variable *var = - nir_find_variable_with_location(p->b->shader, - nir_var_shader_out, - slot); - if (var) - return var; - - const char *name = - gl_varying_slot_name_for_stage(slot, MESA_SHADER_VERTEX); - var = nir_variable_create(p->b->shader, nir_var_shader_out, type, name); - - var->data.location = slot; - var->data.driver_location = p->b->shader->num_outputs++; - + nir_variable *var = nir_get_variable_with_location(p->b->shader, nir_var_shader_out, + slot, type); p->b->shader->info.outputs_written |= BITFIELD64_BIT(slot); return var; } diff --git a/src/mesa/program/prog_to_nir.c b/src/mesa/program/prog_to_nir.c index a86df264a82..daab806b60e 100644 --- a/src/mesa/program/prog_to_nir.c +++ b/src/mesa/program/prog_to_nir.c @@ -871,18 +871,14 @@ setup_registers_and_variables(struct ptn_compile *c) if (c->ctx->Const.GLSLFragCoordIsSysVal && shader->info.stage == MESA_SHADER_FRAGMENT && i == VARYING_SLOT_POS) { - nir_variable *var = nir_variable_create(shader, nir_var_system_value, glsl_vec4_type(), - "frag_coord"); - var->data.location = SYSTEM_VALUE_FRAG_COORD; - c->input_vars[i] = var; + c->input_vars[i] = nir_create_variable_with_location(shader, nir_var_system_value, + SYSTEM_VALUE_FRAG_COORD, glsl_vec4_type()); continue; } nir_variable *var = - nir_variable_create(shader, nir_var_shader_in, glsl_vec4_type(), - ralloc_asprintf(shader, "in_%d", i)); - var->data.location = i; - var->data.index = 0; + nir_create_variable_with_location(shader, nir_var_shader_in, + i, glsl_vec4_type()); if (c->prog->Target == GL_FRAGMENT_PROGRAM_ARB) { if (i == VARYING_SLOT_FOGC) { @@ -918,13 +914,8 @@ setup_registers_and_variables(struct ptn_compile *c) /* Create system value variables */ int i; BITSET_FOREACH_SET(i, c->prog->info.system_values_read, SYSTEM_VALUE_MAX) { - nir_variable *var = - nir_variable_create(shader, nir_var_system_value, glsl_vec4_type(), - ralloc_asprintf(shader, "sv_%d", i)); - var->data.location = i; - var->data.index = 0; - - c->sysval_vars[i] = var; + c->sysval_vars[i] = nir_create_variable_with_location(b->shader, nir_var_system_value, + i, glsl_vec4_type()); } /* Create output registers and variables. */ diff --git a/src/mesa/state_tracker/st_atifs_to_nir.c b/src/mesa/state_tracker/st_atifs_to_nir.c index b44852cd391..3cde37bad68 100644 --- a/src/mesa/state_tracker/st_atifs_to_nir.c +++ b/src/mesa/state_tracker/st_atifs_to_nir.c @@ -118,13 +118,9 @@ static nir_ssa_def * load_input(struct st_translate *t, gl_varying_slot slot) { if (!t->inputs[slot]) { - const char *slot_name = - gl_varying_slot_name_for_stage(slot, MESA_SHADER_FRAGMENT); - nir_variable *var = nir_variable_create(t->b->shader, nir_var_shader_in, - slot == VARYING_SLOT_FOGC ? - glsl_float_type() : glsl_vec4_type(), - slot_name); - var->data.location = slot; + nir_variable *var = nir_create_variable_with_location(t->b->shader, nir_var_shader_in, slot, + slot == VARYING_SLOT_FOGC ? + glsl_float_type() : glsl_vec4_type()); var->data.interpolation = INTERP_MODE_NONE; t->inputs[slot] = nir_load_var(t->b, var); @@ -461,9 +457,8 @@ st_translate_atifs_program(struct ati_fragment_shader *atifs, s->info.name = ralloc_asprintf(s, "ATIFS%d", program->Id); s->info.internal = false; - t->fragcolor = nir_variable_create(b.shader, nir_var_shader_out, - glsl_vec4_type(), "gl_FragColor"); - t->fragcolor->data.location = FRAG_RESULT_COLOR; + t->fragcolor = nir_create_variable_with_location(b.shader, nir_var_shader_out, + FRAG_RESULT_COLOR, glsl_vec4_type()); st_atifs_setup_uniforms(t, program); diff --git a/src/mesa/state_tracker/st_cb_drawpixels.c b/src/mesa/state_tracker/st_cb_drawpixels.c index 6c1e716f23c..52f23491e15 100644 --- a/src/mesa/state_tracker/st_cb_drawpixels.c +++ b/src/mesa/state_tracker/st_cb_drawpixels.c @@ -153,37 +153,29 @@ make_drawpix_z_stencil_program_nir(struct st_context *st, write_stencil ? "S" : ""); nir_variable *texcoord = - nir_variable_create(b.shader, nir_var_shader_in, glsl_vec_type(2), - "texcoord"); - texcoord->data.location = VARYING_SLOT_TEX0; + nir_create_variable_with_location(b.shader, nir_var_shader_in, + VARYING_SLOT_TEX0, glsl_vec_type(2)); if (write_depth) { nir_variable *out = - nir_variable_create(b.shader, nir_var_shader_out, glsl_float_type(), - "gl_FragDepth"); - out->data.location = FRAG_RESULT_DEPTH; + nir_create_variable_with_location(b.shader, nir_var_shader_out, + FRAG_RESULT_DEPTH, glsl_float_type()); nir_ssa_def *depth = sample_via_nir(&b, texcoord, "depth", 0, GLSL_TYPE_FLOAT, nir_type_float32); nir_store_var(&b, out, depth, 0x1); /* Also copy color */ - nir_variable *color_in = - nir_variable_create(b.shader, nir_var_shader_in, glsl_vec_type(4), - "v_color"); - color_in->data.location = VARYING_SLOT_COL0; - - nir_variable *color_out = - nir_variable_create(b.shader, nir_var_shader_out, glsl_vec_type(4), - "gl_FragColor"); - color_out->data.location = FRAG_RESULT_COLOR; - nir_copy_var(&b, color_out, color_in); + nir_copy_var(&b, + nir_create_variable_with_location(b.shader, nir_var_shader_out, + FRAG_RESULT_COLOR, glsl_vec4_type()), + nir_create_variable_with_location(b.shader, nir_var_shader_in, + VARYING_SLOT_COL0, glsl_vec4_type())); } if (write_stencil) { nir_variable *out = - nir_variable_create(b.shader, nir_var_shader_out, glsl_uint_type(), - "gl_FragStencilRefARB"); - out->data.location = FRAG_RESULT_STENCIL; + nir_create_variable_with_location(b.shader, nir_var_shader_out, + FRAG_RESULT_STENCIL, glsl_uint_type()); nir_ssa_def *stencil = sample_via_nir(&b, texcoord, "stencil", 1, GLSL_TYPE_UINT, nir_type_uint32); nir_store_var(&b, out, stencil, 0x1); @@ -203,9 +195,8 @@ make_drawpix_zs_to_color_program_nir(struct st_context *st, "copypixels ZStoC"); nir_variable *texcoord = - nir_variable_create(b.shader, nir_var_shader_in, glsl_vec_type(2), - "texcoord"); - texcoord->data.location = VARYING_SLOT_TEX0; + nir_create_variable_with_location(b.shader, nir_var_shader_in, + VARYING_SLOT_TEX0, glsl_vec_type(2)); /* Sample depth and stencil */ nir_ssa_def *depth = sample_via_nir(&b, texcoord, "depth", 0, @@ -215,9 +206,8 @@ make_drawpix_zs_to_color_program_nir(struct st_context *st, /* Create the variable to store the output color */ nir_variable *color_out = - nir_variable_create(b.shader, nir_var_shader_out, glsl_vec_type(4), - "make_drawpix_zs_to_color_program_nirgl_FragColor"); - color_out->data.location = FRAG_RESULT_COLOR; + nir_create_variable_with_location(b.shader, nir_var_shader_out, + FRAG_RESULT_COLOR, glsl_vec_type(4)); nir_ssa_def *shifted_depth = nir_fmul(&b,nir_f2f64(&b, depth), nir_imm_double(&b,0xffffff)); nir_ssa_def *int_depth = nir_f2u32(&b,shifted_depth); diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp index 4779b16e886..a07ac24b4d1 100644 --- a/src/mesa/state_tracker/st_glsl_to_nir.cpp +++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp @@ -228,15 +228,15 @@ st_nir_assign_uniform_locations(struct gl_context *ctx, } } -/* - create a gl_PointSizeMESA variable +/* - create a gl_PointSize variable * - find every gl_Position write - * - store 1.0 to gl_PointSizeMESA after every gl_Position write + * - store 1.0 to gl_PointSize after every gl_Position write */ void st_nir_add_point_size(nir_shader *nir) { - nir_variable *psiz = nir_variable_create(nir, nir_var_shader_out, glsl_float_type(), "gl_PointSizeMESA"); - psiz->data.location = VARYING_SLOT_PSIZ; + nir_variable *psiz = nir_create_variable_with_location(nir, nir_var_shader_out, + VARYING_SLOT_PSIZ, glsl_float_type()); psiz->data.how_declared = nir_var_hidden; nir_builder b; diff --git a/src/mesa/state_tracker/st_nir_builtins.c b/src/mesa/state_tracker/st_nir_builtins.c index ba403c8615b..e5fed26e640 100644 --- a/src/mesa/state_tracker/st_nir_builtins.c +++ b/src/mesa/state_tracker/st_nir_builtins.c @@ -112,26 +112,22 @@ st_nir_make_passthrough_shader(struct st_context *st, nir_builder b = nir_builder_init_simple_shader(stage, options, "%s", shader_name); - char var_name[15]; - for (unsigned i = 0; i < num_vars; i++) { nir_variable *in; if (sysval_mask & (1 << i)) { - snprintf(var_name, sizeof(var_name), "sys_%u", input_locations[i]); - in = nir_variable_create(b.shader, nir_var_system_value, - glsl_int_type(), var_name); + in = nir_create_variable_with_location(b.shader, nir_var_system_value, + input_locations[i], + glsl_int_type()); } else { - snprintf(var_name, sizeof(var_name), "in_%u", input_locations[i]); - in = nir_variable_create(b.shader, nir_var_shader_in, vec4, var_name); + in = nir_create_variable_with_location(b.shader, nir_var_shader_in, + input_locations[i], vec4); } - in->data.location = input_locations[i]; if (interpolation_modes) in->data.interpolation = interpolation_modes[i]; - snprintf(var_name, sizeof(var_name), "out_%u", output_locations[i]); nir_variable *out = - nir_variable_create(b.shader, nir_var_shader_out, in->type, var_name); - out->data.location = output_locations[i]; + nir_create_variable_with_location(b.shader, nir_var_shader_out, + output_locations[i], in->type); out->data.interpolation = in->data.interpolation; nir_copy_var(&b, out, in); @@ -161,10 +157,8 @@ st_nir_make_clearcolor_shader(struct st_context *st) .range = 16, .dest_type = nir_type_float32); - nir_variable *color_out = - nir_variable_create(b.shader, nir_var_shader_out, glsl_vec_type(4), - "outcolor"); - color_out->data.location = FRAG_RESULT_COLOR; + nir_variable *color_out = nir_create_variable_with_location(b.shader, nir_var_shader_out, + FRAG_RESULT_COLOR, glsl_vec4_type()); /* Write out the color */ nir_store_var(&b, color_out, clear_color, 0xf); diff --git a/src/mesa/state_tracker/st_pbo.c b/src/mesa/state_tracker/st_pbo.c index 210bba4624e..808b53950f2 100644 --- a/src/mesa/state_tracker/st_pbo.c +++ b/src/mesa/state_tracker/st_pbo.c @@ -293,23 +293,17 @@ st_pbo_create_vs(struct st_context *st) nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, options, "st/pbo VS"); - nir_variable *in_pos = nir_variable_create(b.shader, nir_var_shader_in, - vec4, "in_pos"); - in_pos->data.location = VERT_ATTRIB_POS; + nir_variable *in_pos = nir_create_variable_with_location(b.shader, nir_var_shader_in, + VERT_ATTRIB_POS, vec4); - nir_variable *out_pos = nir_variable_create(b.shader, nir_var_shader_out, - vec4, "out_pos"); - out_pos->data.location = VARYING_SLOT_POS; - out_pos->data.interpolation = INTERP_MODE_NONE; + nir_variable *out_pos = nir_create_variable_with_location(b.shader, nir_var_shader_out, + VARYING_SLOT_POS, vec4); nir_copy_var(&b, out_pos, in_pos); if (st->pbo.layers) { - nir_variable *instance_id = nir_variable_create(b.shader, - nir_var_system_value, - glsl_int_type(), - "instance_id"); - instance_id->data.location = SYSTEM_VALUE_INSTANCE_ID; + nir_variable *instance_id = nir_create_variable_with_location(b.shader, nir_var_system_value, + SYSTEM_VALUE_INSTANCE_ID, glsl_int_type()); if (st->pbo.use_gs) { unsigned swiz_x[4] = {0, 0, 0, 0}; @@ -317,11 +311,8 @@ st_pbo_create_vs(struct st_context *st) nir_swizzle(&b, nir_i2f32(&b, nir_load_var(&b, instance_id)), swiz_x, 4), (1 << 2)); } else { - nir_variable *out_layer = nir_variable_create(b.shader, - nir_var_shader_out, - glsl_int_type(), - "out_layer"); - out_layer->data.location = VARYING_SLOT_LAYER; + nir_variable *out_layer = nir_create_variable_with_location(b.shader, nir_var_shader_out, + VARYING_SLOT_LAYER, glsl_int_type()); out_layer->data.interpolation = INTERP_MODE_NONE; nir_copy_var(&b, out_layer, instance_id); } @@ -429,11 +420,13 @@ create_fs(struct st_context *st, bool download, b.shader->num_uniforms += 4; nir_ssa_def *param = nir_load_var(&b, param_var); - nir_variable *fragcoord = - nir_variable_create(b.shader, pos_is_sysval ? nir_var_system_value : - nir_var_shader_in, glsl_vec4_type(), "gl_FragCoord"); - fragcoord->data.location = pos_is_sysval ? SYSTEM_VALUE_FRAG_COORD - : VARYING_SLOT_POS; + nir_variable *fragcoord; + if (pos_is_sysval) + fragcoord = nir_create_variable_with_location(b.shader, nir_var_system_value, + SYSTEM_VALUE_FRAG_COORD, glsl_vec4_type()); + else + fragcoord = nir_create_variable_with_location(b.shader, nir_var_shader_in, + VARYING_SLOT_POS, glsl_vec4_type()); nir_ssa_def *coord = nir_load_var(&b, fragcoord); /* When st->pbo.layers == false, it is guaranteed we only have a single @@ -449,9 +442,8 @@ create_fs(struct st_context *st, bool download, target == PIPE_TEXTURE_CUBE_ARRAY) { if (need_layer) { assert(st->pbo.layers); - nir_variable *var = nir_variable_create(b.shader, nir_var_shader_in, - glsl_int_type(), "gl_Layer"); - var->data.location = VARYING_SLOT_LAYER; + nir_variable *var = nir_create_variable_with_location(b.shader, nir_var_shader_in, + VARYING_SLOT_LAYER, glsl_int_type()); var->data.interpolation = INTERP_MODE_FLAT; layer = nir_load_var(&b, var); } @@ -570,9 +562,8 @@ create_fs(struct st_context *st, bool download, .image_dim = GLSL_SAMPLER_DIM_BUF); } else { nir_variable *color = - nir_variable_create(b.shader, nir_var_shader_out, glsl_vec4_type(), - "gl_FragColor"); - color->data.location = FRAG_RESULT_COLOR; + nir_create_variable_with_location(b.shader, nir_var_shader_out, + FRAG_RESULT_COLOR, glsl_vec4_type()); nir_store_var(&b, color, result, TGSI_WRITEMASK_XYZW); } -- cgit v1.2.1