Path: blob/21.2-virgl/src/gallium/drivers/etnaviv/etnaviv_nir.c
4570 views
/*1* Copyright (c) 2019 Zodiac Inflight Innovations2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sub license,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the11* next paragraph) shall be included in all copies or substantial portions12* of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*22* Authors:23* Jonathan Marek <[email protected]>24*/2526#include "etnaviv_nir.h"2728/* io related lowering29* run after lower_int_to_float because it adds i2f/f2i ops30*/31void32etna_lower_io(nir_shader *shader, struct etna_shader_variant *v)33{34nir_foreach_function(function, shader) {35nir_builder b;36nir_builder_init(&b, function->impl);3738nir_foreach_block(block, function->impl) {39nir_foreach_instr_safe(instr, block) {40if (instr->type == nir_instr_type_intrinsic) {41nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);4243switch (intr->intrinsic) {44case nir_intrinsic_load_front_face: {45/* HW front_face is 0.0/1.0, not 0/~0u for bool46* lower with a comparison with 047*/48intr->dest.ssa.bit_size = 32;4950b.cursor = nir_after_instr(instr);5152nir_ssa_def *ssa = nir_ine(&b, &intr->dest.ssa, nir_imm_int(&b, 0));53if (v->key.front_ccw)54nir_instr_as_alu(ssa->parent_instr)->op = nir_op_ieq;5556nir_ssa_def_rewrite_uses_after(&intr->dest.ssa,57ssa,58ssa->parent_instr);59} break;60case nir_intrinsic_store_deref: {61nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);62if (shader->info.stage != MESA_SHADER_FRAGMENT || !v->key.frag_rb_swap)63break;6465assert(deref->deref_type == nir_deref_type_var);6667if (deref->var->data.location != FRAG_RESULT_COLOR &&68deref->var->data.location != FRAG_RESULT_DATA0)69break;7071b.cursor = nir_before_instr(instr);7273nir_ssa_def *ssa = nir_mov(&b, intr->src[1].ssa);74nir_alu_instr *alu = nir_instr_as_alu(ssa->parent_instr);75alu->src[0].swizzle[0] = 2;76alu->src[0].swizzle[2] = 0;77nir_instr_rewrite_src(instr, &intr->src[1], nir_src_for_ssa(ssa));78} break;79case nir_intrinsic_load_vertex_id:80case nir_intrinsic_load_instance_id:81/* detect use of vertex_id/instance_id */82v->vs_id_in_reg = v->infile.num_reg;83break;84default:85break;86}87}8889if (instr->type != nir_instr_type_tex)90continue;9192nir_tex_instr *tex = nir_instr_as_tex(instr);93nir_src *coord = NULL;94nir_src *lod_bias = NULL;95unsigned lod_bias_idx;9697assert(tex->sampler_index == tex->texture_index);9899for (unsigned i = 0; i < tex->num_srcs; i++) {100switch (tex->src[i].src_type) {101case nir_tex_src_coord:102coord = &tex->src[i].src;103break;104case nir_tex_src_bias:105case nir_tex_src_lod:106assert(!lod_bias);107lod_bias = &tex->src[i].src;108lod_bias_idx = i;109break;110case nir_tex_src_comparator:111break;112default:113assert(0);114break;115}116}117118/* pre HALTI5 needs texture sources in a single source */119120if (!lod_bias || v->shader->specs->halti >= 5)121continue;122123assert(coord && lod_bias && tex->coord_components < 4);124125nir_alu_instr *vec = nir_alu_instr_create(shader, nir_op_vec4);126for (unsigned i = 0; i < tex->coord_components; i++) {127vec->src[i].src = nir_src_for_ssa(coord->ssa);128vec->src[i].swizzle[0] = i;129}130for (unsigned i = tex->coord_components; i < 4; i++)131vec->src[i].src = nir_src_for_ssa(lod_bias->ssa);132133vec->dest.write_mask = 0xf;134nir_ssa_dest_init(&vec->instr, &vec->dest.dest, 4, 32, NULL);135136nir_tex_instr_remove_src(tex, lod_bias_idx);137nir_instr_rewrite_src(&tex->instr, coord, nir_src_for_ssa(&vec->dest.dest.ssa));138tex->coord_components = 4;139140nir_instr_insert_before(&tex->instr, &vec->instr);141}142}143}144}145146static void147etna_lower_alu_impl(nir_function_impl *impl, bool has_new_transcendentals)148{149nir_shader *shader = impl->function->shader;150151nir_builder b;152nir_builder_init(&b, impl);153154/* in a seperate loop so we can apply the multiple-uniform logic to the new fmul */155nir_foreach_block(block, impl) {156nir_foreach_instr_safe(instr, block) {157if (instr->type != nir_instr_type_alu)158continue;159160nir_alu_instr *alu = nir_instr_as_alu(instr);161/* multiply sin/cos src by constant162* TODO: do this earlier (but it breaks const_prop opt)163*/164if (alu->op == nir_op_fsin || alu->op == nir_op_fcos) {165b.cursor = nir_before_instr(instr);166167nir_ssa_def *imm = has_new_transcendentals ?168nir_imm_float(&b, 1.0 / M_PI) :169nir_imm_float(&b, 2.0 / M_PI);170171nir_instr_rewrite_src(instr, &alu->src[0].src,172nir_src_for_ssa(nir_fmul(&b, alu->src[0].src.ssa, imm)));173}174175/* change transcendental ops to vec2 and insert vec1 mul for the result176* TODO: do this earlier (but it breaks with optimizations)177*/178if (has_new_transcendentals && (179alu->op == nir_op_fdiv || alu->op == nir_op_flog2 ||180alu->op == nir_op_fsin || alu->op == nir_op_fcos)) {181nir_ssa_def *ssa = &alu->dest.dest.ssa;182183assert(ssa->num_components == 1);184185nir_alu_instr *mul = nir_alu_instr_create(shader, nir_op_fmul);186mul->src[0].src = mul->src[1].src = nir_src_for_ssa(ssa);187mul->src[1].swizzle[0] = 1;188189mul->dest.write_mask = 1;190nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, 32, NULL);191192ssa->num_components = 2;193194mul->dest.saturate = alu->dest.saturate;195alu->dest.saturate = 0;196197nir_instr_insert_after(instr, &mul->instr);198199nir_ssa_def_rewrite_uses_after(ssa, &mul->dest.dest.ssa,200&mul->instr);201}202}203}204}205206void207etna_lower_alu(nir_shader *shader, bool has_new_transcendentals)208{209nir_foreach_function(function, shader) {210if (function->impl)211etna_lower_alu_impl(function->impl, has_new_transcendentals);212}213}214215216