<p dir="ltr"><br>
On May 19, 2016 12:50 AM, "Michael Schellenberger Costa" <<a href="mailto:mschellenbergercosta@googlemail.com">mschellenbergercosta@googlemail.com</a>> wrote:<br>
><br>
> Hi Jason,<br>
><br>
> Am 19.05.2016 um 09:22 schrieb Jason Ekstrand:<br>
> > ---<br>
> > src/intel/vulkan/anv_nir_apply_pipeline_layout.c | 52 ++++++++++++++++--------<br>
> > 1 file changed, 35 insertions(+), 17 deletions(-)<br>
> ><br>
> > diff --git a/src/intel/vulkan/anv_nir_apply_pipeline_layout.c b/src/intel/vulkan/anv_nir_apply_pipeline_layout.c<br>
> > index 91f4322..7e66149 100644<br>
> > --- a/src/intel/vulkan/anv_nir_apply_pipeline_layout.c<br>
> > +++ b/src/intel/vulkan/anv_nir_apply_pipeline_layout.c<br>
> > @@ -29,6 +29,9 @@ struct apply_pipeline_layout_state {<br>
> > nir_shader *shader;<br>
> > nir_builder builder;<br>
> ><br>
> > + struct anv_pipeline_layout *layout;<br>
> > + bool add_bounds_checks;<br>
> > +<br>
> > struct {<br>
> > BITSET_WORD *used;<br>
> > uint8_t *surface_offsets;<br>
> > @@ -110,17 +113,15 @@ lower_res_index_intrinsic(nir_intrinsic_instr *intrin,<br>
> > uint32_t binding = nir_intrinsic_binding(intrin);<br>
> ><br>
> > uint32_t surface_index = state->set[set].surface_offsets[binding];<br>
> > + uint32_t array_size =<br>
> > + state->layout->set[set].layout->binding[binding].array_size;<br>
> ><br>
> > - nir_const_value *const_block_idx =<br>
> > - nir_src_as_const_value(intrin->src[0]);<br>
> > + nir_ssa_def *block_index = nir_ssa_for_src(b, intrin->src[0], 1);<br>
> ><br>
> > - nir_ssa_def *block_index;<br>
> > - if (const_block_idx) {<br>
> > - block_index = nir_imm_int(b, surface_index + const_block_idx->u32[0]);<br>
> > - } else {<br>
> > - block_index = nir_iadd(b, nir_imm_int(b, surface_index),<br>
> > - nir_ssa_for_src(b, intrin->src[0], 1));<br>
> > - }<br>
> > + if (state->add_bounds_checks)<br>
> > + block_index = nir_umax(b, block_index, nir_imm_int(b, array_size - 1));<br>
> > +<br>
> > + block_index = nir_iadd(b, nir_imm_int(b, surface_index), block_index);<br>
> Here you do<br>
> | if (state->add_bounds_checks)<br>
> | block_index = nir_umax(...);<br>
> | block_index = nir_iadd(...);<br>
><br>
> Below you do<br>
> | nir_ssa_def *index = iadd(...);<br>
> | if (state->add_bounds_checks)<br>
> | index = nir_umax(...);<br>
><br>
> Are both functionally equivalent? Also why do you one time do</p>
<p dir="ltr">No but both are correct. In the case above, we are computing</p>
<p dir="ltr">resource_index(set, binding, index) = resource_index(set, binding, 0) + umin(index, bound)</p>
<p dir="ltr">(I just realized I got umin and umax backwards). In the second case, the index is split into a base and an indirect so we actually have 3 pieces: resource_index(set, binding, 0), base_offset, and the indirect. This means that instead of the index above we have base+indirect. Does that make sense?</p>
<p dir="ltr">> | nir_ssa_def *block_index = nir_ssa_for_src()<br>
><br>
> and the other time put that directly into nir_iadd()?<br>
><br>
> Could you unify the code so that both cases are equivalent?<br>
> --Michael<br>
><br>
><br>
> ><br>
> > assert(intrin->dest.is_ssa);<br>
> > nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(block_index));<br>
> > @@ -129,16 +130,24 @@ lower_res_index_intrinsic(nir_intrinsic_instr *intrin,<br>
> ><br>
> > static void<br>
> > lower_tex_deref(nir_tex_instr *tex, nir_deref_var *deref,<br>
> > - unsigned *const_index, nir_tex_src_type src_type,<br>
> > + unsigned *const_index, unsigned array_size,<br>
> > + nir_tex_src_type src_type,<br>
> > struct apply_pipeline_layout_state *state)<br>
> > {<br>
> > + nir_builder *b = &state->builder;<br>
> > +<br>
> > if (deref->deref.child) {<br>
> > assert(deref->deref.child->deref_type == nir_deref_type_array);<br>
> > nir_deref_array *deref_array = nir_deref_as_array(deref->deref.child);<br>
> ><br>
> > - *const_index += deref_array->base_offset;<br>
> > -<br>
> > if (deref_array->deref_array_type == nir_deref_array_type_indirect) {<br>
> > + nir_ssa_def *index =<br>
> > + nir_iadd(b, nir_imm_int(b, deref_array->base_offset),<br>
> > + nir_ssa_for_src(b, deref_array->indirect, 1));<br>
> > +<br>
> > + if (state->add_bounds_checks)<br>
> > + index = nir_umax(b, index, nir_imm_int(b, array_size - 1));<br>
> > +<br>
> > nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,<br>
> > tex->num_srcs + 1);<br>
> ><br>
> > @@ -154,10 +163,11 @@ lower_tex_deref(nir_tex_instr *tex, nir_deref_var *deref,<br>
> > * first-class texture source.<br>
> > */<br>
> > tex->src[tex->num_srcs].src_type = src_type;<br>
> > + nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs],<br>
> > + nir_src_for_ssa(index));<br>
> > tex->num_srcs++;<br>
> > - assert(deref_array->indirect.is_ssa);<br>
> > - nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs - 1].src,<br>
> > - deref_array->indirect);<br>
> > + } else {<br>
> > + *const_index += MIN2(deref_array->base_offset, array_size - 1);<br>
> > }<br>
> > }<br>
> > }<br>
> > @@ -182,17 +192,23 @@ lower_tex(nir_tex_instr *tex, struct apply_pipeline_layout_state *state)<br>
> > /* No one should have come by and lowered it already */<br>
> > assert(tex->texture);<br>
> ><br>
> > + state->builder.cursor = nir_before_instr(&tex->instr);<br>
> > +<br>
> > unsigned set = tex->texture->var->data.descriptor_set;<br>
> > unsigned binding = tex->texture->var->data.binding;<br>
> > + unsigned array_size =<br>
> > + state->layout->set[set].layout->binding[binding].array_size;<br>
> > tex->texture_index = state->set[set].surface_offsets[binding];<br>
> > - lower_tex_deref(tex, tex->texture, &tex->texture_index,<br>
> > + lower_tex_deref(tex, tex->texture, &tex->texture_index, array_size,<br>
> > nir_tex_src_texture_offset, state);<br>
> ><br>
> > if (tex->sampler) {<br>
> > unsigned set = tex->sampler->var->data.descriptor_set;<br>
> > unsigned binding = tex->sampler->var->data.binding;<br>
> > + unsigned array_size =<br>
> > + state->layout->set[set].layout->binding[binding].array_size;<br>
> > tex->sampler_index = state->set[set].sampler_offsets[binding];<br>
> > - lower_tex_deref(tex, tex->sampler, &tex->sampler_index,<br>
> > + lower_tex_deref(tex, tex->sampler, &tex->sampler_index, array_size,<br>
> > nir_tex_src_sampler_offset, state);<br>
> > }<br>
> ><br>
> > @@ -254,6 +270,8 @@ anv_nir_apply_pipeline_layout(struct anv_pipeline *pipeline,<br>
> ><br>
> > struct apply_pipeline_layout_state state = {<br>
> > .shader = shader,<br>
> > + .layout = layout,<br>
> > + .add_bounds_checks = pipeline->device->robust_buffer_access,<br>
> > };<br>
> ><br>
> > void *mem_ctx = ralloc_context(NULL);<br>
> ><br>
</p>