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