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