2 #include "main/glheader.h"
3 #include "main/macros.h"
4 #include "main/mfeatures.h"
5 #include "main/mtypes.h"
6 #include "main/enums.h"
7 #include "main/bufferobj.h"
8 #include "main/context.h"
9 #include "main/formats.h"
10 #include "main/texcompress.h"
11 #include "main/texstore.h"
12 #include "main/texgetimage.h"
13 #include "main/texobj.h"
14 #include "main/teximage.h"
16 #include "intel_context.h"
17 #include "intel_mipmap_tree.h"
18 #include "intel_buffer_objects.h"
19 #include "intel_batchbuffer.h"
20 #include "intel_tex.h"
21 #include "intel_blit.h"
22 #include "intel_fbo.h"
24 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
26 /* Functions to store texture images. Where possible, mipmap_tree's
27 * will be created or further instantiated with image data, otherwise
28 * images will be stored in malloc'd memory. A validation step is
29 * required to pull those images into a mipmap tree, or otherwise
30 * decide a fallback is required.
48 struct intel_mipmap_tree
*
49 intel_miptree_create_for_teximage(struct intel_context
*intel
,
50 struct intel_texture_object
*intelObj
,
51 struct intel_texture_image
*intelImage
,
52 GLboolean expect_accelerated_upload
)
55 GLuint width
= intelImage
->base
.Width
;
56 GLuint height
= intelImage
->base
.Height
;
57 GLuint depth
= intelImage
->base
.Depth
;
58 GLuint i
, comp_byte
= 0;
61 DBG("%s\n", __FUNCTION__
);
63 if (intelImage
->base
.Border
)
66 /* Figure out image dimensions at start level.
68 for (i
= intelImage
->level
; i
> 0; i
--) {
76 /* Guess a reasonable value for lastLevel. This is probably going
77 * to be wrong fairly often and might mean that we have to look at
78 * resizable buffers, or require that buffers implement lazy
79 * pagetable arrangements.
81 if ((intelObj
->base
.MinFilter
== GL_NEAREST
||
82 intelObj
->base
.MinFilter
== GL_LINEAR
) &&
83 intelImage
->level
== 0) {
87 lastLevel
= logbase2(MAX2(MAX2(width
, height
), depth
));
90 if (_mesa_is_format_compressed(intelImage
->base
.TexFormat
))
91 comp_byte
= intel_compressed_num_bytes(intelImage
->base
.TexFormat
);
93 texelBytes
= _mesa_get_format_bytes(intelImage
->base
.TexFormat
);
95 return intel_miptree_create(intel
,
96 intelObj
->base
.Target
,
97 intelImage
->base
._BaseFormat
,
98 intelImage
->base
.InternalFormat
,
105 expect_accelerated_upload
);
112 target_to_face(GLenum target
)
115 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
116 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
117 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
118 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
119 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
120 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
121 return ((GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
127 /* There are actually quite a few combinations this will work for,
128 * more than what I've listed here.
131 check_pbo_format(GLint internalFormat
,
132 GLenum format
, GLenum type
,
133 gl_format mesa_format
)
135 switch (internalFormat
) {
139 return (format
== GL_BGRA
&&
140 (type
== GL_UNSIGNED_BYTE
||
141 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
142 mesa_format
== MESA_FORMAT_ARGB8888
);
145 return (format
== GL_RGB
&&
146 type
== GL_UNSIGNED_SHORT_5_6_5
&&
147 mesa_format
== MESA_FORMAT_RGB565
);
150 return (format
== GL_LUMINANCE
&&
151 type
== GL_UNSIGNED_BYTE
&&
152 mesa_format
== MESA_FORMAT_L8
);
154 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
161 /* XXX: Do this for TexSubImage also:
164 try_pbo_upload(struct intel_context
*intel
,
165 struct intel_texture_image
*intelImage
,
166 const struct gl_pixelstore_attrib
*unpack
,
167 GLint internalFormat
,
168 GLint width
, GLint height
,
169 GLenum format
, GLenum type
, const void *pixels
)
171 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
172 GLuint src_offset
, src_stride
;
173 GLuint dst_x
, dst_y
, dst_stride
;
174 drm_intel_bo
*dst_buffer
= intel_region_buffer(intel
,
175 intelImage
->mt
->region
,
178 if (!_mesa_is_bufferobj(unpack
->BufferObj
) ||
179 intel
->ctx
._ImageTransferState
||
180 unpack
->SkipPixels
|| unpack
->SkipRows
) {
181 DBG("%s: failure 1\n", __FUNCTION__
);
185 /* note: potential 64-bit ptr to 32-bit int cast */
186 src_offset
= (GLuint
) (unsigned long) pixels
;
188 if (unpack
->RowLength
> 0)
189 src_stride
= unpack
->RowLength
;
193 intel_miptree_get_image_offset(intelImage
->mt
, intelImage
->level
,
197 dst_stride
= intelImage
->mt
->region
->pitch
;
199 if (drm_intel_bo_references(intel
->batch
->buf
, dst_buffer
))
200 intel_flush(&intel
->ctx
);
203 drm_intel_bo
*src_buffer
= intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
205 if (!intelEmitCopyBlit(intel
,
207 src_stride
, src_buffer
, src_offset
, GL_FALSE
,
208 dst_stride
, dst_buffer
, 0,
209 intelImage
->mt
->region
->tiling
,
210 0, 0, dst_x
, dst_y
, width
, height
,
221 try_pbo_zcopy(struct intel_context
*intel
,
222 struct intel_texture_image
*intelImage
,
223 const struct gl_pixelstore_attrib
*unpack
,
224 GLint internalFormat
,
225 GLint width
, GLint height
,
226 GLenum format
, GLenum type
, const void *pixels
)
228 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
229 GLuint src_offset
, src_stride
;
230 GLuint dst_x
, dst_y
, dst_stride
;
232 if (!_mesa_is_bufferobj(unpack
->BufferObj
) ||
233 intel
->ctx
._ImageTransferState
||
234 unpack
->SkipPixels
|| unpack
->SkipRows
) {
235 DBG("%s: failure 1\n", __FUNCTION__
);
239 /* note: potential 64-bit ptr to 32-bit int cast */
240 src_offset
= (GLuint
) (unsigned long) pixels
;
242 if (unpack
->RowLength
> 0)
243 src_stride
= unpack
->RowLength
;
247 intel_miptree_get_image_offset(intelImage
->mt
, intelImage
->level
,
251 dst_stride
= intelImage
->mt
->region
->pitch
;
253 if (src_stride
!= dst_stride
|| dst_x
!= 0 || dst_y
!= 0 ||
255 DBG("%s: failure 2\n", __FUNCTION__
);
259 intel_region_attach_pbo(intel
, intelImage
->mt
->region
, pbo
);
266 intelTexImage(struct gl_context
* ctx
,
268 GLenum target
, GLint level
,
269 GLint internalFormat
,
270 GLint width
, GLint height
, GLint depth
,
272 GLenum format
, GLenum type
, const void *pixels
,
273 const struct gl_pixelstore_attrib
*unpack
,
274 struct gl_texture_object
*texObj
,
275 struct gl_texture_image
*texImage
, GLsizei imageSize
,
276 GLboolean compressed
)
278 struct intel_context
*intel
= intel_context(ctx
);
279 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
280 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
281 GLint texelBytes
, sizeInBytes
;
282 GLuint dstRowStride
= 0, srcRowStride
= texImage
->RowStride
;
284 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
285 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
287 intelImage
->face
= target_to_face(target
);
288 intelImage
->level
= level
;
290 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
294 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
296 if (!intelImage
->mt
) {
297 assert(texImage
->RowStride
== width
);
301 /* Release the reference to a potentially orphaned buffer.
302 * Release any old malloced memory.
304 if (intelImage
->mt
) {
305 intel_miptree_release(intel
, &intelImage
->mt
);
306 assert(!texImage
->Data
);
308 else if (texImage
->Data
) {
309 _mesa_free_texmemory(texImage
->Data
);
310 texImage
->Data
= NULL
;
313 if (intelObj
->mt
&& intel_miptree_match_image(intelObj
->mt
,
314 &intelImage
->base
)) {
315 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
317 intel_miptree_release(intel
, &intelImage
->mt
);
318 intelImage
->mt
= intel_miptree_create_for_teximage(intel
, intelObj
,
321 if (!intelImage
->mt
) {
322 DBG("guess_and_alloc_mipmap_tree: failed\n");
325 /* Speculatively set up the object with this miptree so that the
326 * later levels can just load into the miptree we just made.
328 if (!intelObj
->mt
&& intelImage
->mt
)
329 intel_miptree_reference(&intelObj
->mt
, intelImage
->mt
);
336 _mesa_is_bufferobj(unpack
->BufferObj
) &&
337 check_pbo_format(internalFormat
, format
,
338 type
, intelImage
->base
.TexFormat
)) {
340 DBG("trying pbo upload\n");
342 /* Attempt to texture directly from PBO data (zero copy upload).
344 * Currently disable as it can lead to worse as well as better
345 * performance (in particular when intel_region_cow() is
348 if (intelImage
->mt
->levels
== 1) {
349 if (try_pbo_zcopy(intel
, intelImage
, unpack
,
351 width
, height
, format
, type
, pixels
)) {
353 DBG("pbo zcopy upload succeeded\n");
359 /* Otherwise, attempt to use the blitter for PBO image uploads.
361 if (try_pbo_upload(intel
, intelImage
, unpack
,
363 width
, height
, format
, type
, pixels
)) {
364 DBG("pbo upload succeeded\n");
368 DBG("pbo upload failed\n");
371 /* intelCopyTexImage calls this function with pixels == NULL, with
372 * the expectation that the mipmap tree will be set up but nothing
373 * more will be done. This is where those calls return:
376 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
378 "glCompressedTexImage");
380 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
382 pixels
, unpack
, "glTexImage");
385 if (intelImage
->mt
) {
386 if (pixels
!= NULL
) {
387 /* Flush any queued rendering with the texture before mapping. */
388 if (drm_intel_bo_references(intel
->batch
->buf
,
389 intelImage
->mt
->region
->buffer
)) {
392 texImage
->Data
= intel_miptree_image_map(intel
,
397 intelImage
->base
.ImageOffsets
);
400 texImage
->RowStride
= dstRowStride
/ intelImage
->mt
->cpp
;
403 /* Allocate regular memory and store the image there temporarily. */
404 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
405 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
410 _mesa_format_row_stride(texImage
->TexFormat
, width
);
414 dstRowStride
= width
* texelBytes
;
415 sizeInBytes
= depth
* dstRowStride
* height
;
418 texImage
->Data
= _mesa_alloc_texmemory(sizeInBytes
);
421 DBG("Upload image %dx%dx%d row_len %d "
422 "pitch %d pixels %d compressed %d\n",
423 width
, height
, depth
, width
* texelBytes
, dstRowStride
,
424 pixels
? 1 : 0, compressed
);
426 /* Copy data. Would like to know when it's ok for us to eg. use
427 * the blitter to copy. Or, use the hardware to do the format
428 * conversion and copy:
432 if (intelImage
->mt
) {
433 struct intel_region
*dst
= intelImage
->mt
->region
;
434 _mesa_copy_rect(texImage
->Data
, dst
->cpp
, dst
->pitch
,
436 intelImage
->mt
->level
[level
].width
,
437 (intelImage
->mt
->level
[level
].height
+3)/4,
443 memcpy(texImage
->Data
, pixels
, imageSize
);
446 else if (!_mesa_texstore(ctx
, dims
,
447 texImage
->_BaseFormat
,
449 texImage
->Data
, 0, 0, 0, /* dstX/Y/Zoffset */
451 texImage
->ImageOffsets
,
452 width
, height
, depth
,
453 format
, type
, pixels
, unpack
)) {
454 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
458 _mesa_unmap_teximage_pbo(ctx
, unpack
);
460 if (intelImage
->mt
) {
462 intel_miptree_image_unmap(intel
, intelImage
->mt
);
463 texImage
->Data
= NULL
;
469 intelTexImage3D(struct gl_context
* ctx
,
470 GLenum target
, GLint level
,
471 GLint internalFormat
,
472 GLint width
, GLint height
, GLint depth
,
474 GLenum format
, GLenum type
, const void *pixels
,
475 const struct gl_pixelstore_attrib
*unpack
,
476 struct gl_texture_object
*texObj
,
477 struct gl_texture_image
*texImage
)
479 intelTexImage(ctx
, 3, target
, level
,
480 internalFormat
, width
, height
, depth
, border
,
481 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
486 intelTexImage2D(struct gl_context
* ctx
,
487 GLenum target
, GLint level
,
488 GLint internalFormat
,
489 GLint width
, GLint height
, GLint border
,
490 GLenum format
, GLenum type
, const void *pixels
,
491 const struct gl_pixelstore_attrib
*unpack
,
492 struct gl_texture_object
*texObj
,
493 struct gl_texture_image
*texImage
)
495 intelTexImage(ctx
, 2, target
, level
,
496 internalFormat
, width
, height
, 1, border
,
497 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
502 intelTexImage1D(struct gl_context
* ctx
,
503 GLenum target
, GLint level
,
504 GLint internalFormat
,
505 GLint width
, GLint border
,
506 GLenum format
, GLenum type
, const void *pixels
,
507 const struct gl_pixelstore_attrib
*unpack
,
508 struct gl_texture_object
*texObj
,
509 struct gl_texture_image
*texImage
)
511 intelTexImage(ctx
, 1, target
, level
,
512 internalFormat
, width
, 1, 1, border
,
513 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
518 intelCompressedTexImage2D( struct gl_context
*ctx
, GLenum target
, GLint level
,
519 GLint internalFormat
,
520 GLint width
, GLint height
, GLint border
,
521 GLsizei imageSize
, const GLvoid
*data
,
522 struct gl_texture_object
*texObj
,
523 struct gl_texture_image
*texImage
)
525 intelTexImage(ctx
, 2, target
, level
,
526 internalFormat
, width
, height
, 1, border
,
527 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
532 * Need to map texture image into memory before copying image data,
536 intel_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
537 GLenum format
, GLenum type
, GLvoid
* pixels
,
538 struct gl_texture_object
*texObj
,
539 struct gl_texture_image
*texImage
, GLboolean compressed
)
541 struct intel_context
*intel
= intel_context(ctx
);
542 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
544 /* If we're reading from a texture that has been rendered to, need to
545 * make sure rendering is complete.
546 * We could probably predicate this on texObj->_RenderToTexture
551 if (intelImage
->mt
) {
552 /* Image is stored in hardware format in a buffer managed by the
553 * kernel. Need to explicitly map and unmap it.
555 intelImage
->base
.Data
=
556 intel_miptree_image_map(intel
,
560 &intelImage
->base
.RowStride
,
561 intelImage
->base
.ImageOffsets
);
562 intelImage
->base
.RowStride
/= intelImage
->mt
->cpp
;
565 /* Otherwise, the image should actually be stored in
566 * intelImage->base.Data. This is pretty confusing for
567 * everybody, I'd much prefer to separate the two functions of
568 * texImage->Data - storage for texture images in main memory
569 * and access (ie mappings) of images. In other words, we'd
570 * create a new texImage->Map field and leave Data simply for
573 assert(intelImage
->base
.Data
);
578 _mesa_get_compressed_teximage(ctx
, target
, level
, pixels
,
582 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
588 if (intelImage
->mt
) {
589 intel_miptree_image_unmap(intel
, intelImage
->mt
);
590 intelImage
->base
.Data
= NULL
;
596 intelGetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
597 GLenum format
, GLenum type
, GLvoid
* pixels
,
598 struct gl_texture_object
*texObj
,
599 struct gl_texture_image
*texImage
)
601 intel_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
602 texObj
, texImage
, GL_FALSE
);
607 intelGetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
609 struct gl_texture_object
*texObj
,
610 struct gl_texture_image
*texImage
)
612 intel_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
613 texObj
, texImage
, GL_TRUE
);
617 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
618 GLint texture_format
,
619 __DRIdrawable
*dPriv
)
621 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
622 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
623 struct gl_context
*ctx
= &intel
->ctx
;
624 struct intel_texture_object
*intelObj
;
625 struct intel_texture_image
*intelImage
;
626 struct intel_mipmap_tree
*mt
;
627 struct intel_renderbuffer
*rb
;
628 struct gl_texture_object
*texObj
;
629 struct gl_texture_image
*texImage
;
630 int level
= 0, internalFormat
;
633 texObj
= _mesa_get_current_tex_object(ctx
, target
);
634 intelObj
= intel_texture_object(texObj
);
639 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
640 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
641 intel_update_renderbuffers(pDRICtx
, dPriv
);
643 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
644 /* If the region isn't set, then intel_update_renderbuffers was unable
645 * to get the buffers for the drawable.
647 if (rb
->region
== NULL
)
650 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
651 internalFormat
= GL_RGB
;
652 texFormat
= MESA_FORMAT_XRGB8888
;
655 internalFormat
= GL_RGBA
;
656 texFormat
= MESA_FORMAT_ARGB8888
;
659 mt
= intel_miptree_create_for_region(intel
, target
,
660 internalFormat
, rb
->region
, 1, 0);
664 _mesa_lock_texture(&intel
->ctx
, texObj
);
666 texImage
= _mesa_get_tex_image(&intel
->ctx
, texObj
, target
, level
);
667 intelImage
= intel_texture_image(texImage
);
669 if (intelImage
->mt
) {
670 intel_miptree_release(intel
, &intelImage
->mt
);
671 assert(!texImage
->Data
);
674 intel_miptree_release(intel
, &intelObj
->mt
);
678 _mesa_init_teximage_fields(&intel
->ctx
, target
, texImage
,
679 rb
->region
->width
, rb
->region
->height
, 1,
680 0, internalFormat
, texFormat
);
682 intelImage
->face
= target_to_face(target
);
683 intelImage
->level
= level
;
684 texImage
->RowStride
= rb
->region
->pitch
;
685 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
687 if (!intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
)) {
688 fprintf(stderr
, "miptree doesn't match image\n");
691 _mesa_unlock_texture(&intel
->ctx
, texObj
);
695 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
697 /* The old interface didn't have the format argument, so copy our
698 * implementation's behavior at the time.
700 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
703 #if FEATURE_OES_EGL_image
705 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
706 struct gl_texture_object
*texObj
,
707 struct gl_texture_image
*texImage
,
708 GLeglImageOES image_handle
)
710 struct intel_context
*intel
= intel_context(ctx
);
711 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
712 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
713 struct intel_mipmap_tree
*mt
;
717 screen
= intel
->intelScreen
->driScrnPriv
;
718 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
719 screen
->loaderPrivate
);
723 mt
= intel_miptree_create_for_region(intel
, target
,
724 image
->internal_format
,
725 image
->region
, 1, 0);
729 if (intelImage
->mt
) {
730 intel_miptree_release(intel
, &intelImage
->mt
);
731 assert(!texImage
->Data
);
734 intel_miptree_release(intel
, &intelObj
->mt
);
737 _mesa_init_teximage_fields(&intel
->ctx
, target
, texImage
,
738 image
->region
->width
, image
->region
->height
, 1,
739 0, image
->internal_format
, image
->format
);
741 intelImage
->face
= target_to_face(target
);
742 intelImage
->level
= 0;
743 texImage
->RowStride
= image
->region
->pitch
;
744 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
746 if (!intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
))
747 fprintf(stderr
, "miptree doesn't match image\n");
752 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
754 functions
->TexImage1D
= intelTexImage1D
;
755 functions
->TexImage2D
= intelTexImage2D
;
756 functions
->TexImage3D
= intelTexImage3D
;
757 functions
->GetTexImage
= intelGetTexImage
;
759 functions
->CompressedTexImage2D
= intelCompressedTexImage2D
;
760 functions
->GetCompressedTexImage
= intelGetCompressedTexImage
;
762 #if FEATURE_OES_EGL_image
763 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;