mesa: remove redundant _mesa_prim::is_indexed
[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/arrayobj.h"
29 #include "main/blend.h"
30 #include "main/context.h"
31 #include "main/condrender.h"
32 #include "main/samplerobj.h"
33 #include "main/state.h"
34 #include "main/enums.h"
35 #include "main/macros.h"
36 #include "main/transformfeedback.h"
37 #include "main/framebuffer.h"
38 #include "main/varray.h"
39 #include "tnl/tnl.h"
40 #include "vbo/vbo.h"
41 #include "swrast/swrast.h"
42 #include "swrast_setup/swrast_setup.h"
43 #include "drivers/common/meta.h"
44 #include "util/bitscan.h"
45 #include "util/bitset.h"
46
47 #include "brw_blorp.h"
48 #include "brw_draw.h"
49 #include "brw_defines.h"
50 #include "compiler/brw_eu_defines.h"
51 #include "brw_context.h"
52 #include "brw_state.h"
53
54 #include "intel_batchbuffer.h"
55 #include "intel_buffers.h"
56 #include "intel_fbo.h"
57 #include "intel_mipmap_tree.h"
58 #include "intel_buffer_objects.h"
59
60 #define FILE_DEBUG_FLAG DEBUG_PRIMS
61
62
63 static const GLenum reduced_prim[GL_POLYGON+1] = {
64 [GL_POINTS] = GL_POINTS,
65 [GL_LINES] = GL_LINES,
66 [GL_LINE_LOOP] = GL_LINES,
67 [GL_LINE_STRIP] = GL_LINES,
68 [GL_TRIANGLES] = GL_TRIANGLES,
69 [GL_TRIANGLE_STRIP] = GL_TRIANGLES,
70 [GL_TRIANGLE_FAN] = GL_TRIANGLES,
71 [GL_QUADS] = GL_TRIANGLES,
72 [GL_QUAD_STRIP] = GL_TRIANGLES,
73 [GL_POLYGON] = GL_TRIANGLES
74 };
75
76 /* When the primitive changes, set a state bit and re-validate. Not
77 * the nicest and would rather deal with this by having all the
78 * programs be immune to the active primitive (ie. cope with all
79 * possibilities). That may not be realistic however.
80 */
81 static void
82 brw_set_prim(struct brw_context *brw, const struct _mesa_prim *prim)
83 {
84 struct gl_context *ctx = &brw->ctx;
85 uint32_t hw_prim = get_hw_prim_for_gl_prim(prim->mode);
86
87 DBG("PRIM: %s\n", _mesa_enum_to_string(prim->mode));
88
89 /* Slight optimization to avoid the GS program when not needed:
90 */
91 if (prim->mode == GL_QUAD_STRIP &&
92 ctx->Light.ShadeModel != GL_FLAT &&
93 ctx->Polygon.FrontMode == GL_FILL &&
94 ctx->Polygon.BackMode == GL_FILL)
95 hw_prim = _3DPRIM_TRISTRIP;
96
97 if (prim->mode == GL_QUADS && prim->count == 4 &&
98 ctx->Light.ShadeModel != GL_FLAT &&
99 ctx->Polygon.FrontMode == GL_FILL &&
100 ctx->Polygon.BackMode == GL_FILL) {
101 hw_prim = _3DPRIM_TRIFAN;
102 }
103
104 if (hw_prim != brw->primitive) {
105 brw->primitive = hw_prim;
106 brw->ctx.NewDriverState |= BRW_NEW_PRIMITIVE;
107
108 if (reduced_prim[prim->mode] != brw->reduced_primitive) {
109 brw->reduced_primitive = reduced_prim[prim->mode];
110 brw->ctx.NewDriverState |= BRW_NEW_REDUCED_PRIMITIVE;
111 }
112 }
113 }
114
115 static void
116 gen6_set_prim(struct brw_context *brw, const struct _mesa_prim *prim)
117 {
118 const struct gl_context *ctx = &brw->ctx;
119 uint32_t hw_prim;
120
121 DBG("PRIM: %s\n", _mesa_enum_to_string(prim->mode));
122
123 if (prim->mode == GL_PATCHES) {
124 hw_prim = _3DPRIM_PATCHLIST(ctx->TessCtrlProgram.patch_vertices);
125 } else {
126 hw_prim = get_hw_prim_for_gl_prim(prim->mode);
127 }
128
129 if (hw_prim != brw->primitive) {
130 brw->primitive = hw_prim;
131 brw->ctx.NewDriverState |= BRW_NEW_PRIMITIVE;
132 if (prim->mode == GL_PATCHES)
133 brw->ctx.NewDriverState |= BRW_NEW_PATCH_PRIMITIVE;
134 }
135 }
136
137
138 /**
139 * The hardware is capable of removing dangling vertices on its own; however,
140 * prior to Gen6, we sometimes convert quads into trifans (and quad strips
141 * into tristrips), since pre-Gen6 hardware requires a GS to render quads.
142 * This function manually trims dangling vertices from a draw call involving
143 * quads so that those dangling vertices won't get drawn when we convert to
144 * trifans/tristrips.
145 */
146 static GLuint
147 trim(GLenum prim, GLuint length)
148 {
149 if (prim == GL_QUAD_STRIP)
150 return length > 3 ? (length - length % 2) : 0;
151 else if (prim == GL_QUADS)
152 return length - length % 4;
153 else
154 return length;
155 }
156
157
158 static void
159 brw_emit_prim(struct brw_context *brw,
160 const struct _mesa_prim *prim,
161 uint32_t hw_prim,
162 bool is_indexed,
163 struct brw_transform_feedback_object *xfb_obj,
164 unsigned stream,
165 bool is_indirect,
166 GLsizeiptr indirect_offset)
167 {
168 const struct gen_device_info *devinfo = &brw->screen->devinfo;
169 int verts_per_instance;
170 int vertex_access_type;
171 int indirect_flag;
172
173 DBG("PRIM: %s %d %d\n", _mesa_enum_to_string(prim->mode),
174 prim->start, prim->count);
175
176 int start_vertex_location = prim->start;
177 int base_vertex_location = prim->basevertex;
178
179 if (is_indexed) {
180 vertex_access_type = devinfo->gen >= 7 ?
181 GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM :
182 GEN4_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM;
183 start_vertex_location += brw->ib.start_vertex_offset;
184 base_vertex_location += brw->vb.start_vertex_bias;
185 } else {
186 vertex_access_type = devinfo->gen >= 7 ?
187 GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL :
188 GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
189 start_vertex_location += brw->vb.start_vertex_bias;
190 }
191
192 /* We only need to trim the primitive count on pre-Gen6. */
193 if (devinfo->gen < 6)
194 verts_per_instance = trim(prim->mode, prim->count);
195 else
196 verts_per_instance = prim->count;
197
198 /* If nothing to emit, just return. */
199 if (verts_per_instance == 0 && !is_indirect && !xfb_obj)
200 return;
201
202 /* If we're set to always flush, do it before and after the primitive emit.
203 * We want to catch both missed flushes that hurt instruction/state cache
204 * and missed flushes of the render cache as it heads to other parts of
205 * the besides the draw code.
206 */
207 if (brw->always_flush_cache)
208 brw_emit_mi_flush(brw);
209
210 /* If indirect, emit a bunch of loads from the indirect BO. */
211 if (xfb_obj) {
212 indirect_flag = GEN7_3DPRIM_INDIRECT_PARAMETER_ENABLE;
213
214 brw_load_register_mem(brw, GEN7_3DPRIM_VERTEX_COUNT,
215 xfb_obj->prim_count_bo,
216 stream * sizeof(uint32_t));
217 BEGIN_BATCH(9);
218 OUT_BATCH(MI_LOAD_REGISTER_IMM | (9 - 2));
219 OUT_BATCH(GEN7_3DPRIM_INSTANCE_COUNT);
220 OUT_BATCH(prim->num_instances);
221 OUT_BATCH(GEN7_3DPRIM_START_VERTEX);
222 OUT_BATCH(0);
223 OUT_BATCH(GEN7_3DPRIM_BASE_VERTEX);
224 OUT_BATCH(0);
225 OUT_BATCH(GEN7_3DPRIM_START_INSTANCE);
226 OUT_BATCH(0);
227 ADVANCE_BATCH();
228 } else if (is_indirect) {
229 struct gl_buffer_object *indirect_buffer = brw->ctx.DrawIndirectBuffer;
230 struct brw_bo *bo = intel_bufferobj_buffer(brw,
231 intel_buffer_object(indirect_buffer),
232 indirect_offset, 5 * sizeof(GLuint), false);
233
234 indirect_flag = GEN7_3DPRIM_INDIRECT_PARAMETER_ENABLE;
235
236 brw_load_register_mem(brw, GEN7_3DPRIM_VERTEX_COUNT, bo,
237 indirect_offset + 0);
238 brw_load_register_mem(brw, GEN7_3DPRIM_INSTANCE_COUNT, bo,
239 indirect_offset + 4);
240
241 brw_load_register_mem(brw, GEN7_3DPRIM_START_VERTEX, bo,
242 indirect_offset + 8);
243 if (is_indexed) {
244 brw_load_register_mem(brw, GEN7_3DPRIM_BASE_VERTEX, bo,
245 indirect_offset + 12);
246 brw_load_register_mem(brw, GEN7_3DPRIM_START_INSTANCE, bo,
247 indirect_offset + 16);
248 } else {
249 brw_load_register_mem(brw, GEN7_3DPRIM_START_INSTANCE, bo,
250 indirect_offset + 12);
251 brw_load_register_imm32(brw, GEN7_3DPRIM_BASE_VERTEX, 0);
252 }
253 } else {
254 indirect_flag = 0;
255 }
256
257 BEGIN_BATCH(devinfo->gen >= 7 ? 7 : 6);
258
259 if (devinfo->gen >= 7) {
260 const int predicate_enable =
261 (brw->predicate.state == BRW_PREDICATE_STATE_USE_BIT)
262 ? GEN7_3DPRIM_PREDICATE_ENABLE : 0;
263
264 OUT_BATCH(CMD_3D_PRIM << 16 | (7 - 2) | indirect_flag | predicate_enable);
265 OUT_BATCH(hw_prim | vertex_access_type);
266 } else {
267 OUT_BATCH(CMD_3D_PRIM << 16 | (6 - 2) |
268 hw_prim << GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT |
269 vertex_access_type);
270 }
271 OUT_BATCH(verts_per_instance);
272 OUT_BATCH(start_vertex_location);
273 OUT_BATCH(prim->num_instances);
274 OUT_BATCH(prim->base_instance);
275 OUT_BATCH(base_vertex_location);
276 ADVANCE_BATCH();
277
278 if (brw->always_flush_cache)
279 brw_emit_mi_flush(brw);
280 }
281
282
283 static void
284 brw_merge_inputs(struct brw_context *brw)
285 {
286 const struct gen_device_info *devinfo = &brw->screen->devinfo;
287 const struct gl_context *ctx = &brw->ctx;
288 GLuint i;
289
290 for (i = 0; i < brw->vb.nr_buffers; i++) {
291 brw_bo_unreference(brw->vb.buffers[i].bo);
292 brw->vb.buffers[i].bo = NULL;
293 }
294 brw->vb.nr_buffers = 0;
295
296 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
297 struct brw_vertex_element *input = &brw->vb.inputs[i];
298 input->buffer = -1;
299 _mesa_draw_attrib_and_binding(ctx, i,
300 &input->glattrib, &input->glbinding);
301 }
302
303 if (devinfo->gen < 8 && !devinfo->is_haswell) {
304 uint64_t mask = ctx->VertexProgram._Current->info.inputs_read;
305 /* Prior to Haswell, the hardware can't natively support GL_FIXED or
306 * 2_10_10_10_REV vertex formats. Set appropriate workaround flags.
307 */
308 while (mask) {
309 const struct gl_vertex_format *glformat;
310 uint8_t wa_flags = 0;
311
312 i = u_bit_scan64(&mask);
313 glformat = &brw->vb.inputs[i].glattrib->Format;
314
315 switch (glformat->Type) {
316
317 case GL_FIXED:
318 wa_flags = glformat->Size;
319 break;
320
321 case GL_INT_2_10_10_10_REV:
322 wa_flags |= BRW_ATTRIB_WA_SIGN;
323 /* fallthough */
324
325 case GL_UNSIGNED_INT_2_10_10_10_REV:
326 if (glformat->Format == GL_BGRA)
327 wa_flags |= BRW_ATTRIB_WA_BGRA;
328
329 if (glformat->Normalized)
330 wa_flags |= BRW_ATTRIB_WA_NORMALIZE;
331 else if (!glformat->Integer)
332 wa_flags |= BRW_ATTRIB_WA_SCALE;
333
334 break;
335 }
336
337 if (brw->vb.attrib_wa_flags[i] != wa_flags) {
338 brw->vb.attrib_wa_flags[i] = wa_flags;
339 brw->ctx.NewDriverState |= BRW_NEW_VS_ATTRIB_WORKAROUNDS;
340 }
341 }
342 }
343 }
344
345 /* Disable auxiliary buffers if a renderbuffer is also bound as a texture
346 * or shader image. This causes a self-dependency, where both rendering
347 * and sampling may concurrently read or write the CCS buffer, causing
348 * incorrect pixels.
349 */
350 static bool
351 intel_disable_rb_aux_buffer(struct brw_context *brw,
352 bool *draw_aux_buffer_disabled,
353 struct intel_mipmap_tree *tex_mt,
354 unsigned min_level, unsigned num_levels,
355 const char *usage)
356 {
357 const struct gl_framebuffer *fb = brw->ctx.DrawBuffer;
358 bool found = false;
359
360 /* We only need to worry about color compression and fast clears. */
361 if (tex_mt->aux_usage != ISL_AUX_USAGE_CCS_D &&
362 tex_mt->aux_usage != ISL_AUX_USAGE_CCS_E)
363 return false;
364
365 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
366 const struct intel_renderbuffer *irb =
367 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
368
369 if (irb && irb->mt->bo == tex_mt->bo &&
370 irb->mt_level >= min_level &&
371 irb->mt_level < min_level + num_levels) {
372 found = draw_aux_buffer_disabled[i] = true;
373 }
374 }
375
376 if (found) {
377 perf_debug("Disabling CCS because a renderbuffer is also bound %s.\n",
378 usage);
379 }
380
381 return found;
382 }
383
384 /** Implement the ASTC 5x5 sampler workaround
385 *
386 * Gen9 sampling hardware has a bug where an ASTC 5x5 compressed surface
387 * cannot live in the sampler cache at the same time as an aux compressed
388 * surface. In order to work around the bug we have to stall rendering with a
389 * CS and pixel scoreboard stall (implicit in the CS stall) and invalidate the
390 * texture cache whenever one of ASTC 5x5 or aux compressed may be in the
391 * sampler cache and we're about to render with something which samples from
392 * the other.
393 *
394 * In the case of a single shader which textures from both ASTC 5x5 and
395 * a texture which is CCS or HiZ compressed, we have to resolve the aux
396 * compressed texture prior to rendering. This second part is handled in
397 * brw_predraw_resolve_inputs() below.
398 *
399 * We have observed this issue to affect CCS and HiZ sampling but whether or
400 * not it also affects MCS is unknown. Because MCS has no concept of a
401 * resolve (and doing one would be stupid expensive), we choose to simply
402 * ignore the possibility and hope for the best.
403 */
404 static void
405 gen9_apply_astc5x5_wa_flush(struct brw_context *brw,
406 enum gen9_astc5x5_wa_tex_type curr_mask)
407 {
408 assert(brw->screen->devinfo.gen == 9);
409
410 if (((brw->gen9_astc5x5_wa_tex_mask & GEN9_ASTC5X5_WA_TEX_TYPE_ASTC5x5) &&
411 (curr_mask & GEN9_ASTC5X5_WA_TEX_TYPE_AUX)) ||
412 ((brw->gen9_astc5x5_wa_tex_mask & GEN9_ASTC5X5_WA_TEX_TYPE_AUX) &&
413 (curr_mask & GEN9_ASTC5X5_WA_TEX_TYPE_ASTC5x5))) {
414 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL);
415 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
416 }
417
418 brw->gen9_astc5x5_wa_tex_mask = curr_mask;
419 }
420
421 static enum gen9_astc5x5_wa_tex_type
422 gen9_astc5x5_wa_bits(mesa_format format, enum isl_aux_usage aux_usage)
423 {
424 if (aux_usage != ISL_AUX_USAGE_NONE &&
425 aux_usage != ISL_AUX_USAGE_MCS)
426 return GEN9_ASTC5X5_WA_TEX_TYPE_AUX;
427
428 if (format == MESA_FORMAT_RGBA_ASTC_5x5 ||
429 format == MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5)
430 return GEN9_ASTC5X5_WA_TEX_TYPE_ASTC5x5;
431
432 return 0;
433 }
434
435 /* Helper for the gen9 ASTC 5x5 workaround. This version exists for BLORP's
436 * use-cases where only a single texture is bound.
437 */
438 void
439 gen9_apply_single_tex_astc5x5_wa(struct brw_context *brw,
440 mesa_format format,
441 enum isl_aux_usage aux_usage)
442 {
443 gen9_apply_astc5x5_wa_flush(brw, gen9_astc5x5_wa_bits(format, aux_usage));
444 }
445
446 static void
447 mark_textures_used_for_txf(BITSET_WORD *used_for_txf,
448 const struct gl_program *prog)
449 {
450 if (!prog)
451 return;
452
453 uint32_t mask = prog->info.textures_used_by_txf;
454 while (mask) {
455 int s = u_bit_scan(&mask);
456 BITSET_SET(used_for_txf, prog->SamplerUnits[s]);
457 }
458 }
459
460 /**
461 * \brief Resolve buffers before drawing.
462 *
463 * Resolve the depth buffer's HiZ buffer, resolve the depth buffer of each
464 * enabled depth texture, and flush the render cache for any dirty textures.
465 */
466 void
467 brw_predraw_resolve_inputs(struct brw_context *brw, bool rendering,
468 bool *draw_aux_buffer_disabled)
469 {
470 struct gl_context *ctx = &brw->ctx;
471 struct intel_texture_object *tex_obj;
472
473 BITSET_DECLARE(used_for_txf, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
474 memset(used_for_txf, 0, sizeof(used_for_txf));
475 if (rendering) {
476 mark_textures_used_for_txf(used_for_txf, ctx->VertexProgram._Current);
477 mark_textures_used_for_txf(used_for_txf, ctx->TessCtrlProgram._Current);
478 mark_textures_used_for_txf(used_for_txf, ctx->TessEvalProgram._Current);
479 mark_textures_used_for_txf(used_for_txf, ctx->GeometryProgram._Current);
480 mark_textures_used_for_txf(used_for_txf, ctx->FragmentProgram._Current);
481 } else {
482 mark_textures_used_for_txf(used_for_txf, ctx->ComputeProgram._Current);
483 }
484
485 int maxEnabledUnit = ctx->Texture._MaxEnabledTexImageUnit;
486
487 enum gen9_astc5x5_wa_tex_type astc5x5_wa_bits = 0;
488 if (brw->screen->devinfo.gen == 9) {
489 /* In order to properly implement the ASTC 5x5 workaround for an
490 * arbitrary draw or dispatch call, we have to walk the entire list of
491 * textures looking for ASTC 5x5. If there is any ASTC 5x5 in this draw
492 * call, all aux compressed textures must be resolved and have aux
493 * compression disabled while sampling.
494 */
495 for (int i = 0; i <= maxEnabledUnit; i++) {
496 if (!ctx->Texture.Unit[i]._Current)
497 continue;
498 tex_obj = intel_texture_object(ctx->Texture.Unit[i]._Current);
499 if (!tex_obj || !tex_obj->mt)
500 continue;
501
502 astc5x5_wa_bits |= gen9_astc5x5_wa_bits(tex_obj->_Format,
503 tex_obj->mt->aux_usage);
504 }
505 gen9_apply_astc5x5_wa_flush(brw, astc5x5_wa_bits);
506 }
507
508 /* Resolve depth buffer and render cache of each enabled texture. */
509 for (int i = 0; i <= maxEnabledUnit; i++) {
510 if (!ctx->Texture.Unit[i]._Current)
511 continue;
512 tex_obj = intel_texture_object(ctx->Texture.Unit[i]._Current);
513 if (!tex_obj || !tex_obj->mt)
514 continue;
515
516 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, i);
517 enum isl_format view_format =
518 translate_tex_format(brw, tex_obj->_Format, sampler->sRGBDecode);
519
520 unsigned min_level, min_layer, num_levels, num_layers;
521 if (tex_obj->base.Immutable) {
522 min_level = tex_obj->base.MinLevel;
523 num_levels = MIN2(tex_obj->base.NumLevels, tex_obj->_MaxLevel + 1);
524 min_layer = tex_obj->base.MinLayer;
525 num_layers = tex_obj->base.Target != GL_TEXTURE_3D ?
526 tex_obj->base.NumLayers : INTEL_REMAINING_LAYERS;
527 } else {
528 min_level = tex_obj->base.BaseLevel;
529 num_levels = tex_obj->_MaxLevel - tex_obj->base.BaseLevel + 1;
530 min_layer = 0;
531 num_layers = INTEL_REMAINING_LAYERS;
532 }
533
534 if (rendering) {
535 intel_disable_rb_aux_buffer(brw, draw_aux_buffer_disabled,
536 tex_obj->mt, min_level, num_levels,
537 "for sampling");
538 }
539
540 intel_miptree_prepare_texture(brw, tex_obj->mt, view_format,
541 min_level, num_levels,
542 min_layer, num_layers,
543 astc5x5_wa_bits);
544
545 /* If any programs are using it with texelFetch, we may need to also do
546 * a prepare with an sRGB format to ensure texelFetch works "properly".
547 */
548 if (BITSET_TEST(used_for_txf, i)) {
549 enum isl_format txf_format =
550 translate_tex_format(brw, tex_obj->_Format, GL_DECODE_EXT);
551 if (txf_format != view_format) {
552 intel_miptree_prepare_texture(brw, tex_obj->mt, txf_format,
553 min_level, num_levels,
554 min_layer, num_layers,
555 astc5x5_wa_bits);
556 }
557 }
558
559 brw_cache_flush_for_read(brw, tex_obj->mt->bo);
560
561 if (tex_obj->base.StencilSampling ||
562 tex_obj->mt->format == MESA_FORMAT_S_UINT8) {
563 intel_update_r8stencil(brw, tex_obj->mt);
564 }
565
566 if (intel_miptree_has_etc_shadow(brw, tex_obj->mt) &&
567 tex_obj->mt->shadow_needs_update) {
568 intel_miptree_update_etc_shadow_levels(brw, tex_obj->mt);
569 }
570 }
571
572 /* Resolve color for each active shader image. */
573 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
574 const struct gl_program *prog = ctx->_Shader->CurrentProgram[i];
575
576 if (unlikely(prog && prog->info.num_images)) {
577 for (unsigned j = 0; j < prog->info.num_images; j++) {
578 struct gl_image_unit *u =
579 &ctx->ImageUnits[prog->sh.ImageUnits[j]];
580 tex_obj = intel_texture_object(u->TexObj);
581
582 if (tex_obj && tex_obj->mt) {
583 if (rendering) {
584 intel_disable_rb_aux_buffer(brw, draw_aux_buffer_disabled,
585 tex_obj->mt, 0, ~0,
586 "as a shader image");
587 }
588
589 intel_miptree_prepare_image(brw, tex_obj->mt);
590
591 brw_cache_flush_for_read(brw, tex_obj->mt->bo);
592 }
593 }
594 }
595 }
596 }
597
598 static void
599 brw_predraw_resolve_framebuffer(struct brw_context *brw,
600 bool *draw_aux_buffer_disabled)
601 {
602 struct gl_context *ctx = &brw->ctx;
603 struct intel_renderbuffer *depth_irb;
604
605 /* Resolve the depth buffer's HiZ buffer. */
606 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
607 if (depth_irb && depth_irb->mt) {
608 intel_miptree_prepare_depth(brw, depth_irb->mt,
609 depth_irb->mt_level,
610 depth_irb->mt_layer,
611 depth_irb->layer_count);
612 }
613
614 /* Resolve color buffers for non-coherent framebuffer fetch. */
615 if (!ctx->Extensions.EXT_shader_framebuffer_fetch &&
616 ctx->FragmentProgram._Current &&
617 ctx->FragmentProgram._Current->info.outputs_read) {
618 const struct gl_framebuffer *fb = ctx->DrawBuffer;
619
620 /* This is only used for non-coherent framebuffer fetch, so we don't
621 * need to worry about CCS_E and can simply pass 'false' below.
622 */
623 assert(brw->screen->devinfo.gen < 9);
624
625 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
626 const struct intel_renderbuffer *irb =
627 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
628
629 if (irb) {
630 intel_miptree_prepare_texture(brw, irb->mt, irb->mt->surf.format,
631 irb->mt_level, 1,
632 irb->mt_layer, irb->layer_count,
633 brw->gen9_astc5x5_wa_tex_mask);
634 }
635 }
636 }
637
638 struct gl_framebuffer *fb = ctx->DrawBuffer;
639 for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
640 struct intel_renderbuffer *irb =
641 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
642
643 if (irb == NULL || irb->mt == NULL)
644 continue;
645
646 mesa_format mesa_format =
647 _mesa_get_render_format(ctx, intel_rb_format(irb));
648 enum isl_format isl_format = brw_isl_format_for_mesa_format(mesa_format);
649 bool blend_enabled = ctx->Color.BlendEnabled & (1 << i);
650 enum isl_aux_usage aux_usage =
651 intel_miptree_render_aux_usage(brw, irb->mt, isl_format,
652 blend_enabled,
653 draw_aux_buffer_disabled[i]);
654 if (brw->draw_aux_usage[i] != aux_usage) {
655 brw->ctx.NewDriverState |= BRW_NEW_AUX_STATE;
656 brw->draw_aux_usage[i] = aux_usage;
657 }
658
659 intel_miptree_prepare_render(brw, irb->mt, irb->mt_level,
660 irb->mt_layer, irb->layer_count,
661 aux_usage);
662
663 brw_cache_flush_for_render(brw, irb->mt->bo,
664 isl_format, aux_usage);
665 }
666 }
667
668 /**
669 * \brief Call this after drawing to mark which buffers need resolving
670 *
671 * If the depth buffer was written to and if it has an accompanying HiZ
672 * buffer, then mark that it needs a depth resolve.
673 *
674 * If the stencil buffer was written to then mark that it may need to be
675 * copied to an R8 texture.
676 *
677 * If the color buffer is a multisample window system buffer, then
678 * mark that it needs a downsample.
679 *
680 * Also mark any render targets which will be textured as needing a render
681 * cache flush.
682 */
683 static void
684 brw_postdraw_set_buffers_need_resolve(struct brw_context *brw)
685 {
686 struct gl_context *ctx = &brw->ctx;
687 struct gl_framebuffer *fb = ctx->DrawBuffer;
688
689 struct intel_renderbuffer *front_irb = NULL;
690 struct intel_renderbuffer *back_irb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
691 struct intel_renderbuffer *depth_irb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
692 struct intel_renderbuffer *stencil_irb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
693 struct gl_renderbuffer_attachment *depth_att = &fb->Attachment[BUFFER_DEPTH];
694
695 if (_mesa_is_front_buffer_drawing(fb))
696 front_irb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
697
698 if (front_irb)
699 front_irb->need_downsample = true;
700 if (back_irb)
701 back_irb->need_downsample = true;
702 if (depth_irb) {
703 bool depth_written = brw_depth_writes_enabled(brw);
704 if (depth_att->Layered) {
705 intel_miptree_finish_depth(brw, depth_irb->mt,
706 depth_irb->mt_level,
707 depth_irb->mt_layer,
708 depth_irb->layer_count,
709 depth_written);
710 } else {
711 intel_miptree_finish_depth(brw, depth_irb->mt,
712 depth_irb->mt_level,
713 depth_irb->mt_layer, 1,
714 depth_written);
715 }
716 if (depth_written)
717 brw_depth_cache_add_bo(brw, depth_irb->mt->bo);
718 }
719
720 if (stencil_irb && brw->stencil_write_enabled) {
721 struct intel_mipmap_tree *stencil_mt =
722 stencil_irb->mt->stencil_mt != NULL ?
723 stencil_irb->mt->stencil_mt : stencil_irb->mt;
724 brw_depth_cache_add_bo(brw, stencil_mt->bo);
725 intel_miptree_finish_write(brw, stencil_mt, stencil_irb->mt_level,
726 stencil_irb->mt_layer,
727 stencil_irb->layer_count, ISL_AUX_USAGE_NONE);
728 }
729
730 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
731 struct intel_renderbuffer *irb =
732 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
733
734 if (!irb)
735 continue;
736
737 mesa_format mesa_format =
738 _mesa_get_render_format(ctx, intel_rb_format(irb));
739 enum isl_format isl_format = brw_isl_format_for_mesa_format(mesa_format);
740 enum isl_aux_usage aux_usage = brw->draw_aux_usage[i];
741
742 brw_render_cache_add_bo(brw, irb->mt->bo, isl_format, aux_usage);
743
744 intel_miptree_finish_render(brw, irb->mt, irb->mt_level,
745 irb->mt_layer, irb->layer_count,
746 aux_usage);
747 }
748 }
749
750 static void
751 intel_renderbuffer_move_temp_back(struct brw_context *brw,
752 struct intel_renderbuffer *irb)
753 {
754 if (irb->align_wa_mt == NULL)
755 return;
756
757 brw_cache_flush_for_read(brw, irb->align_wa_mt->bo);
758
759 intel_miptree_copy_slice(brw, irb->align_wa_mt, 0, 0,
760 irb->mt,
761 irb->Base.Base.TexImage->Level, irb->mt_layer);
762
763 intel_miptree_reference(&irb->align_wa_mt, NULL);
764
765 /* Finally restore the x,y to correspond to full miptree. */
766 intel_renderbuffer_set_draw_offset(irb);
767
768 /* Make sure render surface state gets re-emitted with updated miptree. */
769 brw->NewGLState |= _NEW_BUFFERS;
770 }
771
772 static void
773 brw_postdraw_reconcile_align_wa_slices(struct brw_context *brw)
774 {
775 struct gl_context *ctx = &brw->ctx;
776 struct gl_framebuffer *fb = ctx->DrawBuffer;
777
778 struct intel_renderbuffer *depth_irb =
779 intel_get_renderbuffer(fb, BUFFER_DEPTH);
780 struct intel_renderbuffer *stencil_irb =
781 intel_get_renderbuffer(fb, BUFFER_STENCIL);
782
783 if (depth_irb && depth_irb->align_wa_mt)
784 intel_renderbuffer_move_temp_back(brw, depth_irb);
785
786 if (stencil_irb && stencil_irb->align_wa_mt)
787 intel_renderbuffer_move_temp_back(brw, stencil_irb);
788
789 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
790 struct intel_renderbuffer *irb =
791 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
792
793 if (!irb || irb->align_wa_mt == NULL)
794 continue;
795
796 intel_renderbuffer_move_temp_back(brw, irb);
797 }
798 }
799
800 static void
801 brw_prepare_drawing(struct gl_context *ctx,
802 const struct _mesa_index_buffer *ib,
803 bool index_bounds_valid,
804 GLuint min_index,
805 GLuint max_index)
806 {
807 struct brw_context *brw = brw_context(ctx);
808
809 if (ctx->NewState)
810 _mesa_update_state(ctx);
811
812 /* We have to validate the textures *before* checking for fallbacks;
813 * otherwise, the software fallback won't be able to rely on the
814 * texture state, the firstLevel and lastLevel fields won't be
815 * set in the intel texture object (they'll both be 0), and the
816 * software fallback will segfault if it attempts to access any
817 * texture level other than level 0.
818 */
819 brw_validate_textures(brw);
820
821 /* Find the highest sampler unit used by each shader program. A bit-count
822 * won't work since ARB programs use the texture unit number as the sampler
823 * index.
824 */
825 brw->wm.base.sampler_count =
826 util_last_bit(ctx->FragmentProgram._Current->info.textures_used);
827 brw->gs.base.sampler_count = ctx->GeometryProgram._Current ?
828 util_last_bit(ctx->GeometryProgram._Current->info.textures_used) : 0;
829 brw->tes.base.sampler_count = ctx->TessEvalProgram._Current ?
830 util_last_bit(ctx->TessEvalProgram._Current->info.textures_used) : 0;
831 brw->tcs.base.sampler_count = ctx->TessCtrlProgram._Current ?
832 util_last_bit(ctx->TessCtrlProgram._Current->info.textures_used) : 0;
833 brw->vs.base.sampler_count =
834 util_last_bit(ctx->VertexProgram._Current->info.textures_used);
835
836 intel_prepare_render(brw);
837
838 /* This workaround has to happen outside of brw_upload_render_state()
839 * because it may flush the batchbuffer for a blit, affecting the state
840 * flags.
841 */
842 brw_workaround_depthstencil_alignment(brw, 0);
843
844 /* Resolves must occur after updating renderbuffers, updating context state,
845 * and finalizing textures but before setting up any hardware state for
846 * this draw call.
847 */
848 bool draw_aux_buffer_disabled[MAX_DRAW_BUFFERS] = { };
849 brw_predraw_resolve_inputs(brw, true, draw_aux_buffer_disabled);
850 brw_predraw_resolve_framebuffer(brw, draw_aux_buffer_disabled);
851
852 /* Bind all inputs, derive varying and size information:
853 */
854 brw_merge_inputs(brw);
855
856 brw->ib.ib = ib;
857 brw->ctx.NewDriverState |= BRW_NEW_INDICES;
858
859 brw->vb.index_bounds_valid = index_bounds_valid;
860 brw->vb.min_index = min_index;
861 brw->vb.max_index = max_index;
862 brw->ctx.NewDriverState |= BRW_NEW_VERTICES;
863 }
864
865 static void
866 brw_finish_drawing(struct gl_context *ctx)
867 {
868 struct brw_context *brw = brw_context(ctx);
869
870 if (brw->always_flush_batch)
871 intel_batchbuffer_flush(brw);
872
873 brw_program_cache_check_size(brw);
874 brw_postdraw_reconcile_align_wa_slices(brw);
875 brw_postdraw_set_buffers_need_resolve(brw);
876
877 if (brw->draw.draw_params_count_bo) {
878 brw_bo_unreference(brw->draw.draw_params_count_bo);
879 brw->draw.draw_params_count_bo = NULL;
880 }
881
882 if (brw->draw.draw_params_bo) {
883 brw_bo_unreference(brw->draw.draw_params_bo);
884 brw->draw.draw_params_bo = NULL;
885 }
886
887 if (brw->draw.derived_draw_params_bo) {
888 brw_bo_unreference(brw->draw.derived_draw_params_bo);
889 brw->draw.derived_draw_params_bo = NULL;
890 }
891 }
892
893 /**
894 * Implement workarounds for preemption:
895 * - WaDisableMidObjectPreemptionForGSLineStripAdj
896 * - WaDisableMidObjectPreemptionForTrifanOrPolygon
897 * - WaDisableMidObjectPreemptionForLineLoop
898 * - WA#0798
899 */
900 static void
901 gen9_emit_preempt_wa(struct brw_context *brw,
902 const struct _mesa_prim *prim)
903 {
904 bool object_preemption = true;
905 ASSERTED const struct gen_device_info *devinfo = &brw->screen->devinfo;
906
907 /* Only apply these workarounds for gen9 */
908 assert(devinfo->gen == 9);
909
910 /* WaDisableMidObjectPreemptionForGSLineStripAdj
911 *
912 * WA: Disable mid-draw preemption when draw-call is a linestrip_adj and
913 * GS is enabled.
914 */
915 if (brw->primitive == _3DPRIM_LINESTRIP_ADJ && brw->gs.enabled)
916 object_preemption = false;
917
918 /* WaDisableMidObjectPreemptionForTrifanOrPolygon
919 *
920 * TriFan miscompare in Execlist Preemption test. Cut index that is on a
921 * previous context. End the previous, the resume another context with a
922 * tri-fan or polygon, and the vertex count is corrupted. If we prempt
923 * again we will cause corruption.
924 *
925 * WA: Disable mid-draw preemption when draw-call has a tri-fan.
926 */
927 if (brw->primitive == _3DPRIM_TRIFAN)
928 object_preemption = false;
929
930 /* WaDisableMidObjectPreemptionForLineLoop
931 *
932 * VF Stats Counters Missing a vertex when preemption enabled.
933 *
934 * WA: Disable mid-draw preemption when the draw uses a lineloop
935 * topology.
936 */
937 if (brw->primitive == _3DPRIM_LINELOOP)
938 object_preemption = false;
939
940 /* WA#0798
941 *
942 * VF is corrupting GAFS data when preempted on an instance boundary and
943 * replayed with instancing enabled.
944 *
945 * WA: Disable preemption when using instanceing.
946 */
947 if (prim->num_instances > 1)
948 object_preemption = false;
949
950 brw_enable_obj_preemption(brw, object_preemption);
951 }
952
953 /* May fail if out of video memory for texture or vbo upload, or on
954 * fallback conditions.
955 */
956 static void
957 brw_draw_single_prim(struct gl_context *ctx,
958 const struct _mesa_prim *prim,
959 unsigned prim_id,
960 bool is_indexed,
961 struct brw_transform_feedback_object *xfb_obj,
962 unsigned stream,
963 GLsizeiptr indirect_offset)
964 {
965 struct brw_context *brw = brw_context(ctx);
966 const struct gen_device_info *devinfo = &brw->screen->devinfo;
967 bool fail_next;
968 bool is_indirect = brw->draw.draw_indirect_data != NULL;
969
970 /* Flag BRW_NEW_DRAW_CALL on every draw. This allows us to have
971 * atoms that happen on every draw call.
972 */
973 brw->ctx.NewDriverState |= BRW_NEW_DRAW_CALL;
974
975 /* Flush the batch if the batch/state buffers are nearly full. We can
976 * grow them if needed, but this is not free, so we'd like to avoid it.
977 */
978 intel_batchbuffer_require_space(brw, 1500);
979 brw_require_statebuffer_space(brw, 2400);
980 intel_batchbuffer_save_state(brw);
981 fail_next = intel_batchbuffer_saved_state_is_empty(brw);
982
983 if (brw->num_instances != prim->num_instances ||
984 brw->basevertex != prim->basevertex ||
985 brw->baseinstance != prim->base_instance) {
986 brw->num_instances = prim->num_instances;
987 brw->basevertex = prim->basevertex;
988 brw->baseinstance = prim->base_instance;
989 if (prim_id > 0) { /* For i == 0 we just did this before the loop */
990 brw->ctx.NewDriverState |= BRW_NEW_VERTICES;
991 brw_merge_inputs(brw);
992 }
993 }
994
995 /* Determine if we need to flag BRW_NEW_VERTICES for updating the
996 * gl_BaseVertexARB or gl_BaseInstanceARB values. For indirect draw, we
997 * always flag if the shader uses one of the values. For direct draws,
998 * we only flag if the values change.
999 */
1000 const int new_firstvertex =
1001 is_indexed ? prim->basevertex : prim->start;
1002 const int new_baseinstance = prim->base_instance;
1003 const struct brw_vs_prog_data *vs_prog_data =
1004 brw_vs_prog_data(brw->vs.base.prog_data);
1005 if (prim_id > 0) {
1006 const bool uses_draw_parameters =
1007 vs_prog_data->uses_firstvertex ||
1008 vs_prog_data->uses_baseinstance;
1009
1010 if ((uses_draw_parameters && is_indirect) ||
1011 (vs_prog_data->uses_firstvertex &&
1012 brw->draw.params.firstvertex != new_firstvertex) ||
1013 (vs_prog_data->uses_baseinstance &&
1014 brw->draw.params.gl_baseinstance != new_baseinstance))
1015 brw->ctx.NewDriverState |= BRW_NEW_VERTICES;
1016 }
1017
1018 brw->draw.params.firstvertex = new_firstvertex;
1019 brw->draw.params.gl_baseinstance = new_baseinstance;
1020 brw_bo_unreference(brw->draw.draw_params_bo);
1021
1022 if (is_indirect) {
1023 /* Point draw_params_bo at the indirect buffer. */
1024 brw->draw.draw_params_bo =
1025 intel_buffer_object(ctx->DrawIndirectBuffer)->buffer;
1026 brw_bo_reference(brw->draw.draw_params_bo);
1027 brw->draw.draw_params_offset =
1028 indirect_offset + (is_indexed ? 12 : 8);
1029 } else {
1030 /* Set draw_params_bo to NULL so brw_prepare_vertices knows it
1031 * has to upload gl_BaseVertex and such if they're needed.
1032 */
1033 brw->draw.draw_params_bo = NULL;
1034 brw->draw.draw_params_offset = 0;
1035 }
1036
1037 /* gl_DrawID always needs its own vertex buffer since it's not part of
1038 * the indirect parameter buffer. Same for is_indexed_draw, which shares
1039 * the buffer with gl_DrawID. If the program uses gl_DrawID, we need to
1040 * flag BRW_NEW_VERTICES. For the first iteration, we don't have valid
1041 * vs_prog_data, but we always flag BRW_NEW_VERTICES before the loop.
1042 */
1043 if (prim_id > 0 && vs_prog_data->uses_drawid)
1044 brw->ctx.NewDriverState |= BRW_NEW_VERTICES;
1045
1046 brw->draw.derived_params.gl_drawid = prim->draw_id;
1047 brw->draw.derived_params.is_indexed_draw = is_indexed ? ~0 : 0;
1048
1049 brw_bo_unreference(brw->draw.derived_draw_params_bo);
1050 brw->draw.derived_draw_params_bo = NULL;
1051 brw->draw.derived_draw_params_offset = 0;
1052
1053 if (devinfo->gen < 6)
1054 brw_set_prim(brw, prim);
1055 else
1056 gen6_set_prim(brw, prim);
1057
1058 retry:
1059
1060 /* Note that before the loop, brw->ctx.NewDriverState was set to != 0, and
1061 * that the state updated in the loop outside of this block is that in
1062 * *_set_prim or intel_batchbuffer_flush(), which only impacts
1063 * brw->ctx.NewDriverState.
1064 */
1065 if (brw->ctx.NewDriverState) {
1066 brw->batch.no_wrap = true;
1067 brw_upload_render_state(brw);
1068 }
1069
1070 if (devinfo->gen == 9)
1071 gen9_emit_preempt_wa(brw, prim);
1072
1073 brw_emit_prim(brw, prim, brw->primitive, is_indexed, xfb_obj, stream,
1074 is_indirect, indirect_offset);
1075
1076 brw->batch.no_wrap = false;
1077
1078 if (!brw_batch_has_aperture_space(brw, 0)) {
1079 if (!fail_next) {
1080 intel_batchbuffer_reset_to_saved(brw);
1081 intel_batchbuffer_flush(brw);
1082 fail_next = true;
1083 goto retry;
1084 } else {
1085 int ret = intel_batchbuffer_flush(brw);
1086 WARN_ONCE(ret == -ENOSPC,
1087 "i965: Single primitive emit exceeded "
1088 "available aperture space\n");
1089 }
1090 }
1091
1092 /* Now that we know we haven't run out of aperture space, we can safely
1093 * reset the dirty bits.
1094 */
1095 if (brw->ctx.NewDriverState)
1096 brw_render_state_finished(brw);
1097
1098 return;
1099 }
1100
1101
1102
1103 void
1104 brw_draw_prims(struct gl_context *ctx,
1105 const struct _mesa_prim *prims,
1106 GLuint nr_prims,
1107 const struct _mesa_index_buffer *ib,
1108 GLboolean index_bounds_valid,
1109 GLuint min_index,
1110 GLuint max_index,
1111 struct gl_transform_feedback_object *gl_xfb_obj,
1112 unsigned stream)
1113 {
1114 unsigned i;
1115 struct brw_context *brw = brw_context(ctx);
1116 int predicate_state = brw->predicate.state;
1117 struct brw_transform_feedback_object *xfb_obj =
1118 (struct brw_transform_feedback_object *) gl_xfb_obj;
1119
1120 if (!brw_check_conditional_render(brw))
1121 return;
1122
1123 /* Handle primitive restart if needed */
1124 if (brw_handle_primitive_restart(ctx, prims, nr_prims, ib)) {
1125 /* The draw was handled, so we can exit now */
1126 return;
1127 }
1128
1129 /* Do GL_SELECT and GL_FEEDBACK rendering using swrast, even though it
1130 * won't support all the extensions we support.
1131 */
1132 if (ctx->RenderMode != GL_RENDER) {
1133 perf_debug("%s render mode not supported in hardware\n",
1134 _mesa_enum_to_string(ctx->RenderMode));
1135 _swsetup_Wakeup(ctx);
1136 _tnl_wakeup(ctx);
1137 _tnl_draw(ctx, prims, nr_prims, ib,
1138 index_bounds_valid, min_index, max_index, NULL, 0);
1139 return;
1140 }
1141
1142 /* If we're going to have to upload any of the user's vertex arrays, then
1143 * get the minimum and maximum of their index buffer so we know what range
1144 * to upload.
1145 */
1146 if (!index_bounds_valid && _mesa_draw_user_array_bits(ctx) != 0) {
1147 perf_debug("Scanning index buffer to compute index buffer bounds. "
1148 "Use glDrawRangeElements() to avoid this.\n");
1149 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
1150 index_bounds_valid = true;
1151 }
1152
1153 brw_prepare_drawing(ctx, ib, index_bounds_valid, min_index, max_index);
1154 /* Try drawing with the hardware, but don't do anything else if we can't
1155 * manage it. swrast doesn't support our featureset, so we can't fall back
1156 * to it.
1157 */
1158
1159 for (i = 0; i < nr_prims; i++) {
1160 /* Implementation of ARB_indirect_parameters via predicates */
1161 if (brw->draw.draw_params_count_bo) {
1162 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_FLUSH_ENABLE);
1163
1164 /* Upload the current draw count from the draw parameters buffer to
1165 * MI_PREDICATE_SRC0.
1166 */
1167 brw_load_register_mem(brw, MI_PREDICATE_SRC0,
1168 brw->draw.draw_params_count_bo,
1169 brw->draw.draw_params_count_offset);
1170 /* Zero the top 32-bits of MI_PREDICATE_SRC0 */
1171 brw_load_register_imm32(brw, MI_PREDICATE_SRC0 + 4, 0);
1172 /* Upload the id of the current primitive to MI_PREDICATE_SRC1. */
1173 brw_load_register_imm64(brw, MI_PREDICATE_SRC1, prims[i].draw_id);
1174
1175 BEGIN_BATCH(1);
1176 if (i == 0 && brw->predicate.state != BRW_PREDICATE_STATE_USE_BIT) {
1177 OUT_BATCH(GEN7_MI_PREDICATE | MI_PREDICATE_LOADOP_LOADINV |
1178 MI_PREDICATE_COMBINEOP_SET |
1179 MI_PREDICATE_COMPAREOP_SRCS_EQUAL);
1180 } else {
1181 OUT_BATCH(GEN7_MI_PREDICATE |
1182 MI_PREDICATE_LOADOP_LOAD | MI_PREDICATE_COMBINEOP_XOR |
1183 MI_PREDICATE_COMPAREOP_SRCS_EQUAL);
1184 }
1185 ADVANCE_BATCH();
1186
1187 brw->predicate.state = BRW_PREDICATE_STATE_USE_BIT;
1188 }
1189
1190 brw_draw_single_prim(ctx, &prims[i], i, ib != NULL, xfb_obj, stream,
1191 brw->draw.draw_indirect_offset +
1192 brw->draw.draw_indirect_stride * i);
1193 }
1194
1195 brw_finish_drawing(ctx);
1196 brw->predicate.state = predicate_state;
1197 }
1198
1199 void
1200 brw_draw_indirect_prims(struct gl_context *ctx,
1201 GLuint mode,
1202 struct gl_buffer_object *indirect_data,
1203 GLsizeiptr indirect_offset,
1204 unsigned draw_count,
1205 unsigned stride,
1206 struct gl_buffer_object *indirect_params,
1207 GLsizeiptr indirect_params_offset,
1208 const struct _mesa_index_buffer *ib)
1209 {
1210 struct brw_context *brw = brw_context(ctx);
1211 struct _mesa_prim *prim;
1212 GLsizei i;
1213
1214 prim = calloc(draw_count, sizeof(*prim));
1215 if (prim == NULL) {
1216 _mesa_error(ctx, GL_OUT_OF_MEMORY, "gl%sDraw%sIndirect%s",
1217 (draw_count > 1) ? "Multi" : "",
1218 ib ? "Elements" : "Arrays",
1219 indirect_params ? "CountARB" : "");
1220 return;
1221 }
1222
1223 brw->draw.draw_indirect_stride = stride;
1224 brw->draw.draw_indirect_offset = indirect_offset;
1225
1226 prim[0].begin = 1;
1227 prim[draw_count - 1].end = 1;
1228 for (i = 0; i < draw_count; ++i) {
1229 prim[i].mode = mode;
1230 prim[i].draw_id = i;
1231 }
1232
1233 if (indirect_params) {
1234 brw->draw.draw_params_count_bo =
1235 intel_buffer_object(indirect_params)->buffer;
1236 brw_bo_reference(brw->draw.draw_params_count_bo);
1237 brw->draw.draw_params_count_offset = indirect_params_offset;
1238 }
1239
1240 brw->draw.draw_indirect_data = indirect_data;
1241
1242 brw_draw_prims(ctx, prim, draw_count,
1243 ib, false, 0, ~0,
1244 NULL, 0);
1245
1246 brw->draw.draw_indirect_data = NULL;
1247 free(prim);
1248 }
1249
1250 void
1251 brw_init_draw_functions(struct dd_function_table *functions)
1252 {
1253 /* Register our drawing function:
1254 */
1255 functions->Draw = brw_draw_prims;
1256 functions->DrawIndirect = brw_draw_indirect_prims;
1257 }
1258
1259 void
1260 brw_draw_init(struct brw_context *brw)
1261 {
1262 for (int i = 0; i < VERT_ATTRIB_MAX; i++)
1263 brw->vb.inputs[i].buffer = -1;
1264 brw->vb.nr_buffers = 0;
1265 brw->vb.nr_enabled = 0;
1266 }
1267
1268 void
1269 brw_draw_destroy(struct brw_context *brw)
1270 {
1271 unsigned i;
1272
1273 for (i = 0; i < brw->vb.nr_buffers; i++) {
1274 brw_bo_unreference(brw->vb.buffers[i].bo);
1275 brw->vb.buffers[i].bo = NULL;
1276 }
1277 brw->vb.nr_buffers = 0;
1278
1279 for (i = 0; i < brw->vb.nr_enabled; i++) {
1280 brw->vb.enabled[i]->buffer = -1;
1281 }
1282 brw->vb.nr_enabled = 0;
1283
1284 brw_bo_unreference(brw->ib.bo);
1285 brw->ib.bo = NULL;
1286 }