• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2020 Valve Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #ifndef __FREEDRENO_GUARDBAND_H__
25 #define __FREEDRENO_GUARDBAND_H__
26 
27 #include <assert.h>
28 #include <math.h>
29 #include <stdbool.h>
30 
31 /* All 1's but don't overflow the GUARDBAND_CLIP_ADJ bitfields: */
32 #define MAX_GB 0x1ff
33 
34 static inline unsigned
fd_calc_guardband(float offset,float scale,bool is_a3xx)35 fd_calc_guardband(float offset, float scale, bool is_a3xx)
36 {
37    /* On a3xx, the viewport max is 4k and the docs say the max guardband
38     * width is 8k. That is, GRAS cannot handle triangle coordinates more than
39     * 8k, positive or negative. On a4xx+ the viewport width was bumped to
40     * 16k, and so the guardband width was necessarily also bumped. Note that
41     * the numbers here should correspond to
42     * VkPhysicalDeviceLimits::viewportBoundsRange in Vulkan.
43     */
44    const float gb_min = is_a3xx ? -8192. : -32768.;
45    const float gb_max = is_a3xx ? 8191. : 32767.;
46 
47    /* Clipping happens in normalized device coordinates, so we have to
48     * transform gb_min and gb_max to ndc using the inverse of the viewport
49     * transform. Avoid flipping min and max by using the absolute value of
50     * the scale.
51     */
52    const float gb_min_ndc = (gb_min - offset) / fabsf(scale);
53    const float gb_max_ndc = (gb_max - offset) / fabsf(scale);
54 
55    /* There's only one GB_ADJ field, so presumably the guardband is
56     * [-GB_ADJ, GB_ADJ] like on Radeon. It's always safe to make the
57     * guardband smaller, so we have to take the min to get the largest range
58     * contained in [gb_min_ndc, gb_max_ndc].
59     */
60    const float gb_adj = fminf(-gb_min_ndc, gb_max_ndc);
61 
62    /* The viewport should always be contained in the guardband. */
63    if (gb_adj < 1.0)
64       return MAX_GB;
65 
66    /* frexp returns an unspecified value if given an infinite value, which
67     * can happen if scale == 0.
68     */
69    if (isinf(gb_adj))
70       return MAX_GB;
71 
72    /* Convert gb_adj to 3.6 floating point, rounding down since it's always
73     * safe to make the guard band smaller (but not the other way around!).
74     *
75     * Note: After converting back to a float, the value the blob returns here
76     * is sometimes a little smaller than the value we return. This seems to
77     * happen around the boundary between two different rounded values. For
78     * example, using the a6xx blob:
79     *
80     * min  | width  | unrounded gb_adj | blob result | mesa result
81     * ------------------------------------------------------------
82     * 0    | 510    |          127.498 |        127. |        127.
83     * 0    | 511    |          127.247 |        126. |        127.
84     * 0    | 512    |          126.996 |        126. |        126.
85     *
86     * The guardband must be 32767 wide, since that's what the blob reports
87     * for viewportBoundsRange, so I'm guessing that they're rounding slightly
88     * more conservatively somehow.
89     */
90    int gb_adj_exp;
91    float gb_adj_mantissa = frexpf(gb_adj, &gb_adj_exp);
92    if (gb_adj_exp <= 0)
93       return MAX_GB;
94 
95    /* Round non-representable numbers down to the largest possible number. */
96    if (gb_adj_exp > 8)
97       return MAX_GB;
98 
99    return ((gb_adj_exp - 1) << 6) |
100           ((unsigned)truncf(gb_adj_mantissa * (1 << 7)) - (1 << 6));
101 }
102 
103 #endif /* __FREEDRENO_GUARDBAND_H__ */
104