1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker * Copyright © 2019 Intel Corporation
3*61046927SAndroid Build Coastguard Worker *
4*61046927SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
5*61046927SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
6*61046927SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
7*61046927SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*61046927SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
9*61046927SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
10*61046927SAndroid Build Coastguard Worker *
11*61046927SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next
12*61046927SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the
13*61046927SAndroid Build Coastguard Worker * Software.
14*61046927SAndroid Build Coastguard Worker *
15*61046927SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*61046927SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*61046927SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*61046927SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*61046927SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*61046927SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*61046927SAndroid Build Coastguard Worker * IN THE SOFTWARE.
22*61046927SAndroid Build Coastguard Worker */
23*61046927SAndroid Build Coastguard Worker #ifndef INTEL_GUARDBAND_H
24*61046927SAndroid Build Coastguard Worker #define INTEL_GUARDBAND_H
25*61046927SAndroid Build Coastguard Worker
26*61046927SAndroid Build Coastguard Worker 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*61046927SAndroid Build Coastguard Worker intel_calculate_guardband_size(uint32_t x_min, uint32_t x_max,
28*61046927SAndroid Build Coastguard Worker uint32_t y_min, uint32_t y_max,
29*61046927SAndroid Build Coastguard Worker float m00, float m11, float m30, float m31,
30*61046927SAndroid Build Coastguard Worker float *xmin, float *xmax,
31*61046927SAndroid Build Coastguard Worker float *ymin, float *ymax)
32*61046927SAndroid Build Coastguard Worker {
33*61046927SAndroid Build Coastguard Worker /* According to the "Vertex X,Y Clamping and Quantization" section of the
34*61046927SAndroid Build Coastguard Worker * Strips and Fans documentation:
35*61046927SAndroid Build Coastguard Worker *
36*61046927SAndroid Build Coastguard Worker * "The vertex X and Y screen-space coordinates are also /clamped/ to the
37*61046927SAndroid Build Coastguard Worker * fixed-point "guardband" range supported by the rasterization hardware"
38*61046927SAndroid Build Coastguard Worker *
39*61046927SAndroid Build Coastguard Worker * and
40*61046927SAndroid Build Coastguard Worker *
41*61046927SAndroid Build Coastguard Worker * "In almost all circumstances, if an object’s vertices are actually
42*61046927SAndroid Build Coastguard Worker * modified by this clamping (i.e., had X or Y coordinates outside of
43*61046927SAndroid Build Coastguard Worker * the guardband extent the rendered object will not match the intended
44*61046927SAndroid Build Coastguard Worker * result. Therefore software should take steps to ensure that this does
45*61046927SAndroid Build Coastguard Worker * not happen - e.g., by clipping objects such that they do not exceed
46*61046927SAndroid Build Coastguard Worker * these limits after the Drawing Rectangle is applied."
47*61046927SAndroid Build Coastguard Worker *
48*61046927SAndroid Build Coastguard Worker * I believe the fundamental restriction is that the rasterizer (in
49*61046927SAndroid Build Coastguard Worker * the SF/WM stages) have a limit on the number of pixels that can be
50*61046927SAndroid Build Coastguard Worker * rasterized. We need to ensure any coordinates beyond the rasterizer
51*61046927SAndroid Build Coastguard Worker * limit are handled by the clipper. So effectively that limit becomes
52*61046927SAndroid Build Coastguard Worker * the clipper's guardband size.
53*61046927SAndroid Build Coastguard Worker *
54*61046927SAndroid Build Coastguard Worker * It goes on to say:
55*61046927SAndroid Build Coastguard Worker *
56*61046927SAndroid Build Coastguard Worker * "In addition, in order to be correctly rendered, objects must have a
57*61046927SAndroid Build Coastguard Worker * screenspace bounding box not exceeding 8K in the X or Y direction.
58*61046927SAndroid Build Coastguard Worker * This additional restriction must also be comprehended by software,
59*61046927SAndroid Build Coastguard Worker * i.e., enforced by use of clipping."
60*61046927SAndroid Build Coastguard Worker *
61*61046927SAndroid Build Coastguard Worker * This makes no sense. Gfx7+ hardware supports 16K render targets,
62*61046927SAndroid Build Coastguard Worker * and you definitely need to be able to draw polygons that fill the
63*61046927SAndroid Build Coastguard Worker * surface. Our assumption is that the rasterizer was limited to 8K
64*61046927SAndroid Build Coastguard Worker * on Sandybridge, which only supports 8K surfaces, and it was actually
65*61046927SAndroid Build Coastguard Worker * increased to 16K on Ivybridge and later.
66*61046927SAndroid Build Coastguard Worker *
67*61046927SAndroid Build Coastguard Worker * So, limit the guardband to 16K on Gfx7+ and 8K on Sandybridge.
68*61046927SAndroid Build Coastguard Worker */
69*61046927SAndroid Build Coastguard Worker const float gb_size = GFX_VER >= 7 ? 16384.0f : 8192.0f;
70*61046927SAndroid Build Coastguard Worker
71*61046927SAndroid Build Coastguard Worker /* Workaround: prevent gpu hangs on SandyBridge
72*61046927SAndroid Build Coastguard Worker * by disabling guardband clipping for odd dimensions.
73*61046927SAndroid Build Coastguard Worker */
74*61046927SAndroid Build Coastguard Worker if (GFX_VER == 6 && (x_min & 1 || x_max & 1 || y_min & 1 || y_max & 1)) {
75*61046927SAndroid Build Coastguard Worker *xmin = -1.0f;
76*61046927SAndroid Build Coastguard Worker *xmax = 1.0f;
77*61046927SAndroid Build Coastguard Worker *ymin = -1.0f;
78*61046927SAndroid Build Coastguard Worker *ymax = 1.0f;
79*61046927SAndroid Build Coastguard Worker return;
80*61046927SAndroid Build Coastguard Worker }
81*61046927SAndroid Build Coastguard Worker
82*61046927SAndroid Build Coastguard Worker if (m00 != 0 && m11 != 0) {
83*61046927SAndroid Build Coastguard Worker /* First, we compute the screen-space render area */
84*61046927SAndroid Build Coastguard Worker const float ss_ra_xmin = MIN3(x_min, m30 + m00, m30 - m00);
85*61046927SAndroid Build Coastguard Worker const float ss_ra_xmax = MAX3(x_max, m30 + m00, m30 - m00);
86*61046927SAndroid Build Coastguard Worker const float ss_ra_ymin = MIN3(y_min, m31 + m11, m31 - m11);
87*61046927SAndroid Build Coastguard Worker const float ss_ra_ymax = MAX3(y_max, m31 + m11, m31 - m11);
88*61046927SAndroid Build Coastguard Worker
89*61046927SAndroid Build Coastguard Worker /* We want the guardband to be centered on that */
90*61046927SAndroid Build Coastguard Worker const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
91*61046927SAndroid Build Coastguard Worker const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
92*61046927SAndroid Build Coastguard Worker const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
93*61046927SAndroid Build Coastguard Worker const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
94*61046927SAndroid Build Coastguard Worker
95*61046927SAndroid Build Coastguard Worker /* Now we need it in native device coordinates */
96*61046927SAndroid Build Coastguard Worker const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
97*61046927SAndroid Build Coastguard Worker const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
98*61046927SAndroid Build Coastguard Worker const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
99*61046927SAndroid Build Coastguard Worker const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
100*61046927SAndroid Build Coastguard Worker
101*61046927SAndroid Build Coastguard Worker /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
102*61046927SAndroid Build Coastguard Worker * flipped upside-down. X should be fine though.
103*61046927SAndroid Build Coastguard Worker */
104*61046927SAndroid Build Coastguard Worker assert(ndc_gb_xmin <= ndc_gb_xmax);
105*61046927SAndroid Build Coastguard Worker *xmin = ndc_gb_xmin;
106*61046927SAndroid Build Coastguard Worker *xmax = ndc_gb_xmax;
107*61046927SAndroid Build Coastguard Worker *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
108*61046927SAndroid Build Coastguard Worker *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
109*61046927SAndroid Build Coastguard Worker } else {
110*61046927SAndroid Build Coastguard Worker /* The viewport scales to 0, so nothing will be rendered. */
111*61046927SAndroid Build Coastguard Worker *xmin = 0.0f;
112*61046927SAndroid Build Coastguard Worker *xmax = 0.0f;
113*61046927SAndroid Build Coastguard Worker *ymin = 0.0f;
114*61046927SAndroid Build Coastguard Worker *ymax = 0.0f;
115*61046927SAndroid Build Coastguard Worker }
116*61046927SAndroid Build Coastguard Worker }
117*61046927SAndroid Build Coastguard Worker
118*61046927SAndroid Build Coastguard Worker #endif /* INTEL_GUARDBAND_H */
119