nir/lower_wpos_center: support adding sample position to fragment coordinate
authorIago Toral Quiroga <itoral@igalia.com>
Thu, 23 Mar 2017 10:54:16 +0000 (11:54 +0100)
committerIago Toral Quiroga <itoral@igalia.com>
Fri, 24 Mar 2017 07:11:53 +0000 (08:11 +0100)
According to section 14.6 of the Vulkan specification:

   "When sample shading is enabled, the x and y components of FragCoord
    reflect the location of the sample corresponding to the shader
    invocation."

So add a boolean parameter to the lowering pass to select this behavior
when we need it.

Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_wpos_center.c
src/intel/vulkan/anv_pipeline.c

index d032787a2a068145c5a8a4f76dc142e38ec092de..f88707a31a07ff99ef1b09677a6f2a5ddce6502c 100644 (file)
@@ -2518,7 +2518,7 @@ typedef struct nir_lower_wpos_ytransform_options {
 
 bool nir_lower_wpos_ytransform(nir_shader *shader,
                                const nir_lower_wpos_ytransform_options *options);
-bool nir_lower_wpos_center(nir_shader *shader);
+bool nir_lower_wpos_center(nir_shader *shader, const bool for_sample_shading);
 
 typedef struct nir_lower_drawpixels_options {
    int texcoord_state_tokens[5];
index 5a70848eb88ec94b7947d0c9363151276c9b394e..478818d8d667ee06388a5859998dedda429e3718 100644 (file)
  * hardware which provides an integer pixel center.  Vulkan mandates that
  * the pixel center must be half-integer, and also that the coordinate
  * system's origin must be upper left.  This means that there's no need
- * for a uniform - we can always just add a constant.
+ * for a uniform - we can always just add a constant. In the case that
+ * sample shading is enabled, Vulkan expects FragCoord to include sample
+ * positions.
  */
 
 static void
-add_half_to_fragcoord(nir_builder *b, nir_intrinsic_instr *intr)
+update_fragcoord(nir_builder *b, nir_intrinsic_instr *intr,
+                 const bool for_sample_shading)
 {
    nir_ssa_def *wpos = &intr->dest.ssa;
 
@@ -51,14 +54,27 @@ add_half_to_fragcoord(nir_builder *b, nir_intrinsic_instr *intr)
 
    b->cursor = nir_after_instr(&intr->instr);
 
-   wpos = nir_fadd(b, wpos, nir_imm_vec4(b, 0.5f, 0.5f, 0.0f, 0.0f));
+   if (!for_sample_shading) {
+      wpos = nir_fadd(b, wpos, nir_imm_vec4(b, 0.5f, 0.5f, 0.0f, 0.0f));
+   } else {
+      nir_ssa_def *spos =
+         nir_load_system_value(b, nir_intrinsic_load_sample_pos, 0);
+
+      wpos = nir_fadd(b, wpos,
+                      nir_vec4(b,
+                               nir_channel(b, spos, 0),
+                               nir_channel(b, spos, 1),
+                               nir_imm_float(b, 0.0f),
+                               nir_imm_float(b, 0.0f)));
+   }
 
    nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, nir_src_for_ssa(wpos),
                                   wpos->parent_instr);
 }
 
 static bool
-lower_wpos_center_block(nir_builder *b, nir_block *block)
+lower_wpos_center_block(nir_builder *b, nir_block *block,
+                        const bool for_sample_shading)
 {
    bool progress = false;
 
@@ -73,7 +89,7 @@ lower_wpos_center_block(nir_builder *b, nir_block *block)
                 var->data.location == VARYING_SLOT_POS) {
                /* gl_FragCoord should not have array/struct derefs: */
                assert(dvar->deref.child == NULL);
-               add_half_to_fragcoord(b, intr);
+               update_fragcoord(b, intr, for_sample_shading);
                progress = true;
             }
          }
@@ -84,7 +100,7 @@ lower_wpos_center_block(nir_builder *b, nir_block *block)
 }
 
 bool
-nir_lower_wpos_center(nir_shader *shader)
+nir_lower_wpos_center(nir_shader *shader, const bool for_sample_shading)
 {
    bool progress = false;
    nir_builder b;
@@ -96,7 +112,8 @@ nir_lower_wpos_center(nir_shader *shader)
          nir_builder_init(&b, function->impl);
 
          nir_foreach_block(block, function->impl) {
-            progress = lower_wpos_center_block(&b, block) || progress;
+            progress = lower_wpos_center_block(&b, block, for_sample_shading) ||
+                       progress;
          }
          nir_metadata_preserve(function->impl, nir_metadata_block_index |
                                                nir_metadata_dominance);
index 23274ef2a875ff6f3c86563fd8bcefb158168fdd..8ad2d485360036aad944162812f7b76aecd881b4 100644 (file)
@@ -158,7 +158,7 @@ anv_shader_compile_to_nir(struct anv_device *device,
               nir_var_shader_in | nir_var_shader_out | nir_var_system_value);
 
    if (stage == MESA_SHADER_FRAGMENT)
-      NIR_PASS_V(nir, nir_lower_wpos_center);
+      NIR_PASS_V(nir, nir_lower_wpos_center, false);
 
    /* Now that we've deleted all but the main function, we can go ahead and
     * lower the rest of the constant initializers.