Path: blob/21.2-virgl/src/freedreno/common/freedreno_guardband.h
4565 views
/*1* Copyright © 2020 Valve Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#ifndef __FREEDRENO_GUARDBAND_H__24#define __FREEDRENO_GUARDBAND_H__2526#include <assert.h>27#include <math.h>28#include <stdbool.h>2930static inline unsigned31fd_calc_guardband(float offset, float scale, bool is_a3xx)32{33/* On a3xx, the viewport max is 4k and the docs say the max guardband34* width is 8k. That is, GRAS cannot handle triangle coordinates more than35* 8k, positive or negative. On a4xx+ the viewport width was bumped to36* 16k, and so the guardband width was necessarily also bumped. Note that37* the numbers here should correspond to38* VkPhysicalDeviceLimits::viewportBoundsRange in Vulkan.39*/40const float gb_min = is_a3xx ? -8192. : -32768.;41const float gb_max = is_a3xx ? 8191. : 32767.;4243/* Clipping happens in normalized device coordinates, so we have to44* transform gb_min and gb_max to ndc using the inverse of the viewport45* transform. Avoid flipping min and max by using the absolute value of46* the scale.47*/48const float gb_min_ndc = (gb_min - offset) / fabsf(scale);49const float gb_max_ndc = (gb_max - offset) / fabsf(scale);5051/* There's only one GB_ADJ field, so presumably the guardband is52* [-GB_ADJ, GB_ADJ] like on Radeon. It's always safe to make the53* guardband smaller, so we have to take the min to get the largest range54* contained in [gb_min_ndc, gb_max_ndc].55*/56const float gb_adj = fminf(-gb_min_ndc, gb_max_ndc);5758/* The viewport should always be contained in the guardband. */59assert(gb_adj >= 1.0);6061/* frexp returns an unspecified value if given an infinite value, which62* can happen if scale == 0.63*/64if (isinf(gb_adj))65return 0x1ff;6667/* Convert gb_adj to 3.6 floating point, rounding down since it's always68* safe to make the guard band smaller (but not the other way around!).69*70* Note: After converting back to a float, the value the blob returns here71* is sometimes a little smaller than the value we return. This seems to72* happen around the boundary between two different rounded values. For73* example, using the a6xx blob:74*75* min | width | unrounded gb_adj | blob result | mesa result76* ------------------------------------------------------------77* 0 | 510 | 127.498 | 127. | 127.78* 0 | 511 | 127.247 | 126. | 127.79* 0 | 512 | 126.996 | 126. | 126.80*81* The guardband must be 32767 wide, since that's what the blob reports82* for viewportBoundsRange, so I'm guessing that they're rounding slightly83* more conservatively somehow.84*/85int gb_adj_exp;86float gb_adj_mantissa = frexpf(gb_adj, &gb_adj_exp);87assert(gb_adj_exp > 0);8889/* Round non-representable numbers down to the largest possible number. */90if (gb_adj_exp > 8)91return 0x1ff;9293return ((gb_adj_exp - 1) << 6) |94((unsigned)truncf(gb_adj_mantissa * (1 << 7)) - (1 << 6));95}9697#endif /* __FREEDRENO_GUARDBAND_H__ */9899100