i965: clip: Move hpos_offest and ndc_offset into local functions.
[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 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->base.Level,
190 intelImage->base.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->base.Level,
247 intelImage->base.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 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 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 if (_mesa_is_format_compressed(texImage->TexFormat)) {
411 texelBytes = 0;
412 }
413 else {
414 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
415
416 if (!intelImage->mt) {
417 assert(texImage->RowStride == width);
418 }
419 }
420
421 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
422 assert(!intelImage->mt);
423
424 if (intelObj->mt &&
425 intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
426 /* Use an existing miptree when possible */
427 intel_miptree_reference(&intelImage->mt, intelObj->mt);
428 assert(intelImage->mt);
429 } else if (intelImage->base.Border == 0) {
430 /* Didn't fit in the object miptree, but it's suitable for inclusion in
431 * a miptree, so create one just for our level and store it in the image.
432 * It'll get moved into the object miptree at validate time.
433 */
434 intelImage->mt = intel_miptree_create_for_teximage(intel, intelObj,
435 intelImage,
436 pixels == NULL);
437
438 /* Even if the object currently has a mipmap tree associated
439 * with it, this one is a more likely candidate to represent the
440 * whole object since our level didn't fit what was there
441 * before, and any lower levels would fit into our miptree.
442 */
443 if (intelImage->mt) {
444 intel_miptree_release(intel, &intelObj->mt);
445 intel_miptree_reference(&intelObj->mt, intelImage->mt);
446 }
447 }
448
449 /* PBO fastpaths:
450 */
451 if (dims <= 2 &&
452 intelImage->mt &&
453 _mesa_is_bufferobj(unpack->BufferObj) &&
454 check_pbo_format(internalFormat, format,
455 type, intelImage->base.TexFormat)) {
456
457 DBG("trying pbo upload\n");
458
459 /* Attempt to texture directly from PBO data (zero copy upload).
460 *
461 * Currently disable as it can lead to worse as well as better
462 * performance (in particular when intel_region_cow() is
463 * required).
464 */
465 if (intelObj->mt == intelImage->mt &&
466 intelObj->mt->first_level == level &&
467 intelObj->mt->last_level == level) {
468
469 if (try_pbo_zcopy(intel, intelImage, unpack,
470 internalFormat,
471 width, height, format, type, pixels)) {
472
473 DBG("pbo zcopy upload succeeded\n");
474 return;
475 }
476 }
477
478
479 /* Otherwise, attempt to use the blitter for PBO image uploads.
480 */
481 if (try_pbo_upload(intel, intelImage, unpack,
482 internalFormat,
483 width, height, format, type, pixels)) {
484 DBG("pbo upload succeeded\n");
485 return;
486 }
487
488 DBG("pbo upload failed\n");
489 }
490
491 /* intelCopyTexImage calls this function with pixels == NULL, with
492 * the expectation that the mipmap tree will be set up but nothing
493 * more will be done. This is where those calls return:
494 */
495 if (compressed) {
496 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
497 unpack,
498 "glCompressedTexImage");
499 } else {
500 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
501 format, type,
502 pixels, unpack, "glTexImage");
503 }
504
505 if (intelImage->mt) {
506 if (pixels != NULL) {
507 /* Flush any queued rendering with the texture before mapping. */
508 if (drm_intel_bo_references(intel->batch.bo,
509 intelImage->mt->region->buffer)) {
510 intel_flush(ctx);
511 }
512 texImage->Data = intel_miptree_image_map(intel,
513 intelImage->mt,
514 intelImage->base.Face,
515 intelImage->base.Level,
516 &dstRowStride,
517 intelImage->base.ImageOffsets);
518 }
519
520 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
521 }
522 else {
523 /* Allocate regular memory and store the image there temporarily. */
524 if (_mesa_is_format_compressed(texImage->TexFormat)) {
525 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
526 texImage->Width,
527 texImage->Height,
528 texImage->Depth);
529 dstRowStride =
530 _mesa_format_row_stride(texImage->TexFormat, width);
531 assert(dims != 3);
532 }
533 else {
534 dstRowStride = width * texelBytes;
535 sizeInBytes = depth * dstRowStride * height;
536 }
537
538 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
539 }
540
541 DBG("Upload image %dx%dx%d row_len %d "
542 "pitch %d pixels %d compressed %d\n",
543 width, height, depth, width * texelBytes, dstRowStride,
544 pixels ? 1 : 0, compressed);
545
546 /* Copy data. Would like to know when it's ok for us to eg. use
547 * the blitter to copy. Or, use the hardware to do the format
548 * conversion and copy:
549 */
550 if (pixels) {
551 if (compressed) {
552 if (intelImage->mt) {
553 struct intel_region *dst = intelImage->mt->region;
554 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
555 0, 0,
556 intelImage->mt->level[level].width,
557 (intelImage->mt->level[level].height+3)/4,
558 pixels,
559 srcRowStride,
560 0, 0);
561 }
562 else {
563 memcpy(texImage->Data, pixels, imageSize);
564 }
565 }
566 else if (!_mesa_texstore(ctx, dims,
567 texImage->_BaseFormat,
568 texImage->TexFormat,
569 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
570 dstRowStride,
571 texImage->ImageOffsets,
572 width, height, depth,
573 format, type, pixels, unpack)) {
574 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
575 }
576 }
577
578 _mesa_unmap_teximage_pbo(ctx, unpack);
579
580 if (intel->must_use_separate_stencil
581 && texImage->TexFormat == MESA_FORMAT_S8_Z24) {
582 intel_tex_image_s8z24_create_renderbuffers(intel, intelImage);
583 intel_tex_image_s8z24_scatter(intel, intelImage);
584 }
585
586 if (intelImage->mt) {
587 if (pixels != NULL)
588 intel_miptree_image_unmap(intel, intelImage->mt);
589 texImage->Data = NULL;
590 }
591 }
592
593
594 static void
595 intelTexImage3D(struct gl_context * ctx,
596 GLenum target, GLint level,
597 GLint internalFormat,
598 GLint width, GLint height, GLint depth,
599 GLint border,
600 GLenum format, GLenum type, const void *pixels,
601 const struct gl_pixelstore_attrib *unpack,
602 struct gl_texture_object *texObj,
603 struct gl_texture_image *texImage)
604 {
605 intelTexImage(ctx, 3, target, level,
606 internalFormat, width, height, depth, border,
607 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
608 }
609
610
611 static void
612 intelTexImage2D(struct gl_context * ctx,
613 GLenum target, GLint level,
614 GLint internalFormat,
615 GLint width, GLint height, GLint border,
616 GLenum format, GLenum type, const void *pixels,
617 const struct gl_pixelstore_attrib *unpack,
618 struct gl_texture_object *texObj,
619 struct gl_texture_image *texImage)
620 {
621 intelTexImage(ctx, 2, target, level,
622 internalFormat, width, height, 1, border,
623 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
624 }
625
626
627 static void
628 intelTexImage1D(struct gl_context * ctx,
629 GLenum target, GLint level,
630 GLint internalFormat,
631 GLint width, GLint border,
632 GLenum format, GLenum type, const void *pixels,
633 const struct gl_pixelstore_attrib *unpack,
634 struct gl_texture_object *texObj,
635 struct gl_texture_image *texImage)
636 {
637 intelTexImage(ctx, 1, target, level,
638 internalFormat, width, 1, 1, border,
639 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
640 }
641
642
643 static void
644 intelCompressedTexImage2D( struct gl_context *ctx, GLenum target, GLint level,
645 GLint internalFormat,
646 GLint width, GLint height, GLint border,
647 GLsizei imageSize, const GLvoid *data,
648 struct gl_texture_object *texObj,
649 struct gl_texture_image *texImage )
650 {
651 intelTexImage(ctx, 2, target, level,
652 internalFormat, width, height, 1, border,
653 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
654 }
655
656
657 /**
658 * Need to map texture image into memory before copying image data,
659 * then unmap it.
660 */
661 static void
662 intel_get_tex_image(struct gl_context * ctx, GLenum target, GLint level,
663 GLenum format, GLenum type, GLvoid * pixels,
664 struct gl_texture_object *texObj,
665 struct gl_texture_image *texImage, GLboolean compressed)
666 {
667 struct intel_context *intel = intel_context(ctx);
668 struct intel_texture_image *intelImage = intel_texture_image(texImage);
669
670 /* If we're reading from a texture that has been rendered to, need to
671 * make sure rendering is complete.
672 * We could probably predicate this on texObj->_RenderToTexture
673 */
674 intel_flush(ctx);
675
676 /* Map */
677 if (intelImage->mt) {
678 /* Image is stored in hardware format in a buffer managed by the
679 * kernel. Need to explicitly map and unmap it.
680 */
681 intelImage->base.Data =
682 intel_miptree_image_map(intel,
683 intelImage->mt,
684 intelImage->base.Face,
685 intelImage->base.Level,
686 &intelImage->base.RowStride,
687 intelImage->base.ImageOffsets);
688 intelImage->base.RowStride /= intelImage->mt->cpp;
689 }
690 else {
691 /* Otherwise, the image should actually be stored in
692 * intelImage->base.Data. This is pretty confusing for
693 * everybody, I'd much prefer to separate the two functions of
694 * texImage->Data - storage for texture images in main memory
695 * and access (ie mappings) of images. In other words, we'd
696 * create a new texImage->Map field and leave Data simply for
697 * storage.
698 */
699 assert(intelImage->base.Data);
700 }
701
702 if (intelImage->stencil_rb) {
703 /*
704 * The texture has packed depth/stencil format, but uses separate
705 * stencil. The texture's embedded stencil buffer contains the real
706 * stencil data, so copy that into the miptree.
707 */
708 intel_tex_image_s8z24_gather(intel, intelImage);
709 }
710
711 if (compressed) {
712 _mesa_get_compressed_teximage(ctx, target, level, pixels,
713 texObj, texImage);
714 }
715 else {
716 _mesa_get_teximage(ctx, target, level, format, type, pixels,
717 texObj, texImage);
718 }
719
720
721 /* Unmap */
722 if (intelImage->mt) {
723 intel_miptree_image_unmap(intel, intelImage->mt);
724 intelImage->base.Data = NULL;
725 }
726 }
727
728
729 static void
730 intelGetTexImage(struct gl_context * ctx, GLenum target, GLint level,
731 GLenum format, GLenum type, GLvoid * pixels,
732 struct gl_texture_object *texObj,
733 struct gl_texture_image *texImage)
734 {
735 intel_get_tex_image(ctx, target, level, format, type, pixels,
736 texObj, texImage, GL_FALSE);
737 }
738
739
740 static void
741 intelGetCompressedTexImage(struct gl_context *ctx, GLenum target, GLint level,
742 GLvoid *pixels,
743 struct gl_texture_object *texObj,
744 struct gl_texture_image *texImage)
745 {
746 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
747 texObj, texImage, GL_TRUE);
748 }
749
750 void
751 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
752 GLint texture_format,
753 __DRIdrawable *dPriv)
754 {
755 struct gl_framebuffer *fb = dPriv->driverPrivate;
756 struct intel_context *intel = pDRICtx->driverPrivate;
757 struct gl_context *ctx = &intel->ctx;
758 struct intel_texture_object *intelObj;
759 struct intel_texture_image *intelImage;
760 struct intel_mipmap_tree *mt;
761 struct intel_renderbuffer *rb;
762 struct gl_texture_object *texObj;
763 struct gl_texture_image *texImage;
764 int level = 0, internalFormat;
765 gl_format texFormat;
766
767 texObj = _mesa_get_current_tex_object(ctx, target);
768 intelObj = intel_texture_object(texObj);
769
770 if (!intelObj)
771 return;
772
773 if (dPriv->lastStamp != dPriv->dri2.stamp ||
774 !pDRICtx->driScreenPriv->dri2.useInvalidate)
775 intel_update_renderbuffers(pDRICtx, dPriv);
776
777 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
778 /* If the region isn't set, then intel_update_renderbuffers was unable
779 * to get the buffers for the drawable.
780 */
781 if (rb->region == NULL)
782 return;
783
784 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
785 internalFormat = GL_RGB;
786 texFormat = MESA_FORMAT_XRGB8888;
787 }
788 else {
789 internalFormat = GL_RGBA;
790 texFormat = MESA_FORMAT_ARGB8888;
791 }
792
793 mt = intel_miptree_create_for_region(intel, target, texFormat,
794 rb->region, 1);
795 if (mt == NULL)
796 return;
797
798 _mesa_lock_texture(&intel->ctx, texObj);
799
800 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
801 intelImage = intel_texture_image(texImage);
802
803 if (intelImage->mt) {
804 intel_miptree_release(intel, &intelImage->mt);
805 assert(!texImage->Data);
806 }
807 if (intelObj->mt)
808 intel_miptree_release(intel, &intelObj->mt);
809
810 intelObj->mt = mt;
811
812 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
813 rb->region->width, rb->region->height, 1,
814 0, internalFormat, texFormat);
815
816 texImage->RowStride = rb->region->pitch;
817 intel_miptree_reference(&intelImage->mt, intelObj->mt);
818
819 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
820 fprintf(stderr, "miptree doesn't match image\n");
821 }
822
823 _mesa_unlock_texture(&intel->ctx, texObj);
824 }
825
826 void
827 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
828 {
829 /* The old interface didn't have the format argument, so copy our
830 * implementation's behavior at the time.
831 */
832 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
833 }
834
835 #if FEATURE_OES_EGL_image
836 static void
837 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
838 struct gl_texture_object *texObj,
839 struct gl_texture_image *texImage,
840 GLeglImageOES image_handle)
841 {
842 struct intel_context *intel = intel_context(ctx);
843 struct intel_texture_object *intelObj = intel_texture_object(texObj);
844 struct intel_texture_image *intelImage = intel_texture_image(texImage);
845 struct intel_mipmap_tree *mt;
846 __DRIscreen *screen;
847 __DRIimage *image;
848
849 screen = intel->intelScreen->driScrnPriv;
850 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
851 screen->loaderPrivate);
852 if (image == NULL)
853 return;
854
855 mt = intel_miptree_create_for_region(intel, target, image->format,
856 image->region, 1);
857 if (mt == NULL)
858 return;
859
860 if (intelImage->mt) {
861 intel_miptree_release(intel, &intelImage->mt);
862 assert(!texImage->Data);
863 }
864 if (intelObj->mt)
865 intel_miptree_release(intel, &intelObj->mt);
866
867 intelObj->mt = mt;
868 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
869 image->region->width, image->region->height, 1,
870 0, image->internal_format, image->format);
871
872 texImage->RowStride = image->region->pitch;
873 intel_miptree_reference(&intelImage->mt, intelObj->mt);
874
875 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base))
876 fprintf(stderr, "miptree doesn't match image\n");
877 }
878 #endif
879
880 void
881 intelInitTextureImageFuncs(struct dd_function_table *functions)
882 {
883 functions->TexImage1D = intelTexImage1D;
884 functions->TexImage2D = intelTexImage2D;
885 functions->TexImage3D = intelTexImage3D;
886 functions->GetTexImage = intelGetTexImage;
887
888 functions->CompressedTexImage2D = intelCompressedTexImage2D;
889 functions->GetCompressedTexImage = intelGetCompressedTexImage;
890
891 #if FEATURE_OES_EGL_image
892 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
893 #endif
894 }