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