i965: Don't forget to free the old singlesample_mt.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_mipmap_tree.c
1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <GL/gl.h>
29 #include <GL/internal/dri_interface.h>
30
31 #include "intel_batchbuffer.h"
32 #include "intel_chipset.h"
33 #include "intel_mipmap_tree.h"
34 #include "intel_regions.h"
35 #include "intel_resolve_map.h"
36 #include "intel_tex.h"
37 #include "intel_blit.h"
38 #include "intel_fbo.h"
39
40 #include "brw_blorp.h"
41 #include "brw_context.h"
42
43 #include "main/enums.h"
44 #include "main/formats.h"
45 #include "main/glformats.h"
46 #include "main/texcompress_etc.h"
47 #include "main/teximage.h"
48 #include "main/streaming-load-memcpy.h"
49
50 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
51
52 /**
53 * Determine which MSAA layout should be used by the MSAA surface being
54 * created, based on the chip generation and the surface type.
55 */
56 static enum intel_msaa_layout
57 compute_msaa_layout(struct brw_context *brw, mesa_format format, GLenum target)
58 {
59 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
60 if (brw->gen < 7)
61 return INTEL_MSAA_LAYOUT_IMS;
62
63 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
64 switch (_mesa_get_format_base_format(format)) {
65 case GL_DEPTH_COMPONENT:
66 case GL_STENCIL_INDEX:
67 case GL_DEPTH_STENCIL:
68 return INTEL_MSAA_LAYOUT_IMS;
69 default:
70 /* Disable MCS on Broadwell for now. We can enable it once things
71 * are working without it.
72 */
73 if (brw->gen >= 8) {
74 perf_debug("Missing CMS support on Broadwell.\n");
75 return INTEL_MSAA_LAYOUT_UMS;
76 }
77
78 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
79 *
80 * This field must be set to 0 for all SINT MSRTs when all RT channels
81 * are not written
82 *
83 * In practice this means that we have to disable MCS for all signed
84 * integer MSAA buffers. The alternative, to disable MCS only when one
85 * of the render target channels is disabled, is impractical because it
86 * would require converting between CMS and UMS MSAA layouts on the fly,
87 * which is expensive.
88 */
89 if (_mesa_get_format_datatype(format) == GL_INT) {
90 /* TODO: is this workaround needed for future chipsets? */
91 assert(brw->gen == 7);
92 return INTEL_MSAA_LAYOUT_UMS;
93 } else {
94 return INTEL_MSAA_LAYOUT_CMS;
95 }
96 }
97 }
98
99
100 /**
101 * For single-sampled render targets ("non-MSRT"), the MCS buffer is a
102 * scaled-down bitfield representation of the color buffer which is capable of
103 * recording when blocks of the color buffer are equal to the clear value.
104 * This function returns the block size that will be used by the MCS buffer
105 * corresponding to a certain color miptree.
106 *
107 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
108 * beneath the "Fast Color Clear" bullet (p327):
109 *
110 * The following table describes the RT alignment
111 *
112 * Pixels Lines
113 * TiledY RT CL
114 * bpp
115 * 32 8 4
116 * 64 4 4
117 * 128 2 4
118 * TiledX RT CL
119 * bpp
120 * 32 16 2
121 * 64 8 2
122 * 128 4 2
123 *
124 * This alignment has the following uses:
125 *
126 * - For figuring out the size of the MCS buffer. Each 4k tile in the MCS
127 * buffer contains 128 blocks horizontally and 256 blocks vertically.
128 *
129 * - For figuring out alignment restrictions for a fast clear operation. Fast
130 * clear operations must always clear aligned multiples of 16 blocks
131 * horizontally and 32 blocks vertically.
132 *
133 * - For scaling down the coordinates sent through the render pipeline during
134 * a fast clear. X coordinates must be scaled down by 8 times the block
135 * width, and Y coordinates by 16 times the block height.
136 *
137 * - For scaling down the coordinates sent through the render pipeline during
138 * a "Render Target Resolve" operation. X coordinates must be scaled down
139 * by half the block width, and Y coordinates by half the block height.
140 */
141 void
142 intel_get_non_msrt_mcs_alignment(struct brw_context *brw,
143 struct intel_mipmap_tree *mt,
144 unsigned *width_px, unsigned *height)
145 {
146 switch (mt->region->tiling) {
147 default:
148 assert(!"Non-MSRT MCS requires X or Y tiling");
149 /* In release builds, fall through */
150 case I915_TILING_Y:
151 *width_px = 32 / mt->cpp;
152 *height = 4;
153 break;
154 case I915_TILING_X:
155 *width_px = 64 / mt->cpp;
156 *height = 2;
157 }
158 }
159
160
161 /**
162 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
163 * can be used.
164 *
165 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
166 * beneath the "Fast Color Clear" bullet (p326):
167 *
168 * - Support is limited to tiled render targets.
169 * - Support is for non-mip-mapped and non-array surface types only.
170 *
171 * And then later, on p327:
172 *
173 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
174 * 64bpp, and 128bpp.
175 */
176 bool
177 intel_is_non_msrt_mcs_buffer_supported(struct brw_context *brw,
178 struct intel_mipmap_tree *mt)
179 {
180 /* MCS support does not exist prior to Gen7 */
181 if (brw->gen < 7 || brw->gen >= 8)
182 return false;
183
184 /* MCS is only supported for color buffers */
185 switch (_mesa_get_format_base_format(mt->format)) {
186 case GL_DEPTH_COMPONENT:
187 case GL_DEPTH_STENCIL:
188 case GL_STENCIL_INDEX:
189 return false;
190 }
191
192 if (mt->region->tiling != I915_TILING_X &&
193 mt->region->tiling != I915_TILING_Y)
194 return false;
195 if (mt->cpp != 4 && mt->cpp != 8 && mt->cpp != 16)
196 return false;
197 if (mt->first_level != 0 || mt->last_level != 0)
198 return false;
199 if (mt->physical_depth0 != 1)
200 return false;
201
202 /* There's no point in using an MCS buffer if the surface isn't in a
203 * renderable format.
204 */
205 if (!brw->format_supported_as_render_target[mt->format])
206 return false;
207
208 return true;
209 }
210
211
212 /**
213 * @param for_bo Indicates that the caller is
214 * intel_miptree_create_for_bo(). If true, then do not create
215 * \c stencil_mt.
216 */
217 struct intel_mipmap_tree *
218 intel_miptree_create_layout(struct brw_context *brw,
219 GLenum target,
220 mesa_format format,
221 GLuint first_level,
222 GLuint last_level,
223 GLuint width0,
224 GLuint height0,
225 GLuint depth0,
226 bool for_bo,
227 GLuint num_samples)
228 {
229 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
230 if (!mt)
231 return NULL;
232
233 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
234 _mesa_lookup_enum_by_nr(target),
235 _mesa_get_format_name(format),
236 first_level, last_level, mt);
237
238 mt->target = target;
239 mt->format = format;
240 mt->first_level = first_level;
241 mt->last_level = last_level;
242 mt->logical_width0 = width0;
243 mt->logical_height0 = height0;
244 mt->logical_depth0 = depth0;
245 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
246
247 /* The cpp is bytes per (1, blockheight)-sized block for compressed
248 * textures. This is why you'll see divides by blockheight all over
249 */
250 unsigned bw, bh;
251 _mesa_get_format_block_size(format, &bw, &bh);
252 assert(_mesa_get_format_bytes(mt->format) % bw == 0);
253 mt->cpp = _mesa_get_format_bytes(mt->format) / bw;
254
255 mt->num_samples = num_samples;
256 mt->compressed = _mesa_is_format_compressed(format);
257 mt->msaa_layout = INTEL_MSAA_LAYOUT_NONE;
258 mt->refcount = 1;
259
260 if (num_samples > 1) {
261 /* Adjust width/height/depth for MSAA */
262 mt->msaa_layout = compute_msaa_layout(brw, format, mt->target);
263 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
264 /* In the Sandy Bridge PRM, volume 4, part 1, page 31, it says:
265 *
266 * "Any of the other messages (sample*, LOD, load4) used with a
267 * (4x) multisampled surface will in-effect sample a surface with
268 * double the height and width as that indicated in the surface
269 * state. Each pixel position on the original-sized surface is
270 * replaced with a 2x2 of samples with the following arrangement:
271 *
272 * sample 0 sample 2
273 * sample 1 sample 3"
274 *
275 * Thus, when sampling from a multisampled texture, it behaves as
276 * though the layout in memory for (x,y,sample) is:
277 *
278 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
279 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
280 *
281 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
282 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
283 *
284 * However, the actual layout of multisampled data in memory is:
285 *
286 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
287 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
288 *
289 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
290 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
291 *
292 * This pattern repeats for each 2x2 pixel block.
293 *
294 * As a result, when calculating the size of our 4-sample buffer for
295 * an odd width or height, we have to align before scaling up because
296 * sample 3 is in that bottom right 2x2 block.
297 */
298 switch (num_samples) {
299 case 2:
300 assert(brw->gen >= 8);
301 width0 = ALIGN(width0, 2) * 2;
302 height0 = ALIGN(height0, 2);
303 break;
304 case 4:
305 width0 = ALIGN(width0, 2) * 2;
306 height0 = ALIGN(height0, 2) * 2;
307 break;
308 case 8:
309 width0 = ALIGN(width0, 2) * 4;
310 height0 = ALIGN(height0, 2) * 2;
311 break;
312 default:
313 /* num_samples should already have been quantized to 0, 1, 2, 4, or
314 * 8.
315 */
316 assert(false);
317 }
318 } else {
319 /* Non-interleaved */
320 depth0 *= num_samples;
321 }
322 }
323
324 /* array_spacing_lod0 is only used for non-IMS MSAA surfaces. TODO: can we
325 * use it elsewhere?
326 */
327 switch (mt->msaa_layout) {
328 case INTEL_MSAA_LAYOUT_NONE:
329 case INTEL_MSAA_LAYOUT_IMS:
330 mt->array_spacing_lod0 = false;
331 break;
332 case INTEL_MSAA_LAYOUT_UMS:
333 case INTEL_MSAA_LAYOUT_CMS:
334 mt->array_spacing_lod0 = true;
335 break;
336 }
337
338 if (target == GL_TEXTURE_CUBE_MAP) {
339 assert(depth0 == 1);
340 depth0 = 6;
341 }
342
343 mt->physical_width0 = width0;
344 mt->physical_height0 = height0;
345 mt->physical_depth0 = depth0;
346
347 if (!for_bo &&
348 _mesa_get_format_base_format(format) == GL_DEPTH_STENCIL &&
349 (brw->must_use_separate_stencil ||
350 (brw->has_separate_stencil && brw_is_hiz_depth_format(brw, format)))) {
351 mt->stencil_mt = intel_miptree_create(brw,
352 mt->target,
353 MESA_FORMAT_S_UINT8,
354 mt->first_level,
355 mt->last_level,
356 mt->logical_width0,
357 mt->logical_height0,
358 mt->logical_depth0,
359 true,
360 num_samples,
361 INTEL_MIPTREE_TILING_ANY);
362 if (!mt->stencil_mt) {
363 intel_miptree_release(&mt);
364 return NULL;
365 }
366
367 /* Fix up the Z miptree format for how we're splitting out separate
368 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
369 */
370 if (mt->format == MESA_FORMAT_Z24_UNORM_S8_UINT) {
371 mt->format = MESA_FORMAT_Z24_UNORM_X8_UINT;
372 } else if (mt->format == MESA_FORMAT_Z32_FLOAT_S8X24_UINT) {
373 mt->format = MESA_FORMAT_Z_FLOAT32;
374 mt->cpp = 4;
375 } else {
376 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
377 _mesa_get_format_name(mt->format));
378 }
379 }
380
381 brw_miptree_layout(brw, mt);
382
383 return mt;
384 }
385
386 /**
387 * \brief Helper function for intel_miptree_create().
388 */
389 static uint32_t
390 intel_miptree_choose_tiling(struct brw_context *brw,
391 mesa_format format,
392 uint32_t width0,
393 uint32_t num_samples,
394 enum intel_miptree_tiling_mode requested,
395 struct intel_mipmap_tree *mt)
396 {
397 if (format == MESA_FORMAT_S_UINT8) {
398 /* The stencil buffer is W tiled. However, we request from the kernel a
399 * non-tiled buffer because the GTT is incapable of W fencing.
400 */
401 return I915_TILING_NONE;
402 }
403
404 /* Some usages may want only one type of tiling, like depth miptrees (Y
405 * tiled), or temporary BOs for uploading data once (linear).
406 */
407 switch (requested) {
408 case INTEL_MIPTREE_TILING_ANY:
409 break;
410 case INTEL_MIPTREE_TILING_Y:
411 return I915_TILING_Y;
412 case INTEL_MIPTREE_TILING_NONE:
413 return I915_TILING_NONE;
414 }
415
416 if (num_samples > 1) {
417 /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
418 * Surface"):
419 *
420 * [DevSNB+]: For multi-sample render targets, this field must be
421 * 1. MSRTs can only be tiled.
422 *
423 * Our usual reason for preferring X tiling (fast blits using the
424 * blitting engine) doesn't apply to MSAA, since we'll generally be
425 * downsampling or upsampling when blitting between the MSAA buffer
426 * and another buffer, and the blitting engine doesn't support that.
427 * So use Y tiling, since it makes better use of the cache.
428 */
429 return I915_TILING_Y;
430 }
431
432 GLenum base_format = _mesa_get_format_base_format(format);
433 if (base_format == GL_DEPTH_COMPONENT ||
434 base_format == GL_DEPTH_STENCIL_EXT)
435 return I915_TILING_Y;
436
437 int minimum_pitch = mt->total_width * mt->cpp;
438
439 /* If the width is much smaller than a tile, don't bother tiling. */
440 if (minimum_pitch < 64)
441 return I915_TILING_NONE;
442
443 if (ALIGN(minimum_pitch, 512) >= 32768 ||
444 mt->total_width >= 32768 || mt->total_height >= 32768) {
445 perf_debug("%dx%d miptree too large to blit, falling back to untiled",
446 mt->total_width, mt->total_height);
447 return I915_TILING_NONE;
448 }
449
450 /* Pre-gen6 doesn't have BLORP to handle Y-tiling, so use X-tiling. */
451 if (brw->gen < 6)
452 return I915_TILING_X;
453
454 /* From the Sandybridge PRM, Volume 1, Part 2, page 32:
455 * "NOTE: 128BPE Format Color Buffer ( render target ) MUST be either TileX
456 * or Linear."
457 * 128 bits per pixel translates to 16 bytes per pixel. This is necessary
458 * all the way back to 965, but is explicitly permitted on Gen7.
459 */
460 if (brw->gen != 7 && mt->cpp >= 16)
461 return I915_TILING_X;
462
463 /* From the Ivy Bridge PRM, Vol4 Part1 2.12.2.1 (SURFACE_STATE for most
464 * messages), on p64, under the heading "Surface Vertical Alignment":
465 *
466 * This field must be set to VALIGN_4 for all tiled Y Render Target
467 * surfaces.
468 *
469 * So if the surface is renderable and uses a vertical alignment of 2,
470 * force it to be X tiled. This is somewhat conservative (it's possible
471 * that the client won't ever render to this surface), but it's difficult
472 * to know that ahead of time. And besides, since we use a vertical
473 * alignment of 4 as often as we can, this shouldn't happen very often.
474 */
475 if (brw->gen == 7 && mt->align_h == 2 &&
476 brw->format_supported_as_render_target[format]) {
477 return I915_TILING_X;
478 }
479
480 return I915_TILING_Y | I915_TILING_X;
481 }
482
483 struct intel_mipmap_tree *
484 intel_miptree_create(struct brw_context *brw,
485 GLenum target,
486 mesa_format format,
487 GLuint first_level,
488 GLuint last_level,
489 GLuint width0,
490 GLuint height0,
491 GLuint depth0,
492 bool expect_accelerated_upload,
493 GLuint num_samples,
494 enum intel_miptree_tiling_mode requested_tiling)
495 {
496 struct intel_mipmap_tree *mt;
497 mesa_format tex_format = format;
498 mesa_format etc_format = MESA_FORMAT_NONE;
499 GLuint total_width, total_height;
500
501 if (brw->gen < 8 && !brw->is_baytrail) {
502 switch (format) {
503 case MESA_FORMAT_ETC1_RGB8:
504 format = MESA_FORMAT_R8G8B8X8_UNORM;
505 break;
506 case MESA_FORMAT_ETC2_RGB8:
507 format = MESA_FORMAT_R8G8B8X8_UNORM;
508 break;
509 case MESA_FORMAT_ETC2_SRGB8:
510 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
511 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
512 format = MESA_FORMAT_B8G8R8A8_SRGB;
513 break;
514 case MESA_FORMAT_ETC2_RGBA8_EAC:
515 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
516 format = MESA_FORMAT_R8G8B8A8_UNORM;
517 break;
518 case MESA_FORMAT_ETC2_R11_EAC:
519 format = MESA_FORMAT_R_UNORM16;
520 break;
521 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
522 format = MESA_FORMAT_R_SNORM16;
523 break;
524 case MESA_FORMAT_ETC2_RG11_EAC:
525 format = MESA_FORMAT_R16G16_UNORM;
526 break;
527 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
528 format = MESA_FORMAT_R16G16_SNORM;
529 break;
530 default:
531 /* Non ETC1 / ETC2 format */
532 break;
533 }
534 }
535
536 etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
537
538 mt = intel_miptree_create_layout(brw, target, format,
539 first_level, last_level, width0,
540 height0, depth0,
541 false, num_samples);
542 /*
543 * pitch == 0 || height == 0 indicates the null texture
544 */
545 if (!mt || !mt->total_width || !mt->total_height) {
546 intel_miptree_release(&mt);
547 return NULL;
548 }
549
550 total_width = mt->total_width;
551 total_height = mt->total_height;
552
553 if (format == MESA_FORMAT_S_UINT8) {
554 /* Align to size of W tile, 64x64. */
555 total_width = ALIGN(total_width, 64);
556 total_height = ALIGN(total_height, 64);
557 }
558
559 uint32_t tiling = intel_miptree_choose_tiling(brw, format, width0,
560 num_samples, requested_tiling,
561 mt);
562 bool y_or_x = tiling == (I915_TILING_Y | I915_TILING_X);
563
564 mt->etc_format = etc_format;
565 mt->region = intel_region_alloc(brw->intelScreen,
566 y_or_x ? I915_TILING_Y : tiling,
567 mt->cpp,
568 total_width,
569 total_height,
570 expect_accelerated_upload);
571
572 /* If the region is too large to fit in the aperture, we need to use the
573 * BLT engine to support it. The BLT paths can't currently handle Y-tiling,
574 * so we need to fall back to X.
575 */
576 if (y_or_x && mt->region->bo->size >= brw->max_gtt_map_object_size) {
577 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
578 mt->total_width, mt->total_height);
579 intel_region_release(&mt->region);
580
581 mt->region = intel_region_alloc(brw->intelScreen,
582 I915_TILING_X,
583 mt->cpp,
584 total_width,
585 total_height,
586 expect_accelerated_upload);
587 }
588
589 mt->offset = 0;
590
591 if (!mt->region) {
592 intel_miptree_release(&mt);
593 return NULL;
594 }
595
596
597 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
598 if (!intel_miptree_alloc_mcs(brw, mt, num_samples)) {
599 intel_miptree_release(&mt);
600 return NULL;
601 }
602 }
603
604 /* If this miptree is capable of supporting fast color clears, set
605 * fast_clear_state appropriately to ensure that fast clears will occur.
606 * Allocation of the MCS miptree will be deferred until the first fast
607 * clear actually occurs.
608 */
609 if (intel_is_non_msrt_mcs_buffer_supported(brw, mt))
610 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
611
612 return mt;
613 }
614
615 struct intel_mipmap_tree *
616 intel_miptree_create_for_bo(struct brw_context *brw,
617 drm_intel_bo *bo,
618 mesa_format format,
619 uint32_t offset,
620 uint32_t width,
621 uint32_t height,
622 int pitch,
623 uint32_t tiling)
624 {
625 struct intel_mipmap_tree *mt;
626
627 struct intel_region *region = calloc(1, sizeof(*region));
628 if (!region)
629 return NULL;
630
631 /* Nothing will be able to use this miptree with the BO if the offset isn't
632 * aligned.
633 */
634 if (tiling != I915_TILING_NONE)
635 assert(offset % 4096 == 0);
636
637 /* miptrees can't handle negative pitch. If you need flipping of images,
638 * that's outside of the scope of the mt.
639 */
640 assert(pitch >= 0);
641
642 mt = intel_miptree_create_layout(brw, GL_TEXTURE_2D, format,
643 0, 0,
644 width, height, 1,
645 true, 0 /* num_samples */);
646 if (!mt) {
647 free(region);
648 return mt;
649 }
650
651 region->cpp = mt->cpp;
652 region->width = width;
653 region->height = height;
654 region->pitch = pitch;
655 region->refcount = 1;
656 drm_intel_bo_reference(bo);
657 region->bo = bo;
658 region->tiling = tiling;
659
660 mt->region = region;
661 mt->offset = offset;
662
663 return mt;
664 }
665
666 /**
667 * For a singlesample image buffer, this simply wraps the given region with a miptree.
668 *
669 * For a multisample image buffer, this wraps the given region with
670 * a singlesample miptree, then creates a multisample miptree into which the
671 * singlesample miptree is embedded as a child.
672 */
673 void
674 intel_update_winsys_renderbuffer_miptree(struct brw_context *intel,
675 struct intel_renderbuffer *irb,
676 struct intel_region *region)
677 {
678 struct intel_mipmap_tree *singlesample_mt = NULL;
679 struct intel_mipmap_tree *multisample_mt = NULL;
680 struct gl_renderbuffer *rb = &irb->Base.Base;
681 mesa_format format = rb->Format;
682 int num_samples = rb->NumSamples;
683
684 intel_miptree_release(&irb->mt);
685 intel_miptree_release(&irb->singlesample_mt);
686
687 /* Only the front and back buffers, which are color buffers, are allocated
688 * through the image loader.
689 */
690 assert(_mesa_get_format_base_format(format) == GL_RGB ||
691 _mesa_get_format_base_format(format) == GL_RGBA);
692
693 singlesample_mt = intel_miptree_create_for_bo(intel,
694 region->bo,
695 format,
696 0,
697 region->width,
698 region->height,
699 region->pitch,
700 region->tiling);
701 if (!singlesample_mt)
702 return;
703 singlesample_mt->region->name = region->name;
704
705 /* If this miptree is capable of supporting fast color clears, set
706 * mcs_state appropriately to ensure that fast clears will occur.
707 * Allocation of the MCS miptree will be deferred until the first fast
708 * clear actually occurs.
709 */
710 if (intel_is_non_msrt_mcs_buffer_supported(intel, singlesample_mt))
711 singlesample_mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
712
713 if (num_samples == 0) {
714 irb->mt = singlesample_mt;
715 return;
716 }
717
718 multisample_mt = intel_miptree_create_for_renderbuffer(intel,
719 format,
720 region->width,
721 region->height,
722 num_samples);
723 if (!multisample_mt) {
724 intel_miptree_release(&singlesample_mt);
725 return;
726 }
727
728 irb->need_downsample = false;
729 irb->mt = multisample_mt;
730 irb->singlesample_mt = singlesample_mt;
731 }
732
733 struct intel_mipmap_tree*
734 intel_miptree_create_for_renderbuffer(struct brw_context *brw,
735 mesa_format format,
736 uint32_t width,
737 uint32_t height,
738 uint32_t num_samples)
739 {
740 struct intel_mipmap_tree *mt;
741 uint32_t depth = 1;
742 bool ok;
743 GLenum target = num_samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
744
745 mt = intel_miptree_create(brw, target, format, 0, 0,
746 width, height, depth, true, num_samples,
747 INTEL_MIPTREE_TILING_ANY);
748 if (!mt)
749 goto fail;
750
751 if (brw_is_hiz_depth_format(brw, format)) {
752 ok = intel_miptree_alloc_hiz(brw, mt);
753 if (!ok)
754 goto fail;
755 }
756
757 return mt;
758
759 fail:
760 intel_miptree_release(&mt);
761 return NULL;
762 }
763
764 void
765 intel_miptree_reference(struct intel_mipmap_tree **dst,
766 struct intel_mipmap_tree *src)
767 {
768 if (*dst == src)
769 return;
770
771 intel_miptree_release(dst);
772
773 if (src) {
774 src->refcount++;
775 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
776 }
777
778 *dst = src;
779 }
780
781
782 void
783 intel_miptree_release(struct intel_mipmap_tree **mt)
784 {
785 if (!*mt)
786 return;
787
788 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
789 if (--(*mt)->refcount <= 0) {
790 GLuint i;
791
792 DBG("%s deleting %p\n", __FUNCTION__, *mt);
793
794 intel_region_release(&((*mt)->region));
795 intel_miptree_release(&(*mt)->stencil_mt);
796 intel_miptree_release(&(*mt)->hiz_mt);
797 intel_miptree_release(&(*mt)->mcs_mt);
798 intel_resolve_map_clear(&(*mt)->hiz_map);
799
800 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
801 free((*mt)->level[i].slice);
802 }
803
804 free(*mt);
805 }
806 *mt = NULL;
807 }
808
809 void
810 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
811 int *width, int *height, int *depth)
812 {
813 switch (image->TexObject->Target) {
814 case GL_TEXTURE_1D_ARRAY:
815 *width = image->Width;
816 *height = 1;
817 *depth = image->Height;
818 break;
819 default:
820 *width = image->Width;
821 *height = image->Height;
822 *depth = image->Depth;
823 break;
824 }
825 }
826
827 /**
828 * Can the image be pulled into a unified mipmap tree? This mirrors
829 * the completeness test in a lot of ways.
830 *
831 * Not sure whether I want to pass gl_texture_image here.
832 */
833 bool
834 intel_miptree_match_image(struct intel_mipmap_tree *mt,
835 struct gl_texture_image *image)
836 {
837 struct intel_texture_image *intelImage = intel_texture_image(image);
838 GLuint level = intelImage->base.Base.Level;
839 int width, height, depth;
840
841 /* glTexImage* choose the texture object based on the target passed in, and
842 * objects can't change targets over their lifetimes, so this should be
843 * true.
844 */
845 assert(image->TexObject->Target == mt->target);
846
847 mesa_format mt_format = mt->format;
848 if (mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT && mt->stencil_mt)
849 mt_format = MESA_FORMAT_Z24_UNORM_S8_UINT;
850 if (mt->format == MESA_FORMAT_Z_FLOAT32 && mt->stencil_mt)
851 mt_format = MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
852 if (mt->etc_format != MESA_FORMAT_NONE)
853 mt_format = mt->etc_format;
854
855 if (image->TexFormat != mt_format)
856 return false;
857
858 intel_miptree_get_dimensions_for_image(image, &width, &height, &depth);
859
860 if (mt->target == GL_TEXTURE_CUBE_MAP)
861 depth = 6;
862
863 int level_depth = mt->level[level].depth;
864 if (mt->num_samples > 1) {
865 switch (mt->msaa_layout) {
866 case INTEL_MSAA_LAYOUT_NONE:
867 case INTEL_MSAA_LAYOUT_IMS:
868 break;
869 case INTEL_MSAA_LAYOUT_UMS:
870 case INTEL_MSAA_LAYOUT_CMS:
871 level_depth /= mt->num_samples;
872 break;
873 }
874 }
875
876 /* Test image dimensions against the base level image adjusted for
877 * minification. This will also catch images not present in the
878 * tree, changed targets, etc.
879 */
880 if (width != minify(mt->logical_width0, level - mt->first_level) ||
881 height != minify(mt->logical_height0, level - mt->first_level) ||
882 depth != level_depth) {
883 return false;
884 }
885
886 if (image->NumSamples != mt->num_samples)
887 return false;
888
889 return true;
890 }
891
892
893 void
894 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
895 GLuint level,
896 GLuint x, GLuint y, GLuint d)
897 {
898 mt->level[level].depth = d;
899 mt->level[level].level_x = x;
900 mt->level[level].level_y = y;
901
902 DBG("%s level %d, depth %d, offset %d,%d\n", __FUNCTION__,
903 level, d, x, y);
904
905 assert(mt->level[level].slice == NULL);
906
907 mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
908 mt->level[level].slice[0].x_offset = mt->level[level].level_x;
909 mt->level[level].slice[0].y_offset = mt->level[level].level_y;
910 }
911
912
913 void
914 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
915 GLuint level, GLuint img,
916 GLuint x, GLuint y)
917 {
918 if (img == 0 && level == 0)
919 assert(x == 0 && y == 0);
920
921 assert(img < mt->level[level].depth);
922
923 mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
924 mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
925
926 DBG("%s level %d img %d pos %d,%d\n",
927 __FUNCTION__, level, img,
928 mt->level[level].slice[img].x_offset,
929 mt->level[level].slice[img].y_offset);
930 }
931
932 void
933 intel_miptree_get_image_offset(const struct intel_mipmap_tree *mt,
934 GLuint level, GLuint slice,
935 GLuint *x, GLuint *y)
936 {
937 assert(slice < mt->level[level].depth);
938
939 *x = mt->level[level].slice[slice].x_offset;
940 *y = mt->level[level].slice[slice].y_offset;
941 }
942
943 /**
944 * Rendering with tiled buffers requires that the base address of the buffer
945 * be aligned to a page boundary. For renderbuffers, and sometimes with
946 * textures, we may want the surface to point at a texture image level that
947 * isn't at a page boundary.
948 *
949 * This function returns an appropriately-aligned base offset
950 * according to the tiling restrictions, plus any required x/y offset
951 * from there.
952 */
953 uint32_t
954 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree *mt,
955 GLuint level, GLuint slice,
956 uint32_t *tile_x,
957 uint32_t *tile_y)
958 {
959 const struct intel_region *region = mt->region;
960 uint32_t x, y;
961 uint32_t mask_x, mask_y;
962
963 intel_region_get_tile_masks(region, &mask_x, &mask_y, false);
964 intel_miptree_get_image_offset(mt, level, slice, &x, &y);
965
966 *tile_x = x & mask_x;
967 *tile_y = y & mask_y;
968
969 return intel_region_get_aligned_offset(region, x & ~mask_x, y & ~mask_y,
970 false);
971 }
972
973 static void
974 intel_miptree_copy_slice_sw(struct brw_context *brw,
975 struct intel_mipmap_tree *dst_mt,
976 struct intel_mipmap_tree *src_mt,
977 int level,
978 int slice,
979 int width,
980 int height)
981 {
982 void *src, *dst;
983 int src_stride, dst_stride;
984 int cpp = dst_mt->cpp;
985
986 intel_miptree_map(brw, src_mt,
987 level, slice,
988 0, 0,
989 width, height,
990 GL_MAP_READ_BIT | BRW_MAP_DIRECT_BIT,
991 &src, &src_stride);
992
993 intel_miptree_map(brw, dst_mt,
994 level, slice,
995 0, 0,
996 width, height,
997 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
998 BRW_MAP_DIRECT_BIT,
999 &dst, &dst_stride);
1000
1001 DBG("sw blit %s mt %p %p/%d -> %s mt %p %p/%d (%dx%d)\n",
1002 _mesa_get_format_name(src_mt->format),
1003 src_mt, src, src_stride,
1004 _mesa_get_format_name(dst_mt->format),
1005 dst_mt, dst, dst_stride,
1006 width, height);
1007
1008 int row_size = cpp * width;
1009 if (src_stride == row_size &&
1010 dst_stride == row_size) {
1011 memcpy(dst, src, row_size * height);
1012 } else {
1013 for (int i = 0; i < height; i++) {
1014 memcpy(dst, src, row_size);
1015 dst += dst_stride;
1016 src += src_stride;
1017 }
1018 }
1019
1020 intel_miptree_unmap(brw, dst_mt, level, slice);
1021 intel_miptree_unmap(brw, src_mt, level, slice);
1022
1023 /* Don't forget to copy the stencil data over, too. We could have skipped
1024 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1025 * shuffling the two data sources in/out of temporary storage instead of
1026 * the direct mapping we get this way.
1027 */
1028 if (dst_mt->stencil_mt) {
1029 assert(src_mt->stencil_mt);
1030 intel_miptree_copy_slice_sw(brw, dst_mt->stencil_mt, src_mt->stencil_mt,
1031 level, slice, width, height);
1032 }
1033 }
1034
1035 static void
1036 intel_miptree_copy_slice(struct brw_context *brw,
1037 struct intel_mipmap_tree *dst_mt,
1038 struct intel_mipmap_tree *src_mt,
1039 int level,
1040 int face,
1041 int depth)
1042
1043 {
1044 mesa_format format = src_mt->format;
1045 uint32_t width = minify(src_mt->physical_width0, level - src_mt->first_level);
1046 uint32_t height = minify(src_mt->physical_height0, level - src_mt->first_level);
1047 int slice;
1048
1049 if (face > 0)
1050 slice = face;
1051 else
1052 slice = depth;
1053
1054 assert(depth < src_mt->level[level].depth);
1055 assert(src_mt->format == dst_mt->format);
1056
1057 if (dst_mt->compressed) {
1058 height = ALIGN(height, dst_mt->align_h) / dst_mt->align_h;
1059 width = ALIGN(width, dst_mt->align_w);
1060 }
1061
1062 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1063 * below won't apply since we can't do the depth's Y tiling or the
1064 * stencil's W tiling in the blitter.
1065 */
1066 if (src_mt->stencil_mt) {
1067 intel_miptree_copy_slice_sw(brw,
1068 dst_mt, src_mt,
1069 level, slice,
1070 width, height);
1071 return;
1072 }
1073
1074 uint32_t dst_x, dst_y, src_x, src_y;
1075 intel_miptree_get_image_offset(dst_mt, level, slice, &dst_x, &dst_y);
1076 intel_miptree_get_image_offset(src_mt, level, slice, &src_x, &src_y);
1077
1078 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1079 _mesa_get_format_name(src_mt->format),
1080 src_mt, src_x, src_y, src_mt->region->pitch,
1081 _mesa_get_format_name(dst_mt->format),
1082 dst_mt, dst_x, dst_y, dst_mt->region->pitch,
1083 width, height);
1084
1085 if (!intel_miptree_blit(brw,
1086 src_mt, level, slice, 0, 0, false,
1087 dst_mt, level, slice, 0, 0, false,
1088 width, height, GL_COPY)) {
1089 perf_debug("miptree validate blit for %s failed\n",
1090 _mesa_get_format_name(format));
1091
1092 intel_miptree_copy_slice_sw(brw, dst_mt, src_mt, level, slice,
1093 width, height);
1094 }
1095 }
1096
1097 /**
1098 * Copies the image's current data to the given miptree, and associates that
1099 * miptree with the image.
1100 *
1101 * If \c invalidate is true, then the actual image data does not need to be
1102 * copied, but the image still needs to be associated to the new miptree (this
1103 * is set to true if we're about to clear the image).
1104 */
1105 void
1106 intel_miptree_copy_teximage(struct brw_context *brw,
1107 struct intel_texture_image *intelImage,
1108 struct intel_mipmap_tree *dst_mt,
1109 bool invalidate)
1110 {
1111 struct intel_mipmap_tree *src_mt = intelImage->mt;
1112 struct intel_texture_object *intel_obj =
1113 intel_texture_object(intelImage->base.Base.TexObject);
1114 int level = intelImage->base.Base.Level;
1115 int face = intelImage->base.Base.Face;
1116 GLuint depth = intelImage->base.Base.Depth;
1117
1118 if (!invalidate) {
1119 for (int slice = 0; slice < depth; slice++) {
1120 intel_miptree_copy_slice(brw, dst_mt, src_mt, level, face, slice);
1121 }
1122 }
1123
1124 intel_miptree_reference(&intelImage->mt, dst_mt);
1125 intel_obj->needs_validate = true;
1126 }
1127
1128 bool
1129 intel_miptree_alloc_mcs(struct brw_context *brw,
1130 struct intel_mipmap_tree *mt,
1131 GLuint num_samples)
1132 {
1133 assert(brw->gen >= 7); /* MCS only used on Gen7+ */
1134 assert(mt->mcs_mt == NULL);
1135
1136 /* Choose the correct format for the MCS buffer. All that really matters
1137 * is that we allocate the right buffer size, since we'll always be
1138 * accessing this miptree using MCS-specific hardware mechanisms, which
1139 * infer the correct format based on num_samples.
1140 */
1141 mesa_format format;
1142 switch (num_samples) {
1143 case 4:
1144 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
1145 * each sample).
1146 */
1147 format = MESA_FORMAT_R_UNORM8;
1148 break;
1149 case 8:
1150 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
1151 * for each sample, plus 8 padding bits).
1152 */
1153 format = MESA_FORMAT_R_UINT32;
1154 break;
1155 default:
1156 assert(!"Unrecognized sample count in intel_miptree_alloc_mcs");
1157 return false;
1158 };
1159
1160 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
1161 *
1162 * "The MCS surface must be stored as Tile Y."
1163 */
1164 mt->mcs_mt = intel_miptree_create(brw,
1165 mt->target,
1166 format,
1167 mt->first_level,
1168 mt->last_level,
1169 mt->logical_width0,
1170 mt->logical_height0,
1171 mt->logical_depth0,
1172 true,
1173 0 /* num_samples */,
1174 INTEL_MIPTREE_TILING_Y);
1175
1176 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1177 *
1178 * When MCS buffer is enabled and bound to MSRT, it is required that it
1179 * is cleared prior to any rendering.
1180 *
1181 * Since we don't use the MCS buffer for any purpose other than rendering,
1182 * it makes sense to just clear it immediately upon allocation.
1183 *
1184 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1185 */
1186 void *data = intel_miptree_map_raw(brw, mt->mcs_mt);
1187 memset(data, 0xff, mt->mcs_mt->region->bo->size);
1188 intel_miptree_unmap_raw(brw, mt->mcs_mt);
1189 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
1190
1191 return mt->mcs_mt;
1192 }
1193
1194
1195 bool
1196 intel_miptree_alloc_non_msrt_mcs(struct brw_context *brw,
1197 struct intel_mipmap_tree *mt)
1198 {
1199 assert(mt->mcs_mt == NULL);
1200
1201 /* The format of the MCS buffer is opaque to the driver; all that matters
1202 * is that we get its size and pitch right. We'll pretend that the format
1203 * is R32. Since an MCS tile covers 128 blocks horizontally, and a Y-tiled
1204 * R32 buffer is 32 pixels across, we'll need to scale the width down by
1205 * the block width and then a further factor of 4. Since an MCS tile
1206 * covers 256 blocks vertically, and a Y-tiled R32 buffer is 32 rows high,
1207 * we'll need to scale the height down by the block height and then a
1208 * further factor of 8.
1209 */
1210 const mesa_format format = MESA_FORMAT_R_UINT32;
1211 unsigned block_width_px;
1212 unsigned block_height;
1213 intel_get_non_msrt_mcs_alignment(brw, mt, &block_width_px, &block_height);
1214 unsigned width_divisor = block_width_px * 4;
1215 unsigned height_divisor = block_height * 8;
1216 unsigned mcs_width =
1217 ALIGN(mt->logical_width0, width_divisor) / width_divisor;
1218 unsigned mcs_height =
1219 ALIGN(mt->logical_height0, height_divisor) / height_divisor;
1220 assert(mt->logical_depth0 == 1);
1221 mt->mcs_mt = intel_miptree_create(brw,
1222 mt->target,
1223 format,
1224 mt->first_level,
1225 mt->last_level,
1226 mcs_width,
1227 mcs_height,
1228 mt->logical_depth0,
1229 true,
1230 0 /* num_samples */,
1231 INTEL_MIPTREE_TILING_Y);
1232
1233 return mt->mcs_mt;
1234 }
1235
1236
1237 /**
1238 * Helper for intel_miptree_alloc_hiz() that sets
1239 * \c mt->level[level].slice[layer].has_hiz. Return true if and only if
1240 * \c has_hiz was set.
1241 */
1242 static bool
1243 intel_miptree_slice_enable_hiz(struct brw_context *brw,
1244 struct intel_mipmap_tree *mt,
1245 uint32_t level,
1246 uint32_t layer)
1247 {
1248 assert(mt->hiz_mt);
1249
1250 if (brw->gen >= 8 || brw->is_haswell) {
1251 uint32_t width = minify(mt->physical_width0, level);
1252 uint32_t height = minify(mt->physical_height0, level);
1253
1254 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1255 * and the height is 4 aligned. This allows our HiZ support
1256 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1257 * we can grow the width & height to allow the HiZ op to
1258 * force the proper size alignments.
1259 */
1260 if (level > 0 && ((width & 7) || (height & 3))) {
1261 return false;
1262 }
1263 }
1264
1265 mt->level[level].slice[layer].has_hiz = true;
1266 return true;
1267 }
1268
1269
1270
1271 bool
1272 intel_miptree_alloc_hiz(struct brw_context *brw,
1273 struct intel_mipmap_tree *mt)
1274 {
1275 assert(mt->hiz_mt == NULL);
1276 mt->hiz_mt = intel_miptree_create(brw,
1277 mt->target,
1278 mt->format,
1279 mt->first_level,
1280 mt->last_level,
1281 mt->logical_width0,
1282 mt->logical_height0,
1283 mt->logical_depth0,
1284 true,
1285 mt->num_samples,
1286 INTEL_MIPTREE_TILING_ANY);
1287
1288 if (!mt->hiz_mt)
1289 return false;
1290
1291 /* Mark that all slices need a HiZ resolve. */
1292 struct intel_resolve_map *head = &mt->hiz_map;
1293 for (int level = mt->first_level; level <= mt->last_level; ++level) {
1294 for (int layer = 0; layer < mt->level[level].depth; ++layer) {
1295 if (!intel_miptree_slice_enable_hiz(brw, mt, level, layer))
1296 continue;
1297
1298 head->next = malloc(sizeof(*head->next));
1299 head->next->prev = head;
1300 head->next->next = NULL;
1301 head = head->next;
1302
1303 head->level = level;
1304 head->layer = layer;
1305 head->need = GEN6_HIZ_OP_HIZ_RESOLVE;
1306 }
1307 }
1308
1309 return true;
1310 }
1311
1312 /**
1313 * Does the miptree slice have hiz enabled?
1314 */
1315 bool
1316 intel_miptree_slice_has_hiz(struct intel_mipmap_tree *mt,
1317 uint32_t level,
1318 uint32_t layer)
1319 {
1320 intel_miptree_check_level_layer(mt, level, layer);
1321 return mt->level[level].slice[layer].has_hiz;
1322 }
1323
1324 void
1325 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
1326 uint32_t level,
1327 uint32_t layer)
1328 {
1329 if (!intel_miptree_slice_has_hiz(mt, level, layer))
1330 return;
1331
1332 intel_resolve_map_set(&mt->hiz_map,
1333 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
1334 }
1335
1336
1337 void
1338 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
1339 uint32_t level,
1340 uint32_t layer)
1341 {
1342 if (!intel_miptree_slice_has_hiz(mt, level, layer))
1343 return;
1344
1345 intel_resolve_map_set(&mt->hiz_map,
1346 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
1347 }
1348
1349 void
1350 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree *mt,
1351 uint32_t level)
1352 {
1353 uint32_t layer;
1354 uint32_t end_layer = mt->level[level].depth;
1355
1356 for (layer = 0; layer < end_layer; layer++) {
1357 intel_miptree_slice_set_needs_depth_resolve(mt, level, layer);
1358 }
1359 }
1360
1361 static bool
1362 intel_miptree_slice_resolve(struct brw_context *brw,
1363 struct intel_mipmap_tree *mt,
1364 uint32_t level,
1365 uint32_t layer,
1366 enum gen6_hiz_op need)
1367 {
1368 intel_miptree_check_level_layer(mt, level, layer);
1369
1370 struct intel_resolve_map *item =
1371 intel_resolve_map_get(&mt->hiz_map, level, layer);
1372
1373 if (!item || item->need != need)
1374 return false;
1375
1376 intel_hiz_exec(brw, mt, level, layer, need);
1377 intel_resolve_map_remove(item);
1378 return true;
1379 }
1380
1381 bool
1382 intel_miptree_slice_resolve_hiz(struct brw_context *brw,
1383 struct intel_mipmap_tree *mt,
1384 uint32_t level,
1385 uint32_t layer)
1386 {
1387 return intel_miptree_slice_resolve(brw, mt, level, layer,
1388 GEN6_HIZ_OP_HIZ_RESOLVE);
1389 }
1390
1391 bool
1392 intel_miptree_slice_resolve_depth(struct brw_context *brw,
1393 struct intel_mipmap_tree *mt,
1394 uint32_t level,
1395 uint32_t layer)
1396 {
1397 return intel_miptree_slice_resolve(brw, mt, level, layer,
1398 GEN6_HIZ_OP_DEPTH_RESOLVE);
1399 }
1400
1401 static bool
1402 intel_miptree_all_slices_resolve(struct brw_context *brw,
1403 struct intel_mipmap_tree *mt,
1404 enum gen6_hiz_op need)
1405 {
1406 bool did_resolve = false;
1407 struct intel_resolve_map *i, *next;
1408
1409 for (i = mt->hiz_map.next; i; i = next) {
1410 next = i->next;
1411 if (i->need != need)
1412 continue;
1413
1414 intel_hiz_exec(brw, mt, i->level, i->layer, need);
1415 intel_resolve_map_remove(i);
1416 did_resolve = true;
1417 }
1418
1419 return did_resolve;
1420 }
1421
1422 bool
1423 intel_miptree_all_slices_resolve_hiz(struct brw_context *brw,
1424 struct intel_mipmap_tree *mt)
1425 {
1426 return intel_miptree_all_slices_resolve(brw, mt,
1427 GEN6_HIZ_OP_HIZ_RESOLVE);
1428 }
1429
1430 bool
1431 intel_miptree_all_slices_resolve_depth(struct brw_context *brw,
1432 struct intel_mipmap_tree *mt)
1433 {
1434 return intel_miptree_all_slices_resolve(brw, mt,
1435 GEN6_HIZ_OP_DEPTH_RESOLVE);
1436 }
1437
1438
1439 void
1440 intel_miptree_resolve_color(struct brw_context *brw,
1441 struct intel_mipmap_tree *mt)
1442 {
1443 switch (mt->fast_clear_state) {
1444 case INTEL_FAST_CLEAR_STATE_NO_MCS:
1445 case INTEL_FAST_CLEAR_STATE_RESOLVED:
1446 /* No resolve needed */
1447 break;
1448 case INTEL_FAST_CLEAR_STATE_UNRESOLVED:
1449 case INTEL_FAST_CLEAR_STATE_CLEAR:
1450 /* Fast color clear resolves only make sense for non-MSAA buffers. */
1451 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE)
1452 brw_blorp_resolve_color(brw, mt);
1453 break;
1454 }
1455 }
1456
1457
1458 /**
1459 * Make it possible to share the region backing the given miptree with another
1460 * process or another miptree.
1461 *
1462 * Fast color clears are unsafe with shared buffers, so we need to resolve and
1463 * then discard the MCS buffer, if present. We also set the fast_clear_state
1464 * to INTEL_FAST_CLEAR_STATE_NO_MCS to ensure that no MCS buffer gets
1465 * allocated in the future.
1466 */
1467 void
1468 intel_miptree_make_shareable(struct brw_context *brw,
1469 struct intel_mipmap_tree *mt)
1470 {
1471 /* MCS buffers are also used for multisample buffers, but we can't resolve
1472 * away a multisample MCS buffer because it's an integral part of how the
1473 * pixel data is stored. Fortunately this code path should never be
1474 * reached for multisample buffers.
1475 */
1476 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE);
1477
1478 if (mt->mcs_mt) {
1479 intel_miptree_resolve_color(brw, mt);
1480 intel_miptree_release(&mt->mcs_mt);
1481 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
1482 }
1483 }
1484
1485
1486 /**
1487 * \brief Get pointer offset into stencil buffer.
1488 *
1489 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
1490 * must decode the tile's layout in software.
1491 *
1492 * See
1493 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
1494 * Format.
1495 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
1496 *
1497 * Even though the returned offset is always positive, the return type is
1498 * signed due to
1499 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
1500 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
1501 */
1502 static intptr_t
1503 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
1504 {
1505 uint32_t tile_size = 4096;
1506 uint32_t tile_width = 64;
1507 uint32_t tile_height = 64;
1508 uint32_t row_size = 64 * stride;
1509
1510 uint32_t tile_x = x / tile_width;
1511 uint32_t tile_y = y / tile_height;
1512
1513 /* The byte's address relative to the tile's base addres. */
1514 uint32_t byte_x = x % tile_width;
1515 uint32_t byte_y = y % tile_height;
1516
1517 uintptr_t u = tile_y * row_size
1518 + tile_x * tile_size
1519 + 512 * (byte_x / 8)
1520 + 64 * (byte_y / 8)
1521 + 32 * ((byte_y / 4) % 2)
1522 + 16 * ((byte_x / 4) % 2)
1523 + 8 * ((byte_y / 2) % 2)
1524 + 4 * ((byte_x / 2) % 2)
1525 + 2 * (byte_y % 2)
1526 + 1 * (byte_x % 2);
1527
1528 if (swizzled) {
1529 /* adjust for bit6 swizzling */
1530 if (((byte_x / 8) % 2) == 1) {
1531 if (((byte_y / 8) % 2) == 0) {
1532 u += 64;
1533 } else {
1534 u -= 64;
1535 }
1536 }
1537 }
1538
1539 return u;
1540 }
1541
1542 void
1543 intel_miptree_updownsample(struct brw_context *brw,
1544 struct intel_mipmap_tree *src,
1545 struct intel_mipmap_tree *dst)
1546 {
1547 brw_blorp_blit_miptrees(brw,
1548 src, 0 /* level */, 0 /* layer */,
1549 dst, 0 /* level */, 0 /* layer */,
1550 0, 0,
1551 src->logical_width0, src->logical_height0,
1552 0, 0,
1553 dst->logical_width0, dst->logical_height0,
1554 GL_NEAREST, false, false /*mirror x, y*/);
1555
1556 if (src->stencil_mt) {
1557 brw_blorp_blit_miptrees(brw,
1558 src->stencil_mt, 0 /* level */, 0 /* layer */,
1559 dst->stencil_mt, 0 /* level */, 0 /* layer */,
1560 0, 0,
1561 src->logical_width0, src->logical_height0,
1562 0, 0,
1563 dst->logical_width0, dst->logical_height0,
1564 GL_NEAREST, false, false /*mirror x, y*/);
1565 }
1566 }
1567
1568 void *
1569 intel_miptree_map_raw(struct brw_context *brw, struct intel_mipmap_tree *mt)
1570 {
1571 /* CPU accesses to color buffers don't understand fast color clears, so
1572 * resolve any pending fast color clears before we map.
1573 */
1574 intel_miptree_resolve_color(brw, mt);
1575
1576 drm_intel_bo *bo = mt->region->bo;
1577
1578 intel_batchbuffer_flush(brw);
1579
1580 if (mt->region->tiling != I915_TILING_NONE)
1581 brw_bo_map_gtt(brw, bo, "miptree");
1582 else
1583 brw_bo_map(brw, bo, true, "miptree");
1584
1585 return bo->virtual;
1586 }
1587
1588 void
1589 intel_miptree_unmap_raw(struct brw_context *brw,
1590 struct intel_mipmap_tree *mt)
1591 {
1592 drm_intel_bo_unmap(mt->region->bo);
1593 }
1594
1595 static void
1596 intel_miptree_map_gtt(struct brw_context *brw,
1597 struct intel_mipmap_tree *mt,
1598 struct intel_miptree_map *map,
1599 unsigned int level, unsigned int slice)
1600 {
1601 unsigned int bw, bh;
1602 void *base;
1603 unsigned int image_x, image_y;
1604 int x = map->x;
1605 int y = map->y;
1606
1607 /* For compressed formats, the stride is the number of bytes per
1608 * row of blocks. intel_miptree_get_image_offset() already does
1609 * the divide.
1610 */
1611 _mesa_get_format_block_size(mt->format, &bw, &bh);
1612 assert(y % bh == 0);
1613 y /= bh;
1614
1615 base = intel_miptree_map_raw(brw, mt) + mt->offset;
1616
1617 if (base == NULL)
1618 map->ptr = NULL;
1619 else {
1620 /* Note that in the case of cube maps, the caller must have passed the
1621 * slice number referencing the face.
1622 */
1623 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1624 x += image_x;
1625 y += image_y;
1626
1627 map->stride = mt->region->pitch;
1628 map->ptr = base + y * map->stride + x * mt->cpp;
1629 }
1630
1631 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1632 map->x, map->y, map->w, map->h,
1633 mt, _mesa_get_format_name(mt->format),
1634 x, y, map->ptr, map->stride);
1635 }
1636
1637 static void
1638 intel_miptree_unmap_gtt(struct brw_context *brw,
1639 struct intel_mipmap_tree *mt,
1640 struct intel_miptree_map *map,
1641 unsigned int level,
1642 unsigned int slice)
1643 {
1644 intel_miptree_unmap_raw(brw, mt);
1645 }
1646
1647 static void
1648 intel_miptree_map_blit(struct brw_context *brw,
1649 struct intel_mipmap_tree *mt,
1650 struct intel_miptree_map *map,
1651 unsigned int level, unsigned int slice)
1652 {
1653 map->mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
1654 0, 0,
1655 map->w, map->h, 1,
1656 false, 0,
1657 INTEL_MIPTREE_TILING_NONE);
1658 if (!map->mt) {
1659 fprintf(stderr, "Failed to allocate blit temporary\n");
1660 goto fail;
1661 }
1662 map->stride = map->mt->region->pitch;
1663
1664 if (!intel_miptree_blit(brw,
1665 mt, level, slice,
1666 map->x, map->y, false,
1667 map->mt, 0, 0,
1668 0, 0, false,
1669 map->w, map->h, GL_COPY)) {
1670 fprintf(stderr, "Failed to blit\n");
1671 goto fail;
1672 }
1673
1674 map->ptr = intel_miptree_map_raw(brw, map->mt);
1675
1676 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1677 map->x, map->y, map->w, map->h,
1678 mt, _mesa_get_format_name(mt->format),
1679 level, slice, map->ptr, map->stride);
1680
1681 return;
1682
1683 fail:
1684 intel_miptree_release(&map->mt);
1685 map->ptr = NULL;
1686 map->stride = 0;
1687 }
1688
1689 static void
1690 intel_miptree_unmap_blit(struct brw_context *brw,
1691 struct intel_mipmap_tree *mt,
1692 struct intel_miptree_map *map,
1693 unsigned int level,
1694 unsigned int slice)
1695 {
1696 struct gl_context *ctx = &brw->ctx;
1697
1698 intel_miptree_unmap_raw(brw, map->mt);
1699
1700 if (map->mode & GL_MAP_WRITE_BIT) {
1701 bool ok = intel_miptree_blit(brw,
1702 map->mt, 0, 0,
1703 0, 0, false,
1704 mt, level, slice,
1705 map->x, map->y, false,
1706 map->w, map->h, GL_COPY);
1707 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
1708 }
1709
1710 intel_miptree_release(&map->mt);
1711 }
1712
1713 #ifdef __SSE4_1__
1714 /**
1715 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
1716 */
1717 static void
1718 intel_miptree_map_movntdqa(struct brw_context *brw,
1719 struct intel_mipmap_tree *mt,
1720 struct intel_miptree_map *map,
1721 unsigned int level, unsigned int slice)
1722 {
1723 assert(map->mode & GL_MAP_READ_BIT);
1724 assert(!(map->mode & GL_MAP_WRITE_BIT));
1725
1726 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1727 map->x, map->y, map->w, map->h,
1728 mt, _mesa_get_format_name(mt->format),
1729 level, slice, map->ptr, map->stride);
1730
1731 /* Map the original image */
1732 uint32_t image_x;
1733 uint32_t image_y;
1734 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1735 image_x += map->x;
1736 image_y += map->y;
1737
1738 void *src = intel_miptree_map_raw(brw, mt);
1739 if (!src)
1740 return;
1741 src += image_y * mt->region->pitch;
1742 src += image_x * mt->region->cpp;
1743
1744 /* Due to the pixel offsets for the particular image being mapped, our
1745 * src pointer may not be 16-byte aligned. However, if the pitch is
1746 * divisible by 16, then the amount by which it's misaligned will remain
1747 * consistent from row to row.
1748 */
1749 assert((mt->region->pitch % 16) == 0);
1750 const int misalignment = ((uintptr_t) src) & 15;
1751
1752 /* Create an untiled temporary buffer for the mapping. */
1753 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
1754
1755 map->stride = ALIGN(misalignment + width_bytes, 16);
1756
1757 map->buffer = malloc(map->stride * map->h);
1758 /* Offset the destination so it has the same misalignment as src. */
1759 map->ptr = map->buffer + misalignment;
1760
1761 assert((((uintptr_t) map->ptr) & 15) == misalignment);
1762
1763 for (uint32_t y = 0; y < map->h; y++) {
1764 void *dst_ptr = map->ptr + y * map->stride;
1765 void *src_ptr = src + y * mt->region->pitch;
1766
1767 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
1768 }
1769
1770 intel_miptree_unmap_raw(brw, mt);
1771 }
1772
1773 static void
1774 intel_miptree_unmap_movntdqa(struct brw_context *brw,
1775 struct intel_mipmap_tree *mt,
1776 struct intel_miptree_map *map,
1777 unsigned int level,
1778 unsigned int slice)
1779 {
1780 free(map->buffer);
1781 map->buffer = NULL;
1782 map->ptr = NULL;
1783 }
1784 #endif
1785
1786 static void
1787 intel_miptree_map_s8(struct brw_context *brw,
1788 struct intel_mipmap_tree *mt,
1789 struct intel_miptree_map *map,
1790 unsigned int level, unsigned int slice)
1791 {
1792 map->stride = map->w;
1793 map->buffer = map->ptr = malloc(map->stride * map->h);
1794 if (!map->buffer)
1795 return;
1796
1797 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
1798 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
1799 * invalidate is set, since we'll be writing the whole rectangle from our
1800 * temporary buffer back out.
1801 */
1802 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1803 uint8_t *untiled_s8_map = map->ptr;
1804 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
1805 unsigned int image_x, image_y;
1806
1807 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1808
1809 for (uint32_t y = 0; y < map->h; y++) {
1810 for (uint32_t x = 0; x < map->w; x++) {
1811 ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
1812 x + image_x + map->x,
1813 y + image_y + map->y,
1814 brw->has_swizzling);
1815 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
1816 }
1817 }
1818
1819 intel_miptree_unmap_raw(brw, mt);
1820
1821 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__,
1822 map->x, map->y, map->w, map->h,
1823 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
1824 } else {
1825 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1826 map->x, map->y, map->w, map->h,
1827 mt, map->ptr, map->stride);
1828 }
1829 }
1830
1831 static void
1832 intel_miptree_unmap_s8(struct brw_context *brw,
1833 struct intel_mipmap_tree *mt,
1834 struct intel_miptree_map *map,
1835 unsigned int level,
1836 unsigned int slice)
1837 {
1838 if (map->mode & GL_MAP_WRITE_BIT) {
1839 unsigned int image_x, image_y;
1840 uint8_t *untiled_s8_map = map->ptr;
1841 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
1842
1843 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1844
1845 for (uint32_t y = 0; y < map->h; y++) {
1846 for (uint32_t x = 0; x < map->w; x++) {
1847 ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
1848 x + map->x,
1849 y + map->y,
1850 brw->has_swizzling);
1851 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
1852 }
1853 }
1854
1855 intel_miptree_unmap_raw(brw, mt);
1856 }
1857
1858 free(map->buffer);
1859 }
1860
1861 static void
1862 intel_miptree_map_etc(struct brw_context *brw,
1863 struct intel_mipmap_tree *mt,
1864 struct intel_miptree_map *map,
1865 unsigned int level,
1866 unsigned int slice)
1867 {
1868 assert(mt->etc_format != MESA_FORMAT_NONE);
1869 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
1870 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
1871 }
1872
1873 assert(map->mode & GL_MAP_WRITE_BIT);
1874 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
1875
1876 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
1877 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
1878 map->w, map->h, 1));
1879 map->ptr = map->buffer;
1880 }
1881
1882 static void
1883 intel_miptree_unmap_etc(struct brw_context *brw,
1884 struct intel_mipmap_tree *mt,
1885 struct intel_miptree_map *map,
1886 unsigned int level,
1887 unsigned int slice)
1888 {
1889 uint32_t image_x;
1890 uint32_t image_y;
1891 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1892
1893 image_x += map->x;
1894 image_y += map->y;
1895
1896 uint8_t *dst = intel_miptree_map_raw(brw, mt)
1897 + image_y * mt->region->pitch
1898 + image_x * mt->region->cpp;
1899
1900 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
1901 _mesa_etc1_unpack_rgba8888(dst, mt->region->pitch,
1902 map->ptr, map->stride,
1903 map->w, map->h);
1904 else
1905 _mesa_unpack_etc2_format(dst, mt->region->pitch,
1906 map->ptr, map->stride,
1907 map->w, map->h, mt->etc_format);
1908
1909 intel_miptree_unmap_raw(brw, mt);
1910 free(map->buffer);
1911 }
1912
1913 /**
1914 * Mapping function for packed depth/stencil miptrees backed by real separate
1915 * miptrees for depth and stencil.
1916 *
1917 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
1918 * separate from the depth buffer. Yet at the GL API level, we have to expose
1919 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
1920 * be able to map that memory for texture storage and glReadPixels-type
1921 * operations. We give Mesa core that access by mallocing a temporary and
1922 * copying the data between the actual backing store and the temporary.
1923 */
1924 static void
1925 intel_miptree_map_depthstencil(struct brw_context *brw,
1926 struct intel_mipmap_tree *mt,
1927 struct intel_miptree_map *map,
1928 unsigned int level, unsigned int slice)
1929 {
1930 struct intel_mipmap_tree *z_mt = mt;
1931 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
1932 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
1933 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
1934
1935 map->stride = map->w * packed_bpp;
1936 map->buffer = map->ptr = malloc(map->stride * map->h);
1937 if (!map->buffer)
1938 return;
1939
1940 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
1941 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
1942 * invalidate is set, since we'll be writing the whole rectangle from our
1943 * temporary buffer back out.
1944 */
1945 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1946 uint32_t *packed_map = map->ptr;
1947 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
1948 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
1949 unsigned int s_image_x, s_image_y;
1950 unsigned int z_image_x, z_image_y;
1951
1952 intel_miptree_get_image_offset(s_mt, level, slice,
1953 &s_image_x, &s_image_y);
1954 intel_miptree_get_image_offset(z_mt, level, slice,
1955 &z_image_x, &z_image_y);
1956
1957 for (uint32_t y = 0; y < map->h; y++) {
1958 for (uint32_t x = 0; x < map->w; x++) {
1959 int map_x = map->x + x, map_y = map->y + y;
1960 ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
1961 map_x + s_image_x,
1962 map_y + s_image_y,
1963 brw->has_swizzling);
1964 ptrdiff_t z_offset = ((map_y + z_image_y) *
1965 (z_mt->region->pitch / 4) +
1966 (map_x + z_image_x));
1967 uint8_t s = s_map[s_offset];
1968 uint32_t z = z_map[z_offset];
1969
1970 if (map_z32f_x24s8) {
1971 packed_map[(y * map->w + x) * 2 + 0] = z;
1972 packed_map[(y * map->w + x) * 2 + 1] = s;
1973 } else {
1974 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
1975 }
1976 }
1977 }
1978
1979 intel_miptree_unmap_raw(brw, s_mt);
1980 intel_miptree_unmap_raw(brw, z_mt);
1981
1982 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
1983 __FUNCTION__,
1984 map->x, map->y, map->w, map->h,
1985 z_mt, map->x + z_image_x, map->y + z_image_y,
1986 s_mt, map->x + s_image_x, map->y + s_image_y,
1987 map->ptr, map->stride);
1988 } else {
1989 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1990 map->x, map->y, map->w, map->h,
1991 mt, map->ptr, map->stride);
1992 }
1993 }
1994
1995 static void
1996 intel_miptree_unmap_depthstencil(struct brw_context *brw,
1997 struct intel_mipmap_tree *mt,
1998 struct intel_miptree_map *map,
1999 unsigned int level,
2000 unsigned int slice)
2001 {
2002 struct intel_mipmap_tree *z_mt = mt;
2003 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2004 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2005
2006 if (map->mode & GL_MAP_WRITE_BIT) {
2007 uint32_t *packed_map = map->ptr;
2008 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2009 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2010 unsigned int s_image_x, s_image_y;
2011 unsigned int z_image_x, z_image_y;
2012
2013 intel_miptree_get_image_offset(s_mt, level, slice,
2014 &s_image_x, &s_image_y);
2015 intel_miptree_get_image_offset(z_mt, level, slice,
2016 &z_image_x, &z_image_y);
2017
2018 for (uint32_t y = 0; y < map->h; y++) {
2019 for (uint32_t x = 0; x < map->w; x++) {
2020 ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
2021 x + s_image_x + map->x,
2022 y + s_image_y + map->y,
2023 brw->has_swizzling);
2024 ptrdiff_t z_offset = ((y + z_image_y) *
2025 (z_mt->region->pitch / 4) +
2026 (x + z_image_x));
2027
2028 if (map_z32f_x24s8) {
2029 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
2030 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
2031 } else {
2032 uint32_t packed = packed_map[y * map->w + x];
2033 s_map[s_offset] = packed >> 24;
2034 z_map[z_offset] = packed;
2035 }
2036 }
2037 }
2038
2039 intel_miptree_unmap_raw(brw, s_mt);
2040 intel_miptree_unmap_raw(brw, z_mt);
2041
2042 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
2043 __FUNCTION__,
2044 map->x, map->y, map->w, map->h,
2045 z_mt, _mesa_get_format_name(z_mt->format),
2046 map->x + z_image_x, map->y + z_image_y,
2047 s_mt, map->x + s_image_x, map->y + s_image_y,
2048 map->ptr, map->stride);
2049 }
2050
2051 free(map->buffer);
2052 }
2053
2054 /**
2055 * Create and attach a map to the miptree at (level, slice). Return the
2056 * attached map.
2057 */
2058 static struct intel_miptree_map*
2059 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
2060 unsigned int level,
2061 unsigned int slice,
2062 unsigned int x,
2063 unsigned int y,
2064 unsigned int w,
2065 unsigned int h,
2066 GLbitfield mode)
2067 {
2068 struct intel_miptree_map *map = calloc(1, sizeof(*map));
2069
2070 if (!map)
2071 return NULL;
2072
2073 assert(mt->level[level].slice[slice].map == NULL);
2074 mt->level[level].slice[slice].map = map;
2075
2076 map->mode = mode;
2077 map->x = x;
2078 map->y = y;
2079 map->w = w;
2080 map->h = h;
2081
2082 return map;
2083 }
2084
2085 /**
2086 * Release the map at (level, slice).
2087 */
2088 static void
2089 intel_miptree_release_map(struct intel_mipmap_tree *mt,
2090 unsigned int level,
2091 unsigned int slice)
2092 {
2093 struct intel_miptree_map **map;
2094
2095 map = &mt->level[level].slice[slice].map;
2096 free(*map);
2097 *map = NULL;
2098 }
2099
2100 static bool
2101 can_blit_slice(struct intel_mipmap_tree *mt,
2102 unsigned int level, unsigned int slice)
2103 {
2104 uint32_t image_x;
2105 uint32_t image_y;
2106 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2107 if (image_x >= 32768 || image_y >= 32768)
2108 return false;
2109
2110 if (mt->region->pitch >= 32768)
2111 return false;
2112
2113 return true;
2114 }
2115
2116 void
2117 intel_miptree_map(struct brw_context *brw,
2118 struct intel_mipmap_tree *mt,
2119 unsigned int level,
2120 unsigned int slice,
2121 unsigned int x,
2122 unsigned int y,
2123 unsigned int w,
2124 unsigned int h,
2125 GLbitfield mode,
2126 void **out_ptr,
2127 int *out_stride)
2128 {
2129 struct intel_miptree_map *map;
2130
2131 assert(mt->num_samples <= 1);
2132
2133 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
2134 if (!map){
2135 *out_ptr = NULL;
2136 *out_stride = 0;
2137 return;
2138 }
2139
2140 intel_miptree_slice_resolve_depth(brw, mt, level, slice);
2141 if (map->mode & GL_MAP_WRITE_BIT) {
2142 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
2143 }
2144
2145 if (mt->format == MESA_FORMAT_S_UINT8) {
2146 intel_miptree_map_s8(brw, mt, map, level, slice);
2147 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2148 !(mode & BRW_MAP_DIRECT_BIT)) {
2149 intel_miptree_map_etc(brw, mt, map, level, slice);
2150 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
2151 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
2152 }
2153 /* See intel_miptree_blit() for details on the 32k pitch limit. */
2154 else if (brw->has_llc &&
2155 !(mode & GL_MAP_WRITE_BIT) &&
2156 !mt->compressed &&
2157 (mt->region->tiling == I915_TILING_X ||
2158 (brw->gen >= 6 && mt->region->tiling == I915_TILING_Y)) &&
2159 can_blit_slice(mt, level, slice)) {
2160 intel_miptree_map_blit(brw, mt, map, level, slice);
2161 } else if (mt->region->tiling != I915_TILING_NONE &&
2162 mt->region->bo->size >= brw->max_gtt_map_object_size) {
2163 assert(can_blit_slice(mt, level, slice));
2164 intel_miptree_map_blit(brw, mt, map, level, slice);
2165 #ifdef __SSE4_1__
2166 } else if (!(mode & GL_MAP_WRITE_BIT) && !mt->compressed) {
2167 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
2168 #endif
2169 } else {
2170 intel_miptree_map_gtt(brw, mt, map, level, slice);
2171 }
2172
2173 *out_ptr = map->ptr;
2174 *out_stride = map->stride;
2175
2176 if (map->ptr == NULL)
2177 intel_miptree_release_map(mt, level, slice);
2178 }
2179
2180 void
2181 intel_miptree_unmap(struct brw_context *brw,
2182 struct intel_mipmap_tree *mt,
2183 unsigned int level,
2184 unsigned int slice)
2185 {
2186 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
2187
2188 assert(mt->num_samples <= 1);
2189
2190 if (!map)
2191 return;
2192
2193 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__,
2194 mt, _mesa_get_format_name(mt->format), level, slice);
2195
2196 if (mt->format == MESA_FORMAT_S_UINT8) {
2197 intel_miptree_unmap_s8(brw, mt, map, level, slice);
2198 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2199 !(map->mode & BRW_MAP_DIRECT_BIT)) {
2200 intel_miptree_unmap_etc(brw, mt, map, level, slice);
2201 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
2202 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
2203 } else if (map->mt) {
2204 intel_miptree_unmap_blit(brw, mt, map, level, slice);
2205 #ifdef __SSE4_1__
2206 } else if (map->buffer) {
2207 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
2208 #endif
2209 } else {
2210 intel_miptree_unmap_gtt(brw, mt, map, level, slice);
2211 }
2212
2213 intel_miptree_release_map(mt, level, slice);
2214 }