1/************************************************************************** 2 * 3 * Copyright 2019 Red Hat. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 * 24 **************************************************************************/ 25 26/* 27 * NIR lowering passes to handle the draw stages for 28 * - pstipple 29 * - aaline 30 * - aapoint. 31 * 32 * These are all ported from the equivalent TGSI transforms. 33 */ 34 35#include "nir.h" 36#include "tgsi/tgsi_from_mesa.h" 37#include "nir_builder.h" 38 39#include "nir_draw_helpers.h" 40 41typedef struct { 42 nir_builder b; 43 nir_shader *shader; 44 bool fs_pos_is_sysval; 45 nir_variable *stip_tex; 46 nir_ssa_def *fragcoord; 47} lower_pstipple; 48 49static nir_ssa_def * 50load_frag_coord(nir_builder *b) 51{ 52 nir_foreach_shader_in_variable(var, b->shader) { 53 if (var->data.location == VARYING_SLOT_POS) 54 return nir_load_var(b, var); 55 } 56 57 nir_variable *pos = nir_variable_create(b->shader, nir_var_shader_in, 58 glsl_vec4_type(), NULL); 59 pos->data.location = VARYING_SLOT_POS; 60 pos->data.interpolation = INTERP_MODE_NOPERSPECTIVE; 61 pos->data.driver_location = b->shader->num_inputs++; 62 return nir_load_var(b, pos); 63} 64 65static void 66nir_lower_pstipple_block(nir_block *block, 67 lower_pstipple *state) 68{ 69 nir_builder *b = &state->b; 70 nir_ssa_def *texcoord; 71 72 b->cursor = nir_before_block(block); 73 74 nir_ssa_def *frag_coord = state->fs_pos_is_sysval ? nir_load_frag_coord(b) : load_frag_coord(b); 75 76 texcoord = nir_fmul(b, nir_channels(b, frag_coord, 0x3), 77 nir_imm_vec2(b, 1.0/32.0, 1.0/32.0)); 78 79 nir_tex_instr *tex = nir_tex_instr_create(b->shader, 1); 80 tex->op = nir_texop_tex; 81 tex->sampler_dim = GLSL_SAMPLER_DIM_2D; 82 tex->coord_components = 2; 83 tex->dest_type = nir_type_float32; 84 tex->texture_index = state->stip_tex->data.binding; 85 tex->sampler_index = state->stip_tex->data.binding; 86 tex->src[0].src_type = nir_tex_src_coord; 87 tex->src[0].src = nir_src_for_ssa(texcoord); 88 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL); 89 90 nir_builder_instr_insert(b, &tex->instr); 91 92 nir_ssa_def *condition = nir_f2b32(b, nir_channel(b, &tex->dest.ssa, 3)); 93 nir_discard_if(b, condition); 94 b->shader->info.fs.uses_discard = true; 95} 96 97static void 98nir_lower_pstipple_impl(nir_function_impl *impl, 99 lower_pstipple *state) 100{ 101 nir_builder *b = &state->b; 102 103 nir_builder_init(b, impl); 104 105 nir_block *start = nir_start_block(impl); 106 nir_lower_pstipple_block(start, state); 107} 108 109void 110nir_lower_pstipple_fs(struct nir_shader *shader, 111 unsigned *samplerUnitOut, 112 unsigned fixedUnit, 113 bool fs_pos_is_sysval) 114{ 115 lower_pstipple state = { 116 .shader = shader, 117 .fs_pos_is_sysval = fs_pos_is_sysval, 118 }; 119 if (shader->info.stage != MESA_SHADER_FRAGMENT) 120 return; 121 122 int binding = 0; 123 nir_foreach_uniform_variable(var, shader) { 124 if (glsl_type_is_sampler(var->type)) { 125 if (var->data.binding >= binding) 126 binding = var->data.binding + 1; 127 } 128 } 129 const struct glsl_type *sampler2D = 130 glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT); 131 132 nir_variable *tex_var = nir_variable_create(shader, nir_var_uniform, sampler2D, "stipple_tex"); 133 tex_var->data.binding = binding; 134 tex_var->data.explicit_binding = true; 135 tex_var->data.how_declared = nir_var_hidden; 136 137 BITSET_SET(shader->info.textures_used, binding); 138 BITSET_SET(shader->info.samplers_used, binding); 139 state.stip_tex = tex_var; 140 141 nir_foreach_function(function, shader) { 142 if (function->impl) { 143 nir_lower_pstipple_impl(function->impl, &state); 144 } 145 } 146 *samplerUnitOut = binding; 147} 148 149typedef struct { 150 nir_builder b; 151 nir_shader *shader; 152 nir_variable *line_width_input; 153} lower_aaline; 154 155static void 156nir_lower_aaline_block(nir_block *block, 157 lower_aaline *state) 158{ 159 nir_builder *b = &state->b; 160 nir_foreach_instr(instr, block) { 161 if (instr->type != nir_instr_type_intrinsic) 162 continue; 163 164 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 165 if (intrin->intrinsic != nir_intrinsic_store_deref) 166 continue; 167 168 nir_variable *var = nir_intrinsic_get_var(intrin, 0); 169 if (var->data.mode != nir_var_shader_out) 170 continue; 171 if (var->data.location < FRAG_RESULT_DATA0 && var->data.location != FRAG_RESULT_COLOR) 172 continue; 173 174 nir_ssa_def *out_input = intrin->src[1].ssa; 175 b->cursor = nir_before_instr(instr); 176 nir_ssa_def *lw = nir_load_var(b, state->line_width_input); 177 nir_ssa_def *tmp = nir_fsat(b, nir_fadd(b, nir_channel(b, lw, 1), 178 nir_fneg(b, nir_fabs(b, nir_channel(b, lw, 0))))); 179 nir_ssa_def *tmp1 = nir_fsat(b, nir_fadd(b, nir_channel(b, lw, 3), 180 nir_fneg(b, nir_fabs(b, nir_channel(b, lw, 2))))); 181 182 tmp = nir_fmul(b, tmp, tmp1); 183 tmp = nir_fmul(b, nir_channel(b, out_input, 3), tmp); 184 185 nir_ssa_def *out = nir_vec4(b, nir_channel(b, out_input, 0), 186 nir_channel(b, out_input, 1), 187 nir_channel(b, out_input, 2), 188 tmp); 189 nir_instr_rewrite_src(instr, &intrin->src[1], nir_src_for_ssa(out)); 190 } 191 192} 193 194static void 195nir_lower_aaline_impl(nir_function_impl *impl, 196 lower_aaline *state) 197{ 198 nir_builder *b = &state->b; 199 200 nir_builder_init(b, impl); 201 202 nir_foreach_block(block, impl) { 203 nir_lower_aaline_block(block, state); 204 } 205} 206 207void 208nir_lower_aaline_fs(struct nir_shader *shader, int *varying) 209{ 210 lower_aaline state = { 211 .shader = shader, 212 }; 213 if (shader->info.stage != MESA_SHADER_FRAGMENT) 214 return; 215 216 int highest_location = -1, highest_drv_location = -1; 217 nir_foreach_shader_in_variable(var, shader) { 218 if ((int)var->data.location > highest_location) 219 highest_location = var->data.location; 220 if ((int)var->data.driver_location > highest_drv_location) 221 highest_drv_location = var->data.driver_location; 222 } 223 224 nir_variable *line_width = nir_variable_create(shader, nir_var_shader_in, 225 glsl_vec4_type(), "aaline"); 226 if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) { 227 line_width->data.location = VARYING_SLOT_VAR0; 228 line_width->data.driver_location = highest_drv_location + 1; 229 } else { 230 line_width->data.location = highest_location + 1; 231 line_width->data.driver_location = highest_drv_location + 1; 232 } 233 shader->num_inputs++; 234 *varying = tgsi_get_generic_gl_varying_index(line_width->data.location, true); 235 state.line_width_input = line_width; 236 237 nir_foreach_function(function, shader) { 238 if (function->impl) { 239 nir_lower_aaline_impl(function->impl, &state); 240 } 241 } 242} 243 244typedef struct { 245 nir_builder b; 246 nir_shader *shader; 247 nir_variable *input; 248} lower_aapoint; 249 250static void 251nir_lower_aapoint_block(nir_block *block, 252 lower_aapoint *state, nir_ssa_def *sel) 253{ 254 nir_builder *b = &state->b; 255 nir_foreach_instr(instr, block) { 256 if (instr->type != nir_instr_type_intrinsic) 257 continue; 258 259 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 260 if (intrin->intrinsic != nir_intrinsic_store_deref) 261 continue; 262 263 nir_variable *var = nir_intrinsic_get_var(intrin, 0); 264 if (var->data.mode != nir_var_shader_out) 265 continue; 266 if (var->data.location < FRAG_RESULT_DATA0 && var->data.location != FRAG_RESULT_COLOR) 267 continue; 268 269 nir_ssa_def *out_input = intrin->src[1].ssa; 270 b->cursor = nir_before_instr(instr); 271 272 nir_ssa_def *tmp = nir_fmul(b, nir_channel(b, out_input, 3), sel); 273 nir_ssa_def *out = nir_vec4(b, nir_channel(b, out_input, 0), 274 nir_channel(b, out_input, 1), 275 nir_channel(b, out_input, 2), 276 tmp); 277 nir_instr_rewrite_src(instr, &intrin->src[1], nir_src_for_ssa(out)); 278 } 279 280} 281 282static void 283nir_lower_aapoint_impl(nir_function_impl *impl, 284 lower_aapoint *state) 285{ 286 nir_builder *b = &state->b; 287 288 nir_builder_init(b, impl); 289 290 nir_block *block = nir_start_block(impl); 291 b->cursor = nir_before_block(block); 292 293 nir_ssa_def *aainput = nir_load_var(b, state->input); 294 295 nir_ssa_def *dist = nir_fadd(b, nir_fmul(b, nir_channel(b, aainput, 0), nir_channel(b, aainput, 0)), 296 nir_fmul(b, nir_channel(b, aainput, 1), nir_channel(b, aainput, 1))); 297 298 nir_ssa_def *k = nir_channel(b, aainput, 2); 299 nir_ssa_def *chan_val_one = nir_channel(b, aainput, 3); 300 nir_ssa_def *comp = nir_flt32(b, chan_val_one, dist); 301 302 nir_discard_if(b, comp); 303 b->shader->info.fs.uses_discard = true; 304 305 /* compute coverage factor = (1-d)/(1-k) */ 306 /* 1 - k */ 307 nir_ssa_def *tmp = nir_fadd(b, chan_val_one, nir_fneg(b, k)); 308 /* 1.0 / (1 - k) */ 309 tmp = nir_frcp(b, tmp); 310 311 /* 1 - d */ 312 nir_ssa_def *tmp2 = nir_fadd(b, chan_val_one, nir_fneg(b, dist)); 313 314 /* (1 - d) / (1 - k) */ 315 nir_ssa_def *coverage = nir_fmul(b, tmp, tmp2); 316 317 /* if (k >= distance) 318 * sel = coverage; 319 * else 320 * sel = 1.0; 321 */ 322 nir_ssa_def *sel = nir_b32csel(b, nir_fge32(b, k, dist), coverage, chan_val_one); 323 324 nir_foreach_block(block, impl) { 325 nir_lower_aapoint_block(block, state, sel); 326 } 327} 328 329void 330nir_lower_aapoint_fs(struct nir_shader *shader, int *varying) 331{ 332 lower_aapoint state = { 333 .shader = shader, 334 }; 335 if (shader->info.stage != MESA_SHADER_FRAGMENT) 336 return; 337 338 int highest_location = -1, highest_drv_location = -1; 339 nir_foreach_shader_in_variable(var, shader) { 340 if ((int)var->data.location > highest_location) 341 highest_location = var->data.location; 342 if ((int)var->data.driver_location > highest_drv_location) 343 highest_drv_location = var->data.driver_location; 344 } 345 346 nir_variable *aapoint_input = nir_variable_create(shader, nir_var_shader_in, 347 glsl_vec4_type(), "aapoint"); 348 if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) { 349 aapoint_input->data.location = VARYING_SLOT_VAR0; 350 } else { 351 aapoint_input->data.location = highest_location + 1; 352 } 353 aapoint_input->data.driver_location = highest_drv_location + 1; 354 355 shader->num_inputs++; 356 *varying = tgsi_get_generic_gl_varying_index(aapoint_input->data.location, true); 357 state.input = aapoint_input; 358 359 nir_foreach_function(function, shader) { 360 if (function->impl) { 361 nir_lower_aapoint_impl(function->impl, &state); 362 } 363 } 364} 365