cd078f3a5617d56055cd1416d8ce0b6906a2cb8a
[mesa.git] / src / mesa / state_tracker / st_nir_builtins.c
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
29 struct pipe_shader_state *
30 st_nir_finish_builtin_shader(struct st_context *st,
31 nir_shader *nir,
32 const char *name)
33 {
34 struct pipe_context *pipe = st->pipe;
35 struct pipe_screen *screen = pipe->screen;
36
37 nir->info.name = ralloc_strdup(nir, name);
38 nir->info.separate_shader = true;
39 if (nir->info.stage == MESA_SHADER_FRAGMENT)
40 nir->info.fs.untyped_color_outputs = true;
41
42 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
43 NIR_PASS_V(nir, nir_split_var_copies);
44 NIR_PASS_V(nir, nir_lower_var_copies);
45 NIR_PASS_V(nir, nir_lower_system_values);
46 NIR_PASS_V(nir, nir_lower_compute_system_values);
47
48 if (nir->options->lower_to_scalar) {
49 nir_variable_mode mask =
50 (nir->info.stage > MESA_SHADER_VERTEX ? nir_var_shader_in : 0) |
51 (nir->info.stage < MESA_SHADER_FRAGMENT ? nir_var_shader_out : 0);
52
53 NIR_PASS_V(nir, nir_lower_io_to_scalar_early, mask);
54 }
55
56 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
57
58 st_nir_assign_vs_in_locations(nir);
59 st_nir_assign_varying_locations(st, nir);
60
61 st_nir_lower_samplers(screen, nir, NULL, NULL);
62 st_nir_lower_uniforms(st, nir);
63 if (!screen->get_param(screen, PIPE_CAP_NIR_IMAGES_AS_DEREF))
64 NIR_PASS_V(nir, gl_nir_lower_images, false);
65
66 if (screen->finalize_nir)
67 screen->finalize_nir(screen, nir, true);
68 else
69 st_nir_opts(nir);
70
71 struct pipe_shader_state state = {
72 .type = PIPE_SHADER_IR_NIR,
73 .ir.nir = nir,
74 };
75
76 switch (nir->info.stage) {
77 case MESA_SHADER_VERTEX:
78 return pipe->create_vs_state(pipe, &state);
79 case MESA_SHADER_TESS_CTRL:
80 return pipe->create_tcs_state(pipe, &state);
81 case MESA_SHADER_TESS_EVAL:
82 return pipe->create_tes_state(pipe, &state);
83 case MESA_SHADER_GEOMETRY:
84 return pipe->create_gs_state(pipe, &state);
85 case MESA_SHADER_FRAGMENT:
86 return pipe->create_fs_state(pipe, &state);
87 default:
88 unreachable("unsupported shader stage");
89 return NULL;
90 }
91 }
92
93 /**
94 * Make a simple shader that copies inputs to corresponding outputs.
95 */
96 struct pipe_shader_state *
97 st_nir_make_passthrough_shader(struct st_context *st,
98 const char *shader_name,
99 gl_shader_stage stage,
100 unsigned num_vars,
101 unsigned *input_locations,
102 unsigned *output_locations,
103 unsigned *interpolation_modes,
104 unsigned sysval_mask)
105 {
106 struct nir_builder b;
107 const struct glsl_type *vec4 = glsl_vec4_type();
108 const nir_shader_compiler_options *options =
109 st->ctx->Const.ShaderCompilerOptions[stage].NirOptions;
110
111 nir_builder_init_simple_shader(&b, NULL, stage, options);
112
113 char var_name[15];
114
115 for (unsigned i = 0; i < num_vars; i++) {
116 nir_variable *in;
117 if (sysval_mask & (1 << i)) {
118 snprintf(var_name, sizeof(var_name), "sys_%u", input_locations[i]);
119 in = nir_variable_create(b.shader, nir_var_system_value,
120 glsl_int_type(), var_name);
121 in->data.interpolation = INTERP_MODE_FLAT;
122 } else {
123 snprintf(var_name, sizeof(var_name), "in_%u", input_locations[i]);
124 in = nir_variable_create(b.shader, nir_var_shader_in, vec4, var_name);
125 }
126 in->data.location = input_locations[i];
127 if (interpolation_modes)
128 in->data.interpolation = interpolation_modes[i];
129
130 snprintf(var_name, sizeof(var_name), "out_%u", output_locations[i]);
131 nir_variable *out =
132 nir_variable_create(b.shader, nir_var_shader_out, in->type, var_name);
133 out->data.location = output_locations[i];
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, shader_name);
140 }