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
,
104 INTEL_MSAA_LAYOUT_NONE
);
107 /* There are actually quite a few combinations this will work for,
108 * more than what I've listed here.
111 check_pbo_format(GLenum format
, GLenum type
,
112 gl_format mesa_format
)
114 switch (mesa_format
) {
115 case MESA_FORMAT_ARGB8888
:
116 return (format
== GL_BGRA
&& (type
== GL_UNSIGNED_BYTE
||
117 type
== GL_UNSIGNED_INT_8_8_8_8_REV
));
118 case MESA_FORMAT_RGB565
:
119 return (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
);
121 return (format
== GL_LUMINANCE
&& type
== GL_UNSIGNED_BYTE
);
122 case MESA_FORMAT_YCBCR
:
123 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
130 /* XXX: Do this for TexSubImage also:
133 try_pbo_upload(struct gl_context
*ctx
,
134 struct gl_texture_image
*image
,
135 const struct gl_pixelstore_attrib
*unpack
,
136 GLenum format
, GLenum type
,
137 GLint width
, GLint height
, const void *pixels
)
139 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
140 struct intel_context
*intel
= intel_context(ctx
);
141 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
142 GLuint src_offset
, src_stride
;
143 GLuint dst_x
, dst_y
, dst_stride
;
144 drm_intel_bo
*dst_buffer
, *src_buffer
;
146 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
149 DBG("trying pbo upload\n");
151 if (intel
->ctx
._ImageTransferState
||
152 unpack
->SkipPixels
|| unpack
->SkipRows
) {
153 DBG("%s: image transfer\n", __FUNCTION__
);
157 if (!check_pbo_format(format
, type
, intelImage
->base
.Base
.TexFormat
)) {
158 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
159 __FUNCTION__
, _mesa_get_format_name(intelImage
->base
.Base
.TexFormat
),
164 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
, image
->TexFormat
,
167 if (!intelImage
->mt
) {
168 DBG("%s: no miptree\n", __FUNCTION__
);
172 dst_buffer
= intelImage
->mt
->region
->bo
;
173 src_buffer
= intel_bufferobj_source(intel
, pbo
, 64, &src_offset
);
174 /* note: potential 64-bit ptr to 32-bit int cast */
175 src_offset
+= (GLuint
) (unsigned long) pixels
;
177 if (unpack
->RowLength
> 0)
178 src_stride
= unpack
->RowLength
;
182 intel_miptree_get_image_offset(intelImage
->mt
, intelImage
->base
.Base
.Level
,
183 intelImage
->base
.Base
.Face
, 0,
186 dst_stride
= intelImage
->mt
->region
->pitch
;
188 if (!intelEmitCopyBlit(intel
,
190 src_stride
, src_buffer
,
192 dst_stride
, dst_buffer
, 0,
193 intelImage
->mt
->region
->tiling
,
194 0, 0, dst_x
, dst_y
, width
, height
,
196 DBG("%s: blit failed\n", __FUNCTION__
);
200 DBG("%s: success\n", __FUNCTION__
);
205 intelTexImage(struct gl_context
* ctx
,
207 struct gl_texture_image
*texImage
,
208 GLint internalFormat
,
209 GLint width
, GLint height
, GLint depth
, GLint border
,
210 GLenum format
, GLenum type
, const void *pixels
,
211 const struct gl_pixelstore_attrib
*unpack
)
213 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__
,
214 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
215 texImage
->Level
, width
, height
, depth
);
217 /* Attempt to use the blitter for PBO image uploads.
220 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
,
221 width
, height
, pixels
)) {
225 DBG("%s: upload image %dx%dx%d pixels %p\n",
226 __FUNCTION__
, width
, height
, depth
, pixels
);
228 _mesa_store_teximage(ctx
, dims
, texImage
, internalFormat
,
229 width
, height
, depth
, 0,
230 format
, type
, pixels
, unpack
);
235 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
237 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
240 intel_set_texture_image_region(struct gl_context
*ctx
,
241 struct gl_texture_image
*image
,
242 struct intel_region
*region
,
244 GLenum internalFormat
,
248 struct intel_context
*intel
= intel_context(ctx
);
249 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
250 struct gl_texture_object
*texobj
= image
->TexObject
;
251 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
253 _mesa_init_teximage_fields(&intel
->ctx
, image
,
254 region
->width
, region
->height
, 1,
255 0, internalFormat
, format
);
257 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
259 intel_image
->mt
= intel_miptree_create_for_region(intel
, target
,
262 if (intel_image
->mt
== NULL
)
265 intel_image
->mt
->offset
= offset
;
266 intel_image
->base
.RowStride
= region
->pitch
;
268 /* Immediately validate the image to the object. */
269 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
273 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
274 GLint texture_format
,
275 __DRIdrawable
*dPriv
)
277 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
278 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
279 struct gl_context
*ctx
= &intel
->ctx
;
280 struct intel_texture_object
*intelObj
;
281 struct intel_renderbuffer
*rb
;
282 struct gl_texture_object
*texObj
;
283 struct gl_texture_image
*texImage
;
284 int level
= 0, internalFormat
= 0;
285 gl_format texFormat
= MESA_FORMAT_NONE
;
287 texObj
= _mesa_get_current_tex_object(ctx
, target
);
288 intelObj
= intel_texture_object(texObj
);
293 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
294 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
295 intel_update_renderbuffers(pDRICtx
, dPriv
);
297 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
298 /* If the region isn't set, then intel_update_renderbuffers was unable
299 * to get the buffers for the drawable.
304 if (rb
->mt
->cpp
== 4) {
305 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
306 internalFormat
= GL_RGB
;
307 texFormat
= MESA_FORMAT_XRGB8888
;
310 internalFormat
= GL_RGBA
;
311 texFormat
= MESA_FORMAT_ARGB8888
;
313 } else if (rb
->mt
->cpp
== 2) {
314 internalFormat
= GL_RGB
;
315 texFormat
= MESA_FORMAT_RGB565
;
318 _mesa_lock_texture(&intel
->ctx
, texObj
);
319 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
320 intel_set_texture_image_region(ctx
, texImage
, rb
->mt
->region
, target
,
321 internalFormat
, texFormat
, 0);
322 _mesa_unlock_texture(&intel
->ctx
, texObj
);
326 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
328 /* The old interface didn't have the format argument, so copy our
329 * implementation's behavior at the time.
331 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
334 #if FEATURE_OES_EGL_image
336 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
337 struct gl_texture_object
*texObj
,
338 struct gl_texture_image
*texImage
,
339 GLeglImageOES image_handle
)
341 struct intel_context
*intel
= intel_context(ctx
);
345 screen
= intel
->intelScreen
->driScrnPriv
;
346 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
347 screen
->loaderPrivate
);
351 intel_set_texture_image_region(ctx
, texImage
, image
->region
,
352 target
, image
->internal_format
,
353 image
->format
, image
->offset
);
358 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
360 functions
->TexImage
= intelTexImage
;
362 #if FEATURE_OES_EGL_image
363 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;