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 || (shader->info.stage == MESA_SHADER_GEOMETRY)
48 || (shader->info.stage == MESA_SHADER_TESS_EVAL));
49
50 nir_foreach_function(func, shader) {
51 nir_foreach_block(block, func->impl) {
52 nir_foreach_instr_safe(instr, block) {
53 if (instr->type != nir_instr_type_intrinsic)
54 continue;
55
56 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
57 if (intr->intrinsic != nir_intrinsic_store_deref)
58 continue;
59
60 nir_variable *var = nir_intrinsic_get_var(intr, 0);
61 if (var->data.mode != nir_var_shader_out ||
62 var->data.location != VARYING_SLOT_POS)
63 continue;
64
65 nir_builder b;
66 nir_builder_init(&b, func->impl);
67 b.cursor = nir_before_instr(instr);
68
69 /* Grab the source and viewport */
70 nir_ssa_def *input_point = nir_ssa_for_src(&b, intr->src[1], 4);
71 nir_ssa_def *scale = nir_load_viewport_scale(&b);
72 nir_ssa_def *offset = nir_load_viewport_offset(&b);
73
74 /* World space to normalised device coordinates to screen space */
75
76 nir_ssa_def *w_recip = nir_frcp(&b, nir_channel(&b, input_point, 3));
77
78 nir_ssa_def *ndc_point = nir_fmul(&b,
79 nir_channels(&b, input_point, 0x7), w_recip);
80
81 nir_ssa_def *screen = nir_fadd(&b,
82 nir_fmul(&b, ndc_point, scale), offset);
83
84 /* gl_Position will be written out in screenspace xyz, with w set to
85 * the reciprocal we computed earlier. The transformed w component is
86 * then used for perspective-correct varying interpolation. The
87 * transformed w component must preserve its original sign; this is
88 * used in depth clipping computations
89 */
90
91 nir_ssa_def *screen_space = nir_vec4(&b,
92 nir_channel(&b, screen, 0),
93 nir_channel(&b, screen, 1),
94 nir_channel(&b, screen, 2),
95 w_recip);
96
97 nir_instr_rewrite_src(instr, &intr->src[1],
98 nir_src_for_ssa(screen_space));
99 }
100 }
101
102 nir_metadata_preserve(func->impl, nir_metadata_block_index |
103 nir_metadata_dominance);
104 }
105 }
106