• 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 "tgsi/tgsi_from_mesa.h"
24 #include "st_nir.h"
25 
26 #include "compiler/nir/nir_builder.h"
27 #include "compiler/glsl/gl_nir.h"
28 #include "nir/nir_to_tgsi.h"
29 #include "tgsi/tgsi_parse.h"
30 
31 struct pipe_shader_state *
st_nir_finish_builtin_shader(struct st_context * st,nir_shader * nir,const char * name)32 st_nir_finish_builtin_shader(struct st_context *st,
33                              nir_shader *nir,
34                              const char *name)
35 {
36    struct pipe_context *pipe = st->pipe;
37    struct pipe_screen *screen = pipe->screen;
38    gl_shader_stage stage = nir->info.stage;
39    enum pipe_shader_type sh = pipe_shader_type_from_mesa(stage);
40 
41    nir->info.name = ralloc_strdup(nir, name);
42    nir->info.separate_shader = true;
43    if (stage == MESA_SHADER_FRAGMENT)
44       nir->info.fs.untyped_color_outputs = true;
45 
46    NIR_PASS_V(nir, nir_lower_global_vars_to_local);
47    NIR_PASS_V(nir, nir_split_var_copies);
48    NIR_PASS_V(nir, nir_lower_var_copies);
49    NIR_PASS_V(nir, nir_lower_system_values);
50    NIR_PASS_V(nir, nir_lower_compute_system_values, NULL);
51 
52    if (nir->options->lower_to_scalar) {
53       nir_variable_mode mask =
54           (stage > MESA_SHADER_VERTEX ? nir_var_shader_in : 0) |
55           (stage < MESA_SHADER_FRAGMENT ? nir_var_shader_out : 0);
56 
57       NIR_PASS_V(nir, nir_lower_io_to_scalar_early, mask);
58    }
59 
60    nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
61 
62    st_nir_assign_vs_in_locations(nir);
63    st_nir_assign_varying_locations(st, nir);
64 
65    st_nir_lower_samplers(screen, nir, NULL, NULL);
66    st_nir_lower_uniforms(st, nir);
67    if (!screen->get_param(screen, PIPE_CAP_NIR_IMAGES_AS_DEREF))
68       NIR_PASS_V(nir, gl_nir_lower_images, false);
69 
70    if (screen->finalize_nir)
71       screen->finalize_nir(screen, nir, true);
72    else
73       st_nir_opts(nir);
74 
75    struct pipe_shader_state state = {
76       .type = PIPE_SHADER_IR_NIR,
77       .ir.nir = nir,
78    };
79 
80    if (PIPE_SHADER_IR_NIR !=
81        screen->get_shader_param(screen, sh, PIPE_SHADER_CAP_PREFERRED_IR)) {
82       state.type = PIPE_SHADER_IR_TGSI;
83       state.tokens = nir_to_tgsi(nir, screen);
84       ralloc_free(nir);
85    }
86 
87    struct pipe_shader_state *shader;
88    switch (stage) {
89    case MESA_SHADER_VERTEX:
90       shader = pipe->create_vs_state(pipe, &state);
91       break;
92    case MESA_SHADER_TESS_CTRL:
93       shader = pipe->create_tcs_state(pipe, &state);
94       break;
95    case MESA_SHADER_TESS_EVAL:
96       shader = pipe->create_tes_state(pipe, &state);
97       break;
98    case MESA_SHADER_GEOMETRY:
99       shader = pipe->create_gs_state(pipe, &state);
100       break;
101    case MESA_SHADER_FRAGMENT:
102       shader = pipe->create_fs_state(pipe, &state);
103       break;
104    default:
105       unreachable("unsupported shader stage");
106       return NULL;
107    }
108 
109    if (state.type == PIPE_SHADER_IR_TGSI)
110       tgsi_free_tokens(state.tokens);
111 
112    return shader;
113 }
114 
115 /**
116  * Make a simple shader that copies inputs to corresponding outputs.
117  */
118 struct pipe_shader_state *
st_nir_make_passthrough_shader(struct st_context * st,const char * shader_name,gl_shader_stage stage,unsigned num_vars,unsigned * input_locations,unsigned * output_locations,unsigned * interpolation_modes,unsigned sysval_mask)119 st_nir_make_passthrough_shader(struct st_context *st,
120                                const char *shader_name,
121                                gl_shader_stage stage,
122                                unsigned num_vars,
123                                unsigned *input_locations,
124                                unsigned *output_locations,
125                                unsigned *interpolation_modes,
126                                unsigned sysval_mask)
127 {
128    struct nir_builder b;
129    const struct glsl_type *vec4 = glsl_vec4_type();
130    const nir_shader_compiler_options *options =
131       st_get_nir_compiler_options(st, stage);
132 
133    nir_builder_init_simple_shader(&b, NULL, stage, options);
134 
135    char var_name[15];
136 
137    for (unsigned i = 0; i < num_vars; i++) {
138       nir_variable *in;
139       if (sysval_mask & (1 << i)) {
140          snprintf(var_name, sizeof(var_name), "sys_%u", input_locations[i]);
141          in = nir_variable_create(b.shader, nir_var_system_value,
142                                   glsl_int_type(), var_name);
143       } else {
144          snprintf(var_name, sizeof(var_name), "in_%u", input_locations[i]);
145          in = nir_variable_create(b.shader, nir_var_shader_in, vec4, var_name);
146       }
147       in->data.location = input_locations[i];
148       if (interpolation_modes)
149          in->data.interpolation = interpolation_modes[i];
150 
151       snprintf(var_name, sizeof(var_name), "out_%u", output_locations[i]);
152       nir_variable *out =
153          nir_variable_create(b.shader, nir_var_shader_out, in->type, var_name);
154       out->data.location = output_locations[i];
155       out->data.interpolation = in->data.interpolation;
156 
157       nir_copy_var(&b, out, in);
158    }
159 
160    return st_nir_finish_builtin_shader(st, b.shader, shader_name);
161 }
162