xref: /aosp_15_r20/external/mesa3d/src/freedreno/common/freedreno_guardband.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Valve Corporation
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #ifndef __FREEDRENO_GUARDBAND_H__
7 #define __FREEDRENO_GUARDBAND_H__
8 
9 #include <assert.h>
10 #include <math.h>
11 #include <stdbool.h>
12 
13 /* All 1's but don't overflow the GUARDBAND_CLIP_ADJ bitfields: */
14 #define MAX_GB 0x1ff
15 
16 static inline unsigned
fd_calc_guardband(float offset,float scale,bool is_a3xx)17 fd_calc_guardband(float offset, float scale, bool is_a3xx)
18 {
19    /* On a3xx, the viewport max is 4k and the docs say the max guardband
20     * width is 8k. That is, GRAS cannot handle triangle coordinates more than
21     * 8k, positive or negative. On a4xx+ the viewport width was bumped to
22     * 16k, and so the guardband width was necessarily also bumped. Note that
23     * the numbers here should correspond to
24     * VkPhysicalDeviceLimits::viewportBoundsRange in Vulkan.
25     */
26    const float gb_min = is_a3xx ? -8192. : -32768.;
27    const float gb_max = is_a3xx ? 8191. : 32767.;
28 
29    /* Clipping happens in normalized device coordinates, so we have to
30     * transform gb_min and gb_max to ndc using the inverse of the viewport
31     * transform. Avoid flipping min and max by using the absolute value of
32     * the scale.
33     */
34    const float gb_min_ndc = (gb_min - offset) / fabsf(scale);
35    const float gb_max_ndc = (gb_max - offset) / fabsf(scale);
36 
37    /* There's only one GB_ADJ field, so presumably the guardband is
38     * [-GB_ADJ, GB_ADJ] like on Radeon. It's always safe to make the
39     * guardband smaller, so we have to take the min to get the largest range
40     * contained in [gb_min_ndc, gb_max_ndc].
41     */
42    const float gb_adj = fminf(-gb_min_ndc, gb_max_ndc);
43 
44    /* The viewport should always be contained in the guardband. */
45    if (gb_adj < 1.0)
46       return MAX_GB;
47 
48    /* frexp returns an unspecified value if given an infinite value, which
49     * can happen if scale == 0.
50     */
51    if (isinf(gb_adj))
52       return MAX_GB;
53 
54    /* Convert gb_adj to 3.6 floating point, rounding down since it's always
55     * safe to make the guard band smaller (but not the other way around!).
56     *
57     * Note: After converting back to a float, the value the blob returns here
58     * is sometimes a little smaller than the value we return. This seems to
59     * happen around the boundary between two different rounded values. For
60     * example, using the a6xx blob:
61     *
62     * min  | width  | unrounded gb_adj | blob result | mesa result
63     * ------------------------------------------------------------
64     * 0    | 510    |          127.498 |        127. |        127.
65     * 0    | 511    |          127.247 |        126. |        127.
66     * 0    | 512    |          126.996 |        126. |        126.
67     *
68     * The guardband must be 32767 wide, since that's what the blob reports
69     * for viewportBoundsRange, so I'm guessing that they're rounding slightly
70     * more conservatively somehow.
71     */
72    int gb_adj_exp;
73    float gb_adj_mantissa = frexpf(gb_adj, &gb_adj_exp);
74    if (gb_adj_exp <= 0)
75       return MAX_GB;
76 
77    /* Round non-representable numbers down to the largest possible number. */
78    if (gb_adj_exp > 8)
79       return MAX_GB;
80 
81    return ((gb_adj_exp - 1) << 6) |
82           ((unsigned)truncf(gb_adj_mantissa * (1 << 7)) - (1 << 6));
83 }
84 
85 #endif /* __FREEDRENO_GUARDBAND_H__ */
86