1 /*
2 * Copyright 2023 Valve Corpoation
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "nir.h"
7 #include "nir_builder.h"
8 #include "nir_builder_opcodes.h"
9
10 static bool
lower(nir_builder * b,nir_intrinsic_instr * intr,UNUSED void * data)11 lower(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data)
12 {
13 if (intr->intrinsic != nir_intrinsic_load_frag_coord)
14 return false;
15
16 /* load_pixel_coord gives the top-left corner of the pixel, but frag_coord
17 * should return the centre of the pixel.
18 */
19 b->cursor = nir_before_instr(&intr->instr);
20 nir_def *top_left_xy = nir_u2f32(b, nir_load_pixel_coord(b));
21 nir_def *xy = nir_fadd_imm(b, top_left_xy, 0.5);
22
23 nir_def *vec = nir_vec4(b, nir_channel(b, xy, 0), nir_channel(b, xy, 1),
24 nir_load_frag_coord_zw(b, .component = 2),
25 nir_load_frag_coord_zw(b, .component = 3));
26 nir_def_rewrite_uses(&intr->def, vec);
27 return true;
28 }
29
30 bool
nir_lower_frag_coord_to_pixel_coord(nir_shader * shader)31 nir_lower_frag_coord_to_pixel_coord(nir_shader *shader)
32 {
33 return nir_shader_intrinsics_pass(shader, lower,
34 nir_metadata_block_index | nir_metadata_dominance,
35 NULL);
36 }
37