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