i965: Don't clip everything if FRONT_AND_BACK culling while culling disabled.
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_image.c
1
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "main/glheader.h"
6 #include "main/macros.h"
7 #include "main/mtypes.h"
8 #include "main/enums.h"
9 #include "main/colortab.h"
10 #include "main/convolve.h"
11 #include "main/context.h"
12 #include "main/simple_list.h"
13 #include "main/texcompress.h"
14 #include "main/texformat.h"
15 #include "main/texgetimage.h"
16 #include "main/texobj.h"
17 #include "main/texstore.h"
18 #include "main/teximage.h"
19
20 #include "intel_context.h"
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_blit.h"
26 #include "intel_fbo.h"
27
28 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
29
30 /* Functions to store texture images. Where possible, mipmap_tree's
31 * will be created or further instantiated with image data, otherwise
32 * images will be stored in malloc'd memory. A validation step is
33 * required to pull those images into a mipmap tree, or otherwise
34 * decide a fallback is required.
35 */
36
37
38 static int
39 logbase2(int n)
40 {
41 GLint i = 1;
42 GLint log2 = 0;
43
44 while (n > i) {
45 i *= 2;
46 log2++;
47 }
48
49 return log2;
50 }
51
52
53 /* Otherwise, store it in memory if (Border != 0) or (any dimension ==
54 * 1).
55 *
56 * Otherwise, if max_level >= level >= min_level, create tree with
57 * space for textures from min_level down to max_level.
58 *
59 * Otherwise, create tree with space for textures from (level
60 * 0)..(1x1). Consider pruning this tree at a validation if the
61 * saving is worth it.
62 */
63 static void
64 guess_and_alloc_mipmap_tree(struct intel_context *intel,
65 struct intel_texture_object *intelObj,
66 struct intel_texture_image *intelImage,
67 GLboolean expect_accelerated_upload)
68 {
69 GLuint firstLevel;
70 GLuint lastLevel;
71 GLuint width = intelImage->base.Width;
72 GLuint height = intelImage->base.Height;
73 GLuint depth = intelImage->base.Depth;
74 GLuint l2width, l2height, l2depth;
75 GLuint i, comp_byte = 0;
76
77 DBG("%s\n", __FUNCTION__);
78
79 if (intelImage->base.Border ||
80 ((intelImage->base._BaseFormat == GL_DEPTH_COMPONENT) &&
81 ((intelObj->base.WrapS == GL_CLAMP_TO_BORDER) ||
82 (intelObj->base.WrapT == GL_CLAMP_TO_BORDER))))
83 return;
84
85 if (intelImage->level > intelObj->base.BaseLevel &&
86 (intelImage->base.Width == 1 ||
87 (intelObj->base.Target != GL_TEXTURE_1D &&
88 intelImage->base.Height == 1) ||
89 (intelObj->base.Target == GL_TEXTURE_3D &&
90 intelImage->base.Depth == 1)))
91 return;
92
93 /* If this image disrespects BaseLevel, allocate from level zero.
94 * Usually BaseLevel == 0, so it's unlikely to happen.
95 */
96 if (intelImage->level < intelObj->base.BaseLevel)
97 firstLevel = 0;
98 else
99 firstLevel = intelObj->base.BaseLevel;
100
101
102 /* Figure out image dimensions at start level.
103 */
104 for (i = intelImage->level; i > firstLevel; i--) {
105 width <<= 1;
106 if (height != 1)
107 height <<= 1;
108 if (depth != 1)
109 depth <<= 1;
110 }
111
112 /* Guess a reasonable value for lastLevel. This is probably going
113 * to be wrong fairly often and might mean that we have to look at
114 * resizable buffers, or require that buffers implement lazy
115 * pagetable arrangements.
116 */
117 if ((intelObj->base.MinFilter == GL_NEAREST ||
118 intelObj->base.MinFilter == GL_LINEAR) &&
119 intelImage->level == firstLevel) {
120 lastLevel = firstLevel;
121 }
122 else {
123 l2width = logbase2(width);
124 l2height = logbase2(height);
125 l2depth = logbase2(depth);
126 lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
127 }
128
129 assert(!intelObj->mt);
130 if (intelImage->base.IsCompressed)
131 comp_byte = intel_compressed_num_bytes(intelImage->base.TexFormat->MesaFormat);
132 intelObj->mt = intel_miptree_create(intel,
133 intelObj->base.Target,
134 intelImage->base.InternalFormat,
135 firstLevel,
136 lastLevel,
137 width,
138 height,
139 depth,
140 intelImage->base.TexFormat->TexelBytes,
141 comp_byte,
142 expect_accelerated_upload);
143
144 DBG("%s - success\n", __FUNCTION__);
145 }
146
147
148
149
150 static GLuint
151 target_to_face(GLenum target)
152 {
153 switch (target) {
154 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
155 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
156 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
157 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
158 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
159 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
160 return ((GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
161 default:
162 return 0;
163 }
164 }
165
166 /* There are actually quite a few combinations this will work for,
167 * more than what I've listed here.
168 */
169 static GLboolean
170 check_pbo_format(GLint internalFormat,
171 GLenum format, GLenum type,
172 const struct gl_texture_format *mesa_format)
173 {
174 switch (internalFormat) {
175 case 4:
176 case GL_RGBA:
177 return (format == GL_BGRA &&
178 (type == GL_UNSIGNED_BYTE ||
179 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
180 mesa_format == &_mesa_texformat_argb8888);
181 case 3:
182 case GL_RGB:
183 return (format == GL_RGB &&
184 type == GL_UNSIGNED_SHORT_5_6_5 &&
185 mesa_format == &_mesa_texformat_rgb565);
186 case GL_YCBCR_MESA:
187 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
188 default:
189 return GL_FALSE;
190 }
191 }
192
193
194 /* XXX: Do this for TexSubImage also:
195 */
196 static GLboolean
197 try_pbo_upload(struct intel_context *intel,
198 struct intel_texture_image *intelImage,
199 const struct gl_pixelstore_attrib *unpack,
200 GLint internalFormat,
201 GLint width, GLint height,
202 GLenum format, GLenum type, const void *pixels)
203 {
204 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
205 GLuint src_offset, src_stride;
206 GLuint dst_offset, dst_stride;
207
208 if (!pbo ||
209 intel->ctx._ImageTransferState ||
210 unpack->SkipPixels || unpack->SkipRows) {
211 DBG("%s: failure 1\n", __FUNCTION__);
212 return GL_FALSE;
213 }
214
215 /* note: potential 64-bit ptr to 32-bit int cast */
216 src_offset = (GLuint) (unsigned long) pixels;
217
218 if (unpack->RowLength > 0)
219 src_stride = unpack->RowLength;
220 else
221 src_stride = width;
222
223 dst_offset = intel_miptree_image_offset(intelImage->mt,
224 intelImage->face,
225 intelImage->level);
226
227 dst_stride = intelImage->mt->pitch;
228
229 intelFlush(&intel->ctx);
230 LOCK_HARDWARE(intel);
231 {
232 dri_bo *src_buffer = intel_bufferobj_buffer(intel, pbo, INTEL_READ);
233 dri_bo *dst_buffer = intel_region_buffer(intel,
234 intelImage->mt->region,
235 INTEL_WRITE_FULL);
236
237
238 intelEmitCopyBlit(intel,
239 intelImage->mt->cpp,
240 src_stride, src_buffer, src_offset, GL_FALSE,
241 dst_stride, dst_buffer, dst_offset, GL_FALSE,
242 0, 0, 0, 0, width, height,
243 GL_COPY);
244 }
245 UNLOCK_HARDWARE(intel);
246
247 return GL_TRUE;
248 }
249
250
251
252 static GLboolean
253 try_pbo_zcopy(struct intel_context *intel,
254 struct intel_texture_image *intelImage,
255 const struct gl_pixelstore_attrib *unpack,
256 GLint internalFormat,
257 GLint width, GLint height,
258 GLenum format, GLenum type, const void *pixels)
259 {
260 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
261 GLuint src_offset, src_stride;
262 GLuint dst_offset, dst_stride;
263
264 if (!pbo ||
265 intel->ctx._ImageTransferState ||
266 unpack->SkipPixels || unpack->SkipRows) {
267 DBG("%s: failure 1\n", __FUNCTION__);
268 return GL_FALSE;
269 }
270
271 /* note: potential 64-bit ptr to 32-bit int cast */
272 src_offset = (GLuint) (unsigned long) pixels;
273
274 if (unpack->RowLength > 0)
275 src_stride = unpack->RowLength;
276 else
277 src_stride = width;
278
279 dst_offset = intel_miptree_image_offset(intelImage->mt,
280 intelImage->face,
281 intelImage->level);
282
283 dst_stride = intelImage->mt->pitch;
284
285 if (src_stride != dst_stride || dst_offset != 0 || src_offset != 0) {
286 DBG("%s: failure 2\n", __FUNCTION__);
287 return GL_FALSE;
288 }
289
290 intel_region_attach_pbo(intel, intelImage->mt->region, pbo);
291
292 return GL_TRUE;
293 }
294
295
296
297
298
299
300 static void
301 intelTexImage(GLcontext * ctx,
302 GLint dims,
303 GLenum target, GLint level,
304 GLint internalFormat,
305 GLint width, GLint height, GLint depth,
306 GLint border,
307 GLenum format, GLenum type, const void *pixels,
308 const struct gl_pixelstore_attrib *unpack,
309 struct gl_texture_object *texObj,
310 struct gl_texture_image *texImage, GLsizei imageSize, int compressed)
311 {
312 struct intel_context *intel = intel_context(ctx);
313 struct intel_texture_object *intelObj = intel_texture_object(texObj);
314 struct intel_texture_image *intelImage = intel_texture_image(texImage);
315 GLint postConvWidth = width;
316 GLint postConvHeight = height;
317 GLint texelBytes, sizeInBytes;
318 GLuint dstRowStride = 0, srcRowStride = texImage->RowStride;
319 GLboolean needs_map;
320
321 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
322 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
323
324 intelFlush(ctx);
325
326 intelImage->face = target_to_face(target);
327 intelImage->level = level;
328
329 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
330 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
331 &postConvHeight);
332 }
333
334 /* choose the texture format */
335 texImage->TexFormat = intelChooseTextureFormat(ctx, internalFormat,
336 format, type);
337
338 _mesa_set_fetch_functions(texImage, dims);
339
340 if (texImage->TexFormat->TexelBytes == 0) {
341 /* must be a compressed format */
342 texelBytes = 0;
343 texImage->IsCompressed = GL_TRUE;
344 texImage->CompressedSize =
345 ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
346 texImage->Height, texImage->Depth,
347 texImage->TexFormat->MesaFormat);
348 } else {
349 texelBytes = texImage->TexFormat->TexelBytes;
350
351 /* Minimum pitch of 32 bytes */
352 if (postConvWidth * texelBytes < 32) {
353 postConvWidth = 32 / texelBytes;
354 texImage->RowStride = postConvWidth;
355 }
356
357 if (!intelImage->mt) {
358 assert(texImage->RowStride == postConvWidth);
359 }
360 }
361
362 /* Release the reference to a potentially orphaned buffer.
363 * Release any old malloced memory.
364 */
365 if (intelImage->mt) {
366 intel_miptree_release(intel, &intelImage->mt);
367 assert(!texImage->Data);
368 }
369 else if (texImage->Data) {
370 _mesa_free_texmemory(texImage->Data);
371 texImage->Data = NULL;
372 }
373
374 /* If this is the only texture image in the tree, could call
375 * bmBufferData with NULL data to free the old block and avoid
376 * waiting on any outstanding fences.
377 */
378 if (intelObj->mt &&
379 intelObj->mt->first_level == level &&
380 intelObj->mt->last_level == level &&
381 intelObj->mt->target != GL_TEXTURE_CUBE_MAP_ARB &&
382 !intel_miptree_match_image(intelObj->mt, &intelImage->base,
383 intelImage->face, intelImage->level)) {
384
385 DBG("release it\n");
386 intel_miptree_release(intel, &intelObj->mt);
387 assert(!intelObj->mt);
388 }
389
390 if (!intelObj->mt) {
391 guess_and_alloc_mipmap_tree(intel, intelObj, intelImage, pixels == NULL);
392 if (!intelObj->mt) {
393 DBG("guess_and_alloc_mipmap_tree: failed\n");
394 }
395 }
396
397 assert(!intelImage->mt);
398
399 if (intelObj->mt &&
400 intel_miptree_match_image(intelObj->mt, &intelImage->base,
401 intelImage->face, intelImage->level)) {
402
403 intel_miptree_reference(&intelImage->mt, intelObj->mt);
404 assert(intelImage->mt);
405 } else if (intelImage->base.Border == 0) {
406 int comp_byte = 0;
407
408 if (intelImage->base.IsCompressed) {
409 comp_byte =
410 intel_compressed_num_bytes(intelImage->base.TexFormat->MesaFormat);
411 }
412
413 /* Didn't fit in the object miptree, but it's suitable for inclusion in
414 * a miptree, so create one just for our level and store it in the image.
415 * It'll get moved into the object miptree at validate time.
416 */
417 intelImage->mt = intel_miptree_create(intel, target, internalFormat,
418 level, level,
419 width, height, depth,
420 intelImage->base.TexFormat->TexelBytes,
421 comp_byte, pixels == NULL);
422
423 }
424
425 /* PBO fastpaths:
426 */
427 if (dims <= 2 &&
428 intelImage->mt &&
429 intel_buffer_object(unpack->BufferObj) &&
430 check_pbo_format(internalFormat, format,
431 type, intelImage->base.TexFormat)) {
432
433 DBG("trying pbo upload\n");
434
435 /* Attempt to texture directly from PBO data (zero copy upload).
436 *
437 * Currently disable as it can lead to worse as well as better
438 * performance (in particular when intel_region_cow() is
439 * required).
440 */
441 if (intelObj->mt == intelImage->mt &&
442 intelObj->mt->first_level == level &&
443 intelObj->mt->last_level == level) {
444
445 if (try_pbo_zcopy(intel, intelImage, unpack,
446 internalFormat,
447 width, height, format, type, pixels)) {
448
449 DBG("pbo zcopy upload succeeded\n");
450 return;
451 }
452 }
453
454
455 /* Otherwise, attempt to use the blitter for PBO image uploads.
456 */
457 if (try_pbo_upload(intel, intelImage, unpack,
458 internalFormat,
459 width, height, format, type, pixels)) {
460 DBG("pbo upload succeeded\n");
461 return;
462 }
463
464 DBG("pbo upload failed\n");
465 }
466
467
468
469 /* intelCopyTexImage calls this function with pixels == NULL, with
470 * the expectation that the mipmap tree will be set up but nothing
471 * more will be done. This is where those calls return:
472 */
473 if (compressed) {
474 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
475 unpack,
476 "glCompressedTexImage");
477 } else {
478 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
479 format, type,
480 pixels, unpack, "glTexImage");
481 }
482
483 LOCK_HARDWARE(intel);
484
485 /* Two cases where we need a mapping of the miptree: when the user supplied
486 * data is mapped as well (non-PBO, memcpy upload) or when we're going to do
487 * (software) mipmap generation.
488 */
489 needs_map = (pixels != NULL) || (level == texObj->BaseLevel &&
490 texObj->GenerateMipmap);
491
492 if (intelImage->mt) {
493 if (needs_map)
494 texImage->Data = intel_miptree_image_map(intel,
495 intelImage->mt,
496 intelImage->face,
497 intelImage->level,
498 &dstRowStride,
499 intelImage->base.ImageOffsets);
500 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
501 }
502 else {
503 /* Allocate regular memory and store the image there temporarily. */
504 if (texImage->IsCompressed) {
505 sizeInBytes = texImage->CompressedSize;
506 dstRowStride =
507 _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
508 assert(dims != 3);
509 }
510 else {
511 dstRowStride = postConvWidth * texelBytes;
512 sizeInBytes = depth * dstRowStride * postConvHeight;
513 }
514
515 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
516 }
517
518 DBG("Upload image %dx%dx%d row_len %d "
519 "pitch %d pixels %d compressed %d\n",
520 width, height, depth, width * texelBytes, dstRowStride,
521 pixels ? 1 : 0, compressed);
522
523 /* Copy data. Would like to know when it's ok for us to eg. use
524 * the blitter to copy. Or, use the hardware to do the format
525 * conversion and copy:
526 */
527 if (pixels) {
528 if (compressed) {
529 if (intelImage->mt) {
530 struct intel_region *dst = intelImage->mt->region;
531 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
532 0, 0,
533 intelImage->mt->level[level].width,
534 (intelImage->mt->level[level].height+3)/4,
535 pixels,
536 srcRowStride,
537 0, 0);
538 } else
539 memcpy(texImage->Data, pixels, imageSize);
540 } else if (!texImage->TexFormat->StoreImage(ctx, dims,
541 texImage->_BaseFormat,
542 texImage->TexFormat,
543 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
544 dstRowStride,
545 texImage->ImageOffsets,
546 width, height, depth,
547 format, type, pixels, unpack)) {
548 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
549 }
550
551 /* GL_SGIS_generate_mipmap */
552 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
553 intel_generate_mipmap(ctx, target, texObj);
554 }
555 }
556
557 _mesa_unmap_teximage_pbo(ctx, unpack);
558
559 if (intelImage->mt) {
560 if (needs_map)
561 intel_miptree_image_unmap(intel, intelImage->mt);
562 texImage->Data = NULL;
563 }
564
565 UNLOCK_HARDWARE(intel);
566 }
567
568 void
569 intelTexImage3D(GLcontext * ctx,
570 GLenum target, GLint level,
571 GLint internalFormat,
572 GLint width, GLint height, GLint depth,
573 GLint border,
574 GLenum format, GLenum type, const void *pixels,
575 const struct gl_pixelstore_attrib *unpack,
576 struct gl_texture_object *texObj,
577 struct gl_texture_image *texImage)
578 {
579 intelTexImage(ctx, 3, target, level,
580 internalFormat, width, height, depth, border,
581 format, type, pixels, unpack, texObj, texImage, 0, 0);
582 }
583
584
585 void
586 intelTexImage2D(GLcontext * ctx,
587 GLenum target, GLint level,
588 GLint internalFormat,
589 GLint width, GLint height, GLint border,
590 GLenum format, GLenum type, const void *pixels,
591 const struct gl_pixelstore_attrib *unpack,
592 struct gl_texture_object *texObj,
593 struct gl_texture_image *texImage)
594 {
595 intelTexImage(ctx, 2, target, level,
596 internalFormat, width, height, 1, border,
597 format, type, pixels, unpack, texObj, texImage, 0, 0);
598 }
599
600 void
601 intelTexImage1D(GLcontext * ctx,
602 GLenum target, GLint level,
603 GLint internalFormat,
604 GLint width, GLint border,
605 GLenum format, GLenum type, const void *pixels,
606 const struct gl_pixelstore_attrib *unpack,
607 struct gl_texture_object *texObj,
608 struct gl_texture_image *texImage)
609 {
610 intelTexImage(ctx, 1, target, level,
611 internalFormat, width, 1, 1, border,
612 format, type, pixels, unpack, texObj, texImage, 0, 0);
613 }
614
615 void intelCompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
616 GLint internalFormat,
617 GLint width, GLint height, GLint border,
618 GLsizei imageSize, const GLvoid *data,
619 struct gl_texture_object *texObj,
620 struct gl_texture_image *texImage )
621 {
622 intelTexImage(ctx, 2, target, level,
623 internalFormat, width, height, 1, border,
624 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, 1);
625 }
626
627 /**
628 * Need to map texture image into memory before copying image data,
629 * then unmap it.
630 */
631 static void
632 intel_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
633 GLenum format, GLenum type, GLvoid * pixels,
634 struct gl_texture_object *texObj,
635 struct gl_texture_image *texImage, int compressed)
636 {
637 struct intel_context *intel = intel_context(ctx);
638 struct intel_texture_image *intelImage = intel_texture_image(texImage);
639
640 /* If we're reading from a texture that has been rendered to, need to
641 * make sure rendering is complete.
642 * We could probably predicate this on texObj->_RenderToTexture
643 */
644 intelFlush(ctx);
645
646 /* Map */
647 if (intelImage->mt) {
648 /* Image is stored in hardware format in a buffer managed by the
649 * kernel. Need to explicitly map and unmap it.
650 */
651 intelImage->base.Data =
652 intel_miptree_image_map(intel,
653 intelImage->mt,
654 intelImage->face,
655 intelImage->level,
656 &intelImage->base.RowStride,
657 intelImage->base.ImageOffsets);
658 intelImage->base.RowStride /= intelImage->mt->cpp;
659 }
660 else {
661 /* Otherwise, the image should actually be stored in
662 * intelImage->base.Data. This is pretty confusing for
663 * everybody, I'd much prefer to separate the two functions of
664 * texImage->Data - storage for texture images in main memory
665 * and access (ie mappings) of images. In other words, we'd
666 * create a new texImage->Map field and leave Data simply for
667 * storage.
668 */
669 assert(intelImage->base.Data);
670 }
671
672
673 if (compressed) {
674 _mesa_get_compressed_teximage(ctx, target, level, pixels,
675 texObj, texImage);
676 } else {
677 _mesa_get_teximage(ctx, target, level, format, type, pixels,
678 texObj, texImage);
679 }
680
681
682 /* Unmap */
683 if (intelImage->mt) {
684 intel_miptree_image_unmap(intel, intelImage->mt);
685 intelImage->base.Data = NULL;
686 }
687 }
688
689 void
690 intelGetTexImage(GLcontext * ctx, GLenum target, GLint level,
691 GLenum format, GLenum type, GLvoid * pixels,
692 struct gl_texture_object *texObj,
693 struct gl_texture_image *texImage)
694 {
695 intel_get_tex_image(ctx, target, level, format, type, pixels,
696 texObj, texImage, 0);
697
698
699 }
700
701 void
702 intelGetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
703 GLvoid *pixels,
704 struct gl_texture_object *texObj,
705 struct gl_texture_image *texImage)
706 {
707 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
708 texObj, texImage, 1);
709 }
710
711 void
712 intelSetTexOffset(__DRIcontext *pDRICtx, GLint texname,
713 unsigned long long offset, GLint depth, GLuint pitch)
714 {
715 struct intel_context *intel = pDRICtx->driverPrivate;
716 struct gl_texture_object *tObj = _mesa_lookup_texture(&intel->ctx, texname);
717 struct intel_texture_object *intelObj = intel_texture_object(tObj);
718
719 if (!intelObj)
720 return;
721
722 if (intelObj->mt)
723 intel_miptree_release(intel, &intelObj->mt);
724
725 intelObj->imageOverride = GL_TRUE;
726 intelObj->depthOverride = depth;
727 intelObj->pitchOverride = pitch;
728
729 if (offset)
730 intelObj->textureOffset = offset;
731 }
732
733 void
734 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
735 GLint glx_texture_format,
736 __DRIdrawable *dPriv)
737 {
738 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
739 struct intel_context *intel = pDRICtx->driverPrivate;
740 struct intel_texture_object *intelObj;
741 struct intel_texture_image *intelImage;
742 struct intel_mipmap_tree *mt;
743 struct intel_renderbuffer *rb;
744 struct gl_texture_unit *texUnit;
745 struct gl_texture_object *texObj;
746 struct gl_texture_image *texImage;
747 int level = 0, type, format, internalFormat;
748
749 texUnit = &intel->ctx.Texture.Unit[intel->ctx.Texture.CurrentUnit];
750 texObj = _mesa_select_tex_object(&intel->ctx, texUnit, target);
751 intelObj = intel_texture_object(texObj);
752
753 if (!intelObj)
754 return;
755
756 intel_update_renderbuffers(pDRICtx, dPriv);
757
758 rb = intel_fb->color_rb[0];
759 /* If the region isn't set, then intel_update_renderbuffers was unable
760 * to get the buffers for the drawable.
761 */
762 if (rb->region == NULL)
763 return;
764
765 type = GL_BGRA;
766 format = GL_UNSIGNED_BYTE;
767 if (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT)
768 internalFormat = GL_RGB;
769 else
770 internalFormat = GL_RGBA;
771
772 mt = intel_miptree_create_for_region(intel, target,
773 internalFormat,
774 0, 0, rb->region, 1, 0);
775 if (mt == NULL)
776 return;
777
778 _mesa_lock_texture(&intel->ctx, texObj);
779
780 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
781 intelImage = intel_texture_image(texImage);
782
783 if (intelImage->mt) {
784 intel_miptree_release(intel, &intelImage->mt);
785 assert(!texImage->Data);
786 }
787 if (intelObj->mt)
788 intel_miptree_release(intel, &intelObj->mt);
789
790 intelObj->mt = mt;
791 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
792 rb->region->width, rb->region->height, 1,
793 0, internalFormat);
794
795 intelImage->face = target_to_face(target);
796 intelImage->level = level;
797 texImage->TexFormat = intelChooseTextureFormat(&intel->ctx, internalFormat,
798 type, format);
799 _mesa_set_fetch_functions(texImage, 2);
800 texImage->RowStride = rb->region->pitch;
801 intel_miptree_reference(&intelImage->mt, intelObj->mt);
802
803 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base,
804 intelImage->face, intelImage->level)) {
805 fprintf(stderr, "miptree doesn't match image\n");
806 }
807
808 _mesa_unlock_texture(&intel->ctx, texObj);
809 }
810
811 void
812 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
813 {
814 /* The old interface didn't have the format argument, so copy our
815 * implementation's behavior at the time.
816 */
817 intelSetTexBuffer2(pDRICtx, target, GLX_TEXTURE_FORMAT_RGBA_EXT, dPriv);
818 }