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 target %s level %d %dx%dx%d\n", __FUNCTION__
,
177 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
178 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
180 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
181 0, 0, 0, /*x,y,z offsets*/
185 format
, type
, pixels
, unpack
,
186 true /*for_glTexImage*/);
190 /* Attempt to use the blitter for PBO image uploads.
193 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
, pixels
)) {
197 DBG("%s: upload image %dx%dx%d pixels %p\n",
198 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
201 _mesa_store_teximage(ctx
, dims
, texImage
,
202 format
, type
, pixels
, unpack
);
207 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
209 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
212 intel_set_texture_image_bo(struct gl_context
*ctx
,
213 struct gl_texture_image
*image
,
216 GLenum internalFormat
,
219 GLuint width
, GLuint height
,
221 GLuint tile_x
, GLuint tile_y
)
223 struct brw_context
*brw
= brw_context(ctx
);
224 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
225 struct gl_texture_object
*texobj
= image
->TexObject
;
226 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
227 uint32_t draw_x
, draw_y
;
229 _mesa_init_teximage_fields(&brw
->ctx
, image
,
231 0, internalFormat
, format
);
233 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
235 intel_image
->mt
= intel_miptree_create_for_bo(brw
, bo
, image
->TexFormat
,
236 0, width
, height
, pitch
);
237 if (intel_image
->mt
== NULL
)
239 intel_image
->mt
->target
= target
;
240 intel_image
->mt
->total_width
= width
;
241 intel_image
->mt
->total_height
= height
;
242 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
243 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
245 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
247 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
248 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
249 * trouble resolving back to destination image due to alignment issues.
251 if (!brw
->has_surface_tile_offset
&&
252 (draw_x
!= 0 || draw_y
!= 0)) {
253 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
254 intel_miptree_release(&intel_image
->mt
);
258 intel_texobj
->needs_validate
= true;
260 intel_image
->mt
->offset
= offset
;
261 assert(pitch
% intel_image
->mt
->cpp
== 0);
262 intel_image
->base
.RowStride
= pitch
/ intel_image
->mt
->cpp
;
264 /* Immediately validate the image to the object. */
265 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
269 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
270 GLint texture_format
,
271 __DRIdrawable
*dPriv
)
273 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
274 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
275 struct gl_context
*ctx
= &brw
->ctx
;
276 struct intel_renderbuffer
*rb
;
277 struct gl_texture_object
*texObj
;
278 struct gl_texture_image
*texImage
;
279 int level
= 0, internalFormat
= 0;
280 mesa_format texFormat
= MESA_FORMAT_NONE
;
282 texObj
= _mesa_get_current_tex_object(ctx
, target
);
287 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
288 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
289 intel_update_renderbuffers(pDRICtx
, dPriv
);
291 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
292 /* If the miptree isn't set, then intel_update_renderbuffers was unable
293 * to get the BO for the drawable from the window system.
298 if (rb
->mt
->cpp
== 4) {
299 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
300 internalFormat
= GL_RGB
;
301 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
304 internalFormat
= GL_RGBA
;
305 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
307 } else if (rb
->mt
->cpp
== 2) {
308 internalFormat
= GL_RGB
;
309 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
312 _mesa_lock_texture(&brw
->ctx
, texObj
);
313 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
314 intel_miptree_make_shareable(brw
, rb
->mt
);
315 intel_set_texture_image_bo(ctx
, texImage
, rb
->mt
->bo
, target
,
316 internalFormat
, texFormat
, 0,
318 rb
->Base
.Base
.Height
,
321 _mesa_unlock_texture(&brw
->ctx
, texObj
);
325 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
326 struct gl_renderbuffer
*rb
,
327 struct gl_texture_image
*image
)
329 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
330 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
331 struct gl_texture_object
*texobj
= image
->TexObject
;
332 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
334 /* We can only handle RB allocated with AllocRenderbufferStorage, or
335 * window-system renderbuffers.
337 assert(!rb
->TexImage
);
342 _mesa_lock_texture(ctx
, texobj
);
343 _mesa_init_teximage_fields(ctx
, image
,
344 rb
->Width
, rb
->Height
, 1,
345 0, rb
->InternalFormat
, rb
->Format
);
346 image
->NumSamples
= rb
->NumSamples
;
348 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
350 /* Immediately validate the image to the object. */
351 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
353 intel_texobj
->needs_validate
= true;
354 _mesa_unlock_texture(ctx
, texobj
);
360 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
362 /* The old interface didn't have the format argument, so copy our
363 * implementation's behavior at the time.
365 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
369 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
370 struct gl_texture_object
*texObj
,
371 struct gl_texture_image
*texImage
,
372 GLeglImageOES image_handle
)
374 struct brw_context
*brw
= brw_context(ctx
);
378 screen
= brw
->intelScreen
->driScrnPriv
;
379 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
380 screen
->loaderPrivate
);
385 * Images originating via EGL_EXT_image_dma_buf_import can be used only
386 * with GL_OES_EGL_image_external only.
388 if (image
->dma_buf_imported
&& target
!= GL_TEXTURE_EXTERNAL_OES
) {
389 _mesa_error(ctx
, GL_INVALID_OPERATION
,
390 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
391 "GL_OES_EGL_image_external only");
395 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
396 _mesa_error(ctx
, GL_INVALID_OPERATION
,
397 "glEGLImageTargetTexture2DOES(external target is enabled only "
398 "for images created with EGL_EXT_image_dma_buf_import");
402 /* Disallow depth/stencil textures: we don't have a way to pass the
403 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
405 if (image
->has_depthstencil
) {
406 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
410 intel_set_texture_image_bo(ctx
, texImage
, image
->bo
,
411 target
, image
->internal_format
,
412 image
->format
, image
->offset
,
413 image
->width
, image
->height
,
415 image
->tile_x
, image
->tile_y
);
419 blit_texture_to_pbo(struct gl_context
*ctx
,
420 GLenum format
, GLenum type
,
421 GLvoid
* pixels
, struct gl_texture_image
*texImage
)
423 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
424 struct brw_context
*brw
= brw_context(ctx
);
425 const struct gl_pixelstore_attrib
*pack
= &ctx
->Pack
;
426 struct intel_buffer_object
*dst
= intel_buffer_object(pack
->BufferObj
);
428 drm_intel_bo
*dst_buffer
;
429 GLenum target
= texImage
->TexObject
->Target
;
431 /* Check if we can use GPU blit to copy from the hardware texture
432 * format to the user's format/type.
433 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
436 if (!_mesa_format_matches_format_and_type(intelImage
->mt
->format
, format
,
439 perf_debug("%s: unsupported format, fallback to CPU mapping for PBO\n",
445 if (ctx
->_ImageTransferState
) {
446 perf_debug("%s: bad transfer state, fallback to CPU mapping for PBO\n",
451 if (pack
->SwapBytes
|| pack
->LsbFirst
) {
452 perf_debug("%s: unsupported pack swap params\n",
457 if (target
== GL_TEXTURE_1D_ARRAY
||
458 target
== GL_TEXTURE_2D_ARRAY
||
459 target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
460 target
== GL_TEXTURE_3D
) {
461 perf_debug("%s: no support for multiple slices, fallback to CPU mapping "
462 "for PBO\n", __FUNCTION__
);
466 int dst_stride
= _mesa_image_row_stride(pack
, texImage
->Width
, format
, type
);
467 bool dst_flip
= false;
468 /* Mesa flips the dst_stride for ctx->Pack.Invert, our mt must have a
471 struct gl_pixelstore_attrib uninverted_pack
= *pack
;
472 if (ctx
->Pack
.Invert
) {
473 dst_stride
= -dst_stride
;
475 uninverted_pack
.Invert
= false;
477 dst_offset
= (GLintptr
) pixels
;
478 dst_offset
+= _mesa_image_offset(2, &uninverted_pack
, texImage
->Width
,
479 texImage
->Height
, format
, type
, 0, 0, 0);
480 dst_buffer
= intel_bufferobj_buffer(brw
, dst
, dst_offset
,
481 texImage
->Height
* dst_stride
);
483 struct intel_mipmap_tree
*pbo_mt
=
484 intel_miptree_create_for_bo(brw
,
486 intelImage
->mt
->format
,
488 texImage
->Width
, texImage
->Height
,
494 if (!intel_miptree_blit(brw
,
495 intelImage
->mt
, texImage
->Level
, texImage
->Face
,
499 texImage
->Width
, texImage
->Height
, GL_COPY
))
502 intel_miptree_release(&pbo_mt
);
508 intel_get_tex_image(struct gl_context
*ctx
,
509 GLenum format
, GLenum type
, GLvoid
*pixels
,
510 struct gl_texture_image
*texImage
) {
511 DBG("%s\n", __FUNCTION__
);
513 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
514 /* Using PBOs, so try the BLT based path. */
515 if (blit_texture_to_pbo(ctx
, format
, type
, pixels
, texImage
))
520 _mesa_meta_GetTexImage(ctx
, format
, type
, pixels
, texImage
);
522 DBG("%s - DONE\n", __FUNCTION__
);
526 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
528 functions
->TexImage
= intelTexImage
;
529 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
530 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
531 functions
->GetTexImage
= intel_get_tex_image
;