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