nir: Add a helper for adding texture instruction sources

Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
This commit is contained in:
Jason Ekstrand
2017-10-16 08:50:23 -07:00
parent 31fb7bbe0b
commit 41c75b5354
4 changed files with 29 additions and 43 deletions

View File

@@ -541,6 +541,28 @@ nir_tex_instr_create(nir_shader *shader, unsigned num_srcs)
return instr;
}
void
nir_tex_instr_add_src(nir_tex_instr *tex,
nir_tex_src_type src_type,
nir_src src)
{
nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,
tex->num_srcs + 1);
for (unsigned i = 0; i < tex->num_srcs; i++) {
new_srcs[i].src_type = tex->src[i].src_type;
nir_instr_move_src(&tex->instr, &new_srcs[i].src,
&tex->src[i].src);
}
ralloc_free(tex->src);
tex->src = new_srcs;
tex->src[tex->num_srcs].src_type = src_type;
nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src, src);
tex->num_srcs++;
}
void
nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)
{

View File

@@ -1379,6 +1379,10 @@ nir_tex_instr_src_index(const nir_tex_instr *instr, nir_tex_src_type type)
return -1;
}
void nir_tex_instr_add_src(nir_tex_instr *tex,
nir_tex_src_type src_type,
nir_src src);
void nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx);
typedef struct {

View File

@@ -109,32 +109,9 @@ lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_progr
assert(array_elements >= 1);
indirect = nir_umin(b, indirect, nir_imm_int(b, array_elements - 1));
/* First, we have to resize the array of texture sources */
nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
instr->num_srcs + 2);
for (unsigned i = 0; i < instr->num_srcs; i++) {
new_srcs[i].src_type = instr->src[i].src_type;
nir_instr_move_src(&instr->instr, &new_srcs[i].src,
&instr->src[i].src);
}
ralloc_free(instr->src);
instr->src = new_srcs;
/* Now we can go ahead and move the source over to being a
* first-class texture source.
*/
instr->src[instr->num_srcs].src_type = nir_tex_src_texture_offset;
instr->num_srcs++;
nir_instr_rewrite_src(&instr->instr,
&instr->src[instr->num_srcs - 1].src,
nir_tex_instr_add_src(instr, nir_tex_src_texture_offset,
nir_src_for_ssa(indirect));
instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
instr->num_srcs++;
nir_instr_rewrite_src(&instr->instr,
&instr->src[instr->num_srcs - 1].src,
nir_tex_instr_add_src(instr, nir_tex_src_sampler_offset,
nir_src_for_ssa(indirect));
instr->texture_array_size = array_elements;

View File

@@ -157,24 +157,7 @@ lower_tex_deref(nir_tex_instr *tex, nir_deref_var *deref,
if (state->add_bounds_checks)
index = nir_umin(b, index, nir_imm_int(b, array_size - 1));
nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,
tex->num_srcs + 1);
for (unsigned i = 0; i < tex->num_srcs; i++) {
new_srcs[i].src_type = tex->src[i].src_type;
nir_instr_move_src(&tex->instr, &new_srcs[i].src, &tex->src[i].src);
}
ralloc_free(tex->src);
tex->src = new_srcs;
/* Now we can go ahead and move the source over to being a
* first-class texture source.
*/
tex->src[tex->num_srcs].src_type = src_type;
nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src,
nir_src_for_ssa(index));
tex->num_srcs++;
nir_tex_instr_add_src(tex, src_type, nir_src_for_ssa(index));
} else {
*const_index += MIN2(deref_array->base_offset, array_size - 1);
}