17ec681f3Smrg/* 27ec681f3Smrg * Copyright (c) 2019 Zodiac Inflight Innovations 37ec681f3Smrg * 47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 57ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 67ec681f3Smrg * to deal in the Software without restriction, including without limitation 77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license, 87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 97ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 107ec681f3Smrg * 117ec681f3Smrg * The above copyright notice and this permission notice (including the 127ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions 137ec681f3Smrg * of the Software. 147ec681f3Smrg * 157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 217ec681f3Smrg * DEALINGS IN THE SOFTWARE. 227ec681f3Smrg * 237ec681f3Smrg * Authors: 247ec681f3Smrg * Jonathan Marek <jonathan@marek.ca> 257ec681f3Smrg */ 267ec681f3Smrg 277ec681f3Smrg#include "etnaviv_nir.h" 287ec681f3Smrg 297ec681f3Smrg/* io related lowering 307ec681f3Smrg * run after lower_int_to_float because it adds i2f/f2i ops 317ec681f3Smrg */ 327ec681f3Smrgvoid 337ec681f3Smrgetna_lower_io(nir_shader *shader, struct etna_shader_variant *v) 347ec681f3Smrg{ 357ec681f3Smrg nir_foreach_function(function, shader) { 367ec681f3Smrg nir_builder b; 377ec681f3Smrg nir_builder_init(&b, function->impl); 387ec681f3Smrg 397ec681f3Smrg nir_foreach_block(block, function->impl) { 407ec681f3Smrg nir_foreach_instr_safe(instr, block) { 417ec681f3Smrg if (instr->type == nir_instr_type_intrinsic) { 427ec681f3Smrg nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); 437ec681f3Smrg 447ec681f3Smrg switch (intr->intrinsic) { 457ec681f3Smrg case nir_intrinsic_load_front_face: { 467ec681f3Smrg /* HW front_face is 0.0/1.0, not 0/~0u for bool 477ec681f3Smrg * lower with a comparison with 0 487ec681f3Smrg */ 497ec681f3Smrg intr->dest.ssa.bit_size = 32; 507ec681f3Smrg 517ec681f3Smrg b.cursor = nir_after_instr(instr); 527ec681f3Smrg 537ec681f3Smrg nir_ssa_def *ssa = nir_ine(&b, &intr->dest.ssa, nir_imm_int(&b, 0)); 547ec681f3Smrg if (v->key.front_ccw) 557ec681f3Smrg nir_instr_as_alu(ssa->parent_instr)->op = nir_op_ieq; 567ec681f3Smrg 577ec681f3Smrg nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, 587ec681f3Smrg ssa, 597ec681f3Smrg ssa->parent_instr); 607ec681f3Smrg } break; 617ec681f3Smrg case nir_intrinsic_store_deref: { 627ec681f3Smrg nir_deref_instr *deref = nir_src_as_deref(intr->src[0]); 637ec681f3Smrg if (shader->info.stage != MESA_SHADER_FRAGMENT || !v->key.frag_rb_swap) 647ec681f3Smrg break; 657ec681f3Smrg 667ec681f3Smrg assert(deref->deref_type == nir_deref_type_var); 677ec681f3Smrg 687ec681f3Smrg if (deref->var->data.location != FRAG_RESULT_COLOR && 697ec681f3Smrg deref->var->data.location != FRAG_RESULT_DATA0) 707ec681f3Smrg break; 717ec681f3Smrg 727ec681f3Smrg b.cursor = nir_before_instr(instr); 737ec681f3Smrg 747ec681f3Smrg nir_ssa_def *ssa = nir_mov(&b, intr->src[1].ssa); 757ec681f3Smrg nir_alu_instr *alu = nir_instr_as_alu(ssa->parent_instr); 767ec681f3Smrg alu->src[0].swizzle[0] = 2; 777ec681f3Smrg alu->src[0].swizzle[2] = 0; 787ec681f3Smrg nir_instr_rewrite_src(instr, &intr->src[1], nir_src_for_ssa(ssa)); 797ec681f3Smrg } break; 807ec681f3Smrg case nir_intrinsic_load_vertex_id: 817ec681f3Smrg case nir_intrinsic_load_instance_id: 827ec681f3Smrg /* detect use of vertex_id/instance_id */ 837ec681f3Smrg v->vs_id_in_reg = v->infile.num_reg; 847ec681f3Smrg break; 857ec681f3Smrg default: 867ec681f3Smrg break; 877ec681f3Smrg } 887ec681f3Smrg } 897ec681f3Smrg 907ec681f3Smrg if (instr->type != nir_instr_type_tex) 917ec681f3Smrg continue; 927ec681f3Smrg 937ec681f3Smrg nir_tex_instr *tex = nir_instr_as_tex(instr); 947ec681f3Smrg nir_src *coord = NULL; 957ec681f3Smrg nir_src *lod_bias = NULL; 967ec681f3Smrg unsigned lod_bias_idx; 977ec681f3Smrg 987ec681f3Smrg assert(tex->sampler_index == tex->texture_index); 997ec681f3Smrg 1007ec681f3Smrg for (unsigned i = 0; i < tex->num_srcs; i++) { 1017ec681f3Smrg switch (tex->src[i].src_type) { 1027ec681f3Smrg case nir_tex_src_coord: 1037ec681f3Smrg coord = &tex->src[i].src; 1047ec681f3Smrg break; 1057ec681f3Smrg case nir_tex_src_bias: 1067ec681f3Smrg case nir_tex_src_lod: 1077ec681f3Smrg assert(!lod_bias); 1087ec681f3Smrg lod_bias = &tex->src[i].src; 1097ec681f3Smrg lod_bias_idx = i; 1107ec681f3Smrg break; 1117ec681f3Smrg case nir_tex_src_comparator: 1127ec681f3Smrg break; 1137ec681f3Smrg default: 1147ec681f3Smrg assert(0); 1157ec681f3Smrg break; 1167ec681f3Smrg } 1177ec681f3Smrg } 1187ec681f3Smrg 1197ec681f3Smrg /* pre HALTI5 needs texture sources in a single source */ 1207ec681f3Smrg 1217ec681f3Smrg if (!lod_bias || v->shader->specs->halti >= 5) 1227ec681f3Smrg continue; 1237ec681f3Smrg 1247ec681f3Smrg assert(coord && lod_bias && tex->coord_components < 4); 1257ec681f3Smrg 1267ec681f3Smrg nir_alu_instr *vec = nir_alu_instr_create(shader, nir_op_vec4); 1277ec681f3Smrg for (unsigned i = 0; i < tex->coord_components; i++) { 1287ec681f3Smrg vec->src[i].src = nir_src_for_ssa(coord->ssa); 1297ec681f3Smrg vec->src[i].swizzle[0] = i; 1307ec681f3Smrg } 1317ec681f3Smrg for (unsigned i = tex->coord_components; i < 4; i++) 1327ec681f3Smrg vec->src[i].src = nir_src_for_ssa(lod_bias->ssa); 1337ec681f3Smrg 1347ec681f3Smrg vec->dest.write_mask = 0xf; 1357ec681f3Smrg nir_ssa_dest_init(&vec->instr, &vec->dest.dest, 4, 32, NULL); 1367ec681f3Smrg 1377ec681f3Smrg nir_tex_instr_remove_src(tex, lod_bias_idx); 1387ec681f3Smrg nir_instr_rewrite_src(&tex->instr, coord, nir_src_for_ssa(&vec->dest.dest.ssa)); 1397ec681f3Smrg tex->coord_components = 4; 1407ec681f3Smrg 1417ec681f3Smrg nir_instr_insert_before(&tex->instr, &vec->instr); 1427ec681f3Smrg } 1437ec681f3Smrg } 1447ec681f3Smrg } 1457ec681f3Smrg} 1467ec681f3Smrg 1477ec681f3Smrgstatic void 1487ec681f3Smrgetna_lower_alu_impl(nir_function_impl *impl, bool has_new_transcendentals) 1497ec681f3Smrg{ 1507ec681f3Smrg nir_shader *shader = impl->function->shader; 1517ec681f3Smrg 1527ec681f3Smrg nir_builder b; 1537ec681f3Smrg nir_builder_init(&b, impl); 1547ec681f3Smrg 1557ec681f3Smrg /* in a seperate loop so we can apply the multiple-uniform logic to the new fmul */ 1567ec681f3Smrg nir_foreach_block(block, impl) { 1577ec681f3Smrg nir_foreach_instr_safe(instr, block) { 1587ec681f3Smrg if (instr->type != nir_instr_type_alu) 1597ec681f3Smrg continue; 1607ec681f3Smrg 1617ec681f3Smrg nir_alu_instr *alu = nir_instr_as_alu(instr); 1627ec681f3Smrg /* multiply sin/cos src by constant 1637ec681f3Smrg * TODO: do this earlier (but it breaks const_prop opt) 1647ec681f3Smrg */ 1657ec681f3Smrg if (alu->op == nir_op_fsin || alu->op == nir_op_fcos) { 1667ec681f3Smrg b.cursor = nir_before_instr(instr); 1677ec681f3Smrg 1687ec681f3Smrg nir_ssa_def *imm = has_new_transcendentals ? 1697ec681f3Smrg nir_imm_float(&b, 1.0 / M_PI) : 1707ec681f3Smrg nir_imm_float(&b, 2.0 / M_PI); 1717ec681f3Smrg 1727ec681f3Smrg nir_instr_rewrite_src(instr, &alu->src[0].src, 1737ec681f3Smrg nir_src_for_ssa(nir_fmul(&b, alu->src[0].src.ssa, imm))); 1747ec681f3Smrg } 1757ec681f3Smrg 1767ec681f3Smrg /* change transcendental ops to vec2 and insert vec1 mul for the result 1777ec681f3Smrg * TODO: do this earlier (but it breaks with optimizations) 1787ec681f3Smrg */ 1797ec681f3Smrg if (has_new_transcendentals && ( 1807ec681f3Smrg alu->op == nir_op_fdiv || alu->op == nir_op_flog2 || 1817ec681f3Smrg alu->op == nir_op_fsin || alu->op == nir_op_fcos)) { 1827ec681f3Smrg nir_ssa_def *ssa = &alu->dest.dest.ssa; 1837ec681f3Smrg 1847ec681f3Smrg assert(ssa->num_components == 1); 1857ec681f3Smrg 1867ec681f3Smrg nir_alu_instr *mul = nir_alu_instr_create(shader, nir_op_fmul); 1877ec681f3Smrg mul->src[0].src = mul->src[1].src = nir_src_for_ssa(ssa); 1887ec681f3Smrg mul->src[1].swizzle[0] = 1; 1897ec681f3Smrg 1907ec681f3Smrg mul->dest.write_mask = 1; 1917ec681f3Smrg nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, 32, NULL); 1927ec681f3Smrg 1937ec681f3Smrg ssa->num_components = 2; 1947ec681f3Smrg 1957ec681f3Smrg mul->dest.saturate = alu->dest.saturate; 1967ec681f3Smrg alu->dest.saturate = 0; 1977ec681f3Smrg 1987ec681f3Smrg nir_instr_insert_after(instr, &mul->instr); 1997ec681f3Smrg 2007ec681f3Smrg nir_ssa_def_rewrite_uses_after(ssa, &mul->dest.dest.ssa, 2017ec681f3Smrg &mul->instr); 2027ec681f3Smrg } 2037ec681f3Smrg } 2047ec681f3Smrg } 2057ec681f3Smrg} 2067ec681f3Smrg 2077ec681f3Smrgvoid 2087ec681f3Smrgetna_lower_alu(nir_shader *shader, bool has_new_transcendentals) 2097ec681f3Smrg{ 2107ec681f3Smrg nir_foreach_function(function, shader) { 2117ec681f3Smrg if (function->impl) 2127ec681f3Smrg etna_lower_alu_impl(function->impl, has_new_transcendentals); 2137ec681f3Smrg } 2147ec681f3Smrg} 215