nir: Add lower_frag_coord_to_pixel_coord pass
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Fri, 23 Jun 2023 14:23:43 +0000 (10:23 -0400)
committerMarge Bot <emma+marge@anholt.net>
Tue, 27 Jun 2023 14:38:21 +0000 (14:38 +0000)
We've open coded this in a few backends.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23836>

src/compiler/nir/meson.build
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_frag_coord_to_pixel_coord.c [new file with mode: 0644]

index 3147892..87d46eb 100644 (file)
@@ -156,6 +156,7 @@ files_libnir = files(
   'nir_lower_flrp.c',
   'nir_lower_fp16_conv.c',
   'nir_lower_fragcoord_wtrans.c',
+  'nir_lower_frag_coord_to_pixel_coord.c',
   'nir_lower_fragcolor.c',
   'nir_lower_frexp.c',
   'nir_lower_global_vars_to_local.c',
index b1b52f4..fd113ad 100644 (file)
@@ -5252,6 +5252,7 @@ nir_shader * nir_create_passthrough_gs(const nir_shader_compiler_options *option
 
 bool nir_lower_fragcolor(nir_shader *shader, unsigned max_cbufs);
 bool nir_lower_fragcoord_wtrans(nir_shader *shader);
+bool nir_lower_frag_coord_to_pixel_coord(nir_shader *shader);
 bool nir_lower_viewport_transform(nir_shader *shader);
 bool nir_lower_uniforms_to_ubo(nir_shader *shader, bool dword_packed, bool load_vec4);
 
diff --git a/src/compiler/nir/nir_lower_frag_coord_to_pixel_coord.c b/src/compiler/nir/nir_lower_frag_coord_to_pixel_coord.c
new file mode 100644 (file)
index 0000000..20033f4
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2023 Valve Corpoation
+ * SPDX-License-Identifier: MIT
+ */
+
+#include "nir.h"
+#include "nir_builder.h"
+#include "nir_builder_opcodes.h"
+
+static bool
+lower(nir_builder *b, nir_instr *instr, UNUSED void *data)
+{
+   if (instr->type != nir_instr_type_intrinsic)
+      return false;
+
+   nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+   if (intr->intrinsic != nir_intrinsic_load_frag_coord)
+      return false;
+
+   /* load_pixel_coord gives the top-left corner of the pixel, but frag_coord
+    * should return the centre of the pixel.
+    */
+   b->cursor = nir_before_instr(instr);
+   nir_ssa_def *top_left_xy = nir_u2f32(b, nir_load_pixel_coord(b));
+   nir_ssa_def *xy = nir_fadd_imm(b, top_left_xy, 0.5);
+
+   nir_ssa_def *vec = nir_vec4(b, nir_channel(b, xy, 0), nir_channel(b, xy, 1),
+                               nir_load_frag_coord_zw(b, .component = 2),
+                               nir_load_frag_coord_zw(b, .component = 3));
+   nir_ssa_def_rewrite_uses(&intr->dest.ssa, vec);
+   return true;
+}
+
+bool
+nir_lower_frag_coord_to_pixel_coord(nir_shader *shader)
+{
+   return nir_shader_instructions_pass(
+      shader, lower, nir_metadata_block_index | nir_metadata_dominance, NULL);
+}