1f8b885bbec8230d51e98cbd533330d70d214bd4
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_image.c
1
2 #include "main/glheader.h"
3 #include "main/macros.h"
4 #include "main/mfeatures.h"
5 #include "main/mtypes.h"
6 #include "main/enums.h"
7 #include "main/bufferobj.h"
8 #include "main/context.h"
9 #include "main/formats.h"
10 #include "main/pbo.h"
11 #include "main/renderbuffer.h"
12 #include "main/texcompress.h"
13 #include "main/texstore.h"
14 #include "main/texgetimage.h"
15 #include "main/texobj.h"
16 #include "main/teximage.h"
17
18 #include "intel_context.h"
19 #include "intel_mipmap_tree.h"
20 #include "intel_buffer_objects.h"
21 #include "intel_batchbuffer.h"
22 #include "intel_tex.h"
23 #include "intel_blit.h"
24 #include "intel_fbo.h"
25 #include "intel_span.h"
26
27 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
28
29 /* Functions to store texture images. Where possible, mipmap_tree's
30 * will be created or further instantiated with image data, otherwise
31 * images will be stored in malloc'd memory. A validation step is
32 * required to pull those images into a mipmap tree, or otherwise
33 * decide a fallback is required.
34 */
35
36
37
38 /* Otherwise, store it in memory if (Border != 0) or (any dimension ==
39 * 1).
40 *
41 * Otherwise, if max_level >= level >= min_level, create tree with
42 * space for textures from min_level down to max_level.
43 *
44 * Otherwise, create tree with space for textures from (level
45 * 0)..(1x1). Consider pruning this tree at a validation if the
46 * saving is worth it.
47 */
48 static struct intel_mipmap_tree *
49 intel_miptree_create_for_teximage(struct intel_context *intel,
50 struct intel_texture_object *intelObj,
51 struct intel_texture_image *intelImage,
52 GLboolean expect_accelerated_upload)
53 {
54 GLuint firstLevel;
55 GLuint lastLevel;
56 GLuint width = intelImage->base.Width;
57 GLuint height = intelImage->base.Height;
58 GLuint depth = intelImage->base.Depth;
59 GLuint i;
60
61 DBG("%s\n", __FUNCTION__);
62
63 if (intelImage->base.Border)
64 return NULL;
65
66 if (intelImage->level > intelObj->base.BaseLevel &&
67 (intelImage->base.Width == 1 ||
68 (intelObj->base.Target != GL_TEXTURE_1D &&
69 intelImage->base.Height == 1) ||
70 (intelObj->base.Target == GL_TEXTURE_3D &&
71 intelImage->base.Depth == 1))) {
72 /* For this combination, we're at some lower mipmap level and
73 * some important dimension is 1. We can't extrapolate up to a
74 * likely base level width/height/depth for a full mipmap stack
75 * from this info, so just allocate this one level.
76 */
77 firstLevel = intelImage->level;
78 lastLevel = intelImage->level;
79 } else {
80 /* If this image disrespects BaseLevel, allocate from level zero.
81 * Usually BaseLevel == 0, so it's unlikely to happen.
82 */
83 if (intelImage->level < intelObj->base.BaseLevel)
84 firstLevel = 0;
85 else
86 firstLevel = intelObj->base.BaseLevel;
87
88 /* Figure out image dimensions at start level. */
89 for (i = intelImage->level; i > firstLevel; i--) {
90 width <<= 1;
91 if (height != 1)
92 height <<= 1;
93 if (depth != 1)
94 depth <<= 1;
95 }
96
97 /* Guess a reasonable value for lastLevel. This is probably going
98 * to be wrong fairly often and might mean that we have to look at
99 * resizable buffers, or require that buffers implement lazy
100 * pagetable arrangements.
101 */
102 if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
103 intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
104 intelImage->level == firstLevel &&
105 (intel->gen < 4 || firstLevel == 0)) {
106 lastLevel = firstLevel;
107 } else {
108 lastLevel = firstLevel + _mesa_logbase2(MAX2(MAX2(width, height), depth));
109 }
110 }
111
112 return intel_miptree_create(intel,
113 intelObj->base.Target,
114 intelImage->base.TexFormat,
115 firstLevel,
116 lastLevel,
117 width,
118 height,
119 depth,
120 expect_accelerated_upload);
121 }
122
123 /* There are actually quite a few combinations this will work for,
124 * more than what I've listed here.
125 */
126 static GLboolean
127 check_pbo_format(GLint internalFormat,
128 GLenum format, GLenum type,
129 gl_format mesa_format)
130 {
131 switch (internalFormat) {
132 case 4:
133 case GL_RGBA:
134 case GL_RGBA8:
135 return (format == GL_BGRA &&
136 (type == GL_UNSIGNED_BYTE ||
137 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
138 mesa_format == MESA_FORMAT_ARGB8888);
139 case 3:
140 case GL_RGB:
141 return (format == GL_RGB &&
142 type == GL_UNSIGNED_SHORT_5_6_5 &&
143 mesa_format == MESA_FORMAT_RGB565);
144 case 1:
145 case GL_LUMINANCE:
146 return (format == GL_LUMINANCE &&
147 type == GL_UNSIGNED_BYTE &&
148 mesa_format == MESA_FORMAT_L8);
149 case GL_YCBCR_MESA:
150 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
151 default:
152 return GL_FALSE;
153 }
154 }
155
156
157 /* XXX: Do this for TexSubImage also:
158 */
159 static GLboolean
160 try_pbo_upload(struct intel_context *intel,
161 struct intel_texture_image *intelImage,
162 const struct gl_pixelstore_attrib *unpack,
163 GLint internalFormat,
164 GLint width, GLint height,
165 GLenum format, GLenum type, const void *pixels)
166 {
167 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
168 GLuint src_offset, src_stride;
169 GLuint dst_x, dst_y, dst_stride;
170 drm_intel_bo *dst_buffer = intel_region_buffer(intel,
171 intelImage->mt->region,
172 INTEL_WRITE_FULL);
173
174 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
175 intel->ctx._ImageTransferState ||
176 unpack->SkipPixels || unpack->SkipRows) {
177 DBG("%s: failure 1\n", __FUNCTION__);
178 return GL_FALSE;
179 }
180
181 /* note: potential 64-bit ptr to 32-bit int cast */
182 src_offset = (GLuint) (unsigned long) pixels;
183
184 if (unpack->RowLength > 0)
185 src_stride = unpack->RowLength;
186 else
187 src_stride = width;
188
189 intel_miptree_get_image_offset(intelImage->mt, intelImage->level,
190 intelImage->face, 0,
191 &dst_x, &dst_y);
192
193 dst_stride = intelImage->mt->region->pitch;
194
195 if (drm_intel_bo_references(intel->batch.bo, dst_buffer))
196 intel_flush(&intel->ctx);
197
198 {
199 GLuint offset;
200 drm_intel_bo *src_buffer =
201 intel_bufferobj_source(intel, pbo, 64, &offset);
202
203 if (!intelEmitCopyBlit(intel,
204 intelImage->mt->cpp,
205 src_stride, src_buffer,
206 src_offset + offset, GL_FALSE,
207 dst_stride, dst_buffer, 0,
208 intelImage->mt->region->tiling,
209 0, 0, dst_x, dst_y, width, height,
210 GL_COPY)) {
211 return GL_FALSE;
212 }
213 }
214
215 return GL_TRUE;
216 }
217
218
219 static GLboolean
220 try_pbo_zcopy(struct intel_context *intel,
221 struct intel_texture_image *intelImage,
222 const struct gl_pixelstore_attrib *unpack,
223 GLint internalFormat,
224 GLint width, GLint height,
225 GLenum format, GLenum type, const void *pixels)
226 {
227 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
228 GLuint src_offset, src_stride;
229 GLuint dst_x, dst_y, dst_stride;
230
231 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
232 intel->ctx._ImageTransferState ||
233 unpack->SkipPixels || unpack->SkipRows) {
234 DBG("%s: failure 1\n", __FUNCTION__);
235 return GL_FALSE;
236 }
237
238 /* note: potential 64-bit ptr to 32-bit int cast */
239 src_offset = (GLuint) (unsigned long) pixels;
240
241 if (unpack->RowLength > 0)
242 src_stride = unpack->RowLength;
243 else
244 src_stride = width;
245
246 intel_miptree_get_image_offset(intelImage->mt, intelImage->level,
247 intelImage->face, 0,
248 &dst_x, &dst_y);
249
250 dst_stride = intelImage->mt->region->pitch;
251
252 if (src_stride != dst_stride || dst_x != 0 || dst_y != 0 ||
253 src_offset != 0) {
254 DBG("%s: failure 2\n", __FUNCTION__);
255 return GL_FALSE;
256 }
257
258 intel_region_attach_pbo(intel, intelImage->mt->region, pbo);
259
260 return GL_TRUE;
261 }
262
263 /**
264 * \param scatter Scatter if true. Gather if false.
265 *
266 * \see intel_tex_image_x8z24_scatter
267 * \see intel_tex_image_x8z24_gather
268 */
269 static void
270 intel_tex_image_s8z24_scattergather(struct intel_context *intel,
271 struct intel_texture_image *intel_image,
272 bool scatter)
273 {
274 struct gl_context *ctx = &intel->ctx;
275 struct gl_renderbuffer *depth_rb = intel_image->depth_rb;
276 struct gl_renderbuffer *stencil_rb = intel_image->stencil_rb;
277
278 int w = intel_image->base.Width;
279 int h = intel_image->base.Height;
280
281 uint32_t depth_row[w];
282 uint8_t stencil_row[w];
283
284 intel_renderbuffer_map(intel, depth_rb);
285 intel_renderbuffer_map(intel, stencil_rb);
286
287 if (scatter) {
288 for (int y = 0; y < h; ++y) {
289 depth_rb->GetRow(ctx, depth_rb, w, 0, y, depth_row);
290 for (int x = 0; x < w; ++x) {
291 stencil_row[x] = depth_row[x] >> 24;
292 }
293 stencil_rb->PutRow(ctx, stencil_rb, w, 0, y, stencil_row, NULL);
294 }
295 } else { /* gather */
296 for (int y = 0; y < h; ++y) {
297 depth_rb->GetRow(ctx, depth_rb, w, 0, y, depth_row);
298 stencil_rb->GetRow(ctx, stencil_rb, w, 0, y, stencil_row);
299 for (int x = 0; x < w; ++x) {
300 uint32_t s8_x24 = stencil_row[x] << 24;
301 uint32_t x8_z24 = depth_row[x] & 0x00ffffff;
302 depth_row[x] = s8_x24 | x8_z24;
303 }
304 depth_rb->PutRow(ctx, depth_rb, w, 0, y, depth_row, NULL);
305 }
306 }
307
308 intel_renderbuffer_unmap(intel, depth_rb);
309 intel_renderbuffer_unmap(intel, stencil_rb);
310 }
311
312 /**
313 * Copy the x8 bits from intel_image->depth_rb to intel_image->stencil_rb.
314 */
315 static void
316 intel_tex_image_s8z24_scatter(struct intel_context *intel,
317 struct intel_texture_image *intel_image)
318 {
319 intel_tex_image_s8z24_scattergather(intel, intel_image, true);
320 }
321
322 /**
323 * Copy the data in intel_image->stencil_rb to the x8 bits in
324 * intel_image->depth_rb.
325 */
326 static void
327 intel_tex_image_s8z24_gather(struct intel_context *intel,
328 struct intel_texture_image *intel_image)
329 {
330 intel_tex_image_s8z24_scattergather(intel, intel_image, false);
331 }
332
333 static bool
334 intel_tex_image_s8z24_create_renderbuffers(struct intel_context *intel,
335 struct intel_texture_image *image)
336 {
337 struct gl_context *ctx = &intel->ctx;
338
339 bool ok = true;
340 int width = image->base.Width;
341 int height = image->base.Height;
342 struct gl_renderbuffer *drb;
343 struct gl_renderbuffer *srb;
344 struct intel_renderbuffer *idrb;
345 struct intel_renderbuffer *isrb;
346
347 assert(intel->has_separate_stencil);
348 assert(image->base.TexFormat == MESA_FORMAT_S8_Z24);
349 assert(image->mt != NULL);
350
351 drb = intel_create_wrapped_renderbuffer(ctx, width, height,
352 MESA_FORMAT_X8_Z24);
353 srb = intel_create_wrapped_renderbuffer(ctx, width, height,
354 MESA_FORMAT_S8);
355
356 if (!drb || !srb) {
357 if (drb) {
358 drb->Delete(drb);
359 }
360 if (srb) {
361 srb->Delete(srb);
362 }
363 return false;
364 }
365
366 idrb = intel_renderbuffer(drb);
367 isrb = intel_renderbuffer(srb);
368
369 intel_region_reference(&idrb->region, image->mt->region);
370 ok = intel_alloc_renderbuffer_storage(ctx, srb, GL_STENCIL_INDEX8,
371 width, height);
372
373 if (!ok) {
374 drb->Delete(drb);
375 srb->Delete(srb);
376 return false;
377 }
378
379 intel_renderbuffer_set_draw_offset(idrb, image, 0);
380 intel_renderbuffer_set_draw_offset(isrb, image, 0);
381
382 _mesa_reference_renderbuffer(&image->depth_rb, drb);
383 _mesa_reference_renderbuffer(&image->stencil_rb, srb);
384
385 return true;
386 }
387
388 static void
389 intelTexImage(struct gl_context * ctx,
390 GLint dims,
391 GLenum target, GLint level,
392 GLint internalFormat,
393 GLint width, GLint height, GLint depth,
394 GLint border,
395 GLenum format, GLenum type, const void *pixels,
396 const struct gl_pixelstore_attrib *unpack,
397 struct gl_texture_object *texObj,
398 struct gl_texture_image *texImage, GLsizei imageSize,
399 GLboolean compressed)
400 {
401 struct intel_context *intel = intel_context(ctx);
402 struct intel_texture_object *intelObj = intel_texture_object(texObj);
403 struct intel_texture_image *intelImage = intel_texture_image(texImage);
404 GLint texelBytes, sizeInBytes;
405 GLuint dstRowStride = 0, srcRowStride = texImage->RowStride;
406
407 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
408 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
409
410 intelImage->face = _mesa_tex_target_to_face(target);
411 intelImage->level = level;
412
413 if (_mesa_is_format_compressed(texImage->TexFormat)) {
414 texelBytes = 0;
415 }
416 else {
417 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
418
419 if (!intelImage->mt) {
420 assert(texImage->RowStride == width);
421 }
422 }
423
424 ctx->Driver.FreeTexImageData(ctx, texImage);
425 assert(!intelImage->mt);
426
427 if (intelObj->mt &&
428 intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
429 /* Use an existing miptree when possible */
430 intel_miptree_reference(&intelImage->mt, intelObj->mt);
431 assert(intelImage->mt);
432 } else if (intelImage->base.Border == 0) {
433 /* Didn't fit in the object miptree, but it's suitable for inclusion in
434 * a miptree, so create one just for our level and store it in the image.
435 * It'll get moved into the object miptree at validate time.
436 */
437 intelImage->mt = intel_miptree_create_for_teximage(intel, intelObj,
438 intelImage,
439 pixels == NULL);
440
441 /* Even if the object currently has a mipmap tree associated
442 * with it, this one is a more likely candidate to represent the
443 * whole object since our level didn't fit what was there
444 * before, and any lower levels would fit into our miptree.
445 */
446 if (intelImage->mt) {
447 intel_miptree_release(intel, &intelObj->mt);
448 intel_miptree_reference(&intelObj->mt, intelImage->mt);
449 }
450 }
451
452 /* PBO fastpaths:
453 */
454 if (dims <= 2 &&
455 intelImage->mt &&
456 _mesa_is_bufferobj(unpack->BufferObj) &&
457 check_pbo_format(internalFormat, format,
458 type, intelImage->base.TexFormat)) {
459
460 DBG("trying pbo upload\n");
461
462 /* Attempt to texture directly from PBO data (zero copy upload).
463 *
464 * Currently disable as it can lead to worse as well as better
465 * performance (in particular when intel_region_cow() is
466 * required).
467 */
468 if (intelObj->mt == intelImage->mt &&
469 intelObj->mt->first_level == level &&
470 intelObj->mt->last_level == level) {
471
472 if (try_pbo_zcopy(intel, intelImage, unpack,
473 internalFormat,
474 width, height, format, type, pixels)) {
475
476 DBG("pbo zcopy upload succeeded\n");
477 return;
478 }
479 }
480
481
482 /* Otherwise, attempt to use the blitter for PBO image uploads.
483 */
484 if (try_pbo_upload(intel, intelImage, unpack,
485 internalFormat,
486 width, height, format, type, pixels)) {
487 DBG("pbo upload succeeded\n");
488 return;
489 }
490
491 DBG("pbo upload failed\n");
492 }
493
494 /* intelCopyTexImage calls this function with pixels == NULL, with
495 * the expectation that the mipmap tree will be set up but nothing
496 * more will be done. This is where those calls return:
497 */
498 if (compressed) {
499 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
500 unpack,
501 "glCompressedTexImage");
502 } else {
503 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
504 format, type,
505 pixels, unpack, "glTexImage");
506 }
507
508 if (intelImage->mt) {
509 if (pixels != NULL) {
510 /* Flush any queued rendering with the texture before mapping. */
511 if (drm_intel_bo_references(intel->batch.bo,
512 intelImage->mt->region->buffer)) {
513 intel_flush(ctx);
514 }
515 texImage->Data = intel_miptree_image_map(intel,
516 intelImage->mt,
517 intelImage->face,
518 intelImage->level,
519 &dstRowStride,
520 intelImage->base.ImageOffsets);
521 }
522
523 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
524 }
525 else {
526 /* Allocate regular memory and store the image there temporarily. */
527 if (_mesa_is_format_compressed(texImage->TexFormat)) {
528 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
529 texImage->Width,
530 texImage->Height,
531 texImage->Depth);
532 dstRowStride =
533 _mesa_format_row_stride(texImage->TexFormat, width);
534 assert(dims != 3);
535 }
536 else {
537 dstRowStride = width * texelBytes;
538 sizeInBytes = depth * dstRowStride * height;
539 }
540
541 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
542 }
543
544 DBG("Upload image %dx%dx%d row_len %d "
545 "pitch %d pixels %d compressed %d\n",
546 width, height, depth, width * texelBytes, dstRowStride,
547 pixels ? 1 : 0, compressed);
548
549 /* Copy data. Would like to know when it's ok for us to eg. use
550 * the blitter to copy. Or, use the hardware to do the format
551 * conversion and copy:
552 */
553 if (pixels) {
554 if (compressed) {
555 if (intelImage->mt) {
556 struct intel_region *dst = intelImage->mt->region;
557 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
558 0, 0,
559 intelImage->mt->level[level].width,
560 (intelImage->mt->level[level].height+3)/4,
561 pixels,
562 srcRowStride,
563 0, 0);
564 }
565 else {
566 memcpy(texImage->Data, pixels, imageSize);
567 }
568 }
569 else if (!_mesa_texstore(ctx, dims,
570 texImage->_BaseFormat,
571 texImage->TexFormat,
572 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
573 dstRowStride,
574 texImage->ImageOffsets,
575 width, height, depth,
576 format, type, pixels, unpack)) {
577 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
578 }
579 }
580
581 _mesa_unmap_teximage_pbo(ctx, unpack);
582
583 if (intel->must_use_separate_stencil
584 && texImage->TexFormat == MESA_FORMAT_S8_Z24) {
585 intel_tex_image_s8z24_create_renderbuffers(intel, intelImage);
586 intel_tex_image_s8z24_scatter(intel, intelImage);
587 }
588
589 if (intelImage->mt) {
590 if (pixels != NULL)
591 intel_miptree_image_unmap(intel, intelImage->mt);
592 texImage->Data = NULL;
593 }
594 }
595
596
597 static void
598 intelTexImage3D(struct gl_context * ctx,
599 GLenum target, GLint level,
600 GLint internalFormat,
601 GLint width, GLint height, GLint depth,
602 GLint border,
603 GLenum format, GLenum type, const void *pixels,
604 const struct gl_pixelstore_attrib *unpack,
605 struct gl_texture_object *texObj,
606 struct gl_texture_image *texImage)
607 {
608 intelTexImage(ctx, 3, target, level,
609 internalFormat, width, height, depth, border,
610 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
611 }
612
613
614 static void
615 intelTexImage2D(struct gl_context * ctx,
616 GLenum target, GLint level,
617 GLint internalFormat,
618 GLint width, GLint height, GLint border,
619 GLenum format, GLenum type, const void *pixels,
620 const struct gl_pixelstore_attrib *unpack,
621 struct gl_texture_object *texObj,
622 struct gl_texture_image *texImage)
623 {
624 intelTexImage(ctx, 2, target, level,
625 internalFormat, width, height, 1, border,
626 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
627 }
628
629
630 static void
631 intelTexImage1D(struct gl_context * ctx,
632 GLenum target, GLint level,
633 GLint internalFormat,
634 GLint width, GLint border,
635 GLenum format, GLenum type, const void *pixels,
636 const struct gl_pixelstore_attrib *unpack,
637 struct gl_texture_object *texObj,
638 struct gl_texture_image *texImage)
639 {
640 intelTexImage(ctx, 1, target, level,
641 internalFormat, width, 1, 1, border,
642 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
643 }
644
645
646 static void
647 intelCompressedTexImage2D( struct gl_context *ctx, GLenum target, GLint level,
648 GLint internalFormat,
649 GLint width, GLint height, GLint border,
650 GLsizei imageSize, const GLvoid *data,
651 struct gl_texture_object *texObj,
652 struct gl_texture_image *texImage )
653 {
654 intelTexImage(ctx, 2, target, level,
655 internalFormat, width, height, 1, border,
656 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
657 }
658
659
660 /**
661 * Need to map texture image into memory before copying image data,
662 * then unmap it.
663 */
664 static void
665 intel_get_tex_image(struct gl_context * ctx, GLenum target, GLint level,
666 GLenum format, GLenum type, GLvoid * pixels,
667 struct gl_texture_object *texObj,
668 struct gl_texture_image *texImage, GLboolean compressed)
669 {
670 struct intel_context *intel = intel_context(ctx);
671 struct intel_texture_image *intelImage = intel_texture_image(texImage);
672
673 /* If we're reading from a texture that has been rendered to, need to
674 * make sure rendering is complete.
675 * We could probably predicate this on texObj->_RenderToTexture
676 */
677 intel_flush(ctx);
678
679 /* Map */
680 if (intelImage->mt) {
681 /* Image is stored in hardware format in a buffer managed by the
682 * kernel. Need to explicitly map and unmap it.
683 */
684 intelImage->base.Data =
685 intel_miptree_image_map(intel,
686 intelImage->mt,
687 intelImage->face,
688 intelImage->level,
689 &intelImage->base.RowStride,
690 intelImage->base.ImageOffsets);
691 intelImage->base.RowStride /= intelImage->mt->cpp;
692 }
693 else {
694 /* Otherwise, the image should actually be stored in
695 * intelImage->base.Data. This is pretty confusing for
696 * everybody, I'd much prefer to separate the two functions of
697 * texImage->Data - storage for texture images in main memory
698 * and access (ie mappings) of images. In other words, we'd
699 * create a new texImage->Map field and leave Data simply for
700 * storage.
701 */
702 assert(intelImage->base.Data);
703 }
704
705 if (intelImage->stencil_rb) {
706 /*
707 * The texture has packed depth/stencil format, but uses separate
708 * stencil. The texture's embedded stencil buffer contains the real
709 * stencil data, so copy that into the miptree.
710 */
711 intel_tex_image_s8z24_gather(intel, intelImage);
712 }
713
714 if (compressed) {
715 _mesa_get_compressed_teximage(ctx, target, level, pixels,
716 texObj, texImage);
717 }
718 else {
719 _mesa_get_teximage(ctx, target, level, format, type, pixels,
720 texObj, texImage);
721 }
722
723
724 /* Unmap */
725 if (intelImage->mt) {
726 intel_miptree_image_unmap(intel, intelImage->mt);
727 intelImage->base.Data = NULL;
728 }
729 }
730
731
732 static void
733 intelGetTexImage(struct gl_context * ctx, GLenum target, GLint level,
734 GLenum format, GLenum type, GLvoid * pixels,
735 struct gl_texture_object *texObj,
736 struct gl_texture_image *texImage)
737 {
738 intel_get_tex_image(ctx, target, level, format, type, pixels,
739 texObj, texImage, GL_FALSE);
740 }
741
742
743 static void
744 intelGetCompressedTexImage(struct gl_context *ctx, GLenum target, GLint level,
745 GLvoid *pixels,
746 struct gl_texture_object *texObj,
747 struct gl_texture_image *texImage)
748 {
749 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
750 texObj, texImage, GL_TRUE);
751 }
752
753 void
754 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
755 GLint texture_format,
756 __DRIdrawable *dPriv)
757 {
758 struct gl_framebuffer *fb = dPriv->driverPrivate;
759 struct intel_context *intel = pDRICtx->driverPrivate;
760 struct gl_context *ctx = &intel->ctx;
761 struct intel_texture_object *intelObj;
762 struct intel_texture_image *intelImage;
763 struct intel_mipmap_tree *mt;
764 struct intel_renderbuffer *rb;
765 struct gl_texture_object *texObj;
766 struct gl_texture_image *texImage;
767 int level = 0, internalFormat;
768 gl_format texFormat;
769
770 texObj = _mesa_get_current_tex_object(ctx, target);
771 intelObj = intel_texture_object(texObj);
772
773 if (!intelObj)
774 return;
775
776 if (dPriv->lastStamp != dPriv->dri2.stamp ||
777 !pDRICtx->driScreenPriv->dri2.useInvalidate)
778 intel_update_renderbuffers(pDRICtx, dPriv);
779
780 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
781 /* If the region isn't set, then intel_update_renderbuffers was unable
782 * to get the buffers for the drawable.
783 */
784 if (rb->region == NULL)
785 return;
786
787 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
788 internalFormat = GL_RGB;
789 texFormat = MESA_FORMAT_XRGB8888;
790 }
791 else {
792 internalFormat = GL_RGBA;
793 texFormat = MESA_FORMAT_ARGB8888;
794 }
795
796 mt = intel_miptree_create_for_region(intel, target, texFormat,
797 rb->region, 1);
798 if (mt == NULL)
799 return;
800
801 _mesa_lock_texture(&intel->ctx, texObj);
802
803 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
804 intelImage = intel_texture_image(texImage);
805
806 if (intelImage->mt) {
807 intel_miptree_release(intel, &intelImage->mt);
808 assert(!texImage->Data);
809 }
810 if (intelObj->mt)
811 intel_miptree_release(intel, &intelObj->mt);
812
813 intelObj->mt = mt;
814
815 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
816 rb->region->width, rb->region->height, 1,
817 0, internalFormat, texFormat);
818
819 intelImage->face = _mesa_tex_target_to_face(target);
820 intelImage->level = level;
821 texImage->RowStride = rb->region->pitch;
822 intel_miptree_reference(&intelImage->mt, intelObj->mt);
823
824 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
825 fprintf(stderr, "miptree doesn't match image\n");
826 }
827
828 _mesa_unlock_texture(&intel->ctx, texObj);
829 }
830
831 void
832 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
833 {
834 /* The old interface didn't have the format argument, so copy our
835 * implementation's behavior at the time.
836 */
837 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
838 }
839
840 #if FEATURE_OES_EGL_image
841 static void
842 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
843 struct gl_texture_object *texObj,
844 struct gl_texture_image *texImage,
845 GLeglImageOES image_handle)
846 {
847 struct intel_context *intel = intel_context(ctx);
848 struct intel_texture_object *intelObj = intel_texture_object(texObj);
849 struct intel_texture_image *intelImage = intel_texture_image(texImage);
850 struct intel_mipmap_tree *mt;
851 __DRIscreen *screen;
852 __DRIimage *image;
853
854 screen = intel->intelScreen->driScrnPriv;
855 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
856 screen->loaderPrivate);
857 if (image == NULL)
858 return;
859
860 mt = intel_miptree_create_for_region(intel, target, image->format,
861 image->region, 1);
862 if (mt == NULL)
863 return;
864
865 if (intelImage->mt) {
866 intel_miptree_release(intel, &intelImage->mt);
867 assert(!texImage->Data);
868 }
869 if (intelObj->mt)
870 intel_miptree_release(intel, &intelObj->mt);
871
872 intelObj->mt = mt;
873 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
874 image->region->width, image->region->height, 1,
875 0, image->internal_format, image->format);
876
877 intelImage->face = _mesa_tex_target_to_face(target);
878 intelImage->level = 0;
879 texImage->RowStride = image->region->pitch;
880 intel_miptree_reference(&intelImage->mt, intelObj->mt);
881
882 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base))
883 fprintf(stderr, "miptree doesn't match image\n");
884 }
885 #endif
886
887 void
888 intelInitTextureImageFuncs(struct dd_function_table *functions)
889 {
890 functions->TexImage1D = intelTexImage1D;
891 functions->TexImage2D = intelTexImage2D;
892 functions->TexImage3D = intelTexImage3D;
893 functions->GetTexImage = intelGetTexImage;
894
895 functions->CompressedTexImage2D = intelCompressedTexImage2D;
896 functions->GetCompressedTexImage = intelGetCompressedTexImage;
897
898 #if FEATURE_OES_EGL_image
899 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
900 #endif
901 }