<div dir="ltr">ping<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Apr 9, 2019 at 10:03 PM Marek Olšák <<a href="mailto:maraeo@gmail.com">maraeo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">From: Marek Olšák <<a href="mailto:marek.olsak@amd.com" target="_blank">marek.olsak@amd.com</a>><br>
<br>
---<br>
 src/compiler/nir/nir.h                    |  8 +++++<br>
 src/compiler/nir/nir_opt_intrinsics.c     | 40 +++++++++++++++++++++--<br>
 src/gallium/drivers/radeonsi/si_get.c     |  1 +<br>
 src/mesa/state_tracker/st_glsl_to_nir.cpp |  1 +<br>
 4 files changed, 48 insertions(+), 2 deletions(-)<br>
<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 09950bf3398..d3874705235 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2283,20 +2283,28 @@ typedef struct nir_shader_compiler_options {<br>
     *<br>
     * This depends on some possibly hw implementation details, which may<br>
     * not be true for all hw.  In particular that the FS is only executed<br>
     * for covered samples or for helper invocations.  So, do not blindly<br>
     * enable this option.<br>
     *<br>
     * Note: See also issue #22 in ARB_shader_image_load_store<br>
     */<br>
    bool lower_helper_invocation;<br>
<br>
+   /**<br>
+    * Convert gl_SampleMaskIn to gl_HelperInvocation as follows:<br>
+    *<br>
+    *   gl_SampleMaskIn == 0 ---> gl_HelperInvocation<br>
+    *   gl_SampleMaskIn != 0 ---> !gl_HelperInvocation<br>
+    */<br>
+   bool optimize_sample_mask_in;<br>
+<br>
    bool lower_cs_local_index_from_id;<br>
    bool lower_cs_local_id_from_index;<br>
<br>
    bool lower_device_index_to_zero;<br>
<br>
    /* Set if nir_lower_wpos_ytransform() should also invert gl_PointCoord. */<br>
    bool lower_wpos_pntc;<br>
<br>
    bool lower_hadd;<br>
    bool lower_add_sat;<br>
diff --git a/src/compiler/nir/nir_opt_intrinsics.c b/src/compiler/nir/nir_opt_intrinsics.c<br>
index 7b054faa204..2f9e4e466c9 100644<br>
--- a/src/compiler/nir/nir_opt_intrinsics.c<br>
+++ b/src/compiler/nir/nir_opt_intrinsics.c<br>
@@ -22,21 +22,22 @@<br>
  */<br>
<br>
 #include "nir.h"<br>
 #include "nir_builder.h"<br>
<br>
 /**<br>
  * \file nir_opt_intrinsics.c<br>
  */<br>
<br>
 static bool<br>
-opt_intrinsics_impl(nir_function_impl *impl)<br>
+opt_intrinsics_impl(nir_function_impl *impl,<br>
+                    const struct nir_shader_compiler_options *options)<br>
 {<br>
    nir_builder b;<br>
    nir_builder_init(&b, impl);<br>
    bool progress = false;<br>
<br>
    nir_foreach_block(block, impl) {<br>
       nir_foreach_instr_safe(instr, block) {<br>
          if (instr->type != nir_instr_type_intrinsic)<br>
             continue;<br>
<br>
@@ -48,20 +49,55 @@ opt_intrinsics_impl(nir_function_impl *impl)<br>
          case nir_intrinsic_vote_any:<br>
          case nir_intrinsic_vote_all:<br>
             if (nir_src_is_const(intrin->src[0]))<br>
                replacement = nir_ssa_for_src(&b, intrin->src[0], 1);<br>
             break;<br>
          case nir_intrinsic_vote_feq:<br>
          case nir_intrinsic_vote_ieq:<br>
             if (nir_src_is_const(intrin->src[0]))<br>
                replacement = nir_imm_true(&b);<br>
             break;<br>
+         case nir_intrinsic_load_sample_mask_in:<br>
+            /* Transform:<br>
+             *   gl_SampleMaskIn == 0 ---> gl_HelperInvocation<br>
+             *   gl_SampleMaskIn != 0 ---> !gl_HelperInvocation<br>
+             */<br>
+            if (!options->optimize_sample_mask_in)<br>
+               continue;<br>
+<br>
+            nir_foreach_use_safe(use_src, &intrin->dest.ssa) {<br>
+               if (use_src->parent_instr->type == nir_instr_type_alu) {<br>
+                  nir_alu_instr *alu = nir_instr_as_alu(use_src->parent_instr);<br>
+<br>
+                  if (alu->op == nir_op_ieq ||<br>
+                      alu->op == nir_op_ine) {<br>
+                     /* Check for 0 in either operand. */<br>
+                     nir_const_value *const_val =<br>
+                         nir_src_as_const_value(alu->src[0].src);<br>
+                     if (!const_val)<br>
+                        const_val = nir_src_as_const_value(alu->src[1].src);<br>
+                     if (!const_val || const_val->i32[0] != 0)<br>
+                        continue;<br>
+<br>
+                     nir_ssa_def *new_expr = nir_load_helper_invocation(&b, 1);<br>
+<br>
+                     if (alu->op == nir_op_ine32)<br>
+                        new_expr = nir_inot(&b, new_expr);<br>
+<br>
+                     nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa,<br>
+                                              nir_src_for_ssa(new_expr));<br>
+                     nir_instr_remove(&alu->instr);<br>
+                     continue;<br>
+                  }<br>
+               }<br>
+            }<br>
+            continue;<br>
          default:<br>
             break;<br>
          }<br>
<br>
          if (!replacement)<br>
             continue;<br>
<br>
          nir_ssa_def_rewrite_uses(&intrin->dest.ssa,<br>
                                   nir_src_for_ssa(replacement));<br>
          nir_instr_remove(instr);<br>
@@ -74,19 +110,19 @@ opt_intrinsics_impl(nir_function_impl *impl)<br>
<br>
 bool<br>
 nir_opt_intrinsics(nir_shader *shader)<br>
 {<br>
    bool progress = false;<br>
<br>
    nir_foreach_function(function, shader) {<br>
       if (!function->impl)<br>
          continue;<br>
<br>
-      if (opt_intrinsics_impl(function->impl)) {<br>
+      if (opt_intrinsics_impl(function->impl, shader->options)) {<br>
          progress = true;<br>
          nir_metadata_preserve(function->impl, nir_metadata_block_index |<br>
                                                nir_metadata_dominance);<br>
       }<br>
    }<br>
<br>
    return progress;<br>
 }<br>
diff --git a/src/gallium/drivers/radeonsi/si_get.c b/src/gallium/drivers/radeonsi/si_get.c<br>
index 58b56b34d13..2142d5a33f2 100644<br>
--- a/src/gallium/drivers/radeonsi/si_get.c<br>
+++ b/src/gallium/drivers/radeonsi/si_get.c<br>
@@ -494,20 +494,21 @@ static const struct nir_shader_compiler_options nir_options = {<br>
        .lower_pack_snorm_2x16 = true,<br>
        .lower_pack_snorm_4x8 = true,<br>
        .lower_pack_unorm_2x16 = true,<br>
        .lower_pack_unorm_4x8 = true,<br>
        .lower_unpack_snorm_2x16 = true,<br>
        .lower_unpack_snorm_4x8 = true,<br>
        .lower_unpack_unorm_2x16 = true,<br>
        .lower_unpack_unorm_4x8 = true,<br>
        .lower_extract_byte = true,<br>
        .lower_extract_word = true,<br>
+       .optimize_sample_mask_in = true,<br>
        .max_unroll_iterations = 32,<br>
        .native_integers = true,<br>
 };<br>
<br>
 static const void *<br>
 si_get_compiler_options(struct pipe_screen *screen,<br>
                        enum pipe_shader_ir ir,<br>
                        enum pipe_shader_type shader)<br>
 {<br>
        assert(ir == PIPE_SHADER_IR_NIR);<br>
diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp<br>
index fb10869c9f9..8028ccf7110 100644<br>
--- a/src/mesa/state_tracker/st_glsl_to_nir.cpp<br>
+++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp<br>
@@ -497,20 +497,21 @@ st_glsl_to_nir_post_opts(struct st_context *st, struct gl_program *prog,<br>
    /* This has to be done last.  Any operation the can cause<br>
     * prog->ParameterValues to get reallocated (e.g., anything that adds a<br>
     * program constant) has to happen before creating this linkage.<br>
     */<br>
    _mesa_associate_uniform_storage(st->ctx, shader_program, prog, true);<br>
<br>
    st_set_prog_affected_state_flags(prog);<br>
<br>
    NIR_PASS_V(nir, st_nir_lower_builtin);<br>
    NIR_PASS_V(nir, gl_nir_lower_atomics, shader_program, true);<br>
+   NIR_PASS_V(nir, nir_opt_intrinsics);<br>
<br>
    nir_variable_mode mask = nir_var_function_temp;<br>
    nir_remove_dead_variables(nir, mask);<br>
<br>
    if (st->ctx->_Shader->Flags & GLSL_DUMP) {<br>
       _mesa_log("\n");<br>
       _mesa_log("NIR IR for linked %s program %d:\n",<br>
              _mesa_shader_stage_to_string(prog->info.stage),<br>
              shader_program->Name);<br>
       nir_print_shader(nir, _mesa_get_log_file());<br>
-- <br>
2.17.1<br>
<br>
</blockquote></div>