i965: Tell intel_get_memcpy() which direction the memcpy() is going.
[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 #include "intel_tiled_memcpy.h"
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 false);
86 }
87
88 static void
89 intelTexImage(struct gl_context * ctx,
90 GLuint dims,
91 struct gl_texture_image *texImage,
92 GLenum format, GLenum type, const void *pixels,
93 const struct gl_pixelstore_attrib *unpack)
94 {
95 struct intel_texture_image *intelImage = intel_texture_image(texImage);
96 bool ok;
97
98 bool tex_busy = intelImage->mt && drm_intel_bo_busy(intelImage->mt->bo);
99
100 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
101 __FUNCTION__, _mesa_get_format_name(texImage->TexFormat),
102 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
103 _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type),
104 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
105
106 /* Allocate storage for texture data. */
107 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
108 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
109 return;
110 }
111
112 assert(intelImage->mt);
113
114 ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage, 0, 0, 0,
115 texImage->Width, texImage->Height,
116 texImage->Depth,
117 format, type, pixels,
118 false /*allocate_storage*/,
119 tex_busy, unpack);
120 if (ok)
121 return;
122
123 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
124 0, 0, 0, /*x,y,z offsets*/
125 texImage->Width,
126 texImage->Height,
127 texImage->Depth,
128 format, type, pixels, unpack,
129 false /*allocate_storage*/);
130 if (ok)
131 return;
132
133 DBG("%s: upload image %dx%dx%d pixels %p\n",
134 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
135 pixels);
136
137 _mesa_store_teximage(ctx, dims, texImage,
138 format, type, pixels, unpack);
139 }
140
141
142 /**
143 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
144 *
145 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
146 */
147 static void
148 intel_set_texture_image_bo(struct gl_context *ctx,
149 struct gl_texture_image *image,
150 drm_intel_bo *bo,
151 GLenum target,
152 GLenum internalFormat,
153 mesa_format format,
154 uint32_t offset,
155 GLuint width, GLuint height,
156 GLuint pitch,
157 GLuint tile_x, GLuint tile_y)
158 {
159 struct brw_context *brw = brw_context(ctx);
160 struct intel_texture_image *intel_image = intel_texture_image(image);
161 struct gl_texture_object *texobj = image->TexObject;
162 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
163 uint32_t draw_x, draw_y;
164
165 _mesa_init_teximage_fields(&brw->ctx, image,
166 width, height, 1,
167 0, internalFormat, format);
168
169 ctx->Driver.FreeTextureImageBuffer(ctx, image);
170
171 intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
172 0, width, height, 1, pitch);
173 if (intel_image->mt == NULL)
174 return;
175 intel_image->mt->target = target;
176 intel_image->mt->total_width = width;
177 intel_image->mt->total_height = height;
178 intel_image->mt->level[0].slice[0].x_offset = tile_x;
179 intel_image->mt->level[0].slice[0].y_offset = tile_y;
180
181 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
182
183 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
184 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
185 * trouble resolving back to destination image due to alignment issues.
186 */
187 if (!brw->has_surface_tile_offset &&
188 (draw_x != 0 || draw_y != 0)) {
189 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
190 intel_miptree_release(&intel_image->mt);
191 return;
192 }
193
194 intel_texobj->needs_validate = true;
195
196 intel_image->mt->offset = offset;
197 assert(pitch % intel_image->mt->cpp == 0);
198 intel_image->base.RowStride = pitch / intel_image->mt->cpp;
199
200 /* Immediately validate the image to the object. */
201 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
202 }
203
204 void
205 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
206 GLint texture_format,
207 __DRIdrawable *dPriv)
208 {
209 struct gl_framebuffer *fb = dPriv->driverPrivate;
210 struct brw_context *brw = pDRICtx->driverPrivate;
211 struct gl_context *ctx = &brw->ctx;
212 struct intel_renderbuffer *rb;
213 struct gl_texture_object *texObj;
214 struct gl_texture_image *texImage;
215 int level = 0, internalFormat = 0;
216 mesa_format texFormat = MESA_FORMAT_NONE;
217
218 texObj = _mesa_get_current_tex_object(ctx, target);
219
220 if (!texObj)
221 return;
222
223 if (dPriv->lastStamp != dPriv->dri2.stamp ||
224 !pDRICtx->driScreenPriv->dri2.useInvalidate)
225 intel_update_renderbuffers(pDRICtx, dPriv);
226
227 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
228 /* If the miptree isn't set, then intel_update_renderbuffers was unable
229 * to get the BO for the drawable from the window system.
230 */
231 if (!rb || !rb->mt)
232 return;
233
234 if (rb->mt->cpp == 4) {
235 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
236 internalFormat = GL_RGB;
237 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
238 }
239 else {
240 internalFormat = GL_RGBA;
241 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
242 }
243 } else if (rb->mt->cpp == 2) {
244 internalFormat = GL_RGB;
245 texFormat = MESA_FORMAT_B5G6R5_UNORM;
246 }
247
248 _mesa_lock_texture(&brw->ctx, texObj);
249 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
250 intel_miptree_make_shareable(brw, rb->mt);
251 intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
252 internalFormat, texFormat, 0,
253 rb->Base.Base.Width,
254 rb->Base.Base.Height,
255 rb->mt->pitch,
256 0, 0);
257 _mesa_unlock_texture(&brw->ctx, texObj);
258 }
259
260 static GLboolean
261 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
262 struct gl_renderbuffer *rb,
263 struct gl_texture_image *image)
264 {
265 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
266 struct intel_texture_image *intel_image = intel_texture_image(image);
267 struct gl_texture_object *texobj = image->TexObject;
268 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
269
270 /* We can only handle RB allocated with AllocRenderbufferStorage, or
271 * window-system renderbuffers.
272 */
273 assert(!rb->TexImage);
274
275 if (!irb->mt)
276 return false;
277
278 _mesa_lock_texture(ctx, texobj);
279 _mesa_init_teximage_fields(ctx, image,
280 rb->Width, rb->Height, 1,
281 0, rb->InternalFormat, rb->Format);
282 image->NumSamples = rb->NumSamples;
283
284 intel_miptree_reference(&intel_image->mt, irb->mt);
285
286 /* Immediately validate the image to the object. */
287 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
288
289 intel_texobj->needs_validate = true;
290 _mesa_unlock_texture(ctx, texobj);
291
292 return true;
293 }
294
295 void
296 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
297 {
298 /* The old interface didn't have the format argument, so copy our
299 * implementation's behavior at the time.
300 */
301 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
302 }
303
304 static void
305 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
306 struct gl_texture_object *texObj,
307 struct gl_texture_image *texImage,
308 GLeglImageOES image_handle)
309 {
310 struct brw_context *brw = brw_context(ctx);
311 __DRIscreen *screen;
312 __DRIimage *image;
313
314 screen = brw->intelScreen->driScrnPriv;
315 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
316 screen->loaderPrivate);
317 if (image == NULL)
318 return;
319
320 /**
321 * Images originating via EGL_EXT_image_dma_buf_import can be used only
322 * with GL_OES_EGL_image_external only.
323 */
324 if (image->dma_buf_imported && target != GL_TEXTURE_EXTERNAL_OES) {
325 _mesa_error(ctx, GL_INVALID_OPERATION,
326 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
327 "GL_OES_EGL_image_external only");
328 return;
329 }
330
331 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
332 _mesa_error(ctx, GL_INVALID_OPERATION,
333 "glEGLImageTargetTexture2DOES(external target is enabled only "
334 "for images created with EGL_EXT_image_dma_buf_import");
335 return;
336 }
337
338 /* Disallow depth/stencil textures: we don't have a way to pass the
339 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
340 */
341 if (image->has_depthstencil) {
342 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
343 return;
344 }
345
346 intel_set_texture_image_bo(ctx, texImage, image->bo,
347 target, image->internal_format,
348 image->format, image->offset,
349 image->width, image->height,
350 image->pitch,
351 image->tile_x, image->tile_y);
352 }
353
354 /**
355 * \brief A fast path for glGetTexImage.
356 *
357 * \see intel_readpixels_tiled_memcpy()
358 */
359 bool
360 intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
361 struct gl_texture_image *texImage,
362 GLint xoffset, GLint yoffset,
363 GLsizei width, GLsizei height,
364 GLenum format, GLenum type,
365 GLvoid *pixels,
366 const struct gl_pixelstore_attrib *packing)
367 {
368 struct brw_context *brw = brw_context(ctx);
369 struct intel_texture_image *image = intel_texture_image(texImage);
370 int dst_pitch;
371
372 /* The miptree's buffer. */
373 drm_intel_bo *bo;
374
375 int error = 0;
376
377 uint32_t cpp;
378 mem_copy_fn mem_copy = NULL;
379
380 /* This fastpath is restricted to specific texture types:
381 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
382 * more types.
383 *
384 * FINISHME: The restrictions below on packing alignment and packing row
385 * length are likely unneeded now because we calculate the destination stride
386 * with _mesa_image_row_stride. However, before removing the restrictions
387 * we need tests.
388 */
389 if (!brw->has_llc ||
390 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
391 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
392 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
393 pixels == NULL ||
394 _mesa_is_bufferobj(packing->BufferObj) ||
395 packing->Alignment > 4 ||
396 packing->SkipPixels > 0 ||
397 packing->SkipRows > 0 ||
398 (packing->RowLength != 0 && packing->RowLength != width) ||
399 packing->SwapBytes ||
400 packing->LsbFirst ||
401 packing->Invert)
402 return false;
403
404 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
405 * function doesn't set the last channel to 1.
406 */
407 if (texImage->TexFormat == MESA_FORMAT_B8G8R8X8_UNORM ||
408 texImage->TexFormat == MESA_FORMAT_R8G8B8X8_UNORM)
409 return false;
410
411 if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp,
412 INTEL_DOWNLOAD))
413 return false;
414
415 /* If this is a nontrivial texture view, let another path handle it instead. */
416 if (texImage->TexObject->MinLayer)
417 return false;
418
419 if (!image->mt ||
420 (image->mt->tiling != I915_TILING_X &&
421 image->mt->tiling != I915_TILING_Y)) {
422 /* The algorithm is written only for X- or Y-tiled memory. */
423 return false;
424 }
425
426 /* Since we are going to write raw data to the miptree, we need to resolve
427 * any pending fast color clears before we start.
428 */
429 intel_miptree_resolve_color(brw, image->mt);
430
431 bo = image->mt->bo;
432
433 if (drm_intel_bo_references(brw->batch.bo, bo)) {
434 perf_debug("Flushing before mapping a referenced bo.\n");
435 intel_batchbuffer_flush(brw);
436 }
437
438 error = brw_bo_map(brw, bo, false /* write enable */, "miptree");
439 if (error) {
440 DBG("%s: failed to map bo\n", __FUNCTION__);
441 return false;
442 }
443
444 dst_pitch = _mesa_image_row_stride(packing, width, format, type);
445
446 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
447 "mesa_format=0x%x tiling=%d "
448 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
449 __FUNCTION__, texImage->Level, xoffset, yoffset, width, height,
450 format, type, texImage->TexFormat, image->mt->tiling,
451 packing->Alignment, packing->RowLength, packing->SkipPixels,
452 packing->SkipRows);
453
454 int level = texImage->Level + texImage->TexObject->MinLevel;
455
456 /* Adjust x and y offset based on miplevel */
457 xoffset += image->mt->level[level].level_x;
458 yoffset += image->mt->level[level].level_y;
459
460 tiled_to_linear(
461 xoffset * cpp, (xoffset + width) * cpp,
462 yoffset, yoffset + height,
463 pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
464 bo->virtual,
465 dst_pitch, image->mt->pitch,
466 brw->has_swizzling,
467 image->mt->tiling,
468 mem_copy
469 );
470
471 drm_intel_bo_unmap(bo);
472 return true;
473 }
474
475 static void
476 intel_get_tex_image(struct gl_context *ctx,
477 GLenum format, GLenum type, GLvoid *pixels,
478 struct gl_texture_image *texImage) {
479 struct brw_context *brw = brw_context(ctx);
480 bool ok;
481
482 DBG("%s\n", __FUNCTION__);
483
484 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
485 if (_mesa_meta_pbo_GetTexSubImage(ctx, 3, texImage, 0, 0, 0,
486 texImage->Width, texImage->Height,
487 texImage->Depth, format, type,
488 pixels, &ctx->Pack))
489 return;
490
491 perf_debug("%s: fallback to CPU mapping in PBO case\n", __FUNCTION__);
492 }
493
494 ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, 0, 0,
495 texImage->Width, texImage->Height,
496 format, type, pixels, &ctx->Pack);
497
498 if(ok)
499 return;
500
501 _mesa_meta_GetTexImage(ctx, format, type, pixels, texImage);
502
503 DBG("%s - DONE\n", __FUNCTION__);
504 }
505
506 void
507 intelInitTextureImageFuncs(struct dd_function_table *functions)
508 {
509 functions->TexImage = intelTexImage;
510 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
511 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
512 functions->GetTexImage = intel_get_tex_image;
513 }