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