45c148baedd275153ba9aa538f00a6bb7b480547
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_sf_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 "main/macros.h"
33 #include "intel_batchbuffer.h"
34
35 static uint32_t
36 get_attr_override(struct brw_context *brw, int fs_attr, int two_side_color)
37 {
38 int attr_index = 0, i, vs_attr;
39 int bfc = 0;
40
41 if (fs_attr <= FRAG_ATTRIB_TEX7)
42 vs_attr = fs_attr;
43 else if (fs_attr == FRAG_ATTRIB_FACE)
44 vs_attr = 0; /* XXX */
45 else if (fs_attr == FRAG_ATTRIB_PNTC)
46 vs_attr = 0; /* XXX */
47 else {
48 assert(fs_attr >= FRAG_ATTRIB_VAR0);
49 vs_attr = fs_attr - FRAG_ATTRIB_VAR0 + VERT_RESULT_VAR0;
50 }
51
52 /* Find the source index (0 = first attribute after the 4D position)
53 * for this output attribute. attr is currently a VERT_RESULT_* but should
54 * be FRAG_ATTRIB_*.
55 */
56 for (i = 1; i < vs_attr; i++) {
57 if (brw->vs.prog_data->outputs_written & BITFIELD64_BIT(i))
58 attr_index++;
59 }
60
61 assert(attr_index < 32);
62
63 if (two_side_color) {
64 if ((brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_COL1)) &&
65 (brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC1))) {
66 assert(brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_COL0));
67 assert(brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC0));
68 bfc = 2;
69 } else if ((brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_COL0)) &&
70 (brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC0)))
71 bfc = 1;
72 }
73
74 if (bfc && (fs_attr <= FRAG_ATTRIB_TEX7 && fs_attr > FRAG_ATTRIB_WPOS)) {
75 if (fs_attr == FRAG_ATTRIB_COL0)
76 attr_index |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
77 else if (fs_attr == FRAG_ATTRIB_COL1 && bfc == 2) {
78 attr_index++;
79 attr_index |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
80 } else {
81 attr_index += bfc;
82 }
83 }
84
85 return attr_index;
86 }
87
88 static void
89 upload_sf_state(struct brw_context *brw)
90 {
91 struct intel_context *intel = &brw->intel;
92 struct gl_context *ctx = &intel->ctx;
93 /* CACHE_NEW_VS_PROG */
94 uint32_t num_inputs = brw_count_bits(brw->vs.prog_data->outputs_written);
95 /* BRW_NEW_FRAGMENT_PROGRAM */
96 uint32_t num_outputs = brw_count_bits(brw->fragment_program->Base.InputsRead);
97 uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
98 int i;
99 /* _NEW_BUFFER */
100 GLboolean render_to_fbo = brw->intel.ctx.DrawBuffer->Name != 0;
101 int attr = 0;
102 int urb_start;
103 int two_side_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
104
105 /* _NEW_TRANSFORM */
106 if (ctx->Transform.ClipPlanesEnabled)
107 urb_start = 2;
108 else
109 urb_start = 1;
110
111 dw1 =
112 GEN6_SF_SWIZZLE_ENABLE |
113 num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT |
114 (num_inputs + 1) / 2 << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
115 urb_start << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT;
116 dw2 = GEN6_SF_VIEWPORT_TRANSFORM_ENABLE |
117 GEN6_SF_STATISTICS_ENABLE;
118 dw3 = 0;
119 dw4 = 0;
120 dw16 = 0;
121 dw17 = 0;
122
123 /* _NEW_POLYGON */
124 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
125 dw2 |= GEN6_SF_WINDING_CCW;
126
127 if (ctx->Polygon.OffsetFill)
128 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
129
130 if (ctx->Polygon.OffsetLine)
131 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
132
133 if (ctx->Polygon.OffsetPoint)
134 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
135
136 switch (ctx->Polygon.FrontMode) {
137 case GL_FILL:
138 dw2 |= GEN6_SF_FRONT_SOLID;
139 break;
140
141 case GL_LINE:
142 dw2 |= GEN6_SF_FRONT_WIREFRAME;
143 break;
144
145 case GL_POINT:
146 dw2 |= GEN6_SF_FRONT_POINT;
147 break;
148
149 default:
150 assert(0);
151 break;
152 }
153
154 switch (ctx->Polygon.BackMode) {
155 case GL_FILL:
156 dw2 |= GEN6_SF_BACK_SOLID;
157 break;
158
159 case GL_LINE:
160 dw2 |= GEN6_SF_BACK_WIREFRAME;
161 break;
162
163 case GL_POINT:
164 dw2 |= GEN6_SF_BACK_POINT;
165 break;
166
167 default:
168 assert(0);
169 break;
170 }
171
172 /* _NEW_SCISSOR */
173 if (ctx->Scissor.Enabled)
174 dw3 |= GEN6_SF_SCISSOR_ENABLE;
175
176 /* _NEW_POLYGON */
177 if (ctx->Polygon.CullFlag) {
178 switch (ctx->Polygon.CullFaceMode) {
179 case GL_FRONT:
180 dw3 |= GEN6_SF_CULL_FRONT;
181 break;
182 case GL_BACK:
183 dw3 |= GEN6_SF_CULL_BACK;
184 break;
185 case GL_FRONT_AND_BACK:
186 dw3 |= GEN6_SF_CULL_BOTH;
187 break;
188 default:
189 assert(0);
190 break;
191 }
192 } else {
193 dw3 |= GEN6_SF_CULL_NONE;
194 }
195
196 /* _NEW_LINE */
197 dw3 |= U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7) <<
198 GEN6_SF_LINE_WIDTH_SHIFT;
199 if (ctx->Line.SmoothFlag) {
200 dw3 |= GEN6_SF_LINE_AA_ENABLE;
201 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
202 dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
203 }
204
205 /* _NEW_POINT */
206 if (!(ctx->VertexProgram.PointSizeEnabled ||
207 ctx->Point._Attenuated))
208 dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
209
210 dw4 |= U_FIXED(CLAMP(ctx->Point.Size, 0.125, 225.875), 3) <<
211 GEN6_SF_POINT_WIDTH_SHIFT;
212 if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT)
213 dw1 |= GEN6_SF_POINT_SPRITE_LOWERLEFT;
214
215 /* _NEW_LIGHT */
216 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
217 dw4 |=
218 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
219 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
220 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
221 } else {
222 dw4 |=
223 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
224 }
225
226 if (ctx->Point.PointSprite) {
227 for (i = 0; i < 8; i++) {
228 if (ctx->Point.CoordReplace[i])
229 dw16 |= (1 << i);
230 }
231 }
232
233 /* flat shading */
234 if (ctx->Light.ShadeModel == GL_FLAT) {
235 dw17 |= ((brw->fragment_program->Base.InputsRead & (FRAG_BIT_COL0 | FRAG_BIT_COL1)) >>
236 ((brw->fragment_program->Base.InputsRead & FRAG_BIT_WPOS) ? 0 : 1));
237 }
238
239 BEGIN_BATCH(20);
240 OUT_BATCH(CMD_3D_SF_STATE << 16 | (20 - 2));
241 OUT_BATCH(dw1);
242 OUT_BATCH(dw2);
243 OUT_BATCH(dw3);
244 OUT_BATCH(dw4);
245 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
246 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
247 OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
248 for (i = 0; i < 8; i++) {
249 uint32_t attr_overrides = 0;
250
251 for (; attr < 64; attr++) {
252 if (brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)) {
253 attr_overrides |= get_attr_override(brw, attr, two_side_color);
254 attr++;
255 break;
256 }
257 }
258
259 for (; attr < 64; attr++) {
260 if (brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)) {
261 attr_overrides |= get_attr_override(brw, attr, two_side_color) << 16;
262 attr++;
263 break;
264 }
265 }
266 OUT_BATCH(attr_overrides);
267 }
268 OUT_BATCH(dw16); /* point sprite texcoord bitmask */
269 OUT_BATCH(dw17); /* constant interp bitmask */
270 OUT_BATCH(0); /* wrapshortest enables 0-7 */
271 OUT_BATCH(0); /* wrapshortest enables 8-15 */
272 ADVANCE_BATCH();
273 }
274
275 const struct brw_tracked_state gen6_sf_state = {
276 .dirty = {
277 .mesa = (_NEW_LIGHT |
278 _NEW_POLYGON |
279 _NEW_LINE |
280 _NEW_SCISSOR |
281 _NEW_BUFFERS |
282 _NEW_POINT |
283 _NEW_TRANSFORM),
284 .brw = (BRW_NEW_CONTEXT |
285 BRW_NEW_FRAGMENT_PROGRAM),
286 .cache = CACHE_NEW_VS_PROG
287 },
288 .emit = upload_sf_state,
289 };