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