i965: Improve debug output in intelTexImage and intelTexSubimage
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_image.c
1
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"
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 "drivers/common/meta.h"
19
20 #include "intel_mipmap_tree.h"
21 #include "intel_buffer_objects.h"
22 #include "intel_batchbuffer.h"
23 #include "intel_tex.h"
24 #include "intel_blit.h"
25 #include "intel_fbo.h"
26 #include "intel_image.h"
27
28 #include "brw_context.h"
29
30 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
31
32 /* Work back from the specified level of the image to the baselevel and create a
33 * miptree of that size.
34 */
35 struct intel_mipmap_tree *
36 intel_miptree_create_for_teximage(struct brw_context *brw,
37 struct intel_texture_object *intelObj,
38 struct intel_texture_image *intelImage,
39 bool expect_accelerated_upload)
40 {
41 GLuint lastLevel;
42 int width, height, depth;
43 GLuint i;
44
45 intel_miptree_get_dimensions_for_image(&intelImage->base.Base,
46 &width, &height, &depth);
47
48 DBG("%s\n", __FUNCTION__);
49
50 /* Figure out image dimensions at start level. */
51 for (i = intelImage->base.Base.Level; i > 0; i--) {
52 width <<= 1;
53 if (height != 1)
54 height <<= 1;
55 if (depth != 1)
56 depth <<= 1;
57 }
58
59 /* Guess a reasonable value for lastLevel. This is probably going
60 * to be wrong fairly often and might mean that we have to look at
61 * resizable buffers, or require that buffers implement lazy
62 * pagetable arrangements.
63 */
64 if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
65 intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
66 intelImage->base.Base.Level == 0 &&
67 !intelObj->base.GenerateMipmap) {
68 lastLevel = 0;
69 } else {
70 lastLevel = _mesa_get_tex_max_num_levels(intelObj->base.Target,
71 width, height, depth) - 1;
72 }
73
74 return intel_miptree_create(brw,
75 intelObj->base.Target,
76 intelImage->base.Base.TexFormat,
77 0,
78 lastLevel,
79 width,
80 height,
81 depth,
82 expect_accelerated_upload,
83 intelImage->base.Base.NumSamples,
84 INTEL_MIPTREE_TILING_ANY);
85 }
86
87 /* XXX: Do this for TexSubImage also:
88 */
89 static bool
90 try_pbo_upload(struct gl_context *ctx,
91 struct gl_texture_image *image,
92 const struct gl_pixelstore_attrib *unpack,
93 GLenum format, GLenum type, const void *pixels)
94 {
95 struct intel_texture_image *intelImage = intel_texture_image(image);
96 struct brw_context *brw = brw_context(ctx);
97 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
98 GLuint src_offset;
99 drm_intel_bo *src_buffer;
100
101 if (!_mesa_is_bufferobj(unpack->BufferObj))
102 return false;
103
104 DBG("trying pbo upload\n");
105
106 if (ctx->_ImageTransferState || unpack->SkipPixels || unpack->SkipRows) {
107 DBG("%s: image transfer\n", __FUNCTION__);
108 return false;
109 }
110
111 ctx->Driver.AllocTextureImageBuffer(ctx, image);
112
113 if (!intelImage->mt) {
114 DBG("%s: no miptree\n", __FUNCTION__);
115 return false;
116 }
117
118 if (!_mesa_format_matches_format_and_type(intelImage->mt->format,
119 format, type, false)) {
120 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
121 __FUNCTION__, _mesa_get_format_name(intelImage->mt->format),
122 format, type);
123 return false;
124 }
125
126 if (image->TexObject->Target == GL_TEXTURE_1D_ARRAY ||
127 image->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
128 DBG("%s: no support for array textures\n", __FUNCTION__);
129 return false;
130 }
131
132 int src_stride =
133 _mesa_image_row_stride(unpack, image->Width, format, type);
134
135 /* note: potential 64-bit ptr to 32-bit int cast */
136 src_offset = (GLuint) (unsigned long) pixels;
137 src_buffer = intel_bufferobj_buffer(brw, pbo,
138 src_offset, src_stride * image->Height);
139
140 struct intel_mipmap_tree *pbo_mt =
141 intel_miptree_create_for_bo(brw,
142 src_buffer,
143 intelImage->mt->format,
144 src_offset,
145 image->Width, image->Height,
146 src_stride);
147 if (!pbo_mt)
148 return false;
149
150 if (!intel_miptree_blit(brw,
151 pbo_mt, 0, 0,
152 0, 0, false,
153 intelImage->mt, image->Level, image->Face,
154 0, 0, false,
155 image->Width, image->Height, GL_COPY)) {
156 DBG("%s: blit failed\n", __FUNCTION__);
157 intel_miptree_release(&pbo_mt);
158 return false;
159 }
160
161 intel_miptree_release(&pbo_mt);
162
163 DBG("%s: success\n", __FUNCTION__);
164 return true;
165 }
166
167 static void
168 intelTexImage(struct gl_context * ctx,
169 GLuint dims,
170 struct gl_texture_image *texImage,
171 GLenum format, GLenum type, const void *pixels,
172 const struct gl_pixelstore_attrib *unpack)
173 {
174 bool ok;
175
176 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
177 __FUNCTION__, _mesa_get_format_name(texImage->TexFormat),
178 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
179 _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type),
180 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
181
182 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
183 0, 0, 0, /*x,y,z offsets*/
184 texImage->Width,
185 texImage->Height,
186 texImage->Depth,
187 format, type, pixels, unpack,
188 true /*for_glTexImage*/);
189 if (ok)
190 return;
191
192 /* Attempt to use the blitter for PBO image uploads.
193 */
194 if (dims <= 2 &&
195 try_pbo_upload(ctx, texImage, unpack, format, type, pixels)) {
196 return;
197 }
198
199 DBG("%s: upload image %dx%dx%d pixels %p\n",
200 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
201 pixels);
202
203 _mesa_store_teximage(ctx, dims, texImage,
204 format, type, pixels, unpack);
205 }
206
207
208 /**
209 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
210 *
211 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
212 */
213 static void
214 intel_set_texture_image_bo(struct gl_context *ctx,
215 struct gl_texture_image *image,
216 drm_intel_bo *bo,
217 GLenum target,
218 GLenum internalFormat,
219 mesa_format format,
220 uint32_t offset,
221 GLuint width, GLuint height,
222 GLuint pitch,
223 GLuint tile_x, GLuint tile_y)
224 {
225 struct brw_context *brw = brw_context(ctx);
226 struct intel_texture_image *intel_image = intel_texture_image(image);
227 struct gl_texture_object *texobj = image->TexObject;
228 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
229 uint32_t draw_x, draw_y;
230
231 _mesa_init_teximage_fields(&brw->ctx, image,
232 width, height, 1,
233 0, internalFormat, format);
234
235 ctx->Driver.FreeTextureImageBuffer(ctx, image);
236
237 intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
238 0, width, height, pitch);
239 if (intel_image->mt == NULL)
240 return;
241 intel_image->mt->target = target;
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;
246
247 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
248
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.
252 */
253 if (!brw->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);
257 return;
258 }
259
260 intel_texobj->needs_validate = true;
261
262 intel_image->mt->offset = offset;
263 assert(pitch % intel_image->mt->cpp == 0);
264 intel_image->base.RowStride = pitch / intel_image->mt->cpp;
265
266 /* Immediately validate the image to the object. */
267 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
268 }
269
270 void
271 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
272 GLint texture_format,
273 __DRIdrawable *dPriv)
274 {
275 struct gl_framebuffer *fb = dPriv->driverPrivate;
276 struct brw_context *brw = pDRICtx->driverPrivate;
277 struct gl_context *ctx = &brw->ctx;
278 struct intel_renderbuffer *rb;
279 struct gl_texture_object *texObj;
280 struct gl_texture_image *texImage;
281 int level = 0, internalFormat = 0;
282 mesa_format texFormat = MESA_FORMAT_NONE;
283
284 texObj = _mesa_get_current_tex_object(ctx, target);
285
286 if (!texObj)
287 return;
288
289 if (dPriv->lastStamp != dPriv->dri2.stamp ||
290 !pDRICtx->driScreenPriv->dri2.useInvalidate)
291 intel_update_renderbuffers(pDRICtx, dPriv);
292
293 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
294 /* If the miptree isn't set, then intel_update_renderbuffers was unable
295 * to get the BO for the drawable from the window system.
296 */
297 if (!rb || !rb->mt)
298 return;
299
300 if (rb->mt->cpp == 4) {
301 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
302 internalFormat = GL_RGB;
303 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
304 }
305 else {
306 internalFormat = GL_RGBA;
307 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
308 }
309 } else if (rb->mt->cpp == 2) {
310 internalFormat = GL_RGB;
311 texFormat = MESA_FORMAT_B5G6R5_UNORM;
312 }
313
314 _mesa_lock_texture(&brw->ctx, texObj);
315 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
316 intel_miptree_make_shareable(brw, rb->mt);
317 intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
318 internalFormat, texFormat, 0,
319 rb->Base.Base.Width,
320 rb->Base.Base.Height,
321 rb->mt->pitch,
322 0, 0);
323 _mesa_unlock_texture(&brw->ctx, texObj);
324 }
325
326 static GLboolean
327 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
328 struct gl_renderbuffer *rb,
329 struct gl_texture_image *image)
330 {
331 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
332 struct intel_texture_image *intel_image = intel_texture_image(image);
333 struct gl_texture_object *texobj = image->TexObject;
334 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
335
336 /* We can only handle RB allocated with AllocRenderbufferStorage, or
337 * window-system renderbuffers.
338 */
339 assert(!rb->TexImage);
340
341 if (!irb->mt)
342 return false;
343
344 _mesa_lock_texture(ctx, texobj);
345 _mesa_init_teximage_fields(ctx, image,
346 rb->Width, rb->Height, 1,
347 0, rb->InternalFormat, rb->Format);
348 image->NumSamples = rb->NumSamples;
349
350 intel_miptree_reference(&intel_image->mt, irb->mt);
351
352 /* Immediately validate the image to the object. */
353 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
354
355 intel_texobj->needs_validate = true;
356 _mesa_unlock_texture(ctx, texobj);
357
358 return true;
359 }
360
361 void
362 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
363 {
364 /* The old interface didn't have the format argument, so copy our
365 * implementation's behavior at the time.
366 */
367 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
368 }
369
370 static void
371 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
372 struct gl_texture_object *texObj,
373 struct gl_texture_image *texImage,
374 GLeglImageOES image_handle)
375 {
376 struct brw_context *brw = brw_context(ctx);
377 __DRIscreen *screen;
378 __DRIimage *image;
379
380 screen = brw->intelScreen->driScrnPriv;
381 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
382 screen->loaderPrivate);
383 if (image == NULL)
384 return;
385
386 /**
387 * Images originating via EGL_EXT_image_dma_buf_import can be used only
388 * with GL_OES_EGL_image_external only.
389 */
390 if (image->dma_buf_imported && target != GL_TEXTURE_EXTERNAL_OES) {
391 _mesa_error(ctx, GL_INVALID_OPERATION,
392 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
393 "GL_OES_EGL_image_external only");
394 return;
395 }
396
397 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
398 _mesa_error(ctx, GL_INVALID_OPERATION,
399 "glEGLImageTargetTexture2DOES(external target is enabled only "
400 "for images created with EGL_EXT_image_dma_buf_import");
401 return;
402 }
403
404 /* Disallow depth/stencil textures: we don't have a way to pass the
405 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
406 */
407 if (image->has_depthstencil) {
408 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
409 return;
410 }
411
412 intel_set_texture_image_bo(ctx, texImage, image->bo,
413 target, image->internal_format,
414 image->format, image->offset,
415 image->width, image->height,
416 image->pitch,
417 image->tile_x, image->tile_y);
418 }
419
420 static bool
421 blit_texture_to_pbo(struct gl_context *ctx,
422 GLenum format, GLenum type,
423 GLvoid * pixels, struct gl_texture_image *texImage)
424 {
425 struct intel_texture_image *intelImage = intel_texture_image(texImage);
426 struct brw_context *brw = brw_context(ctx);
427 const struct gl_pixelstore_attrib *pack = &ctx->Pack;
428 struct intel_buffer_object *dst = intel_buffer_object(pack->BufferObj);
429 GLuint dst_offset;
430 drm_intel_bo *dst_buffer;
431 GLenum target = texImage->TexObject->Target;
432
433 /* Check if we can use GPU blit to copy from the hardware texture
434 * format to the user's format/type.
435 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
436 */
437
438 if (!_mesa_format_matches_format_and_type(intelImage->mt->format, format,
439 type, false))
440 {
441 perf_debug("%s: unsupported format, fallback to CPU mapping for PBO\n",
442 __FUNCTION__);
443
444 return false;
445 }
446
447 if (ctx->_ImageTransferState) {
448 perf_debug("%s: bad transfer state, fallback to CPU mapping for PBO\n",
449 __FUNCTION__);
450 return false;
451 }
452
453 if (pack->SwapBytes || pack->LsbFirst) {
454 perf_debug("%s: unsupported pack swap params\n",
455 __FUNCTION__);
456 return false;
457 }
458
459 if (target == GL_TEXTURE_1D_ARRAY ||
460 target == GL_TEXTURE_2D_ARRAY ||
461 target == GL_TEXTURE_CUBE_MAP_ARRAY ||
462 target == GL_TEXTURE_3D) {
463 perf_debug("%s: no support for multiple slices, fallback to CPU mapping "
464 "for PBO\n", __FUNCTION__);
465 return false;
466 }
467
468 int dst_stride = _mesa_image_row_stride(pack, texImage->Width, format, type);
469 bool dst_flip = false;
470 /* Mesa flips the dst_stride for ctx->Pack.Invert, our mt must have a
471 * normal dst_stride.
472 */
473 struct gl_pixelstore_attrib uninverted_pack = *pack;
474 if (ctx->Pack.Invert) {
475 dst_stride = -dst_stride;
476 dst_flip = true;
477 uninverted_pack.Invert = false;
478 }
479 dst_offset = (GLintptr) pixels;
480 dst_offset += _mesa_image_offset(2, &uninverted_pack, texImage->Width,
481 texImage->Height, format, type, 0, 0, 0);
482 dst_buffer = intel_bufferobj_buffer(brw, dst, dst_offset,
483 texImage->Height * dst_stride);
484
485 struct intel_mipmap_tree *pbo_mt =
486 intel_miptree_create_for_bo(brw,
487 dst_buffer,
488 intelImage->mt->format,
489 dst_offset,
490 texImage->Width, texImage->Height,
491 dst_stride);
492
493 if (!pbo_mt)
494 return false;
495
496 if (!intel_miptree_blit(brw,
497 intelImage->mt, texImage->Level, texImage->Face,
498 0, 0, false,
499 pbo_mt, 0, 0,
500 0, 0, dst_flip,
501 texImage->Width, texImage->Height, GL_COPY))
502 return false;
503
504 intel_miptree_release(&pbo_mt);
505
506 return true;
507 }
508
509 static void
510 intel_get_tex_image(struct gl_context *ctx,
511 GLenum format, GLenum type, GLvoid *pixels,
512 struct gl_texture_image *texImage) {
513 DBG("%s\n", __FUNCTION__);
514
515 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
516 /* Using PBOs, so try the BLT based path. */
517 if (blit_texture_to_pbo(ctx, format, type, pixels, texImage))
518 return;
519
520 }
521
522 _mesa_meta_GetTexImage(ctx, format, type, pixels, texImage);
523
524 DBG("%s - DONE\n", __FUNCTION__);
525 }
526
527 void
528 intelInitTextureImageFuncs(struct dd_function_table *functions)
529 {
530 functions->TexImage = intelTexImage;
531 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
532 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
533 functions->GetTexImage = intel_get_tex_image;
534 }