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