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