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"
25 #include "brw_context.h"
27 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
29 /* Work back from the specified level of the image to the baselevel and create a
30 * miptree of that size.
32 struct intel_mipmap_tree
*
33 intel_miptree_create_for_teximage(struct brw_context
*brw
,
34 struct intel_texture_object
*intelObj
,
35 struct intel_texture_image
*intelImage
,
36 bool expect_accelerated_upload
)
39 int width
, height
, depth
;
42 intel_miptree_get_dimensions_for_image(&intelImage
->base
.Base
,
43 &width
, &height
, &depth
);
45 DBG("%s\n", __FUNCTION__
);
47 /* Figure out image dimensions at start level. */
48 for (i
= intelImage
->base
.Base
.Level
; i
> 0; i
--) {
56 /* Guess a reasonable value for lastLevel. This is probably going
57 * to be wrong fairly often and might mean that we have to look at
58 * resizable buffers, or require that buffers implement lazy
59 * pagetable arrangements.
61 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
62 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
63 intelImage
->base
.Base
.Level
== 0 &&
64 !intelObj
->base
.GenerateMipmap
) {
67 lastLevel
= _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
68 width
, height
, depth
) - 1;
71 return intel_miptree_create(brw
,
72 intelObj
->base
.Target
,
73 intelImage
->base
.Base
.TexFormat
,
79 expect_accelerated_upload
,
80 intelImage
->base
.Base
.NumSamples
,
81 INTEL_MIPTREE_TILING_ANY
);
84 /* XXX: Do this for TexSubImage also:
87 try_pbo_upload(struct gl_context
*ctx
,
88 struct gl_texture_image
*image
,
89 const struct gl_pixelstore_attrib
*unpack
,
90 GLenum format
, GLenum type
, const void *pixels
)
92 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
93 struct brw_context
*brw
= brw_context(ctx
);
94 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
96 drm_intel_bo
*src_buffer
;
98 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
101 DBG("trying pbo upload\n");
103 if (ctx
->_ImageTransferState
|| unpack
->SkipPixels
|| unpack
->SkipRows
) {
104 DBG("%s: image transfer\n", __FUNCTION__
);
108 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
);
110 if (!intelImage
->mt
) {
111 DBG("%s: no miptree\n", __FUNCTION__
);
115 if (!_mesa_format_matches_format_and_type(intelImage
->mt
->format
,
116 format
, type
, false)) {
117 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
118 __FUNCTION__
, _mesa_get_format_name(intelImage
->mt
->format
),
123 if (image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
||
124 image
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
125 DBG("%s: no support for array textures\n", __FUNCTION__
);
129 src_buffer
= intel_bufferobj_buffer(brw
, pbo
, INTEL_READ
);
130 /* note: potential 64-bit ptr to 32-bit int cast */
131 src_offset
= (GLuint
) (unsigned long) pixels
;
134 _mesa_image_row_stride(unpack
, image
->Width
, format
, type
);
136 struct intel_mipmap_tree
*pbo_mt
=
137 intel_miptree_create_for_bo(brw
,
139 intelImage
->mt
->format
,
141 image
->Width
, image
->Height
,
142 src_stride
, I915_TILING_NONE
);
146 if (!intel_miptree_blit(brw
,
149 intelImage
->mt
, image
->Level
, image
->Face
,
151 image
->Width
, image
->Height
, GL_COPY
)) {
152 DBG("%s: blit failed\n", __FUNCTION__
);
153 intel_miptree_release(&pbo_mt
);
157 intel_miptree_release(&pbo_mt
);
159 DBG("%s: success\n", __FUNCTION__
);
164 intelTexImage(struct gl_context
* ctx
,
166 struct gl_texture_image
*texImage
,
167 GLenum format
, GLenum type
, const void *pixels
,
168 const struct gl_pixelstore_attrib
*unpack
)
172 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__
,
173 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
174 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
176 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
177 0, 0, 0, /*x,y,z offsets*/
181 format
, type
, pixels
, unpack
,
182 true /*for_glTexImage*/);
186 /* Attempt to use the blitter for PBO image uploads.
189 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
, pixels
)) {
193 DBG("%s: upload image %dx%dx%d pixels %p\n",
194 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
197 _mesa_store_teximage(ctx
, dims
, texImage
,
198 format
, type
, pixels
, unpack
);
203 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
205 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
208 intel_set_texture_image_region(struct gl_context
*ctx
,
209 struct gl_texture_image
*image
,
210 struct intel_region
*region
,
212 GLenum internalFormat
,
220 struct brw_context
*brw
= brw_context(ctx
);
221 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
222 struct gl_texture_object
*texobj
= image
->TexObject
;
223 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
224 uint32_t draw_x
, draw_y
;
226 _mesa_init_teximage_fields(&brw
->ctx
, image
,
228 0, internalFormat
, format
);
230 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
232 intel_image
->mt
= intel_miptree_create_layout(brw
, target
, image
->TexFormat
,
235 true, 0 /* num_samples */);
236 if (intel_image
->mt
== NULL
)
238 intel_region_reference(&intel_image
->mt
->region
, region
);
239 intel_image
->mt
->total_width
= width
;
240 intel_image
->mt
->total_height
= height
;
241 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
242 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
244 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
246 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
247 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
248 * trouble resolving back to destination image due to alignment issues.
250 if (!brw
->has_surface_tile_offset
&&
251 (draw_x
!= 0 || draw_y
!= 0)) {
252 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
253 intel_miptree_release(&intel_image
->mt
);
257 intel_texobj
->needs_validate
= true;
259 intel_image
->mt
->offset
= offset
;
260 assert(region
->pitch
% region
->cpp
== 0);
261 intel_image
->base
.RowStride
= region
->pitch
/ region
->cpp
;
263 /* Immediately validate the image to the object. */
264 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
268 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
269 GLint texture_format
,
270 __DRIdrawable
*dPriv
)
272 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
273 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
274 struct gl_context
*ctx
= &brw
->ctx
;
275 struct intel_texture_object
*intelObj
;
276 struct intel_renderbuffer
*rb
;
277 struct gl_texture_object
*texObj
;
278 struct gl_texture_image
*texImage
;
279 int level
= 0, internalFormat
= 0;
280 gl_format texFormat
= MESA_FORMAT_NONE
;
282 texObj
= _mesa_get_current_tex_object(ctx
, target
);
283 intelObj
= intel_texture_object(texObj
);
288 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
289 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
290 intel_update_renderbuffers(pDRICtx
, dPriv
);
292 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
293 /* If the region isn't set, then intel_update_renderbuffers was unable
294 * to get the buffers for the drawable.
299 if (rb
->mt
->cpp
== 4) {
300 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
301 internalFormat
= GL_RGB
;
302 texFormat
= MESA_FORMAT_XRGB8888
;
305 internalFormat
= GL_RGBA
;
306 texFormat
= MESA_FORMAT_ARGB8888
;
308 } else if (rb
->mt
->cpp
== 2) {
309 internalFormat
= GL_RGB
;
310 texFormat
= MESA_FORMAT_RGB565
;
313 _mesa_lock_texture(&brw
->ctx
, texObj
);
314 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
315 intel_miptree_make_shareable(brw
, rb
->mt
);
316 intel_set_texture_image_region(ctx
, texImage
, rb
->mt
->region
, target
,
317 internalFormat
, texFormat
, 0,
318 rb
->mt
->region
->width
,
319 rb
->mt
->region
->height
,
321 _mesa_unlock_texture(&brw
->ctx
, texObj
);
325 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
327 /* The old interface didn't have the format argument, so copy our
328 * implementation's behavior at the time.
330 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
334 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
335 struct gl_texture_object
*texObj
,
336 struct gl_texture_image
*texImage
,
337 GLeglImageOES image_handle
)
339 struct brw_context
*brw
= brw_context(ctx
);
343 screen
= brw
->intelScreen
->driScrnPriv
;
344 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
345 screen
->loaderPrivate
);
350 * Images originating via EGL_EXT_image_dma_buf_import can be used only
351 * with GL_OES_EGL_image_external only.
353 if (image
->dma_buf_imported
&& target
!= GL_TEXTURE_EXTERNAL_OES
) {
354 _mesa_error(ctx
, GL_INVALID_OPERATION
,
355 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
356 "GL_OES_EGL_image_external only");
360 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
361 _mesa_error(ctx
, GL_INVALID_OPERATION
,
362 "glEGLImageTargetTexture2DOES(external target is enabled only "
363 "for images created with EGL_EXT_image_dma_buf_import");
367 /* Disallow depth/stencil textures: we don't have a way to pass the
368 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
370 if (image
->has_depthstencil
) {
371 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
375 intel_set_texture_image_region(ctx
, texImage
, image
->region
,
376 target
, image
->internal_format
,
377 image
->format
, image
->offset
,
378 image
->width
, image
->height
,
379 image
->tile_x
, image
->tile_y
);
383 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
385 functions
->TexImage
= intelTexImage
;
386 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;