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