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_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"
27 #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 intel_context
*intel
,
37 struct intel_texture_object
*intelObj
,
38 struct intel_texture_image
*intelImage
,
39 bool expect_accelerated_upload
)
43 int width
, height
, depth
;
46 intel_miptree_get_dimensions_for_image(&intelImage
->base
.Base
,
47 &width
, &height
, &depth
);
49 DBG("%s\n", __FUNCTION__
);
51 if (intelImage
->base
.Base
.Level
> intelObj
->base
.BaseLevel
&&
53 (intelObj
->base
.Target
!= GL_TEXTURE_1D
&& height
== 1) ||
54 (intelObj
->base
.Target
== GL_TEXTURE_3D
&& depth
== 1))) {
55 /* For this combination, we're at some lower mipmap level and
56 * some important dimension is 1. We can't extrapolate up to a
57 * likely base level width/height/depth for a full mipmap stack
58 * from this info, so just allocate this one level.
60 firstLevel
= intelImage
->base
.Base
.Level
;
61 lastLevel
= intelImage
->base
.Base
.Level
;
63 /* If this image disrespects BaseLevel, allocate from level zero.
64 * Usually BaseLevel == 0, so it's unlikely to happen.
66 if (intelImage
->base
.Base
.Level
< intelObj
->base
.BaseLevel
)
69 firstLevel
= intelObj
->base
.BaseLevel
;
71 /* Figure out image dimensions at start level. */
72 for (i
= intelImage
->base
.Base
.Level
; i
> firstLevel
; i
--) {
80 /* Guess a reasonable value for lastLevel. This is probably going
81 * to be wrong fairly often and might mean that we have to look at
82 * resizable buffers, or require that buffers implement lazy
83 * pagetable arrangements.
85 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
86 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
87 intelImage
->base
.Base
.Level
== firstLevel
&&
89 lastLevel
= firstLevel
;
91 lastLevel
= (firstLevel
+
92 _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
93 width
, height
, depth
) - 1);
97 return intel_miptree_create(intel
,
98 intelObj
->base
.Target
,
99 intelImage
->base
.Base
.TexFormat
,
105 expect_accelerated_upload
,
106 intelImage
->base
.Base
.NumSamples
,
107 INTEL_MIPTREE_TILING_ANY
);
110 /* XXX: Do this for TexSubImage also:
113 try_pbo_upload(struct gl_context
*ctx
,
114 struct gl_texture_image
*image
,
115 const struct gl_pixelstore_attrib
*unpack
,
116 GLenum format
, GLenum type
, const void *pixels
)
118 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
119 struct intel_context
*intel
= intel_context(ctx
);
120 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
122 drm_intel_bo
*src_buffer
;
124 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
127 DBG("trying pbo upload\n");
129 if (intel
->ctx
._ImageTransferState
||
130 unpack
->SkipPixels
|| unpack
->SkipRows
) {
131 DBG("%s: image transfer\n", __FUNCTION__
);
135 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
);
137 if (!intelImage
->mt
) {
138 DBG("%s: no miptree\n", __FUNCTION__
);
142 if (!_mesa_format_matches_format_and_type(intelImage
->mt
->format
,
143 format
, type
, false)) {
144 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
145 __FUNCTION__
, _mesa_get_format_name(intelImage
->mt
->format
),
150 if (image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
||
151 image
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
152 DBG("%s: no support for array textures\n", __FUNCTION__
);
156 src_buffer
= intel_bufferobj_source(intel
, pbo
, 64, &src_offset
);
157 /* note: potential 64-bit ptr to 32-bit int cast */
158 src_offset
+= (GLuint
) (unsigned long) pixels
;
161 _mesa_image_row_stride(unpack
, image
->Width
, format
, type
);
163 struct intel_mipmap_tree
*pbo_mt
=
164 intel_miptree_create_for_bo(intel
,
166 intelImage
->mt
->format
,
168 image
->Width
, image
->Height
,
169 src_stride
, I915_TILING_NONE
);
173 if (!intel_miptree_blit(intel
,
176 intelImage
->mt
, image
->Level
, image
->Face
,
178 image
->Width
, image
->Height
, GL_COPY
)) {
179 DBG("%s: blit failed\n", __FUNCTION__
);
183 intel_miptree_release(&pbo_mt
);
185 DBG("%s: success\n", __FUNCTION__
);
190 intelTexImage(struct gl_context
* ctx
,
192 struct gl_texture_image
*texImage
,
193 GLenum format
, GLenum type
, const void *pixels
,
194 const struct gl_pixelstore_attrib
*unpack
)
198 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__
,
199 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
200 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
202 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
203 0, 0, 0, /*x,y,z offsets*/
207 format
, type
, pixels
, unpack
,
208 true /*for_glTexImage*/);
212 /* Attempt to use the blitter for PBO image uploads.
215 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
, pixels
)) {
219 DBG("%s: upload image %dx%dx%d pixels %p\n",
220 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
223 _mesa_store_teximage(ctx
, dims
, texImage
,
224 format
, type
, pixels
, unpack
);
229 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
231 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
234 intel_set_texture_image_region(struct gl_context
*ctx
,
235 struct gl_texture_image
*image
,
236 struct intel_region
*region
,
238 GLenum internalFormat
,
246 struct intel_context
*intel
= intel_context(ctx
);
247 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
248 struct gl_texture_object
*texobj
= image
->TexObject
;
249 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
250 bool has_surface_tile_offset
= false;
251 uint32_t draw_x
, draw_y
;
253 _mesa_init_teximage_fields(&intel
->ctx
, image
,
255 0, internalFormat
, format
);
257 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
259 intel_image
->mt
= intel_miptree_create_layout(intel
, target
, image
->TexFormat
,
262 true, 0 /* num_samples */);
263 if (intel_image
->mt
== NULL
)
265 intel_region_reference(&intel_image
->mt
->region
, region
);
266 intel_image
->mt
->total_width
= width
;
267 intel_image
->mt
->total_height
= height
;
268 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
269 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
271 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
273 has_surface_tile_offset
= brw_context(ctx
)->has_surface_tile_offset
;
276 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
277 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
278 * trouble resolving back to destination image due to alignment issues.
280 if (!has_surface_tile_offset
&&
281 (draw_x
!= 0 || draw_y
!= 0)) {
282 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
283 intel_miptree_release(&intel_image
->mt
);
287 intel_texobj
->needs_validate
= true;
289 intel_image
->mt
->offset
= offset
;
290 assert(region
->pitch
% region
->cpp
== 0);
291 intel_image
->base
.RowStride
= region
->pitch
/ region
->cpp
;
293 /* Immediately validate the image to the object. */
294 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
298 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
299 GLint texture_format
,
300 __DRIdrawable
*dPriv
)
302 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
303 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
304 struct gl_context
*ctx
= &intel
->ctx
;
305 struct intel_texture_object
*intelObj
;
306 struct intel_renderbuffer
*rb
;
307 struct gl_texture_object
*texObj
;
308 struct gl_texture_image
*texImage
;
309 int level
= 0, internalFormat
= 0;
310 gl_format texFormat
= MESA_FORMAT_NONE
;
312 texObj
= _mesa_get_current_tex_object(ctx
, target
);
313 intelObj
= intel_texture_object(texObj
);
318 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
319 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
320 intel_update_renderbuffers(pDRICtx
, dPriv
);
322 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
323 /* If the region isn't set, then intel_update_renderbuffers was unable
324 * to get the buffers for the drawable.
329 if (rb
->mt
->cpp
== 4) {
330 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
331 internalFormat
= GL_RGB
;
332 texFormat
= MESA_FORMAT_XRGB8888
;
335 internalFormat
= GL_RGBA
;
336 texFormat
= MESA_FORMAT_ARGB8888
;
338 } else if (rb
->mt
->cpp
== 2) {
339 internalFormat
= GL_RGB
;
340 texFormat
= MESA_FORMAT_RGB565
;
343 _mesa_lock_texture(&intel
->ctx
, texObj
);
344 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
345 intel_miptree_make_shareable(intel
, rb
->mt
);
346 intel_set_texture_image_region(ctx
, texImage
, rb
->mt
->region
, target
,
347 internalFormat
, texFormat
, 0,
348 rb
->mt
->region
->width
,
349 rb
->mt
->region
->height
,
351 _mesa_unlock_texture(&intel
->ctx
, texObj
);
355 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
357 /* The old interface didn't have the format argument, so copy our
358 * implementation's behavior at the time.
360 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
364 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
365 struct gl_texture_object
*texObj
,
366 struct gl_texture_image
*texImage
,
367 GLeglImageOES image_handle
)
369 struct intel_context
*intel
= intel_context(ctx
);
373 screen
= intel
->intelScreen
->driScrnPriv
;
374 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
375 screen
->loaderPrivate
);
379 /* Disallow depth/stencil textures: we don't have a way to pass the
380 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
382 if (image
->has_depthstencil
) {
383 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
387 intel_set_texture_image_region(ctx
, texImage
, image
->region
,
388 target
, image
->internal_format
,
389 image
->format
, image
->offset
,
390 image
->width
, image
->height
,
391 image
->tile_x
, image
->tile_y
);
395 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
397 functions
->TexImage
= intelTexImage
;
398 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;