xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_viewport_transform.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 static bool
lower_viewport_transform_instr(nir_builder * b,nir_intrinsic_instr * intr,void * data)44 lower_viewport_transform_instr(nir_builder *b, nir_intrinsic_instr *intr,
45                                void *data)
46 {
47    if (intr->intrinsic != nir_intrinsic_store_deref)
48       return false;
49 
50    nir_variable *var = nir_intrinsic_get_var(intr, 0);
51    if (var->data.mode != nir_var_shader_out ||
52        var->data.location != VARYING_SLOT_POS)
53       return false;
54 
55    b->cursor = nir_before_instr(&intr->instr);
56 
57    /* Grab the source and viewport */
58    nir_def *input_point = intr->src[1].ssa;
59    nir_def *scale = nir_load_viewport_scale(b);
60    nir_def *offset = nir_load_viewport_offset(b);
61 
62    /* World space to normalised device coordinates to screen space */
63 
64    nir_def *w_recip = nir_frcp(b, nir_channel(b, input_point, 3));
65 
66    nir_def *ndc_point = nir_fmul(b, nir_trim_vector(b, input_point, 3),
67                                  w_recip);
68 
69    nir_def *screen = nir_fadd(b, nir_fmul(b, ndc_point, scale),
70                               offset);
71 
72    /* gl_Position will be written out in screenspace xyz, with w set to
73     * the reciprocal we computed earlier. The transformed w component is
74     * then used for perspective-correct varying interpolation. The
75     * transformed w component must preserve its original sign; this is
76     * used in depth clipping computations
77     */
78 
79    nir_def *screen_space = nir_vec4(b,
80                                     nir_channel(b, screen, 0),
81                                     nir_channel(b, screen, 1),
82                                     nir_channel(b, screen, 2),
83                                     w_recip);
84 
85    nir_src_rewrite(&intr->src[1], screen_space);
86    return true;
87 }
88 
89 bool
nir_lower_viewport_transform(nir_shader * shader)90 nir_lower_viewport_transform(nir_shader *shader)
91 {
92    assert((shader->info.stage == MESA_SHADER_VERTEX) || (shader->info.stage == MESA_SHADER_GEOMETRY) || (shader->info.stage == MESA_SHADER_TESS_EVAL));
93 
94    return nir_shader_intrinsics_pass(shader, lower_viewport_transform_instr,
95                                        nir_metadata_control_flow,
96                                        NULL);
97 }
98