1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * © Copyright 2017-2018 Alyssa Rosenzweig
3bf215546Sopenharmony_ci * © Copyright 2017-2018 Connor Abbott
4bf215546Sopenharmony_ci * © Copyright 2017-2018 Lyude Paul
5bf215546Sopenharmony_ci * © Copyright2019 Collabora, Ltd.
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
9bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
10bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
12bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
15bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
16bf215546Sopenharmony_ci * Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24bf215546Sopenharmony_ci * SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#ifndef __PANFROST_JOB_H__
29bf215546Sopenharmony_ci#define __PANFROST_JOB_H__
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include <stdint.h>
32bf215546Sopenharmony_ci#include <stdbool.h>
33bf215546Sopenharmony_ci#include <inttypes.h>
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_citypedef uint8_t  u8;
36bf215546Sopenharmony_citypedef uint16_t u16;
37bf215546Sopenharmony_citypedef uint32_t u32;
38bf215546Sopenharmony_citypedef uint64_t u64;
39bf215546Sopenharmony_citypedef uint64_t mali_ptr;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci/* Compressed per-pixel formats. Each of these formats expands to one to four
42bf215546Sopenharmony_ci * floating-point or integer numbers, as defined by the OpenGL specification.
43bf215546Sopenharmony_ci * There are various places in OpenGL where the user can specify a compressed
44bf215546Sopenharmony_ci * format in memory, which all use the same 8-bit enum in the various
45bf215546Sopenharmony_ci * descriptors, although different hardware units support different formats.
46bf215546Sopenharmony_ci */
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci/* The top 3 bits specify how the bits of each component are interpreted. */
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci/* e.g. ETC2_RGB8 */
51bf215546Sopenharmony_ci#define MALI_FORMAT_COMPRESSED (0 << 5)
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci/* e.g. R11F_G11F_B10F */
54bf215546Sopenharmony_ci#define MALI_FORMAT_SPECIAL (2 << 5)
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci/* signed normalized, e.g. RGBA8_SNORM */
57bf215546Sopenharmony_ci#define MALI_FORMAT_SNORM (3 << 5)
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci/* e.g. RGBA8UI */
60bf215546Sopenharmony_ci#define MALI_FORMAT_UINT (4 << 5)
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci/* e.g. RGBA8 and RGBA32F */
63bf215546Sopenharmony_ci#define MALI_FORMAT_UNORM (5 << 5)
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci/* e.g. RGBA8I and RGBA16F */
66bf215546Sopenharmony_ci#define MALI_FORMAT_SINT (6 << 5)
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci/* These formats seem to largely duplicate the others. They're used at least
69bf215546Sopenharmony_ci * for Bifrost framebuffer output.
70bf215546Sopenharmony_ci */
71bf215546Sopenharmony_ci#define MALI_FORMAT_SPECIAL2 (7 << 5)
72bf215546Sopenharmony_ci#define MALI_EXTRACT_TYPE(fmt) ((fmt) & 0xe0)
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci/* If the high 3 bits are 3 to 6 these two bits say how many components
75bf215546Sopenharmony_ci * there are.
76bf215546Sopenharmony_ci */
77bf215546Sopenharmony_ci#define MALI_NR_CHANNELS(n) ((n - 1) << 3)
78bf215546Sopenharmony_ci#define MALI_EXTRACT_CHANNELS(fmt) ((((fmt) >> 3) & 3) + 1)
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci/* If the high 3 bits are 3 to 6, then the low 3 bits say how big each
81bf215546Sopenharmony_ci * component is, except the special MALI_CHANNEL_FLOAT which overrides what the
82bf215546Sopenharmony_ci * bits mean.
83bf215546Sopenharmony_ci */
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci#define MALI_CHANNEL_4 2
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci#define MALI_CHANNEL_8 3
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci#define MALI_CHANNEL_16 4
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci#define MALI_CHANNEL_32 5
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci/* For MALI_FORMAT_SINT it means a half-float (e.g. RG16F). For
94bf215546Sopenharmony_ci * MALI_FORMAT_UNORM, it means a 32-bit float.
95bf215546Sopenharmony_ci */
96bf215546Sopenharmony_ci#define MALI_CHANNEL_FLOAT 7
97bf215546Sopenharmony_ci#define MALI_EXTRACT_BITS(fmt) (fmt & 0x7)
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci#define MALI_EXTRACT_INDEX(pixfmt) (((pixfmt) >> 12) & 0xFF)
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci/* The raw Midgard blend payload can either be an equation or a shader
102bf215546Sopenharmony_ci * address, depending on the context */
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci/*
105bf215546Sopenharmony_ci * Mali Attributes
106bf215546Sopenharmony_ci *
107bf215546Sopenharmony_ci * This structure lets the attribute unit compute the address of an attribute
108bf215546Sopenharmony_ci * given the vertex and instance ID. Unfortunately, the way this works is
109bf215546Sopenharmony_ci * rather complicated when instancing is enabled.
110bf215546Sopenharmony_ci *
111bf215546Sopenharmony_ci * To explain this, first we need to explain how compute and vertex threads are
112bf215546Sopenharmony_ci * dispatched. This is a guess (although a pretty firm guess!) since the
113bf215546Sopenharmony_ci * details are mostly hidden from the driver, except for attribute instancing.
114bf215546Sopenharmony_ci * When a quad is dispatched, it receives a single, linear index. However, we
115bf215546Sopenharmony_ci * need to translate that index into a (vertex id, instance id) pair, or a
116bf215546Sopenharmony_ci * (local id x, local id y, local id z) triple for compute shaders (although
117bf215546Sopenharmony_ci * vertex shaders and compute shaders are handled almost identically).
118bf215546Sopenharmony_ci * Focusing on vertex shaders, one option would be to do:
119bf215546Sopenharmony_ci *
120bf215546Sopenharmony_ci * vertex_id = linear_id % num_vertices
121bf215546Sopenharmony_ci * instance_id = linear_id / num_vertices
122bf215546Sopenharmony_ci *
123bf215546Sopenharmony_ci * but this involves a costly division and modulus by an arbitrary number.
124bf215546Sopenharmony_ci * Instead, we could pad num_vertices. We dispatch padded_num_vertices *
125bf215546Sopenharmony_ci * num_instances threads instead of num_vertices * num_instances, which results
126bf215546Sopenharmony_ci * in some "extra" threads with vertex_id >= num_vertices, which we have to
127bf215546Sopenharmony_ci * discard.  The more we pad num_vertices, the more "wasted" threads we
128bf215546Sopenharmony_ci * dispatch, but the division is potentially easier.
129bf215546Sopenharmony_ci *
130bf215546Sopenharmony_ci * One straightforward choice is to pad num_vertices to the next power of two,
131bf215546Sopenharmony_ci * which means that the division and modulus are just simple bit shifts and
132bf215546Sopenharmony_ci * masking. But the actual algorithm is a bit more complicated. The thread
133bf215546Sopenharmony_ci * dispatcher has special support for dividing by 3, 5, 7, and 9, in addition
134bf215546Sopenharmony_ci * to dividing by a power of two. This is possibly using the technique
135bf215546Sopenharmony_ci * described in patent US20170010862A1. As a result, padded_num_vertices can be
136bf215546Sopenharmony_ci * 1, 3, 5, 7, or 9 times a power of two. This results in less wasted threads,
137bf215546Sopenharmony_ci * since we need less padding.
138bf215546Sopenharmony_ci *
139bf215546Sopenharmony_ci * padded_num_vertices is picked by the hardware. The driver just specifies the
140bf215546Sopenharmony_ci * actual number of vertices. At least for Mali G71, the first few cases are
141bf215546Sopenharmony_ci * given by:
142bf215546Sopenharmony_ci *
143bf215546Sopenharmony_ci * num_vertices	| padded_num_vertices
144bf215546Sopenharmony_ci * 3		| 4
145bf215546Sopenharmony_ci * 4-7		| 8
146bf215546Sopenharmony_ci * 8-11		| 12 (3 * 4)
147bf215546Sopenharmony_ci * 12-15	| 16
148bf215546Sopenharmony_ci * 16-19	| 20 (5 * 4)
149bf215546Sopenharmony_ci *
150bf215546Sopenharmony_ci * Note that padded_num_vertices is a multiple of four (presumably because
151bf215546Sopenharmony_ci * threads are dispatched in groups of 4). Also, padded_num_vertices is always
152bf215546Sopenharmony_ci * at least one more than num_vertices, which seems like a quirk of the
153bf215546Sopenharmony_ci * hardware. For larger num_vertices, the hardware uses the following
154bf215546Sopenharmony_ci * algorithm: using the binary representation of num_vertices, we look at the
155bf215546Sopenharmony_ci * most significant set bit as well as the following 3 bits. Let n be the
156bf215546Sopenharmony_ci * number of bits after those 4 bits. Then we set padded_num_vertices according
157bf215546Sopenharmony_ci * to the following table:
158bf215546Sopenharmony_ci *
159bf215546Sopenharmony_ci * high bits	| padded_num_vertices
160bf215546Sopenharmony_ci * 1000		| 9 * 2^n
161bf215546Sopenharmony_ci * 1001		| 5 * 2^(n+1)
162bf215546Sopenharmony_ci * 101x		| 3 * 2^(n+2)
163bf215546Sopenharmony_ci * 110x		| 7 * 2^(n+1)
164bf215546Sopenharmony_ci * 111x		| 2^(n+4)
165bf215546Sopenharmony_ci *
166bf215546Sopenharmony_ci * For example, if num_vertices = 70 is passed to glDraw(), its binary
167bf215546Sopenharmony_ci * representation is 1000110, so n = 3 and the high bits are 1000, and
168bf215546Sopenharmony_ci * therefore padded_num_vertices = 9 * 2^3 = 72.
169bf215546Sopenharmony_ci *
170bf215546Sopenharmony_ci * The attribute unit works in terms of the original linear_id. if
171bf215546Sopenharmony_ci * num_instances = 1, then they are the same, and everything is simple.
172bf215546Sopenharmony_ci * However, with instancing things get more complicated. There are four
173bf215546Sopenharmony_ci * possible modes, two of them we can group together:
174bf215546Sopenharmony_ci *
175bf215546Sopenharmony_ci * 1. Use the linear_id directly. Only used when there is no instancing.
176bf215546Sopenharmony_ci *
177bf215546Sopenharmony_ci * 2. Use the linear_id modulo a constant. This is used for per-vertex
178bf215546Sopenharmony_ci * attributes with instancing enabled by making the constant equal
179bf215546Sopenharmony_ci * padded_num_vertices. Because the modulus is always padded_num_vertices, this
180bf215546Sopenharmony_ci * mode only supports a modulus that is a power of 2 times 1, 3, 5, 7, or 9.
181bf215546Sopenharmony_ci * The shift field specifies the power of two, while the extra_flags field
182bf215546Sopenharmony_ci * specifies the odd number. If shift = n and extra_flags = m, then the modulus
183bf215546Sopenharmony_ci * is (2m + 1) * 2^n. As an example, if num_vertices = 70, then as computed
184bf215546Sopenharmony_ci * above, padded_num_vertices = 9 * 2^3, so we should set extra_flags = 4 and
185bf215546Sopenharmony_ci * shift = 3. Note that we must exactly follow the hardware algorithm used to
186bf215546Sopenharmony_ci * get padded_num_vertices in order to correctly implement per-vertex
187bf215546Sopenharmony_ci * attributes.
188bf215546Sopenharmony_ci *
189bf215546Sopenharmony_ci * 3. Divide the linear_id by a constant. In order to correctly implement
190bf215546Sopenharmony_ci * instance divisors, we have to divide linear_id by padded_num_vertices times
191bf215546Sopenharmony_ci * to user-specified divisor. So first we compute padded_num_vertices, again
192bf215546Sopenharmony_ci * following the exact same algorithm that the hardware uses, then multiply it
193bf215546Sopenharmony_ci * by the GL-level divisor to get the hardware-level divisor. This case is
194bf215546Sopenharmony_ci * further divided into two more cases. If the hardware-level divisor is a
195bf215546Sopenharmony_ci * power of two, then we just need to shift. The shift amount is specified by
196bf215546Sopenharmony_ci * the shift field, so that the hardware-level divisor is just 2^shift.
197bf215546Sopenharmony_ci *
198bf215546Sopenharmony_ci * If it isn't a power of two, then we have to divide by an arbitrary integer.
199bf215546Sopenharmony_ci * For that, we use the well-known technique of multiplying by an approximation
200bf215546Sopenharmony_ci * of the inverse. The driver must compute the magic multiplier and shift
201bf215546Sopenharmony_ci * amount, and then the hardware does the multiplication and shift. The
202bf215546Sopenharmony_ci * hardware and driver also use the "round-down" optimization as described in
203bf215546Sopenharmony_ci * http://ridiculousfish.com/files/faster_unsigned_division_by_constants.pdf.
204bf215546Sopenharmony_ci * The hardware further assumes the multiplier is between 2^31 and 2^32, so the
205bf215546Sopenharmony_ci * high bit is implicitly set to 1 even though it is set to 0 by the driver --
206bf215546Sopenharmony_ci * presumably this simplifies the hardware multiplier a little. The hardware
207bf215546Sopenharmony_ci * first multiplies linear_id by the multiplier and takes the high 32 bits,
208bf215546Sopenharmony_ci * then applies the round-down correction if extra_flags = 1, then finally
209bf215546Sopenharmony_ci * shifts right by the shift field.
210bf215546Sopenharmony_ci *
211bf215546Sopenharmony_ci * There are some differences between ridiculousfish's algorithm and the Mali
212bf215546Sopenharmony_ci * hardware algorithm, which means that the reference code from ridiculousfish
213bf215546Sopenharmony_ci * doesn't always produce the right constants. Mali does not use the pre-shift
214bf215546Sopenharmony_ci * optimization, since that would make a hardware implementation slower (it
215bf215546Sopenharmony_ci * would have to always do the pre-shift, multiply, and post-shift operations).
216bf215546Sopenharmony_ci * It also forces the multplier to be at least 2^31, which means that the
217bf215546Sopenharmony_ci * exponent is entirely fixed, so there is no trial-and-error. Altogether,
218bf215546Sopenharmony_ci * given the divisor d, the algorithm the driver must follow is:
219bf215546Sopenharmony_ci *
220bf215546Sopenharmony_ci * 1. Set shift = floor(log2(d)).
221bf215546Sopenharmony_ci * 2. Compute m = ceil(2^(shift + 32) / d) and e = 2^(shift + 32) % d.
222bf215546Sopenharmony_ci * 3. If e <= 2^shift, then we need to use the round-down algorithm. Set
223bf215546Sopenharmony_ci * magic_divisor = m - 1 and extra_flags = 1.
224bf215546Sopenharmony_ci * 4. Otherwise, set magic_divisor = m and extra_flags = 0.
225bf215546Sopenharmony_ci */
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci/* Purposeful off-by-one in width, height fields. For example, a (64, 64)
228bf215546Sopenharmony_ci * texture is stored as (63, 63) in these fields. This adjusts for that.
229bf215546Sopenharmony_ci * There's an identical pattern in the framebuffer descriptor. Even vertex
230bf215546Sopenharmony_ci * count fields work this way, hence the generic name -- integral fields that
231bf215546Sopenharmony_ci * are strictly positive generally need this adjustment. */
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci#define MALI_POSITIVE(dim) (dim - 1)
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci/* Mali hardware can texture up to 65536 x 65536 x 65536 and render up to 16384
236bf215546Sopenharmony_ci * x 16384, but 8192 x 8192 should be enough for anyone.  The OpenGL game
237bf215546Sopenharmony_ci * "Cathedral" requires a texture of width 8192 to start.
238bf215546Sopenharmony_ci */
239bf215546Sopenharmony_ci#define MAX_MIP_LEVELS (14)
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci/* Used for lod encoding. Thanks @urjaman for pointing out these routines can
242bf215546Sopenharmony_ci * be cleaned up a lot. */
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci#define DECODE_FIXED_16(x) ((float) (x / 256.0))
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_cistatic inline int16_t
247bf215546Sopenharmony_ciFIXED_16(float x, bool allow_negative)
248bf215546Sopenharmony_ci{
249bf215546Sopenharmony_ci        /* Clamp inputs, accounting for float error */
250bf215546Sopenharmony_ci        float max_lod = (32.0 - (1.0 / 512.0));
251bf215546Sopenharmony_ci        float min_lod = allow_negative ? -max_lod : 0.0;
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_ci        x = ((x > max_lod) ? max_lod : ((x < min_lod) ? min_lod : x));
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci        return (int) (x * 256.0);
256bf215546Sopenharmony_ci}
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci#endif /* __PANFROST_JOB_H__ */
259