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