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