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"
27 #include "intel_tiled_memcpy.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
,
89 intelTexImage(struct gl_context
* ctx
,
91 struct gl_texture_image
*texImage
,
92 GLenum format
, GLenum type
, const void *pixels
,
93 const struct gl_pixelstore_attrib
*unpack
)
95 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
98 bool tex_busy
= intelImage
->mt
&& drm_intel_bo_busy(intelImage
->mt
->bo
);
100 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
101 __FUNCTION__
, _mesa_get_format_name(texImage
->TexFormat
),
102 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
103 _mesa_lookup_enum_by_nr(format
), _mesa_lookup_enum_by_nr(type
),
104 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
106 /* Allocate storage for texture data. */
107 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
108 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
112 assert(intelImage
->mt
);
114 ok
= _mesa_meta_pbo_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
115 texImage
->Width
, texImage
->Height
,
117 format
, type
, pixels
,
118 false /*allocate_storage*/,
123 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
124 0, 0, 0, /*x,y,z offsets*/
128 format
, type
, pixels
, unpack
,
129 false /*allocate_storage*/);
133 DBG("%s: upload image %dx%dx%d pixels %p\n",
134 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
137 _mesa_store_teximage(ctx
, dims
, texImage
,
138 format
, type
, pixels
, unpack
);
143 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
145 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
148 intel_set_texture_image_bo(struct gl_context
*ctx
,
149 struct gl_texture_image
*image
,
152 GLenum internalFormat
,
155 GLuint width
, GLuint height
,
157 GLuint tile_x
, GLuint tile_y
)
159 struct brw_context
*brw
= brw_context(ctx
);
160 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
161 struct gl_texture_object
*texobj
= image
->TexObject
;
162 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
163 uint32_t draw_x
, draw_y
;
165 _mesa_init_teximage_fields(&brw
->ctx
, image
,
167 0, internalFormat
, format
);
169 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
171 intel_image
->mt
= intel_miptree_create_for_bo(brw
, bo
, image
->TexFormat
,
172 0, width
, height
, 1, pitch
);
173 if (intel_image
->mt
== NULL
)
175 intel_image
->mt
->target
= target
;
176 intel_image
->mt
->total_width
= width
;
177 intel_image
->mt
->total_height
= height
;
178 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
179 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
181 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
183 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
184 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
185 * trouble resolving back to destination image due to alignment issues.
187 if (!brw
->has_surface_tile_offset
&&
188 (draw_x
!= 0 || draw_y
!= 0)) {
189 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
190 intel_miptree_release(&intel_image
->mt
);
194 intel_texobj
->needs_validate
= true;
196 intel_image
->mt
->offset
= offset
;
197 assert(pitch
% intel_image
->mt
->cpp
== 0);
198 intel_image
->base
.RowStride
= pitch
/ intel_image
->mt
->cpp
;
200 /* Immediately validate the image to the object. */
201 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
205 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
206 GLint texture_format
,
207 __DRIdrawable
*dPriv
)
209 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
210 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
211 struct gl_context
*ctx
= &brw
->ctx
;
212 struct intel_renderbuffer
*rb
;
213 struct gl_texture_object
*texObj
;
214 struct gl_texture_image
*texImage
;
215 int level
= 0, internalFormat
= 0;
216 mesa_format texFormat
= MESA_FORMAT_NONE
;
218 texObj
= _mesa_get_current_tex_object(ctx
, target
);
223 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
224 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
225 intel_update_renderbuffers(pDRICtx
, dPriv
);
227 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
228 /* If the miptree isn't set, then intel_update_renderbuffers was unable
229 * to get the BO for the drawable from the window system.
234 if (rb
->mt
->cpp
== 4) {
235 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
236 internalFormat
= GL_RGB
;
237 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
240 internalFormat
= GL_RGBA
;
241 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
243 } else if (rb
->mt
->cpp
== 2) {
244 internalFormat
= GL_RGB
;
245 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
248 _mesa_lock_texture(&brw
->ctx
, texObj
);
249 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
250 intel_miptree_make_shareable(brw
, rb
->mt
);
251 intel_set_texture_image_bo(ctx
, texImage
, rb
->mt
->bo
, target
,
252 internalFormat
, texFormat
, 0,
254 rb
->Base
.Base
.Height
,
257 _mesa_unlock_texture(&brw
->ctx
, texObj
);
261 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
262 struct gl_renderbuffer
*rb
,
263 struct gl_texture_image
*image
)
265 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
266 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
267 struct gl_texture_object
*texobj
= image
->TexObject
;
268 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
270 /* We can only handle RB allocated with AllocRenderbufferStorage, or
271 * window-system renderbuffers.
273 assert(!rb
->TexImage
);
278 _mesa_lock_texture(ctx
, texobj
);
279 _mesa_init_teximage_fields(ctx
, image
,
280 rb
->Width
, rb
->Height
, 1,
281 0, rb
->InternalFormat
, rb
->Format
);
282 image
->NumSamples
= rb
->NumSamples
;
284 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
286 /* Immediately validate the image to the object. */
287 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
289 intel_texobj
->needs_validate
= true;
290 _mesa_unlock_texture(ctx
, texobj
);
296 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
298 /* The old interface didn't have the format argument, so copy our
299 * implementation's behavior at the time.
301 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
305 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
306 struct gl_texture_object
*texObj
,
307 struct gl_texture_image
*texImage
,
308 GLeglImageOES image_handle
)
310 struct brw_context
*brw
= brw_context(ctx
);
314 screen
= brw
->intelScreen
->driScrnPriv
;
315 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
316 screen
->loaderPrivate
);
321 * Images originating via EGL_EXT_image_dma_buf_import can be used only
322 * with GL_OES_EGL_image_external only.
324 if (image
->dma_buf_imported
&& target
!= GL_TEXTURE_EXTERNAL_OES
) {
325 _mesa_error(ctx
, GL_INVALID_OPERATION
,
326 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
327 "GL_OES_EGL_image_external only");
331 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
333 "glEGLImageTargetTexture2DOES(external target is enabled only "
334 "for images created with EGL_EXT_image_dma_buf_import");
338 /* Disallow depth/stencil textures: we don't have a way to pass the
339 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
341 if (image
->has_depthstencil
) {
342 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
346 intel_set_texture_image_bo(ctx
, texImage
, image
->bo
,
347 target
, image
->internal_format
,
348 image
->format
, image
->offset
,
349 image
->width
, image
->height
,
351 image
->tile_x
, image
->tile_y
);
355 * \brief A fast path for glGetTexImage.
357 * \see intel_readpixels_tiled_memcpy()
360 intel_gettexsubimage_tiled_memcpy(struct gl_context
*ctx
,
361 struct gl_texture_image
*texImage
,
362 GLint xoffset
, GLint yoffset
,
363 GLsizei width
, GLsizei height
,
364 GLenum format
, GLenum type
,
366 const struct gl_pixelstore_attrib
*packing
)
368 struct brw_context
*brw
= brw_context(ctx
);
369 struct intel_texture_image
*image
= intel_texture_image(texImage
);
372 /* The miptree's buffer. */
378 mem_copy_fn mem_copy
= NULL
;
380 /* This fastpath is restricted to specific texture types:
381 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
384 * FINISHME: The restrictions below on packing alignment and packing row
385 * length are likely unneeded now because we calculate the destination stride
386 * with _mesa_image_row_stride. However, before removing the restrictions
390 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
391 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
392 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
394 _mesa_is_bufferobj(packing
->BufferObj
) ||
395 packing
->Alignment
> 4 ||
396 packing
->SkipPixels
> 0 ||
397 packing
->SkipRows
> 0 ||
398 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
399 packing
->SwapBytes
||
404 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
405 * function doesn't set the last channel to 1.
407 if (texImage
->TexFormat
== MESA_FORMAT_B8G8R8X8_UNORM
||
408 texImage
->TexFormat
== MESA_FORMAT_R8G8B8X8_UNORM
)
411 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
,
415 /* If this is a nontrivial texture view, let another path handle it instead. */
416 if (texImage
->TexObject
->MinLayer
)
420 (image
->mt
->tiling
!= I915_TILING_X
&&
421 image
->mt
->tiling
!= I915_TILING_Y
)) {
422 /* The algorithm is written only for X- or Y-tiled memory. */
426 /* Since we are going to write raw data to the miptree, we need to resolve
427 * any pending fast color clears before we start.
429 intel_miptree_resolve_color(brw
, image
->mt
);
433 if (drm_intel_bo_references(brw
->batch
.bo
, bo
)) {
434 perf_debug("Flushing before mapping a referenced bo.\n");
435 intel_batchbuffer_flush(brw
);
438 error
= brw_bo_map(brw
, bo
, false /* write enable */, "miptree");
440 DBG("%s: failed to map bo\n", __FUNCTION__
);
444 dst_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
446 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
447 "mesa_format=0x%x tiling=%d "
448 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
449 __FUNCTION__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
450 format
, type
, texImage
->TexFormat
, image
->mt
->tiling
,
451 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
454 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
456 /* Adjust x and y offset based on miplevel */
457 xoffset
+= image
->mt
->level
[level
].level_x
;
458 yoffset
+= image
->mt
->level
[level
].level_y
;
461 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
462 yoffset
, yoffset
+ height
,
463 pixels
- (ptrdiff_t) yoffset
* dst_pitch
- (ptrdiff_t) xoffset
* cpp
,
465 dst_pitch
, image
->mt
->pitch
,
471 drm_intel_bo_unmap(bo
);
476 intel_get_tex_image(struct gl_context
*ctx
,
477 GLenum format
, GLenum type
, GLvoid
*pixels
,
478 struct gl_texture_image
*texImage
) {
479 struct brw_context
*brw
= brw_context(ctx
);
482 DBG("%s\n", __FUNCTION__
);
484 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
485 if (_mesa_meta_pbo_GetTexSubImage(ctx
, 3, texImage
, 0, 0, 0,
486 texImage
->Width
, texImage
->Height
,
487 texImage
->Depth
, format
, type
,
491 perf_debug("%s: fallback to CPU mapping in PBO case\n", __FUNCTION__
);
494 ok
= intel_gettexsubimage_tiled_memcpy(ctx
, texImage
, 0, 0,
495 texImage
->Width
, texImage
->Height
,
496 format
, type
, pixels
, &ctx
->Pack
);
501 _mesa_meta_GetTexImage(ctx
, format
, type
, pixels
, texImage
);
503 DBG("%s - DONE\n", __FUNCTION__
);
507 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
509 functions
->TexImage
= intelTexImage
;
510 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
511 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
512 functions
->GetTexImage
= intel_get_tex_image
;