mesa/colormac: introduce inline helper for 4 unclamped float to ubyte.
[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->base.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->base.Level;
78 lastLevel = intelImage->base.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->base.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->base.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->base.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 width, GLint height, const void *pixels)
164 {
165 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
166 GLuint src_offset, src_stride;
167 GLuint dst_x, dst_y, dst_stride;
168 drm_intel_bo *dst_buffer = intel_region_buffer(intel,
169 intelImage->mt->region,
170 INTEL_WRITE_FULL);
171
172 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
173 intel->ctx._ImageTransferState ||
174 unpack->SkipPixels || unpack->SkipRows) {
175 DBG("%s: failure 1\n", __FUNCTION__);
176 return GL_FALSE;
177 }
178
179 /* note: potential 64-bit ptr to 32-bit int cast */
180 src_offset = (GLuint) (unsigned long) pixels;
181
182 if (unpack->RowLength > 0)
183 src_stride = unpack->RowLength;
184 else
185 src_stride = width;
186
187 intel_miptree_get_image_offset(intelImage->mt, intelImage->base.Level,
188 intelImage->base.Face, 0,
189 &dst_x, &dst_y);
190
191 dst_stride = intelImage->mt->region->pitch;
192
193 if (drm_intel_bo_references(intel->batch.bo, dst_buffer))
194 intel_flush(&intel->ctx);
195
196 {
197 GLuint offset;
198 drm_intel_bo *src_buffer =
199 intel_bufferobj_source(intel, pbo, 64, &offset);
200
201 if (!intelEmitCopyBlit(intel,
202 intelImage->mt->cpp,
203 src_stride, src_buffer,
204 src_offset + offset, GL_FALSE,
205 dst_stride, dst_buffer, 0,
206 intelImage->mt->region->tiling,
207 0, 0, dst_x, dst_y, width, height,
208 GL_COPY)) {
209 return GL_FALSE;
210 }
211 }
212
213 return GL_TRUE;
214 }
215
216
217 static GLboolean
218 try_pbo_zcopy(struct intel_context *intel,
219 struct intel_texture_image *intelImage,
220 const struct gl_pixelstore_attrib *unpack,
221 GLint width, const void *pixels)
222 {
223 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
224 GLuint src_offset, src_stride;
225 GLuint dst_x, dst_y, dst_stride;
226
227 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
228 intel->ctx._ImageTransferState ||
229 unpack->SkipPixels || unpack->SkipRows) {
230 DBG("%s: failure 1\n", __FUNCTION__);
231 return GL_FALSE;
232 }
233
234 /* note: potential 64-bit ptr to 32-bit int cast */
235 src_offset = (GLuint) (unsigned long) pixels;
236
237 if (unpack->RowLength > 0)
238 src_stride = unpack->RowLength;
239 else
240 src_stride = width;
241
242 intel_miptree_get_image_offset(intelImage->mt, intelImage->base.Level,
243 intelImage->base.Face, 0,
244 &dst_x, &dst_y);
245
246 dst_stride = intelImage->mt->region->pitch;
247
248 if (src_stride != dst_stride || dst_x != 0 || dst_y != 0 ||
249 src_offset != 0) {
250 DBG("%s: failure 2\n", __FUNCTION__);
251 return GL_FALSE;
252 }
253
254 intel_region_attach_pbo(intel, intelImage->mt->region, pbo);
255
256 return GL_TRUE;
257 }
258
259 /**
260 * \param scatter Scatter if true. Gather if false.
261 *
262 * \see intel_tex_image_x8z24_scatter
263 * \see intel_tex_image_x8z24_gather
264 */
265 static void
266 intel_tex_image_s8z24_scattergather(struct intel_context *intel,
267 struct intel_texture_image *intel_image,
268 bool scatter)
269 {
270 struct gl_context *ctx = &intel->ctx;
271 struct gl_renderbuffer *depth_rb = intel_image->depth_rb;
272 struct gl_renderbuffer *stencil_rb = intel_image->stencil_rb;
273
274 int w = intel_image->base.Width;
275 int h = intel_image->base.Height;
276
277 uint32_t depth_row[w];
278 uint8_t stencil_row[w];
279
280 intel_renderbuffer_map(intel, depth_rb);
281 intel_renderbuffer_map(intel, stencil_rb);
282
283 if (scatter) {
284 for (int y = 0; y < h; ++y) {
285 depth_rb->GetRow(ctx, depth_rb, w, 0, y, depth_row);
286 for (int x = 0; x < w; ++x) {
287 stencil_row[x] = depth_row[x] >> 24;
288 }
289 stencil_rb->PutRow(ctx, stencil_rb, w, 0, y, stencil_row, NULL);
290 }
291 } else { /* gather */
292 for (int y = 0; y < h; ++y) {
293 depth_rb->GetRow(ctx, depth_rb, w, 0, y, depth_row);
294 stencil_rb->GetRow(ctx, stencil_rb, w, 0, y, stencil_row);
295 for (int x = 0; x < w; ++x) {
296 uint32_t s8_x24 = stencil_row[x] << 24;
297 uint32_t x8_z24 = depth_row[x] & 0x00ffffff;
298 depth_row[x] = s8_x24 | x8_z24;
299 }
300 depth_rb->PutRow(ctx, depth_rb, w, 0, y, depth_row, NULL);
301 }
302 }
303
304 intel_renderbuffer_unmap(intel, depth_rb);
305 intel_renderbuffer_unmap(intel, stencil_rb);
306 }
307
308 /**
309 * Copy the x8 bits from intel_image->depth_rb to intel_image->stencil_rb.
310 */
311 void
312 intel_tex_image_s8z24_scatter(struct intel_context *intel,
313 struct intel_texture_image *intel_image)
314 {
315 intel_tex_image_s8z24_scattergather(intel, intel_image, true);
316 }
317
318 /**
319 * Copy the data in intel_image->stencil_rb to the x8 bits in
320 * intel_image->depth_rb.
321 */
322 void
323 intel_tex_image_s8z24_gather(struct intel_context *intel,
324 struct intel_texture_image *intel_image)
325 {
326 intel_tex_image_s8z24_scattergather(intel, intel_image, false);
327 }
328
329 static bool
330 intel_tex_image_s8z24_create_renderbuffers(struct intel_context *intel,
331 struct intel_texture_image *image)
332 {
333 struct gl_context *ctx = &intel->ctx;
334
335 bool ok = true;
336 int width = image->base.Width;
337 int height = image->base.Height;
338 struct gl_renderbuffer *drb;
339 struct gl_renderbuffer *srb;
340 struct intel_renderbuffer *idrb;
341 struct intel_renderbuffer *isrb;
342
343 assert(intel->has_separate_stencil);
344 assert(image->base.TexFormat == MESA_FORMAT_S8_Z24);
345 assert(image->mt != NULL);
346
347 drb = intel_create_wrapped_renderbuffer(ctx, width, height,
348 MESA_FORMAT_X8_Z24);
349 srb = intel_create_wrapped_renderbuffer(ctx, width, height,
350 MESA_FORMAT_S8);
351
352 if (!drb || !srb) {
353 if (drb) {
354 drb->Delete(drb);
355 }
356 if (srb) {
357 srb->Delete(srb);
358 }
359 return false;
360 }
361
362 idrb = intel_renderbuffer(drb);
363 isrb = intel_renderbuffer(srb);
364
365 intel_region_reference(&idrb->region, image->mt->region);
366 ok = intel_alloc_renderbuffer_storage(ctx, srb, GL_STENCIL_INDEX8,
367 width, height);
368
369 if (!ok) {
370 drb->Delete(drb);
371 srb->Delete(srb);
372 return false;
373 }
374
375 intel_renderbuffer_set_draw_offset(idrb, image, 0);
376 intel_renderbuffer_set_draw_offset(isrb, image, 0);
377
378 _mesa_reference_renderbuffer(&image->depth_rb, drb);
379 _mesa_reference_renderbuffer(&image->stencil_rb, srb);
380
381 return true;
382 }
383
384 static void
385 intelTexImage(struct gl_context * ctx,
386 GLint dims,
387 GLenum target, GLint level,
388 GLint internalFormat,
389 GLint width, GLint height, GLint depth,
390 GLint border,
391 GLenum format, GLenum type, const void *pixels,
392 const struct gl_pixelstore_attrib *unpack,
393 struct gl_texture_object *texObj,
394 struct gl_texture_image *texImage, GLsizei imageSize,
395 GLboolean compressed)
396 {
397 struct intel_context *intel = intel_context(ctx);
398 struct intel_texture_object *intelObj = intel_texture_object(texObj);
399 struct intel_texture_image *intelImage = intel_texture_image(texImage);
400 GLint texelBytes, sizeInBytes;
401 GLuint dstRowStride = 0, srcRowStride = texImage->RowStride;
402
403 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
404 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
405
406 if (_mesa_is_format_compressed(texImage->TexFormat)) {
407 texelBytes = 0;
408 }
409 else {
410 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
411
412 if (!intelImage->mt) {
413 assert(texImage->RowStride == width);
414 }
415 }
416
417 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
418 assert(!intelImage->mt);
419
420 if (intelObj->mt &&
421 intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
422 /* Use an existing miptree when possible */
423 intel_miptree_reference(&intelImage->mt, intelObj->mt);
424 assert(intelImage->mt);
425 } else if (intelImage->base.Border == 0) {
426 /* Didn't fit in the object miptree, but it's suitable for inclusion in
427 * a miptree, so create one just for our level and store it in the image.
428 * It'll get moved into the object miptree at validate time.
429 */
430 intelImage->mt = intel_miptree_create_for_teximage(intel, intelObj,
431 intelImage,
432 pixels == NULL);
433
434 /* Even if the object currently has a mipmap tree associated
435 * with it, this one is a more likely candidate to represent the
436 * whole object since our level didn't fit what was there
437 * before, and any lower levels would fit into our miptree.
438 */
439 if (intelImage->mt) {
440 intel_miptree_release(intel, &intelObj->mt);
441 intel_miptree_reference(&intelObj->mt, intelImage->mt);
442 }
443 }
444
445 /* PBO fastpaths:
446 */
447 if (dims <= 2 &&
448 intelImage->mt &&
449 _mesa_is_bufferobj(unpack->BufferObj) &&
450 check_pbo_format(internalFormat, format,
451 type, intelImage->base.TexFormat)) {
452
453 DBG("trying pbo upload\n");
454
455 /* Attempt to texture directly from PBO data (zero copy upload).
456 *
457 * Currently disable as it can lead to worse as well as better
458 * performance (in particular when intel_region_cow() is
459 * required).
460 */
461 if (intelObj->mt == intelImage->mt &&
462 intelObj->mt->first_level == level &&
463 intelObj->mt->last_level == level) {
464
465 if (try_pbo_zcopy(intel, intelImage, unpack, width, pixels)) {
466 DBG("pbo zcopy upload succeeded\n");
467 return;
468 }
469 }
470
471
472 /* Otherwise, attempt to use the blitter for PBO image uploads.
473 */
474 if (try_pbo_upload(intel, intelImage, unpack, width, height, pixels)) {
475 DBG("pbo upload succeeded\n");
476 return;
477 }
478
479 DBG("pbo upload failed\n");
480 }
481
482 /* intelCopyTexImage calls this function with pixels == NULL, with
483 * the expectation that the mipmap tree will be set up but nothing
484 * more will be done. This is where those calls return:
485 */
486 if (compressed) {
487 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
488 unpack,
489 "glCompressedTexImage");
490 } else {
491 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
492 format, type,
493 pixels, unpack, "glTexImage");
494 }
495
496 if (intelImage->mt) {
497 if (pixels != NULL) {
498 /* Flush any queued rendering with the texture before mapping. */
499 if (drm_intel_bo_references(intel->batch.bo,
500 intelImage->mt->region->buffer)) {
501 intel_flush(ctx);
502 }
503 texImage->Data = intel_miptree_image_map(intel,
504 intelImage->mt,
505 intelImage->base.Face,
506 intelImage->base.Level,
507 &dstRowStride,
508 intelImage->base.ImageOffsets);
509 }
510
511 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
512 }
513 else {
514 /* Allocate regular memory and store the image there temporarily. */
515 if (_mesa_is_format_compressed(texImage->TexFormat)) {
516 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
517 texImage->Width,
518 texImage->Height,
519 texImage->Depth);
520 dstRowStride =
521 _mesa_format_row_stride(texImage->TexFormat, width);
522 assert(dims != 3);
523 }
524 else {
525 dstRowStride = width * texelBytes;
526 sizeInBytes = depth * dstRowStride * height;
527 }
528
529 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
530 }
531
532 DBG("Upload image %dx%dx%d row_len %d "
533 "pitch %d pixels %d compressed %d\n",
534 width, height, depth, width * texelBytes, dstRowStride,
535 pixels ? 1 : 0, compressed);
536
537 /* Copy data. Would like to know when it's ok for us to eg. use
538 * the blitter to copy. Or, use the hardware to do the format
539 * conversion and copy:
540 */
541 if (pixels) {
542 if (compressed) {
543 if (intelImage->mt) {
544 struct intel_region *dst = intelImage->mt->region;
545 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
546 0, 0,
547 intelImage->mt->level[level].width,
548 (intelImage->mt->level[level].height+3)/4,
549 pixels,
550 srcRowStride,
551 0, 0);
552 }
553 else {
554 memcpy(texImage->Data, pixels, imageSize);
555 }
556 }
557 else if (!_mesa_texstore(ctx, dims,
558 texImage->_BaseFormat,
559 texImage->TexFormat,
560 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
561 dstRowStride,
562 texImage->ImageOffsets,
563 width, height, depth,
564 format, type, pixels, unpack)) {
565 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
566 }
567 }
568
569 _mesa_unmap_teximage_pbo(ctx, unpack);
570
571 if (intel->must_use_separate_stencil
572 && texImage->TexFormat == MESA_FORMAT_S8_Z24) {
573 intel_tex_image_s8z24_create_renderbuffers(intel, intelImage);
574 intel_tex_image_s8z24_scatter(intel, intelImage);
575 }
576
577 if (intelImage->mt) {
578 if (pixels != NULL)
579 intel_miptree_image_unmap(intel, intelImage->mt);
580 texImage->Data = NULL;
581 }
582 }
583
584
585 static void
586 intelTexImage3D(struct gl_context * ctx,
587 GLenum target, GLint level,
588 GLint internalFormat,
589 GLint width, GLint height, GLint depth,
590 GLint border,
591 GLenum format, GLenum type, const void *pixels,
592 const struct gl_pixelstore_attrib *unpack,
593 struct gl_texture_object *texObj,
594 struct gl_texture_image *texImage)
595 {
596 intelTexImage(ctx, 3, target, level,
597 internalFormat, width, height, depth, border,
598 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
599 }
600
601
602 static void
603 intelTexImage2D(struct gl_context * ctx,
604 GLenum target, GLint level,
605 GLint internalFormat,
606 GLint width, GLint height, GLint border,
607 GLenum format, GLenum type, const void *pixels,
608 const struct gl_pixelstore_attrib *unpack,
609 struct gl_texture_object *texObj,
610 struct gl_texture_image *texImage)
611 {
612 intelTexImage(ctx, 2, target, level,
613 internalFormat, width, height, 1, border,
614 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
615 }
616
617
618 static void
619 intelTexImage1D(struct gl_context * ctx,
620 GLenum target, GLint level,
621 GLint internalFormat,
622 GLint width, GLint border,
623 GLenum format, GLenum type, const void *pixels,
624 const struct gl_pixelstore_attrib *unpack,
625 struct gl_texture_object *texObj,
626 struct gl_texture_image *texImage)
627 {
628 intelTexImage(ctx, 1, target, level,
629 internalFormat, width, 1, 1, border,
630 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
631 }
632
633
634 static void
635 intelCompressedTexImage2D( struct gl_context *ctx, GLenum target, GLint level,
636 GLint internalFormat,
637 GLint width, GLint height, GLint border,
638 GLsizei imageSize, const GLvoid *data,
639 struct gl_texture_object *texObj,
640 struct gl_texture_image *texImage )
641 {
642 intelTexImage(ctx, 2, target, level,
643 internalFormat, width, height, 1, border,
644 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
645 }
646
647
648 /**
649 * Need to map texture image into memory before copying image data,
650 * then unmap it.
651 */
652 static void
653 intel_get_tex_image(struct gl_context * ctx, GLenum target, GLint level,
654 GLenum format, GLenum type, GLvoid * pixels,
655 struct gl_texture_object *texObj,
656 struct gl_texture_image *texImage, GLboolean compressed)
657 {
658 struct intel_context *intel = intel_context(ctx);
659 struct intel_texture_image *intelImage = intel_texture_image(texImage);
660
661 /* If we're reading from a texture that has been rendered to, need to
662 * make sure rendering is complete.
663 * We could probably predicate this on texObj->_RenderToTexture
664 */
665 intel_flush(ctx);
666
667 /* Map */
668 if (intelImage->mt) {
669 /* Image is stored in hardware format in a buffer managed by the
670 * kernel. Need to explicitly map and unmap it.
671 */
672 intelImage->base.Data =
673 intel_miptree_image_map(intel,
674 intelImage->mt,
675 intelImage->base.Face,
676 intelImage->base.Level,
677 &intelImage->base.RowStride,
678 intelImage->base.ImageOffsets);
679 intelImage->base.RowStride /= intelImage->mt->cpp;
680 }
681 else {
682 /* Otherwise, the image should actually be stored in
683 * intelImage->base.Data. This is pretty confusing for
684 * everybody, I'd much prefer to separate the two functions of
685 * texImage->Data - storage for texture images in main memory
686 * and access (ie mappings) of images. In other words, we'd
687 * create a new texImage->Map field and leave Data simply for
688 * storage.
689 */
690 assert(intelImage->base.Data);
691 }
692
693 if (intelImage->stencil_rb) {
694 /*
695 * The texture has packed depth/stencil format, but uses separate
696 * stencil. The texture's embedded stencil buffer contains the real
697 * stencil data, so copy that into the miptree.
698 */
699 intel_tex_image_s8z24_gather(intel, intelImage);
700 }
701
702 if (compressed) {
703 _mesa_get_compressed_teximage(ctx, target, level, pixels,
704 texObj, texImage);
705 }
706 else {
707 _mesa_get_teximage(ctx, target, level, format, type, pixels,
708 texObj, texImage);
709 }
710
711
712 /* Unmap */
713 if (intelImage->mt) {
714 intel_miptree_image_unmap(intel, intelImage->mt);
715 intelImage->base.Data = NULL;
716 }
717 }
718
719
720 static void
721 intelGetTexImage(struct gl_context * ctx, GLenum target, GLint level,
722 GLenum format, GLenum type, GLvoid * pixels,
723 struct gl_texture_object *texObj,
724 struct gl_texture_image *texImage)
725 {
726 intel_get_tex_image(ctx, target, level, format, type, pixels,
727 texObj, texImage, GL_FALSE);
728 }
729
730
731 static void
732 intelGetCompressedTexImage(struct gl_context *ctx, GLenum target, GLint level,
733 GLvoid *pixels,
734 struct gl_texture_object *texObj,
735 struct gl_texture_image *texImage)
736 {
737 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
738 texObj, texImage, GL_TRUE);
739 }
740
741 void
742 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
743 GLint texture_format,
744 __DRIdrawable *dPriv)
745 {
746 struct gl_framebuffer *fb = dPriv->driverPrivate;
747 struct intel_context *intel = pDRICtx->driverPrivate;
748 struct gl_context *ctx = &intel->ctx;
749 struct intel_texture_object *intelObj;
750 struct intel_texture_image *intelImage;
751 struct intel_mipmap_tree *mt;
752 struct intel_renderbuffer *rb;
753 struct gl_texture_object *texObj;
754 struct gl_texture_image *texImage;
755 int level = 0, internalFormat;
756 gl_format texFormat;
757
758 texObj = _mesa_get_current_tex_object(ctx, target);
759 intelObj = intel_texture_object(texObj);
760
761 if (!intelObj)
762 return;
763
764 if (dPriv->lastStamp != dPriv->dri2.stamp ||
765 !pDRICtx->driScreenPriv->dri2.useInvalidate)
766 intel_update_renderbuffers(pDRICtx, dPriv);
767
768 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
769 /* If the region isn't set, then intel_update_renderbuffers was unable
770 * to get the buffers for the drawable.
771 */
772 if (rb->region == NULL)
773 return;
774
775 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
776 internalFormat = GL_RGB;
777 texFormat = MESA_FORMAT_XRGB8888;
778 }
779 else {
780 internalFormat = GL_RGBA;
781 texFormat = MESA_FORMAT_ARGB8888;
782 }
783
784 mt = intel_miptree_create_for_region(intel, target, texFormat, rb->region);
785 if (mt == NULL)
786 return;
787
788 _mesa_lock_texture(&intel->ctx, texObj);
789
790 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
791 intelImage = intel_texture_image(texImage);
792
793 if (intelImage->mt) {
794 intel_miptree_release(intel, &intelImage->mt);
795 assert(!texImage->Data);
796 }
797 if (intelObj->mt)
798 intel_miptree_release(intel, &intelObj->mt);
799
800 intelObj->mt = mt;
801
802 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
803 rb->region->width, rb->region->height, 1,
804 0, internalFormat, texFormat);
805
806 texImage->RowStride = rb->region->pitch;
807 intel_miptree_reference(&intelImage->mt, intelObj->mt);
808
809 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
810 fprintf(stderr, "miptree doesn't match image\n");
811 }
812
813 _mesa_unlock_texture(&intel->ctx, texObj);
814 }
815
816 void
817 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
818 {
819 /* The old interface didn't have the format argument, so copy our
820 * implementation's behavior at the time.
821 */
822 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
823 }
824
825 #if FEATURE_OES_EGL_image
826 static void
827 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
828 struct gl_texture_object *texObj,
829 struct gl_texture_image *texImage,
830 GLeglImageOES image_handle)
831 {
832 struct intel_context *intel = intel_context(ctx);
833 struct intel_texture_object *intelObj = intel_texture_object(texObj);
834 struct intel_texture_image *intelImage = intel_texture_image(texImage);
835 struct intel_mipmap_tree *mt;
836 __DRIscreen *screen;
837 __DRIimage *image;
838
839 screen = intel->intelScreen->driScrnPriv;
840 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
841 screen->loaderPrivate);
842 if (image == NULL)
843 return;
844
845 mt = intel_miptree_create_for_region(intel, target, image->format,
846 image->region);
847 if (mt == NULL)
848 return;
849
850 if (intelImage->mt) {
851 intel_miptree_release(intel, &intelImage->mt);
852 assert(!texImage->Data);
853 }
854 if (intelObj->mt)
855 intel_miptree_release(intel, &intelObj->mt);
856
857 intelObj->mt = mt;
858 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
859 image->region->width, image->region->height, 1,
860 0, image->internal_format, image->format);
861
862 texImage->RowStride = image->region->pitch;
863 intel_miptree_reference(&intelImage->mt, intelObj->mt);
864
865 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base))
866 fprintf(stderr, "miptree doesn't match image\n");
867 }
868 #endif
869
870 void
871 intelInitTextureImageFuncs(struct dd_function_table *functions)
872 {
873 functions->TexImage1D = intelTexImage1D;
874 functions->TexImage2D = intelTexImage2D;
875 functions->TexImage3D = intelTexImage3D;
876 functions->GetTexImage = intelGetTexImage;
877
878 functions->CompressedTexImage2D = intelCompressedTexImage2D;
879 functions->GetCompressedTexImage = intelGetCompressedTexImage;
880
881 #if FEATURE_OES_EGL_image
882 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
883 #endif
884 }