st/mesa: remove st_get_default_texture()
[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/pbo.h"
39 #include "main/pixeltransfer.h"
40 #include "main/texcompress.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 case GL_TEXTURE_EXTERNAL_OES:
79 return PIPE_TEXTURE_2D;
80 case GL_TEXTURE_RECTANGLE_NV:
81 return PIPE_TEXTURE_RECT;
82 case GL_TEXTURE_3D:
83 return PIPE_TEXTURE_3D;
84 case GL_TEXTURE_CUBE_MAP_ARB:
85 return PIPE_TEXTURE_CUBE;
86 case GL_TEXTURE_1D_ARRAY_EXT:
87 return PIPE_TEXTURE_1D_ARRAY;
88 case GL_TEXTURE_2D_ARRAY_EXT:
89 return PIPE_TEXTURE_2D_ARRAY;
90 case GL_TEXTURE_BUFFER:
91 return PIPE_BUFFER;
92 default:
93 assert(0);
94 return 0;
95 }
96 }
97
98
99 /** called via ctx->Driver.NewTextureImage() */
100 static struct gl_texture_image *
101 st_NewTextureImage(struct gl_context * ctx)
102 {
103 DBG("%s\n", __FUNCTION__);
104 (void) ctx;
105 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
106 }
107
108
109 /** called via ctx->Driver.DeleteTextureImage() */
110 static void
111 st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
112 {
113 /* nothing special (yet) for st_texture_image */
114 _mesa_delete_texture_image(ctx, img);
115 }
116
117
118 /** called via ctx->Driver.NewTextureObject() */
119 static struct gl_texture_object *
120 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
121 {
122 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
123
124 DBG("%s\n", __FUNCTION__);
125 _mesa_initialize_texture_object(&obj->base, name, target);
126
127 return &obj->base;
128 }
129
130 /** called via ctx->Driver.DeleteTextureObject() */
131 static void
132 st_DeleteTextureObject(struct gl_context *ctx,
133 struct gl_texture_object *texObj)
134 {
135 struct st_context *st = st_context(ctx);
136 struct st_texture_object *stObj = st_texture_object(texObj);
137 if (stObj->pt)
138 pipe_resource_reference(&stObj->pt, NULL);
139 if (stObj->sampler_view) {
140 if (stObj->sampler_view->context != st->pipe) {
141 /* Take "ownership" of this texture sampler view by setting
142 * its context pointer to this context. This avoids potential
143 * crashes when the texture object is shared among contexts
144 * and the original/owner context has already been destroyed.
145 */
146 stObj->sampler_view->context = st->pipe;
147 }
148 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
149 }
150 _mesa_delete_texture_object(ctx, texObj);
151 }
152
153
154 /** called via ctx->Driver.FreeTextureImageBuffer() */
155 static void
156 st_FreeTextureImageBuffer(struct gl_context *ctx,
157 struct gl_texture_image *texImage)
158 {
159 struct st_texture_image *stImage = st_texture_image(texImage);
160
161 DBG("%s\n", __FUNCTION__);
162
163 if (stImage->pt) {
164 pipe_resource_reference(&stImage->pt, NULL);
165 }
166
167 if (stImage->TexData) {
168 _mesa_align_free(stImage->TexData);
169 stImage->TexData = NULL;
170 }
171 }
172
173
174 /** called via ctx->Driver.MapTextureImage() */
175 static void
176 st_MapTextureImage(struct gl_context *ctx,
177 struct gl_texture_image *texImage,
178 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
179 GLbitfield mode,
180 GLubyte **mapOut, GLint *rowStrideOut)
181 {
182 struct st_context *st = st_context(ctx);
183 struct st_texture_image *stImage = st_texture_image(texImage);
184 unsigned pipeMode;
185 GLubyte *map;
186
187 pipeMode = 0x0;
188 if (mode & GL_MAP_READ_BIT)
189 pipeMode |= PIPE_TRANSFER_READ;
190 if (mode & GL_MAP_WRITE_BIT)
191 pipeMode |= PIPE_TRANSFER_WRITE;
192 if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
193 pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
194
195 map = st_texture_image_map(st, stImage, slice, pipeMode, x, y, w, h);
196 if (map) {
197 *mapOut = map;
198 *rowStrideOut = stImage->transfer->stride;
199 }
200 else {
201 *mapOut = NULL;
202 *rowStrideOut = 0;
203 }
204 }
205
206
207 /** called via ctx->Driver.UnmapTextureImage() */
208 static void
209 st_UnmapTextureImage(struct gl_context *ctx,
210 struct gl_texture_image *texImage,
211 GLuint slice)
212 {
213 struct st_context *st = st_context(ctx);
214 struct st_texture_image *stImage = st_texture_image(texImage);
215 st_texture_image_unmap(st, stImage);
216 }
217
218
219 /**
220 * Return default texture resource binding bitmask for the given format.
221 */
222 static GLuint
223 default_bindings(struct st_context *st, enum pipe_format format)
224 {
225 struct pipe_screen *screen = st->pipe->screen;
226 const unsigned target = PIPE_TEXTURE_2D;
227 unsigned bindings;
228
229 if (util_format_is_depth_or_stencil(format))
230 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
231 else
232 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
233
234 if (screen->is_format_supported(screen, format, target, 0, bindings))
235 return bindings;
236 else {
237 /* Try non-sRGB. */
238 format = util_format_linear(format);
239
240 if (screen->is_format_supported(screen, format, target, 0, bindings))
241 return bindings;
242 else
243 return PIPE_BIND_SAMPLER_VIEW;
244 }
245 }
246
247
248 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
249 static GLuint
250 get_texture_dims(GLenum target)
251 {
252 switch (target) {
253 case GL_TEXTURE_1D:
254 case GL_TEXTURE_1D_ARRAY_EXT:
255 case GL_TEXTURE_BUFFER:
256 return 1;
257 case GL_TEXTURE_2D:
258 case GL_TEXTURE_CUBE_MAP_ARB:
259 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
260 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
261 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
262 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
263 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
264 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
265 case GL_TEXTURE_RECTANGLE_NV:
266 case GL_TEXTURE_2D_ARRAY_EXT:
267 case GL_TEXTURE_EXTERNAL_OES:
268 return 2;
269 case GL_TEXTURE_3D:
270 return 3;
271 default:
272 assert(0 && "invalid texture target in get_texture_dims()");
273 return 1;
274 }
275 }
276
277
278 /**
279 * Given the size of a mipmap image, try to compute the size of the level=0
280 * mipmap image.
281 *
282 * Note that this isn't always accurate for odd-sized, non-POW textures.
283 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
284 *
285 * \return GL_TRUE for success, GL_FALSE for failure
286 */
287 static GLboolean
288 guess_base_level_size(GLenum target,
289 GLuint width, GLuint height, GLuint depth, GLuint level,
290 GLuint *width0, GLuint *height0, GLuint *depth0)
291 {
292 const GLuint dims = get_texture_dims(target);
293
294 assert(width >= 1);
295 assert(height >= 1);
296 assert(depth >= 1);
297
298 if (level > 0) {
299 /* Depending on the image's size, we can't always make a guess here */
300 if ((dims >= 1 && width == 1) ||
301 (dims >= 2 && height == 1) ||
302 (dims >= 3 && depth == 1)) {
303 /* we can't determine the image size at level=0 */
304 return GL_FALSE;
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
319 *width0 = width;
320 *height0 = height;
321 *depth0 = depth;
322
323 return GL_TRUE;
324 }
325
326
327 /**
328 * Try to allocate a pipe_resource object for the given st_texture_object.
329 *
330 * We use the given st_texture_image as a clue to determine the size of the
331 * mipmap image at level=0.
332 *
333 * \return GL_TRUE for success, GL_FALSE if out of memory.
334 */
335 static GLboolean
336 guess_and_alloc_texture(struct st_context *st,
337 struct st_texture_object *stObj,
338 const struct st_texture_image *stImage)
339 {
340 GLuint lastLevel, width, height, depth;
341 GLuint bindings;
342 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
343 enum pipe_format fmt;
344
345 DBG("%s\n", __FUNCTION__);
346
347 assert(!stObj->pt);
348
349 if (!guess_base_level_size(stObj->base.Target,
350 stImage->base.Width2,
351 stImage->base.Height2,
352 stImage->base.Depth2,
353 stImage->base.Level,
354 &width, &height, &depth)) {
355 /* we can't determine the image size at level=0 */
356 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
357 /* this is not an out of memory error */
358 return GL_TRUE;
359 }
360
361 /* At this point, (width x height x depth) is the expected size of
362 * the level=0 mipmap image.
363 */
364
365 /* Guess a reasonable value for lastLevel. With OpenGL we have no
366 * idea how many mipmap levels will be in a texture until we start
367 * to render with it. Make an educated guess here but be prepared
368 * to re-allocating a texture buffer with space for more (or fewer)
369 * mipmap levels later.
370 */
371 if ((stObj->base.Sampler.MinFilter == GL_NEAREST ||
372 stObj->base.Sampler.MinFilter == GL_LINEAR ||
373 stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
374 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
375 !stObj->base.GenerateMipmap &&
376 stImage->base.Level == 0) {
377 /* only alloc space for a single mipmap level */
378 lastLevel = 0;
379 }
380 else {
381 /* alloc space for a full mipmap */
382 GLuint l2width = util_logbase2(width);
383 GLuint l2height = util_logbase2(height);
384 GLuint l2depth = util_logbase2(depth);
385 lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
386 }
387
388 /* Save the level=0 dimensions */
389 stObj->width0 = width;
390 stObj->height0 = height;
391 stObj->depth0 = depth;
392
393 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
394
395 bindings = default_bindings(st, fmt);
396
397 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
398 width, height, depth,
399 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
400
401 stObj->pt = st_texture_create(st,
402 gl_target_to_pipe(stObj->base.Target),
403 fmt,
404 lastLevel,
405 ptWidth,
406 ptHeight,
407 ptDepth,
408 ptLayers,
409 bindings);
410
411 DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
412
413 return stObj->pt != NULL;
414 }
415
416
417 /**
418 * Called via ctx->Driver.AllocTextureImageBuffer().
419 * If the texture object/buffer already has space for the indicated image,
420 * we're done. Otherwise, allocate memory for the new texture image.
421 */
422 static GLboolean
423 st_AllocTextureImageBuffer(struct gl_context *ctx,
424 struct gl_texture_image *texImage,
425 gl_format format, GLsizei width,
426 GLsizei height, GLsizei depth)
427 {
428 struct st_context *st = st_context(ctx);
429 struct st_texture_image *stImage = st_texture_image(texImage);
430 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
431 const GLuint level = texImage->Level;
432
433 DBG("%s\n", __FUNCTION__);
434
435 assert(width > 0);
436 assert(height > 0);
437 assert(depth > 0);
438 assert(!stImage->TexData);
439 assert(!stImage->pt); /* xxx this might be wrong */
440
441 /* Look if the parent texture object has space for this image */
442 if (stObj->pt &&
443 level <= stObj->pt->last_level &&
444 st_texture_match_image(stObj->pt, texImage)) {
445 /* this image will fit in the existing texture object's memory */
446 pipe_resource_reference(&stImage->pt, stObj->pt);
447 return GL_TRUE;
448 }
449
450 /* The parent texture object does not have space for this image */
451
452 pipe_resource_reference(&stObj->pt, NULL);
453 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
454
455 if (!guess_and_alloc_texture(st, stObj, stImage)) {
456 /* Probably out of memory.
457 * Try flushing any pending rendering, then retry.
458 */
459 st_finish(st);
460 if (!guess_and_alloc_texture(st, stObj, stImage)) {
461 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
462 return GL_FALSE;
463 }
464 }
465
466 if (stObj->pt &&
467 st_texture_match_image(stObj->pt, texImage)) {
468 /* The image will live in the object's mipmap memory */
469 pipe_resource_reference(&stImage->pt, stObj->pt);
470 assert(stImage->pt);
471 return GL_TRUE;
472 }
473 else {
474 /* Create a new, temporary texture/resource/buffer to hold this
475 * one texture image. Note that when we later access this image
476 * (either for mapping or copying) we'll want to always specify
477 * mipmap level=0, even if the image represents some other mipmap
478 * level.
479 */
480 enum pipe_format format =
481 st_mesa_format_to_pipe_format(texImage->TexFormat);
482 GLuint bindings = default_bindings(st, format);
483 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
484
485 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
486 width, height, depth,
487 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
488
489 stImage->pt = st_texture_create(st,
490 gl_target_to_pipe(stObj->base.Target),
491 format,
492 0, /* lastLevel */
493 ptWidth,
494 ptHeight,
495 ptDepth,
496 ptLayers,
497 bindings);
498 return stImage->pt != NULL;
499 }
500 }
501
502
503 /**
504 * Preparation prior to glTexImage. Basically check the 'surface_based'
505 * field and switch to a "normal" tex image if necessary.
506 */
507 static void
508 prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
509 GLint internalFormat,
510 GLint width, GLint height, GLint depth, GLint border,
511 GLenum format, GLenum type)
512 {
513 struct gl_texture_object *texObj = texImage->TexObject;
514 struct st_texture_object *stObj = st_texture_object(texObj);
515
516 /* switch to "normal" */
517 if (stObj->surface_based) {
518 const GLenum target = texObj->Target;
519 const GLuint level = texImage->Level;
520 gl_format texFormat;
521
522 _mesa_clear_texture_object(ctx, texObj);
523 pipe_resource_reference(&stObj->pt, NULL);
524
525 /* oops, need to init this image again */
526 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
527 internalFormat, format, type);
528
529 _mesa_init_teximage_fields(ctx, texImage,
530 width, height, depth, border,
531 internalFormat, texFormat);
532
533 stObj->surface_based = GL_FALSE;
534 }
535 }
536
537
538 static void
539 st_TexImage3D(struct gl_context * ctx,
540 struct gl_texture_image *texImage,
541 GLint internalFormat,
542 GLint width, GLint height, GLint depth,
543 GLint border,
544 GLenum format, GLenum type, const void *pixels,
545 const struct gl_pixelstore_attrib *unpack)
546 {
547 prep_teximage(ctx, texImage, internalFormat, width, height, depth, border,
548 format, type);
549 _mesa_store_teximage3d(ctx, texImage, internalFormat, width, height, depth,
550 border, format, type, pixels, unpack);
551 }
552
553
554 static void
555 st_TexImage2D(struct gl_context * ctx,
556 struct gl_texture_image *texImage,
557 GLint internalFormat,
558 GLint width, GLint height, GLint border,
559 GLenum format, GLenum type, const void *pixels,
560 const struct gl_pixelstore_attrib *unpack)
561 {
562 prep_teximage(ctx, texImage, internalFormat, width, height, 1, border,
563 format, type);
564 _mesa_store_teximage2d(ctx, texImage, internalFormat, width, height,
565 border, format, type, pixels, unpack);
566 }
567
568
569 static void
570 st_TexImage1D(struct gl_context * ctx,
571 struct gl_texture_image *texImage,
572 GLint internalFormat,
573 GLint width, GLint border,
574 GLenum format, GLenum type, const void *pixels,
575 const struct gl_pixelstore_attrib *unpack)
576 {
577 prep_teximage(ctx, texImage, internalFormat, width, 1, 1, border,
578 format, type);
579 _mesa_store_teximage1d(ctx, texImage, internalFormat, width,
580 border, format, type, pixels, unpack);
581 }
582
583
584 static void
585 st_CompressedTexImage2D(struct gl_context *ctx,
586 struct gl_texture_image *texImage,
587 GLint internalFormat,
588 GLint width, GLint height, GLint border,
589 GLsizei imageSize, const GLvoid *data)
590 {
591 prep_teximage(ctx, texImage, internalFormat, width, 1, 1, border,
592 GL_NONE, GL_NONE);
593 _mesa_store_compressed_teximage2d(ctx, texImage, internalFormat, width,
594 height, border, imageSize, data);
595 }
596
597
598
599 /**
600 * glGetTexImage() helper: decompress a compressed texture by rendering
601 * a textured quad. Store the results in the user's buffer.
602 */
603 static void
604 decompress_with_blit(struct gl_context * ctx,
605 GLenum format, GLenum type, GLvoid *pixels,
606 struct gl_texture_image *texImage)
607 {
608 struct st_context *st = st_context(ctx);
609 struct pipe_context *pipe = st->pipe;
610 struct st_texture_image *stImage = st_texture_image(texImage);
611 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
612 struct pipe_sampler_view *src_view =
613 st_get_texture_sampler_view(stObj, pipe);
614 const GLuint width = texImage->Width;
615 const GLuint height = texImage->Height;
616 struct pipe_surface *dst_surface;
617 struct pipe_resource *dst_texture;
618 struct pipe_transfer *tex_xfer;
619 unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
620 PIPE_BIND_TRANSFER_READ);
621
622 /* create temp / dest surface */
623 if (!util_create_rgba_surface(pipe, width, height, bind,
624 &dst_texture, &dst_surface)) {
625 _mesa_problem(ctx, "util_create_rgba_surface() failed "
626 "in decompress_with_blit()");
627 return;
628 }
629
630 /* Disable conditional rendering. */
631 if (st->render_condition) {
632 pipe->render_condition(pipe, NULL, 0);
633 }
634
635 /* Choose the source mipmap level */
636 src_view->u.tex.first_level = src_view->u.tex.last_level = texImage->Level;
637
638 /* blit/render/decompress */
639 util_blit_pixels_tex(st->blit,
640 src_view, /* pipe_resource (src) */
641 0, 0, /* src x0, y0 */
642 width, height, /* src x1, y1 */
643 dst_surface, /* pipe_surface (dst) */
644 0, 0, /* dst x0, y0 */
645 width, height, /* dst x1, y1 */
646 0.0, /* z */
647 PIPE_TEX_MIPFILTER_NEAREST);
648
649 /* Restore conditional rendering state. */
650 if (st->render_condition) {
651 pipe->render_condition(pipe, st->render_condition,
652 st->condition_mode);
653 }
654
655 /* map the dst_surface so we can read from it */
656 tex_xfer = pipe_get_transfer(pipe,
657 dst_texture, 0, 0,
658 PIPE_TRANSFER_READ,
659 0, 0, width, height);
660
661 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
662
663 /* copy/pack data into user buffer */
664 if (st_equal_formats(stImage->pt->format, format, type)) {
665 /* memcpy */
666 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
667 ubyte *map = pipe_transfer_map(pipe, tex_xfer);
668 GLuint row;
669 for (row = 0; row < height; row++) {
670 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
671 height, format, type, row, 0);
672 memcpy(dest, map, bytesPerRow);
673 map += tex_xfer->stride;
674 }
675 pipe_transfer_unmap(pipe, tex_xfer);
676 }
677 else {
678 /* format translation via floats */
679 GLuint row;
680 enum pipe_format pformat = util_format_linear(dst_texture->format);
681 for (row = 0; row < height; row++) {
682 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
683 GLfloat rgba[4 * MAX_WIDTH];
684 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
685 height, format, type, row, 0);
686
687 if (ST_DEBUG & DEBUG_FALLBACK)
688 debug_printf("%s: fallback format translation\n", __FUNCTION__);
689
690 /* get float[4] rgba row from surface */
691 pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
692 pformat, rgba);
693
694 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
695 type, dest, &ctx->Pack, transferOps);
696 }
697 }
698
699 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
700
701 pipe->transfer_destroy(pipe, tex_xfer);
702
703 /* destroy the temp / dest surface */
704 util_destroy_rgba_surface(dst_texture, dst_surface);
705 }
706
707
708
709 /**
710 * Called via ctx->Driver.GetTexImage()
711 */
712 static void
713 st_GetTexImage(struct gl_context * ctx,
714 GLenum format, GLenum type, GLvoid * pixels,
715 struct gl_texture_image *texImage)
716 {
717 struct st_texture_image *stImage = st_texture_image(texImage);
718
719 if (stImage->pt && util_format_is_s3tc(stImage->pt->format)) {
720 /* Need to decompress the texture.
721 * We'll do this by rendering a textured quad (which is hopefully
722 * faster than using the fallback code in texcompress.c).
723 * Note that we only expect RGBA formats (no Z/depth formats).
724 */
725 decompress_with_blit(ctx, format, type, pixels, texImage);
726 }
727 else {
728 _mesa_get_teximage(ctx, format, type, pixels, texImage);
729 }
730 }
731
732
733 /**
734 * Do a CopyTexSubImage operation using a read transfer from the source,
735 * a write transfer to the destination and get_tile()/put_tile() to access
736 * the pixels/texels.
737 *
738 * Note: srcY=0=TOP of renderbuffer
739 */
740 static void
741 fallback_copy_texsubimage(struct gl_context *ctx,
742 struct st_renderbuffer *strb,
743 struct st_texture_image *stImage,
744 GLenum baseFormat,
745 GLint destX, GLint destY, GLint destZ,
746 GLint srcX, GLint srcY,
747 GLsizei width, GLsizei height)
748 {
749 struct st_context *st = st_context(ctx);
750 struct pipe_context *pipe = st->pipe;
751 struct pipe_transfer *src_trans;
752 GLvoid *texDest;
753 enum pipe_transfer_usage transfer_usage;
754
755 if (ST_DEBUG & DEBUG_FALLBACK)
756 debug_printf("%s: fallback processing\n", __FUNCTION__);
757
758 assert(width <= MAX_WIDTH);
759
760 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
761 srcY = strb->Base.Height - srcY - height;
762 }
763
764 src_trans = pipe_get_transfer(pipe,
765 strb->texture,
766 strb->rtt_level,
767 strb->rtt_face + strb->rtt_slice,
768 PIPE_TRANSFER_READ,
769 srcX, srcY,
770 width, height);
771
772 if ((baseFormat == GL_DEPTH_COMPONENT ||
773 baseFormat == GL_DEPTH_STENCIL) &&
774 util_format_is_depth_and_stencil(stImage->pt->format))
775 transfer_usage = PIPE_TRANSFER_READ_WRITE;
776 else
777 transfer_usage = PIPE_TRANSFER_WRITE;
778
779 /* XXX this used to ignore destZ param */
780 texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
781 destX, destY, width, height);
782
783 if (baseFormat == GL_DEPTH_COMPONENT ||
784 baseFormat == GL_DEPTH_STENCIL) {
785 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
786 ctx->Pixel.DepthBias != 0.0F);
787 GLint row, yStep;
788
789 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
790 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
791 srcY = height - 1;
792 yStep = -1;
793 }
794 else {
795 srcY = 0;
796 yStep = 1;
797 }
798
799 /* To avoid a large temp memory allocation, do copy row by row */
800 for (row = 0; row < height; row++, srcY += yStep) {
801 uint data[MAX_WIDTH];
802 pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
803 if (scaleOrBias) {
804 _mesa_scale_and_bias_depth_uint(ctx, width, data);
805 }
806 pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
807 }
808 }
809 else {
810 /* RGBA format */
811 GLfloat *tempSrc =
812 (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
813
814 if (tempSrc && texDest) {
815 const GLint dims = 2;
816 const GLint dstRowStride = stImage->transfer->stride;
817 struct gl_texture_image *texImage = &stImage->base;
818 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
819
820 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
821 unpack.Invert = GL_TRUE;
822 }
823
824 /* get float/RGBA image from framebuffer */
825 /* XXX this usually involves a lot of int/float conversion.
826 * try to avoid that someday.
827 */
828 pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
829 util_format_linear(strb->texture->format),
830 tempSrc);
831
832 /* Store into texture memory.
833 * Note that this does some special things such as pixel transfer
834 * ops and format conversion. In particular, if the dest tex format
835 * is actually RGBA but the user created the texture as GL_RGB we
836 * need to fill-in/override the alpha channel with 1.0.
837 */
838 _mesa_texstore(ctx, dims,
839 texImage->_BaseFormat,
840 texImage->TexFormat,
841 dstRowStride,
842 (GLubyte **) &texDest,
843 width, height, 1,
844 GL_RGBA, GL_FLOAT, tempSrc, /* src */
845 &unpack);
846 }
847 else {
848 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
849 }
850
851 if (tempSrc)
852 free(tempSrc);
853 }
854
855 st_texture_image_unmap(st, stImage);
856 pipe->transfer_destroy(pipe, src_trans);
857 }
858
859
860
861 /**
862 * If the format of the src renderbuffer and the format of the dest
863 * texture are compatible (in terms of blitting), return a TGSI writemask
864 * to be used during the blit.
865 * If the src/dest are incompatible, return 0.
866 */
867 static unsigned
868 compatible_src_dst_formats(struct gl_context *ctx,
869 const struct gl_renderbuffer *src,
870 const struct gl_texture_image *dst)
871 {
872 /* Get logical base formats for the src and dest.
873 * That is, use the user-requested formats and not the actual, device-
874 * chosen formats.
875 * For example, the user may have requested an A8 texture but the
876 * driver may actually be using an RGBA texture format. When we
877 * copy/blit to that texture, we only want to copy the Alpha channel
878 * and not the RGB channels.
879 *
880 * Similarly, when the src FBO was created an RGB format may have been
881 * requested but the driver actually chose an RGBA format. In that case,
882 * we don't want to copy the undefined Alpha channel to the dest texture
883 * (it should be 1.0).
884 */
885 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
886 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
887
888 /**
889 * XXX when we have red-only and red/green renderbuffers we'll need
890 * to add more cases here (or implement a general-purpose routine that
891 * queries the existance of the R,G,B,A channels in the src and dest).
892 */
893 if (srcFormat == dstFormat) {
894 /* This is the same as matching_base_formats, which should
895 * always pass, as it did previously.
896 */
897 return TGSI_WRITEMASK_XYZW;
898 }
899 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
900 /* Make sure that A in the dest is 1. The actual src format
901 * may be RGBA and have undefined A values.
902 */
903 return TGSI_WRITEMASK_XYZ;
904 }
905 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
906 /* Make sure that A in the dest is 1. The actual dst format
907 * may be RGBA and will need A=1 to provide proper alpha values
908 * when sampled later.
909 */
910 return TGSI_WRITEMASK_XYZ;
911 }
912 else {
913 if (ST_DEBUG & DEBUG_FALLBACK)
914 debug_printf("%s failed for src %s, dst %s\n",
915 __FUNCTION__,
916 _mesa_lookup_enum_by_nr(srcFormat),
917 _mesa_lookup_enum_by_nr(dstFormat));
918
919 /* Otherwise fail.
920 */
921 return 0;
922 }
923 }
924
925
926
927 /**
928 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
929 * Note that the region to copy has already been clipped so we know we
930 * won't read from outside the source renderbuffer's bounds.
931 *
932 * Note: srcY=0=Bottom of renderbuffer (GL convention)
933 */
934 static void
935 st_copy_texsubimage(struct gl_context *ctx,
936 struct gl_texture_image *texImage,
937 GLint destX, GLint destY, GLint destZ,
938 struct gl_renderbuffer *rb,
939 GLint srcX, GLint srcY,
940 GLsizei width, GLsizei height)
941 {
942 struct st_texture_image *stImage = st_texture_image(texImage);
943 const GLenum texBaseFormat = texImage->_BaseFormat;
944 struct gl_framebuffer *fb = ctx->ReadBuffer;
945 struct st_renderbuffer *strb;
946 struct st_context *st = st_context(ctx);
947 struct pipe_context *pipe = st->pipe;
948 struct pipe_screen *screen = pipe->screen;
949 enum pipe_format dest_format, src_format;
950 GLboolean matching_base_formats;
951 GLuint format_writemask, sample_count;
952 struct pipe_surface *dest_surface = NULL;
953 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
954 struct pipe_surface surf_tmpl;
955 unsigned int dst_usage;
956 GLint srcY0, srcY1;
957
958 /* make sure finalize_textures has been called?
959 */
960 if (0) st_validate_state(st);
961
962 /* determine if copying depth or color data */
963 if (texBaseFormat == GL_DEPTH_COMPONENT ||
964 texBaseFormat == GL_DEPTH_STENCIL) {
965 strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
966 }
967 else {
968 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
969 strb = st_renderbuffer(fb->_ColorReadBuffer);
970 }
971
972 if (!strb || !strb->surface || !stImage->pt) {
973 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
974 return;
975 }
976
977 sample_count = strb->surface->texture->nr_samples;
978 /* I believe this would be legal, presumably would need to do a resolve
979 for color, and for depth/stencil spec says to just use one of the
980 depth/stencil samples per pixel? Need some transfer clarifications. */
981 assert(sample_count < 2);
982
983 assert(strb);
984 assert(strb->surface);
985 assert(stImage->pt);
986
987 src_format = strb->surface->format;
988 dest_format = stImage->pt->format;
989
990 /*
991 * Determine if the src framebuffer and dest texture have the same
992 * base format. We need this to detect a case such as the framebuffer
993 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
994 * texture format stores RGBA we need to set A=1 (overriding the
995 * framebuffer's alpha values). We can't do that with the blit or
996 * textured-quad paths.
997 */
998 matching_base_formats =
999 (_mesa_get_format_base_format(strb->Base.Format) ==
1000 _mesa_get_format_base_format(texImage->TexFormat));
1001
1002 if (ctx->_ImageTransferState) {
1003 goto fallback;
1004 }
1005
1006 if (matching_base_formats &&
1007 src_format == dest_format &&
1008 !do_flip) {
1009 /* use surface_copy() / blit */
1010 struct pipe_box src_box;
1011 u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1012 width, height, &src_box);
1013
1014 /* for resource_copy_region(), y=0=top, always */
1015 pipe->resource_copy_region(pipe,
1016 /* dest */
1017 stImage->pt,
1018 stImage->base.Level,
1019 destX, destY, destZ + stImage->base.Face,
1020 /* src */
1021 strb->texture,
1022 strb->surface->u.tex.level,
1023 &src_box);
1024 return;
1025 }
1026
1027 if (texBaseFormat == GL_DEPTH_STENCIL) {
1028 goto fallback;
1029 }
1030
1031 if (texBaseFormat == GL_DEPTH_COMPONENT) {
1032 format_writemask = TGSI_WRITEMASK_XYZW;
1033 dst_usage = PIPE_BIND_DEPTH_STENCIL;
1034 }
1035 else {
1036 format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1037 dst_usage = PIPE_BIND_RENDER_TARGET;
1038 }
1039
1040 if (!format_writemask ||
1041 !screen->is_format_supported(screen, src_format,
1042 PIPE_TEXTURE_2D, sample_count,
1043 PIPE_BIND_SAMPLER_VIEW) ||
1044 !screen->is_format_supported(screen, dest_format,
1045 PIPE_TEXTURE_2D, 0,
1046 dst_usage)) {
1047 goto fallback;
1048 }
1049
1050 if (do_flip) {
1051 srcY1 = strb->Base.Height - srcY - height;
1052 srcY0 = srcY1 + height;
1053 }
1054 else {
1055 srcY0 = srcY;
1056 srcY1 = srcY0 + height;
1057 }
1058
1059 /* Disable conditional rendering. */
1060 if (st->render_condition) {
1061 pipe->render_condition(pipe, NULL, 0);
1062 }
1063
1064 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1065 surf_tmpl.format = util_format_linear(stImage->pt->format);
1066 surf_tmpl.usage = dst_usage;
1067 surf_tmpl.u.tex.level = stImage->base.Level;
1068 surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ;
1069 surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ;
1070
1071 dest_surface = pipe->create_surface(pipe, stImage->pt,
1072 &surf_tmpl);
1073 util_blit_pixels_writemask(st->blit,
1074 strb->texture,
1075 strb->surface->u.tex.level,
1076 srcX, srcY0,
1077 srcX + width, srcY1,
1078 strb->surface->u.tex.first_layer,
1079 dest_surface,
1080 destX, destY,
1081 destX + width, destY + height,
1082 0.0, PIPE_TEX_MIPFILTER_NEAREST,
1083 format_writemask);
1084 pipe_surface_reference(&dest_surface, NULL);
1085
1086 /* Restore conditional rendering state. */
1087 if (st->render_condition) {
1088 pipe->render_condition(pipe, st->render_condition,
1089 st->condition_mode);
1090 }
1091
1092 return;
1093
1094 fallback:
1095 /* software fallback */
1096 fallback_copy_texsubimage(ctx,
1097 strb, stImage, texBaseFormat,
1098 destX, destY, destZ,
1099 srcX, srcY, width, height);
1100 }
1101
1102
1103
1104 static void
1105 st_CopyTexSubImage1D(struct gl_context *ctx,
1106 struct gl_texture_image *texImage,
1107 GLint xoffset,
1108 struct gl_renderbuffer *rb,
1109 GLint x, GLint y, GLsizei width)
1110 {
1111 const GLint yoffset = 0, zoffset = 0;
1112 const GLsizei height = 1;
1113 st_copy_texsubimage(ctx, texImage,
1114 xoffset, yoffset, zoffset, /* destX,Y,Z */
1115 rb, x, y, width, height); /* src X, Y, size */
1116 }
1117
1118
1119 static void
1120 st_CopyTexSubImage2D(struct gl_context *ctx,
1121 struct gl_texture_image *texImage,
1122 GLint xoffset, GLint yoffset,
1123 struct gl_renderbuffer *rb,
1124 GLint x, GLint y, GLsizei width, GLsizei height)
1125 {
1126 const GLint zoffset = 0;
1127 st_copy_texsubimage(ctx, texImage,
1128 xoffset, yoffset, zoffset, /* destX,Y,Z */
1129 rb, x, y, width, height); /* src X, Y, size */
1130 }
1131
1132
1133 static void
1134 st_CopyTexSubImage3D(struct gl_context *ctx,
1135 struct gl_texture_image *texImage,
1136 GLint xoffset, GLint yoffset, GLint zoffset,
1137 struct gl_renderbuffer *rb,
1138 GLint x, GLint y, GLsizei width, GLsizei height)
1139 {
1140 st_copy_texsubimage(ctx, texImage,
1141 xoffset, yoffset, zoffset, /* destX,Y,Z */
1142 rb, x, y, width, height); /* src X, Y, size */
1143 }
1144
1145
1146 /**
1147 * Copy image data from stImage into the texture object 'stObj' at level
1148 * 'dstLevel'.
1149 */
1150 static void
1151 copy_image_data_to_texture(struct st_context *st,
1152 struct st_texture_object *stObj,
1153 GLuint dstLevel,
1154 struct st_texture_image *stImage)
1155 {
1156 /* debug checks */
1157 {
1158 const struct gl_texture_image *dstImage =
1159 stObj->base.Image[stImage->base.Face][dstLevel];
1160 assert(dstImage);
1161 assert(dstImage->Width == stImage->base.Width);
1162 assert(dstImage->Height == stImage->base.Height);
1163 assert(dstImage->Depth == stImage->base.Depth);
1164 }
1165
1166 if (stImage->pt) {
1167 /* Copy potentially with the blitter:
1168 */
1169 GLuint src_level;
1170 if (stImage->pt != stObj->pt)
1171 src_level = 0;
1172 else
1173 src_level = stImage->base.Level;
1174
1175 st_texture_image_copy(st->pipe,
1176 stObj->pt, dstLevel, /* dest texture, level */
1177 stImage->pt, src_level, /* src texture, level */
1178 stImage->base.Face);
1179
1180 pipe_resource_reference(&stImage->pt, NULL);
1181 }
1182 else if (stImage->TexData) {
1183 /* Copy from malloc'd memory */
1184 /* XXX this should be re-examined/tested with a compressed format */
1185 GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1186 GLuint srcRowStride = stImage->base.Width * blockSize;
1187 GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1188 st_texture_image_data(st,
1189 stObj->pt,
1190 stImage->base.Face,
1191 dstLevel,
1192 stImage->TexData,
1193 srcRowStride,
1194 srcSliceStride);
1195 _mesa_align_free(stImage->TexData);
1196 stImage->TexData = NULL;
1197 }
1198
1199 pipe_resource_reference(&stImage->pt, stObj->pt);
1200 }
1201
1202
1203 /**
1204 * Called during state validation. When this function is finished,
1205 * the texture object should be ready for rendering.
1206 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1207 */
1208 GLboolean
1209 st_finalize_texture(struct gl_context *ctx,
1210 struct pipe_context *pipe,
1211 struct gl_texture_object *tObj)
1212 {
1213 struct st_context *st = st_context(ctx);
1214 struct st_texture_object *stObj = st_texture_object(tObj);
1215 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1216 GLuint face;
1217 struct st_texture_image *firstImage;
1218 enum pipe_format firstImageFormat;
1219 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1220
1221 if (stObj->base._Complete) {
1222 /* The texture is complete and we know exactly how many mipmap levels
1223 * are present/needed. This is conditional because we may be called
1224 * from the st_generate_mipmap() function when the texture object is
1225 * incomplete. In that case, we'll have set stObj->lastLevel before
1226 * we get here.
1227 */
1228 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1229 stObj->base.Sampler.MinFilter == GL_NEAREST)
1230 stObj->lastLevel = stObj->base.BaseLevel;
1231 else
1232 stObj->lastLevel = stObj->base._MaxLevel;
1233 }
1234
1235 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1236 assert(firstImage);
1237
1238 /* If both firstImage and stObj point to a texture which can contain
1239 * all active images, favour firstImage. Note that because of the
1240 * completeness requirement, we know that the image dimensions
1241 * will match.
1242 */
1243 if (firstImage->pt &&
1244 firstImage->pt != stObj->pt &&
1245 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1246 pipe_resource_reference(&stObj->pt, firstImage->pt);
1247 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1248 }
1249
1250 /* Find gallium format for the Mesa texture */
1251 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1252
1253 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1254 {
1255 GLuint width, height, depth;
1256 if (!guess_base_level_size(stObj->base.Target,
1257 firstImage->base.Width2,
1258 firstImage->base.Height2,
1259 firstImage->base.Depth2,
1260 firstImage->base.Level,
1261 &width, &height, &depth)) {
1262 width = stObj->width0;
1263 height = stObj->height0;
1264 depth = stObj->depth0;
1265 }
1266 /* convert GL dims to Gallium dims */
1267 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1268 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1269 }
1270
1271 /* If we already have a gallium texture, check that it matches the texture
1272 * object's format, target, size, num_levels, etc.
1273 */
1274 if (stObj->pt) {
1275 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1276 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1277 stObj->pt->last_level < stObj->lastLevel ||
1278 stObj->pt->width0 != ptWidth ||
1279 stObj->pt->height0 != ptHeight ||
1280 stObj->pt->depth0 != ptDepth ||
1281 stObj->pt->array_size != ptLayers)
1282 {
1283 /* The gallium texture does not match the Mesa texture so delete the
1284 * gallium texture now. We'll make a new one below.
1285 */
1286 pipe_resource_reference(&stObj->pt, NULL);
1287 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1288 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1289 }
1290 }
1291
1292 /* May need to create a new gallium texture:
1293 */
1294 if (!stObj->pt) {
1295 GLuint bindings = default_bindings(st, firstImageFormat);
1296
1297 stObj->pt = st_texture_create(st,
1298 gl_target_to_pipe(stObj->base.Target),
1299 firstImageFormat,
1300 stObj->lastLevel,
1301 ptWidth,
1302 ptHeight,
1303 ptDepth,
1304 ptLayers,
1305 bindings);
1306
1307 if (!stObj->pt) {
1308 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1309 return GL_FALSE;
1310 }
1311 }
1312
1313 /* Pull in any images not in the object's texture:
1314 */
1315 for (face = 0; face < nr_faces; face++) {
1316 GLuint level;
1317 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1318 struct st_texture_image *stImage =
1319 st_texture_image(stObj->base.Image[face][level]);
1320
1321 /* Need to import images in main memory or held in other textures.
1322 */
1323 if (stImage && stObj->pt != stImage->pt) {
1324 if (level == 0 ||
1325 (stImage->base.Width == u_minify(stObj->width0, level) &&
1326 stImage->base.Height == u_minify(stObj->height0, level) &&
1327 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1328 /* src image fits expected dest mipmap level size */
1329 copy_image_data_to_texture(st, stObj, level, stImage);
1330 }
1331 }
1332 }
1333 }
1334
1335 return GL_TRUE;
1336 }
1337
1338
1339 /**
1340 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1341 * for a whole mipmap stack.
1342 */
1343 static GLboolean
1344 st_AllocTextureStorage(struct gl_context *ctx,
1345 struct gl_texture_object *texObj,
1346 GLsizei levels, GLsizei width,
1347 GLsizei height, GLsizei depth)
1348 {
1349 const GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1350 struct st_context *st = st_context(ctx);
1351 struct st_texture_object *stObj = st_texture_object(texObj);
1352 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1353 enum pipe_format fmt;
1354 GLint level;
1355
1356 assert(levels > 0);
1357
1358 /* Save the level=0 dimensions */
1359 stObj->width0 = width;
1360 stObj->height0 = height;
1361 stObj->depth0 = depth;
1362 stObj->lastLevel = levels - 1;
1363
1364 fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
1365
1366 bindings = default_bindings(st, fmt);
1367
1368 st_gl_texture_dims_to_pipe_dims(texObj->Target,
1369 width, height, depth,
1370 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1371
1372 stObj->pt = st_texture_create(st,
1373 gl_target_to_pipe(texObj->Target),
1374 fmt,
1375 levels,
1376 ptWidth,
1377 ptHeight,
1378 ptDepth,
1379 ptLayers,
1380 bindings);
1381 if (!stObj->pt)
1382 return GL_FALSE;
1383
1384 /* Set image resource pointers */
1385 for (level = 0; level < levels; level++) {
1386 GLuint face;
1387 for (face = 0; face < numFaces; face++) {
1388 struct st_texture_image *stImage =
1389 st_texture_image(texObj->Image[face][level]);
1390 pipe_resource_reference(&stImage->pt, stObj->pt);
1391 }
1392 }
1393
1394 return GL_TRUE;
1395 }
1396
1397
1398
1399 void
1400 st_init_texture_functions(struct dd_function_table *functions)
1401 {
1402 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1403 functions->TexImage1D = st_TexImage1D;
1404 functions->TexImage2D = st_TexImage2D;
1405 functions->TexImage3D = st_TexImage3D;
1406 functions->TexSubImage1D = _mesa_store_texsubimage1d;
1407 functions->TexSubImage2D = _mesa_store_texsubimage2d;
1408 functions->TexSubImage3D = _mesa_store_texsubimage3d;
1409 functions->CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
1410 functions->CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
1411 functions->CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
1412 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1413 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1414 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1415 functions->GenerateMipmap = st_generate_mipmap;
1416
1417 functions->GetTexImage = st_GetTexImage;
1418
1419 /* compressed texture functions */
1420 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1421 functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1422
1423 functions->NewTextureObject = st_NewTextureObject;
1424 functions->NewTextureImage = st_NewTextureImage;
1425 functions->DeleteTextureImage = st_DeleteTextureImage;
1426 functions->DeleteTexture = st_DeleteTextureObject;
1427 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1428 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1429 functions->MapTextureImage = st_MapTextureImage;
1430 functions->UnmapTextureImage = st_UnmapTextureImage;
1431
1432 /* XXX Temporary until we can query pipe's texture sizes */
1433 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1434
1435 functions->AllocTextureStorage = st_AllocTextureStorage;
1436 }