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
;
44 intel_get_image_dims(&intelImage
->base
.Base
, &width
, &height
, &depth
);
46 DBG("%s\n", __func__
);
48 /* Figure out image dimensions at start level. */
49 switch(intelObj
->base
.Target
) {
50 case GL_TEXTURE_2D_MULTISAMPLE
:
51 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
52 case GL_TEXTURE_RECTANGLE
:
53 case GL_TEXTURE_EXTERNAL_OES
:
54 assert(intelImage
->base
.Base
.Level
== 0);
57 depth
<<= intelImage
->base
.Base
.Level
;
60 case GL_TEXTURE_2D_ARRAY
:
61 case GL_TEXTURE_CUBE_MAP
:
62 case GL_TEXTURE_CUBE_MAP_ARRAY
:
63 height
<<= intelImage
->base
.Base
.Level
;
66 case GL_TEXTURE_1D_ARRAY
:
67 width
<<= intelImage
->base
.Base
.Level
;
70 unreachable("Unexpected target");
73 /* Guess a reasonable value for lastLevel. This is probably going
74 * to be wrong fairly often and might mean that we have to look at
75 * resizable buffers, or require that buffers implement lazy
76 * pagetable arrangements.
78 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
79 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
80 intelImage
->base
.Base
.Level
== 0 &&
81 !intelObj
->base
.GenerateMipmap
) {
84 lastLevel
= _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
85 width
, height
, depth
) - 1;
88 return intel_miptree_create(brw
,
89 intelObj
->base
.Target
,
90 intelImage
->base
.Base
.TexFormat
,
96 intelImage
->base
.Base
.NumSamples
,
97 layout_flags
| MIPTREE_LAYOUT_TILING_ANY
);
101 intelTexImage(struct gl_context
* ctx
,
103 struct gl_texture_image
*texImage
,
104 GLenum format
, GLenum type
, const void *pixels
,
105 const struct gl_pixelstore_attrib
*unpack
)
107 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
110 bool tex_busy
= intelImage
->mt
&& drm_intel_bo_busy(intelImage
->mt
->bo
);
112 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
113 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
114 _mesa_enum_to_string(texImage
->TexObject
->Target
),
115 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
116 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
118 /* Allocate storage for texture data. */
119 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
120 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
124 assert(intelImage
->mt
);
126 if (intelImage
->mt
->format
== MESA_FORMAT_S_UINT8
)
127 intelImage
->mt
->r8stencil_needs_update
= true;
129 ok
= _mesa_meta_pbo_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
130 texImage
->Width
, texImage
->Height
,
132 format
, type
, pixels
,
137 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
138 0, 0, 0, /*x,y,z offsets*/
142 format
, type
, pixels
, unpack
,
143 false /*allocate_storage*/);
147 DBG("%s: upload image %dx%dx%d pixels %p\n",
148 __func__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
151 _mesa_store_teximage(ctx
, dims
, texImage
,
152 format
, type
, pixels
, unpack
);
157 intel_set_texture_image_mt(struct brw_context
*brw
,
158 struct gl_texture_image
*image
,
159 GLenum internal_format
,
160 struct intel_mipmap_tree
*mt
)
163 struct gl_texture_object
*texobj
= image
->TexObject
;
164 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
165 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
167 _mesa_init_teximage_fields(&brw
->ctx
, image
,
168 mt
->logical_width0
, mt
->logical_height0
, 1,
169 0, internal_format
, mt
->format
);
171 brw
->ctx
.Driver
.FreeTextureImageBuffer(&brw
->ctx
, image
);
173 intel_texobj
->needs_validate
= true;
174 intel_image
->base
.RowStride
= mt
->pitch
/ mt
->cpp
;
175 assert(mt
->pitch
% mt
->cpp
== 0);
177 intel_miptree_reference(&intel_image
->mt
, mt
);
179 /* Immediately validate the image to the object. */
180 intel_miptree_reference(&intel_texobj
->mt
, mt
);
183 static struct intel_mipmap_tree
*
184 create_mt_for_planar_dri_image(struct brw_context
*brw
,
185 GLenum target
, __DRIimage
*image
)
187 struct intel_image_format
*f
= image
->planar_format
;
188 struct intel_mipmap_tree
*planar_mt
;
190 for (int i
= 0; i
< f
->nplanes
; i
++) {
191 const int index
= f
->planes
[i
].buffer_index
;
192 const uint32_t dri_format
= f
->planes
[i
].dri_format
;
193 const mesa_format format
= driImageFormatToGLFormat(dri_format
);
194 const uint32_t width
= image
->width
>> f
->planes
[i
].width_shift
;
195 const uint32_t height
= image
->height
>> f
->planes
[i
].height_shift
;
197 /* Disable creation of the texture's aux buffers because the driver
198 * exposes no EGL API to manage them. That is, there is no API for
199 * resolving the aux buffer's content to the main buffer nor for
200 * invalidating the aux buffer's content.
202 struct intel_mipmap_tree
*mt
=
203 intel_miptree_create_for_bo(brw
, image
->bo
, format
,
204 image
->offsets
[index
],
206 image
->strides
[index
],
207 MIPTREE_LAYOUT_DISABLE_AUX
);
212 mt
->total_width
= width
;
213 mt
->total_height
= height
;
218 planar_mt
->plane
[i
- 1] = mt
;
225 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
227 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
229 static struct intel_mipmap_tree
*
230 create_mt_for_dri_image(struct brw_context
*brw
,
231 GLenum target
, __DRIimage
*image
)
233 struct intel_mipmap_tree
*mt
;
234 uint32_t draw_x
, draw_y
;
236 /* Disable creation of the texture's aux buffers because the driver exposes
237 * no EGL API to manage them. That is, there is no API for resolving the aux
238 * buffer's content to the main buffer nor for invalidating the aux buffer's
241 mt
= intel_miptree_create_for_bo(brw
, image
->bo
, image
->format
,
242 0, image
->width
, image
->height
, 1,
244 MIPTREE_LAYOUT_DISABLE_AUX
);
249 mt
->total_width
= image
->width
;
250 mt
->total_height
= image
->height
;
251 mt
->level
[0].slice
[0].x_offset
= image
->tile_x
;
252 mt
->level
[0].slice
[0].y_offset
= image
->tile_y
;
254 intel_miptree_get_tile_offsets(mt
, 0, 0, &draw_x
, &draw_y
);
256 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
257 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
258 * trouble resolving back to destination image due to alignment issues.
260 if (!brw
->has_surface_tile_offset
&&
261 (draw_x
!= 0 || draw_y
!= 0)) {
262 _mesa_error(&brw
->ctx
, GL_INVALID_OPERATION
, __func__
);
263 intel_miptree_release(&mt
);
267 mt
->offset
= image
->offset
;
273 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
274 GLint texture_format
,
275 __DRIdrawable
*dPriv
)
277 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
278 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
279 struct gl_context
*ctx
= &brw
->ctx
;
280 struct intel_renderbuffer
*rb
;
281 struct gl_texture_object
*texObj
;
282 struct gl_texture_image
*texImage
;
283 mesa_format texFormat
= MESA_FORMAT_NONE
;
284 struct intel_mipmap_tree
*mt
;
285 GLenum internal_format
= 0;
287 texObj
= _mesa_get_current_tex_object(ctx
, target
);
292 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
293 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
294 intel_update_renderbuffers(pDRICtx
, dPriv
);
296 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
297 /* If the miptree isn't set, then intel_update_renderbuffers was unable
298 * to get the BO for the drawable from the window system.
303 if (rb
->mt
->cpp
== 4) {
304 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
305 internal_format
= GL_RGB
;
306 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
309 internal_format
= GL_RGBA
;
310 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
312 } else if (rb
->mt
->cpp
== 2) {
313 internal_format
= GL_RGB
;
314 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
317 intel_miptree_make_shareable(brw
, rb
->mt
);
318 mt
= intel_miptree_create_for_bo(brw
, rb
->mt
->bo
, texFormat
, 0,
320 rb
->Base
.Base
.Height
,
321 1, rb
->mt
->pitch
, 0);
325 mt
->total_width
= rb
->Base
.Base
.Width
;
326 mt
->total_height
= rb
->Base
.Base
.Height
;
328 _mesa_lock_texture(&brw
->ctx
, texObj
);
329 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
330 intel_set_texture_image_mt(brw
, texImage
, internal_format
, mt
);
331 intel_miptree_release(&mt
);
332 _mesa_unlock_texture(&brw
->ctx
, texObj
);
336 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
337 struct gl_renderbuffer
*rb
,
338 struct gl_texture_image
*image
)
340 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
341 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
342 struct gl_texture_object
*texobj
= image
->TexObject
;
343 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
345 /* We can only handle RB allocated with AllocRenderbufferStorage, or
346 * window-system renderbuffers.
348 assert(!rb
->TexImage
);
353 _mesa_lock_texture(ctx
, texobj
);
354 _mesa_init_teximage_fields(ctx
, image
,
355 rb
->Width
, rb
->Height
, 1,
356 0, rb
->InternalFormat
, rb
->Format
);
357 image
->NumSamples
= rb
->NumSamples
;
359 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
361 /* Immediately validate the image to the object. */
362 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
364 intel_texobj
->needs_validate
= true;
365 _mesa_unlock_texture(ctx
, texobj
);
371 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
373 /* The old interface didn't have the format argument, so copy our
374 * implementation's behavior at the time.
376 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
380 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
381 struct gl_texture_object
*texObj
,
382 struct gl_texture_image
*texImage
,
383 GLeglImageOES image_handle
)
385 struct brw_context
*brw
= brw_context(ctx
);
386 struct intel_mipmap_tree
*mt
;
387 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
390 image
= dri_screen
->dri2
.image
->lookupEGLImage(dri_screen
, image_handle
,
391 dri_screen
->loaderPrivate
);
395 /* We support external textures only for EGLImages created with
396 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
398 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
400 "glEGLImageTargetTexture2DOES(external target is enabled only "
401 "for images created with EGL_EXT_image_dma_buf_import");
405 /* Disallow depth/stencil textures: we don't have a way to pass the
406 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
408 if (image
->has_depthstencil
) {
409 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
413 if (image
->planar_format
&& image
->planar_format
->nplanes
> 0)
414 mt
= create_mt_for_planar_dri_image(brw
, target
, image
);
416 mt
= create_mt_for_dri_image(brw
, target
, image
);
420 struct intel_texture_object
*intel_texobj
= intel_texture_object(texObj
);
421 intel_texobj
->planar_format
= image
->planar_format
;
423 const GLenum internal_format
=
424 image
->internal_format
!= 0 ?
425 image
->internal_format
: _mesa_get_format_base_format(mt
->format
);
426 intel_set_texture_image_mt(brw
, texImage
, internal_format
, mt
);
427 intel_miptree_release(&mt
);
431 * \brief A fast path for glGetTexImage.
433 * \see intel_readpixels_tiled_memcpy()
436 intel_gettexsubimage_tiled_memcpy(struct gl_context
*ctx
,
437 struct gl_texture_image
*texImage
,
438 GLint xoffset
, GLint yoffset
,
439 GLsizei width
, GLsizei height
,
440 GLenum format
, GLenum type
,
442 const struct gl_pixelstore_attrib
*packing
)
444 struct brw_context
*brw
= brw_context(ctx
);
445 struct intel_texture_image
*image
= intel_texture_image(texImage
);
448 /* The miptree's buffer. */
454 mem_copy_fn mem_copy
= NULL
;
456 /* This fastpath is restricted to specific texture types:
457 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
460 * FINISHME: The restrictions below on packing alignment and packing row
461 * length are likely unneeded now because we calculate the destination stride
462 * with _mesa_image_row_stride. However, before removing the restrictions
466 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
467 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
468 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
470 _mesa_is_bufferobj(packing
->BufferObj
) ||
471 packing
->Alignment
> 4 ||
472 packing
->SkipPixels
> 0 ||
473 packing
->SkipRows
> 0 ||
474 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
475 packing
->SwapBytes
||
480 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
481 * function doesn't set the last channel to 1. Note this checks BaseFormat
482 * rather than TexFormat in case the RGBX format is being simulated with an
485 if (texImage
->_BaseFormat
== GL_RGB
)
488 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
))
491 /* If this is a nontrivial texture view, let another path handle it instead. */
492 if (texImage
->TexObject
->MinLayer
)
496 (image
->mt
->tiling
!= I915_TILING_X
&&
497 image
->mt
->tiling
!= I915_TILING_Y
)) {
498 /* The algorithm is written only for X- or Y-tiled memory. */
502 /* Since we are going to write raw data to the miptree, we need to resolve
503 * any pending fast color clears before we start.
505 intel_miptree_resolve_color(brw
, image
->mt
, 0);
509 if (drm_intel_bo_references(brw
->batch
.bo
, bo
)) {
510 perf_debug("Flushing before mapping a referenced bo.\n");
511 intel_batchbuffer_flush(brw
);
514 error
= brw_bo_map(brw
, bo
, false /* write enable */, "miptree");
516 DBG("%s: failed to map bo\n", __func__
);
520 dst_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
522 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
523 "mesa_format=0x%x tiling=%d "
524 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
525 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
526 format
, type
, texImage
->TexFormat
, image
->mt
->tiling
,
527 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
530 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
532 /* Adjust x and y offset based on miplevel */
533 xoffset
+= image
->mt
->level
[level
].level_x
;
534 yoffset
+= image
->mt
->level
[level
].level_y
;
537 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
538 yoffset
, yoffset
+ height
,
539 pixels
- (ptrdiff_t) yoffset
* dst_pitch
- (ptrdiff_t) xoffset
* cpp
,
541 dst_pitch
, image
->mt
->pitch
,
547 drm_intel_bo_unmap(bo
);
552 intel_get_tex_sub_image(struct gl_context
*ctx
,
553 GLint xoffset
, GLint yoffset
, GLint zoffset
,
554 GLsizei width
, GLsizei height
, GLint depth
,
555 GLenum format
, GLenum type
, GLvoid
*pixels
,
556 struct gl_texture_image
*texImage
)
558 struct brw_context
*brw
= brw_context(ctx
);
561 DBG("%s\n", __func__
);
563 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
564 if (_mesa_meta_pbo_GetTexSubImage(ctx
, 3, texImage
,
565 xoffset
, yoffset
, zoffset
,
566 width
, height
, depth
, format
, type
,
567 pixels
, &ctx
->Pack
)) {
568 /* Flush to guarantee coherency between the render cache and other
569 * caches the PBO could potentially be bound to after this point.
570 * See the related comment in intelReadPixels() for a more detailed
573 brw_emit_mi_flush(brw
);
577 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__
);
580 ok
= intel_gettexsubimage_tiled_memcpy(ctx
, texImage
, xoffset
, yoffset
,
582 format
, type
, pixels
, &ctx
->Pack
);
587 _mesa_meta_GetTexSubImage(ctx
, xoffset
, yoffset
, zoffset
,
588 width
, height
, depth
,
589 format
, type
, pixels
, texImage
);
591 DBG("%s - DONE\n", __func__
);
595 flush_astc_denorms(struct gl_context
*ctx
, GLuint dims
,
596 struct gl_texture_image
*texImage
,
597 GLint xoffset
, GLint yoffset
, GLint zoffset
,
598 GLsizei width
, GLsizei height
, GLsizei depth
)
600 struct compressed_pixelstore store
;
601 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
,
602 width
, height
, depth
,
603 &ctx
->Unpack
, &store
);
605 for (int slice
= 0; slice
< store
.CopySlices
; slice
++) {
607 /* Map dest texture buffer */
610 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
+ zoffset
,
611 xoffset
, yoffset
, width
, height
,
612 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
613 &dstMap
, &dstRowStride
);
617 for (int i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
619 /* An ASTC block is stored in little endian mode. The byte that
620 * contains bits 0..7 is stored at the lower address in memory.
622 struct astc_void_extent
{
623 uint16_t header
: 12;
624 uint16_t dontcare
[3];
629 } *blocks
= (struct astc_void_extent
*) dstMap
;
631 /* Iterate over every copied block in the row */
632 for (int j
= 0; j
< store
.CopyBytesPerRow
/ 16; j
++) {
634 /* Check if the header matches that of an LDR void-extent block */
635 if (blocks
[j
].header
== 0xDFC) {
637 /* Flush UNORM16 values that would be denormalized */
638 if (blocks
[j
].A
< 4) blocks
[j
].A
= 0;
639 if (blocks
[j
].B
< 4) blocks
[j
].B
= 0;
640 if (blocks
[j
].G
< 4) blocks
[j
].G
= 0;
641 if (blocks
[j
].R
< 4) blocks
[j
].R
= 0;
645 dstMap
+= dstRowStride
;
648 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ zoffset
);
654 intelCompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
655 struct gl_texture_image
*texImage
,
656 GLint xoffset
, GLint yoffset
, GLint zoffset
,
657 GLsizei width
, GLsizei height
, GLsizei depth
,
659 GLsizei imageSize
, const GLvoid
*data
)
661 /* Upload the compressed data blocks */
662 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
663 xoffset
, yoffset
, zoffset
,
664 width
, height
, depth
,
665 format
, imageSize
, data
);
667 /* Fix up copied ASTC blocks if necessary */
668 GLenum gl_format
= _mesa_compressed_format_to_glenum(ctx
,
669 texImage
->TexFormat
);
670 bool is_linear_astc
= _mesa_is_astc_format(gl_format
) &&
671 !_mesa_is_srgb_format(gl_format
);
672 struct brw_context
*brw
= (struct brw_context
*) ctx
;
673 if (brw
->gen
== 9 && is_linear_astc
)
674 flush_astc_denorms(ctx
, dims
, texImage
,
675 xoffset
, yoffset
, zoffset
,
676 width
, height
, depth
);
680 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
682 functions
->TexImage
= intelTexImage
;
683 functions
->CompressedTexSubImage
= intelCompressedTexSubImage
;
684 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
685 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
686 functions
->GetTexSubImage
= intel_get_tex_sub_image
;