i965: Implement threaded GL support.
[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 && brw_bo_busy(mt->bo);
329
330 if (_mesa_is_bufferobj(packing->BufferObj) || tex_busy ||
331 mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
332 ok = intel_texsubimage_blorp(brw, dims, texImage,
333 xoffset, yoffset, zoffset,
334 width, height, depth, format, type,
335 pixels, packing);
336 if (ok)
337 return;
338 }
339
340 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
341 xoffset, yoffset, zoffset,
342 width, height, depth,
343 format, type, pixels, packing);
344 if (ok)
345 return;
346
347 _mesa_store_texsubimage(ctx, dims, texImage,
348 xoffset, yoffset, zoffset,
349 width, height, depth,
350 format, type, pixels, packing);
351 }
352
353
354 static void
355 intelTexImage(struct gl_context * ctx,
356 GLuint dims,
357 struct gl_texture_image *texImage,
358 GLenum format, GLenum type, const void *pixels,
359 const struct gl_pixelstore_attrib *unpack)
360 {
361 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
362 __func__, _mesa_get_format_name(texImage->TexFormat),
363 _mesa_enum_to_string(texImage->TexObject->Target),
364 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
365 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
366
367 /* Allocate storage for texture data. */
368 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
369 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
370 return;
371 }
372
373 assert(intel_texture_image(texImage)->mt);
374
375 intel_upload_tex(ctx, dims, texImage, 0, 0, 0,
376 texImage->Width, texImage->Height, texImage->Depth,
377 format, type, pixels, unpack);
378 }
379
380
381 static void
382 intelTexSubImage(struct gl_context * ctx,
383 GLuint dims,
384 struct gl_texture_image *texImage,
385 GLint xoffset, GLint yoffset, GLint zoffset,
386 GLsizei width, GLsizei height, GLsizei depth,
387 GLenum format, GLenum type,
388 const GLvoid * pixels,
389 const struct gl_pixelstore_attrib *packing)
390 {
391 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
392 __func__, _mesa_get_format_name(texImage->TexFormat),
393 _mesa_enum_to_string(texImage->TexObject->Target),
394 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
395 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
396
397 intel_upload_tex(ctx, dims, texImage, xoffset, yoffset, zoffset,
398 width, height, depth, format, type, pixels, packing);
399 }
400
401
402 static void
403 intel_set_texture_image_mt(struct brw_context *brw,
404 struct gl_texture_image *image,
405 GLenum internal_format,
406 mesa_format format,
407 struct intel_mipmap_tree *mt)
408
409 {
410 struct gl_texture_object *texobj = image->TexObject;
411 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
412 struct intel_texture_image *intel_image = intel_texture_image(image);
413
414 _mesa_init_teximage_fields(&brw->ctx, image,
415 mt->surf.logical_level0_px.width,
416 mt->surf.logical_level0_px.height, 1,
417 0, internal_format, format);
418
419 brw->ctx.Driver.FreeTextureImageBuffer(&brw->ctx, image);
420
421 intel_texobj->needs_validate = true;
422 intel_image->base.RowStride = mt->surf.row_pitch_B / mt->cpp;
423 assert(mt->surf.row_pitch_B % mt->cpp == 0);
424
425 intel_miptree_reference(&intel_image->mt, mt);
426
427 /* Immediately validate the image to the object. */
428 intel_miptree_reference(&intel_texobj->mt, mt);
429 }
430
431
432 void
433 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
434 GLint texture_format,
435 __DRIdrawable *dPriv)
436 {
437 struct gl_framebuffer *fb = dPriv->driverPrivate;
438 struct brw_context *brw = pDRICtx->driverPrivate;
439 struct gl_context *ctx = &brw->ctx;
440 struct intel_renderbuffer *rb;
441 struct gl_texture_object *texObj;
442 struct gl_texture_image *texImage;
443 mesa_format texFormat = MESA_FORMAT_NONE;
444 GLenum internal_format = 0;
445
446 _mesa_glthread_finish(ctx);
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_finish_external(brw, rb->mt);
491
492 _mesa_lock_texture(&brw->ctx, texObj);
493 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
494 intel_set_texture_image_mt(brw, texImage, internal_format,
495 texFormat, rb->mt);
496 _mesa_unlock_texture(&brw->ctx, texObj);
497 }
498
499 void
500 intelReleaseTexBuffer(__DRIcontext *pDRICtx, GLint target,
501 __DRIdrawable *dPriv)
502 {
503 struct brw_context *brw = pDRICtx->driverPrivate;
504 struct gl_context *ctx = &brw->ctx;
505 struct gl_texture_object *tex_obj;
506 struct intel_texture_object *intel_tex;
507
508 tex_obj = _mesa_get_current_tex_object(ctx, target);
509 if (!tex_obj)
510 return;
511
512 _mesa_lock_texture(&brw->ctx, tex_obj);
513
514 intel_tex = intel_texture_object(tex_obj);
515 if (!intel_tex->mt) {
516 _mesa_unlock_texture(&brw->ctx, tex_obj);
517 return;
518 }
519
520 /* The intel_miptree_prepare_external below as well as the finish_external
521 * above in intelSetTexBuffer2 *should* do nothing. The BindTexImage call
522 * from both GLX and EGL has TexImage2D and not TexSubImage2D semantics so
523 * the texture is not immutable. This means that the user cannot create a
524 * texture view of the image with a different format. Since the only three
525 * formats available when using BindTexImage are all UNORM, we can never
526 * end up with an sRGB format being used for texturing and so we shouldn't
527 * get any format-related resolves when texturing from it.
528 *
529 * While very unlikely, it is possible that the client could use the bound
530 * texture with GL_ARB_image_load_store. In that case, we'll do a resolve
531 * but that's not actually a problem as it just means that we lose
532 * compression on this texture until the next time it's used as a render
533 * target.
534 *
535 * The only other way we could end up with an unexpected aux usage would be
536 * if we rendered to the image from the same context as we have it bound as
537 * a texture between BindTexImage and ReleaseTexImage. However, the spec
538 * clearly calls this case out and says you shouldn't do that. It doesn't
539 * explicitly prevent binding the texture to a framebuffer but it says the
540 * results of trying to render to it while bound are undefined.
541 *
542 * Just to keep everything safe and sane, we do a prepare_external but it
543 * should be a no-op in almost all cases. On the off chance that someone
544 * ever triggers this, we should at least warn them.
545 */
546 if (intel_tex->mt->aux_buf &&
547 intel_miptree_get_aux_state(intel_tex->mt, 0, 0) !=
548 isl_drm_modifier_get_default_aux_state(intel_tex->mt->drm_modifier)) {
549 _mesa_warning(ctx, "Aux state changed between BindTexImage and "
550 "ReleaseTexImage. Most likely someone tried to draw "
551 "to the pixmap bound in BindTexImage or used it with "
552 "image_load_store.");
553 }
554
555 intel_miptree_prepare_external(brw, intel_tex->mt);
556
557 _mesa_unlock_texture(&brw->ctx, tex_obj);
558 }
559
560 static GLboolean
561 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
562 struct gl_renderbuffer *rb,
563 struct gl_texture_image *image)
564 {
565 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
566 struct intel_texture_image *intel_image = intel_texture_image(image);
567 struct gl_texture_object *texobj = image->TexObject;
568 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
569
570 /* We can only handle RB allocated with AllocRenderbufferStorage, or
571 * window-system renderbuffers.
572 */
573 assert(!rb->TexImage);
574
575 if (!irb->mt)
576 return false;
577
578 _mesa_lock_texture(ctx, texobj);
579 _mesa_init_teximage_fields(ctx, image,
580 rb->Width, rb->Height, 1,
581 0, rb->InternalFormat, rb->Format);
582 image->NumSamples = rb->NumSamples;
583
584 intel_miptree_reference(&intel_image->mt, irb->mt);
585
586 /* Immediately validate the image to the object. */
587 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
588
589 intel_texobj->needs_validate = true;
590 _mesa_unlock_texture(ctx, texobj);
591
592 return true;
593 }
594
595 void
596 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
597 {
598 /* The old interface didn't have the format argument, so copy our
599 * implementation's behavior at the time.
600 */
601 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
602 }
603
604 static void
605 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
606 struct gl_texture_object *texObj,
607 struct gl_texture_image *texImage,
608 GLeglImageOES image_handle)
609 {
610 struct brw_context *brw = brw_context(ctx);
611 struct intel_mipmap_tree *mt;
612 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
613 __DRIimage *image;
614
615 image = dri_screen->dri2.image->lookupEGLImage(dri_screen, image_handle,
616 dri_screen->loaderPrivate);
617 if (image == NULL)
618 return;
619
620 /* Disallow depth/stencil textures: we don't have a way to pass the
621 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
622 */
623 if (image->has_depthstencil) {
624 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
625 return;
626 }
627
628 mt = intel_miptree_create_for_dri_image(brw, image, target, image->format,
629 false);
630 if (mt == NULL)
631 return;
632
633 struct intel_texture_object *intel_texobj = intel_texture_object(texObj);
634 intel_texobj->planar_format = image->planar_format;
635
636 const GLenum internal_format =
637 image->internal_format != 0 ?
638 image->internal_format : _mesa_get_format_base_format(mt->format);
639 intel_set_texture_image_mt(brw, texImage, internal_format, mt->format, mt);
640 intel_miptree_release(&mt);
641 }
642
643 static bool
644 intel_gettexsubimage_blorp(struct brw_context *brw,
645 struct gl_texture_image *tex_image,
646 unsigned x, unsigned y, unsigned z,
647 unsigned width, unsigned height, unsigned depth,
648 GLenum format, GLenum type, const void *pixels,
649 const struct gl_pixelstore_attrib *packing)
650 {
651 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
652 const unsigned mt_level = tex_image->Level + tex_image->TexObject->MinLevel;
653 const unsigned mt_z = tex_image->TexObject->MinLayer + tex_image->Face + z;
654
655 /* The blorp path can't understand crazy format hackery */
656 if (_mesa_base_tex_format(&brw->ctx, tex_image->InternalFormat) !=
657 _mesa_get_format_base_format(tex_image->TexFormat))
658 return false;
659
660 return brw_blorp_download_miptree(brw, intel_image->mt,
661 tex_image->TexFormat, SWIZZLE_XYZW,
662 mt_level, x, y, mt_z,
663 width, height, depth,
664 tex_image->TexObject->Target,
665 format, type, false, pixels, packing);
666 }
667
668 /**
669 * \brief A fast path for glGetTexImage.
670 *
671 * \see intel_readpixels_tiled_memcpy()
672 */
673 static bool
674 intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
675 struct gl_texture_image *texImage,
676 GLint xoffset, GLint yoffset,
677 GLsizei width, GLsizei height,
678 GLenum format, GLenum type,
679 GLvoid *pixels,
680 const struct gl_pixelstore_attrib *packing)
681 {
682 struct brw_context *brw = brw_context(ctx);
683 const struct gen_device_info *devinfo = &brw->screen->devinfo;
684 struct intel_texture_image *image = intel_texture_image(texImage);
685 int dst_pitch;
686
687 /* The miptree's buffer. */
688 struct brw_bo *bo;
689
690 uint32_t cpp;
691 isl_memcpy_type copy_type;
692
693 /* This fastpath is restricted to specific texture types:
694 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
695 * more types.
696 *
697 * FINISHME: The restrictions below on packing alignment and packing row
698 * length are likely unneeded now because we calculate the destination stride
699 * with _mesa_image_row_stride. However, before removing the restrictions
700 * we need tests.
701 */
702 if (!devinfo->has_llc ||
703 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
704 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
705 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
706 pixels == NULL ||
707 _mesa_is_bufferobj(packing->BufferObj) ||
708 packing->Alignment > 4 ||
709 packing->SkipPixels > 0 ||
710 packing->SkipRows > 0 ||
711 (packing->RowLength != 0 && packing->RowLength != width) ||
712 packing->SwapBytes ||
713 packing->LsbFirst ||
714 packing->Invert)
715 return false;
716
717 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
718 * function doesn't set the last channel to 1. Note this checks BaseFormat
719 * rather than TexFormat in case the RGBX format is being simulated with an
720 * RGBA format.
721 */
722 if (texImage->_BaseFormat == GL_RGB)
723 return false;
724
725 copy_type = intel_miptree_get_memcpy_type(texImage->TexFormat, format, type,
726 &cpp);
727 if (copy_type == ISL_MEMCPY_INVALID)
728 return false;
729
730 /* If this is a nontrivial texture view, let another path handle it instead. */
731 if (texImage->TexObject->MinLayer)
732 return false;
733
734 if (!image->mt ||
735 (image->mt->surf.tiling != ISL_TILING_X &&
736 image->mt->surf.tiling != ISL_TILING_Y0)) {
737 /* The algorithm is written only for X- or Y-tiled memory. */
738 return false;
739 }
740
741 /* tiled_to_linear() assumes that if the object is swizzled, it is using
742 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
743 * true on gen5 and above.
744 *
745 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
746 * parts of the memory aren't swizzled at all. Userspace just can't handle
747 * that.
748 */
749 if (devinfo->gen < 5 && brw->has_swizzling)
750 return false;
751
752 int level = texImage->Level + texImage->TexObject->MinLevel;
753
754 /* Since we are going to write raw data to the miptree, we need to resolve
755 * any pending fast color clears before we start.
756 */
757 assert(image->mt->surf.logical_level0_px.depth == 1);
758 assert(image->mt->surf.logical_level0_px.array_len == 1);
759
760 intel_miptree_access_raw(brw, image->mt, level, 0, true);
761
762 bo = image->mt->bo;
763
764 if (brw_batch_references(&brw->batch, bo)) {
765 perf_debug("Flushing before mapping a referenced bo.\n");
766 intel_batchbuffer_flush(brw);
767 }
768
769 void *map = brw_bo_map(brw, bo, MAP_READ | MAP_RAW);
770 if (map == NULL) {
771 DBG("%s: failed to map bo\n", __func__);
772 return false;
773 }
774
775 dst_pitch = _mesa_image_row_stride(packing, width, format, type);
776
777 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
778 "mesa_format=0x%x tiling=%d "
779 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
780 __func__, texImage->Level, xoffset, yoffset, width, height,
781 format, type, texImage->TexFormat, image->mt->surf.tiling,
782 packing->Alignment, packing->RowLength, packing->SkipPixels,
783 packing->SkipRows);
784
785 /* Adjust x and y offset based on miplevel */
786 unsigned level_x, level_y;
787 intel_miptree_get_image_offset(image->mt, level, 0, &level_x, &level_y);
788 xoffset += level_x;
789 yoffset += level_y;
790
791 isl_memcpy_tiled_to_linear(
792 xoffset * cpp, (xoffset + width) * cpp,
793 yoffset, yoffset + height,
794 pixels,
795 map,
796 dst_pitch, image->mt->surf.row_pitch_B,
797 brw->has_swizzling,
798 image->mt->surf.tiling,
799 copy_type
800 );
801
802 brw_bo_unmap(bo);
803 return true;
804 }
805
806 static void
807 intel_get_tex_sub_image(struct gl_context *ctx,
808 GLint xoffset, GLint yoffset, GLint zoffset,
809 GLsizei width, GLsizei height, GLint depth,
810 GLenum format, GLenum type, GLvoid *pixels,
811 struct gl_texture_image *texImage)
812 {
813 struct brw_context *brw = brw_context(ctx);
814 bool ok;
815
816 DBG("%s\n", __func__);
817
818 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
819 if (intel_gettexsubimage_blorp(brw, texImage,
820 xoffset, yoffset, zoffset,
821 width, height, depth, format, type,
822 pixels, &ctx->Pack))
823 return;
824
825 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
826 }
827
828 ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, xoffset, yoffset,
829 width, height,
830 format, type, pixels, &ctx->Pack);
831
832 if(ok)
833 return;
834
835 _mesa_meta_GetTexSubImage(ctx, xoffset, yoffset, zoffset,
836 width, height, depth,
837 format, type, pixels, texImage);
838
839 DBG("%s - DONE\n", __func__);
840 }
841
842 static void
843 flush_astc_denorms(struct gl_context *ctx, GLuint dims,
844 struct gl_texture_image *texImage,
845 GLint xoffset, GLint yoffset, GLint zoffset,
846 GLsizei width, GLsizei height, GLsizei depth)
847 {
848 struct compressed_pixelstore store;
849 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat,
850 width, height, depth,
851 &ctx->Unpack, &store);
852
853 for (int slice = 0; slice < store.CopySlices; slice++) {
854
855 /* Map dest texture buffer */
856 GLubyte *dstMap;
857 GLint dstRowStride;
858 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
859 xoffset, yoffset, width, height,
860 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
861 &dstMap, &dstRowStride);
862 if (!dstMap)
863 continue;
864
865 for (int i = 0; i < store.CopyRowsPerSlice; i++) {
866
867 /* An ASTC block is stored in little endian mode. The byte that
868 * contains bits 0..7 is stored at the lower address in memory.
869 */
870 struct astc_void_extent {
871 uint16_t header : 12;
872 uint16_t dontcare[3];
873 uint16_t R;
874 uint16_t G;
875 uint16_t B;
876 uint16_t A;
877 } *blocks = (struct astc_void_extent*) dstMap;
878
879 /* Iterate over every copied block in the row */
880 for (int j = 0; j < store.CopyBytesPerRow / 16; j++) {
881
882 /* Check if the header matches that of an LDR void-extent block */
883 if (blocks[j].header == 0xDFC) {
884
885 /* Flush UNORM16 values that would be denormalized */
886 if (blocks[j].A < 4) blocks[j].A = 0;
887 if (blocks[j].B < 4) blocks[j].B = 0;
888 if (blocks[j].G < 4) blocks[j].G = 0;
889 if (blocks[j].R < 4) blocks[j].R = 0;
890 }
891 }
892
893 dstMap += dstRowStride;
894 }
895
896 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
897 }
898 }
899
900
901 static void
902 intelCompressedTexSubImage(struct gl_context *ctx, GLuint dims,
903 struct gl_texture_image *texImage,
904 GLint xoffset, GLint yoffset, GLint zoffset,
905 GLsizei width, GLsizei height, GLsizei depth,
906 GLenum format,
907 GLsizei imageSize, const GLvoid *data)
908 {
909 /* Upload the compressed data blocks */
910 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
911 xoffset, yoffset, zoffset,
912 width, height, depth,
913 format, imageSize, data);
914
915 /* Fix up copied ASTC blocks if necessary */
916 GLenum gl_format = _mesa_compressed_format_to_glenum(ctx,
917 texImage->TexFormat);
918 bool is_linear_astc = _mesa_is_astc_format(gl_format) &&
919 !_mesa_is_srgb_format(gl_format);
920 struct brw_context *brw = (struct brw_context*) ctx;
921 const struct gen_device_info *devinfo = &brw->screen->devinfo;
922 if (devinfo->gen == 9 && !gen_device_info_is_9lp(devinfo) && is_linear_astc)
923 flush_astc_denorms(ctx, dims, texImage,
924 xoffset, yoffset, zoffset,
925 width, height, depth);
926 }
927
928 void
929 intelInitTextureImageFuncs(struct dd_function_table *functions)
930 {
931 functions->TexImage = intelTexImage;
932 functions->TexSubImage = intelTexSubImage;
933 functions->CompressedTexSubImage = intelCompressedTexSubImage;
934 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
935 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
936 functions->GetTexSubImage = intel_get_tex_sub_image;
937 }