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#include "st_program.h" 26 27#include "compiler/nir/nir_builder.h" 28#include "compiler/glsl/gl_nir.h" 29#include "compiler/glsl/gl_nir_linker.h" 30#include "tgsi/tgsi_parse.h" 31 32struct pipe_shader_state * 33st_nir_finish_builtin_shader(struct st_context *st, 34 nir_shader *nir) 35{ 36 struct pipe_screen *screen = st->screen; 37 gl_shader_stage stage = nir->info.stage; 38 39 nir->info.separate_shader = true; 40 if (stage == MESA_SHADER_FRAGMENT) 41 nir->info.fs.untyped_color_outputs = true; 42 43 NIR_PASS_V(nir, nir_lower_global_vars_to_local); 44 NIR_PASS_V(nir, nir_split_var_copies); 45 NIR_PASS_V(nir, nir_lower_var_copies); 46 NIR_PASS_V(nir, nir_lower_system_values); 47 NIR_PASS_V(nir, nir_lower_compute_system_values, NULL); 48 49 if (nir->options->lower_to_scalar) { 50 nir_variable_mode mask = 51 (stage > MESA_SHADER_VERTEX ? nir_var_shader_in : 0) | 52 (stage < MESA_SHADER_FRAGMENT ? nir_var_shader_out : 0); 53 54 NIR_PASS_V(nir, nir_lower_io_to_scalar_early, mask); 55 } 56 57 if (st->lower_rect_tex) { 58 const struct nir_lower_tex_options opts = { .lower_rect = true, }; 59 NIR_PASS_V(nir, nir_lower_tex, &opts); 60 } 61 62 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir)); 63 64 st_nir_assign_vs_in_locations(nir); 65 st_nir_assign_varying_locations(st, nir); 66 67 st_nir_lower_samplers(screen, nir, NULL, NULL); 68 st_nir_lower_uniforms(st, nir); 69 if (!screen->get_param(screen, PIPE_CAP_NIR_IMAGES_AS_DEREF)) 70 NIR_PASS_V(nir, gl_nir_lower_images, false); 71 72 if (screen->finalize_nir) { 73 char *msg = screen->finalize_nir(screen, nir); 74 free(msg); 75 } else { 76 gl_nir_opts(nir); 77 } 78 79 struct pipe_shader_state state = { 80 .type = PIPE_SHADER_IR_NIR, 81 .ir.nir = nir, 82 }; 83 84 return st_create_nir_shader(st, &state); 85} 86 87/** 88 * Make a simple shader that copies inputs to corresponding outputs. 89 */ 90struct pipe_shader_state * 91st_nir_make_passthrough_shader(struct st_context *st, 92 const char *shader_name, 93 gl_shader_stage stage, 94 unsigned num_vars, 95 unsigned *input_locations, 96 unsigned *output_locations, 97 unsigned *interpolation_modes, 98 unsigned sysval_mask) 99{ 100 const struct glsl_type *vec4 = glsl_vec4_type(); 101 const nir_shader_compiler_options *options = 102 st_get_nir_compiler_options(st, stage); 103 104 nir_builder b = nir_builder_init_simple_shader(stage, options, 105 "%s", shader_name); 106 107 char var_name[15]; 108 109 for (unsigned i = 0; i < num_vars; i++) { 110 nir_variable *in; 111 if (sysval_mask & (1 << i)) { 112 snprintf(var_name, sizeof(var_name), "sys_%u", input_locations[i]); 113 in = nir_variable_create(b.shader, nir_var_system_value, 114 glsl_int_type(), var_name); 115 } else { 116 snprintf(var_name, sizeof(var_name), "in_%u", input_locations[i]); 117 in = nir_variable_create(b.shader, nir_var_shader_in, vec4, var_name); 118 } 119 in->data.location = input_locations[i]; 120 if (interpolation_modes) 121 in->data.interpolation = interpolation_modes[i]; 122 123 snprintf(var_name, sizeof(var_name), "out_%u", output_locations[i]); 124 nir_variable *out = 125 nir_variable_create(b.shader, nir_var_shader_out, in->type, var_name); 126 out->data.location = output_locations[i]; 127 out->data.interpolation = in->data.interpolation; 128 129 nir_copy_var(&b, out, in); 130 } 131 132 return st_nir_finish_builtin_shader(st, b.shader); 133} 134 135/** 136 * Make a simple shader that reads color value from a constant buffer 137 * and uses it to clear all color buffers. 138 */ 139struct pipe_shader_state * 140st_nir_make_clearcolor_shader(struct st_context *st) 141{ 142 const nir_shader_compiler_options *options = 143 st_get_nir_compiler_options(st, MESA_SHADER_FRAGMENT); 144 145 nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, options, 146 "clear color FS"); 147 b.shader->info.num_ubos = 1; 148 b.shader->num_outputs = 1; 149 b.shader->num_uniforms = 1; 150 151 /* Read clear color from constant buffer */ 152 nir_ssa_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_variable *color_out = 157 nir_variable_create(b.shader, nir_var_shader_out, glsl_vec_type(4), 158 "outcolor"); 159 color_out->data.location = FRAG_RESULT_COLOR; 160 161 /* Write out the color */ 162 nir_store_var(&b, color_out, clear_color, 0xf); 163 164 return st_nir_finish_builtin_shader(st, b.shader); 165} 166