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 /* Work back from the specified level of the image to the baselevel and create a
33 * miptree of that size.
35 struct intel_mipmap_tree
*
36 intel_miptree_create_for_teximage(struct brw_context
*brw
,
37 struct intel_texture_object
*intelObj
,
38 struct intel_texture_image
*intelImage
,
39 uint32_t layout_flags
)
42 int width
, height
, depth
;
45 intel_get_image_dims(&intelImage
->base
.Base
, &width
, &height
, &depth
);
47 DBG("%s\n", __func__
);
49 /* Figure out image dimensions at start level. */
50 for (i
= intelImage
->base
.Base
.Level
; i
> 0; i
--) {
54 if (intelObj
->base
.Target
== GL_TEXTURE_3D
)
58 /* Guess a reasonable value for lastLevel. This is probably going
59 * to be wrong fairly often and might mean that we have to look at
60 * resizable buffers, or require that buffers implement lazy
61 * pagetable arrangements.
63 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
64 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
65 intelImage
->base
.Base
.Level
== 0 &&
66 !intelObj
->base
.GenerateMipmap
) {
69 lastLevel
= _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
70 width
, height
, depth
) - 1;
73 return intel_miptree_create(brw
,
74 intelObj
->base
.Target
,
75 intelImage
->base
.Base
.TexFormat
,
81 intelImage
->base
.Base
.NumSamples
,
82 layout_flags
| MIPTREE_LAYOUT_TILING_ANY
);
86 intelTexImage(struct gl_context
* ctx
,
88 struct gl_texture_image
*texImage
,
89 GLenum format
, GLenum type
, const void *pixels
,
90 const struct gl_pixelstore_attrib
*unpack
)
92 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
95 bool tex_busy
= intelImage
->mt
&& drm_intel_bo_busy(intelImage
->mt
->bo
);
97 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
98 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
99 _mesa_enum_to_string(texImage
->TexObject
->Target
),
100 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
101 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
103 /* Allocate storage for texture data. */
104 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
105 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
109 assert(intelImage
->mt
);
111 ok
= _mesa_meta_pbo_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
112 texImage
->Width
, texImage
->Height
,
114 format
, type
, pixels
,
119 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
120 0, 0, 0, /*x,y,z offsets*/
124 format
, type
, pixels
, unpack
,
125 false /*allocate_storage*/);
129 DBG("%s: upload image %dx%dx%d pixels %p\n",
130 __func__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
133 _mesa_store_teximage(ctx
, dims
, texImage
,
134 format
, type
, pixels
, unpack
);
139 intel_set_texture_image_mt(struct brw_context
*brw
,
140 struct gl_texture_image
*image
,
141 struct intel_mipmap_tree
*mt
)
144 const uint32_t internal_format
= _mesa_get_format_base_format(mt
->format
);
145 struct gl_texture_object
*texobj
= image
->TexObject
;
146 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
147 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
149 _mesa_init_teximage_fields(&brw
->ctx
, image
,
150 mt
->logical_width0
, mt
->logical_height0
, 1,
151 0, internal_format
, mt
->format
);
153 brw
->ctx
.Driver
.FreeTextureImageBuffer(&brw
->ctx
, image
);
155 intel_texobj
->needs_validate
= true;
156 intel_image
->base
.RowStride
= mt
->pitch
/ mt
->cpp
;
157 assert(mt
->pitch
% mt
->cpp
== 0);
159 intel_miptree_reference(&intel_image
->mt
, mt
);
161 /* Immediately validate the image to the object. */
162 intel_miptree_reference(&intel_texobj
->mt
, mt
);
165 static struct intel_mipmap_tree
*
166 create_mt_for_planar_dri_image(struct brw_context
*brw
,
167 GLenum target
, __DRIimage
*image
)
169 struct intel_image_format
*f
= image
->planar_format
;
170 struct intel_mipmap_tree
*planar_mt
;
172 for (int i
= 0; i
< f
->nplanes
; i
++) {
173 const int index
= f
->planes
[i
].buffer_index
;
174 const uint32_t dri_format
= f
->planes
[i
].dri_format
;
175 const mesa_format format
= driImageFormatToGLFormat(dri_format
);
176 const uint32_t width
= image
->width
>> f
->planes
[i
].width_shift
;
177 const uint32_t height
= image
->height
>> f
->planes
[i
].height_shift
;
179 /* Disable creation of the texture's aux buffers because the driver
180 * exposes no EGL API to manage them. That is, there is no API for
181 * resolving the aux buffer's content to the main buffer nor for
182 * invalidating the aux buffer's content.
184 struct intel_mipmap_tree
*mt
=
185 intel_miptree_create_for_bo(brw
, image
->bo
, format
,
186 image
->offsets
[index
],
188 image
->strides
[index
],
189 MIPTREE_LAYOUT_DISABLE_AUX
);
194 mt
->total_width
= width
;
195 mt
->total_height
= height
;
200 planar_mt
->plane
[i
- 1] = mt
;
207 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
209 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
211 static struct intel_mipmap_tree
*
212 create_mt_for_dri_image(struct brw_context
*brw
,
213 GLenum target
, __DRIimage
*image
)
215 struct intel_mipmap_tree
*mt
;
216 uint32_t draw_x
, draw_y
;
218 /* Disable creation of the texture's aux buffers because the driver exposes
219 * no EGL API to manage them. That is, there is no API for resolving the aux
220 * buffer's content to the main buffer nor for invalidating the aux buffer's
223 mt
= intel_miptree_create_for_bo(brw
, image
->bo
, image
->format
,
224 0, image
->width
, image
->height
, 1,
226 MIPTREE_LAYOUT_DISABLE_AUX
);
231 mt
->total_width
= image
->width
;
232 mt
->total_height
= image
->height
;
233 mt
->level
[0].slice
[0].x_offset
= image
->tile_x
;
234 mt
->level
[0].slice
[0].y_offset
= image
->tile_y
;
236 intel_miptree_get_tile_offsets(mt
, 0, 0, &draw_x
, &draw_y
);
238 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
239 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
240 * trouble resolving back to destination image due to alignment issues.
242 if (!brw
->has_surface_tile_offset
&&
243 (draw_x
!= 0 || draw_y
!= 0)) {
244 _mesa_error(&brw
->ctx
, GL_INVALID_OPERATION
, __func__
);
245 intel_miptree_release(&mt
);
249 mt
->offset
= image
->offset
;
255 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
256 GLint texture_format
,
257 __DRIdrawable
*dPriv
)
259 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
260 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
261 struct gl_context
*ctx
= &brw
->ctx
;
262 struct intel_renderbuffer
*rb
;
263 struct gl_texture_object
*texObj
;
264 struct gl_texture_image
*texImage
;
265 mesa_format texFormat
= MESA_FORMAT_NONE
;
266 struct intel_mipmap_tree
*mt
;
268 texObj
= _mesa_get_current_tex_object(ctx
, target
);
273 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
274 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
275 intel_update_renderbuffers(pDRICtx
, dPriv
);
277 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
278 /* If the miptree isn't set, then intel_update_renderbuffers was unable
279 * to get the BO for the drawable from the window system.
284 if (rb
->mt
->cpp
== 4) {
285 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
286 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
289 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
291 } else if (rb
->mt
->cpp
== 2) {
292 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
295 intel_miptree_make_shareable(brw
, rb
->mt
);
296 mt
= intel_miptree_create_for_bo(brw
, rb
->mt
->bo
, texFormat
, 0,
298 rb
->Base
.Base
.Height
,
299 1, rb
->mt
->pitch
, 0);
303 mt
->total_width
= rb
->Base
.Base
.Width
;
304 mt
->total_height
= rb
->Base
.Base
.Height
;
306 _mesa_lock_texture(&brw
->ctx
, texObj
);
307 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
308 intel_set_texture_image_mt(brw
, texImage
, mt
);
309 intel_miptree_release(&mt
);
310 _mesa_unlock_texture(&brw
->ctx
, texObj
);
314 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
315 struct gl_renderbuffer
*rb
,
316 struct gl_texture_image
*image
)
318 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
319 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
320 struct gl_texture_object
*texobj
= image
->TexObject
;
321 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
323 /* We can only handle RB allocated with AllocRenderbufferStorage, or
324 * window-system renderbuffers.
326 assert(!rb
->TexImage
);
331 _mesa_lock_texture(ctx
, texobj
);
332 _mesa_init_teximage_fields(ctx
, image
,
333 rb
->Width
, rb
->Height
, 1,
334 0, rb
->InternalFormat
, rb
->Format
);
335 image
->NumSamples
= rb
->NumSamples
;
337 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
339 /* Immediately validate the image to the object. */
340 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
342 intel_texobj
->needs_validate
= true;
343 _mesa_unlock_texture(ctx
, texobj
);
349 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
351 /* The old interface didn't have the format argument, so copy our
352 * implementation's behavior at the time.
354 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
358 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
359 struct gl_texture_object
*texObj
,
360 struct gl_texture_image
*texImage
,
361 GLeglImageOES image_handle
)
363 struct brw_context
*brw
= brw_context(ctx
);
364 struct intel_mipmap_tree
*mt
;
368 screen
= brw
->intelScreen
->driScrnPriv
;
369 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
370 screen
->loaderPrivate
);
374 /* We support external textures only for EGLImages created with
375 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
377 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
379 "glEGLImageTargetTexture2DOES(external target is enabled only "
380 "for images created with EGL_EXT_image_dma_buf_import");
384 /* Disallow depth/stencil textures: we don't have a way to pass the
385 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
387 if (image
->has_depthstencil
) {
388 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
392 if (image
->planar_format
&& image
->planar_format
->nplanes
> 0)
393 mt
= create_mt_for_planar_dri_image(brw
, target
, image
);
395 mt
= create_mt_for_dri_image(brw
, target
, image
);
399 struct intel_texture_object
*intel_texobj
= intel_texture_object(texObj
);
400 intel_texobj
->planar_format
= image
->planar_format
;
402 intel_set_texture_image_mt(brw
, texImage
, mt
);
403 intel_miptree_release(&mt
);
407 * \brief A fast path for glGetTexImage.
409 * \see intel_readpixels_tiled_memcpy()
412 intel_gettexsubimage_tiled_memcpy(struct gl_context
*ctx
,
413 struct gl_texture_image
*texImage
,
414 GLint xoffset
, GLint yoffset
,
415 GLsizei width
, GLsizei height
,
416 GLenum format
, GLenum type
,
418 const struct gl_pixelstore_attrib
*packing
)
420 struct brw_context
*brw
= brw_context(ctx
);
421 struct intel_texture_image
*image
= intel_texture_image(texImage
);
424 /* The miptree's buffer. */
430 mem_copy_fn mem_copy
= NULL
;
432 /* This fastpath is restricted to specific texture types:
433 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
436 * FINISHME: The restrictions below on packing alignment and packing row
437 * length are likely unneeded now because we calculate the destination stride
438 * with _mesa_image_row_stride. However, before removing the restrictions
442 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
443 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
444 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
446 _mesa_is_bufferobj(packing
->BufferObj
) ||
447 packing
->Alignment
> 4 ||
448 packing
->SkipPixels
> 0 ||
449 packing
->SkipRows
> 0 ||
450 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
451 packing
->SwapBytes
||
456 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
457 * function doesn't set the last channel to 1. Note this checks BaseFormat
458 * rather than TexFormat in case the RGBX format is being simulated with an
461 if (texImage
->_BaseFormat
== GL_RGB
)
464 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
))
467 /* If this is a nontrivial texture view, let another path handle it instead. */
468 if (texImage
->TexObject
->MinLayer
)
472 (image
->mt
->tiling
!= I915_TILING_X
&&
473 image
->mt
->tiling
!= I915_TILING_Y
)) {
474 /* The algorithm is written only for X- or Y-tiled memory. */
478 /* Since we are going to write raw data to the miptree, we need to resolve
479 * any pending fast color clears before we start.
481 intel_miptree_resolve_color(brw
, image
->mt
, 0);
485 if (drm_intel_bo_references(brw
->batch
.bo
, bo
)) {
486 perf_debug("Flushing before mapping a referenced bo.\n");
487 intel_batchbuffer_flush(brw
);
490 error
= brw_bo_map(brw
, bo
, false /* write enable */, "miptree");
492 DBG("%s: failed to map bo\n", __func__
);
496 dst_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
498 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
499 "mesa_format=0x%x tiling=%d "
500 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
501 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
502 format
, type
, texImage
->TexFormat
, image
->mt
->tiling
,
503 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
506 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
508 /* Adjust x and y offset based on miplevel */
509 xoffset
+= image
->mt
->level
[level
].level_x
;
510 yoffset
+= image
->mt
->level
[level
].level_y
;
513 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
514 yoffset
, yoffset
+ height
,
515 pixels
- (ptrdiff_t) yoffset
* dst_pitch
- (ptrdiff_t) xoffset
* cpp
,
517 dst_pitch
, image
->mt
->pitch
,
523 drm_intel_bo_unmap(bo
);
528 intel_get_tex_sub_image(struct gl_context
*ctx
,
529 GLint xoffset
, GLint yoffset
, GLint zoffset
,
530 GLsizei width
, GLsizei height
, GLint depth
,
531 GLenum format
, GLenum type
, GLvoid
*pixels
,
532 struct gl_texture_image
*texImage
)
534 struct brw_context
*brw
= brw_context(ctx
);
537 DBG("%s\n", __func__
);
539 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
540 if (_mesa_meta_pbo_GetTexSubImage(ctx
, 3, texImage
,
541 xoffset
, yoffset
, zoffset
,
542 width
, height
, depth
, format
, type
,
543 pixels
, &ctx
->Pack
)) {
544 /* Flush to guarantee coherency between the render cache and other
545 * caches the PBO could potentially be bound to after this point.
546 * See the related comment in intelReadPixels() for a more detailed
549 brw_emit_mi_flush(brw
);
553 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__
);
556 ok
= intel_gettexsubimage_tiled_memcpy(ctx
, texImage
, xoffset
, yoffset
,
558 format
, type
, pixels
, &ctx
->Pack
);
563 _mesa_meta_GetTexSubImage(ctx
, xoffset
, yoffset
, zoffset
,
564 width
, height
, depth
,
565 format
, type
, pixels
, texImage
);
567 DBG("%s - DONE\n", __func__
);
571 flush_astc_denorms(struct gl_context
*ctx
, GLuint dims
,
572 struct gl_texture_image
*texImage
,
573 GLint xoffset
, GLint yoffset
, GLint zoffset
,
574 GLsizei width
, GLsizei height
, GLsizei depth
)
576 struct compressed_pixelstore store
;
577 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
,
578 width
, height
, depth
,
579 &ctx
->Unpack
, &store
);
581 for (int slice
= 0; slice
< store
.CopySlices
; slice
++) {
583 /* Map dest texture buffer */
586 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
+ zoffset
,
587 xoffset
, yoffset
, width
, height
,
588 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
589 &dstMap
, &dstRowStride
);
593 for (int i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
595 /* An ASTC block is stored in little endian mode. The byte that
596 * contains bits 0..7 is stored at the lower address in memory.
598 struct astc_void_extent
{
599 uint16_t header
: 12;
600 uint16_t dontcare
[3];
605 } *blocks
= (struct astc_void_extent
*) dstMap
;
607 /* Iterate over every copied block in the row */
608 for (int j
= 0; j
< store
.CopyBytesPerRow
/ 16; j
++) {
610 /* Check if the header matches that of an LDR void-extent block */
611 if (blocks
[j
].header
== 0xDFC) {
613 /* Flush UNORM16 values that would be denormalized */
614 if (blocks
[j
].A
< 4) blocks
[j
].A
= 0;
615 if (blocks
[j
].B
< 4) blocks
[j
].B
= 0;
616 if (blocks
[j
].G
< 4) blocks
[j
].G
= 0;
617 if (blocks
[j
].R
< 4) blocks
[j
].R
= 0;
621 dstMap
+= dstRowStride
;
624 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ zoffset
);
630 intelCompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
631 struct gl_texture_image
*texImage
,
632 GLint xoffset
, GLint yoffset
, GLint zoffset
,
633 GLsizei width
, GLsizei height
, GLsizei depth
,
635 GLsizei imageSize
, const GLvoid
*data
)
637 /* Upload the compressed data blocks */
638 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
639 xoffset
, yoffset
, zoffset
,
640 width
, height
, depth
,
641 format
, imageSize
, data
);
643 /* Fix up copied ASTC blocks if necessary */
644 GLenum gl_format
= _mesa_compressed_format_to_glenum(ctx
,
645 texImage
->TexFormat
);
646 bool is_linear_astc
= _mesa_is_astc_format(gl_format
) &&
647 !_mesa_is_srgb_format(gl_format
);
648 struct brw_context
*brw
= (struct brw_context
*) ctx
;
649 if (brw
->gen
== 9 && is_linear_astc
)
650 flush_astc_denorms(ctx
, dims
, texImage
,
651 xoffset
, yoffset
, zoffset
,
652 width
, height
, depth
);
656 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
658 functions
->TexImage
= intelTexImage
;
659 functions
->CompressedTexSubImage
= intelCompressedTexSubImage
;
660 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
661 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
662 functions
->GetTexSubImage
= intel_get_tex_sub_image
;