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. This pass must run after lower_io_to_temporaries so
38 * that the transformed position is not visible to user code.
39 */
40
41 #include "nir/nir.h"
42 #include "nir/nir_builder.h"
43
44 /* Vulkan spec requires float accuracy to ~10^-5, so we need to differentiate
45 * between W=0 and W=10^-5. Clamping 1/W to 2^15 gives a bit of margin on top
46 * of this. */
47 #define MAX_W_RECIP_ABS (float)(1l << 15)
48
49 static bool
lower_viewport_transform_instr(nir_builder * b,nir_intrinsic_instr * intr,void * data)50 lower_viewport_transform_instr(nir_builder *b, nir_intrinsic_instr *intr,
51 void *data)
52 {
53 if (intr->intrinsic != nir_intrinsic_store_deref)
54 return false;
55
56 nir_variable *var = nir_intrinsic_get_var(intr, 0);
57 if (var->data.mode != nir_var_shader_out ||
58 var->data.location != VARYING_SLOT_POS)
59 return false;
60
61 b->cursor = nir_before_instr(&intr->instr);
62
63 /* Grab the source and viewport */
64 nir_def *input_point = intr->src[1].ssa;
65 nir_def *scale = nir_load_viewport_scale(b);
66 nir_def *offset = nir_load_viewport_offset(b);
67
68 /* World space to normalised device coordinates to screen space */
69
70 nir_def *w_recip = nir_frcp(b, nir_channel(b, input_point, 3));
71
72 /* Clamp w_recip so that small w does not result in Inf or unacceptable
73 * precision loss relative to clipping pre-transform. */
74 w_recip = nir_fclamp(b, w_recip, nir_imm_float(b, -MAX_W_RECIP_ABS),
75 nir_imm_float(b, MAX_W_RECIP_ABS));
76
77 nir_def *ndc_point = nir_fmul(b, nir_trim_vector(b, input_point, 3),
78 w_recip);
79
80 nir_def *screen = nir_fadd(b, nir_fmul(b, ndc_point, scale),
81 offset);
82
83 /* gl_Position will be written out in screenspace xyz, with w set to
84 * the reciprocal we computed earlier. The transformed w component is
85 * then used for perspective-correct varying interpolation. The
86 * transformed w component must preserve its original sign; this is
87 * used in depth clipping computations
88 */
89
90 nir_def *screen_space = nir_vec4(b,
91 nir_channel(b, screen, 0),
92 nir_channel(b, screen, 1),
93 nir_channel(b, screen, 2),
94 w_recip);
95
96 nir_src_rewrite(&intr->src[1], screen_space);
97 return true;
98 }
99
100 bool
nir_lower_viewport_transform(nir_shader * shader)101 nir_lower_viewport_transform(nir_shader *shader)
102 {
103 assert((shader->info.stage == MESA_SHADER_VERTEX) || (shader->info.stage == MESA_SHADER_GEOMETRY) || (shader->info.stage == MESA_SHADER_TESS_EVAL));
104
105 return nir_shader_intrinsics_pass(shader, lower_viewport_transform_instr,
106 nir_metadata_control_flow,
107 NULL);
108 }
109