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