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