12 #include "simple_list.h"
13 #include "texcompress.h"
14 #include "texformat.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_ioctl.h"
25 #include "intel_blit.h"
26 #include "intel_fbo.h"
28 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
30 /* Functions to store texture images. Where possible, mipmap_tree's
31 * will be created or further instantiated with image data, otherwise
32 * images will be stored in malloc'd memory. A validation step is
33 * required to pull those images into a mipmap tree, or otherwise
34 * decide a fallback is required.
53 /* Otherwise, store it in memory if (Border != 0) or (any dimension ==
56 * Otherwise, if max_level >= level >= min_level, create tree with
57 * space for textures from min_level down to max_level.
59 * Otherwise, create tree with space for textures from (level
60 * 0)..(1x1). Consider pruning this tree at a validation if the
64 guess_and_alloc_mipmap_tree(struct intel_context
*intel
,
65 struct intel_texture_object
*intelObj
,
66 struct intel_texture_image
*intelImage
)
70 GLuint width
= intelImage
->base
.Width
;
71 GLuint height
= intelImage
->base
.Height
;
72 GLuint depth
= intelImage
->base
.Depth
;
73 GLuint l2width
, l2height
, l2depth
;
74 GLuint i
, comp_byte
= 0;
76 DBG("%s\n", __FUNCTION__
);
78 if (intelImage
->base
.Border
||
79 ((intelImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
) &&
80 ((intelObj
->base
.WrapS
== GL_CLAMP_TO_BORDER
) ||
81 (intelObj
->base
.WrapT
== GL_CLAMP_TO_BORDER
))))
84 if (intelImage
->level
> intelObj
->base
.BaseLevel
&&
85 (intelImage
->base
.Width
== 1 ||
86 (intelObj
->base
.Target
!= GL_TEXTURE_1D
&&
87 intelImage
->base
.Height
== 1) ||
88 (intelObj
->base
.Target
== GL_TEXTURE_3D
&&
89 intelImage
->base
.Depth
== 1)))
92 /* If this image disrespects BaseLevel, allocate from level zero.
93 * Usually BaseLevel == 0, so it's unlikely to happen.
95 if (intelImage
->level
< intelObj
->base
.BaseLevel
)
98 firstLevel
= intelObj
->base
.BaseLevel
;
101 /* Figure out image dimensions at start level.
103 for (i
= intelImage
->level
; i
> firstLevel
; i
--) {
111 /* Guess a reasonable value for lastLevel. This is probably going
112 * to be wrong fairly often and might mean that we have to look at
113 * resizable buffers, or require that buffers implement lazy
114 * pagetable arrangements.
116 if ((intelObj
->base
.MinFilter
== GL_NEAREST
||
117 intelObj
->base
.MinFilter
== GL_LINEAR
) &&
118 intelImage
->level
== firstLevel
) {
119 lastLevel
= firstLevel
;
122 l2width
= logbase2(width
);
123 l2height
= logbase2(height
);
124 l2depth
= logbase2(depth
);
125 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
128 assert(!intelObj
->mt
);
129 if (intelImage
->base
.IsCompressed
)
130 comp_byte
= intel_compressed_num_bytes(intelImage
->base
.TexFormat
->MesaFormat
);
131 intelObj
->mt
= intel_miptree_create(intel
,
132 intelObj
->base
.Target
,
133 intelImage
->base
.InternalFormat
,
139 intelImage
->base
.TexFormat
->TexelBytes
,
142 DBG("%s - success\n", __FUNCTION__
);
149 target_to_face(GLenum target
)
152 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
153 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
154 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
155 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
156 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
157 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
158 return ((GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
164 /* There are actually quite a few combinations this will work for,
165 * more than what I've listed here.
168 check_pbo_format(GLint internalFormat
,
169 GLenum format
, GLenum type
,
170 const struct gl_texture_format
*mesa_format
)
172 switch (internalFormat
) {
175 return (format
== GL_BGRA
&&
176 (type
== GL_UNSIGNED_BYTE
||
177 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
178 mesa_format
== &_mesa_texformat_argb8888
);
181 return (format
== GL_RGB
&&
182 type
== GL_UNSIGNED_SHORT_5_6_5
&&
183 mesa_format
== &_mesa_texformat_rgb565
);
185 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
192 /* XXX: Do this for TexSubImage also:
195 try_pbo_upload(struct intel_context
*intel
,
196 struct intel_texture_image
*intelImage
,
197 const struct gl_pixelstore_attrib
*unpack
,
198 GLint internalFormat
,
199 GLint width
, GLint height
,
200 GLenum format
, GLenum type
, const void *pixels
)
202 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
203 GLuint src_offset
, src_stride
;
204 GLuint dst_offset
, dst_stride
;
207 intel
->ctx
._ImageTransferState
||
208 unpack
->SkipPixels
|| unpack
->SkipRows
) {
209 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
213 src_offset
= (GLuint
) pixels
;
215 if (unpack
->RowLength
> 0)
216 src_stride
= unpack
->RowLength
;
220 dst_offset
= intel_miptree_image_offset(intelImage
->mt
,
224 dst_stride
= intelImage
->mt
->pitch
;
226 intelFlush(&intel
->ctx
);
227 LOCK_HARDWARE(intel
);
229 dri_bo
*src_buffer
= intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
230 dri_bo
*dst_buffer
= intel_region_buffer(intel
,
231 intelImage
->mt
->region
,
235 intelEmitCopyBlit(intel
,
237 src_stride
, src_buffer
, src_offset
, GL_FALSE
,
238 dst_stride
, dst_buffer
, dst_offset
, GL_FALSE
,
239 0, 0, 0, 0, width
, height
,
242 intel_batchbuffer_flush(intel
->batch
);
244 UNLOCK_HARDWARE(intel
);
252 try_pbo_zcopy(struct intel_context
*intel
,
253 struct intel_texture_image
*intelImage
,
254 const struct gl_pixelstore_attrib
*unpack
,
255 GLint internalFormat
,
256 GLint width
, GLint height
,
257 GLenum format
, GLenum type
, const void *pixels
)
259 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
260 GLuint src_offset
, src_stride
;
261 GLuint dst_offset
, dst_stride
;
264 intel
->ctx
._ImageTransferState
||
265 unpack
->SkipPixels
|| unpack
->SkipRows
) {
266 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
270 src_offset
= (GLuint
) pixels
;
272 if (unpack
->RowLength
> 0)
273 src_stride
= unpack
->RowLength
;
277 dst_offset
= intel_miptree_image_offset(intelImage
->mt
,
281 dst_stride
= intelImage
->mt
->pitch
;
283 if (src_stride
!= dst_stride
|| dst_offset
!= 0 || src_offset
!= 0) {
284 _mesa_printf("%s: failure 2\n", __FUNCTION__
);
288 intel_region_attach_pbo(intel
, intelImage
->mt
->region
, pbo
);
299 intelTexImage(GLcontext
* ctx
,
301 GLenum target
, GLint level
,
302 GLint internalFormat
,
303 GLint width
, GLint height
, GLint depth
,
305 GLenum format
, GLenum type
, const void *pixels
,
306 const struct gl_pixelstore_attrib
*unpack
,
307 struct gl_texture_object
*texObj
,
308 struct gl_texture_image
*texImage
, GLsizei imageSize
, int compressed
)
310 struct intel_context
*intel
= intel_context(ctx
);
311 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
312 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
313 GLint postConvWidth
= width
;
314 GLint postConvHeight
= height
;
315 GLint texelBytes
, sizeInBytes
;
316 GLuint dstRowStride
, srcRowStride
= texImage
->RowStride
;
319 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
320 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
324 intelImage
->face
= target_to_face(target
);
325 intelImage
->level
= level
;
327 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
328 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
332 /* choose the texture format */
333 texImage
->TexFormat
= intelChooseTextureFormat(ctx
, internalFormat
,
336 _mesa_set_fetch_functions(texImage
, dims
);
338 if (texImage
->TexFormat
->TexelBytes
== 0) {
339 /* must be a compressed format */
341 texImage
->IsCompressed
= GL_TRUE
;
342 texImage
->CompressedSize
=
343 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
344 texImage
->Height
, texImage
->Depth
,
345 texImage
->TexFormat
->MesaFormat
);
347 texelBytes
= texImage
->TexFormat
->TexelBytes
;
349 /* Minimum pitch of 32 bytes */
350 if (postConvWidth
* texelBytes
< 32) {
351 postConvWidth
= 32 / texelBytes
;
352 texImage
->RowStride
= postConvWidth
;
355 if (!intelImage
->mt
) {
356 assert(texImage
->RowStride
== postConvWidth
);
360 /* Release the reference to a potentially orphaned buffer.
361 * Release any old malloced memory.
363 if (intelImage
->mt
) {
364 intel_miptree_release(intel
, &intelImage
->mt
);
365 assert(!texImage
->Data
);
367 else if (texImage
->Data
) {
368 _mesa_free_texmemory(texImage
->Data
);
369 texImage
->Data
= NULL
;
372 /* If this is the only texture image in the tree, could call
373 * bmBufferData with NULL data to free the old block and avoid
374 * waiting on any outstanding fences.
377 intelObj
->mt
->first_level
== level
&&
378 intelObj
->mt
->last_level
== level
&&
379 intelObj
->mt
->target
!= GL_TEXTURE_CUBE_MAP_ARB
&&
380 !intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
381 intelImage
->face
, intelImage
->level
)) {
384 intel_miptree_release(intel
, &intelObj
->mt
);
385 assert(!intelObj
->mt
);
389 guess_and_alloc_mipmap_tree(intel
, intelObj
, intelImage
);
391 DBG("guess_and_alloc_mipmap_tree: failed\n");
395 assert(!intelImage
->mt
);
398 intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
399 intelImage
->face
, intelImage
->level
)) {
401 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
402 assert(intelImage
->mt
);
406 DBG("XXX: Image did not fit into tree - storing in local memory!\n");
412 intel_buffer_object(unpack
->BufferObj
) &&
413 check_pbo_format(internalFormat
, format
,
414 type
, intelImage
->base
.TexFormat
)) {
416 DBG("trying pbo upload\n");
418 /* Attempt to texture directly from PBO data (zero copy upload).
420 * Currently disable as it can lead to worse as well as better
421 * performance (in particular when intel_region_cow() is
424 if (intelObj
->mt
== intelImage
->mt
&&
425 intelObj
->mt
->first_level
== level
&&
426 intelObj
->mt
->last_level
== level
) {
428 if (try_pbo_zcopy(intel
, intelImage
, unpack
,
430 width
, height
, format
, type
, pixels
)) {
432 DBG("pbo zcopy upload succeeded\n");
438 /* Otherwise, attempt to use the blitter for PBO image uploads.
440 if (try_pbo_upload(intel
, intelImage
, unpack
,
442 width
, height
, format
, type
, pixels
)) {
443 DBG("pbo upload succeeded\n");
447 DBG("pbo upload failed\n");
452 /* intelCopyTexImage calls this function with pixels == NULL, with
453 * the expectation that the mipmap tree will be set up but nothing
454 * more will be done. This is where those calls return:
457 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
459 "glCompressedTexImage");
461 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
463 pixels
, unpack
, "glTexImage");
466 LOCK_HARDWARE(intel
);
468 if (intelImage
->mt
) {
469 texImage
->Data
= intel_miptree_image_map(intel
,
474 intelImage
->base
.ImageOffsets
);
475 texImage
->RowStride
= dstRowStride
/ intelImage
->mt
->cpp
;
478 /* Allocate regular memory and store the image there temporarily. */
479 if (texImage
->IsCompressed
) {
480 sizeInBytes
= texImage
->CompressedSize
;
482 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
486 dstRowStride
= postConvWidth
* texelBytes
;
487 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
490 texImage
->Data
= _mesa_alloc_texmemory(sizeInBytes
);
493 DBG("Upload image %dx%dx%d row_len %d "
495 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
497 /* Copy data. Would like to know when it's ok for us to eg. use
498 * the blitter to copy. Or, use the hardware to do the format
499 * conversion and copy:
503 if (intelImage
->mt
) {
504 struct intel_region
*dst
= intelImage
->mt
->region
;
505 _mesa_copy_rect(texImage
->Data
, dst
->cpp
, dst
->pitch
,
507 intelImage
->mt
->level
[level
].width
,
508 intelImage
->mt
->level
[level
].height
/4,
513 memcpy(texImage
->Data
, pixels
, imageSize
);
514 } else if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
515 texImage
->_BaseFormat
,
517 texImage
->Data
, 0, 0, 0, /* dstX/Y/Zoffset */
519 texImage
->ImageOffsets
,
520 width
, height
, depth
,
521 format
, type
, pixels
, unpack
)) {
522 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
526 /* GL_SGIS_generate_mipmap */
527 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
528 intel_generate_mipmap(ctx
, target
, texObj
);
531 _mesa_unmap_teximage_pbo(ctx
, unpack
);
533 if (intelImage
->mt
) {
534 intel_miptree_image_unmap(intel
, intelImage
->mt
);
535 texImage
->Data
= NULL
;
538 UNLOCK_HARDWARE(intel
);
542 intelTexImage3D(GLcontext
* ctx
,
543 GLenum target
, GLint level
,
544 GLint internalFormat
,
545 GLint width
, GLint height
, GLint depth
,
547 GLenum format
, GLenum type
, const void *pixels
,
548 const struct gl_pixelstore_attrib
*unpack
,
549 struct gl_texture_object
*texObj
,
550 struct gl_texture_image
*texImage
)
552 intelTexImage(ctx
, 3, target
, level
,
553 internalFormat
, width
, height
, depth
, border
,
554 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
559 intelTexImage2D(GLcontext
* ctx
,
560 GLenum target
, GLint level
,
561 GLint internalFormat
,
562 GLint width
, GLint height
, GLint border
,
563 GLenum format
, GLenum type
, const void *pixels
,
564 const struct gl_pixelstore_attrib
*unpack
,
565 struct gl_texture_object
*texObj
,
566 struct gl_texture_image
*texImage
)
568 intelTexImage(ctx
, 2, target
, level
,
569 internalFormat
, width
, height
, 1, border
,
570 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
574 intelTexImage1D(GLcontext
* ctx
,
575 GLenum target
, GLint level
,
576 GLint internalFormat
,
577 GLint width
, GLint border
,
578 GLenum format
, GLenum type
, const void *pixels
,
579 const struct gl_pixelstore_attrib
*unpack
,
580 struct gl_texture_object
*texObj
,
581 struct gl_texture_image
*texImage
)
583 intelTexImage(ctx
, 1, target
, level
,
584 internalFormat
, width
, 1, 1, border
,
585 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
588 void intelCompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
589 GLint internalFormat
,
590 GLint width
, GLint height
, GLint border
,
591 GLsizei imageSize
, const GLvoid
*data
,
592 struct gl_texture_object
*texObj
,
593 struct gl_texture_image
*texImage
)
595 intelTexImage(ctx
, 2, target
, level
,
596 internalFormat
, width
, height
, 1, border
,
597 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
601 * Need to map texture image into memory before copying image data,
605 intel_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
606 GLenum format
, GLenum type
, GLvoid
* pixels
,
607 struct gl_texture_object
*texObj
,
608 struct gl_texture_image
*texImage
, int compressed
)
610 struct intel_context
*intel
= intel_context(ctx
);
611 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
614 if (intelImage
->mt
) {
615 /* Image is stored in hardware format in a buffer managed by the
616 * kernel. Need to explicitly map and unmap it.
618 intelImage
->base
.Data
=
619 intel_miptree_image_map(intel
,
623 &intelImage
->base
.RowStride
,
624 intelImage
->base
.ImageOffsets
);
625 intelImage
->base
.RowStride
/= intelImage
->mt
->cpp
;
628 /* Otherwise, the image should actually be stored in
629 * intelImage->base.Data. This is pretty confusing for
630 * everybody, I'd much prefer to separate the two functions of
631 * texImage->Data - storage for texture images in main memory
632 * and access (ie mappings) of images. In other words, we'd
633 * create a new texImage->Map field and leave Data simply for
636 assert(intelImage
->base
.Data
);
641 _mesa_get_compressed_teximage(ctx
, target
, level
, pixels
,
644 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
650 if (intelImage
->mt
) {
651 intel_miptree_image_unmap(intel
, intelImage
->mt
);
652 intelImage
->base
.Data
= NULL
;
657 intelGetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
658 GLenum format
, GLenum type
, GLvoid
* pixels
,
659 struct gl_texture_object
*texObj
,
660 struct gl_texture_image
*texImage
)
662 intel_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
663 texObj
, texImage
, 0);
669 intelGetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
671 struct gl_texture_object
*texObj
,
672 struct gl_texture_image
*texImage
)
674 intel_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
675 texObj
, texImage
, 1);
679 intelSetTexOffset(__DRIcontext
*pDRICtx
, GLint texname
,
680 unsigned long long offset
, GLint depth
, GLuint pitch
)
682 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
683 struct gl_texture_object
*tObj
= _mesa_lookup_texture(&intel
->ctx
, texname
);
684 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
690 intel_miptree_release(intel
, &intelObj
->mt
);
692 intelObj
->imageOverride
= GL_TRUE
;
693 intelObj
->depthOverride
= depth
;
694 intelObj
->pitchOverride
= pitch
;
697 intelObj
->textureOffset
= offset
;
701 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
703 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
704 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
705 struct intel_texture_object
*intelObj
;
706 struct intel_texture_image
*intelImage
;
707 struct intel_mipmap_tree
*mt
;
708 struct intel_renderbuffer
*rb
;
709 struct gl_texture_unit
*texUnit
;
710 struct gl_texture_object
*texObj
;
711 struct gl_texture_image
*texImage
;
712 int level
= 0, type
, format
, internalFormat
;
714 texUnit
= &intel
->ctx
.Texture
.Unit
[intel
->ctx
.Texture
.CurrentUnit
];
715 texObj
= _mesa_select_tex_object(&intel
->ctx
, texUnit
, target
);
716 intelObj
= intel_texture_object(texObj
);
721 __driParseEvents(pDRICtx
, dPriv
);
723 rb
= intel_fb
->color_rb
[0];
725 format
= GL_UNSIGNED_BYTE
;
726 internalFormat
= (rb
->region
->cpp
== 3 ? 3 : 4);
728 mt
= intel_miptree_create_for_region(intel
, target
,
730 0, 0, rb
->region
, 1, 0);
734 _mesa_lock_texture(&intel
->ctx
, texObj
);
737 intel_miptree_release(intel
, &intelObj
->mt
);
740 texImage
= _mesa_get_tex_image(&intel
->ctx
, texObj
, target
, level
);
741 _mesa_init_teximage_fields(&intel
->ctx
, target
, texImage
,
742 rb
->region
->pitch
, rb
->region
->height
, 1,
745 intelImage
= intel_texture_image(texImage
);
746 intelImage
->face
= target_to_face(target
);
747 intelImage
->level
= level
;
748 texImage
->TexFormat
= intelChooseTextureFormat(&intel
->ctx
, internalFormat
,
750 _mesa_set_fetch_functions(texImage
, 2);
751 texImage
->RowStride
= rb
->region
->pitch
;
752 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
754 if (!intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
755 intelImage
->face
, intelImage
->level
)) {
756 fprintf(stderr
, "miptree doesn't match image\n");
759 _mesa_unlock_texture(&intel
->ctx
, texObj
);