i965/tex_image: Pull the tex format from the renderbuffer in intelSetTexBuffer2
[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 #include "brw_blorp.h"
30
31 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
32
33 /* Make sure one doesn't end up shrinking base level zero unnecessarily.
34 * Determining the base level dimension by shifting higher level dimension
35 * ends up in off-by-one value in case base level has NPOT size (for example,
36 * 293 != 146 << 1).
37 * Choose the original base level dimension when shifted dimensions agree.
38 * Otherwise assume real resize is intended and use the new shifted value.
39 */
40 static unsigned
41 get_base_dim(unsigned old_base_dim, unsigned new_level_dim, unsigned level)
42 {
43 const unsigned old_level_dim = old_base_dim >> level;
44 const unsigned new_base_dim = new_level_dim << level;
45
46 return old_level_dim == new_level_dim ? old_base_dim : new_base_dim;
47 }
48
49 /* Work back from the specified level of the image to the baselevel and create a
50 * miptree of that size.
51 */
52 struct intel_mipmap_tree *
53 intel_miptree_create_for_teximage(struct brw_context *brw,
54 struct intel_texture_object *intelObj,
55 struct intel_texture_image *intelImage,
56 enum intel_miptree_create_flags flags)
57 {
58 GLuint lastLevel;
59 int width, height, depth;
60 unsigned old_width = 0, old_height = 0, old_depth = 0;
61 const struct intel_mipmap_tree *old_mt = intelObj->mt;
62 const unsigned level = intelImage->base.Base.Level;
63
64 intel_get_image_dims(&intelImage->base.Base, &width, &height, &depth);
65
66 if (old_mt) {
67 old_width = old_mt->surf.logical_level0_px.width;
68 old_height = old_mt->surf.logical_level0_px.height;
69 old_depth = old_mt->surf.dim == ISL_SURF_DIM_3D ?
70 old_mt->surf.logical_level0_px.depth :
71 old_mt->surf.logical_level0_px.array_len;
72 }
73
74 DBG("%s\n", __func__);
75
76 /* Figure out image dimensions at start level. */
77 switch(intelObj->base.Target) {
78 case GL_TEXTURE_2D_MULTISAMPLE:
79 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
80 case GL_TEXTURE_RECTANGLE:
81 case GL_TEXTURE_EXTERNAL_OES:
82 assert(level == 0);
83 break;
84 case GL_TEXTURE_3D:
85 depth = old_mt ? get_base_dim(old_depth, depth, level) :
86 depth << level;
87 /* Fall through */
88 case GL_TEXTURE_2D:
89 case GL_TEXTURE_2D_ARRAY:
90 case GL_TEXTURE_CUBE_MAP:
91 case GL_TEXTURE_CUBE_MAP_ARRAY:
92 height = old_mt ? get_base_dim(old_height, height, level) :
93 height << level;
94 /* Fall through */
95 case GL_TEXTURE_1D:
96 case GL_TEXTURE_1D_ARRAY:
97 width = old_mt ? get_base_dim(old_width, width, level) :
98 width << level;
99 break;
100 default:
101 unreachable("Unexpected target");
102 }
103
104 /* Guess a reasonable value for lastLevel. This is probably going
105 * to be wrong fairly often and might mean that we have to look at
106 * resizable buffers, or require that buffers implement lazy
107 * pagetable arrangements.
108 */
109 if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
110 intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
111 intelImage->base.Base.Level == 0 &&
112 !intelObj->base.GenerateMipmap) {
113 lastLevel = 0;
114 } else {
115 lastLevel = _mesa_get_tex_max_num_levels(intelObj->base.Target,
116 width, height, depth) - 1;
117 }
118
119 return intel_miptree_create(brw,
120 intelObj->base.Target,
121 intelImage->base.Base.TexFormat,
122 0,
123 lastLevel,
124 width,
125 height,
126 depth,
127 MAX2(intelImage->base.Base.NumSamples, 1),
128 flags);
129 }
130
131 static bool
132 intel_texsubimage_blorp(struct brw_context *brw, GLuint dims,
133 struct gl_texture_image *tex_image,
134 unsigned x, unsigned y, unsigned z,
135 unsigned width, unsigned height, unsigned depth,
136 GLenum format, GLenum type, const void *pixels,
137 const struct gl_pixelstore_attrib *packing)
138 {
139 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
140 const unsigned mt_level = tex_image->Level + tex_image->TexObject->MinLevel;
141 const unsigned mt_z = tex_image->TexObject->MinLayer + tex_image->Face + z;
142
143 /* The blorp path can't understand crazy format hackery */
144 if (_mesa_base_tex_format(&brw->ctx, tex_image->InternalFormat) !=
145 _mesa_get_format_base_format(tex_image->TexFormat))
146 return false;
147
148 return brw_blorp_upload_miptree(brw, intel_image->mt, tex_image->TexFormat,
149 mt_level, x, y, mt_z, width, height, depth,
150 tex_image->TexObject->Target, format, type,
151 pixels, packing);
152 }
153
154 /**
155 * \brief A fast path for glTexImage and glTexSubImage.
156 *
157 * This fast path is taken when the texture format is BGRA, RGBA,
158 * A or L and when the texture memory is X- or Y-tiled. It uploads
159 * the texture data by mapping the texture memory without a GTT fence, thus
160 * acquiring a tiled view of the memory, and then copying sucessive
161 * spans within each tile.
162 *
163 * This is a performance win over the conventional texture upload path because
164 * it avoids the performance penalty of writing through the write-combine
165 * buffer. In the conventional texture upload path,
166 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
167 * fence, thus acquiring a linear view of the memory, then each row in the
168 * image is memcpy'd. In this fast path, we replace each row's copy with
169 * a sequence of copies over each linear span in tile.
170 *
171 * One use case is Google Chrome's paint rectangles. Chrome (as
172 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
173 * Each page's content is initially uploaded with glTexImage2D and damaged
174 * regions are updated with glTexSubImage2D. On some workloads, the
175 * performance gain of this fastpath on Sandybridge is over 5x.
176 */
177 static bool
178 intel_texsubimage_tiled_memcpy(struct gl_context * ctx,
179 GLuint dims,
180 struct gl_texture_image *texImage,
181 GLint xoffset, GLint yoffset, GLint zoffset,
182 GLsizei width, GLsizei height, GLsizei depth,
183 GLenum format, GLenum type,
184 const GLvoid *pixels,
185 const struct gl_pixelstore_attrib *packing)
186 {
187 struct brw_context *brw = brw_context(ctx);
188 const struct gen_device_info *devinfo = &brw->screen->devinfo;
189 struct intel_texture_image *image = intel_texture_image(texImage);
190 int src_pitch;
191
192 /* The miptree's buffer. */
193 struct brw_bo *bo;
194
195 uint32_t cpp;
196 mem_copy_fn mem_copy = NULL;
197
198 /* This fastpath is restricted to specific texture types:
199 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
200 * more types.
201 *
202 * FINISHME: The restrictions below on packing alignment and packing row
203 * length are likely unneeded now because we calculate the source stride
204 * with _mesa_image_row_stride. However, before removing the restrictions
205 * we need tests.
206 */
207 if (!devinfo->has_llc ||
208 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
209 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
210 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
211 pixels == NULL ||
212 _mesa_is_bufferobj(packing->BufferObj) ||
213 packing->Alignment > 4 ||
214 packing->SkipPixels > 0 ||
215 packing->SkipRows > 0 ||
216 (packing->RowLength != 0 && packing->RowLength != width) ||
217 packing->SwapBytes ||
218 packing->LsbFirst ||
219 packing->Invert)
220 return false;
221
222 /* Only a simple blit, no scale, bias or other mapping. */
223 if (ctx->_ImageTransferState)
224 return false;
225
226 if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp))
227 return false;
228
229 /* If this is a nontrivial texture view, let another path handle it instead. */
230 if (texImage->TexObject->MinLayer)
231 return false;
232
233 if (!image->mt ||
234 (image->mt->surf.tiling != ISL_TILING_X &&
235 image->mt->surf.tiling != ISL_TILING_Y0)) {
236 /* The algorithm is written only for X- or Y-tiled memory. */
237 return false;
238 }
239
240 /* linear_to_tiled() assumes that if the object is swizzled, it is using
241 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
242 * true on gen5 and above.
243 *
244 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
245 * parts of the memory aren't swizzled at all. Userspace just can't handle
246 * that.
247 */
248 if (devinfo->gen < 5 && brw->has_swizzling)
249 return false;
250
251 int level = texImage->Level + texImage->TexObject->MinLevel;
252
253 /* Since we are going to write raw data to the miptree, we need to resolve
254 * any pending fast color clears before we start.
255 */
256 assert(image->mt->surf.logical_level0_px.depth == 1);
257 assert(image->mt->surf.logical_level0_px.array_len == 1);
258
259 intel_miptree_access_raw(brw, image->mt, level, 0, true);
260
261 bo = image->mt->bo;
262
263 if (brw_batch_references(&brw->batch, bo)) {
264 perf_debug("Flushing before mapping a referenced bo.\n");
265 intel_batchbuffer_flush(brw);
266 }
267
268 void *map = brw_bo_map(brw, bo, MAP_WRITE | MAP_RAW);
269 if (map == NULL) {
270 DBG("%s: failed to map bo\n", __func__);
271 return false;
272 }
273
274 src_pitch = _mesa_image_row_stride(packing, width, format, type);
275
276 /* We postponed printing this message until having committed to executing
277 * the function.
278 */
279 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
280 "mesa_format=0x%x tiling=%d "
281 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) ",
282 __func__, texImage->Level, xoffset, yoffset, width, height,
283 format, type, texImage->TexFormat, image->mt->surf.tiling,
284 packing->Alignment, packing->RowLength, packing->SkipPixels,
285 packing->SkipRows);
286
287 /* Adjust x and y offset based on miplevel */
288 unsigned level_x, level_y;
289 intel_miptree_get_image_offset(image->mt, level, 0, &level_x, &level_y);
290 xoffset += level_x;
291 yoffset += level_y;
292
293 linear_to_tiled(
294 xoffset * cpp, (xoffset + width) * cpp,
295 yoffset, yoffset + height,
296 map,
297 pixels,
298 image->mt->surf.row_pitch, src_pitch,
299 brw->has_swizzling,
300 image->mt->surf.tiling,
301 mem_copy
302 );
303
304 brw_bo_unmap(bo);
305 return true;
306 }
307
308
309 static void
310 intel_upload_tex(struct gl_context * ctx,
311 GLuint dims,
312 struct gl_texture_image *texImage,
313 GLint xoffset, GLint yoffset, GLint zoffset,
314 GLsizei width, GLsizei height, GLsizei depth,
315 GLenum format, GLenum type,
316 const GLvoid * pixels,
317 const struct gl_pixelstore_attrib *packing)
318 {
319 struct brw_context *brw = brw_context(ctx);
320 struct intel_mipmap_tree *mt = intel_texture_image(texImage)->mt;
321 bool ok;
322
323 /* Check that there is actually data to store. */
324 if (pixels == NULL && !_mesa_is_bufferobj(packing->BufferObj))
325 return;
326
327 bool tex_busy = mt && brw_bo_busy(mt->bo);
328
329 if (mt && mt->format == MESA_FORMAT_S_UINT8)
330 mt->r8stencil_needs_update = true;
331
332 if (_mesa_is_bufferobj(packing->BufferObj) || tex_busy ||
333 mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
334 ok = intel_texsubimage_blorp(brw, dims, texImage,
335 xoffset, yoffset, zoffset,
336 width, height, depth, format, type,
337 pixels, packing);
338 if (ok)
339 return;
340 }
341
342 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
343 xoffset, yoffset, zoffset,
344 width, height, depth,
345 format, type, pixels, packing);
346 if (ok)
347 return;
348
349 _mesa_store_texsubimage(ctx, dims, texImage,
350 xoffset, yoffset, zoffset,
351 width, height, depth,
352 format, type, pixels, packing);
353 }
354
355
356 static void
357 intelTexImage(struct gl_context * ctx,
358 GLuint dims,
359 struct gl_texture_image *texImage,
360 GLenum format, GLenum type, const void *pixels,
361 const struct gl_pixelstore_attrib *unpack)
362 {
363 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
364 __func__, _mesa_get_format_name(texImage->TexFormat),
365 _mesa_enum_to_string(texImage->TexObject->Target),
366 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
367 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
368
369 /* Allocate storage for texture data. */
370 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
371 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
372 return;
373 }
374
375 assert(intel_texture_image(texImage)->mt);
376
377 intel_upload_tex(ctx, dims, texImage, 0, 0, 0,
378 texImage->Width, texImage->Height, texImage->Depth,
379 format, type, pixels, unpack);
380 }
381
382
383 static void
384 intelTexSubImage(struct gl_context * ctx,
385 GLuint dims,
386 struct gl_texture_image *texImage,
387 GLint xoffset, GLint yoffset, GLint zoffset,
388 GLsizei width, GLsizei height, GLsizei depth,
389 GLenum format, GLenum type,
390 const GLvoid * pixels,
391 const struct gl_pixelstore_attrib *packing)
392 {
393 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
394 __func__, _mesa_get_format_name(texImage->TexFormat),
395 _mesa_enum_to_string(texImage->TexObject->Target),
396 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
397 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
398
399 intel_upload_tex(ctx, dims, texImage, xoffset, yoffset, zoffset,
400 width, height, depth, format, type, pixels, packing);
401 }
402
403
404 static void
405 intel_set_texture_image_mt(struct brw_context *brw,
406 struct gl_texture_image *image,
407 GLenum internal_format,
408 struct intel_mipmap_tree *mt)
409
410 {
411 struct gl_texture_object *texobj = image->TexObject;
412 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
413 struct intel_texture_image *intel_image = intel_texture_image(image);
414
415 _mesa_init_teximage_fields(&brw->ctx, image,
416 mt->surf.logical_level0_px.width,
417 mt->surf.logical_level0_px.height, 1,
418 0, internal_format, mt->format);
419
420 brw->ctx.Driver.FreeTextureImageBuffer(&brw->ctx, image);
421
422 intel_texobj->needs_validate = true;
423 intel_image->base.RowStride = mt->surf.row_pitch / mt->cpp;
424 assert(mt->surf.row_pitch % mt->cpp == 0);
425
426 intel_miptree_reference(&intel_image->mt, mt);
427
428 /* Immediately validate the image to the object. */
429 intel_miptree_reference(&intel_texobj->mt, mt);
430 }
431
432
433 void
434 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
435 GLint texture_format,
436 __DRIdrawable *dPriv)
437 {
438 struct gl_framebuffer *fb = dPriv->driverPrivate;
439 struct brw_context *brw = pDRICtx->driverPrivate;
440 struct gl_context *ctx = &brw->ctx;
441 struct intel_renderbuffer *rb;
442 struct gl_texture_object *texObj;
443 struct gl_texture_image *texImage;
444 mesa_format texFormat = MESA_FORMAT_NONE;
445 struct intel_mipmap_tree *mt;
446 GLenum internal_format = 0;
447
448 texObj = _mesa_get_current_tex_object(ctx, target);
449
450 if (!texObj)
451 return;
452
453 if (dPriv->lastStamp != dPriv->dri2.stamp ||
454 !pDRICtx->driScreenPriv->dri2.useInvalidate)
455 intel_update_renderbuffers(pDRICtx, dPriv);
456
457 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
458 /* If the miptree isn't set, then intel_update_renderbuffers was unable
459 * to get the BO for the drawable from the window system.
460 */
461 if (!rb || !rb->mt)
462 return;
463
464 /* Neither the EGL and GLX texture_from_pixmap specs say anything about
465 * sRGB. They are both from a time where sRGB was considered an extra
466 * encoding step you did as part of rendering/blending and not a format.
467 * Even though we have concept of sRGB visuals, X has classically assumed
468 * that your data is just bits and sRGB rendering is entirely a client-side
469 * rendering construct. The assumption is that the result of BindTexImage
470 * is a texture with a linear format even if it was rendered with sRGB
471 * encoding enabled.
472 */
473 texFormat = _mesa_get_srgb_format_linear(intel_rb_format(rb));
474
475 if (rb->mt->cpp == 4) {
476 /* The extra texture_format parameter indicates whether the alpha
477 * channel should be respected or ignored. If we set internal_format to
478 * GL_RGB, the texture handling code is smart enough to swap the format
479 * or apply a swizzle if the underlying format is RGBA so we don't need
480 * to stomp it to RGBX or anything like that.
481 */
482 if (texture_format == __DRI_TEXTURE_FORMAT_RGB)
483 internal_format = GL_RGB;
484 else
485 internal_format = GL_RGBA;
486 } else if (rb->mt->cpp == 2) {
487 internal_format = GL_RGB;
488 }
489
490 intel_miptree_make_shareable(brw, rb->mt);
491 mt = intel_miptree_create_for_bo(brw, rb->mt->bo, texFormat, 0,
492 rb->Base.Base.Width,
493 rb->Base.Base.Height,
494 1, rb->mt->surf.row_pitch,
495 rb->mt->surf.tiling,
496 MIPTREE_CREATE_DEFAULT);
497 if (mt == NULL)
498 return;
499 mt->target = target;
500
501 _mesa_lock_texture(&brw->ctx, texObj);
502 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
503 intel_set_texture_image_mt(brw, texImage, internal_format, mt);
504 intel_miptree_release(&mt);
505 _mesa_unlock_texture(&brw->ctx, texObj);
506 }
507
508 static GLboolean
509 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
510 struct gl_renderbuffer *rb,
511 struct gl_texture_image *image)
512 {
513 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
514 struct intel_texture_image *intel_image = intel_texture_image(image);
515 struct gl_texture_object *texobj = image->TexObject;
516 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
517
518 /* We can only handle RB allocated with AllocRenderbufferStorage, or
519 * window-system renderbuffers.
520 */
521 assert(!rb->TexImage);
522
523 if (!irb->mt)
524 return false;
525
526 _mesa_lock_texture(ctx, texobj);
527 _mesa_init_teximage_fields(ctx, image,
528 rb->Width, rb->Height, 1,
529 0, rb->InternalFormat, rb->Format);
530 image->NumSamples = rb->NumSamples;
531
532 intel_miptree_reference(&intel_image->mt, irb->mt);
533
534 /* Immediately validate the image to the object. */
535 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
536
537 intel_texobj->needs_validate = true;
538 _mesa_unlock_texture(ctx, texobj);
539
540 return true;
541 }
542
543 void
544 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
545 {
546 /* The old interface didn't have the format argument, so copy our
547 * implementation's behavior at the time.
548 */
549 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
550 }
551
552 static void
553 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
554 struct gl_texture_object *texObj,
555 struct gl_texture_image *texImage,
556 GLeglImageOES image_handle)
557 {
558 struct brw_context *brw = brw_context(ctx);
559 struct intel_mipmap_tree *mt;
560 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
561 __DRIimage *image;
562
563 image = dri_screen->dri2.image->lookupEGLImage(dri_screen, image_handle,
564 dri_screen->loaderPrivate);
565 if (image == NULL)
566 return;
567
568 /* We support external textures only for EGLImages created with
569 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
570 */
571 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
572 _mesa_error(ctx, GL_INVALID_OPERATION,
573 "glEGLImageTargetTexture2DOES(external target is enabled only "
574 "for images created with EGL_EXT_image_dma_buf_import");
575 return;
576 }
577
578 /* Disallow depth/stencil textures: we don't have a way to pass the
579 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
580 */
581 if (image->has_depthstencil) {
582 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
583 return;
584 }
585
586 mt = intel_miptree_create_for_dri_image(brw, image, target, image->format,
587 false);
588 if (mt == NULL)
589 return;
590
591 struct intel_texture_object *intel_texobj = intel_texture_object(texObj);
592 intel_texobj->planar_format = image->planar_format;
593
594 const GLenum internal_format =
595 image->internal_format != 0 ?
596 image->internal_format : _mesa_get_format_base_format(mt->format);
597 intel_set_texture_image_mt(brw, texImage, internal_format, mt);
598 intel_miptree_release(&mt);
599 }
600
601 static bool
602 intel_gettexsubimage_blorp(struct brw_context *brw,
603 struct gl_texture_image *tex_image,
604 unsigned x, unsigned y, unsigned z,
605 unsigned width, unsigned height, unsigned depth,
606 GLenum format, GLenum type, const void *pixels,
607 const struct gl_pixelstore_attrib *packing)
608 {
609 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
610 const unsigned mt_level = tex_image->Level + tex_image->TexObject->MinLevel;
611 const unsigned mt_z = tex_image->TexObject->MinLayer + tex_image->Face + z;
612
613 /* The blorp path can't understand crazy format hackery */
614 if (_mesa_base_tex_format(&brw->ctx, tex_image->InternalFormat) !=
615 _mesa_get_format_base_format(tex_image->TexFormat))
616 return false;
617
618 return brw_blorp_download_miptree(brw, intel_image->mt,
619 tex_image->TexFormat, SWIZZLE_XYZW,
620 mt_level, x, y, mt_z,
621 width, height, depth,
622 tex_image->TexObject->Target,
623 format, type, false, pixels, packing);
624 }
625
626 /**
627 * \brief A fast path for glGetTexImage.
628 *
629 * \see intel_readpixels_tiled_memcpy()
630 */
631 static bool
632 intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
633 struct gl_texture_image *texImage,
634 GLint xoffset, GLint yoffset,
635 GLsizei width, GLsizei height,
636 GLenum format, GLenum type,
637 GLvoid *pixels,
638 const struct gl_pixelstore_attrib *packing)
639 {
640 struct brw_context *brw = brw_context(ctx);
641 const struct gen_device_info *devinfo = &brw->screen->devinfo;
642 struct intel_texture_image *image = intel_texture_image(texImage);
643 int dst_pitch;
644
645 /* The miptree's buffer. */
646 struct brw_bo *bo;
647
648 uint32_t cpp;
649 mem_copy_fn mem_copy = NULL;
650
651 /* This fastpath is restricted to specific texture types:
652 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
653 * more types.
654 *
655 * FINISHME: The restrictions below on packing alignment and packing row
656 * length are likely unneeded now because we calculate the destination stride
657 * with _mesa_image_row_stride. However, before removing the restrictions
658 * we need tests.
659 */
660 if (!devinfo->has_llc ||
661 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
662 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
663 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
664 pixels == NULL ||
665 _mesa_is_bufferobj(packing->BufferObj) ||
666 packing->Alignment > 4 ||
667 packing->SkipPixels > 0 ||
668 packing->SkipRows > 0 ||
669 (packing->RowLength != 0 && packing->RowLength != width) ||
670 packing->SwapBytes ||
671 packing->LsbFirst ||
672 packing->Invert)
673 return false;
674
675 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
676 * function doesn't set the last channel to 1. Note this checks BaseFormat
677 * rather than TexFormat in case the RGBX format is being simulated with an
678 * RGBA format.
679 */
680 if (texImage->_BaseFormat == GL_RGB)
681 return false;
682
683 if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp))
684 return false;
685
686 /* If this is a nontrivial texture view, let another path handle it instead. */
687 if (texImage->TexObject->MinLayer)
688 return false;
689
690 if (!image->mt ||
691 (image->mt->surf.tiling != ISL_TILING_X &&
692 image->mt->surf.tiling != ISL_TILING_Y0)) {
693 /* The algorithm is written only for X- or Y-tiled memory. */
694 return false;
695 }
696
697 /* tiled_to_linear() assumes that if the object is swizzled, it is using
698 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
699 * true on gen5 and above.
700 *
701 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
702 * parts of the memory aren't swizzled at all. Userspace just can't handle
703 * that.
704 */
705 if (devinfo->gen < 5 && brw->has_swizzling)
706 return false;
707
708 int level = texImage->Level + texImage->TexObject->MinLevel;
709
710 /* Since we are going to write raw data to the miptree, we need to resolve
711 * any pending fast color clears before we start.
712 */
713 assert(image->mt->surf.logical_level0_px.depth == 1);
714 assert(image->mt->surf.logical_level0_px.array_len == 1);
715
716 intel_miptree_access_raw(brw, image->mt, level, 0, true);
717
718 bo = image->mt->bo;
719
720 if (brw_batch_references(&brw->batch, bo)) {
721 perf_debug("Flushing before mapping a referenced bo.\n");
722 intel_batchbuffer_flush(brw);
723 }
724
725 void *map = brw_bo_map(brw, bo, MAP_READ | MAP_RAW);
726 if (map == NULL) {
727 DBG("%s: failed to map bo\n", __func__);
728 return false;
729 }
730
731 dst_pitch = _mesa_image_row_stride(packing, width, format, type);
732
733 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
734 "mesa_format=0x%x tiling=%d "
735 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
736 __func__, texImage->Level, xoffset, yoffset, width, height,
737 format, type, texImage->TexFormat, image->mt->surf.tiling,
738 packing->Alignment, packing->RowLength, packing->SkipPixels,
739 packing->SkipRows);
740
741 /* Adjust x and y offset based on miplevel */
742 unsigned level_x, level_y;
743 intel_miptree_get_image_offset(image->mt, level, 0, &level_x, &level_y);
744 xoffset += level_x;
745 yoffset += level_y;
746
747 tiled_to_linear(
748 xoffset * cpp, (xoffset + width) * cpp,
749 yoffset, yoffset + height,
750 pixels,
751 map,
752 dst_pitch, image->mt->surf.row_pitch,
753 brw->has_swizzling,
754 image->mt->surf.tiling,
755 mem_copy
756 );
757
758 brw_bo_unmap(bo);
759 return true;
760 }
761
762 static void
763 intel_get_tex_sub_image(struct gl_context *ctx,
764 GLint xoffset, GLint yoffset, GLint zoffset,
765 GLsizei width, GLsizei height, GLint depth,
766 GLenum format, GLenum type, GLvoid *pixels,
767 struct gl_texture_image *texImage)
768 {
769 struct brw_context *brw = brw_context(ctx);
770 bool ok;
771
772 DBG("%s\n", __func__);
773
774 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
775 if (intel_gettexsubimage_blorp(brw, texImage,
776 xoffset, yoffset, zoffset,
777 width, height, depth, format, type,
778 pixels, &ctx->Pack))
779 return;
780
781 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
782 }
783
784 ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, xoffset, yoffset,
785 width, height,
786 format, type, pixels, &ctx->Pack);
787
788 if(ok)
789 return;
790
791 _mesa_meta_GetTexSubImage(ctx, xoffset, yoffset, zoffset,
792 width, height, depth,
793 format, type, pixels, texImage);
794
795 DBG("%s - DONE\n", __func__);
796 }
797
798 static void
799 flush_astc_denorms(struct gl_context *ctx, GLuint dims,
800 struct gl_texture_image *texImage,
801 GLint xoffset, GLint yoffset, GLint zoffset,
802 GLsizei width, GLsizei height, GLsizei depth)
803 {
804 struct compressed_pixelstore store;
805 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat,
806 width, height, depth,
807 &ctx->Unpack, &store);
808
809 for (int slice = 0; slice < store.CopySlices; slice++) {
810
811 /* Map dest texture buffer */
812 GLubyte *dstMap;
813 GLint dstRowStride;
814 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
815 xoffset, yoffset, width, height,
816 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
817 &dstMap, &dstRowStride);
818 if (!dstMap)
819 continue;
820
821 for (int i = 0; i < store.CopyRowsPerSlice; i++) {
822
823 /* An ASTC block is stored in little endian mode. The byte that
824 * contains bits 0..7 is stored at the lower address in memory.
825 */
826 struct astc_void_extent {
827 uint16_t header : 12;
828 uint16_t dontcare[3];
829 uint16_t R;
830 uint16_t G;
831 uint16_t B;
832 uint16_t A;
833 } *blocks = (struct astc_void_extent*) dstMap;
834
835 /* Iterate over every copied block in the row */
836 for (int j = 0; j < store.CopyBytesPerRow / 16; j++) {
837
838 /* Check if the header matches that of an LDR void-extent block */
839 if (blocks[j].header == 0xDFC) {
840
841 /* Flush UNORM16 values that would be denormalized */
842 if (blocks[j].A < 4) blocks[j].A = 0;
843 if (blocks[j].B < 4) blocks[j].B = 0;
844 if (blocks[j].G < 4) blocks[j].G = 0;
845 if (blocks[j].R < 4) blocks[j].R = 0;
846 }
847 }
848
849 dstMap += dstRowStride;
850 }
851
852 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
853 }
854 }
855
856
857 static void
858 intelCompressedTexSubImage(struct gl_context *ctx, GLuint dims,
859 struct gl_texture_image *texImage,
860 GLint xoffset, GLint yoffset, GLint zoffset,
861 GLsizei width, GLsizei height, GLsizei depth,
862 GLenum format,
863 GLsizei imageSize, const GLvoid *data)
864 {
865 /* Upload the compressed data blocks */
866 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
867 xoffset, yoffset, zoffset,
868 width, height, depth,
869 format, imageSize, data);
870
871 /* Fix up copied ASTC blocks if necessary */
872 GLenum gl_format = _mesa_compressed_format_to_glenum(ctx,
873 texImage->TexFormat);
874 bool is_linear_astc = _mesa_is_astc_format(gl_format) &&
875 !_mesa_is_srgb_format(gl_format);
876 struct brw_context *brw = (struct brw_context*) ctx;
877 const struct gen_device_info *devinfo = &brw->screen->devinfo;
878 if (devinfo->gen == 9 && is_linear_astc)
879 flush_astc_denorms(ctx, dims, texImage,
880 xoffset, yoffset, zoffset,
881 width, height, depth);
882 }
883
884 void
885 intelInitTextureImageFuncs(struct dd_function_table *functions)
886 {
887 functions->TexImage = intelTexImage;
888 functions->TexSubImage = intelTexSubImage;
889 functions->CompressedTexSubImage = intelCompressedTexSubImage;
890 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
891 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
892 functions->GetTexSubImage = intel_get_tex_sub_image;
893 }