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
)
81 if (intelImage
->level
> intelObj
->base
.BaseLevel
&&
82 (intelImage
->base
.Width
== 1 ||
83 (intelObj
->base
.Target
!= GL_TEXTURE_1D
&&
84 intelImage
->base
.Height
== 1) ||
85 (intelObj
->base
.Target
== GL_TEXTURE_3D
&&
86 intelImage
->base
.Depth
== 1)))
89 /* If this image disrespects BaseLevel, allocate from level zero.
90 * Usually BaseLevel == 0, so it's unlikely to happen.
92 if (intelImage
->level
< intelObj
->base
.BaseLevel
)
95 firstLevel
= intelObj
->base
.BaseLevel
;
98 /* Figure out image dimensions at start level.
100 for (i
= intelImage
->level
; i
> firstLevel
; i
--) {
108 /* Guess a reasonable value for lastLevel. This is probably going
109 * to be wrong fairly often and might mean that we have to look at
110 * resizable buffers, or require that buffers implement lazy
111 * pagetable arrangements.
113 if ((intelObj
->base
.MinFilter
== GL_NEAREST
||
114 intelObj
->base
.MinFilter
== GL_LINEAR
) &&
115 intelImage
->level
== firstLevel
) {
116 lastLevel
= firstLevel
;
119 l2width
= logbase2(width
);
120 l2height
= logbase2(height
);
121 l2depth
= logbase2(depth
);
122 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
125 assert(!intelObj
->mt
);
126 if (intelImage
->base
.IsCompressed
)
127 comp_byte
= intel_compressed_num_bytes(intelImage
->base
.TexFormat
->MesaFormat
);
128 intelObj
->mt
= intel_miptree_create(intel
,
129 intelObj
->base
.Target
,
130 intelImage
->base
.InternalFormat
,
136 intelImage
->base
.TexFormat
->TexelBytes
,
139 DBG("%s - success\n", __FUNCTION__
);
146 target_to_face(GLenum target
)
149 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
150 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
151 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
152 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
153 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
154 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
155 return ((GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
161 /* There are actually quite a few combinations this will work for,
162 * more than what I've listed here.
165 check_pbo_format(GLint internalFormat
,
166 GLenum format
, GLenum type
,
167 const struct gl_texture_format
*mesa_format
)
169 switch (internalFormat
) {
172 return (format
== GL_BGRA
&&
173 (type
== GL_UNSIGNED_BYTE
||
174 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
175 mesa_format
== &_mesa_texformat_argb8888
);
178 return (format
== GL_RGB
&&
179 type
== GL_UNSIGNED_SHORT_5_6_5
&&
180 mesa_format
== &_mesa_texformat_rgb565
);
182 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
189 /* XXX: Do this for TexSubImage also:
192 try_pbo_upload(struct intel_context
*intel
,
193 struct intel_texture_image
*intelImage
,
194 const struct gl_pixelstore_attrib
*unpack
,
195 GLint internalFormat
,
196 GLint width
, GLint height
,
197 GLenum format
, GLenum type
, const void *pixels
)
199 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
200 GLuint src_offset
, src_stride
;
201 GLuint dst_offset
, dst_stride
;
204 intel
->ctx
._ImageTransferState
||
205 unpack
->SkipPixels
|| unpack
->SkipRows
) {
206 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
210 src_offset
= (GLuint
) pixels
;
212 if (unpack
->RowLength
> 0)
213 src_stride
= unpack
->RowLength
;
217 dst_offset
= intel_miptree_image_offset(intelImage
->mt
,
221 dst_stride
= intelImage
->mt
->pitch
;
223 intelFlush(&intel
->ctx
);
224 LOCK_HARDWARE(intel
);
226 dri_bo
*src_buffer
= intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
227 dri_bo
*dst_buffer
= intel_region_buffer(intel
,
228 intelImage
->mt
->region
,
232 intelEmitCopyBlit(intel
,
234 src_stride
, src_buffer
, src_offset
, GL_FALSE
,
235 dst_stride
, dst_buffer
, dst_offset
, GL_FALSE
,
236 0, 0, 0, 0, width
, height
,
239 intel_batchbuffer_flush(intel
->batch
);
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 assert(texImage
->RowStride
== postConvWidth
);
355 /* Release the reference to a potentially orphaned buffer.
356 * Release any old malloced memory.
358 if (intelImage
->mt
) {
359 intel_miptree_release(intel
, &intelImage
->mt
);
360 assert(!texImage
->Data
);
362 else if (texImage
->Data
) {
363 _mesa_align_free(texImage
->Data
);
366 /* If this is the only texture image in the tree, could call
367 * bmBufferData with NULL data to free the old block and avoid
368 * waiting on any outstanding fences.
371 intelObj
->mt
->first_level
== level
&&
372 intelObj
->mt
->last_level
== level
&&
373 intelObj
->mt
->target
!= GL_TEXTURE_CUBE_MAP_ARB
&&
374 !intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
375 intelImage
->face
, intelImage
->level
)) {
378 intel_miptree_release(intel
, &intelObj
->mt
);
379 assert(!intelObj
->mt
);
383 guess_and_alloc_mipmap_tree(intel
, intelObj
, intelImage
);
385 DBG("guess_and_alloc_mipmap_tree: failed\n");
389 assert(!intelImage
->mt
);
392 intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
393 intelImage
->face
, intelImage
->level
)) {
395 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
396 assert(intelImage
->mt
);
400 DBG("XXX: Image did not fit into tree - storing in local memory!\n");
406 intel_buffer_object(unpack
->BufferObj
) &&
407 check_pbo_format(internalFormat
, format
,
408 type
, intelImage
->base
.TexFormat
)) {
410 DBG("trying pbo upload\n");
412 /* Attempt to texture directly from PBO data (zero copy upload).
414 * Currently disable as it can lead to worse as well as better
415 * performance (in particular when intel_region_cow() is
418 if (intelObj
->mt
== intelImage
->mt
&&
419 intelObj
->mt
->first_level
== level
&&
420 intelObj
->mt
->last_level
== level
) {
422 if (try_pbo_zcopy(intel
, intelImage
, unpack
,
424 width
, height
, format
, type
, pixels
)) {
426 DBG("pbo zcopy upload succeeded\n");
432 /* Otherwise, attempt to use the blitter for PBO image uploads.
434 if (try_pbo_upload(intel
, intelImage
, unpack
,
436 width
, height
, format
, type
, pixels
)) {
437 DBG("pbo upload succeeded\n");
441 DBG("pbo upload failed\n");
446 /* intelCopyTexImage calls this function with pixels == NULL, with
447 * the expectation that the mipmap tree will be set up but nothing
448 * more will be done. This is where those calls return:
451 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
453 "glCompressedTexImage");
455 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
457 pixels
, unpack
, "glTexImage");
460 LOCK_HARDWARE(intel
);
462 if (intelImage
->mt
) {
463 texImage
->Data
= intel_miptree_image_map(intel
,
468 intelImage
->base
.ImageOffsets
);
469 texImage
->RowStride
= dstRowStride
/ intelImage
->mt
->cpp
;
472 /* Allocate regular memory and store the image there temporarily. */
473 if (texImage
->IsCompressed
) {
474 sizeInBytes
= texImage
->CompressedSize
;
476 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
480 dstRowStride
= postConvWidth
* texelBytes
;
481 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
484 texImage
->Data
= malloc(sizeInBytes
);
487 DBG("Upload image %dx%dx%d row_len %d "
489 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
491 /* Copy data. Would like to know when it's ok for us to eg. use
492 * the blitter to copy. Or, use the hardware to do the format
493 * conversion and copy:
497 if (intelImage
->mt
) {
498 struct intel_region
*dst
= intelImage
->mt
->region
;
499 _mesa_copy_rect(texImage
->Data
, dst
->cpp
, dst
->pitch
,
501 intelImage
->mt
->level
[level
].width
,
502 intelImage
->mt
->level
[level
].height
/4,
507 memcpy(texImage
->Data
, pixels
, imageSize
);
508 } else if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
509 texImage
->_BaseFormat
,
511 texImage
->Data
, 0, 0, 0, /* dstX/Y/Zoffset */
513 texImage
->ImageOffsets
,
514 width
, height
, depth
,
515 format
, type
, pixels
, unpack
)) {
516 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
520 /* GL_SGIS_generate_mipmap */
521 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
522 intel_generate_mipmap(ctx
, target
,
523 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
527 _mesa_unmap_teximage_pbo(ctx
, unpack
);
529 if (intelImage
->mt
) {
530 intel_miptree_image_unmap(intel
, intelImage
->mt
);
531 texImage
->Data
= NULL
;
534 UNLOCK_HARDWARE(intel
);
538 intelTexImage3D(GLcontext
* ctx
,
539 GLenum target
, GLint level
,
540 GLint internalFormat
,
541 GLint width
, GLint height
, GLint depth
,
543 GLenum format
, GLenum type
, const void *pixels
,
544 const struct gl_pixelstore_attrib
*unpack
,
545 struct gl_texture_object
*texObj
,
546 struct gl_texture_image
*texImage
)
548 intelTexImage(ctx
, 3, target
, level
,
549 internalFormat
, width
, height
, depth
, border
,
550 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
555 intelTexImage2D(GLcontext
* ctx
,
556 GLenum target
, GLint level
,
557 GLint internalFormat
,
558 GLint width
, GLint height
, GLint border
,
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
, 2, target
, level
,
565 internalFormat
, width
, height
, 1, border
,
566 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
570 intelTexImage1D(GLcontext
* ctx
,
571 GLenum target
, GLint level
,
572 GLint internalFormat
,
573 GLint width
, GLint border
,
574 GLenum format
, GLenum type
, const void *pixels
,
575 const struct gl_pixelstore_attrib
*unpack
,
576 struct gl_texture_object
*texObj
,
577 struct gl_texture_image
*texImage
)
579 intelTexImage(ctx
, 1, target
, level
,
580 internalFormat
, width
, 1, 1, border
,
581 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
584 void intelCompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
585 GLint internalFormat
,
586 GLint width
, GLint height
, GLint border
,
587 GLsizei imageSize
, const GLvoid
*data
,
588 struct gl_texture_object
*texObj
,
589 struct gl_texture_image
*texImage
)
591 intelTexImage(ctx
, 2, target
, level
,
592 internalFormat
, width
, height
, 1, border
,
593 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
597 * Need to map texture image into memory before copying image data,
601 intel_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
602 GLenum format
, GLenum type
, GLvoid
* pixels
,
603 struct gl_texture_object
*texObj
,
604 struct gl_texture_image
*texImage
, int compressed
)
606 struct intel_context
*intel
= intel_context(ctx
);
607 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
610 if (intelImage
->mt
) {
611 /* Image is stored in hardware format in a buffer managed by the
612 * kernel. Need to explicitly map and unmap it.
614 intelImage
->base
.Data
=
615 intel_miptree_image_map(intel
,
619 &intelImage
->base
.RowStride
,
620 intelImage
->base
.ImageOffsets
);
621 intelImage
->base
.RowStride
/= intelImage
->mt
->cpp
;
624 /* Otherwise, the image should actually be stored in
625 * intelImage->base.Data. This is pretty confusing for
626 * everybody, I'd much prefer to separate the two functions of
627 * texImage->Data - storage for texture images in main memory
628 * and access (ie mappings) of images. In other words, we'd
629 * create a new texImage->Map field and leave Data simply for
632 assert(intelImage
->base
.Data
);
637 _mesa_get_compressed_teximage(ctx
, target
, level
, pixels
,
640 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
646 if (intelImage
->mt
) {
647 intel_miptree_image_unmap(intel
, intelImage
->mt
);
648 intelImage
->base
.Data
= NULL
;
653 intelGetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
654 GLenum format
, GLenum type
, GLvoid
* pixels
,
655 struct gl_texture_object
*texObj
,
656 struct gl_texture_image
*texImage
)
658 intel_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
659 texObj
, texImage
, 0);
665 intelGetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
667 struct gl_texture_object
*texObj
,
668 struct gl_texture_image
*texImage
)
670 intel_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
671 texObj
, texImage
, 1);
675 intelSetTexOffset(__DRIcontext
*pDRICtx
, GLint texname
,
676 unsigned long long offset
, GLint depth
, GLuint pitch
)
678 struct intel_context
*intel
= (struct intel_context
*)
679 ((__DRIcontextPrivate
*)pDRICtx
->private)->driverPrivate
;
680 struct gl_texture_object
*tObj
= _mesa_lookup_texture(&intel
->ctx
, texname
);
681 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
687 intel_miptree_release(intel
, &intelObj
->mt
);
689 intelObj
->imageOverride
= GL_TRUE
;
690 intelObj
->depthOverride
= depth
;
691 intelObj
->pitchOverride
= pitch
;
694 intelObj
->textureOffset
= offset
;
698 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*pDraw
)
700 __DRIcontextPrivate
*driContext
= pDRICtx
->private;
701 __DRIdrawablePrivate
*dPriv
= pDraw
->private;
702 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
703 struct intel_context
*intel
= driContext
->driverPrivate
;
704 struct intel_texture_object
*intelObj
;
705 struct intel_texture_image
*intelImage
;
706 struct intel_mipmap_tree
*mt
;
707 struct intel_renderbuffer
*rb
;
708 struct gl_texture_unit
*texUnit
;
709 struct gl_texture_object
*texObj
;
710 struct gl_texture_image
*texImage
;
711 int level
= 0, type
, format
, internalFormat
;
713 texUnit
= &intel
->ctx
.Texture
.Unit
[intel
->ctx
.Texture
.CurrentUnit
];
714 texObj
= _mesa_select_tex_object(&intel
->ctx
, texUnit
, target
);
715 intelObj
= intel_texture_object(texObj
);
720 __driParseEvents(driContext
, dPriv
);
722 rb
= intel_fb
->color_rb
[0];
724 format
= GL_UNSIGNED_BYTE
;
725 internalFormat
= (rb
->region
->cpp
== 3 ? 3 : 4);
727 mt
= intel_miptree_create_for_region(intel
, target
,
729 0, 0, rb
->region
, 1, 0);
733 _mesa_lock_texture(&intel
->ctx
, texObj
);
736 intel_miptree_release(intel
, &intelObj
->mt
);
739 texImage
= _mesa_get_tex_image(&intel
->ctx
, texObj
, target
, level
);
740 _mesa_init_teximage_fields(&intel
->ctx
, target
, texImage
,
741 rb
->region
->pitch
, rb
->region
->height
, 1,
744 intelImage
= intel_texture_image(texImage
);
745 intelImage
->face
= target_to_face(target
);
746 intelImage
->level
= level
;
747 texImage
->TexFormat
= intelChooseTextureFormat(&intel
->ctx
, internalFormat
,
749 _mesa_set_fetch_functions(texImage
, 2);
750 texImage
->RowStride
= rb
->region
->pitch
;
751 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
753 if (!intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
754 intelImage
->face
, intelImage
->level
)) {
755 fprintf(stderr
, "miptree doesn't match image\n");
758 _mesa_unlock_texture(&intel
->ctx
, texObj
);