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