i965: Drop index buffer re-alignment code.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_draw.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <sys/errno.h>
27
28 #include "main/context.h"
29 #include "main/condrender.h"
30 #include "main/samplerobj.h"
31 #include "main/state.h"
32 #include "main/enums.h"
33 #include "main/macros.h"
34 #include "main/transformfeedback.h"
35 #include "main/framebuffer.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 #include "drivers/common/meta.h"
41 #include "util/bitscan.h"
42
43 #include "brw_blorp.h"
44 #include "brw_draw.h"
45 #include "brw_defines.h"
46 #include "compiler/brw_eu_defines.h"
47 #include "brw_context.h"
48 #include "brw_state.h"
49
50 #include "intel_batchbuffer.h"
51 #include "intel_buffers.h"
52 #include "intel_fbo.h"
53 #include "intel_mipmap_tree.h"
54 #include "intel_buffer_objects.h"
55
56 #define FILE_DEBUG_FLAG DEBUG_PRIMS
57
58
59 static const GLenum reduced_prim[GL_POLYGON+1] = {
60 [GL_POINTS] = GL_POINTS,
61 [GL_LINES] = GL_LINES,
62 [GL_LINE_LOOP] = GL_LINES,
63 [GL_LINE_STRIP] = GL_LINES,
64 [GL_TRIANGLES] = GL_TRIANGLES,
65 [GL_TRIANGLE_STRIP] = GL_TRIANGLES,
66 [GL_TRIANGLE_FAN] = GL_TRIANGLES,
67 [GL_QUADS] = GL_TRIANGLES,
68 [GL_QUAD_STRIP] = GL_TRIANGLES,
69 [GL_POLYGON] = GL_TRIANGLES
70 };
71
72 /* When the primitive changes, set a state bit and re-validate. Not
73 * the nicest and would rather deal with this by having all the
74 * programs be immune to the active primitive (ie. cope with all
75 * possibilities). That may not be realistic however.
76 */
77 static void
78 brw_set_prim(struct brw_context *brw, const struct _mesa_prim *prim)
79 {
80 struct gl_context *ctx = &brw->ctx;
81 uint32_t hw_prim = get_hw_prim_for_gl_prim(prim->mode);
82
83 DBG("PRIM: %s\n", _mesa_enum_to_string(prim->mode));
84
85 /* Slight optimization to avoid the GS program when not needed:
86 */
87 if (prim->mode == GL_QUAD_STRIP &&
88 ctx->Light.ShadeModel != GL_FLAT &&
89 ctx->Polygon.FrontMode == GL_FILL &&
90 ctx->Polygon.BackMode == GL_FILL)
91 hw_prim = _3DPRIM_TRISTRIP;
92
93 if (prim->mode == GL_QUADS && prim->count == 4 &&
94 ctx->Light.ShadeModel != GL_FLAT &&
95 ctx->Polygon.FrontMode == GL_FILL &&
96 ctx->Polygon.BackMode == GL_FILL) {
97 hw_prim = _3DPRIM_TRIFAN;
98 }
99
100 if (hw_prim != brw->primitive) {
101 brw->primitive = hw_prim;
102 brw->ctx.NewDriverState |= BRW_NEW_PRIMITIVE;
103
104 if (reduced_prim[prim->mode] != brw->reduced_primitive) {
105 brw->reduced_primitive = reduced_prim[prim->mode];
106 brw->ctx.NewDriverState |= BRW_NEW_REDUCED_PRIMITIVE;
107 }
108 }
109 }
110
111 static void
112 gen6_set_prim(struct brw_context *brw, const struct _mesa_prim *prim)
113 {
114 const struct gl_context *ctx = &brw->ctx;
115 uint32_t hw_prim;
116
117 DBG("PRIM: %s\n", _mesa_enum_to_string(prim->mode));
118
119 if (prim->mode == GL_PATCHES) {
120 hw_prim = _3DPRIM_PATCHLIST(ctx->TessCtrlProgram.patch_vertices);
121 } else {
122 hw_prim = get_hw_prim_for_gl_prim(prim->mode);
123 }
124
125 if (hw_prim != brw->primitive) {
126 brw->primitive = hw_prim;
127 brw->ctx.NewDriverState |= BRW_NEW_PRIMITIVE;
128 if (prim->mode == GL_PATCHES)
129 brw->ctx.NewDriverState |= BRW_NEW_PATCH_PRIMITIVE;
130 }
131 }
132
133
134 /**
135 * The hardware is capable of removing dangling vertices on its own; however,
136 * prior to Gen6, we sometimes convert quads into trifans (and quad strips
137 * into tristrips), since pre-Gen6 hardware requires a GS to render quads.
138 * This function manually trims dangling vertices from a draw call involving
139 * quads so that those dangling vertices won't get drawn when we convert to
140 * trifans/tristrips.
141 */
142 static GLuint
143 trim(GLenum prim, GLuint length)
144 {
145 if (prim == GL_QUAD_STRIP)
146 return length > 3 ? (length - length % 2) : 0;
147 else if (prim == GL_QUADS)
148 return length - length % 4;
149 else
150 return length;
151 }
152
153
154 static void
155 brw_emit_prim(struct brw_context *brw,
156 const struct _mesa_prim *prim,
157 uint32_t hw_prim,
158 struct brw_transform_feedback_object *xfb_obj,
159 unsigned stream)
160 {
161 int verts_per_instance;
162 int vertex_access_type;
163 int indirect_flag;
164
165 DBG("PRIM: %s %d %d\n", _mesa_enum_to_string(prim->mode),
166 prim->start, prim->count);
167
168 int start_vertex_location = prim->start;
169 int base_vertex_location = prim->basevertex;
170
171 if (prim->indexed) {
172 vertex_access_type = brw->gen >= 7 ?
173 GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM :
174 GEN4_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM;
175 start_vertex_location += brw->ib.start_vertex_offset;
176 base_vertex_location += brw->vb.start_vertex_bias;
177 } else {
178 vertex_access_type = brw->gen >= 7 ?
179 GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL :
180 GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
181 start_vertex_location += brw->vb.start_vertex_bias;
182 }
183
184 /* We only need to trim the primitive count on pre-Gen6. */
185 if (brw->gen < 6)
186 verts_per_instance = trim(prim->mode, prim->count);
187 else
188 verts_per_instance = prim->count;
189
190 /* If nothing to emit, just return. */
191 if (verts_per_instance == 0 && !prim->is_indirect && !xfb_obj)
192 return;
193
194 /* If we're set to always flush, do it before and after the primitive emit.
195 * We want to catch both missed flushes that hurt instruction/state cache
196 * and missed flushes of the render cache as it heads to other parts of
197 * the besides the draw code.
198 */
199 if (brw->always_flush_cache)
200 brw_emit_mi_flush(brw);
201
202 /* If indirect, emit a bunch of loads from the indirect BO. */
203 if (xfb_obj) {
204 indirect_flag = GEN7_3DPRIM_INDIRECT_PARAMETER_ENABLE;
205
206 brw_load_register_mem(brw, GEN7_3DPRIM_VERTEX_COUNT,
207 xfb_obj->prim_count_bo,
208 I915_GEM_DOMAIN_VERTEX, 0,
209 stream * sizeof(uint32_t));
210 BEGIN_BATCH(9);
211 OUT_BATCH(MI_LOAD_REGISTER_IMM | (9 - 2));
212 OUT_BATCH(GEN7_3DPRIM_INSTANCE_COUNT);
213 OUT_BATCH(prim->num_instances);
214 OUT_BATCH(GEN7_3DPRIM_START_VERTEX);
215 OUT_BATCH(0);
216 OUT_BATCH(GEN7_3DPRIM_BASE_VERTEX);
217 OUT_BATCH(0);
218 OUT_BATCH(GEN7_3DPRIM_START_INSTANCE);
219 OUT_BATCH(0);
220 ADVANCE_BATCH();
221 } else if (prim->is_indirect) {
222 struct gl_buffer_object *indirect_buffer = brw->ctx.DrawIndirectBuffer;
223 struct brw_bo *bo = intel_bufferobj_buffer(brw,
224 intel_buffer_object(indirect_buffer),
225 prim->indirect_offset, 5 * sizeof(GLuint));
226
227 indirect_flag = GEN7_3DPRIM_INDIRECT_PARAMETER_ENABLE;
228
229 brw_load_register_mem(brw, GEN7_3DPRIM_VERTEX_COUNT, bo,
230 I915_GEM_DOMAIN_VERTEX, 0,
231 prim->indirect_offset + 0);
232 brw_load_register_mem(brw, GEN7_3DPRIM_INSTANCE_COUNT, bo,
233 I915_GEM_DOMAIN_VERTEX, 0,
234 prim->indirect_offset + 4);
235
236 brw_load_register_mem(brw, GEN7_3DPRIM_START_VERTEX, bo,
237 I915_GEM_DOMAIN_VERTEX, 0,
238 prim->indirect_offset + 8);
239 if (prim->indexed) {
240 brw_load_register_mem(brw, GEN7_3DPRIM_BASE_VERTEX, bo,
241 I915_GEM_DOMAIN_VERTEX, 0,
242 prim->indirect_offset + 12);
243 brw_load_register_mem(brw, GEN7_3DPRIM_START_INSTANCE, bo,
244 I915_GEM_DOMAIN_VERTEX, 0,
245 prim->indirect_offset + 16);
246 } else {
247 brw_load_register_mem(brw, GEN7_3DPRIM_START_INSTANCE, bo,
248 I915_GEM_DOMAIN_VERTEX, 0,
249 prim->indirect_offset + 12);
250 BEGIN_BATCH(3);
251 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
252 OUT_BATCH(GEN7_3DPRIM_BASE_VERTEX);
253 OUT_BATCH(0);
254 ADVANCE_BATCH();
255 }
256 } else {
257 indirect_flag = 0;
258 }
259
260 BEGIN_BATCH(brw->gen >= 7 ? 7 : 6);
261
262 if (brw->gen >= 7) {
263 const int predicate_enable =
264 (brw->predicate.state == BRW_PREDICATE_STATE_USE_BIT)
265 ? GEN7_3DPRIM_PREDICATE_ENABLE : 0;
266
267 OUT_BATCH(CMD_3D_PRIM << 16 | (7 - 2) | indirect_flag | predicate_enable);
268 OUT_BATCH(hw_prim | vertex_access_type);
269 } else {
270 OUT_BATCH(CMD_3D_PRIM << 16 | (6 - 2) |
271 hw_prim << GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT |
272 vertex_access_type);
273 }
274 OUT_BATCH(verts_per_instance);
275 OUT_BATCH(start_vertex_location);
276 OUT_BATCH(prim->num_instances);
277 OUT_BATCH(prim->base_instance);
278 OUT_BATCH(base_vertex_location);
279 ADVANCE_BATCH();
280
281 if (brw->always_flush_cache)
282 brw_emit_mi_flush(brw);
283 }
284
285
286 static void
287 brw_merge_inputs(struct brw_context *brw,
288 const struct gl_vertex_array *arrays[])
289 {
290 const struct gl_context *ctx = &brw->ctx;
291 GLuint i;
292
293 for (i = 0; i < brw->vb.nr_buffers; i++) {
294 brw_bo_unreference(brw->vb.buffers[i].bo);
295 brw->vb.buffers[i].bo = NULL;
296 }
297 brw->vb.nr_buffers = 0;
298
299 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
300 brw->vb.inputs[i].buffer = -1;
301 brw->vb.inputs[i].glarray = arrays[i];
302 }
303
304 if (brw->gen < 8 && !brw->is_haswell) {
305 uint64_t mask = ctx->VertexProgram._Current->info.inputs_read;
306 /* Prior to Haswell, the hardware can't natively support GL_FIXED or
307 * 2_10_10_10_REV vertex formats. Set appropriate workaround flags.
308 */
309 while (mask) {
310 uint8_t wa_flags = 0;
311
312 i = u_bit_scan64(&mask);
313
314 switch (brw->vb.inputs[i].glarray->Type) {
315
316 case GL_FIXED:
317 wa_flags = brw->vb.inputs[i].glarray->Size;
318 break;
319
320 case GL_INT_2_10_10_10_REV:
321 wa_flags |= BRW_ATTRIB_WA_SIGN;
322 /* fallthough */
323
324 case GL_UNSIGNED_INT_2_10_10_10_REV:
325 if (brw->vb.inputs[i].glarray->Format == GL_BGRA)
326 wa_flags |= BRW_ATTRIB_WA_BGRA;
327
328 if (brw->vb.inputs[i].glarray->Normalized)
329 wa_flags |= BRW_ATTRIB_WA_NORMALIZE;
330 else if (!brw->vb.inputs[i].glarray->Integer)
331 wa_flags |= BRW_ATTRIB_WA_SCALE;
332
333 break;
334 }
335
336 if (brw->vb.attrib_wa_flags[i] != wa_flags) {
337 brw->vb.attrib_wa_flags[i] = wa_flags;
338 brw->ctx.NewDriverState |= BRW_NEW_VS_ATTRIB_WORKAROUNDS;
339 }
340 }
341 }
342 }
343
344 /**
345 * \brief Call this after drawing to mark which buffers need resolving
346 *
347 * If the depth buffer was written to and if it has an accompanying HiZ
348 * buffer, then mark that it needs a depth resolve.
349 *
350 * If the color buffer is a multisample window system buffer, then
351 * mark that it needs a downsample.
352 *
353 * Also mark any render targets which will be textured as needing a render
354 * cache flush.
355 */
356 static void
357 brw_postdraw_set_buffers_need_resolve(struct brw_context *brw)
358 {
359 struct gl_context *ctx = &brw->ctx;
360 struct gl_framebuffer *fb = ctx->DrawBuffer;
361
362 struct intel_renderbuffer *front_irb = NULL;
363 struct intel_renderbuffer *back_irb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
364 struct intel_renderbuffer *depth_irb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
365 struct intel_renderbuffer *stencil_irb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
366 struct gl_renderbuffer_attachment *depth_att = &fb->Attachment[BUFFER_DEPTH];
367
368 if (_mesa_is_front_buffer_drawing(fb))
369 front_irb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
370
371 if (front_irb)
372 front_irb->need_downsample = true;
373 if (back_irb)
374 back_irb->need_downsample = true;
375 if (depth_irb) {
376 bool depth_written = brw_depth_writes_enabled(brw);
377 if (depth_att->Layered) {
378 intel_miptree_finish_depth(brw, depth_irb->mt,
379 depth_irb->mt_level,
380 depth_irb->mt_layer,
381 depth_irb->layer_count,
382 depth_written);
383 } else {
384 intel_miptree_finish_depth(brw, depth_irb->mt,
385 depth_irb->mt_level,
386 depth_irb->mt_layer, 1,
387 depth_written);
388 }
389 if (depth_written)
390 brw_render_cache_set_add_bo(brw, depth_irb->mt->bo);
391 }
392
393 if (ctx->Extensions.ARB_stencil_texturing &&
394 stencil_irb && brw->stencil_write_enabled) {
395 brw_render_cache_set_add_bo(brw, stencil_irb->mt->bo);
396 }
397
398 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
399 struct intel_renderbuffer *irb =
400 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
401
402 if (!irb)
403 continue;
404
405 brw_render_cache_set_add_bo(brw, irb->mt->bo);
406 intel_miptree_finish_render(brw, irb->mt, irb->mt_level,
407 irb->mt_layer, irb->layer_count);
408 }
409 }
410
411 static void
412 intel_renderbuffer_move_temp_back(struct brw_context *brw,
413 struct intel_renderbuffer *irb)
414 {
415 if (irb->align_wa_mt == NULL)
416 return;
417
418 brw_render_cache_set_check_flush(brw, irb->align_wa_mt->bo);
419
420 intel_miptree_copy_slice(brw, irb->align_wa_mt, 0, 0,
421 irb->mt,
422 irb->Base.Base.TexImage->Level, irb->mt_layer);
423
424 intel_miptree_reference(&irb->align_wa_mt, NULL);
425
426 /* Finally restore the x,y to correspond to full miptree. */
427 intel_renderbuffer_set_draw_offset(irb);
428
429 /* Make sure render surface state gets re-emitted with updated miptree. */
430 brw->NewGLState |= _NEW_BUFFERS;
431 }
432
433 static void
434 brw_postdraw_reconcile_align_wa_slices(struct brw_context *brw)
435 {
436 struct gl_context *ctx = &brw->ctx;
437 struct gl_framebuffer *fb = ctx->DrawBuffer;
438
439 struct intel_renderbuffer *depth_irb =
440 intel_get_renderbuffer(fb, BUFFER_DEPTH);
441 struct intel_renderbuffer *stencil_irb =
442 intel_get_renderbuffer(fb, BUFFER_STENCIL);
443
444 if (depth_irb && depth_irb->align_wa_mt)
445 intel_renderbuffer_move_temp_back(brw, depth_irb);
446
447 if (stencil_irb && stencil_irb->align_wa_mt)
448 intel_renderbuffer_move_temp_back(brw, stencil_irb);
449
450 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
451 struct intel_renderbuffer *irb =
452 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
453
454 if (!irb || irb->align_wa_mt == NULL)
455 continue;
456
457 intel_renderbuffer_move_temp_back(brw, irb);
458 }
459 }
460
461 /* May fail if out of video memory for texture or vbo upload, or on
462 * fallback conditions.
463 */
464 static void
465 brw_try_draw_prims(struct gl_context *ctx,
466 const struct gl_vertex_array *arrays[],
467 const struct _mesa_prim *prims,
468 GLuint nr_prims,
469 const struct _mesa_index_buffer *ib,
470 bool index_bounds_valid,
471 GLuint min_index,
472 GLuint max_index,
473 struct brw_transform_feedback_object *xfb_obj,
474 unsigned stream,
475 struct gl_buffer_object *indirect)
476 {
477 struct brw_context *brw = brw_context(ctx);
478 GLuint i;
479 bool fail_next = false;
480
481 if (ctx->NewState)
482 _mesa_update_state(ctx);
483
484 /* We have to validate the textures *before* checking for fallbacks;
485 * otherwise, the software fallback won't be able to rely on the
486 * texture state, the firstLevel and lastLevel fields won't be
487 * set in the intel texture object (they'll both be 0), and the
488 * software fallback will segfault if it attempts to access any
489 * texture level other than level 0.
490 */
491 brw_validate_textures(brw);
492
493 /* Find the highest sampler unit used by each shader program. A bit-count
494 * won't work since ARB programs use the texture unit number as the sampler
495 * index.
496 */
497 brw->wm.base.sampler_count =
498 util_last_bit(ctx->FragmentProgram._Current->SamplersUsed);
499 brw->gs.base.sampler_count = ctx->GeometryProgram._Current ?
500 util_last_bit(ctx->GeometryProgram._Current->SamplersUsed) : 0;
501 brw->tes.base.sampler_count = ctx->TessEvalProgram._Current ?
502 util_last_bit(ctx->TessEvalProgram._Current->SamplersUsed) : 0;
503 brw->tcs.base.sampler_count = ctx->TessCtrlProgram._Current ?
504 util_last_bit(ctx->TessCtrlProgram._Current->SamplersUsed) : 0;
505 brw->vs.base.sampler_count =
506 util_last_bit(ctx->VertexProgram._Current->SamplersUsed);
507
508 intel_prepare_render(brw);
509
510 /* This workaround has to happen outside of brw_upload_render_state()
511 * because it may flush the batchbuffer for a blit, affecting the state
512 * flags.
513 */
514 brw_workaround_depthstencil_alignment(brw, 0);
515
516 /* Bind all inputs, derive varying and size information:
517 */
518 brw_merge_inputs(brw, arrays);
519
520 brw->ib.ib = ib;
521 brw->ctx.NewDriverState |= BRW_NEW_INDICES;
522
523 brw->vb.index_bounds_valid = index_bounds_valid;
524 brw->vb.min_index = min_index;
525 brw->vb.max_index = max_index;
526 brw->ctx.NewDriverState |= BRW_NEW_VERTICES;
527
528 for (i = 0; i < nr_prims; i++) {
529 int estimated_max_prim_size;
530 const int sampler_state_size = 16;
531
532 estimated_max_prim_size = 512; /* batchbuffer commands */
533 estimated_max_prim_size += BRW_MAX_TEX_UNIT *
534 (sampler_state_size + sizeof(struct gen5_sampler_default_color));
535 estimated_max_prim_size += 1024; /* gen6 VS push constants */
536 estimated_max_prim_size += 1024; /* gen6 WM push constants */
537 estimated_max_prim_size += 512; /* misc. pad */
538
539 /* Flush the batch if it's approaching full, so that we don't wrap while
540 * we've got validated state that needs to be in the same batch as the
541 * primitives.
542 */
543 intel_batchbuffer_require_space(brw, estimated_max_prim_size, RENDER_RING);
544 intel_batchbuffer_save_state(brw);
545
546 if (brw->num_instances != prims[i].num_instances ||
547 brw->basevertex != prims[i].basevertex ||
548 brw->baseinstance != prims[i].base_instance) {
549 brw->num_instances = prims[i].num_instances;
550 brw->basevertex = prims[i].basevertex;
551 brw->baseinstance = prims[i].base_instance;
552 if (i > 0) { /* For i == 0 we just did this before the loop */
553 brw->ctx.NewDriverState |= BRW_NEW_VERTICES;
554 brw_merge_inputs(brw, arrays);
555 }
556 }
557
558 /* Determine if we need to flag BRW_NEW_VERTICES for updating the
559 * gl_BaseVertexARB or gl_BaseInstanceARB values. For indirect draw, we
560 * always flag if the shader uses one of the values. For direct draws,
561 * we only flag if the values change.
562 */
563 const int new_basevertex =
564 prims[i].indexed ? prims[i].basevertex : prims[i].start;
565 const int new_baseinstance = prims[i].base_instance;
566 const struct brw_vs_prog_data *vs_prog_data =
567 brw_vs_prog_data(brw->vs.base.prog_data);
568 if (i > 0) {
569 const bool uses_draw_parameters =
570 vs_prog_data->uses_basevertex ||
571 vs_prog_data->uses_baseinstance;
572
573 if ((uses_draw_parameters && prims[i].is_indirect) ||
574 (vs_prog_data->uses_basevertex &&
575 brw->draw.params.gl_basevertex != new_basevertex) ||
576 (vs_prog_data->uses_baseinstance &&
577 brw->draw.params.gl_baseinstance != new_baseinstance))
578 brw->ctx.NewDriverState |= BRW_NEW_VERTICES;
579 }
580
581 brw->draw.params.gl_basevertex = new_basevertex;
582 brw->draw.params.gl_baseinstance = new_baseinstance;
583 brw_bo_unreference(brw->draw.draw_params_bo);
584
585 if (prims[i].is_indirect) {
586 /* Point draw_params_bo at the indirect buffer. */
587 brw->draw.draw_params_bo =
588 intel_buffer_object(ctx->DrawIndirectBuffer)->buffer;
589 brw_bo_reference(brw->draw.draw_params_bo);
590 brw->draw.draw_params_offset =
591 prims[i].indirect_offset + (prims[i].indexed ? 12 : 8);
592 } else {
593 /* Set draw_params_bo to NULL so brw_prepare_vertices knows it
594 * has to upload gl_BaseVertex and such if they're needed.
595 */
596 brw->draw.draw_params_bo = NULL;
597 brw->draw.draw_params_offset = 0;
598 }
599
600 /* gl_DrawID always needs its own vertex buffer since it's not part of
601 * the indirect parameter buffer. If the program uses gl_DrawID we need
602 * to flag BRW_NEW_VERTICES. For the first iteration, we don't have
603 * valid vs_prog_data, but we always flag BRW_NEW_VERTICES before
604 * the loop.
605 */
606 brw->draw.gl_drawid = prims[i].draw_id;
607 brw_bo_unreference(brw->draw.draw_id_bo);
608 brw->draw.draw_id_bo = NULL;
609 if (i > 0 && vs_prog_data->uses_drawid)
610 brw->ctx.NewDriverState |= BRW_NEW_VERTICES;
611
612 if (brw->gen < 6)
613 brw_set_prim(brw, &prims[i]);
614 else
615 gen6_set_prim(brw, &prims[i]);
616
617 retry:
618
619 /* Note that before the loop, brw->ctx.NewDriverState was set to != 0, and
620 * that the state updated in the loop outside of this block is that in
621 * *_set_prim or intel_batchbuffer_flush(), which only impacts
622 * brw->ctx.NewDriverState.
623 */
624 if (brw->ctx.NewDriverState) {
625 brw->no_batch_wrap = true;
626 brw_upload_render_state(brw);
627 }
628
629 brw_emit_prim(brw, &prims[i], brw->primitive, xfb_obj, stream);
630
631 brw->no_batch_wrap = false;
632
633 if (!brw_batch_has_aperture_space(brw, 0)) {
634 if (!fail_next) {
635 intel_batchbuffer_reset_to_saved(brw);
636 intel_batchbuffer_flush(brw);
637 fail_next = true;
638 goto retry;
639 } else {
640 int ret = intel_batchbuffer_flush(brw);
641 WARN_ONCE(ret == -ENOSPC,
642 "i965: Single primitive emit exceeded "
643 "available aperture space\n");
644 }
645 }
646
647 /* Now that we know we haven't run out of aperture space, we can safely
648 * reset the dirty bits.
649 */
650 if (brw->ctx.NewDriverState)
651 brw_render_state_finished(brw);
652 }
653
654 if (brw->always_flush_batch)
655 intel_batchbuffer_flush(brw);
656
657 brw_program_cache_check_size(brw);
658 brw_postdraw_reconcile_align_wa_slices(brw);
659 brw_postdraw_set_buffers_need_resolve(brw);
660
661 return;
662 }
663
664 void
665 brw_draw_prims(struct gl_context *ctx,
666 const struct _mesa_prim *prims,
667 GLuint nr_prims,
668 const struct _mesa_index_buffer *ib,
669 GLboolean index_bounds_valid,
670 GLuint min_index,
671 GLuint max_index,
672 struct gl_transform_feedback_object *gl_xfb_obj,
673 unsigned stream,
674 struct gl_buffer_object *indirect)
675 {
676 struct brw_context *brw = brw_context(ctx);
677 const struct gl_vertex_array **arrays = ctx->Array._DrawArrays;
678 struct brw_transform_feedback_object *xfb_obj =
679 (struct brw_transform_feedback_object *) gl_xfb_obj;
680
681 if (!brw_check_conditional_render(brw))
682 return;
683
684 /* Handle primitive restart if needed */
685 if (brw_handle_primitive_restart(ctx, prims, nr_prims, ib, indirect)) {
686 /* The draw was handled, so we can exit now */
687 return;
688 }
689
690 /* Do GL_SELECT and GL_FEEDBACK rendering using swrast, even though it
691 * won't support all the extensions we support.
692 */
693 if (ctx->RenderMode != GL_RENDER) {
694 perf_debug("%s render mode not supported in hardware\n",
695 _mesa_enum_to_string(ctx->RenderMode));
696 _swsetup_Wakeup(ctx);
697 _tnl_wakeup(ctx);
698 _tnl_draw_prims(ctx, prims, nr_prims, ib,
699 index_bounds_valid, min_index, max_index, NULL, 0, NULL);
700 return;
701 }
702
703 /* If we're going to have to upload any of the user's vertex arrays, then
704 * get the minimum and maximum of their index buffer so we know what range
705 * to upload.
706 */
707 if (!index_bounds_valid && !vbo_all_varyings_in_vbos(arrays)) {
708 perf_debug("Scanning index buffer to compute index buffer bounds. "
709 "Use glDrawRangeElements() to avoid this.\n");
710 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
711 index_bounds_valid = true;
712 }
713
714 /* Try drawing with the hardware, but don't do anything else if we can't
715 * manage it. swrast doesn't support our featureset, so we can't fall back
716 * to it.
717 */
718 brw_try_draw_prims(ctx, arrays, prims, nr_prims, ib, index_bounds_valid,
719 min_index, max_index, xfb_obj, stream, indirect);
720 }
721
722 void
723 brw_draw_init(struct brw_context *brw)
724 {
725 struct gl_context *ctx = &brw->ctx;
726 struct vbo_context *vbo = vbo_context(ctx);
727
728 /* Register our drawing function:
729 */
730 vbo->draw_prims = brw_draw_prims;
731
732 for (int i = 0; i < VERT_ATTRIB_MAX; i++)
733 brw->vb.inputs[i].buffer = -1;
734 brw->vb.nr_buffers = 0;
735 brw->vb.nr_enabled = 0;
736 }
737
738 void
739 brw_draw_destroy(struct brw_context *brw)
740 {
741 unsigned i;
742
743 for (i = 0; i < brw->vb.nr_buffers; i++) {
744 brw_bo_unreference(brw->vb.buffers[i].bo);
745 brw->vb.buffers[i].bo = NULL;
746 }
747 brw->vb.nr_buffers = 0;
748
749 for (i = 0; i < brw->vb.nr_enabled; i++) {
750 brw->vb.enabled[i]->buffer = -1;
751 }
752 brw->vb.nr_enabled = 0;
753
754 brw_bo_unreference(brw->ib.bo);
755 brw->ib.bo = NULL;
756 }