i965: Add brw_populate_default_key
[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 .tile_x_sa = mt->level[*level].level_x,
156 .tile_y_sa = mt->level[*level].level_y,
157 };
158
159 if (mt->format == MESA_FORMAT_S_UINT8 && is_render_target &&
160 devinfo->gen <= 7)
161 mt->r8stencil_needs_update = true;
162
163 if (surf->aux_usage == ISL_AUX_USAGE_HIZ &&
164 !intel_miptree_level_has_hiz(mt, *level))
165 surf->aux_usage = ISL_AUX_USAGE_NONE;
166
167 if (surf->aux_usage != ISL_AUX_USAGE_NONE) {
168 /* We only really need a clear color if we also have an auxiliary
169 * surface. Without one, it does nothing.
170 */
171 surf->clear_color =
172 intel_miptree_get_clear_color(devinfo, mt, mt->surf.format,
173 !is_render_target, (struct brw_bo **)
174 &surf->clear_color_addr.buffer,
175 &surf->clear_color_addr.offset);
176
177 surf->aux_surf = &mt->aux_buf->surf;
178 surf->aux_addr = (struct blorp_address) {
179 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
180 .mocs = surf->addr.mocs,
181 };
182
183 surf->aux_addr.buffer = mt->aux_buf->bo;
184 surf->aux_addr.offset = mt->aux_buf->offset;
185 } else {
186 surf->aux_addr = (struct blorp_address) {
187 .buffer = NULL,
188 };
189 memset(&surf->clear_color, 0, sizeof(surf->clear_color));
190 }
191 assert((surf->aux_usage == ISL_AUX_USAGE_NONE) ==
192 (surf->aux_addr.buffer == NULL));
193
194 /* ISL wants real levels, not offset ones. */
195 *level -= mt->first_level;
196 }
197
198 static bool
199 brw_blorp_supports_dst_format(struct brw_context *brw, mesa_format format)
200 {
201 /* If it's renderable, it's definitely supported. */
202 if (brw->mesa_format_supports_render[format])
203 return true;
204
205 /* BLORP can't compress anything */
206 if (_mesa_is_format_compressed(format))
207 return false;
208
209 /* No exotic formats such as GL_LUMINANCE_ALPHA */
210 if (_mesa_get_format_bits(format, GL_RED_BITS) == 0 &&
211 _mesa_get_format_bits(format, GL_DEPTH_BITS) == 0 &&
212 _mesa_get_format_bits(format, GL_STENCIL_BITS) == 0)
213 return false;
214
215 return true;
216 }
217
218 static enum isl_format
219 brw_blorp_to_isl_format(struct brw_context *brw, mesa_format format,
220 bool is_render_target)
221 {
222 switch (format) {
223 case MESA_FORMAT_NONE:
224 return ISL_FORMAT_UNSUPPORTED;
225 case MESA_FORMAT_S_UINT8:
226 return ISL_FORMAT_R8_UINT;
227 case MESA_FORMAT_Z24_UNORM_X8_UINT:
228 case MESA_FORMAT_Z24_UNORM_S8_UINT:
229 return ISL_FORMAT_R24_UNORM_X8_TYPELESS;
230 case MESA_FORMAT_Z_FLOAT32:
231 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
232 return ISL_FORMAT_R32_FLOAT;
233 case MESA_FORMAT_Z_UNORM16:
234 return ISL_FORMAT_R16_UNORM;
235 default:
236 if (is_render_target) {
237 assert(brw_blorp_supports_dst_format(brw, format));
238 if (brw->mesa_format_supports_render[format]) {
239 return brw->mesa_to_isl_render_format[format];
240 } else {
241 return brw_isl_format_for_mesa_format(format);
242 }
243 } else {
244 /* Some destinations (is_render_target == true) are supported by
245 * blorp even though we technically can't render to them.
246 */
247 return brw_isl_format_for_mesa_format(format);
248 }
249 }
250 }
251
252 /**
253 * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
254 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
255 *
256 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
257 * 0 1 2 3 4 5
258 * 4 5 6 7 0 1
259 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
260 *
261 * which is simply adding 4 then modding by 8 (or anding with 7).
262 *
263 * We then may need to apply workarounds for textureGather hardware bugs.
264 */
265 static enum isl_channel_select
266 swizzle_to_scs(GLenum swizzle)
267 {
268 return (enum isl_channel_select)((swizzle + 4) & 7);
269 }
270
271 /**
272 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
273 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
274 * the physical layer holding sample 0. So, for example, if
275 * src_mt->surf.samples == 4, then logical layer n corresponds to src_layer ==
276 * 4*n.
277 */
278 void
279 brw_blorp_blit_miptrees(struct brw_context *brw,
280 struct intel_mipmap_tree *src_mt,
281 unsigned src_level, unsigned src_layer,
282 mesa_format src_format, int src_swizzle,
283 struct intel_mipmap_tree *dst_mt,
284 unsigned dst_level, unsigned dst_layer,
285 mesa_format dst_format,
286 float src_x0, float src_y0,
287 float src_x1, float src_y1,
288 float dst_x0, float dst_y0,
289 float dst_x1, float dst_y1,
290 GLenum filter, bool mirror_x, bool mirror_y,
291 bool decode_srgb, bool encode_srgb)
292 {
293 const struct gen_device_info *devinfo = &brw->screen->devinfo;
294
295 DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f) "
296 "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
297 __func__,
298 src_mt->surf.samples, _mesa_get_format_name(src_mt->format), src_mt,
299 src_level, src_layer, src_x0, src_y0, src_x1, src_y1,
300 dst_mt->surf.samples, _mesa_get_format_name(dst_mt->format), dst_mt,
301 dst_level, dst_layer, dst_x0, dst_y0, dst_x1, dst_y1,
302 mirror_x, mirror_y);
303
304 if (!decode_srgb && _mesa_get_format_color_encoding(src_format) == GL_SRGB)
305 src_format = _mesa_get_srgb_format_linear(src_format);
306
307 if (!encode_srgb && _mesa_get_format_color_encoding(dst_format) == GL_SRGB)
308 dst_format = _mesa_get_srgb_format_linear(dst_format);
309
310 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
311 * texture, the above code configures the source format for L32_FLOAT or
312 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
313 * the SAMPLE message appears to handle multisampled L32_FLOAT and
314 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
315 * around the problem by using a source format of R32_FLOAT. This
316 * shouldn't affect rendering correctness, since the destination format is
317 * R32_FLOAT, so only the contents of the red channel matters.
318 */
319 if (devinfo->gen == 6 &&
320 src_mt->surf.samples > 1 && dst_mt->surf.samples <= 1 &&
321 src_mt->format == dst_mt->format &&
322 (dst_format == MESA_FORMAT_L_FLOAT32 ||
323 dst_format == MESA_FORMAT_I_FLOAT32)) {
324 src_format = dst_format = MESA_FORMAT_R_FLOAT32;
325 }
326
327 enum isl_format src_isl_format =
328 brw_blorp_to_isl_format(brw, src_format, false);
329 enum isl_aux_usage src_aux_usage =
330 intel_miptree_texture_aux_usage(brw, src_mt, src_isl_format);
331 /* We do format workarounds for some depth formats so we can't reliably
332 * sample with HiZ. One of these days, we should fix that.
333 */
334 if (src_aux_usage == ISL_AUX_USAGE_HIZ)
335 src_aux_usage = ISL_AUX_USAGE_NONE;
336 const bool src_clear_supported =
337 src_aux_usage != ISL_AUX_USAGE_NONE && src_mt->format == src_format;
338 intel_miptree_prepare_access(brw, src_mt, src_level, 1, src_layer, 1,
339 src_aux_usage, src_clear_supported);
340
341 enum isl_format dst_isl_format =
342 brw_blorp_to_isl_format(brw, dst_format, true);
343 enum isl_aux_usage dst_aux_usage =
344 intel_miptree_render_aux_usage(brw, dst_mt, dst_isl_format,
345 false, false);
346 const bool dst_clear_supported = dst_aux_usage != ISL_AUX_USAGE_NONE;
347 intel_miptree_prepare_access(brw, dst_mt, dst_level, 1, dst_layer, 1,
348 dst_aux_usage, dst_clear_supported);
349
350 struct isl_surf tmp_surfs[2];
351 struct blorp_surf src_surf, dst_surf;
352 blorp_surf_for_miptree(brw, &src_surf, src_mt, src_aux_usage, false,
353 &src_level, src_layer, 1, &tmp_surfs[0]);
354 blorp_surf_for_miptree(brw, &dst_surf, dst_mt, dst_aux_usage, true,
355 &dst_level, dst_layer, 1, &tmp_surfs[1]);
356
357 struct isl_swizzle src_isl_swizzle = {
358 .r = swizzle_to_scs(GET_SWZ(src_swizzle, 0)),
359 .g = swizzle_to_scs(GET_SWZ(src_swizzle, 1)),
360 .b = swizzle_to_scs(GET_SWZ(src_swizzle, 2)),
361 .a = swizzle_to_scs(GET_SWZ(src_swizzle, 3)),
362 };
363
364 struct blorp_batch batch;
365 blorp_batch_init(&brw->blorp, &batch, brw, 0);
366 blorp_blit(&batch, &src_surf, src_level, src_layer,
367 src_isl_format, src_isl_swizzle,
368 &dst_surf, dst_level, dst_layer,
369 dst_isl_format, ISL_SWIZZLE_IDENTITY,
370 src_x0, src_y0, src_x1, src_y1,
371 dst_x0, dst_y0, dst_x1, dst_y1,
372 filter, mirror_x, mirror_y);
373 blorp_batch_finish(&batch);
374
375 intel_miptree_finish_write(brw, dst_mt, dst_level, dst_layer, 1,
376 dst_aux_usage);
377 }
378
379 void
380 brw_blorp_copy_miptrees(struct brw_context *brw,
381 struct intel_mipmap_tree *src_mt,
382 unsigned src_level, unsigned src_layer,
383 struct intel_mipmap_tree *dst_mt,
384 unsigned dst_level, unsigned dst_layer,
385 unsigned src_x, unsigned src_y,
386 unsigned dst_x, unsigned dst_y,
387 unsigned src_width, unsigned src_height)
388 {
389 const struct gen_device_info *devinfo = &brw->screen->devinfo;
390
391 DBG("%s from %dx %s mt %p %d %d (%d,%d) %dx%d"
392 "to %dx %s mt %p %d %d (%d,%d)\n",
393 __func__,
394 src_mt->surf.samples, _mesa_get_format_name(src_mt->format), src_mt,
395 src_level, src_layer, src_x, src_y, src_width, src_height,
396 dst_mt->surf.samples, _mesa_get_format_name(dst_mt->format), dst_mt,
397 dst_level, dst_layer, dst_x, dst_y);
398
399 enum isl_aux_usage src_aux_usage, dst_aux_usage;
400 bool src_clear_supported, dst_clear_supported;
401
402 switch (src_mt->aux_usage) {
403 case ISL_AUX_USAGE_MCS:
404 case ISL_AUX_USAGE_CCS_E:
405 src_aux_usage = src_mt->aux_usage;
406 /* Prior to gen9, fast-clear only supported 0/1 clear colors. Since
407 * we're going to re-interpret the format as an integer format possibly
408 * with a different number of components, we can't handle clear colors
409 * until gen9.
410 */
411 src_clear_supported = devinfo->gen >= 9;
412 break;
413 default:
414 src_aux_usage = ISL_AUX_USAGE_NONE;
415 src_clear_supported = false;
416 break;
417 }
418
419 switch (dst_mt->aux_usage) {
420 case ISL_AUX_USAGE_MCS:
421 case ISL_AUX_USAGE_CCS_E:
422 dst_aux_usage = dst_mt->aux_usage;
423 /* Prior to gen9, fast-clear only supported 0/1 clear colors. Since
424 * we're going to re-interpret the format as an integer format possibly
425 * with a different number of components, we can't handle clear colors
426 * until gen9.
427 */
428 dst_clear_supported = devinfo->gen >= 9;
429 break;
430 default:
431 dst_aux_usage = ISL_AUX_USAGE_NONE;
432 dst_clear_supported = false;
433 break;
434 }
435
436 intel_miptree_prepare_access(brw, src_mt, src_level, 1, src_layer, 1,
437 src_aux_usage, src_clear_supported);
438 intel_miptree_prepare_access(brw, dst_mt, dst_level, 1, dst_layer, 1,
439 dst_aux_usage, dst_clear_supported);
440
441 struct isl_surf tmp_surfs[2];
442 struct blorp_surf src_surf, dst_surf;
443 blorp_surf_for_miptree(brw, &src_surf, src_mt, src_aux_usage, false,
444 &src_level, src_layer, 1, &tmp_surfs[0]);
445 blorp_surf_for_miptree(brw, &dst_surf, dst_mt, dst_aux_usage, true,
446 &dst_level, dst_layer, 1, &tmp_surfs[1]);
447
448 /* The hardware seems to have issues with having a two different format
449 * views of the same texture in the sampler cache at the same time. It's
450 * unclear exactly what the issue is but it hurts glCopyImageSubData
451 * particularly badly because it does a lot of format reinterprets. We
452 * badly need better understanding of the issue and a better fix but this
453 * works for now and fixes CTS tests.
454 *
455 * TODO: Remove this hack!
456 */
457 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL |
458 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
459
460 struct blorp_batch batch;
461 blorp_batch_init(&brw->blorp, &batch, brw, 0);
462 blorp_copy(&batch, &src_surf, src_level, src_layer,
463 &dst_surf, dst_level, dst_layer,
464 src_x, src_y, dst_x, dst_y, src_width, src_height);
465 blorp_batch_finish(&batch);
466
467 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL |
468 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
469
470 intel_miptree_finish_write(brw, dst_mt, dst_level, dst_layer, 1,
471 dst_aux_usage);
472 }
473
474 void
475 brw_blorp_copy_buffers(struct brw_context *brw,
476 struct brw_bo *src_bo,
477 unsigned src_offset,
478 struct brw_bo *dst_bo,
479 unsigned dst_offset,
480 unsigned size)
481 {
482 DBG("%s %d bytes from %p[%d] to %p[%d]",
483 __func__, size, src_bo, src_offset, dst_bo, dst_offset);
484
485 struct blorp_batch batch;
486 struct blorp_address src = { .buffer = src_bo, .offset = src_offset };
487 struct blorp_address dst = { .buffer = dst_bo, .offset = dst_offset };
488
489 blorp_batch_init(&brw->blorp, &batch, brw, 0);
490 blorp_buffer_copy(&batch, src, dst, size);
491 blorp_batch_finish(&batch);
492 }
493
494
495 static struct intel_mipmap_tree *
496 find_miptree(GLbitfield buffer_bit, struct intel_renderbuffer *irb)
497 {
498 struct intel_mipmap_tree *mt = irb->mt;
499 if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
500 mt = mt->stencil_mt;
501 return mt;
502 }
503
504 static int
505 blorp_get_texture_swizzle(const struct intel_renderbuffer *irb)
506 {
507 return irb->Base.Base._BaseFormat == GL_RGB ?
508 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE) :
509 SWIZZLE_XYZW;
510 }
511
512 static void
513 do_blorp_blit(struct brw_context *brw, GLbitfield buffer_bit,
514 struct intel_renderbuffer *src_irb, mesa_format src_format,
515 struct intel_renderbuffer *dst_irb, mesa_format dst_format,
516 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
517 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
518 GLenum filter, bool mirror_x, bool mirror_y)
519 {
520 const struct gl_context *ctx = &brw->ctx;
521
522 /* Find source/dst miptrees */
523 struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_irb);
524 struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_irb);
525
526 const bool do_srgb = ctx->Color.sRGBEnabled;
527
528 /* Do the blit */
529 brw_blorp_blit_miptrees(brw,
530 src_mt, src_irb->mt_level, src_irb->mt_layer,
531 src_format, blorp_get_texture_swizzle(src_irb),
532 dst_mt, dst_irb->mt_level, dst_irb->mt_layer,
533 dst_format,
534 srcX0, srcY0, srcX1, srcY1,
535 dstX0, dstY0, dstX1, dstY1,
536 filter, mirror_x, mirror_y,
537 do_srgb, do_srgb);
538
539 dst_irb->need_downsample = true;
540 }
541
542 static bool
543 try_blorp_blit(struct brw_context *brw,
544 const struct gl_framebuffer *read_fb,
545 const struct gl_framebuffer *draw_fb,
546 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
547 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
548 GLenum filter, GLbitfield buffer_bit)
549 {
550 const struct gen_device_info *devinfo = &brw->screen->devinfo;
551 struct gl_context *ctx = &brw->ctx;
552
553 /* Sync up the state of window system buffers. We need to do this before
554 * we go looking for the buffers.
555 */
556 intel_prepare_render(brw);
557
558 bool mirror_x, mirror_y;
559 if (brw_meta_mirror_clip_and_scissor(ctx, read_fb, draw_fb,
560 &srcX0, &srcY0, &srcX1, &srcY1,
561 &dstX0, &dstY0, &dstX1, &dstY1,
562 &mirror_x, &mirror_y))
563 return true;
564
565 /* Find buffers */
566 struct intel_renderbuffer *src_irb;
567 struct intel_renderbuffer *dst_irb;
568 struct intel_mipmap_tree *src_mt;
569 struct intel_mipmap_tree *dst_mt;
570 switch (buffer_bit) {
571 case GL_COLOR_BUFFER_BIT:
572 src_irb = intel_renderbuffer(read_fb->_ColorReadBuffer);
573 for (unsigned i = 0; i < draw_fb->_NumColorDrawBuffers; ++i) {
574 dst_irb = intel_renderbuffer(draw_fb->_ColorDrawBuffers[i]);
575 if (dst_irb)
576 do_blorp_blit(brw, buffer_bit,
577 src_irb, src_irb->Base.Base.Format,
578 dst_irb, dst_irb->Base.Base.Format,
579 srcX0, srcY0, srcX1, srcY1,
580 dstX0, dstY0, dstX1, dstY1,
581 filter, mirror_x, mirror_y);
582 }
583 break;
584 case GL_DEPTH_BUFFER_BIT:
585 src_irb =
586 intel_renderbuffer(read_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
587 dst_irb =
588 intel_renderbuffer(draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
589 src_mt = find_miptree(buffer_bit, src_irb);
590 dst_mt = find_miptree(buffer_bit, dst_irb);
591
592 /* We also can't handle any combined depth-stencil formats because we
593 * have to reinterpret as a color format.
594 */
595 if (_mesa_get_format_base_format(src_mt->format) == GL_DEPTH_STENCIL ||
596 _mesa_get_format_base_format(dst_mt->format) == GL_DEPTH_STENCIL)
597 return false;
598
599 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
600 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
601 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
602 filter, mirror_x, mirror_y);
603 break;
604 case GL_STENCIL_BUFFER_BIT:
605 /* Blorp doesn't support combined depth stencil which is all we have
606 * prior to gen6.
607 */
608 if (devinfo->gen < 6)
609 return false;
610
611 src_irb =
612 intel_renderbuffer(read_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
613 dst_irb =
614 intel_renderbuffer(draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
615 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
616 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
617 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
618 filter, mirror_x, mirror_y);
619 break;
620 default:
621 unreachable("not reached");
622 }
623
624 return true;
625 }
626
627 static void
628 apply_y_flip(int *y0, int *y1, int height)
629 {
630 int tmp = height - *y0;
631 *y0 = height - *y1;
632 *y1 = tmp;
633 }
634
635 bool
636 brw_blorp_copytexsubimage(struct brw_context *brw,
637 struct gl_renderbuffer *src_rb,
638 struct gl_texture_image *dst_image,
639 int slice,
640 int srcX0, int srcY0,
641 int dstX0, int dstY0,
642 int width, int height)
643 {
644 struct gl_context *ctx = &brw->ctx;
645 struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
646 struct intel_texture_image *intel_image = intel_texture_image(dst_image);
647
648 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
649 if (brw->ctx._ImageTransferState)
650 return false;
651
652 /* Sync up the state of window system buffers. We need to do this before
653 * we go looking at the src renderbuffer's miptree.
654 */
655 intel_prepare_render(brw);
656
657 struct intel_mipmap_tree *src_mt = src_irb->mt;
658 struct intel_mipmap_tree *dst_mt = intel_image->mt;
659
660 /* We can't handle any combined depth-stencil formats because we have to
661 * reinterpret as a color format.
662 */
663 if (_mesa_get_format_base_format(src_mt->format) == GL_DEPTH_STENCIL ||
664 _mesa_get_format_base_format(dst_mt->format) == GL_DEPTH_STENCIL)
665 return false;
666
667 if (!brw_blorp_supports_dst_format(brw, dst_image->TexFormat))
668 return false;
669
670 /* Source clipping shouldn't be necessary, since copytexsubimage (in
671 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
672 * takes care of it.
673 *
674 * Destination clipping shouldn't be necessary since the restrictions on
675 * glCopyTexSubImage prevent the user from specifying a destination rectangle
676 * that falls outside the bounds of the destination texture.
677 * See error_check_subtexture_dimensions().
678 */
679
680 int srcY1 = srcY0 + height;
681 int srcX1 = srcX0 + width;
682 int dstX1 = dstX0 + width;
683 int dstY1 = dstY0 + height;
684
685 /* Account for the fact that in the system framebuffer, the origin is at
686 * the lower left.
687 */
688 bool mirror_y = _mesa_is_winsys_fbo(ctx->ReadBuffer);
689 if (mirror_y)
690 apply_y_flip(&srcY0, &srcY1, src_rb->Height);
691
692 /* Account for face selection and texture view MinLayer */
693 int dst_slice = slice + dst_image->TexObject->MinLayer + dst_image->Face;
694 int dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
695
696 brw_blorp_blit_miptrees(brw,
697 src_mt, src_irb->mt_level, src_irb->mt_layer,
698 src_rb->Format, blorp_get_texture_swizzle(src_irb),
699 dst_mt, dst_level, dst_slice,
700 dst_image->TexFormat,
701 srcX0, srcY0, srcX1, srcY1,
702 dstX0, dstY0, dstX1, dstY1,
703 GL_NEAREST, false, mirror_y,
704 false, false);
705
706 /* If we're copying to a packed depth stencil texture and the source
707 * framebuffer has separate stencil, we need to also copy the stencil data
708 * over.
709 */
710 src_rb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
711 if (_mesa_get_format_bits(dst_image->TexFormat, GL_STENCIL_BITS) > 0 &&
712 src_rb != NULL) {
713 src_irb = intel_renderbuffer(src_rb);
714 src_mt = src_irb->mt;
715
716 if (src_mt->stencil_mt)
717 src_mt = src_mt->stencil_mt;
718 if (dst_mt->stencil_mt)
719 dst_mt = dst_mt->stencil_mt;
720
721 if (src_mt != dst_mt) {
722 brw_blorp_blit_miptrees(brw,
723 src_mt, src_irb->mt_level, src_irb->mt_layer,
724 src_mt->format,
725 blorp_get_texture_swizzle(src_irb),
726 dst_mt, dst_level, dst_slice,
727 dst_mt->format,
728 srcX0, srcY0, srcX1, srcY1,
729 dstX0, dstY0, dstX1, dstY1,
730 GL_NEAREST, false, mirror_y,
731 false, false);
732 }
733 }
734
735 return true;
736 }
737
738
739 GLbitfield
740 brw_blorp_framebuffer(struct brw_context *brw,
741 struct gl_framebuffer *readFb,
742 struct gl_framebuffer *drawFb,
743 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
744 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
745 GLbitfield mask, GLenum filter)
746 {
747 static GLbitfield buffer_bits[] = {
748 GL_COLOR_BUFFER_BIT,
749 GL_DEPTH_BUFFER_BIT,
750 GL_STENCIL_BUFFER_BIT,
751 };
752
753 for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
754 if ((mask & buffer_bits[i]) &&
755 try_blorp_blit(brw, readFb, drawFb,
756 srcX0, srcY0, srcX1, srcY1,
757 dstX0, dstY0, dstX1, dstY1,
758 filter, buffer_bits[i])) {
759 mask &= ~buffer_bits[i];
760 }
761 }
762
763 return mask;
764 }
765
766 static struct brw_bo *
767 blorp_get_client_bo(struct brw_context *brw,
768 unsigned w, unsigned h, unsigned d,
769 GLenum target, GLenum format, GLenum type,
770 const void *pixels,
771 const struct gl_pixelstore_attrib *packing,
772 uint32_t *offset_out, uint32_t *row_stride_out,
773 uint32_t *image_stride_out, bool read_only)
774 {
775 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
776 const GLuint dims = _mesa_get_texture_dimensions(target);
777 const uint32_t first_pixel = _mesa_image_offset(dims, packing, w, h,
778 format, type, 0, 0, 0);
779 const uint32_t last_pixel = _mesa_image_offset(dims, packing, w, h,
780 format, type,
781 d - 1, h - 1, w);
782 const uint32_t stride = _mesa_image_row_stride(packing, w, format, type);
783 const uint32_t cpp = _mesa_bytes_per_pixel(format, type);
784 const uint32_t size = last_pixel - first_pixel;
785
786 *row_stride_out = stride;
787 *image_stride_out = _mesa_image_image_stride(packing, w, h, format, type);
788
789 if (_mesa_is_bufferobj(packing->BufferObj)) {
790 const uint32_t offset = first_pixel + (intptr_t)pixels;
791 if (!read_only && ((offset % cpp) || (stride % cpp))) {
792 perf_debug("Bad PBO alignment; fallback to CPU mapping\n");
793 return NULL;
794 }
795
796 /* This is a user-provided PBO. We just need to get the BO out */
797 struct intel_buffer_object *intel_pbo =
798 intel_buffer_object(packing->BufferObj);
799 struct brw_bo *bo =
800 intel_bufferobj_buffer(brw, intel_pbo, offset, size, !read_only);
801
802 /* We take a reference to the BO so that the caller can just always
803 * unref without having to worry about whether it's a user PBO or one
804 * we created.
805 */
806 brw_bo_reference(bo);
807
808 *offset_out = offset;
809 return bo;
810 } else {
811 /* Someone should have already checked that there is data to upload. */
812 assert(pixels);
813
814 /* Creating a temp buffer currently only works for upload */
815 assert(read_only);
816
817 /* This is not a user-provided PBO. Instead, pixels is a pointer to CPU
818 * data which we need to copy into a BO.
819 */
820 struct brw_bo *bo =
821 brw_bo_alloc(brw->bufmgr, "tmp_tex_subimage_src", size,
822 BRW_MEMZONE_OTHER);
823 if (bo == NULL) {
824 perf_debug("intel_texsubimage: temp bo creation failed: size = %u\n",
825 size);
826 return NULL;
827 }
828
829 if (brw_bo_subdata(bo, 0, size, pixels + first_pixel)) {
830 perf_debug("intel_texsubimage: temp bo upload failed\n");
831 brw_bo_unreference(bo);
832 return NULL;
833 }
834
835 *offset_out = 0;
836 return bo;
837 }
838 }
839
840 /* Consider all the restrictions and determine the format of the source. */
841 static mesa_format
842 blorp_get_client_format(struct brw_context *brw,
843 GLenum format, GLenum type,
844 const struct gl_pixelstore_attrib *packing)
845 {
846 if (brw->ctx._ImageTransferState)
847 return MESA_FORMAT_NONE;
848
849 if (packing->SwapBytes || packing->LsbFirst || packing->Invert) {
850 perf_debug("intel_texsubimage_blorp: unsupported gl_pixelstore_attrib\n");
851 return MESA_FORMAT_NONE;
852 }
853
854 if (format != GL_RED &&
855 format != GL_RG &&
856 format != GL_RGB &&
857 format != GL_BGR &&
858 format != GL_RGBA &&
859 format != GL_BGRA &&
860 format != GL_ALPHA &&
861 format != GL_RED_INTEGER &&
862 format != GL_RG_INTEGER &&
863 format != GL_RGB_INTEGER &&
864 format != GL_BGR_INTEGER &&
865 format != GL_RGBA_INTEGER &&
866 format != GL_BGRA_INTEGER) {
867 perf_debug("intel_texsubimage_blorp: %s not supported",
868 _mesa_enum_to_string(format));
869 return MESA_FORMAT_NONE;
870 }
871
872 return _mesa_tex_format_from_format_and_type(&brw->ctx, format, type);
873 }
874
875 static bool
876 need_signed_unsigned_int_conversion(mesa_format src_format,
877 mesa_format dst_format)
878 {
879 const GLenum src_type = _mesa_get_format_datatype(src_format);
880 const GLenum dst_type = _mesa_get_format_datatype(dst_format);
881 return (src_type == GL_INT && dst_type == GL_UNSIGNED_INT) ||
882 (src_type == GL_UNSIGNED_INT && dst_type == GL_INT);
883 }
884
885 bool
886 brw_blorp_upload_miptree(struct brw_context *brw,
887 struct intel_mipmap_tree *dst_mt,
888 mesa_format dst_format,
889 uint32_t level, uint32_t x, uint32_t y, uint32_t z,
890 uint32_t width, uint32_t height, uint32_t depth,
891 GLenum target, GLenum format, GLenum type,
892 const void *pixels,
893 const struct gl_pixelstore_attrib *packing)
894 {
895 const mesa_format src_format =
896 blorp_get_client_format(brw, format, type, packing);
897 if (src_format == MESA_FORMAT_NONE)
898 return false;
899
900 if (!brw->mesa_format_supports_render[dst_format]) {
901 perf_debug("intel_texsubimage: can't use %s as render target\n",
902 _mesa_get_format_name(dst_format));
903 return false;
904 }
905
906 /* This function relies on blorp_blit to upload the pixel data to the
907 * miptree. But, blorp_blit doesn't support signed to unsigned or
908 * unsigned to signed integer conversions.
909 */
910 if (need_signed_unsigned_int_conversion(src_format, dst_format))
911 return false;
912
913 uint32_t src_offset, src_row_stride, src_image_stride;
914 struct brw_bo *src_bo =
915 blorp_get_client_bo(brw, width, height, depth,
916 target, format, type, pixels, packing,
917 &src_offset, &src_row_stride,
918 &src_image_stride, true);
919 if (src_bo == NULL)
920 return false;
921
922 /* Now that source is offset to correct starting point, adjust the
923 * given dimensions to treat 1D arrays as 2D.
924 */
925 if (target == GL_TEXTURE_1D_ARRAY) {
926 assert(depth == 1);
927 assert(z == 0);
928 depth = height;
929 height = 1;
930 z = y;
931 y = 0;
932 src_image_stride = src_row_stride;
933 }
934
935 intel_miptree_check_level_layer(dst_mt, level, z + depth - 1);
936
937 bool result = false;
938
939 /* Blit slice-by-slice creating a single-slice miptree for each layer. Even
940 * in case of linear buffers hardware wants image arrays to be aligned by
941 * four rows. This way hardware only gets one image at a time and any
942 * source alignment will do.
943 */
944 for (unsigned i = 0; i < depth; ++i) {
945 struct intel_mipmap_tree *src_mt = intel_miptree_create_for_bo(
946 brw, src_bo, src_format,
947 src_offset + i * src_image_stride,
948 width, height, 1,
949 src_row_stride,
950 ISL_TILING_LINEAR, 0);
951
952 if (!src_mt) {
953 perf_debug("intel_texsubimage: miptree creation for src failed\n");
954 goto err;
955 }
956
957 /* In case exact match is needed, copy using equivalent UINT formats
958 * preventing hardware from changing presentation for SNORM -1.
959 */
960 if (src_mt->format == dst_format) {
961 brw_blorp_copy_miptrees(brw, src_mt, 0, 0,
962 dst_mt, level, z + i,
963 0, 0, x, y, width, height);
964 } else {
965 brw_blorp_blit_miptrees(brw, src_mt, 0, 0,
966 src_format, SWIZZLE_XYZW,
967 dst_mt, level, z + i,
968 dst_format,
969 0, 0, width, height,
970 x, y, x + width, y + height,
971 GL_NEAREST, false, false, false, false);
972 }
973
974 intel_miptree_release(&src_mt);
975 }
976
977 result = true;
978
979 err:
980 brw_bo_unreference(src_bo);
981
982 return result;
983 }
984
985 bool
986 brw_blorp_download_miptree(struct brw_context *brw,
987 struct intel_mipmap_tree *src_mt,
988 mesa_format src_format, uint32_t src_swizzle,
989 uint32_t level, uint32_t x, uint32_t y, uint32_t z,
990 uint32_t width, uint32_t height, uint32_t depth,
991 GLenum target, GLenum format, GLenum type,
992 bool y_flip, const void *pixels,
993 const struct gl_pixelstore_attrib *packing)
994 {
995 const mesa_format dst_format =
996 blorp_get_client_format(brw, format, type, packing);
997 if (dst_format == MESA_FORMAT_NONE)
998 return false;
999
1000 if (!brw->mesa_format_supports_render[dst_format]) {
1001 perf_debug("intel_texsubimage: can't use %s as render target\n",
1002 _mesa_get_format_name(dst_format));
1003 return false;
1004 }
1005
1006 /* This function relies on blorp_blit to download the pixel data from the
1007 * miptree. But, blorp_blit doesn't support signed to unsigned or unsigned
1008 * to signed integer conversions.
1009 */
1010 if (need_signed_unsigned_int_conversion(src_format, dst_format))
1011 return false;
1012
1013 /* We can't fetch from LUMINANCE or intensity as that would require a
1014 * non-trivial swizzle.
1015 */
1016 switch (_mesa_get_format_base_format(src_format)) {
1017 case GL_LUMINANCE:
1018 case GL_LUMINANCE_ALPHA:
1019 case GL_INTENSITY:
1020 return false;
1021 default:
1022 break;
1023 }
1024
1025 /* This pass only works for PBOs */
1026 assert(_mesa_is_bufferobj(packing->BufferObj));
1027
1028 uint32_t dst_offset, dst_row_stride, dst_image_stride;
1029 struct brw_bo *dst_bo =
1030 blorp_get_client_bo(brw, width, height, depth,
1031 target, format, type, pixels, packing,
1032 &dst_offset, &dst_row_stride,
1033 &dst_image_stride, false);
1034 if (dst_bo == NULL)
1035 return false;
1036
1037 /* Now that source is offset to correct starting point, adjust the
1038 * given dimensions to treat 1D arrays as 2D.
1039 */
1040 if (target == GL_TEXTURE_1D_ARRAY) {
1041 assert(depth == 1);
1042 assert(z == 0);
1043 depth = height;
1044 height = 1;
1045 z = y;
1046 y = 0;
1047 dst_image_stride = dst_row_stride;
1048 }
1049
1050 intel_miptree_check_level_layer(src_mt, level, z + depth - 1);
1051
1052 int y0 = y;
1053 int y1 = y + height;
1054 if (y_flip) {
1055 apply_y_flip(&y0, &y1, minify(src_mt->surf.phys_level0_sa.height,
1056 level - src_mt->first_level));
1057 }
1058
1059 bool result = false;
1060
1061 /* Blit slice-by-slice creating a single-slice miptree for each layer. Even
1062 * in case of linear buffers hardware wants image arrays to be aligned by
1063 * four rows. This way hardware only gets one image at a time and any
1064 * source alignment will do.
1065 */
1066 for (unsigned i = 0; i < depth; ++i) {
1067 struct intel_mipmap_tree *dst_mt = intel_miptree_create_for_bo(
1068 brw, dst_bo, dst_format,
1069 dst_offset + i * dst_image_stride,
1070 width, height, 1,
1071 dst_row_stride,
1072 ISL_TILING_LINEAR, 0);
1073
1074 if (!dst_mt) {
1075 perf_debug("intel_texsubimage: miptree creation for src failed\n");
1076 goto err;
1077 }
1078
1079 /* In case exact match is needed, copy using equivalent UINT formats
1080 * preventing hardware from changing presentation for SNORM -1.
1081 */
1082 if (dst_mt->format == src_format && !y_flip &&
1083 src_swizzle == SWIZZLE_XYZW) {
1084 brw_blorp_copy_miptrees(brw, src_mt, level, z + i,
1085 dst_mt, 0, 0,
1086 x, y, 0, 0, width, height);
1087 } else {
1088 brw_blorp_blit_miptrees(brw, src_mt, level, z + i,
1089 src_format, src_swizzle,
1090 dst_mt, 0, 0, dst_format,
1091 x, y0, x + width, y1,
1092 0, 0, width, height,
1093 GL_NEAREST, false, y_flip, false, false);
1094 }
1095
1096 intel_miptree_release(&dst_mt);
1097 }
1098
1099 result = true;
1100
1101 /* As we implement PBO transfers by binding the user-provided BO as a
1102 * fake framebuffer and rendering to it. This breaks the invariant of the
1103 * GL that nothing is able to render to a BO, causing nondeterministic
1104 * corruption issues because the render cache is not coherent with a
1105 * number of other caches that the BO could potentially be bound to
1106 * afterwards.
1107 *
1108 * This could be solved in the same way that we guarantee texture
1109 * coherency after a texture is attached to a framebuffer and
1110 * rendered to, but that would involve checking *all* BOs bound to
1111 * the pipeline for the case we need to emit a cache flush due to
1112 * previous rendering to any of them -- Including vertex, index,
1113 * uniform, atomic counter, shader image, transform feedback,
1114 * indirect draw buffers, etc.
1115 *
1116 * That would increase the per-draw call overhead even though it's
1117 * very unlikely that any of the BOs bound to the pipeline has been
1118 * rendered to via a PBO at any point, so it seems better to just
1119 * flush here unconditionally.
1120 */
1121 brw_emit_mi_flush(brw);
1122
1123 err:
1124 brw_bo_unreference(dst_bo);
1125
1126 return result;
1127 }
1128
1129 static bool
1130 set_write_disables(const struct intel_renderbuffer *irb,
1131 const unsigned color_mask, bool *color_write_disable)
1132 {
1133 /* Format information in the renderbuffer represents the requirements
1134 * given by the client. There are cases where the backing miptree uses,
1135 * for example, RGBA to represent RGBX. Since the client is only expecting
1136 * RGB we can treat alpha as not used and write whatever we like into it.
1137 */
1138 const GLenum base_format = irb->Base.Base._BaseFormat;
1139 const int components = _mesa_base_format_component_count(base_format);
1140 bool disables = false;
1141
1142 assert(components > 0);
1143
1144 for (int i = 0; i < components; i++) {
1145 color_write_disable[i] = !(color_mask & (1 << i));
1146 disables = disables || color_write_disable[i];
1147 }
1148
1149 return disables;
1150 }
1151
1152 static void
1153 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
1154 struct gl_renderbuffer *rb, unsigned buf,
1155 bool partial_clear, bool encode_srgb)
1156 {
1157 struct gl_context *ctx = &brw->ctx;
1158 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
1159 uint32_t x0, x1, y0, y1;
1160
1161 mesa_format format = irb->Base.Base.Format;
1162 if (!encode_srgb && _mesa_get_format_color_encoding(format) == GL_SRGB)
1163 format = _mesa_get_srgb_format_linear(format);
1164 enum isl_format isl_format = brw->mesa_to_isl_render_format[format];
1165
1166 x0 = fb->_Xmin;
1167 x1 = fb->_Xmax;
1168 if (rb->Name != 0) {
1169 y0 = fb->_Ymin;
1170 y1 = fb->_Ymax;
1171 } else {
1172 y0 = rb->Height - fb->_Ymax;
1173 y1 = rb->Height - fb->_Ymin;
1174 }
1175
1176 /* If the clear region is empty, just return. */
1177 if (x0 == x1 || y0 == y1)
1178 return;
1179
1180 bool can_fast_clear = !partial_clear;
1181
1182 bool color_write_disable[4] = { false, false, false, false };
1183 if (set_write_disables(irb, GET_COLORMASK(ctx->Color.ColorMask, buf),
1184 color_write_disable))
1185 can_fast_clear = false;
1186
1187 /* We store clear colors as floats or uints as needed. If there are
1188 * texture views in play, the formats will not properly be respected
1189 * during resolves because the resolve operations only know about the
1190 * miptree and not the renderbuffer.
1191 */
1192 if (irb->Base.Base.Format != irb->mt->format)
1193 can_fast_clear = false;
1194
1195 if (!irb->mt->supports_fast_clear ||
1196 !brw_is_color_fast_clear_compatible(brw, irb->mt, &ctx->Color.ClearColor))
1197 can_fast_clear = false;
1198
1199 /* Surface state can only record one fast clear color value. Therefore
1200 * unless different levels/layers agree on the color it can be used to
1201 * represent only single level/layer. Here it will be reserved for the
1202 * first slice (level 0, layer 0).
1203 */
1204 if (irb->layer_count > 1 || irb->mt_level || irb->mt_layer)
1205 can_fast_clear = false;
1206
1207 unsigned level = irb->mt_level;
1208 const unsigned num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
1209
1210 /* If the MCS buffer hasn't been allocated yet, we need to allocate it now.
1211 */
1212 if (can_fast_clear && !irb->mt->aux_buf) {
1213 assert(irb->mt->aux_usage == ISL_AUX_USAGE_CCS_D);
1214 if (!intel_miptree_alloc_aux(brw, irb->mt)) {
1215 /* We're out of memory. Fall back to a non-fast clear. */
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 }