xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_nir_builtins.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2018 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 shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "st_nir.h"
24 #include "st_program.h"
25 
26 #include "compiler/nir/nir_builder.h"
27 #include "compiler/glsl/gl_nir.h"
28 #include "compiler/glsl/gl_nir_linker.h"
29 
30 void
st_nir_finish_builtin_nir(struct st_context * st,nir_shader * nir)31 st_nir_finish_builtin_nir(struct st_context *st, nir_shader *nir)
32 {
33    struct pipe_screen *screen = st->screen;
34    gl_shader_stage stage = nir->info.stage;
35 
36    MESA_TRACE_FUNC();
37 
38    nir->info.separate_shader = true;
39    if (stage == MESA_SHADER_FRAGMENT)
40       nir->info.fs.untyped_color_outputs = true;
41 
42    NIR_PASS(_, nir, nir_lower_global_vars_to_local);
43    NIR_PASS(_, nir, nir_split_var_copies);
44    NIR_PASS(_, nir, nir_lower_var_copies);
45    NIR_PASS(_, nir, nir_lower_system_values);
46 
47    struct nir_lower_compute_system_values_options cs_options = {
48       .has_base_global_invocation_id = false,
49       .has_base_workgroup_id = false,
50    };
51    NIR_PASS(_, nir, nir_lower_compute_system_values, &cs_options);
52 
53    if (nir->options->lower_to_scalar) {
54       nir_variable_mode mask =
55           (stage > MESA_SHADER_VERTEX ? nir_var_shader_in : 0) |
56           (stage < MESA_SHADER_FRAGMENT ? nir_var_shader_out : 0);
57 
58       NIR_PASS(_, nir, nir_lower_io_to_scalar_early, mask);
59    }
60 
61    if (st->lower_rect_tex) {
62       const struct nir_lower_tex_options opts = { .lower_rect = true, };
63       NIR_PASS(_, nir, nir_lower_tex, &opts);
64    }
65 
66    nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
67 
68    st_nir_assign_vs_in_locations(nir);
69    st_nir_assign_varying_locations(st, nir);
70 
71    st_nir_lower_samplers(screen, nir, NULL, NULL);
72    st_nir_lower_uniforms(st, nir);
73    if (!screen->get_param(screen, PIPE_CAP_NIR_IMAGES_AS_DEREF))
74       NIR_PASS(_, nir, gl_nir_lower_images, false);
75 
76    if (screen->finalize_nir) {
77       char *msg = screen->finalize_nir(screen, nir);
78       free(msg);
79    } else {
80       gl_nir_opts(nir);
81    }
82 }
83 
84 struct pipe_shader_state *
st_nir_finish_builtin_shader(struct st_context * st,nir_shader * nir)85 st_nir_finish_builtin_shader(struct st_context *st,
86                              nir_shader *nir)
87 {
88    st_nir_finish_builtin_nir(st, nir);
89 
90    struct pipe_shader_state state = {
91       .type = PIPE_SHADER_IR_NIR,
92       .ir.nir = nir,
93    };
94 
95    return st_create_nir_shader(st, &state);
96 }
97 
98 /**
99  * Make a simple shader that copies inputs to corresponding outputs.
100  */
101 struct pipe_shader_state *
st_nir_make_passthrough_shader(struct st_context * st,const char * shader_name,gl_shader_stage stage,unsigned num_vars,const unsigned * input_locations,const gl_varying_slot * output_locations,unsigned * interpolation_modes,unsigned sysval_mask)102 st_nir_make_passthrough_shader(struct st_context *st,
103                                const char *shader_name,
104                                gl_shader_stage stage,
105                                unsigned num_vars,
106                                const unsigned *input_locations,
107                                const gl_varying_slot *output_locations,
108                                unsigned *interpolation_modes,
109                                unsigned sysval_mask)
110 {
111    const struct glsl_type *vec4 = glsl_vec4_type();
112    const nir_shader_compiler_options *options =
113       st_get_nir_compiler_options(st, stage);
114 
115    nir_builder b = nir_builder_init_simple_shader(stage, options,
116                                                   "%s", shader_name);
117 
118    for (unsigned i = 0; i < num_vars; i++) {
119       nir_variable *in;
120       if (sysval_mask & (1 << i)) {
121          in = nir_create_variable_with_location(b.shader, nir_var_system_value,
122                                                 input_locations[i],
123                                   glsl_int_type());
124       } else {
125          in = nir_create_variable_with_location(b.shader, nir_var_shader_in,
126                                                 input_locations[i], vec4);
127       }
128       if (interpolation_modes)
129          in->data.interpolation = interpolation_modes[i];
130 
131       nir_variable *out =
132          nir_create_variable_with_location(b.shader, nir_var_shader_out,
133                                            output_locations[i], in->type);
134       out->data.interpolation = in->data.interpolation;
135 
136       nir_copy_var(&b, out, in);
137    }
138 
139    return st_nir_finish_builtin_shader(st, b.shader);
140 }
141 
142 /**
143  * Make a simple shader that reads color value from a constant buffer
144  * and uses it to clear all color buffers.
145  */
146 struct pipe_shader_state *
st_nir_make_clearcolor_shader(struct st_context * st)147 st_nir_make_clearcolor_shader(struct st_context *st)
148 {
149    const nir_shader_compiler_options *options =
150       st_get_nir_compiler_options(st, MESA_SHADER_FRAGMENT);
151 
152    nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, options,
153                                                   "clear color FS");
154    b.shader->info.num_ubos = 1;
155    b.shader->num_outputs = 1;
156    b.shader->num_uniforms = 1;
157 
158    /* Read clear color from constant buffer */
159    nir_def *clear_color = nir_load_uniform(&b, 4, 32, nir_imm_int(&b,0),
160                                                .range = 16,
161                                                .dest_type = nir_type_float32);
162 
163    nir_variable *color_out = nir_create_variable_with_location(b.shader, nir_var_shader_out,
164                                                                FRAG_RESULT_COLOR, glsl_vec4_type());
165 
166    /* Write out the color */
167    nir_store_var(&b, color_out, clear_color, 0xf);
168 
169    return st_nir_finish_builtin_shader(st, b.shader);
170 }
171