xref: /aosp_15_r20/external/mesa3d/src/intel/common/intel_guardband.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2019 Intel 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
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #ifndef INTEL_GUARDBAND_H
24 #define INTEL_GUARDBAND_H
25 
26 static inline void
intel_calculate_guardband_size(uint32_t x_min,uint32_t x_max,uint32_t y_min,uint32_t y_max,float m00,float m11,float m30,float m31,float * xmin,float * xmax,float * ymin,float * ymax)27 intel_calculate_guardband_size(uint32_t x_min, uint32_t x_max,
28                                uint32_t y_min, uint32_t y_max,
29                                float m00, float m11, float m30, float m31,
30                                float *xmin, float *xmax,
31                                float *ymin, float *ymax)
32 {
33    /* According to the "Vertex X,Y Clamping and Quantization" section of the
34     * Strips and Fans documentation:
35     *
36     * "The vertex X and Y screen-space coordinates are also /clamped/ to the
37     *  fixed-point "guardband" range supported by the rasterization hardware"
38     *
39     * and
40     *
41     * "In almost all circumstances, if an object’s vertices are actually
42     *  modified by this clamping (i.e., had X or Y coordinates outside of
43     *  the guardband extent the rendered object will not match the intended
44     *  result.  Therefore software should take steps to ensure that this does
45     *  not happen - e.g., by clipping objects such that they do not exceed
46     *  these limits after the Drawing Rectangle is applied."
47     *
48     * I believe the fundamental restriction is that the rasterizer (in
49     * the SF/WM stages) have a limit on the number of pixels that can be
50     * rasterized.  We need to ensure any coordinates beyond the rasterizer
51     * limit are handled by the clipper.  So effectively that limit becomes
52     * the clipper's guardband size.
53     *
54     * It goes on to say:
55     *
56     * "In addition, in order to be correctly rendered, objects must have a
57     *  screenspace bounding box not exceeding 8K in the X or Y direction.
58     *  This additional restriction must also be comprehended by software,
59     *  i.e., enforced by use of clipping."
60     *
61     * This makes no sense.  Gfx7+ hardware supports 16K render targets,
62     * and you definitely need to be able to draw polygons that fill the
63     * surface.  Our assumption is that the rasterizer was limited to 8K
64     * on Sandybridge, which only supports 8K surfaces, and it was actually
65     * increased to 16K on Ivybridge and later.
66     *
67     * So, limit the guardband to 16K on Gfx7+ and 8K on Sandybridge.
68     */
69    const float gb_size = GFX_VER >= 7 ? 16384.0f : 8192.0f;
70 
71    /* Workaround: prevent gpu hangs on SandyBridge
72     * by disabling guardband clipping for odd dimensions.
73     */
74    if (GFX_VER == 6 && (x_min & 1 || x_max & 1 || y_min & 1 || y_max & 1)) {
75       *xmin = -1.0f;
76       *xmax =  1.0f;
77       *ymin = -1.0f;
78       *ymax =  1.0f;
79       return;
80    }
81 
82    if (m00 != 0 && m11 != 0) {
83       /* First, we compute the screen-space render area */
84       const float ss_ra_xmin = MIN3(x_min, m30 + m00, m30 - m00);
85       const float ss_ra_xmax = MAX3(x_max, m30 + m00, m30 - m00);
86       const float ss_ra_ymin = MIN3(y_min, m31 + m11, m31 - m11);
87       const float ss_ra_ymax = MAX3(y_max, m31 + m11, m31 - m11);
88 
89       /* We want the guardband to be centered on that */
90       const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
91       const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
92       const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
93       const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
94 
95       /* Now we need it in native device coordinates */
96       const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
97       const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
98       const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
99       const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
100 
101       /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
102        * flipped upside-down.  X should be fine though.
103        */
104       assert(ndc_gb_xmin <= ndc_gb_xmax);
105       *xmin = ndc_gb_xmin;
106       *xmax = ndc_gb_xmax;
107       *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
108       *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
109    } else {
110       /* The viewport scales to 0, so nothing will be rendered. */
111       *xmin = 0.0f;
112       *xmax = 0.0f;
113       *ymin = 0.0f;
114       *ymax = 0.0f;
115    }
116 }
117 
118 #endif /* INTEL_GUARDBAND_H */
119