i965: Add GPU BLIT of texture image to PBO in Intel driver
[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 target %s level %d %dx%dx%d\n", __FUNCTION__,
177 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
178 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
179
180 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
181 0, 0, 0, /*x,y,z offsets*/
182 texImage->Width,
183 texImage->Height,
184 texImage->Depth,
185 format, type, pixels, unpack,
186 true /*for_glTexImage*/);
187 if (ok)
188 return;
189
190 /* Attempt to use the blitter for PBO image uploads.
191 */
192 if (dims <= 2 &&
193 try_pbo_upload(ctx, texImage, unpack, format, type, pixels)) {
194 return;
195 }
196
197 DBG("%s: upload image %dx%dx%d pixels %p\n",
198 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
199 pixels);
200
201 _mesa_store_teximage(ctx, dims, texImage,
202 format, type, pixels, unpack);
203 }
204
205
206 /**
207 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
208 *
209 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
210 */
211 static void
212 intel_set_texture_image_bo(struct gl_context *ctx,
213 struct gl_texture_image *image,
214 drm_intel_bo *bo,
215 GLenum target,
216 GLenum internalFormat,
217 mesa_format format,
218 uint32_t offset,
219 GLuint width, GLuint height,
220 GLuint pitch,
221 GLuint tile_x, GLuint tile_y)
222 {
223 struct brw_context *brw = brw_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 uint32_t draw_x, draw_y;
228
229 _mesa_init_teximage_fields(&brw->ctx, image,
230 width, height, 1,
231 0, internalFormat, format);
232
233 ctx->Driver.FreeTextureImageBuffer(ctx, image);
234
235 intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
236 0, width, height, pitch);
237 if (intel_image->mt == NULL)
238 return;
239 intel_image->mt->target = target;
240 intel_image->mt->total_width = width;
241 intel_image->mt->total_height = height;
242 intel_image->mt->level[0].slice[0].x_offset = tile_x;
243 intel_image->mt->level[0].slice[0].y_offset = tile_y;
244
245 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
246
247 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
248 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
249 * trouble resolving back to destination image due to alignment issues.
250 */
251 if (!brw->has_surface_tile_offset &&
252 (draw_x != 0 || draw_y != 0)) {
253 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
254 intel_miptree_release(&intel_image->mt);
255 return;
256 }
257
258 intel_texobj->needs_validate = true;
259
260 intel_image->mt->offset = offset;
261 assert(pitch % intel_image->mt->cpp == 0);
262 intel_image->base.RowStride = pitch / intel_image->mt->cpp;
263
264 /* Immediately validate the image to the object. */
265 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
266 }
267
268 void
269 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
270 GLint texture_format,
271 __DRIdrawable *dPriv)
272 {
273 struct gl_framebuffer *fb = dPriv->driverPrivate;
274 struct brw_context *brw = pDRICtx->driverPrivate;
275 struct gl_context *ctx = &brw->ctx;
276 struct intel_renderbuffer *rb;
277 struct gl_texture_object *texObj;
278 struct gl_texture_image *texImage;
279 int level = 0, internalFormat = 0;
280 mesa_format texFormat = MESA_FORMAT_NONE;
281
282 texObj = _mesa_get_current_tex_object(ctx, target);
283
284 if (!texObj)
285 return;
286
287 if (dPriv->lastStamp != dPriv->dri2.stamp ||
288 !pDRICtx->driScreenPriv->dri2.useInvalidate)
289 intel_update_renderbuffers(pDRICtx, dPriv);
290
291 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
292 /* If the miptree isn't set, then intel_update_renderbuffers was unable
293 * to get the BO for the drawable from the window system.
294 */
295 if (!rb || !rb->mt)
296 return;
297
298 if (rb->mt->cpp == 4) {
299 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
300 internalFormat = GL_RGB;
301 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
302 }
303 else {
304 internalFormat = GL_RGBA;
305 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
306 }
307 } else if (rb->mt->cpp == 2) {
308 internalFormat = GL_RGB;
309 texFormat = MESA_FORMAT_B5G6R5_UNORM;
310 }
311
312 _mesa_lock_texture(&brw->ctx, texObj);
313 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
314 intel_miptree_make_shareable(brw, rb->mt);
315 intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
316 internalFormat, texFormat, 0,
317 rb->Base.Base.Width,
318 rb->Base.Base.Height,
319 rb->mt->pitch,
320 0, 0);
321 _mesa_unlock_texture(&brw->ctx, texObj);
322 }
323
324 static GLboolean
325 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
326 struct gl_renderbuffer *rb,
327 struct gl_texture_image *image)
328 {
329 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
330 struct intel_texture_image *intel_image = intel_texture_image(image);
331 struct gl_texture_object *texobj = image->TexObject;
332 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
333
334 /* We can only handle RB allocated with AllocRenderbufferStorage, or
335 * window-system renderbuffers.
336 */
337 assert(!rb->TexImage);
338
339 if (!irb->mt)
340 return false;
341
342 _mesa_lock_texture(ctx, texobj);
343 _mesa_init_teximage_fields(ctx, image,
344 rb->Width, rb->Height, 1,
345 0, rb->InternalFormat, rb->Format);
346 image->NumSamples = rb->NumSamples;
347
348 intel_miptree_reference(&intel_image->mt, irb->mt);
349
350 /* Immediately validate the image to the object. */
351 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
352
353 intel_texobj->needs_validate = true;
354 _mesa_unlock_texture(ctx, texobj);
355
356 return true;
357 }
358
359 void
360 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
361 {
362 /* The old interface didn't have the format argument, so copy our
363 * implementation's behavior at the time.
364 */
365 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
366 }
367
368 static void
369 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
370 struct gl_texture_object *texObj,
371 struct gl_texture_image *texImage,
372 GLeglImageOES image_handle)
373 {
374 struct brw_context *brw = brw_context(ctx);
375 __DRIscreen *screen;
376 __DRIimage *image;
377
378 screen = brw->intelScreen->driScrnPriv;
379 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
380 screen->loaderPrivate);
381 if (image == NULL)
382 return;
383
384 /**
385 * Images originating via EGL_EXT_image_dma_buf_import can be used only
386 * with GL_OES_EGL_image_external only.
387 */
388 if (image->dma_buf_imported && target != GL_TEXTURE_EXTERNAL_OES) {
389 _mesa_error(ctx, GL_INVALID_OPERATION,
390 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
391 "GL_OES_EGL_image_external only");
392 return;
393 }
394
395 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
396 _mesa_error(ctx, GL_INVALID_OPERATION,
397 "glEGLImageTargetTexture2DOES(external target is enabled only "
398 "for images created with EGL_EXT_image_dma_buf_import");
399 return;
400 }
401
402 /* Disallow depth/stencil textures: we don't have a way to pass the
403 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
404 */
405 if (image->has_depthstencil) {
406 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
407 return;
408 }
409
410 intel_set_texture_image_bo(ctx, texImage, image->bo,
411 target, image->internal_format,
412 image->format, image->offset,
413 image->width, image->height,
414 image->pitch,
415 image->tile_x, image->tile_y);
416 }
417
418 static bool
419 blit_texture_to_pbo(struct gl_context *ctx,
420 GLenum format, GLenum type,
421 GLvoid * pixels, struct gl_texture_image *texImage)
422 {
423 struct intel_texture_image *intelImage = intel_texture_image(texImage);
424 struct brw_context *brw = brw_context(ctx);
425 const struct gl_pixelstore_attrib *pack = &ctx->Pack;
426 struct intel_buffer_object *dst = intel_buffer_object(pack->BufferObj);
427 GLuint dst_offset;
428 drm_intel_bo *dst_buffer;
429 GLenum target = texImage->TexObject->Target;
430
431 /* Check if we can use GPU blit to copy from the hardware texture
432 * format to the user's format/type.
433 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
434 */
435
436 if (!_mesa_format_matches_format_and_type(intelImage->mt->format, format,
437 type, false))
438 {
439 perf_debug("%s: unsupported format, fallback to CPU mapping for PBO\n",
440 __FUNCTION__);
441
442 return false;
443 }
444
445 if (ctx->_ImageTransferState) {
446 perf_debug("%s: bad transfer state, fallback to CPU mapping for PBO\n",
447 __FUNCTION__);
448 return false;
449 }
450
451 if (pack->SwapBytes || pack->LsbFirst) {
452 perf_debug("%s: unsupported pack swap params\n",
453 __FUNCTION__);
454 return false;
455 }
456
457 if (target == GL_TEXTURE_1D_ARRAY ||
458 target == GL_TEXTURE_2D_ARRAY ||
459 target == GL_TEXTURE_CUBE_MAP_ARRAY ||
460 target == GL_TEXTURE_3D) {
461 perf_debug("%s: no support for multiple slices, fallback to CPU mapping "
462 "for PBO\n", __FUNCTION__);
463 return false;
464 }
465
466 int dst_stride = _mesa_image_row_stride(pack, texImage->Width, format, type);
467 bool dst_flip = false;
468 /* Mesa flips the dst_stride for ctx->Pack.Invert, our mt must have a
469 * normal dst_stride.
470 */
471 struct gl_pixelstore_attrib uninverted_pack = *pack;
472 if (ctx->Pack.Invert) {
473 dst_stride = -dst_stride;
474 dst_flip = true;
475 uninverted_pack.Invert = false;
476 }
477 dst_offset = (GLintptr) pixels;
478 dst_offset += _mesa_image_offset(2, &uninverted_pack, texImage->Width,
479 texImage->Height, format, type, 0, 0, 0);
480 dst_buffer = intel_bufferobj_buffer(brw, dst, dst_offset,
481 texImage->Height * dst_stride);
482
483 struct intel_mipmap_tree *pbo_mt =
484 intel_miptree_create_for_bo(brw,
485 dst_buffer,
486 intelImage->mt->format,
487 dst_offset,
488 texImage->Width, texImage->Height,
489 dst_stride);
490
491 if (!pbo_mt)
492 return false;
493
494 if (!intel_miptree_blit(brw,
495 intelImage->mt, texImage->Level, texImage->Face,
496 0, 0, false,
497 pbo_mt, 0, 0,
498 0, 0, dst_flip,
499 texImage->Width, texImage->Height, GL_COPY))
500 return false;
501
502 intel_miptree_release(&pbo_mt);
503
504 return true;
505 }
506
507 static void
508 intel_get_tex_image(struct gl_context *ctx,
509 GLenum format, GLenum type, GLvoid *pixels,
510 struct gl_texture_image *texImage) {
511 DBG("%s\n", __FUNCTION__);
512
513 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
514 /* Using PBOs, so try the BLT based path. */
515 if (blit_texture_to_pbo(ctx, format, type, pixels, texImage))
516 return;
517
518 }
519
520 _mesa_meta_GetTexImage(ctx, format, type, pixels, texImage);
521
522 DBG("%s - DONE\n", __FUNCTION__);
523 }
524
525 void
526 intelInitTextureImageFuncs(struct dd_function_table *functions)
527 {
528 functions->TexImage = intelTexImage;
529 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
530 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
531 functions->GetTexImage = intel_get_tex_image;
532 }