2 #include "main/macros.h"
3 #include "main/mtypes.h"
4 #include "main/enums.h"
5 #include "main/bufferobj.h"
6 #include "main/context.h"
7 #include "main/formats.h"
8 #include "main/glformats.h"
9 #include "main/image.h"
11 #include "main/renderbuffer.h"
12 #include "main/texcompress.h"
13 #include "main/texgetimage.h"
14 #include "main/texobj.h"
15 #include "main/teximage.h"
16 #include "main/texstore.h"
18 #include "drivers/common/meta.h"
20 #include "intel_mipmap_tree.h"
21 #include "intel_buffer_objects.h"
22 #include "intel_batchbuffer.h"
23 #include "intel_tex.h"
24 #include "intel_blit.h"
25 #include "intel_fbo.h"
26 #include "intel_image.h"
27 #include "intel_tiled_memcpy.h"
28 #include "brw_context.h"
30 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
32 /* Make sure one doesn't end up shrinking base level zero unnecessarily.
33 * Determining the base level dimension by shifting higher level dimension
34 * ends up in off-by-one value in case base level has NPOT size (for example,
36 * Choose the original base level dimension when shifted dimensions agree.
37 * Otherwise assume real resize is intended and use the new shifted value.
40 get_base_dim(unsigned old_base_dim
, unsigned new_level_dim
, unsigned level
)
42 const unsigned old_level_dim
= old_base_dim
>> level
;
43 const unsigned new_base_dim
= new_level_dim
<< level
;
45 return old_level_dim
== new_level_dim
? old_base_dim
: new_base_dim
;
48 /* Work back from the specified level of the image to the baselevel and create a
49 * miptree of that size.
51 struct intel_mipmap_tree
*
52 intel_miptree_create_for_teximage(struct brw_context
*brw
,
53 struct intel_texture_object
*intelObj
,
54 struct intel_texture_image
*intelImage
,
55 uint32_t layout_flags
)
58 int width
, height
, depth
;
59 unsigned old_width
= 0, old_height
= 0, old_depth
= 0;
60 const struct intel_mipmap_tree
*old_mt
= intelObj
->mt
;
61 const unsigned level
= intelImage
->base
.Base
.Level
;
63 intel_get_image_dims(&intelImage
->base
.Base
, &width
, &height
, &depth
);
65 if (old_mt
&& old_mt
->surf
.size
> 0) {
66 old_width
= old_mt
->surf
.logical_level0_px
.width
;
67 old_height
= old_mt
->surf
.logical_level0_px
.height
;
68 old_depth
= old_mt
->surf
.dim
== ISL_SURF_DIM_3D
?
69 old_mt
->surf
.logical_level0_px
.depth
:
70 old_mt
->surf
.logical_level0_px
.array_len
;
72 old_width
= old_mt
->logical_width0
;
73 old_height
= old_mt
->logical_height0
;
74 old_depth
= old_mt
->logical_depth0
;
77 DBG("%s\n", __func__
);
79 /* Figure out image dimensions at start level. */
80 switch(intelObj
->base
.Target
) {
81 case GL_TEXTURE_2D_MULTISAMPLE
:
82 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
83 case GL_TEXTURE_RECTANGLE
:
84 case GL_TEXTURE_EXTERNAL_OES
:
88 depth
= old_mt
? get_base_dim(old_depth
, depth
, level
) :
92 case GL_TEXTURE_2D_ARRAY
:
93 case GL_TEXTURE_CUBE_MAP
:
94 case GL_TEXTURE_CUBE_MAP_ARRAY
:
95 height
= old_mt
? get_base_dim(old_height
, height
, level
) :
99 case GL_TEXTURE_1D_ARRAY
:
100 width
= old_mt
? get_base_dim(old_width
, width
, level
) :
104 unreachable("Unexpected target");
107 /* Guess a reasonable value for lastLevel. This is probably going
108 * to be wrong fairly often and might mean that we have to look at
109 * resizable buffers, or require that buffers implement lazy
110 * pagetable arrangements.
112 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
113 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
114 intelImage
->base
.Base
.Level
== 0 &&
115 !intelObj
->base
.GenerateMipmap
) {
118 lastLevel
= _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
119 width
, height
, depth
) - 1;
122 return intel_miptree_create(brw
,
123 intelObj
->base
.Target
,
124 intelImage
->base
.Base
.TexFormat
,
130 MAX2(intelImage
->base
.Base
.NumSamples
, 1),
131 layout_flags
| MIPTREE_LAYOUT_TILING_ANY
);
135 intelTexImage(struct gl_context
* ctx
,
137 struct gl_texture_image
*texImage
,
138 GLenum format
, GLenum type
, const void *pixels
,
139 const struct gl_pixelstore_attrib
*unpack
)
141 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
144 bool tex_busy
= intelImage
->mt
&& brw_bo_busy(intelImage
->mt
->bo
);
146 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
147 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
148 _mesa_enum_to_string(texImage
->TexObject
->Target
),
149 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
150 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
152 /* Allocate storage for texture data. */
153 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
154 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
158 assert(intelImage
->mt
);
160 if (intelImage
->mt
->format
== MESA_FORMAT_S_UINT8
)
161 intelImage
->mt
->r8stencil_needs_update
= true;
163 ok
= _mesa_meta_pbo_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
164 texImage
->Width
, texImage
->Height
,
166 format
, type
, pixels
,
171 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
172 0, 0, 0, /*x,y,z offsets*/
176 format
, type
, pixels
, unpack
,
177 false /*allocate_storage*/);
181 DBG("%s: upload image %dx%dx%d pixels %p\n",
182 __func__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
185 _mesa_store_teximage(ctx
, dims
, texImage
,
186 format
, type
, pixels
, unpack
);
191 intel_set_texture_image_mt(struct brw_context
*brw
,
192 struct gl_texture_image
*image
,
193 GLenum internal_format
,
194 struct intel_mipmap_tree
*mt
)
197 struct gl_texture_object
*texobj
= image
->TexObject
;
198 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
199 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
201 if (mt
->surf
.size
> 0) {
202 _mesa_init_teximage_fields(&brw
->ctx
, image
,
203 mt
->surf
.logical_level0_px
.width
,
204 mt
->surf
.logical_level0_px
.height
, 1,
205 0, internal_format
, mt
->format
);
207 _mesa_init_teximage_fields(&brw
->ctx
, image
,
208 mt
->logical_width0
, mt
->logical_height0
, 1,
209 0, internal_format
, mt
->format
);
212 brw
->ctx
.Driver
.FreeTextureImageBuffer(&brw
->ctx
, image
);
214 intel_texobj
->needs_validate
= true;
215 intel_image
->base
.RowStride
= mt
->surf
.row_pitch
/ mt
->cpp
;
216 assert(mt
->surf
.row_pitch
% mt
->cpp
== 0);
218 intel_miptree_reference(&intel_image
->mt
, mt
);
220 /* Immediately validate the image to the object. */
221 intel_miptree_reference(&intel_texobj
->mt
, mt
);
226 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
227 GLint texture_format
,
228 __DRIdrawable
*dPriv
)
230 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
231 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
232 struct gl_context
*ctx
= &brw
->ctx
;
233 struct intel_renderbuffer
*rb
;
234 struct gl_texture_object
*texObj
;
235 struct gl_texture_image
*texImage
;
236 mesa_format texFormat
= MESA_FORMAT_NONE
;
237 struct intel_mipmap_tree
*mt
;
238 GLenum internal_format
= 0;
240 texObj
= _mesa_get_current_tex_object(ctx
, target
);
245 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
246 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
247 intel_update_renderbuffers(pDRICtx
, dPriv
);
249 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
250 /* If the miptree isn't set, then intel_update_renderbuffers was unable
251 * to get the BO for the drawable from the window system.
256 if (rb
->mt
->cpp
== 4) {
257 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
258 internal_format
= GL_RGB
;
259 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
262 internal_format
= GL_RGBA
;
263 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
265 } else if (rb
->mt
->cpp
== 2) {
266 internal_format
= GL_RGB
;
267 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
270 intel_miptree_make_shareable(brw
, rb
->mt
);
271 mt
= intel_miptree_create_for_bo(brw
, rb
->mt
->bo
, texFormat
, 0,
273 rb
->Base
.Base
.Height
,
274 1, rb
->mt
->surf
.row_pitch
, 0);
279 _mesa_lock_texture(&brw
->ctx
, texObj
);
280 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
281 intel_set_texture_image_mt(brw
, texImage
, internal_format
, mt
);
282 intel_miptree_release(&mt
);
283 _mesa_unlock_texture(&brw
->ctx
, texObj
);
287 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
288 struct gl_renderbuffer
*rb
,
289 struct gl_texture_image
*image
)
291 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
292 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
293 struct gl_texture_object
*texobj
= image
->TexObject
;
294 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
296 /* We can only handle RB allocated with AllocRenderbufferStorage, or
297 * window-system renderbuffers.
299 assert(!rb
->TexImage
);
304 _mesa_lock_texture(ctx
, texobj
);
305 _mesa_init_teximage_fields(ctx
, image
,
306 rb
->Width
, rb
->Height
, 1,
307 0, rb
->InternalFormat
, rb
->Format
);
308 image
->NumSamples
= rb
->NumSamples
;
310 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
312 /* Immediately validate the image to the object. */
313 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
315 intel_texobj
->needs_validate
= true;
316 _mesa_unlock_texture(ctx
, texobj
);
322 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
324 /* The old interface didn't have the format argument, so copy our
325 * implementation's behavior at the time.
327 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
331 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
332 struct gl_texture_object
*texObj
,
333 struct gl_texture_image
*texImage
,
334 GLeglImageOES image_handle
)
336 struct brw_context
*brw
= brw_context(ctx
);
337 struct intel_mipmap_tree
*mt
;
338 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
341 image
= dri_screen
->dri2
.image
->lookupEGLImage(dri_screen
, image_handle
,
342 dri_screen
->loaderPrivate
);
346 /* We support external textures only for EGLImages created with
347 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
349 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
350 _mesa_error(ctx
, GL_INVALID_OPERATION
,
351 "glEGLImageTargetTexture2DOES(external target is enabled only "
352 "for images created with EGL_EXT_image_dma_buf_import");
356 /* Disallow depth/stencil textures: we don't have a way to pass the
357 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
359 if (image
->has_depthstencil
) {
360 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
364 mt
= intel_miptree_create_for_dri_image(brw
, image
, target
,
365 ISL_COLORSPACE_NONE
, false);
369 struct intel_texture_object
*intel_texobj
= intel_texture_object(texObj
);
370 intel_texobj
->planar_format
= image
->planar_format
;
372 const GLenum internal_format
=
373 image
->internal_format
!= 0 ?
374 image
->internal_format
: _mesa_get_format_base_format(mt
->format
);
375 intel_set_texture_image_mt(brw
, texImage
, internal_format
, mt
);
376 intel_miptree_release(&mt
);
380 * \brief A fast path for glGetTexImage.
382 * \see intel_readpixels_tiled_memcpy()
385 intel_gettexsubimage_tiled_memcpy(struct gl_context
*ctx
,
386 struct gl_texture_image
*texImage
,
387 GLint xoffset
, GLint yoffset
,
388 GLsizei width
, GLsizei height
,
389 GLenum format
, GLenum type
,
391 const struct gl_pixelstore_attrib
*packing
)
393 struct brw_context
*brw
= brw_context(ctx
);
394 struct intel_texture_image
*image
= intel_texture_image(texImage
);
397 /* The miptree's buffer. */
401 mem_copy_fn mem_copy
= NULL
;
403 /* This fastpath is restricted to specific texture types:
404 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
407 * FINISHME: The restrictions below on packing alignment and packing row
408 * length are likely unneeded now because we calculate the destination stride
409 * with _mesa_image_row_stride. However, before removing the restrictions
413 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
414 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
415 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
417 _mesa_is_bufferobj(packing
->BufferObj
) ||
418 packing
->Alignment
> 4 ||
419 packing
->SkipPixels
> 0 ||
420 packing
->SkipRows
> 0 ||
421 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
422 packing
->SwapBytes
||
427 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
428 * function doesn't set the last channel to 1. Note this checks BaseFormat
429 * rather than TexFormat in case the RGBX format is being simulated with an
432 if (texImage
->_BaseFormat
== GL_RGB
)
435 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
))
438 /* If this is a nontrivial texture view, let another path handle it instead. */
439 if (texImage
->TexObject
->MinLayer
)
443 (image
->mt
->surf
.tiling
!= ISL_TILING_X
&&
444 image
->mt
->surf
.tiling
!= ISL_TILING_Y0
)) {
445 /* The algorithm is written only for X- or Y-tiled memory. */
449 /* tiled_to_linear() assumes that if the object is swizzled, it is using
450 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
451 * true on gen5 and above.
453 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
454 * parts of the memory aren't swizzled at all. Userspace just can't handle
457 if (brw
->gen
< 5 && brw
->has_swizzling
)
460 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
462 /* Since we are going to write raw data to the miptree, we need to resolve
463 * any pending fast color clears before we start.
465 if (image
->mt
->surf
.size
> 0) {
466 assert(image
->mt
->surf
.logical_level0_px
.depth
== 1);
467 assert(image
->mt
->surf
.logical_level0_px
.array_len
== 1);
469 assert(image
->mt
->logical_depth0
== 1);
471 intel_miptree_access_raw(brw
, image
->mt
, level
, 0, true);
475 if (brw_batch_references(&brw
->batch
, bo
)) {
476 perf_debug("Flushing before mapping a referenced bo.\n");
477 intel_batchbuffer_flush(brw
);
480 void *map
= brw_bo_map(brw
, bo
, MAP_READ
| MAP_RAW
);
482 DBG("%s: failed to map bo\n", __func__
);
486 dst_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
488 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
489 "mesa_format=0x%x tiling=%d "
490 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
491 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
492 format
, type
, texImage
->TexFormat
, image
->mt
->surf
.tiling
,
493 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
496 /* Adjust x and y offset based on miplevel */
497 unsigned level_x
, level_y
;
498 intel_miptree_get_image_offset(image
->mt
, level
, 0, &level_x
, &level_y
);
503 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
504 yoffset
, yoffset
+ height
,
505 pixels
- (ptrdiff_t) yoffset
* dst_pitch
- (ptrdiff_t) xoffset
* cpp
,
507 dst_pitch
, image
->mt
->surf
.row_pitch
,
509 image
->mt
->surf
.tiling
,
518 intel_get_tex_sub_image(struct gl_context
*ctx
,
519 GLint xoffset
, GLint yoffset
, GLint zoffset
,
520 GLsizei width
, GLsizei height
, GLint depth
,
521 GLenum format
, GLenum type
, GLvoid
*pixels
,
522 struct gl_texture_image
*texImage
)
524 struct brw_context
*brw
= brw_context(ctx
);
527 DBG("%s\n", __func__
);
529 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
530 if (_mesa_meta_pbo_GetTexSubImage(ctx
, 3, texImage
,
531 xoffset
, yoffset
, zoffset
,
532 width
, height
, depth
, format
, type
,
533 pixels
, &ctx
->Pack
)) {
534 /* Flush to guarantee coherency between the render cache and other
535 * caches the PBO could potentially be bound to after this point.
536 * See the related comment in intelReadPixels() for a more detailed
539 brw_emit_mi_flush(brw
);
543 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__
);
546 ok
= intel_gettexsubimage_tiled_memcpy(ctx
, texImage
, xoffset
, yoffset
,
548 format
, type
, pixels
, &ctx
->Pack
);
553 _mesa_meta_GetTexSubImage(ctx
, xoffset
, yoffset
, zoffset
,
554 width
, height
, depth
,
555 format
, type
, pixels
, texImage
);
557 DBG("%s - DONE\n", __func__
);
561 flush_astc_denorms(struct gl_context
*ctx
, GLuint dims
,
562 struct gl_texture_image
*texImage
,
563 GLint xoffset
, GLint yoffset
, GLint zoffset
,
564 GLsizei width
, GLsizei height
, GLsizei depth
)
566 struct compressed_pixelstore store
;
567 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
,
568 width
, height
, depth
,
569 &ctx
->Unpack
, &store
);
571 for (int slice
= 0; slice
< store
.CopySlices
; slice
++) {
573 /* Map dest texture buffer */
576 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
+ zoffset
,
577 xoffset
, yoffset
, width
, height
,
578 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
579 &dstMap
, &dstRowStride
);
583 for (int i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
585 /* An ASTC block is stored in little endian mode. The byte that
586 * contains bits 0..7 is stored at the lower address in memory.
588 struct astc_void_extent
{
589 uint16_t header
: 12;
590 uint16_t dontcare
[3];
595 } *blocks
= (struct astc_void_extent
*) dstMap
;
597 /* Iterate over every copied block in the row */
598 for (int j
= 0; j
< store
.CopyBytesPerRow
/ 16; j
++) {
600 /* Check if the header matches that of an LDR void-extent block */
601 if (blocks
[j
].header
== 0xDFC) {
603 /* Flush UNORM16 values that would be denormalized */
604 if (blocks
[j
].A
< 4) blocks
[j
].A
= 0;
605 if (blocks
[j
].B
< 4) blocks
[j
].B
= 0;
606 if (blocks
[j
].G
< 4) blocks
[j
].G
= 0;
607 if (blocks
[j
].R
< 4) blocks
[j
].R
= 0;
611 dstMap
+= dstRowStride
;
614 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ zoffset
);
620 intelCompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
621 struct gl_texture_image
*texImage
,
622 GLint xoffset
, GLint yoffset
, GLint zoffset
,
623 GLsizei width
, GLsizei height
, GLsizei depth
,
625 GLsizei imageSize
, const GLvoid
*data
)
627 /* Upload the compressed data blocks */
628 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
629 xoffset
, yoffset
, zoffset
,
630 width
, height
, depth
,
631 format
, imageSize
, data
);
633 /* Fix up copied ASTC blocks if necessary */
634 GLenum gl_format
= _mesa_compressed_format_to_glenum(ctx
,
635 texImage
->TexFormat
);
636 bool is_linear_astc
= _mesa_is_astc_format(gl_format
) &&
637 !_mesa_is_srgb_format(gl_format
);
638 struct brw_context
*brw
= (struct brw_context
*) ctx
;
639 if (brw
->gen
== 9 && is_linear_astc
)
640 flush_astc_denorms(ctx
, dims
, texImage
,
641 xoffset
, yoffset
, zoffset
,
642 width
, height
, depth
);
646 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
648 functions
->TexImage
= intelTexImage
;
649 functions
->CompressedTexSubImage
= intelCompressedTexSubImage
;
650 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
651 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
652 functions
->GetTexSubImage
= intel_get_tex_sub_image
;