Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 uint32_t layout_flags)
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", __func__);
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 intelImage->base.Base.NumSamples,
83 layout_flags | MIPTREE_LAYOUT_TILING_ANY);
84 }
85
86 static void
87 intelTexImage(struct gl_context * ctx,
88 GLuint dims,
89 struct gl_texture_image *texImage,
90 GLenum format, GLenum type, const void *pixels,
91 const struct gl_pixelstore_attrib *unpack)
92 {
93 struct intel_texture_image *intelImage = intel_texture_image(texImage);
94 bool ok;
95
96 bool tex_busy = intelImage->mt && drm_intel_bo_busy(intelImage->mt->bo);
97
98 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
99 __func__, _mesa_get_format_name(texImage->TexFormat),
100 _mesa_enum_to_string(texImage->TexObject->Target),
101 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
102 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
103
104 /* Allocate storage for texture data. */
105 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
106 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
107 return;
108 }
109
110 assert(intelImage->mt);
111
112 ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage, 0, 0, 0,
113 texImage->Width, texImage->Height,
114 texImage->Depth,
115 format, type, pixels,
116 false /*allocate_storage*/,
117 tex_busy, unpack);
118 if (ok)
119 return;
120
121 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
122 0, 0, 0, /*x,y,z offsets*/
123 texImage->Width,
124 texImage->Height,
125 texImage->Depth,
126 format, type, pixels, unpack,
127 false /*allocate_storage*/);
128 if (ok)
129 return;
130
131 DBG("%s: upload image %dx%dx%d pixels %p\n",
132 __func__, texImage->Width, texImage->Height, texImage->Depth,
133 pixels);
134
135 _mesa_store_teximage(ctx, dims, texImage,
136 format, type, pixels, unpack);
137 }
138
139
140 /**
141 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
142 *
143 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
144 */
145 static void
146 intel_set_texture_image_bo(struct gl_context *ctx,
147 struct gl_texture_image *image,
148 drm_intel_bo *bo,
149 GLenum target,
150 GLenum internalFormat,
151 mesa_format format,
152 uint32_t offset,
153 GLuint width, GLuint height,
154 GLuint pitch,
155 GLuint tile_x, GLuint tile_y,
156 uint32_t layout_flags)
157 {
158 struct brw_context *brw = brw_context(ctx);
159 struct intel_texture_image *intel_image = intel_texture_image(image);
160 struct gl_texture_object *texobj = image->TexObject;
161 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
162 uint32_t draw_x, draw_y;
163
164 _mesa_init_teximage_fields(&brw->ctx, image,
165 width, height, 1,
166 0, internalFormat, format);
167
168 ctx->Driver.FreeTextureImageBuffer(ctx, image);
169
170 intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
171 0, width, height, 1, pitch,
172 layout_flags);
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, 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 /* We support external textures only for EGLImages created with
321 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
322 */
323 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
324 _mesa_error(ctx, GL_INVALID_OPERATION,
325 "glEGLImageTargetTexture2DOES(external target is enabled only "
326 "for images created with EGL_EXT_image_dma_buf_import");
327 return;
328 }
329
330 /* Disallow depth/stencil textures: we don't have a way to pass the
331 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
332 */
333 if (image->has_depthstencil) {
334 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
335 return;
336 }
337
338 /* Disable creation of the texture's aux buffers because the driver exposes
339 * no EGL API to manage them. That is, there is no API for resolving the aux
340 * buffer's content to the main buffer nor for invalidating the aux buffer's
341 * content.
342 */
343 intel_set_texture_image_bo(ctx, texImage, image->bo,
344 target, image->internal_format,
345 image->format, image->offset,
346 image->width, image->height,
347 image->pitch,
348 image->tile_x, image->tile_y,
349 MIPTREE_LAYOUT_DISABLE_AUX);
350 }
351
352 /**
353 * \brief A fast path for glGetTexImage.
354 *
355 * \see intel_readpixels_tiled_memcpy()
356 */
357 bool
358 intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
359 struct gl_texture_image *texImage,
360 GLint xoffset, GLint yoffset,
361 GLsizei width, GLsizei height,
362 GLenum format, GLenum type,
363 GLvoid *pixels,
364 const struct gl_pixelstore_attrib *packing)
365 {
366 struct brw_context *brw = brw_context(ctx);
367 struct intel_texture_image *image = intel_texture_image(texImage);
368 int dst_pitch;
369
370 /* The miptree's buffer. */
371 drm_intel_bo *bo;
372
373 int error = 0;
374
375 uint32_t cpp;
376 mem_copy_fn mem_copy = NULL;
377
378 /* This fastpath is restricted to specific texture types:
379 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
380 * more types.
381 *
382 * FINISHME: The restrictions below on packing alignment and packing row
383 * length are likely unneeded now because we calculate the destination stride
384 * with _mesa_image_row_stride. However, before removing the restrictions
385 * we need tests.
386 */
387 if (!brw->has_llc ||
388 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
389 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
390 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
391 pixels == NULL ||
392 _mesa_is_bufferobj(packing->BufferObj) ||
393 packing->Alignment > 4 ||
394 packing->SkipPixels > 0 ||
395 packing->SkipRows > 0 ||
396 (packing->RowLength != 0 && packing->RowLength != width) ||
397 packing->SwapBytes ||
398 packing->LsbFirst ||
399 packing->Invert)
400 return false;
401
402 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
403 * function doesn't set the last channel to 1.
404 */
405 if (texImage->TexFormat == MESA_FORMAT_B8G8R8X8_UNORM ||
406 texImage->TexFormat == MESA_FORMAT_R8G8B8X8_UNORM)
407 return false;
408
409 if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp,
410 INTEL_DOWNLOAD))
411 return false;
412
413 /* If this is a nontrivial texture view, let another path handle it instead. */
414 if (texImage->TexObject->MinLayer)
415 return false;
416
417 if (!image->mt ||
418 (image->mt->tiling != I915_TILING_X &&
419 image->mt->tiling != I915_TILING_Y)) {
420 /* The algorithm is written only for X- or Y-tiled memory. */
421 return false;
422 }
423
424 /* Since we are going to write raw data to the miptree, we need to resolve
425 * any pending fast color clears before we start.
426 */
427 intel_miptree_resolve_color(brw, image->mt);
428
429 bo = image->mt->bo;
430
431 if (drm_intel_bo_references(brw->batch.bo, bo)) {
432 perf_debug("Flushing before mapping a referenced bo.\n");
433 intel_batchbuffer_flush(brw);
434 }
435
436 error = brw_bo_map(brw, bo, false /* write enable */, "miptree");
437 if (error) {
438 DBG("%s: failed to map bo\n", __func__);
439 return false;
440 }
441
442 dst_pitch = _mesa_image_row_stride(packing, width, format, type);
443
444 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
445 "mesa_format=0x%x tiling=%d "
446 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
447 __func__, texImage->Level, xoffset, yoffset, width, height,
448 format, type, texImage->TexFormat, image->mt->tiling,
449 packing->Alignment, packing->RowLength, packing->SkipPixels,
450 packing->SkipRows);
451
452 int level = texImage->Level + texImage->TexObject->MinLevel;
453
454 /* Adjust x and y offset based on miplevel */
455 xoffset += image->mt->level[level].level_x;
456 yoffset += image->mt->level[level].level_y;
457
458 tiled_to_linear(
459 xoffset * cpp, (xoffset + width) * cpp,
460 yoffset, yoffset + height,
461 pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
462 bo->virtual,
463 dst_pitch, image->mt->pitch,
464 brw->has_swizzling,
465 image->mt->tiling,
466 mem_copy
467 );
468
469 drm_intel_bo_unmap(bo);
470 return true;
471 }
472
473 static void
474 intel_get_tex_sub_image(struct gl_context *ctx,
475 GLint xoffset, GLint yoffset, GLint zoffset,
476 GLsizei width, GLsizei height, GLint depth,
477 GLenum format, GLenum type, GLvoid *pixels,
478 struct gl_texture_image *texImage)
479 {
480 struct brw_context *brw = brw_context(ctx);
481 bool ok;
482
483 DBG("%s\n", __func__);
484
485 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
486 if (_mesa_meta_pbo_GetTexSubImage(ctx, 3, texImage,
487 xoffset, yoffset, zoffset,
488 width, height, depth, format, type,
489 pixels, &ctx->Pack)) {
490 /* Flush to guarantee coherency between the render cache and other
491 * caches the PBO could potentially be bound to after this point.
492 * See the related comment in intelReadPixels() for a more detailed
493 * explanation.
494 */
495 brw_emit_mi_flush(brw);
496 return;
497 }
498
499 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
500 }
501
502 ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, xoffset, yoffset,
503 width, height,
504 format, type, pixels, &ctx->Pack);
505
506 if(ok)
507 return;
508
509 _mesa_meta_GetTexSubImage(ctx, xoffset, yoffset, zoffset,
510 width, height, depth,
511 format, type, pixels, texImage);
512
513 DBG("%s - DONE\n", __func__);
514 }
515
516 void
517 intelInitTextureImageFuncs(struct dd_function_table *functions)
518 {
519 functions->TexImage = intelTexImage;
520 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
521 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
522 functions->GetTexSubImage = intel_get_tex_sub_image;
523 }