d227ee5a8c60d99aeb50e398a5a274bd01499ffe
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_image.c
1
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"
10 #include "main/pbo.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"
17
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"
26
27 #ifndef I915
28 #include "brw_context.h"
29 #endif
30
31 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
32
33 /* Work back from the specified level of the image to the baselevel and create a
34 * miptree of that size.
35 */
36 struct intel_mipmap_tree *
37 intel_miptree_create_for_teximage(struct intel_context *intel,
38 struct intel_texture_object *intelObj,
39 struct intel_texture_image *intelImage,
40 bool expect_accelerated_upload)
41 {
42 GLuint firstLevel;
43 GLuint lastLevel;
44 int width, height, depth;
45 GLuint i;
46
47 intel_miptree_get_dimensions_for_image(&intelImage->base.Base,
48 &width, &height, &depth);
49
50 DBG("%s\n", __FUNCTION__);
51
52 if (intelImage->base.Base.Level > intelObj->base.BaseLevel &&
53 (width == 1 ||
54 (intelObj->base.Target != GL_TEXTURE_1D && height == 1) ||
55 (intelObj->base.Target == GL_TEXTURE_3D && depth == 1))) {
56 /* For this combination, we're at some lower mipmap level and
57 * some important dimension is 1. We can't extrapolate up to a
58 * likely base level width/height/depth for a full mipmap stack
59 * from this info, so just allocate this one level.
60 */
61 firstLevel = intelImage->base.Base.Level;
62 lastLevel = intelImage->base.Base.Level;
63 } else {
64 /* If this image disrespects BaseLevel, allocate from level zero.
65 * Usually BaseLevel == 0, so it's unlikely to happen.
66 */
67 if (intelImage->base.Base.Level < intelObj->base.BaseLevel)
68 firstLevel = 0;
69 else
70 firstLevel = intelObj->base.BaseLevel;
71
72 /* Figure out image dimensions at start level. */
73 for (i = intelImage->base.Base.Level; i > firstLevel; i--) {
74 width <<= 1;
75 if (height != 1)
76 height <<= 1;
77 if (depth != 1)
78 depth <<= 1;
79 }
80
81 /* Guess a reasonable value for lastLevel. This is probably going
82 * to be wrong fairly often and might mean that we have to look at
83 * resizable buffers, or require that buffers implement lazy
84 * pagetable arrangements.
85 */
86 if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
87 intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
88 intelImage->base.Base.Level == firstLevel &&
89 (intel->gen < 4 || firstLevel == 0)) {
90 lastLevel = firstLevel;
91 } else {
92 lastLevel = (firstLevel +
93 _mesa_get_tex_max_num_levels(intelObj->base.Target,
94 width, height, depth) - 1);
95 }
96 }
97
98 return intel_miptree_create(intel,
99 intelObj->base.Target,
100 intelImage->base.Base.TexFormat,
101 firstLevel,
102 lastLevel,
103 width,
104 height,
105 depth,
106 expect_accelerated_upload,
107 0 /* num_samples */,
108 false /* force_y_tiling */);
109 }
110
111 /* There are actually quite a few combinations this will work for,
112 * more than what I've listed here.
113 */
114 static bool
115 check_pbo_format(GLenum format, GLenum type,
116 gl_format mesa_format)
117 {
118 switch (mesa_format) {
119 case MESA_FORMAT_ARGB8888:
120 return (format == GL_BGRA && (type == GL_UNSIGNED_BYTE ||
121 type == GL_UNSIGNED_INT_8_8_8_8_REV));
122 case MESA_FORMAT_RGB565:
123 return (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5);
124 case MESA_FORMAT_L8:
125 return (format == GL_LUMINANCE && type == GL_UNSIGNED_BYTE);
126 case MESA_FORMAT_YCBCR:
127 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
128 default:
129 return false;
130 }
131 }
132
133
134 /* XXX: Do this for TexSubImage also:
135 */
136 static bool
137 try_pbo_upload(struct gl_context *ctx,
138 struct gl_texture_image *image,
139 const struct gl_pixelstore_attrib *unpack,
140 GLenum format, GLenum type, const void *pixels)
141 {
142 struct intel_texture_image *intelImage = intel_texture_image(image);
143 struct intel_context *intel = intel_context(ctx);
144 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
145 GLuint src_offset, src_stride;
146 GLuint dst_x, dst_y;
147 drm_intel_bo *dst_buffer, *src_buffer;
148
149 if (!_mesa_is_bufferobj(unpack->BufferObj))
150 return false;
151
152 DBG("trying pbo upload\n");
153
154 if (intel->ctx._ImageTransferState ||
155 unpack->SkipPixels || unpack->SkipRows) {
156 DBG("%s: image transfer\n", __FUNCTION__);
157 return false;
158 }
159
160 if (!check_pbo_format(format, type, intelImage->base.Base.TexFormat)) {
161 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
162 __FUNCTION__, _mesa_get_format_name(intelImage->base.Base.TexFormat),
163 format, type);
164 return false;
165 }
166
167 ctx->Driver.AllocTextureImageBuffer(ctx, image);
168
169 if (!intelImage->mt) {
170 DBG("%s: no miptree\n", __FUNCTION__);
171 return false;
172 }
173
174 if (image->TexObject->Target == GL_TEXTURE_1D_ARRAY ||
175 image->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
176 DBG("%s: no support for array textures\n", __FUNCTION__);
177 return false;
178 }
179
180 dst_buffer = intelImage->mt->region->bo;
181 src_buffer = intel_bufferobj_source(intel, pbo, 64, &src_offset);
182 /* note: potential 64-bit ptr to 32-bit int cast */
183 src_offset += (GLuint) (unsigned long) pixels;
184
185 if (unpack->RowLength > 0)
186 src_stride = unpack->RowLength;
187 else
188 src_stride = image->Width;
189 src_stride *= intelImage->mt->region->cpp;
190
191 intel_miptree_get_image_offset(intelImage->mt, intelImage->base.Base.Level,
192 intelImage->base.Base.Face,
193 &dst_x, &dst_y);
194
195 if (!intelEmitCopyBlit(intel,
196 intelImage->mt->cpp,
197 src_stride, src_buffer,
198 src_offset, false,
199 intelImage->mt->region->pitch, dst_buffer, 0,
200 intelImage->mt->region->tiling,
201 0, 0, dst_x, dst_y, image->Width, image->Height,
202 GL_COPY)) {
203 DBG("%s: blit failed\n", __FUNCTION__);
204 return false;
205 }
206
207 DBG("%s: success\n", __FUNCTION__);
208 return true;
209 }
210
211 static void
212 intelTexImage(struct gl_context * ctx,
213 GLuint dims,
214 struct gl_texture_image *texImage,
215 GLenum format, GLenum type, const void *pixels,
216 const struct gl_pixelstore_attrib *unpack)
217 {
218 bool ok;
219
220 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__,
221 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
222 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
223
224 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
225 0, 0, 0, /*x,y,z offsets*/
226 texImage->Width,
227 texImage->Height,
228 texImage->Depth,
229 format, type, pixels, unpack,
230 true /*for_glTexImage*/);
231 if (ok)
232 return;
233
234 /* Attempt to use the blitter for PBO image uploads.
235 */
236 if (dims <= 2 &&
237 try_pbo_upload(ctx, texImage, unpack, format, type, pixels)) {
238 return;
239 }
240
241 DBG("%s: upload image %dx%dx%d pixels %p\n",
242 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
243 pixels);
244
245 _mesa_store_teximage(ctx, dims, texImage,
246 format, type, pixels, unpack);
247 }
248
249
250 /**
251 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
252 *
253 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
254 */
255 static void
256 intel_set_texture_image_region(struct gl_context *ctx,
257 struct gl_texture_image *image,
258 struct intel_region *region,
259 GLenum target,
260 GLenum internalFormat,
261 gl_format format,
262 uint32_t offset,
263 GLuint width,
264 GLuint height,
265 GLuint tile_x,
266 GLuint tile_y)
267 {
268 struct intel_context *intel = intel_context(ctx);
269 struct intel_texture_image *intel_image = intel_texture_image(image);
270 struct gl_texture_object *texobj = image->TexObject;
271 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
272 bool has_surface_tile_offset = false;
273 uint32_t draw_x, draw_y;
274
275 _mesa_init_teximage_fields(&intel->ctx, image,
276 width, height, 1,
277 0, internalFormat, format);
278
279 ctx->Driver.FreeTextureImageBuffer(ctx, image);
280
281 intel_image->mt = intel_miptree_create_layout(intel, target, image->TexFormat,
282 0, 0,
283 width, height, 1,
284 true, 0 /* num_samples */);
285 if (intel_image->mt == NULL)
286 return;
287 intel_region_reference(&intel_image->mt->region, region);
288 intel_image->mt->total_width = width;
289 intel_image->mt->total_height = height;
290 intel_image->mt->level[0].slice[0].x_offset = tile_x;
291 intel_image->mt->level[0].slice[0].y_offset = tile_y;
292
293 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
294 #ifndef I915
295 has_surface_tile_offset = brw_context(ctx)->has_surface_tile_offset;
296 #endif
297
298 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
299 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
300 * trouble resolving back to destination image due to alignment issues.
301 */
302 if (!has_surface_tile_offset &&
303 (draw_x != 0 || draw_y != 0)) {
304 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
305 intel_miptree_release(&intel_image->mt);
306 return;
307 }
308
309 intel_texobj->needs_validate = true;
310
311 intel_image->mt->offset = offset;
312 assert(region->pitch % region->cpp == 0);
313 intel_image->base.RowStride = region->pitch / region->cpp;
314
315 /* Immediately validate the image to the object. */
316 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
317 }
318
319 void
320 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
321 GLint texture_format,
322 __DRIdrawable *dPriv)
323 {
324 struct gl_framebuffer *fb = dPriv->driverPrivate;
325 struct intel_context *intel = pDRICtx->driverPrivate;
326 struct gl_context *ctx = &intel->ctx;
327 struct intel_texture_object *intelObj;
328 struct intel_renderbuffer *rb;
329 struct gl_texture_object *texObj;
330 struct gl_texture_image *texImage;
331 int level = 0, internalFormat = 0;
332 gl_format texFormat = MESA_FORMAT_NONE;
333
334 texObj = _mesa_get_current_tex_object(ctx, target);
335 intelObj = intel_texture_object(texObj);
336
337 if (!intelObj)
338 return;
339
340 if (dPriv->lastStamp != dPriv->dri2.stamp ||
341 !pDRICtx->driScreenPriv->dri2.useInvalidate)
342 intel_update_renderbuffers(pDRICtx, dPriv);
343
344 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
345 /* If the region isn't set, then intel_update_renderbuffers was unable
346 * to get the buffers for the drawable.
347 */
348 if (!rb || !rb->mt)
349 return;
350
351 if (rb->mt->cpp == 4) {
352 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
353 internalFormat = GL_RGB;
354 texFormat = MESA_FORMAT_XRGB8888;
355 }
356 else {
357 internalFormat = GL_RGBA;
358 texFormat = MESA_FORMAT_ARGB8888;
359 }
360 } else if (rb->mt->cpp == 2) {
361 internalFormat = GL_RGB;
362 texFormat = MESA_FORMAT_RGB565;
363 }
364
365 _mesa_lock_texture(&intel->ctx, texObj);
366 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
367 intel_set_texture_image_region(ctx, texImage, rb->mt->region, target,
368 internalFormat, texFormat, 0,
369 rb->mt->region->width,
370 rb->mt->region->height,
371 0, 0);
372 _mesa_unlock_texture(&intel->ctx, texObj);
373 }
374
375 void
376 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
377 {
378 /* The old interface didn't have the format argument, so copy our
379 * implementation's behavior at the time.
380 */
381 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
382 }
383
384 static void
385 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
386 struct gl_texture_object *texObj,
387 struct gl_texture_image *texImage,
388 GLeglImageOES image_handle)
389 {
390 struct intel_context *intel = intel_context(ctx);
391 __DRIscreen *screen;
392 __DRIimage *image;
393
394 screen = intel->intelScreen->driScrnPriv;
395 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
396 screen->loaderPrivate);
397 if (image == NULL)
398 return;
399
400 /* Disallow depth/stencil textures: we don't have a way to pass the
401 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
402 */
403 if (image->has_depthstencil) {
404 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
405 return;
406 }
407
408 intel_set_texture_image_region(ctx, texImage, image->region,
409 target, image->internal_format,
410 image->format, image->offset,
411 image->width, image->height,
412 image->tile_x, image->tile_y);
413 }
414
415 void
416 intelInitTextureImageFuncs(struct dd_function_table *functions)
417 {
418 functions->TexImage = intelTexImage;
419 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
420 }