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