Path: blob/21.2-virgl/src/panfrost/include/panfrost-job.h
4560 views
/*1* © Copyright 2017-2018 Alyssa Rosenzweig2* © Copyright 2017-2018 Connor Abbott3* © Copyright 2017-2018 Lyude Paul4* © Copyright2019 Collabora, Ltd.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the "Software"),8* to deal in the Software without restriction, including without limitation9* the rights to use, copy, modify, merge, publish, distribute, sublicense,10* and/or sell copies of the Software, and to permit persons to whom the11* Software is furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice (including the next14* paragraph) shall be included in all copies or substantial portions of the15* Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR18* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL20* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER21* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,22* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE23* SOFTWARE.24*25*/2627#ifndef __PANFROST_JOB_H__28#define __PANFROST_JOB_H__2930#include <stdint.h>31#include <stdbool.h>32#include <inttypes.h>3334typedef uint8_t u8;35typedef uint16_t u16;36typedef uint32_t u32;37typedef uint64_t u64;38typedef uint64_t mali_ptr;3940/* Compressed per-pixel formats. Each of these formats expands to one to four41* floating-point or integer numbers, as defined by the OpenGL specification.42* There are various places in OpenGL where the user can specify a compressed43* format in memory, which all use the same 8-bit enum in the various44* descriptors, although different hardware units support different formats.45*/4647/* The top 3 bits specify how the bits of each component are interpreted. */4849/* e.g. ETC2_RGB8 */50#define MALI_FORMAT_COMPRESSED (0 << 5)5152/* e.g. R11F_G11F_B10F */53#define MALI_FORMAT_SPECIAL (2 << 5)5455/* signed normalized, e.g. RGBA8_SNORM */56#define MALI_FORMAT_SNORM (3 << 5)5758/* e.g. RGBA8UI */59#define MALI_FORMAT_UINT (4 << 5)6061/* e.g. RGBA8 and RGBA32F */62#define MALI_FORMAT_UNORM (5 << 5)6364/* e.g. RGBA8I and RGBA16F */65#define MALI_FORMAT_SINT (6 << 5)6667/* These formats seem to largely duplicate the others. They're used at least68* for Bifrost framebuffer output.69*/70#define MALI_FORMAT_SPECIAL2 (7 << 5)71#define MALI_EXTRACT_TYPE(fmt) ((fmt) & 0xe0)7273/* If the high 3 bits are 3 to 6 these two bits say how many components74* there are.75*/76#define MALI_NR_CHANNELS(n) ((n - 1) << 3)77#define MALI_EXTRACT_CHANNELS(fmt) ((((fmt) >> 3) & 3) + 1)7879/* If the high 3 bits are 3 to 6, then the low 3 bits say how big each80* component is, except the special MALI_CHANNEL_FLOAT which overrides what the81* bits mean.82*/8384#define MALI_CHANNEL_4 28586#define MALI_CHANNEL_8 38788#define MALI_CHANNEL_16 48990#define MALI_CHANNEL_32 59192/* For MALI_FORMAT_SINT it means a half-float (e.g. RG16F). For93* MALI_FORMAT_UNORM, it means a 32-bit float.94*/95#define MALI_CHANNEL_FLOAT 796#define MALI_EXTRACT_BITS(fmt) (fmt & 0x7)9798#define MALI_EXTRACT_INDEX(pixfmt) (((pixfmt) >> 12) & 0xFF)99100/* The raw Midgard blend payload can either be an equation or a shader101* address, depending on the context */102103/*104* Mali Attributes105*106* This structure lets the attribute unit compute the address of an attribute107* given the vertex and instance ID. Unfortunately, the way this works is108* rather complicated when instancing is enabled.109*110* To explain this, first we need to explain how compute and vertex threads are111* dispatched. This is a guess (although a pretty firm guess!) since the112* details are mostly hidden from the driver, except for attribute instancing.113* When a quad is dispatched, it receives a single, linear index. However, we114* need to translate that index into a (vertex id, instance id) pair, or a115* (local id x, local id y, local id z) triple for compute shaders (although116* vertex shaders and compute shaders are handled almost identically).117* Focusing on vertex shaders, one option would be to do:118*119* vertex_id = linear_id % num_vertices120* instance_id = linear_id / num_vertices121*122* but this involves a costly division and modulus by an arbitrary number.123* Instead, we could pad num_vertices. We dispatch padded_num_vertices *124* num_instances threads instead of num_vertices * num_instances, which results125* in some "extra" threads with vertex_id >= num_vertices, which we have to126* discard. The more we pad num_vertices, the more "wasted" threads we127* dispatch, but the division is potentially easier.128*129* One straightforward choice is to pad num_vertices to the next power of two,130* which means that the division and modulus are just simple bit shifts and131* masking. But the actual algorithm is a bit more complicated. The thread132* dispatcher has special support for dividing by 3, 5, 7, and 9, in addition133* to dividing by a power of two. This is possibly using the technique134* described in patent US20170010862A1. As a result, padded_num_vertices can be135* 1, 3, 5, 7, or 9 times a power of two. This results in less wasted threads,136* since we need less padding.137*138* padded_num_vertices is picked by the hardware. The driver just specifies the139* actual number of vertices. At least for Mali G71, the first few cases are140* given by:141*142* num_vertices | padded_num_vertices143* 3 | 4144* 4-7 | 8145* 8-11 | 12 (3 * 4)146* 12-15 | 16147* 16-19 | 20 (5 * 4)148*149* Note that padded_num_vertices is a multiple of four (presumably because150* threads are dispatched in groups of 4). Also, padded_num_vertices is always151* at least one more than num_vertices, which seems like a quirk of the152* hardware. For larger num_vertices, the hardware uses the following153* algorithm: using the binary representation of num_vertices, we look at the154* most significant set bit as well as the following 3 bits. Let n be the155* number of bits after those 4 bits. Then we set padded_num_vertices according156* to the following table:157*158* high bits | padded_num_vertices159* 1000 | 9 * 2^n160* 1001 | 5 * 2^(n+1)161* 101x | 3 * 2^(n+2)162* 110x | 7 * 2^(n+1)163* 111x | 2^(n+4)164*165* For example, if num_vertices = 70 is passed to glDraw(), its binary166* representation is 1000110, so n = 3 and the high bits are 1000, and167* therefore padded_num_vertices = 9 * 2^3 = 72.168*169* The attribute unit works in terms of the original linear_id. if170* num_instances = 1, then they are the same, and everything is simple.171* However, with instancing things get more complicated. There are four172* possible modes, two of them we can group together:173*174* 1. Use the linear_id directly. Only used when there is no instancing.175*176* 2. Use the linear_id modulo a constant. This is used for per-vertex177* attributes with instancing enabled by making the constant equal178* padded_num_vertices. Because the modulus is always padded_num_vertices, this179* mode only supports a modulus that is a power of 2 times 1, 3, 5, 7, or 9.180* The shift field specifies the power of two, while the extra_flags field181* specifies the odd number. If shift = n and extra_flags = m, then the modulus182* is (2m + 1) * 2^n. As an example, if num_vertices = 70, then as computed183* above, padded_num_vertices = 9 * 2^3, so we should set extra_flags = 4 and184* shift = 3. Note that we must exactly follow the hardware algorithm used to185* get padded_num_vertices in order to correctly implement per-vertex186* attributes.187*188* 3. Divide the linear_id by a constant. In order to correctly implement189* instance divisors, we have to divide linear_id by padded_num_vertices times190* to user-specified divisor. So first we compute padded_num_vertices, again191* following the exact same algorithm that the hardware uses, then multiply it192* by the GL-level divisor to get the hardware-level divisor. This case is193* further divided into two more cases. If the hardware-level divisor is a194* power of two, then we just need to shift. The shift amount is specified by195* the shift field, so that the hardware-level divisor is just 2^shift.196*197* If it isn't a power of two, then we have to divide by an arbitrary integer.198* For that, we use the well-known technique of multiplying by an approximation199* of the inverse. The driver must compute the magic multiplier and shift200* amount, and then the hardware does the multiplication and shift. The201* hardware and driver also use the "round-down" optimization as described in202* http://ridiculousfish.com/files/faster_unsigned_division_by_constants.pdf.203* The hardware further assumes the multiplier is between 2^31 and 2^32, so the204* high bit is implicitly set to 1 even though it is set to 0 by the driver --205* presumably this simplifies the hardware multiplier a little. The hardware206* first multiplies linear_id by the multiplier and takes the high 32 bits,207* then applies the round-down correction if extra_flags = 1, then finally208* shifts right by the shift field.209*210* There are some differences between ridiculousfish's algorithm and the Mali211* hardware algorithm, which means that the reference code from ridiculousfish212* doesn't always produce the right constants. Mali does not use the pre-shift213* optimization, since that would make a hardware implementation slower (it214* would have to always do the pre-shift, multiply, and post-shift operations).215* It also forces the multplier to be at least 2^31, which means that the216* exponent is entirely fixed, so there is no trial-and-error. Altogether,217* given the divisor d, the algorithm the driver must follow is:218*219* 1. Set shift = floor(log2(d)).220* 2. Compute m = ceil(2^(shift + 32) / d) and e = 2^(shift + 32) % d.221* 3. If e <= 2^shift, then we need to use the round-down algorithm. Set222* magic_divisor = m - 1 and extra_flags = 1.223* 4. Otherwise, set magic_divisor = m and extra_flags = 0.224*/225226/* Purposeful off-by-one in width, height fields. For example, a (64, 64)227* texture is stored as (63, 63) in these fields. This adjusts for that.228* There's an identical pattern in the framebuffer descriptor. Even vertex229* count fields work this way, hence the generic name -- integral fields that230* are strictly positive generally need this adjustment. */231232#define MALI_POSITIVE(dim) (dim - 1)233234/* 8192x8192 */235#define MAX_MIP_LEVELS (13)236237/* Cubemap bloats everything up */238#define MAX_CUBE_FACES (6)239240/* For each pointer, there is an address and optionally also a stride */241#define MAX_ELEMENTS (2)242243/* Used for lod encoding. Thanks @urjaman for pointing out these routines can244* be cleaned up a lot. */245246#define DECODE_FIXED_16(x) ((float) (x / 256.0))247248static inline int16_t249FIXED_16(float x, bool allow_negative)250{251/* Clamp inputs, accounting for float error */252float max_lod = (32.0 - (1.0 / 512.0));253float min_lod = allow_negative ? -max_lod : 0.0;254255x = ((x > max_lod) ? max_lod : ((x < min_lod) ? min_lod : x));256257return (int) (x * 256.0);258}259260#endif /* __PANFROST_JOB_H__ */261262263