i965/msaa: Only do multisample rasterization if GL_MULTISAMPLE enabled.
[profile/ivi/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 "main/fbobject.h"
34 #include "intel_batchbuffer.h"
35
36 /**
37  * Determine the appropriate attribute override value to store into the
38  * 3DSTATE_SF structure for a given fragment shader attribute.  The attribute
39  * override value contains two pieces of information: the location of the
40  * attribute in the VUE (relative to urb_entry_read_offset, see below), and a
41  * flag indicating whether to "swizzle" the attribute based on the direction
42  * the triangle is facing.
43  *
44  * If an attribute is "swizzled", then the given VUE location is used for
45  * front-facing triangles, and the VUE location that immediately follows is
46  * used for back-facing triangles.  We use this to implement the mapping from
47  * gl_FrontColor/gl_BackColor to gl_Color.
48  *
49  * urb_entry_read_offset is the offset into the VUE at which the SF unit is
50  * being instructed to begin reading attribute data.  It can be set to a
51  * nonzero value to prevent the SF unit from wasting time reading elements of
52  * the VUE that are not needed by the fragment shader.  It is measured in
53  * 256-bit increments.
54  */
55 uint32_t
56 get_attr_override(struct brw_vue_map *vue_map, int urb_entry_read_offset,
57                   int fs_attr, bool two_side_color)
58 {
59    int attr_override, slot;
60    int vs_attr = _mesa_frag_attrib_to_vert_result(fs_attr);
61    if (vs_attr < 0 || vs_attr == VERT_RESULT_HPOS) {
62       /* These attributes will be overwritten by the fragment shader's
63        * interpolation code (see emit_interp() in brw_wm_fp.c), so just let
64        * them reference the first available attribute.
65        */
66       return 0;
67    }
68
69    /* Find the VUE slot for this attribute. */
70    slot = vue_map->vert_result_to_slot[vs_attr];
71
72    /* If there was only a back color written but not front, use back
73     * as the color instead of undefined
74     */
75    if (slot == -1 && vs_attr == VERT_RESULT_COL0)
76       slot = vue_map->vert_result_to_slot[VERT_RESULT_BFC0];
77    if (slot == -1 && vs_attr == VERT_RESULT_COL1)
78       slot = vue_map->vert_result_to_slot[VERT_RESULT_BFC1];
79
80    if (slot == -1) {
81       /* This attribute does not exist in the VUE--that means that the vertex
82        * shader did not write to it.  Behavior is undefined in this case, so
83        * just reference the first available attribute.
84        */
85       return 0;
86    }
87
88    /* Compute the location of the attribute relative to urb_entry_read_offset.
89     * Each increment of urb_entry_read_offset represents a 256-bit value, so
90     * it counts for two 128-bit VUE slots.
91     */
92    attr_override = slot - 2 * urb_entry_read_offset;
93    assert (attr_override >= 0 && attr_override < 32);
94
95    /* If we are doing two-sided color, and the VUE slot following this one
96     * represents a back-facing color, then we need to instruct the SF unit to
97     * do back-facing swizzling.
98     */
99    if (two_side_color) {
100       if (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL0 &&
101           vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC0)
102          attr_override |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
103       else if (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL1 &&
104                vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC1)
105          attr_override |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
106    }
107
108    return attr_override;
109 }
110
111 static void
112 upload_sf_state(struct brw_context *brw)
113 {
114    struct intel_context *intel = &brw->intel;
115    struct gl_context *ctx = &intel->ctx;
116    uint32_t urb_entry_read_length;
117    /* BRW_NEW_FRAGMENT_PROGRAM */
118    uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
119    /* _NEW_LIGHT */
120    bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
121    uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
122    int i;
123    /* _NEW_BUFFER */
124    bool render_to_fbo = _mesa_is_user_fbo(brw->intel.ctx.DrawBuffer);
125    bool multisampled_fbo = false;
126    if (ctx->DrawBuffer->_ColorDrawBuffers[0])
127       multisampled_fbo = ctx->DrawBuffer->_ColorDrawBuffers[0]->NumSamples > 0;
128
129    int attr = 0, input_index = 0;
130    int urb_entry_read_offset = 1;
131    float point_size;
132    uint16_t attr_overrides[FRAG_ATTRIB_MAX];
133    uint32_t point_sprite_origin;
134
135    /* CACHE_NEW_VS_PROG */
136    urb_entry_read_length = ((brw->vs.prog_data->vue_map.num_slots + 1) / 2 -
137                             urb_entry_read_offset);
138    if (urb_entry_read_length == 0) {
139       /* Setting the URB entry read length to 0 causes undefined behavior, so
140        * if we have no URB data to read, set it to 1.
141        */
142       urb_entry_read_length = 1;
143    }
144
145    dw1 =
146       GEN6_SF_SWIZZLE_ENABLE |
147       num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT |
148       urb_entry_read_length << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
149       urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT;
150
151    dw2 = GEN6_SF_STATISTICS_ENABLE |
152          GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
153
154    dw3 = 0;
155    dw4 = 0;
156    dw16 = 0;
157    dw17 = 0;
158
159    /* _NEW_POLYGON */
160    if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
161       dw2 |= GEN6_SF_WINDING_CCW;
162
163    if (ctx->Polygon.OffsetFill)
164        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
165
166    if (ctx->Polygon.OffsetLine)
167        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
168
169    if (ctx->Polygon.OffsetPoint)
170        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
171
172    switch (ctx->Polygon.FrontMode) {
173    case GL_FILL:
174        dw2 |= GEN6_SF_FRONT_SOLID;
175        break;
176
177    case GL_LINE:
178        dw2 |= GEN6_SF_FRONT_WIREFRAME;
179        break;
180
181    case GL_POINT:
182        dw2 |= GEN6_SF_FRONT_POINT;
183        break;
184
185    default:
186        assert(0);
187        break;
188    }
189
190    switch (ctx->Polygon.BackMode) {
191    case GL_FILL:
192        dw2 |= GEN6_SF_BACK_SOLID;
193        break;
194
195    case GL_LINE:
196        dw2 |= GEN6_SF_BACK_WIREFRAME;
197        break;
198
199    case GL_POINT:
200        dw2 |= GEN6_SF_BACK_POINT;
201        break;
202
203    default:
204        assert(0);
205        break;
206    }
207
208    /* _NEW_SCISSOR */
209    if (ctx->Scissor.Enabled)
210       dw3 |= GEN6_SF_SCISSOR_ENABLE;
211
212    /* _NEW_POLYGON */
213    if (ctx->Polygon.CullFlag) {
214       switch (ctx->Polygon.CullFaceMode) {
215       case GL_FRONT:
216          dw3 |= GEN6_SF_CULL_FRONT;
217          break;
218       case GL_BACK:
219          dw3 |= GEN6_SF_CULL_BACK;
220          break;
221       case GL_FRONT_AND_BACK:
222          dw3 |= GEN6_SF_CULL_BOTH;
223          break;
224       default:
225          assert(0);
226          break;
227       }
228    } else {
229       dw3 |= GEN6_SF_CULL_NONE;
230    }
231
232    /* _NEW_LINE */
233    {
234       uint32_t line_width_u3_7 = U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7);
235       /* TODO: line width of 0 is not allowed when MSAA enabled */
236       if (line_width_u3_7 == 0)
237          line_width_u3_7 = 1;
238       dw3 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
239    }
240    if (ctx->Line.SmoothFlag) {
241       dw3 |= GEN6_SF_LINE_AA_ENABLE;
242       dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
243       dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
244    }
245    /* _NEW_MULTISAMPLE */
246    if (multisampled_fbo && ctx->Multisample.Enabled)
247       dw3 |= GEN6_SF_MSRAST_ON_PATTERN;
248
249    /* _NEW_PROGRAM | _NEW_POINT */
250    if (!(ctx->VertexProgram.PointSizeEnabled ||
251          ctx->Point._Attenuated))
252       dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
253
254    /* Clamp to ARB_point_parameters user limits */
255    point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
256
257    /* Clamp to the hardware limits and convert to fixed point */
258    dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
259
260    /*
261     * Window coordinates in an FBO are inverted, which means point
262     * sprite origin must be inverted, too.
263     */
264    if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
265       point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
266    } else {
267       point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
268    }
269    dw1 |= point_sprite_origin;
270
271    /* _NEW_LIGHT */
272    if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
273       dw4 |=
274          (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
275          (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
276          (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
277    } else {
278       dw4 |=
279          (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
280    }
281
282    /* Create the mapping from the FS inputs we produce to the VS outputs
283     * they source from.
284     */
285    for (; attr < FRAG_ATTRIB_MAX; attr++) {
286       enum glsl_interp_qualifier interp_qualifier =
287          brw->fragment_program->InterpQualifier[attr];
288       bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
289
290       if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
291          continue;
292
293       /* _NEW_POINT */
294       if (ctx->Point.PointSprite &&
295           (attr >= FRAG_ATTRIB_TEX0 && attr <= FRAG_ATTRIB_TEX7) &&
296           ctx->Point.CoordReplace[attr - FRAG_ATTRIB_TEX0]) {
297          dw16 |= (1 << input_index);
298       }
299
300       if (attr == FRAG_ATTRIB_PNTC)
301          dw16 |= (1 << input_index);
302
303       /* flat shading */
304       if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
305           (shade_model_flat && is_gl_Color &&
306            interp_qualifier == INTERP_QUALIFIER_NONE))
307          dw17 |= (1 << input_index);
308
309       /* The hardware can only do the overrides on 16 overrides at a
310        * time, and the other up to 16 have to be lined up so that the
311        * input index = the output index.  We'll need to do some
312        * tweaking to make sure that's the case.
313        */
314       assert(input_index < 16 || attr == input_index);
315
316       /* CACHE_NEW_VS_PROG | _NEW_LIGHT | _NEW_PROGRAM */
317       attr_overrides[input_index++] =
318          get_attr_override(&brw->vs.prog_data->vue_map,
319                            urb_entry_read_offset, attr,
320                            ctx->VertexProgram._TwoSideEnabled);
321    }
322
323    for (; input_index < FRAG_ATTRIB_MAX; input_index++)
324       attr_overrides[input_index] = 0;
325
326    BEGIN_BATCH(20);
327    OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
328    OUT_BATCH(dw1);
329    OUT_BATCH(dw2);
330    OUT_BATCH(dw3);
331    OUT_BATCH(dw4);
332    OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant.  copied from gen4 */
333    OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
334    OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
335    for (i = 0; i < 8; i++) {
336       OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
337    }
338    OUT_BATCH(dw16); /* point sprite texcoord bitmask */
339    OUT_BATCH(dw17); /* constant interp bitmask */
340    OUT_BATCH(0); /* wrapshortest enables 0-7 */
341    OUT_BATCH(0); /* wrapshortest enables 8-15 */
342    ADVANCE_BATCH();
343 }
344
345 const struct brw_tracked_state gen6_sf_state = {
346    .dirty = {
347       .mesa  = (_NEW_LIGHT |
348                 _NEW_PROGRAM |
349                 _NEW_POLYGON |
350                 _NEW_LINE |
351                 _NEW_SCISSOR |
352                 _NEW_BUFFERS |
353                 _NEW_POINT |
354                 _NEW_MULTISAMPLE),
355       .brw   = (BRW_NEW_CONTEXT |
356                 BRW_NEW_FRAGMENT_PROGRAM),
357       .cache = CACHE_NEW_VS_PROG
358    },
359    .emit = upload_sf_state,
360 };