• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    nir_recompute_io_bases(nir, nir_var_shader_in | nir_var_shader_out);
68 
69    st_nir_assign_vs_in_locations(nir);
70    st_nir_assign_varying_locations(st, nir);
71 
72    st_nir_lower_samplers(screen, nir, NULL, NULL);
73    st_nir_lower_uniforms(st, nir);
74    if (!screen->caps.nir_images_as_deref)
75       NIR_PASS(_, nir, gl_nir_lower_images, false);
76 
77    if (nir->info.io_lowered &&
78        !(nir->options->io_options & nir_io_has_intrinsics)) {
79       NIR_PASS(_, nir, st_nir_unlower_io_to_vars);
80       gl_nir_opts(nir);
81    }
82 
83    if (screen->finalize_nir) {
84       char *msg = screen->finalize_nir(screen, nir);
85       free(msg);
86    } else {
87       gl_nir_opts(nir);
88    }
89 }
90 
91 void *
st_nir_finish_builtin_shader(struct st_context * st,nir_shader * nir)92 st_nir_finish_builtin_shader(struct st_context *st,
93                              nir_shader *nir)
94 {
95    st_nir_finish_builtin_nir(st, nir);
96 
97    struct pipe_shader_state state = {
98       .type = PIPE_SHADER_IR_NIR,
99       .ir.nir = nir,
100    };
101 
102    return st_create_nir_shader(st, &state);
103 }
104 
105 /**
106  * Make a simple vertex shader that copies inputs to corresponding outputs.
107  */
108 void *
st_nir_make_passthrough_vs(struct st_context * st,const char * shader_name,unsigned num_vars,const unsigned * input_locations,const gl_varying_slot * output_locations,unsigned sysval_mask)109 st_nir_make_passthrough_vs(struct st_context *st,
110                            const char *shader_name,
111                            unsigned num_vars,
112                            const unsigned *input_locations,
113                            const gl_varying_slot *output_locations,
114                            unsigned sysval_mask)
115 {
116    const nir_shader_compiler_options *options =
117       st_get_nir_compiler_options(st, MESA_SHADER_VERTEX);
118 
119    nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, options,
120                                                   "%s", shader_name);
121    b.shader->info.io_lowered = true;
122 
123    for (unsigned i = 0; i < num_vars; i++) {
124       nir_def *in;
125 
126       if (sysval_mask & (1 << i)) {
127          nir_variable *var =
128             nir_create_variable_with_location(b.shader, nir_var_system_value,
129                                               input_locations[i],
130                                               glsl_int_type());
131          in = nir_load_var(&b, var);
132       } else {
133          in = nir_load_input(&b, 4, 32, nir_imm_int(&b, 0),
134                              .io_semantics.location = input_locations[i]);
135       }
136 
137       nir_store_output(&b, in, nir_imm_int(&b, 0),
138                        .src_type = output_locations[i] == VARYING_SLOT_LAYER ?
139                                       nir_type_int32 : nir_type_float32,
140                        .io_semantics.location = output_locations[i]);
141    }
142 
143    return st_nir_finish_builtin_shader(st, b.shader);
144 }
145 
146 /**
147  * Make a simple shader that reads color value from a constant buffer
148  * and uses it to clear all color buffers.
149  */
150 void *
st_nir_make_clearcolor_shader(struct st_context * st)151 st_nir_make_clearcolor_shader(struct st_context *st)
152 {
153    const nir_shader_compiler_options *options =
154       st_get_nir_compiler_options(st, MESA_SHADER_FRAGMENT);
155 
156    nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, options,
157                                                   "clear color FS");
158    b.shader->info.num_ubos = 1;
159    b.shader->num_outputs = 1;
160    b.shader->num_uniforms = 1;
161    b.shader->info.io_lowered = true;
162 
163    /* Read clear color from constant buffer */
164    nir_def *clear_color = nir_load_uniform(&b, 4, 32, nir_imm_int(&b,0),
165                                                .range = 16,
166                                                .dest_type = nir_type_float32);
167 
168    nir_store_output(&b, clear_color, nir_imm_int(&b, 0),
169                     .io_semantics.location = FRAG_RESULT_COLOR);
170 
171    return st_nir_finish_builtin_shader(st, b.shader);
172 }
173