Tizen 2.0 Release
[profile/ivi/osmesa.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 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 (i == VERT_RESULT_PSIZ)
58          continue;
59       if (brw->vs.prog_data->outputs_written & BITFIELD64_BIT(i))
60          attr_index++;
61    }
62
63    assert(attr_index < 32);
64
65    if (two_side_color) {
66        if ((brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_COL1)) &&
67            (brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC1))) {
68            assert(brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_COL0));
69            assert(brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC0));
70            bfc = 2;
71        } else if ((brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_COL0)) &&
72                 (brw->vs.prog_data->outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC0)))
73            bfc = 1;
74    }
75
76    if (bfc && (fs_attr <= FRAG_ATTRIB_TEX7 && fs_attr > FRAG_ATTRIB_WPOS)) {
77        if (fs_attr == FRAG_ATTRIB_COL0)
78            attr_index |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
79        else if (fs_attr == FRAG_ATTRIB_COL1 && bfc == 2) {
80            attr_index++;
81            attr_index |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
82        } else {
83            attr_index += bfc;
84        }
85    }
86
87    return attr_index;
88 }
89
90 static void
91 upload_sf_state(struct brw_context *brw)
92 {
93    struct intel_context *intel = &brw->intel;
94    struct gl_context *ctx = &intel->ctx;
95    /* CACHE_NEW_VS_PROG */
96    uint32_t num_inputs = brw_count_bits(brw->vs.prog_data->outputs_written);
97    /* BRW_NEW_FRAGMENT_PROGRAM */
98    uint32_t num_outputs = brw_count_bits(brw->fragment_program->Base.InputsRead);
99    uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
100    int i;
101    /* _NEW_BUFFER */
102    GLboolean render_to_fbo = brw->intel.ctx.DrawBuffer->Name != 0;
103    int attr = 0, input_index = 0;
104    int urb_start;
105    int two_side_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
106    float point_size;
107    uint16_t attr_overrides[FRAG_ATTRIB_MAX];
108
109    /* _NEW_TRANSFORM */
110    if (ctx->Transform.ClipPlanesEnabled)
111       urb_start = 2;
112    else
113       urb_start = 1;
114
115    dw1 =
116       GEN6_SF_SWIZZLE_ENABLE |
117       num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT |
118       (num_inputs + 1) / 2 << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
119       urb_start << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT;
120    dw2 = GEN6_SF_VIEWPORT_TRANSFORM_ENABLE |
121       GEN6_SF_STATISTICS_ENABLE;
122    dw3 = 0;
123    dw4 = 0;
124    dw16 = 0;
125    dw17 = 0;
126
127    /* _NEW_POLYGON */
128    if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
129       dw2 |= GEN6_SF_WINDING_CCW;
130
131    if (ctx->Polygon.OffsetFill)
132        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
133
134    if (ctx->Polygon.OffsetLine)
135        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
136
137    if (ctx->Polygon.OffsetPoint)
138        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
139
140    switch (ctx->Polygon.FrontMode) {
141    case GL_FILL:
142        dw2 |= GEN6_SF_FRONT_SOLID;
143        break;
144
145    case GL_LINE:
146        dw2 |= GEN6_SF_FRONT_WIREFRAME;
147        break;
148
149    case GL_POINT:
150        dw2 |= GEN6_SF_FRONT_POINT;
151        break;
152
153    default:
154        assert(0);
155        break;
156    }
157
158    switch (ctx->Polygon.BackMode) {
159    case GL_FILL:
160        dw2 |= GEN6_SF_BACK_SOLID;
161        break;
162
163    case GL_LINE:
164        dw2 |= GEN6_SF_BACK_WIREFRAME;
165        break;
166
167    case GL_POINT:
168        dw2 |= GEN6_SF_BACK_POINT;
169        break;
170
171    default:
172        assert(0);
173        break;
174    }
175
176    /* _NEW_SCISSOR */
177    if (ctx->Scissor.Enabled)
178       dw3 |= GEN6_SF_SCISSOR_ENABLE;
179
180    /* _NEW_POLYGON */
181    if (ctx->Polygon.CullFlag) {
182       switch (ctx->Polygon.CullFaceMode) {
183       case GL_FRONT:
184          dw3 |= GEN6_SF_CULL_FRONT;
185          break;
186       case GL_BACK:
187          dw3 |= GEN6_SF_CULL_BACK;
188          break;
189       case GL_FRONT_AND_BACK:
190          dw3 |= GEN6_SF_CULL_BOTH;
191          break;
192       default:
193          assert(0);
194          break;
195       }
196    } else {
197       dw3 |= GEN6_SF_CULL_NONE;
198    }
199
200    /* _NEW_LINE */
201    dw3 |= U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7) <<
202       GEN6_SF_LINE_WIDTH_SHIFT;
203    if (ctx->Line.SmoothFlag) {
204       dw3 |= GEN6_SF_LINE_AA_ENABLE;
205       dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
206       dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
207    }
208
209    /* _NEW_POINT */
210    if (!(ctx->VertexProgram.PointSizeEnabled ||
211          ctx->Point._Attenuated))
212       dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
213
214    /* Clamp to ARB_point_parameters user limits */
215    point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
216
217    /* Clamp to the hardware limits and convert to fixed point */
218    dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
219
220    if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT)
221       dw1 |= GEN6_SF_POINT_SPRITE_LOWERLEFT;
222
223    /* _NEW_LIGHT */
224    if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
225       dw4 |=
226          (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
227          (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
228          (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
229    } else {
230       dw4 |=
231          (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
232    }
233
234    /* Create the mapping from the FS inputs we produce to the VS outputs
235     * they source from.
236     */
237    for (; attr < FRAG_ATTRIB_MAX; attr++) {
238       if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
239          continue;
240
241       /* _NEW_POINT */
242       if (ctx->Point.PointSprite &&
243           (attr >= FRAG_ATTRIB_TEX0 && attr <= FRAG_ATTRIB_TEX7) &&
244           ctx->Point.CoordReplace[attr - FRAG_ATTRIB_TEX0]) {
245          dw16 |= (1 << input_index);
246       }
247
248       if (attr == FRAG_ATTRIB_PNTC)
249          dw16 |= (1 << input_index);
250
251       /* flat shading */
252       if (ctx->Light.ShadeModel == GL_FLAT) {
253          /*
254           * Setup the Constant Interpolation Enable bit mask for each
255           * corresponding attribute(currently, we only care two attrs:
256           * FRAG_BIT_COL0 and FRAG_BIT_COL1).
257           *
258           * FIXME: should we care other attributes?
259           */
260           if (attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1)
261              dw17 |= (1 << input_index);
262       }
263
264       /* The hardware can only do the overrides on 16 overrides at a
265        * time, and the other up to 16 have to be lined up so that the
266        * input index = the output index.  We'll need to do some
267        * tweaking to make sure that's the case.
268        */
269       assert(input_index < 16 || attr == input_index);
270
271       attr_overrides[input_index++] = get_attr_override(brw, attr,
272                                                         two_side_color);
273    }
274
275    for (; input_index < FRAG_ATTRIB_MAX; input_index++)
276       attr_overrides[input_index] = 0;
277
278    BEGIN_BATCH(20);
279    OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
280    OUT_BATCH(dw1);
281    OUT_BATCH(dw2);
282    OUT_BATCH(dw3);
283    OUT_BATCH(dw4);
284    OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant.  copied from gen4 */
285    OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
286    OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
287    for (i = 0; i < 8; i++) {
288       OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
289    }
290    OUT_BATCH(dw16); /* point sprite texcoord bitmask */
291    OUT_BATCH(dw17); /* constant interp bitmask */
292    OUT_BATCH(0); /* wrapshortest enables 0-7 */
293    OUT_BATCH(0); /* wrapshortest enables 8-15 */
294    ADVANCE_BATCH();
295 }
296
297 const struct brw_tracked_state gen6_sf_state = {
298    .dirty = {
299       .mesa  = (_NEW_LIGHT |
300                 _NEW_POLYGON |
301                 _NEW_LINE |
302                 _NEW_SCISSOR |
303                 _NEW_BUFFERS |
304                 _NEW_POINT |
305                 _NEW_TRANSFORM),
306       .brw   = (BRW_NEW_CONTEXT |
307                 BRW_NEW_FRAGMENT_PROGRAM),
308       .cache = CACHE_NEW_VS_PROG
309    },
310    .emit = upload_sf_state,
311 };