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