Path: blob/21.2-virgl/src/broadcom/compiler/v3d_nir_lower_logic_ops.c
4564 views
/*1* Copyright © 2019 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/**24* Implements lowering for logical operations.25*26* V3D doesn't have any hardware support for logic ops. Instead, you read the27* current contents of the destination from the tile buffer, then do math using28* your output color and that destination value, and update the output color29* appropriately.30*/3132#include "util/format/u_format.h"33#include "compiler/nir/nir_builder.h"34#include "compiler/nir/nir_format_convert.h"35#include "v3d_compiler.h"363738typedef nir_ssa_def *(*nir_pack_func)(nir_builder *b, nir_ssa_def *c);39typedef nir_ssa_def *(*nir_unpack_func)(nir_builder *b, nir_ssa_def *c);4041static bool42logicop_depends_on_dst_color(int logicop_func)43{44switch (logicop_func) {45case PIPE_LOGICOP_SET:46case PIPE_LOGICOP_CLEAR:47case PIPE_LOGICOP_COPY:48case PIPE_LOGICOP_COPY_INVERTED:49return false;50default:51return true;52}53}5455static nir_ssa_def *56v3d_logicop(nir_builder *b, int logicop_func,57nir_ssa_def *src, nir_ssa_def *dst)58{59switch (logicop_func) {60case PIPE_LOGICOP_CLEAR:61return nir_imm_int(b, 0);62case PIPE_LOGICOP_NOR:63return nir_inot(b, nir_ior(b, src, dst));64case PIPE_LOGICOP_AND_INVERTED:65return nir_iand(b, nir_inot(b, src), dst);66case PIPE_LOGICOP_COPY_INVERTED:67return nir_inot(b, src);68case PIPE_LOGICOP_AND_REVERSE:69return nir_iand(b, src, nir_inot(b, dst));70case PIPE_LOGICOP_INVERT:71return nir_inot(b, dst);72case PIPE_LOGICOP_XOR:73return nir_ixor(b, src, dst);74case PIPE_LOGICOP_NAND:75return nir_inot(b, nir_iand(b, src, dst));76case PIPE_LOGICOP_AND:77return nir_iand(b, src, dst);78case PIPE_LOGICOP_EQUIV:79return nir_inot(b, nir_ixor(b, src, dst));80case PIPE_LOGICOP_NOOP:81return dst;82case PIPE_LOGICOP_OR_INVERTED:83return nir_ior(b, nir_inot(b, src), dst);84case PIPE_LOGICOP_OR_REVERSE:85return nir_ior(b, src, nir_inot(b, dst));86case PIPE_LOGICOP_OR:87return nir_ior(b, src, dst);88case PIPE_LOGICOP_SET:89return nir_imm_int(b, ~0);90default:91fprintf(stderr, "Unknown logic op %d\n", logicop_func);92FALLTHROUGH;93case PIPE_LOGICOP_COPY:94return src;95}96}9798static nir_ssa_def *99v3d_nir_get_swizzled_channel(nir_builder *b, nir_ssa_def **srcs, int swiz)100{101switch (swiz) {102default:103case PIPE_SWIZZLE_NONE:104fprintf(stderr, "warning: unknown swizzle\n");105FALLTHROUGH;106case PIPE_SWIZZLE_0:107return nir_imm_float(b, 0.0);108case PIPE_SWIZZLE_1:109return nir_imm_float(b, 1.0);110case PIPE_SWIZZLE_X:111case PIPE_SWIZZLE_Y:112case PIPE_SWIZZLE_Z:113case PIPE_SWIZZLE_W:114return srcs[swiz];115}116}117118static nir_ssa_def *119v3d_nir_swizzle_and_pack(nir_builder *b, nir_ssa_def **chans,120const uint8_t *swiz, nir_pack_func pack_func)121{122nir_ssa_def *c[4];123for (int i = 0; i < 4; i++)124c[i] = v3d_nir_get_swizzled_channel(b, chans, swiz[i]);125126return pack_func(b, nir_vec4(b, c[0], c[1], c[2], c[3]));127}128129static nir_ssa_def *130v3d_nir_unpack_and_swizzle(nir_builder *b, nir_ssa_def *packed,131const uint8_t *swiz, nir_unpack_func unpack_func)132{133nir_ssa_def *unpacked = unpack_func(b, packed);134135nir_ssa_def *unpacked_chans[4];136for (int i = 0; i < 4; i++)137unpacked_chans[i] = nir_channel(b, unpacked, i);138139nir_ssa_def *c[4];140for (int i = 0; i < 4; i++)141c[i] = v3d_nir_get_swizzled_channel(b, unpacked_chans, swiz[i]);142143return nir_vec4(b, c[0], c[1], c[2], c[3]);144}145146static nir_ssa_def *147pack_unorm_rgb10a2(nir_builder *b, nir_ssa_def *c)148{149static const unsigned bits[4] = { 10, 10, 10, 2 };150nir_ssa_def *unorm = nir_format_float_to_unorm(b, c, bits);151152nir_ssa_def *chans[4];153for (int i = 0; i < 4; i++)154chans[i] = nir_channel(b, unorm, i);155156nir_ssa_def *result = nir_mov(b, chans[0]);157int offset = bits[0];158for (int i = 1; i < 4; i++) {159nir_ssa_def *shifted_chan =160nir_ishl(b, chans[i], nir_imm_int(b, offset));161result = nir_ior(b, result, shifted_chan);162offset += bits[i];163}164return result;165}166167static nir_ssa_def *168unpack_unorm_rgb10a2(nir_builder *b, nir_ssa_def *c)169{170static const unsigned bits[4] = { 10, 10, 10, 2 };171const unsigned masks[4] = { BITFIELD_MASK(bits[0]),172BITFIELD_MASK(bits[1]),173BITFIELD_MASK(bits[2]),174BITFIELD_MASK(bits[3]) };175176nir_ssa_def *chans[4];177for (int i = 0; i < 4; i++) {178nir_ssa_def *unorm = nir_iand(b, c, nir_imm_int(b, masks[i]));179chans[i] = nir_format_unorm_to_float(b, unorm, &bits[i]);180c = nir_ushr(b, c, nir_imm_int(b, bits[i]));181}182183return nir_vec4(b, chans[0], chans[1], chans[2], chans[3]);184}185186static const uint8_t *187v3d_get_format_swizzle_for_rt(struct v3d_compile *c, int rt)188{189static const uint8_t ident[4] = { 0, 1, 2, 3 };190191/* We will automatically swap R and B channels for BGRA formats192* on tile loads and stores (see 'swap_rb' field in v3d_resource) so193* we want to treat these surfaces as if they were regular RGBA formats.194*/195if (c->fs_key->color_fmt[rt].swizzle[0] == 2 &&196c->fs_key->color_fmt[rt].format != PIPE_FORMAT_B5G6R5_UNORM) {197return ident;198} else {199return c->fs_key->color_fmt[rt].swizzle;200}201}202203static nir_ssa_def *204v3d_nir_get_tlb_color(nir_builder *b, struct v3d_compile *c, int rt, int sample)205{206uint32_t num_components =207util_format_get_nr_components(c->fs_key->color_fmt[rt].format);208209nir_ssa_def *color[4];210for (int i = 0; i < 4; i++) {211if (i < num_components) {212color[i] =213nir_load_tlb_color_v3d(b, 1, 32, nir_imm_int(b, rt),214.base = sample,215.component = i);216} else {217/* These will be DCEd */218color[i] = nir_imm_int(b, 0);219}220}221return nir_vec4(b, color[0], color[1], color[2], color[3]);222}223224static nir_ssa_def *225v3d_emit_logic_op_raw(struct v3d_compile *c, nir_builder *b,226nir_ssa_def **src_chans, nir_ssa_def **dst_chans,227int rt, int sample)228{229const uint8_t *fmt_swz = v3d_get_format_swizzle_for_rt(c, rt);230231nir_ssa_def *op_res[4];232for (int i = 0; i < 4; i++) {233nir_ssa_def *src = src_chans[i];234nir_ssa_def *dst =235v3d_nir_get_swizzled_channel(b, dst_chans, fmt_swz[i]);236op_res[i] = v3d_logicop(b, c->fs_key->logicop_func, src, dst);237238/* In Vulkan we configure our integer RTs to clamp, so we need239* to ignore result bits that don't fit in the destination RT240* component size.241*/242if (c->key->environment == V3D_ENVIRONMENT_VULKAN) {243uint32_t bits =244util_format_get_component_bits(245c->fs_key->color_fmt[rt].format,246UTIL_FORMAT_COLORSPACE_RGB, i);247if (bits > 0 && bits < 32) {248nir_ssa_def *mask =249nir_imm_int(b, (1u << bits) - 1);250op_res[i] = nir_iand(b, op_res[i], mask);251}252}253}254255nir_ssa_def *r[4];256for (int i = 0; i < 4; i++)257r[i] = v3d_nir_get_swizzled_channel(b, op_res, fmt_swz[i]);258259return nir_vec4(b, r[0], r[1], r[2], r[3]);260}261262static nir_ssa_def *263v3d_emit_logic_op_unorm(struct v3d_compile *c, nir_builder *b,264nir_ssa_def **src_chans, nir_ssa_def **dst_chans,265int rt, int sample,266nir_pack_func pack_func, nir_unpack_func unpack_func)267{268static const uint8_t src_swz[4] = { 0, 1, 2, 3 };269nir_ssa_def *packed_src =270v3d_nir_swizzle_and_pack(b, src_chans, src_swz, pack_func);271272const uint8_t *fmt_swz = v3d_get_format_swizzle_for_rt(c, rt);273nir_ssa_def *packed_dst =274v3d_nir_swizzle_and_pack(b, dst_chans, fmt_swz, pack_func);275276nir_ssa_def *packed_result =277v3d_logicop(b, c->fs_key->logicop_func, packed_src, packed_dst);278279return v3d_nir_unpack_and_swizzle(b, packed_result, fmt_swz, unpack_func);280}281282static nir_ssa_def *283v3d_nir_emit_logic_op(struct v3d_compile *c, nir_builder *b,284nir_ssa_def *src, int rt, int sample)285{286nir_ssa_def *dst = v3d_nir_get_tlb_color(b, c, rt, sample);287288nir_ssa_def *src_chans[4], *dst_chans[4];289for (unsigned i = 0; i < 4; i++) {290src_chans[i] = nir_channel(b, src, i);291dst_chans[i] = nir_channel(b, dst, i);292}293294if (c->fs_key->color_fmt[rt].format == PIPE_FORMAT_R10G10B10A2_UNORM) {295return v3d_emit_logic_op_unorm(296c, b, src_chans, dst_chans, rt, 0,297pack_unorm_rgb10a2, unpack_unorm_rgb10a2);298}299300if (util_format_is_unorm(c->fs_key->color_fmt[rt].format)) {301return v3d_emit_logic_op_unorm(302c, b, src_chans, dst_chans, rt, 0,303nir_pack_unorm_4x8, nir_unpack_unorm_4x8);304}305306return v3d_emit_logic_op_raw(c, b, src_chans, dst_chans, rt, 0);307}308309static void310v3d_emit_ms_output(nir_builder *b,311nir_ssa_def *color, nir_src *offset,312nir_alu_type type, int rt, int sample)313{314nir_store_tlb_sample_color_v3d(b, color, nir_imm_int(b, rt), .base = sample, .component = 0, .src_type = type);315}316317static void318v3d_nir_lower_logic_op_instr(struct v3d_compile *c,319nir_builder *b,320nir_intrinsic_instr *intr,321int rt)322{323nir_ssa_def *frag_color = intr->src[0].ssa;324325326const int logic_op = c->fs_key->logicop_func;327if (c->fs_key->msaa && logicop_depends_on_dst_color(logic_op)) {328c->msaa_per_sample_output = true;329330nir_src *offset = &intr->src[1];331nir_alu_type type = nir_intrinsic_src_type(intr);332for (int i = 0; i < V3D_MAX_SAMPLES; i++) {333nir_ssa_def *sample =334v3d_nir_emit_logic_op(c, b, frag_color, rt, i);335336v3d_emit_ms_output(b, sample, offset, type, rt, i);337}338339nir_instr_remove(&intr->instr);340} else {341nir_ssa_def *result =342v3d_nir_emit_logic_op(c, b, frag_color, rt, 0);343344nir_instr_rewrite_src(&intr->instr, &intr->src[0],345nir_src_for_ssa(result));346intr->num_components = result->num_components;347}348}349350static bool351v3d_nir_lower_logic_ops_block(nir_block *block, struct v3d_compile *c)352{353nir_foreach_instr_safe(instr, block) {354if (instr->type != nir_instr_type_intrinsic)355continue;356357nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);358if (intr->intrinsic != nir_intrinsic_store_output)359continue;360361nir_foreach_shader_out_variable(var, c->s) {362const int driver_loc = var->data.driver_location;363if (driver_loc != nir_intrinsic_base(intr))364continue;365366const int loc = var->data.location;367if (loc != FRAG_RESULT_COLOR &&368(loc < FRAG_RESULT_DATA0 ||369loc >= FRAG_RESULT_DATA0 + V3D_MAX_DRAW_BUFFERS)) {370continue;371}372373/* Logic operations do not apply on floating point or374* sRGB enabled render targets.375*/376const int rt = driver_loc;377assert(rt < V3D_MAX_DRAW_BUFFERS);378379const enum pipe_format format =380c->fs_key->color_fmt[rt].format;381if (util_format_is_float(format) ||382util_format_is_srgb(format)) {383continue;384}385386nir_function_impl *impl =387nir_cf_node_get_function(&block->cf_node);388nir_builder b;389nir_builder_init(&b, impl);390b.cursor = nir_before_instr(&intr->instr);391v3d_nir_lower_logic_op_instr(c, &b, intr, rt);392}393}394395return true;396}397398void399v3d_nir_lower_logic_ops(nir_shader *s, struct v3d_compile *c)400{401/* Nothing to do if logic op is 'copy src to dst' or if logic ops are402* disabled (we set the logic op to copy in that case).403*/404if (c->fs_key->logicop_func == PIPE_LOGICOP_COPY)405return;406407nir_foreach_function(function, s) {408if (function->impl) {409nir_foreach_block(block, function->impl)410v3d_nir_lower_logic_ops_block(block, c);411412nir_metadata_preserve(function->impl,413nir_metadata_block_index |414nir_metadata_dominance);415}416}417}418419420