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