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