xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_patch_vertices.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2016 Intel Corporation
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_builder.h"
25 
26 static nir_variable *
make_uniform(nir_shader * nir,const gl_state_index16 * tokens)27 make_uniform(nir_shader *nir, const gl_state_index16 *tokens)
28 {
29    /* Note: name must be prefixed with "gl_" to trigger slot based
30     * special handling in uniform setup.
31     */
32    nir_variable *var =
33       nir_state_variable_create(nir, glsl_int_type(),
34                                 "gl_PatchVerticesIn", tokens);
35 
36    return var;
37 }
38 
39 /**
40  * This pass lowers the load_patch_vertices_in intrinsic.
41  *
42  * - If we statically know the value, we lower it to a constant.
43  *   (If a TES is linked against a TCS, the TCS tells us the TES input count.)
44  *
45  * - If not, and we're given Mesa state slots, we lower it to a uniform.
46  *
47  * - Otherwise, we leave it as a system value.
48  *
49  * This pass must be run after nir_lower_system_values().
50  */
51 bool
nir_lower_patch_vertices(nir_shader * nir,unsigned static_count,const gl_state_index16 * uniform_state_tokens)52 nir_lower_patch_vertices(nir_shader *nir,
53                          unsigned static_count,
54                          const gl_state_index16 *uniform_state_tokens)
55 {
56    bool progress = false;
57    nir_variable *var = NULL;
58 
59    /* If there's no static count and we don't want uniforms, there's no
60     * lowering to do...just bail early.
61     */
62    if (static_count == 0 && !uniform_state_tokens)
63       return false;
64 
65    nir_foreach_function_impl(impl, nir) {
66       nir_foreach_block(block, impl) {
67          nir_builder b = nir_builder_create(impl);
68          nir_foreach_instr_safe(instr, block) {
69             if (instr->type == nir_instr_type_intrinsic) {
70                nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
71                if (intr->intrinsic != nir_intrinsic_load_patch_vertices_in)
72                   continue;
73 
74                b.cursor = nir_before_instr(&intr->instr);
75 
76                nir_def *val = NULL;
77                if (static_count) {
78                   val = nir_imm_int(&b, static_count);
79                } else {
80                   if (!var)
81                      var = make_uniform(nir, uniform_state_tokens);
82 
83                   val = nir_load_var(&b, var);
84                }
85 
86                progress = true;
87                nir_def_replace(&intr->def, val);
88             }
89          }
90       }
91 
92       if (progress) {
93          nir_metadata_preserve(impl, nir_metadata_control_flow);
94       }
95    }
96 
97    return progress;
98 }
99