5 #include "main/glheader.h"
6 #include "main/macros.h"
7 #include "main/mtypes.h"
8 #include "main/enums.h"
9 #include "main/colortab.h"
10 #include "main/convolve.h"
11 #include "main/context.h"
12 #include "main/simple_list.h"
13 #include "main/texcompress.h"
14 #include "main/texformat.h"
15 #include "main/texobj.h"
16 #include "main/texstore.h"
17 #include "main/teximage.h"
19 #include "intel_context.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"
27 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
29 /* Functions to store texture images. Where possible, mipmap_tree's
30 * will be created or further instantiated with image data, otherwise
31 * images will be stored in malloc'd memory. A validation step is
32 * required to pull those images into a mipmap tree, or otherwise
33 * decide a fallback is required.
52 /* Otherwise, store it in memory if (Border != 0) or (any dimension ==
55 * Otherwise, if max_level >= level >= min_level, create tree with
56 * space for textures from min_level down to max_level.
58 * Otherwise, create tree with space for textures from (level
59 * 0)..(1x1). Consider pruning this tree at a validation if the
63 guess_and_alloc_mipmap_tree(struct intel_context
*intel
,
64 struct intel_texture_object
*intelObj
,
65 struct intel_texture_image
*intelImage
)
69 GLuint width
= intelImage
->base
.Width
;
70 GLuint height
= intelImage
->base
.Height
;
71 GLuint depth
= intelImage
->base
.Depth
;
72 GLuint l2width
, l2height
, l2depth
;
73 GLuint i
, comp_byte
= 0;
75 DBG("%s\n", __FUNCTION__
);
77 if (intelImage
->base
.Border
||
78 ((intelImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
) &&
79 ((intelObj
->base
.WrapS
== GL_CLAMP_TO_BORDER
) ||
80 (intelObj
->base
.WrapT
== GL_CLAMP_TO_BORDER
))))
83 if (intelImage
->level
> intelObj
->base
.BaseLevel
&&
84 (intelImage
->base
.Width
== 1 ||
85 (intelObj
->base
.Target
!= GL_TEXTURE_1D
&&
86 intelImage
->base
.Height
== 1) ||
87 (intelObj
->base
.Target
== GL_TEXTURE_3D
&&
88 intelImage
->base
.Depth
== 1)))
91 /* If this image disrespects BaseLevel, allocate from level zero.
92 * Usually BaseLevel == 0, so it's unlikely to happen.
94 if (intelImage
->level
< intelObj
->base
.BaseLevel
)
97 firstLevel
= intelObj
->base
.BaseLevel
;
100 /* Figure out image dimensions at start level.
102 for (i
= intelImage
->level
; i
> firstLevel
; i
--) {
110 /* Guess a reasonable value for lastLevel. This is probably going
111 * to be wrong fairly often and might mean that we have to look at
112 * resizable buffers, or require that buffers implement lazy
113 * pagetable arrangements.
115 if ((intelObj
->base
.MinFilter
== GL_NEAREST
||
116 intelObj
->base
.MinFilter
== GL_LINEAR
) &&
117 intelImage
->level
== firstLevel
) {
118 lastLevel
= firstLevel
;
121 l2width
= logbase2(width
);
122 l2height
= logbase2(height
);
123 l2depth
= logbase2(depth
);
124 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
127 assert(!intelObj
->mt
);
128 if (intelImage
->base
.IsCompressed
)
129 comp_byte
= intel_compressed_num_bytes(intelImage
->base
.TexFormat
->MesaFormat
);
130 intelObj
->mt
= intel_miptree_create(intel
,
131 intelObj
->base
.Target
,
132 intelImage
->base
.InternalFormat
,
138 intelImage
->base
.TexFormat
->TexelBytes
,
141 DBG("%s - success\n", __FUNCTION__
);
148 target_to_face(GLenum target
)
151 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
152 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
153 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
154 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
155 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
156 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
157 return ((GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
163 /* There are actually quite a few combinations this will work for,
164 * more than what I've listed here.
167 check_pbo_format(GLint internalFormat
,
168 GLenum format
, GLenum type
,
169 const struct gl_texture_format
*mesa_format
)
171 switch (internalFormat
) {
174 return (format
== GL_BGRA
&&
175 (type
== GL_UNSIGNED_BYTE
||
176 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
177 mesa_format
== &_mesa_texformat_argb8888
);
180 return (format
== GL_RGB
&&
181 type
== GL_UNSIGNED_SHORT_5_6_5
&&
182 mesa_format
== &_mesa_texformat_rgb565
);
184 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
191 /* XXX: Do this for TexSubImage also:
194 try_pbo_upload(struct intel_context
*intel
,
195 struct intel_texture_image
*intelImage
,
196 const struct gl_pixelstore_attrib
*unpack
,
197 GLint internalFormat
,
198 GLint width
, GLint height
,
199 GLenum format
, GLenum type
, const void *pixels
)
201 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
202 GLuint src_offset
, src_stride
;
203 GLuint dst_offset
, dst_stride
;
206 intel
->ctx
._ImageTransferState
||
207 unpack
->SkipPixels
|| unpack
->SkipRows
) {
208 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
212 src_offset
= (GLuint
) pixels
;
214 if (unpack
->RowLength
> 0)
215 src_stride
= unpack
->RowLength
;
219 dst_offset
= intel_miptree_image_offset(intelImage
->mt
,
223 dst_stride
= intelImage
->mt
->pitch
;
225 intelFlush(&intel
->ctx
);
226 LOCK_HARDWARE(intel
);
228 dri_bo
*src_buffer
= intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
229 dri_bo
*dst_buffer
= intel_region_buffer(intel
,
230 intelImage
->mt
->region
,
234 intelEmitCopyBlit(intel
,
236 src_stride
, src_buffer
, src_offset
, GL_FALSE
,
237 dst_stride
, dst_buffer
, dst_offset
, GL_FALSE
,
238 0, 0, 0, 0, width
, height
,
241 UNLOCK_HARDWARE(intel
);
249 try_pbo_zcopy(struct intel_context
*intel
,
250 struct intel_texture_image
*intelImage
,
251 const struct gl_pixelstore_attrib
*unpack
,
252 GLint internalFormat
,
253 GLint width
, GLint height
,
254 GLenum format
, GLenum type
, const void *pixels
)
256 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
257 GLuint src_offset
, src_stride
;
258 GLuint dst_offset
, dst_stride
;
261 intel
->ctx
._ImageTransferState
||
262 unpack
->SkipPixels
|| unpack
->SkipRows
) {
263 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
267 src_offset
= (GLuint
) pixels
;
269 if (unpack
->RowLength
> 0)
270 src_stride
= unpack
->RowLength
;
274 dst_offset
= intel_miptree_image_offset(intelImage
->mt
,
278 dst_stride
= intelImage
->mt
->pitch
;
280 if (src_stride
!= dst_stride
|| dst_offset
!= 0 || src_offset
!= 0) {
281 _mesa_printf("%s: failure 2\n", __FUNCTION__
);
285 intel_region_attach_pbo(intel
, intelImage
->mt
->region
, pbo
);
296 intelTexImage(GLcontext
* ctx
,
298 GLenum target
, GLint level
,
299 GLint internalFormat
,
300 GLint width
, GLint height
, GLint depth
,
302 GLenum format
, GLenum type
, const void *pixels
,
303 const struct gl_pixelstore_attrib
*unpack
,
304 struct gl_texture_object
*texObj
,
305 struct gl_texture_image
*texImage
, GLsizei imageSize
, int compressed
)
307 struct intel_context
*intel
= intel_context(ctx
);
308 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
309 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
310 GLint postConvWidth
= width
;
311 GLint postConvHeight
= height
;
312 GLint texelBytes
, sizeInBytes
;
313 GLuint dstRowStride
, srcRowStride
= texImage
->RowStride
;
316 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
317 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
321 intelImage
->face
= target_to_face(target
);
322 intelImage
->level
= level
;
324 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
325 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
329 /* choose the texture format */
330 texImage
->TexFormat
= intelChooseTextureFormat(ctx
, internalFormat
,
333 _mesa_set_fetch_functions(texImage
, dims
);
335 if (texImage
->TexFormat
->TexelBytes
== 0) {
336 /* must be a compressed format */
338 texImage
->IsCompressed
= GL_TRUE
;
339 texImage
->CompressedSize
=
340 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
341 texImage
->Height
, texImage
->Depth
,
342 texImage
->TexFormat
->MesaFormat
);
344 texelBytes
= texImage
->TexFormat
->TexelBytes
;
346 /* Minimum pitch of 32 bytes */
347 if (postConvWidth
* texelBytes
< 32) {
348 postConvWidth
= 32 / texelBytes
;
349 texImage
->RowStride
= postConvWidth
;
352 if (!intelImage
->mt
) {
353 assert(texImage
->RowStride
== postConvWidth
);
357 /* Release the reference to a potentially orphaned buffer.
358 * Release any old malloced memory.
360 if (intelImage
->mt
) {
361 intel_miptree_release(intel
, &intelImage
->mt
);
362 assert(!texImage
->Data
);
364 else if (texImage
->Data
) {
365 _mesa_free_texmemory(texImage
->Data
);
366 texImage
->Data
= NULL
;
369 /* If this is the only texture image in the tree, could call
370 * bmBufferData with NULL data to free the old block and avoid
371 * waiting on any outstanding fences.
374 intelObj
->mt
->first_level
== level
&&
375 intelObj
->mt
->last_level
== level
&&
376 intelObj
->mt
->target
!= GL_TEXTURE_CUBE_MAP_ARB
&&
377 !intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
378 intelImage
->face
, intelImage
->level
)) {
381 intel_miptree_release(intel
, &intelObj
->mt
);
382 assert(!intelObj
->mt
);
386 guess_and_alloc_mipmap_tree(intel
, intelObj
, intelImage
);
388 DBG("guess_and_alloc_mipmap_tree: failed\n");
392 assert(!intelImage
->mt
);
395 intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
396 intelImage
->face
, intelImage
->level
)) {
398 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
399 assert(intelImage
->mt
);
400 } else if (intelImage
->base
.Border
== 0) {
403 if (intelImage
->base
.IsCompressed
) {
405 intel_compressed_num_bytes(intelImage
->base
.TexFormat
->MesaFormat
);
408 /* Didn't fit in the object miptree, but it's suitable for inclusion in
409 * a miptree, so create one just for our level and store it in the image.
410 * It'll get moved into the object miptree at validate time.
412 intelImage
->mt
= intel_miptree_create(intel
, target
, internalFormat
,
414 width
, height
, depth
,
415 intelImage
->base
.TexFormat
->TexelBytes
,
424 intel_buffer_object(unpack
->BufferObj
) &&
425 check_pbo_format(internalFormat
, format
,
426 type
, intelImage
->base
.TexFormat
)) {
428 DBG("trying pbo upload\n");
430 /* Attempt to texture directly from PBO data (zero copy upload).
432 * Currently disable as it can lead to worse as well as better
433 * performance (in particular when intel_region_cow() is
436 if (intelObj
->mt
== intelImage
->mt
&&
437 intelObj
->mt
->first_level
== level
&&
438 intelObj
->mt
->last_level
== level
) {
440 if (try_pbo_zcopy(intel
, intelImage
, unpack
,
442 width
, height
, format
, type
, pixels
)) {
444 DBG("pbo zcopy upload succeeded\n");
450 /* Otherwise, attempt to use the blitter for PBO image uploads.
452 if (try_pbo_upload(intel
, intelImage
, unpack
,
454 width
, height
, format
, type
, pixels
)) {
455 DBG("pbo upload succeeded\n");
459 DBG("pbo upload failed\n");
464 /* intelCopyTexImage calls this function with pixels == NULL, with
465 * the expectation that the mipmap tree will be set up but nothing
466 * more will be done. This is where those calls return:
469 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
471 "glCompressedTexImage");
473 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
475 pixels
, unpack
, "glTexImage");
478 LOCK_HARDWARE(intel
);
480 if (intelImage
->mt
) {
481 texImage
->Data
= intel_miptree_image_map(intel
,
486 intelImage
->base
.ImageOffsets
);
487 texImage
->RowStride
= dstRowStride
/ intelImage
->mt
->cpp
;
490 /* Allocate regular memory and store the image there temporarily. */
491 if (texImage
->IsCompressed
) {
492 sizeInBytes
= texImage
->CompressedSize
;
494 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
498 dstRowStride
= postConvWidth
* texelBytes
;
499 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
502 texImage
->Data
= _mesa_alloc_texmemory(sizeInBytes
);
505 DBG("Upload image %dx%dx%d row_len %d "
507 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
509 /* Copy data. Would like to know when it's ok for us to eg. use
510 * the blitter to copy. Or, use the hardware to do the format
511 * conversion and copy:
515 if (intelImage
->mt
) {
516 struct intel_region
*dst
= intelImage
->mt
->region
;
517 _mesa_copy_rect(texImage
->Data
, dst
->cpp
, dst
->pitch
,
519 intelImage
->mt
->level
[level
].width
,
520 intelImage
->mt
->level
[level
].height
/4,
525 memcpy(texImage
->Data
, pixels
, imageSize
);
526 } else if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
527 texImage
->_BaseFormat
,
529 texImage
->Data
, 0, 0, 0, /* dstX/Y/Zoffset */
531 texImage
->ImageOffsets
,
532 width
, height
, depth
,
533 format
, type
, pixels
, unpack
)) {
534 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
538 /* GL_SGIS_generate_mipmap */
539 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
540 intel_generate_mipmap(ctx
, target
, texObj
);
543 _mesa_unmap_teximage_pbo(ctx
, unpack
);
545 if (intelImage
->mt
) {
546 intel_miptree_image_unmap(intel
, intelImage
->mt
);
547 texImage
->Data
= NULL
;
550 UNLOCK_HARDWARE(intel
);
554 intelTexImage3D(GLcontext
* ctx
,
555 GLenum target
, GLint level
,
556 GLint internalFormat
,
557 GLint width
, GLint height
, GLint depth
,
559 GLenum format
, GLenum type
, const void *pixels
,
560 const struct gl_pixelstore_attrib
*unpack
,
561 struct gl_texture_object
*texObj
,
562 struct gl_texture_image
*texImage
)
564 intelTexImage(ctx
, 3, target
, level
,
565 internalFormat
, width
, height
, depth
, border
,
566 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
571 intelTexImage2D(GLcontext
* ctx
,
572 GLenum target
, GLint level
,
573 GLint internalFormat
,
574 GLint width
, GLint height
, GLint border
,
575 GLenum format
, GLenum type
, const void *pixels
,
576 const struct gl_pixelstore_attrib
*unpack
,
577 struct gl_texture_object
*texObj
,
578 struct gl_texture_image
*texImage
)
580 intelTexImage(ctx
, 2, target
, level
,
581 internalFormat
, width
, height
, 1, border
,
582 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
586 intelTexImage1D(GLcontext
* ctx
,
587 GLenum target
, GLint level
,
588 GLint internalFormat
,
589 GLint width
, GLint border
,
590 GLenum format
, GLenum type
, const void *pixels
,
591 const struct gl_pixelstore_attrib
*unpack
,
592 struct gl_texture_object
*texObj
,
593 struct gl_texture_image
*texImage
)
595 intelTexImage(ctx
, 1, target
, level
,
596 internalFormat
, width
, 1, 1, border
,
597 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
600 void intelCompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
601 GLint internalFormat
,
602 GLint width
, GLint height
, GLint border
,
603 GLsizei imageSize
, const GLvoid
*data
,
604 struct gl_texture_object
*texObj
,
605 struct gl_texture_image
*texImage
)
607 intelTexImage(ctx
, 2, target
, level
,
608 internalFormat
, width
, height
, 1, border
,
609 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
613 * Need to map texture image into memory before copying image data,
617 intel_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
618 GLenum format
, GLenum type
, GLvoid
* pixels
,
619 struct gl_texture_object
*texObj
,
620 struct gl_texture_image
*texImage
, int compressed
)
622 struct intel_context
*intel
= intel_context(ctx
);
623 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
626 if (intelImage
->mt
) {
627 /* Image is stored in hardware format in a buffer managed by the
628 * kernel. Need to explicitly map and unmap it.
630 intelImage
->base
.Data
=
631 intel_miptree_image_map(intel
,
635 &intelImage
->base
.RowStride
,
636 intelImage
->base
.ImageOffsets
);
637 intelImage
->base
.RowStride
/= intelImage
->mt
->cpp
;
640 /* Otherwise, the image should actually be stored in
641 * intelImage->base.Data. This is pretty confusing for
642 * everybody, I'd much prefer to separate the two functions of
643 * texImage->Data - storage for texture images in main memory
644 * and access (ie mappings) of images. In other words, we'd
645 * create a new texImage->Map field and leave Data simply for
648 assert(intelImage
->base
.Data
);
653 _mesa_get_compressed_teximage(ctx
, target
, level
, pixels
,
656 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
662 if (intelImage
->mt
) {
663 intel_miptree_image_unmap(intel
, intelImage
->mt
);
664 intelImage
->base
.Data
= NULL
;
669 intelGetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
670 GLenum format
, GLenum type
, GLvoid
* pixels
,
671 struct gl_texture_object
*texObj
,
672 struct gl_texture_image
*texImage
)
674 intel_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
675 texObj
, texImage
, 0);
681 intelGetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
683 struct gl_texture_object
*texObj
,
684 struct gl_texture_image
*texImage
)
686 intel_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
687 texObj
, texImage
, 1);
691 intelSetTexOffset(__DRIcontext
*pDRICtx
, GLint texname
,
692 unsigned long long offset
, GLint depth
, GLuint pitch
)
694 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
695 struct gl_texture_object
*tObj
= _mesa_lookup_texture(&intel
->ctx
, texname
);
696 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
702 intel_miptree_release(intel
, &intelObj
->mt
);
704 intelObj
->imageOverride
= GL_TRUE
;
705 intelObj
->depthOverride
= depth
;
706 intelObj
->pitchOverride
= pitch
;
709 intelObj
->textureOffset
= offset
;
713 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
715 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
716 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
717 struct intel_texture_object
*intelObj
;
718 struct intel_texture_image
*intelImage
;
719 struct intel_mipmap_tree
*mt
;
720 struct intel_renderbuffer
*rb
;
721 struct gl_texture_unit
*texUnit
;
722 struct gl_texture_object
*texObj
;
723 struct gl_texture_image
*texImage
;
724 int level
= 0, type
, format
, internalFormat
;
726 texUnit
= &intel
->ctx
.Texture
.Unit
[intel
->ctx
.Texture
.CurrentUnit
];
727 texObj
= _mesa_select_tex_object(&intel
->ctx
, texUnit
, target
);
728 intelObj
= intel_texture_object(texObj
);
733 intel_update_renderbuffers(pDRICtx
, dPriv
);
735 rb
= intel_fb
->color_rb
[0];
736 /* If the region isn't set, then intel_update_renderbuffers was unable
737 * to get the buffers for the drawable.
739 if (rb
->region
== NULL
)
743 format
= GL_UNSIGNED_BYTE
;
744 internalFormat
= (rb
->region
->cpp
== 3 ? 3 : 4);
746 mt
= intel_miptree_create_for_region(intel
, target
,
748 0, 0, rb
->region
, 1, 0);
752 _mesa_lock_texture(&intel
->ctx
, texObj
);
755 intel_miptree_release(intel
, &intelObj
->mt
);
758 texImage
= _mesa_get_tex_image(&intel
->ctx
, texObj
, target
, level
);
759 _mesa_init_teximage_fields(&intel
->ctx
, target
, texImage
,
760 rb
->region
->width
, rb
->region
->height
, 1,
763 intelImage
= intel_texture_image(texImage
);
764 intelImage
->face
= target_to_face(target
);
765 intelImage
->level
= level
;
766 texImage
->TexFormat
= intelChooseTextureFormat(&intel
->ctx
, internalFormat
,
768 _mesa_set_fetch_functions(texImage
, 2);
769 texImage
->RowStride
= rb
->region
->pitch
;
770 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
772 if (!intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
773 intelImage
->face
, intelImage
->level
)) {
774 fprintf(stderr
, "miptree doesn't match image\n");
777 _mesa_unlock_texture(&intel
->ctx
, texObj
);