mesa/st: Fix vertex buffer leak
[mesa.git] / src / mesa / state_tracker / st_cb_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
38 #include "main/pixeltransfer.h"
39 #include "main/texcompress.h"
40 #include "main/texfetch.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
45
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_atom.h"
55
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "util/u_inlines.h"
59 #include "pipe/p_shader_tokens.h"
60 #include "util/u_tile.h"
61 #include "util/u_blit.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
67
68 #define DBG if (0) printf
69
70
71 static enum pipe_texture_target
72 gl_target_to_pipe(GLenum target)
73 {
74 switch (target) {
75 case GL_TEXTURE_1D:
76 return PIPE_TEXTURE_1D;
77 case GL_TEXTURE_2D:
78 return PIPE_TEXTURE_2D;
79 case GL_TEXTURE_RECTANGLE_NV:
80 return PIPE_TEXTURE_RECT;
81 case GL_TEXTURE_3D:
82 return PIPE_TEXTURE_3D;
83 case GL_TEXTURE_CUBE_MAP_ARB:
84 return PIPE_TEXTURE_CUBE;
85 case GL_TEXTURE_1D_ARRAY_EXT:
86 return PIPE_TEXTURE_1D_ARRAY;
87 case GL_TEXTURE_2D_ARRAY_EXT:
88 return PIPE_TEXTURE_2D_ARRAY;
89 default:
90 assert(0);
91 return 0;
92 }
93 }
94
95
96 /** called via ctx->Driver.NewTextureImage() */
97 static struct gl_texture_image *
98 st_NewTextureImage(struct gl_context * ctx)
99 {
100 DBG("%s\n", __FUNCTION__);
101 (void) ctx;
102 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
103 }
104
105
106 /** called via ctx->Driver.NewTextureObject() */
107 static struct gl_texture_object *
108 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
109 {
110 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
111
112 DBG("%s\n", __FUNCTION__);
113 _mesa_initialize_texture_object(&obj->base, name, target);
114
115 return &obj->base;
116 }
117
118 /** called via ctx->Driver.DeleteTextureObject() */
119 static void
120 st_DeleteTextureObject(struct gl_context *ctx,
121 struct gl_texture_object *texObj)
122 {
123 struct st_context *st = st_context(ctx);
124 struct st_texture_object *stObj = st_texture_object(texObj);
125 if (stObj->pt)
126 pipe_resource_reference(&stObj->pt, NULL);
127 if (stObj->sampler_view) {
128 if (stObj->sampler_view->context != st->pipe) {
129 /* Take "ownership" of this texture sampler view by setting
130 * its context pointer to this context. This avoids potential
131 * crashes when the texture object is shared among contexts
132 * and the original/owner context has already been destroyed.
133 */
134 stObj->sampler_view->context = st->pipe;
135 }
136 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
137 }
138 _mesa_delete_texture_object(ctx, texObj);
139 }
140
141
142 /** called via ctx->Driver.FreeTexImageData() */
143 static void
144 st_FreeTextureImageData(struct gl_context * ctx, struct gl_texture_image *texImage)
145 {
146 struct st_texture_image *stImage = st_texture_image(texImage);
147
148 DBG("%s\n", __FUNCTION__);
149
150 if (stImage->pt) {
151 pipe_resource_reference(&stImage->pt, NULL);
152 }
153
154 if (texImage->Data) {
155 _mesa_align_free(texImage->Data);
156 texImage->Data = NULL;
157 }
158 }
159
160
161 /**
162 * From linux kernel i386 header files, copes with odd sizes better
163 * than COPY_DWORDS would:
164 * XXX Put this in src/mesa/main/imports.h ???
165 */
166 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
167 static INLINE void *
168 __memcpy(void *to, const void *from, size_t n)
169 {
170 int d0, d1, d2;
171 __asm__ __volatile__("rep ; movsl\n\t"
172 "testb $2,%b4\n\t"
173 "je 1f\n\t"
174 "movsw\n"
175 "1:\ttestb $1,%b4\n\t"
176 "je 2f\n\t"
177 "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2)
178 :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from)
179 :"memory");
180 return (to);
181 }
182 #else
183 #define __memcpy(a,b,c) memcpy(a,b,c)
184 #endif
185
186
187 /**
188 * The system memcpy (at least on ubuntu 5.10) has problems copying
189 * to agp (writecombined) memory from a source which isn't 64-byte
190 * aligned - there is a 4x performance falloff.
191 *
192 * The x86 __memcpy is immune to this but is slightly slower
193 * (10%-ish) than the system memcpy.
194 *
195 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
196 * isn't much faster than x86_memcpy for agp copies.
197 *
198 * TODO: switch dynamically.
199 */
200 static void *
201 do_memcpy(void *dest, const void *src, size_t n)
202 {
203 if ((((unsigned long) src) & 63) || (((unsigned long) dest) & 63)) {
204 return __memcpy(dest, src, n);
205 }
206 else
207 return memcpy(dest, src, n);
208 }
209
210
211 /**
212 * Return default texture resource binding bitmask for the given format.
213 */
214 static GLuint
215 default_bindings(struct st_context *st, enum pipe_format format)
216 {
217 struct pipe_screen *screen = st->pipe->screen;
218 const unsigned target = PIPE_TEXTURE_2D;
219 const unsigned geom = 0x0;
220 unsigned bindings;
221
222 if (util_format_is_depth_or_stencil(format))
223 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
224 else
225 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
226
227 if (screen->is_format_supported(screen, format, target, 0, bindings, geom))
228 return bindings;
229 else
230 return PIPE_BIND_SAMPLER_VIEW;
231 }
232
233
234 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
235 static GLuint
236 get_texture_dims(GLenum target)
237 {
238 switch (target) {
239 case GL_TEXTURE_1D:
240 case GL_TEXTURE_1D_ARRAY_EXT:
241 return 1;
242 case GL_TEXTURE_2D:
243 case GL_TEXTURE_CUBE_MAP_ARB:
244 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
245 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
246 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
247 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
248 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
249 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
250 case GL_TEXTURE_RECTANGLE_NV:
251 case GL_TEXTURE_2D_ARRAY_EXT:
252 return 2;
253 case GL_TEXTURE_3D:
254 return 3;
255 default:
256 assert(0 && "invalid texture target in get_texture_dims()");
257 return 1;
258 }
259 }
260
261
262 /**
263 * Try to allocate a pipe_resource object for the given st_texture_object.
264 *
265 * We use the given st_texture_image as a clue to determine the size of the
266 * mipmap image at level=0.
267 *
268 * \return GL_TRUE for success, GL_FALSE if out of memory.
269 */
270 static GLboolean
271 guess_and_alloc_texture(struct st_context *st,
272 struct st_texture_object *stObj,
273 const struct st_texture_image *stImage)
274 {
275 const GLuint dims = get_texture_dims(stObj->base.Target);
276 GLuint level, lastLevel, width, height, depth;
277 GLuint bindings;
278 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
279 enum pipe_format fmt;
280
281 DBG("%s\n", __FUNCTION__);
282
283 assert(!stObj->pt);
284
285 level = stImage->level;
286 width = stImage->base.Width2; /* size w/out border */
287 height = stImage->base.Height2;
288 depth = stImage->base.Depth2;
289
290 assert(width > 0);
291 assert(height > 0);
292 assert(depth > 0);
293
294 /* Depending on the image's size, we can't always make a guess here.
295 */
296 if (level > 0) {
297 if ( (dims >= 1 && width == 1) ||
298 (dims >= 2 && height == 1) ||
299 (dims >= 3 && depth == 1) ) {
300 /* we can't determine the image size at level=0 */
301 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
302 /* this is not an out of memory error */
303 return GL_TRUE;
304 }
305 }
306
307 /* grow the image size until we hit level = 0 */
308 while (level > 0) {
309 if (width != 1)
310 width <<= 1;
311 if (height != 1)
312 height <<= 1;
313 if (depth != 1)
314 depth <<= 1;
315 level--;
316 }
317
318 assert(level == 0);
319
320 /* At this point, (width x height x depth) is the expected size of
321 * the level=0 mipmap image.
322 */
323
324 /* Guess a reasonable value for lastLevel. With OpenGL we have no
325 * idea how many mipmap levels will be in a texture until we start
326 * to render with it. Make an educated guess here but be prepared
327 * to re-allocating a texture buffer with space for more (or fewer)
328 * mipmap levels later.
329 */
330 if ((stObj->base.MinFilter == GL_NEAREST ||
331 stObj->base.MinFilter == GL_LINEAR ||
332 stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
333 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
334 !stObj->base.GenerateMipmap &&
335 stImage->level == 0) {
336 /* only alloc space for a single mipmap level */
337 lastLevel = 0;
338 }
339 else {
340 /* alloc space for a full mipmap */
341 GLuint l2width = util_logbase2(width);
342 GLuint l2height = util_logbase2(height);
343 GLuint l2depth = util_logbase2(depth);
344 lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
345 }
346
347 /* Save the level=0 dimensions */
348 stObj->width0 = width;
349 stObj->height0 = height;
350 stObj->depth0 = depth;
351
352 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
353
354 bindings = default_bindings(st, fmt);
355
356 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
357 width, height, depth,
358 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
359
360 stObj->pt = st_texture_create(st,
361 gl_target_to_pipe(stObj->base.Target),
362 fmt,
363 lastLevel,
364 ptWidth,
365 ptHeight,
366 ptDepth,
367 ptLayers,
368 bindings);
369
370 DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
371
372 return stObj->pt != NULL;
373 }
374
375
376 /**
377 * Adjust pixel unpack params and image dimensions to strip off the
378 * texture border.
379 * Gallium doesn't support texture borders. They've seldem been used
380 * and seldom been implemented correctly anyway.
381 * \param unpackNew returns the new pixel unpack parameters
382 */
383 static void
384 strip_texture_border(GLint border,
385 GLint *width, GLint *height, GLint *depth,
386 const struct gl_pixelstore_attrib *unpack,
387 struct gl_pixelstore_attrib *unpackNew)
388 {
389 assert(border > 0); /* sanity check */
390
391 *unpackNew = *unpack;
392
393 if (unpackNew->RowLength == 0)
394 unpackNew->RowLength = *width;
395
396 if (depth && unpackNew->ImageHeight == 0)
397 unpackNew->ImageHeight = *height;
398
399 unpackNew->SkipPixels += border;
400 if (height)
401 unpackNew->SkipRows += border;
402 if (depth)
403 unpackNew->SkipImages += border;
404
405 assert(*width >= 3);
406 *width = *width - 2 * border;
407 if (height && *height >= 3)
408 *height = *height - 2 * border;
409 if (depth && *depth >= 3)
410 *depth = *depth - 2 * border;
411 }
412
413
414 /**
415 * Try to do texture compression via rendering. If the Gallium driver
416 * can render into a compressed surface this will allow us to do texture
417 * compression.
418 * \return GL_TRUE for success, GL_FALSE for failure
419 */
420 static GLboolean
421 compress_with_blit(struct gl_context * ctx,
422 GLenum target, GLint level,
423 GLint xoffset, GLint yoffset, GLint zoffset,
424 GLint width, GLint height, GLint depth,
425 GLenum format, GLenum type, const void *pixels,
426 const struct gl_pixelstore_attrib *unpack,
427 struct gl_texture_image *texImage)
428 {
429 const GLuint dstImageOffsets[1] = {0};
430 struct st_texture_image *stImage = st_texture_image(texImage);
431 struct st_context *st = st_context(ctx);
432 struct pipe_context *pipe = st->pipe;
433 struct pipe_screen *screen = pipe->screen;
434 gl_format mesa_format;
435 struct pipe_resource templ;
436 struct pipe_resource *src_tex;
437 struct pipe_sampler_view view_templ;
438 struct pipe_sampler_view *src_view;
439 struct pipe_surface *dst_surface, surf_tmpl;
440 struct pipe_transfer *tex_xfer;
441 void *map;
442
443 if (!stImage->pt) {
444 /* XXX: Can this happen? Should we assert? */
445 return GL_FALSE;
446 }
447
448 /* get destination surface (in the compressed texture) */
449 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
450 surf_tmpl.format = stImage->pt->format;
451 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
452 surf_tmpl.u.tex.level = stImage->level;
453 surf_tmpl.u.tex.first_layer = stImage->face;
454 surf_tmpl.u.tex.last_layer = stImage->face;
455 dst_surface = pipe->create_surface(pipe, stImage->pt, &surf_tmpl);
456 if (!dst_surface) {
457 /* can't render into this format (or other problem) */
458 return GL_FALSE;
459 }
460
461 /* Choose format for the temporary RGBA texture image.
462 */
463 mesa_format = st_ChooseTextureFormat(ctx, GL_RGBA, format, type);
464 assert(mesa_format);
465 if (!mesa_format)
466 return GL_FALSE;
467
468 /* Create the temporary source texture
469 */
470 memset(&templ, 0, sizeof(templ));
471 templ.target = st->internal_target;
472 templ.format = st_mesa_format_to_pipe_format(mesa_format);
473 templ.width0 = width;
474 templ.height0 = height;
475 templ.depth0 = 1;
476 templ.array_size = 1;
477 templ.last_level = 0;
478 templ.usage = PIPE_USAGE_DEFAULT;
479 templ.bind = PIPE_BIND_SAMPLER_VIEW;
480 src_tex = screen->resource_create(screen, &templ);
481
482 if (!src_tex)
483 return GL_FALSE;
484
485 /* Put user's tex data into the temporary texture
486 */
487 tex_xfer = pipe_get_transfer(st_context(ctx)->pipe, src_tex,
488 0, 0, /* layer, level are zero */
489 PIPE_TRANSFER_WRITE,
490 0, 0, width, height); /* x, y, w, h */
491 map = pipe_transfer_map(pipe, tex_xfer);
492
493 _mesa_texstore(ctx, 2, GL_RGBA, mesa_format,
494 map, /* dest ptr */
495 0, 0, 0, /* dest x/y/z offset */
496 tex_xfer->stride, /* dest row stride (bytes) */
497 dstImageOffsets, /* image offsets (for 3D only) */
498 width, height, 1, /* size */
499 format, type, /* source format/type */
500 pixels, /* source data */
501 unpack); /* source data packing */
502
503 pipe_transfer_unmap(pipe, tex_xfer);
504 pipe->transfer_destroy(pipe, tex_xfer);
505
506 /* Create temporary sampler view */
507 u_sampler_view_default_template(&view_templ,
508 src_tex,
509 src_tex->format);
510 src_view = pipe->create_sampler_view(pipe, src_tex, &view_templ);
511
512
513 /* copy / compress image */
514 util_blit_pixels_tex(st->blit,
515 src_view, /* sampler view (src) */
516 0, 0, /* src x0, y0 */
517 width, height, /* src x1, y1 */
518 dst_surface, /* pipe_surface (dst) */
519 xoffset, yoffset, /* dst x0, y0 */
520 xoffset + width, /* dst x1 */
521 yoffset + height, /* dst y1 */
522 0.0, /* z */
523 PIPE_TEX_MIPFILTER_NEAREST);
524
525 pipe_surface_reference(&dst_surface, NULL);
526 pipe_resource_reference(&src_tex, NULL);
527 pipe_sampler_view_reference(&src_view, NULL);
528
529 return GL_TRUE;
530 }
531
532
533 /**
534 * Do glTexImage1/2/3D().
535 */
536 static void
537 st_TexImage(struct gl_context * ctx,
538 GLint dims,
539 GLenum target, GLint level,
540 GLint internalFormat,
541 GLint width, GLint height, GLint depth,
542 GLint border,
543 GLenum format, GLenum type, const void *pixels,
544 const struct gl_pixelstore_attrib *unpack,
545 struct gl_texture_object *texObj,
546 struct gl_texture_image *texImage,
547 GLsizei imageSize, GLboolean compressed_src)
548 {
549 struct st_context *st = st_context(ctx);
550 struct pipe_screen *screen = st->pipe->screen;
551 struct st_texture_object *stObj = st_texture_object(texObj);
552 struct st_texture_image *stImage = st_texture_image(texImage);
553 GLuint dstRowStride = 0;
554 struct gl_pixelstore_attrib unpackNB;
555 enum pipe_transfer_usage transfer_usage = 0;
556
557 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
558 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
559
560 /* switch to "normal" */
561 if (stObj->surface_based) {
562 gl_format texFormat;
563
564 _mesa_clear_texture_object(ctx, texObj);
565 pipe_resource_reference(&stObj->pt, NULL);
566
567 /* oops, need to init this image again */
568 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
569 internalFormat, format, type);
570
571 _mesa_init_teximage_fields(ctx, target, texImage,
572 width, height, depth, border,
573 internalFormat, texFormat);
574
575 stObj->surface_based = GL_FALSE;
576 }
577
578 /* gallium does not support texture borders, strip it off */
579 if (border) {
580 strip_texture_border(border, &width, &height, &depth, unpack, &unpackNB);
581 unpack = &unpackNB;
582 texImage->Width = width;
583 texImage->Height = height;
584 texImage->Depth = depth;
585 texImage->Border = 0;
586 border = 0;
587 }
588 else {
589 assert(texImage->Width == width);
590 assert(texImage->Height == height);
591 assert(texImage->Depth == depth);
592 }
593
594 stImage->face = _mesa_tex_target_to_face(target);
595 stImage->level = level;
596
597 _mesa_set_fetch_functions(texImage, dims);
598
599 /* Release the reference to a potentially orphaned buffer.
600 * Release any old malloced memory.
601 */
602 if (stImage->pt) {
603 pipe_resource_reference(&stImage->pt, NULL);
604 assert(!texImage->Data);
605 }
606 else if (texImage->Data) {
607 _mesa_align_free(texImage->Data);
608 }
609
610 /*
611 * See if the new image is somehow incompatible with the existing
612 * mipmap. If so, free the old mipmap.
613 */
614 if (stObj->pt) {
615 if (level > (GLint) stObj->pt->last_level ||
616 !st_texture_match_image(stObj->pt, &stImage->base,
617 stImage->face, stImage->level)) {
618 DBG("release it\n");
619 pipe_resource_reference(&stObj->pt, NULL);
620 assert(!stObj->pt);
621 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
622 }
623 }
624
625 if (width == 0 || height == 0 || depth == 0) {
626 /* stop after freeing old image */
627 return;
628 }
629
630 if (!stObj->pt) {
631 if (!guess_and_alloc_texture(st, stObj, stImage)) {
632 /* Probably out of memory.
633 * Try flushing any pending rendering, then retry.
634 */
635 st_finish(st);
636 if (!guess_and_alloc_texture(st, stObj, stImage)) {
637 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
638 return;
639 }
640 }
641 }
642
643 assert(!stImage->pt);
644
645 /* Check if this texture image can live inside the texture object's buffer.
646 * If so, store the image there. Otherwise the image will temporarily live
647 * in its own buffer.
648 */
649 if (stObj->pt &&
650 st_texture_match_image(stObj->pt, &stImage->base,
651 stImage->face, stImage->level)) {
652
653 pipe_resource_reference(&stImage->pt, stObj->pt);
654 assert(stImage->pt);
655 }
656
657 if (!stImage->pt)
658 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
659
660 /* Pixel data may come from regular user memory or a PBO. For the later,
661 * do bounds checking and map the PBO to read pixels data from it.
662 *
663 * XXX we should try to use a GPU-accelerated path to copy the image data
664 * from the PBO to the texture.
665 */
666 if (compressed_src) {
667 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
668 unpack,
669 "glCompressedTexImage");
670 }
671 else {
672 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
673 format, type,
674 pixels, unpack, "glTexImage");
675 }
676
677 /* See if we can do texture compression with a blit/render.
678 */
679 if (!compressed_src &&
680 !ctx->Mesa_DXTn &&
681 _mesa_is_format_compressed(texImage->TexFormat) &&
682 screen->is_format_supported(screen,
683 stImage->pt->format,
684 stImage->pt->target, 0,
685 PIPE_BIND_RENDER_TARGET, 0)) {
686 if (!pixels)
687 goto done;
688
689 if (compress_with_blit(ctx, target, level, 0, 0, 0, width, height, depth,
690 format, type, pixels, unpack, texImage)) {
691 goto done;
692 }
693 }
694
695 /*
696 * Prepare to store the texture data. Either map the gallium texture buffer
697 * memory or malloc space for it.
698 */
699 if (stImage->pt) {
700 /* Store the image in the gallium texture memory buffer */
701 if (format == GL_DEPTH_COMPONENT &&
702 util_format_is_depth_and_stencil(stImage->pt->format))
703 transfer_usage = PIPE_TRANSFER_READ_WRITE;
704 else
705 transfer_usage = PIPE_TRANSFER_WRITE;
706
707 texImage->Data = st_texture_image_map(st, stImage, 0,
708 transfer_usage, 0, 0, width, height);
709 if(stImage->transfer)
710 dstRowStride = stImage->transfer->stride;
711 }
712 else {
713 /* Allocate regular memory and store the image there temporarily. */
714 GLuint imageSize = _mesa_format_image_size(texImage->TexFormat,
715 width, height, depth);
716 dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
717
718 texImage->Data = _mesa_align_malloc(imageSize, 16);
719 }
720
721 if (!texImage->Data) {
722 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
723 return;
724 }
725
726 if (!pixels) {
727 /* We've allocated texture memory, but have no pixel data - all done. */
728 goto done;
729 }
730
731 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
732 width, height, depth, width, dstRowStride);
733
734 /* Copy user texture image into the texture buffer.
735 */
736 if (compressed_src) {
737 const GLuint srcRowStride =
738 _mesa_format_row_stride(texImage->TexFormat, width);
739 if (dstRowStride == srcRowStride) {
740 memcpy(texImage->Data, pixels, imageSize);
741 }
742 else {
743 char *dst = texImage->Data;
744 const char *src = pixels;
745 GLuint i, bw, bh, lines;
746 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
747 lines = (height + bh - 1) / bh;
748
749 for (i = 0; i < lines; ++i) {
750 memcpy(dst, src, srcRowStride);
751 dst += dstRowStride;
752 src += srcRowStride;
753 }
754 }
755 }
756 else {
757 const GLuint srcImageStride =
758 _mesa_image_image_stride(unpack, width, height, format, type);
759 GLint i;
760 const GLubyte *src = (const GLubyte *) pixels;
761
762 for (i = 0; i < depth; i++) {
763 if (!_mesa_texstore(ctx, dims,
764 texImage->_BaseFormat,
765 texImage->TexFormat,
766 texImage->Data,
767 0, 0, 0, /* dstX/Y/Zoffset */
768 dstRowStride,
769 texImage->ImageOffsets,
770 width, height, 1,
771 format, type, src, unpack)) {
772 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
773 }
774
775 if (stImage->pt && i + 1 < depth) {
776 /* unmap this slice */
777 st_texture_image_unmap(st, stImage);
778 /* map next slice of 3D texture */
779 texImage->Data = st_texture_image_map(st, stImage, i + 1,
780 transfer_usage, 0, 0,
781 width, height);
782 src += srcImageStride;
783 }
784 }
785 }
786
787 done:
788 _mesa_unmap_teximage_pbo(ctx, unpack);
789
790 if (stImage->pt && texImage->Data) {
791 st_texture_image_unmap(st, stImage);
792 texImage->Data = NULL;
793 }
794 }
795
796
797 static void
798 st_TexImage3D(struct gl_context * ctx,
799 GLenum target, GLint level,
800 GLint internalFormat,
801 GLint width, GLint height, GLint depth,
802 GLint border,
803 GLenum format, GLenum type, const void *pixels,
804 const struct gl_pixelstore_attrib *unpack,
805 struct gl_texture_object *texObj,
806 struct gl_texture_image *texImage)
807 {
808 st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth,
809 border, format, type, pixels, unpack, texObj, texImage,
810 0, GL_FALSE);
811 }
812
813
814 static void
815 st_TexImage2D(struct gl_context * ctx,
816 GLenum target, GLint level,
817 GLint internalFormat,
818 GLint width, GLint height, GLint border,
819 GLenum format, GLenum type, const void *pixels,
820 const struct gl_pixelstore_attrib *unpack,
821 struct gl_texture_object *texObj,
822 struct gl_texture_image *texImage)
823 {
824 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
825 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
826 }
827
828
829 static void
830 st_TexImage1D(struct gl_context * ctx,
831 GLenum target, GLint level,
832 GLint internalFormat,
833 GLint width, GLint border,
834 GLenum format, GLenum type, const void *pixels,
835 const struct gl_pixelstore_attrib *unpack,
836 struct gl_texture_object *texObj,
837 struct gl_texture_image *texImage)
838 {
839 st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border,
840 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
841 }
842
843
844 static void
845 st_CompressedTexImage2D(struct gl_context *ctx, GLenum target, GLint level,
846 GLint internalFormat,
847 GLint width, GLint height, GLint border,
848 GLsizei imageSize, const GLvoid *data,
849 struct gl_texture_object *texObj,
850 struct gl_texture_image *texImage)
851 {
852 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
853 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
854 }
855
856
857
858 /**
859 * glGetTexImage() helper: decompress a compressed texture by rendering
860 * a textured quad. Store the results in the user's buffer.
861 */
862 static void
863 decompress_with_blit(struct gl_context * ctx, GLenum target, GLint level,
864 GLenum format, GLenum type, GLvoid *pixels,
865 struct gl_texture_object *texObj,
866 struct gl_texture_image *texImage)
867 {
868 struct st_context *st = st_context(ctx);
869 struct pipe_context *pipe = st->pipe;
870 struct st_texture_image *stImage = st_texture_image(texImage);
871 struct st_texture_object *stObj = st_texture_object(texObj);
872 struct pipe_sampler_view *src_view =
873 st_get_texture_sampler_view(stObj, pipe);
874 const GLuint width = texImage->Width;
875 const GLuint height = texImage->Height;
876 struct pipe_surface *dst_surface;
877 struct pipe_resource *dst_texture;
878 struct pipe_transfer *tex_xfer;
879 unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
880 PIPE_BIND_TRANSFER_READ);
881
882 /* create temp / dest surface */
883 if (!util_create_rgba_surface(pipe, width, height, bind,
884 &dst_texture, &dst_surface)) {
885 _mesa_problem(ctx, "util_create_rgba_surface() failed "
886 "in decompress_with_blit()");
887 return;
888 }
889
890 /* blit/render/decompress */
891 util_blit_pixels_tex(st->blit,
892 src_view, /* pipe_resource (src) */
893 0, 0, /* src x0, y0 */
894 width, height, /* src x1, y1 */
895 dst_surface, /* pipe_surface (dst) */
896 0, 0, /* dst x0, y0 */
897 width, height, /* dst x1, y1 */
898 0.0, /* z */
899 PIPE_TEX_MIPFILTER_NEAREST);
900
901 /* map the dst_surface so we can read from it */
902 tex_xfer = pipe_get_transfer(st_context(ctx)->pipe,
903 dst_texture, 0, 0,
904 PIPE_TRANSFER_READ,
905 0, 0, width, height);
906
907 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
908
909 /* copy/pack data into user buffer */
910 if (st_equal_formats(stImage->pt->format, format, type)) {
911 /* memcpy */
912 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
913 ubyte *map = pipe_transfer_map(pipe, tex_xfer);
914 GLuint row;
915 for (row = 0; row < height; row++) {
916 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
917 height, format, type, row, 0);
918 memcpy(dest, map, bytesPerRow);
919 map += tex_xfer->stride;
920 }
921 pipe_transfer_unmap(pipe, tex_xfer);
922 }
923 else {
924 /* format translation via floats */
925 GLuint row;
926 enum pipe_format format = util_format_linear(dst_texture->format);
927 for (row = 0; row < height; row++) {
928 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
929 GLfloat rgba[4 * MAX_WIDTH];
930 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
931 height, format, type, row, 0);
932
933 if (ST_DEBUG & DEBUG_FALLBACK)
934 debug_printf("%s: fallback format translation\n", __FUNCTION__);
935
936 /* get float[4] rgba row from surface */
937 pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
938 format, rgba);
939
940 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
941 type, dest, &ctx->Pack, transferOps);
942 }
943 }
944
945 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
946
947 pipe->transfer_destroy(pipe, tex_xfer);
948
949 /* destroy the temp / dest surface */
950 util_destroy_rgba_surface(dst_texture, dst_surface);
951 }
952
953
954
955 /**
956 * Need to map texture image into memory before copying image data,
957 * then unmap it.
958 */
959 static void
960 st_get_tex_image(struct gl_context * ctx, GLenum target, GLint level,
961 GLenum format, GLenum type, GLvoid * pixels,
962 struct gl_texture_object *texObj,
963 struct gl_texture_image *texImage, GLboolean compressed_dst)
964 {
965 struct st_context *st = st_context(ctx);
966 struct st_texture_image *stImage = st_texture_image(texImage);
967 const GLuint dstImageStride =
968 _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
969 format, type);
970 GLuint depth, i;
971 GLubyte *dest;
972
973 if (stImage->pt &&
974 util_format_is_s3tc(stImage->pt->format) &&
975 !compressed_dst) {
976 /* Need to decompress the texture.
977 * We'll do this by rendering a textured quad.
978 * Note that we only expect RGBA formats (no Z/depth formats).
979 */
980 decompress_with_blit(ctx, target, level, format, type, pixels,
981 texObj, texImage);
982 return;
983 }
984
985 /* Map */
986 if (stImage->pt) {
987 /* Image is stored in hardware format in a buffer managed by the
988 * kernel. Need to explicitly map and unmap it.
989 */
990 texImage->Data = st_texture_image_map(st, stImage, 0,
991 PIPE_TRANSFER_READ, 0, 0,
992 stImage->base.Width,
993 stImage->base.Height);
994 /* compute stride in texels from stride in bytes */
995 texImage->RowStride = stImage->transfer->stride
996 * util_format_get_blockwidth(stImage->pt->format)
997 / util_format_get_blocksize(stImage->pt->format);
998 }
999 else {
1000 /* Otherwise, the image should actually be stored in
1001 * texImage->Data. This is pretty confusing for
1002 * everybody, I'd much prefer to separate the two functions of
1003 * texImage->Data - storage for texture images in main memory
1004 * and access (ie mappings) of images. In other words, we'd
1005 * create a new texImage->Map field and leave Data simply for
1006 * storage.
1007 */
1008 assert(texImage->Data);
1009 }
1010
1011 depth = texImage->Depth;
1012 texImage->Depth = 1;
1013
1014 dest = (GLubyte *) pixels;
1015
1016 _mesa_set_fetch_functions(texImage, get_texture_dims(target));
1017
1018 for (i = 0; i < depth; i++) {
1019 if (compressed_dst) {
1020 _mesa_get_compressed_teximage(ctx, target, level, dest,
1021 texObj, texImage);
1022 }
1023 else {
1024 _mesa_get_teximage(ctx, target, level, format, type, dest,
1025 texObj, texImage);
1026 }
1027
1028 if (stImage->pt && i + 1 < depth) {
1029 /* unmap this slice */
1030 st_texture_image_unmap(st, stImage);
1031 /* map next slice of 3D texture */
1032 texImage->Data = st_texture_image_map(st, stImage, i + 1,
1033 PIPE_TRANSFER_READ, 0, 0,
1034 stImage->base.Width,
1035 stImage->base.Height);
1036 dest += dstImageStride;
1037 }
1038 }
1039
1040 texImage->Depth = depth;
1041
1042 /* Unmap */
1043 if (stImage->pt) {
1044 st_texture_image_unmap(st, stImage);
1045 texImage->Data = NULL;
1046 }
1047 }
1048
1049
1050 static void
1051 st_GetTexImage(struct gl_context * ctx, GLenum target, GLint level,
1052 GLenum format, GLenum type, GLvoid * pixels,
1053 struct gl_texture_object *texObj,
1054 struct gl_texture_image *texImage)
1055 {
1056 st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage,
1057 GL_FALSE);
1058 }
1059
1060
1061 static void
1062 st_GetCompressedTexImage(struct gl_context *ctx, GLenum target, GLint level,
1063 GLvoid *pixels,
1064 struct gl_texture_object *texObj,
1065 struct gl_texture_image *texImage)
1066 {
1067 st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage,
1068 GL_TRUE);
1069 }
1070
1071
1072
1073 static void
1074 st_TexSubimage(struct gl_context *ctx, GLint dims, GLenum target, GLint level,
1075 GLint xoffset, GLint yoffset, GLint zoffset,
1076 GLint width, GLint height, GLint depth,
1077 GLenum format, GLenum type, const void *pixels,
1078 const struct gl_pixelstore_attrib *packing,
1079 struct gl_texture_object *texObj,
1080 struct gl_texture_image *texImage)
1081 {
1082 struct st_context *st = st_context(ctx);
1083 struct pipe_screen *screen = st->pipe->screen;
1084 struct st_texture_image *stImage = st_texture_image(texImage);
1085 GLuint dstRowStride;
1086 const GLuint srcImageStride =
1087 _mesa_image_image_stride(packing, width, height, format, type);
1088 GLint i;
1089 const GLubyte *src;
1090 /* init to silence warning only: */
1091 enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE;
1092
1093 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
1094 _mesa_lookup_enum_by_nr(target),
1095 level, xoffset, yoffset, width, height);
1096
1097 pixels =
1098 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
1099 type, pixels, packing, "glTexSubImage2D");
1100 if (!pixels)
1101 return;
1102
1103 /* See if we can do texture compression with a blit/render.
1104 */
1105 if (!ctx->Mesa_DXTn &&
1106 _mesa_is_format_compressed(texImage->TexFormat) &&
1107 screen->is_format_supported(screen,
1108 stImage->pt->format,
1109 stImage->pt->target, 0,
1110 PIPE_BIND_RENDER_TARGET, 0)) {
1111 if (compress_with_blit(ctx, target, level,
1112 xoffset, yoffset, zoffset,
1113 width, height, depth,
1114 format, type, pixels, packing, texImage)) {
1115 goto done;
1116 }
1117 }
1118
1119 /* Map buffer if necessary. Need to lock to prevent other contexts
1120 * from uploading the buffer under us.
1121 */
1122 if (stImage->pt) {
1123 if (format == GL_DEPTH_COMPONENT &&
1124 util_format_is_depth_and_stencil(stImage->pt->format))
1125 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1126 else
1127 transfer_usage = PIPE_TRANSFER_WRITE;
1128
1129 texImage->Data = st_texture_image_map(st, stImage, zoffset,
1130 transfer_usage,
1131 xoffset, yoffset,
1132 width, height);
1133 }
1134
1135 if (!texImage->Data) {
1136 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1137 goto done;
1138 }
1139
1140 src = (const GLubyte *) pixels;
1141 dstRowStride = stImage->transfer->stride;
1142
1143 for (i = 0; i < depth; i++) {
1144 if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
1145 texImage->TexFormat,
1146 texImage->Data,
1147 0, 0, 0,
1148 dstRowStride,
1149 texImage->ImageOffsets,
1150 width, height, 1,
1151 format, type, src, packing)) {
1152 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1153 }
1154
1155 if (stImage->pt && i + 1 < depth) {
1156 /* unmap this slice */
1157 st_texture_image_unmap(st, stImage);
1158 /* map next slice of 3D texture */
1159 texImage->Data = st_texture_image_map(st, stImage,
1160 zoffset + i + 1,
1161 transfer_usage,
1162 xoffset, yoffset,
1163 width, height);
1164 src += srcImageStride;
1165 }
1166 }
1167
1168 done:
1169 _mesa_unmap_teximage_pbo(ctx, packing);
1170
1171 if (stImage->pt && texImage->Data) {
1172 st_texture_image_unmap(st, stImage);
1173 texImage->Data = NULL;
1174 }
1175 }
1176
1177
1178
1179 static void
1180 st_TexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
1181 GLint xoffset, GLint yoffset, GLint zoffset,
1182 GLsizei width, GLsizei height, GLsizei depth,
1183 GLenum format, GLenum type, const GLvoid *pixels,
1184 const struct gl_pixelstore_attrib *packing,
1185 struct gl_texture_object *texObj,
1186 struct gl_texture_image *texImage)
1187 {
1188 st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
1189 width, height, depth, format, type,
1190 pixels, packing, texObj, texImage);
1191 }
1192
1193
1194 static void
1195 st_TexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
1196 GLint xoffset, GLint yoffset,
1197 GLsizei width, GLsizei height,
1198 GLenum format, GLenum type, const GLvoid * pixels,
1199 const struct gl_pixelstore_attrib *packing,
1200 struct gl_texture_object *texObj,
1201 struct gl_texture_image *texImage)
1202 {
1203 st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0,
1204 width, height, 1, format, type,
1205 pixels, packing, texObj, texImage);
1206 }
1207
1208
1209 static void
1210 st_TexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
1211 GLint xoffset, GLsizei width, GLenum format, GLenum type,
1212 const GLvoid * pixels,
1213 const struct gl_pixelstore_attrib *packing,
1214 struct gl_texture_object *texObj,
1215 struct gl_texture_image *texImage)
1216 {
1217 st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1,
1218 format, type, pixels, packing, texObj, texImage);
1219 }
1220
1221
1222 static void
1223 st_CompressedTexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
1224 GLint xoffset, GLsizei width,
1225 GLenum format,
1226 GLsizei imageSize, const GLvoid *data,
1227 struct gl_texture_object *texObj,
1228 struct gl_texture_image *texImage)
1229 {
1230 assert(0);
1231 }
1232
1233
1234 static void
1235 st_CompressedTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
1236 GLint xoffset, GLint yoffset,
1237 GLsizei width, GLint height,
1238 GLenum format,
1239 GLsizei imageSize, const GLvoid *data,
1240 struct gl_texture_object *texObj,
1241 struct gl_texture_image *texImage)
1242 {
1243 struct st_context *st = st_context(ctx);
1244 struct st_texture_image *stImage = st_texture_image(texImage);
1245 int srcBlockStride;
1246 int dstBlockStride;
1247 int y;
1248 enum pipe_format pformat;
1249
1250 if (stImage->pt) {
1251 pformat = stImage->pt->format;
1252
1253 texImage->Data = st_texture_image_map(st, stImage, 0,
1254 PIPE_TRANSFER_WRITE,
1255 xoffset, yoffset,
1256 width, height);
1257
1258 srcBlockStride = util_format_get_stride(pformat, width);
1259 dstBlockStride = stImage->transfer->stride;
1260 } else {
1261 assert(stImage->pt);
1262 /* TODO find good values for block and strides */
1263 /* TODO also adjust texImage->data for yoffset/xoffset */
1264 return;
1265 }
1266
1267 if (!texImage->Data) {
1268 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage");
1269 return;
1270 }
1271
1272 assert(xoffset % util_format_get_blockwidth(pformat) == 0);
1273 assert(yoffset % util_format_get_blockheight(pformat) == 0);
1274
1275 for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
1276 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1277 const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
1278 char *dst = (char*)texImage->Data + dstBlockStride * util_format_get_nblocksy(pformat, y);
1279 memcpy(dst, src, util_format_get_stride(pformat, width));
1280 }
1281
1282 if (stImage->pt) {
1283 st_texture_image_unmap(st, stImage);
1284 texImage->Data = NULL;
1285 }
1286 }
1287
1288
1289 static void
1290 st_CompressedTexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
1291 GLint xoffset, GLint yoffset, GLint zoffset,
1292 GLsizei width, GLint height, GLint depth,
1293 GLenum format,
1294 GLsizei imageSize, const GLvoid *data,
1295 struct gl_texture_object *texObj,
1296 struct gl_texture_image *texImage)
1297 {
1298 assert(0);
1299 }
1300
1301
1302
1303 /**
1304 * Do a CopyTexSubImage operation using a read transfer from the source,
1305 * a write transfer to the destination and get_tile()/put_tile() to access
1306 * the pixels/texels.
1307 *
1308 * Note: srcY=0=TOP of renderbuffer
1309 */
1310 static void
1311 fallback_copy_texsubimage(struct gl_context *ctx, GLenum target, GLint level,
1312 struct st_renderbuffer *strb,
1313 struct st_texture_image *stImage,
1314 GLenum baseFormat,
1315 GLint destX, GLint destY, GLint destZ,
1316 GLint srcX, GLint srcY,
1317 GLsizei width, GLsizei height)
1318 {
1319 struct st_context *st = st_context(ctx);
1320 struct pipe_context *pipe = st->pipe;
1321 struct pipe_transfer *src_trans;
1322 GLvoid *texDest;
1323 enum pipe_transfer_usage transfer_usage;
1324
1325 if (ST_DEBUG & DEBUG_FALLBACK)
1326 debug_printf("%s: fallback processing\n", __FUNCTION__);
1327
1328 assert(width <= MAX_WIDTH);
1329
1330 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1331 srcY = strb->Base.Height - srcY - height;
1332 }
1333
1334 src_trans = pipe_get_transfer(st_context(ctx)->pipe,
1335 strb->texture,
1336 0, 0,
1337 PIPE_TRANSFER_READ,
1338 srcX, srcY,
1339 width, height);
1340
1341 if ((baseFormat == GL_DEPTH_COMPONENT ||
1342 baseFormat == GL_DEPTH_STENCIL) &&
1343 util_format_is_depth_and_stencil(stImage->pt->format))
1344 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1345 else
1346 transfer_usage = PIPE_TRANSFER_WRITE;
1347
1348 /* XXX this used to ignore destZ param */
1349 texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
1350 destX, destY, width, height);
1351
1352 if (baseFormat == GL_DEPTH_COMPONENT ||
1353 baseFormat == GL_DEPTH_STENCIL) {
1354 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1355 ctx->Pixel.DepthBias != 0.0F);
1356 GLint row, yStep;
1357
1358 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1359 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1360 srcY = height - 1;
1361 yStep = -1;
1362 }
1363 else {
1364 srcY = 0;
1365 yStep = 1;
1366 }
1367
1368 /* To avoid a large temp memory allocation, do copy row by row */
1369 for (row = 0; row < height; row++, srcY += yStep) {
1370 uint data[MAX_WIDTH];
1371 pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1372 if (scaleOrBias) {
1373 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1374 }
1375 pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1376 }
1377 }
1378 else {
1379 /* RGBA format */
1380 GLfloat *tempSrc =
1381 (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1382
1383 if (tempSrc && texDest) {
1384 const GLint dims = 2;
1385 const GLint dstRowStride = stImage->transfer->stride;
1386 struct gl_texture_image *texImage = &stImage->base;
1387 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1388
1389 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1390 unpack.Invert = GL_TRUE;
1391 }
1392
1393 /* get float/RGBA image from framebuffer */
1394 /* XXX this usually involves a lot of int/float conversion.
1395 * try to avoid that someday.
1396 */
1397 pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
1398 util_format_linear(strb->texture->format),
1399 tempSrc);
1400
1401 /* Store into texture memory.
1402 * Note that this does some special things such as pixel transfer
1403 * ops and format conversion. In particular, if the dest tex format
1404 * is actually RGBA but the user created the texture as GL_RGB we
1405 * need to fill-in/override the alpha channel with 1.0.
1406 */
1407 _mesa_texstore(ctx, dims,
1408 texImage->_BaseFormat,
1409 texImage->TexFormat,
1410 texDest,
1411 0, 0, 0,
1412 dstRowStride,
1413 texImage->ImageOffsets,
1414 width, height, 1,
1415 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1416 &unpack);
1417 }
1418 else {
1419 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1420 }
1421
1422 if (tempSrc)
1423 free(tempSrc);
1424 }
1425
1426 st_texture_image_unmap(st, stImage);
1427 pipe->transfer_destroy(pipe, src_trans);
1428 }
1429
1430
1431
1432 /**
1433 * If the format of the src renderbuffer and the format of the dest
1434 * texture are compatible (in terms of blitting), return a TGSI writemask
1435 * to be used during the blit.
1436 * If the src/dest are incompatible, return 0.
1437 */
1438 static unsigned
1439 compatible_src_dst_formats(struct gl_context *ctx,
1440 const struct gl_renderbuffer *src,
1441 const struct gl_texture_image *dst)
1442 {
1443 /* Get logical base formats for the src and dest.
1444 * That is, use the user-requested formats and not the actual, device-
1445 * chosen formats.
1446 * For example, the user may have requested an A8 texture but the
1447 * driver may actually be using an RGBA texture format. When we
1448 * copy/blit to that texture, we only want to copy the Alpha channel
1449 * and not the RGB channels.
1450 *
1451 * Similarly, when the src FBO was created an RGB format may have been
1452 * requested but the driver actually chose an RGBA format. In that case,
1453 * we don't want to copy the undefined Alpha channel to the dest texture
1454 * (it should be 1.0).
1455 */
1456 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1457 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1458
1459 /**
1460 * XXX when we have red-only and red/green renderbuffers we'll need
1461 * to add more cases here (or implement a general-purpose routine that
1462 * queries the existance of the R,G,B,A channels in the src and dest).
1463 */
1464 if (srcFormat == dstFormat) {
1465 /* This is the same as matching_base_formats, which should
1466 * always pass, as it did previously.
1467 */
1468 return TGSI_WRITEMASK_XYZW;
1469 }
1470 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1471 /* Make sure that A in the dest is 1. The actual src format
1472 * may be RGBA and have undefined A values.
1473 */
1474 return TGSI_WRITEMASK_XYZ;
1475 }
1476 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1477 /* Make sure that A in the dest is 1. The actual dst format
1478 * may be RGBA and will need A=1 to provide proper alpha values
1479 * when sampled later.
1480 */
1481 return TGSI_WRITEMASK_XYZ;
1482 }
1483 else {
1484 if (ST_DEBUG & DEBUG_FALLBACK)
1485 debug_printf("%s failed for src %s, dst %s\n",
1486 __FUNCTION__,
1487 _mesa_lookup_enum_by_nr(srcFormat),
1488 _mesa_lookup_enum_by_nr(dstFormat));
1489
1490 /* Otherwise fail.
1491 */
1492 return 0;
1493 }
1494 }
1495
1496
1497
1498 /**
1499 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1500 * Note that the region to copy has already been clipped so we know we
1501 * won't read from outside the source renderbuffer's bounds.
1502 *
1503 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1504 */
1505 static void
1506 st_copy_texsubimage(struct gl_context *ctx,
1507 GLenum target, GLint level,
1508 GLint destX, GLint destY, GLint destZ,
1509 GLint srcX, GLint srcY,
1510 GLsizei width, GLsizei height)
1511 {
1512 struct gl_texture_unit *texUnit =
1513 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1514 struct gl_texture_object *texObj =
1515 _mesa_select_tex_object(ctx, texUnit, target);
1516 struct gl_texture_image *texImage =
1517 _mesa_select_tex_image(ctx, texObj, target, level);
1518 struct st_texture_image *stImage = st_texture_image(texImage);
1519 const GLenum texBaseFormat = texImage->_BaseFormat;
1520 struct gl_framebuffer *fb = ctx->ReadBuffer;
1521 struct st_renderbuffer *strb;
1522 struct st_context *st = st_context(ctx);
1523 struct pipe_context *pipe = st->pipe;
1524 struct pipe_screen *screen = pipe->screen;
1525 enum pipe_format dest_format, src_format;
1526 GLboolean use_fallback = GL_TRUE;
1527 GLboolean matching_base_formats;
1528 GLuint format_writemask, sample_count;
1529 struct pipe_surface *dest_surface = NULL;
1530 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1531
1532 /* make sure finalize_textures has been called?
1533 */
1534 if (0) st_validate_state(st);
1535
1536 /* determine if copying depth or color data */
1537 if (texBaseFormat == GL_DEPTH_COMPONENT ||
1538 texBaseFormat == GL_DEPTH_STENCIL) {
1539 strb = st_renderbuffer(fb->_DepthBuffer);
1540 if (strb->Base.Wrapped) {
1541 strb = st_renderbuffer(strb->Base.Wrapped);
1542 }
1543 }
1544 else {
1545 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1546 strb = st_renderbuffer(fb->_ColorReadBuffer);
1547 }
1548
1549 if (!strb || !strb->surface || !stImage->pt) {
1550 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1551 return;
1552 }
1553
1554 sample_count = strb->surface->texture->nr_samples;
1555 /* I believe this would be legal, presumably would need to do a resolve
1556 for color, and for depth/stencil spec says to just use one of the
1557 depth/stencil samples per pixel? Need some transfer clarifications. */
1558 assert(sample_count < 2);
1559
1560 if (srcX < 0) {
1561 width -= -srcX;
1562 destX += -srcX;
1563 srcX = 0;
1564 }
1565
1566 if (srcY < 0) {
1567 height -= -srcY;
1568 destY += -srcY;
1569 srcY = 0;
1570 }
1571
1572 if (destX < 0) {
1573 width -= -destX;
1574 srcX += -destX;
1575 destX = 0;
1576 }
1577
1578 if (destY < 0) {
1579 height -= -destY;
1580 srcY += -destY;
1581 destY = 0;
1582 }
1583
1584 if (width < 0 || height < 0)
1585 return;
1586
1587
1588 assert(strb);
1589 assert(strb->surface);
1590 assert(stImage->pt);
1591
1592 src_format = strb->surface->format;
1593 dest_format = stImage->pt->format;
1594
1595 /*
1596 * Determine if the src framebuffer and dest texture have the same
1597 * base format. We need this to detect a case such as the framebuffer
1598 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1599 * texture format stores RGBA we need to set A=1 (overriding the
1600 * framebuffer's alpha values). We can't do that with the blit or
1601 * textured-quad paths.
1602 */
1603 matching_base_formats =
1604 (_mesa_get_format_base_format(strb->Base.Format) ==
1605 _mesa_get_format_base_format(texImage->TexFormat));
1606 format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1607
1608 if (ctx->_ImageTransferState == 0x0) {
1609
1610 if (matching_base_formats &&
1611 src_format == dest_format &&
1612 !do_flip)
1613 {
1614 /* use surface_copy() / blit */
1615 struct pipe_box src_box;
1616 u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1617 width, height, &src_box);
1618
1619 /* for resource_copy_region(), y=0=top, always */
1620 pipe->resource_copy_region(pipe,
1621 /* dest */
1622 stImage->pt,
1623 stImage->level,
1624 destX, destY, destZ + stImage->face,
1625 /* src */
1626 strb->texture,
1627 strb->surface->u.tex.level,
1628 &src_box);
1629 use_fallback = GL_FALSE;
1630 }
1631 else if (format_writemask &&
1632 texBaseFormat != GL_DEPTH_COMPONENT &&
1633 texBaseFormat != GL_DEPTH_STENCIL &&
1634 screen->is_format_supported(screen, src_format,
1635 PIPE_TEXTURE_2D, sample_count,
1636 PIPE_BIND_SAMPLER_VIEW,
1637 0) &&
1638 screen->is_format_supported(screen, dest_format,
1639 PIPE_TEXTURE_2D, 0,
1640 PIPE_BIND_RENDER_TARGET,
1641 0)) {
1642 /* draw textured quad to do the copy */
1643 GLint srcY0, srcY1;
1644 struct pipe_surface surf_tmpl;
1645 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1646 surf_tmpl.format = stImage->pt->format;
1647 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
1648 surf_tmpl.u.tex.level = stImage->level;
1649 surf_tmpl.u.tex.first_layer = stImage->face + destZ;
1650 surf_tmpl.u.tex.last_layer = stImage->face + destZ;
1651
1652 dest_surface = pipe->create_surface(pipe, stImage->pt,
1653 &surf_tmpl);
1654
1655 if (do_flip) {
1656 srcY1 = strb->Base.Height - srcY - height;
1657 srcY0 = srcY1 + height;
1658 }
1659 else {
1660 srcY0 = srcY;
1661 srcY1 = srcY0 + height;
1662 }
1663
1664 util_blit_pixels_writemask(st->blit,
1665 strb->texture,
1666 strb->surface->u.tex.level,
1667 srcX, srcY0,
1668 srcX + width, srcY1,
1669 strb->surface->u.tex.first_layer,
1670 dest_surface,
1671 destX, destY,
1672 destX + width, destY + height,
1673 0.0, PIPE_TEX_MIPFILTER_NEAREST,
1674 format_writemask);
1675 use_fallback = GL_FALSE;
1676 }
1677
1678 if (dest_surface)
1679 pipe_surface_reference(&dest_surface, NULL);
1680 }
1681
1682 if (use_fallback) {
1683 /* software fallback */
1684 fallback_copy_texsubimage(ctx, target, level,
1685 strb, stImage, texBaseFormat,
1686 destX, destY, destZ,
1687 srcX, srcY, width, height);
1688 }
1689 }
1690
1691
1692
1693 static void
1694 st_CopyTexImage1D(struct gl_context * ctx, GLenum target, GLint level,
1695 GLenum internalFormat,
1696 GLint x, GLint y, GLsizei width, GLint border)
1697 {
1698 struct gl_texture_unit *texUnit =
1699 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1700 struct gl_texture_object *texObj =
1701 _mesa_select_tex_object(ctx, texUnit, target);
1702 struct gl_texture_image *texImage =
1703 _mesa_select_tex_image(ctx, texObj, target, level);
1704
1705 /* Setup or redefine the texture object, texture and texture
1706 * image. Don't populate yet.
1707 */
1708 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1709 width, border,
1710 GL_RGBA, CHAN_TYPE, NULL,
1711 &ctx->DefaultPacking, texObj, texImage);
1712
1713 st_copy_texsubimage(ctx, target, level,
1714 0, 0, 0, /* destX,Y,Z */
1715 x, y, width, 1); /* src X, Y, size */
1716 }
1717
1718
1719 static void
1720 st_CopyTexImage2D(struct gl_context * ctx, GLenum target, GLint level,
1721 GLenum internalFormat,
1722 GLint x, GLint y, GLsizei width, GLsizei height,
1723 GLint border)
1724 {
1725 struct gl_texture_unit *texUnit =
1726 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1727 struct gl_texture_object *texObj =
1728 _mesa_select_tex_object(ctx, texUnit, target);
1729 struct gl_texture_image *texImage =
1730 _mesa_select_tex_image(ctx, texObj, target, level);
1731
1732 /* Setup or redefine the texture object, texture and texture
1733 * image. Don't populate yet.
1734 */
1735 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1736 width, height, border,
1737 GL_RGBA, CHAN_TYPE, NULL,
1738 &ctx->DefaultPacking, texObj, texImage);
1739
1740 st_copy_texsubimage(ctx, target, level,
1741 0, 0, 0, /* destX,Y,Z */
1742 x, y, width, height); /* src X, Y, size */
1743 }
1744
1745
1746 static void
1747 st_CopyTexSubImage1D(struct gl_context * ctx, GLenum target, GLint level,
1748 GLint xoffset, GLint x, GLint y, GLsizei width)
1749 {
1750 const GLint yoffset = 0, zoffset = 0;
1751 const GLsizei height = 1;
1752 st_copy_texsubimage(ctx, target, level,
1753 xoffset, yoffset, zoffset, /* destX,Y,Z */
1754 x, y, width, height); /* src X, Y, size */
1755 }
1756
1757
1758 static void
1759 st_CopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
1760 GLint xoffset, GLint yoffset,
1761 GLint x, GLint y, GLsizei width, GLsizei height)
1762 {
1763 const GLint zoffset = 0;
1764 st_copy_texsubimage(ctx, target, level,
1765 xoffset, yoffset, zoffset, /* destX,Y,Z */
1766 x, y, width, height); /* src X, Y, size */
1767 }
1768
1769
1770 static void
1771 st_CopyTexSubImage3D(struct gl_context * ctx, GLenum target, GLint level,
1772 GLint xoffset, GLint yoffset, GLint zoffset,
1773 GLint x, GLint y, GLsizei width, GLsizei height)
1774 {
1775 st_copy_texsubimage(ctx, target, level,
1776 xoffset, yoffset, zoffset, /* destX,Y,Z */
1777 x, y, width, height); /* src X, Y, size */
1778 }
1779
1780
1781 /**
1782 * Copy image data from stImage into the texture object 'stObj' at level
1783 * 'dstLevel'.
1784 */
1785 static void
1786 copy_image_data_to_texture(struct st_context *st,
1787 struct st_texture_object *stObj,
1788 GLuint dstLevel,
1789 struct st_texture_image *stImage)
1790 {
1791 /* debug checks */
1792 {
1793 const struct gl_texture_image *dstImage =
1794 stObj->base.Image[stImage->face][stImage->level];
1795 assert(dstImage);
1796 assert(dstImage->Width == stImage->base.Width);
1797 assert(dstImage->Height == stImage->base.Height);
1798 assert(dstImage->Depth == stImage->base.Depth);
1799 }
1800
1801 if (stImage->pt) {
1802 /* Copy potentially with the blitter:
1803 */
1804 st_texture_image_copy(st->pipe,
1805 stObj->pt, dstLevel, /* dest texture, level */
1806 stImage->pt, stImage->level, /* src texture, level */
1807 stImage->face);
1808
1809 pipe_resource_reference(&stImage->pt, NULL);
1810 }
1811 else if (stImage->base.Data) {
1812 st_texture_image_data(st,
1813 stObj->pt,
1814 stImage->face,
1815 dstLevel,
1816 stImage->base.Data,
1817 stImage->base.RowStride *
1818 util_format_get_blocksize(stObj->pt->format),
1819 stImage->base.RowStride *
1820 stImage->base.Height *
1821 util_format_get_blocksize(stObj->pt->format));
1822 _mesa_align_free(stImage->base.Data);
1823 stImage->base.Data = NULL;
1824 }
1825
1826 pipe_resource_reference(&stImage->pt, stObj->pt);
1827 }
1828
1829
1830 /**
1831 * Called during state validation. When this function is finished,
1832 * the texture object should be ready for rendering.
1833 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1834 */
1835 GLboolean
1836 st_finalize_texture(struct gl_context *ctx,
1837 struct pipe_context *pipe,
1838 struct gl_texture_object *tObj)
1839 {
1840 struct st_context *st = st_context(ctx);
1841 struct st_texture_object *stObj = st_texture_object(tObj);
1842 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1843 GLuint face;
1844 struct st_texture_image *firstImage;
1845 enum pipe_format firstImageFormat;
1846 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1847
1848 if (stObj->base._Complete) {
1849 /* The texture is complete and we know exactly how many mipmap levels
1850 * are present/needed. This is conditional because we may be called
1851 * from the st_generate_mipmap() function when the texture object is
1852 * incomplete. In that case, we'll have set stObj->lastLevel before
1853 * we get here.
1854 */
1855 if (stObj->base.MinFilter == GL_LINEAR ||
1856 stObj->base.MinFilter == GL_NEAREST)
1857 stObj->lastLevel = stObj->base.BaseLevel;
1858 else
1859 stObj->lastLevel = stObj->base._MaxLevel;
1860 }
1861
1862 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1863 assert(firstImage);
1864
1865 /* If both firstImage and stObj point to a texture which can contain
1866 * all active images, favour firstImage. Note that because of the
1867 * completeness requirement, we know that the image dimensions
1868 * will match.
1869 */
1870 if (firstImage->pt &&
1871 firstImage->pt != stObj->pt &&
1872 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1873 pipe_resource_reference(&stObj->pt, firstImage->pt);
1874 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1875 }
1876
1877 /* Find gallium format for the Mesa texture */
1878 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1879 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, stObj->width0,
1880 stObj->height0, stObj->depth0,
1881 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1882
1883 /* If we already have a gallium texture, check that it matches the texture
1884 * object's format, target, size, num_levels, etc.
1885 */
1886 if (stObj->pt) {
1887 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1888 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1889 stObj->pt->last_level < stObj->lastLevel ||
1890 stObj->pt->width0 != ptWidth ||
1891 stObj->pt->height0 != ptHeight ||
1892 stObj->pt->depth0 != ptDepth ||
1893 stObj->pt->array_size != ptLayers)
1894 {
1895 /* The gallium texture does not match the Mesa texture so delete the
1896 * gallium texture now. We'll make a new one below.
1897 */
1898 pipe_resource_reference(&stObj->pt, NULL);
1899 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1900 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1901 }
1902 }
1903
1904 /* May need to create a new gallium texture:
1905 */
1906 if (!stObj->pt) {
1907 GLuint bindings = default_bindings(st, firstImageFormat);
1908
1909 stObj->pt = st_texture_create(st,
1910 gl_target_to_pipe(stObj->base.Target),
1911 firstImageFormat,
1912 stObj->lastLevel,
1913 ptWidth,
1914 ptHeight,
1915 ptDepth,
1916 ptLayers,
1917 bindings);
1918
1919 if (!stObj->pt) {
1920 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1921 return GL_FALSE;
1922 }
1923 }
1924
1925 /* Pull in any images not in the object's texture:
1926 */
1927 for (face = 0; face < nr_faces; face++) {
1928 GLuint level;
1929 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1930 struct st_texture_image *stImage =
1931 st_texture_image(stObj->base.Image[face][level]);
1932
1933 /* Need to import images in main memory or held in other textures.
1934 */
1935 if (stImage && stObj->pt != stImage->pt) {
1936 copy_image_data_to_texture(st, stObj, level, stImage);
1937 }
1938 }
1939 }
1940
1941 return GL_TRUE;
1942 }
1943
1944
1945 /**
1946 * Returns pointer to a default/dummy texture.
1947 * This is typically used when the current shader has tex/sample instructions
1948 * but the user has not provided a (any) texture(s).
1949 */
1950 struct gl_texture_object *
1951 st_get_default_texture(struct st_context *st)
1952 {
1953 if (!st->default_texture) {
1954 static const GLenum target = GL_TEXTURE_2D;
1955 GLubyte pixels[16][16][4];
1956 struct gl_texture_object *texObj;
1957 struct gl_texture_image *texImg;
1958 GLuint i, j;
1959
1960 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1961 * when attempting to sample incomplete textures.
1962 */
1963 for (i = 0; i < 16; i++) {
1964 for (j = 0; j < 16; j++) {
1965 pixels[i][j][0] = 0;
1966 pixels[i][j][1] = 0;
1967 pixels[i][j][2] = 0;
1968 pixels[i][j][3] = 255;
1969 }
1970 }
1971
1972 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1973
1974 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1975
1976 _mesa_init_teximage_fields(st->ctx, target, texImg,
1977 16, 16, 1, 0, /* w, h, d, border */
1978 GL_RGBA, MESA_FORMAT_RGBA8888);
1979
1980 st_TexImage(st->ctx, 2, target,
1981 0, GL_RGBA, /* level, intformat */
1982 16, 16, 1, 0, /* w, h, d, border */
1983 GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1984 &st->ctx->DefaultPacking,
1985 texObj, texImg,
1986 0, 0);
1987
1988 texObj->MinFilter = GL_NEAREST;
1989 texObj->MagFilter = GL_NEAREST;
1990 texObj->_Complete = GL_TRUE;
1991
1992 st->default_texture = texObj;
1993 }
1994 return st->default_texture;
1995 }
1996
1997
1998 void
1999 st_init_texture_functions(struct dd_function_table *functions)
2000 {
2001 functions->ChooseTextureFormat = st_ChooseTextureFormat;
2002 functions->TexImage1D = st_TexImage1D;
2003 functions->TexImage2D = st_TexImage2D;
2004 functions->TexImage3D = st_TexImage3D;
2005 functions->TexSubImage1D = st_TexSubImage1D;
2006 functions->TexSubImage2D = st_TexSubImage2D;
2007 functions->TexSubImage3D = st_TexSubImage3D;
2008 functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
2009 functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
2010 functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
2011 functions->CopyTexImage1D = st_CopyTexImage1D;
2012 functions->CopyTexImage2D = st_CopyTexImage2D;
2013 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
2014 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
2015 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
2016 functions->GenerateMipmap = st_generate_mipmap;
2017
2018 functions->GetTexImage = st_GetTexImage;
2019
2020 /* compressed texture functions */
2021 functions->CompressedTexImage2D = st_CompressedTexImage2D;
2022 functions->GetCompressedTexImage = st_GetCompressedTexImage;
2023
2024 functions->NewTextureObject = st_NewTextureObject;
2025 functions->NewTextureImage = st_NewTextureImage;
2026 functions->DeleteTexture = st_DeleteTextureObject;
2027 functions->FreeTexImageData = st_FreeTextureImageData;
2028
2029 functions->TextureMemCpy = do_memcpy;
2030
2031 /* XXX Temporary until we can query pipe's texture sizes */
2032 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
2033 }