Path: blob/21.2-virgl/src/panfrost/bifrost/bifrost.h
4564 views
/*1* Copyright (C) 2019 Connor Abbott <[email protected]>2* Copyright (C) 2019 Lyude Paul <[email protected]>3* Copyright (C) 2019 Ryan Houdek <[email protected]>4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice (including the next13* paragraph) shall be included in all copies or substantial portions of the14* Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22* SOFTWARE.23*/2425#ifndef __bifrost_h__26#define __bifrost_h__2728#include <stdint.h>29#include <stdbool.h>30#include <assert.h>3132#define BIFROST_DBG_MSGS 0x000133#define BIFROST_DBG_SHADERS 0x000234#define BIFROST_DBG_SHADERDB 0x000435#define BIFROST_DBG_VERBOSE 0x000836#define BIFROST_DBG_INTERNAL 0x001037#define BIFROST_DBG_NOSCHED 0x002038#define BIFROST_DBG_INORDER 0x00403940extern int bifrost_debug;4142enum bifrost_message_type {43BIFROST_MESSAGE_NONE = 0,44BIFROST_MESSAGE_VARYING = 1,45BIFROST_MESSAGE_ATTRIBUTE = 2,46BIFROST_MESSAGE_TEX = 3,47BIFROST_MESSAGE_VARTEX = 4,48BIFROST_MESSAGE_LOAD = 5,49BIFROST_MESSAGE_STORE = 6,50BIFROST_MESSAGE_ATOMIC = 7,51BIFROST_MESSAGE_BARRIER = 8,52BIFROST_MESSAGE_BLEND = 9,53BIFROST_MESSAGE_TILE = 10,54/* type 11 reserved */55BIFROST_MESSAGE_Z_STENCIL = 12,56BIFROST_MESSAGE_ATEST = 13,57BIFROST_MESSAGE_JOB = 14,58BIFROST_MESSAGE_64BIT = 1559};6061enum bifrost_ftz {62BIFROST_FTZ_DISABLE = 0,63BIFROST_FTZ_DX11 = 1,64BIFROST_FTZ_ALWAYS = 2,65BIFROST_FTZ_ABRUPT = 366};6768enum bifrost_exceptions {69BIFROST_EXCEPTIONS_ENABLED = 0,70BIFROST_EXCEPTIONS_DISABLED = 1,71BIFROST_EXCEPTIONS_PRECISE_DIVISION = 2,72BIFROST_EXCEPTIONS_PRECISE_SQRT = 3,73};7475/* Describes clause flow control, with respect to control flow and branch76* reconvergence.77*78* Control flow may be considered back-to-back (execute clauses back-to-back),79* non-back-to-back (switch warps after clause before the next clause), write80* elision (back-to-back and elide register slot #3 write from the clause), or81* end of shader.82*83* Branch reconvergence may be disabled, enabled unconditionally, or enabled84* based on the program counter. A clause requires reconvergence if it has a85* successor that can be executed without first executing the clause itself.86* Separate iterations of a loop are treated separately here, so it is also the87* case for a loop exit where the iteration count is not warp-invariant.88*89*/9091enum bifrost_flow {92/* End-of-shader */93BIFROST_FLOW_END = 0,9495/* Non back-to-back, PC-encoded reconvergence */96BIFROST_FLOW_NBTB_PC = 1,9798/* Non back-to-back, unconditional reconvergence */99BIFROST_FLOW_NBTB_UNCONDITIONAL = 2,100101/* Non back-to-back, no reconvergence */102BIFROST_FLOW_NBTB = 3,103104/* Back-to-back, unconditional reconvergence */105BIFROST_FLOW_BTB_UNCONDITIONAL = 4,106107/* Back-to-back, no reconvergence */108BIFROST_FLOW_BTB_NONE = 5,109110/* Write elision, unconditional reconvergence */111BIFROST_FLOW_WE_UNCONDITIONAL = 6,112113/* Write elision, no reconvergence */114BIFROST_FLOW_WE = 7,115};116117enum bifrost_slot {118/* 0-5 are general purpose */119BIFROST_SLOT_ELDEST_DEPTH = 6,120BIFROST_SLOT_ELDEST_COLOUR = 7,121};122123struct bifrost_header {124/* Reserved */125unsigned zero1 : 5;126127/* Flush-to-zero mode, leave zero for GL */128enum bifrost_ftz flush_to_zero : 2;129130/* Convert any infinite result of any floating-point operation to the131* biggest representable number */132unsigned suppress_inf: 1;133134/* Convert NaN to +0.0 */135unsigned suppress_nan : 1;136137/* Floating-point excception handling mode */138enum bifrost_exceptions float_exceptions : 2;139140/* Enum describing the flow control, which matters for handling141* divergence and reconvergence efficiently */142enum bifrost_flow flow_control : 3;143144/* Reserved */145unsigned zero2 : 1;146147/* Terminate discarded threads, rather than continuing execution. Set148* for fragment shaders for standard GL behaviour of DISCARD. Also in a149* fragment shader, this disables helper invocations, so cannot be used150* in a shader that requires derivatives or texture LOD computation */151unsigned terminate_discarded_threads : 1;152153/* If set, the hardware may prefetch the next clause. If false, the154* hardware may not. Clear for unconditional branches. */155unsigned next_clause_prefetch : 1;156157/* If set, a barrier will be inserted after the clause waiting for all158* message passing instructions to read their staging registers, such159* that it is safe for the next clause to write them. */160unsigned staging_barrier: 1;161unsigned staging_register : 6;162163/* Slots to wait on and slot to be used for message passing164* instructions respectively */165unsigned dependency_wait : 8;166unsigned dependency_slot : 3;167168enum bifrost_message_type message_type : 5;169enum bifrost_message_type next_message_type : 5;170} __attribute__((packed));171172enum bifrost_packed_src {173BIFROST_SRC_PORT0 = 0,174BIFROST_SRC_PORT1 = 1,175BIFROST_SRC_PORT2 = 2,176BIFROST_SRC_STAGE = 3,177BIFROST_SRC_FAU_LO = 4,178BIFROST_SRC_FAU_HI = 5,179BIFROST_SRC_PASS_FMA = 6,180BIFROST_SRC_PASS_ADD = 7,181};182183struct bifrost_fma_inst {184unsigned src0 : 3;185unsigned op : 20;186} __attribute__((packed));187188struct bifrost_add_inst {189unsigned src0 : 3;190unsigned op : 17;191} __attribute__((packed));192193enum branch_bit_size {194BR_SIZE_32 = 0,195BR_SIZE_16XX = 1,196BR_SIZE_16YY = 2,197// For the above combinations of bitsize and location, an extra bit is198// encoded via comparing the sources. The only possible source of ambiguity199// would be if the sources were the same, but then the branch condition200// would be always true or always false anyways, so we can ignore it. But201// this no longer works when comparing the y component to the x component,202// since it's valid to compare the y component of a source against its own203// x component. Instead, the extra bit is encoded via an extra bitsize.204BR_SIZE_16YX0 = 3,205BR_SIZE_16YX1 = 4,206BR_SIZE_32_AND_16X = 5,207BR_SIZE_32_AND_16Y = 6,208// Used for comparisons with zero and always-true, see below. I think this209// only works for integer comparisons.210BR_SIZE_ZERO = 7,211};212213struct bifrost_regs {214unsigned fau_idx : 8;215unsigned reg3 : 6;216unsigned reg2 : 6;217unsigned reg0 : 5;218unsigned reg1 : 6;219unsigned ctrl : 4;220} __attribute__((packed));221222#define BIFROST_FMTC_CONSTANTS 0b0011223#define BIFROST_FMTC_FINAL 0b0111224225struct bifrost_fmt_constant {226unsigned pos : 4;227unsigned tag : 4;228uint64_t imm_1 : 60;229uint64_t imm_2 : 60;230} __attribute__((packed));231232/* Clause formats, encoded in a table */233234enum bi_clause_subword {235/* Literal 3-bit values */236BI_CLAUSE_SUBWORD_LITERAL_0 = 0,237/* etc */238BI_CLAUSE_SUBWORD_LITERAL_7 = 7,239240/* The value of the corresponding tuple in the corresponding bits */241BI_CLAUSE_SUBWORD_TUPLE_0 = 8,242/* etc */243BI_CLAUSE_SUBWORD_TUPLE_7 = 15,244245/* Clause header */246BI_CLAUSE_SUBWORD_HEADER = 16,247248/* Leave zero, but semantically distinct from literal 0 */249BI_CLAUSE_SUBWORD_RESERVED = 17,250251/* Embedded constant 0 */252BI_CLAUSE_SUBWORD_CONSTANT = 18,253254/* M bits controlling modifier for the constant */255BI_CLAUSE_SUBWORD_M = 19,256257/* Z bit: 1 to begin encoding constants, 0 to terminate the clause */258BI_CLAUSE_SUBWORD_Z = 20,259260/* Upper 3-bits of a given tuple and zero extended */261BI_CLAUSE_SUBWORD_UPPER_0 = 32,262/* etc */263BI_CLAUSE_SUBWORD_UPPER_7 = BI_CLAUSE_SUBWORD_UPPER_0 + 7,264265/* Upper 3-bits of two tuples, concatenated and zero-extended */266BI_CLAUSE_SUBWORD_UPPER_23 = BI_CLAUSE_SUBWORD_UPPER_0 + 23,267BI_CLAUSE_SUBWORD_UPPER_56 = BI_CLAUSE_SUBWORD_UPPER_0 + 56,268};269270#define L(x) (BI_CLAUSE_SUBWORD_LITERAL_0 + x)271#define U(x) (BI_CLAUSE_SUBWORD_UPPER_0 + x)272#define T(x) (BI_CLAUSE_SUBWORD_TUPLE_0 + x)273#define EC BI_CLAUSE_SUBWORD_CONSTANT274#define M BI_CLAUSE_SUBWORD_M275#define Z BI_CLAUSE_SUBWORD_Z276#define H BI_CLAUSE_SUBWORD_HEADER277#define R BI_CLAUSE_SUBWORD_RESERVED278279struct bi_clause_format {280unsigned format; /* format number */281unsigned pos; /* index in the clause */282enum bi_clause_subword tag_1; /* 2-bits */283enum bi_clause_subword tag_2; /* 3-bits */284enum bi_clause_subword tag_3; /* 3-bits */285enum bi_clause_subword s0_s3; /* 60 bits */286enum bi_clause_subword s4; /* 15 bits */287enum bi_clause_subword s5_s6; /* 30 bits */288enum bi_clause_subword s7; /* 15 bits */289};290291static const struct bi_clause_format bi_clause_formats[] = {292{ 0, 0, L(0), L(5), U(0), T(0), T(0), H, H },293{ 0, 0, Z, L(1), U(0), T(0), T(0), H, H },294{ 1, 1, Z, L(0), L(3), T(1), T(1), R, U(1) },295{ 2, 1, L(0), L(4), U(1), T(1), T(1), T(2), T(2) },296{ 3, 2, Z, L(0), L(4), EC, M, T(2), U(2) },297{ 4, 2, L(0), L(0), L(1), T(3), T(3), T(2), U(23) },298{ 4, 2, Z, L(0), L(5), T(3), T(3), T(2), U(23) },299{ 5, 2, L(2), U(3), U(2), T(3), T(3), T(2), EC },300{ 6, 3, Z, L(2), U(4), T(4), T(4), EC, EC },301{ 7, 3, L(1), L(4), U(4), T(4), T(4), T(5), T(5) },302{ 8, 4, Z, L(0), L(6), EC, M, T(5), U(5) },303{ 9, 4, Z, L(0), L(7), T(6), T(6), T(5), U(56) },304{ 10, 4, L(3), U(6), U(5), T(6), T(6), T(5), EC },305{ 11, 5, Z, L(3), U(7), T(7), T(7), EC, EC },306};307308#undef L309#undef U310#undef T311#undef EC312#undef M313#undef Z314#undef H315#undef R316317/* 32-bit modes for slots 2/3, as encoded in the register block. Other values318* are reserved. First part specifies behaviour of slot 2 (Idle, Read, Write319* Full, Write Low, Write High), second part behaviour of slot 3, and the last320* part specifies the source for the write (FMA, ADD, or MIX for FMA/ADD).321*322* IDLE is a special mode disabling both slots, except for the first323* instruction in the clause which uses IDLE_1 for the same purpose.324*325* All fields 0 used as sentinel for reserved encoding, so IDLE(_1) have FMA326* set (and ignored) as a placeholder to differentiate from reserved.327*/328enum bifrost_reg_mode {329BIFROST_R_WL_FMA = 1,330BIFROST_R_WH_FMA = 2,331BIFROST_R_W_FMA = 3,332BIFROST_R_WL_ADD = 4,333BIFROST_R_WH_ADD = 5,334BIFROST_R_W_ADD = 6,335BIFROST_WL_WL_ADD = 7,336BIFROST_WL_WH_ADD = 8,337BIFROST_WL_W_ADD = 9,338BIFROST_WH_WL_ADD = 10,339BIFROST_WH_WH_ADD = 11,340BIFROST_WH_W_ADD = 12,341BIFROST_W_WL_ADD = 13,342BIFROST_W_WH_ADD = 14,343BIFROST_W_W_ADD = 15,344BIFROST_IDLE_1 = 16,345BIFROST_I_W_FMA = 17,346BIFROST_I_WL_FMA = 18,347BIFROST_I_WH_FMA = 19,348BIFROST_R_I = 20,349BIFROST_I_W_ADD = 21,350BIFROST_I_WL_ADD = 22,351BIFROST_I_WH_ADD = 23,352BIFROST_WL_WH_MIX = 24,353BIFROST_WH_WL_MIX = 26,354BIFROST_IDLE = 27,355};356357enum bifrost_reg_op {358BIFROST_OP_IDLE = 0,359BIFROST_OP_READ = 1,360BIFROST_OP_WRITE = 2,361BIFROST_OP_WRITE_LO = 3,362BIFROST_OP_WRITE_HI = 4,363};364365struct bifrost_reg_ctrl_23 {366enum bifrost_reg_op slot2;367enum bifrost_reg_op slot3;368bool slot3_fma;369};370371static const struct bifrost_reg_ctrl_23 bifrost_reg_ctrl_lut[32] = {372[BIFROST_R_WL_FMA] = { BIFROST_OP_READ, BIFROST_OP_WRITE_LO, true },373[BIFROST_R_WH_FMA] = { BIFROST_OP_READ, BIFROST_OP_WRITE_HI, true },374[BIFROST_R_W_FMA] = { BIFROST_OP_READ, BIFROST_OP_WRITE, true },375[BIFROST_R_WL_ADD] = { BIFROST_OP_READ, BIFROST_OP_WRITE_LO, false },376[BIFROST_R_WH_ADD] = { BIFROST_OP_READ, BIFROST_OP_WRITE_HI, false },377[BIFROST_R_W_ADD] = { BIFROST_OP_READ, BIFROST_OP_WRITE, false },378[BIFROST_WL_WL_ADD] = { BIFROST_OP_WRITE_LO, BIFROST_OP_WRITE_LO, false },379[BIFROST_WL_WH_ADD] = { BIFROST_OP_WRITE_LO, BIFROST_OP_WRITE_HI, false },380[BIFROST_WL_W_ADD] = { BIFROST_OP_WRITE_LO, BIFROST_OP_WRITE, false },381[BIFROST_WH_WL_ADD] = { BIFROST_OP_WRITE_HI, BIFROST_OP_WRITE_LO, false },382[BIFROST_WH_WH_ADD] = { BIFROST_OP_WRITE_HI, BIFROST_OP_WRITE_HI, false },383[BIFROST_WH_W_ADD] = { BIFROST_OP_WRITE_HI, BIFROST_OP_WRITE, false },384[BIFROST_W_WL_ADD] = { BIFROST_OP_WRITE, BIFROST_OP_WRITE_LO, false },385[BIFROST_W_WH_ADD] = { BIFROST_OP_WRITE, BIFROST_OP_WRITE_HI, false },386[BIFROST_W_W_ADD] = { BIFROST_OP_WRITE, BIFROST_OP_WRITE, false },387[BIFROST_IDLE_1] = { BIFROST_OP_IDLE, BIFROST_OP_IDLE, true },388[BIFROST_I_W_FMA] = { BIFROST_OP_IDLE, BIFROST_OP_WRITE, true },389[BIFROST_I_WL_FMA] = { BIFROST_OP_IDLE, BIFROST_OP_WRITE_LO, true },390[BIFROST_I_WH_FMA] = { BIFROST_OP_IDLE, BIFROST_OP_WRITE_HI, true },391[BIFROST_R_I] = { BIFROST_OP_READ, BIFROST_OP_IDLE, false },392[BIFROST_I_W_ADD] = { BIFROST_OP_IDLE, BIFROST_OP_WRITE, false },393[BIFROST_I_WL_ADD] = { BIFROST_OP_IDLE, BIFROST_OP_WRITE_LO, false },394[BIFROST_I_WH_ADD] = { BIFROST_OP_IDLE, BIFROST_OP_WRITE_HI, false },395[BIFROST_WL_WH_MIX] = { BIFROST_OP_WRITE_LO, BIFROST_OP_WRITE_HI, false },396[BIFROST_WH_WL_MIX] = { BIFROST_OP_WRITE_HI, BIFROST_OP_WRITE_LO, false },397[BIFROST_IDLE] = { BIFROST_OP_IDLE, BIFROST_OP_IDLE, true },398};399400/* Texture operator descriptors in various states. Usually packed in the401* compiler and stored as a constant */402403enum bifrost_index {404/* Both texture/sampler index immediate */405BIFROST_INDEX_IMMEDIATE_SHARED = 0,406407/* Sampler index immediate, texture index from staging */408BIFROST_INDEX_IMMEDIATE_SAMPLER = 1,409410/* Texture index immediate, sampler index from staging */411BIFROST_INDEX_IMMEDIATE_TEXTURE = 2,412413/* Both indices from (separate) staging registers */414BIFROST_INDEX_REGISTER = 3,415};416417enum bifrost_tex_op {418/* Given explicit derivatives, compute a gradient descriptor */419BIFROST_TEX_OP_GRDESC_DER = 4,420421/* Given implicit derivatives (texture coordinates in a fragment422* shader), compute a gradient descriptor */423BIFROST_TEX_OP_GRDESC = 5,424425/* Fetch a texel. Takes a staging register with LOD level / face index426* packed 16:16 */427BIFROST_TEX_OP_FETCH = 6,428429/* Filtered texture */430BIFROST_TEX_OP_TEX = 7,431};432433enum bifrost_lod_mode {434/* Takes two staging registers forming a 64-bit gradient descriptor435* (computed by a previous GRDESC or GRDESC_DER operation) */436BIFROST_LOD_MODE_GRDESC = 3,437438/* Take a staging register with 8:8 fixed-point in bottom 16-bits439* specifying an explicit LOD */440BIFROST_LOD_MODE_EXPLICIT = 4,441442/* Takes a staging register with bottom 16-bits as 8:8 fixed-point LOD443* bias and top 16-bit as 8:8 fixed-point lower bound (generally left444* zero), added and clamped to a computed LOD */445BIFROST_LOD_MODE_BIAS = 5,446447/* Set LOD to zero */448BIFROST_LOD_MODE_ZERO = 6,449450/* Compute LOD */451BIFROST_LOD_MODE_COMPUTE = 7,452};453454enum bifrost_texture_format {455/* 16-bit floating point, with optional clamping */456BIFROST_TEXTURE_FORMAT_F16 = 0,457BIFROST_TEXTURE_FORMAT_F16_POS = 1,458BIFROST_TEXTURE_FORMAT_F16_PM1 = 2,459BIFROST_TEXTURE_FORMAT_F16_1 = 3,460461/* 32-bit floating point, with optional clamping */462BIFROST_TEXTURE_FORMAT_F32 = 4,463BIFROST_TEXTURE_FORMAT_F32_POS = 5,464BIFROST_TEXTURE_FORMAT_F32_PM1 = 6,465BIFROST_TEXTURE_FORMAT_F32_1 = 7,466};467468enum bifrost_texture_format_full {469/* Transclude bifrost_texture_format from above */470471/* Integers, unclamped */472BIFROST_TEXTURE_FORMAT_U16 = 12,473BIFROST_TEXTURE_FORMAT_S16 = 13,474BIFROST_TEXTURE_FORMAT_U32 = 14,475BIFROST_TEXTURE_FORMAT_S32 = 15,476};477478enum bifrost_texture_fetch {479/* Default texelFetch */480BIFROST_TEXTURE_FETCH_TEXEL = 1,481482/* Deprecated, fetches 4x U32 of a U8 x 4 texture. Do not use. */483BIFROST_TEXTURE_FETCH_GATHER4_RGBA = 3,484485/* Gathers */486BIFROST_TEXTURE_FETCH_GATHER4_R = 4,487BIFROST_TEXTURE_FETCH_GATHER4_G = 5,488BIFROST_TEXTURE_FETCH_GATHER4_B = 6,489BIFROST_TEXTURE_FETCH_GATHER4_A = 7490};491492struct bifrost_texture_operation {493/* If immediate_indices is set:494* - immediate sampler index495* - index used as texture index496* Otherwise:497* - bifrost_single_index in lower 2 bits498* - 0x3 in upper 2 bits (single-texturing)499*/500unsigned sampler_index_or_mode : 4;501unsigned index : 7;502bool immediate_indices : 1;503enum bifrost_tex_op op : 3;504505/* If set for TEX/FETCH, loads texel offsets and multisample index from506* a staging register containing offset_x:offset_y:offset_z:ms_index507* packed 8:8:8:8. Offsets must be in [-31, +31]. If set for508* GRDESC(_DER), disable LOD bias. */509bool offset_or_bias_disable : 1;510511/* If set for TEX/FETCH, loads fp32 shadow comparison value from a512* staging register. Implies fetch_component = gather4_r. If set for513* GRDESC(_DER), disables LOD clamping. */514bool shadow_or_clamp_disable : 1;515516/* If set, loads an uint32 array index from a staging register. */517bool array : 1;518519/* Texture dimension, or 0 for a cubemap */520unsigned dimension : 2;521522/* Method to compute LOD value or for a FETCH, the523* bifrost_texture_fetch component specification */524enum bifrost_lod_mode lod_or_fetch : 3;525526/* Reserved */527unsigned zero : 1;528529/* Register format for the result */530enum bifrost_texture_format_full format : 4;531532/* Write mask for the result */533unsigned mask : 4;534} __attribute__((packed));535536#define BIFROST_MEGA_SAMPLE 128537#define BIFROST_ALL_SAMPLES 255538#define BIFROST_CURRENT_PIXEL 255539540struct bifrost_pixel_indices {541unsigned sample : 8;542unsigned rt : 8;543unsigned x : 8;544unsigned y : 8;545} __attribute__((packed));546547enum bi_constmod {548BI_CONSTMOD_NONE,549BI_CONSTMOD_PC_LO,550BI_CONSTMOD_PC_HI,551BI_CONSTMOD_PC_LO_HI552};553554struct bi_constants {555/* Raw constant values */556uint64_t raw[6];557558/* Associated modifier derived from M values */559enum bi_constmod mods[6];560};561562/* FAU selectors for constants are out-of-order, construct the top bits563* here given a embedded constant index in a clause */564565static inline unsigned566bi_constant_field(unsigned idx)567{568const unsigned values[] = {5694, 5, 6, 7, 2, 3570};571572assert(idx <= 5);573return values[idx] << 4;574}575576#endif577578579