xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_wpos_center.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2015 Red Hat
3  * Copyright © 2016 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "nir.h"
26 #include "nir_builder.h"
27 
28 /**
29  * This pass adds sample position to gl_FragCoord, intended for Vulkan drivers
30  * on hardware which provides an integer pixel center. Vulkan mandates that the
31  * pixel center must be half-integer, and also that the coordinate system's
32  * origin must be upper left. This means that there's no need for a uniform - we
33  * can always just add a constant. In the case that sample shading is enabled,
34  * Vulkan expects FragCoord to include sample positions.
35  *
36  * Run before nir_lower_io().
37  *
38  * For a more full featured pass, consider using nir_lower_wpos_ytransform(),
39  * which can handle pixel center integer / half integer, and origin lower
40  * left / upper left transformations.
41  */
42 
43 static bool
lower_wpos_center_instr(nir_builder * b,nir_intrinsic_instr * intr,void * data)44 lower_wpos_center_instr(nir_builder *b, nir_intrinsic_instr *intr, void *data)
45 {
46    if (intr->intrinsic != nir_intrinsic_load_frag_coord)
47       return false;
48 
49    nir_def *wpos = &intr->def;
50    b->cursor = nir_after_instr(&intr->instr);
51 
52    nir_def *spos = nir_load_sample_pos_or_center(b);
53    wpos = nir_fadd(b, wpos, nir_pad_vector_imm_int(b, spos, 0, 4));
54 
55    nir_def_rewrite_uses_after(&intr->def, wpos, wpos->parent_instr);
56    return true;
57 }
58 
59 bool
nir_lower_wpos_center(nir_shader * shader)60 nir_lower_wpos_center(nir_shader *shader)
61 {
62    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
63 
64    return nir_shader_intrinsics_pass(shader, lower_wpos_center_instr,
65                                      nir_metadata_control_flow,
66                                      NULL);
67 }
68