2 #include "main/glheader.h"
3 #include "main/macros.h"
4 #include "main/mtypes.h"
5 #include "main/enums.h"
6 #include "main/bufferobj.h"
7 #include "main/context.h"
8 #include "main/formats.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"
28 #include "brw_context.h"
30 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
32 /* Work back from the specified level of the image to the baselevel and create a
33 * miptree of that size.
35 struct intel_mipmap_tree
*
36 intel_miptree_create_for_teximage(struct brw_context
*brw
,
37 struct intel_texture_object
*intelObj
,
38 struct intel_texture_image
*intelImage
,
39 bool expect_accelerated_upload
)
42 int width
, height
, depth
;
45 intel_miptree_get_dimensions_for_image(&intelImage
->base
.Base
,
46 &width
, &height
, &depth
);
48 DBG("%s\n", __FUNCTION__
);
50 /* Figure out image dimensions at start level. */
51 for (i
= intelImage
->base
.Base
.Level
; i
> 0; i
--) {
59 /* Guess a reasonable value for lastLevel. This is probably going
60 * to be wrong fairly often and might mean that we have to look at
61 * resizable buffers, or require that buffers implement lazy
62 * pagetable arrangements.
64 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
65 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
66 intelImage
->base
.Base
.Level
== 0 &&
67 !intelObj
->base
.GenerateMipmap
) {
70 lastLevel
= _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
71 width
, height
, depth
) - 1;
74 return intel_miptree_create(brw
,
75 intelObj
->base
.Target
,
76 intelImage
->base
.Base
.TexFormat
,
82 expect_accelerated_upload
,
83 intelImage
->base
.Base
.NumSamples
,
84 INTEL_MIPTREE_TILING_ANY
);
87 /* XXX: Do this for TexSubImage also:
90 try_pbo_upload(struct gl_context
*ctx
,
91 struct gl_texture_image
*image
,
92 const struct gl_pixelstore_attrib
*unpack
,
93 GLenum format
, GLenum type
, const void *pixels
)
95 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
96 struct brw_context
*brw
= brw_context(ctx
);
97 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
99 drm_intel_bo
*src_buffer
;
101 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
104 DBG("trying pbo upload\n");
106 if (ctx
->_ImageTransferState
|| unpack
->SkipPixels
|| unpack
->SkipRows
) {
107 DBG("%s: image transfer\n", __FUNCTION__
);
111 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
);
113 if (!intelImage
->mt
) {
114 DBG("%s: no miptree\n", __FUNCTION__
);
118 if (!_mesa_format_matches_format_and_type(intelImage
->mt
->format
,
119 format
, type
, false)) {
120 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
121 __FUNCTION__
, _mesa_get_format_name(intelImage
->mt
->format
),
126 if (image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
||
127 image
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
128 DBG("%s: no support for array textures\n", __FUNCTION__
);
133 _mesa_image_row_stride(unpack
, image
->Width
, format
, type
);
135 /* note: potential 64-bit ptr to 32-bit int cast */
136 src_offset
= (GLuint
) (unsigned long) pixels
;
137 src_buffer
= intel_bufferobj_buffer(brw
, pbo
,
138 src_offset
, src_stride
* image
->Height
);
140 struct intel_mipmap_tree
*pbo_mt
=
141 intel_miptree_create_for_bo(brw
,
143 intelImage
->mt
->format
,
145 image
->Width
, image
->Height
,
150 if (!intel_miptree_blit(brw
,
153 intelImage
->mt
, image
->Level
, image
->Face
,
155 image
->Width
, image
->Height
, GL_COPY
)) {
156 DBG("%s: blit failed\n", __FUNCTION__
);
157 intel_miptree_release(&pbo_mt
);
161 intel_miptree_release(&pbo_mt
);
163 DBG("%s: success\n", __FUNCTION__
);
168 intelTexImage(struct gl_context
* ctx
,
170 struct gl_texture_image
*texImage
,
171 GLenum format
, GLenum type
, const void *pixels
,
172 const struct gl_pixelstore_attrib
*unpack
)
176 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
177 __FUNCTION__
, _mesa_get_format_name(texImage
->TexFormat
),
178 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
179 _mesa_lookup_enum_by_nr(format
), _mesa_lookup_enum_by_nr(type
),
180 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
182 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
183 0, 0, 0, /*x,y,z offsets*/
187 format
, type
, pixels
, unpack
,
188 true /*for_glTexImage*/);
192 /* Attempt to use the blitter for PBO image uploads.
195 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
, pixels
)) {
199 DBG("%s: upload image %dx%dx%d pixels %p\n",
200 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
203 _mesa_store_teximage(ctx
, dims
, texImage
,
204 format
, type
, pixels
, unpack
);
209 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
211 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
214 intel_set_texture_image_bo(struct gl_context
*ctx
,
215 struct gl_texture_image
*image
,
218 GLenum internalFormat
,
221 GLuint width
, GLuint height
,
223 GLuint tile_x
, GLuint tile_y
)
225 struct brw_context
*brw
= brw_context(ctx
);
226 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
227 struct gl_texture_object
*texobj
= image
->TexObject
;
228 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
229 uint32_t draw_x
, draw_y
;
231 _mesa_init_teximage_fields(&brw
->ctx
, image
,
233 0, internalFormat
, format
);
235 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
237 intel_image
->mt
= intel_miptree_create_for_bo(brw
, bo
, image
->TexFormat
,
238 0, width
, height
, pitch
);
239 if (intel_image
->mt
== NULL
)
241 intel_image
->mt
->target
= target
;
242 intel_image
->mt
->total_width
= width
;
243 intel_image
->mt
->total_height
= height
;
244 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
245 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
247 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
249 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
250 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
251 * trouble resolving back to destination image due to alignment issues.
253 if (!brw
->has_surface_tile_offset
&&
254 (draw_x
!= 0 || draw_y
!= 0)) {
255 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
256 intel_miptree_release(&intel_image
->mt
);
260 intel_texobj
->needs_validate
= true;
262 intel_image
->mt
->offset
= offset
;
263 assert(pitch
% intel_image
->mt
->cpp
== 0);
264 intel_image
->base
.RowStride
= pitch
/ intel_image
->mt
->cpp
;
266 /* Immediately validate the image to the object. */
267 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
271 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
272 GLint texture_format
,
273 __DRIdrawable
*dPriv
)
275 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
276 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
277 struct gl_context
*ctx
= &brw
->ctx
;
278 struct intel_renderbuffer
*rb
;
279 struct gl_texture_object
*texObj
;
280 struct gl_texture_image
*texImage
;
281 int level
= 0, internalFormat
= 0;
282 mesa_format texFormat
= MESA_FORMAT_NONE
;
284 texObj
= _mesa_get_current_tex_object(ctx
, target
);
289 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
290 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
291 intel_update_renderbuffers(pDRICtx
, dPriv
);
293 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
294 /* If the miptree isn't set, then intel_update_renderbuffers was unable
295 * to get the BO for the drawable from the window system.
300 if (rb
->mt
->cpp
== 4) {
301 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
302 internalFormat
= GL_RGB
;
303 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
306 internalFormat
= GL_RGBA
;
307 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
309 } else if (rb
->mt
->cpp
== 2) {
310 internalFormat
= GL_RGB
;
311 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
314 _mesa_lock_texture(&brw
->ctx
, texObj
);
315 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
316 intel_miptree_make_shareable(brw
, rb
->mt
);
317 intel_set_texture_image_bo(ctx
, texImage
, rb
->mt
->bo
, target
,
318 internalFormat
, texFormat
, 0,
320 rb
->Base
.Base
.Height
,
323 _mesa_unlock_texture(&brw
->ctx
, texObj
);
327 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
328 struct gl_renderbuffer
*rb
,
329 struct gl_texture_image
*image
)
331 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
332 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
333 struct gl_texture_object
*texobj
= image
->TexObject
;
334 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
336 /* We can only handle RB allocated with AllocRenderbufferStorage, or
337 * window-system renderbuffers.
339 assert(!rb
->TexImage
);
344 _mesa_lock_texture(ctx
, texobj
);
345 _mesa_init_teximage_fields(ctx
, image
,
346 rb
->Width
, rb
->Height
, 1,
347 0, rb
->InternalFormat
, rb
->Format
);
348 image
->NumSamples
= rb
->NumSamples
;
350 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
352 /* Immediately validate the image to the object. */
353 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
355 intel_texobj
->needs_validate
= true;
356 _mesa_unlock_texture(ctx
, texobj
);
362 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
364 /* The old interface didn't have the format argument, so copy our
365 * implementation's behavior at the time.
367 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
371 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
372 struct gl_texture_object
*texObj
,
373 struct gl_texture_image
*texImage
,
374 GLeglImageOES image_handle
)
376 struct brw_context
*brw
= brw_context(ctx
);
380 screen
= brw
->intelScreen
->driScrnPriv
;
381 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
382 screen
->loaderPrivate
);
387 * Images originating via EGL_EXT_image_dma_buf_import can be used only
388 * with GL_OES_EGL_image_external only.
390 if (image
->dma_buf_imported
&& target
!= GL_TEXTURE_EXTERNAL_OES
) {
391 _mesa_error(ctx
, GL_INVALID_OPERATION
,
392 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
393 "GL_OES_EGL_image_external only");
397 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
398 _mesa_error(ctx
, GL_INVALID_OPERATION
,
399 "glEGLImageTargetTexture2DOES(external target is enabled only "
400 "for images created with EGL_EXT_image_dma_buf_import");
404 /* Disallow depth/stencil textures: we don't have a way to pass the
405 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
407 if (image
->has_depthstencil
) {
408 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
412 intel_set_texture_image_bo(ctx
, texImage
, image
->bo
,
413 target
, image
->internal_format
,
414 image
->format
, image
->offset
,
415 image
->width
, image
->height
,
417 image
->tile_x
, image
->tile_y
);
421 blit_texture_to_pbo(struct gl_context
*ctx
,
422 GLenum format
, GLenum type
,
423 GLvoid
* pixels
, struct gl_texture_image
*texImage
)
425 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
426 struct brw_context
*brw
= brw_context(ctx
);
427 const struct gl_pixelstore_attrib
*pack
= &ctx
->Pack
;
428 struct intel_buffer_object
*dst
= intel_buffer_object(pack
->BufferObj
);
430 drm_intel_bo
*dst_buffer
;
431 GLenum target
= texImage
->TexObject
->Target
;
433 /* Check if we can use GPU blit to copy from the hardware texture
434 * format to the user's format/type.
435 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
438 if (!_mesa_format_matches_format_and_type(intelImage
->mt
->format
, format
,
441 perf_debug("%s: unsupported format, fallback to CPU mapping for PBO\n",
447 if (ctx
->_ImageTransferState
) {
448 perf_debug("%s: bad transfer state, fallback to CPU mapping for PBO\n",
453 if (pack
->SwapBytes
|| pack
->LsbFirst
) {
454 perf_debug("%s: unsupported pack swap params\n",
459 if (target
== GL_TEXTURE_1D_ARRAY
||
460 target
== GL_TEXTURE_2D_ARRAY
||
461 target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
462 target
== GL_TEXTURE_3D
) {
463 perf_debug("%s: no support for multiple slices, fallback to CPU mapping "
464 "for PBO\n", __FUNCTION__
);
468 int dst_stride
= _mesa_image_row_stride(pack
, texImage
->Width
, format
, type
);
469 bool dst_flip
= false;
470 /* Mesa flips the dst_stride for ctx->Pack.Invert, our mt must have a
473 struct gl_pixelstore_attrib uninverted_pack
= *pack
;
474 if (ctx
->Pack
.Invert
) {
475 dst_stride
= -dst_stride
;
477 uninverted_pack
.Invert
= false;
479 dst_offset
= (GLintptr
) pixels
;
480 dst_offset
+= _mesa_image_offset(2, &uninverted_pack
, texImage
->Width
,
481 texImage
->Height
, format
, type
, 0, 0, 0);
482 dst_buffer
= intel_bufferobj_buffer(brw
, dst
, dst_offset
,
483 texImage
->Height
* dst_stride
);
485 struct intel_mipmap_tree
*pbo_mt
=
486 intel_miptree_create_for_bo(brw
,
488 intelImage
->mt
->format
,
490 texImage
->Width
, texImage
->Height
,
496 if (!intel_miptree_blit(brw
,
497 intelImage
->mt
, texImage
->Level
, texImage
->Face
,
501 texImage
->Width
, texImage
->Height
, GL_COPY
))
504 intel_miptree_release(&pbo_mt
);
510 intel_get_tex_image(struct gl_context
*ctx
,
511 GLenum format
, GLenum type
, GLvoid
*pixels
,
512 struct gl_texture_image
*texImage
) {
513 DBG("%s\n", __FUNCTION__
);
515 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
516 /* Using PBOs, so try the BLT based path. */
517 if (blit_texture_to_pbo(ctx
, format
, type
, pixels
, texImage
))
522 _mesa_meta_GetTexImage(ctx
, format
, type
, pixels
, texImage
);
524 DBG("%s - DONE\n", __FUNCTION__
);
528 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
530 functions
->TexImage
= intelTexImage
;
531 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
532 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
533 functions
->GetTexImage
= intel_get_tex_image
;