[Mesa-dev] [PATCH 5/6] nir/lower_tex: add support to clamp texture coords
Rob Clark
robdclark at gmail.com
Thu Sep 17 05:48:51 PDT 2015
From: Rob Clark <robclark at freedesktop.org>
Some hardware needs to clamp texture coordinates to [0.0, 1.0] in the
shader to emulate GL_CLAMP. This is added to lower_tex_proj since, in
the case of projected coords, the clamping needs to happen *after*
projection.
Signed-off-by: Rob Clark <robclark at freedesktop.org>
---
src/glsl/nir/nir.h | 23 ++++++++++++
src/glsl/nir/nir_lower_tex.c | 84 ++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 105 insertions(+), 2 deletions(-)
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index a40dbae..5690dc1 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1844,6 +1844,29 @@ typedef struct nir_lower_tex_options {
* texture dims to normalize.
*/
bool lower_rect;
+
+ /**
+ * To emulate certain texture wrap modes, this can be used
+ * to saturate the specified tex coord to [0.0, 1.0]. The
+ * bits are according to sampler #, ie. if, for example:
+ *
+ * (conf->saturate_s & (1 << n))
+ *
+ * is true, then the s coord for sampler n is saturated.
+ *
+ * Note that clamping must happen *after* projector lowering
+ * so any projected texture sample instruction with a clamped
+ * coordinate gets automatically lowered, regardless of the
+ * 'lower_txp' setting.
+ *
+ * Note that clamping must happen *after* normalizing texture
+ * coords, so any RECT texture sample instructions with a
+ * clamped coordinate get automatically lowered, regardless
+ * of the 'lower_rect' setting.
+ */
+ unsigned saturate_s;
+ unsigned saturate_t;
+ unsigned saturate_r;
} nir_lower_tex_options;
void nir_lower_tex(nir_shader *shader,
diff --git a/src/glsl/nir/nir_lower_tex.c b/src/glsl/nir/nir_lower_tex.c
index 002c82d..4a10c7c 100644
--- a/src/glsl/nir/nir_lower_tex.c
+++ b/src/glsl/nir/nir_lower_tex.c
@@ -29,6 +29,11 @@
* asking the texture operation to do so.
* + lowering RECT: converts the un-normalized RECT texture coordinates
* to normalized coordinates with txs plus ALU instructions
+ * + saturate s/t/r coords: to emulate certain texture clamp/wrap modes,
+ * inserts instructions to clamp specified coordinates to [0.0, 1.0].
+ * Note that this automatically triggers the previous two steps if
+ * needed, since clamping must happen after projector and rect
+ * lowering.
*/
#include "nir.h"
@@ -168,6 +173,62 @@ lower_rect(nir_builder *b, nir_tex_instr *tex)
}
}
+static void
+saturate_src(nir_builder *b, nir_tex_instr *tex, unsigned sat_mask)
+{
+ b->cursor = nir_before_instr(&tex->instr);
+
+ /* Walk through the sources saturating the requested arguments. */
+ for (unsigned i = 0; i < tex->num_srcs; i++) {
+ switch (tex->src[i].src_type) {
+ case nir_tex_src_coord:
+ break;
+ default:
+ continue;
+ }
+ nir_ssa_def *src =
+ nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
+
+ /* split src into components: */
+ nir_ssa_def *comp[4];
+
+ for (unsigned j = 0; j < tex->coord_components; j++)
+ comp[j] = nir_channel(b, src, j);
+
+ /* clamp requested components, array index does not get clamped: */
+ unsigned ncomp = tex->coord_components;
+ if (tex->is_array)
+ ncomp--;
+
+ for (unsigned j = 0; j < ncomp; j++)
+ if ((1 << j) & sat_mask)
+ comp[j] = nir_fsat(b, comp[j]);
+
+ /* and move the result back into a single vecN: */
+ switch (tex->coord_components) {
+ case 4:
+ src = nir_vec4(b, comp[0], comp[1], comp[2], comp[3]);
+ break;
+ case 3:
+ src = nir_vec3(b, comp[0], comp[1], comp[2]);
+ break;
+ case 2:
+ src = nir_vec2(b, comp[0], comp[1]);
+ break;
+ case 1:
+ src = comp[0];
+ break;
+ default:
+ unreachable("bad texture coord count");
+ break;
+ }
+
+ nir_instr_rewrite_src(&tex->instr,
+ &tex->src[i].src,
+ nir_src_for_ssa(src));
+ }
+}
+
static bool
nir_lower_tex_block(nir_block *block, void *void_state)
{
@@ -181,12 +242,31 @@ nir_lower_tex_block(nir_block *block, void *void_state)
nir_tex_instr *tex = nir_instr_as_tex(instr);
bool lower_txp = !!(state->options->lower_txp & (1 << tex->sampler_dim));
- if (lower_txp)
+ /* mask of src coords to saturate (clamp): */
+ unsigned sat_mask = 0;
+
+ if ((1 << tex->sampler_index) & state->options->saturate_r)
+ sat_mask |= (1 << 2); /* .z */
+ if ((1 << tex->sampler_index) & state->options->saturate_t)
+ sat_mask |= (1 << 1); /* .y */
+ if ((1 << tex->sampler_index) & state->options->saturate_s)
+ sat_mask |= (1 << 0); /* .x */
+
+ /* If we are clamping any coords, we must lower projector first
+ * as clamping happens *after* projection:
+ */
+ if (lower_txp || sat_mask)
project_src(b, tex);
+ /* If we are clamping any coords, we must lower texture RECT to
+ * 2D first, as clamping happens with normalized coordinates:
+ */
if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) &&
- state->options->lower_rect)
+ (state->options->lower_rect || sat_mask))
lower_rect(b, tex);
+
+ if (sat_mask)
+ saturate_src(b, tex, sat_mask);
}
return true;
--
2.4.3
More information about the mesa-dev
mailing list