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