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
,
78 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
79 int compress_byte
= 0;
81 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
82 _mesa_lookup_enum_by_nr(target
),
83 _mesa_get_format_name(format
),
84 first_level
, last_level
, mt
);
86 if (_mesa_is_format_compressed(format
))
87 compress_byte
= intel_compressed_num_bytes(format
);
89 mt
->target
= target_to_target(target
);
91 mt
->first_level
= first_level
;
92 mt
->last_level
= last_level
;
94 mt
->height0
= height0
;
95 mt
->cpp
= compress_byte
? compress_byte
: _mesa_get_format_bytes(mt
->format
);
96 mt
->num_samples
= num_samples
;
97 mt
->compressed
= compress_byte
? 1 : 0;
100 if (target
== GL_TEXTURE_CUBE_MAP
) {
108 _mesa_is_depthstencil_format(_mesa_get_format_base_format(format
)) &&
109 (intel
->must_use_separate_stencil
||
110 (intel
->has_separate_stencil
&&
111 intel
->vtbl
.is_hiz_depth_format(intel
, format
)))) {
112 mt
->stencil_mt
= intel_miptree_create(intel
,
122 if (!mt
->stencil_mt
) {
123 intel_miptree_release(&mt
);
127 /* Fix up the Z miptree format for how we're splitting out separate
128 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
130 if (mt
->format
== MESA_FORMAT_S8_Z24
) {
131 mt
->format
= MESA_FORMAT_X8_Z24
;
132 } else if (mt
->format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
133 mt
->format
= MESA_FORMAT_Z32_FLOAT
;
136 _mesa_problem(NULL
, "Unknown format %s in separate stencil mt\n",
137 _mesa_get_format_name(mt
->format
));
141 intel_get_texture_alignment_unit(intel
, mt
->format
,
142 &mt
->align_w
, &mt
->align_h
);
147 i945_miptree_layout(mt
);
149 i915_miptree_layout(mt
);
151 brw_miptree_layout(intel
, mt
);
158 struct intel_mipmap_tree
*
159 intel_miptree_create(struct intel_context
*intel
,
167 bool expect_accelerated_upload
,
170 struct intel_mipmap_tree
*mt
;
171 uint32_t tiling
= I915_TILING_NONE
;
172 GLenum base_format
= _mesa_get_format_base_format(format
);
174 if (intel
->use_texture_tiling
&& !_mesa_is_format_compressed(format
)) {
175 if (intel
->gen
>= 4 &&
176 (base_format
== GL_DEPTH_COMPONENT
||
177 base_format
== GL_DEPTH_STENCIL_EXT
))
178 tiling
= I915_TILING_Y
;
179 else if (num_samples
> 0) {
180 /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
183 * [DevSNB+]: For multi-sample render targets, this field must be
184 * 1. MSRTs can only be tiled.
186 * Our usual reason for preferring X tiling (fast blits using the
187 * blitting engine) doesn't apply to MSAA, since we'll generally be
188 * downsampling or upsampling when blitting between the MSAA buffer
189 * and another buffer, and the blitting engine doesn't support that.
190 * So use Y tiling, since it makes better use of the cache.
192 tiling
= I915_TILING_Y
;
193 } else if (width0
>= 64)
194 tiling
= I915_TILING_X
;
197 if (format
== MESA_FORMAT_S8
) {
198 /* The stencil buffer is W tiled. However, we request from the kernel a
199 * non-tiled buffer because the GTT is incapable of W fencing. So round
200 * up the width and height to match the size of W tiles (64x64).
202 tiling
= I915_TILING_NONE
;
203 width0
= ALIGN(width0
, 64);
204 height0
= ALIGN(height0
, 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,
246 true, 0 /* num_samples */);
250 intel_region_reference(&mt
->region
, region
);
255 struct intel_mipmap_tree
*
256 intel_miptree_create_for_renderbuffer(struct intel_context
*intel
,
260 uint32_t num_samples
)
262 struct intel_mipmap_tree
*mt
;
264 /* Adjust width/height for MSAA */
265 if (num_samples
> 4) {
269 } else if (num_samples
> 0) {
275 mt
= intel_miptree_create(intel
, GL_TEXTURE_2D
, format
, 0, 0,
276 width
, height
, 1, true, num_samples
);
282 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
283 struct intel_mipmap_tree
*src
)
288 intel_miptree_release(dst
);
292 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
300 intel_miptree_release(struct intel_mipmap_tree
**mt
)
305 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
306 if (--(*mt
)->refcount
<= 0) {
309 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
311 intel_region_release(&((*mt
)->region
));
312 intel_miptree_release(&(*mt
)->stencil_mt
);
313 intel_miptree_release(&(*mt
)->hiz_mt
);
314 intel_resolve_map_clear(&(*mt
)->hiz_map
);
316 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
317 free((*mt
)->level
[i
].slice
);
326 intel_miptree_get_dimensions_for_image(struct gl_texture_image
*image
,
327 int *width
, int *height
, int *depth
)
329 switch (image
->TexObject
->Target
) {
330 case GL_TEXTURE_1D_ARRAY
:
331 *width
= image
->Width
;
333 *depth
= image
->Height
;
336 *width
= image
->Width
;
337 *height
= image
->Height
;
338 *depth
= image
->Depth
;
344 * Can the image be pulled into a unified mipmap tree? This mirrors
345 * the completeness test in a lot of ways.
347 * Not sure whether I want to pass gl_texture_image here.
350 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
351 struct gl_texture_image
*image
)
353 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
354 GLuint level
= intelImage
->base
.Base
.Level
;
355 int width
, height
, depth
;
357 if (target_to_target(image
->TexObject
->Target
) != mt
->target
)
360 if (image
->TexFormat
!= mt
->format
&&
361 !(image
->TexFormat
== MESA_FORMAT_S8_Z24
&&
362 mt
->format
== MESA_FORMAT_X8_Z24
&&
367 intel_miptree_get_dimensions_for_image(image
, &width
, &height
, &depth
);
369 if (mt
->target
== GL_TEXTURE_CUBE_MAP
)
372 /* Test image dimensions against the base level image adjusted for
373 * minification. This will also catch images not present in the
374 * tree, changed targets, etc.
376 if (width
!= mt
->level
[level
].width
||
377 height
!= mt
->level
[level
].height
||
378 depth
!= mt
->level
[level
].depth
)
386 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
389 GLuint w
, GLuint h
, GLuint d
)
391 mt
->level
[level
].width
= w
;
392 mt
->level
[level
].height
= h
;
393 mt
->level
[level
].depth
= d
;
394 mt
->level
[level
].level_x
= x
;
395 mt
->level
[level
].level_y
= y
;
397 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__
,
398 level
, w
, h
, d
, x
, y
);
400 assert(mt
->level
[level
].slice
== NULL
);
402 mt
->level
[level
].slice
= calloc(d
, sizeof(*mt
->level
[0].slice
));
403 mt
->level
[level
].slice
[0].x_offset
= mt
->level
[level
].level_x
;
404 mt
->level
[level
].slice
[0].y_offset
= mt
->level
[level
].level_y
;
409 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
410 GLuint level
, GLuint img
,
413 if (img
== 0 && level
== 0)
414 assert(x
== 0 && y
== 0);
416 assert(img
< mt
->level
[level
].depth
);
418 mt
->level
[level
].slice
[img
].x_offset
= mt
->level
[level
].level_x
+ x
;
419 mt
->level
[level
].slice
[img
].y_offset
= mt
->level
[level
].level_y
+ y
;
421 DBG("%s level %d img %d pos %d,%d\n",
422 __FUNCTION__
, level
, img
,
423 mt
->level
[level
].slice
[img
].x_offset
,
424 mt
->level
[level
].slice
[img
].y_offset
);
429 * For cube map textures, either the \c face parameter can be used, of course,
430 * or the cube face can be interpreted as a depth layer and the \c layer
434 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
435 GLuint level
, GLuint face
, GLuint layer
,
436 GLuint
*x
, GLuint
*y
)
441 assert(mt
->target
== GL_TEXTURE_CUBE_MAP
);
446 /* This branch may be taken even if the texture target is a cube map. In
447 * that case, the caller chose to interpret each cube face as a layer.
453 *x
= mt
->level
[level
].slice
[slice
].x_offset
;
454 *y
= mt
->level
[level
].slice
[slice
].y_offset
;
458 intel_miptree_copy_slice(struct intel_context
*intel
,
459 struct intel_mipmap_tree
*dst_mt
,
460 struct intel_mipmap_tree
*src_mt
,
466 gl_format format
= src_mt
->format
;
467 uint32_t width
= src_mt
->level
[level
].width
;
468 uint32_t height
= src_mt
->level
[level
].height
;
470 assert(depth
< src_mt
->level
[level
].depth
);
472 if (dst_mt
->compressed
) {
473 height
= ALIGN(height
, dst_mt
->align_h
) / dst_mt
->align_h
;
474 width
= ALIGN(width
, dst_mt
->align_w
);
477 uint32_t dst_x
, dst_y
, src_x
, src_y
;
478 intel_miptree_get_image_offset(dst_mt
, level
, face
, depth
,
480 intel_miptree_get_image_offset(src_mt
, level
, face
, depth
,
483 DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
484 src_mt
, src_x
, src_y
, src_mt
->region
->pitch
* src_mt
->region
->cpp
,
485 dst_mt
, dst_x
, dst_y
, dst_mt
->region
->pitch
* dst_mt
->region
->cpp
,
488 if (!intelEmitCopyBlit(intel
,
490 src_mt
->region
->pitch
, src_mt
->region
->bo
,
491 0, src_mt
->region
->tiling
,
492 dst_mt
->region
->pitch
, dst_mt
->region
->bo
,
493 0, dst_mt
->region
->tiling
,
499 fallback_debug("miptree validate blit for %s failed\n",
500 _mesa_get_format_name(format
));
501 void *dst
= intel_region_map(intel
, dst_mt
->region
, GL_MAP_WRITE_BIT
);
502 void *src
= intel_region_map(intel
, src_mt
->region
, GL_MAP_READ_BIT
);
506 dst_mt
->region
->pitch
,
509 src
, src_mt
->region
->pitch
,
512 intel_region_unmap(intel
, dst_mt
->region
);
513 intel_region_unmap(intel
, src_mt
->region
);
516 if (src_mt
->stencil_mt
) {
517 intel_miptree_copy_slice(intel
,
518 dst_mt
->stencil_mt
, src_mt
->stencil_mt
,
524 * Copies the image's current data to the given miptree, and associates that
525 * miptree with the image.
528 intel_miptree_copy_teximage(struct intel_context
*intel
,
529 struct intel_texture_image
*intelImage
,
530 struct intel_mipmap_tree
*dst_mt
)
532 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
533 int level
= intelImage
->base
.Base
.Level
;
534 int face
= intelImage
->base
.Base
.Face
;
535 GLuint depth
= intelImage
->base
.Base
.Depth
;
537 for (int slice
= 0; slice
< depth
; slice
++) {
538 intel_miptree_copy_slice(intel
, dst_mt
, src_mt
, level
, face
, slice
);
541 intel_miptree_reference(&intelImage
->mt
, dst_mt
);
545 intel_miptree_alloc_hiz(struct intel_context
*intel
,
546 struct intel_mipmap_tree
*mt
,
549 assert(mt
->hiz_mt
== NULL
);
550 mt
->hiz_mt
= intel_miptree_create(intel
,
564 /* Mark that all slices need a HiZ resolve. */
565 struct intel_resolve_map
*head
= &mt
->hiz_map
;
566 for (int level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
567 for (int layer
= 0; layer
< mt
->level
[level
].depth
; ++layer
) {
568 head
->next
= malloc(sizeof(*head
->next
));
569 head
->next
->prev
= head
;
570 head
->next
->next
= NULL
;
575 head
->need
= GEN6_HIZ_OP_HIZ_RESOLVE
;
583 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree
*mt
,
587 intel_miptree_check_level_layer(mt
, level
, layer
);
592 intel_resolve_map_set(&mt
->hiz_map
,
593 level
, layer
, GEN6_HIZ_OP_HIZ_RESOLVE
);
598 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree
*mt
,
602 intel_miptree_check_level_layer(mt
, level
, layer
);
607 intel_resolve_map_set(&mt
->hiz_map
,
608 level
, layer
, GEN6_HIZ_OP_DEPTH_RESOLVE
);
612 intel_miptree_slice_resolve(struct intel_context
*intel
,
613 struct intel_mipmap_tree
*mt
,
616 enum gen6_hiz_op need
)
618 intel_miptree_check_level_layer(mt
, level
, layer
);
620 struct intel_resolve_map
*item
=
621 intel_resolve_map_get(&mt
->hiz_map
, level
, layer
);
623 if (!item
|| item
->need
!= need
)
626 intel_hiz_exec(intel
, mt
, level
, layer
, need
);
627 intel_resolve_map_remove(item
);
632 intel_miptree_slice_resolve_hiz(struct intel_context
*intel
,
633 struct intel_mipmap_tree
*mt
,
637 return intel_miptree_slice_resolve(intel
, mt
, level
, layer
,
638 GEN6_HIZ_OP_HIZ_RESOLVE
);
642 intel_miptree_slice_resolve_depth(struct intel_context
*intel
,
643 struct intel_mipmap_tree
*mt
,
647 return intel_miptree_slice_resolve(intel
, mt
, level
, layer
,
648 GEN6_HIZ_OP_DEPTH_RESOLVE
);
652 intel_miptree_all_slices_resolve(struct intel_context
*intel
,
653 struct intel_mipmap_tree
*mt
,
654 enum gen6_hiz_op need
)
656 bool did_resolve
= false;
657 struct intel_resolve_map
*i
, *next
;
659 for (i
= mt
->hiz_map
.next
; i
; i
= next
) {
664 intel_hiz_exec(intel
, mt
, i
->level
, i
->layer
, need
);
665 intel_resolve_map_remove(i
);
673 intel_miptree_all_slices_resolve_hiz(struct intel_context
*intel
,
674 struct intel_mipmap_tree
*mt
)
676 return intel_miptree_all_slices_resolve(intel
, mt
,
677 GEN6_HIZ_OP_HIZ_RESOLVE
);
681 intel_miptree_all_slices_resolve_depth(struct intel_context
*intel
,
682 struct intel_mipmap_tree
*mt
)
684 return intel_miptree_all_slices_resolve(intel
, mt
,
685 GEN6_HIZ_OP_DEPTH_RESOLVE
);
689 intel_miptree_map_gtt(struct intel_context
*intel
,
690 struct intel_mipmap_tree
*mt
,
691 struct intel_miptree_map
*map
,
692 unsigned int level
, unsigned int slice
)
696 unsigned int image_x
, image_y
;
700 /* For compressed formats, the stride is the number of bytes per
701 * row of blocks. intel_miptree_get_image_offset() already does
704 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
708 base
= intel_region_map(intel
, mt
->region
, map
->mode
);
713 /* Note that in the case of cube maps, the caller must have passed the
714 * slice number referencing the face.
716 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
720 map
->stride
= mt
->region
->pitch
* mt
->cpp
;
721 map
->ptr
= base
+ y
* map
->stride
+ x
* mt
->cpp
;
724 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__
,
725 map
->x
, map
->y
, map
->w
, map
->h
,
726 mt
, _mesa_get_format_name(mt
->format
),
727 x
, y
, map
->ptr
, map
->stride
);
731 intel_miptree_unmap_gtt(struct intel_context
*intel
,
732 struct intel_mipmap_tree
*mt
,
733 struct intel_miptree_map
*map
,
737 intel_region_unmap(intel
, mt
->region
);
741 intel_miptree_map_blit(struct intel_context
*intel
,
742 struct intel_mipmap_tree
*mt
,
743 struct intel_miptree_map
*map
,
744 unsigned int level
, unsigned int slice
)
746 unsigned int image_x
, image_y
;
751 /* The blitter requires the pitch to be aligned to 4. */
752 map
->stride
= ALIGN(map
->w
* mt
->region
->cpp
, 4);
754 map
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "intel_miptree_map_blit() temp",
755 map
->stride
* map
->h
, 4096);
757 fprintf(stderr
, "Failed to allocate blit temporary\n");
761 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
765 if (!intelEmitCopyBlit(intel
,
767 mt
->region
->pitch
, mt
->region
->bo
,
768 0, mt
->region
->tiling
,
769 map
->stride
/ mt
->region
->cpp
, map
->bo
,
775 fprintf(stderr
, "Failed to blit\n");
779 intel_batchbuffer_flush(intel
);
780 ret
= drm_intel_bo_map(map
->bo
, (map
->mode
& GL_MAP_WRITE_BIT
) != 0);
782 fprintf(stderr
, "Failed to map blit temporary\n");
786 map
->ptr
= map
->bo
->virtual;
788 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__
,
789 map
->x
, map
->y
, map
->w
, map
->h
,
790 mt
, _mesa_get_format_name(mt
->format
),
791 x
, y
, map
->ptr
, map
->stride
);
796 drm_intel_bo_unreference(map
->bo
);
802 intel_miptree_unmap_blit(struct intel_context
*intel
,
803 struct intel_mipmap_tree
*mt
,
804 struct intel_miptree_map
*map
,
808 assert(!(map
->mode
& GL_MAP_WRITE_BIT
));
810 drm_intel_bo_unmap(map
->bo
);
811 drm_intel_bo_unreference(map
->bo
);
815 intel_miptree_map_s8(struct intel_context
*intel
,
816 struct intel_mipmap_tree
*mt
,
817 struct intel_miptree_map
*map
,
818 unsigned int level
, unsigned int slice
)
820 map
->stride
= map
->w
;
821 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
825 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
826 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
827 * invalidate is set, since we'll be writing the whole rectangle from our
828 * temporary buffer back out.
830 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
831 uint8_t *untiled_s8_map
= map
->ptr
;
832 uint8_t *tiled_s8_map
= intel_region_map(intel
, mt
->region
,
834 unsigned int image_x
, image_y
;
836 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
838 for (uint32_t y
= 0; y
< map
->h
; y
++) {
839 for (uint32_t x
= 0; x
< map
->w
; x
++) {
840 ptrdiff_t offset
= intel_offset_S8(mt
->region
->pitch
,
841 x
+ image_x
+ map
->x
,
842 y
+ image_y
+ map
->y
,
843 intel
->has_swizzling
);
844 untiled_s8_map
[y
* map
->w
+ x
] = tiled_s8_map
[offset
];
848 intel_region_unmap(intel
, mt
->region
);
850 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__
,
851 map
->x
, map
->y
, map
->w
, map
->h
,
852 mt
, map
->x
+ image_x
, map
->y
+ image_y
, map
->ptr
, map
->stride
);
854 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__
,
855 map
->x
, map
->y
, map
->w
, map
->h
,
856 mt
, map
->ptr
, map
->stride
);
861 intel_miptree_unmap_s8(struct intel_context
*intel
,
862 struct intel_mipmap_tree
*mt
,
863 struct intel_miptree_map
*map
,
867 if (map
->mode
& GL_MAP_WRITE_BIT
) {
868 unsigned int image_x
, image_y
;
869 uint8_t *untiled_s8_map
= map
->ptr
;
870 uint8_t *tiled_s8_map
= intel_region_map(intel
, mt
->region
, map
->mode
);
872 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
874 for (uint32_t y
= 0; y
< map
->h
; y
++) {
875 for (uint32_t x
= 0; x
< map
->w
; x
++) {
876 ptrdiff_t offset
= intel_offset_S8(mt
->region
->pitch
,
879 intel
->has_swizzling
);
880 tiled_s8_map
[offset
] = untiled_s8_map
[y
* map
->w
+ x
];
884 intel_region_unmap(intel
, mt
->region
);
891 * Mapping function for packed depth/stencil miptrees backed by real separate
892 * miptrees for depth and stencil.
894 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
895 * separate from the depth buffer. Yet at the GL API level, we have to expose
896 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
897 * be able to map that memory for texture storage and glReadPixels-type
898 * operations. We give Mesa core that access by mallocing a temporary and
899 * copying the data between the actual backing store and the temporary.
902 intel_miptree_map_depthstencil(struct intel_context
*intel
,
903 struct intel_mipmap_tree
*mt
,
904 struct intel_miptree_map
*map
,
905 unsigned int level
, unsigned int slice
)
907 struct intel_mipmap_tree
*z_mt
= mt
;
908 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
909 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z32_FLOAT
;
910 int packed_bpp
= map_z32f_x24s8
? 8 : 4;
912 map
->stride
= map
->w
* packed_bpp
;
913 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
917 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
918 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
919 * invalidate is set, since we'll be writing the whole rectangle from our
920 * temporary buffer back out.
922 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
923 uint32_t *packed_map
= map
->ptr
;
924 uint8_t *s_map
= intel_region_map(intel
, s_mt
->region
, GL_MAP_READ_BIT
);
925 uint32_t *z_map
= intel_region_map(intel
, z_mt
->region
, GL_MAP_READ_BIT
);
926 unsigned int s_image_x
, s_image_y
;
927 unsigned int z_image_x
, z_image_y
;
929 intel_miptree_get_image_offset(s_mt
, level
, 0, slice
,
930 &s_image_x
, &s_image_y
);
931 intel_miptree_get_image_offset(z_mt
, level
, 0, slice
,
932 &z_image_x
, &z_image_y
);
934 for (uint32_t y
= 0; y
< map
->h
; y
++) {
935 for (uint32_t x
= 0; x
< map
->w
; x
++) {
936 int map_x
= map
->x
+ x
, map_y
= map
->y
+ y
;
937 ptrdiff_t s_offset
= intel_offset_S8(s_mt
->region
->pitch
,
940 intel
->has_swizzling
);
941 ptrdiff_t z_offset
= ((map_y
+ z_image_y
) * z_mt
->region
->pitch
+
942 (map_x
+ z_image_x
));
943 uint8_t s
= s_map
[s_offset
];
944 uint32_t z
= z_map
[z_offset
];
946 if (map_z32f_x24s8
) {
947 packed_map
[(y
* map
->w
+ x
) * 2 + 0] = z
;
948 packed_map
[(y
* map
->w
+ x
) * 2 + 1] = s
;
950 packed_map
[y
* map
->w
+ x
] = (s
<< 24) | (z
& 0x00ffffff);
955 intel_region_unmap(intel
, s_mt
->region
);
956 intel_region_unmap(intel
, z_mt
->region
);
958 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
960 map
->x
, map
->y
, map
->w
, map
->h
,
961 z_mt
, map
->x
+ z_image_x
, map
->y
+ z_image_y
,
962 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
963 map
->ptr
, map
->stride
);
965 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__
,
966 map
->x
, map
->y
, map
->w
, map
->h
,
967 mt
, map
->ptr
, map
->stride
);
972 intel_miptree_unmap_depthstencil(struct intel_context
*intel
,
973 struct intel_mipmap_tree
*mt
,
974 struct intel_miptree_map
*map
,
978 struct intel_mipmap_tree
*z_mt
= mt
;
979 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
980 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z32_FLOAT
;
982 if (map
->mode
& GL_MAP_WRITE_BIT
) {
983 uint32_t *packed_map
= map
->ptr
;
984 uint8_t *s_map
= intel_region_map(intel
, s_mt
->region
, map
->mode
);
985 uint32_t *z_map
= intel_region_map(intel
, z_mt
->region
, map
->mode
);
986 unsigned int s_image_x
, s_image_y
;
987 unsigned int z_image_x
, z_image_y
;
989 intel_miptree_get_image_offset(s_mt
, level
, 0, slice
,
990 &s_image_x
, &s_image_y
);
991 intel_miptree_get_image_offset(z_mt
, level
, 0, slice
,
992 &z_image_x
, &z_image_y
);
994 for (uint32_t y
= 0; y
< map
->h
; y
++) {
995 for (uint32_t x
= 0; x
< map
->w
; x
++) {
996 ptrdiff_t s_offset
= intel_offset_S8(s_mt
->region
->pitch
,
997 x
+ s_image_x
+ map
->x
,
998 y
+ s_image_y
+ map
->y
,
999 intel
->has_swizzling
);
1000 ptrdiff_t z_offset
= ((y
+ z_image_y
) * z_mt
->region
->pitch
+
1003 if (map_z32f_x24s8
) {
1004 z_map
[z_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 0];
1005 s_map
[s_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 1];
1007 uint32_t packed
= packed_map
[y
* map
->w
+ x
];
1008 s_map
[s_offset
] = packed
>> 24;
1009 z_map
[z_offset
] = packed
;
1014 intel_region_unmap(intel
, s_mt
->region
);
1015 intel_region_unmap(intel
, z_mt
->region
);
1017 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
1019 map
->x
, map
->y
, map
->w
, map
->h
,
1020 z_mt
, _mesa_get_format_name(z_mt
->format
),
1021 map
->x
+ z_image_x
, map
->y
+ z_image_y
,
1022 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
1023 map
->ptr
, map
->stride
);
1030 intel_miptree_map(struct intel_context
*intel
,
1031 struct intel_mipmap_tree
*mt
,
1042 struct intel_miptree_map
*map
;
1044 map
= calloc(1, sizeof(struct intel_miptree_map
));
1051 assert(!mt
->level
[level
].slice
[slice
].map
);
1052 mt
->level
[level
].slice
[slice
].map
= map
;
1059 intel_miptree_slice_resolve_depth(intel
, mt
, level
, slice
);
1060 if (map
->mode
& GL_MAP_WRITE_BIT
) {
1061 intel_miptree_slice_set_needs_hiz_resolve(mt
, level
, slice
);
1064 if (mt
->format
== MESA_FORMAT_S8
) {
1065 intel_miptree_map_s8(intel
, mt
, map
, level
, slice
);
1066 } else if (mt
->stencil_mt
) {
1067 intel_miptree_map_depthstencil(intel
, mt
, map
, level
, slice
);
1068 } else if (intel
->has_llc
&&
1069 !(mode
& GL_MAP_WRITE_BIT
) &&
1071 mt
->region
->tiling
== I915_TILING_X
) {
1072 intel_miptree_map_blit(intel
, mt
, map
, level
, slice
);
1074 intel_miptree_map_gtt(intel
, mt
, map
, level
, slice
);
1077 *out_ptr
= map
->ptr
;
1078 *out_stride
= map
->stride
;
1080 if (map
->ptr
== NULL
) {
1081 mt
->level
[level
].slice
[slice
].map
= NULL
;
1087 intel_miptree_unmap(struct intel_context
*intel
,
1088 struct intel_mipmap_tree
*mt
,
1092 struct intel_miptree_map
*map
= mt
->level
[level
].slice
[slice
].map
;
1097 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__
,
1098 mt
, _mesa_get_format_name(mt
->format
), level
, slice
);
1100 if (mt
->format
== MESA_FORMAT_S8
) {
1101 intel_miptree_unmap_s8(intel
, mt
, map
, level
, slice
);
1102 } else if (mt
->stencil_mt
) {
1103 intel_miptree_unmap_depthstencil(intel
, mt
, map
, level
, slice
);
1104 } else if (map
->bo
) {
1105 intel_miptree_unmap_blit(intel
, mt
, map
, level
, slice
);
1107 intel_miptree_unmap_gtt(intel
, mt
, map
, level
, slice
);
1110 mt
->level
[level
].slice
[slice
].map
= NULL
;