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
,
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 ok
= _mesa_meta_pbo_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
107 texImage
->Width
, texImage
->Height
,
109 format
, type
, pixels
,
110 true, tex_busy
, unpack
);
114 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
115 0, 0, 0, /*x,y,z offsets*/
119 format
, type
, pixels
, unpack
,
120 true /*for_glTexImage*/);
124 DBG("%s: upload image %dx%dx%d pixels %p\n",
125 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
128 _mesa_store_teximage(ctx
, dims
, texImage
,
129 format
, type
, pixels
, unpack
);
134 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
136 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
139 intel_set_texture_image_bo(struct gl_context
*ctx
,
140 struct gl_texture_image
*image
,
143 GLenum internalFormat
,
146 GLuint width
, GLuint height
,
148 GLuint tile_x
, GLuint tile_y
)
150 struct brw_context
*brw
= brw_context(ctx
);
151 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
152 struct gl_texture_object
*texobj
= image
->TexObject
;
153 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
154 uint32_t draw_x
, draw_y
;
156 _mesa_init_teximage_fields(&brw
->ctx
, image
,
158 0, internalFormat
, format
);
160 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
162 intel_image
->mt
= intel_miptree_create_for_bo(brw
, bo
, image
->TexFormat
,
163 0, width
, height
, 1, pitch
);
164 if (intel_image
->mt
== NULL
)
166 intel_image
->mt
->target
= target
;
167 intel_image
->mt
->total_width
= width
;
168 intel_image
->mt
->total_height
= height
;
169 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
170 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
172 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
174 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
175 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
176 * trouble resolving back to destination image due to alignment issues.
178 if (!brw
->has_surface_tile_offset
&&
179 (draw_x
!= 0 || draw_y
!= 0)) {
180 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
181 intel_miptree_release(&intel_image
->mt
);
185 intel_texobj
->needs_validate
= true;
187 intel_image
->mt
->offset
= offset
;
188 assert(pitch
% intel_image
->mt
->cpp
== 0);
189 intel_image
->base
.RowStride
= pitch
/ intel_image
->mt
->cpp
;
191 /* Immediately validate the image to the object. */
192 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
196 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
197 GLint texture_format
,
198 __DRIdrawable
*dPriv
)
200 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
201 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
202 struct gl_context
*ctx
= &brw
->ctx
;
203 struct intel_renderbuffer
*rb
;
204 struct gl_texture_object
*texObj
;
205 struct gl_texture_image
*texImage
;
206 int level
= 0, internalFormat
= 0;
207 mesa_format texFormat
= MESA_FORMAT_NONE
;
209 texObj
= _mesa_get_current_tex_object(ctx
, target
);
214 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
215 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
216 intel_update_renderbuffers(pDRICtx
, dPriv
);
218 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
219 /* If the miptree isn't set, then intel_update_renderbuffers was unable
220 * to get the BO for the drawable from the window system.
225 if (rb
->mt
->cpp
== 4) {
226 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
227 internalFormat
= GL_RGB
;
228 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
231 internalFormat
= GL_RGBA
;
232 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
234 } else if (rb
->mt
->cpp
== 2) {
235 internalFormat
= GL_RGB
;
236 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
239 _mesa_lock_texture(&brw
->ctx
, texObj
);
240 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
241 intel_miptree_make_shareable(brw
, rb
->mt
);
242 intel_set_texture_image_bo(ctx
, texImage
, rb
->mt
->bo
, target
,
243 internalFormat
, texFormat
, 0,
245 rb
->Base
.Base
.Height
,
248 _mesa_unlock_texture(&brw
->ctx
, texObj
);
252 intel_bind_renderbuffer_tex_image(struct gl_context
*ctx
,
253 struct gl_renderbuffer
*rb
,
254 struct gl_texture_image
*image
)
256 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
257 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
258 struct gl_texture_object
*texobj
= image
->TexObject
;
259 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
261 /* We can only handle RB allocated with AllocRenderbufferStorage, or
262 * window-system renderbuffers.
264 assert(!rb
->TexImage
);
269 _mesa_lock_texture(ctx
, texobj
);
270 _mesa_init_teximage_fields(ctx
, image
,
271 rb
->Width
, rb
->Height
, 1,
272 0, rb
->InternalFormat
, rb
->Format
);
273 image
->NumSamples
= rb
->NumSamples
;
275 intel_miptree_reference(&intel_image
->mt
, irb
->mt
);
277 /* Immediately validate the image to the object. */
278 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
280 intel_texobj
->needs_validate
= true;
281 _mesa_unlock_texture(ctx
, texobj
);
287 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
289 /* The old interface didn't have the format argument, so copy our
290 * implementation's behavior at the time.
292 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
296 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
297 struct gl_texture_object
*texObj
,
298 struct gl_texture_image
*texImage
,
299 GLeglImageOES image_handle
)
301 struct brw_context
*brw
= brw_context(ctx
);
305 screen
= brw
->intelScreen
->driScrnPriv
;
306 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
307 screen
->loaderPrivate
);
312 * Images originating via EGL_EXT_image_dma_buf_import can be used only
313 * with GL_OES_EGL_image_external only.
315 if (image
->dma_buf_imported
&& target
!= GL_TEXTURE_EXTERNAL_OES
) {
316 _mesa_error(ctx
, GL_INVALID_OPERATION
,
317 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
318 "GL_OES_EGL_image_external only");
322 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
323 _mesa_error(ctx
, GL_INVALID_OPERATION
,
324 "glEGLImageTargetTexture2DOES(external target is enabled only "
325 "for images created with EGL_EXT_image_dma_buf_import");
329 /* Disallow depth/stencil textures: we don't have a way to pass the
330 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
332 if (image
->has_depthstencil
) {
333 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
337 intel_set_texture_image_bo(ctx
, texImage
, image
->bo
,
338 target
, image
->internal_format
,
339 image
->format
, image
->offset
,
340 image
->width
, image
->height
,
342 image
->tile_x
, image
->tile_y
);
346 intel_get_tex_image(struct gl_context
*ctx
,
347 GLenum format
, GLenum type
, GLvoid
*pixels
,
348 struct gl_texture_image
*texImage
) {
349 struct brw_context
*brw
= brw_context(ctx
);
351 DBG("%s\n", __FUNCTION__
);
353 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
354 if (_mesa_meta_pbo_GetTexSubImage(ctx
, 3, texImage
, 0, 0, 0,
355 texImage
->Width
, texImage
->Height
,
356 texImage
->Depth
, format
, type
,
360 perf_debug("%s: fallback to CPU mapping in PBO case\n", __FUNCTION__
);
363 _mesa_meta_GetTexImage(ctx
, format
, type
, pixels
, texImage
);
365 DBG("%s - DONE\n", __FUNCTION__
);
369 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
371 functions
->TexImage
= intelTexImage
;
372 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;
373 functions
->BindRenderbufferTexImage
= intel_bind_renderbuffer_tex_image
;
374 functions
->GetTexImage
= intel_get_tex_image
;