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