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 UNLOCK_HARDWARE(intel
);
247 try_pbo_zcopy(struct intel_context
*intel
,
248 struct intel_texture_image
*intelImage
,
249 const struct gl_pixelstore_attrib
*unpack
,
250 GLint internalFormat
,
251 GLint width
, GLint height
,
252 GLenum format
, GLenum type
, const void *pixels
)
254 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
255 GLuint src_offset
, src_stride
;
256 GLuint dst_offset
, dst_stride
;
259 intel
->ctx
._ImageTransferState
||
260 unpack
->SkipPixels
|| unpack
->SkipRows
) {
261 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
265 src_offset
= (GLuint
) pixels
;
267 if (unpack
->RowLength
> 0)
268 src_stride
= unpack
->RowLength
;
272 dst_offset
= intel_miptree_image_offset(intelImage
->mt
,
276 dst_stride
= intelImage
->mt
->pitch
;
278 if (src_stride
!= dst_stride
|| dst_offset
!= 0 || src_offset
!= 0) {
279 _mesa_printf("%s: failure 2\n", __FUNCTION__
);
283 intel_region_attach_pbo(intel
, intelImage
->mt
->region
, pbo
);
294 intelTexImage(GLcontext
* ctx
,
296 GLenum target
, GLint level
,
297 GLint internalFormat
,
298 GLint width
, GLint height
, GLint depth
,
300 GLenum format
, GLenum type
, const void *pixels
,
301 const struct gl_pixelstore_attrib
*unpack
,
302 struct gl_texture_object
*texObj
,
303 struct gl_texture_image
*texImage
, GLsizei imageSize
, int compressed
)
305 struct intel_context
*intel
= intel_context(ctx
);
306 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
307 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
308 GLint postConvWidth
= width
;
309 GLint postConvHeight
= height
;
310 GLint texelBytes
, sizeInBytes
;
311 GLuint dstRowStride
, srcRowStride
= texImage
->RowStride
;
314 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
315 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
319 intelImage
->face
= target_to_face(target
);
320 intelImage
->level
= level
;
322 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
323 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
327 /* choose the texture format */
328 texImage
->TexFormat
= intelChooseTextureFormat(ctx
, internalFormat
,
331 _mesa_set_fetch_functions(texImage
, dims
);
333 if (texImage
->TexFormat
->TexelBytes
== 0) {
334 /* must be a compressed format */
336 texImage
->IsCompressed
= GL_TRUE
;
337 texImage
->CompressedSize
=
338 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
339 texImage
->Height
, texImage
->Depth
,
340 texImage
->TexFormat
->MesaFormat
);
342 texelBytes
= texImage
->TexFormat
->TexelBytes
;
344 /* Minimum pitch of 32 bytes */
345 if (postConvWidth
* texelBytes
< 32) {
346 postConvWidth
= 32 / texelBytes
;
347 texImage
->RowStride
= postConvWidth
;
350 if (!intelImage
->mt
) {
351 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_free_texmemory(texImage
->Data
);
364 texImage
->Data
= NULL
;
367 /* If this is the only texture image in the tree, could call
368 * bmBufferData with NULL data to free the old block and avoid
369 * waiting on any outstanding fences.
372 intelObj
->mt
->first_level
== level
&&
373 intelObj
->mt
->last_level
== level
&&
374 intelObj
->mt
->target
!= GL_TEXTURE_CUBE_MAP_ARB
&&
375 !intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
376 intelImage
->face
, intelImage
->level
)) {
379 intel_miptree_release(intel
, &intelObj
->mt
);
380 assert(!intelObj
->mt
);
384 guess_and_alloc_mipmap_tree(intel
, intelObj
, intelImage
);
386 DBG("guess_and_alloc_mipmap_tree: failed\n");
390 assert(!intelImage
->mt
);
393 intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
394 intelImage
->face
, intelImage
->level
)) {
396 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
397 assert(intelImage
->mt
);
401 DBG("XXX: Image did not fit into tree - storing in local memory!\n");
407 intel_buffer_object(unpack
->BufferObj
) &&
408 check_pbo_format(internalFormat
, format
,
409 type
, intelImage
->base
.TexFormat
)) {
411 DBG("trying pbo upload\n");
413 /* Attempt to texture directly from PBO data (zero copy upload).
415 * Currently disable as it can lead to worse as well as better
416 * performance (in particular when intel_region_cow() is
419 if (intelObj
->mt
== intelImage
->mt
&&
420 intelObj
->mt
->first_level
== level
&&
421 intelObj
->mt
->last_level
== level
) {
423 if (try_pbo_zcopy(intel
, intelImage
, unpack
,
425 width
, height
, format
, type
, pixels
)) {
427 DBG("pbo zcopy upload succeeded\n");
433 /* Otherwise, attempt to use the blitter for PBO image uploads.
435 if (try_pbo_upload(intel
, intelImage
, unpack
,
437 width
, height
, format
, type
, pixels
)) {
438 DBG("pbo upload succeeded\n");
442 DBG("pbo upload failed\n");
447 /* intelCopyTexImage calls this function with pixels == NULL, with
448 * the expectation that the mipmap tree will be set up but nothing
449 * more will be done. This is where those calls return:
452 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
454 "glCompressedTexImage");
456 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
458 pixels
, unpack
, "glTexImage");
461 LOCK_HARDWARE(intel
);
463 if (intelImage
->mt
) {
464 texImage
->Data
= intel_miptree_image_map(intel
,
469 intelImage
->base
.ImageOffsets
);
470 texImage
->RowStride
= dstRowStride
/ intelImage
->mt
->cpp
;
473 /* Allocate regular memory and store the image there temporarily. */
474 if (texImage
->IsCompressed
) {
475 sizeInBytes
= texImage
->CompressedSize
;
477 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
481 dstRowStride
= postConvWidth
* texelBytes
;
482 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
485 texImage
->Data
= _mesa_alloc_texmemory(sizeInBytes
);
488 DBG("Upload image %dx%dx%d row_len %d "
490 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
492 /* Copy data. Would like to know when it's ok for us to eg. use
493 * the blitter to copy. Or, use the hardware to do the format
494 * conversion and copy:
498 if (intelImage
->mt
) {
499 struct intel_region
*dst
= intelImage
->mt
->region
;
500 _mesa_copy_rect(texImage
->Data
, dst
->cpp
, dst
->pitch
,
502 intelImage
->mt
->level
[level
].width
,
503 intelImage
->mt
->level
[level
].height
/4,
508 memcpy(texImage
->Data
, pixels
, imageSize
);
509 } else if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
510 texImage
->_BaseFormat
,
512 texImage
->Data
, 0, 0, 0, /* dstX/Y/Zoffset */
514 texImage
->ImageOffsets
,
515 width
, height
, depth
,
516 format
, type
, pixels
, unpack
)) {
517 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
521 /* GL_SGIS_generate_mipmap */
522 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
523 intel_generate_mipmap(ctx
, target
, texObj
);
526 _mesa_unmap_teximage_pbo(ctx
, unpack
);
528 if (intelImage
->mt
) {
529 intel_miptree_image_unmap(intel
, intelImage
->mt
);
530 texImage
->Data
= NULL
;
533 UNLOCK_HARDWARE(intel
);
537 intelTexImage3D(GLcontext
* ctx
,
538 GLenum target
, GLint level
,
539 GLint internalFormat
,
540 GLint width
, GLint height
, GLint depth
,
542 GLenum format
, GLenum type
, const void *pixels
,
543 const struct gl_pixelstore_attrib
*unpack
,
544 struct gl_texture_object
*texObj
,
545 struct gl_texture_image
*texImage
)
547 intelTexImage(ctx
, 3, target
, level
,
548 internalFormat
, width
, height
, depth
, border
,
549 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
554 intelTexImage2D(GLcontext
* ctx
,
555 GLenum target
, GLint level
,
556 GLint internalFormat
,
557 GLint width
, GLint height
, GLint border
,
558 GLenum format
, GLenum type
, const void *pixels
,
559 const struct gl_pixelstore_attrib
*unpack
,
560 struct gl_texture_object
*texObj
,
561 struct gl_texture_image
*texImage
)
563 intelTexImage(ctx
, 2, target
, level
,
564 internalFormat
, width
, height
, 1, border
,
565 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
569 intelTexImage1D(GLcontext
* ctx
,
570 GLenum target
, GLint level
,
571 GLint internalFormat
,
572 GLint width
, GLint border
,
573 GLenum format
, GLenum type
, const void *pixels
,
574 const struct gl_pixelstore_attrib
*unpack
,
575 struct gl_texture_object
*texObj
,
576 struct gl_texture_image
*texImage
)
578 intelTexImage(ctx
, 1, target
, level
,
579 internalFormat
, width
, 1, 1, border
,
580 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
583 void intelCompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
584 GLint internalFormat
,
585 GLint width
, GLint height
, GLint border
,
586 GLsizei imageSize
, const GLvoid
*data
,
587 struct gl_texture_object
*texObj
,
588 struct gl_texture_image
*texImage
)
590 intelTexImage(ctx
, 2, target
, level
,
591 internalFormat
, width
, height
, 1, border
,
592 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
596 * Need to map texture image into memory before copying image data,
600 intel_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
601 GLenum format
, GLenum type
, GLvoid
* pixels
,
602 struct gl_texture_object
*texObj
,
603 struct gl_texture_image
*texImage
, int compressed
)
605 struct intel_context
*intel
= intel_context(ctx
);
606 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
609 if (intelImage
->mt
) {
610 /* Image is stored in hardware format in a buffer managed by the
611 * kernel. Need to explicitly map and unmap it.
613 intelImage
->base
.Data
=
614 intel_miptree_image_map(intel
,
618 &intelImage
->base
.RowStride
,
619 intelImage
->base
.ImageOffsets
);
620 intelImage
->base
.RowStride
/= intelImage
->mt
->cpp
;
623 /* Otherwise, the image should actually be stored in
624 * intelImage->base.Data. This is pretty confusing for
625 * everybody, I'd much prefer to separate the two functions of
626 * texImage->Data - storage for texture images in main memory
627 * and access (ie mappings) of images. In other words, we'd
628 * create a new texImage->Map field and leave Data simply for
631 assert(intelImage
->base
.Data
);
636 _mesa_get_compressed_teximage(ctx
, target
, level
, pixels
,
639 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
645 if (intelImage
->mt
) {
646 intel_miptree_image_unmap(intel
, intelImage
->mt
);
647 intelImage
->base
.Data
= NULL
;
652 intelGetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
653 GLenum format
, GLenum type
, GLvoid
* pixels
,
654 struct gl_texture_object
*texObj
,
655 struct gl_texture_image
*texImage
)
657 intel_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
658 texObj
, texImage
, 0);
664 intelGetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
666 struct gl_texture_object
*texObj
,
667 struct gl_texture_image
*texImage
)
669 intel_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
670 texObj
, texImage
, 1);
674 intelSetTexOffset(__DRIcontext
*pDRICtx
, GLint texname
,
675 unsigned long long offset
, GLint depth
, GLuint pitch
)
677 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
678 struct gl_texture_object
*tObj
= _mesa_lookup_texture(&intel
->ctx
, texname
);
679 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
685 intel_miptree_release(intel
, &intelObj
->mt
);
687 intelObj
->imageOverride
= GL_TRUE
;
688 intelObj
->depthOverride
= depth
;
689 intelObj
->pitchOverride
= pitch
;
692 intelObj
->textureOffset
= offset
;
696 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
698 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
699 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
700 struct intel_texture_object
*intelObj
;
701 struct intel_texture_image
*intelImage
;
702 struct intel_mipmap_tree
*mt
;
703 struct intel_renderbuffer
*rb
;
704 struct gl_texture_unit
*texUnit
;
705 struct gl_texture_object
*texObj
;
706 struct gl_texture_image
*texImage
;
707 int level
= 0, type
, format
, internalFormat
;
709 texUnit
= &intel
->ctx
.Texture
.Unit
[intel
->ctx
.Texture
.CurrentUnit
];
710 texObj
= _mesa_select_tex_object(&intel
->ctx
, texUnit
, target
);
711 intelObj
= intel_texture_object(texObj
);
716 __driParseEvents(pDRICtx
, dPriv
);
718 rb
= intel_fb
->color_rb
[0];
720 format
= GL_UNSIGNED_BYTE
;
721 internalFormat
= (rb
->region
->cpp
== 3 ? 3 : 4);
723 mt
= intel_miptree_create_for_region(intel
, target
,
725 0, 0, rb
->region
, 1, 0);
729 _mesa_lock_texture(&intel
->ctx
, texObj
);
732 intel_miptree_release(intel
, &intelObj
->mt
);
735 texImage
= _mesa_get_tex_image(&intel
->ctx
, texObj
, target
, level
);
736 _mesa_init_teximage_fields(&intel
->ctx
, target
, texImage
,
737 rb
->region
->pitch
, rb
->region
->height
, 1,
740 intelImage
= intel_texture_image(texImage
);
741 intelImage
->face
= target_to_face(target
);
742 intelImage
->level
= level
;
743 texImage
->TexFormat
= intelChooseTextureFormat(&intel
->ctx
, internalFormat
,
745 _mesa_set_fetch_functions(texImage
, 2);
746 texImage
->RowStride
= rb
->region
->pitch
;
747 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
749 if (!intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
750 intelImage
->face
, intelImage
->level
)) {
751 fprintf(stderr
, "miptree doesn't match image\n");
754 _mesa_unlock_texture(&intel
->ctx
, texObj
);