1 /*
2  * Copyright (C) 2019 Alyssa Rosenzweig
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /* On some hardware (particularly, all current versions of Mali GPUs),
25  * vertex shaders do not output gl_Position in world-space. Instead, they
26  * output gl_Position in transformed screen space via the "pseudo"
27  * position varying. Thus, this pass finds writes to gl_Position and
28  * changes them to transformed writes, still to gl_Position. The
29  * outputted screen space is still written back to VARYING_SLOT_POS,
30  * which is semantically ambiguous but nevertheless a good match for
31  * Gallium/NIR/Mali.
32  *
33  * Implements coordinate transformation as defined in section 12.5
34  * "Coordinate Transformation" of the OpenGL ES 3.2 full specification.
35  *
36  * This pass must run before lower_vars/lower_io such that derefs are
37  * still in place.
38  */
39 
40 #include "nir/nir.h"
41 #include "nir/nir_builder.h"
42 
43 void
nir_lower_viewport_transform(nir_shader * shader)44 nir_lower_viewport_transform(nir_shader *shader)
45 {
46    assert(shader->info.stage == MESA_SHADER_VERTEX);
47 
48    nir_foreach_function(func, shader) {
49       nir_foreach_block(block, func->impl) {
50          nir_foreach_instr_safe(instr, block) {
51             if (instr->type != nir_instr_type_intrinsic)
52                continue;
53 
54             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
55             if (intr->intrinsic != nir_intrinsic_store_deref)
56                continue;
57 
58             nir_variable *var = nir_intrinsic_get_var(intr, 0);
59             if (var->data.mode != nir_var_shader_out ||
60                 var->data.location != VARYING_SLOT_POS)
61                continue;
62 
63             nir_builder b;
64             nir_builder_init(&b, func->impl);
65             b.cursor = nir_before_instr(instr);
66 
67             /* Grab the source and viewport */
68             nir_ssa_def *input_point = nir_ssa_for_src(&b, intr->src[1], 4);
69             nir_ssa_def *scale = nir_load_viewport_scale(&b);
70             nir_ssa_def *offset = nir_load_viewport_offset(&b);
71 
72             /* World space to normalised device coordinates to screen space */
73 
74             nir_ssa_def *w_recip = nir_frcp(&b, nir_channel(&b, input_point, 3));
75 
76             nir_ssa_def *ndc_point = nir_fmul(&b,
77                   nir_channels(&b, input_point, 0x7), w_recip);
78 
79             nir_ssa_def *screen = nir_fadd(&b,
80                   nir_fmul(&b, ndc_point, scale), offset);
81 
82             /* gl_Position will be written out in screenspace xyz, with w set to
83              * the reciprocal we computed earlier. The transformed w component is
84              * then used for perspective-correct varying interpolation. The
85              * transformed w component must preserve its original sign; this is
86              * used in depth clipping computations
87              */
88 
89             nir_ssa_def *screen_space = nir_vec4(&b,
90                              nir_channel(&b, screen, 0),
91                              nir_channel(&b, screen, 1),
92                              nir_channel(&b, screen, 2),
93                              w_recip);
94 
95             nir_instr_rewrite_src(instr, &intr->src[1],
96                                   nir_src_for_ssa(screen_space));
97          }
98       }
99 
100       nir_metadata_preserve(func->impl, nir_metadata_block_index |
101                             nir_metadata_dominance);
102    }
103 }
104