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 /* Note: frag_coord should already have pixel-center lowering applied with
17 * nir_lower_wpos_center for VK, or PIPE_CAP_PIXEL_CENTER_INTEGER for GL.
18 */
19 b->cursor = nir_before_instr(&intr->instr);
20 nir_def *xy = nir_u2f32(b, nir_load_pixel_coord(b));
21 nir_def *vec = nir_vec4(b, nir_channel(b, xy, 0), nir_channel(b, xy, 1),
22 nir_load_frag_coord_zw(b, .component = 2),
23 nir_load_frag_coord_zw(b, .component = 3));
24 nir_def_rewrite_uses(&intr->def, vec);
25 return true;
26 }
27
28 bool
nir_lower_frag_coord_to_pixel_coord(nir_shader * shader)29 nir_lower_frag_coord_to_pixel_coord(nir_shader *shader)
30 {
31 return nir_shader_intrinsics_pass(shader, lower,
32 nir_metadata_control_flow,
33 NULL);
34 }
35