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_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"
25 #include "brw_context.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 brw_context
*brw
,
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 this image disrespects BaseLevel, allocate from level zero.
49 * Usually BaseLevel == 0, so it's unlikely to happen.
51 if (intelImage
->base
.Base
.Level
< intelObj
->base
.BaseLevel
)
54 firstLevel
= intelObj
->base
.BaseLevel
;
56 /* Figure out image dimensions at start level. */
57 for (i
= intelImage
->base
.Base
.Level
; i
> firstLevel
; i
--) {
65 /* Guess a reasonable value for lastLevel. This is probably going
66 * to be wrong fairly often and might mean that we have to look at
67 * resizable buffers, or require that buffers implement lazy
68 * pagetable arrangements.
70 if ((intelObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
71 intelObj
->base
.Sampler
.MinFilter
== GL_LINEAR
) &&
72 intelImage
->base
.Base
.Level
== firstLevel
&&
74 lastLevel
= firstLevel
;
76 lastLevel
= (firstLevel
+
77 _mesa_get_tex_max_num_levels(intelObj
->base
.Target
,
78 width
, height
, depth
) - 1);
81 return intel_miptree_create(brw
,
82 intelObj
->base
.Target
,
83 intelImage
->base
.Base
.TexFormat
,
89 expect_accelerated_upload
,
90 intelImage
->base
.Base
.NumSamples
,
91 INTEL_MIPTREE_TILING_ANY
);
94 /* XXX: Do this for TexSubImage also:
97 try_pbo_upload(struct gl_context
*ctx
,
98 struct gl_texture_image
*image
,
99 const struct gl_pixelstore_attrib
*unpack
,
100 GLenum format
, GLenum type
, const void *pixels
)
102 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
103 struct brw_context
*brw
= brw_context(ctx
);
104 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
106 drm_intel_bo
*src_buffer
;
108 if (!_mesa_is_bufferobj(unpack
->BufferObj
))
111 DBG("trying pbo upload\n");
113 if (ctx
->_ImageTransferState
|| unpack
->SkipPixels
|| unpack
->SkipRows
) {
114 DBG("%s: image transfer\n", __FUNCTION__
);
118 ctx
->Driver
.AllocTextureImageBuffer(ctx
, image
);
120 if (!intelImage
->mt
) {
121 DBG("%s: no miptree\n", __FUNCTION__
);
125 if (!_mesa_format_matches_format_and_type(intelImage
->mt
->format
,
126 format
, type
, false)) {
127 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
128 __FUNCTION__
, _mesa_get_format_name(intelImage
->mt
->format
),
133 if (image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
||
134 image
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
135 DBG("%s: no support for array textures\n", __FUNCTION__
);
139 src_buffer
= intel_bufferobj_source(brw
, pbo
, 64, &src_offset
);
140 /* note: potential 64-bit ptr to 32-bit int cast */
141 src_offset
+= (GLuint
) (unsigned long) pixels
;
144 _mesa_image_row_stride(unpack
, image
->Width
, format
, type
);
146 struct intel_mipmap_tree
*pbo_mt
=
147 intel_miptree_create_for_bo(brw
,
149 intelImage
->mt
->format
,
151 image
->Width
, image
->Height
,
152 src_stride
, I915_TILING_NONE
);
156 if (!intel_miptree_blit(brw
,
159 intelImage
->mt
, image
->Level
, image
->Face
,
161 image
->Width
, image
->Height
, GL_COPY
)) {
162 DBG("%s: blit failed\n", __FUNCTION__
);
163 intel_miptree_release(&pbo_mt
);
167 intel_miptree_release(&pbo_mt
);
169 DBG("%s: success\n", __FUNCTION__
);
174 intelTexImage(struct gl_context
* ctx
,
176 struct gl_texture_image
*texImage
,
177 GLenum format
, GLenum type
, const void *pixels
,
178 const struct gl_pixelstore_attrib
*unpack
)
182 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__
,
183 _mesa_lookup_enum_by_nr(texImage
->TexObject
->Target
),
184 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
186 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
187 0, 0, 0, /*x,y,z offsets*/
191 format
, type
, pixels
, unpack
,
192 true /*for_glTexImage*/);
196 /* Attempt to use the blitter for PBO image uploads.
199 try_pbo_upload(ctx
, texImage
, unpack
, format
, type
, pixels
)) {
203 DBG("%s: upload image %dx%dx%d pixels %p\n",
204 __FUNCTION__
, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
207 _mesa_store_teximage(ctx
, dims
, texImage
,
208 format
, type
, pixels
, unpack
);
213 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
215 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
218 intel_set_texture_image_region(struct gl_context
*ctx
,
219 struct gl_texture_image
*image
,
220 struct intel_region
*region
,
222 GLenum internalFormat
,
230 struct brw_context
*brw
= brw_context(ctx
);
231 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
232 struct gl_texture_object
*texobj
= image
->TexObject
;
233 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
234 uint32_t draw_x
, draw_y
;
236 _mesa_init_teximage_fields(&brw
->ctx
, image
,
238 0, internalFormat
, format
);
240 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
242 intel_image
->mt
= intel_miptree_create_layout(brw
, target
, image
->TexFormat
,
245 true, 0 /* num_samples */);
246 if (intel_image
->mt
== NULL
)
248 intel_region_reference(&intel_image
->mt
->region
, region
);
249 intel_image
->mt
->total_width
= width
;
250 intel_image
->mt
->total_height
= height
;
251 intel_image
->mt
->level
[0].slice
[0].x_offset
= tile_x
;
252 intel_image
->mt
->level
[0].slice
[0].y_offset
= tile_y
;
254 intel_miptree_get_tile_offsets(intel_image
->mt
, 0, 0, &draw_x
, &draw_y
);
256 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
257 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
258 * trouble resolving back to destination image due to alignment issues.
260 if (!brw
->has_surface_tile_offset
&&
261 (draw_x
!= 0 || draw_y
!= 0)) {
262 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
263 intel_miptree_release(&intel_image
->mt
);
267 intel_texobj
->needs_validate
= true;
269 intel_image
->mt
->offset
= offset
;
270 assert(region
->pitch
% region
->cpp
== 0);
271 intel_image
->base
.RowStride
= region
->pitch
/ region
->cpp
;
273 /* Immediately validate the image to the object. */
274 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
278 intelSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
279 GLint texture_format
,
280 __DRIdrawable
*dPriv
)
282 struct gl_framebuffer
*fb
= dPriv
->driverPrivate
;
283 struct brw_context
*brw
= pDRICtx
->driverPrivate
;
284 struct gl_context
*ctx
= &brw
->ctx
;
285 struct intel_texture_object
*intelObj
;
286 struct intel_renderbuffer
*rb
;
287 struct gl_texture_object
*texObj
;
288 struct gl_texture_image
*texImage
;
289 int level
= 0, internalFormat
= 0;
290 gl_format texFormat
= MESA_FORMAT_NONE
;
292 texObj
= _mesa_get_current_tex_object(ctx
, target
);
293 intelObj
= intel_texture_object(texObj
);
298 if (dPriv
->lastStamp
!= dPriv
->dri2
.stamp
||
299 !pDRICtx
->driScreenPriv
->dri2
.useInvalidate
)
300 intel_update_renderbuffers(pDRICtx
, dPriv
);
302 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
303 /* If the region isn't set, then intel_update_renderbuffers was unable
304 * to get the buffers for the drawable.
309 if (rb
->mt
->cpp
== 4) {
310 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
311 internalFormat
= GL_RGB
;
312 texFormat
= MESA_FORMAT_XRGB8888
;
315 internalFormat
= GL_RGBA
;
316 texFormat
= MESA_FORMAT_ARGB8888
;
318 } else if (rb
->mt
->cpp
== 2) {
319 internalFormat
= GL_RGB
;
320 texFormat
= MESA_FORMAT_RGB565
;
323 _mesa_lock_texture(&brw
->ctx
, texObj
);
324 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
325 intel_miptree_make_shareable(brw
, rb
->mt
);
326 intel_set_texture_image_region(ctx
, texImage
, rb
->mt
->region
, target
,
327 internalFormat
, texFormat
, 0,
328 rb
->mt
->region
->width
,
329 rb
->mt
->region
->height
,
331 _mesa_unlock_texture(&brw
->ctx
, texObj
);
335 intelSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
337 /* The old interface didn't have the format argument, so copy our
338 * implementation's behavior at the time.
340 intelSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
344 intel_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
345 struct gl_texture_object
*texObj
,
346 struct gl_texture_image
*texImage
,
347 GLeglImageOES image_handle
)
349 struct brw_context
*brw
= brw_context(ctx
);
353 screen
= brw
->intelScreen
->driScrnPriv
;
354 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
355 screen
->loaderPrivate
);
360 * Images originating via EGL_EXT_image_dma_buf_import can be used only
361 * with GL_OES_EGL_image_external only.
363 if (image
->dma_buf_imported
&& target
!= GL_TEXTURE_EXTERNAL_OES
) {
364 _mesa_error(ctx
, GL_INVALID_OPERATION
,
365 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
366 "GL_OES_EGL_image_external only");
370 if (target
== GL_TEXTURE_EXTERNAL_OES
&& !image
->dma_buf_imported
) {
371 _mesa_error(ctx
, GL_INVALID_OPERATION
,
372 "glEGLImageTargetTexture2DOES(external target is enabled only "
373 "for images created with EGL_EXT_image_dma_buf_import");
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
;