i965/miptree: Avoid -Wswitch for the Gen12 aux modes
[mesa.git] / src / mesa / drivers / dri / i965 / intel_mipmap_tree.c
1 /*
2 * Copyright 2006 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <GL/gl.h>
27 #include <GL/internal/dri_interface.h>
28 #include "drm-uapi/drm_fourcc.h"
29
30 #include "intel_batchbuffer.h"
31 #include "intel_image.h"
32 #include "intel_mipmap_tree.h"
33 #include "intel_tex.h"
34 #include "intel_blit.h"
35 #include "intel_fbo.h"
36
37 #include "brw_blorp.h"
38 #include "brw_context.h"
39 #include "brw_state.h"
40
41 #include "main/enums.h"
42 #include "main/fbobject.h"
43 #include "main/formats.h"
44 #include "main/glformats.h"
45 #include "main/texcompress_etc.h"
46 #include "main/teximage.h"
47 #include "main/streaming-load-memcpy.h"
48
49 #include "util/format_srgb.h"
50
51 #include "x86/common_x86_asm.h"
52
53 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
54
55 static void *intel_miptree_map_raw(struct brw_context *brw,
56 struct intel_mipmap_tree *mt,
57 GLbitfield mode);
58
59 static void intel_miptree_unmap_raw(struct intel_mipmap_tree *mt);
60
61 /**
62 * Return true if the format that will be used to access the miptree is
63 * CCS_E-compatible with the miptree's linear/non-sRGB format.
64 *
65 * Why use the linear format? Well, although the miptree may be specified with
66 * an sRGB format, the usage of that color space/format can be toggled. Since
67 * our HW tends to support more linear formats than sRGB ones, we use this
68 * format variant for check for CCS_E compatibility.
69 */
70 static bool
71 format_ccs_e_compat_with_miptree(const struct gen_device_info *devinfo,
72 const struct intel_mipmap_tree *mt,
73 enum isl_format access_format)
74 {
75 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_E);
76
77 mesa_format linear_format = _mesa_get_srgb_format_linear(mt->format);
78 enum isl_format isl_format = brw_isl_format_for_mesa_format(linear_format);
79 return isl_formats_are_ccs_e_compatible(devinfo, isl_format, access_format);
80 }
81
82 /* Determine if CCS_E is supported for a given platform and mesa format. */
83 static bool
84 format_supports_ccs_e(const struct brw_context *brw, mesa_format format)
85 {
86 /* For now compression is only enabled for integer formats even though
87 * there exist supported floating point formats also. This is a heuristic
88 * decision based on current public benchmarks. In none of the cases these
89 * formats provided any improvement but a few cases were seen to regress.
90 * Hence these are left to to be enabled in the future when they are known
91 * to improve things.
92 */
93 if (_mesa_get_format_datatype(format) == GL_FLOAT)
94 return false;
95
96 /* Many window system buffers are sRGB even if they are never rendered as
97 * sRGB. For those, we want CCS_E for when sRGBEncode is false. When the
98 * surface is used as sRGB, we fall back to CCS_D.
99 */
100 mesa_format linear_format = _mesa_get_srgb_format_linear(format);
101 enum isl_format isl_format = brw_isl_format_for_mesa_format(linear_format);
102 return isl_format_supports_ccs_e(&brw->screen->devinfo, isl_format);
103 }
104
105 /**
106 * Determine depth format corresponding to a depth+stencil format,
107 * for separate stencil.
108 */
109 mesa_format
110 intel_depth_format_for_depthstencil_format(mesa_format format) {
111 switch (format) {
112 case MESA_FORMAT_Z24_UNORM_S8_UINT:
113 return MESA_FORMAT_Z24_UNORM_X8_UINT;
114 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
115 return MESA_FORMAT_Z_FLOAT32;
116 default:
117 return format;
118 }
119 }
120
121 static bool
122 create_mapping_table(GLenum target, unsigned first_level, unsigned last_level,
123 unsigned depth0, struct intel_mipmap_level *table)
124 {
125 for (unsigned level = first_level; level <= last_level; level++) {
126 const unsigned d =
127 target == GL_TEXTURE_3D ? minify(depth0, level) : depth0;
128
129 table[level].slice = calloc(d, sizeof(*table[0].slice));
130 if (!table[level].slice)
131 goto unwind;
132 }
133
134 return true;
135
136 unwind:
137 for (unsigned level = first_level; level <= last_level; level++)
138 free(table[level].slice);
139
140 return false;
141 }
142
143 static bool
144 needs_separate_stencil(const struct brw_context *brw,
145 struct intel_mipmap_tree *mt,
146 mesa_format format)
147 {
148 const struct gen_device_info *devinfo = &brw->screen->devinfo;
149
150 if (_mesa_get_format_base_format(format) != GL_DEPTH_STENCIL)
151 return false;
152
153 if (devinfo->must_use_separate_stencil)
154 return true;
155
156 return brw->has_separate_stencil && brw->has_hiz;
157 }
158
159 /**
160 * Choose the aux usage for this miptree. This function must be called fairly
161 * late in the miptree create process after we have a tiling.
162 */
163 static void
164 intel_miptree_choose_aux_usage(struct brw_context *brw,
165 struct intel_mipmap_tree *mt)
166 {
167 assert(mt->aux_usage == ISL_AUX_USAGE_NONE);
168
169 if (_mesa_is_format_color_format(mt->format)) {
170 if (mt->surf.samples > 1) {
171 mt->aux_usage = ISL_AUX_USAGE_MCS;
172 } else if (!unlikely(INTEL_DEBUG & DEBUG_NO_RBC) &&
173 format_supports_ccs_e(brw, mt->format)) {
174 mt->aux_usage = ISL_AUX_USAGE_CCS_E;
175 } else if (brw->mesa_format_supports_render[mt->format]) {
176 mt->aux_usage = ISL_AUX_USAGE_CCS_D;
177 }
178 } else if (isl_surf_usage_is_depth(mt->surf.usage) && brw->has_hiz) {
179 mt->aux_usage = ISL_AUX_USAGE_HIZ;
180 }
181
182 /* We can do fast-clear on all auxiliary surface types that are
183 * allocated through the normal texture creation paths.
184 */
185 if (mt->aux_usage != ISL_AUX_USAGE_NONE)
186 mt->supports_fast_clear = true;
187 }
188
189
190 /**
191 * Choose an appropriate uncompressed format for a requested
192 * compressed format, if unsupported.
193 */
194 mesa_format
195 intel_lower_compressed_format(struct brw_context *brw, mesa_format format)
196 {
197 const struct gen_device_info *devinfo = &brw->screen->devinfo;
198
199 /* No need to lower ETC formats on these platforms,
200 * they are supported natively.
201 */
202 if (devinfo->gen >= 8 || devinfo->is_baytrail)
203 return format;
204
205 switch (format) {
206 case MESA_FORMAT_ETC1_RGB8:
207 return MESA_FORMAT_R8G8B8X8_UNORM;
208 case MESA_FORMAT_ETC2_RGB8:
209 return MESA_FORMAT_R8G8B8X8_UNORM;
210 case MESA_FORMAT_ETC2_SRGB8:
211 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
212 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
213 return MESA_FORMAT_B8G8R8A8_SRGB;
214 case MESA_FORMAT_ETC2_RGBA8_EAC:
215 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
216 return MESA_FORMAT_R8G8B8A8_UNORM;
217 case MESA_FORMAT_ETC2_R11_EAC:
218 return MESA_FORMAT_R_UNORM16;
219 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
220 return MESA_FORMAT_R_SNORM16;
221 case MESA_FORMAT_ETC2_RG11_EAC:
222 return MESA_FORMAT_RG_UNORM16;
223 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
224 return MESA_FORMAT_RG_SNORM16;
225 default:
226 /* Non ETC1 / ETC2 format */
227 return format;
228 }
229 }
230
231 unsigned
232 brw_get_num_logical_layers(const struct intel_mipmap_tree *mt, unsigned level)
233 {
234 if (mt->surf.dim == ISL_SURF_DIM_3D)
235 return minify(mt->surf.logical_level0_px.depth, level);
236 else
237 return mt->surf.logical_level0_px.array_len;
238 }
239
240 UNUSED static unsigned
241 get_num_phys_layers(const struct isl_surf *surf, unsigned level)
242 {
243 /* In case of physical dimensions one needs to consider also the layout.
244 * See isl_calc_phys_level0_extent_sa().
245 */
246 if (surf->dim != ISL_SURF_DIM_3D)
247 return surf->phys_level0_sa.array_len;
248
249 if (surf->dim_layout == ISL_DIM_LAYOUT_GEN4_2D)
250 return minify(surf->phys_level0_sa.array_len, level);
251
252 return minify(surf->phys_level0_sa.depth, level);
253 }
254
255 /** \brief Assert that the level and layer are valid for the miptree. */
256 void
257 intel_miptree_check_level_layer(const struct intel_mipmap_tree *mt,
258 uint32_t level,
259 uint32_t layer)
260 {
261 (void) mt;
262 (void) level;
263 (void) layer;
264
265 assert(level >= mt->first_level);
266 assert(level <= mt->last_level);
267 assert(layer < get_num_phys_layers(&mt->surf, level));
268 }
269
270 static enum isl_aux_state **
271 create_aux_state_map(struct intel_mipmap_tree *mt,
272 enum isl_aux_state initial)
273 {
274 const uint32_t levels = mt->last_level + 1;
275
276 uint32_t total_slices = 0;
277 for (uint32_t level = 0; level < levels; level++)
278 total_slices += brw_get_num_logical_layers(mt, level);
279
280 const size_t per_level_array_size = levels * sizeof(enum isl_aux_state *);
281
282 /* We're going to allocate a single chunk of data for both the per-level
283 * reference array and the arrays of aux_state. This makes cleanup
284 * significantly easier.
285 */
286 const size_t total_size = per_level_array_size +
287 total_slices * sizeof(enum isl_aux_state);
288 void *data = malloc(total_size);
289 if (data == NULL)
290 return NULL;
291
292 enum isl_aux_state **per_level_arr = data;
293 enum isl_aux_state *s = data + per_level_array_size;
294 for (uint32_t level = 0; level < levels; level++) {
295 per_level_arr[level] = s;
296 const unsigned level_layers = brw_get_num_logical_layers(mt, level);
297 for (uint32_t a = 0; a < level_layers; a++)
298 *(s++) = initial;
299 }
300 assert((void *)s == data + total_size);
301
302 return per_level_arr;
303 }
304
305 static void
306 free_aux_state_map(enum isl_aux_state **state)
307 {
308 free(state);
309 }
310
311 static bool
312 need_to_retile_as_linear(struct brw_context *brw, unsigned blt_pitch,
313 enum isl_tiling tiling, unsigned samples)
314 {
315 if (samples > 1)
316 return false;
317
318 if (tiling == ISL_TILING_LINEAR)
319 return false;
320
321 if (blt_pitch >= 32768) {
322 perf_debug("blt pitch %u too large to blit, falling back to untiled",
323 blt_pitch);
324 return true;
325 }
326
327 return false;
328 }
329
330 static bool
331 need_to_retile_as_x(const struct brw_context *brw, uint64_t size,
332 enum isl_tiling tiling)
333 {
334 const struct gen_device_info *devinfo = &brw->screen->devinfo;
335
336 /* If the BO is too large to fit in the aperture, we need to use the
337 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
338 * handle Y-tiling, so we need to fall back to X.
339 */
340 if (devinfo->gen < 6 && size >= brw->max_gtt_map_object_size &&
341 tiling == ISL_TILING_Y0)
342 return true;
343
344 return false;
345 }
346
347 static struct intel_mipmap_tree *
348 make_surface(struct brw_context *brw, GLenum target, mesa_format format,
349 unsigned first_level, unsigned last_level,
350 unsigned width0, unsigned height0, unsigned depth0,
351 unsigned num_samples, isl_tiling_flags_t tiling_flags,
352 isl_surf_usage_flags_t isl_usage_flags, uint32_t alloc_flags,
353 unsigned row_pitch_B, struct brw_bo *bo)
354 {
355 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
356 if (!mt)
357 return NULL;
358
359 if (!create_mapping_table(target, first_level, last_level, depth0,
360 mt->level)) {
361 free(mt);
362 return NULL;
363 }
364
365 mt->refcount = 1;
366
367 if (target == GL_TEXTURE_CUBE_MAP ||
368 target == GL_TEXTURE_CUBE_MAP_ARRAY)
369 isl_usage_flags |= ISL_SURF_USAGE_CUBE_BIT;
370
371 DBG("%s: %s %s %ux %u:%u:%u %d..%d <-- %p\n",
372 __func__,
373 _mesa_enum_to_string(target),
374 _mesa_get_format_name(format),
375 num_samples, width0, height0, depth0,
376 first_level, last_level, mt);
377
378 struct isl_surf_init_info init_info = {
379 .dim = get_isl_surf_dim(target),
380 .format = translate_tex_format(brw, format, false),
381 .width = width0,
382 .height = height0,
383 .depth = target == GL_TEXTURE_3D ? depth0 : 1,
384 .levels = last_level - first_level + 1,
385 .array_len = target == GL_TEXTURE_3D ? 1 : depth0,
386 .samples = num_samples,
387 .row_pitch_B = row_pitch_B,
388 .usage = isl_usage_flags,
389 .tiling_flags = tiling_flags,
390 };
391
392 if (!isl_surf_init_s(&brw->isl_dev, &mt->surf, &init_info))
393 goto fail;
394
395 /* Depth surfaces are always Y-tiled and stencil is always W-tiled, although
396 * on gen7 platforms we also need to create Y-tiled copies of stencil for
397 * texturing since the hardware can't sample from W-tiled surfaces. For
398 * everything else, check for corner cases needing special treatment.
399 */
400 bool is_depth_stencil =
401 mt->surf.usage & (ISL_SURF_USAGE_STENCIL_BIT | ISL_SURF_USAGE_DEPTH_BIT);
402 if (!is_depth_stencil) {
403 if (need_to_retile_as_linear(brw, intel_miptree_blt_pitch(mt),
404 mt->surf.tiling, mt->surf.samples)) {
405 init_info.tiling_flags = 1u << ISL_TILING_LINEAR;
406 if (!isl_surf_init_s(&brw->isl_dev, &mt->surf, &init_info))
407 goto fail;
408 } else if (need_to_retile_as_x(brw, mt->surf.size_B, mt->surf.tiling)) {
409 init_info.tiling_flags = 1u << ISL_TILING_X;
410 if (!isl_surf_init_s(&brw->isl_dev, &mt->surf, &init_info))
411 goto fail;
412 }
413 }
414
415 /* In case of linear the buffer gets padded by fixed 64 bytes and therefore
416 * the size may not be multiple of row_pitch.
417 * See isl_apply_surface_padding().
418 */
419 if (mt->surf.tiling != ISL_TILING_LINEAR)
420 assert(mt->surf.size_B % mt->surf.row_pitch_B == 0);
421
422 if (!bo) {
423 mt->bo = brw_bo_alloc_tiled(brw->bufmgr, "isl-miptree",
424 mt->surf.size_B,
425 BRW_MEMZONE_OTHER,
426 isl_tiling_to_i915_tiling(
427 mt->surf.tiling),
428 mt->surf.row_pitch_B, alloc_flags);
429 if (!mt->bo)
430 goto fail;
431 } else {
432 mt->bo = bo;
433 }
434
435 mt->first_level = first_level;
436 mt->last_level = last_level;
437 mt->target = target;
438 mt->format = format;
439 mt->aux_state = NULL;
440 mt->cpp = isl_format_get_layout(mt->surf.format)->bpb / 8;
441 mt->compressed = _mesa_is_format_compressed(format);
442 mt->drm_modifier = DRM_FORMAT_MOD_INVALID;
443
444 return mt;
445
446 fail:
447 intel_miptree_release(&mt);
448 return NULL;
449 }
450
451 /* Return the usual surface usage flags for the given format. */
452 static isl_surf_usage_flags_t
453 mt_surf_usage(mesa_format format)
454 {
455 switch(_mesa_get_format_base_format(format)) {
456 case GL_DEPTH_COMPONENT:
457 return ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
458 case GL_DEPTH_STENCIL:
459 return ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_STENCIL_BIT |
460 ISL_SURF_USAGE_TEXTURE_BIT;
461 case GL_STENCIL_INDEX:
462 return ISL_SURF_USAGE_STENCIL_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
463 default:
464 return ISL_SURF_USAGE_RENDER_TARGET_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
465 }
466 }
467
468 static struct intel_mipmap_tree *
469 miptree_create(struct brw_context *brw,
470 GLenum target,
471 mesa_format format,
472 GLuint first_level,
473 GLuint last_level,
474 GLuint width0,
475 GLuint height0,
476 GLuint depth0,
477 GLuint num_samples,
478 enum intel_miptree_create_flags flags)
479 {
480 const struct gen_device_info *devinfo = &brw->screen->devinfo;
481 const uint32_t alloc_flags =
482 (flags & MIPTREE_CREATE_BUSY || num_samples > 1) ? BO_ALLOC_BUSY : 0;
483 isl_tiling_flags_t tiling_flags = ISL_TILING_ANY_MASK;
484
485 /* TODO: This used to be because there wasn't BLORP to handle Y-tiling. */
486 if (devinfo->gen < 6 && _mesa_is_format_color_format(format))
487 tiling_flags &= ~ISL_TILING_Y0_BIT;
488
489 mesa_format mt_fmt = format;
490 if (!_mesa_is_format_color_format(format) && devinfo->gen >= 6) {
491 /* Fix up the Z miptree format for how we're splitting out separate
492 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
493 */
494 mt_fmt = intel_depth_format_for_depthstencil_format(format);
495 }
496
497 struct intel_mipmap_tree *mt =
498 make_surface(brw, target, mt_fmt, first_level, last_level,
499 width0, height0, depth0, num_samples,
500 tiling_flags, mt_surf_usage(mt_fmt),
501 alloc_flags, 0, NULL);
502
503 if (mt == NULL)
504 return NULL;
505
506 if (intel_miptree_needs_fake_etc(brw, mt)) {
507 mesa_format decomp_format = intel_lower_compressed_format(brw, format);
508 mt->shadow_mt = make_surface(brw, target, decomp_format, first_level,
509 last_level, width0, height0, depth0,
510 num_samples, tiling_flags,
511 mt_surf_usage(decomp_format),
512 alloc_flags, 0, NULL);
513
514 if (mt->shadow_mt == NULL) {
515 intel_miptree_release(&mt);
516 return NULL;
517 }
518 }
519
520 if (needs_separate_stencil(brw, mt, format)) {
521 mt->stencil_mt =
522 make_surface(brw, target, MESA_FORMAT_S_UINT8, first_level, last_level,
523 width0, height0, depth0, num_samples,
524 ISL_TILING_W_BIT, mt_surf_usage(MESA_FORMAT_S_UINT8),
525 alloc_flags, 0, NULL);
526 if (mt->stencil_mt == NULL) {
527 intel_miptree_release(&mt);
528 return NULL;
529 }
530 }
531
532 if (!(flags & MIPTREE_CREATE_NO_AUX))
533 intel_miptree_choose_aux_usage(brw, mt);
534
535 return mt;
536 }
537
538 struct intel_mipmap_tree *
539 intel_miptree_create(struct brw_context *brw,
540 GLenum target,
541 mesa_format format,
542 GLuint first_level,
543 GLuint last_level,
544 GLuint width0,
545 GLuint height0,
546 GLuint depth0,
547 GLuint num_samples,
548 enum intel_miptree_create_flags flags)
549 {
550 assert(num_samples > 0);
551
552 struct intel_mipmap_tree *mt = miptree_create(
553 brw, target, format,
554 first_level, last_level,
555 width0, height0, depth0, num_samples,
556 flags);
557 if (!mt)
558 return NULL;
559
560 mt->offset = 0;
561
562 /* Create the auxiliary surface up-front. CCS_D, on the other hand, can only
563 * compress clear color so we wait until an actual fast-clear to allocate
564 * it.
565 */
566 if (mt->aux_usage != ISL_AUX_USAGE_CCS_D &&
567 !intel_miptree_alloc_aux(brw, mt)) {
568 mt->aux_usage = ISL_AUX_USAGE_NONE;
569 mt->supports_fast_clear = false;
570 }
571
572 return mt;
573 }
574
575 struct intel_mipmap_tree *
576 intel_miptree_create_for_bo(struct brw_context *brw,
577 struct brw_bo *bo,
578 mesa_format format,
579 uint32_t offset,
580 uint32_t width,
581 uint32_t height,
582 uint32_t depth,
583 int pitch,
584 enum isl_tiling tiling,
585 enum intel_miptree_create_flags flags)
586 {
587 const struct gen_device_info *devinfo = &brw->screen->devinfo;
588 struct intel_mipmap_tree *mt;
589 const GLenum target = depth > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
590 const GLenum base_format = _mesa_get_format_base_format(format);
591
592 if ((base_format == GL_DEPTH_COMPONENT ||
593 base_format == GL_DEPTH_STENCIL)) {
594 const mesa_format mt_fmt = (devinfo->gen < 6) ? format :
595 intel_depth_format_for_depthstencil_format(format);
596 mt = make_surface(brw, target, mt_fmt,
597 0, 0, width, height, depth, 1, ISL_TILING_Y0_BIT,
598 mt_surf_usage(mt_fmt),
599 0, pitch, bo);
600 if (!mt)
601 return NULL;
602
603 brw_bo_reference(bo);
604
605 if (!(flags & MIPTREE_CREATE_NO_AUX))
606 intel_miptree_choose_aux_usage(brw, mt);
607
608 return mt;
609 } else if (format == MESA_FORMAT_S_UINT8) {
610 mt = make_surface(brw, target, MESA_FORMAT_S_UINT8,
611 0, 0, width, height, depth, 1,
612 ISL_TILING_W_BIT,
613 mt_surf_usage(MESA_FORMAT_S_UINT8),
614 0, pitch, bo);
615 if (!mt)
616 return NULL;
617
618 assert(bo->size >= mt->surf.size_B);
619
620 brw_bo_reference(bo);
621 return mt;
622 }
623
624 /* Nothing will be able to use this miptree with the BO if the offset isn't
625 * aligned.
626 */
627 if (tiling != ISL_TILING_LINEAR)
628 assert(offset % 4096 == 0);
629
630 /* miptrees can't handle negative pitch. If you need flipping of images,
631 * that's outside of the scope of the mt.
632 */
633 assert(pitch >= 0);
634
635 mt = make_surface(brw, target, format,
636 0, 0, width, height, depth, 1,
637 1lu << tiling,
638 mt_surf_usage(format),
639 0, pitch, bo);
640 if (!mt)
641 return NULL;
642
643 brw_bo_reference(bo);
644 mt->bo = bo;
645 mt->offset = offset;
646
647 if (!(flags & MIPTREE_CREATE_NO_AUX)) {
648 intel_miptree_choose_aux_usage(brw, mt);
649
650 /* Create the auxiliary surface up-front. CCS_D, on the other hand, can
651 * only compress clear color so we wait until an actual fast-clear to
652 * allocate it.
653 */
654 if (mt->aux_usage != ISL_AUX_USAGE_CCS_D &&
655 !intel_miptree_alloc_aux(brw, mt)) {
656 mt->aux_usage = ISL_AUX_USAGE_NONE;
657 mt->supports_fast_clear = false;
658 }
659 }
660
661 return mt;
662 }
663
664 static struct intel_mipmap_tree *
665 miptree_create_for_planar_image(struct brw_context *brw,
666 __DRIimage *image, GLenum target,
667 enum isl_tiling tiling)
668 {
669 const struct intel_image_format *f = image->planar_format;
670 struct intel_mipmap_tree *planar_mt = NULL;
671
672 for (int i = 0; i < f->nplanes; i++) {
673 const int index = f->planes[i].buffer_index;
674 const uint32_t dri_format = f->planes[i].dri_format;
675 const mesa_format format = driImageFormatToGLFormat(dri_format);
676 const uint32_t width = image->width >> f->planes[i].width_shift;
677 const uint32_t height = image->height >> f->planes[i].height_shift;
678
679 /* Disable creation of the texture's aux buffers because the driver
680 * exposes no EGL API to manage them. That is, there is no API for
681 * resolving the aux buffer's content to the main buffer nor for
682 * invalidating the aux buffer's content.
683 */
684 struct intel_mipmap_tree *mt =
685 intel_miptree_create_for_bo(brw, image->bo, format,
686 image->offsets[index],
687 width, height, 1,
688 image->strides[index],
689 tiling,
690 MIPTREE_CREATE_NO_AUX);
691 if (mt == NULL) {
692 intel_miptree_release(&planar_mt);
693 return NULL;
694 }
695
696 mt->target = target;
697
698 if (i == 0)
699 planar_mt = mt;
700 else
701 planar_mt->plane[i - 1] = mt;
702 }
703
704 planar_mt->drm_modifier = image->modifier;
705
706 return planar_mt;
707 }
708
709 static bool
710 create_ccs_buf_for_image(struct brw_context *brw,
711 __DRIimage *image,
712 struct intel_mipmap_tree *mt,
713 enum isl_aux_state initial_state)
714 {
715 struct isl_surf temp_ccs_surf;
716
717 /* CCS is only supported for very simple miptrees */
718 assert(image->aux_offset != 0 && image->aux_pitch != 0);
719 assert(image->tile_x == 0 && image->tile_y == 0);
720 assert(mt->surf.samples == 1);
721 assert(mt->surf.levels == 1);
722 assert(mt->surf.logical_level0_px.depth == 1);
723 assert(mt->surf.logical_level0_px.array_len == 1);
724 assert(mt->first_level == 0);
725 assert(mt->last_level == 0);
726
727 /* We shouldn't already have a CCS */
728 assert(!mt->aux_buf);
729
730 if (!isl_surf_get_ccs_surf(&brw->isl_dev, &mt->surf, &temp_ccs_surf,
731 image->aux_pitch))
732 return false;
733
734 assert(image->aux_offset < image->bo->size);
735 assert(temp_ccs_surf.size_B <= image->bo->size - image->aux_offset);
736
737 mt->aux_buf = calloc(sizeof(*mt->aux_buf), 1);
738 if (mt->aux_buf == NULL)
739 return false;
740
741 mt->aux_state = create_aux_state_map(mt, initial_state);
742 if (!mt->aux_state) {
743 free(mt->aux_buf);
744 mt->aux_buf = NULL;
745 return false;
746 }
747
748 /* On gen10+ we start using an extra space in the aux buffer to store the
749 * indirect clear color. However, if we imported an image from the window
750 * system with CCS, we don't have the extra space at the end of the aux
751 * buffer. So create a new bo here that will store that clear color.
752 */
753 if (brw->isl_dev.ss.clear_color_state_size > 0) {
754 mt->aux_buf->clear_color_bo =
755 brw_bo_alloc_tiled(brw->bufmgr, "clear_color_bo",
756 brw->isl_dev.ss.clear_color_state_size,
757 BRW_MEMZONE_OTHER, I915_TILING_NONE, 0,
758 BO_ALLOC_ZEROED);
759 if (!mt->aux_buf->clear_color_bo) {
760 free(mt->aux_buf);
761 mt->aux_buf = NULL;
762 return false;
763 }
764 }
765
766 mt->aux_buf->bo = image->bo;
767 brw_bo_reference(image->bo);
768
769 mt->aux_buf->offset = image->aux_offset;
770 mt->aux_buf->surf = temp_ccs_surf;
771
772 return true;
773 }
774
775 struct intel_mipmap_tree *
776 intel_miptree_create_for_dri_image(struct brw_context *brw,
777 __DRIimage *image, GLenum target,
778 mesa_format format,
779 bool allow_internal_aux)
780 {
781 uint32_t bo_tiling, bo_swizzle;
782 brw_bo_get_tiling(image->bo, &bo_tiling, &bo_swizzle);
783
784 const struct isl_drm_modifier_info *mod_info =
785 isl_drm_modifier_get_info(image->modifier);
786
787 const enum isl_tiling tiling =
788 mod_info ? mod_info->tiling : isl_tiling_from_i915_tiling(bo_tiling);
789
790 if (image->planar_format && image->planar_format->nplanes > 1)
791 return miptree_create_for_planar_image(brw, image, target, tiling);
792
793 if (image->planar_format)
794 assert(image->planar_format->planes[0].dri_format == image->dri_format);
795
796 if (!brw->ctx.TextureFormatSupported[format]) {
797 /* The texture storage paths in core Mesa detect if the driver does not
798 * support the user-requested format, and then searches for a
799 * fallback format. The DRIimage code bypasses core Mesa, though. So we
800 * do the fallbacks here for important formats.
801 *
802 * We must support DRM_FOURCC_XBGR8888 textures because the Android
803 * framework produces HAL_PIXEL_FORMAT_RGBX8888 winsys surfaces, which
804 * the Chrome OS compositor consumes as dma_buf EGLImages.
805 */
806 format = _mesa_format_fallback_rgbx_to_rgba(format);
807 }
808
809 if (!brw->ctx.TextureFormatSupported[format])
810 return NULL;
811
812 enum intel_miptree_create_flags mt_create_flags = 0;
813
814 /* If this image comes in from a window system, we have different
815 * requirements than if it comes in via an EGL import operation. Window
816 * system images can use any form of auxiliary compression we wish because
817 * they get "flushed" before being handed off to the window system and we
818 * have the opportunity to do resolves. Non window-system images, on the
819 * other hand, have no resolve point so we can't have aux without a
820 * modifier.
821 */
822 if (!allow_internal_aux)
823 mt_create_flags |= MIPTREE_CREATE_NO_AUX;
824
825 /* If we have a modifier which specifies aux, don't create one yet */
826 if (mod_info && mod_info->aux_usage != ISL_AUX_USAGE_NONE)
827 mt_create_flags |= MIPTREE_CREATE_NO_AUX;
828
829 /* Disable creation of the texture's aux buffers because the driver exposes
830 * no EGL API to manage them. That is, there is no API for resolving the aux
831 * buffer's content to the main buffer nor for invalidating the aux buffer's
832 * content.
833 */
834 struct intel_mipmap_tree *mt =
835 intel_miptree_create_for_bo(brw, image->bo, format,
836 image->offset, image->width, image->height, 1,
837 image->pitch, tiling, mt_create_flags);
838 if (mt == NULL)
839 return NULL;
840
841 mt->target = target;
842 mt->level[0].level_x = image->tile_x;
843 mt->level[0].level_y = image->tile_y;
844 mt->drm_modifier = image->modifier;
845
846 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
847 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
848 * trouble resolving back to destination image due to alignment issues.
849 */
850 const struct gen_device_info *devinfo = &brw->screen->devinfo;
851 if (!devinfo->has_surface_tile_offset) {
852 uint32_t draw_x, draw_y;
853 intel_miptree_get_tile_offsets(mt, 0, 0, &draw_x, &draw_y);
854
855 if (draw_x != 0 || draw_y != 0) {
856 _mesa_error(&brw->ctx, GL_INVALID_OPERATION, __func__);
857 intel_miptree_release(&mt);
858 return NULL;
859 }
860 }
861
862 if (mod_info && mod_info->aux_usage != ISL_AUX_USAGE_NONE) {
863 assert(mod_info->aux_usage == ISL_AUX_USAGE_CCS_E);
864
865 mt->aux_usage = mod_info->aux_usage;
866 /* If we are a window system buffer, then we can support fast-clears
867 * even if the modifier doesn't support them by doing a partial resolve
868 * as part of the flush operation.
869 */
870 mt->supports_fast_clear =
871 allow_internal_aux || mod_info->supports_clear_color;
872
873 /* We don't know the actual state of the surface when we get it but we
874 * can make a pretty good guess based on the modifier. What we do know
875 * for sure is that it isn't in the AUX_INVALID state, so we just assume
876 * a worst case of compression.
877 */
878 enum isl_aux_state initial_state =
879 isl_drm_modifier_get_default_aux_state(image->modifier);
880
881 if (!create_ccs_buf_for_image(brw, image, mt, initial_state)) {
882 intel_miptree_release(&mt);
883 return NULL;
884 }
885 }
886
887 /* Don't assume coherency for imported EGLimages. We don't know what
888 * external clients are going to do with it. They may scan it out.
889 */
890 image->bo->cache_coherent = false;
891
892 return mt;
893 }
894
895 /**
896 * For a singlesample renderbuffer, this simply wraps the given BO with a
897 * miptree.
898 *
899 * For a multisample renderbuffer, this wraps the window system's
900 * (singlesample) BO with a singlesample miptree attached to the
901 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
902 * that will contain the actual rendering (which is lazily resolved to
903 * irb->singlesample_mt).
904 */
905 bool
906 intel_update_winsys_renderbuffer_miptree(struct brw_context *intel,
907 struct intel_renderbuffer *irb,
908 struct intel_mipmap_tree *singlesample_mt,
909 uint32_t width, uint32_t height,
910 uint32_t pitch)
911 {
912 struct intel_mipmap_tree *multisample_mt = NULL;
913 struct gl_renderbuffer *rb = &irb->Base.Base;
914 mesa_format format = rb->Format;
915 const unsigned num_samples = MAX2(rb->NumSamples, 1);
916
917 /* Only the front and back buffers, which are color buffers, are allocated
918 * through the image loader.
919 */
920 assert(_mesa_get_format_base_format(format) == GL_RGB ||
921 _mesa_get_format_base_format(format) == GL_RGBA);
922
923 assert(singlesample_mt);
924
925 if (num_samples == 1) {
926 intel_miptree_release(&irb->mt);
927 irb->mt = singlesample_mt;
928
929 assert(!irb->singlesample_mt);
930 } else {
931 intel_miptree_release(&irb->singlesample_mt);
932 irb->singlesample_mt = singlesample_mt;
933
934 if (!irb->mt ||
935 irb->mt->surf.logical_level0_px.width != width ||
936 irb->mt->surf.logical_level0_px.height != height) {
937 multisample_mt = intel_miptree_create_for_renderbuffer(intel,
938 format,
939 width,
940 height,
941 num_samples);
942 if (!multisample_mt)
943 goto fail;
944
945 irb->need_downsample = false;
946 intel_miptree_release(&irb->mt);
947 irb->mt = multisample_mt;
948 }
949 }
950 return true;
951
952 fail:
953 intel_miptree_release(&irb->mt);
954 return false;
955 }
956
957 struct intel_mipmap_tree*
958 intel_miptree_create_for_renderbuffer(struct brw_context *brw,
959 mesa_format format,
960 uint32_t width,
961 uint32_t height,
962 uint32_t num_samples)
963 {
964 struct intel_mipmap_tree *mt;
965 uint32_t depth = 1;
966 GLenum target = num_samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
967
968 mt = intel_miptree_create(brw, target, format, 0, 0,
969 width, height, depth, num_samples,
970 MIPTREE_CREATE_BUSY);
971 if (!mt)
972 goto fail;
973
974 return mt;
975
976 fail:
977 intel_miptree_release(&mt);
978 return NULL;
979 }
980
981 void
982 intel_miptree_reference(struct intel_mipmap_tree **dst,
983 struct intel_mipmap_tree *src)
984 {
985 if (*dst == src)
986 return;
987
988 intel_miptree_release(dst);
989
990 if (src) {
991 src->refcount++;
992 DBG("%s %p refcount now %d\n", __func__, src, src->refcount);
993 }
994
995 *dst = src;
996 }
997
998 static void
999 intel_miptree_aux_buffer_free(struct intel_miptree_aux_buffer *aux_buf)
1000 {
1001 if (aux_buf == NULL)
1002 return;
1003
1004 brw_bo_unreference(aux_buf->bo);
1005 brw_bo_unreference(aux_buf->clear_color_bo);
1006
1007 free(aux_buf);
1008 }
1009
1010 void
1011 intel_miptree_release(struct intel_mipmap_tree **mt)
1012 {
1013 if (!*mt)
1014 return;
1015
1016 DBG("%s %p refcount will be %d\n", __func__, *mt, (*mt)->refcount - 1);
1017 if (--(*mt)->refcount <= 0) {
1018 GLuint i;
1019
1020 DBG("%s deleting %p\n", __func__, *mt);
1021
1022 brw_bo_unreference((*mt)->bo);
1023 intel_miptree_release(&(*mt)->stencil_mt);
1024 intel_miptree_release(&(*mt)->shadow_mt);
1025 intel_miptree_aux_buffer_free((*mt)->aux_buf);
1026 free_aux_state_map((*mt)->aux_state);
1027
1028 intel_miptree_release(&(*mt)->plane[0]);
1029 intel_miptree_release(&(*mt)->plane[1]);
1030
1031 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
1032 free((*mt)->level[i].slice);
1033 }
1034
1035 free(*mt);
1036 }
1037 *mt = NULL;
1038 }
1039
1040
1041 void
1042 intel_get_image_dims(struct gl_texture_image *image,
1043 int *width, int *height, int *depth)
1044 {
1045 switch (image->TexObject->Target) {
1046 case GL_TEXTURE_1D_ARRAY:
1047 /* For a 1D Array texture the OpenGL API will treat the image height as
1048 * the number of array slices. For Intel hardware, we treat the 1D array
1049 * as a 2D Array with a height of 1. So, here we want to swap image
1050 * height and depth.
1051 */
1052 assert(image->Depth == 1);
1053 *width = image->Width;
1054 *height = 1;
1055 *depth = image->Height;
1056 break;
1057 case GL_TEXTURE_CUBE_MAP:
1058 /* For Cube maps, the mesa/main api layer gives us a depth of 1 even
1059 * though we really have 6 slices.
1060 */
1061 assert(image->Depth == 1);
1062 *width = image->Width;
1063 *height = image->Height;
1064 *depth = 6;
1065 break;
1066 default:
1067 *width = image->Width;
1068 *height = image->Height;
1069 *depth = image->Depth;
1070 break;
1071 }
1072 }
1073
1074 /**
1075 * Can the image be pulled into a unified mipmap tree? This mirrors
1076 * the completeness test in a lot of ways.
1077 *
1078 * Not sure whether I want to pass gl_texture_image here.
1079 */
1080 bool
1081 intel_miptree_match_image(struct intel_mipmap_tree *mt,
1082 struct gl_texture_image *image)
1083 {
1084 struct intel_texture_image *intelImage = intel_texture_image(image);
1085 GLuint level = intelImage->base.Base.Level;
1086 int width, height, depth;
1087
1088 /* glTexImage* choose the texture object based on the target passed in, and
1089 * objects can't change targets over their lifetimes, so this should be
1090 * true.
1091 */
1092 assert(image->TexObject->Target == mt->target);
1093
1094 mesa_format mt_format = mt->format;
1095 if (mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT && mt->stencil_mt)
1096 mt_format = MESA_FORMAT_Z24_UNORM_S8_UINT;
1097 if (mt->format == MESA_FORMAT_Z_FLOAT32 && mt->stencil_mt)
1098 mt_format = MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
1099
1100 if (_mesa_get_srgb_format_linear(image->TexFormat) !=
1101 _mesa_get_srgb_format_linear(mt_format))
1102 return false;
1103
1104 intel_get_image_dims(image, &width, &height, &depth);
1105
1106 if (mt->target == GL_TEXTURE_CUBE_MAP)
1107 depth = 6;
1108
1109 if (level >= mt->surf.levels)
1110 return false;
1111
1112 const unsigned level_depth =
1113 mt->surf.dim == ISL_SURF_DIM_3D ?
1114 minify(mt->surf.logical_level0_px.depth, level) :
1115 mt->surf.logical_level0_px.array_len;
1116
1117 return width == minify(mt->surf.logical_level0_px.width, level) &&
1118 height == minify(mt->surf.logical_level0_px.height, level) &&
1119 depth == level_depth &&
1120 MAX2(image->NumSamples, 1) == mt->surf.samples;
1121 }
1122
1123 void
1124 intel_miptree_get_image_offset(const struct intel_mipmap_tree *mt,
1125 GLuint level, GLuint slice,
1126 GLuint *x, GLuint *y)
1127 {
1128 if (level == 0 && slice == 0) {
1129 *x = mt->level[0].level_x;
1130 *y = mt->level[0].level_y;
1131 return;
1132 }
1133
1134 uint32_t x_offset_sa, y_offset_sa;
1135
1136 /* Miptree itself can have an offset only if it represents a single
1137 * slice in an imported buffer object.
1138 * See intel_miptree_create_for_dri_image().
1139 */
1140 assert(mt->level[0].level_x == 0);
1141 assert(mt->level[0].level_y == 0);
1142
1143 /* Given level is relative to level zero while the miptree may be
1144 * represent just a subset of all levels starting from 'first_level'.
1145 */
1146 assert(level >= mt->first_level);
1147 level -= mt->first_level;
1148
1149 const unsigned z = mt->surf.dim == ISL_SURF_DIM_3D ? slice : 0;
1150 slice = mt->surf.dim == ISL_SURF_DIM_3D ? 0 : slice;
1151 isl_surf_get_image_offset_el(&mt->surf, level, slice, z,
1152 &x_offset_sa, &y_offset_sa);
1153
1154 *x = x_offset_sa;
1155 *y = y_offset_sa;
1156 }
1157
1158
1159 /**
1160 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1161 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1162 * and tile_h is set to 1.
1163 */
1164 void
1165 intel_get_tile_dims(enum isl_tiling tiling, uint32_t cpp,
1166 uint32_t *tile_w, uint32_t *tile_h)
1167 {
1168 switch (tiling) {
1169 case ISL_TILING_X:
1170 *tile_w = 512;
1171 *tile_h = 8;
1172 break;
1173 case ISL_TILING_Y0:
1174 *tile_w = 128;
1175 *tile_h = 32;
1176 break;
1177 case ISL_TILING_LINEAR:
1178 *tile_w = cpp;
1179 *tile_h = 1;
1180 break;
1181 default:
1182 unreachable("not reached");
1183 }
1184 }
1185
1186
1187 /**
1188 * This function computes masks that may be used to select the bits of the X
1189 * and Y coordinates that indicate the offset within a tile. If the BO is
1190 * untiled, the masks are set to 0.
1191 */
1192 void
1193 intel_get_tile_masks(enum isl_tiling tiling, uint32_t cpp,
1194 uint32_t *mask_x, uint32_t *mask_y)
1195 {
1196 uint32_t tile_w_bytes, tile_h;
1197
1198 intel_get_tile_dims(tiling, cpp, &tile_w_bytes, &tile_h);
1199
1200 *mask_x = tile_w_bytes / cpp - 1;
1201 *mask_y = tile_h - 1;
1202 }
1203
1204 /**
1205 * Compute the offset (in bytes) from the start of the BO to the given x
1206 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1207 * multiples of the tile size.
1208 */
1209 uint32_t
1210 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree *mt,
1211 uint32_t x, uint32_t y)
1212 {
1213 int cpp = mt->cpp;
1214 uint32_t pitch = mt->surf.row_pitch_B;
1215
1216 switch (mt->surf.tiling) {
1217 default:
1218 unreachable("not reached");
1219 case ISL_TILING_LINEAR:
1220 return y * pitch + x * cpp;
1221 case ISL_TILING_X:
1222 assert((x % (512 / cpp)) == 0);
1223 assert((y % 8) == 0);
1224 return y * pitch + x / (512 / cpp) * 4096;
1225 case ISL_TILING_Y0:
1226 assert((x % (128 / cpp)) == 0);
1227 assert((y % 32) == 0);
1228 return y * pitch + x / (128 / cpp) * 4096;
1229 }
1230 }
1231
1232 /**
1233 * Rendering with tiled buffers requires that the base address of the buffer
1234 * be aligned to a page boundary. For renderbuffers, and sometimes with
1235 * textures, we may want the surface to point at a texture image level that
1236 * isn't at a page boundary.
1237 *
1238 * This function returns an appropriately-aligned base offset
1239 * according to the tiling restrictions, plus any required x/y offset
1240 * from there.
1241 */
1242 uint32_t
1243 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree *mt,
1244 GLuint level, GLuint slice,
1245 uint32_t *tile_x,
1246 uint32_t *tile_y)
1247 {
1248 uint32_t x, y;
1249 uint32_t mask_x, mask_y;
1250
1251 intel_get_tile_masks(mt->surf.tiling, mt->cpp, &mask_x, &mask_y);
1252 intel_miptree_get_image_offset(mt, level, slice, &x, &y);
1253
1254 *tile_x = x & mask_x;
1255 *tile_y = y & mask_y;
1256
1257 return intel_miptree_get_aligned_offset(mt, x & ~mask_x, y & ~mask_y);
1258 }
1259
1260 static void
1261 intel_miptree_copy_slice_sw(struct brw_context *brw,
1262 struct intel_mipmap_tree *src_mt,
1263 unsigned src_level, unsigned src_layer,
1264 struct intel_mipmap_tree *dst_mt,
1265 unsigned dst_level, unsigned dst_layer,
1266 unsigned width, unsigned height)
1267 {
1268 void *src, *dst;
1269 ptrdiff_t src_stride, dst_stride;
1270 const unsigned cpp = (isl_format_get_layout(dst_mt->surf.format)->bpb / 8);
1271
1272 intel_miptree_map(brw, src_mt,
1273 src_level, src_layer,
1274 0, 0,
1275 width, height,
1276 GL_MAP_READ_BIT | BRW_MAP_DIRECT_BIT,
1277 &src, &src_stride);
1278
1279 intel_miptree_map(brw, dst_mt,
1280 dst_level, dst_layer,
1281 0, 0,
1282 width, height,
1283 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
1284 BRW_MAP_DIRECT_BIT,
1285 &dst, &dst_stride);
1286
1287 DBG("sw blit %s mt %p %p/%"PRIdPTR" -> %s mt %p %p/%"PRIdPTR" (%dx%d)\n",
1288 _mesa_get_format_name(src_mt->format),
1289 src_mt, src, src_stride,
1290 _mesa_get_format_name(dst_mt->format),
1291 dst_mt, dst, dst_stride,
1292 width, height);
1293
1294 int row_size = cpp * width;
1295 if (src_stride == row_size &&
1296 dst_stride == row_size) {
1297 memcpy(dst, src, row_size * height);
1298 } else {
1299 for (int i = 0; i < height; i++) {
1300 memcpy(dst, src, row_size);
1301 dst += dst_stride;
1302 src += src_stride;
1303 }
1304 }
1305
1306 intel_miptree_unmap(brw, dst_mt, dst_level, dst_layer);
1307 intel_miptree_unmap(brw, src_mt, src_level, src_layer);
1308
1309 /* Don't forget to copy the stencil data over, too. We could have skipped
1310 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1311 * shuffling the two data sources in/out of temporary storage instead of
1312 * the direct mapping we get this way.
1313 */
1314 if (dst_mt->stencil_mt) {
1315 assert(src_mt->stencil_mt);
1316 intel_miptree_copy_slice_sw(brw,
1317 src_mt->stencil_mt, src_level, src_layer,
1318 dst_mt->stencil_mt, dst_level, dst_layer,
1319 width, height);
1320 }
1321 }
1322
1323 void
1324 intel_miptree_copy_slice(struct brw_context *brw,
1325 struct intel_mipmap_tree *src_mt,
1326 unsigned src_level, unsigned src_layer,
1327 struct intel_mipmap_tree *dst_mt,
1328 unsigned dst_level, unsigned dst_layer)
1329
1330 {
1331 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1332 mesa_format format = src_mt->format;
1333 unsigned width = minify(src_mt->surf.phys_level0_sa.width,
1334 src_level - src_mt->first_level);
1335 unsigned height = minify(src_mt->surf.phys_level0_sa.height,
1336 src_level - src_mt->first_level);
1337
1338 assert(src_layer < get_num_phys_layers(&src_mt->surf,
1339 src_level - src_mt->first_level));
1340
1341 assert(_mesa_get_srgb_format_linear(src_mt->format) ==
1342 _mesa_get_srgb_format_linear(dst_mt->format));
1343
1344 DBG("validate blit mt %s %p %d,%d -> mt %s %p %d,%d (%dx%d)\n",
1345 _mesa_get_format_name(src_mt->format),
1346 src_mt, src_level, src_layer,
1347 _mesa_get_format_name(dst_mt->format),
1348 dst_mt, dst_level, dst_layer,
1349 width, height);
1350
1351 if (devinfo->gen >= 6) {
1352 /* On gen6 and above, we just use blorp. It's faster than the blitter
1353 * and can handle everything without software fallbacks.
1354 */
1355 brw_blorp_copy_miptrees(brw,
1356 src_mt, src_level, src_layer,
1357 dst_mt, dst_level, dst_layer,
1358 0, 0, 0, 0, width, height);
1359
1360 if (src_mt->stencil_mt) {
1361 assert(dst_mt->stencil_mt);
1362 brw_blorp_copy_miptrees(brw,
1363 src_mt->stencil_mt, src_level, src_layer,
1364 dst_mt->stencil_mt, dst_level, dst_layer,
1365 0, 0, 0, 0, width, height);
1366 }
1367 return;
1368 }
1369
1370 if (dst_mt->compressed) {
1371 unsigned int i, j;
1372 _mesa_get_format_block_size(dst_mt->format, &i, &j);
1373 height = ALIGN_NPOT(height, j) / j;
1374 width = ALIGN_NPOT(width, i) / i;
1375 }
1376
1377 /* Gen4-5 doesn't support separate stencil */
1378 assert(!src_mt->stencil_mt);
1379
1380 uint32_t dst_x, dst_y, src_x, src_y;
1381 intel_miptree_get_image_offset(dst_mt, dst_level, dst_layer,
1382 &dst_x, &dst_y);
1383 intel_miptree_get_image_offset(src_mt, src_level, src_layer,
1384 &src_x, &src_y);
1385
1386 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1387 _mesa_get_format_name(src_mt->format),
1388 src_mt, src_x, src_y, src_mt->surf.row_pitch_B,
1389 _mesa_get_format_name(dst_mt->format),
1390 dst_mt, dst_x, dst_y, dst_mt->surf.row_pitch_B,
1391 width, height);
1392
1393 if (!intel_miptree_blit(brw,
1394 src_mt, src_level, src_layer, 0, 0, false,
1395 dst_mt, dst_level, dst_layer, 0, 0, false,
1396 width, height, COLOR_LOGICOP_COPY)) {
1397 perf_debug("miptree validate blit for %s failed\n",
1398 _mesa_get_format_name(format));
1399
1400 intel_miptree_copy_slice_sw(brw,
1401 src_mt, src_level, src_layer,
1402 dst_mt, dst_level, dst_layer,
1403 width, height);
1404 }
1405 }
1406
1407 /**
1408 * Copies the image's current data to the given miptree, and associates that
1409 * miptree with the image.
1410 */
1411 void
1412 intel_miptree_copy_teximage(struct brw_context *brw,
1413 struct intel_texture_image *intelImage,
1414 struct intel_mipmap_tree *dst_mt)
1415 {
1416 struct intel_mipmap_tree *src_mt = intelImage->mt;
1417 struct intel_texture_object *intel_obj =
1418 intel_texture_object(intelImage->base.Base.TexObject);
1419 int level = intelImage->base.Base.Level;
1420 const unsigned face = intelImage->base.Base.Face;
1421 unsigned start_layer, end_layer;
1422
1423 if (intel_obj->base.Target == GL_TEXTURE_1D_ARRAY) {
1424 assert(face == 0);
1425 assert(intelImage->base.Base.Height);
1426 start_layer = 0;
1427 end_layer = intelImage->base.Base.Height - 1;
1428 } else if (face > 0) {
1429 start_layer = face;
1430 end_layer = face;
1431 } else {
1432 assert(intelImage->base.Base.Depth);
1433 start_layer = 0;
1434 end_layer = intelImage->base.Base.Depth - 1;
1435 }
1436
1437 for (unsigned i = start_layer; i <= end_layer; i++) {
1438 intel_miptree_copy_slice(brw,
1439 src_mt, level, i,
1440 dst_mt, level, i);
1441 }
1442
1443 intel_miptree_reference(&intelImage->mt, dst_mt);
1444 intel_obj->needs_validate = true;
1445 }
1446
1447 static struct intel_miptree_aux_buffer *
1448 intel_alloc_aux_buffer(struct brw_context *brw,
1449 const struct isl_surf *aux_surf,
1450 bool wants_memset,
1451 uint8_t memset_value)
1452 {
1453 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1454 if (!buf)
1455 return false;
1456
1457 uint64_t size = aux_surf->size_B;
1458
1459 const bool has_indirect_clear = brw->isl_dev.ss.clear_color_state_size > 0;
1460 if (has_indirect_clear) {
1461 /* On CNL+, instead of setting the clear color in the SURFACE_STATE, we
1462 * will set a pointer to a dword somewhere that contains the color. So,
1463 * allocate the space for the clear color value here on the aux buffer.
1464 */
1465 buf->clear_color_offset = size;
1466 size += brw->isl_dev.ss.clear_color_state_size;
1467 }
1468
1469 /* If the buffer needs to be initialised (requiring the buffer to be
1470 * immediately mapped to cpu space for writing), do not use the gpu access
1471 * flag which can cause an unnecessary delay if the backing pages happened
1472 * to be just used by the GPU.
1473 */
1474 const bool alloc_zeroed = wants_memset && memset_value == 0;
1475 const bool needs_memset =
1476 !alloc_zeroed && (wants_memset || has_indirect_clear);
1477 const uint32_t alloc_flags =
1478 alloc_zeroed ? BO_ALLOC_ZEROED : (needs_memset ? 0 : BO_ALLOC_BUSY);
1479
1480 /* ISL has stricter set of alignment rules then the drm allocator.
1481 * Therefore one can pass the ISL dimensions in terms of bytes instead of
1482 * trying to recalculate based on different format block sizes.
1483 */
1484 buf->bo = brw_bo_alloc_tiled(brw->bufmgr, "aux-miptree", size,
1485 BRW_MEMZONE_OTHER, I915_TILING_Y,
1486 aux_surf->row_pitch_B, alloc_flags);
1487 if (!buf->bo) {
1488 free(buf);
1489 return NULL;
1490 }
1491
1492 /* Initialize the bo to the desired value */
1493 if (needs_memset) {
1494 assert(!(alloc_flags & BO_ALLOC_BUSY));
1495
1496 void *map = brw_bo_map(brw, buf->bo, MAP_WRITE | MAP_RAW);
1497 if (map == NULL) {
1498 intel_miptree_aux_buffer_free(buf);
1499 return NULL;
1500 }
1501
1502 /* Memset the aux_surf portion of the BO. */
1503 if (wants_memset)
1504 memset(map, memset_value, aux_surf->size_B);
1505
1506 /* Zero the indirect clear color to match ::fast_clear_color. */
1507 if (has_indirect_clear) {
1508 memset((char *)map + buf->clear_color_offset, 0,
1509 brw->isl_dev.ss.clear_color_state_size);
1510 }
1511
1512 brw_bo_unmap(buf->bo);
1513 }
1514
1515 if (has_indirect_clear) {
1516 buf->clear_color_bo = buf->bo;
1517 brw_bo_reference(buf->clear_color_bo);
1518 }
1519
1520 buf->surf = *aux_surf;
1521
1522 return buf;
1523 }
1524
1525
1526 /**
1527 * Helper for intel_miptree_alloc_aux() that sets
1528 * \c mt->level[level].has_hiz. Return true if and only if
1529 * \c has_hiz was set.
1530 */
1531 static bool
1532 intel_miptree_level_enable_hiz(struct brw_context *brw,
1533 struct intel_mipmap_tree *mt,
1534 uint32_t level)
1535 {
1536 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1537
1538 assert(mt->aux_buf);
1539 assert(mt->surf.size_B > 0);
1540
1541 if (devinfo->gen >= 8 || devinfo->is_haswell) {
1542 uint32_t width = minify(mt->surf.phys_level0_sa.width, level);
1543 uint32_t height = minify(mt->surf.phys_level0_sa.height, level);
1544
1545 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1546 * and the height is 4 aligned. This allows our HiZ support
1547 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1548 * we can grow the width & height to allow the HiZ op to
1549 * force the proper size alignments.
1550 */
1551 if (level > 0 && ((width & 7) || (height & 3))) {
1552 DBG("mt %p level %d: HiZ DISABLED\n", mt, level);
1553 return false;
1554 }
1555 }
1556
1557 DBG("mt %p level %d: HiZ enabled\n", mt, level);
1558 mt->level[level].has_hiz = true;
1559 return true;
1560 }
1561
1562
1563 /**
1564 * Allocate the initial aux surface for a miptree based on mt->aux_usage
1565 *
1566 * Since MCS, HiZ, and CCS_E can compress more than just clear color, we
1567 * create the auxiliary surfaces up-front. CCS_D, on the other hand, can only
1568 * compress clear color so we wait until an actual fast-clear to allocate it.
1569 */
1570 bool
1571 intel_miptree_alloc_aux(struct brw_context *brw,
1572 struct intel_mipmap_tree *mt)
1573 {
1574 assert(mt->aux_buf == NULL);
1575
1576 /* Get the aux buf allocation parameters for this miptree. */
1577 enum isl_aux_state initial_state;
1578 uint8_t memset_value;
1579 struct isl_surf aux_surf;
1580 bool aux_surf_ok = false;
1581
1582 switch (mt->aux_usage) {
1583 case ISL_AUX_USAGE_NONE:
1584 aux_surf.size_B = 0;
1585 aux_surf_ok = true;
1586 break;
1587 case ISL_AUX_USAGE_HIZ:
1588 initial_state = ISL_AUX_STATE_AUX_INVALID;
1589 memset_value = 0;
1590 aux_surf_ok = isl_surf_get_hiz_surf(&brw->isl_dev, &mt->surf, &aux_surf);
1591 break;
1592 case ISL_AUX_USAGE_MCS:
1593 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1594 *
1595 * When MCS buffer is enabled and bound to MSRT, it is required that
1596 * it is cleared prior to any rendering.
1597 *
1598 * Since we don't use the MCS buffer for any purpose other than
1599 * rendering, it makes sense to just clear it immediately upon
1600 * allocation.
1601 *
1602 * Note: the clear value for MCS buffers is all 1's, so we memset to
1603 * 0xff.
1604 */
1605 initial_state = ISL_AUX_STATE_CLEAR;
1606 memset_value = 0xFF;
1607 aux_surf_ok = isl_surf_get_mcs_surf(&brw->isl_dev, &mt->surf, &aux_surf);
1608 break;
1609 case ISL_AUX_USAGE_CCS_D:
1610 case ISL_AUX_USAGE_CCS_E:
1611 /* When CCS_E is used, we need to ensure that the CCS starts off in a
1612 * valid state. From the Sky Lake PRM, "MCS Buffer for Render
1613 * Target(s)":
1614 *
1615 * "If Software wants to enable Color Compression without Fast
1616 * clear, Software needs to initialize MCS with zeros."
1617 *
1618 * A CCS value of 0 indicates that the corresponding block is in the
1619 * pass-through state which is what we want.
1620 *
1621 * For CCS_D, do the same thing. On gen9+, this avoids having any
1622 * undefined bits in the aux buffer.
1623 */
1624 initial_state = ISL_AUX_STATE_PASS_THROUGH;
1625 memset_value = 0;
1626 aux_surf_ok =
1627 isl_surf_get_ccs_surf(&brw->isl_dev, &mt->surf, &aux_surf, 0);
1628 break;
1629
1630 default:
1631 unreachable("Invalid aux usage");
1632 }
1633
1634 /* We should have a valid aux_surf. */
1635 if (!aux_surf_ok)
1636 return false;
1637
1638 /* No work is needed for a zero-sized auxiliary buffer. */
1639 if (aux_surf.size_B == 0)
1640 return true;
1641
1642 /* Create the aux_state for the auxiliary buffer. */
1643 mt->aux_state = create_aux_state_map(mt, initial_state);
1644 if (mt->aux_state == NULL)
1645 return false;
1646
1647 /* Allocate the auxiliary buffer. */
1648 const bool needs_memset = initial_state != ISL_AUX_STATE_AUX_INVALID;
1649 mt->aux_buf = intel_alloc_aux_buffer(brw, &aux_surf, needs_memset,
1650 memset_value);
1651 if (mt->aux_buf == NULL) {
1652 free_aux_state_map(mt->aux_state);
1653 mt->aux_state = NULL;
1654 return false;
1655 }
1656
1657 /* Perform aux_usage-specific initialization. */
1658 if (mt->aux_usage == ISL_AUX_USAGE_HIZ) {
1659 for (unsigned level = mt->first_level; level <= mt->last_level; ++level)
1660 intel_miptree_level_enable_hiz(brw, mt, level);
1661 }
1662
1663 return true;
1664 }
1665
1666
1667 /**
1668 * Can the miptree sample using the hiz buffer?
1669 */
1670 bool
1671 intel_miptree_sample_with_hiz(struct brw_context *brw,
1672 struct intel_mipmap_tree *mt)
1673 {
1674 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1675
1676 if (!devinfo->has_sample_with_hiz) {
1677 return false;
1678 }
1679
1680 if (!mt->aux_buf) {
1681 return false;
1682 }
1683
1684 /* It seems the hardware won't fallback to the depth buffer if some of the
1685 * mipmap levels aren't available in the HiZ buffer. So we need all levels
1686 * of the texture to be HiZ enabled.
1687 */
1688 for (unsigned level = 0; level < mt->surf.levels; ++level) {
1689 if (!intel_miptree_level_has_hiz(mt, level))
1690 return false;
1691 }
1692
1693 /* If compressed multisampling is enabled, then we use it for the auxiliary
1694 * buffer instead.
1695 *
1696 * From the BDW PRM (Volume 2d: Command Reference: Structures
1697 * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
1698 *
1699 * "If this field is set to AUX_HIZ, Number of Multisamples must be
1700 * MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
1701 *
1702 * There is no such blurb for 1D textures, but there is sufficient evidence
1703 * that this is broken on SKL+.
1704 */
1705 return (mt->surf.samples == 1 &&
1706 mt->target != GL_TEXTURE_3D &&
1707 mt->target != GL_TEXTURE_1D /* gen9+ restriction */);
1708 }
1709
1710 /**
1711 * Does the miptree slice have hiz enabled?
1712 */
1713 bool
1714 intel_miptree_level_has_hiz(const struct intel_mipmap_tree *mt, uint32_t level)
1715 {
1716 intel_miptree_check_level_layer(mt, level, 0);
1717 return mt->level[level].has_hiz;
1718 }
1719
1720 static inline uint32_t
1721 miptree_level_range_length(const struct intel_mipmap_tree *mt,
1722 uint32_t start_level, uint32_t num_levels)
1723 {
1724 assert(start_level >= mt->first_level);
1725 assert(start_level <= mt->last_level);
1726
1727 if (num_levels == INTEL_REMAINING_LAYERS)
1728 num_levels = mt->last_level - start_level + 1;
1729 /* Check for overflow */
1730 assert(start_level + num_levels >= start_level);
1731 assert(start_level + num_levels <= mt->last_level + 1);
1732
1733 return num_levels;
1734 }
1735
1736 static inline uint32_t
1737 miptree_layer_range_length(const struct intel_mipmap_tree *mt, uint32_t level,
1738 uint32_t start_layer, uint32_t num_layers)
1739 {
1740 assert(level <= mt->last_level);
1741
1742 const uint32_t total_num_layers = brw_get_num_logical_layers(mt, level);
1743 assert(start_layer < total_num_layers);
1744 if (num_layers == INTEL_REMAINING_LAYERS)
1745 num_layers = total_num_layers - start_layer;
1746 /* Check for overflow */
1747 assert(start_layer + num_layers >= start_layer);
1748 assert(start_layer + num_layers <= total_num_layers);
1749
1750 return num_layers;
1751 }
1752
1753 bool
1754 intel_miptree_has_color_unresolved(const struct intel_mipmap_tree *mt,
1755 unsigned start_level, unsigned num_levels,
1756 unsigned start_layer, unsigned num_layers)
1757 {
1758 assert(_mesa_is_format_color_format(mt->format));
1759
1760 if (!mt->aux_buf)
1761 return false;
1762
1763 /* Clamp the level range to fit the miptree */
1764 num_levels = miptree_level_range_length(mt, start_level, num_levels);
1765
1766 for (uint32_t l = 0; l < num_levels; l++) {
1767 const uint32_t level = start_level + l;
1768 const uint32_t level_layers =
1769 miptree_layer_range_length(mt, level, start_layer, num_layers);
1770 for (unsigned a = 0; a < level_layers; a++) {
1771 enum isl_aux_state aux_state =
1772 intel_miptree_get_aux_state(mt, level, start_layer + a);
1773 assert(aux_state != ISL_AUX_STATE_AUX_INVALID);
1774 if (aux_state != ISL_AUX_STATE_PASS_THROUGH)
1775 return true;
1776 }
1777 }
1778
1779 return false;
1780 }
1781
1782 static void
1783 intel_miptree_check_color_resolve(const struct brw_context *brw,
1784 const struct intel_mipmap_tree *mt,
1785 unsigned level, unsigned layer)
1786 {
1787 if (!mt->aux_buf)
1788 return;
1789
1790 /* Fast color clear is supported for mipmapped surfaces only on Gen8+. */
1791 assert(brw->screen->devinfo.gen >= 8 ||
1792 (level == 0 && mt->first_level == 0 && mt->last_level == 0));
1793
1794 /* Compression of arrayed msaa surfaces is supported. */
1795 if (mt->surf.samples > 1)
1796 return;
1797
1798 /* Fast color clear is supported for non-msaa arrays only on Gen8+. */
1799 assert(brw->screen->devinfo.gen >= 8 ||
1800 (layer == 0 &&
1801 mt->surf.logical_level0_px.depth == 1 &&
1802 mt->surf.logical_level0_px.array_len == 1));
1803
1804 (void)level;
1805 (void)layer;
1806 }
1807
1808 static enum isl_aux_op
1809 get_ccs_d_resolve_op(enum isl_aux_state aux_state,
1810 enum isl_aux_usage aux_usage,
1811 bool fast_clear_supported)
1812 {
1813 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_CCS_D);
1814
1815 const bool ccs_supported = aux_usage == ISL_AUX_USAGE_CCS_D;
1816
1817 assert(ccs_supported == fast_clear_supported);
1818
1819 switch (aux_state) {
1820 case ISL_AUX_STATE_CLEAR:
1821 case ISL_AUX_STATE_PARTIAL_CLEAR:
1822 if (!ccs_supported)
1823 return ISL_AUX_OP_FULL_RESOLVE;
1824 else
1825 return ISL_AUX_OP_NONE;
1826
1827 case ISL_AUX_STATE_PASS_THROUGH:
1828 return ISL_AUX_OP_NONE;
1829
1830 case ISL_AUX_STATE_RESOLVED:
1831 case ISL_AUX_STATE_AUX_INVALID:
1832 case ISL_AUX_STATE_COMPRESSED_CLEAR:
1833 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
1834 break;
1835 }
1836
1837 unreachable("Invalid aux state for CCS_D");
1838 }
1839
1840 static enum isl_aux_op
1841 get_ccs_e_resolve_op(enum isl_aux_state aux_state,
1842 enum isl_aux_usage aux_usage,
1843 bool fast_clear_supported)
1844 {
1845 /* CCS_E surfaces can be accessed as CCS_D if we're careful. */
1846 assert(aux_usage == ISL_AUX_USAGE_NONE ||
1847 aux_usage == ISL_AUX_USAGE_CCS_D ||
1848 aux_usage == ISL_AUX_USAGE_CCS_E);
1849
1850 if (aux_usage == ISL_AUX_USAGE_CCS_D)
1851 assert(fast_clear_supported);
1852
1853 switch (aux_state) {
1854 case ISL_AUX_STATE_CLEAR:
1855 case ISL_AUX_STATE_PARTIAL_CLEAR:
1856 if (fast_clear_supported)
1857 return ISL_AUX_OP_NONE;
1858 else if (aux_usage == ISL_AUX_USAGE_CCS_E)
1859 return ISL_AUX_OP_PARTIAL_RESOLVE;
1860 else
1861 return ISL_AUX_OP_FULL_RESOLVE;
1862
1863 case ISL_AUX_STATE_COMPRESSED_CLEAR:
1864 if (aux_usage != ISL_AUX_USAGE_CCS_E)
1865 return ISL_AUX_OP_FULL_RESOLVE;
1866 else if (!fast_clear_supported)
1867 return ISL_AUX_OP_PARTIAL_RESOLVE;
1868 else
1869 return ISL_AUX_OP_NONE;
1870
1871 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
1872 if (aux_usage != ISL_AUX_USAGE_CCS_E)
1873 return ISL_AUX_OP_FULL_RESOLVE;
1874 else
1875 return ISL_AUX_OP_NONE;
1876
1877 case ISL_AUX_STATE_PASS_THROUGH:
1878 return ISL_AUX_OP_NONE;
1879
1880 case ISL_AUX_STATE_RESOLVED:
1881 case ISL_AUX_STATE_AUX_INVALID:
1882 break;
1883 }
1884
1885 unreachable("Invalid aux state for CCS_E");
1886 }
1887
1888 static void
1889 intel_miptree_prepare_ccs_access(struct brw_context *brw,
1890 struct intel_mipmap_tree *mt,
1891 uint32_t level, uint32_t layer,
1892 enum isl_aux_usage aux_usage,
1893 bool fast_clear_supported)
1894 {
1895 enum isl_aux_state aux_state = intel_miptree_get_aux_state(mt, level, layer);
1896
1897 enum isl_aux_op resolve_op;
1898 if (mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
1899 resolve_op = get_ccs_e_resolve_op(aux_state, aux_usage,
1900 fast_clear_supported);
1901 } else {
1902 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_D);
1903 resolve_op = get_ccs_d_resolve_op(aux_state, aux_usage,
1904 fast_clear_supported);
1905 }
1906
1907 if (resolve_op != ISL_AUX_OP_NONE) {
1908 intel_miptree_check_color_resolve(brw, mt, level, layer);
1909 brw_blorp_resolve_color(brw, mt, level, layer, resolve_op);
1910
1911 switch (resolve_op) {
1912 case ISL_AUX_OP_FULL_RESOLVE:
1913 /* The CCS full resolve operation destroys the CCS and sets it to the
1914 * pass-through state. (You can also think of this as being both a
1915 * resolve and an ambiguate in one operation.)
1916 */
1917 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
1918 ISL_AUX_STATE_PASS_THROUGH);
1919 break;
1920
1921 case ISL_AUX_OP_PARTIAL_RESOLVE:
1922 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
1923 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
1924 break;
1925
1926 default:
1927 unreachable("Invalid resolve op");
1928 }
1929 }
1930 }
1931
1932 static void
1933 intel_miptree_finish_ccs_write(struct brw_context *brw,
1934 struct intel_mipmap_tree *mt,
1935 uint32_t level, uint32_t layer,
1936 enum isl_aux_usage aux_usage)
1937 {
1938 assert(aux_usage == ISL_AUX_USAGE_NONE ||
1939 aux_usage == ISL_AUX_USAGE_CCS_D ||
1940 aux_usage == ISL_AUX_USAGE_CCS_E);
1941
1942 enum isl_aux_state aux_state = intel_miptree_get_aux_state(mt, level, layer);
1943
1944 if (mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
1945 switch (aux_state) {
1946 case ISL_AUX_STATE_CLEAR:
1947 case ISL_AUX_STATE_PARTIAL_CLEAR:
1948 assert(aux_usage == ISL_AUX_USAGE_CCS_E ||
1949 aux_usage == ISL_AUX_USAGE_CCS_D);
1950
1951 if (aux_usage == ISL_AUX_USAGE_CCS_E) {
1952 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
1953 ISL_AUX_STATE_COMPRESSED_CLEAR);
1954 } else if (aux_state != ISL_AUX_STATE_PARTIAL_CLEAR) {
1955 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
1956 ISL_AUX_STATE_PARTIAL_CLEAR);
1957 }
1958 break;
1959
1960 case ISL_AUX_STATE_COMPRESSED_CLEAR:
1961 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
1962 assert(aux_usage == ISL_AUX_USAGE_CCS_E);
1963 break; /* Nothing to do */
1964
1965 case ISL_AUX_STATE_PASS_THROUGH:
1966 if (aux_usage == ISL_AUX_USAGE_CCS_E) {
1967 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
1968 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
1969 } else {
1970 /* Nothing to do */
1971 }
1972 break;
1973
1974 case ISL_AUX_STATE_RESOLVED:
1975 case ISL_AUX_STATE_AUX_INVALID:
1976 unreachable("Invalid aux state for CCS_E");
1977 }
1978 } else {
1979 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_D);
1980 /* CCS_D is a bit simpler */
1981 switch (aux_state) {
1982 case ISL_AUX_STATE_CLEAR:
1983 assert(aux_usage == ISL_AUX_USAGE_CCS_D);
1984 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
1985 ISL_AUX_STATE_PARTIAL_CLEAR);
1986 break;
1987
1988 case ISL_AUX_STATE_PARTIAL_CLEAR:
1989 assert(aux_usage == ISL_AUX_USAGE_CCS_D);
1990 break; /* Nothing to do */
1991
1992 case ISL_AUX_STATE_PASS_THROUGH:
1993 /* Nothing to do */
1994 break;
1995
1996 case ISL_AUX_STATE_COMPRESSED_CLEAR:
1997 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
1998 case ISL_AUX_STATE_RESOLVED:
1999 case ISL_AUX_STATE_AUX_INVALID:
2000 unreachable("Invalid aux state for CCS_D");
2001 }
2002 }
2003 }
2004
2005 static void
2006 intel_miptree_prepare_mcs_access(struct brw_context *brw,
2007 struct intel_mipmap_tree *mt,
2008 uint32_t layer,
2009 enum isl_aux_usage aux_usage,
2010 bool fast_clear_supported)
2011 {
2012 assert(aux_usage == ISL_AUX_USAGE_MCS);
2013
2014 switch (intel_miptree_get_aux_state(mt, 0, layer)) {
2015 case ISL_AUX_STATE_CLEAR:
2016 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2017 if (!fast_clear_supported) {
2018 brw_blorp_mcs_partial_resolve(brw, mt, layer, 1);
2019 intel_miptree_set_aux_state(brw, mt, 0, layer, 1,
2020 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2021 }
2022 break;
2023
2024 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2025 break; /* Nothing to do */
2026
2027 case ISL_AUX_STATE_RESOLVED:
2028 case ISL_AUX_STATE_PASS_THROUGH:
2029 case ISL_AUX_STATE_AUX_INVALID:
2030 case ISL_AUX_STATE_PARTIAL_CLEAR:
2031 unreachable("Invalid aux state for MCS");
2032 }
2033 }
2034
2035 static void
2036 intel_miptree_finish_mcs_write(struct brw_context *brw,
2037 struct intel_mipmap_tree *mt,
2038 uint32_t layer,
2039 enum isl_aux_usage aux_usage)
2040 {
2041 assert(aux_usage == ISL_AUX_USAGE_MCS);
2042
2043 switch (intel_miptree_get_aux_state(mt, 0, layer)) {
2044 case ISL_AUX_STATE_CLEAR:
2045 intel_miptree_set_aux_state(brw, mt, 0, layer, 1,
2046 ISL_AUX_STATE_COMPRESSED_CLEAR);
2047 break;
2048
2049 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2050 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2051 break; /* Nothing to do */
2052
2053 case ISL_AUX_STATE_RESOLVED:
2054 case ISL_AUX_STATE_PASS_THROUGH:
2055 case ISL_AUX_STATE_AUX_INVALID:
2056 case ISL_AUX_STATE_PARTIAL_CLEAR:
2057 unreachable("Invalid aux state for MCS");
2058 }
2059 }
2060
2061 static void
2062 intel_miptree_prepare_hiz_access(struct brw_context *brw,
2063 struct intel_mipmap_tree *mt,
2064 uint32_t level, uint32_t layer,
2065 enum isl_aux_usage aux_usage,
2066 bool fast_clear_supported)
2067 {
2068 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_HIZ);
2069
2070 enum isl_aux_op hiz_op = ISL_AUX_OP_NONE;
2071 switch (intel_miptree_get_aux_state(mt, level, layer)) {
2072 case ISL_AUX_STATE_CLEAR:
2073 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2074 if (aux_usage != ISL_AUX_USAGE_HIZ || !fast_clear_supported)
2075 hiz_op = ISL_AUX_OP_FULL_RESOLVE;
2076 break;
2077
2078 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2079 if (aux_usage != ISL_AUX_USAGE_HIZ)
2080 hiz_op = ISL_AUX_OP_FULL_RESOLVE;
2081 break;
2082
2083 case ISL_AUX_STATE_PASS_THROUGH:
2084 case ISL_AUX_STATE_RESOLVED:
2085 break;
2086
2087 case ISL_AUX_STATE_AUX_INVALID:
2088 if (aux_usage == ISL_AUX_USAGE_HIZ)
2089 hiz_op = ISL_AUX_OP_AMBIGUATE;
2090 break;
2091
2092 case ISL_AUX_STATE_PARTIAL_CLEAR:
2093 unreachable("Invalid HiZ state");
2094 }
2095
2096 if (hiz_op != ISL_AUX_OP_NONE) {
2097 intel_hiz_exec(brw, mt, level, layer, 1, hiz_op);
2098
2099 switch (hiz_op) {
2100 case ISL_AUX_OP_FULL_RESOLVE:
2101 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2102 ISL_AUX_STATE_RESOLVED);
2103 break;
2104
2105 case ISL_AUX_OP_AMBIGUATE:
2106 /* The HiZ resolve operation is actually an ambiguate */
2107 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2108 ISL_AUX_STATE_PASS_THROUGH);
2109 break;
2110
2111 default:
2112 unreachable("Invalid HiZ op");
2113 }
2114 }
2115 }
2116
2117 static void
2118 intel_miptree_finish_hiz_write(struct brw_context *brw,
2119 struct intel_mipmap_tree *mt,
2120 uint32_t level, uint32_t layer,
2121 enum isl_aux_usage aux_usage)
2122 {
2123 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_HIZ);
2124
2125 switch (intel_miptree_get_aux_state(mt, level, layer)) {
2126 case ISL_AUX_STATE_CLEAR:
2127 assert(aux_usage == ISL_AUX_USAGE_HIZ);
2128 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2129 ISL_AUX_STATE_COMPRESSED_CLEAR);
2130 break;
2131
2132 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2133 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2134 assert(aux_usage == ISL_AUX_USAGE_HIZ);
2135 break; /* Nothing to do */
2136
2137 case ISL_AUX_STATE_RESOLVED:
2138 if (aux_usage == ISL_AUX_USAGE_HIZ) {
2139 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2140 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2141 } else {
2142 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2143 ISL_AUX_STATE_AUX_INVALID);
2144 }
2145 break;
2146
2147 case ISL_AUX_STATE_PASS_THROUGH:
2148 if (aux_usage == ISL_AUX_USAGE_HIZ) {
2149 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2150 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2151 }
2152 break;
2153
2154 case ISL_AUX_STATE_AUX_INVALID:
2155 assert(aux_usage != ISL_AUX_USAGE_HIZ);
2156 break;
2157
2158 case ISL_AUX_STATE_PARTIAL_CLEAR:
2159 unreachable("Invalid HiZ state");
2160 }
2161 }
2162
2163 void
2164 intel_miptree_prepare_access(struct brw_context *brw,
2165 struct intel_mipmap_tree *mt,
2166 uint32_t start_level, uint32_t num_levels,
2167 uint32_t start_layer, uint32_t num_layers,
2168 enum isl_aux_usage aux_usage,
2169 bool fast_clear_supported)
2170 {
2171 num_levels = miptree_level_range_length(mt, start_level, num_levels);
2172
2173 switch (mt->aux_usage) {
2174 case ISL_AUX_USAGE_NONE:
2175 /* Nothing to do */
2176 break;
2177
2178 case ISL_AUX_USAGE_MCS:
2179 assert(mt->aux_buf);
2180 assert(start_level == 0 && num_levels == 1);
2181 const uint32_t level_layers =
2182 miptree_layer_range_length(mt, 0, start_layer, num_layers);
2183 for (uint32_t a = 0; a < level_layers; a++) {
2184 intel_miptree_prepare_mcs_access(brw, mt, start_layer + a,
2185 aux_usage, fast_clear_supported);
2186 }
2187 break;
2188
2189 case ISL_AUX_USAGE_CCS_D:
2190 case ISL_AUX_USAGE_CCS_E:
2191 if (!mt->aux_buf)
2192 return;
2193
2194 for (uint32_t l = 0; l < num_levels; l++) {
2195 const uint32_t level = start_level + l;
2196 const uint32_t level_layers =
2197 miptree_layer_range_length(mt, level, start_layer, num_layers);
2198 for (uint32_t a = 0; a < level_layers; a++) {
2199 intel_miptree_prepare_ccs_access(brw, mt, level,
2200 start_layer + a,
2201 aux_usage, fast_clear_supported);
2202 }
2203 }
2204 break;
2205
2206 case ISL_AUX_USAGE_HIZ:
2207 assert(mt->aux_buf);
2208 for (uint32_t l = 0; l < num_levels; l++) {
2209 const uint32_t level = start_level + l;
2210 if (!intel_miptree_level_has_hiz(mt, level))
2211 continue;
2212
2213 const uint32_t level_layers =
2214 miptree_layer_range_length(mt, level, start_layer, num_layers);
2215 for (uint32_t a = 0; a < level_layers; a++) {
2216 intel_miptree_prepare_hiz_access(brw, mt, level, start_layer + a,
2217 aux_usage, fast_clear_supported);
2218 }
2219 }
2220 break;
2221
2222 default:
2223 unreachable("Invalid aux usage");
2224 }
2225 }
2226
2227 void
2228 intel_miptree_finish_write(struct brw_context *brw,
2229 struct intel_mipmap_tree *mt, uint32_t level,
2230 uint32_t start_layer, uint32_t num_layers,
2231 enum isl_aux_usage aux_usage)
2232 {
2233 const struct gen_device_info *devinfo = &brw->screen->devinfo;
2234 num_layers = miptree_layer_range_length(mt, level, start_layer, num_layers);
2235
2236 switch (mt->aux_usage) {
2237 case ISL_AUX_USAGE_NONE:
2238 if (mt->format == MESA_FORMAT_S_UINT8 && devinfo->gen <= 7) {
2239 mt->shadow_needs_update = true;
2240 } else if (intel_miptree_has_etc_shadow(brw, mt)) {
2241 mt->shadow_needs_update = true;
2242 }
2243 break;
2244
2245 case ISL_AUX_USAGE_MCS:
2246 assert(mt->aux_buf);
2247 for (uint32_t a = 0; a < num_layers; a++) {
2248 intel_miptree_finish_mcs_write(brw, mt, start_layer + a,
2249 aux_usage);
2250 }
2251 break;
2252
2253 case ISL_AUX_USAGE_CCS_D:
2254 case ISL_AUX_USAGE_CCS_E:
2255 if (!mt->aux_buf)
2256 return;
2257
2258 for (uint32_t a = 0; a < num_layers; a++) {
2259 intel_miptree_finish_ccs_write(brw, mt, level, start_layer + a,
2260 aux_usage);
2261 }
2262 break;
2263
2264 case ISL_AUX_USAGE_HIZ:
2265 if (!intel_miptree_level_has_hiz(mt, level))
2266 return;
2267
2268 for (uint32_t a = 0; a < num_layers; a++) {
2269 intel_miptree_finish_hiz_write(brw, mt, level, start_layer + a,
2270 aux_usage);
2271 }
2272 break;
2273
2274 default:
2275 unreachable("Invavlid aux usage");
2276 }
2277 }
2278
2279 enum isl_aux_state
2280 intel_miptree_get_aux_state(const struct intel_mipmap_tree *mt,
2281 uint32_t level, uint32_t layer)
2282 {
2283 intel_miptree_check_level_layer(mt, level, layer);
2284
2285 if (_mesa_is_format_color_format(mt->format)) {
2286 assert(mt->aux_buf != NULL);
2287 assert(mt->surf.samples == 1 ||
2288 mt->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
2289 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2290 unreachable("Cannot get aux state for stencil");
2291 } else {
2292 assert(intel_miptree_level_has_hiz(mt, level));
2293 }
2294
2295 return mt->aux_state[level][layer];
2296 }
2297
2298 void
2299 intel_miptree_set_aux_state(struct brw_context *brw,
2300 struct intel_mipmap_tree *mt, uint32_t level,
2301 uint32_t start_layer, uint32_t num_layers,
2302 enum isl_aux_state aux_state)
2303 {
2304 num_layers = miptree_layer_range_length(mt, level, start_layer, num_layers);
2305
2306 if (_mesa_is_format_color_format(mt->format)) {
2307 assert(mt->aux_buf != NULL);
2308 assert(mt->surf.samples == 1 ||
2309 mt->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
2310 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2311 unreachable("Cannot get aux state for stencil");
2312 } else {
2313 assert(intel_miptree_level_has_hiz(mt, level));
2314 }
2315
2316 for (unsigned a = 0; a < num_layers; a++) {
2317 if (mt->aux_state[level][start_layer + a] != aux_state) {
2318 mt->aux_state[level][start_layer + a] = aux_state;
2319 brw->ctx.NewDriverState |= BRW_NEW_AUX_STATE;
2320 }
2321 }
2322 }
2323
2324 /* On Gen9 color buffers may be compressed by the hardware (lossless
2325 * compression). There are, however, format restrictions and care needs to be
2326 * taken that the sampler engine is capable for re-interpreting a buffer with
2327 * format different the buffer was originally written with.
2328 *
2329 * For example, SRGB formats are not compressible and the sampler engine isn't
2330 * capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case the underlying
2331 * color buffer needs to be resolved so that the sampling surface can be
2332 * sampled as non-compressed (i.e., without the auxiliary MCS buffer being
2333 * set).
2334 */
2335 static bool
2336 can_texture_with_ccs(struct brw_context *brw,
2337 struct intel_mipmap_tree *mt,
2338 enum isl_format view_format)
2339 {
2340 if (mt->aux_usage != ISL_AUX_USAGE_CCS_E)
2341 return false;
2342
2343 if (!format_ccs_e_compat_with_miptree(&brw->screen->devinfo,
2344 mt, view_format)) {
2345 perf_debug("Incompatible sampling format (%s) for rbc (%s)\n",
2346 isl_format_get_layout(view_format)->name,
2347 _mesa_get_format_name(mt->format));
2348 return false;
2349 }
2350
2351 return true;
2352 }
2353
2354 enum isl_aux_usage
2355 intel_miptree_texture_aux_usage(struct brw_context *brw,
2356 struct intel_mipmap_tree *mt,
2357 enum isl_format view_format,
2358 enum gen9_astc5x5_wa_tex_type astc5x5_wa_bits)
2359 {
2360 assert(brw->screen->devinfo.gen == 9 || astc5x5_wa_bits == 0);
2361
2362 /* On gen9, ASTC 5x5 textures cannot live in the sampler cache along side
2363 * CCS or HiZ compressed textures. See gen9_apply_astc5x5_wa_flush() for
2364 * details.
2365 */
2366 if ((astc5x5_wa_bits & GEN9_ASTC5X5_WA_TEX_TYPE_ASTC5x5) &&
2367 mt->aux_usage != ISL_AUX_USAGE_MCS)
2368 return ISL_AUX_USAGE_NONE;
2369
2370 switch (mt->aux_usage) {
2371 case ISL_AUX_USAGE_HIZ:
2372 if (intel_miptree_sample_with_hiz(brw, mt))
2373 return ISL_AUX_USAGE_HIZ;
2374 break;
2375
2376 case ISL_AUX_USAGE_MCS:
2377 return ISL_AUX_USAGE_MCS;
2378
2379 case ISL_AUX_USAGE_CCS_D:
2380 case ISL_AUX_USAGE_CCS_E:
2381 if (!mt->aux_buf) {
2382 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_D);
2383 return ISL_AUX_USAGE_NONE;
2384 }
2385
2386 /* If we don't have any unresolved color, report an aux usage of
2387 * ISL_AUX_USAGE_NONE. This way, texturing won't even look at the
2388 * aux surface and we can save some bandwidth.
2389 */
2390 if (!intel_miptree_has_color_unresolved(mt, 0, INTEL_REMAINING_LEVELS,
2391 0, INTEL_REMAINING_LAYERS))
2392 return ISL_AUX_USAGE_NONE;
2393
2394 if (can_texture_with_ccs(brw, mt, view_format))
2395 return ISL_AUX_USAGE_CCS_E;
2396 break;
2397
2398 default:
2399 break;
2400 }
2401
2402 return ISL_AUX_USAGE_NONE;
2403 }
2404
2405 static bool
2406 isl_formats_are_fast_clear_compatible(enum isl_format a, enum isl_format b)
2407 {
2408 /* On gen8 and earlier, the hardware was only capable of handling 0/1 clear
2409 * values so sRGB curve application was a no-op for all fast-clearable
2410 * formats.
2411 *
2412 * On gen9+, the hardware supports arbitrary clear values. For sRGB clear
2413 * values, the hardware interprets the floats, not as what would be
2414 * returned from the sampler (or written by the shader), but as being
2415 * between format conversion and sRGB curve application. This means that
2416 * we can switch between sRGB and UNORM without having to whack the clear
2417 * color.
2418 */
2419 return isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b);
2420 }
2421
2422 void
2423 intel_miptree_prepare_texture(struct brw_context *brw,
2424 struct intel_mipmap_tree *mt,
2425 enum isl_format view_format,
2426 uint32_t start_level, uint32_t num_levels,
2427 uint32_t start_layer, uint32_t num_layers,
2428 enum gen9_astc5x5_wa_tex_type astc5x5_wa_bits)
2429 {
2430 enum isl_aux_usage aux_usage =
2431 intel_miptree_texture_aux_usage(brw, mt, view_format, astc5x5_wa_bits);
2432
2433 bool clear_supported = aux_usage != ISL_AUX_USAGE_NONE;
2434
2435 /* Clear color is specified as ints or floats and the conversion is done by
2436 * the sampler. If we have a texture view, we would have to perform the
2437 * clear color conversion manually. Just disable clear color.
2438 */
2439 if (!isl_formats_are_fast_clear_compatible(mt->surf.format, view_format))
2440 clear_supported = false;
2441
2442 intel_miptree_prepare_access(brw, mt, start_level, num_levels,
2443 start_layer, num_layers,
2444 aux_usage, clear_supported);
2445 }
2446
2447 void
2448 intel_miptree_prepare_image(struct brw_context *brw,
2449 struct intel_mipmap_tree *mt)
2450 {
2451 /* The data port doesn't understand any compression */
2452 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2453 0, INTEL_REMAINING_LAYERS,
2454 ISL_AUX_USAGE_NONE, false);
2455 }
2456
2457 enum isl_aux_usage
2458 intel_miptree_render_aux_usage(struct brw_context *brw,
2459 struct intel_mipmap_tree *mt,
2460 enum isl_format render_format,
2461 bool blend_enabled,
2462 bool draw_aux_disabled)
2463 {
2464 struct gen_device_info *devinfo = &brw->screen->devinfo;
2465
2466 if (draw_aux_disabled)
2467 return ISL_AUX_USAGE_NONE;
2468
2469 switch (mt->aux_usage) {
2470 case ISL_AUX_USAGE_MCS:
2471 assert(mt->aux_buf);
2472 return ISL_AUX_USAGE_MCS;
2473
2474 case ISL_AUX_USAGE_CCS_D:
2475 case ISL_AUX_USAGE_CCS_E:
2476 if (!mt->aux_buf) {
2477 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_D);
2478 return ISL_AUX_USAGE_NONE;
2479 }
2480
2481 /* gen9+ hardware technically supports non-0/1 clear colors with sRGB
2482 * formats. However, there are issues with blending where it doesn't
2483 * properly apply the sRGB curve to the clear color when blending.
2484 */
2485 if (devinfo->gen >= 9 && blend_enabled &&
2486 isl_format_is_srgb(render_format) &&
2487 !isl_color_value_is_zero_one(mt->fast_clear_color, render_format))
2488 return ISL_AUX_USAGE_NONE;
2489
2490 if (mt->aux_usage == ISL_AUX_USAGE_CCS_E &&
2491 format_ccs_e_compat_with_miptree(&brw->screen->devinfo,
2492 mt, render_format))
2493 return ISL_AUX_USAGE_CCS_E;
2494
2495 /* Otherwise, we have to fall back to CCS_D */
2496 return ISL_AUX_USAGE_CCS_D;
2497
2498 default:
2499 return ISL_AUX_USAGE_NONE;
2500 }
2501 }
2502
2503 void
2504 intel_miptree_prepare_render(struct brw_context *brw,
2505 struct intel_mipmap_tree *mt, uint32_t level,
2506 uint32_t start_layer, uint32_t layer_count,
2507 enum isl_aux_usage aux_usage)
2508 {
2509 intel_miptree_prepare_access(brw, mt, level, 1, start_layer, layer_count,
2510 aux_usage, aux_usage != ISL_AUX_USAGE_NONE);
2511 }
2512
2513 void
2514 intel_miptree_finish_render(struct brw_context *brw,
2515 struct intel_mipmap_tree *mt, uint32_t level,
2516 uint32_t start_layer, uint32_t layer_count,
2517 enum isl_aux_usage aux_usage)
2518 {
2519 assert(_mesa_is_format_color_format(mt->format));
2520
2521 intel_miptree_finish_write(brw, mt, level, start_layer, layer_count,
2522 aux_usage);
2523 }
2524
2525 void
2526 intel_miptree_prepare_depth(struct brw_context *brw,
2527 struct intel_mipmap_tree *mt, uint32_t level,
2528 uint32_t start_layer, uint32_t layer_count)
2529 {
2530 intel_miptree_prepare_access(brw, mt, level, 1, start_layer, layer_count,
2531 mt->aux_usage, mt->aux_buf != NULL);
2532 }
2533
2534 void
2535 intel_miptree_finish_depth(struct brw_context *brw,
2536 struct intel_mipmap_tree *mt, uint32_t level,
2537 uint32_t start_layer, uint32_t layer_count,
2538 bool depth_written)
2539 {
2540 if (depth_written) {
2541 intel_miptree_finish_write(brw, mt, level, start_layer, layer_count,
2542 mt->aux_usage);
2543 }
2544 }
2545
2546 void
2547 intel_miptree_prepare_external(struct brw_context *brw,
2548 struct intel_mipmap_tree *mt)
2549 {
2550 enum isl_aux_usage aux_usage = ISL_AUX_USAGE_NONE;
2551 bool supports_fast_clear = false;
2552
2553 const struct isl_drm_modifier_info *mod_info =
2554 isl_drm_modifier_get_info(mt->drm_modifier);
2555
2556 if (mod_info && mod_info->aux_usage != ISL_AUX_USAGE_NONE) {
2557 /* CCS_E is the only supported aux for external images and it's only
2558 * supported on very simple images.
2559 */
2560 assert(mod_info->aux_usage == ISL_AUX_USAGE_CCS_E);
2561 assert(_mesa_is_format_color_format(mt->format));
2562 assert(mt->first_level == 0 && mt->last_level == 0);
2563 assert(mt->surf.logical_level0_px.depth == 1);
2564 assert(mt->surf.logical_level0_px.array_len == 1);
2565 assert(mt->surf.samples == 1);
2566 assert(mt->aux_buf != NULL);
2567
2568 aux_usage = mod_info->aux_usage;
2569 supports_fast_clear = mod_info->supports_clear_color;
2570 }
2571
2572 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2573 0, INTEL_REMAINING_LAYERS,
2574 aux_usage, supports_fast_clear);
2575 }
2576
2577 void
2578 intel_miptree_finish_external(struct brw_context *brw,
2579 struct intel_mipmap_tree *mt)
2580 {
2581 if (!mt->aux_buf)
2582 return;
2583
2584 /* We don't know the actual aux state of the aux surface. The previous
2585 * owner could have given it to us in a number of different states.
2586 * Because we don't know the aux state, we reset the aux state to the
2587 * least common denominator of possible valid states.
2588 */
2589 enum isl_aux_state default_aux_state =
2590 isl_drm_modifier_get_default_aux_state(mt->drm_modifier);
2591 assert(mt->last_level == mt->first_level);
2592 intel_miptree_set_aux_state(brw, mt, 0, 0, INTEL_REMAINING_LAYERS,
2593 default_aux_state);
2594 }
2595
2596 /**
2597 * Make it possible to share the BO backing the given miptree with another
2598 * process or another miptree.
2599 *
2600 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2601 * then discard the MCS buffer, if present. We also set the no_ccs flag to
2602 * ensure that no MCS buffer gets allocated in the future.
2603 *
2604 * HiZ is similarly unsafe with shared buffers.
2605 */
2606 void
2607 intel_miptree_make_shareable(struct brw_context *brw,
2608 struct intel_mipmap_tree *mt)
2609 {
2610 /* MCS buffers are also used for multisample buffers, but we can't resolve
2611 * away a multisample MCS buffer because it's an integral part of how the
2612 * pixel data is stored. Fortunately this code path should never be
2613 * reached for multisample buffers.
2614 */
2615 assert(mt->surf.msaa_layout == ISL_MSAA_LAYOUT_NONE ||
2616 mt->surf.samples == 1);
2617
2618 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2619 0, INTEL_REMAINING_LAYERS,
2620 ISL_AUX_USAGE_NONE, false);
2621
2622 if (mt->aux_buf) {
2623 intel_miptree_aux_buffer_free(mt->aux_buf);
2624 mt->aux_buf = NULL;
2625
2626 /* Make future calls of intel_miptree_level_has_hiz() return false. */
2627 for (uint32_t l = mt->first_level; l <= mt->last_level; ++l) {
2628 mt->level[l].has_hiz = false;
2629 }
2630
2631 free(mt->aux_state);
2632 mt->aux_state = NULL;
2633 brw->ctx.NewDriverState |= BRW_NEW_AUX_STATE;
2634 }
2635
2636 mt->aux_usage = ISL_AUX_USAGE_NONE;
2637 mt->supports_fast_clear = false;
2638 }
2639
2640
2641 /**
2642 * \brief Get pointer offset into stencil buffer.
2643 *
2644 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2645 * must decode the tile's layout in software.
2646 *
2647 * See
2648 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2649 * Format.
2650 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2651 *
2652 * Even though the returned offset is always positive, the return type is
2653 * signed due to
2654 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2655 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2656 */
2657 static intptr_t
2658 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
2659 {
2660 uint32_t tile_size = 4096;
2661 uint32_t tile_width = 64;
2662 uint32_t tile_height = 64;
2663 uint32_t row_size = 64 * stride / 2; /* Two rows are interleaved. */
2664
2665 uint32_t tile_x = x / tile_width;
2666 uint32_t tile_y = y / tile_height;
2667
2668 /* The byte's address relative to the tile's base addres. */
2669 uint32_t byte_x = x % tile_width;
2670 uint32_t byte_y = y % tile_height;
2671
2672 uintptr_t u = tile_y * row_size
2673 + tile_x * tile_size
2674 + 512 * (byte_x / 8)
2675 + 64 * (byte_y / 8)
2676 + 32 * ((byte_y / 4) % 2)
2677 + 16 * ((byte_x / 4) % 2)
2678 + 8 * ((byte_y / 2) % 2)
2679 + 4 * ((byte_x / 2) % 2)
2680 + 2 * (byte_y % 2)
2681 + 1 * (byte_x % 2);
2682
2683 if (swizzled) {
2684 /* adjust for bit6 swizzling */
2685 if (((byte_x / 8) % 2) == 1) {
2686 if (((byte_y / 8) % 2) == 0) {
2687 u += 64;
2688 } else {
2689 u -= 64;
2690 }
2691 }
2692 }
2693
2694 return u;
2695 }
2696
2697 void
2698 intel_miptree_updownsample(struct brw_context *brw,
2699 struct intel_mipmap_tree *src,
2700 struct intel_mipmap_tree *dst)
2701 {
2702 unsigned src_w = src->surf.logical_level0_px.width;
2703 unsigned src_h = src->surf.logical_level0_px.height;
2704 unsigned dst_w = dst->surf.logical_level0_px.width;
2705 unsigned dst_h = dst->surf.logical_level0_px.height;
2706
2707 brw_blorp_blit_miptrees(brw,
2708 src, 0 /* level */, 0 /* layer */,
2709 src->format, SWIZZLE_XYZW,
2710 dst, 0 /* level */, 0 /* layer */, dst->format,
2711 0, 0, src_w, src_h,
2712 0, 0, dst_w, dst_h,
2713 GL_NEAREST, false, false /*mirror x, y*/,
2714 false, false);
2715
2716 if (src->stencil_mt) {
2717 src_w = src->stencil_mt->surf.logical_level0_px.width;
2718 src_h = src->stencil_mt->surf.logical_level0_px.height;
2719 dst_w = dst->stencil_mt->surf.logical_level0_px.width;
2720 dst_h = dst->stencil_mt->surf.logical_level0_px.height;
2721
2722 brw_blorp_blit_miptrees(brw,
2723 src->stencil_mt, 0 /* level */, 0 /* layer */,
2724 src->stencil_mt->format, SWIZZLE_XYZW,
2725 dst->stencil_mt, 0 /* level */, 0 /* layer */,
2726 dst->stencil_mt->format,
2727 0, 0, src_w, src_h,
2728 0, 0, dst_w, dst_h,
2729 GL_NEAREST, false, false /*mirror x, y*/,
2730 false, false /* decode/encode srgb */);
2731 }
2732 }
2733
2734 void
2735 intel_update_r8stencil(struct brw_context *brw,
2736 struct intel_mipmap_tree *mt)
2737 {
2738 const struct gen_device_info *devinfo = &brw->screen->devinfo;
2739
2740 assert(devinfo->gen >= 7);
2741 struct intel_mipmap_tree *src =
2742 mt->format == MESA_FORMAT_S_UINT8 ? mt : mt->stencil_mt;
2743 if (!src || devinfo->gen >= 8)
2744 return;
2745
2746 assert(src->surf.size_B > 0);
2747
2748 if (!mt->shadow_mt) {
2749 assert(devinfo->gen > 6); /* Handle MIPTREE_LAYOUT_GEN6_HIZ_STENCIL */
2750 mt->shadow_mt = make_surface(
2751 brw,
2752 src->target,
2753 MESA_FORMAT_R_UINT8,
2754 src->first_level, src->last_level,
2755 src->surf.logical_level0_px.width,
2756 src->surf.logical_level0_px.height,
2757 src->surf.dim == ISL_SURF_DIM_3D ?
2758 src->surf.logical_level0_px.depth :
2759 src->surf.logical_level0_px.array_len,
2760 src->surf.samples,
2761 ISL_TILING_Y0_BIT,
2762 ISL_SURF_USAGE_TEXTURE_BIT,
2763 BO_ALLOC_BUSY, 0, NULL);
2764 assert(mt->shadow_mt);
2765 }
2766
2767 if (src->shadow_needs_update == false)
2768 return;
2769
2770 struct intel_mipmap_tree *dst = mt->shadow_mt;
2771
2772 for (int level = src->first_level; level <= src->last_level; level++) {
2773 const unsigned depth = src->surf.dim == ISL_SURF_DIM_3D ?
2774 minify(src->surf.phys_level0_sa.depth, level) :
2775 src->surf.phys_level0_sa.array_len;
2776
2777 for (unsigned layer = 0; layer < depth; layer++) {
2778 brw_blorp_copy_miptrees(brw,
2779 src, level, layer,
2780 dst, level, layer,
2781 0, 0, 0, 0,
2782 minify(src->surf.logical_level0_px.width,
2783 level),
2784 minify(src->surf.logical_level0_px.height,
2785 level));
2786 }
2787 }
2788
2789 brw_cache_flush_for_read(brw, dst->bo);
2790 src->shadow_needs_update = false;
2791 }
2792
2793 static void *
2794 intel_miptree_map_raw(struct brw_context *brw,
2795 struct intel_mipmap_tree *mt,
2796 GLbitfield mode)
2797 {
2798 struct brw_bo *bo = mt->bo;
2799
2800 if (brw_batch_references(&brw->batch, bo))
2801 intel_batchbuffer_flush(brw);
2802
2803 return brw_bo_map(brw, bo, mode);
2804 }
2805
2806 static void
2807 intel_miptree_unmap_raw(struct intel_mipmap_tree *mt)
2808 {
2809 brw_bo_unmap(mt->bo);
2810 }
2811
2812 static void
2813 intel_miptree_unmap_map(struct brw_context *brw,
2814 struct intel_mipmap_tree *mt,
2815 struct intel_miptree_map *map,
2816 unsigned int level, unsigned int slice)
2817 {
2818 intel_miptree_unmap_raw(mt);
2819 }
2820
2821 static void
2822 intel_miptree_map_map(struct brw_context *brw,
2823 struct intel_mipmap_tree *mt,
2824 struct intel_miptree_map *map,
2825 unsigned int level, unsigned int slice)
2826 {
2827 unsigned int bw, bh;
2828 void *base;
2829 unsigned int image_x, image_y;
2830 intptr_t x = map->x;
2831 intptr_t y = map->y;
2832
2833 /* For compressed formats, the stride is the number of bytes per
2834 * row of blocks. intel_miptree_get_image_offset() already does
2835 * the divide.
2836 */
2837 _mesa_get_format_block_size(mt->format, &bw, &bh);
2838 assert(y % bh == 0);
2839 assert(x % bw == 0);
2840 y /= bh;
2841 x /= bw;
2842
2843 intel_miptree_access_raw(brw, mt, level, slice,
2844 map->mode & GL_MAP_WRITE_BIT);
2845
2846 base = intel_miptree_map_raw(brw, mt, map->mode);
2847
2848 if (base == NULL)
2849 map->ptr = NULL;
2850 else {
2851 base += mt->offset;
2852
2853 /* Note that in the case of cube maps, the caller must have passed the
2854 * slice number referencing the face.
2855 */
2856 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2857 x += image_x;
2858 y += image_y;
2859
2860 map->stride = mt->surf.row_pitch_B;
2861 map->ptr = base + y * map->stride + x * mt->cpp;
2862 }
2863
2864 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2865 "%"PRIiPTR",%"PRIiPTR" = %p/%d\n", __func__,
2866 map->x, map->y, map->w, map->h,
2867 mt, _mesa_get_format_name(mt->format),
2868 x, y, map->ptr, map->stride);
2869
2870 map->unmap = intel_miptree_unmap_map;
2871 }
2872
2873 static void
2874 intel_miptree_unmap_blit(struct brw_context *brw,
2875 struct intel_mipmap_tree *mt,
2876 struct intel_miptree_map *map,
2877 unsigned int level,
2878 unsigned int slice)
2879 {
2880 const struct gen_device_info *devinfo = &brw->screen->devinfo;
2881 struct gl_context *ctx = &brw->ctx;
2882
2883 intel_miptree_unmap_raw(map->linear_mt);
2884
2885 if (map->mode & GL_MAP_WRITE_BIT) {
2886 if (devinfo->gen >= 6) {
2887 brw_blorp_copy_miptrees(brw, map->linear_mt, 0, 0,
2888 mt, level, slice,
2889 0, 0, map->x, map->y, map->w, map->h);
2890 } else {
2891 bool ok = intel_miptree_copy(brw,
2892 map->linear_mt, 0, 0, 0, 0,
2893 mt, level, slice, map->x, map->y,
2894 map->w, map->h);
2895 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
2896 }
2897 }
2898
2899 intel_miptree_release(&map->linear_mt);
2900 }
2901
2902 /* Compute extent parameters for use with tiled_memcpy functions.
2903 * xs are in units of bytes and ys are in units of strides.
2904 */
2905 static inline void
2906 tile_extents(struct intel_mipmap_tree *mt, struct intel_miptree_map *map,
2907 unsigned int level, unsigned int slice, unsigned int *x1_B,
2908 unsigned int *x2_B, unsigned int *y1_el, unsigned int *y2_el)
2909 {
2910 unsigned int block_width, block_height;
2911 unsigned int x0_el, y0_el;
2912
2913 _mesa_get_format_block_size(mt->format, &block_width, &block_height);
2914
2915 assert(map->x % block_width == 0);
2916 assert(map->y % block_height == 0);
2917
2918 intel_miptree_get_image_offset(mt, level, slice, &x0_el, &y0_el);
2919 *x1_B = (map->x / block_width + x0_el) * mt->cpp;
2920 *y1_el = map->y / block_height + y0_el;
2921 *x2_B = (DIV_ROUND_UP(map->x + map->w, block_width) + x0_el) * mt->cpp;
2922 *y2_el = DIV_ROUND_UP(map->y + map->h, block_height) + y0_el;
2923 }
2924
2925 static void
2926 intel_miptree_unmap_tiled_memcpy(struct brw_context *brw,
2927 struct intel_mipmap_tree *mt,
2928 struct intel_miptree_map *map,
2929 unsigned int level,
2930 unsigned int slice)
2931 {
2932 if (map->mode & GL_MAP_WRITE_BIT) {
2933 unsigned int x1, x2, y1, y2;
2934 tile_extents(mt, map, level, slice, &x1, &x2, &y1, &y2);
2935
2936 char *dst = intel_miptree_map_raw(brw, mt, map->mode | MAP_RAW);
2937 dst += mt->offset;
2938
2939 isl_memcpy_linear_to_tiled(
2940 x1, x2, y1, y2, dst, map->ptr, mt->surf.row_pitch_B, map->stride,
2941 brw->has_swizzling, mt->surf.tiling, ISL_MEMCPY);
2942
2943 intel_miptree_unmap_raw(mt);
2944 }
2945 _mesa_align_free(map->buffer);
2946 map->buffer = map->ptr = NULL;
2947 }
2948
2949 /**
2950 * Determine which copy function to use for the given format combination
2951 *
2952 * The only two possible copy functions which are ever returned are a
2953 * direct memcpy and a RGBA <-> BGRA copy function. Since RGBA -> BGRA and
2954 * BGRA -> RGBA are exactly the same operation (and memcpy is obviously
2955 * symmetric), it doesn't matter whether the copy is from the tiled image
2956 * to the untiled or vice versa. The copy function required is the same in
2957 * either case so this function can be used.
2958 *
2959 * \param[in] tiledFormat The format of the tiled image
2960 * \param[in] format The GL format of the client data
2961 * \param[in] type The GL type of the client data
2962 * \param[out] mem_copy Will be set to one of either the standard
2963 * library's memcpy or a different copy function
2964 * that performs an RGBA to BGRA conversion
2965 * \param[out] cpp Number of bytes per channel
2966 *
2967 * \return true if the format and type combination are valid
2968 */
2969 isl_memcpy_type
2970 intel_miptree_get_memcpy_type(mesa_format tiledFormat, GLenum format, GLenum type,
2971 uint32_t *cpp)
2972 {
2973 if (type == GL_UNSIGNED_INT_8_8_8_8_REV &&
2974 !(format == GL_RGBA || format == GL_BGRA))
2975 return ISL_MEMCPY_INVALID; /* Invalid type/format combination */
2976
2977 if ((tiledFormat == MESA_FORMAT_L_UNORM8 && format == GL_LUMINANCE) ||
2978 (tiledFormat == MESA_FORMAT_A_UNORM8 && format == GL_ALPHA)) {
2979 *cpp = 1;
2980 return ISL_MEMCPY;
2981 } else if ((tiledFormat == MESA_FORMAT_B8G8R8A8_UNORM) ||
2982 (tiledFormat == MESA_FORMAT_B8G8R8X8_UNORM) ||
2983 (tiledFormat == MESA_FORMAT_B8G8R8A8_SRGB) ||
2984 (tiledFormat == MESA_FORMAT_B8G8R8X8_SRGB)) {
2985 *cpp = 4;
2986 if (format == GL_BGRA) {
2987 return ISL_MEMCPY;
2988 } else if (format == GL_RGBA) {
2989 return ISL_MEMCPY_BGRA8;
2990 }
2991 } else if ((tiledFormat == MESA_FORMAT_R8G8B8A8_UNORM) ||
2992 (tiledFormat == MESA_FORMAT_R8G8B8X8_UNORM) ||
2993 (tiledFormat == MESA_FORMAT_R8G8B8A8_SRGB) ||
2994 (tiledFormat == MESA_FORMAT_R8G8B8X8_SRGB)) {
2995 *cpp = 4;
2996 if (format == GL_BGRA) {
2997 /* Copying from RGBA to BGRA is the same as BGRA to RGBA so we can
2998 * use the same function.
2999 */
3000 return ISL_MEMCPY_BGRA8;
3001 } else if (format == GL_RGBA) {
3002 return ISL_MEMCPY;
3003 }
3004 }
3005
3006 return ISL_MEMCPY_INVALID;
3007 }
3008
3009 static void
3010 intel_miptree_map_tiled_memcpy(struct brw_context *brw,
3011 struct intel_mipmap_tree *mt,
3012 struct intel_miptree_map *map,
3013 unsigned int level, unsigned int slice)
3014 {
3015 intel_miptree_access_raw(brw, mt, level, slice,
3016 map->mode & GL_MAP_WRITE_BIT);
3017
3018 unsigned int x1, x2, y1, y2;
3019 tile_extents(mt, map, level, slice, &x1, &x2, &y1, &y2);
3020 map->stride = ALIGN(_mesa_format_row_stride(mt->format, map->w), 16);
3021
3022 /* The tiling and detiling functions require that the linear buffer
3023 * has proper 16-byte alignment (that is, its `x0` is 16-byte
3024 * aligned). Here we over-allocate the linear buffer by enough
3025 * bytes to get the proper alignment.
3026 */
3027 map->buffer = _mesa_align_malloc(map->stride * (y2 - y1) + (x1 & 0xf), 16);
3028 map->ptr = (char *)map->buffer + (x1 & 0xf);
3029 assert(map->buffer);
3030
3031 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3032 char *src = intel_miptree_map_raw(brw, mt, map->mode | MAP_RAW);
3033 src += mt->offset;
3034
3035 const isl_memcpy_type copy_type =
3036 #if defined(USE_SSE41)
3037 cpu_has_sse4_1 ? ISL_MEMCPY_STREAMING_LOAD :
3038 #endif
3039 ISL_MEMCPY;
3040
3041 isl_memcpy_tiled_to_linear(
3042 x1, x2, y1, y2, map->ptr, src, map->stride,
3043 mt->surf.row_pitch_B, brw->has_swizzling, mt->surf.tiling,
3044 copy_type);
3045
3046 intel_miptree_unmap_raw(mt);
3047 }
3048
3049 map->unmap = intel_miptree_unmap_tiled_memcpy;
3050 }
3051
3052 static void
3053 intel_miptree_map_blit(struct brw_context *brw,
3054 struct intel_mipmap_tree *mt,
3055 struct intel_miptree_map *map,
3056 unsigned int level, unsigned int slice)
3057 {
3058 const struct gen_device_info *devinfo = &brw->screen->devinfo;
3059 map->linear_mt = make_surface(brw, GL_TEXTURE_2D, mt->format,
3060 0, 0, map->w, map->h, 1, 1,
3061 ISL_TILING_LINEAR_BIT,
3062 ISL_SURF_USAGE_RENDER_TARGET_BIT |
3063 ISL_SURF_USAGE_TEXTURE_BIT,
3064 0, 0, NULL);
3065
3066 if (!map->linear_mt) {
3067 fprintf(stderr, "Failed to allocate blit temporary\n");
3068 goto fail;
3069 }
3070 map->stride = map->linear_mt->surf.row_pitch_B;
3071
3072 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3073 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3074 * invalidate is set, since we'll be writing the whole rectangle from our
3075 * temporary buffer back out.
3076 */
3077 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3078 if (devinfo->gen >= 6) {
3079 brw_blorp_copy_miptrees(brw, mt, level, slice,
3080 map->linear_mt, 0, 0,
3081 map->x, map->y, 0, 0, map->w, map->h);
3082 } else {
3083 if (!intel_miptree_copy(brw,
3084 mt, level, slice, map->x, map->y,
3085 map->linear_mt, 0, 0, 0, 0,
3086 map->w, map->h)) {
3087 fprintf(stderr, "Failed to blit\n");
3088 goto fail;
3089 }
3090 }
3091 }
3092
3093 map->ptr = intel_miptree_map_raw(brw, map->linear_mt, map->mode);
3094
3095 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
3096 map->x, map->y, map->w, map->h,
3097 mt, _mesa_get_format_name(mt->format),
3098 level, slice, map->ptr, map->stride);
3099
3100 map->unmap = intel_miptree_unmap_blit;
3101 return;
3102
3103 fail:
3104 intel_miptree_release(&map->linear_mt);
3105 map->ptr = NULL;
3106 map->stride = 0;
3107 }
3108
3109 /**
3110 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
3111 */
3112 #if defined(USE_SSE41)
3113 static void
3114 intel_miptree_unmap_movntdqa(struct brw_context *brw,
3115 struct intel_mipmap_tree *mt,
3116 struct intel_miptree_map *map,
3117 unsigned int level,
3118 unsigned int slice)
3119 {
3120 _mesa_align_free(map->buffer);
3121 map->buffer = NULL;
3122 map->ptr = NULL;
3123 }
3124
3125 static void
3126 intel_miptree_map_movntdqa(struct brw_context *brw,
3127 struct intel_mipmap_tree *mt,
3128 struct intel_miptree_map *map,
3129 unsigned int level, unsigned int slice)
3130 {
3131 assert(map->mode & GL_MAP_READ_BIT);
3132 assert(!(map->mode & GL_MAP_WRITE_BIT));
3133
3134 intel_miptree_access_raw(brw, mt, level, slice, false);
3135
3136 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
3137 map->x, map->y, map->w, map->h,
3138 mt, _mesa_get_format_name(mt->format),
3139 level, slice, map->ptr, map->stride);
3140
3141 /* Map the original image */
3142 uint32_t image_x;
3143 uint32_t image_y;
3144 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3145 image_x += map->x;
3146 image_y += map->y;
3147
3148 void *src = intel_miptree_map_raw(brw, mt, map->mode);
3149 if (!src)
3150 return;
3151
3152 src += mt->offset;
3153
3154 src += image_y * mt->surf.row_pitch_B;
3155 src += image_x * mt->cpp;
3156
3157 /* Due to the pixel offsets for the particular image being mapped, our
3158 * src pointer may not be 16-byte aligned. However, if the pitch is
3159 * divisible by 16, then the amount by which it's misaligned will remain
3160 * consistent from row to row.
3161 */
3162 assert((mt->surf.row_pitch_B % 16) == 0);
3163 const int misalignment = ((uintptr_t) src) & 15;
3164
3165 /* Create an untiled temporary buffer for the mapping. */
3166 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
3167
3168 map->stride = ALIGN(misalignment + width_bytes, 16);
3169
3170 map->buffer = _mesa_align_malloc(map->stride * map->h, 16);
3171 /* Offset the destination so it has the same misalignment as src. */
3172 map->ptr = map->buffer + misalignment;
3173
3174 assert((((uintptr_t) map->ptr) & 15) == misalignment);
3175
3176 for (uint32_t y = 0; y < map->h; y++) {
3177 void *dst_ptr = map->ptr + y * map->stride;
3178 void *src_ptr = src + y * mt->surf.row_pitch_B;
3179
3180 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
3181 }
3182
3183 intel_miptree_unmap_raw(mt);
3184
3185 map->unmap = intel_miptree_unmap_movntdqa;
3186 }
3187 #endif
3188
3189 static void
3190 intel_miptree_unmap_s8(struct brw_context *brw,
3191 struct intel_mipmap_tree *mt,
3192 struct intel_miptree_map *map,
3193 unsigned int level,
3194 unsigned int slice)
3195 {
3196 if (map->mode & GL_MAP_WRITE_BIT) {
3197 unsigned int image_x, image_y;
3198 uint8_t *untiled_s8_map = map->ptr;
3199 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt, GL_MAP_WRITE_BIT);
3200
3201 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3202
3203 for (uint32_t y = 0; y < map->h; y++) {
3204 for (uint32_t x = 0; x < map->w; x++) {
3205 ptrdiff_t offset = intel_offset_S8(mt->surf.row_pitch_B,
3206 image_x + x + map->x,
3207 image_y + y + map->y,
3208 brw->has_swizzling);
3209 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
3210 }
3211 }
3212
3213 intel_miptree_unmap_raw(mt);
3214 }
3215
3216 free(map->buffer);
3217 }
3218
3219 static void
3220 intel_miptree_map_s8(struct brw_context *brw,
3221 struct intel_mipmap_tree *mt,
3222 struct intel_miptree_map *map,
3223 unsigned int level, unsigned int slice)
3224 {
3225 map->stride = map->w;
3226 map->buffer = map->ptr = malloc(map->stride * map->h);
3227 if (!map->buffer)
3228 return;
3229
3230 intel_miptree_access_raw(brw, mt, level, slice,
3231 map->mode & GL_MAP_WRITE_BIT);
3232
3233 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3234 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3235 * invalidate is set, since we'll be writing the whole rectangle from our
3236 * temporary buffer back out.
3237 */
3238 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3239 uint8_t *untiled_s8_map = map->ptr;
3240 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt, GL_MAP_READ_BIT);
3241 unsigned int image_x, image_y;
3242
3243 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3244
3245 for (uint32_t y = 0; y < map->h; y++) {
3246 for (uint32_t x = 0; x < map->w; x++) {
3247 ptrdiff_t offset = intel_offset_S8(mt->surf.row_pitch_B,
3248 x + image_x + map->x,
3249 y + image_y + map->y,
3250 brw->has_swizzling);
3251 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
3252 }
3253 }
3254
3255 intel_miptree_unmap_raw(mt);
3256
3257 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__,
3258 map->x, map->y, map->w, map->h,
3259 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
3260 } else {
3261 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
3262 map->x, map->y, map->w, map->h,
3263 mt, map->ptr, map->stride);
3264 }
3265
3266 map->unmap = intel_miptree_unmap_s8;
3267 }
3268
3269 /**
3270 * Mapping functions for packed depth/stencil miptrees backed by real separate
3271 * miptrees for depth and stencil.
3272 *
3273 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
3274 * separate from the depth buffer. Yet at the GL API level, we have to expose
3275 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
3276 * be able to map that memory for texture storage and glReadPixels-type
3277 * operations. We give Mesa core that access by mallocing a temporary and
3278 * copying the data between the actual backing store and the temporary.
3279 */
3280 static void
3281 intel_miptree_unmap_depthstencil(struct brw_context *brw,
3282 struct intel_mipmap_tree *mt,
3283 struct intel_miptree_map *map,
3284 unsigned int level,
3285 unsigned int slice)
3286 {
3287 struct intel_mipmap_tree *z_mt = mt;
3288 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
3289 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
3290
3291 if (map->mode & GL_MAP_WRITE_BIT) {
3292 uint32_t *packed_map = map->ptr;
3293 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt, GL_MAP_WRITE_BIT);
3294 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt, GL_MAP_WRITE_BIT);
3295 unsigned int s_image_x, s_image_y;
3296 unsigned int z_image_x, z_image_y;
3297
3298 intel_miptree_get_image_offset(s_mt, level, slice,
3299 &s_image_x, &s_image_y);
3300 intel_miptree_get_image_offset(z_mt, level, slice,
3301 &z_image_x, &z_image_y);
3302
3303 for (uint32_t y = 0; y < map->h; y++) {
3304 for (uint32_t x = 0; x < map->w; x++) {
3305 ptrdiff_t s_offset = intel_offset_S8(s_mt->surf.row_pitch_B,
3306 x + s_image_x + map->x,
3307 y + s_image_y + map->y,
3308 brw->has_swizzling);
3309 ptrdiff_t z_offset = ((y + z_image_y + map->y) *
3310 (z_mt->surf.row_pitch_B / 4) +
3311 (x + z_image_x + map->x));
3312
3313 if (map_z32f_x24s8) {
3314 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
3315 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
3316 } else {
3317 uint32_t packed = packed_map[y * map->w + x];
3318 s_map[s_offset] = packed >> 24;
3319 z_map[z_offset] = packed;
3320 }
3321 }
3322 }
3323
3324 intel_miptree_unmap_raw(s_mt);
3325 intel_miptree_unmap_raw(z_mt);
3326
3327 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
3328 __func__,
3329 map->x, map->y, map->w, map->h,
3330 z_mt, _mesa_get_format_name(z_mt->format),
3331 map->x + z_image_x, map->y + z_image_y,
3332 s_mt, map->x + s_image_x, map->y + s_image_y,
3333 map->ptr, map->stride);
3334 }
3335
3336 free(map->buffer);
3337 }
3338
3339 static void
3340 intel_miptree_map_depthstencil(struct brw_context *brw,
3341 struct intel_mipmap_tree *mt,
3342 struct intel_miptree_map *map,
3343 unsigned int level, unsigned int slice)
3344 {
3345 struct intel_mipmap_tree *z_mt = mt;
3346 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
3347 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
3348 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
3349
3350 map->stride = map->w * packed_bpp;
3351 map->buffer = map->ptr = malloc(map->stride * map->h);
3352 if (!map->buffer)
3353 return;
3354
3355 intel_miptree_access_raw(brw, z_mt, level, slice,
3356 map->mode & GL_MAP_WRITE_BIT);
3357 intel_miptree_access_raw(brw, s_mt, level, slice,
3358 map->mode & GL_MAP_WRITE_BIT);
3359
3360 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3361 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3362 * invalidate is set, since we'll be writing the whole rectangle from our
3363 * temporary buffer back out.
3364 */
3365 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3366 uint32_t *packed_map = map->ptr;
3367 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt, GL_MAP_READ_BIT);
3368 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt, GL_MAP_READ_BIT);
3369 unsigned int s_image_x, s_image_y;
3370 unsigned int z_image_x, z_image_y;
3371
3372 intel_miptree_get_image_offset(s_mt, level, slice,
3373 &s_image_x, &s_image_y);
3374 intel_miptree_get_image_offset(z_mt, level, slice,
3375 &z_image_x, &z_image_y);
3376
3377 for (uint32_t y = 0; y < map->h; y++) {
3378 for (uint32_t x = 0; x < map->w; x++) {
3379 int map_x = map->x + x, map_y = map->y + y;
3380 ptrdiff_t s_offset = intel_offset_S8(s_mt->surf.row_pitch_B,
3381 map_x + s_image_x,
3382 map_y + s_image_y,
3383 brw->has_swizzling);
3384 ptrdiff_t z_offset = ((map_y + z_image_y) *
3385 (z_mt->surf.row_pitch_B / 4) +
3386 (map_x + z_image_x));
3387 uint8_t s = s_map[s_offset];
3388 uint32_t z = z_map[z_offset];
3389
3390 if (map_z32f_x24s8) {
3391 packed_map[(y * map->w + x) * 2 + 0] = z;
3392 packed_map[(y * map->w + x) * 2 + 1] = s;
3393 } else {
3394 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
3395 }
3396 }
3397 }
3398
3399 intel_miptree_unmap_raw(s_mt);
3400 intel_miptree_unmap_raw(z_mt);
3401
3402 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
3403 __func__,
3404 map->x, map->y, map->w, map->h,
3405 z_mt, map->x + z_image_x, map->y + z_image_y,
3406 s_mt, map->x + s_image_x, map->y + s_image_y,
3407 map->ptr, map->stride);
3408 } else {
3409 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
3410 map->x, map->y, map->w, map->h,
3411 mt, map->ptr, map->stride);
3412 }
3413
3414 map->unmap = intel_miptree_unmap_depthstencil;
3415 }
3416
3417 /**
3418 * Create and attach a map to the miptree at (level, slice). Return the
3419 * attached map.
3420 */
3421 static struct intel_miptree_map*
3422 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
3423 unsigned int level,
3424 unsigned int slice,
3425 unsigned int x,
3426 unsigned int y,
3427 unsigned int w,
3428 unsigned int h,
3429 GLbitfield mode)
3430 {
3431 struct intel_miptree_map *map = calloc(1, sizeof(*map));
3432
3433 if (!map)
3434 return NULL;
3435
3436 assert(mt->level[level].slice[slice].map == NULL);
3437 mt->level[level].slice[slice].map = map;
3438
3439 map->mode = mode;
3440 map->x = x;
3441 map->y = y;
3442 map->w = w;
3443 map->h = h;
3444
3445 return map;
3446 }
3447
3448 /**
3449 * Release the map at (level, slice).
3450 */
3451 static void
3452 intel_miptree_release_map(struct intel_mipmap_tree *mt,
3453 unsigned int level,
3454 unsigned int slice)
3455 {
3456 struct intel_miptree_map **map;
3457
3458 map = &mt->level[level].slice[slice].map;
3459 free(*map);
3460 *map = NULL;
3461 }
3462
3463 static bool
3464 can_blit_slice(struct intel_mipmap_tree *mt,
3465 const struct intel_miptree_map *map)
3466 {
3467 /* See intel_miptree_blit() for details on the 32k pitch limit. */
3468 const unsigned src_blt_pitch = intel_miptree_blt_pitch(mt);
3469 const unsigned dst_blt_pitch = ALIGN(map->w * mt->cpp, 64);
3470 return src_blt_pitch < 32768 && dst_blt_pitch < 32768;
3471 }
3472
3473 static bool
3474 use_intel_mipree_map_blit(struct brw_context *brw,
3475 struct intel_mipmap_tree *mt,
3476 const struct intel_miptree_map *map)
3477 {
3478 const struct gen_device_info *devinfo = &brw->screen->devinfo;
3479
3480 if (devinfo->has_llc &&
3481 /* It's probably not worth swapping to the blit ring because of
3482 * all the overhead involved.
3483 */
3484 !(map->mode & GL_MAP_WRITE_BIT) &&
3485 !mt->compressed &&
3486 (mt->surf.tiling == ISL_TILING_X ||
3487 /* Prior to Sandybridge, the blitter can't handle Y tiling */
3488 (devinfo->gen >= 6 && mt->surf.tiling == ISL_TILING_Y0) ||
3489 /* Fast copy blit on skl+ supports all tiling formats. */
3490 devinfo->gen >= 9) &&
3491 can_blit_slice(mt, map))
3492 return true;
3493
3494 if (mt->surf.tiling != ISL_TILING_LINEAR &&
3495 mt->bo->size >= brw->max_gtt_map_object_size) {
3496 assert(can_blit_slice(mt, map));
3497 return true;
3498 }
3499
3500 return false;
3501 }
3502
3503 /**
3504 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
3505 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
3506 * arithmetic overflow.
3507 *
3508 * If you call this function and use \a out_stride, then you're doing pointer
3509 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
3510 * bugs. The caller must still take care to avoid 32-bit overflow errors in
3511 * all arithmetic expressions that contain buffer offsets and pixel sizes,
3512 * which usually have type uint32_t or GLuint.
3513 */
3514 void
3515 intel_miptree_map(struct brw_context *brw,
3516 struct intel_mipmap_tree *mt,
3517 unsigned int level,
3518 unsigned int slice,
3519 unsigned int x,
3520 unsigned int y,
3521 unsigned int w,
3522 unsigned int h,
3523 GLbitfield mode,
3524 void **out_ptr,
3525 ptrdiff_t *out_stride)
3526 {
3527 const struct gen_device_info *devinfo = &brw->screen->devinfo;
3528 struct intel_miptree_map *map;
3529
3530 assert(mt->surf.samples == 1);
3531
3532 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
3533 if (!map){
3534 *out_ptr = NULL;
3535 *out_stride = 0;
3536 return;
3537 }
3538
3539 if (mt->format == MESA_FORMAT_S_UINT8) {
3540 intel_miptree_map_s8(brw, mt, map, level, slice);
3541 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
3542 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
3543 } else if (use_intel_mipree_map_blit(brw, mt, map)) {
3544 intel_miptree_map_blit(brw, mt, map, level, slice);
3545 } else if (mt->surf.tiling != ISL_TILING_LINEAR && devinfo->gen > 4) {
3546 intel_miptree_map_tiled_memcpy(brw, mt, map, level, slice);
3547 #if defined(USE_SSE41)
3548 } else if (!(mode & GL_MAP_WRITE_BIT) &&
3549 !mt->compressed && cpu_has_sse4_1 &&
3550 (mt->surf.row_pitch_B % 16 == 0)) {
3551 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
3552 #endif
3553 } else {
3554 if (mt->surf.tiling != ISL_TILING_LINEAR)
3555 perf_debug("intel_miptree_map: mapping via gtt");
3556 intel_miptree_map_map(brw, mt, map, level, slice);
3557 }
3558
3559 *out_ptr = map->ptr;
3560 *out_stride = map->stride;
3561
3562 if (map->ptr == NULL)
3563 intel_miptree_release_map(mt, level, slice);
3564 }
3565
3566 void
3567 intel_miptree_unmap(struct brw_context *brw,
3568 struct intel_mipmap_tree *mt,
3569 unsigned int level,
3570 unsigned int slice)
3571 {
3572 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
3573
3574 assert(mt->surf.samples == 1);
3575
3576 if (!map)
3577 return;
3578
3579 DBG("%s: mt %p (%s) level %d slice %d\n", __func__,
3580 mt, _mesa_get_format_name(mt->format), level, slice);
3581
3582 if (map->unmap)
3583 map->unmap(brw, mt, map, level, slice);
3584
3585 intel_miptree_release_map(mt, level, slice);
3586 }
3587
3588 enum isl_surf_dim
3589 get_isl_surf_dim(GLenum target)
3590 {
3591 switch (target) {
3592 case GL_TEXTURE_1D:
3593 case GL_TEXTURE_1D_ARRAY:
3594 return ISL_SURF_DIM_1D;
3595
3596 case GL_TEXTURE_2D:
3597 case GL_TEXTURE_2D_ARRAY:
3598 case GL_TEXTURE_RECTANGLE:
3599 case GL_TEXTURE_CUBE_MAP:
3600 case GL_TEXTURE_CUBE_MAP_ARRAY:
3601 case GL_TEXTURE_2D_MULTISAMPLE:
3602 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3603 case GL_TEXTURE_EXTERNAL_OES:
3604 return ISL_SURF_DIM_2D;
3605
3606 case GL_TEXTURE_3D:
3607 return ISL_SURF_DIM_3D;
3608 }
3609
3610 unreachable("Invalid texture target");
3611 }
3612
3613 enum isl_dim_layout
3614 get_isl_dim_layout(const struct gen_device_info *devinfo,
3615 enum isl_tiling tiling, GLenum target)
3616 {
3617 switch (target) {
3618 case GL_TEXTURE_1D:
3619 case GL_TEXTURE_1D_ARRAY:
3620 return (devinfo->gen >= 9 && tiling == ISL_TILING_LINEAR ?
3621 ISL_DIM_LAYOUT_GEN9_1D : ISL_DIM_LAYOUT_GEN4_2D);
3622
3623 case GL_TEXTURE_2D:
3624 case GL_TEXTURE_2D_ARRAY:
3625 case GL_TEXTURE_RECTANGLE:
3626 case GL_TEXTURE_2D_MULTISAMPLE:
3627 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3628 case GL_TEXTURE_EXTERNAL_OES:
3629 return ISL_DIM_LAYOUT_GEN4_2D;
3630
3631 case GL_TEXTURE_CUBE_MAP:
3632 case GL_TEXTURE_CUBE_MAP_ARRAY:
3633 return (devinfo->gen == 4 ? ISL_DIM_LAYOUT_GEN4_3D :
3634 ISL_DIM_LAYOUT_GEN4_2D);
3635
3636 case GL_TEXTURE_3D:
3637 return (devinfo->gen >= 9 ?
3638 ISL_DIM_LAYOUT_GEN4_2D : ISL_DIM_LAYOUT_GEN4_3D);
3639 }
3640
3641 unreachable("Invalid texture target");
3642 }
3643
3644 bool
3645 intel_miptree_set_clear_color(struct brw_context *brw,
3646 struct intel_mipmap_tree *mt,
3647 union isl_color_value clear_color)
3648 {
3649 if (memcmp(&mt->fast_clear_color, &clear_color, sizeof(clear_color)) != 0) {
3650 mt->fast_clear_color = clear_color;
3651 if (mt->aux_buf->clear_color_bo) {
3652 /* We can't update the clear color while the hardware is still using
3653 * the previous one for a resolve or sampling from it. Make sure that
3654 * there are no pending commands at this point.
3655 */
3656 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL);
3657 for (int i = 0; i < 4; i++) {
3658 brw_store_data_imm32(brw, mt->aux_buf->clear_color_bo,
3659 mt->aux_buf->clear_color_offset + i * 4,
3660 mt->fast_clear_color.u32[i]);
3661 }
3662 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_STATE_CACHE_INVALIDATE);
3663 }
3664 brw->ctx.NewDriverState |= BRW_NEW_AUX_STATE;
3665 return true;
3666 }
3667 return false;
3668 }
3669
3670 union isl_color_value
3671 intel_miptree_get_clear_color(const struct gen_device_info *devinfo,
3672 const struct intel_mipmap_tree *mt,
3673 enum isl_format view_format, bool sampling,
3674 struct brw_bo **clear_color_bo,
3675 uint64_t *clear_color_offset)
3676 {
3677 assert(mt->aux_buf);
3678
3679 if (devinfo->gen == 10 && isl_format_is_srgb(view_format) && sampling) {
3680 /* The gen10 sampler doesn't gamma-correct the clear color. In this case,
3681 * we switch to using the inline clear color and do the sRGB color
3682 * conversion process defined in the OpenGL spec. The red, green, and
3683 * blue channels take part in gamma correction, while the alpha channel
3684 * is unchanged.
3685 */
3686 union isl_color_value srgb_decoded_value = mt->fast_clear_color;
3687 for (unsigned i = 0; i < 3; i++) {
3688 srgb_decoded_value.f32[i] =
3689 util_format_srgb_to_linear_float(mt->fast_clear_color.f32[i]);
3690 }
3691 *clear_color_bo = 0;
3692 *clear_color_offset = 0;
3693 return srgb_decoded_value;
3694 } else {
3695 *clear_color_bo = mt->aux_buf->clear_color_bo;
3696 *clear_color_offset = mt->aux_buf->clear_color_offset;
3697 return mt->fast_clear_color;
3698 }
3699 }
3700
3701 static void
3702 intel_miptree_update_etc_shadow(struct brw_context *brw,
3703 struct intel_mipmap_tree *mt,
3704 unsigned int level,
3705 unsigned int slice,
3706 int level_w,
3707 int level_h)
3708 {
3709 ptrdiff_t etc_stride, shadow_stride;
3710 void *mptr, *sptr;
3711 struct intel_mipmap_tree *smt = mt->shadow_mt;
3712
3713 assert(intel_miptree_has_etc_shadow(brw, mt));
3714
3715 intel_miptree_map(brw, mt, level, slice, 0, 0, level_w, level_h,
3716 GL_MAP_READ_BIT, &mptr, &etc_stride);
3717 intel_miptree_map(brw, smt, level, slice, 0, 0, level_w, level_h,
3718 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT,
3719 &sptr, &shadow_stride);
3720
3721 if (mt->format == MESA_FORMAT_ETC1_RGB8) {
3722 _mesa_etc1_unpack_rgba8888(sptr, shadow_stride, mptr, etc_stride,
3723 level_w, level_h);
3724 } else {
3725 /* destination and source images must have the same swizzle */
3726 bool is_bgra = (smt->format == MESA_FORMAT_B8G8R8A8_SRGB);
3727 _mesa_unpack_etc2_format(sptr, shadow_stride, mptr, etc_stride,
3728 level_w, level_h, mt->format, is_bgra);
3729 }
3730
3731 intel_miptree_unmap(brw, mt, level, slice);
3732 intel_miptree_unmap(brw, smt, level, slice);
3733 }
3734
3735 void
3736 intel_miptree_update_etc_shadow_levels(struct brw_context *brw,
3737 struct intel_mipmap_tree *mt)
3738 {
3739 struct intel_mipmap_tree *smt;
3740 int num_slices;
3741
3742 assert(mt);
3743 assert(mt->surf.size_B > 0);
3744 assert(intel_miptree_has_etc_shadow(brw, mt));
3745
3746 smt = mt->shadow_mt;
3747 num_slices = smt->surf.logical_level0_px.array_len;
3748
3749 for (int level = smt->first_level; level <= smt->last_level; level++) {
3750 int level_w = minify(smt->surf.logical_level0_px.width,
3751 level - smt->first_level);
3752 int level_h = minify(smt->surf.logical_level0_px.height,
3753 level - smt->first_level);
3754
3755 for (unsigned int slice = 0; slice < num_slices; slice++) {
3756 intel_miptree_update_etc_shadow(brw, mt, level, slice, level_w,
3757 level_h);
3758 }
3759 }
3760
3761 mt->shadow_needs_update = false;
3762 }