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