2 * Copyright © 2009 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "intel_batchbuffer.h"
32 #include "main/fbobject.h"
33 #include "main/framebuffer.h"
34 #include "main/viewport.h"
37 brw_calculate_guardband_size(const struct gen_device_info
*devinfo
,
38 uint32_t fb_width
, uint32_t fb_height
,
39 float m00
, float m11
, float m30
, float m31
,
40 float *xmin
, float *xmax
,
41 float *ymin
, float *ymax
)
43 /* According to the "Vertex X,Y Clamping and Quantization" section of the
44 * Strips and Fans documentation:
46 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
47 * fixed-point "guardband" range supported by the rasterization hardware"
51 * "In almost all circumstances, if an object’s vertices are actually
52 * modified by this clamping (i.e., had X or Y coordinates outside of
53 * the guardband extent the rendered object will not match the intended
54 * result. Therefore software should take steps to ensure that this does
55 * not happen - e.g., by clipping objects such that they do not exceed
56 * these limits after the Drawing Rectangle is applied."
58 * I believe the fundamental restriction is that the rasterizer (in
59 * the SF/WM stages) have a limit on the number of pixels that can be
60 * rasterized. We need to ensure any coordinates beyond the rasterizer
61 * limit are handled by the clipper. So effectively that limit becomes
62 * the clipper's guardband size.
66 * "In addition, in order to be correctly rendered, objects must have a
67 * screenspace bounding box not exceeding 8K in the X or Y direction.
68 * This additional restriction must also be comprehended by software,
69 * i.e., enforced by use of clipping."
71 * This makes no sense. Gen7+ hardware supports 16K render targets,
72 * and you definitely need to be able to draw polygons that fill the
73 * surface. Our assumption is that the rasterizer was limited to 8K
74 * on Sandybridge, which only supports 8K surfaces, and it was actually
75 * increased to 16K on Ivybridge and later.
77 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
79 const float gb_size
= devinfo
->gen
>= 7 ? 16384.0f
: 8192.0f
;
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
);
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
;
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
;
100 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
101 * flipped upside-down. X should be fine though.
103 assert(ndc_gb_xmin
<= ndc_gb_xmax
);
106 *ymin
= MIN2(ndc_gb_ymin
, ndc_gb_ymax
);
107 *ymax
= MAX2(ndc_gb_ymin
, ndc_gb_ymax
);
109 /* The viewport scales to 0, so nothing will be rendered. */
118 gen6_upload_sf_and_clip_viewports(struct brw_context
*brw
)
120 struct gl_context
*ctx
= &brw
->ctx
;
121 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
122 struct gen6_sf_viewport
*sfv
;
123 struct brw_clipper_viewport
*clv
;
124 GLfloat y_scale
, y_bias
;
126 /* BRW_NEW_VIEWPORT_COUNT */
127 const unsigned viewport_count
= brw
->clip
.viewport_count
;
130 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
131 const bool render_to_fbo
= _mesa_is_user_fbo(fb
);
132 const uint32_t fb_width
= _mesa_geometric_width(ctx
->DrawBuffer
);
133 const uint32_t fb_height
= _mesa_geometric_height(ctx
->DrawBuffer
);
135 sfv
= brw_state_batch(brw
, AUB_TRACE_SF_VP_STATE
,
136 sizeof(*sfv
) * viewport_count
,
137 32, &brw
->sf
.vp_offset
);
138 memset(sfv
, 0, sizeof(*sfv
) * viewport_count
);
140 clv
= brw_state_batch(brw
, AUB_TRACE_CLIP_VP_STATE
,
141 sizeof(*clv
) * viewport_count
,
142 32, &brw
->clip
.vp_offset
);
149 y_bias
= (float)fb_height
;
152 for (unsigned i
= 0; i
< viewport_count
; i
++) {
153 float scale
[3], translate
[3];
156 _mesa_get_viewport_xform(ctx
, i
, scale
, translate
);
157 sfv
[i
].m00
= scale
[0];
158 sfv
[i
].m11
= scale
[1] * y_scale
;
159 sfv
[i
].m22
= scale
[2];
160 sfv
[i
].m30
= translate
[0];
161 sfv
[i
].m31
= translate
[1] * y_scale
+ y_bias
;
162 sfv
[i
].m32
= translate
[2];
164 brw_calculate_guardband_size(devinfo
, fb_width
, fb_height
,
165 sfv
[i
].m00
, sfv
[i
].m11
,
166 sfv
[i
].m30
, sfv
[i
].m31
,
167 &clv
[i
].xmin
, &clv
[i
].xmax
,
168 &clv
[i
].ymin
, &clv
[i
].ymax
);
171 brw
->ctx
.NewDriverState
|= BRW_NEW_SF_VP
| BRW_NEW_CLIP_VP
;
174 const struct brw_tracked_state gen6_sf_and_clip_viewports
= {
176 .mesa
= _NEW_BUFFERS
|
178 .brw
= BRW_NEW_BATCH
|
180 BRW_NEW_VIEWPORT_COUNT
,
182 .emit
= gen6_upload_sf_and_clip_viewports
,
185 static void upload_viewport_state_pointers(struct brw_context
*brw
)
188 OUT_BATCH(_3DSTATE_VIEWPORT_STATE_POINTERS
<< 16 | (4 - 2) |
189 GEN6_CC_VIEWPORT_MODIFY
|
190 GEN6_SF_VIEWPORT_MODIFY
|
191 GEN6_CLIP_VIEWPORT_MODIFY
);
192 OUT_BATCH(brw
->clip
.vp_offset
);
193 OUT_BATCH(brw
->sf
.vp_offset
);
194 OUT_BATCH(brw
->cc
.vp_offset
);
198 const struct brw_tracked_state gen6_viewport_state
= {
201 .brw
= BRW_NEW_BATCH
|
206 BRW_NEW_STATE_BASE_ADDRESS
,
208 .emit
= upload_viewport_state_pointers
,