i965: Add new intel_set_texture_image_mt() helper
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_image.c
1
2 #include "main/macros.h"
3 #include "main/mtypes.h"
4 #include "main/enums.h"
5 #include "main/bufferobj.h"
6 #include "main/context.h"
7 #include "main/formats.h"
8 #include "main/glformats.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 (intelObj->base.Target == GL_TEXTURE_3D)
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 tex_busy, unpack);
116 if (ok)
117 return;
118
119 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
120 0, 0, 0, /*x,y,z offsets*/
121 texImage->Width,
122 texImage->Height,
123 texImage->Depth,
124 format, type, pixels, unpack,
125 false /*allocate_storage*/);
126 if (ok)
127 return;
128
129 DBG("%s: upload image %dx%dx%d pixels %p\n",
130 __func__, texImage->Width, texImage->Height, texImage->Depth,
131 pixels);
132
133 _mesa_store_teximage(ctx, dims, texImage,
134 format, type, pixels, unpack);
135 }
136
137
138 static void
139 intel_set_texture_image_mt(struct brw_context *brw,
140 struct gl_texture_image *image,
141 struct intel_mipmap_tree *mt)
142
143 {
144 const uint32_t internal_format = _mesa_get_format_base_format(mt->format);
145 struct gl_texture_object *texobj = image->TexObject;
146 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
147 struct intel_texture_image *intel_image = intel_texture_image(image);
148
149 _mesa_init_teximage_fields(&brw->ctx, image,
150 mt->logical_width0, mt->logical_height0, 1,
151 0, internal_format, mt->format);
152
153 brw->ctx.Driver.FreeTextureImageBuffer(&brw->ctx, image);
154
155 intel_texobj->needs_validate = true;
156 intel_image->base.RowStride = mt->pitch / mt->cpp;
157 assert(mt->pitch % mt->cpp == 0);
158
159 intel_miptree_reference(&intel_image->mt, mt);
160
161 /* Immediately validate the image to the object. */
162 intel_miptree_reference(&intel_texobj->mt, mt);
163 }
164
165 /**
166 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
167 *
168 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
169 */
170 static void
171 intel_set_texture_image_bo(struct gl_context *ctx,
172 struct gl_texture_image *image,
173 drm_intel_bo *bo,
174 GLenum target,
175 GLenum internalFormat,
176 mesa_format format,
177 uint32_t offset,
178 GLuint width, GLuint height,
179 GLuint pitch,
180 GLuint tile_x, GLuint tile_y,
181 uint32_t layout_flags)
182 {
183 struct brw_context *brw = brw_context(ctx);
184 uint32_t draw_x, draw_y;
185 struct intel_mipmap_tree *mt;
186
187 mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
188 0, width, height, 1, pitch,
189 layout_flags);
190 if (mt == NULL)
191 return;
192 mt->target = target;
193 mt->total_width = width;
194 mt->total_height = height;
195 mt->level[0].slice[0].x_offset = tile_x;
196 mt->level[0].slice[0].y_offset = tile_y;
197
198 intel_miptree_get_tile_offsets(mt, 0, 0, &draw_x, &draw_y);
199
200 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
201 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
202 * trouble resolving back to destination image due to alignment issues.
203 */
204 if (!brw->has_surface_tile_offset &&
205 (draw_x != 0 || draw_y != 0)) {
206 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
207 intel_miptree_release(&mt);
208 return;
209 }
210
211 mt->offset = offset;
212
213 intel_set_texture_image_mt(brw, image, mt);
214 intel_miptree_release(&mt);
215 }
216
217 void
218 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
219 GLint texture_format,
220 __DRIdrawable *dPriv)
221 {
222 struct gl_framebuffer *fb = dPriv->driverPrivate;
223 struct brw_context *brw = pDRICtx->driverPrivate;
224 struct gl_context *ctx = &brw->ctx;
225 struct intel_renderbuffer *rb;
226 struct gl_texture_object *texObj;
227 struct gl_texture_image *texImage;
228 int level = 0, internalFormat = 0;
229 mesa_format texFormat = MESA_FORMAT_NONE;
230
231 texObj = _mesa_get_current_tex_object(ctx, target);
232
233 if (!texObj)
234 return;
235
236 if (dPriv->lastStamp != dPriv->dri2.stamp ||
237 !pDRICtx->driScreenPriv->dri2.useInvalidate)
238 intel_update_renderbuffers(pDRICtx, dPriv);
239
240 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
241 /* If the miptree isn't set, then intel_update_renderbuffers was unable
242 * to get the BO for the drawable from the window system.
243 */
244 if (!rb || !rb->mt)
245 return;
246
247 if (rb->mt->cpp == 4) {
248 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
249 internalFormat = GL_RGB;
250 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
251 }
252 else {
253 internalFormat = GL_RGBA;
254 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
255 }
256 } else if (rb->mt->cpp == 2) {
257 internalFormat = GL_RGB;
258 texFormat = MESA_FORMAT_B5G6R5_UNORM;
259 }
260
261 _mesa_lock_texture(&brw->ctx, texObj);
262 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
263 intel_miptree_make_shareable(brw, rb->mt);
264 intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
265 internalFormat, texFormat, 0,
266 rb->Base.Base.Width,
267 rb->Base.Base.Height,
268 rb->mt->pitch,
269 0, 0, 0);
270 _mesa_unlock_texture(&brw->ctx, texObj);
271 }
272
273 static GLboolean
274 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
275 struct gl_renderbuffer *rb,
276 struct gl_texture_image *image)
277 {
278 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
279 struct intel_texture_image *intel_image = intel_texture_image(image);
280 struct gl_texture_object *texobj = image->TexObject;
281 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
282
283 /* We can only handle RB allocated with AllocRenderbufferStorage, or
284 * window-system renderbuffers.
285 */
286 assert(!rb->TexImage);
287
288 if (!irb->mt)
289 return false;
290
291 _mesa_lock_texture(ctx, texobj);
292 _mesa_init_teximage_fields(ctx, image,
293 rb->Width, rb->Height, 1,
294 0, rb->InternalFormat, rb->Format);
295 image->NumSamples = rb->NumSamples;
296
297 intel_miptree_reference(&intel_image->mt, irb->mt);
298
299 /* Immediately validate the image to the object. */
300 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
301
302 intel_texobj->needs_validate = true;
303 _mesa_unlock_texture(ctx, texobj);
304
305 return true;
306 }
307
308 void
309 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
310 {
311 /* The old interface didn't have the format argument, so copy our
312 * implementation's behavior at the time.
313 */
314 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
315 }
316
317 static void
318 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
319 struct gl_texture_object *texObj,
320 struct gl_texture_image *texImage,
321 GLeglImageOES image_handle)
322 {
323 struct brw_context *brw = brw_context(ctx);
324 __DRIscreen *screen;
325 __DRIimage *image;
326
327 screen = brw->intelScreen->driScrnPriv;
328 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
329 screen->loaderPrivate);
330 if (image == NULL)
331 return;
332
333 /* We support external textures only for EGLImages created with
334 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
335 */
336 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
337 _mesa_error(ctx, GL_INVALID_OPERATION,
338 "glEGLImageTargetTexture2DOES(external target is enabled only "
339 "for images created with EGL_EXT_image_dma_buf_import");
340 return;
341 }
342
343 /* Disallow depth/stencil textures: we don't have a way to pass the
344 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
345 */
346 if (image->has_depthstencil) {
347 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
348 return;
349 }
350
351 /* Disable creation of the texture's aux buffers because the driver exposes
352 * no EGL API to manage them. That is, there is no API for resolving the aux
353 * buffer's content to the main buffer nor for invalidating the aux buffer's
354 * content.
355 */
356 intel_set_texture_image_bo(ctx, texImage, image->bo,
357 target, image->internal_format,
358 image->format, image->offset,
359 image->width, image->height,
360 image->pitch,
361 image->tile_x, image->tile_y,
362 MIPTREE_LAYOUT_DISABLE_AUX);
363 }
364
365 /**
366 * \brief A fast path for glGetTexImage.
367 *
368 * \see intel_readpixels_tiled_memcpy()
369 */
370 bool
371 intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
372 struct gl_texture_image *texImage,
373 GLint xoffset, GLint yoffset,
374 GLsizei width, GLsizei height,
375 GLenum format, GLenum type,
376 GLvoid *pixels,
377 const struct gl_pixelstore_attrib *packing)
378 {
379 struct brw_context *brw = brw_context(ctx);
380 struct intel_texture_image *image = intel_texture_image(texImage);
381 int dst_pitch;
382
383 /* The miptree's buffer. */
384 drm_intel_bo *bo;
385
386 int error = 0;
387
388 uint32_t cpp;
389 mem_copy_fn mem_copy = NULL;
390
391 /* This fastpath is restricted to specific texture types:
392 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
393 * more types.
394 *
395 * FINISHME: The restrictions below on packing alignment and packing row
396 * length are likely unneeded now because we calculate the destination stride
397 * with _mesa_image_row_stride. However, before removing the restrictions
398 * we need tests.
399 */
400 if (!brw->has_llc ||
401 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
402 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
403 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
404 pixels == NULL ||
405 _mesa_is_bufferobj(packing->BufferObj) ||
406 packing->Alignment > 4 ||
407 packing->SkipPixels > 0 ||
408 packing->SkipRows > 0 ||
409 (packing->RowLength != 0 && packing->RowLength != width) ||
410 packing->SwapBytes ||
411 packing->LsbFirst ||
412 packing->Invert)
413 return false;
414
415 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
416 * function doesn't set the last channel to 1. Note this checks BaseFormat
417 * rather than TexFormat in case the RGBX format is being simulated with an
418 * RGBA format.
419 */
420 if (texImage->_BaseFormat == GL_RGB)
421 return false;
422
423 if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp))
424 return false;
425
426 /* If this is a nontrivial texture view, let another path handle it instead. */
427 if (texImage->TexObject->MinLayer)
428 return false;
429
430 if (!image->mt ||
431 (image->mt->tiling != I915_TILING_X &&
432 image->mt->tiling != I915_TILING_Y)) {
433 /* The algorithm is written only for X- or Y-tiled memory. */
434 return false;
435 }
436
437 /* Since we are going to write raw data to the miptree, we need to resolve
438 * any pending fast color clears before we start.
439 */
440 intel_miptree_resolve_color(brw, image->mt, 0);
441
442 bo = image->mt->bo;
443
444 if (drm_intel_bo_references(brw->batch.bo, bo)) {
445 perf_debug("Flushing before mapping a referenced bo.\n");
446 intel_batchbuffer_flush(brw);
447 }
448
449 error = brw_bo_map(brw, bo, false /* write enable */, "miptree");
450 if (error) {
451 DBG("%s: failed to map bo\n", __func__);
452 return false;
453 }
454
455 dst_pitch = _mesa_image_row_stride(packing, width, format, type);
456
457 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
458 "mesa_format=0x%x tiling=%d "
459 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
460 __func__, texImage->Level, xoffset, yoffset, width, height,
461 format, type, texImage->TexFormat, image->mt->tiling,
462 packing->Alignment, packing->RowLength, packing->SkipPixels,
463 packing->SkipRows);
464
465 int level = texImage->Level + texImage->TexObject->MinLevel;
466
467 /* Adjust x and y offset based on miplevel */
468 xoffset += image->mt->level[level].level_x;
469 yoffset += image->mt->level[level].level_y;
470
471 tiled_to_linear(
472 xoffset * cpp, (xoffset + width) * cpp,
473 yoffset, yoffset + height,
474 pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
475 bo->virtual,
476 dst_pitch, image->mt->pitch,
477 brw->has_swizzling,
478 image->mt->tiling,
479 mem_copy
480 );
481
482 drm_intel_bo_unmap(bo);
483 return true;
484 }
485
486 static void
487 intel_get_tex_sub_image(struct gl_context *ctx,
488 GLint xoffset, GLint yoffset, GLint zoffset,
489 GLsizei width, GLsizei height, GLint depth,
490 GLenum format, GLenum type, GLvoid *pixels,
491 struct gl_texture_image *texImage)
492 {
493 struct brw_context *brw = brw_context(ctx);
494 bool ok;
495
496 DBG("%s\n", __func__);
497
498 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
499 if (_mesa_meta_pbo_GetTexSubImage(ctx, 3, texImage,
500 xoffset, yoffset, zoffset,
501 width, height, depth, format, type,
502 pixels, &ctx->Pack)) {
503 /* Flush to guarantee coherency between the render cache and other
504 * caches the PBO could potentially be bound to after this point.
505 * See the related comment in intelReadPixels() for a more detailed
506 * explanation.
507 */
508 brw_emit_mi_flush(brw);
509 return;
510 }
511
512 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
513 }
514
515 ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, xoffset, yoffset,
516 width, height,
517 format, type, pixels, &ctx->Pack);
518
519 if(ok)
520 return;
521
522 _mesa_meta_GetTexSubImage(ctx, xoffset, yoffset, zoffset,
523 width, height, depth,
524 format, type, pixels, texImage);
525
526 DBG("%s - DONE\n", __func__);
527 }
528
529 static void
530 flush_astc_denorms(struct gl_context *ctx, GLuint dims,
531 struct gl_texture_image *texImage,
532 GLint xoffset, GLint yoffset, GLint zoffset,
533 GLsizei width, GLsizei height, GLsizei depth)
534 {
535 struct compressed_pixelstore store;
536 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat,
537 width, height, depth,
538 &ctx->Unpack, &store);
539
540 for (int slice = 0; slice < store.CopySlices; slice++) {
541
542 /* Map dest texture buffer */
543 GLubyte *dstMap;
544 GLint dstRowStride;
545 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
546 xoffset, yoffset, width, height,
547 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
548 &dstMap, &dstRowStride);
549 if (!dstMap)
550 continue;
551
552 for (int i = 0; i < store.CopyRowsPerSlice; i++) {
553
554 /* An ASTC block is stored in little endian mode. The byte that
555 * contains bits 0..7 is stored at the lower address in memory.
556 */
557 struct astc_void_extent {
558 uint16_t header : 12;
559 uint16_t dontcare[3];
560 uint16_t R;
561 uint16_t G;
562 uint16_t B;
563 uint16_t A;
564 } *blocks = (struct astc_void_extent*) dstMap;
565
566 /* Iterate over every copied block in the row */
567 for (int j = 0; j < store.CopyBytesPerRow / 16; j++) {
568
569 /* Check if the header matches that of an LDR void-extent block */
570 if (blocks[j].header == 0xDFC) {
571
572 /* Flush UNORM16 values that would be denormalized */
573 if (blocks[j].A < 4) blocks[j].A = 0;
574 if (blocks[j].B < 4) blocks[j].B = 0;
575 if (blocks[j].G < 4) blocks[j].G = 0;
576 if (blocks[j].R < 4) blocks[j].R = 0;
577 }
578 }
579
580 dstMap += dstRowStride;
581 }
582
583 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
584 }
585 }
586
587
588 static void
589 intelCompressedTexSubImage(struct gl_context *ctx, GLuint dims,
590 struct gl_texture_image *texImage,
591 GLint xoffset, GLint yoffset, GLint zoffset,
592 GLsizei width, GLsizei height, GLsizei depth,
593 GLenum format,
594 GLsizei imageSize, const GLvoid *data)
595 {
596 /* Upload the compressed data blocks */
597 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
598 xoffset, yoffset, zoffset,
599 width, height, depth,
600 format, imageSize, data);
601
602 /* Fix up copied ASTC blocks if necessary */
603 GLenum gl_format = _mesa_compressed_format_to_glenum(ctx,
604 texImage->TexFormat);
605 bool is_linear_astc = _mesa_is_astc_format(gl_format) &&
606 !_mesa_is_srgb_format(gl_format);
607 struct brw_context *brw = (struct brw_context*) ctx;
608 if (brw->gen == 9 && is_linear_astc)
609 flush_astc_denorms(ctx, dims, texImage,
610 xoffset, yoffset, zoffset,
611 width, height, depth);
612 }
613
614 void
615 intelInitTextureImageFuncs(struct dd_function_table *functions)
616 {
617 functions->TexImage = intelTexImage;
618 functions->CompressedTexSubImage = intelCompressedTexSubImage;
619 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
620 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
621 functions->GetTexSubImage = intel_get_tex_sub_image;
622 }