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