17ec681f3Smrg/* 27ec681f3Smrg * Copyright © 2020 Valve Corporation 37ec681f3Smrg * 47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 57ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 67ec681f3Smrg * to deal in the Software without restriction, including without limitation 77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 97ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 107ec681f3Smrg * 117ec681f3Smrg * The above copyright notice and this permission notice (including the next 127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 137ec681f3Smrg * Software. 147ec681f3Smrg * 157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 217ec681f3Smrg * SOFTWARE. 227ec681f3Smrg */ 237ec681f3Smrg 247ec681f3Smrg#ifndef __FREEDRENO_GUARDBAND_H__ 257ec681f3Smrg#define __FREEDRENO_GUARDBAND_H__ 267ec681f3Smrg 277ec681f3Smrg#include <assert.h> 287ec681f3Smrg#include <math.h> 297ec681f3Smrg#include <stdbool.h> 307ec681f3Smrg 317ec681f3Smrgstatic inline unsigned 327ec681f3Smrgfd_calc_guardband(float offset, float scale, bool is_a3xx) 337ec681f3Smrg{ 347ec681f3Smrg /* On a3xx, the viewport max is 4k and the docs say the max guardband 357ec681f3Smrg * width is 8k. That is, GRAS cannot handle triangle coordinates more than 367ec681f3Smrg * 8k, positive or negative. On a4xx+ the viewport width was bumped to 377ec681f3Smrg * 16k, and so the guardband width was necessarily also bumped. Note that 387ec681f3Smrg * the numbers here should correspond to 397ec681f3Smrg * VkPhysicalDeviceLimits::viewportBoundsRange in Vulkan. 407ec681f3Smrg */ 417ec681f3Smrg const float gb_min = is_a3xx ? -8192. : -32768.; 427ec681f3Smrg const float gb_max = is_a3xx ? 8191. : 32767.; 437ec681f3Smrg 447ec681f3Smrg /* Clipping happens in normalized device coordinates, so we have to 457ec681f3Smrg * transform gb_min and gb_max to ndc using the inverse of the viewport 467ec681f3Smrg * transform. Avoid flipping min and max by using the absolute value of 477ec681f3Smrg * the scale. 487ec681f3Smrg */ 497ec681f3Smrg const float gb_min_ndc = (gb_min - offset) / fabsf(scale); 507ec681f3Smrg const float gb_max_ndc = (gb_max - offset) / fabsf(scale); 517ec681f3Smrg 527ec681f3Smrg /* There's only one GB_ADJ field, so presumably the guardband is 537ec681f3Smrg * [-GB_ADJ, GB_ADJ] like on Radeon. It's always safe to make the 547ec681f3Smrg * guardband smaller, so we have to take the min to get the largest range 557ec681f3Smrg * contained in [gb_min_ndc, gb_max_ndc]. 567ec681f3Smrg */ 577ec681f3Smrg const float gb_adj = fminf(-gb_min_ndc, gb_max_ndc); 587ec681f3Smrg 597ec681f3Smrg /* The viewport should always be contained in the guardband. */ 607ec681f3Smrg assert(gb_adj >= 1.0); 617ec681f3Smrg 627ec681f3Smrg /* frexp returns an unspecified value if given an infinite value, which 637ec681f3Smrg * can happen if scale == 0. 647ec681f3Smrg */ 657ec681f3Smrg if (isinf(gb_adj)) 667ec681f3Smrg return 0x1ff; 677ec681f3Smrg 687ec681f3Smrg /* Convert gb_adj to 3.6 floating point, rounding down since it's always 697ec681f3Smrg * safe to make the guard band smaller (but not the other way around!). 707ec681f3Smrg * 717ec681f3Smrg * Note: After converting back to a float, the value the blob returns here 727ec681f3Smrg * is sometimes a little smaller than the value we return. This seems to 737ec681f3Smrg * happen around the boundary between two different rounded values. For 747ec681f3Smrg * example, using the a6xx blob: 757ec681f3Smrg * 767ec681f3Smrg * min | width | unrounded gb_adj | blob result | mesa result 777ec681f3Smrg * ------------------------------------------------------------ 787ec681f3Smrg * 0 | 510 | 127.498 | 127. | 127. 797ec681f3Smrg * 0 | 511 | 127.247 | 126. | 127. 807ec681f3Smrg * 0 | 512 | 126.996 | 126. | 126. 817ec681f3Smrg * 827ec681f3Smrg * The guardband must be 32767 wide, since that's what the blob reports 837ec681f3Smrg * for viewportBoundsRange, so I'm guessing that they're rounding slightly 847ec681f3Smrg * more conservatively somehow. 857ec681f3Smrg */ 867ec681f3Smrg int gb_adj_exp; 877ec681f3Smrg float gb_adj_mantissa = frexpf(gb_adj, &gb_adj_exp); 887ec681f3Smrg assert(gb_adj_exp > 0); 897ec681f3Smrg 907ec681f3Smrg /* Round non-representable numbers down to the largest possible number. */ 917ec681f3Smrg if (gb_adj_exp > 8) 927ec681f3Smrg return 0x1ff; 937ec681f3Smrg 947ec681f3Smrg return ((gb_adj_exp - 1) << 6) | 957ec681f3Smrg ((unsigned)truncf(gb_adj_mantissa * (1 << 7)) - (1 << 6)); 967ec681f3Smrg} 977ec681f3Smrg 987ec681f3Smrg#endif /* __FREEDRENO_GUARDBAND_H__ */ 99