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