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"
32 #include "intel_batchbuffer.h"
33 #include "main/fbobject.h"
34 #include "main/framebuffer.h"
37 brw_is_drawing_points(const struct brw_context
*brw
)
39 /* Determine if the primitives *reaching the SF* are points */
41 if (brw
->ctx
.Polygon
.FrontMode
== GL_POINT
||
42 brw
->ctx
.Polygon
.BackMode
== GL_POINT
) {
46 if (brw
->gs
.base
.prog_data
) {
47 /* BRW_NEW_GS_PROG_DATA */
48 return brw_gs_prog_data(brw
->gs
.base
.prog_data
)->output_topology
==
50 } else if (brw
->tes
.base
.prog_data
) {
51 /* BRW_NEW_TES_PROG_DATA */
52 return brw_tes_prog_data(brw
->tes
.base
.prog_data
)->output_topology
==
53 BRW_TESS_OUTPUT_TOPOLOGY_POINT
;
55 /* BRW_NEW_PRIMITIVE */
56 return brw
->primitive
== _3DPRIM_POINTLIST
;
61 brw_is_drawing_lines(const struct brw_context
*brw
)
63 /* Determine if the primitives *reaching the SF* are points */
65 if (brw
->ctx
.Polygon
.FrontMode
== GL_LINE
||
66 brw
->ctx
.Polygon
.BackMode
== GL_LINE
) {
70 if (brw
->gs
.base
.prog_data
) {
71 /* BRW_NEW_GS_PROG_DATA */
72 return brw_gs_prog_data(brw
->gs
.base
.prog_data
)->output_topology
==
74 } else if (brw
->tes
.base
.prog_data
) {
75 /* BRW_NEW_TES_PROG_DATA */
76 return brw_tes_prog_data(brw
->tes
.base
.prog_data
)->output_topology
==
77 BRW_TESS_OUTPUT_TOPOLOGY_LINE
;
79 /* BRW_NEW_PRIMITIVE */
80 switch (brw
->primitive
) {
81 case _3DPRIM_LINELIST
:
82 case _3DPRIM_LINESTRIP
:
83 case _3DPRIM_LINELOOP
:
91 upload_clip_state(struct brw_context
*brw
)
93 struct gl_context
*ctx
= &brw
->ctx
;
94 /* BRW_NEW_META_IN_PROGRESS */
95 uint32_t dw1
= brw
->meta_in_progress
? 0 : GEN6_CLIP_STATISTICS_ENABLE
;
99 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
101 /* BRW_NEW_FS_PROG_DATA */
102 if (brw_wm_prog_data(brw
->wm
.base
.prog_data
)->barycentric_interp_modes
&
103 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS
) {
104 dw2
|= GEN6_CLIP_NON_PERSPECTIVE_BARYCENTRIC_ENABLE
;
107 /* BRW_NEW_VS_PROG_DATA */
108 dw1
|= brw_vue_prog_data(brw
->vs
.base
.prog_data
)->cull_distance_mask
;
111 dw1
|= GEN7_CLIP_EARLY_CULL
;
115 if (ctx
->Polygon
._FrontBit
== _mesa_is_user_fbo(fb
))
116 dw1
|= GEN7_CLIP_WINDING_CCW
;
118 if (ctx
->Polygon
.CullFlag
) {
119 switch (ctx
->Polygon
.CullFaceMode
) {
121 dw1
|= GEN7_CLIP_CULLMODE_FRONT
;
124 dw1
|= GEN7_CLIP_CULLMODE_BACK
;
126 case GL_FRONT_AND_BACK
:
127 dw1
|= GEN7_CLIP_CULLMODE_BOTH
;
130 unreachable("Should not get here: invalid CullFlag");
133 dw1
|= GEN7_CLIP_CULLMODE_NONE
;
137 if (brw
->gen
< 8 && !ctx
->Transform
.DepthClamp
)
138 dw2
|= GEN6_CLIP_Z_TEST
;
141 if (ctx
->Light
.ProvokingVertex
== GL_FIRST_VERTEX_CONVENTION
) {
143 (0 << GEN6_CLIP_TRI_PROVOKE_SHIFT
) |
144 (1 << GEN6_CLIP_TRIFAN_PROVOKE_SHIFT
) |
145 (0 << GEN6_CLIP_LINE_PROVOKE_SHIFT
);
148 (2 << GEN6_CLIP_TRI_PROVOKE_SHIFT
) |
149 (2 << GEN6_CLIP_TRIFAN_PROVOKE_SHIFT
) |
150 (1 << GEN6_CLIP_LINE_PROVOKE_SHIFT
);
154 dw2
|= (ctx
->Transform
.ClipPlanesEnabled
<<
155 GEN6_USER_CLIP_CLIP_DISTANCES_SHIFT
);
157 /* Have the hardware use the user clip distance clip test enable bitmask
158 * specified here in 3DSTATE_CLIP rather than the one in 3DSTATE_VS/DS/GS.
159 * We already listen to _NEW_TRANSFORM here, but the other atoms don't
160 * need to other than this.
163 dw1
|= GEN8_CLIP_FORCE_USER_CLIP_DISTANCE_BITMASK
;
165 if (ctx
->Transform
.ClipDepthMode
== GL_ZERO_TO_ONE
)
166 dw2
|= GEN6_CLIP_API_D3D
;
168 dw2
|= GEN6_CLIP_API_OGL
;
170 dw2
|= GEN6_CLIP_GB_TEST
;
172 /* BRW_NEW_VIEWPORT_COUNT */
173 const unsigned viewport_count
= brw
->clip
.viewport_count
;
175 /* We need to disable guardband clipping if the guardband (which we always
176 * program to the maximum screen-space bounding box of 8K x 8K) will be
177 * smaller than the viewport.
179 * Closely examining the clip determination formulas in the documentation
180 * reveals that objects will be discarded entirely if they're outside the
181 * (small) guardband, even if they're within the (large) viewport:
183 * TR = TR_GB || TR_VPXY || TR_VPZ || TR_UC || TR_NEGW
184 * TA = !TR && TA_GB && TA_VPZ && TA_NEGW
187 * (TA is "Trivial Accept", TR is "Trivial Reject", MC is "Must Clip".)
189 * Disabling guardband clipping removes the TR_GB condition, which means
190 * they'll be considered MC ("Must Clip") unless they're rejected for
193 * Note that there is no TA_VPXY condition. If there were, objects entirely
194 * inside a 16384x16384 viewport would be trivially accepted, breaking the
195 * "objects must have a screenspace bounding box not exceeding 8K in the X
196 * or Y direction" restriction. Instead, they're clipped.
198 for (unsigned i
= 0; i
< viewport_count
; i
++) {
199 if (ctx
->ViewportArray
[i
].Width
> 8192 ||
200 ctx
->ViewportArray
[i
].Height
> 8192) {
201 dw2
&= ~GEN6_CLIP_GB_TEST
;
206 /* If the viewport dimensions are smaller than the drawable dimensions,
207 * we have to disable guardband clipping prior to Gen8. We always program
208 * the guardband to a fixed size, which is almost always larger than the
209 * viewport. Any geometry which intersects the viewport but lies within
210 * the guardband would bypass the 3D clipping stage, so it wouldn't be
211 * clipped to the viewport. Rendering would happen beyond the viewport,
212 * but still inside the drawable.
214 * Gen8+ introduces a viewport extents test which restricts rendering to
215 * the viewport, so we can ignore this restriction.
218 const float fb_width
= (float)_mesa_geometric_width(fb
);
219 const float fb_height
= (float)_mesa_geometric_height(fb
);
221 for (unsigned i
= 0; i
< viewport_count
; i
++) {
222 if (ctx
->ViewportArray
[i
].X
!= 0 ||
223 ctx
->ViewportArray
[i
].Y
!= 0 ||
224 ctx
->ViewportArray
[i
].Width
!= fb_width
||
225 ctx
->ViewportArray
[i
].Height
!= fb_height
) {
226 dw2
&= ~GEN6_CLIP_GB_TEST
;
232 /* BRW_NEW_RASTERIZER_DISCARD */
233 if (ctx
->RasterDiscard
) {
234 dw2
|= GEN6_CLIP_MODE_REJECT_ALL
;
236 perf_debug("Rasterizer discard is currently implemented via the "
237 "clipper; having the GS not write primitives would "
238 "likely be faster.\n");
243 if (brw
->primitive
== _3DPRIM_RECTLIST
)
246 enable
= GEN6_CLIP_ENABLE
;
249 * BRW_NEW_GEOMETRY_PROGRAM | BRW_NEW_TES_PROG_DATA | BRW_NEW_PRIMITIVE
251 if (!brw_is_drawing_points(brw
) && !brw_is_drawing_lines(brw
))
252 dw2
|= GEN6_CLIP_XY_TEST
;
255 OUT_BATCH(_3DSTATE_CLIP
<< 16 | (4 - 2));
258 GEN6_CLIP_MODE_NORMAL
|
260 OUT_BATCH(U_FIXED(0.125, 3) << GEN6_CLIP_MIN_POINT_WIDTH_SHIFT
|
261 U_FIXED(255.875, 3) << GEN6_CLIP_MAX_POINT_WIDTH_SHIFT
|
262 (_mesa_geometric_layers(fb
) > 0 ? 0 : GEN6_CLIP_FORCE_ZERO_RTAINDEX
) |
263 ((viewport_count
- 1) & GEN6_CLIP_MAX_VP_INDEX_MASK
));
267 const struct brw_tracked_state gen6_clip_state
= {
269 .mesa
= _NEW_BUFFERS
|
273 .brw
= BRW_NEW_BLORP
|
275 BRW_NEW_FS_PROG_DATA
|
276 BRW_NEW_GS_PROG_DATA
|
277 BRW_NEW_VS_PROG_DATA
|
278 BRW_NEW_META_IN_PROGRESS
|
280 BRW_NEW_RASTERIZER_DISCARD
|
281 BRW_NEW_TES_PROG_DATA
|
282 BRW_NEW_VIEWPORT_COUNT
,
284 .emit
= upload_clip_state
,