Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_nir_lower_io.c
4570 views
/*1* Copyright © 2015 Broadcom2*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, sublicense,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 the next11* paragraph) shall be included in all copies or substantial portions of the12* 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 NONINFRINGEMENT. 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 OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "vc4_qir.h"24#include "compiler/nir/nir_builder.h"25#include "util/format/u_format.h"26#include "util/u_helpers.h"2728/**29* Walks the NIR generated by TGSI-to-NIR or GLSL-to-NIR to lower its io30* intrinsics into something amenable to the VC4 architecture.31*32* Currently, it splits VS inputs and uniforms into scalars, drops any33* non-position outputs in coordinate shaders, and fixes up the addressing on34* indirect uniform loads. FS input and VS output scalarization is handled by35* nir_lower_io_to_scalar().36*/3738static void39replace_intrinsic_with_vec(nir_builder *b, nir_intrinsic_instr *intr,40nir_ssa_def **comps)41{4243/* Batch things back together into a vector. This will get split by44* the later ALU scalarization pass.45*/46nir_ssa_def *vec = nir_vec(b, comps, intr->num_components);4748/* Replace the old intrinsic with a reference to our reconstructed49* vector.50*/51nir_ssa_def_rewrite_uses(&intr->dest.ssa, vec);52nir_instr_remove(&intr->instr);53}5455static nir_ssa_def *56vc4_nir_unpack_8i(nir_builder *b, nir_ssa_def *src, unsigned chan)57{58return nir_ubitfield_extract(b,59src,60nir_imm_int(b, 8 * chan),61nir_imm_int(b, 8));62}6364/** Returns the 16 bit field as a sign-extended 32-bit value. */65static nir_ssa_def *66vc4_nir_unpack_16i(nir_builder *b, nir_ssa_def *src, unsigned chan)67{68return nir_ibitfield_extract(b,69src,70nir_imm_int(b, 16 * chan),71nir_imm_int(b, 16));72}7374/** Returns the 16 bit field as an unsigned 32 bit value. */75static nir_ssa_def *76vc4_nir_unpack_16u(nir_builder *b, nir_ssa_def *src, unsigned chan)77{78if (chan == 0) {79return nir_iand(b, src, nir_imm_int(b, 0xffff));80} else {81return nir_ushr(b, src, nir_imm_int(b, 16));82}83}8485static nir_ssa_def *86vc4_nir_unpack_8f(nir_builder *b, nir_ssa_def *src, unsigned chan)87{88return nir_channel(b, nir_unpack_unorm_4x8(b, src), chan);89}9091static nir_ssa_def *92vc4_nir_get_vattr_channel_vpm(struct vc4_compile *c,93nir_builder *b,94nir_ssa_def **vpm_reads,95uint8_t swiz,96const struct util_format_description *desc)97{98const struct util_format_channel_description *chan =99&desc->channel[swiz];100nir_ssa_def *temp;101102if (swiz > PIPE_SWIZZLE_W) {103return vc4_nir_get_swizzled_channel(b, vpm_reads, swiz);104} else if (chan->size == 32 && chan->type == UTIL_FORMAT_TYPE_FLOAT) {105return vc4_nir_get_swizzled_channel(b, vpm_reads, swiz);106} else if (chan->size == 32 && chan->type == UTIL_FORMAT_TYPE_SIGNED) {107if (chan->normalized) {108return nir_fmul(b,109nir_i2f32(b, vpm_reads[swiz]),110nir_imm_float(b,1111.0 / 0x7fffffff));112} else {113return nir_i2f32(b, vpm_reads[swiz]);114}115} else if (chan->size == 8 &&116(chan->type == UTIL_FORMAT_TYPE_UNSIGNED ||117chan->type == UTIL_FORMAT_TYPE_SIGNED)) {118nir_ssa_def *vpm = vpm_reads[0];119if (chan->type == UTIL_FORMAT_TYPE_SIGNED) {120temp = nir_ixor(b, vpm, nir_imm_int(b, 0x80808080));121if (chan->normalized) {122return nir_fsub(b, nir_fmul(b,123vc4_nir_unpack_8f(b, temp, swiz),124nir_imm_float(b, 2.0)),125nir_imm_float(b, 1.0));126} else {127return nir_fadd(b,128nir_i2f32(b,129vc4_nir_unpack_8i(b, temp,130swiz)),131nir_imm_float(b, -128.0));132}133} else {134if (chan->normalized) {135return vc4_nir_unpack_8f(b, vpm, swiz);136} else {137return nir_i2f32(b, vc4_nir_unpack_8i(b, vpm, swiz));138}139}140} else if (chan->size == 16 &&141(chan->type == UTIL_FORMAT_TYPE_UNSIGNED ||142chan->type == UTIL_FORMAT_TYPE_SIGNED)) {143nir_ssa_def *vpm = vpm_reads[swiz / 2];144145/* Note that UNPACK_16F eats a half float, not ints, so we use146* UNPACK_16_I for all of these.147*/148if (chan->type == UTIL_FORMAT_TYPE_SIGNED) {149temp = nir_i2f32(b, vc4_nir_unpack_16i(b, vpm, swiz & 1));150if (chan->normalized) {151return nir_fmul(b, temp,152nir_imm_float(b, 1/32768.0f));153} else {154return temp;155}156} else {157temp = nir_i2f32(b, vc4_nir_unpack_16u(b, vpm, swiz & 1));158if (chan->normalized) {159return nir_fmul(b, temp,160nir_imm_float(b, 1 / 65535.0));161} else {162return temp;163}164}165} else {166return NULL;167}168}169170static void171vc4_nir_lower_vertex_attr(struct vc4_compile *c, nir_builder *b,172nir_intrinsic_instr *intr)173{174b->cursor = nir_before_instr(&intr->instr);175176int attr = nir_intrinsic_base(intr);177enum pipe_format format = c->vs_key->attr_formats[attr];178uint32_t attr_size = util_format_get_blocksize(format);179180/* We only accept direct outputs and TGSI only ever gives them to us181* with an offset value of 0.182*/183assert(nir_src_as_uint(intr->src[0]) == 0);184185/* Generate dword loads for the VPM values (Since these intrinsics may186* be reordered, the actual reads will be generated at the top of the187* shader by ntq_setup_inputs().188*/189nir_ssa_def *vpm_reads[4];190for (int i = 0; i < align(attr_size, 4) / 4; i++)191vpm_reads[i] = nir_load_input(b, 1, 32, nir_imm_int(b, 0),192.base = nir_intrinsic_base(intr),193.component = i);194195bool format_warned = false;196const struct util_format_description *desc =197util_format_description(format);198199nir_ssa_def *dests[4];200for (int i = 0; i < intr->num_components; i++) {201uint8_t swiz = desc->swizzle[i];202dests[i] = vc4_nir_get_vattr_channel_vpm(c, b, vpm_reads, swiz,203desc);204205if (!dests[i]) {206if (!format_warned) {207fprintf(stderr,208"vtx element %d unsupported type: %s\n",209attr, util_format_name(format));210format_warned = true;211}212dests[i] = nir_imm_float(b, 0.0);213}214}215216replace_intrinsic_with_vec(b, intr, dests);217}218219static void220vc4_nir_lower_fs_input(struct vc4_compile *c, nir_builder *b,221nir_intrinsic_instr *intr)222{223b->cursor = nir_after_instr(&intr->instr);224225if (nir_intrinsic_base(intr) >= VC4_NIR_TLB_COLOR_READ_INPUT &&226nir_intrinsic_base(intr) < (VC4_NIR_TLB_COLOR_READ_INPUT +227VC4_MAX_SAMPLES)) {228/* This doesn't need any lowering. */229return;230}231232nir_variable *input_var =233nir_find_variable_with_driver_location(c->s, nir_var_shader_in,234nir_intrinsic_base(intr));235assert(input_var);236237int comp = nir_intrinsic_component(intr);238239/* Lower away point coordinates, and fix up PNTC. */240if (util_varying_is_point_coord(input_var->data.location,241c->fs_key->point_sprite_mask)) {242assert(intr->num_components == 1);243244nir_ssa_def *result = &intr->dest.ssa;245246switch (comp) {247case 0:248case 1:249/* If we're not rendering points, we need to set a250* defined value for the input that would come from251* PNTC.252*/253if (!c->fs_key->is_points)254result = nir_imm_float(b, 0.0);255break;256case 2:257result = nir_imm_float(b, 0.0);258break;259case 3:260result = nir_imm_float(b, 1.0);261break;262}263264if (c->fs_key->point_coord_upper_left && comp == 1)265result = nir_fsub(b, nir_imm_float(b, 1.0), result);266267if (result != &intr->dest.ssa) {268nir_ssa_def_rewrite_uses_after(&intr->dest.ssa,269result,270result->parent_instr);271}272}273}274275static void276vc4_nir_lower_output(struct vc4_compile *c, nir_builder *b,277nir_intrinsic_instr *intr)278{279nir_variable *output_var =280nir_find_variable_with_driver_location(c->s, nir_var_shader_out,281nir_intrinsic_base(intr));282assert(output_var);283284if (c->stage == QSTAGE_COORD &&285output_var->data.location != VARYING_SLOT_POS &&286output_var->data.location != VARYING_SLOT_PSIZ) {287nir_instr_remove(&intr->instr);288return;289}290}291292static void293vc4_nir_lower_uniform(struct vc4_compile *c, nir_builder *b,294nir_intrinsic_instr *intr)295{296b->cursor = nir_before_instr(&intr->instr);297298/* Generate scalar loads equivalent to the original vector. */299nir_ssa_def *dests[4];300for (unsigned i = 0; i < intr->num_components; i++) {301nir_intrinsic_instr *intr_comp =302nir_intrinsic_instr_create(c->s, intr->intrinsic);303intr_comp->num_components = 1;304nir_ssa_dest_init(&intr_comp->instr, &intr_comp->dest, 1,305intr->dest.ssa.bit_size, NULL);306307/* Convert the uniform offset to bytes. If it happens308* to be a constant, constant-folding will clean up309* the shift for us.310*/311nir_intrinsic_set_base(intr_comp,312nir_intrinsic_base(intr) * 16 +313i * 4);314nir_intrinsic_set_range(intr_comp,315nir_intrinsic_range(intr) * 16 - i * 4);316317intr_comp->src[0] =318nir_src_for_ssa(nir_ishl(b, intr->src[0].ssa,319nir_imm_int(b, 4)));320321dests[i] = &intr_comp->dest.ssa;322323nir_builder_instr_insert(b, &intr_comp->instr);324}325326replace_intrinsic_with_vec(b, intr, dests);327}328329static void330vc4_nir_lower_io_instr(struct vc4_compile *c, nir_builder *b,331struct nir_instr *instr)332{333if (instr->type != nir_instr_type_intrinsic)334return;335nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);336337switch (intr->intrinsic) {338case nir_intrinsic_load_input:339if (c->stage == QSTAGE_FRAG)340vc4_nir_lower_fs_input(c, b, intr);341else342vc4_nir_lower_vertex_attr(c, b, intr);343break;344345case nir_intrinsic_store_output:346vc4_nir_lower_output(c, b, intr);347break;348349case nir_intrinsic_load_uniform:350vc4_nir_lower_uniform(c, b, intr);351break;352353case nir_intrinsic_load_user_clip_plane:354default:355break;356}357}358359static bool360vc4_nir_lower_io_impl(struct vc4_compile *c, nir_function_impl *impl)361{362nir_builder b;363nir_builder_init(&b, impl);364365nir_foreach_block(block, impl) {366nir_foreach_instr_safe(instr, block)367vc4_nir_lower_io_instr(c, &b, instr);368}369370nir_metadata_preserve(impl, nir_metadata_block_index |371nir_metadata_dominance);372373return true;374}375376void377vc4_nir_lower_io(nir_shader *s, struct vc4_compile *c)378{379nir_foreach_function(function, s) {380if (function->impl)381vc4_nir_lower_io_impl(c, function->impl);382}383}384385386