2 #include "main/macros.h"
3 #include "main/mtypes.h"
4 #include "main/enums.h"
5 #include "main/bufferobj.h"
6 #include "main/context.h"
7 #include "main/formats.h"
8 #include "main/glformats.h"
9 #include "main/image.h"
11 #include "main/renderbuffer.h"
12 #include "main/texcompress.h"
13 #include "main/texgetimage.h"
14 #include "main/texobj.h"
15 #include "main/teximage.h"
16 #include "main/texstore.h"
18 #include "drivers/common/meta.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"
26 #include "intel_image.h"
27 #include "intel_tiled_memcpy.h"
28 #include "brw_context.h"
29 #include "brw_blorp.h"
31 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
33 /* Make sure one doesn't end up shrinking base level zero unnecessarily.
34 * Determining the base level dimension by shifting higher level dimension
35 * ends up in off-by-one value in case base level has NPOT size (for example,
37 * Choose the original base level dimension when shifted dimensions agree.
38 * Otherwise assume real resize is intended and use the new shifted value.
41 get_base_dim(unsigned old_base_dim
, unsigned new_level_dim
, unsigned level
)
43 const unsigned old_level_dim
= old_base_dim
>> level
;
44 const unsigned new_base_dim
= new_level_dim
<< level
;
46 return old_level_dim
== new_level_dim
? old_base_dim
: new_base_dim
;
49 /* Work back from the specified level of the image to the baselevel and create a
50 * miptree of that size.
52 struct intel_mipmap_tree
*
53 intel_miptree_create_for_teximage(struct brw_context
*brw
,
54 struct intel_texture_object
*intelObj
,
55 struct intel_texture_image
*intelImage
,
56 enum intel_miptree_create_flags flags
)
59 int width
, height
, depth
;
60 unsigned old_width
= 0, old_height
= 0, old_depth
= 0;
61 const struct intel_mipmap_tree
*old_mt
= intelObj
->mt
;
62 const unsigned level
= intelImage
->base
.Base
.Level
;
64 intel_get_image_dims(&intelImage
->base
.Base
, &width
, &height
, &depth
);
67 old_width
= old_mt
->surf
.logical_level0_px
.width
;
68 old_height
= old_mt
->surf
.logical_level0_px
.height
;
69 old_depth
= old_mt
->surf
.dim
== ISL_SURF_DIM_3D
?
70 old_mt
->surf
.logical_level0_px
.depth
:
71 old_mt
->surf
.logical_level0_px
.array_len
;
74 DBG("%s\n", __func__
);
76 /* Figure out image dimensions at start level. */
77 switch(intelObj
->base
.Target
) {
78 case GL_TEXTURE_2D_MULTISAMPLE
:
79 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
80 case GL_TEXTURE_RECTANGLE
:
81 case GL_TEXTURE_EXTERNAL_OES
:
85 depth
= old_mt
? get_base_dim(old_depth
, depth
, level
) :
89 case GL_TEXTURE_2D_ARRAY
:
90 case GL_TEXTURE_CUBE_MAP
:
91 case GL_TEXTURE_CUBE_MAP_ARRAY
:
92 height
= old_mt
? get_base_dim(old_height
, height
, level
) :
96 case GL_TEXTURE_1D_ARRAY
:
97 width
= old_mt
? get_base_dim(old_width
, width
, level
) :
101 unreachable("Unexpected target");
104 /* Guess a reasonable value for lastLevel. This is probably going
105 * to be wrong fairly often and might mean that we have to look at
106 * resizable buffers, or require that buffers implement lazy
107 * pagetable arrangements.
109 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
110 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
111 intelImage
->base
.Base
.Level
== 0 &&
112 !intelObj
->base
.GenerateMipmap
) {
115 lastLevel
= _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
116 width
, height
, depth
) - 1;
119 return intel_miptree_create(brw
,
120 intelObj
->base
.Target
,
121 intelImage
->base
.Base
.TexFormat
,
127 MAX2(intelImage
->base
.Base
.NumSamples
, 1),
132 intel_texsubimage_blorp(struct brw_context
*brw
, GLuint dims
,
133 struct gl_texture_image
*tex_image
,
134 unsigned x
, unsigned y
, unsigned z
,
135 unsigned width
, unsigned height
, unsigned depth
,
136 GLenum format
, GLenum type
, const void *pixels
,
137 const struct gl_pixelstore_attrib
*packing
)
139 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
140 const unsigned mt_level
= tex_image
->Level
+ tex_image
->TexObject
->MinLevel
;
141 const unsigned mt_z
= tex_image
->TexObject
->MinLayer
+ tex_image
->Face
+ z
;
143 /* The blorp path can't understand crazy format hackery */
144 if (_mesa_base_tex_format(&brw
->ctx
, tex_image
->InternalFormat
) !=
145 _mesa_get_format_base_format(tex_image
->TexFormat
))
148 return brw_blorp_upload_miptree(brw
, intel_image
->mt
, tex_image
->TexFormat
,
149 mt_level
, x
, y
, mt_z
, width
, height
, depth
,
150 tex_image
->TexObject
->Target
, format
, type
,
155 * \brief A fast path for glTexImage and glTexSubImage.
157 * This fast path is taken when the texture format is BGRA, RGBA,
158 * A or L and when the texture memory is X- or Y-tiled. It uploads
159 * the texture data by mapping the texture memory without a GTT fence, thus
160 * acquiring a tiled view of the memory, and then copying sucessive
161 * spans within each tile.
163 * This is a performance win over the conventional texture upload path because
164 * it avoids the performance penalty of writing through the write-combine
165 * buffer. In the conventional texture upload path,
166 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
167 * fence, thus acquiring a linear view of the memory, then each row in the
168 * image is memcpy'd. In this fast path, we replace each row's copy with
169 * a sequence of copies over each linear span in tile.
171 * One use case is Google Chrome's paint rectangles. Chrome (as
172 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
173 * Each page's content is initially uploaded with glTexImage2D and damaged
174 * regions are updated with glTexSubImage2D. On some workloads, the
175 * performance gain of this fastpath on Sandybridge is over 5x.
178 intel_texsubimage_tiled_memcpy(struct gl_context
* ctx
,
180 struct gl_texture_image
*texImage
,
181 GLint xoffset
, GLint yoffset
, GLint zoffset
,
182 GLsizei width
, GLsizei height
, GLsizei depth
,
183 GLenum format
, GLenum type
,
184 const GLvoid
*pixels
,
185 const struct gl_pixelstore_attrib
*packing
)
187 struct brw_context
*brw
= brw_context(ctx
);
188 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
189 struct intel_texture_image
*image
= intel_texture_image(texImage
);
192 /* The miptree's buffer. */
196 mem_copy_fn mem_copy
= NULL
;
198 /* This fastpath is restricted to specific texture types:
199 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
202 * FINISHME: The restrictions below on packing alignment and packing row
203 * length are likely unneeded now because we calculate the source stride
204 * with _mesa_image_row_stride. However, before removing the restrictions
207 if (!devinfo
->has_llc
||
208 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
209 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
210 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
212 _mesa_is_bufferobj(packing
->BufferObj
) ||
213 packing
->Alignment
> 4 ||
214 packing
->SkipPixels
> 0 ||
215 packing
->SkipRows
> 0 ||
216 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
217 packing
->SwapBytes
||
222 /* Only a simple blit, no scale, bias or other mapping. */
223 if (ctx
->_ImageTransferState
)
226 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
))
229 /* If this is a nontrivial texture view, let another path handle it instead. */
230 if (texImage
->TexObject
->MinLayer
)
234 (image
->mt
->surf
.tiling
!= ISL_TILING_X
&&
235 image
->mt
->surf
.tiling
!= ISL_TILING_Y0
)) {
236 /* The algorithm is written only for X- or Y-tiled memory. */
240 /* linear_to_tiled() assumes that if the object is swizzled, it is using
241 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
242 * true on gen5 and above.
244 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
245 * parts of the memory aren't swizzled at all. Userspace just can't handle
248 if (devinfo
->gen
< 5 && brw
->has_swizzling
)
251 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
253 /* Since we are going to write raw data to the miptree, we need to resolve
254 * any pending fast color clears before we start.
256 assert(image
->mt
->surf
.logical_level0_px
.depth
== 1);
257 assert(image
->mt
->surf
.logical_level0_px
.array_len
== 1);
259 intel_miptree_access_raw(brw
, image
->mt
, level
, 0, true);
263 if (brw_batch_references(&brw
->batch
, bo
)) {
264 perf_debug("Flushing before mapping a referenced bo.\n");
265 intel_batchbuffer_flush(brw
);
268 void *map
= brw_bo_map(brw
, bo
, MAP_WRITE
| MAP_RAW
);
270 DBG("%s: failed to map bo\n", __func__
);
274 src_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
276 /* We postponed printing this message until having committed to executing
279 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
280 "mesa_format=0x%x tiling=%d "
281 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) ",
282 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
283 format
, type
, texImage
->TexFormat
, image
->mt
->surf
.tiling
,
284 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
287 /* Adjust x and y offset based on miplevel */
288 unsigned level_x
, level_y
;
289 intel_miptree_get_image_offset(image
->mt
, level
, 0, &level_x
, &level_y
);
294 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
295 yoffset
, yoffset
+ height
,
297 pixels
- (ptrdiff_t) yoffset
* src_pitch
- (ptrdiff_t) xoffset
* cpp
,
298 image
->mt
->surf
.row_pitch
, src_pitch
,
300 image
->mt
->surf
.tiling
,
310 intel_upload_tex(struct gl_context
* ctx
,
312 struct gl_texture_image
*texImage
,
313 GLint xoffset
, GLint yoffset
, GLint zoffset
,
314 GLsizei width
, GLsizei height
, GLsizei depth
,
315 GLenum format
, GLenum type
,
316 const GLvoid
* pixels
,
317 const struct gl_pixelstore_attrib
*packing
)
319 struct brw_context
*brw
= brw_context(ctx
);
320 struct intel_mipmap_tree
*mt
= intel_texture_image(texImage
)->mt
;
323 /* Check that there is actually data to store. */
324 if (pixels
== NULL
&& !_mesa_is_bufferobj(packing
->BufferObj
))
327 bool tex_busy
= mt
&& brw_bo_busy(mt
->bo
);
329 if (mt
&& mt
->format
== MESA_FORMAT_S_UINT8
)
330 mt
->r8stencil_needs_update
= true;
332 if (_mesa_is_bufferobj(packing
->BufferObj
) || tex_busy
||
333 mt
->aux_usage
== ISL_AUX_USAGE_CCS_E
) {
334 ok
= intel_texsubimage_blorp(brw
, dims
, texImage
,
335 xoffset
, yoffset
, zoffset
,
336 width
, height
, depth
, format
, type
,
342 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
343 xoffset
, yoffset
, zoffset
,
344 width
, height
, depth
,
345 format
, type
, pixels
, packing
);
349 _mesa_store_texsubimage(ctx
, dims
, texImage
,
350 xoffset
, yoffset
, zoffset
,
351 width
, height
, depth
,
352 format
, type
, pixels
, packing
);
357 intelTexImage(struct gl_context
* ctx
,
359 struct gl_texture_image
*texImage
,
360 GLenum format
, GLenum type
, const void *pixels
,
361 const struct gl_pixelstore_attrib
*unpack
)
363 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
364 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
365 _mesa_enum_to_string(texImage
->TexObject
->Target
),
366 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
367 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
369 /* Allocate storage for texture data. */
370 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
371 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
375 assert(intel_texture_image(texImage
)->mt
);
377 intel_upload_tex(ctx
, dims
, texImage
, 0, 0, 0,
378 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
379 format
, type
, pixels
, unpack
);
384 intelTexSubImage(struct gl_context
* ctx
,
386 struct gl_texture_image
*texImage
,
387 GLint xoffset
, GLint yoffset
, GLint zoffset
,
388 GLsizei width
, GLsizei height
, GLsizei depth
,
389 GLenum format
, GLenum type
,
390 const GLvoid
* pixels
,
391 const struct gl_pixelstore_attrib
*packing
)
393 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
394 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
395 _mesa_enum_to_string(texImage
->TexObject
->Target
),
396 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
397 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
399 intel_upload_tex(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
400 width
, height
, depth
, format
, type
, pixels
, packing
);
405 intel_set_texture_image_mt(struct brw_context
*brw
,
406 struct gl_texture_image
*image
,
407 GLenum internal_format
,
408 struct intel_mipmap_tree
*mt
)
411 struct gl_texture_object
*texobj
= image
->TexObject
;
412 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
413 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
415 _mesa_init_teximage_fields(&brw
->ctx
, image
,
416 mt
->surf
.logical_level0_px
.width
,
417 mt
->surf
.logical_level0_px
.height
, 1,
418 0, internal_format
, mt
->format
);
420 brw
->ctx
.Driver
.FreeTextureImageBuffer(&brw
->ctx
, image
);
422 intel_texobj
->needs_validate
= true;
423 intel_image
->base
.RowStride
= mt
->surf
.row_pitch
/ mt
->cpp
;
424 assert(mt
->surf
.row_pitch
% mt
->cpp
== 0);
426 intel_miptree_reference(&intel_image
->mt
, mt
);
428 /* Immediately validate the image to the object. */
429 intel_miptree_reference(&intel_texobj
->mt
, mt
);
434 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
435 GLint texture_format
,
436 __DRIdrawable
*dPriv
)
438 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
439 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
440 struct gl_context
*ctx
= &brw
->ctx
;
441 struct intel_renderbuffer
*rb
;
442 struct gl_texture_object
*texObj
;
443 struct gl_texture_image
*texImage
;
444 GLenum internal_format
= 0;
446 texObj
= _mesa_get_current_tex_object(ctx
, target
);
451 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
452 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
453 intel_update_renderbuffers(pDRICtx
, dPriv
);
455 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
456 /* If the miptree isn't set, then intel_update_renderbuffers was unable
457 * to get the BO for the drawable from the window system.
462 if (rb
->mt
->cpp
== 4) {
463 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
)
464 internal_format
= GL_RGB
;
466 internal_format
= GL_RGBA
;
467 } else if (rb
->mt
->cpp
== 2) {
469 internal_format
= GL_RGB
;
472 intel_miptree_prepare_external(brw
, rb
->mt
);
474 _mesa_lock_texture(&brw
->ctx
, texObj
);
475 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
476 intel_set_texture_image_mt(brw
, texImage
, internal_format
, rb
->mt
);
477 _mesa_unlock_texture(&brw
->ctx
, texObj
);
481 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
482 struct gl_renderbuffer
*rb
,
483 struct gl_texture_image
*image
)
485 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
486 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
487 struct gl_texture_object
*texobj
= image
->TexObject
;
488 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
490 /* We can only handle RB allocated with AllocRenderbufferStorage, or
491 * window-system renderbuffers.
493 assert(!rb
->TexImage
);
498 _mesa_lock_texture(ctx
, texobj
);
499 _mesa_init_teximage_fields(ctx
, image
,
500 rb
->Width
, rb
->Height
, 1,
501 0, rb
->InternalFormat
, rb
->Format
);
502 image
->NumSamples
= rb
->NumSamples
;
504 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
506 /* Immediately validate the image to the object. */
507 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
509 intel_texobj
->needs_validate
= true;
510 _mesa_unlock_texture(ctx
, texobj
);
516 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
518 /* The old interface didn't have the format argument, so copy our
519 * implementation's behavior at the time.
521 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
525 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
526 struct gl_texture_object
*texObj
,
527 struct gl_texture_image
*texImage
,
528 GLeglImageOES image_handle
)
530 struct brw_context
*brw
= brw_context(ctx
);
531 struct intel_mipmap_tree
*mt
;
532 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
535 image
= dri_screen
->dri2
.image
->lookupEGLImage(dri_screen
, image_handle
,
536 dri_screen
->loaderPrivate
);
540 /* We support external textures only for EGLImages created with
541 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
543 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
544 _mesa_error(ctx
, GL_INVALID_OPERATION
,
545 "glEGLImageTargetTexture2DOES(external target is enabled only "
546 "for images created with EGL_EXT_image_dma_buf_import");
550 /* Disallow depth/stencil textures: we don't have a way to pass the
551 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
553 if (image
->has_depthstencil
) {
554 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
558 mt
= intel_miptree_create_for_dri_image(brw
, image
, target
, image
->format
,
563 struct intel_texture_object
*intel_texobj
= intel_texture_object(texObj
);
564 intel_texobj
->planar_format
= image
->planar_format
;
566 const GLenum internal_format
=
567 image
->internal_format
!= 0 ?
568 image
->internal_format
: _mesa_get_format_base_format(mt
->format
);
569 intel_set_texture_image_mt(brw
, texImage
, internal_format
, mt
);
570 intel_miptree_release(&mt
);
574 intel_gettexsubimage_blorp(struct brw_context
*brw
,
575 struct gl_texture_image
*tex_image
,
576 unsigned x
, unsigned y
, unsigned z
,
577 unsigned width
, unsigned height
, unsigned depth
,
578 GLenum format
, GLenum type
, const void *pixels
,
579 const struct gl_pixelstore_attrib
*packing
)
581 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
582 const unsigned mt_level
= tex_image
->Level
+ tex_image
->TexObject
->MinLevel
;
583 const unsigned mt_z
= tex_image
->TexObject
->MinLayer
+ tex_image
->Face
+ z
;
585 /* The blorp path can't understand crazy format hackery */
586 if (_mesa_base_tex_format(&brw
->ctx
, tex_image
->InternalFormat
) !=
587 _mesa_get_format_base_format(tex_image
->TexFormat
))
590 return brw_blorp_download_miptree(brw
, intel_image
->mt
,
591 tex_image
->TexFormat
, SWIZZLE_XYZW
,
592 mt_level
, x
, y
, mt_z
,
593 width
, height
, depth
,
594 tex_image
->TexObject
->Target
,
595 format
, type
, false, pixels
, packing
);
599 * \brief A fast path for glGetTexImage.
601 * \see intel_readpixels_tiled_memcpy()
604 intel_gettexsubimage_tiled_memcpy(struct gl_context
*ctx
,
605 struct gl_texture_image
*texImage
,
606 GLint xoffset
, GLint yoffset
,
607 GLsizei width
, GLsizei height
,
608 GLenum format
, GLenum type
,
610 const struct gl_pixelstore_attrib
*packing
)
612 struct brw_context
*brw
= brw_context(ctx
);
613 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
614 struct intel_texture_image
*image
= intel_texture_image(texImage
);
617 /* The miptree's buffer. */
621 mem_copy_fn mem_copy
= NULL
;
623 /* This fastpath is restricted to specific texture types:
624 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
627 * FINISHME: The restrictions below on packing alignment and packing row
628 * length are likely unneeded now because we calculate the destination stride
629 * with _mesa_image_row_stride. However, before removing the restrictions
632 if (!devinfo
->has_llc
||
633 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
634 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
635 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
637 _mesa_is_bufferobj(packing
->BufferObj
) ||
638 packing
->Alignment
> 4 ||
639 packing
->SkipPixels
> 0 ||
640 packing
->SkipRows
> 0 ||
641 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
642 packing
->SwapBytes
||
647 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
648 * function doesn't set the last channel to 1. Note this checks BaseFormat
649 * rather than TexFormat in case the RGBX format is being simulated with an
652 if (texImage
->_BaseFormat
== GL_RGB
)
655 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
))
658 /* If this is a nontrivial texture view, let another path handle it instead. */
659 if (texImage
->TexObject
->MinLayer
)
663 (image
->mt
->surf
.tiling
!= ISL_TILING_X
&&
664 image
->mt
->surf
.tiling
!= ISL_TILING_Y0
)) {
665 /* The algorithm is written only for X- or Y-tiled memory. */
669 /* tiled_to_linear() assumes that if the object is swizzled, it is using
670 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
671 * true on gen5 and above.
673 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
674 * parts of the memory aren't swizzled at all. Userspace just can't handle
677 if (devinfo
->gen
< 5 && brw
->has_swizzling
)
680 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
682 /* Since we are going to write raw data to the miptree, we need to resolve
683 * any pending fast color clears before we start.
685 assert(image
->mt
->surf
.logical_level0_px
.depth
== 1);
686 assert(image
->mt
->surf
.logical_level0_px
.array_len
== 1);
688 intel_miptree_access_raw(brw
, image
->mt
, level
, 0, true);
692 if (brw_batch_references(&brw
->batch
, bo
)) {
693 perf_debug("Flushing before mapping a referenced bo.\n");
694 intel_batchbuffer_flush(brw
);
697 void *map
= brw_bo_map(brw
, bo
, MAP_READ
| MAP_RAW
);
699 DBG("%s: failed to map bo\n", __func__
);
703 dst_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
705 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
706 "mesa_format=0x%x tiling=%d "
707 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
708 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
709 format
, type
, texImage
->TexFormat
, image
->mt
->surf
.tiling
,
710 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
713 /* Adjust x and y offset based on miplevel */
714 unsigned level_x
, level_y
;
715 intel_miptree_get_image_offset(image
->mt
, level
, 0, &level_x
, &level_y
);
720 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
721 yoffset
, yoffset
+ height
,
722 pixels
- (ptrdiff_t) yoffset
* dst_pitch
- (ptrdiff_t) xoffset
* cpp
,
724 dst_pitch
, image
->mt
->surf
.row_pitch
,
726 image
->mt
->surf
.tiling
,
735 intel_get_tex_sub_image(struct gl_context
*ctx
,
736 GLint xoffset
, GLint yoffset
, GLint zoffset
,
737 GLsizei width
, GLsizei height
, GLint depth
,
738 GLenum format
, GLenum type
, GLvoid
*pixels
,
739 struct gl_texture_image
*texImage
)
741 struct brw_context
*brw
= brw_context(ctx
);
744 DBG("%s\n", __func__
);
746 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
747 if (intel_gettexsubimage_blorp(brw
, texImage
,
748 xoffset
, yoffset
, zoffset
,
749 width
, height
, depth
, format
, type
,
750 pixels
, &ctx
->Pack
)) {
751 /* Flush to guarantee coherency between the render cache and other
752 * caches the PBO could potentially be bound to after this point.
753 * See the related comment in intelReadPixels() for a more detailed
756 brw_emit_mi_flush(brw
);
760 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__
);
763 ok
= intel_gettexsubimage_tiled_memcpy(ctx
, texImage
, xoffset
, yoffset
,
765 format
, type
, pixels
, &ctx
->Pack
);
770 _mesa_meta_GetTexSubImage(ctx
, xoffset
, yoffset
, zoffset
,
771 width
, height
, depth
,
772 format
, type
, pixels
, texImage
);
774 DBG("%s - DONE\n", __func__
);
778 flush_astc_denorms(struct gl_context
*ctx
, GLuint dims
,
779 struct gl_texture_image
*texImage
,
780 GLint xoffset
, GLint yoffset
, GLint zoffset
,
781 GLsizei width
, GLsizei height
, GLsizei depth
)
783 struct compressed_pixelstore store
;
784 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
,
785 width
, height
, depth
,
786 &ctx
->Unpack
, &store
);
788 for (int slice
= 0; slice
< store
.CopySlices
; slice
++) {
790 /* Map dest texture buffer */
793 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
+ zoffset
,
794 xoffset
, yoffset
, width
, height
,
795 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
796 &dstMap
, &dstRowStride
);
800 for (int i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
802 /* An ASTC block is stored in little endian mode. The byte that
803 * contains bits 0..7 is stored at the lower address in memory.
805 struct astc_void_extent
{
806 uint16_t header
: 12;
807 uint16_t dontcare
[3];
812 } *blocks
= (struct astc_void_extent
*) dstMap
;
814 /* Iterate over every copied block in the row */
815 for (int j
= 0; j
< store
.CopyBytesPerRow
/ 16; j
++) {
817 /* Check if the header matches that of an LDR void-extent block */
818 if (blocks
[j
].header
== 0xDFC) {
820 /* Flush UNORM16 values that would be denormalized */
821 if (blocks
[j
].A
< 4) blocks
[j
].A
= 0;
822 if (blocks
[j
].B
< 4) blocks
[j
].B
= 0;
823 if (blocks
[j
].G
< 4) blocks
[j
].G
= 0;
824 if (blocks
[j
].R
< 4) blocks
[j
].R
= 0;
828 dstMap
+= dstRowStride
;
831 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ zoffset
);
837 intelCompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
838 struct gl_texture_image
*texImage
,
839 GLint xoffset
, GLint yoffset
, GLint zoffset
,
840 GLsizei width
, GLsizei height
, GLsizei depth
,
842 GLsizei imageSize
, const GLvoid
*data
)
844 /* Upload the compressed data blocks */
845 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
846 xoffset
, yoffset
, zoffset
,
847 width
, height
, depth
,
848 format
, imageSize
, data
);
850 /* Fix up copied ASTC blocks if necessary */
851 GLenum gl_format
= _mesa_compressed_format_to_glenum(ctx
,
852 texImage
->TexFormat
);
853 bool is_linear_astc
= _mesa_is_astc_format(gl_format
) &&
854 !_mesa_is_srgb_format(gl_format
);
855 struct brw_context
*brw
= (struct brw_context
*) ctx
;
856 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
857 if (devinfo
->gen
== 9 && is_linear_astc
)
858 flush_astc_denorms(ctx
, dims
, texImage
,
859 xoffset
, yoffset
, zoffset
,
860 width
, height
, depth
);
864 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
866 functions
->TexImage
= intelTexImage
;
867 functions
->TexSubImage
= intelTexSubImage
;
868 functions
->CompressedTexSubImage
= intelCompressedTexSubImage
;
869 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
870 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
871 functions
->GetTexSubImage
= intel_get_tex_sub_image
;