<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jul 22, 2016 at 1:11 AM, Pohjolainen, Topi <span dir="ltr"><<a href="mailto:topi.pohjolainen@intel.com" target="_blank">topi.pohjolainen@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>On Thu, Jul 21, 2016 at 09:21:53PM -0700, Jason Ekstrand wrote:<br>
> On i965, we can't support coordinate offsets for texelFetch or rectangle<br>
> textures. Previously, we were doing this with a GLSL pass but we need to<br>
> do it in NIR if we want those workarounds for SPIR-V.<br>
><br>
> Signed-off-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>><br>
> Cc: "12.0" <<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a>><br>
> ---<br>
> src/compiler/nir/nir.h | 10 ++++++++<br>
> src/compiler/nir/nir_lower_tex.c | 54 ++++++++++++++++++++++++++++++++++++++++<br>
> 2 files changed, 64 insertions(+)<br>
><br>
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
> index d0f52b0..45f758c 100644<br>
> --- a/src/compiler/nir/nir.h<br>
> +++ b/src/compiler/nir/nir.h<br>
> @@ -2405,6 +2405,16 @@ typedef struct nir_lower_tex_options {<br>
> unsigned lower_txp;<br>
><br>
> /**<br>
> + * If true, lower away nir_tex_src_offset for all texelfetch instructions.<br>
> + */<br>
> + bool lower_txf_offset;<br>
> +<br>
> + /**<br>
> + * If true, lower away nir_tex_src_offset for all rect textures.<br>
> + */<br>
> + bool lower_rect_offset;<br>
> +<br>
> + /**<br>
> * If true, lower rect textures to 2D, using txs to fetch the<br>
> * texture dimensions and dividing the texture coords by the<br>
> * texture dims to normalize.<br>
> diff --git a/src/compiler/nir/nir_lower_tex.c b/src/compiler/nir/nir_lower_tex.c<br>
> index 0cf1071..a1280e1 100644<br>
> --- a/src/compiler/nir/nir_lower_tex.c<br>
> +++ b/src/compiler/nir/nir_lower_tex.c<br>
> @@ -128,6 +128,54 @@ project_src(nir_builder *b, nir_tex_instr *tex)<br>
> tex_instr_remove_src(tex, proj_index);<br>
> }<br>
><br>
> +static bool<br>
> +lower_offset(nir_builder *b, nir_tex_instr *tex)<br>
> +{<br>
> + int offset_index = tex_instr_find_src(tex, nir_tex_src_offset);<br>
<br>
</div></div>Could be 'const'.<br>
<span><br>
> + if (offset_index < 0)<br>
> + return false;<br>
> +<br>
> + int coord_index = tex_instr_find_src(tex, nir_tex_src_coord);<br>
<br>
</span>Same here.<br>
<span><br>
> + assert(coord_index >= 0);<br>
> +<br>
> + assert(tex->src[offset_index].src.is_ssa);<br>
> + assert(tex->src[coord_index].src.is_ssa);<br>
> + nir_ssa_def *offset = tex->src[offset_index].src.ssa;<br>
> + nir_ssa_def *coord = tex->src[coord_index].src.ssa;<br>
<br>
</span>In principle, it looks these could be declared constants as well:<br>
<br>
const nir_ssa_def * const offset = tex->src[offset_index].src.ssa;<br>
const nir_ssa_def * const coord = tex->src[coord_index].src.ssa;<br>
<br>
But further digging tells me that they can be only:<br>
<br>
nir_ssa_def * const offset = tex->src[offset_index].src.ssa;<br>
nir_ssa_def * const coord = tex->src[coord_index].src.ssa;<br>
<br>
<br>
Quite a few of the helpers in nir declare their inputs as read-write even<br>
though they only read the contents...<br></blockquote><div><br></div><div>I have tried on multiple occasions to make NIR more const-safe. Unfortunately, every attempt only seems to lead to frustration. Some of this is due to limitations in C (which are fixed in C11 which we can't use) that make it very hard to make constructs that can handle either const or non-const. This, in turn, makes it very hard to constify things. There is also the fact that we use a lot of embedded linked lists which make a bunch of things that look like they should be const non-const. I've given up. It's better to not claim any sort of constness than to try and claim it and have it all be lies.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div><br>
> +<br>
> + b->cursor = nir_before_instr(&tex->instr);<br>
> +<br>
> + nir_ssa_def *offset_coord;<br>
> + if (nir_tex_instr_src_type(tex, coord_index) == nir_type_float) {<br>
> + assert(tex->sampler_dim == GLSL_SAMPLER_DIM_RECT);<br>
> + offset_coord = nir_fadd(b, coord, nir_i2f(b, offset));<br>
> + } else {<br>
> + offset_coord = nir_iadd(b, coord, offset);<br>
> + }<br>
> +<br>
> + if (tex->is_array) {<br>
> + /* The offset is not applied to the array index */<br>
> + if (tex->coord_components == 2) {<br>
> + offset_coord = nir_vec2(b, nir_channel(b, offset_coord, 0),<br>
> + nir_channel(b, coord, 1));<br>
> + } else if (tex->coord_components == 3) {<br>
> + offset_coord = nir_vec3(b, nir_channel(b, offset_coord, 0),<br>
> + nir_channel(b, offset_coord, 1),<br>
> + nir_channel(b, coord, 2));<br>
> + } else {<br>
> + unreachable("Invalid number of components");<br>
> + }<br>
> + }<br>
> +<br>
> + nir_instr_rewrite_src(&tex->instr, &tex->src[coord_index].src,<br>
> + nir_src_for_ssa(offset_coord));<br>
> +<br>
> + tex_instr_remove_src(tex, offset_index);<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +<br>
> static nir_ssa_def *<br>
> get_texture_size(nir_builder *b, nir_tex_instr *tex)<br>
> {<br>
> @@ -458,6 +506,12 @@ nir_lower_tex_block(nir_block *block, nir_builder *b,<br>
> progress = true;<br>
> }<br>
><br>
> + if ((tex->op == nir_texop_txf && options->lower_txf_offset) ||<br>
> + (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT &&<br>
> + options->lower_rect_offset)) {<br>
> + progress = lower_offset(b, tex) || progress;<br>
> + }<br>
> +<br>
> if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) && options->lower_rect) {<br>
> lower_rect(b, tex);<br>
> progress = true;<br>
> --<br>
> 2.5.0.400.gff86faf<br>
><br>
</div></div>> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div><br></div></div>