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"
10 #include "main/renderbuffer.h"
11 #include "main/texcompress.h"
12 #include "main/texgetimage.h"
13 #include "main/texobj.h"
14 #include "main/teximage.h"
15 #include "main/texstore.h"
17 #include "intel_context.h"
18 #include "intel_mipmap_tree.h"
19 #include "intel_buffer_objects.h"
20 #include "intel_batchbuffer.h"
21 #include "intel_tex.h"
22 #include "intel_blit.h"
23 #include "intel_fbo.h"
24 #include "intel_span.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
&&
88 (intel
->gen
< 4 || firstLevel
== 0)) {
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 false /* force_y_tiling */);
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
);
121 GLuint src_offset
, src_stride
;
123 drm_intel_bo
*dst_buffer
, *src_buffer
;
125 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
128 DBG("trying pbo upload\n");
130 if (intel
->ctx
._ImageTransferState
||
131 unpack
->SkipPixels
|| unpack
->SkipRows
) {
132 DBG("%s: image transfer\n", __FUNCTION__
);
136 if (!_mesa_format_matches_format_and_type(image
->TexFormat
,
137 format
, type
, false)) {
138 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
139 __FUNCTION__
, _mesa_get_format_name(image
->TexFormat
),
144 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
);
146 if (!intelImage
->mt
) {
147 DBG("%s: no miptree\n", __FUNCTION__
);
151 if (image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
||
152 image
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
153 DBG("%s: no support for array textures\n", __FUNCTION__
);
157 dst_buffer
= intelImage
->mt
->region
->bo
;
158 src_buffer
= intel_bufferobj_source(intel
, pbo
, 64, &src_offset
);
159 /* note: potential 64-bit ptr to 32-bit int cast */
160 src_offset
+= (GLuint
) (unsigned long) pixels
;
162 if (unpack
->RowLength
> 0)
163 src_stride
= unpack
->RowLength
;
165 src_stride
= image
->Width
;
166 src_stride
*= intelImage
->mt
->region
->cpp
;
168 intel_miptree_get_image_offset(intelImage
->mt
, intelImage
->base
.Base
.Level
,
169 intelImage
->base
.Base
.Face
,
172 if (!intelEmitCopyBlit(intel
,
174 src_stride
, src_buffer
,
176 intelImage
->mt
->region
->pitch
, dst_buffer
, 0,
177 intelImage
->mt
->region
->tiling
,
178 0, 0, dst_x
, dst_y
, image
->Width
, image
->Height
,
180 DBG("%s: blit failed\n", __FUNCTION__
);
184 DBG("%s: success\n", __FUNCTION__
);
189 intelTexImage(struct gl_context
* ctx
,
191 struct gl_texture_image
*texImage
,
192 GLenum format
, GLenum type
, const void *pixels
,
193 const struct gl_pixelstore_attrib
*unpack
)
197 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__
,
198 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
199 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
201 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
202 0, 0, 0, /*x,y,z offsets*/
206 format
, type
, pixels
, unpack
,
207 true /*for_glTexImage*/);
211 /* Attempt to use the blitter for PBO image uploads.
214 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
, pixels
)) {
218 DBG("%s: upload image %dx%dx%d pixels %p\n",
219 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
222 _mesa_store_teximage(ctx
, dims
, texImage
,
223 format
, type
, pixels
, unpack
);
228 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
230 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
233 intel_set_texture_image_region(struct gl_context
*ctx
,
234 struct gl_texture_image
*image
,
235 struct intel_region
*region
,
237 GLenum internalFormat
,
245 struct intel_context
*intel
= intel_context(ctx
);
246 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
247 struct gl_texture_object
*texobj
= image
->TexObject
;
248 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
249 bool has_surface_tile_offset
= false;
250 uint32_t draw_x
, draw_y
;
252 _mesa_init_teximage_fields(&intel
->ctx
, image
,
254 0, internalFormat
, format
);
256 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
258 intel_image
->mt
= intel_miptree_create_layout(intel
, target
, image
->TexFormat
,
261 true, 0 /* num_samples */);
262 if (intel_image
->mt
== NULL
)
264 intel_region_reference(&intel_image
->mt
->region
, region
);
265 intel_image
->mt
->total_width
= width
;
266 intel_image
->mt
->total_height
= height
;
267 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
268 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
270 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
272 has_surface_tile_offset
= brw_context(ctx
)->has_surface_tile_offset
;
275 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
276 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
277 * trouble resolving back to destination image due to alignment issues.
279 if (!has_surface_tile_offset
&&
280 (draw_x
!= 0 || draw_y
!= 0)) {
281 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
282 intel_miptree_release(&intel_image
->mt
);
286 intel_texobj
->needs_validate
= true;
288 intel_image
->mt
->offset
= offset
;
289 assert(region
->pitch
% region
->cpp
== 0);
290 intel_image
->base
.RowStride
= region
->pitch
/ region
->cpp
;
292 /* Immediately validate the image to the object. */
293 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
297 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
298 GLint texture_format
,
299 __DRIdrawable
*dPriv
)
301 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
302 struct intel_context
*intel
= pDRICtx
->driverPrivate
;
303 struct gl_context
*ctx
= &intel
->ctx
;
304 struct intel_texture_object
*intelObj
;
305 struct intel_renderbuffer
*rb
;
306 struct gl_texture_object
*texObj
;
307 struct gl_texture_image
*texImage
;
308 int level
= 0, internalFormat
= 0;
309 gl_format texFormat
= MESA_FORMAT_NONE
;
311 texObj
= _mesa_get_current_tex_object(ctx
, target
);
312 intelObj
= intel_texture_object(texObj
);
317 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
318 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
319 intel_update_renderbuffers(pDRICtx
, dPriv
);
321 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
322 /* If the region isn't set, then intel_update_renderbuffers was unable
323 * to get the buffers for the drawable.
328 if (rb
->mt
->cpp
== 4) {
329 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
330 internalFormat
= GL_RGB
;
331 texFormat
= MESA_FORMAT_XRGB8888
;
334 internalFormat
= GL_RGBA
;
335 texFormat
= MESA_FORMAT_ARGB8888
;
337 } else if (rb
->mt
->cpp
== 2) {
338 internalFormat
= GL_RGB
;
339 texFormat
= MESA_FORMAT_RGB565
;
342 _mesa_lock_texture(&intel
->ctx
, texObj
);
343 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
344 intel_set_texture_image_region(ctx
, texImage
, rb
->mt
->region
, target
,
345 internalFormat
, texFormat
, 0,
346 rb
->mt
->region
->width
,
347 rb
->mt
->region
->height
,
349 _mesa_unlock_texture(&intel
->ctx
, texObj
);
353 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
355 /* The old interface didn't have the format argument, so copy our
356 * implementation's behavior at the time.
358 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
362 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
363 struct gl_texture_object
*texObj
,
364 struct gl_texture_image
*texImage
,
365 GLeglImageOES image_handle
)
367 struct intel_context
*intel
= intel_context(ctx
);
371 screen
= intel
->intelScreen
->driScrnPriv
;
372 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
373 screen
->loaderPrivate
);
377 /* Disallow depth/stencil textures: we don't have a way to pass the
378 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
380 if (image
->has_depthstencil
) {
381 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
385 intel_set_texture_image_region(ctx
, texImage
, image
->region
,
386 target
, image
->internal_format
,
387 image
->format
, image
->offset
,
388 image
->width
, image
->height
,
389 image
->tile_x
, image
->tile_y
);
393 intelInitTextureImageFuncs(struct dd_function_table
*functions
)
395 functions
->TexImage
= intelTexImage
;
396 functions
->EGLImageTargetTexture2D
= intel_image_target_texture_2d
;