<p dir="ltr">Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>></p>
<div class="gmail_quote">On Apr 19, 2016 1:14 PM, "Rob Clark" <<a href="mailto:robdclark@gmail.com">robdclark@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
<br>
Signed-off-by: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
---<br>
src/compiler/nir/nir.h | 7 ++++++<br>
src/compiler/nir/nir_lower_tex.c | 46 ++++++++++++++++++++++++++++++++++++++++<br>
2 files changed, 53 insertions(+)<br>
<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index bbbc208..3b82cfa 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2301,6 +2301,13 @@ typedef struct nir_lower_tex_options {<br>
* while 4 and 5 represent 0 and 1 respectively.<br>
*/<br>
uint8_t swizzles[32][4];<br>
+<br>
+ /**<br>
+ * Bitmap of textures that need srgb to linear conversion. If<br>
+ * (lower_srgb & (1 << texture_index)) then the rgb (xyz) components<br>
+ * of the texture are lowered to linear.<br>
+ */<br>
+ unsigned lower_srgb;<br>
} nir_lower_tex_options;<br>
<br>
bool nir_lower_tex(nir_shader *shader,<br>
diff --git a/src/compiler/nir/nir_lower_tex.c b/src/compiler/nir/nir_lower_tex.c<br>
index 7740e58..4cc1822 100644<br>
--- a/src/compiler/nir/nir_lower_tex.c<br>
+++ b/src/compiler/nir/nir_lower_tex.c<br>
@@ -275,6 +275,45 @@ swizzle_result(nir_builder *b, nir_tex_instr *tex, const uint8_t swizzle[4])<br>
swizzled->parent_instr);<br>
}<br>
<br>
+static void<br>
+linearize_srgb_result(nir_builder *b, nir_tex_instr *tex)<br>
+{<br>
+ assert(tex->dest.is_ssa);<br>
+ assert(nir_tex_instr_dest_size(tex) == 4);<br>
+ assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);<br>
+<br>
+ b->cursor = nir_after_instr(&tex->instr);<br>
+<br>
+ static const unsigned swiz[4] = {0, 1, 2, 0};<br>
+ nir_ssa_def *comp = nir_swizzle(b, &tex->dest.ssa, swiz, 3, true);<br>
+<br>
+ /* Formula is:<br>
+ * (comp <= 0.04045) ?<br>
+ * (comp / 12.92) :<br>
+ * pow((comp + 0.055) / 1.055, 2.4)<br>
+ */<br>
+ nir_ssa_def *low = nir_fmul(b, comp, nir_imm_float(b, 1.0 / 12.92));<br>
+ nir_ssa_def *high = nir_fpow(b,<br>
+ nir_fmul(b,<br>
+ nir_fadd(b,<br>
+ comp,<br>
+ nir_imm_float(b, 0.055)),<br>
+ nir_imm_float(b, 1.0 / 1.055)),<br>
+ nir_imm_float(b, 2.4));<br>
+ nir_ssa_def *cond = nir_fge(b, nir_imm_float(b, 0.04045), comp);<br>
+ nir_ssa_def *rgb = nir_bcsel(b, cond, low, high);<br>
+<br>
+ /* alpha is untouched: */<br>
+ nir_ssa_def *result = nir_vec4(b,<br>
+ nir_channel(b, rgb, 0),<br>
+ nir_channel(b, rgb, 1),<br>
+ nir_channel(b, rgb, 2),<br>
+ nir_channel(b, &tex->dest.ssa, 3));<br>
+<br>
+ nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(result),<br>
+ result->parent_instr);<br>
+}<br>
+<br>
static bool<br>
nir_lower_tex_block(nir_block *block, void *void_state)<br>
{<br>
@@ -323,6 +362,13 @@ nir_lower_tex_block(nir_block *block, void *void_state)<br>
swizzle_result(b, tex, options->swizzles[tex->texture_index]);<br>
state->progress = true;<br>
}<br>
+<br>
+ /* should be after swizzle so we know which channels are rgb: */<br>
+ if (((1 << tex->texture_index) & options->lower_srgb) &&<br>
+ !nir_tex_instr_is_query(tex) && !tex->is_shadow) {<br>
+ linearize_srgb_result(b, tex);<br>
+ state->progress = true;<br>
+ }<br>
}<br>
<br>
return true;<br>
--<br>
2.5.5<br>
<br>
</blockquote></div>