Path: blob/21.2-virgl/src/freedreno/ir3/ir3_a4xx.c
4565 views
/*1* Copyright (C) 2017-2018 Rob Clark <[email protected]>2*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, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#define GPU 4002728#include "ir3_context.h"29#include "ir3_image.h"3031/*32* Handlers for instructions changed/added in a4xx:33*/3435/* src[] = { buffer_index, offset }. No const_index */36static void37emit_intrinsic_load_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr,38struct ir3_instruction **dst)39{40struct ir3_block *b = ctx->block;41struct ir3_instruction *ldgb, *src0, *src1, *byte_offset, *offset;4243struct ir3_instruction *ssbo = ir3_ssbo_to_ibo(ctx, intr->src[0]);4445byte_offset = ir3_get_src(ctx, &intr->src[1])[0];46offset = ir3_get_src(ctx, &intr->src[2])[0];4748/* src0 is uvec2(offset*4, 0), src1 is offset.. nir already *= 4: */49src0 = ir3_collect(ctx, byte_offset, create_immed(b, 0));50src1 = offset;5152ldgb = ir3_LDGB(b, ssbo, 0, src0, 0, src1, 0);53ldgb->dsts[0]->wrmask = MASK(intr->num_components);54ldgb->cat6.iim_val = intr->num_components;55ldgb->cat6.d = 4;56ldgb->cat6.type = TYPE_U32;57ldgb->barrier_class = IR3_BARRIER_BUFFER_R;58ldgb->barrier_conflict = IR3_BARRIER_BUFFER_W;5960ir3_split_dest(b, dst, ldgb, 0, intr->num_components);61}6263/* src[] = { value, block_index, offset }. const_index[] = { write_mask } */64static void65emit_intrinsic_store_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr)66{67struct ir3_block *b = ctx->block;68struct ir3_instruction *stgb, *src0, *src1, *src2, *byte_offset, *offset;69unsigned wrmask = nir_intrinsic_write_mask(intr);70unsigned ncomp = ffs(~wrmask) - 1;7172assert(wrmask == BITFIELD_MASK(intr->num_components));7374struct ir3_instruction *ssbo = ir3_ssbo_to_ibo(ctx, intr->src[1]);7576byte_offset = ir3_get_src(ctx, &intr->src[2])[0];77offset = ir3_get_src(ctx, &intr->src[3])[0];7879/* src0 is value, src1 is offset, src2 is uvec2(offset*4, 0)..80* nir already *= 4:81*/82src0 = ir3_create_collect(ctx, ir3_get_src(ctx, &intr->src[0]), ncomp);83src1 = offset;84src2 = ir3_collect(ctx, byte_offset, create_immed(b, 0));8586stgb = ir3_STGB(b, ssbo, 0, src0, 0, src1, 0, src2, 0);87stgb->cat6.iim_val = ncomp;88stgb->cat6.d = 4;89stgb->cat6.type = TYPE_U32;90stgb->barrier_class = IR3_BARRIER_BUFFER_W;91stgb->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;9293array_insert(b, b->keeps, stgb);94}9596/*97* SSBO atomic intrinsics98*99* All of the SSBO atomic memory operations read a value from memory,100* compute a new value using one of the operations below, write the new101* value to memory, and return the original value read.102*103* All operations take 3 sources except CompSwap that takes 4. These104* sources represent:105*106* 0: The SSBO buffer index.107* 1: The offset into the SSBO buffer of the variable that the atomic108* operation will operate on.109* 2: The data parameter to the atomic function (i.e. the value to add110* in ssbo_atomic_add, etc).111* 3: For CompSwap only: the second data parameter.112*/113static struct ir3_instruction *114emit_intrinsic_atomic_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr)115{116struct ir3_block *b = ctx->block;117struct ir3_instruction *atomic, *ssbo, *src0, *src1, *src2, *byte_offset,118*offset;119type_t type = TYPE_U32;120121ssbo = ir3_ssbo_to_ibo(ctx, intr->src[0]);122123byte_offset = ir3_get_src(ctx, &intr->src[1])[0];124offset = ir3_get_src(ctx, &intr->src[3])[0];125126/* src0 is data (or uvec2(data, compare))127* src1 is offset128* src2 is uvec2(offset*4, 0) (appears to be 64b byte offset)129*130* Note that nir already multiplies the offset by four131*/132src0 = ir3_get_src(ctx, &intr->src[2])[0];133src1 = offset;134src2 = ir3_collect(ctx, byte_offset, create_immed(b, 0));135136switch (intr->intrinsic) {137case nir_intrinsic_ssbo_atomic_add_ir3:138atomic = ir3_ATOMIC_ADD_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);139break;140case nir_intrinsic_ssbo_atomic_imin_ir3:141atomic = ir3_ATOMIC_MIN_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);142type = TYPE_S32;143break;144case nir_intrinsic_ssbo_atomic_umin_ir3:145atomic = ir3_ATOMIC_MIN_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);146break;147case nir_intrinsic_ssbo_atomic_imax_ir3:148atomic = ir3_ATOMIC_MAX_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);149type = TYPE_S32;150break;151case nir_intrinsic_ssbo_atomic_umax_ir3:152atomic = ir3_ATOMIC_MAX_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);153break;154case nir_intrinsic_ssbo_atomic_and_ir3:155atomic = ir3_ATOMIC_AND_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);156break;157case nir_intrinsic_ssbo_atomic_or_ir3:158atomic = ir3_ATOMIC_OR_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);159break;160case nir_intrinsic_ssbo_atomic_xor_ir3:161atomic = ir3_ATOMIC_XOR_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);162break;163case nir_intrinsic_ssbo_atomic_exchange_ir3:164atomic = ir3_ATOMIC_XCHG_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);165break;166case nir_intrinsic_ssbo_atomic_comp_swap_ir3:167/* for cmpxchg, src0 is [ui]vec2(data, compare): */168src0 = ir3_collect(ctx, ir3_get_src(ctx, &intr->src[3])[0], src0);169src1 = ir3_get_src(ctx, &intr->src[4])[0];170atomic = ir3_ATOMIC_CMPXCHG_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);171break;172default:173unreachable("boo");174}175176atomic->cat6.iim_val = 1;177atomic->cat6.d = 4;178atomic->cat6.type = type;179atomic->barrier_class = IR3_BARRIER_BUFFER_W;180atomic->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;181182/* even if nothing consume the result, we can't DCE the instruction: */183array_insert(b, b->keeps, atomic);184185return atomic;186}187188static struct ir3_instruction *189get_image_offset(struct ir3_context *ctx, const nir_intrinsic_instr *instr,190struct ir3_instruction *const *coords, bool byteoff)191{192struct ir3_block *b = ctx->block;193struct ir3_instruction *offset;194unsigned index = nir_src_as_uint(instr->src[0]);195unsigned ncoords = ir3_get_image_coords(instr, NULL);196197/* to calculate the byte offset (yes, uggg) we need (up to) three198* const values to know the bytes per pixel, and y and z stride:199*/200const struct ir3_const_state *const_state = ir3_const_state(ctx->so);201unsigned cb = regid(const_state->offsets.image_dims, 0) +202const_state->image_dims.off[index];203204debug_assert(const_state->image_dims.mask & (1 << index));205206/* offset = coords.x * bytes_per_pixel: */207offset = ir3_MUL_S24(b, coords[0], 0, create_uniform(b, cb + 0), 0);208if (ncoords > 1) {209/* offset += coords.y * y_pitch: */210offset =211ir3_MAD_S24(b, create_uniform(b, cb + 1), 0, coords[1], 0, offset, 0);212}213if (ncoords > 2) {214/* offset += coords.z * z_pitch: */215offset =216ir3_MAD_S24(b, create_uniform(b, cb + 2), 0, coords[2], 0, offset, 0);217}218219if (!byteoff) {220/* Some cases, like atomics, seem to use dword offset instead221* of byte offsets.. blob just puts an extra shr.b in there222* in those cases:223*/224offset = ir3_SHR_B(b, offset, 0, create_immed(b, 2), 0);225}226227return ir3_collect(ctx, offset, create_immed(b, 0));228}229230/* src[] = { index, coord, sample_index, value }. const_index[] = {} */231static void232emit_intrinsic_store_image(struct ir3_context *ctx, nir_intrinsic_instr *intr)233{234struct ir3_block *b = ctx->block;235struct ir3_instruction *stib, *offset;236struct ir3_instruction *const *value = ir3_get_src(ctx, &intr->src[3]);237struct ir3_instruction *const *coords = ir3_get_src(ctx, &intr->src[1]);238struct ir3_instruction *ibo = ir3_image_to_ibo(ctx, intr->src[0]);239unsigned ncoords = ir3_get_image_coords(intr, NULL);240unsigned ncomp =241ir3_get_num_components_for_image_format(nir_intrinsic_format(intr));242243/* src0 is value244* src1 is coords245* src2 is 64b byte offset246*/247248offset = get_image_offset(ctx, intr, coords, true);249250/* NOTE: stib seems to take byte offset, but stgb.typed can be used251* too and takes a dword offset.. not quite sure yet why blob uses252* one over the other in various cases.253*/254255stib = ir3_STIB(b, ibo, 0, ir3_create_collect(ctx, value, ncomp), 0,256ir3_create_collect(ctx, coords, ncoords), 0, offset, 0);257stib->cat6.iim_val = ncomp;258stib->cat6.d = ncoords;259stib->cat6.type = ir3_get_type_for_image_intrinsic(intr);260stib->cat6.typed = true;261stib->barrier_class = IR3_BARRIER_IMAGE_W;262stib->barrier_conflict = IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W;263264array_insert(b, b->keeps, stib);265}266267/* src[] = { deref, coord, sample_index, value, compare }. const_index[] = {} */268static struct ir3_instruction *269emit_intrinsic_atomic_image(struct ir3_context *ctx, nir_intrinsic_instr *intr)270{271struct ir3_block *b = ctx->block;272struct ir3_instruction *atomic, *src0, *src1, *src2;273struct ir3_instruction *const *coords = ir3_get_src(ctx, &intr->src[1]);274struct ir3_instruction *image = ir3_image_to_ibo(ctx, intr->src[0]);275unsigned ncoords = ir3_get_image_coords(intr, NULL);276277/* src0 is value (or uvec2(value, compare))278* src1 is coords279* src2 is 64b byte offset280*/281src0 = ir3_get_src(ctx, &intr->src[3])[0];282src1 = ir3_create_collect(ctx, coords, ncoords);283src2 = get_image_offset(ctx, intr, coords, false);284285switch (intr->intrinsic) {286case nir_intrinsic_image_atomic_add:287atomic = ir3_ATOMIC_ADD_G(b, image, 0, src0, 0, src1, 0, src2, 0);288break;289case nir_intrinsic_image_atomic_imin:290case nir_intrinsic_image_atomic_umin:291atomic = ir3_ATOMIC_MIN_G(b, image, 0, src0, 0, src1, 0, src2, 0);292break;293case nir_intrinsic_image_atomic_imax:294case nir_intrinsic_image_atomic_umax:295atomic = ir3_ATOMIC_MAX_G(b, image, 0, src0, 0, src1, 0, src2, 0);296break;297case nir_intrinsic_image_atomic_and:298atomic = ir3_ATOMIC_AND_G(b, image, 0, src0, 0, src1, 0, src2, 0);299break;300case nir_intrinsic_image_atomic_or:301atomic = ir3_ATOMIC_OR_G(b, image, 0, src0, 0, src1, 0, src2, 0);302break;303case nir_intrinsic_image_atomic_xor:304atomic = ir3_ATOMIC_XOR_G(b, image, 0, src0, 0, src1, 0, src2, 0);305break;306case nir_intrinsic_image_atomic_exchange:307atomic = ir3_ATOMIC_XCHG_G(b, image, 0, src0, 0, src1, 0, src2, 0);308break;309case nir_intrinsic_image_atomic_comp_swap:310/* for cmpxchg, src0 is [ui]vec2(data, compare): */311src0 = ir3_collect(ctx, ir3_get_src(ctx, &intr->src[4])[0], src0);312atomic = ir3_ATOMIC_CMPXCHG_G(b, image, 0, src0, 0, src1, 0, src2, 0);313break;314default:315unreachable("boo");316}317318atomic->cat6.iim_val = 1;319atomic->cat6.d = ncoords;320atomic->cat6.type = ir3_get_type_for_image_intrinsic(intr);321atomic->cat6.typed = true;322atomic->barrier_class = IR3_BARRIER_IMAGE_W;323atomic->barrier_conflict = IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W;324325/* even if nothing consume the result, we can't DCE the instruction: */326array_insert(b, b->keeps, atomic);327328return atomic;329}330331const struct ir3_context_funcs ir3_a4xx_funcs = {332.emit_intrinsic_load_ssbo = emit_intrinsic_load_ssbo,333.emit_intrinsic_store_ssbo = emit_intrinsic_store_ssbo,334.emit_intrinsic_atomic_ssbo = emit_intrinsic_atomic_ssbo,335.emit_intrinsic_store_image = emit_intrinsic_store_image,336.emit_intrinsic_atomic_image = emit_intrinsic_atomic_image,337.emit_intrinsic_image_size = emit_intrinsic_image_size_tex,338.emit_intrinsic_load_global_ir3 = NULL,339.emit_intrinsic_store_global_ir3 = NULL,340};341342343