i965/gen6: Manipulate state batches for HiZ meta-ops [v4]
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_clip_state.c
1 /*
2 * Copyright © 2009 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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "brw_util.h"
32 #include "intel_batchbuffer.h"
33
34 /**
35 * Return true if at least one of the inputs used by the given fragment
36 * program has the GLSL "noperspective" interpolation qualifier.
37 */
38 bool
39 brw_fprog_uses_noperspective(const struct gl_fragment_program *fprog)
40 {
41 int attr;
42 for (attr = 0; attr < FRAG_ATTRIB_MAX; ++attr) {
43 /* Ignore unused inputs. */
44 if (!(fprog->Base.InputsRead & BITFIELD64_BIT(attr)))
45 continue;
46
47 if (fprog->InterpQualifier[attr] == INTERP_QUALIFIER_NOPERSPECTIVE)
48 return true;
49 }
50 return false;
51 }
52
53
54 static void
55 upload_clip_state(struct brw_context *brw)
56 {
57 struct intel_context *intel = &brw->intel;
58 struct gl_context *ctx = &intel->ctx;
59 uint32_t depth_clamp = 0;
60 uint32_t provoking, userclip;
61 uint32_t nonperspective_barycentric_enable_flag = 0;
62 /* BRW_NEW_FRAGMENT_PROGRAM */
63 const struct gl_fragment_program *fprog = brw->fragment_program;
64
65 if (brw_fprog_uses_noperspective(fprog)) {
66 nonperspective_barycentric_enable_flag =
67 GEN6_CLIP_NON_PERSPECTIVE_BARYCENTRIC_ENABLE;
68 }
69
70 if (brw->hiz.op) {
71 /* HiZ operations emit a rectangle primitive, which requires clipping to
72 * be disabled. From page 10 of the Sandy Bridge PRM Volume 2 Part 1
73 * Section 1.3 3D Primitives Overview:
74 * RECTLIST:
75 * Either the CLIP unit should be DISABLED, or the CLIP unit's Clip
76 * Mode should be set to a value other than CLIPMODE_NORMAL.
77 */
78 BEGIN_BATCH(4);
79 OUT_BATCH(_3DSTATE_CLIP << 16 | (4 - 2));
80 OUT_BATCH(0);
81 OUT_BATCH(0);
82 OUT_BATCH(0);
83 ADVANCE_BATCH();
84 return;
85 }
86
87 if (!ctx->Transform.DepthClamp)
88 depth_clamp = GEN6_CLIP_Z_TEST;
89
90 /* _NEW_LIGHT */
91 if (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION) {
92 provoking =
93 (0 << GEN6_CLIP_TRI_PROVOKE_SHIFT) |
94 (1 << GEN6_CLIP_TRIFAN_PROVOKE_SHIFT) |
95 (0 << GEN6_CLIP_LINE_PROVOKE_SHIFT);
96 } else {
97 provoking =
98 (2 << GEN6_CLIP_TRI_PROVOKE_SHIFT) |
99 (2 << GEN6_CLIP_TRIFAN_PROVOKE_SHIFT) |
100 (1 << GEN6_CLIP_LINE_PROVOKE_SHIFT);
101 }
102
103 /* _NEW_TRANSFORM */
104 userclip = ctx->Transform.ClipPlanesEnabled;
105
106 BEGIN_BATCH(4);
107 OUT_BATCH(_3DSTATE_CLIP << 16 | (4 - 2));
108 OUT_BATCH(GEN6_CLIP_STATISTICS_ENABLE);
109 OUT_BATCH(GEN6_CLIP_ENABLE |
110 GEN6_CLIP_API_OGL |
111 GEN6_CLIP_MODE_NORMAL |
112 nonperspective_barycentric_enable_flag |
113 GEN6_CLIP_XY_TEST |
114 userclip << GEN6_USER_CLIP_CLIP_DISTANCES_SHIFT |
115 depth_clamp |
116 provoking);
117 OUT_BATCH(U_FIXED(0.125, 3) << GEN6_CLIP_MIN_POINT_WIDTH_SHIFT |
118 U_FIXED(255.875, 3) << GEN6_CLIP_MAX_POINT_WIDTH_SHIFT |
119 GEN6_CLIP_FORCE_ZERO_RTAINDEX);
120 ADVANCE_BATCH();
121 }
122
123 const struct brw_tracked_state gen6_clip_state = {
124 .dirty = {
125 .mesa = _NEW_TRANSFORM | _NEW_LIGHT,
126 .brw = (BRW_NEW_CONTEXT |
127 BRW_NEW_FRAGMENT_PROGRAM |
128 BRW_NEW_HIZ),
129 .cache = 0
130 },
131 .emit = upload_clip_state,
132 };