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 **************************************************************************/
28 #include "intel_batchbuffer.h"
29 #include "intel_context.h"
30 #include "intel_mipmap_tree.h"
31 #include "intel_regions.h"
32 #include "intel_resolve_map.h"
33 #include "intel_span.h"
34 #include "intel_tex_layout.h"
35 #include "intel_tex.h"
36 #include "intel_blit.h"
38 #include "main/enums.h"
39 #include "main/formats.h"
40 #include "main/image.h"
41 #include "main/teximage.h"
43 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
46 target_to_target(GLenum target)
49 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
50 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
51 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
52 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
55 return GL_TEXTURE_CUBE_MAP_ARB;
62 * @param for_region Indicates that the caller is
63 * intel_miptree_create_for_region(). If true, then do not create
66 static struct intel_mipmap_tree *
67 intel_miptree_create_internal(struct intel_context *intel,
77 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
78 int compress_byte = 0;
80 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
81 _mesa_lookup_enum_by_nr(target),
82 _mesa_get_format_name(format),
83 first_level, last_level, mt);
85 if (_mesa_is_format_compressed(format))
86 compress_byte = intel_compressed_num_bytes(format);
88 mt->target = target_to_target(target);
90 mt->first_level = first_level;
91 mt->last_level = last_level;
93 mt->height0 = height0;
94 mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
95 mt->compressed = compress_byte ? 1 : 0;
98 if (target == GL_TEXTURE_CUBE_MAP) {
105 if (format == MESA_FORMAT_S8) {
106 /* The stencil buffer has quirky pitch requirements. From Vol 2a,
107 * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
108 * The pitch must be set to 2x the value computed based on width, as
109 * the stencil buffer is stored with two rows interleaved.
111 assert(intel->has_separate_stencil);
116 _mesa_is_depthstencil_format(_mesa_get_format_base_format(format)) &&
117 (intel->must_use_separate_stencil ||
118 (intel->has_separate_stencil &&
119 intel->vtbl.is_hiz_depth_format(intel, format)))) {
120 mt->stencil_mt = intel_miptree_create(intel,
129 if (!mt->stencil_mt) {
130 intel_miptree_release(&mt);
134 /* Fix up the Z miptree format for how we're splitting out separate
135 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
137 if (mt->format == MESA_FORMAT_S8_Z24) {
138 mt->format = MESA_FORMAT_X8_Z24;
139 } else if (mt->format == MESA_FORMAT_Z32_FLOAT_X24S8) {
140 mt->format = MESA_FORMAT_Z32_FLOAT;
143 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
144 _mesa_get_format_name(mt->format));
148 intel_get_texture_alignment_unit(intel, mt->format,
149 &mt->align_w, &mt->align_h);
154 i945_miptree_layout(mt);
156 i915_miptree_layout(mt);
158 brw_miptree_layout(intel, mt);
165 struct intel_mipmap_tree *
166 intel_miptree_create(struct intel_context *intel,
174 bool expect_accelerated_upload)
176 struct intel_mipmap_tree *mt;
177 uint32_t tiling = I915_TILING_NONE;
178 GLenum base_format = _mesa_get_format_base_format(format);
180 if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
181 if (intel->gen >= 4 &&
182 (base_format == GL_DEPTH_COMPONENT ||
183 base_format == GL_DEPTH_STENCIL_EXT))
184 tiling = I915_TILING_Y;
185 else if (width0 >= 64)
186 tiling = I915_TILING_X;
189 if (format == MESA_FORMAT_S8) {
190 /* The stencil buffer is W tiled. However, we request from the kernel a
191 * non-tiled buffer because the GTT is incapable of W fencing.
193 * The stencil buffer has quirky pitch requirements. From Vol 2a,
194 * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
195 * The pitch must be set to 2x the value computed based on width, as
196 * the stencil buffer is stored with two rows interleaved.
197 * To accomplish this, we resort to the nasty hack of doubling the drm
198 * region's cpp and halving its height.
200 * If we neglect to double the pitch, then render corruption occurs.
202 tiling = I915_TILING_NONE;
203 width0 = ALIGN(width0, 64);
204 height0 = ALIGN((height0 + 1) / 2, 64);
207 mt = intel_miptree_create_internal(intel, target, format,
208 first_level, last_level, width0,
212 * pitch == 0 || height == 0 indicates the null texture
214 if (!mt || !mt->total_width || !mt->total_height) {
215 intel_miptree_release(&mt);
219 mt->region = intel_region_alloc(intel->intelScreen,
224 expect_accelerated_upload);
227 intel_miptree_release(&mt);
235 struct intel_mipmap_tree *
236 intel_miptree_create_for_region(struct intel_context *intel,
239 struct intel_region *region)
241 struct intel_mipmap_tree *mt;
243 mt = intel_miptree_create_internal(intel, target, format,
245 region->width, region->height, 1,
250 intel_region_reference(&mt->region, region);
255 struct intel_mipmap_tree*
256 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
261 struct intel_mipmap_tree *mt;
263 mt = intel_miptree_create(intel, GL_TEXTURE_2D, format, 0, 0,
264 width, height, 1, true);
270 intel_miptree_reference(struct intel_mipmap_tree **dst,
271 struct intel_mipmap_tree *src)
276 intel_miptree_release(dst);
280 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
288 intel_miptree_release(struct intel_mipmap_tree **mt)
293 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
294 if (--(*mt)->refcount <= 0) {
297 DBG("%s deleting %p\n", __FUNCTION__, *mt);
299 intel_region_release(&((*mt)->region));
300 intel_miptree_release(&(*mt)->stencil_mt);
301 intel_miptree_release(&(*mt)->hiz_mt);
302 intel_resolve_map_clear(&(*mt)->hiz_map);
304 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
305 free((*mt)->level[i].slice);
314 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
315 int *width, int *height, int *depth)
317 switch (image->TexObject->Target) {
318 case GL_TEXTURE_1D_ARRAY:
319 *width = image->Width;
321 *depth = image->Height;
324 *width = image->Width;
325 *height = image->Height;
326 *depth = image->Depth;
332 * Can the image be pulled into a unified mipmap tree? This mirrors
333 * the completeness test in a lot of ways.
335 * Not sure whether I want to pass gl_texture_image here.
338 intel_miptree_match_image(struct intel_mipmap_tree *mt,
339 struct gl_texture_image *image)
341 struct intel_texture_image *intelImage = intel_texture_image(image);
342 GLuint level = intelImage->base.Base.Level;
343 int width, height, depth;
345 if (target_to_target(image->TexObject->Target) != mt->target)
348 if (image->TexFormat != mt->format &&
349 !(image->TexFormat == MESA_FORMAT_S8_Z24 &&
350 mt->format == MESA_FORMAT_X8_Z24 &&
355 intel_miptree_get_dimensions_for_image(image, &width, &height, &depth);
357 if (mt->target == GL_TEXTURE_CUBE_MAP)
360 /* Test image dimensions against the base level image adjusted for
361 * minification. This will also catch images not present in the
362 * tree, changed targets, etc.
364 if (width != mt->level[level].width ||
365 height != mt->level[level].height ||
366 depth != mt->level[level].depth)
374 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
377 GLuint w, GLuint h, GLuint d)
379 mt->level[level].width = w;
380 mt->level[level].height = h;
381 mt->level[level].depth = d;
382 mt->level[level].level_x = x;
383 mt->level[level].level_y = y;
385 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
386 level, w, h, d, x, y);
388 assert(mt->level[level].slice == NULL);
390 mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
391 mt->level[level].slice[0].x_offset = mt->level[level].level_x;
392 mt->level[level].slice[0].y_offset = mt->level[level].level_y;
397 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
398 GLuint level, GLuint img,
401 if (img == 0 && level == 0)
402 assert(x == 0 && y == 0);
404 assert(img < mt->level[level].depth);
406 mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
407 mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
409 DBG("%s level %d img %d pos %d,%d\n",
410 __FUNCTION__, level, img,
411 mt->level[level].slice[img].x_offset,
412 mt->level[level].slice[img].y_offset);
417 * For cube map textures, either the \c face parameter can be used, of course,
418 * or the cube face can be interpreted as a depth layer and the \c layer
422 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
423 GLuint level, GLuint face, GLuint layer,
424 GLuint *x, GLuint *y)
429 assert(mt->target == GL_TEXTURE_CUBE_MAP);
434 /* This branch may be taken even if the texture target is a cube map. In
435 * that case, the caller chose to interpret each cube face as a layer.
441 *x = mt->level[level].slice[slice].x_offset;
442 *y = mt->level[level].slice[slice].y_offset;
446 intel_miptree_copy_slice(struct intel_context *intel,
447 struct intel_mipmap_tree *dst_mt,
448 struct intel_mipmap_tree *src_mt,
454 gl_format format = src_mt->format;
455 uint32_t width = src_mt->level[level].width;
456 uint32_t height = src_mt->level[level].height;
458 assert(depth < src_mt->level[level].depth);
460 if (dst_mt->compressed) {
461 height = ALIGN(height, dst_mt->align_h) / dst_mt->align_h;
462 width = ALIGN(width, dst_mt->align_w);
465 uint32_t dst_x, dst_y, src_x, src_y;
466 intel_miptree_get_image_offset(dst_mt, level, face, depth,
468 intel_miptree_get_image_offset(src_mt, level, face, depth,
471 DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
472 src_mt, src_x, src_y, src_mt->region->pitch * src_mt->region->cpp,
473 dst_mt, dst_x, dst_y, dst_mt->region->pitch * dst_mt->region->cpp,
476 if (!intelEmitCopyBlit(intel,
478 src_mt->region->pitch, src_mt->region->bo,
479 0, src_mt->region->tiling,
480 dst_mt->region->pitch, dst_mt->region->bo,
481 0, dst_mt->region->tiling,
487 fallback_debug("miptree validate blit for %s failed\n",
488 _mesa_get_format_name(format));
489 void *dst = intel_region_map(intel, dst_mt->region, GL_MAP_WRITE_BIT);
490 void *src = intel_region_map(intel, src_mt->region, GL_MAP_READ_BIT);
494 dst_mt->region->pitch,
497 src, src_mt->region->pitch,
500 intel_region_unmap(intel, dst_mt->region);
501 intel_region_unmap(intel, src_mt->region);
504 if (src_mt->stencil_mt) {
505 intel_miptree_copy_slice(intel,
506 dst_mt->stencil_mt, src_mt->stencil_mt,
512 * Copies the image's current data to the given miptree, and associates that
513 * miptree with the image.
516 intel_miptree_copy_teximage(struct intel_context *intel,
517 struct intel_texture_image *intelImage,
518 struct intel_mipmap_tree *dst_mt)
520 struct intel_mipmap_tree *src_mt = intelImage->mt;
521 int level = intelImage->base.Base.Level;
522 int face = intelImage->base.Base.Face;
523 GLuint depth = intelImage->base.Base.Depth;
525 for (int slice = 0; slice < depth; slice++) {
526 intel_miptree_copy_slice(intel, dst_mt, src_mt, level, face, slice);
529 intel_miptree_reference(&intelImage->mt, dst_mt);
533 intel_miptree_alloc_hiz(struct intel_context *intel,
534 struct intel_mipmap_tree *mt)
536 assert(mt->hiz_mt == NULL);
537 mt->hiz_mt = intel_miptree_create(intel,
550 /* Mark that all slices need a HiZ resolve. */
551 struct intel_resolve_map *head = &mt->hiz_map;
552 for (int level = mt->first_level; level <= mt->last_level; ++level) {
553 for (int layer = 0; layer < mt->level[level].depth; ++layer) {
554 head->next = malloc(sizeof(*head->next));
555 head->next->prev = head;
556 head->next->next = NULL;
561 head->need = INTEL_NEED_HIZ_RESOLVE;
569 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
573 intel_miptree_check_level_layer(mt, level, layer);
578 intel_resolve_map_set(&mt->hiz_map,
579 level, layer, INTEL_NEED_HIZ_RESOLVE);
584 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
588 intel_miptree_check_level_layer(mt, level, layer);
593 intel_resolve_map_set(&mt->hiz_map,
594 level, layer, INTEL_NEED_DEPTH_RESOLVE);
597 typedef void (*resolve_func_t)(struct intel_context *intel,
598 struct intel_mipmap_tree *mt,
603 intel_miptree_slice_resolve(struct intel_context *intel,
604 struct intel_mipmap_tree *mt,
607 enum intel_need_resolve need,
610 intel_miptree_check_level_layer(mt, level, layer);
612 struct intel_resolve_map *item =
613 intel_resolve_map_get(&mt->hiz_map, level, layer);
615 if (!item || item->need != need)
618 func(intel, mt, level, layer);
619 intel_resolve_map_remove(item);
624 intel_miptree_slice_resolve_hiz(struct intel_context *intel,
625 struct intel_mipmap_tree *mt,
629 return intel_miptree_slice_resolve(intel, mt, level, layer,
630 INTEL_NEED_HIZ_RESOLVE,
631 intel->vtbl.resolve_hiz_slice);
635 intel_miptree_slice_resolve_depth(struct intel_context *intel,
636 struct intel_mipmap_tree *mt,
640 return intel_miptree_slice_resolve(intel, mt, level, layer,
641 INTEL_NEED_DEPTH_RESOLVE,
642 intel->vtbl.resolve_depth_slice);
646 intel_miptree_all_slices_resolve(struct intel_context *intel,
647 struct intel_mipmap_tree *mt,
648 enum intel_need_resolve need,
651 bool did_resolve = false;
652 struct intel_resolve_map *i, *next;
654 for (i = mt->hiz_map.next; i; i = next) {
658 func(intel, mt, i->level, i->layer);
659 intel_resolve_map_remove(i);
667 intel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
668 struct intel_mipmap_tree *mt)
670 return intel_miptree_all_slices_resolve(intel, mt,
671 INTEL_NEED_HIZ_RESOLVE,
672 intel->vtbl.resolve_hiz_slice);
676 intel_miptree_all_slices_resolve_depth(struct intel_context *intel,
677 struct intel_mipmap_tree *mt)
679 return intel_miptree_all_slices_resolve(intel, mt,
680 INTEL_NEED_DEPTH_RESOLVE,
681 intel->vtbl.resolve_depth_slice);
685 intel_miptree_map_gtt(struct intel_context *intel,
686 struct intel_mipmap_tree *mt,
687 struct intel_miptree_map *map,
688 unsigned int level, unsigned int slice)
692 unsigned int image_x, image_y;
696 /* For compressed formats, the stride is the number of bytes per
697 * row of blocks. intel_miptree_get_image_offset() already does
700 _mesa_get_format_block_size(mt->format, &bw, &bh);
704 base = intel_region_map(intel, mt->region, map->mode);
709 /* Note that in the case of cube maps, the caller must have passed the
710 * slice number referencing the face.
712 intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
716 map->stride = mt->region->pitch * mt->cpp;
717 map->ptr = base + y * map->stride + x * mt->cpp;
720 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
721 map->x, map->y, map->w, map->h,
722 mt, _mesa_get_format_name(mt->format),
723 x, y, map->ptr, map->stride);
727 intel_miptree_unmap_gtt(struct intel_context *intel,
728 struct intel_mipmap_tree *mt,
729 struct intel_miptree_map *map,
733 intel_region_unmap(intel, mt->region);
737 intel_miptree_map_blit(struct intel_context *intel,
738 struct intel_mipmap_tree *mt,
739 struct intel_miptree_map *map,
740 unsigned int level, unsigned int slice)
742 unsigned int image_x, image_y;
747 /* The blitter requires the pitch to be aligned to 4. */
748 map->stride = ALIGN(map->w * mt->region->cpp, 4);
750 map->bo = drm_intel_bo_alloc(intel->bufmgr, "intel_miptree_map_blit() temp",
751 map->stride * map->h, 4096);
753 fprintf(stderr, "Failed to allocate blit temporary\n");
757 intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
761 if (!intelEmitCopyBlit(intel,
763 mt->region->pitch, mt->region->bo,
764 0, mt->region->tiling,
765 map->stride / mt->region->cpp, map->bo,
771 fprintf(stderr, "Failed to blit\n");
775 intel_batchbuffer_flush(intel);
776 ret = drm_intel_bo_map(map->bo, (map->mode & GL_MAP_WRITE_BIT) != 0);
778 fprintf(stderr, "Failed to map blit temporary\n");
782 map->ptr = map->bo->virtual;
784 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
785 map->x, map->y, map->w, map->h,
786 mt, _mesa_get_format_name(mt->format),
787 x, y, map->ptr, map->stride);
792 drm_intel_bo_unreference(map->bo);
798 intel_miptree_unmap_blit(struct intel_context *intel,
799 struct intel_mipmap_tree *mt,
800 struct intel_miptree_map *map,
804 assert(!(map->mode & GL_MAP_WRITE_BIT));
806 drm_intel_bo_unmap(map->bo);
807 drm_intel_bo_unreference(map->bo);
811 intel_miptree_map_s8(struct intel_context *intel,
812 struct intel_mipmap_tree *mt,
813 struct intel_miptree_map *map,
814 unsigned int level, unsigned int slice)
816 map->stride = map->w;
817 map->buffer = map->ptr = malloc(map->stride * map->h);
821 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
822 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
823 * invalidate is set, since we'll be writing the whole rectangle from our
824 * temporary buffer back out.
826 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
827 uint8_t *untiled_s8_map = map->ptr;
828 uint8_t *tiled_s8_map = intel_region_map(intel, mt->region,
830 unsigned int image_x, image_y;
832 intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
834 for (uint32_t y = 0; y < map->h; y++) {
835 for (uint32_t x = 0; x < map->w; x++) {
836 ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
837 x + image_x + map->x,
838 y + image_y + map->y,
839 intel->has_swizzling);
840 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
844 intel_region_unmap(intel, mt->region);
846 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__,
847 map->x, map->y, map->w, map->h,
848 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
850 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
851 map->x, map->y, map->w, map->h,
852 mt, map->ptr, map->stride);
857 intel_miptree_unmap_s8(struct intel_context *intel,
858 struct intel_mipmap_tree *mt,
859 struct intel_miptree_map *map,
863 if (map->mode & GL_MAP_WRITE_BIT) {
864 unsigned int image_x, image_y;
865 uint8_t *untiled_s8_map = map->ptr;
866 uint8_t *tiled_s8_map = intel_region_map(intel, mt->region, map->mode);
868 intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
870 for (uint32_t y = 0; y < map->h; y++) {
871 for (uint32_t x = 0; x < map->w; x++) {
872 ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
875 intel->has_swizzling);
876 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
880 intel_region_unmap(intel, mt->region);
887 * Mapping function for packed depth/stencil miptrees backed by real separate
888 * miptrees for depth and stencil.
890 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
891 * separate from the depth buffer. Yet at the GL API level, we have to expose
892 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
893 * be able to map that memory for texture storage and glReadPixels-type
894 * operations. We give Mesa core that access by mallocing a temporary and
895 * copying the data between the actual backing store and the temporary.
898 intel_miptree_map_depthstencil(struct intel_context *intel,
899 struct intel_mipmap_tree *mt,
900 struct intel_miptree_map *map,
901 unsigned int level, unsigned int slice)
903 struct intel_mipmap_tree *z_mt = mt;
904 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
905 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
906 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
908 map->stride = map->w * packed_bpp;
909 map->buffer = map->ptr = malloc(map->stride * map->h);
913 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
914 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
915 * invalidate is set, since we'll be writing the whole rectangle from our
916 * temporary buffer back out.
918 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
919 uint32_t *packed_map = map->ptr;
920 uint8_t *s_map = intel_region_map(intel, s_mt->region, GL_MAP_READ_BIT);
921 uint32_t *z_map = intel_region_map(intel, z_mt->region, GL_MAP_READ_BIT);
922 unsigned int s_image_x, s_image_y;
923 unsigned int z_image_x, z_image_y;
925 intel_miptree_get_image_offset(s_mt, level, 0, slice,
926 &s_image_x, &s_image_y);
927 intel_miptree_get_image_offset(z_mt, level, 0, slice,
928 &z_image_x, &z_image_y);
930 for (uint32_t y = 0; y < map->h; y++) {
931 for (uint32_t x = 0; x < map->w; x++) {
932 int map_x = map->x + x, map_y = map->y + y;
933 ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
936 intel->has_swizzling);
937 ptrdiff_t z_offset = ((map_y + z_image_y) * z_mt->region->pitch +
938 (map_x + z_image_x));
939 uint8_t s = s_map[s_offset];
940 uint32_t z = z_map[z_offset];
942 if (map_z32f_x24s8) {
943 packed_map[(y * map->w + x) * 2 + 0] = z;
944 packed_map[(y * map->w + x) * 2 + 1] = s;
946 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
951 intel_region_unmap(intel, s_mt->region);
952 intel_region_unmap(intel, z_mt->region);
954 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
956 map->x, map->y, map->w, map->h,
957 z_mt, map->x + z_image_x, map->y + z_image_y,
958 s_mt, map->x + s_image_x, map->y + s_image_y,
959 map->ptr, map->stride);
961 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
962 map->x, map->y, map->w, map->h,
963 mt, map->ptr, map->stride);
968 intel_miptree_unmap_depthstencil(struct intel_context *intel,
969 struct intel_mipmap_tree *mt,
970 struct intel_miptree_map *map,
974 struct intel_mipmap_tree *z_mt = mt;
975 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
976 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
978 if (map->mode & GL_MAP_WRITE_BIT) {
979 uint32_t *packed_map = map->ptr;
980 uint8_t *s_map = intel_region_map(intel, s_mt->region, map->mode);
981 uint32_t *z_map = intel_region_map(intel, z_mt->region, map->mode);
982 unsigned int s_image_x, s_image_y;
983 unsigned int z_image_x, z_image_y;
985 intel_miptree_get_image_offset(s_mt, level, 0, slice,
986 &s_image_x, &s_image_y);
987 intel_miptree_get_image_offset(z_mt, level, 0, slice,
988 &z_image_x, &z_image_y);
990 for (uint32_t y = 0; y < map->h; y++) {
991 for (uint32_t x = 0; x < map->w; x++) {
992 ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
993 x + s_image_x + map->x,
994 y + s_image_y + map->y,
995 intel->has_swizzling);
996 ptrdiff_t z_offset = ((y + z_image_y) * z_mt->region->pitch +
999 if (map_z32f_x24s8) {
1000 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
1001 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
1003 uint32_t packed = packed_map[y * map->w + x];
1004 s_map[s_offset] = packed >> 24;
1005 z_map[z_offset] = packed;
1010 intel_region_unmap(intel, s_mt->region);
1011 intel_region_unmap(intel, z_mt->region);
1013 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
1015 map->x, map->y, map->w, map->h,
1016 z_mt, _mesa_get_format_name(z_mt->format),
1017 map->x + z_image_x, map->y + z_image_y,
1018 s_mt, map->x + s_image_x, map->y + s_image_y,
1019 map->ptr, map->stride);
1026 intel_miptree_map(struct intel_context *intel,
1027 struct intel_mipmap_tree *mt,
1038 struct intel_miptree_map *map;
1040 map = calloc(1, sizeof(struct intel_miptree_map));
1047 assert(!mt->level[level].slice[slice].map);
1048 mt->level[level].slice[slice].map = map;
1055 intel_miptree_slice_resolve_depth(intel, mt, level, slice);
1056 if (map->mode & GL_MAP_WRITE_BIT) {
1057 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
1060 if (mt->format == MESA_FORMAT_S8) {
1061 intel_miptree_map_s8(intel, mt, map, level, slice);
1062 } else if (mt->stencil_mt) {
1063 intel_miptree_map_depthstencil(intel, mt, map, level, slice);
1064 } else if (intel->has_llc &&
1065 !(mode & GL_MAP_WRITE_BIT) &&
1067 mt->region->tiling == I915_TILING_X) {
1068 intel_miptree_map_blit(intel, mt, map, level, slice);
1070 intel_miptree_map_gtt(intel, mt, map, level, slice);
1073 *out_ptr = map->ptr;
1074 *out_stride = map->stride;
1076 if (map->ptr == NULL) {
1077 mt->level[level].slice[slice].map = NULL;
1083 intel_miptree_unmap(struct intel_context *intel,
1084 struct intel_mipmap_tree *mt,
1088 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
1093 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__,
1094 mt, _mesa_get_format_name(mt->format), level, slice);
1096 if (mt->format == MESA_FORMAT_S8) {
1097 intel_miptree_unmap_s8(intel, mt, map, level, slice);
1098 } else if (mt->stencil_mt) {
1099 intel_miptree_unmap_depthstencil(intel, mt, map, level, slice);
1100 } else if (map->bo) {
1101 intel_miptree_unmap_blit(intel, mt, map, level, slice);
1103 intel_miptree_unmap_gtt(intel, mt, map, level, slice);
1106 mt->level[level].slice[slice].map = NULL;