i965: Fix flat integral varyings.
[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 #include <sys/errno.h>
29
30 #include "main/glheader.h"
31 #include "main/context.h"
32 #include "main/condrender.h"
33 #include "main/samplerobj.h"
34 #include "main/state.h"
35 #include "main/enums.h"
36 #include "tnl/tnl.h"
37 #include "vbo/vbo_context.h"
38 #include "swrast/swrast.h"
39 #include "swrast_setup/swrast_setup.h"
40
41 #include "brw_draw.h"
42 #include "brw_defines.h"
43 #include "brw_context.h"
44 #include "brw_state.h"
45
46 #include "intel_batchbuffer.h"
47
48 #define FILE_DEBUG_FLAG DEBUG_PRIMS
49
50 static GLuint prim_to_hw_prim[GL_POLYGON+1] = {
51 _3DPRIM_POINTLIST,
52 _3DPRIM_LINELIST,
53 _3DPRIM_LINELOOP,
54 _3DPRIM_LINESTRIP,
55 _3DPRIM_TRILIST,
56 _3DPRIM_TRISTRIP,
57 _3DPRIM_TRIFAN,
58 _3DPRIM_QUADLIST,
59 _3DPRIM_QUADSTRIP,
60 _3DPRIM_POLYGON
61 };
62
63
64 static const GLenum reduced_prim[GL_POLYGON+1] = {
65 GL_POINTS,
66 GL_LINES,
67 GL_LINES,
68 GL_LINES,
69 GL_TRIANGLES,
70 GL_TRIANGLES,
71 GL_TRIANGLES,
72 GL_TRIANGLES,
73 GL_TRIANGLES,
74 GL_TRIANGLES
75 };
76
77
78 /* When the primitive changes, set a state bit and re-validate. Not
79 * the nicest and would rather deal with this by having all the
80 * programs be immune to the active primitive (ie. cope with all
81 * possibilities). That may not be realistic however.
82 */
83 static void brw_set_prim(struct brw_context *brw,
84 const struct _mesa_prim *prim)
85 {
86 struct gl_context *ctx = &brw->intel.ctx;
87 uint32_t hw_prim = prim_to_hw_prim[prim->mode];
88
89 DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim->mode));
90
91 /* Slight optimization to avoid the GS program when not needed:
92 */
93 if (prim->mode == GL_QUAD_STRIP &&
94 ctx->Light.ShadeModel != GL_FLAT &&
95 ctx->Polygon.FrontMode == GL_FILL &&
96 ctx->Polygon.BackMode == GL_FILL)
97 hw_prim = _3DPRIM_TRISTRIP;
98
99 if (prim->mode == GL_QUADS && prim->count == 4 &&
100 ctx->Light.ShadeModel != GL_FLAT &&
101 ctx->Polygon.FrontMode == GL_FILL &&
102 ctx->Polygon.BackMode == GL_FILL) {
103 hw_prim = _3DPRIM_TRIFAN;
104 }
105
106 if (hw_prim != brw->primitive) {
107 brw->primitive = hw_prim;
108 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
109
110 if (reduced_prim[prim->mode] != brw->intel.reduced_primitive) {
111 brw->intel.reduced_primitive = reduced_prim[prim->mode];
112 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
113 }
114 }
115 }
116
117 static void gen6_set_prim(struct brw_context *brw,
118 const struct _mesa_prim *prim)
119 {
120 uint32_t hw_prim = prim_to_hw_prim[prim->mode];
121
122 DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim->mode));
123
124 if (hw_prim != brw->primitive) {
125 brw->primitive = hw_prim;
126 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
127 }
128 }
129
130
131 static GLuint trim(GLenum prim, GLuint length)
132 {
133 if (prim == GL_QUAD_STRIP)
134 return length > 3 ? (length - length % 2) : 0;
135 else if (prim == GL_QUADS)
136 return length - length % 4;
137 else
138 return length;
139 }
140
141
142 static void brw_emit_prim(struct brw_context *brw,
143 const struct _mesa_prim *prim,
144 uint32_t hw_prim)
145 {
146 struct intel_context *intel = &brw->intel;
147 int verts_per_instance;
148 int vertex_access_type;
149 int start_vertex_location;
150 int base_vertex_location;
151
152 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
153 prim->start, prim->count);
154
155 start_vertex_location = prim->start;
156 base_vertex_location = prim->basevertex;
157 if (prim->indexed) {
158 vertex_access_type = GEN4_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM;
159 start_vertex_location += brw->ib.start_vertex_offset;
160 base_vertex_location += brw->vb.start_vertex_bias;
161 } else {
162 vertex_access_type = GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
163 start_vertex_location += brw->vb.start_vertex_bias;
164 }
165
166 verts_per_instance = trim(prim->mode, prim->count);
167
168 /* If nothing to emit, just return. */
169 if (verts_per_instance == 0)
170 return;
171
172 /* If we're set to always flush, do it before and after the primitive emit.
173 * We want to catch both missed flushes that hurt instruction/state cache
174 * and missed flushes of the render cache as it heads to other parts of
175 * the besides the draw code.
176 */
177 if (intel->always_flush_cache) {
178 intel_batchbuffer_emit_mi_flush(intel);
179 }
180
181 BEGIN_BATCH(6);
182 OUT_BATCH(CMD_3D_PRIM << 16 | (6 - 2) |
183 hw_prim << GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT |
184 vertex_access_type);
185 OUT_BATCH(verts_per_instance);
186 OUT_BATCH(start_vertex_location);
187 OUT_BATCH(1); // instance count
188 OUT_BATCH(0); // start instance location
189 OUT_BATCH(base_vertex_location);
190 ADVANCE_BATCH();
191
192 intel->batch.need_workaround_flush = true;
193
194 if (intel->always_flush_cache) {
195 intel_batchbuffer_emit_mi_flush(intel);
196 }
197 }
198
199 static void gen7_emit_prim(struct brw_context *brw,
200 const struct _mesa_prim *prim,
201 uint32_t hw_prim)
202 {
203 struct intel_context *intel = &brw->intel;
204 int verts_per_instance;
205 int vertex_access_type;
206 int start_vertex_location;
207 int base_vertex_location;
208
209 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
210 prim->start, prim->count);
211
212 start_vertex_location = prim->start;
213 base_vertex_location = prim->basevertex;
214 if (prim->indexed) {
215 vertex_access_type = GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM;
216 start_vertex_location += brw->ib.start_vertex_offset;
217 base_vertex_location += brw->vb.start_vertex_bias;
218 } else {
219 vertex_access_type = GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
220 start_vertex_location += brw->vb.start_vertex_bias;
221 }
222
223 verts_per_instance = trim(prim->mode, prim->count);
224
225 /* If nothing to emit, just return. */
226 if (verts_per_instance == 0)
227 return;
228
229 /* If we're set to always flush, do it before and after the primitive emit.
230 * We want to catch both missed flushes that hurt instruction/state cache
231 * and missed flushes of the render cache as it heads to other parts of
232 * the besides the draw code.
233 */
234 if (intel->always_flush_cache) {
235 intel_batchbuffer_emit_mi_flush(intel);
236 }
237
238 BEGIN_BATCH(7);
239 OUT_BATCH(CMD_3D_PRIM << 16 | (7 - 2));
240 OUT_BATCH(hw_prim | vertex_access_type);
241 OUT_BATCH(verts_per_instance);
242 OUT_BATCH(start_vertex_location);
243 OUT_BATCH(1); // instance count
244 OUT_BATCH(0); // start instance location
245 OUT_BATCH(base_vertex_location);
246 ADVANCE_BATCH();
247
248 if (intel->always_flush_cache) {
249 intel_batchbuffer_emit_mi_flush(intel);
250 }
251 }
252
253
254 static void brw_merge_inputs( struct brw_context *brw,
255 const struct gl_client_array *arrays[])
256 {
257 struct brw_vertex_info old = brw->vb.info;
258 GLuint i;
259
260 for (i = 0; i < brw->vb.nr_buffers; i++) {
261 drm_intel_bo_unreference(brw->vb.buffers[i].bo);
262 brw->vb.buffers[i].bo = NULL;
263 }
264 brw->vb.nr_buffers = 0;
265
266 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
267
268 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
269 brw->vb.inputs[i].buffer = -1;
270 brw->vb.inputs[i].glarray = arrays[i];
271 brw->vb.inputs[i].attrib = (gl_vert_attrib) i;
272
273 if (arrays[i]->StrideB != 0)
274 brw->vb.info.sizes[i/16] |= (brw->vb.inputs[i].glarray->Size - 1) <<
275 ((i%16) * 2);
276 }
277
278 /* Raise statechanges if input sizes have changed. */
279 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
280 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
281 }
282
283 /* May fail if out of video memory for texture or vbo upload, or on
284 * fallback conditions.
285 */
286 static bool brw_try_draw_prims( struct gl_context *ctx,
287 const struct gl_client_array *arrays[],
288 const struct _mesa_prim *prim,
289 GLuint nr_prims,
290 const struct _mesa_index_buffer *ib,
291 GLuint min_index,
292 GLuint max_index )
293 {
294 struct intel_context *intel = intel_context(ctx);
295 struct brw_context *brw = brw_context(ctx);
296 bool retval = true;
297 GLuint i;
298 bool fail_next = false;
299
300 if (ctx->NewState)
301 _mesa_update_state( ctx );
302
303 /* We have to validate the textures *before* checking for fallbacks;
304 * otherwise, the software fallback won't be able to rely on the
305 * texture state, the firstLevel and lastLevel fields won't be
306 * set in the intel texture object (they'll both be 0), and the
307 * software fallback will segfault if it attempts to access any
308 * texture level other than level 0.
309 */
310 brw_validate_textures( brw );
311
312 /* Bind all inputs, derive varying and size information:
313 */
314 brw_merge_inputs( brw, arrays );
315
316 brw->ib.ib = ib;
317 brw->state.dirty.brw |= BRW_NEW_INDICES;
318
319 brw->vb.min_index = min_index;
320 brw->vb.max_index = max_index;
321 brw->state.dirty.brw |= BRW_NEW_VERTICES;
322
323 /* Have to validate state quite late. Will rebuild tnl_program,
324 * which depends on varying information.
325 *
326 * Note this is where brw->vs->prog_data.inputs_read is calculated,
327 * so can't access it earlier.
328 */
329
330 intel_prepare_render(intel);
331
332 for (i = 0; i < nr_prims; i++) {
333 int estimated_max_prim_size;
334
335 estimated_max_prim_size = 512; /* batchbuffer commands */
336 estimated_max_prim_size += (BRW_MAX_TEX_UNIT *
337 (sizeof(struct brw_sampler_state) +
338 sizeof(struct gen5_sampler_default_color)));
339 estimated_max_prim_size += 1024; /* gen6 VS push constants */
340 estimated_max_prim_size += 1024; /* gen6 WM push constants */
341 estimated_max_prim_size += 512; /* misc. pad */
342
343 /* Flush the batch if it's approaching full, so that we don't wrap while
344 * we've got validated state that needs to be in the same batch as the
345 * primitives.
346 */
347 intel_batchbuffer_require_space(intel, estimated_max_prim_size, false);
348 intel_batchbuffer_save_state(intel);
349
350 if (intel->gen < 6)
351 brw_set_prim(brw, &prim[i]);
352 else
353 gen6_set_prim(brw, &prim[i]);
354
355 retry:
356 /* Note that before the loop, brw->state.dirty.brw was set to != 0, and
357 * that the state updated in the loop outside of this block is that in
358 * *_set_prim or intel_batchbuffer_flush(), which only impacts
359 * brw->state.dirty.brw.
360 */
361 if (brw->state.dirty.brw) {
362 intel->no_batch_wrap = true;
363 brw_upload_state(brw);
364
365 if (unlikely(brw->intel.Fallback)) {
366 intel->no_batch_wrap = false;
367 retval = false;
368 goto out;
369 }
370 }
371
372 if (intel->gen >= 7)
373 gen7_emit_prim(brw, &prim[i], brw->primitive);
374 else
375 brw_emit_prim(brw, &prim[i], brw->primitive);
376
377 intel->no_batch_wrap = false;
378
379 if (dri_bufmgr_check_aperture_space(&intel->batch.bo, 1)) {
380 if (!fail_next) {
381 intel_batchbuffer_reset_to_saved(intel);
382 intel_batchbuffer_flush(intel);
383 fail_next = true;
384 goto retry;
385 } else {
386 if (intel_batchbuffer_flush(intel) == -ENOSPC) {
387 static bool warned = false;
388
389 if (!warned) {
390 fprintf(stderr, "i965: Single primitive emit exceeded"
391 "available aperture space\n");
392 warned = true;
393 }
394
395 retval = false;
396 }
397 }
398 }
399 }
400
401 if (intel->always_flush_batch)
402 intel_batchbuffer_flush(intel);
403 out:
404
405 brw_state_cache_check_size(brw);
406
407 return retval;
408 }
409
410 void brw_draw_prims( struct gl_context *ctx,
411 const struct gl_client_array *arrays[],
412 const struct _mesa_prim *prim,
413 GLuint nr_prims,
414 const struct _mesa_index_buffer *ib,
415 GLboolean index_bounds_valid,
416 GLuint min_index,
417 GLuint max_index )
418 {
419 bool retval;
420
421 if (!_mesa_check_conditional_render(ctx))
422 return;
423
424 if (!vbo_all_varyings_in_vbos(arrays)) {
425 if (!index_bounds_valid)
426 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
427
428 /* Decide if we want to rebase. If so we end up recursing once
429 * only into this function.
430 */
431 if (min_index != 0 && !vbo_any_varyings_in_vbos(arrays)) {
432 vbo_rebase_prims(ctx, arrays,
433 prim, nr_prims,
434 ib, min_index, max_index,
435 brw_draw_prims );
436 return;
437 }
438 }
439
440 /* Make a first attempt at drawing:
441 */
442 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
443
444 /* Otherwise, we really are out of memory. Pass the drawing
445 * command to the software tnl module and which will in turn call
446 * swrast to do the drawing.
447 */
448 if (!retval) {
449 _swsetup_Wakeup(ctx);
450 _tnl_wakeup(ctx);
451 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
452 }
453
454 }
455
456 void brw_draw_init( struct brw_context *brw )
457 {
458 struct gl_context *ctx = &brw->intel.ctx;
459 struct vbo_context *vbo = vbo_context(ctx);
460 int i;
461
462 /* Register our drawing function:
463 */
464 vbo->draw_prims = brw_draw_prims;
465
466 for (i = 0; i < VERT_ATTRIB_MAX; i++)
467 brw->vb.inputs[i].buffer = -1;
468 brw->vb.nr_buffers = 0;
469 brw->vb.nr_enabled = 0;
470 }
471
472 void brw_draw_destroy( struct brw_context *brw )
473 {
474 int i;
475
476 for (i = 0; i < brw->vb.nr_buffers; i++) {
477 drm_intel_bo_unreference(brw->vb.buffers[i].bo);
478 brw->vb.buffers[i].bo = NULL;
479 }
480 brw->vb.nr_buffers = 0;
481
482 for (i = 0; i < brw->vb.nr_enabled; i++) {
483 brw->vb.enabled[i]->buffer = -1;
484 }
485 brw->vb.nr_enabled = 0;
486
487 drm_intel_bo_unreference(brw->ib.bo);
488 brw->ib.bo = NULL;
489 }