st/mesa: use pipe_screen::finalize_nir
[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_glsl_types.h"
25 #include "st_nir.h"
26
27 #include "compiler/nir/nir_builder.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
47 if (nir->options->lower_to_scalar) {
48 nir_variable_mode mask =
49 (nir->info.stage > MESA_SHADER_VERTEX ? nir_var_shader_in : 0) |
50 (nir->info.stage < MESA_SHADER_FRAGMENT ? nir_var_shader_out : 0);
51
52 NIR_PASS_V(nir, nir_lower_io_to_scalar_early, mask);
53 }
54
55 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
56
57 st_nir_assign_vs_in_locations(nir);
58 st_nir_assign_varying_locations(st, nir);
59
60 st_nir_lower_samplers(screen, nir, NULL, NULL);
61
62 if (st->ctx->Const.PackedDriverUniformStorage) {
63 NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_type_dword_size,
64 (nir_lower_io_options)0);
65 NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 4);
66 } else {
67 NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_uniforms_type_size,
68 (nir_lower_io_options)0);
69 }
70
71 if (screen->finalize_nir)
72 screen->finalize_nir(screen, nir, true);
73 else
74 st_nir_opts(nir);
75
76 struct pipe_shader_state state = {
77 .type = PIPE_SHADER_IR_NIR,
78 .ir.nir = nir,
79 };
80
81 switch (nir->info.stage) {
82 case MESA_SHADER_VERTEX:
83 return pipe->create_vs_state(pipe, &state);
84 case MESA_SHADER_TESS_CTRL:
85 return pipe->create_tcs_state(pipe, &state);
86 case MESA_SHADER_TESS_EVAL:
87 return pipe->create_tes_state(pipe, &state);
88 case MESA_SHADER_GEOMETRY:
89 return pipe->create_gs_state(pipe, &state);
90 case MESA_SHADER_FRAGMENT:
91 return pipe->create_fs_state(pipe, &state);
92 default:
93 unreachable("unsupported shader stage");
94 return NULL;
95 }
96 }
97
98 /**
99 * Make a simple shader that copies inputs to corresponding outputs.
100 */
101 struct pipe_shader_state *
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 unsigned *input_locations,
107 unsigned *output_locations,
108 unsigned *interpolation_modes,
109 unsigned sysval_mask)
110 {
111 struct nir_builder b;
112 const struct glsl_type *vec4 = glsl_vec4_type();
113 const nir_shader_compiler_options *options =
114 st->ctx->Const.ShaderCompilerOptions[stage].NirOptions;
115
116 nir_builder_init_simple_shader(&b, NULL, stage, options);
117
118 char var_name[15];
119
120 for (unsigned i = 0; i < num_vars; i++) {
121 nir_variable *in;
122 if (sysval_mask & (1 << i)) {
123 snprintf(var_name, sizeof(var_name), "sys_%u", input_locations[i]);
124 in = nir_variable_create(b.shader, nir_var_system_value,
125 glsl_int_type(), var_name);
126 in->data.interpolation = INTERP_MODE_FLAT;
127 } else {
128 snprintf(var_name, sizeof(var_name), "in_%u", input_locations[i]);
129 in = nir_variable_create(b.shader, nir_var_shader_in, vec4, var_name);
130 }
131 in->data.location = input_locations[i];
132 if (interpolation_modes)
133 in->data.interpolation = interpolation_modes[i];
134
135 snprintf(var_name, sizeof(var_name), "out_%u", output_locations[i]);
136 nir_variable *out =
137 nir_variable_create(b.shader, nir_var_shader_out, in->type, var_name);
138 out->data.location = output_locations[i];
139 out->data.interpolation = in->data.interpolation;
140
141 nir_copy_var(&b, out, in);
142 }
143
144 return st_nir_finish_builtin_shader(st, b.shader, shader_name);
145 }