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