Merge branch 'lp-offset-twoside'
[mesa.git] / src / mesa / drivers / dri / i965 / brw_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/state.h"
32 #include "main/enums.h"
33 #include "tnl/tnl.h"
34 #include "vbo/vbo_context.h"
35 #include "swrast/swrast.h"
36 #include "swrast_setup/swrast_setup.h"
37
38 #include "brw_draw.h"
39 #include "brw_defines.h"
40 #include "brw_context.h"
41 #include "brw_state.h"
42
43 #include "intel_batchbuffer.h"
44
45 #define FILE_DEBUG_FLAG DEBUG_PRIMS
46
47 static GLuint prim_to_hw_prim[GL_POLYGON+1] = {
48 _3DPRIM_POINTLIST,
49 _3DPRIM_LINELIST,
50 _3DPRIM_LINELOOP,
51 _3DPRIM_LINESTRIP,
52 _3DPRIM_TRILIST,
53 _3DPRIM_TRISTRIP,
54 _3DPRIM_TRIFAN,
55 _3DPRIM_QUADLIST,
56 _3DPRIM_QUADSTRIP,
57 _3DPRIM_POLYGON
58 };
59
60
61 static const GLenum reduced_prim[GL_POLYGON+1] = {
62 GL_POINTS,
63 GL_LINES,
64 GL_LINES,
65 GL_LINES,
66 GL_TRIANGLES,
67 GL_TRIANGLES,
68 GL_TRIANGLES,
69 GL_TRIANGLES,
70 GL_TRIANGLES,
71 GL_TRIANGLES
72 };
73
74
75 /* When the primitive changes, set a state bit and re-validate. Not
76 * the nicest and would rather deal with this by having all the
77 * programs be immune to the active primitive (ie. cope with all
78 * possibilities). That may not be realistic however.
79 */
80 static GLuint brw_set_prim(struct brw_context *brw,
81 const struct _mesa_prim *prim)
82 {
83 struct gl_context *ctx = &brw->intel.ctx;
84 GLenum mode = prim->mode;
85
86 DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim->mode));
87
88 /* Slight optimization to avoid the GS program when not needed:
89 */
90 if (mode == GL_QUAD_STRIP &&
91 ctx->Light.ShadeModel != GL_FLAT &&
92 ctx->Polygon.FrontMode == GL_FILL &&
93 ctx->Polygon.BackMode == GL_FILL)
94 mode = GL_TRIANGLE_STRIP;
95
96 if (prim->mode == GL_QUADS && prim->count == 4 &&
97 ctx->Light.ShadeModel != GL_FLAT &&
98 ctx->Polygon.FrontMode == GL_FILL &&
99 ctx->Polygon.BackMode == GL_FILL) {
100 mode = GL_TRIANGLE_FAN;
101 }
102
103 if (mode != brw->primitive) {
104 brw->primitive = mode;
105 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
106
107 if (reduced_prim[mode] != brw->intel.reduced_primitive) {
108 brw->intel.reduced_primitive = reduced_prim[mode];
109 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
110 }
111 }
112
113 return prim_to_hw_prim[mode];
114 }
115
116
117 static GLuint trim(GLenum prim, GLuint length)
118 {
119 if (prim == GL_QUAD_STRIP)
120 return length > 3 ? (length - length % 2) : 0;
121 else if (prim == GL_QUADS)
122 return length - length % 4;
123 else
124 return length;
125 }
126
127
128 static void brw_emit_prim(struct brw_context *brw,
129 const struct _mesa_prim *prim,
130 uint32_t hw_prim)
131 {
132 struct brw_3d_primitive prim_packet;
133 struct intel_context *intel = &brw->intel;
134
135 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
136 prim->start, prim->count);
137
138 prim_packet.header.opcode = CMD_3D_PRIM;
139 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
140 prim_packet.header.pad = 0;
141 prim_packet.header.topology = hw_prim;
142 prim_packet.header.indexed = prim->indexed;
143
144 prim_packet.verts_per_instance = trim(prim->mode, prim->count);
145 prim_packet.start_vert_location = prim->start;
146 if (prim->indexed)
147 prim_packet.start_vert_location += brw->ib.start_vertex_offset;
148 prim_packet.instance_count = 1;
149 prim_packet.start_instance_location = 0;
150 prim_packet.base_vert_location = prim->basevertex;
151
152 /* If we're set to always flush, do it before and after the primitive emit.
153 * We want to catch both missed flushes that hurt instruction/state cache
154 * and missed flushes of the render cache as it heads to other parts of
155 * the besides the draw code.
156 */
157 if (intel->always_flush_cache) {
158 intel_batchbuffer_emit_mi_flush(intel->batch);
159 }
160 if (prim_packet.verts_per_instance) {
161 intel_batchbuffer_data( brw->intel.batch, &prim_packet,
162 sizeof(prim_packet));
163 }
164 if (intel->always_flush_cache) {
165 intel_batchbuffer_emit_mi_flush(intel->batch);
166 }
167 }
168
169 static void brw_merge_inputs( struct brw_context *brw,
170 const struct gl_client_array *arrays[])
171 {
172 struct brw_vertex_info old = brw->vb.info;
173 GLuint i;
174
175 for (i = 0; i < VERT_ATTRIB_MAX; i++)
176 drm_intel_bo_unreference(brw->vb.inputs[i].bo);
177
178 memset(&brw->vb.inputs, 0, sizeof(brw->vb.inputs));
179 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
180
181 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
182 brw->vb.inputs[i].glarray = arrays[i];
183 brw->vb.inputs[i].attrib = (gl_vert_attrib) i;
184
185 if (arrays[i]->StrideB != 0)
186 brw->vb.info.sizes[i/16] |= (brw->vb.inputs[i].glarray->Size - 1) <<
187 ((i%16) * 2);
188 }
189
190 /* Raise statechanges if input sizes have changed. */
191 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
192 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
193 }
194
195 /* XXX: could split the primitive list to fallback only on the
196 * non-conformant primitives.
197 */
198 static GLboolean check_fallbacks( struct brw_context *brw,
199 const struct _mesa_prim *prim,
200 GLuint nr_prims )
201 {
202 struct gl_context *ctx = &brw->intel.ctx;
203 GLuint i;
204
205 /* If we don't require strict OpenGL conformance, never
206 * use fallbacks. If we're forcing fallbacks, always
207 * use fallfacks.
208 */
209 if (brw->intel.conformance_mode == 0)
210 return GL_FALSE;
211
212 if (brw->intel.conformance_mode == 2)
213 return GL_TRUE;
214
215 if (ctx->Polygon.SmoothFlag) {
216 for (i = 0; i < nr_prims; i++)
217 if (reduced_prim[prim[i].mode] == GL_TRIANGLES)
218 return GL_TRUE;
219 }
220
221 /* BRW hardware will do AA lines, but they are non-conformant it
222 * seems. TBD whether we keep this fallback:
223 */
224 if (ctx->Line.SmoothFlag) {
225 for (i = 0; i < nr_prims; i++)
226 if (reduced_prim[prim[i].mode] == GL_LINES)
227 return GL_TRUE;
228 }
229
230 /* Stipple -- these fallbacks could be resolved with a little
231 * bit of work?
232 */
233 if (ctx->Line.StippleFlag) {
234 for (i = 0; i < nr_prims; i++) {
235 /* GS doesn't get enough information to know when to reset
236 * the stipple counter?!?
237 */
238 if (prim[i].mode == GL_LINE_LOOP || prim[i].mode == GL_LINE_STRIP)
239 return GL_TRUE;
240
241 if (prim[i].mode == GL_POLYGON &&
242 (ctx->Polygon.FrontMode == GL_LINE ||
243 ctx->Polygon.BackMode == GL_LINE))
244 return GL_TRUE;
245 }
246 }
247
248 if (ctx->Point.SmoothFlag) {
249 for (i = 0; i < nr_prims; i++)
250 if (prim[i].mode == GL_POINTS)
251 return GL_TRUE;
252 }
253
254 /* BRW hardware doesn't handle GL_CLAMP texturing correctly;
255 * brw_wm_sampler_state:translate_wrap_mode() treats GL_CLAMP
256 * as GL_CLAMP_TO_EDGE instead. If we're using GL_CLAMP, and
257 * we want strict conformance, force the fallback.
258 * Right now, we only do this for 2D textures.
259 */
260 {
261 int u;
262 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
263 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[u];
264 if (texUnit->Enabled) {
265 if (texUnit->Enabled & TEXTURE_1D_BIT) {
266 if (texUnit->CurrentTex[TEXTURE_1D_INDEX]->WrapS == GL_CLAMP) {
267 return GL_TRUE;
268 }
269 }
270 if (texUnit->Enabled & TEXTURE_2D_BIT) {
271 if (texUnit->CurrentTex[TEXTURE_2D_INDEX]->WrapS == GL_CLAMP ||
272 texUnit->CurrentTex[TEXTURE_2D_INDEX]->WrapT == GL_CLAMP) {
273 return GL_TRUE;
274 }
275 }
276 if (texUnit->Enabled & TEXTURE_3D_BIT) {
277 if (texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapS == GL_CLAMP ||
278 texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapT == GL_CLAMP ||
279 texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapR == GL_CLAMP) {
280 return GL_TRUE;
281 }
282 }
283 }
284 }
285 }
286
287 /* Nothing stopping us from the fast path now */
288 return GL_FALSE;
289 }
290
291 /* May fail if out of video memory for texture or vbo upload, or on
292 * fallback conditions.
293 */
294 static GLboolean brw_try_draw_prims( struct gl_context *ctx,
295 const struct gl_client_array *arrays[],
296 const struct _mesa_prim *prim,
297 GLuint nr_prims,
298 const struct _mesa_index_buffer *ib,
299 GLuint min_index,
300 GLuint max_index )
301 {
302 struct intel_context *intel = intel_context(ctx);
303 struct brw_context *brw = brw_context(ctx);
304 GLboolean retval = GL_FALSE;
305 GLboolean warn = GL_FALSE;
306 GLboolean first_time = GL_TRUE;
307 GLuint i;
308
309 if (ctx->NewState)
310 _mesa_update_state( ctx );
311
312 /* We have to validate the textures *before* checking for fallbacks;
313 * otherwise, the software fallback won't be able to rely on the
314 * texture state, the firstLevel and lastLevel fields won't be
315 * set in the intel texture object (they'll both be 0), and the
316 * software fallback will segfault if it attempts to access any
317 * texture level other than level 0.
318 */
319 brw_validate_textures( brw );
320
321 if (check_fallbacks(brw, prim, nr_prims))
322 return GL_FALSE;
323
324 /* Bind all inputs, derive varying and size information:
325 */
326 brw_merge_inputs( brw, arrays );
327
328 brw->ib.ib = ib;
329 brw->state.dirty.brw |= BRW_NEW_INDICES;
330
331 brw->vb.min_index = min_index;
332 brw->vb.max_index = max_index;
333 brw->state.dirty.brw |= BRW_NEW_VERTICES;
334
335 /* Have to validate state quite late. Will rebuild tnl_program,
336 * which depends on varying information.
337 *
338 * Note this is where brw->vs->prog_data.inputs_read is calculated,
339 * so can't access it earlier.
340 */
341
342 intel_prepare_render(intel);
343
344 for (i = 0; i < nr_prims; i++) {
345 uint32_t hw_prim;
346
347 /* Flush the batch if it's approaching full, so that we don't wrap while
348 * we've got validated state that needs to be in the same batch as the
349 * primitives. This fraction is just a guess (minimal full state plus
350 * a primitive is around 512 bytes), and would be better if we had
351 * an upper bound of how much we might emit in a single
352 * brw_try_draw_prims().
353 */
354 intel_batchbuffer_require_space(intel->batch, intel->batch->size / 4);
355
356 hw_prim = brw_set_prim(brw, &prim[i]);
357
358 if (first_time || (brw->state.dirty.brw & BRW_NEW_PRIMITIVE)) {
359 first_time = GL_FALSE;
360
361 brw_validate_state(brw);
362
363 /* Various fallback checks: */
364 if (brw->intel.Fallback)
365 goto out;
366
367 /* Check that we can fit our state in with our existing batchbuffer, or
368 * flush otherwise.
369 */
370 if (dri_bufmgr_check_aperture_space(brw->state.validated_bos,
371 brw->state.validated_bo_count)) {
372 static GLboolean warned;
373 intel_batchbuffer_flush(intel->batch);
374
375 /* Validate the state after we flushed the batch (which would have
376 * changed the set of dirty state). If we still fail to
377 * check_aperture, warn of what's happening, but attempt to continue
378 * on since it may succeed anyway, and the user would probably rather
379 * see a failure and a warning than a fallback.
380 */
381 brw_validate_state(brw);
382 if (!warned &&
383 dri_bufmgr_check_aperture_space(brw->state.validated_bos,
384 brw->state.validated_bo_count)) {
385 warn = GL_TRUE;
386 warned = GL_TRUE;
387 }
388 }
389
390 intel->no_batch_wrap = GL_TRUE;
391 brw_upload_state(brw);
392 }
393
394 brw_emit_prim(brw, &prim[i], hw_prim);
395
396 intel->no_batch_wrap = GL_FALSE;
397
398 retval = GL_TRUE;
399 }
400
401 if (intel->always_flush_batch)
402 intel_batchbuffer_flush(intel->batch);
403 out:
404
405 brw_state_cache_check_size(brw);
406
407 if (warn)
408 fprintf(stderr, "i965: Single primitive emit potentially exceeded "
409 "available aperture space\n");
410
411 if (!retval)
412 DBG("%s failed\n", __FUNCTION__);
413
414 return retval;
415 }
416
417 void brw_draw_prims( struct gl_context *ctx,
418 const struct gl_client_array *arrays[],
419 const struct _mesa_prim *prim,
420 GLuint nr_prims,
421 const struct _mesa_index_buffer *ib,
422 GLboolean index_bounds_valid,
423 GLuint min_index,
424 GLuint max_index )
425 {
426 GLboolean retval;
427
428 if (!vbo_all_varyings_in_vbos(arrays)) {
429 if (!index_bounds_valid)
430 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
431
432 /* Decide if we want to rebase. If so we end up recursing once
433 * only into this function.
434 */
435 if (min_index != 0 && !vbo_any_varyings_in_vbos(arrays)) {
436 vbo_rebase_prims(ctx, arrays,
437 prim, nr_prims,
438 ib, min_index, max_index,
439 brw_draw_prims );
440 return;
441 }
442 }
443
444 /* Make a first attempt at drawing:
445 */
446 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
447
448 /* Otherwise, we really are out of memory. Pass the drawing
449 * command to the software tnl module and which will in turn call
450 * swrast to do the drawing.
451 */
452 if (!retval) {
453 _swsetup_Wakeup(ctx);
454 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
455 }
456
457 }
458
459 void brw_draw_init( struct brw_context *brw )
460 {
461 struct gl_context *ctx = &brw->intel.ctx;
462 struct vbo_context *vbo = vbo_context(ctx);
463
464 /* Register our drawing function:
465 */
466 vbo->draw_prims = brw_draw_prims;
467 }
468
469 void brw_draw_destroy( struct brw_context *brw )
470 {
471 int i;
472
473 if (brw->vb.upload.bo != NULL) {
474 drm_intel_bo_unreference(brw->vb.upload.bo);
475 brw->vb.upload.bo = NULL;
476 }
477
478 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
479 drm_intel_bo_unreference(brw->vb.inputs[i].bo);
480 brw->vb.inputs[i].bo = NULL;
481 }
482
483 drm_intel_bo_unreference(brw->ib.bo);
484 brw->ib.bo = NULL;
485 }