• 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_system_values);
43 
44    struct nir_lower_compute_system_values_options cs_options = {
45       .has_base_global_invocation_id = false,
46       .has_base_workgroup_id = false,
47    };
48    NIR_PASS(_, nir, nir_lower_compute_system_values, &cs_options);
49 
50    if (st->lower_rect_tex) {
51       const struct nir_lower_tex_options opts = { .lower_rect = true, };
52       NIR_PASS(_, nir, nir_lower_tex, &opts);
53    }
54 
55    nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
56    nir_recompute_io_bases(nir, nir_var_shader_in | nir_var_shader_out);
57 
58    st_nir_lower_samplers(screen, nir, NULL, NULL);
59    st_nir_lower_uniforms(st, nir);
60    if (!screen->caps.nir_images_as_deref)
61       NIR_PASS(_, nir, gl_nir_lower_images, false);
62 
63    assert(nir->info.stage == MESA_SHADER_COMPUTE || nir->info.io_lowered);
64 
65    if (nir->info.io_lowered &&
66        !(nir->options->io_options & nir_io_has_intrinsics)) {
67       NIR_PASS(_, nir, st_nir_unlower_io_to_vars);
68       gl_nir_opts(nir);
69    }
70 
71    if (screen->finalize_nir) {
72       char *msg = screen->finalize_nir(screen, nir);
73       free(msg);
74    } else {
75       gl_nir_opts(nir);
76    }
77 }
78 
79 void *
st_nir_finish_builtin_shader(struct st_context * st,nir_shader * nir)80 st_nir_finish_builtin_shader(struct st_context *st,
81                              nir_shader *nir)
82 {
83    st_nir_finish_builtin_nir(st, nir);
84 
85    struct pipe_shader_state state = {
86       .type = PIPE_SHADER_IR_NIR,
87       .ir.nir = nir,
88    };
89 
90    return st_create_nir_shader(st, &state);
91 }
92 
93 /**
94  * Make a simple vertex shader that copies inputs to corresponding outputs.
95  */
96 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)97 st_nir_make_passthrough_vs(struct st_context *st,
98                            const char *shader_name,
99                            unsigned num_vars,
100                            const unsigned *input_locations,
101                            const gl_varying_slot *output_locations,
102                            unsigned sysval_mask)
103 {
104    const nir_shader_compiler_options *options =
105       st_get_nir_compiler_options(st, MESA_SHADER_VERTEX);
106 
107    nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, options,
108                                                   "%s", shader_name);
109    b.shader->info.io_lowered = true;
110 
111    for (unsigned i = 0; i < num_vars; i++) {
112       nir_def *in;
113 
114       if (sysval_mask & (1 << i)) {
115          nir_variable *var =
116             nir_create_variable_with_location(b.shader, nir_var_system_value,
117                                               input_locations[i],
118                                               glsl_int_type());
119          in = nir_load_var(&b, var);
120       } else {
121          in = nir_load_input(&b, 4, 32, nir_imm_int(&b, 0),
122                              .io_semantics.location = input_locations[i]);
123       }
124 
125       nir_store_output(&b, in, nir_imm_int(&b, 0),
126                        .src_type = output_locations[i] == VARYING_SLOT_LAYER ?
127                                       nir_type_int32 : nir_type_float32,
128                        .io_semantics.location = output_locations[i]);
129    }
130 
131    return st_nir_finish_builtin_shader(st, b.shader);
132 }
133 
134 /**
135  * Make a simple shader that reads color value from a constant buffer
136  * and uses it to clear all color buffers.
137  */
138 void *
st_nir_make_clearcolor_shader(struct st_context * st)139 st_nir_make_clearcolor_shader(struct st_context *st)
140 {
141    const nir_shader_compiler_options *options =
142       st_get_nir_compiler_options(st, MESA_SHADER_FRAGMENT);
143 
144    nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, options,
145                                                   "clear color FS");
146    b.shader->info.num_ubos = 1;
147    b.shader->num_outputs = 1;
148    b.shader->num_uniforms = 1;
149    b.shader->info.io_lowered = true;
150 
151    /* Read clear color from constant buffer */
152    nir_def *clear_color = nir_load_uniform(&b, 4, 32, nir_imm_int(&b,0),
153                                                .range = 16,
154                                                .dest_type = nir_type_float32);
155 
156    nir_store_output(&b, clear_color, nir_imm_int(&b, 0),
157                     .io_semantics.location = FRAG_RESULT_COLOR);
158 
159    return st_nir_finish_builtin_shader(st, b.shader);
160 }
161