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_fbo.h"
25 #include "intel_image.h"
26 #include "intel_tiled_memcpy.h"
27 #include "brw_context.h"
28 #include "brw_blorp.h"
30 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
32 /* Make sure one doesn't end up shrinking base level zero unnecessarily.
33 * Determining the base level dimension by shifting higher level dimension
34 * ends up in off-by-one value in case base level has NPOT size (for example,
36 * Choose the original base level dimension when shifted dimensions agree.
37 * Otherwise assume real resize is intended and use the new shifted value.
40 get_base_dim(unsigned old_base_dim
, unsigned new_level_dim
, unsigned level
)
42 const unsigned old_level_dim
= old_base_dim
>> level
;
43 const unsigned new_base_dim
= new_level_dim
<< level
;
45 return old_level_dim
== new_level_dim
? old_base_dim
: new_base_dim
;
48 /* Work back from the specified level of the image to the baselevel and create a
49 * miptree of that size.
51 struct intel_mipmap_tree
*
52 intel_miptree_create_for_teximage(struct brw_context
*brw
,
53 struct intel_texture_object
*intelObj
,
54 struct intel_texture_image
*intelImage
,
55 enum intel_miptree_create_flags flags
)
58 int width
, height
, depth
;
59 unsigned old_width
= 0, old_height
= 0, old_depth
= 0;
60 const struct intel_mipmap_tree
*old_mt
= intelObj
->mt
;
61 const unsigned level
= intelImage
->base
.Base
.Level
;
63 intel_get_image_dims(&intelImage
->base
.Base
, &width
, &height
, &depth
);
66 old_width
= old_mt
->surf
.logical_level0_px
.width
;
67 old_height
= old_mt
->surf
.logical_level0_px
.height
;
68 old_depth
= old_mt
->surf
.dim
== ISL_SURF_DIM_3D
?
69 old_mt
->surf
.logical_level0_px
.depth
:
70 old_mt
->surf
.logical_level0_px
.array_len
;
73 DBG("%s\n", __func__
);
75 /* Figure out image dimensions at start level. */
76 switch(intelObj
->base
.Target
) {
77 case GL_TEXTURE_2D_MULTISAMPLE
:
78 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
79 case GL_TEXTURE_RECTANGLE
:
80 case GL_TEXTURE_EXTERNAL_OES
:
84 depth
= old_mt
? get_base_dim(old_depth
, depth
, level
) :
88 case GL_TEXTURE_2D_ARRAY
:
89 case GL_TEXTURE_CUBE_MAP
:
90 case GL_TEXTURE_CUBE_MAP_ARRAY
:
91 height
= old_mt
? get_base_dim(old_height
, height
, level
) :
95 case GL_TEXTURE_1D_ARRAY
:
96 width
= old_mt
? get_base_dim(old_width
, width
, level
) :
100 unreachable("Unexpected target");
103 /* Guess a reasonable value for lastLevel. This is probably going
104 * to be wrong fairly often and might mean that we have to look at
105 * resizable buffers, or require that buffers implement lazy
106 * pagetable arrangements.
108 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
109 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
110 intelImage
->base
.Base
.Level
== 0 &&
111 !intelObj
->base
.GenerateMipmap
) {
114 lastLevel
= _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
115 width
, height
, depth
) - 1;
118 return intel_miptree_create(brw
,
119 intelObj
->base
.Target
,
120 intelImage
->base
.Base
.TexFormat
,
126 MAX2(intelImage
->base
.Base
.NumSamples
, 1),
131 intel_texsubimage_blorp(struct brw_context
*brw
, GLuint dims
,
132 struct gl_texture_image
*tex_image
,
133 unsigned x
, unsigned y
, unsigned z
,
134 unsigned width
, unsigned height
, unsigned depth
,
135 GLenum format
, GLenum type
, const void *pixels
,
136 const struct gl_pixelstore_attrib
*packing
)
138 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
139 const unsigned mt_level
= tex_image
->Level
+ tex_image
->TexObject
->MinLevel
;
140 const unsigned mt_z
= tex_image
->TexObject
->MinLayer
+ tex_image
->Face
+ z
;
142 /* The blorp path can't understand crazy format hackery */
143 if (_mesa_base_tex_format(&brw
->ctx
, tex_image
->InternalFormat
) !=
144 _mesa_get_format_base_format(tex_image
->TexFormat
))
147 return brw_blorp_upload_miptree(brw
, intel_image
->mt
, tex_image
->TexFormat
,
148 mt_level
, x
, y
, mt_z
, width
, height
, depth
,
149 tex_image
->TexObject
->Target
, format
, type
,
154 * \brief A fast path for glTexImage and glTexSubImage.
156 * This fast path is taken when the texture format is BGRA, RGBA,
157 * A or L and when the texture memory is X- or Y-tiled. It uploads
158 * the texture data by mapping the texture memory without a GTT fence, thus
159 * acquiring a tiled view of the memory, and then copying sucessive
160 * spans within each tile.
162 * This is a performance win over the conventional texture upload path because
163 * it avoids the performance penalty of writing through the write-combine
164 * buffer. In the conventional texture upload path,
165 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
166 * fence, thus acquiring a linear view of the memory, then each row in the
167 * image is memcpy'd. In this fast path, we replace each row's copy with
168 * a sequence of copies over each linear span in tile.
170 * One use case is Google Chrome's paint rectangles. Chrome (as
171 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
172 * Each page's content is initially uploaded with glTexImage2D and damaged
173 * regions are updated with glTexSubImage2D. On some workloads, the
174 * performance gain of this fastpath on Sandybridge is over 5x.
177 intel_texsubimage_tiled_memcpy(struct gl_context
* ctx
,
179 struct gl_texture_image
*texImage
,
180 GLint xoffset
, GLint yoffset
, GLint zoffset
,
181 GLsizei width
, GLsizei height
, GLsizei depth
,
182 GLenum format
, GLenum type
,
183 const GLvoid
*pixels
,
184 const struct gl_pixelstore_attrib
*packing
)
186 struct brw_context
*brw
= brw_context(ctx
);
187 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
188 struct intel_texture_image
*image
= intel_texture_image(texImage
);
191 /* The miptree's buffer. */
195 mem_copy_fn mem_copy
= NULL
;
197 /* This fastpath is restricted to specific texture types:
198 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
201 * FINISHME: The restrictions below on packing alignment and packing row
202 * length are likely unneeded now because we calculate the source stride
203 * with _mesa_image_row_stride. However, before removing the restrictions
206 if (!devinfo
->has_llc
||
207 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
208 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
209 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
211 _mesa_is_bufferobj(packing
->BufferObj
) ||
212 packing
->Alignment
> 4 ||
213 packing
->SkipPixels
> 0 ||
214 packing
->SkipRows
> 0 ||
215 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
216 packing
->SwapBytes
||
221 /* Only a simple blit, no scale, bias or other mapping. */
222 if (ctx
->_ImageTransferState
)
225 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
))
228 /* If this is a nontrivial texture view, let another path handle it instead. */
229 if (texImage
->TexObject
->MinLayer
)
233 (image
->mt
->surf
.tiling
!= ISL_TILING_X
&&
234 image
->mt
->surf
.tiling
!= ISL_TILING_Y0
)) {
235 /* The algorithm is written only for X- or Y-tiled memory. */
239 /* linear_to_tiled() assumes that if the object is swizzled, it is using
240 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
241 * true on gen5 and above.
243 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
244 * parts of the memory aren't swizzled at all. Userspace just can't handle
247 if (devinfo
->gen
< 5 && brw
->has_swizzling
)
250 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
252 /* Since we are going to write raw data to the miptree, we need to resolve
253 * any pending fast color clears before we start.
255 assert(image
->mt
->surf
.logical_level0_px
.depth
== 1);
256 assert(image
->mt
->surf
.logical_level0_px
.array_len
== 1);
258 intel_miptree_access_raw(brw
, image
->mt
, level
, 0, true);
262 if (brw_batch_references(&brw
->batch
, bo
)) {
263 perf_debug("Flushing before mapping a referenced bo.\n");
264 intel_batchbuffer_flush(brw
);
267 void *map
= brw_bo_map(brw
, bo
, MAP_WRITE
| MAP_RAW
);
269 DBG("%s: failed to map bo\n", __func__
);
273 src_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
275 /* We postponed printing this message until having committed to executing
278 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
279 "mesa_format=0x%x tiling=%d "
280 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) ",
281 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
282 format
, type
, texImage
->TexFormat
, image
->mt
->surf
.tiling
,
283 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
286 /* Adjust x and y offset based on miplevel */
287 unsigned level_x
, level_y
;
288 intel_miptree_get_image_offset(image
->mt
, level
, 0, &level_x
, &level_y
);
293 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
294 yoffset
, yoffset
+ height
,
297 image
->mt
->surf
.row_pitch
, src_pitch
,
299 image
->mt
->surf
.tiling
,
309 intel_upload_tex(struct gl_context
* ctx
,
311 struct gl_texture_image
*texImage
,
312 GLint xoffset
, GLint yoffset
, GLint zoffset
,
313 GLsizei width
, GLsizei height
, GLsizei depth
,
314 GLenum format
, GLenum type
,
315 const GLvoid
* pixels
,
316 const struct gl_pixelstore_attrib
*packing
)
318 struct brw_context
*brw
= brw_context(ctx
);
319 struct intel_mipmap_tree
*mt
= intel_texture_image(texImage
)->mt
;
322 /* Check that there is actually data to store. */
323 if (pixels
== NULL
&& !_mesa_is_bufferobj(packing
->BufferObj
))
326 bool tex_busy
= mt
&& brw_bo_busy(mt
->bo
);
328 if (mt
&& mt
->format
== MESA_FORMAT_S_UINT8
)
329 mt
->r8stencil_needs_update
= true;
331 if (_mesa_is_bufferobj(packing
->BufferObj
) || tex_busy
||
332 mt
->aux_usage
== ISL_AUX_USAGE_CCS_E
) {
333 ok
= intel_texsubimage_blorp(brw
, dims
, texImage
,
334 xoffset
, yoffset
, zoffset
,
335 width
, height
, depth
, format
, type
,
341 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
342 xoffset
, yoffset
, zoffset
,
343 width
, height
, depth
,
344 format
, type
, pixels
, packing
);
348 _mesa_store_texsubimage(ctx
, dims
, texImage
,
349 xoffset
, yoffset
, zoffset
,
350 width
, height
, depth
,
351 format
, type
, pixels
, packing
);
356 intelTexImage(struct gl_context
* ctx
,
358 struct gl_texture_image
*texImage
,
359 GLenum format
, GLenum type
, const void *pixels
,
360 const struct gl_pixelstore_attrib
*unpack
)
362 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
363 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
364 _mesa_enum_to_string(texImage
->TexObject
->Target
),
365 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
366 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
368 /* Allocate storage for texture data. */
369 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
370 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
374 assert(intel_texture_image(texImage
)->mt
);
376 intel_upload_tex(ctx
, dims
, texImage
, 0, 0, 0,
377 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
378 format
, type
, pixels
, unpack
);
383 intelTexSubImage(struct gl_context
* ctx
,
385 struct gl_texture_image
*texImage
,
386 GLint xoffset
, GLint yoffset
, GLint zoffset
,
387 GLsizei width
, GLsizei height
, GLsizei depth
,
388 GLenum format
, GLenum type
,
389 const GLvoid
* pixels
,
390 const struct gl_pixelstore_attrib
*packing
)
392 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
393 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
394 _mesa_enum_to_string(texImage
->TexObject
->Target
),
395 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
396 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
398 intel_upload_tex(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
399 width
, height
, depth
, format
, type
, pixels
, packing
);
404 intel_set_texture_image_mt(struct brw_context
*brw
,
405 struct gl_texture_image
*image
,
406 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
, 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 mesa_format texFormat
= MESA_FORMAT_NONE
;
445 GLenum internal_format
= 0;
447 texObj
= _mesa_get_current_tex_object(ctx
, target
);
452 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
453 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
454 intel_update_renderbuffers(pDRICtx
, dPriv
);
456 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
457 /* If the miptree isn't set, then intel_update_renderbuffers was unable
458 * to get the BO for the drawable from the window system.
463 /* Neither the EGL and GLX texture_from_pixmap specs say anything about
464 * sRGB. They are both from a time where sRGB was considered an extra
465 * encoding step you did as part of rendering/blending and not a format.
466 * Even though we have concept of sRGB visuals, X has classically assumed
467 * that your data is just bits and sRGB rendering is entirely a client-side
468 * rendering construct. The assumption is that the result of BindTexImage
469 * is a texture with a linear format even if it was rendered with sRGB
472 texFormat
= _mesa_get_srgb_format_linear(intel_rb_format(rb
));
474 if (rb
->mt
->cpp
== 4) {
475 /* The extra texture_format parameter indicates whether the alpha
476 * channel should be respected or ignored. If we set internal_format to
477 * GL_RGB, the texture handling code is smart enough to swap the format
478 * or apply a swizzle if the underlying format is RGBA so we don't need
479 * to stomp it to RGBX or anything like that.
481 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
)
482 internal_format
= GL_RGB
;
484 internal_format
= GL_RGBA
;
485 } else if (rb
->mt
->cpp
== 2) {
486 internal_format
= GL_RGB
;
489 intel_miptree_finish_external(brw
, rb
->mt
);
491 _mesa_lock_texture(&brw
->ctx
, texObj
);
492 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
493 intel_set_texture_image_mt(brw
, texImage
, internal_format
,
495 _mesa_unlock_texture(&brw
->ctx
, texObj
);
499 intelReleaseTexBuffer(__DRIcontext
*pDRICtx
, GLint target
,
500 __DRIdrawable
*dPriv
)
502 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
503 struct gl_context
*ctx
= &brw
->ctx
;
504 struct gl_texture_object
*tex_obj
;
505 struct intel_texture_object
*intel_tex
;
507 tex_obj
= _mesa_get_current_tex_object(ctx
, target
);
511 _mesa_lock_texture(&brw
->ctx
, tex_obj
);
513 intel_tex
= intel_texture_object(tex_obj
);
514 if (!intel_tex
->mt
) {
515 _mesa_unlock_texture(&brw
->ctx
, tex_obj
);
519 /* The intel_miptree_prepare_external below as well as the finish_external
520 * above in intelSetTexBuffer2 *should* do nothing. The BindTexImage call
521 * from both GLX and EGL has TexImage2D and not TexSubImage2D semantics so
522 * the texture is not immutable. This means that the user cannot create a
523 * texture view of the image with a different format. Since the only three
524 * formats available when using BindTexImage are all UNORM, we can never
525 * end up with an sRGB format being used for texturing and so we shouldn't
526 * get any format-related resolves when texturing from it.
528 * While very unlikely, it is possible that the client could use the bound
529 * texture with GL_ARB_image_load_store. In that case, we'll do a resolve
530 * but that's not actually a problem as it just means that we lose
531 * compression on this texture until the next time it's used as a render
534 * The only other way we could end up with an unexpected aux usage would be
535 * if we rendered to the image from the same context as we have it bound as
536 * a texture between BindTexImage and ReleaseTexImage. However, the spec
537 * clearly calls this case out and says you shouldn't do that. It doesn't
538 * explicitly prevent binding the texture to a framebuffer but it says the
539 * results of trying to render to it while bound are undefined.
541 * Just to keep everything safe and sane, we do a prepare_external but it
542 * should be a no-op in almost all cases. On the off chance that someone
543 * ever triggers this, we should at least warn them.
545 if (intel_tex
->mt
->aux_buf
&&
546 intel_miptree_get_aux_state(intel_tex
->mt
, 0, 0) !=
547 isl_drm_modifier_get_default_aux_state(intel_tex
->mt
->drm_modifier
)) {
548 _mesa_warning(ctx
, "Aux state changed between BindTexImage and "
549 "ReleaseTexImage. Most likely someone tried to draw "
550 "to the pixmap bound in BindTexImage or used it with "
551 "image_load_store.");
554 intel_miptree_prepare_external(brw
, intel_tex
->mt
);
556 _mesa_unlock_texture(&brw
->ctx
, tex_obj
);
560 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
561 struct gl_renderbuffer
*rb
,
562 struct gl_texture_image
*image
)
564 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
565 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
566 struct gl_texture_object
*texobj
= image
->TexObject
;
567 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
569 /* We can only handle RB allocated with AllocRenderbufferStorage, or
570 * window-system renderbuffers.
572 assert(!rb
->TexImage
);
577 _mesa_lock_texture(ctx
, texobj
);
578 _mesa_init_teximage_fields(ctx
, image
,
579 rb
->Width
, rb
->Height
, 1,
580 0, rb
->InternalFormat
, rb
->Format
);
581 image
->NumSamples
= rb
->NumSamples
;
583 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
585 /* Immediately validate the image to the object. */
586 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
588 intel_texobj
->needs_validate
= true;
589 _mesa_unlock_texture(ctx
, texobj
);
595 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
597 /* The old interface didn't have the format argument, so copy our
598 * implementation's behavior at the time.
600 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
604 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
605 struct gl_texture_object
*texObj
,
606 struct gl_texture_image
*texImage
,
607 GLeglImageOES image_handle
)
609 struct brw_context
*brw
= brw_context(ctx
);
610 struct intel_mipmap_tree
*mt
;
611 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
614 image
= dri_screen
->dri2
.image
->lookupEGLImage(dri_screen
, image_handle
,
615 dri_screen
->loaderPrivate
);
619 /* We support external textures only for EGLImages created with
620 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
622 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
624 "glEGLImageTargetTexture2DOES(external target is enabled only "
625 "for images created with EGL_EXT_image_dma_buf_import");
629 /* Disallow depth/stencil textures: we don't have a way to pass the
630 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
632 if (image
->has_depthstencil
) {
633 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
637 mt
= intel_miptree_create_for_dri_image(brw
, image
, target
, image
->format
,
642 struct intel_texture_object
*intel_texobj
= intel_texture_object(texObj
);
643 intel_texobj
->planar_format
= image
->planar_format
;
645 const GLenum internal_format
=
646 image
->internal_format
!= 0 ?
647 image
->internal_format
: _mesa_get_format_base_format(mt
->format
);
648 intel_set_texture_image_mt(brw
, texImage
, internal_format
, mt
->format
, mt
);
649 intel_miptree_release(&mt
);
653 intel_gettexsubimage_blorp(struct brw_context
*brw
,
654 struct gl_texture_image
*tex_image
,
655 unsigned x
, unsigned y
, unsigned z
,
656 unsigned width
, unsigned height
, unsigned depth
,
657 GLenum format
, GLenum type
, const void *pixels
,
658 const struct gl_pixelstore_attrib
*packing
)
660 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
661 const unsigned mt_level
= tex_image
->Level
+ tex_image
->TexObject
->MinLevel
;
662 const unsigned mt_z
= tex_image
->TexObject
->MinLayer
+ tex_image
->Face
+ z
;
664 /* The blorp path can't understand crazy format hackery */
665 if (_mesa_base_tex_format(&brw
->ctx
, tex_image
->InternalFormat
) !=
666 _mesa_get_format_base_format(tex_image
->TexFormat
))
669 return brw_blorp_download_miptree(brw
, intel_image
->mt
,
670 tex_image
->TexFormat
, SWIZZLE_XYZW
,
671 mt_level
, x
, y
, mt_z
,
672 width
, height
, depth
,
673 tex_image
->TexObject
->Target
,
674 format
, type
, false, pixels
, packing
);
678 * \brief A fast path for glGetTexImage.
680 * \see intel_readpixels_tiled_memcpy()
683 intel_gettexsubimage_tiled_memcpy(struct gl_context
*ctx
,
684 struct gl_texture_image
*texImage
,
685 GLint xoffset
, GLint yoffset
,
686 GLsizei width
, GLsizei height
,
687 GLenum format
, GLenum type
,
689 const struct gl_pixelstore_attrib
*packing
)
691 struct brw_context
*brw
= brw_context(ctx
);
692 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
693 struct intel_texture_image
*image
= intel_texture_image(texImage
);
696 /* The miptree's buffer. */
700 mem_copy_fn mem_copy
= NULL
;
702 /* This fastpath is restricted to specific texture types:
703 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
706 * FINISHME: The restrictions below on packing alignment and packing row
707 * length are likely unneeded now because we calculate the destination stride
708 * with _mesa_image_row_stride. However, before removing the restrictions
711 if (!devinfo
->has_llc
||
712 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
713 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
714 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
716 _mesa_is_bufferobj(packing
->BufferObj
) ||
717 packing
->Alignment
> 4 ||
718 packing
->SkipPixels
> 0 ||
719 packing
->SkipRows
> 0 ||
720 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
721 packing
->SwapBytes
||
726 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
727 * function doesn't set the last channel to 1. Note this checks BaseFormat
728 * rather than TexFormat in case the RGBX format is being simulated with an
731 if (texImage
->_BaseFormat
== GL_RGB
)
734 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
))
737 /* If this is a nontrivial texture view, let another path handle it instead. */
738 if (texImage
->TexObject
->MinLayer
)
742 (image
->mt
->surf
.tiling
!= ISL_TILING_X
&&
743 image
->mt
->surf
.tiling
!= ISL_TILING_Y0
)) {
744 /* The algorithm is written only for X- or Y-tiled memory. */
748 /* tiled_to_linear() assumes that if the object is swizzled, it is using
749 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
750 * true on gen5 and above.
752 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
753 * parts of the memory aren't swizzled at all. Userspace just can't handle
756 if (devinfo
->gen
< 5 && brw
->has_swizzling
)
759 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
761 /* Since we are going to write raw data to the miptree, we need to resolve
762 * any pending fast color clears before we start.
764 assert(image
->mt
->surf
.logical_level0_px
.depth
== 1);
765 assert(image
->mt
->surf
.logical_level0_px
.array_len
== 1);
767 intel_miptree_access_raw(brw
, image
->mt
, level
, 0, true);
771 if (brw_batch_references(&brw
->batch
, bo
)) {
772 perf_debug("Flushing before mapping a referenced bo.\n");
773 intel_batchbuffer_flush(brw
);
776 void *map
= brw_bo_map(brw
, bo
, MAP_READ
| MAP_RAW
);
778 DBG("%s: failed to map bo\n", __func__
);
782 dst_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
784 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
785 "mesa_format=0x%x tiling=%d "
786 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
787 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
788 format
, type
, texImage
->TexFormat
, image
->mt
->surf
.tiling
,
789 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
792 /* Adjust x and y offset based on miplevel */
793 unsigned level_x
, level_y
;
794 intel_miptree_get_image_offset(image
->mt
, level
, 0, &level_x
, &level_y
);
799 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
800 yoffset
, yoffset
+ height
,
803 dst_pitch
, image
->mt
->surf
.row_pitch
,
805 image
->mt
->surf
.tiling
,
814 intel_get_tex_sub_image(struct gl_context
*ctx
,
815 GLint xoffset
, GLint yoffset
, GLint zoffset
,
816 GLsizei width
, GLsizei height
, GLint depth
,
817 GLenum format
, GLenum type
, GLvoid
*pixels
,
818 struct gl_texture_image
*texImage
)
820 struct brw_context
*brw
= brw_context(ctx
);
823 DBG("%s\n", __func__
);
825 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
826 if (intel_gettexsubimage_blorp(brw
, texImage
,
827 xoffset
, yoffset
, zoffset
,
828 width
, height
, depth
, format
, type
,
832 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__
);
835 ok
= intel_gettexsubimage_tiled_memcpy(ctx
, texImage
, xoffset
, yoffset
,
837 format
, type
, pixels
, &ctx
->Pack
);
842 _mesa_meta_GetTexSubImage(ctx
, xoffset
, yoffset
, zoffset
,
843 width
, height
, depth
,
844 format
, type
, pixels
, texImage
);
846 DBG("%s - DONE\n", __func__
);
850 flush_astc_denorms(struct gl_context
*ctx
, GLuint dims
,
851 struct gl_texture_image
*texImage
,
852 GLint xoffset
, GLint yoffset
, GLint zoffset
,
853 GLsizei width
, GLsizei height
, GLsizei depth
)
855 struct compressed_pixelstore store
;
856 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
,
857 width
, height
, depth
,
858 &ctx
->Unpack
, &store
);
860 for (int slice
= 0; slice
< store
.CopySlices
; slice
++) {
862 /* Map dest texture buffer */
865 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
+ zoffset
,
866 xoffset
, yoffset
, width
, height
,
867 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
868 &dstMap
, &dstRowStride
);
872 for (int i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
874 /* An ASTC block is stored in little endian mode. The byte that
875 * contains bits 0..7 is stored at the lower address in memory.
877 struct astc_void_extent
{
878 uint16_t header
: 12;
879 uint16_t dontcare
[3];
884 } *blocks
= (struct astc_void_extent
*) dstMap
;
886 /* Iterate over every copied block in the row */
887 for (int j
= 0; j
< store
.CopyBytesPerRow
/ 16; j
++) {
889 /* Check if the header matches that of an LDR void-extent block */
890 if (blocks
[j
].header
== 0xDFC) {
892 /* Flush UNORM16 values that would be denormalized */
893 if (blocks
[j
].A
< 4) blocks
[j
].A
= 0;
894 if (blocks
[j
].B
< 4) blocks
[j
].B
= 0;
895 if (blocks
[j
].G
< 4) blocks
[j
].G
= 0;
896 if (blocks
[j
].R
< 4) blocks
[j
].R
= 0;
900 dstMap
+= dstRowStride
;
903 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ zoffset
);
909 intelCompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
910 struct gl_texture_image
*texImage
,
911 GLint xoffset
, GLint yoffset
, GLint zoffset
,
912 GLsizei width
, GLsizei height
, GLsizei depth
,
914 GLsizei imageSize
, const GLvoid
*data
)
916 /* Upload the compressed data blocks */
917 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
918 xoffset
, yoffset
, zoffset
,
919 width
, height
, depth
,
920 format
, imageSize
, data
);
922 /* Fix up copied ASTC blocks if necessary */
923 GLenum gl_format
= _mesa_compressed_format_to_glenum(ctx
,
924 texImage
->TexFormat
);
925 bool is_linear_astc
= _mesa_is_astc_format(gl_format
) &&
926 !_mesa_is_srgb_format(gl_format
);
927 struct brw_context
*brw
= (struct brw_context
*) ctx
;
928 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
929 if (devinfo
->gen
== 9 && !gen_device_info_is_9lp(devinfo
) && is_linear_astc
)
930 flush_astc_denorms(ctx
, dims
, texImage
,
931 xoffset
, yoffset
, zoffset
,
932 width
, height
, depth
);
936 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
938 functions
->TexImage
= intelTexImage
;
939 functions
->TexSubImage
= intelTexSubImage
;
940 functions
->CompressedTexSubImage
= intelCompressedTexSubImage
;
941 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
942 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
943 functions
->GetTexSubImage
= intel_get_tex_sub_image
;