i965/blorp: Disable BLORP clear color updates
[mesa.git] / src / mesa / drivers / dri / i965 / brw_blorp.c
1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "main/context.h"
25 #include "main/teximage.h"
26 #include "main/blend.h"
27 #include "main/bufferobj.h"
28 #include "main/enums.h"
29 #include "main/fbobject.h"
30 #include "main/image.h"
31 #include "main/renderbuffer.h"
32 #include "main/glformats.h"
33
34 #include "brw_blorp.h"
35 #include "brw_context.h"
36 #include "brw_defines.h"
37 #include "brw_meta_util.h"
38 #include "brw_state.h"
39 #include "intel_buffer_objects.h"
40 #include "intel_fbo.h"
41 #include "common/gen_debug.h"
42
43 #define FILE_DEBUG_FLAG DEBUG_BLORP
44
45 static bool
46 brw_blorp_lookup_shader(struct blorp_context *blorp,
47 const void *key, uint32_t key_size,
48 uint32_t *kernel_out, void *prog_data_out)
49 {
50 struct brw_context *brw = blorp->driver_ctx;
51 return brw_search_cache(&brw->cache, BRW_CACHE_BLORP_PROG,
52 key, key_size, kernel_out, prog_data_out);
53 }
54
55 static bool
56 brw_blorp_upload_shader(struct blorp_context *blorp,
57 const void *key, uint32_t key_size,
58 const void *kernel, uint32_t kernel_size,
59 const struct brw_stage_prog_data *prog_data,
60 uint32_t prog_data_size,
61 uint32_t *kernel_out, void *prog_data_out)
62 {
63 struct brw_context *brw = blorp->driver_ctx;
64 brw_upload_cache(&brw->cache, BRW_CACHE_BLORP_PROG, key, key_size,
65 kernel, kernel_size, prog_data, prog_data_size,
66 kernel_out, prog_data_out);
67 return true;
68 }
69
70 void
71 brw_blorp_init(struct brw_context *brw)
72 {
73 const struct gen_device_info *devinfo = &brw->screen->devinfo;
74
75 blorp_init(&brw->blorp, brw, &brw->isl_dev);
76
77 brw->blorp.compiler = brw->screen->compiler;
78
79 switch (devinfo->gen) {
80 case 4:
81 if (devinfo->is_g4x) {
82 brw->blorp.exec = gen45_blorp_exec;
83 } else {
84 brw->blorp.exec = gen4_blorp_exec;
85 }
86 break;
87 case 5:
88 brw->blorp.exec = gen5_blorp_exec;
89 break;
90 case 6:
91 brw->blorp.exec = gen6_blorp_exec;
92 break;
93 case 7:
94 if (devinfo->is_haswell) {
95 brw->blorp.exec = gen75_blorp_exec;
96 } else {
97 brw->blorp.exec = gen7_blorp_exec;
98 }
99 break;
100 case 8:
101 brw->blorp.exec = gen8_blorp_exec;
102 break;
103 case 9:
104 brw->blorp.exec = gen9_blorp_exec;
105 break;
106 case 10:
107 brw->blorp.exec = gen10_blorp_exec;
108 break;
109 case 11:
110 brw->blorp.exec = gen11_blorp_exec;
111 break;
112
113 default:
114 unreachable("Invalid gen");
115 }
116
117 brw->blorp.lookup_shader = brw_blorp_lookup_shader;
118 brw->blorp.upload_shader = brw_blorp_upload_shader;
119 }
120
121 static void
122 blorp_surf_for_miptree(struct brw_context *brw,
123 struct blorp_surf *surf,
124 struct intel_mipmap_tree *mt,
125 enum isl_aux_usage aux_usage,
126 bool is_render_target,
127 unsigned *level,
128 unsigned start_layer, unsigned num_layers,
129 struct isl_surf tmp_surfs[1])
130 {
131 const struct gen_device_info *devinfo = &brw->screen->devinfo;
132
133 if (mt->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY) {
134 const unsigned num_samples = mt->surf.samples;
135 for (unsigned i = 0; i < num_layers; i++) {
136 for (unsigned s = 0; s < num_samples; s++) {
137 const unsigned phys_layer = (start_layer + i) * num_samples + s;
138 intel_miptree_check_level_layer(mt, *level, phys_layer);
139 }
140 }
141 } else {
142 for (unsigned i = 0; i < num_layers; i++)
143 intel_miptree_check_level_layer(mt, *level, start_layer + i);
144 }
145
146 *surf = (struct blorp_surf) {
147 .surf = &mt->surf,
148 .addr = (struct blorp_address) {
149 .buffer = mt->bo,
150 .offset = mt->offset,
151 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
152 .mocs = brw_get_bo_mocs(devinfo, mt->bo),
153 },
154 .aux_usage = aux_usage,
155 };
156
157 if (mt->format == MESA_FORMAT_S_UINT8 && is_render_target &&
158 devinfo->gen <= 7)
159 mt->r8stencil_needs_update = true;
160
161 if (surf->aux_usage == ISL_AUX_USAGE_HIZ &&
162 !intel_miptree_level_has_hiz(mt, *level))
163 surf->aux_usage = ISL_AUX_USAGE_NONE;
164
165 if (surf->aux_usage != ISL_AUX_USAGE_NONE) {
166 /* We only really need a clear color if we also have an auxiliary
167 * surface. Without one, it does nothing.
168 */
169 surf->clear_color =
170 intel_miptree_get_clear_color(devinfo, mt, mt->surf.format,
171 !is_render_target, (struct brw_bo **)
172 &surf->clear_color_addr.buffer,
173 &surf->clear_color_addr.offset);
174
175 surf->aux_surf = &mt->aux_buf->surf;
176 surf->aux_addr = (struct blorp_address) {
177 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
178 .mocs = surf->addr.mocs,
179 };
180
181 surf->aux_addr.buffer = mt->aux_buf->bo;
182 surf->aux_addr.offset = mt->aux_buf->offset;
183 } else {
184 surf->aux_addr = (struct blorp_address) {
185 .buffer = NULL,
186 };
187 memset(&surf->clear_color, 0, sizeof(surf->clear_color));
188 }
189 assert((surf->aux_usage == ISL_AUX_USAGE_NONE) ==
190 (surf->aux_addr.buffer == NULL));
191
192 /* ISL wants real levels, not offset ones. */
193 *level -= mt->first_level;
194 }
195
196 static bool
197 brw_blorp_supports_dst_format(struct brw_context *brw, mesa_format format)
198 {
199 /* If it's renderable, it's definitely supported. */
200 if (brw->mesa_format_supports_render[format])
201 return true;
202
203 /* BLORP can't compress anything */
204 if (_mesa_is_format_compressed(format))
205 return false;
206
207 /* No exotic formats such as GL_LUMINANCE_ALPHA */
208 if (_mesa_get_format_bits(format, GL_RED_BITS) == 0 &&
209 _mesa_get_format_bits(format, GL_DEPTH_BITS) == 0 &&
210 _mesa_get_format_bits(format, GL_STENCIL_BITS) == 0)
211 return false;
212
213 return true;
214 }
215
216 static enum isl_format
217 brw_blorp_to_isl_format(struct brw_context *brw, mesa_format format,
218 bool is_render_target)
219 {
220 switch (format) {
221 case MESA_FORMAT_NONE:
222 return ISL_FORMAT_UNSUPPORTED;
223 case MESA_FORMAT_S_UINT8:
224 return ISL_FORMAT_R8_UINT;
225 case MESA_FORMAT_Z24_UNORM_X8_UINT:
226 case MESA_FORMAT_Z24_UNORM_S8_UINT:
227 return ISL_FORMAT_R24_UNORM_X8_TYPELESS;
228 case MESA_FORMAT_Z_FLOAT32:
229 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
230 return ISL_FORMAT_R32_FLOAT;
231 case MESA_FORMAT_Z_UNORM16:
232 return ISL_FORMAT_R16_UNORM;
233 default:
234 if (is_render_target) {
235 assert(brw_blorp_supports_dst_format(brw, format));
236 if (brw->mesa_format_supports_render[format]) {
237 return brw->mesa_to_isl_render_format[format];
238 } else {
239 return brw_isl_format_for_mesa_format(format);
240 }
241 } else {
242 /* Some destinations (is_render_target == true) are supported by
243 * blorp even though we technically can't render to them.
244 */
245 return brw_isl_format_for_mesa_format(format);
246 }
247 }
248 }
249
250 /**
251 * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
252 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
253 *
254 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
255 * 0 1 2 3 4 5
256 * 4 5 6 7 0 1
257 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
258 *
259 * which is simply adding 4 then modding by 8 (or anding with 7).
260 *
261 * We then may need to apply workarounds for textureGather hardware bugs.
262 */
263 static enum isl_channel_select
264 swizzle_to_scs(GLenum swizzle)
265 {
266 return (enum isl_channel_select)((swizzle + 4) & 7);
267 }
268
269 /**
270 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
271 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
272 * the physical layer holding sample 0. So, for example, if
273 * src_mt->surf.samples == 4, then logical layer n corresponds to src_layer ==
274 * 4*n.
275 */
276 void
277 brw_blorp_blit_miptrees(struct brw_context *brw,
278 struct intel_mipmap_tree *src_mt,
279 unsigned src_level, unsigned src_layer,
280 mesa_format src_format, int src_swizzle,
281 struct intel_mipmap_tree *dst_mt,
282 unsigned dst_level, unsigned dst_layer,
283 mesa_format dst_format,
284 float src_x0, float src_y0,
285 float src_x1, float src_y1,
286 float dst_x0, float dst_y0,
287 float dst_x1, float dst_y1,
288 GLenum filter, bool mirror_x, bool mirror_y,
289 bool decode_srgb, bool encode_srgb)
290 {
291 const struct gen_device_info *devinfo = &brw->screen->devinfo;
292
293 DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f)"
294 "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
295 __func__,
296 src_mt->surf.samples, _mesa_get_format_name(src_mt->format), src_mt,
297 src_level, src_layer, src_x0, src_y0, src_x1, src_y1,
298 dst_mt->surf.samples, _mesa_get_format_name(dst_mt->format), dst_mt,
299 dst_level, dst_layer, dst_x0, dst_y0, dst_x1, dst_y1,
300 mirror_x, mirror_y);
301
302 if (!decode_srgb && _mesa_get_format_color_encoding(src_format) == GL_SRGB)
303 src_format = _mesa_get_srgb_format_linear(src_format);
304
305 if (!encode_srgb && _mesa_get_format_color_encoding(dst_format) == GL_SRGB)
306 dst_format = _mesa_get_srgb_format_linear(dst_format);
307
308 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
309 * texture, the above code configures the source format for L32_FLOAT or
310 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
311 * the SAMPLE message appears to handle multisampled L32_FLOAT and
312 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
313 * around the problem by using a source format of R32_FLOAT. This
314 * shouldn't affect rendering correctness, since the destination format is
315 * R32_FLOAT, so only the contents of the red channel matters.
316 */
317 if (devinfo->gen == 6 &&
318 src_mt->surf.samples > 1 && dst_mt->surf.samples <= 1 &&
319 src_mt->format == dst_mt->format &&
320 (dst_format == MESA_FORMAT_L_FLOAT32 ||
321 dst_format == MESA_FORMAT_I_FLOAT32)) {
322 src_format = dst_format = MESA_FORMAT_R_FLOAT32;
323 }
324
325 enum isl_format src_isl_format =
326 brw_blorp_to_isl_format(brw, src_format, false);
327 enum isl_aux_usage src_aux_usage =
328 intel_miptree_texture_aux_usage(brw, src_mt, src_isl_format);
329 /* We do format workarounds for some depth formats so we can't reliably
330 * sample with HiZ. One of these days, we should fix that.
331 */
332 if (src_aux_usage == ISL_AUX_USAGE_HIZ)
333 src_aux_usage = ISL_AUX_USAGE_NONE;
334 const bool src_clear_supported =
335 src_aux_usage != ISL_AUX_USAGE_NONE && src_mt->format == src_format;
336 intel_miptree_prepare_access(brw, src_mt, src_level, 1, src_layer, 1,
337 src_aux_usage, src_clear_supported);
338
339 enum isl_format dst_isl_format =
340 brw_blorp_to_isl_format(brw, dst_format, true);
341 enum isl_aux_usage dst_aux_usage =
342 intel_miptree_render_aux_usage(brw, dst_mt, dst_isl_format,
343 false, false);
344 const bool dst_clear_supported = dst_aux_usage != ISL_AUX_USAGE_NONE;
345 intel_miptree_prepare_access(brw, dst_mt, dst_level, 1, dst_layer, 1,
346 dst_aux_usage, dst_clear_supported);
347
348 struct isl_surf tmp_surfs[2];
349 struct blorp_surf src_surf, dst_surf;
350 blorp_surf_for_miptree(brw, &src_surf, src_mt, src_aux_usage, false,
351 &src_level, src_layer, 1, &tmp_surfs[0]);
352 blorp_surf_for_miptree(brw, &dst_surf, dst_mt, dst_aux_usage, true,
353 &dst_level, dst_layer, 1, &tmp_surfs[1]);
354
355 struct isl_swizzle src_isl_swizzle = {
356 .r = swizzle_to_scs(GET_SWZ(src_swizzle, 0)),
357 .g = swizzle_to_scs(GET_SWZ(src_swizzle, 1)),
358 .b = swizzle_to_scs(GET_SWZ(src_swizzle, 2)),
359 .a = swizzle_to_scs(GET_SWZ(src_swizzle, 3)),
360 };
361
362 struct blorp_batch batch;
363 blorp_batch_init(&brw->blorp, &batch, brw, 0);
364 blorp_blit(&batch, &src_surf, src_level, src_layer,
365 src_isl_format, src_isl_swizzle,
366 &dst_surf, dst_level, dst_layer,
367 dst_isl_format, ISL_SWIZZLE_IDENTITY,
368 src_x0, src_y0, src_x1, src_y1,
369 dst_x0, dst_y0, dst_x1, dst_y1,
370 filter, mirror_x, mirror_y);
371 blorp_batch_finish(&batch);
372
373 intel_miptree_finish_write(brw, dst_mt, dst_level, dst_layer, 1,
374 dst_aux_usage);
375 }
376
377 void
378 brw_blorp_copy_miptrees(struct brw_context *brw,
379 struct intel_mipmap_tree *src_mt,
380 unsigned src_level, unsigned src_layer,
381 struct intel_mipmap_tree *dst_mt,
382 unsigned dst_level, unsigned dst_layer,
383 unsigned src_x, unsigned src_y,
384 unsigned dst_x, unsigned dst_y,
385 unsigned src_width, unsigned src_height)
386 {
387 const struct gen_device_info *devinfo = &brw->screen->devinfo;
388
389 DBG("%s from %dx %s mt %p %d %d (%d,%d) %dx%d"
390 "to %dx %s mt %p %d %d (%d,%d)\n",
391 __func__,
392 src_mt->surf.samples, _mesa_get_format_name(src_mt->format), src_mt,
393 src_level, src_layer, src_x, src_y, src_width, src_height,
394 dst_mt->surf.samples, _mesa_get_format_name(dst_mt->format), dst_mt,
395 dst_level, dst_layer, dst_x, dst_y);
396
397 enum isl_aux_usage src_aux_usage, dst_aux_usage;
398 bool src_clear_supported, dst_clear_supported;
399
400 switch (src_mt->aux_usage) {
401 case ISL_AUX_USAGE_MCS:
402 case ISL_AUX_USAGE_CCS_E:
403 src_aux_usage = src_mt->aux_usage;
404 /* Prior to gen9, fast-clear only supported 0/1 clear colors. Since
405 * we're going to re-interpret the format as an integer format possibly
406 * with a different number of components, we can't handle clear colors
407 * until gen9.
408 */
409 src_clear_supported = devinfo->gen >= 9;
410 break;
411 default:
412 src_aux_usage = ISL_AUX_USAGE_NONE;
413 src_clear_supported = false;
414 break;
415 }
416
417 switch (dst_mt->aux_usage) {
418 case ISL_AUX_USAGE_MCS:
419 case ISL_AUX_USAGE_CCS_E:
420 dst_aux_usage = dst_mt->aux_usage;
421 /* Prior to gen9, fast-clear only supported 0/1 clear colors. Since
422 * we're going to re-interpret the format as an integer format possibly
423 * with a different number of components, we can't handle clear colors
424 * until gen9.
425 */
426 dst_clear_supported = devinfo->gen >= 9;
427 break;
428 default:
429 dst_aux_usage = ISL_AUX_USAGE_NONE;
430 dst_clear_supported = false;
431 break;
432 }
433
434 intel_miptree_prepare_access(brw, src_mt, src_level, 1, src_layer, 1,
435 src_aux_usage, src_clear_supported);
436 intel_miptree_prepare_access(brw, dst_mt, dst_level, 1, dst_layer, 1,
437 dst_aux_usage, dst_clear_supported);
438
439 struct isl_surf tmp_surfs[2];
440 struct blorp_surf src_surf, dst_surf;
441 blorp_surf_for_miptree(brw, &src_surf, src_mt, src_aux_usage, false,
442 &src_level, src_layer, 1, &tmp_surfs[0]);
443 blorp_surf_for_miptree(brw, &dst_surf, dst_mt, dst_aux_usage, true,
444 &dst_level, dst_layer, 1, &tmp_surfs[1]);
445
446 /* The hardware seems to have issues with having a two different format
447 * views of the same texture in the sampler cache at the same time. It's
448 * unclear exactly what the issue is but it hurts glCopyImageSubData
449 * particularly badly because it does a lot of format reinterprets. We
450 * badly need better understanding of the issue and a better fix but this
451 * works for now and fixes CTS tests.
452 *
453 * TODO: Remove this hack!
454 */
455 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL |
456 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
457
458 struct blorp_batch batch;
459 blorp_batch_init(&brw->blorp, &batch, brw, 0);
460 blorp_copy(&batch, &src_surf, src_level, src_layer,
461 &dst_surf, dst_level, dst_layer,
462 src_x, src_y, dst_x, dst_y, src_width, src_height);
463 blorp_batch_finish(&batch);
464
465 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL |
466 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
467
468 intel_miptree_finish_write(brw, dst_mt, dst_level, dst_layer, 1,
469 dst_aux_usage);
470 }
471
472 void
473 brw_blorp_copy_buffers(struct brw_context *brw,
474 struct brw_bo *src_bo,
475 unsigned src_offset,
476 struct brw_bo *dst_bo,
477 unsigned dst_offset,
478 unsigned size)
479 {
480 DBG("%s %d bytes from %p[%d] to %p[%d]",
481 __func__, size, src_bo, src_offset, dst_bo, dst_offset);
482
483 struct blorp_batch batch;
484 struct blorp_address src = { .buffer = src_bo, .offset = src_offset };
485 struct blorp_address dst = { .buffer = dst_bo, .offset = dst_offset };
486
487 blorp_batch_init(&brw->blorp, &batch, brw, 0);
488 blorp_buffer_copy(&batch, src, dst, size);
489 blorp_batch_finish(&batch);
490 }
491
492
493 static struct intel_mipmap_tree *
494 find_miptree(GLbitfield buffer_bit, struct intel_renderbuffer *irb)
495 {
496 struct intel_mipmap_tree *mt = irb->mt;
497 if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
498 mt = mt->stencil_mt;
499 return mt;
500 }
501
502 static int
503 blorp_get_texture_swizzle(const struct intel_renderbuffer *irb)
504 {
505 return irb->Base.Base._BaseFormat == GL_RGB ?
506 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE) :
507 SWIZZLE_XYZW;
508 }
509
510 static void
511 do_blorp_blit(struct brw_context *brw, GLbitfield buffer_bit,
512 struct intel_renderbuffer *src_irb, mesa_format src_format,
513 struct intel_renderbuffer *dst_irb, mesa_format dst_format,
514 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
515 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
516 GLenum filter, bool mirror_x, bool mirror_y)
517 {
518 const struct gl_context *ctx = &brw->ctx;
519
520 /* Find source/dst miptrees */
521 struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_irb);
522 struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_irb);
523
524 const bool do_srgb = ctx->Color.sRGBEnabled;
525
526 /* Do the blit */
527 brw_blorp_blit_miptrees(brw,
528 src_mt, src_irb->mt_level, src_irb->mt_layer,
529 src_format, blorp_get_texture_swizzle(src_irb),
530 dst_mt, dst_irb->mt_level, dst_irb->mt_layer,
531 dst_format,
532 srcX0, srcY0, srcX1, srcY1,
533 dstX0, dstY0, dstX1, dstY1,
534 filter, mirror_x, mirror_y,
535 do_srgb, do_srgb);
536
537 dst_irb->need_downsample = true;
538 }
539
540 static bool
541 try_blorp_blit(struct brw_context *brw,
542 const struct gl_framebuffer *read_fb,
543 const struct gl_framebuffer *draw_fb,
544 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
545 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
546 GLenum filter, GLbitfield buffer_bit)
547 {
548 const struct gen_device_info *devinfo = &brw->screen->devinfo;
549 struct gl_context *ctx = &brw->ctx;
550
551 /* Sync up the state of window system buffers. We need to do this before
552 * we go looking for the buffers.
553 */
554 intel_prepare_render(brw);
555
556 bool mirror_x, mirror_y;
557 if (brw_meta_mirror_clip_and_scissor(ctx, read_fb, draw_fb,
558 &srcX0, &srcY0, &srcX1, &srcY1,
559 &dstX0, &dstY0, &dstX1, &dstY1,
560 &mirror_x, &mirror_y))
561 return true;
562
563 /* Find buffers */
564 struct intel_renderbuffer *src_irb;
565 struct intel_renderbuffer *dst_irb;
566 struct intel_mipmap_tree *src_mt;
567 struct intel_mipmap_tree *dst_mt;
568 switch (buffer_bit) {
569 case GL_COLOR_BUFFER_BIT:
570 src_irb = intel_renderbuffer(read_fb->_ColorReadBuffer);
571 for (unsigned i = 0; i < draw_fb->_NumColorDrawBuffers; ++i) {
572 dst_irb = intel_renderbuffer(draw_fb->_ColorDrawBuffers[i]);
573 if (dst_irb)
574 do_blorp_blit(brw, buffer_bit,
575 src_irb, src_irb->Base.Base.Format,
576 dst_irb, dst_irb->Base.Base.Format,
577 srcX0, srcY0, srcX1, srcY1,
578 dstX0, dstY0, dstX1, dstY1,
579 filter, mirror_x, mirror_y);
580 }
581 break;
582 case GL_DEPTH_BUFFER_BIT:
583 src_irb =
584 intel_renderbuffer(read_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
585 dst_irb =
586 intel_renderbuffer(draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
587 src_mt = find_miptree(buffer_bit, src_irb);
588 dst_mt = find_miptree(buffer_bit, dst_irb);
589
590 /* We also can't handle any combined depth-stencil formats because we
591 * have to reinterpret as a color format.
592 */
593 if (_mesa_get_format_base_format(src_mt->format) == GL_DEPTH_STENCIL ||
594 _mesa_get_format_base_format(dst_mt->format) == GL_DEPTH_STENCIL)
595 return false;
596
597 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
598 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
599 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
600 filter, mirror_x, mirror_y);
601 break;
602 case GL_STENCIL_BUFFER_BIT:
603 /* Blorp doesn't support combined depth stencil which is all we have
604 * prior to gen6.
605 */
606 if (devinfo->gen < 6)
607 return false;
608
609 src_irb =
610 intel_renderbuffer(read_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
611 dst_irb =
612 intel_renderbuffer(draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
613 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
614 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
615 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
616 filter, mirror_x, mirror_y);
617 break;
618 default:
619 unreachable("not reached");
620 }
621
622 return true;
623 }
624
625 static void
626 apply_y_flip(int *y0, int *y1, int height)
627 {
628 int tmp = height - *y0;
629 *y0 = height - *y1;
630 *y1 = tmp;
631 }
632
633 bool
634 brw_blorp_copytexsubimage(struct brw_context *brw,
635 struct gl_renderbuffer *src_rb,
636 struct gl_texture_image *dst_image,
637 int slice,
638 int srcX0, int srcY0,
639 int dstX0, int dstY0,
640 int width, int height)
641 {
642 struct gl_context *ctx = &brw->ctx;
643 struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
644 struct intel_texture_image *intel_image = intel_texture_image(dst_image);
645
646 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
647 if (brw->ctx._ImageTransferState)
648 return false;
649
650 /* Sync up the state of window system buffers. We need to do this before
651 * we go looking at the src renderbuffer's miptree.
652 */
653 intel_prepare_render(brw);
654
655 struct intel_mipmap_tree *src_mt = src_irb->mt;
656 struct intel_mipmap_tree *dst_mt = intel_image->mt;
657
658 /* We can't handle any combined depth-stencil formats because we have to
659 * reinterpret as a color format.
660 */
661 if (_mesa_get_format_base_format(src_mt->format) == GL_DEPTH_STENCIL ||
662 _mesa_get_format_base_format(dst_mt->format) == GL_DEPTH_STENCIL)
663 return false;
664
665 if (!brw_blorp_supports_dst_format(brw, dst_image->TexFormat))
666 return false;
667
668 /* Source clipping shouldn't be necessary, since copytexsubimage (in
669 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
670 * takes care of it.
671 *
672 * Destination clipping shouldn't be necessary since the restrictions on
673 * glCopyTexSubImage prevent the user from specifying a destination rectangle
674 * that falls outside the bounds of the destination texture.
675 * See error_check_subtexture_dimensions().
676 */
677
678 int srcY1 = srcY0 + height;
679 int srcX1 = srcX0 + width;
680 int dstX1 = dstX0 + width;
681 int dstY1 = dstY0 + height;
682
683 /* Account for the fact that in the system framebuffer, the origin is at
684 * the lower left.
685 */
686 bool mirror_y = _mesa_is_winsys_fbo(ctx->ReadBuffer);
687 if (mirror_y)
688 apply_y_flip(&srcY0, &srcY1, src_rb->Height);
689
690 /* Account for face selection and texture view MinLayer */
691 int dst_slice = slice + dst_image->TexObject->MinLayer + dst_image->Face;
692 int dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
693
694 brw_blorp_blit_miptrees(brw,
695 src_mt, src_irb->mt_level, src_irb->mt_layer,
696 src_rb->Format, blorp_get_texture_swizzle(src_irb),
697 dst_mt, dst_level, dst_slice,
698 dst_image->TexFormat,
699 srcX0, srcY0, srcX1, srcY1,
700 dstX0, dstY0, dstX1, dstY1,
701 GL_NEAREST, false, mirror_y,
702 false, false);
703
704 /* If we're copying to a packed depth stencil texture and the source
705 * framebuffer has separate stencil, we need to also copy the stencil data
706 * over.
707 */
708 src_rb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
709 if (_mesa_get_format_bits(dst_image->TexFormat, GL_STENCIL_BITS) > 0 &&
710 src_rb != NULL) {
711 src_irb = intel_renderbuffer(src_rb);
712 src_mt = src_irb->mt;
713
714 if (src_mt->stencil_mt)
715 src_mt = src_mt->stencil_mt;
716 if (dst_mt->stencil_mt)
717 dst_mt = dst_mt->stencil_mt;
718
719 if (src_mt != dst_mt) {
720 brw_blorp_blit_miptrees(brw,
721 src_mt, src_irb->mt_level, src_irb->mt_layer,
722 src_mt->format,
723 blorp_get_texture_swizzle(src_irb),
724 dst_mt, dst_level, dst_slice,
725 dst_mt->format,
726 srcX0, srcY0, srcX1, srcY1,
727 dstX0, dstY0, dstX1, dstY1,
728 GL_NEAREST, false, mirror_y,
729 false, false);
730 }
731 }
732
733 return true;
734 }
735
736
737 GLbitfield
738 brw_blorp_framebuffer(struct brw_context *brw,
739 struct gl_framebuffer *readFb,
740 struct gl_framebuffer *drawFb,
741 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
742 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
743 GLbitfield mask, GLenum filter)
744 {
745 static GLbitfield buffer_bits[] = {
746 GL_COLOR_BUFFER_BIT,
747 GL_DEPTH_BUFFER_BIT,
748 GL_STENCIL_BUFFER_BIT,
749 };
750
751 for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
752 if ((mask & buffer_bits[i]) &&
753 try_blorp_blit(brw, readFb, drawFb,
754 srcX0, srcY0, srcX1, srcY1,
755 dstX0, dstY0, dstX1, dstY1,
756 filter, buffer_bits[i])) {
757 mask &= ~buffer_bits[i];
758 }
759 }
760
761 return mask;
762 }
763
764 static struct brw_bo *
765 blorp_get_client_bo(struct brw_context *brw,
766 unsigned w, unsigned h, unsigned d,
767 GLenum target, GLenum format, GLenum type,
768 const void *pixels,
769 const struct gl_pixelstore_attrib *packing,
770 uint32_t *offset_out, uint32_t *row_stride_out,
771 uint32_t *image_stride_out, bool read_only)
772 {
773 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
774 const GLuint dims = _mesa_get_texture_dimensions(target);
775 const uint32_t first_pixel = _mesa_image_offset(dims, packing, w, h,
776 format, type, 0, 0, 0);
777 const uint32_t last_pixel = _mesa_image_offset(dims, packing, w, h,
778 format, type,
779 d - 1, h - 1, w);
780 const uint32_t stride = _mesa_image_row_stride(packing, w, format, type);
781 const uint32_t cpp = _mesa_bytes_per_pixel(format, type);
782 const uint32_t size = last_pixel - first_pixel;
783
784 *row_stride_out = stride;
785 *image_stride_out = _mesa_image_image_stride(packing, w, h, format, type);
786
787 if (_mesa_is_bufferobj(packing->BufferObj)) {
788 const uint32_t offset = first_pixel + (intptr_t)pixels;
789 if (!read_only && ((offset % cpp) || (stride % cpp))) {
790 perf_debug("Bad PBO alignment; fallback to CPU mapping\n");
791 return NULL;
792 }
793
794 /* This is a user-provided PBO. We just need to get the BO out */
795 struct intel_buffer_object *intel_pbo =
796 intel_buffer_object(packing->BufferObj);
797 struct brw_bo *bo =
798 intel_bufferobj_buffer(brw, intel_pbo, offset, size, !read_only);
799
800 /* We take a reference to the BO so that the caller can just always
801 * unref without having to worry about whether it's a user PBO or one
802 * we created.
803 */
804 brw_bo_reference(bo);
805
806 *offset_out = offset;
807 return bo;
808 } else {
809 /* Someone should have already checked that there is data to upload. */
810 assert(pixels);
811
812 /* Creating a temp buffer currently only works for upload */
813 assert(read_only);
814
815 /* This is not a user-provided PBO. Instead, pixels is a pointer to CPU
816 * data which we need to copy into a BO.
817 */
818 struct brw_bo *bo =
819 brw_bo_alloc(brw->bufmgr, "tmp_tex_subimage_src", size);
820 if (bo == NULL) {
821 perf_debug("intel_texsubimage: temp bo creation failed: size = %u\n",
822 size);
823 return NULL;
824 }
825
826 if (brw_bo_subdata(bo, 0, size, pixels + first_pixel)) {
827 perf_debug("intel_texsubimage: temp bo upload failed\n");
828 brw_bo_unreference(bo);
829 return NULL;
830 }
831
832 *offset_out = 0;
833 return bo;
834 }
835 }
836
837 /* Consider all the restrictions and determine the format of the source. */
838 static mesa_format
839 blorp_get_client_format(struct brw_context *brw,
840 GLenum format, GLenum type,
841 const struct gl_pixelstore_attrib *packing)
842 {
843 if (brw->ctx._ImageTransferState)
844 return MESA_FORMAT_NONE;
845
846 if (packing->SwapBytes || packing->LsbFirst || packing->Invert) {
847 perf_debug("intel_texsubimage_blorp: unsupported gl_pixelstore_attrib\n");
848 return MESA_FORMAT_NONE;
849 }
850
851 if (format != GL_RED &&
852 format != GL_RG &&
853 format != GL_RGB &&
854 format != GL_BGR &&
855 format != GL_RGBA &&
856 format != GL_BGRA &&
857 format != GL_ALPHA &&
858 format != GL_RED_INTEGER &&
859 format != GL_RG_INTEGER &&
860 format != GL_RGB_INTEGER &&
861 format != GL_BGR_INTEGER &&
862 format != GL_RGBA_INTEGER &&
863 format != GL_BGRA_INTEGER) {
864 perf_debug("intel_texsubimage_blorp: %s not supported",
865 _mesa_enum_to_string(format));
866 return MESA_FORMAT_NONE;
867 }
868
869 return _mesa_tex_format_from_format_and_type(&brw->ctx, format, type);
870 }
871
872 static bool
873 need_signed_unsigned_int_conversion(mesa_format src_format,
874 mesa_format dst_format)
875 {
876 const GLenum src_type = _mesa_get_format_datatype(src_format);
877 const GLenum dst_type = _mesa_get_format_datatype(dst_format);
878 return (src_type == GL_INT && dst_type == GL_UNSIGNED_INT) ||
879 (src_type == GL_UNSIGNED_INT && dst_type == GL_INT);
880 }
881
882 bool
883 brw_blorp_upload_miptree(struct brw_context *brw,
884 struct intel_mipmap_tree *dst_mt,
885 mesa_format dst_format,
886 uint32_t level, uint32_t x, uint32_t y, uint32_t z,
887 uint32_t width, uint32_t height, uint32_t depth,
888 GLenum target, GLenum format, GLenum type,
889 const void *pixels,
890 const struct gl_pixelstore_attrib *packing)
891 {
892 const mesa_format src_format =
893 blorp_get_client_format(brw, format, type, packing);
894 if (src_format == MESA_FORMAT_NONE)
895 return false;
896
897 if (!brw->mesa_format_supports_render[dst_format]) {
898 perf_debug("intel_texsubimage: can't use %s as render target\n",
899 _mesa_get_format_name(dst_format));
900 return false;
901 }
902
903 /* This function relies on blorp_blit to upload the pixel data to the
904 * miptree. But, blorp_blit doesn't support signed to unsigned or
905 * unsigned to signed integer conversions.
906 */
907 if (need_signed_unsigned_int_conversion(src_format, dst_format))
908 return false;
909
910 uint32_t src_offset, src_row_stride, src_image_stride;
911 struct brw_bo *src_bo =
912 blorp_get_client_bo(brw, width, height, depth,
913 target, format, type, pixels, packing,
914 &src_offset, &src_row_stride,
915 &src_image_stride, true);
916 if (src_bo == NULL)
917 return false;
918
919 /* Now that source is offset to correct starting point, adjust the
920 * given dimensions to treat 1D arrays as 2D.
921 */
922 if (target == GL_TEXTURE_1D_ARRAY) {
923 assert(depth == 1);
924 assert(z == 0);
925 depth = height;
926 height = 1;
927 z = y;
928 y = 0;
929 src_image_stride = src_row_stride;
930 }
931
932 intel_miptree_check_level_layer(dst_mt, level, z + depth - 1);
933
934 bool result = false;
935
936 /* Blit slice-by-slice creating a single-slice miptree for each layer. Even
937 * in case of linear buffers hardware wants image arrays to be aligned by
938 * four rows. This way hardware only gets one image at a time and any
939 * source alignment will do.
940 */
941 for (unsigned i = 0; i < depth; ++i) {
942 struct intel_mipmap_tree *src_mt = intel_miptree_create_for_bo(
943 brw, src_bo, src_format,
944 src_offset + i * src_image_stride,
945 width, height, 1,
946 src_row_stride,
947 ISL_TILING_LINEAR, 0);
948
949 if (!src_mt) {
950 perf_debug("intel_texsubimage: miptree creation for src failed\n");
951 goto err;
952 }
953
954 /* In case exact match is needed, copy using equivalent UINT formats
955 * preventing hardware from changing presentation for SNORM -1.
956 */
957 if (src_mt->format == dst_format) {
958 brw_blorp_copy_miptrees(brw, src_mt, 0, 0,
959 dst_mt, level, z + i,
960 0, 0, x, y, width, height);
961 } else {
962 brw_blorp_blit_miptrees(brw, src_mt, 0, 0,
963 src_format, SWIZZLE_XYZW,
964 dst_mt, level, z + i,
965 dst_format,
966 0, 0, width, height,
967 x, y, x + width, y + height,
968 GL_NEAREST, false, false, false, false);
969 }
970
971 intel_miptree_release(&src_mt);
972 }
973
974 result = true;
975
976 err:
977 brw_bo_unreference(src_bo);
978
979 return result;
980 }
981
982 bool
983 brw_blorp_download_miptree(struct brw_context *brw,
984 struct intel_mipmap_tree *src_mt,
985 mesa_format src_format, uint32_t src_swizzle,
986 uint32_t level, uint32_t x, uint32_t y, uint32_t z,
987 uint32_t width, uint32_t height, uint32_t depth,
988 GLenum target, GLenum format, GLenum type,
989 bool y_flip, const void *pixels,
990 const struct gl_pixelstore_attrib *packing)
991 {
992 const mesa_format dst_format =
993 blorp_get_client_format(brw, format, type, packing);
994 if (dst_format == MESA_FORMAT_NONE)
995 return false;
996
997 if (!brw->mesa_format_supports_render[dst_format]) {
998 perf_debug("intel_texsubimage: can't use %s as render target\n",
999 _mesa_get_format_name(dst_format));
1000 return false;
1001 }
1002
1003 /* This function relies on blorp_blit to download the pixel data from the
1004 * miptree. But, blorp_blit doesn't support signed to unsigned or unsigned
1005 * to signed integer conversions.
1006 */
1007 if (need_signed_unsigned_int_conversion(src_format, dst_format))
1008 return false;
1009
1010 /* We can't fetch from LUMINANCE or intensity as that would require a
1011 * non-trivial swizzle.
1012 */
1013 switch (_mesa_get_format_base_format(src_format)) {
1014 case GL_LUMINANCE:
1015 case GL_LUMINANCE_ALPHA:
1016 case GL_INTENSITY:
1017 return false;
1018 default:
1019 break;
1020 }
1021
1022 /* This pass only works for PBOs */
1023 assert(_mesa_is_bufferobj(packing->BufferObj));
1024
1025 uint32_t dst_offset, dst_row_stride, dst_image_stride;
1026 struct brw_bo *dst_bo =
1027 blorp_get_client_bo(brw, width, height, depth,
1028 target, format, type, pixels, packing,
1029 &dst_offset, &dst_row_stride,
1030 &dst_image_stride, false);
1031 if (dst_bo == NULL)
1032 return false;
1033
1034 /* Now that source is offset to correct starting point, adjust the
1035 * given dimensions to treat 1D arrays as 2D.
1036 */
1037 if (target == GL_TEXTURE_1D_ARRAY) {
1038 assert(depth == 1);
1039 assert(z == 0);
1040 depth = height;
1041 height = 1;
1042 z = y;
1043 y = 0;
1044 dst_image_stride = dst_row_stride;
1045 }
1046
1047 intel_miptree_check_level_layer(src_mt, level, z + depth - 1);
1048
1049 int y0 = y;
1050 int y1 = y + height;
1051 if (y_flip) {
1052 apply_y_flip(&y0, &y1, minify(src_mt->surf.phys_level0_sa.height,
1053 level - src_mt->first_level));
1054 }
1055
1056 bool result = false;
1057
1058 /* Blit slice-by-slice creating a single-slice miptree for each layer. Even
1059 * in case of linear buffers hardware wants image arrays to be aligned by
1060 * four rows. This way hardware only gets one image at a time and any
1061 * source alignment will do.
1062 */
1063 for (unsigned i = 0; i < depth; ++i) {
1064 struct intel_mipmap_tree *dst_mt = intel_miptree_create_for_bo(
1065 brw, dst_bo, dst_format,
1066 dst_offset + i * dst_image_stride,
1067 width, height, 1,
1068 dst_row_stride,
1069 ISL_TILING_LINEAR, 0);
1070
1071 if (!dst_mt) {
1072 perf_debug("intel_texsubimage: miptree creation for src failed\n");
1073 goto err;
1074 }
1075
1076 /* In case exact match is needed, copy using equivalent UINT formats
1077 * preventing hardware from changing presentation for SNORM -1.
1078 */
1079 if (dst_mt->format == src_format && !y_flip &&
1080 src_swizzle == SWIZZLE_XYZW) {
1081 brw_blorp_copy_miptrees(brw, src_mt, level, z + i,
1082 dst_mt, 0, 0,
1083 x, y, 0, 0, width, height);
1084 } else {
1085 brw_blorp_blit_miptrees(brw, src_mt, level, z + i,
1086 src_format, src_swizzle,
1087 dst_mt, 0, 0, dst_format,
1088 x, y0, x + width, y1,
1089 0, 0, width, height,
1090 GL_NEAREST, false, y_flip, false, false);
1091 }
1092
1093 intel_miptree_release(&dst_mt);
1094 }
1095
1096 result = true;
1097
1098 /* As we implement PBO transfers by binding the user-provided BO as a
1099 * fake framebuffer and rendering to it. This breaks the invariant of the
1100 * GL that nothing is able to render to a BO, causing nondeterministic
1101 * corruption issues because the render cache is not coherent with a
1102 * number of other caches that the BO could potentially be bound to
1103 * afterwards.
1104 *
1105 * This could be solved in the same way that we guarantee texture
1106 * coherency after a texture is attached to a framebuffer and
1107 * rendered to, but that would involve checking *all* BOs bound to
1108 * the pipeline for the case we need to emit a cache flush due to
1109 * previous rendering to any of them -- Including vertex, index,
1110 * uniform, atomic counter, shader image, transform feedback,
1111 * indirect draw buffers, etc.
1112 *
1113 * That would increase the per-draw call overhead even though it's
1114 * very unlikely that any of the BOs bound to the pipeline has been
1115 * rendered to via a PBO at any point, so it seems better to just
1116 * flush here unconditionally.
1117 */
1118 brw_emit_mi_flush(brw);
1119
1120 err:
1121 brw_bo_unreference(dst_bo);
1122
1123 return result;
1124 }
1125
1126 static bool
1127 set_write_disables(const struct intel_renderbuffer *irb,
1128 const unsigned color_mask, bool *color_write_disable)
1129 {
1130 /* Format information in the renderbuffer represents the requirements
1131 * given by the client. There are cases where the backing miptree uses,
1132 * for example, RGBA to represent RGBX. Since the client is only expecting
1133 * RGB we can treat alpha as not used and write whatever we like into it.
1134 */
1135 const GLenum base_format = irb->Base.Base._BaseFormat;
1136 const int components = _mesa_base_format_component_count(base_format);
1137 bool disables = false;
1138
1139 assert(components > 0);
1140
1141 for (int i = 0; i < components; i++) {
1142 color_write_disable[i] = !(color_mask & (1 << i));
1143 disables = disables || color_write_disable[i];
1144 }
1145
1146 return disables;
1147 }
1148
1149 static void
1150 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
1151 struct gl_renderbuffer *rb, unsigned buf,
1152 bool partial_clear, bool encode_srgb)
1153 {
1154 struct gl_context *ctx = &brw->ctx;
1155 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
1156 uint32_t x0, x1, y0, y1;
1157
1158 mesa_format format = irb->Base.Base.Format;
1159 if (!encode_srgb && _mesa_get_format_color_encoding(format) == GL_SRGB)
1160 format = _mesa_get_srgb_format_linear(format);
1161 enum isl_format isl_format = brw->mesa_to_isl_render_format[format];
1162
1163 x0 = fb->_Xmin;
1164 x1 = fb->_Xmax;
1165 if (rb->Name != 0) {
1166 y0 = fb->_Ymin;
1167 y1 = fb->_Ymax;
1168 } else {
1169 y0 = rb->Height - fb->_Ymax;
1170 y1 = rb->Height - fb->_Ymin;
1171 }
1172
1173 /* If the clear region is empty, just return. */
1174 if (x0 == x1 || y0 == y1)
1175 return;
1176
1177 bool can_fast_clear = !partial_clear;
1178
1179 bool color_write_disable[4] = { false, false, false, false };
1180 if (set_write_disables(irb, GET_COLORMASK(ctx->Color.ColorMask, buf),
1181 color_write_disable))
1182 can_fast_clear = false;
1183
1184 /* We store clear colors as floats or uints as needed. If there are
1185 * texture views in play, the formats will not properly be respected
1186 * during resolves because the resolve operations only know about the
1187 * miptree and not the renderbuffer.
1188 */
1189 if (irb->Base.Base.Format != irb->mt->format)
1190 can_fast_clear = false;
1191
1192 if (!irb->mt->supports_fast_clear ||
1193 !brw_is_color_fast_clear_compatible(brw, irb->mt, &ctx->Color.ClearColor))
1194 can_fast_clear = false;
1195
1196 /* Surface state can only record one fast clear color value. Therefore
1197 * unless different levels/layers agree on the color it can be used to
1198 * represent only single level/layer. Here it will be reserved for the
1199 * first slice (level 0, layer 0).
1200 */
1201 if (irb->layer_count > 1 || irb->mt_level || irb->mt_layer)
1202 can_fast_clear = false;
1203
1204 unsigned level = irb->mt_level;
1205 const unsigned num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
1206
1207 /* If the MCS buffer hasn't been allocated yet, we need to allocate it now.
1208 */
1209 if (can_fast_clear && !irb->mt->aux_buf) {
1210 assert(irb->mt->aux_usage == ISL_AUX_USAGE_CCS_D);
1211 if (!intel_miptree_alloc_aux(brw, irb->mt)) {
1212 /* There are a few reasons in addition to out-of-memory, that can
1213 * cause intel_miptree_alloc_non_msrt_mcs to fail. Try to recover by
1214 * falling back to non-fast clear.
1215 */
1216 can_fast_clear = false;
1217 }
1218 }
1219
1220 /* FINISHME: Debug and enable fast clears */
1221 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1222 if (devinfo->gen >= 11)
1223 can_fast_clear = false;
1224
1225 if (can_fast_clear) {
1226 const enum isl_aux_state aux_state =
1227 intel_miptree_get_aux_state(irb->mt, irb->mt_level, irb->mt_layer);
1228 union isl_color_value clear_color =
1229 brw_meta_convert_fast_clear_color(brw, irb->mt,
1230 &ctx->Color.ClearColor);
1231
1232 intel_miptree_set_clear_color(brw, irb->mt, clear_color);
1233
1234 /* If the buffer is already in ISL_AUX_STATE_CLEAR, the clear
1235 * is redundant and can be skipped.
1236 */
1237 if (aux_state == ISL_AUX_STATE_CLEAR)
1238 return;
1239
1240 DBG("%s (fast) to mt %p level %d layers %d+%d\n", __FUNCTION__,
1241 irb->mt, irb->mt_level, irb->mt_layer, num_layers);
1242
1243 /* We can't setup the blorp_surf until we've allocated the MCS above */
1244 struct isl_surf isl_tmp[2];
1245 struct blorp_surf surf;
1246 blorp_surf_for_miptree(brw, &surf, irb->mt, irb->mt->aux_usage, true,
1247 &level, irb->mt_layer, num_layers, isl_tmp);
1248
1249 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
1250 *
1251 * "Any transition from any value in {Clear, Render, Resolve} to a
1252 * different value in {Clear, Render, Resolve} requires end of pipe
1253 * synchronization."
1254 *
1255 * In other words, fast clear ops are not properly synchronized with
1256 * other drawing. We need to use a PIPE_CONTROL to ensure that the
1257 * contents of the previous draw hit the render target before we resolve
1258 * and again afterwards to ensure that the resolve is complete before we
1259 * do any more regular drawing.
1260 */
1261 brw_emit_end_of_pipe_sync(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH);
1262
1263 struct blorp_batch batch;
1264 blorp_batch_init(&brw->blorp, &batch, brw,
1265 BLORP_BATCH_NO_UPDATE_CLEAR_COLOR);
1266 blorp_fast_clear(&batch, &surf, isl_format,
1267 level, irb->mt_layer, num_layers,
1268 x0, y0, x1, y1);
1269 blorp_batch_finish(&batch);
1270
1271 brw_emit_end_of_pipe_sync(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH);
1272
1273 /* Now that the fast clear has occurred, put the buffer in
1274 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
1275 * redundant clears.
1276 */
1277 intel_miptree_set_aux_state(brw, irb->mt, irb->mt_level,
1278 irb->mt_layer, num_layers,
1279 ISL_AUX_STATE_CLEAR);
1280 } else {
1281 DBG("%s (slow) to mt %p level %d layer %d+%d\n", __FUNCTION__,
1282 irb->mt, irb->mt_level, irb->mt_layer, num_layers);
1283
1284 enum isl_aux_usage aux_usage =
1285 intel_miptree_render_aux_usage(brw, irb->mt, isl_format,
1286 false, false);
1287 intel_miptree_prepare_render(brw, irb->mt, level, irb->mt_layer,
1288 num_layers, aux_usage);
1289
1290 struct isl_surf isl_tmp[2];
1291 struct blorp_surf surf;
1292 blorp_surf_for_miptree(brw, &surf, irb->mt, aux_usage, true,
1293 &level, irb->mt_layer, num_layers, isl_tmp);
1294
1295 union isl_color_value clear_color;
1296 memcpy(clear_color.f32, ctx->Color.ClearColor.f, sizeof(float) * 4);
1297
1298 struct blorp_batch batch;
1299 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1300 blorp_clear(&batch, &surf, isl_format, ISL_SWIZZLE_IDENTITY,
1301 level, irb->mt_layer, num_layers,
1302 x0, y0, x1, y1,
1303 clear_color, color_write_disable);
1304 blorp_batch_finish(&batch);
1305
1306 intel_miptree_finish_render(brw, irb->mt, level, irb->mt_layer,
1307 num_layers, aux_usage);
1308 }
1309
1310 return;
1311 }
1312
1313 void
1314 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
1315 GLbitfield mask, bool partial_clear, bool encode_srgb)
1316 {
1317 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
1318 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
1319 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
1320
1321 /* Only clear the buffers present in the provided mask */
1322 if (((1 << fb->_ColorDrawBufferIndexes[buf]) & mask) == 0)
1323 continue;
1324
1325 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
1326 * the framebuffer can be complete with some attachments missing. In
1327 * this case the _ColorDrawBuffers pointer will be NULL.
1328 */
1329 if (rb == NULL)
1330 continue;
1331
1332 do_single_blorp_clear(brw, fb, rb, buf, partial_clear, encode_srgb);
1333 irb->need_downsample = true;
1334 }
1335
1336 return;
1337 }
1338
1339 void
1340 brw_blorp_clear_depth_stencil(struct brw_context *brw,
1341 struct gl_framebuffer *fb,
1342 GLbitfield mask, bool partial_clear)
1343 {
1344 const struct gl_context *ctx = &brw->ctx;
1345 struct gl_renderbuffer *depth_rb =
1346 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
1347 struct gl_renderbuffer *stencil_rb =
1348 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
1349
1350 if (!depth_rb || ctx->Depth.Mask == GL_FALSE)
1351 mask &= ~BUFFER_BIT_DEPTH;
1352
1353 if (!stencil_rb || (ctx->Stencil.WriteMask[0] & 0xff) == 0)
1354 mask &= ~BUFFER_BIT_STENCIL;
1355
1356 if (!(mask & (BUFFER_BITS_DEPTH_STENCIL)))
1357 return;
1358
1359 uint32_t x0, x1, y0, y1, rb_name, rb_height;
1360 if (depth_rb) {
1361 rb_name = depth_rb->Name;
1362 rb_height = depth_rb->Height;
1363 if (stencil_rb) {
1364 assert(depth_rb->Width == stencil_rb->Width);
1365 assert(depth_rb->Height == stencil_rb->Height);
1366 }
1367 } else {
1368 assert(stencil_rb);
1369 rb_name = stencil_rb->Name;
1370 rb_height = stencil_rb->Height;
1371 }
1372
1373 x0 = fb->_Xmin;
1374 x1 = fb->_Xmax;
1375 if (rb_name != 0) {
1376 y0 = fb->_Ymin;
1377 y1 = fb->_Ymax;
1378 } else {
1379 y0 = rb_height - fb->_Ymax;
1380 y1 = rb_height - fb->_Ymin;
1381 }
1382
1383 /* If the clear region is empty, just return. */
1384 if (x0 == x1 || y0 == y1)
1385 return;
1386
1387 uint32_t level, start_layer, num_layers;
1388 struct isl_surf isl_tmp[4];
1389 struct blorp_surf depth_surf, stencil_surf;
1390
1391 struct intel_mipmap_tree *depth_mt = NULL;
1392 if (mask & BUFFER_BIT_DEPTH) {
1393 struct intel_renderbuffer *irb = intel_renderbuffer(depth_rb);
1394 depth_mt = find_miptree(GL_DEPTH_BUFFER_BIT, irb);
1395
1396 level = irb->mt_level;
1397 start_layer = irb->mt_layer;
1398 num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
1399
1400 intel_miptree_prepare_depth(brw, depth_mt, level,
1401 start_layer, num_layers);
1402
1403 unsigned depth_level = level;
1404 blorp_surf_for_miptree(brw, &depth_surf, depth_mt, depth_mt->aux_usage,
1405 true, &depth_level, start_layer, num_layers,
1406 &isl_tmp[0]);
1407 assert(depth_level == level);
1408 }
1409
1410 uint8_t stencil_mask = 0;
1411 struct intel_mipmap_tree *stencil_mt = NULL;
1412 if (mask & BUFFER_BIT_STENCIL) {
1413 struct intel_renderbuffer *irb = intel_renderbuffer(stencil_rb);
1414 stencil_mt = find_miptree(GL_STENCIL_BUFFER_BIT, irb);
1415
1416 if (mask & BUFFER_BIT_DEPTH) {
1417 assert(level == irb->mt_level);
1418 assert(start_layer == irb->mt_layer);
1419 assert(num_layers == fb->MaxNumLayers ? irb->layer_count : 1);
1420 } else {
1421 level = irb->mt_level;
1422 start_layer = irb->mt_layer;
1423 }
1424 num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
1425
1426 stencil_mask = ctx->Stencil.WriteMask[0] & 0xff;
1427
1428 intel_miptree_prepare_access(brw, stencil_mt, level, 1,
1429 start_layer, num_layers,
1430 ISL_AUX_USAGE_NONE, false);
1431
1432 unsigned stencil_level = level;
1433 blorp_surf_for_miptree(brw, &stencil_surf, stencil_mt,
1434 ISL_AUX_USAGE_NONE, true,
1435 &stencil_level, start_layer, num_layers,
1436 &isl_tmp[2]);
1437 }
1438
1439 assert((mask & BUFFER_BIT_DEPTH) || stencil_mask);
1440
1441 struct blorp_batch batch;
1442 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1443 blorp_clear_depth_stencil(&batch, &depth_surf, &stencil_surf,
1444 level, start_layer, num_layers,
1445 x0, y0, x1, y1,
1446 (mask & BUFFER_BIT_DEPTH), ctx->Depth.Clear,
1447 stencil_mask, ctx->Stencil.Clear);
1448 blorp_batch_finish(&batch);
1449
1450 if (mask & BUFFER_BIT_DEPTH) {
1451 intel_miptree_finish_depth(brw, depth_mt, level,
1452 start_layer, num_layers, true);
1453 }
1454
1455 if (stencil_mask) {
1456 intel_miptree_finish_write(brw, stencil_mt, level,
1457 start_layer, num_layers,
1458 ISL_AUX_USAGE_NONE);
1459 }
1460 }
1461
1462 void
1463 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt,
1464 unsigned level, unsigned layer,
1465 enum isl_aux_op resolve_op)
1466 {
1467 DBG("%s to mt %p level %u layer %u\n", __FUNCTION__, mt, level, layer);
1468
1469 const mesa_format format = _mesa_get_srgb_format_linear(mt->format);
1470
1471 struct isl_surf isl_tmp[1];
1472 struct blorp_surf surf;
1473 blorp_surf_for_miptree(brw, &surf, mt, mt->aux_usage, true,
1474 &level, layer, 1 /* num_layers */,
1475 isl_tmp);
1476
1477 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
1478 *
1479 * "Any transition from any value in {Clear, Render, Resolve} to a
1480 * different value in {Clear, Render, Resolve} requires end of pipe
1481 * synchronization."
1482 *
1483 * In other words, fast clear ops are not properly synchronized with
1484 * other drawing. We need to use a PIPE_CONTROL to ensure that the
1485 * contents of the previous draw hit the render target before we resolve
1486 * and again afterwards to ensure that the resolve is complete before we
1487 * do any more regular drawing.
1488 */
1489 brw_emit_end_of_pipe_sync(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH);
1490
1491
1492 struct blorp_batch batch;
1493 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1494 blorp_ccs_resolve(&batch, &surf, level, layer, 1,
1495 brw_blorp_to_isl_format(brw, format, true),
1496 resolve_op);
1497 blorp_batch_finish(&batch);
1498
1499 /* See comment above */
1500 brw_emit_end_of_pipe_sync(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH);
1501 }
1502
1503 void
1504 brw_blorp_mcs_partial_resolve(struct brw_context *brw,
1505 struct intel_mipmap_tree *mt,
1506 uint32_t start_layer, uint32_t num_layers)
1507 {
1508 DBG("%s to mt %p layers %u-%u\n", __FUNCTION__, mt,
1509 start_layer, start_layer + num_layers - 1);
1510
1511 assert(mt->aux_usage == ISL_AUX_USAGE_MCS);
1512
1513 const mesa_format format = _mesa_get_srgb_format_linear(mt->format);
1514 enum isl_format isl_format = brw_blorp_to_isl_format(brw, format, true);
1515
1516 struct isl_surf isl_tmp[1];
1517 struct blorp_surf surf;
1518 uint32_t level = 0;
1519 blorp_surf_for_miptree(brw, &surf, mt, ISL_AUX_USAGE_MCS, true,
1520 &level, start_layer, num_layers, isl_tmp);
1521
1522 struct blorp_batch batch;
1523 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1524 blorp_mcs_partial_resolve(&batch, &surf, isl_format,
1525 start_layer, num_layers);
1526 blorp_batch_finish(&batch);
1527 }
1528
1529 /**
1530 * Perform a HiZ or depth resolve operation.
1531 *
1532 * For an overview of HiZ ops, see the following sections of the Sandy Bridge
1533 * PRM, Volume 1, Part 2:
1534 * - 7.5.3.1 Depth Buffer Clear
1535 * - 7.5.3.2 Depth Buffer Resolve
1536 * - 7.5.3.3 Hierarchical Depth Buffer Resolve
1537 */
1538 void
1539 intel_hiz_exec(struct brw_context *brw, struct intel_mipmap_tree *mt,
1540 unsigned int level, unsigned int start_layer,
1541 unsigned int num_layers, enum isl_aux_op op)
1542 {
1543 assert(intel_miptree_level_has_hiz(mt, level));
1544 assert(op != ISL_AUX_OP_NONE);
1545 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1546 const char *opname = NULL;
1547
1548 switch (op) {
1549 case ISL_AUX_OP_FULL_RESOLVE:
1550 opname = "depth resolve";
1551 break;
1552 case ISL_AUX_OP_AMBIGUATE:
1553 opname = "hiz ambiguate";
1554 break;
1555 case ISL_AUX_OP_FAST_CLEAR:
1556 opname = "depth clear";
1557 break;
1558 case ISL_AUX_OP_PARTIAL_RESOLVE:
1559 case ISL_AUX_OP_NONE:
1560 unreachable("Invalid HiZ op");
1561 }
1562
1563 DBG("%s %s to mt %p level %d layers %d-%d\n",
1564 __func__, opname, mt, level, start_layer, start_layer + num_layers - 1);
1565
1566 /* The following stalls and flushes are only documented to be required for
1567 * HiZ clear operations. However, they also seem to be required for
1568 * resolve operations.
1569 */
1570 if (devinfo->gen == 6) {
1571 /* From the Sandy Bridge PRM, volume 2 part 1, page 313:
1572 *
1573 * "If other rendering operations have preceded this clear, a
1574 * PIPE_CONTROL with write cache flush enabled and Z-inhibit
1575 * disabled must be issued before the rectangle primitive used for
1576 * the depth buffer clear operation.
1577 */
1578 brw_emit_pipe_control_flush(brw,
1579 PIPE_CONTROL_RENDER_TARGET_FLUSH |
1580 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
1581 PIPE_CONTROL_CS_STALL);
1582 } else if (devinfo->gen >= 7) {
1583 /*
1584 * From the Ivybridge PRM, volume 2, "Depth Buffer Clear":
1585 *
1586 * If other rendering operations have preceded this clear, a
1587 * PIPE_CONTROL with depth cache flush enabled, Depth Stall bit
1588 * enabled must be issued before the rectangle primitive used for
1589 * the depth buffer clear operation.
1590 *
1591 * Same applies for Gen8 and Gen9.
1592 *
1593 * In addition, from the Ivybridge PRM, volume 2, 1.10.4.1
1594 * PIPE_CONTROL, Depth Cache Flush Enable:
1595 *
1596 * This bit must not be set when Depth Stall Enable bit is set in
1597 * this packet.
1598 *
1599 * This is confirmed to hold for real, HSW gets immediate gpu hangs.
1600 *
1601 * Therefore issue two pipe control flushes, one for cache flush and
1602 * another for depth stall.
1603 */
1604 brw_emit_pipe_control_flush(brw,
1605 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
1606 PIPE_CONTROL_CS_STALL);
1607
1608 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_DEPTH_STALL);
1609 }
1610
1611 assert(mt->aux_usage == ISL_AUX_USAGE_HIZ && mt->aux_buf);
1612
1613 struct isl_surf isl_tmp[2];
1614 struct blorp_surf surf;
1615 blorp_surf_for_miptree(brw, &surf, mt, ISL_AUX_USAGE_HIZ, true,
1616 &level, start_layer, num_layers, isl_tmp);
1617
1618 struct blorp_batch batch;
1619 blorp_batch_init(&brw->blorp, &batch, brw,
1620 BLORP_BATCH_NO_UPDATE_CLEAR_COLOR);
1621 blorp_hiz_op(&batch, &surf, level, start_layer, num_layers, op);
1622 blorp_batch_finish(&batch);
1623
1624 /* The following stalls and flushes are only documented to be required for
1625 * HiZ clear operations. However, they also seem to be required for
1626 * resolve operations.
1627 */
1628 if (devinfo->gen == 6) {
1629 /* From the Sandy Bridge PRM, volume 2 part 1, page 314:
1630 *
1631 * "DevSNB, DevSNB-B{W/A}]: Depth buffer clear pass must be
1632 * followed by a PIPE_CONTROL command with DEPTH_STALL bit set
1633 * and Then followed by Depth FLUSH'
1634 */
1635 brw_emit_pipe_control_flush(brw,
1636 PIPE_CONTROL_DEPTH_STALL);
1637
1638 brw_emit_pipe_control_flush(brw,
1639 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
1640 PIPE_CONTROL_CS_STALL);
1641 } else if (devinfo->gen >= 8) {
1642 /*
1643 * From the Broadwell PRM, volume 7, "Depth Buffer Clear":
1644 *
1645 * "Depth buffer clear pass using any of the methods (WM_STATE,
1646 * 3DSTATE_WM or 3DSTATE_WM_HZ_OP) must be followed by a
1647 * PIPE_CONTROL command with DEPTH_STALL bit and Depth FLUSH bits
1648 * "set" before starting to render. DepthStall and DepthFlush are
1649 * not needed between consecutive depth clear passes nor is it
1650 * required if the depth clear pass was done with
1651 * 'full_surf_clear' bit set in the 3DSTATE_WM_HZ_OP."
1652 *
1653 * TODO: Such as the spec says, this could be conditional.
1654 */
1655 brw_emit_pipe_control_flush(brw,
1656 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
1657 PIPE_CONTROL_DEPTH_STALL);
1658
1659 }
1660 }