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