1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 #include <GL/internal/dri_interface.h>
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_span.h"
38 #include "intel_tex_layout.h"
39 #include "intel_tex.h"
40 #include "intel_blit.h"
43 #include "brw_blorp.h"
46 #include "main/enums.h"
47 #include "main/formats.h"
48 #include "main/glformats.h"
49 #include "main/texcompress_etc.h"
50 #include "main/teximage.h"
52 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
55 target_to_target(GLenum target)
58 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
59 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
60 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
61 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
62 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
63 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
64 return GL_TEXTURE_CUBE_MAP_ARB;
72 * Determine which MSAA layout should be used by the MSAA surface being
73 * created, based on the chip generation and the surface type.
75 static enum intel_msaa_layout
76 compute_msaa_layout(struct intel_context *intel, gl_format format, GLenum target)
78 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
80 return INTEL_MSAA_LAYOUT_IMS;
82 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
83 switch (_mesa_get_format_base_format(format)) {
84 case GL_DEPTH_COMPONENT:
85 case GL_STENCIL_INDEX:
86 case GL_DEPTH_STENCIL:
87 return INTEL_MSAA_LAYOUT_IMS;
89 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
91 * This field must be set to 0 for all SINT MSRTs when all RT channels
94 * In practice this means that we have to disable MCS for all signed
95 * integer MSAA buffers. The alternative, to disable MCS only when one
96 * of the render target channels is disabled, is impractical because it
97 * would require converting between CMS and UMS MSAA layouts on the fly,
100 if (_mesa_get_format_datatype(format) == GL_INT) {
101 /* TODO: is this workaround needed for future chipsets? */
102 assert(intel->gen == 7);
103 return INTEL_MSAA_LAYOUT_UMS;
105 /* For now, if we're going to be texturing from this surface,
106 * force UMS, so that the shader doesn't have to do different things
107 * based on whether there's a multisample control surface needing sampled first.
108 * We can't just blindly read the MCS surface in all cases because:
110 * From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
112 * If this field is disabled and the sampling engine <ld_mcs> message
113 * is issued on this surface, the MCS surface may be accessed. Software
114 * must ensure that the surface is defined to avoid GTT errors.
116 if (target == GL_TEXTURE_2D_MULTISAMPLE ||
117 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
118 return INTEL_MSAA_LAYOUT_UMS;
120 return INTEL_MSAA_LAYOUT_CMS;
128 * @param for_region Indicates that the caller is
129 * intel_miptree_create_for_region(). If true, then do not create
132 struct intel_mipmap_tree *
133 intel_miptree_create_layout(struct intel_context *intel,
144 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
145 int compress_byte = 0;
147 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
148 _mesa_lookup_enum_by_nr(target),
149 _mesa_get_format_name(format),
150 first_level, last_level, mt);
152 if (_mesa_is_format_compressed(format))
153 compress_byte = intel_compressed_num_bytes(format);
155 mt->target = target_to_target(target);
157 mt->first_level = first_level;
158 mt->last_level = last_level;
159 mt->logical_width0 = width0;
160 mt->logical_height0 = height0;
161 mt->logical_depth0 = depth0;
162 mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
163 mt->num_samples = num_samples;
164 mt->compressed = compress_byte ? 1 : 0;
165 mt->msaa_layout = INTEL_MSAA_LAYOUT_NONE;
168 if (num_samples > 1) {
169 /* Adjust width/height/depth for MSAA */
170 mt->msaa_layout = compute_msaa_layout(intel, format, mt->target);
171 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
172 /* In the Sandy Bridge PRM, volume 4, part 1, page 31, it says:
174 * "Any of the other messages (sample*, LOD, load4) used with a
175 * (4x) multisampled surface will in-effect sample a surface with
176 * double the height and width as that indicated in the surface
177 * state. Each pixel position on the original-sized surface is
178 * replaced with a 2x2 of samples with the following arrangement:
183 * Thus, when sampling from a multisampled texture, it behaves as
184 * though the layout in memory for (x,y,sample) is:
186 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
187 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
189 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
190 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
192 * However, the actual layout of multisampled data in memory is:
194 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
195 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
197 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
198 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
200 * This pattern repeats for each 2x2 pixel block.
202 * As a result, when calculating the size of our 4-sample buffer for
203 * an odd width or height, we have to align before scaling up because
204 * sample 3 is in that bottom right 2x2 block.
206 switch (num_samples) {
208 width0 = ALIGN(width0, 2) * 2;
209 height0 = ALIGN(height0, 2) * 2;
212 width0 = ALIGN(width0, 2) * 4;
213 height0 = ALIGN(height0, 2) * 2;
216 /* num_samples should already have been quantized to 0, 1, 4, or
222 /* Non-interleaved */
223 depth0 *= num_samples;
227 /* array_spacing_lod0 is only used for non-IMS MSAA surfaces. TODO: can we
230 switch (mt->msaa_layout) {
231 case INTEL_MSAA_LAYOUT_NONE:
232 case INTEL_MSAA_LAYOUT_IMS:
233 mt->array_spacing_lod0 = false;
235 case INTEL_MSAA_LAYOUT_UMS:
236 case INTEL_MSAA_LAYOUT_CMS:
237 mt->array_spacing_lod0 = true;
241 if (target == GL_TEXTURE_CUBE_MAP) {
246 mt->physical_width0 = width0;
247 mt->physical_height0 = height0;
248 mt->physical_depth0 = depth0;
251 _mesa_get_format_base_format(format) == GL_DEPTH_STENCIL &&
252 (intel->must_use_separate_stencil ||
253 (intel->has_separate_stencil &&
254 intel->vtbl.is_hiz_depth_format(intel, format)))) {
255 mt->stencil_mt = intel_miptree_create(intel,
265 false /* force_y_tiling */);
266 if (!mt->stencil_mt) {
267 intel_miptree_release(&mt);
271 /* Fix up the Z miptree format for how we're splitting out separate
272 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
274 if (mt->format == MESA_FORMAT_S8_Z24) {
275 mt->format = MESA_FORMAT_X8_Z24;
276 } else if (mt->format == MESA_FORMAT_Z32_FLOAT_X24S8) {
277 mt->format = MESA_FORMAT_Z32_FLOAT;
280 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
281 _mesa_get_format_name(mt->format));
285 intel_get_texture_alignment_unit(intel, mt->format,
286 &mt->align_w, &mt->align_h);
291 i945_miptree_layout(mt);
293 i915_miptree_layout(mt);
295 brw_miptree_layout(intel, mt);
302 * \brief Helper function for intel_miptree_create().
305 intel_miptree_choose_tiling(struct intel_context *intel,
308 uint32_t num_samples,
310 struct intel_mipmap_tree *mt)
313 if (format == MESA_FORMAT_S8) {
314 /* The stencil buffer is W tiled. However, we request from the kernel a
315 * non-tiled buffer because the GTT is incapable of W fencing.
317 return I915_TILING_NONE;
321 return I915_TILING_Y;
323 if (num_samples > 1) {
324 /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
327 * [DevSNB+]: For multi-sample render targets, this field must be
328 * 1. MSRTs can only be tiled.
330 * Our usual reason for preferring X tiling (fast blits using the
331 * blitting engine) doesn't apply to MSAA, since we'll generally be
332 * downsampling or upsampling when blitting between the MSAA buffer
333 * and another buffer, and the blitting engine doesn't support that.
334 * So use Y tiling, since it makes better use of the cache.
336 return I915_TILING_Y;
339 GLenum base_format = _mesa_get_format_base_format(format);
340 if (intel->gen >= 4 &&
341 (base_format == GL_DEPTH_COMPONENT ||
342 base_format == GL_DEPTH_STENCIL_EXT))
343 return I915_TILING_Y;
345 /* If the width is smaller than a tile, don't bother tiling. */
347 return I915_TILING_NONE;
349 if (ALIGN(mt->total_width * mt->cpp, 512) >= 32768) {
350 perf_debug("%dx%d miptree too large to blit, falling back to untiled",
351 mt->total_width, mt->total_height);
352 return I915_TILING_NONE;
355 return intel->gen >= 6 ? I915_TILING_Y : I915_TILING_X;
358 struct intel_mipmap_tree *
359 intel_miptree_create(struct intel_context *intel,
367 bool expect_accelerated_upload,
371 struct intel_mipmap_tree *mt;
372 gl_format tex_format = format;
373 gl_format etc_format = MESA_FORMAT_NONE;
374 GLuint total_width, total_height;
377 case MESA_FORMAT_ETC1_RGB8:
378 format = MESA_FORMAT_RGBX8888_REV;
380 case MESA_FORMAT_ETC2_RGB8:
381 format = MESA_FORMAT_RGBX8888_REV;
383 case MESA_FORMAT_ETC2_SRGB8:
384 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
385 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
386 format = MESA_FORMAT_SARGB8;
388 case MESA_FORMAT_ETC2_RGBA8_EAC:
389 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
390 format = MESA_FORMAT_RGBA8888_REV;
392 case MESA_FORMAT_ETC2_R11_EAC:
393 format = MESA_FORMAT_R16;
395 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
396 format = MESA_FORMAT_SIGNED_R16;
398 case MESA_FORMAT_ETC2_RG11_EAC:
399 format = MESA_FORMAT_GR1616;
401 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
402 format = MESA_FORMAT_SIGNED_GR1616;
405 /* Non ETC1 / ETC2 format */
409 etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
411 mt = intel_miptree_create_layout(intel, target, format,
412 first_level, last_level, width0,
416 * pitch == 0 || height == 0 indicates the null texture
418 if (!mt || !mt->total_width || !mt->total_height) {
419 intel_miptree_release(&mt);
423 total_width = mt->total_width;
424 total_height = mt->total_height;
426 if (format == MESA_FORMAT_S8) {
427 /* Align to size of W tile, 64x64. */
428 total_width = ALIGN(total_width, 64);
429 total_height = ALIGN(total_height, 64);
432 uint32_t tiling = intel_miptree_choose_tiling(intel, format, width0,
433 num_samples, force_y_tiling,
435 mt->etc_format = etc_format;
436 mt->region = intel_region_alloc(intel->intelScreen,
441 expect_accelerated_upload);
445 intel_miptree_release(&mt);
452 struct intel_mipmap_tree *
453 intel_miptree_create_for_region(struct intel_context *intel,
456 struct intel_region *region)
458 struct intel_mipmap_tree *mt;
460 mt = intel_miptree_create_layout(intel, target, format,
462 region->width, region->height, 1,
463 true, 0 /* num_samples */);
467 intel_region_reference(&mt->region, region);
474 * For a singlesample DRI2 buffer, this simply wraps the given region with a miptree.
476 * For a multisample DRI2 buffer, this wraps the given region with
477 * a singlesample miptree, then creates a multisample miptree into which the
478 * singlesample miptree is embedded as a child.
480 struct intel_mipmap_tree*
481 intel_miptree_create_for_dri2_buffer(struct intel_context *intel,
482 unsigned dri_attachment,
484 uint32_t num_samples,
485 struct intel_region *region)
487 struct intel_mipmap_tree *singlesample_mt = NULL;
488 struct intel_mipmap_tree *multisample_mt = NULL;
489 GLenum base_format = _mesa_get_format_base_format(format);
491 /* Only the front and back buffers, which are color buffers, are shared
494 assert(dri_attachment == __DRI_BUFFER_BACK_LEFT ||
495 dri_attachment == __DRI_BUFFER_FRONT_LEFT ||
496 dri_attachment == __DRI_BUFFER_FAKE_FRONT_LEFT);
497 assert(base_format == GL_RGB || base_format == GL_RGBA);
499 singlesample_mt = intel_miptree_create_for_region(intel, GL_TEXTURE_2D,
501 if (!singlesample_mt)
504 if (num_samples == 0)
505 return singlesample_mt;
507 multisample_mt = intel_miptree_create_for_renderbuffer(intel,
512 if (!multisample_mt) {
513 intel_miptree_release(&singlesample_mt);
517 multisample_mt->singlesample_mt = singlesample_mt;
518 multisample_mt->need_downsample = false;
520 if (intel->is_front_buffer_rendering &&
521 (dri_attachment == __DRI_BUFFER_FRONT_LEFT ||
522 dri_attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)) {
523 intel_miptree_upsample(intel, multisample_mt);
526 return multisample_mt;
529 struct intel_mipmap_tree*
530 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
534 uint32_t num_samples)
536 struct intel_mipmap_tree *mt;
540 mt = intel_miptree_create(intel, GL_TEXTURE_2D, format, 0, 0,
541 width, height, depth, true, num_samples,
542 false /* force_y_tiling */);
546 if (intel->vtbl.is_hiz_depth_format(intel, format)) {
547 ok = intel_miptree_alloc_hiz(intel, mt, num_samples);
552 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
553 ok = intel_miptree_alloc_mcs(intel, mt, num_samples);
561 intel_miptree_release(&mt);
566 intel_miptree_reference(struct intel_mipmap_tree **dst,
567 struct intel_mipmap_tree *src)
572 intel_miptree_release(dst);
576 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
584 intel_miptree_release(struct intel_mipmap_tree **mt)
589 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
590 if (--(*mt)->refcount <= 0) {
593 DBG("%s deleting %p\n", __FUNCTION__, *mt);
595 intel_region_release(&((*mt)->region));
596 intel_miptree_release(&(*mt)->stencil_mt);
597 intel_miptree_release(&(*mt)->hiz_mt);
598 intel_miptree_release(&(*mt)->mcs_mt);
599 intel_miptree_release(&(*mt)->singlesample_mt);
600 intel_resolve_map_clear(&(*mt)->hiz_map);
602 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
603 free((*mt)->level[i].slice);
612 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
613 int *width, int *height, int *depth)
615 switch (image->TexObject->Target) {
616 case GL_TEXTURE_1D_ARRAY:
617 *width = image->Width;
619 *depth = image->Height;
622 *width = image->Width;
623 *height = image->Height;
624 *depth = image->Depth;
630 * Can the image be pulled into a unified mipmap tree? This mirrors
631 * the completeness test in a lot of ways.
633 * Not sure whether I want to pass gl_texture_image here.
636 intel_miptree_match_image(struct intel_mipmap_tree *mt,
637 struct gl_texture_image *image)
639 struct intel_texture_image *intelImage = intel_texture_image(image);
640 GLuint level = intelImage->base.Base.Level;
641 int width, height, depth;
643 /* glTexImage* choose the texture object based on the target passed in, and
644 * objects can't change targets over their lifetimes, so this should be
647 assert(target_to_target(image->TexObject->Target) == mt->target);
649 gl_format mt_format = mt->format;
650 if (mt->format == MESA_FORMAT_X8_Z24 && mt->stencil_mt)
651 mt_format = MESA_FORMAT_S8_Z24;
652 if (mt->format == MESA_FORMAT_Z32_FLOAT && mt->stencil_mt)
653 mt_format = MESA_FORMAT_Z32_FLOAT_X24S8;
654 if (mt->etc_format != MESA_FORMAT_NONE)
655 mt_format = mt->etc_format;
657 if (image->TexFormat != mt_format)
660 intel_miptree_get_dimensions_for_image(image, &width, &height, &depth);
662 if (mt->target == GL_TEXTURE_CUBE_MAP)
665 /* Test image dimensions against the base level image adjusted for
666 * minification. This will also catch images not present in the
667 * tree, changed targets, etc.
669 if (mt->target == GL_TEXTURE_2D_MULTISAMPLE ||
670 mt->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
671 /* nonzero level here is always bogus */
674 if (width != mt->logical_width0 ||
675 height != mt->logical_height0 ||
676 depth != mt->logical_depth0) {
681 /* all normal textures, renderbuffers, etc */
682 if (width != mt->level[level].width ||
683 height != mt->level[level].height ||
684 depth != mt->level[level].depth) {
689 if (image->NumSamples != mt->num_samples)
697 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
700 GLuint w, GLuint h, GLuint d)
702 mt->level[level].width = w;
703 mt->level[level].height = h;
704 mt->level[level].depth = d;
705 mt->level[level].level_x = x;
706 mt->level[level].level_y = y;
708 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
709 level, w, h, d, x, y);
711 assert(mt->level[level].slice == NULL);
713 mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
714 mt->level[level].slice[0].x_offset = mt->level[level].level_x;
715 mt->level[level].slice[0].y_offset = mt->level[level].level_y;
720 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
721 GLuint level, GLuint img,
724 if (img == 0 && level == 0)
725 assert(x == 0 && y == 0);
727 assert(img < mt->level[level].depth);
729 mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
730 mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
732 DBG("%s level %d img %d pos %d,%d\n",
733 __FUNCTION__, level, img,
734 mt->level[level].slice[img].x_offset,
735 mt->level[level].slice[img].y_offset);
739 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
740 GLuint level, GLuint slice,
741 GLuint *x, GLuint *y)
743 assert(slice < mt->level[level].depth);
745 *x = mt->level[level].slice[slice].x_offset;
746 *y = mt->level[level].slice[slice].y_offset;
750 intel_miptree_get_tile_offsets(struct intel_mipmap_tree *mt,
751 GLuint level, GLuint slice,
755 struct intel_region *region = mt->region;
756 uint32_t mask_x, mask_y;
758 intel_region_get_tile_masks(region, &mask_x, &mask_y, false);
760 *tile_x = mt->level[level].slice[slice].x_offset & mask_x;
761 *tile_y = mt->level[level].slice[slice].y_offset & mask_y;
765 intel_miptree_copy_slice_sw(struct intel_context *intel,
766 struct intel_mipmap_tree *dst_mt,
767 struct intel_mipmap_tree *src_mt,
774 int src_stride, dst_stride;
775 int cpp = dst_mt->cpp;
777 intel_miptree_map(intel, src_mt,
781 GL_MAP_READ_BIT | BRW_MAP_DIRECT_BIT,
784 intel_miptree_map(intel, dst_mt,
788 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
792 DBG("sw blit %s mt %p %p/%d -> %s mt %p %p/%d (%dx%d)\n",
793 _mesa_get_format_name(src_mt->format),
794 src_mt, src, src_stride,
795 _mesa_get_format_name(dst_mt->format),
796 dst_mt, dst, dst_stride,
799 int row_size = cpp * width;
800 if (src_stride == row_size &&
801 dst_stride == row_size) {
802 memcpy(dst, src, row_size * height);
804 for (int i = 0; i < height; i++) {
805 memcpy(dst, src, row_size);
811 intel_miptree_unmap(intel, dst_mt, level, slice);
812 intel_miptree_unmap(intel, src_mt, level, slice);
814 /* Don't forget to copy the stencil data over, too. We could have skipped
815 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
816 * shuffling the two data sources in/out of temporary storage instead of
817 * the direct mapping we get this way.
819 if (dst_mt->stencil_mt) {
820 assert(src_mt->stencil_mt);
821 intel_miptree_copy_slice_sw(intel, dst_mt->stencil_mt, src_mt->stencil_mt,
822 level, slice, width, height);
827 intel_miptree_copy_slice(struct intel_context *intel,
828 struct intel_mipmap_tree *dst_mt,
829 struct intel_mipmap_tree *src_mt,
835 gl_format format = src_mt->format;
836 uint32_t width = src_mt->level[level].width;
837 uint32_t height = src_mt->level[level].height;
845 assert(depth < src_mt->level[level].depth);
846 assert(src_mt->format == dst_mt->format);
848 if (dst_mt->compressed) {
849 height = ALIGN(height, dst_mt->align_h) / dst_mt->align_h;
850 width = ALIGN(width, dst_mt->align_w);
853 /* If it's a packed depth/stencil buffer with separate stencil, the blit
854 * below won't apply since we can't do the depth's Y tiling or the
855 * stencil's W tiling in the blitter.
857 if (src_mt->stencil_mt) {
858 intel_miptree_copy_slice_sw(intel,
865 uint32_t dst_x, dst_y, src_x, src_y;
866 intel_miptree_get_image_offset(dst_mt, level, slice, &dst_x, &dst_y);
867 intel_miptree_get_image_offset(src_mt, level, slice, &src_x, &src_y);
869 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
870 _mesa_get_format_name(src_mt->format),
871 src_mt, src_x, src_y, src_mt->region->pitch,
872 _mesa_get_format_name(dst_mt->format),
873 dst_mt, dst_x, dst_y, dst_mt->region->pitch,
876 if (!intelEmitCopyBlit(intel,
878 src_mt->region->pitch, src_mt->region->bo,
879 0, src_mt->region->tiling,
880 dst_mt->region->pitch, dst_mt->region->bo,
881 0, dst_mt->region->tiling,
887 perf_debug("miptree validate blit for %s failed\n",
888 _mesa_get_format_name(format));
890 intel_miptree_copy_slice_sw(intel, dst_mt, src_mt, level, slice,
896 * Copies the image's current data to the given miptree, and associates that
897 * miptree with the image.
899 * If \c invalidate is true, then the actual image data does not need to be
900 * copied, but the image still needs to be associated to the new miptree (this
901 * is set to true if we're about to clear the image).
904 intel_miptree_copy_teximage(struct intel_context *intel,
905 struct intel_texture_image *intelImage,
906 struct intel_mipmap_tree *dst_mt,
909 struct intel_mipmap_tree *src_mt = intelImage->mt;
910 struct intel_texture_object *intel_obj =
911 intel_texture_object(intelImage->base.Base.TexObject);
912 int level = intelImage->base.Base.Level;
913 int face = intelImage->base.Base.Face;
914 GLuint depth = intelImage->base.Base.Depth;
917 for (int slice = 0; slice < depth; slice++) {
918 intel_miptree_copy_slice(intel, dst_mt, src_mt, level, face, slice);
922 intel_miptree_reference(&intelImage->mt, dst_mt);
923 intel_obj->needs_validate = true;
927 intel_miptree_alloc_mcs(struct intel_context *intel,
928 struct intel_mipmap_tree *mt,
931 assert(mt->mcs_mt == NULL);
932 assert(intel->gen >= 7); /* MCS only used on Gen7+ */
934 /* Choose the correct format for the MCS buffer. All that really matters
935 * is that we allocate the right buffer size, since we'll always be
936 * accessing this miptree using MCS-specific hardware mechanisms, which
937 * infer the correct format based on num_samples.
940 switch (num_samples) {
942 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
945 format = MESA_FORMAT_R8;
948 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
949 * for each sample, plus 8 padding bits).
951 format = MESA_FORMAT_R_UINT32;
954 assert(!"Unrecognized sample count in intel_miptree_alloc_mcs");
958 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
960 * "The MCS surface must be stored as Tile Y."
962 mt->mcs_mt = intel_miptree_create(intel,
972 true /* force_y_tiling */);
974 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
976 * When MCS buffer is enabled and bound to MSRT, it is required that it
977 * is cleared prior to any rendering.
979 * Since we don't use the MCS buffer for any purpose other than rendering,
980 * it makes sense to just clear it immediately upon allocation.
982 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
984 void *data = intel_miptree_map_raw(intel, mt->mcs_mt);
985 memset(data, 0xff, mt->mcs_mt->region->bo->size);
986 intel_miptree_unmap_raw(intel, mt->mcs_mt);
992 * Helper for intel_miptree_alloc_hiz() that sets
993 * \c mt->level[level].slice[layer].has_hiz. Return true if and only if
994 * \c has_hiz was set.
997 intel_miptree_slice_enable_hiz(struct intel_context *intel,
998 struct intel_mipmap_tree *mt,
1004 if (intel->is_haswell) {
1005 /* Disable HiZ for some slices to work around a hardware bug.
1007 * Haswell hardware fails to respect
1008 * 3DSTATE_DEPTH_BUFFER.Depth_Coordinate_Offset_X/Y when during HiZ
1009 * ambiguate operations. The failure is inconsistent and affected by
1010 * other GPU contexts. Running a heavy GPU workload in a separate
1011 * process causes the failure rate to drop to nearly 0.
1013 * To workaround the bug, we enable HiZ only when we can guarantee that
1014 * the Depth Coordinate Offset fields will be set to 0. The function
1015 * brw_get_depthstencil_tile_masks() is used to calculate the fields,
1016 * and the function is sometimes called in such a way that the presence
1017 * of an attached stencil buffer changes the fuction's return value.
1019 * The largest tile size considered by brw_get_depthstencil_tile_masks()
1020 * is that of the stencil buffer. Therefore, if this hiz slice's
1021 * corresponding depth slice has an offset that is aligned to the
1022 * stencil buffer tile size, 64x64 pixels, then
1023 * 3DSTATE_DEPTH_BUFFER.Depth_Coordinate_Offset_X/Y is set to 0.
1025 uint32_t depth_x_offset = mt->level[level].slice[layer].x_offset;
1026 uint32_t depth_y_offset = mt->level[level].slice[layer].y_offset;
1027 if ((depth_x_offset & 63) || (depth_y_offset & 63)) {
1032 mt->level[level].slice[layer].has_hiz = true;
1039 intel_miptree_alloc_hiz(struct intel_context *intel,
1040 struct intel_mipmap_tree *mt,
1043 assert(mt->hiz_mt == NULL);
1044 mt->hiz_mt = intel_miptree_create(intel,
1050 mt->logical_height0,
1054 false /* force_y_tiling */);
1059 /* Mark that all slices need a HiZ resolve. */
1060 struct intel_resolve_map *head = &mt->hiz_map;
1061 for (int level = mt->first_level; level <= mt->last_level; ++level) {
1062 for (int layer = 0; layer < mt->level[level].depth; ++layer) {
1063 if (!intel_miptree_slice_enable_hiz(intel, mt, level, layer))
1066 head->next = malloc(sizeof(*head->next));
1067 head->next->prev = head;
1068 head->next->next = NULL;
1071 head->level = level;
1072 head->layer = layer;
1073 head->need = GEN6_HIZ_OP_HIZ_RESOLVE;
1081 * Does the miptree slice have hiz enabled?
1084 intel_miptree_slice_has_hiz(struct intel_mipmap_tree *mt,
1088 intel_miptree_check_level_layer(mt, level, layer);
1089 return mt->level[level].slice[layer].has_hiz;
1093 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
1097 if (!intel_miptree_slice_has_hiz(mt, level, layer))
1100 intel_resolve_map_set(&mt->hiz_map,
1101 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
1106 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
1110 if (!intel_miptree_slice_has_hiz(mt, level, layer))
1113 intel_resolve_map_set(&mt->hiz_map,
1114 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
1118 intel_miptree_slice_resolve(struct intel_context *intel,
1119 struct intel_mipmap_tree *mt,
1122 enum gen6_hiz_op need)
1124 intel_miptree_check_level_layer(mt, level, layer);
1126 struct intel_resolve_map *item =
1127 intel_resolve_map_get(&mt->hiz_map, level, layer);
1129 if (!item || item->need != need)
1132 intel_hiz_exec(intel, mt, level, layer, need);
1133 intel_resolve_map_remove(item);
1138 intel_miptree_slice_resolve_hiz(struct intel_context *intel,
1139 struct intel_mipmap_tree *mt,
1143 return intel_miptree_slice_resolve(intel, mt, level, layer,
1144 GEN6_HIZ_OP_HIZ_RESOLVE);
1148 intel_miptree_slice_resolve_depth(struct intel_context *intel,
1149 struct intel_mipmap_tree *mt,
1153 return intel_miptree_slice_resolve(intel, mt, level, layer,
1154 GEN6_HIZ_OP_DEPTH_RESOLVE);
1158 intel_miptree_all_slices_resolve(struct intel_context *intel,
1159 struct intel_mipmap_tree *mt,
1160 enum gen6_hiz_op need)
1162 bool did_resolve = false;
1163 struct intel_resolve_map *i, *next;
1165 for (i = mt->hiz_map.next; i; i = next) {
1167 if (i->need != need)
1170 intel_hiz_exec(intel, mt, i->level, i->layer, need);
1171 intel_resolve_map_remove(i);
1179 intel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
1180 struct intel_mipmap_tree *mt)
1182 return intel_miptree_all_slices_resolve(intel, mt,
1183 GEN6_HIZ_OP_HIZ_RESOLVE);
1187 intel_miptree_all_slices_resolve_depth(struct intel_context *intel,
1188 struct intel_mipmap_tree *mt)
1190 return intel_miptree_all_slices_resolve(intel, mt,
1191 GEN6_HIZ_OP_DEPTH_RESOLVE);
1195 intel_miptree_updownsample(struct intel_context *intel,
1196 struct intel_mipmap_tree *src,
1197 struct intel_mipmap_tree *dst,
1207 intel_miptree_slice_resolve_depth(intel, src, 0, 0);
1208 intel_miptree_slice_resolve_depth(intel, dst, 0, 0);
1210 brw_blorp_blit_miptrees(intel,
1211 src, 0 /* level */, 0 /* layer */,
1212 dst, 0 /* level */, 0 /* layer */,
1216 false, false /*mirror x, y*/);
1218 if (src->stencil_mt) {
1219 brw_blorp_blit_miptrees(intel,
1220 src->stencil_mt, 0 /* level */, 0 /* layer */,
1221 dst->stencil_mt, 0 /* level */, 0 /* layer */,
1225 false, false /*mirror x, y*/);
1231 assert_is_flat(struct intel_mipmap_tree *mt)
1233 assert(mt->target == GL_TEXTURE_2D);
1234 assert(mt->first_level == 0);
1235 assert(mt->last_level == 0);
1239 * \brief Downsample from mt to mt->singlesample_mt.
1241 * If the miptree needs no downsample, then skip.
1244 intel_miptree_downsample(struct intel_context *intel,
1245 struct intel_mipmap_tree *mt)
1247 /* Only flat, renderbuffer-like miptrees are supported. */
1250 if (!mt->need_downsample)
1252 intel_miptree_updownsample(intel,
1253 mt, mt->singlesample_mt,
1255 mt->logical_height0);
1256 mt->need_downsample = false;
1258 /* Strictly speaking, after a downsample on a depth miptree, a hiz
1259 * resolve is needed on the singlesample miptree. However, since the
1260 * singlesample miptree is never rendered to, the hiz resolve will never
1261 * occur. Therefore we do not mark the needed hiz resolve after
1267 * \brief Upsample from mt->singlesample_mt to mt.
1269 * The upsample is done unconditionally.
1272 intel_miptree_upsample(struct intel_context *intel,
1273 struct intel_mipmap_tree *mt)
1275 /* Only flat, renderbuffer-like miptrees are supported. */
1277 assert(!mt->need_downsample);
1279 intel_miptree_updownsample(intel,
1280 mt->singlesample_mt, mt,
1282 mt->logical_height0);
1283 intel_miptree_slice_set_needs_hiz_resolve(mt, 0, 0);
1287 intel_miptree_map_raw(struct intel_context *intel, struct intel_mipmap_tree *mt)
1289 drm_intel_bo *bo = mt->region->bo;
1291 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
1292 if (drm_intel_bo_busy(bo)) {
1293 perf_debug("Mapping a busy BO, causing a stall on the GPU.\n");
1297 intel_flush(&intel->ctx);
1299 if (mt->region->tiling != I915_TILING_NONE)
1300 drm_intel_gem_bo_map_gtt(bo);
1302 drm_intel_bo_map(bo, true);
1308 intel_miptree_unmap_raw(struct intel_context *intel,
1309 struct intel_mipmap_tree *mt)
1311 drm_intel_bo_unmap(mt->region->bo);
1315 intel_miptree_map_gtt(struct intel_context *intel,
1316 struct intel_mipmap_tree *mt,
1317 struct intel_miptree_map *map,
1318 unsigned int level, unsigned int slice)
1320 unsigned int bw, bh;
1322 unsigned int image_x, image_y;
1326 /* For compressed formats, the stride is the number of bytes per
1327 * row of blocks. intel_miptree_get_image_offset() already does
1330 _mesa_get_format_block_size(mt->format, &bw, &bh);
1331 assert(y % bh == 0);
1334 base = intel_miptree_map_raw(intel, mt) + mt->offset;
1339 /* Note that in the case of cube maps, the caller must have passed the
1340 * slice number referencing the face.
1342 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1346 map->stride = mt->region->pitch;
1347 map->ptr = base + y * map->stride + x * mt->cpp;
1350 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1351 map->x, map->y, map->w, map->h,
1352 mt, _mesa_get_format_name(mt->format),
1353 x, y, map->ptr, map->stride);
1357 intel_miptree_unmap_gtt(struct intel_context *intel,
1358 struct intel_mipmap_tree *mt,
1359 struct intel_miptree_map *map,
1363 intel_miptree_unmap_raw(intel, mt);
1367 intel_miptree_map_blit(struct intel_context *intel,
1368 struct intel_mipmap_tree *mt,
1369 struct intel_miptree_map *map,
1370 unsigned int level, unsigned int slice)
1372 unsigned int image_x, image_y;
1377 /* The blitter requires the pitch to be aligned to 4. */
1378 map->stride = ALIGN(map->w * mt->region->cpp, 4);
1380 map->bo = drm_intel_bo_alloc(intel->bufmgr, "intel_miptree_map_blit() temp",
1381 map->stride * map->h, 4096);
1383 fprintf(stderr, "Failed to allocate blit temporary\n");
1387 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1391 if (!intelEmitCopyBlit(intel,
1393 mt->region->pitch, mt->region->bo,
1394 mt->offset, mt->region->tiling,
1395 map->stride, map->bo,
1396 0, I915_TILING_NONE,
1401 fprintf(stderr, "Failed to blit\n");
1405 intel_batchbuffer_flush(intel);
1406 ret = drm_intel_bo_map(map->bo, (map->mode & GL_MAP_WRITE_BIT) != 0);
1408 fprintf(stderr, "Failed to map blit temporary\n");
1412 map->ptr = map->bo->virtual;
1414 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1415 map->x, map->y, map->w, map->h,
1416 mt, _mesa_get_format_name(mt->format),
1417 x, y, map->ptr, map->stride);
1422 drm_intel_bo_unreference(map->bo);
1428 intel_miptree_unmap_blit(struct intel_context *intel,
1429 struct intel_mipmap_tree *mt,
1430 struct intel_miptree_map *map,
1434 struct gl_context *ctx = &intel->ctx;
1435 drm_intel_bo_unmap(map->bo);
1437 if (map->mode & GL_MAP_WRITE_BIT) {
1438 unsigned int image_x, image_y;
1441 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1445 bool ok = intelEmitCopyBlit(intel,
1447 map->stride, map->bo,
1448 0, I915_TILING_NONE,
1449 mt->region->pitch, mt->region->bo,
1450 mt->offset, mt->region->tiling,
1455 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
1458 drm_intel_bo_unreference(map->bo);
1462 intel_miptree_map_s8(struct intel_context *intel,
1463 struct intel_mipmap_tree *mt,
1464 struct intel_miptree_map *map,
1465 unsigned int level, unsigned int slice)
1467 map->stride = map->w;
1468 map->buffer = map->ptr = malloc(map->stride * map->h);
1472 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
1473 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
1474 * invalidate is set, since we'll be writing the whole rectangle from our
1475 * temporary buffer back out.
1477 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1478 uint8_t *untiled_s8_map = map->ptr;
1479 uint8_t *tiled_s8_map = intel_miptree_map_raw(intel, mt);
1480 unsigned int image_x, image_y;
1482 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1484 for (uint32_t y = 0; y < map->h; y++) {
1485 for (uint32_t x = 0; x < map->w; x++) {
1486 ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
1487 x + image_x + map->x,
1488 y + image_y + map->y,
1489 intel->has_swizzling);
1490 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
1494 intel_miptree_unmap_raw(intel, mt);
1496 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__,
1497 map->x, map->y, map->w, map->h,
1498 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
1500 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1501 map->x, map->y, map->w, map->h,
1502 mt, map->ptr, map->stride);
1507 intel_miptree_unmap_s8(struct intel_context *intel,
1508 struct intel_mipmap_tree *mt,
1509 struct intel_miptree_map *map,
1513 if (map->mode & GL_MAP_WRITE_BIT) {
1514 unsigned int image_x, image_y;
1515 uint8_t *untiled_s8_map = map->ptr;
1516 uint8_t *tiled_s8_map = intel_miptree_map_raw(intel, mt);
1518 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1520 for (uint32_t y = 0; y < map->h; y++) {
1521 for (uint32_t x = 0; x < map->w; x++) {
1522 ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
1525 intel->has_swizzling);
1526 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
1530 intel_miptree_unmap_raw(intel, mt);
1537 intel_miptree_map_etc(struct intel_context *intel,
1538 struct intel_mipmap_tree *mt,
1539 struct intel_miptree_map *map,
1543 assert(mt->etc_format != MESA_FORMAT_NONE);
1544 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
1545 assert(mt->format == MESA_FORMAT_RGBX8888_REV);
1548 assert(map->mode & GL_MAP_WRITE_BIT);
1549 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
1551 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
1552 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
1553 map->w, map->h, 1));
1554 map->ptr = map->buffer;
1558 intel_miptree_unmap_etc(struct intel_context *intel,
1559 struct intel_mipmap_tree *mt,
1560 struct intel_miptree_map *map,
1566 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1571 uint8_t *dst = intel_miptree_map_raw(intel, mt)
1572 + image_y * mt->region->pitch
1573 + image_x * mt->region->cpp;
1575 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
1576 _mesa_etc1_unpack_rgba8888(dst, mt->region->pitch,
1577 map->ptr, map->stride,
1580 _mesa_unpack_etc2_format(dst, mt->region->pitch,
1581 map->ptr, map->stride,
1582 map->w, map->h, mt->etc_format);
1584 intel_miptree_unmap_raw(intel, mt);
1589 * Mapping function for packed depth/stencil miptrees backed by real separate
1590 * miptrees for depth and stencil.
1592 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
1593 * separate from the depth buffer. Yet at the GL API level, we have to expose
1594 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
1595 * be able to map that memory for texture storage and glReadPixels-type
1596 * operations. We give Mesa core that access by mallocing a temporary and
1597 * copying the data between the actual backing store and the temporary.
1600 intel_miptree_map_depthstencil(struct intel_context *intel,
1601 struct intel_mipmap_tree *mt,
1602 struct intel_miptree_map *map,
1603 unsigned int level, unsigned int slice)
1605 struct intel_mipmap_tree *z_mt = mt;
1606 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
1607 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
1608 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
1610 map->stride = map->w * packed_bpp;
1611 map->buffer = map->ptr = malloc(map->stride * map->h);
1615 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
1616 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
1617 * invalidate is set, since we'll be writing the whole rectangle from our
1618 * temporary buffer back out.
1620 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1621 uint32_t *packed_map = map->ptr;
1622 uint8_t *s_map = intel_miptree_map_raw(intel, s_mt);
1623 uint32_t *z_map = intel_miptree_map_raw(intel, z_mt);
1624 unsigned int s_image_x, s_image_y;
1625 unsigned int z_image_x, z_image_y;
1627 intel_miptree_get_image_offset(s_mt, level, slice,
1628 &s_image_x, &s_image_y);
1629 intel_miptree_get_image_offset(z_mt, level, slice,
1630 &z_image_x, &z_image_y);
1632 for (uint32_t y = 0; y < map->h; y++) {
1633 for (uint32_t x = 0; x < map->w; x++) {
1634 int map_x = map->x + x, map_y = map->y + y;
1635 ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
1638 intel->has_swizzling);
1639 ptrdiff_t z_offset = ((map_y + z_image_y) *
1640 (z_mt->region->pitch / 4) +
1641 (map_x + z_image_x));
1642 uint8_t s = s_map[s_offset];
1643 uint32_t z = z_map[z_offset];
1645 if (map_z32f_x24s8) {
1646 packed_map[(y * map->w + x) * 2 + 0] = z;
1647 packed_map[(y * map->w + x) * 2 + 1] = s;
1649 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
1654 intel_miptree_unmap_raw(intel, s_mt);
1655 intel_miptree_unmap_raw(intel, z_mt);
1657 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
1659 map->x, map->y, map->w, map->h,
1660 z_mt, map->x + z_image_x, map->y + z_image_y,
1661 s_mt, map->x + s_image_x, map->y + s_image_y,
1662 map->ptr, map->stride);
1664 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1665 map->x, map->y, map->w, map->h,
1666 mt, map->ptr, map->stride);
1671 intel_miptree_unmap_depthstencil(struct intel_context *intel,
1672 struct intel_mipmap_tree *mt,
1673 struct intel_miptree_map *map,
1677 struct intel_mipmap_tree *z_mt = mt;
1678 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
1679 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
1681 if (map->mode & GL_MAP_WRITE_BIT) {
1682 uint32_t *packed_map = map->ptr;
1683 uint8_t *s_map = intel_miptree_map_raw(intel, s_mt);
1684 uint32_t *z_map = intel_miptree_map_raw(intel, z_mt);
1685 unsigned int s_image_x, s_image_y;
1686 unsigned int z_image_x, z_image_y;
1688 intel_miptree_get_image_offset(s_mt, level, slice,
1689 &s_image_x, &s_image_y);
1690 intel_miptree_get_image_offset(z_mt, level, slice,
1691 &z_image_x, &z_image_y);
1693 for (uint32_t y = 0; y < map->h; y++) {
1694 for (uint32_t x = 0; x < map->w; x++) {
1695 ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
1696 x + s_image_x + map->x,
1697 y + s_image_y + map->y,
1698 intel->has_swizzling);
1699 ptrdiff_t z_offset = ((y + z_image_y) *
1700 (z_mt->region->pitch / 4) +
1703 if (map_z32f_x24s8) {
1704 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
1705 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
1707 uint32_t packed = packed_map[y * map->w + x];
1708 s_map[s_offset] = packed >> 24;
1709 z_map[z_offset] = packed;
1714 intel_miptree_unmap_raw(intel, s_mt);
1715 intel_miptree_unmap_raw(intel, z_mt);
1717 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
1719 map->x, map->y, map->w, map->h,
1720 z_mt, _mesa_get_format_name(z_mt->format),
1721 map->x + z_image_x, map->y + z_image_y,
1722 s_mt, map->x + s_image_x, map->y + s_image_y,
1723 map->ptr, map->stride);
1730 * Create and attach a map to the miptree at (level, slice). Return the
1733 static struct intel_miptree_map*
1734 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
1743 struct intel_miptree_map *map = calloc(1, sizeof(*map));
1748 assert(mt->level[level].slice[slice].map == NULL);
1749 mt->level[level].slice[slice].map = map;
1761 * Release the map at (level, slice).
1764 intel_miptree_release_map(struct intel_mipmap_tree *mt,
1768 struct intel_miptree_map **map;
1770 map = &mt->level[level].slice[slice].map;
1776 intel_miptree_map_singlesample(struct intel_context *intel,
1777 struct intel_mipmap_tree *mt,
1788 struct intel_miptree_map *map;
1790 assert(mt->num_samples <= 1);
1792 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
1799 intel_miptree_slice_resolve_depth(intel, mt, level, slice);
1800 if (map->mode & GL_MAP_WRITE_BIT) {
1801 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
1804 if (mt->format == MESA_FORMAT_S8) {
1805 intel_miptree_map_s8(intel, mt, map, level, slice);
1806 } else if (mt->etc_format != MESA_FORMAT_NONE &&
1807 !(mode & BRW_MAP_DIRECT_BIT)) {
1808 intel_miptree_map_etc(intel, mt, map, level, slice);
1809 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
1810 intel_miptree_map_depthstencil(intel, mt, map, level, slice);
1812 /* According to the Ivy Bridge PRM, Vol1 Part4, section 1.2.1.2 (Graphics
1813 * Data Size Limitations):
1815 * The BLT engine is capable of transferring very large quantities of
1816 * graphics data. Any graphics data read from and written to the
1817 * destination is permitted to represent a number of pixels that
1818 * occupies up to 65,536 scan lines and up to 32,768 bytes per scan line
1819 * at the destination. The maximum number of pixels that may be
1820 * represented per scan line’s worth of graphics data depends on the
1823 * Furthermore, intelEmitCopyBlit (which is called by
1824 * intel_miptree_map_blit) uses a signed 16-bit integer to represent buffer
1825 * pitch, so it can only handle buffer pitches < 32k.
1827 * As a result of these two limitations, we can only use
1828 * intel_miptree_map_blit() when the region's pitch is less than 32k.
1830 else if (intel->has_llc &&
1831 !(mode & GL_MAP_WRITE_BIT) &&
1833 mt->region->tiling == I915_TILING_X &&
1834 mt->region->pitch < 32768) {
1835 intel_miptree_map_blit(intel, mt, map, level, slice);
1836 } else if (mt->region->tiling != I915_TILING_NONE &&
1837 mt->region->bo->size >= intel->max_gtt_map_object_size) {
1838 assert(mt->region->pitch < 32768);
1839 intel_miptree_map_blit(intel, mt, map, level, slice);
1841 intel_miptree_map_gtt(intel, mt, map, level, slice);
1844 *out_ptr = map->ptr;
1845 *out_stride = map->stride;
1847 if (map->ptr == NULL)
1848 intel_miptree_release_map(mt, level, slice);
1852 intel_miptree_unmap_singlesample(struct intel_context *intel,
1853 struct intel_mipmap_tree *mt,
1857 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
1859 assert(mt->num_samples <= 1);
1864 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__,
1865 mt, _mesa_get_format_name(mt->format), level, slice);
1867 if (mt->format == MESA_FORMAT_S8) {
1868 intel_miptree_unmap_s8(intel, mt, map, level, slice);
1869 } else if (mt->etc_format != MESA_FORMAT_NONE &&
1870 !(map->mode & BRW_MAP_DIRECT_BIT)) {
1871 intel_miptree_unmap_etc(intel, mt, map, level, slice);
1872 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
1873 intel_miptree_unmap_depthstencil(intel, mt, map, level, slice);
1874 } else if (map->bo) {
1875 intel_miptree_unmap_blit(intel, mt, map, level, slice);
1877 intel_miptree_unmap_gtt(intel, mt, map, level, slice);
1880 intel_miptree_release_map(mt, level, slice);
1884 intel_miptree_map_multisample(struct intel_context *intel,
1885 struct intel_mipmap_tree *mt,
1896 struct intel_miptree_map *map;
1898 assert(mt->num_samples > 1);
1900 /* Only flat, renderbuffer-like miptrees are supported. */
1901 if (mt->target != GL_TEXTURE_2D ||
1902 mt->first_level != 0 ||
1903 mt->last_level != 0) {
1904 _mesa_problem(&intel->ctx, "attempt to map a multisample miptree for "
1905 "which (target, first_level, last_level != "
1906 "(GL_TEXTURE_2D, 0, 0)");
1910 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
1914 if (!mt->singlesample_mt) {
1915 mt->singlesample_mt =
1916 intel_miptree_create_for_renderbuffer(intel,
1919 mt->logical_height0,
1921 if (!mt->singlesample_mt)
1924 map->singlesample_mt_is_tmp = true;
1925 mt->need_downsample = true;
1928 intel_miptree_downsample(intel, mt);
1929 intel_miptree_map_singlesample(intel, mt->singlesample_mt,
1933 out_ptr, out_stride);
1937 intel_miptree_release_map(mt, level, slice);
1943 intel_miptree_unmap_multisample(struct intel_context *intel,
1944 struct intel_mipmap_tree *mt,
1948 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
1950 assert(mt->num_samples > 1);
1955 intel_miptree_unmap_singlesample(intel, mt->singlesample_mt, level, slice);
1957 mt->need_downsample = false;
1958 if (map->mode & GL_MAP_WRITE_BIT)
1959 intel_miptree_upsample(intel, mt);
1961 if (map->singlesample_mt_is_tmp)
1962 intel_miptree_release(&mt->singlesample_mt);
1964 intel_miptree_release_map(mt, level, slice);
1968 intel_miptree_map(struct intel_context *intel,
1969 struct intel_mipmap_tree *mt,
1980 if (mt->num_samples <= 1)
1981 intel_miptree_map_singlesample(intel, mt,
1985 out_ptr, out_stride);
1987 intel_miptree_map_multisample(intel, mt,
1991 out_ptr, out_stride);
1995 intel_miptree_unmap(struct intel_context *intel,
1996 struct intel_mipmap_tree *mt,
2000 if (mt->num_samples <= 1)
2001 intel_miptree_unmap_singlesample(intel, mt, level, slice);
2003 intel_miptree_unmap_multisample(intel, mt, level, slice);