i965: Use prepare_external instead of make_shareable in setTexBuffer2
[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 intel_upload_tex(struct gl_context * ctx,
288 GLuint dims,
289 struct gl_texture_image *texImage,
290 GLint xoffset, GLint yoffset, GLint zoffset,
291 GLsizei width, GLsizei height, GLsizei depth,
292 GLenum format, GLenum type,
293 const GLvoid * pixels,
294 const struct gl_pixelstore_attrib *packing)
295 {
296 struct intel_mipmap_tree *mt = intel_texture_image(texImage)->mt;
297 bool ok;
298
299 bool tex_busy = mt && brw_bo_busy(mt->bo);
300
301 if (mt && mt->format == MESA_FORMAT_S_UINT8)
302 mt->r8stencil_needs_update = true;
303
304 ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage,
305 xoffset, yoffset, zoffset,
306 width, height, depth, format, type,
307 pixels, tex_busy, packing);
308 if (ok)
309 return;
310
311 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
312 xoffset, yoffset, zoffset,
313 width, height, depth,
314 format, type, pixels, packing);
315 if (ok)
316 return;
317
318 _mesa_store_texsubimage(ctx, dims, texImage,
319 xoffset, yoffset, zoffset,
320 width, height, depth,
321 format, type, pixels, packing);
322 }
323
324
325 static void
326 intelTexImage(struct gl_context * ctx,
327 GLuint dims,
328 struct gl_texture_image *texImage,
329 GLenum format, GLenum type, const void *pixels,
330 const struct gl_pixelstore_attrib *unpack)
331 {
332 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
333 __func__, _mesa_get_format_name(texImage->TexFormat),
334 _mesa_enum_to_string(texImage->TexObject->Target),
335 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
336 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
337
338 /* Allocate storage for texture data. */
339 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
340 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
341 return;
342 }
343
344 assert(intel_texture_image(texImage)->mt);
345
346 intel_upload_tex(ctx, dims, texImage, 0, 0, 0,
347 texImage->Width, texImage->Height, texImage->Depth,
348 format, type, pixels, unpack);
349 }
350
351
352 static void
353 intelTexSubImage(struct gl_context * ctx,
354 GLuint dims,
355 struct gl_texture_image *texImage,
356 GLint xoffset, GLint yoffset, GLint zoffset,
357 GLsizei width, GLsizei height, GLsizei depth,
358 GLenum format, GLenum type,
359 const GLvoid * pixels,
360 const struct gl_pixelstore_attrib *packing)
361 {
362 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
363 __func__, _mesa_get_format_name(texImage->TexFormat),
364 _mesa_enum_to_string(texImage->TexObject->Target),
365 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
366 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
367
368 intel_upload_tex(ctx, dims, texImage, xoffset, yoffset, zoffset,
369 width, height, depth, format, type, pixels, packing);
370 }
371
372
373 static void
374 intel_set_texture_image_mt(struct brw_context *brw,
375 struct gl_texture_image *image,
376 GLenum internal_format,
377 struct intel_mipmap_tree *mt)
378
379 {
380 struct gl_texture_object *texobj = image->TexObject;
381 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
382 struct intel_texture_image *intel_image = intel_texture_image(image);
383
384 _mesa_init_teximage_fields(&brw->ctx, image,
385 mt->surf.logical_level0_px.width,
386 mt->surf.logical_level0_px.height, 1,
387 0, internal_format, mt->format);
388
389 brw->ctx.Driver.FreeTextureImageBuffer(&brw->ctx, image);
390
391 intel_texobj->needs_validate = true;
392 intel_image->base.RowStride = mt->surf.row_pitch / mt->cpp;
393 assert(mt->surf.row_pitch % mt->cpp == 0);
394
395 intel_miptree_reference(&intel_image->mt, mt);
396
397 /* Immediately validate the image to the object. */
398 intel_miptree_reference(&intel_texobj->mt, mt);
399 }
400
401
402 void
403 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
404 GLint texture_format,
405 __DRIdrawable *dPriv)
406 {
407 struct gl_framebuffer *fb = dPriv->driverPrivate;
408 struct brw_context *brw = pDRICtx->driverPrivate;
409 struct gl_context *ctx = &brw->ctx;
410 struct intel_renderbuffer *rb;
411 struct gl_texture_object *texObj;
412 struct gl_texture_image *texImage;
413 GLenum internal_format = 0;
414
415 texObj = _mesa_get_current_tex_object(ctx, target);
416
417 if (!texObj)
418 return;
419
420 if (dPriv->lastStamp != dPriv->dri2.stamp ||
421 !pDRICtx->driScreenPriv->dri2.useInvalidate)
422 intel_update_renderbuffers(pDRICtx, dPriv);
423
424 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
425 /* If the miptree isn't set, then intel_update_renderbuffers was unable
426 * to get the BO for the drawable from the window system.
427 */
428 if (!rb || !rb->mt)
429 return;
430
431 if (rb->mt->cpp == 4) {
432 if (texture_format == __DRI_TEXTURE_FORMAT_RGB)
433 internal_format = GL_RGB;
434 else
435 internal_format = GL_RGBA;
436 } else if (rb->mt->cpp == 2) {
437 /* This is 565 */
438 internal_format = GL_RGB;
439 }
440
441 intel_miptree_prepare_external(brw, rb->mt);
442
443 _mesa_lock_texture(&brw->ctx, texObj);
444 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
445 intel_set_texture_image_mt(brw, texImage, internal_format, rb->mt);
446 _mesa_unlock_texture(&brw->ctx, texObj);
447 }
448
449 static GLboolean
450 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
451 struct gl_renderbuffer *rb,
452 struct gl_texture_image *image)
453 {
454 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
455 struct intel_texture_image *intel_image = intel_texture_image(image);
456 struct gl_texture_object *texobj = image->TexObject;
457 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
458
459 /* We can only handle RB allocated with AllocRenderbufferStorage, or
460 * window-system renderbuffers.
461 */
462 assert(!rb->TexImage);
463
464 if (!irb->mt)
465 return false;
466
467 _mesa_lock_texture(ctx, texobj);
468 _mesa_init_teximage_fields(ctx, image,
469 rb->Width, rb->Height, 1,
470 0, rb->InternalFormat, rb->Format);
471 image->NumSamples = rb->NumSamples;
472
473 intel_miptree_reference(&intel_image->mt, irb->mt);
474
475 /* Immediately validate the image to the object. */
476 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
477
478 intel_texobj->needs_validate = true;
479 _mesa_unlock_texture(ctx, texobj);
480
481 return true;
482 }
483
484 void
485 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
486 {
487 /* The old interface didn't have the format argument, so copy our
488 * implementation's behavior at the time.
489 */
490 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
491 }
492
493 static void
494 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
495 struct gl_texture_object *texObj,
496 struct gl_texture_image *texImage,
497 GLeglImageOES image_handle)
498 {
499 struct brw_context *brw = brw_context(ctx);
500 struct intel_mipmap_tree *mt;
501 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
502 __DRIimage *image;
503
504 image = dri_screen->dri2.image->lookupEGLImage(dri_screen, image_handle,
505 dri_screen->loaderPrivate);
506 if (image == NULL)
507 return;
508
509 /* We support external textures only for EGLImages created with
510 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
511 */
512 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
513 _mesa_error(ctx, GL_INVALID_OPERATION,
514 "glEGLImageTargetTexture2DOES(external target is enabled only "
515 "for images created with EGL_EXT_image_dma_buf_import");
516 return;
517 }
518
519 /* Disallow depth/stencil textures: we don't have a way to pass the
520 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
521 */
522 if (image->has_depthstencil) {
523 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
524 return;
525 }
526
527 mt = intel_miptree_create_for_dri_image(brw, image, target,
528 ISL_COLORSPACE_NONE, false);
529 if (mt == NULL)
530 return;
531
532 struct intel_texture_object *intel_texobj = intel_texture_object(texObj);
533 intel_texobj->planar_format = image->planar_format;
534
535 const GLenum internal_format =
536 image->internal_format != 0 ?
537 image->internal_format : _mesa_get_format_base_format(mt->format);
538 intel_set_texture_image_mt(brw, texImage, internal_format, mt);
539 intel_miptree_release(&mt);
540 }
541
542 /**
543 * \brief A fast path for glGetTexImage.
544 *
545 * \see intel_readpixels_tiled_memcpy()
546 */
547 static bool
548 intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
549 struct gl_texture_image *texImage,
550 GLint xoffset, GLint yoffset,
551 GLsizei width, GLsizei height,
552 GLenum format, GLenum type,
553 GLvoid *pixels,
554 const struct gl_pixelstore_attrib *packing)
555 {
556 struct brw_context *brw = brw_context(ctx);
557 const struct gen_device_info *devinfo = &brw->screen->devinfo;
558 struct intel_texture_image *image = intel_texture_image(texImage);
559 int dst_pitch;
560
561 /* The miptree's buffer. */
562 struct brw_bo *bo;
563
564 uint32_t cpp;
565 mem_copy_fn mem_copy = NULL;
566
567 /* This fastpath is restricted to specific texture types:
568 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
569 * more types.
570 *
571 * FINISHME: The restrictions below on packing alignment and packing row
572 * length are likely unneeded now because we calculate the destination stride
573 * with _mesa_image_row_stride. However, before removing the restrictions
574 * we need tests.
575 */
576 if (!devinfo->has_llc ||
577 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
578 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
579 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
580 pixels == NULL ||
581 _mesa_is_bufferobj(packing->BufferObj) ||
582 packing->Alignment > 4 ||
583 packing->SkipPixels > 0 ||
584 packing->SkipRows > 0 ||
585 (packing->RowLength != 0 && packing->RowLength != width) ||
586 packing->SwapBytes ||
587 packing->LsbFirst ||
588 packing->Invert)
589 return false;
590
591 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
592 * function doesn't set the last channel to 1. Note this checks BaseFormat
593 * rather than TexFormat in case the RGBX format is being simulated with an
594 * RGBA format.
595 */
596 if (texImage->_BaseFormat == GL_RGB)
597 return false;
598
599 if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp))
600 return false;
601
602 /* If this is a nontrivial texture view, let another path handle it instead. */
603 if (texImage->TexObject->MinLayer)
604 return false;
605
606 if (!image->mt ||
607 (image->mt->surf.tiling != ISL_TILING_X &&
608 image->mt->surf.tiling != ISL_TILING_Y0)) {
609 /* The algorithm is written only for X- or Y-tiled memory. */
610 return false;
611 }
612
613 /* tiled_to_linear() assumes that if the object is swizzled, it is using
614 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
615 * true on gen5 and above.
616 *
617 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
618 * parts of the memory aren't swizzled at all. Userspace just can't handle
619 * that.
620 */
621 if (devinfo->gen < 5 && brw->has_swizzling)
622 return false;
623
624 int level = texImage->Level + texImage->TexObject->MinLevel;
625
626 /* Since we are going to write raw data to the miptree, we need to resolve
627 * any pending fast color clears before we start.
628 */
629 assert(image->mt->surf.logical_level0_px.depth == 1);
630 assert(image->mt->surf.logical_level0_px.array_len == 1);
631
632 intel_miptree_access_raw(brw, image->mt, level, 0, true);
633
634 bo = image->mt->bo;
635
636 if (brw_batch_references(&brw->batch, bo)) {
637 perf_debug("Flushing before mapping a referenced bo.\n");
638 intel_batchbuffer_flush(brw);
639 }
640
641 void *map = brw_bo_map(brw, bo, MAP_READ | MAP_RAW);
642 if (map == NULL) {
643 DBG("%s: failed to map bo\n", __func__);
644 return false;
645 }
646
647 dst_pitch = _mesa_image_row_stride(packing, width, format, type);
648
649 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
650 "mesa_format=0x%x tiling=%d "
651 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
652 __func__, texImage->Level, xoffset, yoffset, width, height,
653 format, type, texImage->TexFormat, image->mt->surf.tiling,
654 packing->Alignment, packing->RowLength, packing->SkipPixels,
655 packing->SkipRows);
656
657 /* Adjust x and y offset based on miplevel */
658 unsigned level_x, level_y;
659 intel_miptree_get_image_offset(image->mt, level, 0, &level_x, &level_y);
660 xoffset += level_x;
661 yoffset += level_y;
662
663 tiled_to_linear(
664 xoffset * cpp, (xoffset + width) * cpp,
665 yoffset, yoffset + height,
666 pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
667 map,
668 dst_pitch, image->mt->surf.row_pitch,
669 brw->has_swizzling,
670 image->mt->surf.tiling,
671 mem_copy
672 );
673
674 brw_bo_unmap(bo);
675 return true;
676 }
677
678 static void
679 intel_get_tex_sub_image(struct gl_context *ctx,
680 GLint xoffset, GLint yoffset, GLint zoffset,
681 GLsizei width, GLsizei height, GLint depth,
682 GLenum format, GLenum type, GLvoid *pixels,
683 struct gl_texture_image *texImage)
684 {
685 struct brw_context *brw = brw_context(ctx);
686 bool ok;
687
688 DBG("%s\n", __func__);
689
690 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
691 if (_mesa_meta_pbo_GetTexSubImage(ctx, 3, texImage,
692 xoffset, yoffset, zoffset,
693 width, height, depth, format, type,
694 pixels, &ctx->Pack)) {
695 /* Flush to guarantee coherency between the render cache and other
696 * caches the PBO could potentially be bound to after this point.
697 * See the related comment in intelReadPixels() for a more detailed
698 * explanation.
699 */
700 brw_emit_mi_flush(brw);
701 return;
702 }
703
704 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
705 }
706
707 ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, xoffset, yoffset,
708 width, height,
709 format, type, pixels, &ctx->Pack);
710
711 if(ok)
712 return;
713
714 _mesa_meta_GetTexSubImage(ctx, xoffset, yoffset, zoffset,
715 width, height, depth,
716 format, type, pixels, texImage);
717
718 DBG("%s - DONE\n", __func__);
719 }
720
721 static void
722 flush_astc_denorms(struct gl_context *ctx, GLuint dims,
723 struct gl_texture_image *texImage,
724 GLint xoffset, GLint yoffset, GLint zoffset,
725 GLsizei width, GLsizei height, GLsizei depth)
726 {
727 struct compressed_pixelstore store;
728 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat,
729 width, height, depth,
730 &ctx->Unpack, &store);
731
732 for (int slice = 0; slice < store.CopySlices; slice++) {
733
734 /* Map dest texture buffer */
735 GLubyte *dstMap;
736 GLint dstRowStride;
737 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
738 xoffset, yoffset, width, height,
739 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
740 &dstMap, &dstRowStride);
741 if (!dstMap)
742 continue;
743
744 for (int i = 0; i < store.CopyRowsPerSlice; i++) {
745
746 /* An ASTC block is stored in little endian mode. The byte that
747 * contains bits 0..7 is stored at the lower address in memory.
748 */
749 struct astc_void_extent {
750 uint16_t header : 12;
751 uint16_t dontcare[3];
752 uint16_t R;
753 uint16_t G;
754 uint16_t B;
755 uint16_t A;
756 } *blocks = (struct astc_void_extent*) dstMap;
757
758 /* Iterate over every copied block in the row */
759 for (int j = 0; j < store.CopyBytesPerRow / 16; j++) {
760
761 /* Check if the header matches that of an LDR void-extent block */
762 if (blocks[j].header == 0xDFC) {
763
764 /* Flush UNORM16 values that would be denormalized */
765 if (blocks[j].A < 4) blocks[j].A = 0;
766 if (blocks[j].B < 4) blocks[j].B = 0;
767 if (blocks[j].G < 4) blocks[j].G = 0;
768 if (blocks[j].R < 4) blocks[j].R = 0;
769 }
770 }
771
772 dstMap += dstRowStride;
773 }
774
775 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
776 }
777 }
778
779
780 static void
781 intelCompressedTexSubImage(struct gl_context *ctx, GLuint dims,
782 struct gl_texture_image *texImage,
783 GLint xoffset, GLint yoffset, GLint zoffset,
784 GLsizei width, GLsizei height, GLsizei depth,
785 GLenum format,
786 GLsizei imageSize, const GLvoid *data)
787 {
788 /* Upload the compressed data blocks */
789 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
790 xoffset, yoffset, zoffset,
791 width, height, depth,
792 format, imageSize, data);
793
794 /* Fix up copied ASTC blocks if necessary */
795 GLenum gl_format = _mesa_compressed_format_to_glenum(ctx,
796 texImage->TexFormat);
797 bool is_linear_astc = _mesa_is_astc_format(gl_format) &&
798 !_mesa_is_srgb_format(gl_format);
799 struct brw_context *brw = (struct brw_context*) ctx;
800 const struct gen_device_info *devinfo = &brw->screen->devinfo;
801 if (devinfo->gen == 9 && is_linear_astc)
802 flush_astc_denorms(ctx, dims, texImage,
803 xoffset, yoffset, zoffset,
804 width, height, depth);
805 }
806
807 void
808 intelInitTextureImageFuncs(struct dd_function_table *functions)
809 {
810 functions->TexImage = intelTexImage;
811 functions->TexSubImage = intelTexSubImage;
812 functions->CompressedTexSubImage = intelCompressedTexSubImage;
813 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
814 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
815 functions->GetTexSubImage = intel_get_tex_sub_image;
816 }