2 #include "main/glheader.h"
3 #include "main/macros.h"
4 #include "main/mfeatures.h"
5 #include "main/mtypes.h"
6 #include "main/enums.h"
7 #include "main/bufferobj.h"
8 #include "main/context.h"
9 #include "main/formats.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_context.h"
19 #include "intel_mipmap_tree.h"
20 #include "intel_buffer_objects.h"
21 #include "intel_batchbuffer.h"
22 #include "intel_tex.h"
23 #include "intel_blit.h"
24 #include "intel_fbo.h"
25 #include "intel_span.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 intel_context
*intel
,
34 struct intel_texture_object
*intelObj
,
35 struct intel_texture_image
*intelImage
,
36 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 if (intelImage
->base
.Base
.Level
> intelObj
->base
.BaseLevel
&&
50 (intelObj
->base
.Target
!= GL_TEXTURE_1D
&& height
== 1) ||
51 (intelObj
->base
.Target
== GL_TEXTURE_3D
&& depth
== 1))) {
52 /* For this combination, we're at some lower mipmap level and
53 * some important dimension is 1. We can't extrapolate up to a
54 * likely base level width/height/depth for a full mipmap stack
55 * from this info, so just allocate this one level.
57 firstLevel
= intelImage
->base
.Base
.Level
;
58 lastLevel
= intelImage
->base
.Base
.Level
;
60 /* If this image disrespects BaseLevel, allocate from level zero.
61 * Usually BaseLevel == 0, so it's unlikely to happen.
63 if (intelImage
->base
.Base
.Level
< intelObj
->base
.BaseLevel
)
66 firstLevel
= intelObj
->base
.BaseLevel
;
68 /* Figure out image dimensions at start level. */
69 for (i
= intelImage
->base
.Base
.Level
; i
> firstLevel
; i
--) {
77 /* Guess a reasonable value for lastLevel. This is probably going
78 * to be wrong fairly often and might mean that we have to look at
79 * resizable buffers, or require that buffers implement lazy
80 * pagetable arrangements.
82 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
83 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
84 intelImage
->base
.Base
.Level
== firstLevel
&&
85 (intel
->gen
< 4 || firstLevel
== 0)) {
86 lastLevel
= firstLevel
;
87 } else if (intelObj
->base
.Target
== GL_TEXTURE_EXTERNAL_OES
) {
88 lastLevel
= firstLevel
;
90 lastLevel
= firstLevel
+ _mesa_logbase2(MAX2(MAX2(width
, height
), depth
));
94 return intel_miptree_create(intel
,
95 intelObj
->base
.Target
,
96 intelImage
->base
.Base
.TexFormat
,
102 expect_accelerated_upload
);
105 /* There are actually quite a few combinations this will work for,
106 * more than what I've listed here.
109 check_pbo_format(GLenum format
, GLenum type
,
110 gl_format mesa_format
)
112 switch (mesa_format
) {
113 case MESA_FORMAT_ARGB8888
:
114 return (format
== GL_BGRA
&& (type
== GL_UNSIGNED_BYTE
||
115 type
== GL_UNSIGNED_INT_8_8_8_8_REV
));
116 case MESA_FORMAT_RGB565
:
117 return (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
);
119 return (format
== GL_LUMINANCE
&& type
== GL_UNSIGNED_BYTE
);
120 case MESA_FORMAT_YCBCR
:
121 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
128 /* XXX: Do this for TexSubImage also:
131 try_pbo_upload(struct gl_context
*ctx
,
132 struct gl_texture_image
*image
,
133 const struct gl_pixelstore_attrib
*unpack
,
134 GLenum format
, GLenum type
,
135 GLint width
, GLint height
, const void *pixels
)
137 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
138 struct intel_context
*intel
= intel_context(ctx
);
139 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
140 GLuint src_offset
, src_stride
;
141 GLuint dst_x
, dst_y
, dst_stride
;
142 drm_intel_bo
*dst_buffer
, *src_buffer
;
144 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
147 DBG("trying pbo upload\n");
149 if (intel
->ctx
._ImageTransferState
||
150 unpack
->SkipPixels
|| unpack
->SkipRows
) {
151 DBG("%s: image transfer\n", __FUNCTION__
);
155 if (!check_pbo_format(format
, type
, intelImage
->base
.Base
.TexFormat
)) {
156 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
157 __FUNCTION__
, _mesa_get_format_name(intelImage
->base
.Base
.TexFormat
),
162 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
, image
->TexFormat
,
165 if (!intelImage
->mt
) {
166 DBG("%s: no miptree\n", __FUNCTION__
);
170 dst_buffer
= intelImage
->mt
->region
->bo
;
171 src_buffer
= intel_bufferobj_source(intel
, pbo
, 64, &src_offset
);
172 /* note: potential 64-bit ptr to 32-bit int cast */
173 src_offset
+= (GLuint
) (unsigned long) pixels
;
175 if (unpack
->RowLength
> 0)
176 src_stride
= unpack
->RowLength
;
180 intel_miptree_get_image_offset(intelImage
->mt
, intelImage
->base
.Base
.Level
,
181 intelImage
->base
.Base
.Face
, 0,
184 dst_stride
= intelImage
->mt
->region
->pitch
;
186 if (!intelEmitCopyBlit(intel
,
188 src_stride
, src_buffer
,
190 dst_stride
, dst_buffer
, 0,
191 intelImage
->mt
->region
->tiling
,
192 0, 0, dst_x
, dst_y
, width
, height
,
194 DBG("%s: blit failed\n", __FUNCTION__
);
198 DBG("%s: success\n", __FUNCTION__
);
203 intelTexImage(struct gl_context
* ctx
,
205 struct gl_texture_image
*texImage
,
206 GLint internalFormat
,
207 GLint width
, GLint height
, GLint depth
,
208 GLenum format
, GLenum type
, const void *pixels
,
209 const struct gl_pixelstore_attrib
*unpack
,
212 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__
,
213 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
214 texImage
->Level
, width
, height
, depth
);
216 /* Attempt to use the blitter for PBO image uploads.
219 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
,
220 width
, height
, pixels
)) {
224 DBG("%s: upload image %dx%dx%d pixels %p\n",
225 __FUNCTION__
, width
, height
, depth
, pixels
);
227 _mesa_store_teximage3d(ctx
, texImage
, internalFormat
,
228 width
, height
, depth
, 0,
229 format
, type
, pixels
, unpack
);
234 intelTexImage3D(struct gl_context
* ctx
,
235 struct gl_texture_image
*texImage
,
236 GLint internalFormat
,
237 GLint width
, GLint height
, GLint depth
,
239 GLenum format
, GLenum type
, const void *pixels
,
240 const struct gl_pixelstore_attrib
*unpack
)
242 intelTexImage(ctx
, 3, texImage
,
243 internalFormat
, width
, height
, depth
,
244 format
, type
, pixels
, unpack
, 0);
249 intelTexImage2D(struct gl_context
* ctx
,
250 struct gl_texture_image
*texImage
,
251 GLint internalFormat
,
252 GLint width
, GLint height
, GLint border
,
253 GLenum format
, GLenum type
, const void *pixels
,
254 const struct gl_pixelstore_attrib
*unpack
)
256 intelTexImage(ctx
, 2, texImage
,
257 internalFormat
, width
, height
, 1,
258 format
, type
, pixels
, unpack
, 0);
263 intelTexImage1D(struct gl_context
* ctx
,
264 struct gl_texture_image
*texImage
,
265 GLint internalFormat
,
266 GLint width
, GLint border
,
267 GLenum format
, GLenum type
, const void *pixels
,
268 const struct gl_pixelstore_attrib
*unpack
)
270 intelTexImage(ctx
, 1, texImage
,
271 internalFormat
, width
, 1, 1,
272 format
, type
, pixels
, unpack
, 0);
277 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
279 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
282 intel_set_texture_image_region(struct gl_context
*ctx
,
283 struct gl_texture_image
*image
,
284 struct intel_region
*region
,
286 GLenum internalFormat
,
289 struct intel_context
*intel
= intel_context(ctx
);
290 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
291 struct gl_texture_object
*texobj
= image
->TexObject
;
292 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
294 _mesa_init_teximage_fields(&intel
->ctx
, image
,
295 region
->width
, region
->height
, 1,
296 0, internalFormat
, format
);
298 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
300 intel_image
->mt
= intel_miptree_create_for_region(intel
, target
,
303 if (intel_image
->mt
== NULL
)
306 intel_image
->base
.RowStride
= region
->pitch
;
308 /* Immediately validate the image to the object. */
309 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
313 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
314 GLint texture_format
,
315 __DRIdrawable
*dPriv
)
317 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
318 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
319 struct gl_context
*ctx
= &intel
->ctx
;
320 struct intel_texture_object
*intelObj
;
321 struct intel_renderbuffer
*rb
;
322 struct gl_texture_object
*texObj
;
323 struct gl_texture_image
*texImage
;
324 int level
= 0, internalFormat
;
327 texObj
= _mesa_get_current_tex_object(ctx
, target
);
328 intelObj
= intel_texture_object(texObj
);
333 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
334 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
335 intel_update_renderbuffers(pDRICtx
, dPriv
);
337 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
338 /* If the region isn't set, then intel_update_renderbuffers was unable
339 * to get the buffers for the drawable.
344 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
345 internalFormat
= GL_RGB
;
346 texFormat
= MESA_FORMAT_XRGB8888
;
349 internalFormat
= GL_RGBA
;
350 texFormat
= MESA_FORMAT_ARGB8888
;
353 _mesa_lock_texture(&intel
->ctx
, texObj
);
354 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
355 intel_set_texture_image_region(ctx
, texImage
, rb
->mt
->region
, target
,
356 internalFormat
, texFormat
);
357 _mesa_unlock_texture(&intel
->ctx
, texObj
);
361 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
363 /* The old interface didn't have the format argument, so copy our
364 * implementation's behavior at the time.
366 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
369 #if FEATURE_OES_EGL_image
371 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
372 struct gl_texture_object
*texObj
,
373 struct gl_texture_image
*texImage
,
374 GLeglImageOES image_handle
)
376 struct intel_context
*intel
= intel_context(ctx
);
380 screen
= intel
->intelScreen
->driScrnPriv
;
381 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
382 screen
->loaderPrivate
);
386 intel_set_texture_image_region(ctx
, texImage
, image
->region
,
387 target
, image
->internal_format
, image
->format
);
392 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
394 functions
->TexImage1D
= intelTexImage1D
;
395 functions
->TexImage2D
= intelTexImage2D
;
396 functions
->TexImage3D
= intelTexImage3D
;
398 #if FEATURE_OES_EGL_image
399 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;