i965: Use brw_batch_references in tex_busy check
[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 #include "main/glthread.h"
18
19 #include "drivers/common/meta.h"
20
21 #include "intel_mipmap_tree.h"
22 #include "intel_buffer_objects.h"
23 #include "intel_batchbuffer.h"
24 #include "intel_tex.h"
25 #include "intel_fbo.h"
26 #include "intel_image.h"
27 #include "brw_context.h"
28 #include "brw_blorp.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 static bool
131 intel_texsubimage_blorp(struct brw_context *brw, GLuint dims,
132 struct gl_texture_image *tex_image,
133 unsigned x, unsigned y, unsigned z,
134 unsigned width, unsigned height, unsigned depth,
135 GLenum format, GLenum type, const void *pixels,
136 const struct gl_pixelstore_attrib *packing)
137 {
138 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
139 const unsigned mt_level = tex_image->Level + tex_image->TexObject->MinLevel;
140 const unsigned mt_z = tex_image->TexObject->MinLayer + tex_image->Face + z;
141
142 /* The blorp path can't understand crazy format hackery */
143 if (_mesa_base_tex_format(&brw->ctx, tex_image->InternalFormat) !=
144 _mesa_get_format_base_format(tex_image->TexFormat))
145 return false;
146
147 return brw_blorp_upload_miptree(brw, intel_image->mt, tex_image->TexFormat,
148 mt_level, x, y, mt_z, width, height, depth,
149 tex_image->TexObject->Target, format, type,
150 pixels, packing);
151 }
152
153 /**
154 * \brief A fast path for glTexImage and glTexSubImage.
155 *
156 * This fast path is taken when the texture format is BGRA, RGBA,
157 * A or L and when the texture memory is X- or Y-tiled. It uploads
158 * the texture data by mapping the texture memory without a GTT fence, thus
159 * acquiring a tiled view of the memory, and then copying sucessive
160 * spans within each tile.
161 *
162 * This is a performance win over the conventional texture upload path because
163 * it avoids the performance penalty of writing through the write-combine
164 * buffer. In the conventional texture upload path,
165 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
166 * fence, thus acquiring a linear view of the memory, then each row in the
167 * image is memcpy'd. In this fast path, we replace each row's copy with
168 * a sequence of copies over each linear span in tile.
169 *
170 * One use case is Google Chrome's paint rectangles. Chrome (as
171 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
172 * Each page's content is initially uploaded with glTexImage2D and damaged
173 * regions are updated with glTexSubImage2D. On some workloads, the
174 * performance gain of this fastpath on Sandybridge is over 5x.
175 */
176 static bool
177 intel_texsubimage_tiled_memcpy(struct gl_context * ctx,
178 GLuint dims,
179 struct gl_texture_image *texImage,
180 GLint xoffset, GLint yoffset, GLint zoffset,
181 GLsizei width, GLsizei height, GLsizei depth,
182 GLenum format, GLenum type,
183 const GLvoid *pixels,
184 const struct gl_pixelstore_attrib *packing)
185 {
186 struct brw_context *brw = brw_context(ctx);
187 const struct gen_device_info *devinfo = &brw->screen->devinfo;
188 struct intel_texture_image *image = intel_texture_image(texImage);
189 int src_pitch;
190
191 /* The miptree's buffer. */
192 struct brw_bo *bo;
193
194 uint32_t cpp;
195 isl_memcpy_type copy_type;
196
197 /* This fastpath is restricted to specific texture types:
198 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
199 * more types.
200 *
201 * FINISHME: The restrictions below on packing alignment and packing row
202 * length are likely unneeded now because we calculate the source stride
203 * with _mesa_image_row_stride. However, before removing the restrictions
204 * we need tests.
205 */
206 if (!devinfo->has_llc ||
207 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
208 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
209 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
210 pixels == NULL ||
211 _mesa_is_bufferobj(packing->BufferObj) ||
212 packing->Alignment > 4 ||
213 packing->SkipPixels > 0 ||
214 packing->SkipRows > 0 ||
215 (packing->RowLength != 0 && packing->RowLength != width) ||
216 packing->SwapBytes ||
217 packing->LsbFirst ||
218 packing->Invert)
219 return false;
220
221 /* Only a simple blit, no scale, bias or other mapping. */
222 if (ctx->_ImageTransferState)
223 return false;
224
225 copy_type = intel_miptree_get_memcpy_type(texImage->TexFormat, format, type,
226 &cpp);
227 if (copy_type == ISL_MEMCPY_INVALID)
228 return false;
229
230 /* If this is a nontrivial texture view, let another path handle it instead. */
231 if (texImage->TexObject->MinLayer)
232 return false;
233
234 if (!image->mt ||
235 (image->mt->surf.tiling != ISL_TILING_X &&
236 image->mt->surf.tiling != ISL_TILING_Y0)) {
237 /* The algorithm is written only for X- or Y-tiled memory. */
238 return false;
239 }
240
241 /* linear_to_tiled() assumes that if the object is swizzled, it is using
242 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
243 * true on gen5 and above.
244 *
245 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
246 * parts of the memory aren't swizzled at all. Userspace just can't handle
247 * that.
248 */
249 if (devinfo->gen < 5 && brw->has_swizzling)
250 return false;
251
252 int level = texImage->Level + texImage->TexObject->MinLevel;
253
254 /* Since we are going to write raw data to the miptree, we need to resolve
255 * any pending fast color clears before we start.
256 */
257 assert(image->mt->surf.logical_level0_px.depth == 1);
258 assert(image->mt->surf.logical_level0_px.array_len == 1);
259
260 intel_miptree_access_raw(brw, image->mt, level, 0, true);
261
262 bo = image->mt->bo;
263
264 if (brw_batch_references(&brw->batch, bo)) {
265 perf_debug("Flushing before mapping a referenced bo.\n");
266 intel_batchbuffer_flush(brw);
267 }
268
269 void *map = brw_bo_map(brw, bo, MAP_WRITE | MAP_RAW);
270 if (map == NULL) {
271 DBG("%s: failed to map bo\n", __func__);
272 return false;
273 }
274
275 src_pitch = _mesa_image_row_stride(packing, width, format, type);
276
277 /* We postponed printing this message until having committed to executing
278 * the function.
279 */
280 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
281 "mesa_format=0x%x tiling=%d "
282 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) ",
283 __func__, texImage->Level, xoffset, yoffset, width, height,
284 format, type, texImage->TexFormat, image->mt->surf.tiling,
285 packing->Alignment, packing->RowLength, packing->SkipPixels,
286 packing->SkipRows);
287
288 /* Adjust x and y offset based on miplevel */
289 unsigned level_x, level_y;
290 intel_miptree_get_image_offset(image->mt, level, 0, &level_x, &level_y);
291 xoffset += level_x;
292 yoffset += level_y;
293
294 isl_memcpy_linear_to_tiled(
295 xoffset * cpp, (xoffset + width) * cpp,
296 yoffset, yoffset + height,
297 map,
298 pixels,
299 image->mt->surf.row_pitch_B, src_pitch,
300 brw->has_swizzling,
301 image->mt->surf.tiling,
302 copy_type
303 );
304
305 brw_bo_unmap(bo);
306 return true;
307 }
308
309
310 static void
311 intel_upload_tex(struct gl_context * ctx,
312 GLuint dims,
313 struct gl_texture_image *texImage,
314 GLint xoffset, GLint yoffset, GLint zoffset,
315 GLsizei width, GLsizei height, GLsizei depth,
316 GLenum format, GLenum type,
317 const GLvoid * pixels,
318 const struct gl_pixelstore_attrib *packing)
319 {
320 struct brw_context *brw = brw_context(ctx);
321 struct intel_mipmap_tree *mt = intel_texture_image(texImage)->mt;
322 bool ok;
323
324 /* Check that there is actually data to store. */
325 if (pixels == NULL && !_mesa_is_bufferobj(packing->BufferObj))
326 return;
327
328 bool tex_busy = mt &&
329 (brw_batch_references(&brw->batch, mt->bo) || brw_bo_busy(mt->bo));
330
331 if (_mesa_is_bufferobj(packing->BufferObj) || tex_busy ||
332 mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
333 ok = intel_texsubimage_blorp(brw, dims, texImage,
334 xoffset, yoffset, zoffset,
335 width, height, depth, format, type,
336 pixels, packing);
337 if (ok)
338 return;
339 }
340
341 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
342 xoffset, yoffset, zoffset,
343 width, height, depth,
344 format, type, pixels, packing);
345 if (ok)
346 return;
347
348 _mesa_store_texsubimage(ctx, dims, texImage,
349 xoffset, yoffset, zoffset,
350 width, height, depth,
351 format, type, pixels, packing);
352 }
353
354
355 static void
356 intelTexImage(struct gl_context * ctx,
357 GLuint dims,
358 struct gl_texture_image *texImage,
359 GLenum format, GLenum type, const void *pixels,
360 const struct gl_pixelstore_attrib *unpack)
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 /* Allocate storage for texture data. */
369 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
370 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
371 return;
372 }
373
374 assert(intel_texture_image(texImage)->mt);
375
376 intel_upload_tex(ctx, dims, texImage, 0, 0, 0,
377 texImage->Width, texImage->Height, texImage->Depth,
378 format, type, pixels, unpack);
379 }
380
381
382 static void
383 intelTexSubImage(struct gl_context * ctx,
384 GLuint dims,
385 struct gl_texture_image *texImage,
386 GLint xoffset, GLint yoffset, GLint zoffset,
387 GLsizei width, GLsizei height, GLsizei depth,
388 GLenum format, GLenum type,
389 const GLvoid * pixels,
390 const struct gl_pixelstore_attrib *packing)
391 {
392 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
393 __func__, _mesa_get_format_name(texImage->TexFormat),
394 _mesa_enum_to_string(texImage->TexObject->Target),
395 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
396 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
397
398 intel_upload_tex(ctx, dims, texImage, xoffset, yoffset, zoffset,
399 width, height, depth, format, type, pixels, packing);
400 }
401
402
403 static void
404 intel_set_texture_image_mt(struct brw_context *brw,
405 struct gl_texture_image *image,
406 GLenum internal_format,
407 mesa_format 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, 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_B / mt->cpp;
424 assert(mt->surf.row_pitch_B % 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 GLenum internal_format = 0;
446
447 _mesa_glthread_finish(ctx);
448
449 texObj = _mesa_get_current_tex_object(ctx, target);
450
451 if (!texObj)
452 return;
453
454 if (dPriv->lastStamp != dPriv->dri2.stamp ||
455 !pDRICtx->driScreenPriv->dri2.useInvalidate)
456 intel_update_renderbuffers(pDRICtx, dPriv);
457
458 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
459 /* If the miptree isn't set, then intel_update_renderbuffers was unable
460 * to get the BO for the drawable from the window system.
461 */
462 if (!rb || !rb->mt)
463 return;
464
465 /* Neither the EGL and GLX texture_from_pixmap specs say anything about
466 * sRGB. They are both from a time where sRGB was considered an extra
467 * encoding step you did as part of rendering/blending and not a format.
468 * Even though we have concept of sRGB visuals, X has classically assumed
469 * that your data is just bits and sRGB rendering is entirely a client-side
470 * rendering construct. The assumption is that the result of BindTexImage
471 * is a texture with a linear format even if it was rendered with sRGB
472 * encoding enabled.
473 */
474 texFormat = _mesa_get_srgb_format_linear(intel_rb_format(rb));
475
476 if (rb->mt->cpp == 4) {
477 /* The extra texture_format parameter indicates whether the alpha
478 * channel should be respected or ignored. If we set internal_format to
479 * GL_RGB, the texture handling code is smart enough to swap the format
480 * or apply a swizzle if the underlying format is RGBA so we don't need
481 * to stomp it to RGBX or anything like that.
482 */
483 if (texture_format == __DRI_TEXTURE_FORMAT_RGB)
484 internal_format = GL_RGB;
485 else
486 internal_format = GL_RGBA;
487 } else if (rb->mt->cpp == 2) {
488 internal_format = GL_RGB;
489 }
490
491 intel_miptree_finish_external(brw, rb->mt);
492
493 _mesa_lock_texture(&brw->ctx, texObj);
494 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
495 intel_set_texture_image_mt(brw, texImage, internal_format,
496 texFormat, rb->mt);
497 _mesa_unlock_texture(&brw->ctx, texObj);
498 }
499
500 void
501 intelReleaseTexBuffer(__DRIcontext *pDRICtx, GLint target,
502 __DRIdrawable *dPriv)
503 {
504 struct brw_context *brw = pDRICtx->driverPrivate;
505 struct gl_context *ctx = &brw->ctx;
506 struct gl_texture_object *tex_obj;
507 struct intel_texture_object *intel_tex;
508
509 tex_obj = _mesa_get_current_tex_object(ctx, target);
510 if (!tex_obj)
511 return;
512
513 _mesa_lock_texture(&brw->ctx, tex_obj);
514
515 intel_tex = intel_texture_object(tex_obj);
516 if (!intel_tex->mt) {
517 _mesa_unlock_texture(&brw->ctx, tex_obj);
518 return;
519 }
520
521 /* The intel_miptree_prepare_external below as well as the finish_external
522 * above in intelSetTexBuffer2 *should* do nothing. The BindTexImage call
523 * from both GLX and EGL has TexImage2D and not TexSubImage2D semantics so
524 * the texture is not immutable. This means that the user cannot create a
525 * texture view of the image with a different format. Since the only three
526 * formats available when using BindTexImage are all UNORM, we can never
527 * end up with an sRGB format being used for texturing and so we shouldn't
528 * get any format-related resolves when texturing from it.
529 *
530 * While very unlikely, it is possible that the client could use the bound
531 * texture with GL_ARB_image_load_store. In that case, we'll do a resolve
532 * but that's not actually a problem as it just means that we lose
533 * compression on this texture until the next time it's used as a render
534 * target.
535 *
536 * The only other way we could end up with an unexpected aux usage would be
537 * if we rendered to the image from the same context as we have it bound as
538 * a texture between BindTexImage and ReleaseTexImage. However, the spec
539 * clearly calls this case out and says you shouldn't do that. It doesn't
540 * explicitly prevent binding the texture to a framebuffer but it says the
541 * results of trying to render to it while bound are undefined.
542 *
543 * Just to keep everything safe and sane, we do a prepare_external but it
544 * should be a no-op in almost all cases. On the off chance that someone
545 * ever triggers this, we should at least warn them.
546 */
547 if (intel_tex->mt->aux_buf &&
548 intel_miptree_get_aux_state(intel_tex->mt, 0, 0) !=
549 isl_drm_modifier_get_default_aux_state(intel_tex->mt->drm_modifier)) {
550 _mesa_warning(ctx, "Aux state changed between BindTexImage and "
551 "ReleaseTexImage. Most likely someone tried to draw "
552 "to the pixmap bound in BindTexImage or used it with "
553 "image_load_store.");
554 }
555
556 intel_miptree_prepare_external(brw, intel_tex->mt);
557
558 _mesa_unlock_texture(&brw->ctx, tex_obj);
559 }
560
561 static GLboolean
562 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
563 struct gl_renderbuffer *rb,
564 struct gl_texture_image *image)
565 {
566 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
567 struct intel_texture_image *intel_image = intel_texture_image(image);
568 struct gl_texture_object *texobj = image->TexObject;
569 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
570
571 /* We can only handle RB allocated with AllocRenderbufferStorage, or
572 * window-system renderbuffers.
573 */
574 assert(!rb->TexImage);
575
576 if (!irb->mt)
577 return false;
578
579 _mesa_lock_texture(ctx, texobj);
580 _mesa_init_teximage_fields(ctx, image,
581 rb->Width, rb->Height, 1,
582 0, rb->InternalFormat, rb->Format);
583 image->NumSamples = rb->NumSamples;
584
585 intel_miptree_reference(&intel_image->mt, irb->mt);
586
587 /* Immediately validate the image to the object. */
588 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
589
590 intel_texobj->needs_validate = true;
591 _mesa_unlock_texture(ctx, texobj);
592
593 return true;
594 }
595
596 void
597 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
598 {
599 /* The old interface didn't have the format argument, so copy our
600 * implementation's behavior at the time.
601 */
602 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
603 }
604
605 static void
606 intel_image_target_texture(struct gl_context *ctx, GLenum target,
607 struct gl_texture_object *texObj,
608 struct gl_texture_image *texImage,
609 GLeglImageOES image_handle,
610 bool storage)
611 {
612 struct brw_context *brw = brw_context(ctx);
613 struct intel_mipmap_tree *mt;
614 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
615 __DRIimage *image;
616
617 image = dri_screen->dri2.image->lookupEGLImage(dri_screen, image_handle,
618 dri_screen->loaderPrivate);
619 if (image == NULL)
620 return;
621
622 /* Disallow depth/stencil textures: we don't have a way to pass the
623 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
624 */
625 if (image->has_depthstencil) {
626 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
627 return;
628 }
629
630 mt = intel_miptree_create_for_dri_image(brw, image, target, image->format,
631 false);
632 if (mt == NULL)
633 return;
634
635 struct intel_texture_object *intel_texobj = intel_texture_object(texObj);
636 intel_texobj->planar_format = image->planar_format;
637
638 GLenum internal_format =
639 image->internal_format != 0 ?
640 image->internal_format : _mesa_get_format_base_format(mt->format);
641
642 /* Fix the internal format when _mesa_get_format_base_format(mt->format)
643 * isn't a valid one for that particular format.
644 */
645 if (brw->mesa_format_supports_render[image->format]) {
646 if (image->format == MESA_FORMAT_R10G10B10A2_UNORM ||
647 image->format == MESA_FORMAT_R10G10B10X2_UNORM ||
648 image->format == MESA_FORMAT_B10G10R10A2_UNORM ||
649 image->format == MESA_FORMAT_B10G10R10X2_UNORM)
650 internal_format = GL_RGB10_A2;
651 }
652
653 /* Guess sized internal format for dma-bufs, as specified by
654 * EXT_EGL_image_storage.
655 */
656 if (storage && target == GL_TEXTURE_2D && image->imported_dmabuf) {
657 internal_format = driGLFormatToSizedInternalGLFormat(image->format);
658 if (internal_format == GL_NONE) {
659 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
660 return;
661 }
662 }
663
664 intel_set_texture_image_mt(brw, texImage, internal_format, mt->format, mt);
665 intel_miptree_release(&mt);
666 }
667
668 static void
669 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
670 struct gl_texture_object *texObj,
671 struct gl_texture_image *texImage,
672 GLeglImageOES image_handle)
673 {
674 intel_image_target_texture(ctx, target, texObj, texImage, image_handle,
675 false);
676 }
677
678 static void
679 intel_image_target_tex_storage(struct gl_context *ctx, GLenum target,
680 struct gl_texture_object *texObj,
681 struct gl_texture_image *texImage,
682 GLeglImageOES image_handle)
683 {
684 struct intel_texture_object *intel_texobj = intel_texture_object(texObj);
685 intel_image_target_texture(ctx, target, texObj, texImage, image_handle,
686 true);
687
688 /* The miptree is in a validated state, so no need to check later. */
689 intel_texobj->needs_validate = false;
690 intel_texobj->validated_first_level = 0;
691 intel_texobj->validated_last_level = 0;
692 intel_texobj->_Format = texImage->TexFormat;
693 }
694
695 static bool
696 intel_gettexsubimage_blorp(struct brw_context *brw,
697 struct gl_texture_image *tex_image,
698 unsigned x, unsigned y, unsigned z,
699 unsigned width, unsigned height, unsigned depth,
700 GLenum format, GLenum type, const void *pixels,
701 const struct gl_pixelstore_attrib *packing)
702 {
703 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
704 const unsigned mt_level = tex_image->Level + tex_image->TexObject->MinLevel;
705 const unsigned mt_z = tex_image->TexObject->MinLayer + tex_image->Face + z;
706
707 /* The blorp path can't understand crazy format hackery */
708 if (_mesa_base_tex_format(&brw->ctx, tex_image->InternalFormat) !=
709 _mesa_get_format_base_format(tex_image->TexFormat))
710 return false;
711
712 return brw_blorp_download_miptree(brw, intel_image->mt,
713 tex_image->TexFormat, SWIZZLE_XYZW,
714 mt_level, x, y, mt_z,
715 width, height, depth,
716 tex_image->TexObject->Target,
717 format, type, false, pixels, packing);
718 }
719
720 /**
721 * \brief A fast path for glGetTexImage.
722 *
723 * \see intel_readpixels_tiled_memcpy()
724 */
725 static bool
726 intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
727 struct gl_texture_image *texImage,
728 GLint xoffset, GLint yoffset,
729 GLsizei width, GLsizei height,
730 GLenum format, GLenum type,
731 GLvoid *pixels,
732 const struct gl_pixelstore_attrib *packing)
733 {
734 struct brw_context *brw = brw_context(ctx);
735 const struct gen_device_info *devinfo = &brw->screen->devinfo;
736 struct intel_texture_image *image = intel_texture_image(texImage);
737 int dst_pitch;
738
739 /* The miptree's buffer. */
740 struct brw_bo *bo;
741
742 uint32_t cpp;
743 isl_memcpy_type copy_type;
744
745 /* This fastpath is restricted to specific texture types:
746 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
747 * more types.
748 *
749 * FINISHME: The restrictions below on packing alignment and packing row
750 * length are likely unneeded now because we calculate the destination stride
751 * with _mesa_image_row_stride. However, before removing the restrictions
752 * we need tests.
753 */
754 if (!devinfo->has_llc ||
755 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
756 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
757 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
758 pixels == NULL ||
759 _mesa_is_bufferobj(packing->BufferObj) ||
760 packing->Alignment > 4 ||
761 packing->SkipPixels > 0 ||
762 packing->SkipRows > 0 ||
763 (packing->RowLength != 0 && packing->RowLength != width) ||
764 packing->SwapBytes ||
765 packing->LsbFirst ||
766 packing->Invert)
767 return false;
768
769 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
770 * function doesn't set the last channel to 1. Note this checks BaseFormat
771 * rather than TexFormat in case the RGBX format is being simulated with an
772 * RGBA format.
773 */
774 if (texImage->_BaseFormat == GL_RGB)
775 return false;
776
777 copy_type = intel_miptree_get_memcpy_type(texImage->TexFormat, format, type,
778 &cpp);
779 if (copy_type == ISL_MEMCPY_INVALID)
780 return false;
781
782 /* If this is a nontrivial texture view, let another path handle it instead. */
783 if (texImage->TexObject->MinLayer)
784 return false;
785
786 if (!image->mt ||
787 (image->mt->surf.tiling != ISL_TILING_X &&
788 image->mt->surf.tiling != ISL_TILING_Y0)) {
789 /* The algorithm is written only for X- or Y-tiled memory. */
790 return false;
791 }
792
793 /* tiled_to_linear() assumes that if the object is swizzled, it is using
794 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
795 * true on gen5 and above.
796 *
797 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
798 * parts of the memory aren't swizzled at all. Userspace just can't handle
799 * that.
800 */
801 if (devinfo->gen < 5 && brw->has_swizzling)
802 return false;
803
804 int level = texImage->Level + texImage->TexObject->MinLevel;
805
806 /* Since we are going to write raw data to the miptree, we need to resolve
807 * any pending fast color clears before we start.
808 */
809 assert(image->mt->surf.logical_level0_px.depth == 1);
810 assert(image->mt->surf.logical_level0_px.array_len == 1);
811
812 intel_miptree_access_raw(brw, image->mt, level, 0, true);
813
814 bo = image->mt->bo;
815
816 if (brw_batch_references(&brw->batch, bo)) {
817 perf_debug("Flushing before mapping a referenced bo.\n");
818 intel_batchbuffer_flush(brw);
819 }
820
821 void *map = brw_bo_map(brw, bo, MAP_READ | MAP_RAW);
822 if (map == NULL) {
823 DBG("%s: failed to map bo\n", __func__);
824 return false;
825 }
826
827 dst_pitch = _mesa_image_row_stride(packing, width, format, type);
828
829 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
830 "mesa_format=0x%x tiling=%d "
831 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
832 __func__, texImage->Level, xoffset, yoffset, width, height,
833 format, type, texImage->TexFormat, image->mt->surf.tiling,
834 packing->Alignment, packing->RowLength, packing->SkipPixels,
835 packing->SkipRows);
836
837 /* Adjust x and y offset based on miplevel */
838 unsigned level_x, level_y;
839 intel_miptree_get_image_offset(image->mt, level, 0, &level_x, &level_y);
840 xoffset += level_x;
841 yoffset += level_y;
842
843 isl_memcpy_tiled_to_linear(
844 xoffset * cpp, (xoffset + width) * cpp,
845 yoffset, yoffset + height,
846 pixels,
847 map,
848 dst_pitch, image->mt->surf.row_pitch_B,
849 brw->has_swizzling,
850 image->mt->surf.tiling,
851 copy_type
852 );
853
854 brw_bo_unmap(bo);
855 return true;
856 }
857
858 static void
859 intel_get_tex_sub_image(struct gl_context *ctx,
860 GLint xoffset, GLint yoffset, GLint zoffset,
861 GLsizei width, GLsizei height, GLint depth,
862 GLenum format, GLenum type, GLvoid *pixels,
863 struct gl_texture_image *texImage)
864 {
865 struct brw_context *brw = brw_context(ctx);
866 bool ok;
867
868 DBG("%s\n", __func__);
869
870 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
871 if (intel_gettexsubimage_blorp(brw, texImage,
872 xoffset, yoffset, zoffset,
873 width, height, depth, format, type,
874 pixels, &ctx->Pack))
875 return;
876
877 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
878 }
879
880 ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, xoffset, yoffset,
881 width, height,
882 format, type, pixels, &ctx->Pack);
883
884 if(ok)
885 return;
886
887 _mesa_meta_GetTexSubImage(ctx, xoffset, yoffset, zoffset,
888 width, height, depth,
889 format, type, pixels, texImage);
890
891 DBG("%s - DONE\n", __func__);
892 }
893
894 static void
895 flush_astc_denorms(struct gl_context *ctx, GLuint dims,
896 struct gl_texture_image *texImage,
897 GLint xoffset, GLint yoffset, GLint zoffset,
898 GLsizei width, GLsizei height, GLsizei depth)
899 {
900 struct compressed_pixelstore store;
901 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat,
902 width, height, depth,
903 &ctx->Unpack, &store);
904
905 for (int slice = 0; slice < store.CopySlices; slice++) {
906
907 /* Map dest texture buffer */
908 GLubyte *dstMap;
909 GLint dstRowStride;
910 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
911 xoffset, yoffset, width, height,
912 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
913 &dstMap, &dstRowStride);
914 if (!dstMap)
915 continue;
916
917 for (int i = 0; i < store.CopyRowsPerSlice; i++) {
918
919 /* An ASTC block is stored in little endian mode. The byte that
920 * contains bits 0..7 is stored at the lower address in memory.
921 */
922 struct astc_void_extent {
923 uint16_t header : 12;
924 uint16_t dontcare[3];
925 uint16_t R;
926 uint16_t G;
927 uint16_t B;
928 uint16_t A;
929 } *blocks = (struct astc_void_extent*) dstMap;
930
931 /* Iterate over every copied block in the row */
932 for (int j = 0; j < store.CopyBytesPerRow / 16; j++) {
933
934 /* Check if the header matches that of an LDR void-extent block */
935 if (blocks[j].header == 0xDFC) {
936
937 /* Flush UNORM16 values that would be denormalized */
938 if (blocks[j].A < 4) blocks[j].A = 0;
939 if (blocks[j].B < 4) blocks[j].B = 0;
940 if (blocks[j].G < 4) blocks[j].G = 0;
941 if (blocks[j].R < 4) blocks[j].R = 0;
942 }
943 }
944
945 dstMap += dstRowStride;
946 }
947
948 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
949 }
950 }
951
952
953 static void
954 intelCompressedTexSubImage(struct gl_context *ctx, GLuint dims,
955 struct gl_texture_image *texImage,
956 GLint xoffset, GLint yoffset, GLint zoffset,
957 GLsizei width, GLsizei height, GLsizei depth,
958 GLenum format,
959 GLsizei imageSize, const GLvoid *data)
960 {
961 /* Upload the compressed data blocks */
962 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
963 xoffset, yoffset, zoffset,
964 width, height, depth,
965 format, imageSize, data);
966
967 /* Fix up copied ASTC blocks if necessary */
968 GLenum gl_format = _mesa_compressed_format_to_glenum(ctx,
969 texImage->TexFormat);
970 bool is_linear_astc = _mesa_is_astc_format(gl_format) &&
971 !_mesa_is_srgb_format(gl_format);
972 struct brw_context *brw = (struct brw_context*) ctx;
973 const struct gen_device_info *devinfo = &brw->screen->devinfo;
974 if (devinfo->gen == 9 && !gen_device_info_is_9lp(devinfo) && is_linear_astc)
975 flush_astc_denorms(ctx, dims, texImage,
976 xoffset, yoffset, zoffset,
977 width, height, depth);
978 }
979
980 void
981 intelInitTextureImageFuncs(struct dd_function_table *functions)
982 {
983 functions->TexImage = intelTexImage;
984 functions->TexSubImage = intelTexSubImage;
985 functions->CompressedTexSubImage = intelCompressedTexSubImage;
986 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
987 functions->EGLImageTargetTexStorage = intel_image_target_tex_storage;
988 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
989 functions->GetTexSubImage = intel_get_tex_sub_image;
990 }