sync with tizen_2.2
[sdk/emulator/qemu.git] / gl / mesa / src / mesa / drivers / dri / i965 / gen7_sf_state.c
1 /*
2  * Copyright © 2011 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
24 #include "brw_context.h"
25 #include "brw_state.h"
26 #include "brw_defines.h"
27 #include "brw_util.h"
28 #include "main/macros.h"
29 #include "intel_batchbuffer.h"
30
31 static void
32 upload_sbe_state(struct brw_context *brw)
33 {
34    struct intel_context *intel = &brw->intel;
35    struct gl_context *ctx = &intel->ctx;
36    struct brw_vue_map vue_map;
37    uint32_t urb_entry_read_length;
38    /* CACHE_NEW_VS_PROG */
39    GLbitfield64 vs_outputs_written = brw->vs.prog_data->outputs_written;
40    /* BRW_NEW_FRAGMENT_PROGRAM */
41    uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
42    /* _NEW_LIGHT */
43    bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
44    uint32_t dw1, dw10, dw11;
45    int i;
46    int attr = 0, input_index = 0;
47    /* _NEW_TRANSFORM */
48    int urb_entry_read_offset = 1;
49    bool userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
50    uint16_t attr_overrides[FRAG_ATTRIB_MAX];
51    /* _NEW_BUFFERS */
52    bool render_to_fbo = ctx->DrawBuffer->Name != 0;
53    uint32_t point_sprite_origin;
54
55    brw_compute_vue_map(&vue_map, intel, userclip_active, vs_outputs_written);
56    urb_entry_read_length = (vue_map.num_slots + 1)/2 - urb_entry_read_offset;
57    if (urb_entry_read_length == 0) {
58       /* Setting the URB entry read length to 0 causes undefined behavior, so
59        * if we have no URB data to read, set it to 1.
60        */
61       urb_entry_read_length = 1;
62    }
63
64    /* FINISHME: Attribute Swizzle Control Mode? */
65    dw1 =
66       GEN7_SBE_SWIZZLE_ENABLE |
67       num_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT |
68       urb_entry_read_length << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
69       urb_entry_read_offset << GEN7_SBE_URB_ENTRY_READ_OFFSET_SHIFT;
70
71    /* _NEW_POINT
72     *
73     * Window coordinates in an FBO are inverted, which means point
74     * sprite origin must be inverted.
75     */
76    if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
77       point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
78    } else {
79       point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
80    }
81    dw1 |= point_sprite_origin;
82
83
84    dw10 = 0;
85    dw11 = 0;
86
87    /* Create the mapping from the FS inputs we produce to the VS outputs
88     * they source from.
89     */
90    for (; attr < FRAG_ATTRIB_MAX; attr++) {
91       enum glsl_interp_qualifier interp_qualifier =
92          brw->fragment_program->InterpQualifier[attr];
93       bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
94
95       if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
96          continue;
97
98       if (ctx->Point.PointSprite &&
99           attr >= FRAG_ATTRIB_TEX0 && attr <= FRAG_ATTRIB_TEX7 &&
100           ctx->Point.CoordReplace[attr - FRAG_ATTRIB_TEX0]) {
101          dw10 |= (1 << input_index);
102       }
103
104       if (attr == FRAG_ATTRIB_PNTC)
105          dw10 |= (1 << input_index);
106
107       /* flat shading */
108       if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
109           (shade_model_flat && is_gl_Color &&
110            interp_qualifier == INTERP_QUALIFIER_NONE))
111          dw11 |= (1 << input_index);
112
113       /* The hardware can only do the overrides on 16 overrides at a
114        * time, and the other up to 16 have to be lined up so that the
115        * input index = the output index.  We'll need to do some
116        * tweaking to make sure that's the case.
117        */
118       assert(input_index < 16 || attr == input_index);
119
120       /* _NEW_LIGHT | _NEW_PROGRAM */
121       attr_overrides[input_index++] =
122          get_attr_override(&vue_map, urb_entry_read_offset, attr,
123                            ctx->VertexProgram._TwoSideEnabled);
124    }
125
126    for (; input_index < FRAG_ATTRIB_MAX; input_index++)
127       attr_overrides[input_index] = 0;
128
129    BEGIN_BATCH(14);
130    OUT_BATCH(_3DSTATE_SBE << 16 | (14 - 2));
131    OUT_BATCH(dw1);
132
133    /* Output dwords 2 through 9 */
134    for (i = 0; i < 8; i++) {
135       OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
136    }
137
138    OUT_BATCH(dw10); /* point sprite texcoord bitmask */
139    OUT_BATCH(dw11); /* constant interp bitmask */
140    OUT_BATCH(0); /* wrapshortest enables 0-7 */
141    OUT_BATCH(0); /* wrapshortest enables 8-15 */
142    ADVANCE_BATCH();
143 }
144
145 const struct brw_tracked_state gen7_sbe_state = {
146    .dirty = {
147       .mesa  = (_NEW_LIGHT |
148                 _NEW_POINT |
149                 _NEW_PROGRAM |
150                 _NEW_TRANSFORM),
151       .brw   = (BRW_NEW_CONTEXT |
152                 BRW_NEW_FRAGMENT_PROGRAM),
153       .cache = CACHE_NEW_VS_PROG
154    },
155    .emit = upload_sbe_state,
156 };
157
158 static void
159 upload_sf_state(struct brw_context *brw)
160 {
161    struct intel_context *intel = &brw->intel;
162    struct gl_context *ctx = &intel->ctx;
163    uint32_t dw1, dw2, dw3;
164    float point_size;
165    /* _NEW_BUFFERS */
166    bool render_to_fbo = brw->intel.ctx.DrawBuffer->Name != 0;
167
168    dw1 = GEN6_SF_STATISTICS_ENABLE |
169          GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
170
171    /* _NEW_BUFFERS */
172    dw1 |= (brw_depthbuffer_format(brw) << GEN7_SF_DEPTH_BUFFER_SURFACE_FORMAT_SHIFT);
173
174    /* _NEW_POLYGON */
175    if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
176       dw1 |= GEN6_SF_WINDING_CCW;
177
178    if (ctx->Polygon.OffsetFill)
179        dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
180
181    if (ctx->Polygon.OffsetLine)
182        dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
183
184    if (ctx->Polygon.OffsetPoint)
185        dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
186
187    switch (ctx->Polygon.FrontMode) {
188    case GL_FILL:
189        dw1 |= GEN6_SF_FRONT_SOLID;
190        break;
191
192    case GL_LINE:
193        dw1 |= GEN6_SF_FRONT_WIREFRAME;
194        break;
195
196    case GL_POINT:
197        dw1 |= GEN6_SF_FRONT_POINT;
198        break;
199
200    default:
201        assert(0);
202        break;
203    }
204
205    switch (ctx->Polygon.BackMode) {
206    case GL_FILL:
207        dw1 |= GEN6_SF_BACK_SOLID;
208        break;
209
210    case GL_LINE:
211        dw1 |= GEN6_SF_BACK_WIREFRAME;
212        break;
213
214    case GL_POINT:
215        dw1 |= GEN6_SF_BACK_POINT;
216        break;
217
218    default:
219        assert(0);
220        break;
221    }
222
223    dw2 = 0;
224
225    if (ctx->Polygon.CullFlag) {
226       switch (ctx->Polygon.CullFaceMode) {
227       case GL_FRONT:
228          dw2 |= GEN6_SF_CULL_FRONT;
229          break;
230       case GL_BACK:
231          dw2 |= GEN6_SF_CULL_BACK;
232          break;
233       case GL_FRONT_AND_BACK:
234          dw2 |= GEN6_SF_CULL_BOTH;
235          break;
236       default:
237          assert(0);
238          break;
239       }
240    } else {
241       dw2 |= GEN6_SF_CULL_NONE;
242    }
243
244    /* _NEW_SCISSOR */
245    if (ctx->Scissor.Enabled)
246       dw2 |= GEN6_SF_SCISSOR_ENABLE;
247
248    /* _NEW_LINE */
249    dw2 |= U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7) <<
250       GEN6_SF_LINE_WIDTH_SHIFT;
251    if (ctx->Line.SmoothFlag) {
252       dw2 |= GEN6_SF_LINE_AA_ENABLE;
253       dw2 |= GEN6_SF_LINE_AA_MODE_TRUE;
254       dw2 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
255    }
256
257    /* FINISHME: Last Pixel Enable?  Vertex Sub Pixel Precision Select?
258     * FINISHME: AA Line Distance Mode?
259     */
260
261    dw3 = 0;
262
263    /* _NEW_PROGRAM | _NEW_POINT */
264    if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
265       dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
266
267    /* Clamp to ARB_point_parameters user limits */
268    point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
269
270    /* Clamp to the hardware limits and convert to fixed point */
271    dw3 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
272
273    /* _NEW_LIGHT */
274    if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
275       dw3 |=
276          (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
277          (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
278          (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
279    } else {
280       dw3 |= (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
281    }
282
283    BEGIN_BATCH(7);
284    OUT_BATCH(_3DSTATE_SF << 16 | (7 - 2));
285    OUT_BATCH(dw1);
286    OUT_BATCH(dw2);
287    OUT_BATCH(dw3);
288    OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant.  copied from gen4 */
289    OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
290    OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
291    ADVANCE_BATCH();
292 }
293
294 const struct brw_tracked_state gen7_sf_state = {
295    .dirty = {
296       .mesa  = (_NEW_LIGHT |
297                 _NEW_PROGRAM |
298                 _NEW_POLYGON |
299                 _NEW_LINE |
300                 _NEW_SCISSOR |
301                 _NEW_BUFFERS |
302                 _NEW_POINT),
303       .brw   = BRW_NEW_CONTEXT,
304       .cache = CACHE_NEW_VS_PROG
305    },
306    .emit = upload_sf_state,
307 };