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"
26 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
28 /* Work back from the specified level of the image to the baselevel and create a
29 * miptree of that size.
31 struct intel_mipmap_tree
*
32 intel_miptree_create_for_teximage(struct intel_context
*intel
,
33 struct intel_texture_object
*intelObj
,
34 struct intel_texture_image
*intelImage
,
35 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", __func__
);
47 if (intelImage
->base
.Base
.Level
> intelObj
->base
.BaseLevel
&&
49 (intelObj
->base
.Target
!= GL_TEXTURE_1D
&& height
== 1) ||
50 (intelObj
->base
.Target
== GL_TEXTURE_3D
&& depth
== 1))) {
51 /* For this combination, we're at some lower mipmap level and
52 * some important dimension is 1. We can't extrapolate up to a
53 * likely base level width/height/depth for a full mipmap stack
54 * from this info, so just allocate this one level.
56 firstLevel
= intelImage
->base
.Base
.Level
;
57 lastLevel
= intelImage
->base
.Base
.Level
;
59 /* If this image disrespects BaseLevel, allocate from level zero.
60 * Usually BaseLevel == 0, so it's unlikely to happen.
62 if (intelImage
->base
.Base
.Level
< intelObj
->base
.BaseLevel
)
65 firstLevel
= intelObj
->base
.BaseLevel
;
67 /* Figure out image dimensions at start level. */
68 for (i
= intelImage
->base
.Base
.Level
; i
> firstLevel
; i
--) {
76 /* Guess a reasonable value for lastLevel. This is probably going
77 * to be wrong fairly often and might mean that we have to look at
78 * resizable buffers, or require that buffers implement lazy
79 * pagetable arrangements.
81 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
82 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
83 intelImage
->base
.Base
.Level
== firstLevel
) {
84 lastLevel
= firstLevel
;
86 lastLevel
= (firstLevel
+
87 _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
88 width
, height
, depth
) - 1);
92 return intel_miptree_create(intel
,
93 intelObj
->base
.Target
,
94 intelImage
->base
.Base
.TexFormat
,
100 expect_accelerated_upload
,
101 INTEL_MIPTREE_TILING_ANY
);
104 /* XXX: Do this for TexSubImage also:
107 try_pbo_upload(struct gl_context
*ctx
,
108 struct gl_texture_image
*image
,
109 const struct gl_pixelstore_attrib
*unpack
,
110 GLenum format
, GLenum type
, const void *pixels
)
112 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
113 struct intel_context
*intel
= intel_context(ctx
);
114 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
116 drm_intel_bo
*src_buffer
;
118 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
121 DBG("trying pbo upload\n");
123 if (intel
->ctx
._ImageTransferState
||
124 unpack
->SkipPixels
|| unpack
->SkipRows
) {
125 DBG("%s: image transfer\n", __func__
);
129 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
);
131 if (!intelImage
->mt
) {
132 DBG("%s: no miptree\n", __func__
);
136 if (!_mesa_format_matches_format_and_type(intelImage
->mt
->format
,
137 format
, type
, false, NULL
)) {
138 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
139 __func__
, _mesa_get_format_name(intelImage
->mt
->format
),
144 src_buffer
= intel_bufferobj_source(intel
, pbo
, 64, &src_offset
);
145 /* note: potential 64-bit ptr to 32-bit int cast */
146 src_offset
+= (GLuint
) (unsigned long) pixels
;
149 _mesa_image_row_stride(unpack
, image
->Width
, format
, type
);
151 struct intel_mipmap_tree
*pbo_mt
=
152 intel_miptree_create_for_bo(intel
,
154 intelImage
->mt
->format
,
156 image
->Width
, image
->Height
,
157 src_stride
, I915_TILING_NONE
);
161 if (!intel_miptree_blit(intel
,
164 intelImage
->mt
, image
->Level
, image
->Face
,
166 image
->Width
, image
->Height
, COLOR_LOGICOP_COPY
)) {
167 DBG("%s: blit failed\n", __func__
);
168 intel_miptree_release(&pbo_mt
);
172 intel_miptree_release(&pbo_mt
);
174 DBG("%s: success\n", __func__
);
179 intelTexImage(struct gl_context
* ctx
,
181 struct gl_texture_image
*texImage
,
182 GLenum format
, GLenum type
, const void *pixels
,
183 const struct gl_pixelstore_attrib
*unpack
)
185 DBG("%s target %s level %d %dx%dx%d\n", __func__
,
186 _mesa_enum_to_string(texImage
->TexObject
->Target
),
187 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
189 /* Attempt to use the blitter for PBO image uploads.
192 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
, pixels
)) {
196 DBG("%s: upload image %dx%dx%d pixels %p\n",
197 __func__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
200 _mesa_store_teximage(ctx
, dims
, texImage
,
201 format
, type
, pixels
, unpack
);
206 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
208 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
211 intel_set_texture_image_region(struct gl_context
*ctx
,
212 struct gl_texture_image
*image
,
213 struct intel_region
*region
,
215 GLenum internalFormat
,
223 struct intel_context
*intel
= intel_context(ctx
);
224 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
225 struct gl_texture_object
*texobj
= image
->TexObject
;
226 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
227 bool has_surface_tile_offset
= false;
228 uint32_t draw_x
, draw_y
;
230 _mesa_init_teximage_fields(&intel
->ctx
, image
,
232 0, internalFormat
, format
);
234 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
236 intel_image
->mt
= intel_miptree_create_layout(intel
, target
, image
->TexFormat
,
239 if (intel_image
->mt
== NULL
)
241 intel_region_reference(&intel_image
->mt
->region
, region
);
242 intel_image
->mt
->total_width
= width
;
243 intel_image
->mt
->total_height
= height
;
244 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
245 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
247 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
249 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
250 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
251 * trouble resolving back to destination image due to alignment issues.
253 if (!has_surface_tile_offset
&&
254 (draw_x
!= 0 || draw_y
!= 0)) {
255 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
256 intel_miptree_release(&intel_image
->mt
);
260 intel_texobj
->needs_validate
= true;
262 intel_image
->mt
->offset
= offset
;
263 assert(region
->pitch
% region
->cpp
== 0);
264 intel_image
->base
.RowStride
= region
->pitch
/ region
->cpp
;
266 /* Immediately validate the image to the object. */
267 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
271 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
272 GLint texture_format
,
273 __DRIdrawable
*dPriv
)
275 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
276 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
277 struct gl_context
*ctx
= &intel
->ctx
;
278 struct intel_texture_object
*intelObj
;
279 struct intel_renderbuffer
*rb
;
280 struct gl_texture_object
*texObj
;
281 struct gl_texture_image
*texImage
;
282 int level
= 0, internalFormat
= 0;
283 mesa_format texFormat
= MESA_FORMAT_NONE
;
285 texObj
= _mesa_get_current_tex_object(ctx
, target
);
286 intelObj
= intel_texture_object(texObj
);
291 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
292 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
293 intel_update_renderbuffers(pDRICtx
, dPriv
);
295 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
296 /* If the region isn't set, then intel_update_renderbuffers was unable
297 * to get the buffers for the drawable.
302 if (rb
->mt
->cpp
== 4) {
303 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
304 internalFormat
= GL_RGB
;
305 texFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
308 internalFormat
= GL_RGBA
;
309 texFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
311 } else if (rb
->mt
->cpp
== 2) {
312 internalFormat
= GL_RGB
;
313 texFormat
= MESA_FORMAT_B5G6R5_UNORM
;
316 _mesa_lock_texture(&intel
->ctx
, texObj
);
317 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
318 intel_set_texture_image_region(ctx
, texImage
, rb
->mt
->region
, target
,
319 internalFormat
, texFormat
, 0,
320 rb
->mt
->region
->width
,
321 rb
->mt
->region
->height
,
323 _mesa_unlock_texture(&intel
->ctx
, texObj
);
327 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
329 /* The old interface didn't have the format argument, so copy our
330 * implementation's behavior at the time.
332 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
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
,
354 image
->width
, image
->height
,
355 image
->tile_x
, image
->tile_y
);
359 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
361 functions
->TexImage
= intelTexImage
;
362 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;