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 "intel_mipmap_tree.h"
19 #include "intel_buffer_objects.h"
20 #include "intel_batchbuffer.h"
21 #include "intel_tex.h"
22 #include "intel_blit.h"
23 #include "intel_fbo.h"
24 #include "intel_image.h"
26 #include "brw_context.h"
28 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
30 /* Work back from the specified level of the image to the baselevel and create a
31 * miptree of that size.
33 struct intel_mipmap_tree
*
34 intel_miptree_create_for_teximage(struct brw_context
*brw
,
35 struct intel_texture_object
*intelObj
,
36 struct intel_texture_image
*intelImage
,
37 bool expect_accelerated_upload
)
40 int width
, height
, depth
;
43 intel_miptree_get_dimensions_for_image(&intelImage
->base
.Base
,
44 &width
, &height
, &depth
);
46 DBG("%s\n", __FUNCTION__
);
48 /* Figure out image dimensions at start level. */
49 for (i
= intelImage
->base
.Base
.Level
; i
> 0; i
--) {
57 /* Guess a reasonable value for lastLevel. This is probably going
58 * to be wrong fairly often and might mean that we have to look at
59 * resizable buffers, or require that buffers implement lazy
60 * pagetable arrangements.
62 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
63 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
64 intelImage
->base
.Base
.Level
== 0 &&
65 !intelObj
->base
.GenerateMipmap
) {
68 lastLevel
= _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
69 width
, height
, depth
) - 1;
72 return intel_miptree_create(brw
,
73 intelObj
->base
.Target
,
74 intelImage
->base
.Base
.TexFormat
,
80 expect_accelerated_upload
,
81 intelImage
->base
.Base
.NumSamples
,
82 INTEL_MIPTREE_TILING_ANY
);
85 /* XXX: Do this for TexSubImage also:
88 try_pbo_upload(struct gl_context
*ctx
,
89 struct gl_texture_image
*image
,
90 const struct gl_pixelstore_attrib
*unpack
,
91 GLenum format
, GLenum type
, const void *pixels
)
93 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
94 struct brw_context
*brw
= brw_context(ctx
);
95 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
97 drm_intel_bo
*src_buffer
;
99 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
102 DBG("trying pbo upload\n");
104 if (ctx
->_ImageTransferState
|| unpack
->SkipPixels
|| unpack
->SkipRows
) {
105 DBG("%s: image transfer\n", __FUNCTION__
);
109 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
);
111 if (!intelImage
->mt
) {
112 DBG("%s: no miptree\n", __FUNCTION__
);
116 if (!_mesa_format_matches_format_and_type(intelImage
->mt
->format
,
117 format
, type
, false)) {
118 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
119 __FUNCTION__
, _mesa_get_format_name(intelImage
->mt
->format
),
124 if (image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
||
125 image
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
126 DBG("%s: no support for array textures\n", __FUNCTION__
);
131 _mesa_image_row_stride(unpack
, image
->Width
, format
, type
);
133 /* note: potential 64-bit ptr to 32-bit int cast */
134 src_offset
= (GLuint
) (unsigned long) pixels
;
135 src_buffer
= intel_bufferobj_buffer(brw
, pbo
,
136 src_offset
, src_stride
* image
->Height
);
138 struct intel_mipmap_tree
*pbo_mt
=
139 intel_miptree_create_for_bo(brw
,
141 intelImage
->mt
->format
,
143 image
->Width
, image
->Height
,
148 if (!intel_miptree_blit(brw
,
151 intelImage
->mt
, image
->Level
, image
->Face
,
153 image
->Width
, image
->Height
, GL_COPY
)) {
154 DBG("%s: blit failed\n", __FUNCTION__
);
155 intel_miptree_release(&pbo_mt
);
159 intel_miptree_release(&pbo_mt
);
161 DBG("%s: success\n", __FUNCTION__
);
166 intelTexImage(struct gl_context
* ctx
,
168 struct gl_texture_image
*texImage
,
169 GLenum format
, GLenum type
, const void *pixels
,
170 const struct gl_pixelstore_attrib
*unpack
)
174 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__
,
175 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
176 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
178 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
179 0, 0, 0, /*x,y,z offsets*/
183 format
, type
, pixels
, unpack
,
184 true /*for_glTexImage*/);
188 /* Attempt to use the blitter for PBO image uploads.
191 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
, pixels
)) {
195 DBG("%s: upload image %dx%dx%d pixels %p\n",
196 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
199 _mesa_store_teximage(ctx
, dims
, texImage
,
200 format
, type
, pixels
, unpack
);
205 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
207 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
210 intel_set_texture_image_bo(struct gl_context
*ctx
,
211 struct gl_texture_image
*image
,
214 GLenum internalFormat
,
217 GLuint width
, GLuint height
,
219 GLuint tile_x
, GLuint tile_y
)
221 struct brw_context
*brw
= brw_context(ctx
);
222 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
223 struct gl_texture_object
*texobj
= image
->TexObject
;
224 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
225 uint32_t draw_x
, draw_y
;
227 _mesa_init_teximage_fields(&brw
->ctx
, image
,
229 0, internalFormat
, format
);
231 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
233 intel_image
->mt
= intel_miptree_create_for_bo(brw
, bo
, image
->TexFormat
,
234 0, width
, height
, pitch
);
235 if (intel_image
->mt
== NULL
)
237 intel_image
->mt
->target
= target
;
238 intel_image
->mt
->total_width
= width
;
239 intel_image
->mt
->total_height
= height
;
240 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
241 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
243 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
245 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
246 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
247 * trouble resolving back to destination image due to alignment issues.
249 if (!brw
->has_surface_tile_offset
&&
250 (draw_x
!= 0 || draw_y
!= 0)) {
251 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
252 intel_miptree_release(&intel_image
->mt
);
256 intel_texobj
->needs_validate
= true;
258 intel_image
->mt
->offset
= offset
;
259 assert(pitch
% intel_image
->mt
->cpp
== 0);
260 intel_image
->base
.RowStride
= pitch
/ intel_image
->mt
->cpp
;
262 /* Immediately validate the image to the object. */
263 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
267 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
268 GLint texture_format
,
269 __DRIdrawable
*dPriv
)
271 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
272 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
273 struct gl_context
*ctx
= &brw
->ctx
;
274 struct intel_renderbuffer
*rb
;
275 struct gl_texture_object
*texObj
;
276 struct gl_texture_image
*texImage
;
277 int level
= 0, internalFormat
= 0;
278 mesa_format texFormat
= MESA_FORMAT_NONE
;
280 texObj
= _mesa_get_current_tex_object(ctx
, target
);
285 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
286 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
287 intel_update_renderbuffers(pDRICtx
, dPriv
);
289 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
290 /* If the miptree isn't set, then intel_update_renderbuffers was unable
291 * to get the BO for the drawable from the window system.
296 if (rb
->mt
->cpp
== 4) {
297 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
298 internalFormat
= GL_RGB
;
299 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
302 internalFormat
= GL_RGBA
;
303 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
305 } else if (rb
->mt
->cpp
== 2) {
306 internalFormat
= GL_RGB
;
307 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
310 _mesa_lock_texture(&brw
->ctx
, texObj
);
311 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
312 intel_miptree_make_shareable(brw
, rb
->mt
);
313 intel_set_texture_image_bo(ctx
, texImage
, rb
->mt
->bo
, target
,
314 internalFormat
, texFormat
, 0,
316 rb
->Base
.Base
.Height
,
319 _mesa_unlock_texture(&brw
->ctx
, texObj
);
323 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
324 struct gl_renderbuffer
*rb
,
325 struct gl_texture_image
*image
)
327 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
328 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
329 struct gl_texture_object
*texobj
= image
->TexObject
;
330 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
332 /* We can only handle RB allocated with AllocRenderbufferStorage, or
333 * window-system renderbuffers.
335 assert(!rb
->TexImage
);
340 _mesa_lock_texture(ctx
, texobj
);
341 _mesa_init_teximage_fields(ctx
, image
,
342 rb
->Width
, rb
->Height
, 1,
343 0, rb
->InternalFormat
, rb
->Format
);
344 image
->NumSamples
= rb
->NumSamples
;
346 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
348 /* Immediately validate the image to the object. */
349 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
351 intel_texobj
->needs_validate
= true;
352 _mesa_unlock_texture(ctx
, texobj
);
358 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
360 /* The old interface didn't have the format argument, so copy our
361 * implementation's behavior at the time.
363 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
367 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
368 struct gl_texture_object
*texObj
,
369 struct gl_texture_image
*texImage
,
370 GLeglImageOES image_handle
)
372 struct brw_context
*brw
= brw_context(ctx
);
376 screen
= brw
->intelScreen
->driScrnPriv
;
377 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
378 screen
->loaderPrivate
);
383 * Images originating via EGL_EXT_image_dma_buf_import can be used only
384 * with GL_OES_EGL_image_external only.
386 if (image
->dma_buf_imported
&& target
!= GL_TEXTURE_EXTERNAL_OES
) {
387 _mesa_error(ctx
, GL_INVALID_OPERATION
,
388 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
389 "GL_OES_EGL_image_external only");
393 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
394 _mesa_error(ctx
, GL_INVALID_OPERATION
,
395 "glEGLImageTargetTexture2DOES(external target is enabled only "
396 "for images created with EGL_EXT_image_dma_buf_import");
400 /* Disallow depth/stencil textures: we don't have a way to pass the
401 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
403 if (image
->has_depthstencil
) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
408 intel_set_texture_image_bo(ctx
, texImage
, image
->bo
,
409 target
, image
->internal_format
,
410 image
->format
, image
->offset
,
411 image
->width
, image
->height
,
413 image
->tile_x
, image
->tile_y
);
417 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
419 functions
->TexImage
= intelTexImage
;
420 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
421 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;