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