12 #include "simple_list.h"
13 #include "texcompress.h"
14 #include "texformat.h"
18 #include "intel_context.h"
19 #include "intel_mipmap_tree.h"
20 #include "intel_buffer_objects.h"
21 #include "intel_batchbuffer.h"
22 #include "intel_tex.h"
23 #include "intel_ioctl.h"
24 #include "intel_blit.h"
26 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
28 /* Functions to store texture images. Where possible, mipmap_tree's
29 * will be created or further instantiated with image data, otherwise
30 * images will be stored in malloc'd memory. A validation step is
31 * required to pull those images into a mipmap tree, or otherwise
32 * decide a fallback is required.
51 /* Otherwise, store it in memory if (Border != 0) or (any dimension ==
54 * Otherwise, if max_level >= level >= min_level, create tree with
55 * space for textures from min_level down to max_level.
57 * Otherwise, create tree with space for textures from (level
58 * 0)..(1x1). Consider pruning this tree at a validation if the
62 guess_and_alloc_mipmap_tree(struct intel_context
*intel
,
63 struct intel_texture_object
*intelObj
,
64 struct intel_texture_image
*intelImage
)
68 GLuint width
= intelImage
->base
.Width
;
69 GLuint height
= intelImage
->base
.Height
;
70 GLuint depth
= intelImage
->base
.Depth
;
71 GLuint l2width
, l2height
, l2depth
;
72 GLuint i
, comp_byte
= 0;
74 DBG("%s\n", __FUNCTION__
);
76 if (intelImage
->base
.Border
)
79 if (intelImage
->level
> intelObj
->base
.BaseLevel
&&
80 (intelImage
->base
.Width
== 1 ||
81 (intelObj
->base
.Target
!= GL_TEXTURE_1D
&&
82 intelImage
->base
.Height
== 1) ||
83 (intelObj
->base
.Target
== GL_TEXTURE_3D
&&
84 intelImage
->base
.Depth
== 1)))
87 /* If this image disrespects BaseLevel, allocate from level zero.
88 * Usually BaseLevel == 0, so it's unlikely to happen.
90 if (intelImage
->level
< intelObj
->base
.BaseLevel
)
93 firstLevel
= intelObj
->base
.BaseLevel
;
96 /* Figure out image dimensions at start level.
98 for (i
= intelImage
->level
; i
> firstLevel
; i
--) {
106 /* Guess a reasonable value for lastLevel. This is probably going
107 * to be wrong fairly often and might mean that we have to look at
108 * resizable buffers, or require that buffers implement lazy
109 * pagetable arrangements.
111 if ((intelObj
->base
.MinFilter
== GL_NEAREST
||
112 intelObj
->base
.MinFilter
== GL_LINEAR
) &&
113 intelImage
->level
== firstLevel
) {
114 lastLevel
= firstLevel
;
117 l2width
= logbase2(width
);
118 l2height
= logbase2(height
);
119 l2depth
= logbase2(depth
);
120 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
123 assert(!intelObj
->mt
);
124 if (intelImage
->base
.IsCompressed
)
125 comp_byte
= intel_compressed_num_bytes(intelImage
->base
.TexFormat
->MesaFormat
);
126 intelObj
->mt
= intel_miptree_create(intel
,
127 intelObj
->base
.Target
,
128 intelImage
->base
.InternalFormat
,
134 intelImage
->base
.TexFormat
->TexelBytes
,
137 DBG("%s - success\n", __FUNCTION__
);
144 target_to_face(GLenum target
)
147 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
148 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
149 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
150 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
151 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
152 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
153 return ((GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
159 /* There are actually quite a few combinations this will work for,
160 * more than what I've listed here.
163 check_pbo_format(GLint internalFormat
,
164 GLenum format
, GLenum type
,
165 const struct gl_texture_format
*mesa_format
)
167 switch (internalFormat
) {
170 return (format
== GL_BGRA
&&
171 (type
== GL_UNSIGNED_BYTE
||
172 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
173 mesa_format
== &_mesa_texformat_argb8888
);
176 return (format
== GL_RGB
&&
177 type
== GL_UNSIGNED_SHORT_5_6_5
&&
178 mesa_format
== &_mesa_texformat_rgb565
);
180 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
187 /* XXX: Do this for TexSubImage also:
190 try_pbo_upload(struct intel_context
*intel
,
191 struct intel_texture_image
*intelImage
,
192 const struct gl_pixelstore_attrib
*unpack
,
193 GLint internalFormat
,
194 GLint width
, GLint height
,
195 GLenum format
, GLenum type
, const void *pixels
)
197 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
198 GLuint src_offset
, src_stride
;
199 GLuint dst_offset
, dst_stride
;
202 intel
->ctx
._ImageTransferState
||
203 unpack
->SkipPixels
|| unpack
->SkipRows
) {
204 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
208 src_offset
= (GLuint
) pixels
;
210 if (unpack
->RowLength
> 0)
211 src_stride
= unpack
->RowLength
;
215 dst_offset
= intel_miptree_image_offset(intelImage
->mt
,
219 dst_stride
= intelImage
->mt
->pitch
;
221 intelFlush(&intel
->ctx
);
222 LOCK_HARDWARE(intel
);
224 dri_bo
*src_buffer
= intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
225 dri_bo
*dst_buffer
= intel_region_buffer(intel
,
226 intelImage
->mt
->region
,
230 intelEmitCopyBlit(intel
,
232 src_stride
, src_buffer
, src_offset
, GL_FALSE
,
233 dst_stride
, dst_buffer
, dst_offset
, GL_FALSE
,
234 0, 0, 0, 0, width
, height
,
237 intel_batchbuffer_flush(intel
->batch
);
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
;
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 assert(texImage
->RowStride
== postConvWidth
);
353 /* Release the reference to a potentially orphaned buffer.
354 * Release any old malloced memory.
356 if (intelImage
->mt
) {
357 intel_miptree_release(intel
, &intelImage
->mt
);
358 assert(!texImage
->Data
);
360 else if (texImage
->Data
) {
361 _mesa_align_free(texImage
->Data
);
364 /* If this is the only texture image in the tree, could call
365 * bmBufferData with NULL data to free the old block and avoid
366 * waiting on any outstanding fences.
369 intelObj
->mt
->first_level
== level
&&
370 intelObj
->mt
->last_level
== level
&&
371 intelObj
->mt
->target
!= GL_TEXTURE_CUBE_MAP_ARB
&&
372 !intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
373 intelImage
->face
, intelImage
->level
)) {
376 intel_miptree_release(intel
, &intelObj
->mt
);
377 assert(!intelObj
->mt
);
381 guess_and_alloc_mipmap_tree(intel
, intelObj
, intelImage
);
383 DBG("guess_and_alloc_mipmap_tree: failed\n");
387 assert(!intelImage
->mt
);
390 intel_miptree_match_image(intelObj
->mt
, &intelImage
->base
,
391 intelImage
->face
, intelImage
->level
)) {
393 intel_miptree_reference(&intelImage
->mt
, intelObj
->mt
);
394 assert(intelImage
->mt
);
398 DBG("XXX: Image did not fit into tree - storing in local memory!\n");
404 intel_buffer_object(unpack
->BufferObj
) &&
405 check_pbo_format(internalFormat
, format
,
406 type
, intelImage
->base
.TexFormat
)) {
408 DBG("trying pbo upload\n");
410 /* Attempt to texture directly from PBO data (zero copy upload).
412 * Currently disable as it can lead to worse as well as better
413 * performance (in particular when intel_region_cow() is
416 if (intelObj
->mt
== intelImage
->mt
&&
417 intelObj
->mt
->first_level
== level
&&
418 intelObj
->mt
->last_level
== level
) {
420 if (try_pbo_zcopy(intel
, intelImage
, unpack
,
422 width
, height
, format
, type
, pixels
)) {
424 DBG("pbo zcopy upload succeeded\n");
430 /* Otherwise, attempt to use the blitter for PBO image uploads.
432 if (try_pbo_upload(intel
, intelImage
, unpack
,
434 width
, height
, format
, type
, pixels
)) {
435 DBG("pbo upload succeeded\n");
439 DBG("pbo upload failed\n");
444 /* intelCopyTexImage calls this function with pixels == NULL, with
445 * the expectation that the mipmap tree will be set up but nothing
446 * more will be done. This is where those calls return:
449 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
451 "glCompressedTexImage");
453 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
455 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:
496 memcpy(texImage
->Data
, pixels
, imageSize
);
497 } else if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
498 texImage
->_BaseFormat
,
500 texImage
->Data
, 0, 0, 0, /* dstX/Y/Zoffset */
502 texImage
->ImageOffsets
,
503 width
, height
, depth
,
504 format
, type
, pixels
, unpack
)) {
505 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
508 /* GL_SGIS_generate_mipmap */
509 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
510 intel_generate_mipmap(ctx
, target
,
511 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
515 _mesa_unmap_teximage_pbo(ctx
, unpack
);
517 if (intelImage
->mt
) {
518 intel_miptree_image_unmap(intel
, intelImage
->mt
);
519 texImage
->Data
= NULL
;
522 UNLOCK_HARDWARE(intel
);
526 intelTexImage3D(GLcontext
* ctx
,
527 GLenum target
, GLint level
,
528 GLint internalFormat
,
529 GLint width
, GLint height
, GLint depth
,
531 GLenum format
, GLenum type
, const void *pixels
,
532 const struct gl_pixelstore_attrib
*unpack
,
533 struct gl_texture_object
*texObj
,
534 struct gl_texture_image
*texImage
)
536 intelTexImage(ctx
, 3, target
, level
,
537 internalFormat
, width
, height
, depth
, border
,
538 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
543 intelTexImage2D(GLcontext
* ctx
,
544 GLenum target
, GLint level
,
545 GLint internalFormat
,
546 GLint width
, GLint height
, GLint border
,
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
, 2, target
, level
,
553 internalFormat
, width
, height
, 1, border
,
554 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
558 intelTexImage1D(GLcontext
* ctx
,
559 GLenum target
, GLint level
,
560 GLint internalFormat
,
561 GLint width
, GLint border
,
562 GLenum format
, GLenum type
, const void *pixels
,
563 const struct gl_pixelstore_attrib
*unpack
,
564 struct gl_texture_object
*texObj
,
565 struct gl_texture_image
*texImage
)
567 intelTexImage(ctx
, 1, target
, level
,
568 internalFormat
, width
, 1, 1, border
,
569 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
572 void intelCompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
573 GLint internalFormat
,
574 GLint width
, GLint height
, GLint border
,
575 GLsizei imageSize
, const GLvoid
*data
,
576 struct gl_texture_object
*texObj
,
577 struct gl_texture_image
*texImage
)
579 intelTexImage(ctx
, 2, target
, level
,
580 internalFormat
, width
, height
, 1, border
,
581 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
585 * Need to map texture image into memory before copying image data,
589 intel_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
590 GLenum format
, GLenum type
, GLvoid
* pixels
,
591 struct gl_texture_object
*texObj
,
592 struct gl_texture_image
*texImage
, int compressed
)
594 struct intel_context
*intel
= intel_context(ctx
);
595 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
598 if (intelImage
->mt
) {
599 /* Image is stored in hardware format in a buffer managed by the
600 * kernel. Need to explicitly map and unmap it.
602 intelImage
->base
.Data
=
603 intel_miptree_image_map(intel
,
607 &intelImage
->base
.RowStride
,
608 intelImage
->base
.ImageOffsets
);
609 intelImage
->base
.RowStride
/= intelImage
->mt
->cpp
;
612 /* Otherwise, the image should actually be stored in
613 * intelImage->base.Data. This is pretty confusing for
614 * everybody, I'd much prefer to separate the two functions of
615 * texImage->Data - storage for texture images in main memory
616 * and access (ie mappings) of images. In other words, we'd
617 * create a new texImage->Map field and leave Data simply for
620 assert(intelImage
->base
.Data
);
625 _mesa_get_compressed_teximage(ctx
, target
, level
, pixels
,
628 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
634 if (intelImage
->mt
) {
635 intel_miptree_image_unmap(intel
, intelImage
->mt
);
636 intelImage
->base
.Data
= NULL
;
641 intelGetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
642 GLenum format
, GLenum type
, GLvoid
* pixels
,
643 struct gl_texture_object
*texObj
,
644 struct gl_texture_image
*texImage
)
646 intel_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
647 texObj
, texImage
, 0);
653 intelGetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
655 const struct gl_texture_object
*texObj
,
656 const struct gl_texture_image
*texImage
)
658 intel_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
659 texObj
, texImage
, 1);
664 intelSetTexOffset(__DRIcontext
*pDRICtx
, GLint texname
,
665 unsigned long long offset
, GLint depth
, GLuint pitch
)
667 struct intel_context
*intel
= (struct intel_context
*)
668 ((__DRIcontextPrivate
*)pDRICtx
->private)->driverPrivate
;
669 struct gl_texture_object
*tObj
= _mesa_lookup_texture(&intel
->ctx
, texname
);
670 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
676 intel_miptree_release(intel
, &intelObj
->mt
);
678 intelObj
->imageOverride
= GL_TRUE
;
679 intelObj
->depthOverride
= depth
;
680 intelObj
->pitchOverride
= pitch
;
683 intelObj
->textureOffset
= offset
;