nir: Add and use nir_tex_src_ssa

This makes texture instructions a lot less annoying to construct, especially in
cases where the deref-based helpers don't work.

I only converted core NIR, not the drivers. Since it was by hand.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Rhys Perry <pendingchaos02@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23352>
This commit is contained in:
Alyssa Rosenzweig 2023-05-25 16:51:33 -04:00 committed by Marge Bot
parent 218c00319b
commit 3863280399
8 changed files with 64 additions and 88 deletions

View file

@ -257,16 +257,12 @@ nir_build_tex_deref_instr(nir_builder *build, nir_texop op,
}
unsigned src_idx = 0;
tex->src[src_idx++] = (nir_tex_src) {
.src_type = nir_tex_src_texture_deref,
.src = nir_src_for_ssa(&texture->dest.ssa),
};
tex->src[src_idx++] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
&texture->dest.ssa);
if (sampler != NULL) {
assert(glsl_type_is_sampler(sampler->type));
tex->src[src_idx++] = (nir_tex_src) {
.src_type = nir_tex_src_sampler_deref,
.src = nir_src_for_ssa(&sampler->dest.ssa),
};
tex->src[src_idx++] = nir_tex_src_for_ssa(nir_tex_src_sampler_deref,
&sampler->dest.ssa);
}
for (unsigned i = 0; i < num_extra_srcs; i++) {
switch (extra_srcs[i].src_type) {

View file

@ -1641,14 +1641,20 @@ nir_load_param(nir_builder *build, uint32_t param_idx)
return nir_build_load_param(build, param->num_components, param->bit_size, param_idx);
}
static inline nir_tex_src
nir_tex_src_for_ssa(nir_tex_src_type src_type, nir_ssa_def *def)
{
nir_tex_src src;
src.src = nir_src_for_ssa(def);
src.src_type = src_type;
return src;
}
static inline nir_ssa_def *
nir_tex_deref(nir_builder *b, nir_deref_instr *t, nir_deref_instr *s,
nir_ssa_def *coord)
{
nir_tex_src srcs[] = {{
nir_src_for_ssa(coord),
nir_tex_src_coord,
}};
nir_tex_src srcs[] = {nir_tex_src_for_ssa(nir_tex_src_coord, coord)};
return nir_build_tex_deref_instr(b, nir_texop_tex, t, s,
ARRAY_SIZE(srcs), srcs);
@ -1658,13 +1664,10 @@ static inline nir_ssa_def *
nir_txl_deref(nir_builder *b, nir_deref_instr *t, nir_deref_instr *s,
nir_ssa_def *coord, nir_ssa_def *lod)
{
nir_tex_src srcs[] = {{
nir_src_for_ssa(coord),
nir_tex_src_coord,
}, {
nir_src_for_ssa(lod),
nir_tex_src_lod,
}};
nir_tex_src srcs[] = {
nir_tex_src_for_ssa(nir_tex_src_coord, coord),
nir_tex_src_for_ssa(nir_tex_src_lod, lod),
};
return nir_build_tex_deref_instr(b, nir_texop_txl, t, s,
ARRAY_SIZE(srcs), srcs);
@ -1684,9 +1687,7 @@ nir_txf_deref(nir_builder *b, nir_deref_instr *t,
nir_tex_src srcs[2];
unsigned num_srcs = 0;
srcs[num_srcs].src_type = nir_tex_src_coord;
srcs[num_srcs].src = nir_src_for_ssa(coord);
num_srcs++;
srcs[num_srcs++] = nir_tex_src_for_ssa(nir_tex_src_coord, coord);
if (lod == NULL) {
switch (glsl_get_sampler_dim(t->type)) {
@ -1701,11 +1702,8 @@ nir_txf_deref(nir_builder *b, nir_deref_instr *t,
}
}
if (lod != NULL) {
srcs[num_srcs].src_type = nir_tex_src_lod;
srcs[num_srcs].src = nir_src_for_ssa(lod);
num_srcs++;
}
if (lod != NULL)
srcs[num_srcs++] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
return nir_build_tex_deref_instr(b, nir_texop_txf, t, NULL,
num_srcs, srcs);
@ -1715,13 +1713,10 @@ static inline nir_ssa_def *
nir_txf_ms_deref(nir_builder *b, nir_deref_instr *t,
nir_ssa_def *coord, nir_ssa_def *ms_index)
{
nir_tex_src srcs[] = {{
nir_src_for_ssa(coord),
nir_tex_src_coord,
}, {
nir_src_for_ssa(ms_index),
nir_tex_src_ms_index,
}};
nir_tex_src srcs[] = {
nir_tex_src_for_ssa(nir_tex_src_coord, coord),
nir_tex_src_for_ssa(nir_tex_src_ms_index, ms_index),
};
return nir_build_tex_deref_instr(b, nir_texop_txf_ms, t, NULL,
ARRAY_SIZE(srcs), srcs);
@ -1731,10 +1726,7 @@ static inline nir_ssa_def *
nir_samples_identical_deref(nir_builder *b, nir_deref_instr *t,
nir_ssa_def *coord)
{
nir_tex_src srcs[] = {{
nir_src_for_ssa(coord),
nir_tex_src_coord,
}};
nir_tex_src srcs[] = {nir_tex_src_for_ssa(nir_tex_src_coord, coord)};
return nir_build_tex_deref_instr(b, nir_texop_samples_identical, t, NULL,
ARRAY_SIZE(srcs), srcs);

View file

@ -371,8 +371,7 @@ nir_get_texture_size(nir_builder *b, nir_tex_instr *tex)
}
}
/* Add in an LOD because some back-ends require it */
txs->src[idx].src = nir_src_for_ssa(nir_imm_int(b, 0));
txs->src[idx].src_type = nir_tex_src_lod;
txs->src[idx] = nir_tex_src_for_ssa(nir_tex_src_lod, nir_imm_int(b, 0));
nir_ssa_dest_init(&txs->instr, &txs->dest, nir_tex_instr_dest_size(txs),
32);

View file

@ -169,8 +169,8 @@ get_texture_size(struct ycbcr_state *state, nir_deref_instr *texture)
tex->is_shadow = glsl_sampler_type_is_shadow(type);
tex->dest_type = nir_type_int32;
tex->src[0].src_type = nir_tex_src_texture_deref;
tex->src[0].src = nir_src_for_ssa(&texture->dest.ssa);
tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
&texture->dest.ssa);
nir_ssa_dest_init(&tex->instr, &tex->dest, nir_tex_instr_dest_size(tex),
32);
@ -256,9 +256,8 @@ create_plane_tex_instr_implicit(struct ycbcr_state *state,
break;
}
}
tex->src[tex->num_srcs - 1].src = nir_src_for_ssa(nir_imm_int(b, plane));
tex->src[tex->num_srcs - 1].src_type = nir_tex_src_plane;
tex->src[tex->num_srcs - 1] = nir_tex_src_for_ssa(nir_tex_src_plane,
nir_imm_int(b, plane));
tex->sampler_dim = old_tex->sampler_dim;
tex->dest_type = old_tex->dest_type;

View file

@ -112,14 +112,14 @@ lower_color(nir_builder *b, lower_drawpixels_state *state, nir_intrinsic_instr *
tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
tex->coord_components = 2;
tex->dest_type = nir_type_float32;
tex->src[0].src_type = nir_tex_src_texture_deref;
tex->src[0].src = nir_src_for_ssa(&tex_deref->dest.ssa);
tex->src[1].src_type = nir_tex_src_sampler_deref;
tex->src[1].src = nir_src_for_ssa(&tex_deref->dest.ssa);
tex->src[2].src_type = nir_tex_src_coord;
tex->src[2].src =
nir_src_for_ssa(nir_channels(b, texcoord,
(1 << tex->coord_components) - 1));
tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
&tex_deref->dest.ssa);
tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_sampler_deref,
&tex_deref->dest.ssa);
tex->src[2] =
nir_tex_src_for_ssa(nir_tex_src_coord,
nir_channels(b, texcoord,
(1 << tex->coord_components) - 1));
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32);
nir_builder_instr_insert(b, &tex->instr);
@ -154,12 +154,12 @@ lower_color(nir_builder *b, lower_drawpixels_state *state, nir_intrinsic_instr *
tex->sampler_index = state->options->pixelmap_sampler;
tex->texture_index = state->options->pixelmap_sampler;
tex->dest_type = nir_type_float32;
tex->src[0].src_type = nir_tex_src_texture_deref;
tex->src[0].src = nir_src_for_ssa(&pixelmap_deref->dest.ssa);
tex->src[1].src_type = nir_tex_src_sampler_deref;
tex->src[1].src = nir_src_for_ssa(&pixelmap_deref->dest.ssa);
tex->src[2].src_type = nir_tex_src_coord;
tex->src[2].src = nir_src_for_ssa(nir_channels(b, def, 0x3));
tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
&pixelmap_deref->dest.ssa);
tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_sampler_deref,
&pixelmap_deref->dest.ssa);
tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_coord,
nir_channels(b, def, 0x3));
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32);
nir_builder_instr_insert(b, &tex->instr);
@ -172,8 +172,8 @@ lower_color(nir_builder *b, lower_drawpixels_state *state, nir_intrinsic_instr *
tex->coord_components = 2;
tex->sampler_index = state->options->pixelmap_sampler;
tex->dest_type = nir_type_float32;
tex->src[0].src_type = nir_tex_src_coord;
tex->src[0].src = nir_src_for_ssa(nir_channels(b, def, 0xc));
tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_coord,
nir_channels(b, def, 0xc));
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32);
nir_builder_instr_insert(b, &tex->instr);

View file

@ -123,15 +123,12 @@ try_lower_input_load(nir_builder *b, nir_intrinsic_instr *load,
tex->texture_index = 0;
tex->sampler_index = 0;
tex->src[0].src_type = nir_tex_src_texture_deref;
tex->src[0].src = nir_src_for_ssa(&deref->dest.ssa);
tex->src[1].src_type = nir_tex_src_coord;
tex->src[1].src = nir_src_for_ssa(coord);
tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
&deref->dest.ssa);
tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_coord, coord);
tex->coord_components = 3;
tex->src[2].src_type = nir_tex_src_lod;
tex->src[2].src = nir_src_for_ssa(nir_imm_int(b, 0));
tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_lod, nir_imm_int(b, 0));
if (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) {
tex->op = nir_texop_txf_ms;

View file

@ -127,8 +127,8 @@ lower_readonly_image_instr_intrinsic(nir_builder *b, nir_intrinsic_instr *intrin
if (glsl_sampler_type_is_array(deref->type))
coord_components++;
tex->src[0].src_type = nir_tex_src_texture_deref;
tex->src[0].src = nir_src_for_ssa(&deref->dest.ssa);
tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
&deref->dest.ssa);
if (options->per_variable) {
assert(nir_deref_instr_get_variable(deref));
@ -140,14 +140,12 @@ lower_readonly_image_instr_intrinsic(nir_builder *b, nir_intrinsic_instr *intrin
assert(intrin->src[1].is_ssa);
nir_ssa_def *coord =
nir_trim_vector(b, intrin->src[1].ssa, coord_components);
tex->src[1].src_type = nir_tex_src_coord;
tex->src[1].src = nir_src_for_ssa(coord);
tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_coord, coord);
tex->coord_components = coord_components;
assert(intrin->src[3].is_ssa);
nir_ssa_def *lod = intrin->src[3].ssa;
tex->src[2].src_type = nir_tex_src_lod;
tex->src[2].src = nir_src_for_ssa(lod);
tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
assert(num_srcs == 3);
@ -159,8 +157,7 @@ lower_readonly_image_instr_intrinsic(nir_builder *b, nir_intrinsic_instr *intrin
case nir_intrinsic_image_deref_size: {
assert(intrin->src[1].is_ssa);
nir_ssa_def *lod = intrin->src[1].ssa;
tex->src[1].src_type = nir_tex_src_lod;
tex->src[1].src = nir_src_for_ssa(lod);
tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
assert(num_srcs == 2);

View file

@ -318,8 +318,8 @@ sample_plane(nir_builder *b, nir_tex_instr *tex, int plane,
nir_src_copy(&plane_tex->src[i].src, &tex->src[i].src, &plane_tex->instr);
plane_tex->src[i].src_type = tex->src[i].src_type;
}
plane_tex->src[tex->num_srcs].src = nir_src_for_ssa(nir_imm_int(b, plane));
plane_tex->src[tex->num_srcs].src_type = nir_tex_src_plane;
plane_tex->src[tex->num_srcs] = nir_tex_src_for_ssa(nir_tex_src_plane,
nir_imm_int(b, plane));
plane_tex->op = nir_texop_tex;
plane_tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
plane_tex->dest_type = nir_type_float | nir_dest_bit_size(tex->dest);
@ -819,10 +819,8 @@ lower_tex_to_txd(nir_builder *b, nir_tex_instr *tex)
assert(coord >= 0);
nir_ssa_def *dfdx = nir_fddx(b, tex->src[coord].src.ssa);
nir_ssa_def *dfdy = nir_fddy(b, tex->src[coord].src.ssa);
txd->src[tex->num_srcs].src = nir_src_for_ssa(dfdx);
txd->src[tex->num_srcs].src_type = nir_tex_src_ddx;
txd->src[tex->num_srcs + 1].src = nir_src_for_ssa(dfdy);
txd->src[tex->num_srcs + 1].src_type = nir_tex_src_ddy;
txd->src[tex->num_srcs] = nir_tex_src_for_ssa(nir_tex_src_ddx, dfdx);
txd->src[tex->num_srcs + 1] = nir_tex_src_for_ssa(nir_tex_src_ddy, dfdy);
nir_ssa_dest_init(&txd->instr, &txd->dest,
nir_dest_num_components(tex->dest),
@ -862,8 +860,7 @@ lower_txb_to_txl(nir_builder *b, nir_tex_instr *tex)
int bias_idx = nir_tex_instr_src_index(tex, nir_tex_src_bias);
assert(bias_idx >= 0);
lod = nir_fadd(b, nir_channel(b, lod, 1), nir_ssa_for_src(b, tex->src[bias_idx].src, 1));
txl->src[tex->num_srcs - 1].src = nir_src_for_ssa(lod);
txl->src[tex->num_srcs - 1].src_type = nir_tex_src_lod;
txl->src[tex->num_srcs - 1] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
nir_ssa_dest_init(&txl->instr, &txl->dest,
nir_dest_num_components(tex->dest),
@ -1150,10 +1147,9 @@ lower_tg4_offsets(nir_builder *b, nir_tex_instr *tex)
tex_copy->src[j].src_type = tex->src[j].src_type;
}
nir_tex_src src;
src.src = nir_src_for_ssa(nir_imm_ivec2(b, tex->tg4_offsets[i][0],
tex->tg4_offsets[i][1]));
src.src_type = nir_tex_src_offset;
nir_ssa_def *offset = nir_imm_ivec2(b, tex->tg4_offsets[i][0],
tex->tg4_offsets[i][1]);
nir_tex_src src = nir_tex_src_for_ssa(nir_tex_src_offset, offset);
tex_copy->src[tex_copy->num_srcs - 1] = src;
nir_ssa_dest_init(&tex_copy->instr, &tex_copy->dest,