• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <0.5, 0.5> to all uses of gl_FragCoord.
30  *
31  * Run before nir_lower_io().
32  *
33  * For a more full featured pass, consider using nir_lower_wpos_ytransform(),
34  * which can handle pixel center integer / half integer, and origin lower
35  * left / upper left transformations.
36  *
37  * This simple pass is primarily intended for use by Vulkan drivers on
38  * hardware which provides an integer pixel center.  Vulkan mandates that
39  * the pixel center must be half-integer, and also that the coordinate
40  * system's origin must be upper left.  This means that there's no need
41  * for a uniform - we can always just add a constant. In the case that
42  * sample shading is enabled, Vulkan expects FragCoord to include sample
43  * positions.
44  */
45 
46 static void
update_fragcoord(nir_builder * b,nir_intrinsic_instr * intr)47 update_fragcoord(nir_builder *b, nir_intrinsic_instr *intr)
48 {
49    nir_def *wpos = &intr->def;
50 
51    b->cursor = nir_after_instr(&intr->instr);
52 
53    nir_def *spos = nir_load_sample_pos_or_center(b);
54 
55    wpos = nir_fadd(b, wpos,
56                    nir_vec4(b,
57                             nir_channel(b, spos, 0),
58                             nir_channel(b, spos, 1),
59                             nir_imm_float(b, 0.0f),
60                             nir_imm_float(b, 0.0f)));
61 
62    nir_def_rewrite_uses_after(&intr->def, wpos,
63                               wpos->parent_instr);
64 }
65 
66 static bool
lower_wpos_center_instr(nir_builder * b,nir_intrinsic_instr * intr,void * data)67 lower_wpos_center_instr(nir_builder *b, nir_intrinsic_instr *intr, void *data)
68 {
69    if (intr->intrinsic != nir_intrinsic_load_frag_coord)
70       return false;
71 
72    update_fragcoord(b, intr);
73    return true;
74 }
75 
76 bool
nir_lower_wpos_center(nir_shader * shader)77 nir_lower_wpos_center(nir_shader *shader)
78 {
79    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
80 
81    return nir_shader_intrinsics_pass(shader, lower_wpos_center_instr,
82                                        nir_metadata_block_index |
83                                           nir_metadata_dominance,
84                                        NULL);
85 }
86