xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_sysvals_to_varyings.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2021 Collabora Ltd.
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 #include "nir.h"
25 #include "nir_builder.h"
26 
27 /*
28  * spirv_to_nir() creates system values for some builtin inputs, but
29  * backends might want to have those inputs exposed as varyings. This
30  * lowering pass allows backends to convert system values to input
31  * varyings and should be called just after spirv_to_nir() when needed.
32  */
33 
34 bool
nir_lower_sysvals_to_varyings(nir_shader * shader,const struct nir_lower_sysvals_to_varyings_options * options)35 nir_lower_sysvals_to_varyings(nir_shader *shader,
36                               const struct nir_lower_sysvals_to_varyings_options *options)
37 {
38    bool progress = false;
39 
40    nir_foreach_variable_with_modes(var, shader, nir_var_system_value) {
41       switch (var->data.location) {
42 #define SYSVAL_TO_VARYING(opt, sysval, varying)       \
43    case SYSTEM_VALUE_##sysval:                        \
44       if (options->opt) {                             \
45          var->data.mode = nir_var_shader_in;          \
46          var->data.location = VARYING_SLOT_##varying; \
47          progress = true;                             \
48       }                                               \
49       break
50 
51          SYSVAL_TO_VARYING(frag_coord, FRAG_COORD, POS);
52          SYSVAL_TO_VARYING(point_coord, POINT_COORD, PNTC);
53          SYSVAL_TO_VARYING(front_face, FRONT_FACE, FACE);
54 
55 #undef SYSVAL_TO_VARYING
56 
57       default:
58          break;
59       }
60    }
61 
62    if (progress)
63       nir_fixup_deref_modes(shader);
64 
65    /* Nothing this does actually changes anything tracked by metadata.
66     * If we ever made this pass more complicated, we might need to care
67     * more about metadata.
68     */
69    nir_shader_preserve_all_metadata(shader);
70 
71    return progress;
72 }
73