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