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