st/mesa: use private pipe_sampler_view in decompress_with_blit()
[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 const GLuint width = texImage->Width;
614 const GLuint height = texImage->Height;
615 struct pipe_surface *dst_surface;
616 struct pipe_resource *dst_texture;
617 struct pipe_transfer *tex_xfer;
618 unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
619 PIPE_BIND_TRANSFER_READ);
620
621 /* create temp / dest surface */
622 if (!util_create_rgba_surface(pipe, width, height, bind,
623 &dst_texture, &dst_surface)) {
624 _mesa_problem(ctx, "util_create_rgba_surface() failed "
625 "in decompress_with_blit()");
626 return;
627 }
628
629 /* Disable conditional rendering. */
630 if (st->render_condition) {
631 pipe->render_condition(pipe, NULL, 0);
632 }
633
634 /* Create sampler view that limits fetches to the source mipmap level */
635 {
636 struct pipe_sampler_view sv_temp;
637
638 u_sampler_view_default_template(&sv_temp, stObj->pt, stObj->pt->format);
639
640 sv_temp.u.tex.first_level =
641 sv_temp.u.tex.last_level = texImage->Level;
642
643 src_view = pipe->create_sampler_view(pipe, stObj->pt, &sv_temp);
644 if (!src_view) {
645 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
646 return;
647 }
648 }
649
650 /* blit/render/decompress */
651 util_blit_pixels_tex(st->blit,
652 src_view, /* pipe_resource (src) */
653 0, 0, /* src x0, y0 */
654 width, height, /* src x1, y1 */
655 dst_surface, /* pipe_surface (dst) */
656 0, 0, /* dst x0, y0 */
657 width, height, /* dst x1, y1 */
658 0.0, /* z */
659 PIPE_TEX_MIPFILTER_NEAREST);
660
661 /* Restore conditional rendering state. */
662 if (st->render_condition) {
663 pipe->render_condition(pipe, st->render_condition,
664 st->condition_mode);
665 }
666
667 /* map the dst_surface so we can read from it */
668 tex_xfer = pipe_get_transfer(pipe,
669 dst_texture, 0, 0,
670 PIPE_TRANSFER_READ,
671 0, 0, width, height);
672
673 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
674
675 /* copy/pack data into user buffer */
676 if (st_equal_formats(stImage->pt->format, format, type)) {
677 /* memcpy */
678 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
679 ubyte *map = pipe_transfer_map(pipe, tex_xfer);
680 GLuint row;
681 for (row = 0; row < height; row++) {
682 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
683 height, format, type, row, 0);
684 memcpy(dest, map, bytesPerRow);
685 map += tex_xfer->stride;
686 }
687 pipe_transfer_unmap(pipe, tex_xfer);
688 }
689 else {
690 /* format translation via floats */
691 GLuint row;
692 enum pipe_format pformat = util_format_linear(dst_texture->format);
693 for (row = 0; row < height; row++) {
694 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
695 GLfloat rgba[4 * MAX_WIDTH];
696 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
697 height, format, type, row, 0);
698
699 if (ST_DEBUG & DEBUG_FALLBACK)
700 debug_printf("%s: fallback format translation\n", __FUNCTION__);
701
702 /* get float[4] rgba row from surface */
703 pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
704 pformat, rgba);
705
706 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
707 type, dest, &ctx->Pack, transferOps);
708 }
709 }
710
711 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
712
713 pipe->transfer_destroy(pipe, tex_xfer);
714
715 /* destroy the temp / dest surface */
716 util_destroy_rgba_surface(dst_texture, dst_surface);
717
718 pipe_sampler_view_reference(&src_view, NULL);
719 }
720
721
722
723 /**
724 * Called via ctx->Driver.GetTexImage()
725 */
726 static void
727 st_GetTexImage(struct gl_context * ctx,
728 GLenum format, GLenum type, GLvoid * pixels,
729 struct gl_texture_image *texImage)
730 {
731 struct st_texture_image *stImage = st_texture_image(texImage);
732
733 if (stImage->pt && util_format_is_s3tc(stImage->pt->format)) {
734 /* Need to decompress the texture.
735 * We'll do this by rendering a textured quad (which is hopefully
736 * faster than using the fallback code in texcompress.c).
737 * Note that we only expect RGBA formats (no Z/depth formats).
738 */
739 decompress_with_blit(ctx, format, type, pixels, texImage);
740 }
741 else {
742 _mesa_get_teximage(ctx, format, type, pixels, texImage);
743 }
744 }
745
746
747 /**
748 * Do a CopyTexSubImage operation using a read transfer from the source,
749 * a write transfer to the destination and get_tile()/put_tile() to access
750 * the pixels/texels.
751 *
752 * Note: srcY=0=TOP of renderbuffer
753 */
754 static void
755 fallback_copy_texsubimage(struct gl_context *ctx,
756 struct st_renderbuffer *strb,
757 struct st_texture_image *stImage,
758 GLenum baseFormat,
759 GLint destX, GLint destY, GLint destZ,
760 GLint srcX, GLint srcY,
761 GLsizei width, GLsizei height)
762 {
763 struct st_context *st = st_context(ctx);
764 struct pipe_context *pipe = st->pipe;
765 struct pipe_transfer *src_trans;
766 GLvoid *texDest;
767 enum pipe_transfer_usage transfer_usage;
768
769 if (ST_DEBUG & DEBUG_FALLBACK)
770 debug_printf("%s: fallback processing\n", __FUNCTION__);
771
772 assert(width <= MAX_WIDTH);
773
774 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
775 srcY = strb->Base.Height - srcY - height;
776 }
777
778 src_trans = pipe_get_transfer(pipe,
779 strb->texture,
780 strb->rtt_level,
781 strb->rtt_face + strb->rtt_slice,
782 PIPE_TRANSFER_READ,
783 srcX, srcY,
784 width, height);
785
786 if ((baseFormat == GL_DEPTH_COMPONENT ||
787 baseFormat == GL_DEPTH_STENCIL) &&
788 util_format_is_depth_and_stencil(stImage->pt->format))
789 transfer_usage = PIPE_TRANSFER_READ_WRITE;
790 else
791 transfer_usage = PIPE_TRANSFER_WRITE;
792
793 /* XXX this used to ignore destZ param */
794 texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
795 destX, destY, width, height);
796
797 if (baseFormat == GL_DEPTH_COMPONENT ||
798 baseFormat == GL_DEPTH_STENCIL) {
799 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
800 ctx->Pixel.DepthBias != 0.0F);
801 GLint row, yStep;
802
803 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
804 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
805 srcY = height - 1;
806 yStep = -1;
807 }
808 else {
809 srcY = 0;
810 yStep = 1;
811 }
812
813 /* To avoid a large temp memory allocation, do copy row by row */
814 for (row = 0; row < height; row++, srcY += yStep) {
815 uint data[MAX_WIDTH];
816 pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
817 if (scaleOrBias) {
818 _mesa_scale_and_bias_depth_uint(ctx, width, data);
819 }
820 pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
821 }
822 }
823 else {
824 /* RGBA format */
825 GLfloat *tempSrc =
826 (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
827
828 if (tempSrc && texDest) {
829 const GLint dims = 2;
830 const GLint dstRowStride = stImage->transfer->stride;
831 struct gl_texture_image *texImage = &stImage->base;
832 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
833
834 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
835 unpack.Invert = GL_TRUE;
836 }
837
838 /* get float/RGBA image from framebuffer */
839 /* XXX this usually involves a lot of int/float conversion.
840 * try to avoid that someday.
841 */
842 pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
843 util_format_linear(strb->texture->format),
844 tempSrc);
845
846 /* Store into texture memory.
847 * Note that this does some special things such as pixel transfer
848 * ops and format conversion. In particular, if the dest tex format
849 * is actually RGBA but the user created the texture as GL_RGB we
850 * need to fill-in/override the alpha channel with 1.0.
851 */
852 _mesa_texstore(ctx, dims,
853 texImage->_BaseFormat,
854 texImage->TexFormat,
855 dstRowStride,
856 (GLubyte **) &texDest,
857 width, height, 1,
858 GL_RGBA, GL_FLOAT, tempSrc, /* src */
859 &unpack);
860 }
861 else {
862 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
863 }
864
865 if (tempSrc)
866 free(tempSrc);
867 }
868
869 st_texture_image_unmap(st, stImage);
870 pipe->transfer_destroy(pipe, src_trans);
871 }
872
873
874
875 /**
876 * If the format of the src renderbuffer and the format of the dest
877 * texture are compatible (in terms of blitting), return a TGSI writemask
878 * to be used during the blit.
879 * If the src/dest are incompatible, return 0.
880 */
881 static unsigned
882 compatible_src_dst_formats(struct gl_context *ctx,
883 const struct gl_renderbuffer *src,
884 const struct gl_texture_image *dst)
885 {
886 /* Get logical base formats for the src and dest.
887 * That is, use the user-requested formats and not the actual, device-
888 * chosen formats.
889 * For example, the user may have requested an A8 texture but the
890 * driver may actually be using an RGBA texture format. When we
891 * copy/blit to that texture, we only want to copy the Alpha channel
892 * and not the RGB channels.
893 *
894 * Similarly, when the src FBO was created an RGB format may have been
895 * requested but the driver actually chose an RGBA format. In that case,
896 * we don't want to copy the undefined Alpha channel to the dest texture
897 * (it should be 1.0).
898 */
899 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
900 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
901
902 /**
903 * XXX when we have red-only and red/green renderbuffers we'll need
904 * to add more cases here (or implement a general-purpose routine that
905 * queries the existance of the R,G,B,A channels in the src and dest).
906 */
907 if (srcFormat == dstFormat) {
908 /* This is the same as matching_base_formats, which should
909 * always pass, as it did previously.
910 */
911 return TGSI_WRITEMASK_XYZW;
912 }
913 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
914 /* Make sure that A in the dest is 1. The actual src format
915 * may be RGBA and have undefined A values.
916 */
917 return TGSI_WRITEMASK_XYZ;
918 }
919 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
920 /* Make sure that A in the dest is 1. The actual dst format
921 * may be RGBA and will need A=1 to provide proper alpha values
922 * when sampled later.
923 */
924 return TGSI_WRITEMASK_XYZ;
925 }
926 else {
927 if (ST_DEBUG & DEBUG_FALLBACK)
928 debug_printf("%s failed for src %s, dst %s\n",
929 __FUNCTION__,
930 _mesa_lookup_enum_by_nr(srcFormat),
931 _mesa_lookup_enum_by_nr(dstFormat));
932
933 /* Otherwise fail.
934 */
935 return 0;
936 }
937 }
938
939
940
941 /**
942 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
943 * Note that the region to copy has already been clipped so we know we
944 * won't read from outside the source renderbuffer's bounds.
945 *
946 * Note: srcY=0=Bottom of renderbuffer (GL convention)
947 */
948 static void
949 st_copy_texsubimage(struct gl_context *ctx,
950 struct gl_texture_image *texImage,
951 GLint destX, GLint destY, GLint destZ,
952 struct gl_renderbuffer *rb,
953 GLint srcX, GLint srcY,
954 GLsizei width, GLsizei height)
955 {
956 struct st_texture_image *stImage = st_texture_image(texImage);
957 const GLenum texBaseFormat = texImage->_BaseFormat;
958 struct gl_framebuffer *fb = ctx->ReadBuffer;
959 struct st_renderbuffer *strb;
960 struct st_context *st = st_context(ctx);
961 struct pipe_context *pipe = st->pipe;
962 struct pipe_screen *screen = pipe->screen;
963 enum pipe_format dest_format, src_format;
964 GLboolean matching_base_formats;
965 GLuint format_writemask, sample_count;
966 struct pipe_surface *dest_surface = NULL;
967 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
968 struct pipe_surface surf_tmpl;
969 unsigned int dst_usage;
970 GLint srcY0, srcY1;
971
972 /* make sure finalize_textures has been called?
973 */
974 if (0) st_validate_state(st);
975
976 /* determine if copying depth or color data */
977 if (texBaseFormat == GL_DEPTH_COMPONENT ||
978 texBaseFormat == GL_DEPTH_STENCIL) {
979 strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
980 }
981 else {
982 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
983 strb = st_renderbuffer(fb->_ColorReadBuffer);
984 }
985
986 if (!strb || !strb->surface || !stImage->pt) {
987 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
988 return;
989 }
990
991 sample_count = strb->surface->texture->nr_samples;
992 /* I believe this would be legal, presumably would need to do a resolve
993 for color, and for depth/stencil spec says to just use one of the
994 depth/stencil samples per pixel? Need some transfer clarifications. */
995 assert(sample_count < 2);
996
997 assert(strb);
998 assert(strb->surface);
999 assert(stImage->pt);
1000
1001 src_format = strb->surface->format;
1002 dest_format = stImage->pt->format;
1003
1004 /*
1005 * Determine if the src framebuffer and dest texture have the same
1006 * base format. We need this to detect a case such as the framebuffer
1007 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1008 * texture format stores RGBA we need to set A=1 (overriding the
1009 * framebuffer's alpha values). We can't do that with the blit or
1010 * textured-quad paths.
1011 */
1012 matching_base_formats =
1013 (_mesa_get_format_base_format(strb->Base.Format) ==
1014 _mesa_get_format_base_format(texImage->TexFormat));
1015
1016 if (ctx->_ImageTransferState) {
1017 goto fallback;
1018 }
1019
1020 if (matching_base_formats &&
1021 src_format == dest_format &&
1022 !do_flip) {
1023 /* use surface_copy() / blit */
1024 struct pipe_box src_box;
1025 u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1026 width, height, &src_box);
1027
1028 /* for resource_copy_region(), y=0=top, always */
1029 pipe->resource_copy_region(pipe,
1030 /* dest */
1031 stImage->pt,
1032 stImage->base.Level,
1033 destX, destY, destZ + stImage->base.Face,
1034 /* src */
1035 strb->texture,
1036 strb->surface->u.tex.level,
1037 &src_box);
1038 return;
1039 }
1040
1041 if (texBaseFormat == GL_DEPTH_STENCIL) {
1042 goto fallback;
1043 }
1044
1045 if (texBaseFormat == GL_DEPTH_COMPONENT) {
1046 format_writemask = TGSI_WRITEMASK_XYZW;
1047 dst_usage = PIPE_BIND_DEPTH_STENCIL;
1048 }
1049 else {
1050 format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1051 dst_usage = PIPE_BIND_RENDER_TARGET;
1052 }
1053
1054 if (!format_writemask ||
1055 !screen->is_format_supported(screen, src_format,
1056 PIPE_TEXTURE_2D, sample_count,
1057 PIPE_BIND_SAMPLER_VIEW) ||
1058 !screen->is_format_supported(screen, dest_format,
1059 PIPE_TEXTURE_2D, 0,
1060 dst_usage)) {
1061 goto fallback;
1062 }
1063
1064 if (do_flip) {
1065 srcY1 = strb->Base.Height - srcY - height;
1066 srcY0 = srcY1 + height;
1067 }
1068 else {
1069 srcY0 = srcY;
1070 srcY1 = srcY0 + height;
1071 }
1072
1073 /* Disable conditional rendering. */
1074 if (st->render_condition) {
1075 pipe->render_condition(pipe, NULL, 0);
1076 }
1077
1078 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1079 surf_tmpl.format = util_format_linear(stImage->pt->format);
1080 surf_tmpl.usage = dst_usage;
1081 surf_tmpl.u.tex.level = stImage->base.Level;
1082 surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ;
1083 surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ;
1084
1085 dest_surface = pipe->create_surface(pipe, stImage->pt,
1086 &surf_tmpl);
1087 util_blit_pixels_writemask(st->blit,
1088 strb->texture,
1089 strb->surface->u.tex.level,
1090 srcX, srcY0,
1091 srcX + width, srcY1,
1092 strb->surface->u.tex.first_layer,
1093 dest_surface,
1094 destX, destY,
1095 destX + width, destY + height,
1096 0.0, PIPE_TEX_MIPFILTER_NEAREST,
1097 format_writemask);
1098 pipe_surface_reference(&dest_surface, NULL);
1099
1100 /* Restore conditional rendering state. */
1101 if (st->render_condition) {
1102 pipe->render_condition(pipe, st->render_condition,
1103 st->condition_mode);
1104 }
1105
1106 return;
1107
1108 fallback:
1109 /* software fallback */
1110 fallback_copy_texsubimage(ctx,
1111 strb, stImage, texBaseFormat,
1112 destX, destY, destZ,
1113 srcX, srcY, width, height);
1114 }
1115
1116
1117
1118 static void
1119 st_CopyTexSubImage1D(struct gl_context *ctx,
1120 struct gl_texture_image *texImage,
1121 GLint xoffset,
1122 struct gl_renderbuffer *rb,
1123 GLint x, GLint y, GLsizei width)
1124 {
1125 const GLint yoffset = 0, zoffset = 0;
1126 const GLsizei height = 1;
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_CopyTexSubImage2D(struct gl_context *ctx,
1135 struct gl_texture_image *texImage,
1136 GLint xoffset, GLint yoffset,
1137 struct gl_renderbuffer *rb,
1138 GLint x, GLint y, GLsizei width, GLsizei height)
1139 {
1140 const GLint zoffset = 0;
1141 st_copy_texsubimage(ctx, texImage,
1142 xoffset, yoffset, zoffset, /* destX,Y,Z */
1143 rb, x, y, width, height); /* src X, Y, size */
1144 }
1145
1146
1147 static void
1148 st_CopyTexSubImage3D(struct gl_context *ctx,
1149 struct gl_texture_image *texImage,
1150 GLint xoffset, GLint yoffset, GLint zoffset,
1151 struct gl_renderbuffer *rb,
1152 GLint x, GLint y, GLsizei width, GLsizei height)
1153 {
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 /**
1161 * Copy image data from stImage into the texture object 'stObj' at level
1162 * 'dstLevel'.
1163 */
1164 static void
1165 copy_image_data_to_texture(struct st_context *st,
1166 struct st_texture_object *stObj,
1167 GLuint dstLevel,
1168 struct st_texture_image *stImage)
1169 {
1170 /* debug checks */
1171 {
1172 const struct gl_texture_image *dstImage =
1173 stObj->base.Image[stImage->base.Face][dstLevel];
1174 assert(dstImage);
1175 assert(dstImage->Width == stImage->base.Width);
1176 assert(dstImage->Height == stImage->base.Height);
1177 assert(dstImage->Depth == stImage->base.Depth);
1178 }
1179
1180 if (stImage->pt) {
1181 /* Copy potentially with the blitter:
1182 */
1183 GLuint src_level;
1184 if (stImage->pt != stObj->pt)
1185 src_level = 0;
1186 else
1187 src_level = stImage->base.Level;
1188
1189 st_texture_image_copy(st->pipe,
1190 stObj->pt, dstLevel, /* dest texture, level */
1191 stImage->pt, src_level, /* src texture, level */
1192 stImage->base.Face);
1193
1194 pipe_resource_reference(&stImage->pt, NULL);
1195 }
1196 else if (stImage->TexData) {
1197 /* Copy from malloc'd memory */
1198 /* XXX this should be re-examined/tested with a compressed format */
1199 GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1200 GLuint srcRowStride = stImage->base.Width * blockSize;
1201 GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1202 st_texture_image_data(st,
1203 stObj->pt,
1204 stImage->base.Face,
1205 dstLevel,
1206 stImage->TexData,
1207 srcRowStride,
1208 srcSliceStride);
1209 _mesa_align_free(stImage->TexData);
1210 stImage->TexData = NULL;
1211 }
1212
1213 pipe_resource_reference(&stImage->pt, stObj->pt);
1214 }
1215
1216
1217 /**
1218 * Called during state validation. When this function is finished,
1219 * the texture object should be ready for rendering.
1220 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1221 */
1222 GLboolean
1223 st_finalize_texture(struct gl_context *ctx,
1224 struct pipe_context *pipe,
1225 struct gl_texture_object *tObj)
1226 {
1227 struct st_context *st = st_context(ctx);
1228 struct st_texture_object *stObj = st_texture_object(tObj);
1229 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1230 GLuint face;
1231 struct st_texture_image *firstImage;
1232 enum pipe_format firstImageFormat;
1233 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1234
1235 if (stObj->base._Complete) {
1236 /* The texture is complete and we know exactly how many mipmap levels
1237 * are present/needed. This is conditional because we may be called
1238 * from the st_generate_mipmap() function when the texture object is
1239 * incomplete. In that case, we'll have set stObj->lastLevel before
1240 * we get here.
1241 */
1242 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1243 stObj->base.Sampler.MinFilter == GL_NEAREST)
1244 stObj->lastLevel = stObj->base.BaseLevel;
1245 else
1246 stObj->lastLevel = stObj->base._MaxLevel;
1247 }
1248
1249 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1250 assert(firstImage);
1251
1252 /* If both firstImage and stObj point to a texture which can contain
1253 * all active images, favour firstImage. Note that because of the
1254 * completeness requirement, we know that the image dimensions
1255 * will match.
1256 */
1257 if (firstImage->pt &&
1258 firstImage->pt != stObj->pt &&
1259 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1260 pipe_resource_reference(&stObj->pt, firstImage->pt);
1261 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1262 }
1263
1264 /* Find gallium format for the Mesa texture */
1265 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1266
1267 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1268 {
1269 GLuint width, height, depth;
1270 if (!guess_base_level_size(stObj->base.Target,
1271 firstImage->base.Width2,
1272 firstImage->base.Height2,
1273 firstImage->base.Depth2,
1274 firstImage->base.Level,
1275 &width, &height, &depth)) {
1276 width = stObj->width0;
1277 height = stObj->height0;
1278 depth = stObj->depth0;
1279 }
1280 /* convert GL dims to Gallium dims */
1281 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1282 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1283 }
1284
1285 /* If we already have a gallium texture, check that it matches the texture
1286 * object's format, target, size, num_levels, etc.
1287 */
1288 if (stObj->pt) {
1289 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1290 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1291 stObj->pt->last_level < stObj->lastLevel ||
1292 stObj->pt->width0 != ptWidth ||
1293 stObj->pt->height0 != ptHeight ||
1294 stObj->pt->depth0 != ptDepth ||
1295 stObj->pt->array_size != ptLayers)
1296 {
1297 /* The gallium texture does not match the Mesa texture so delete the
1298 * gallium texture now. We'll make a new one below.
1299 */
1300 pipe_resource_reference(&stObj->pt, NULL);
1301 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1302 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1303 }
1304 }
1305
1306 /* May need to create a new gallium texture:
1307 */
1308 if (!stObj->pt) {
1309 GLuint bindings = default_bindings(st, firstImageFormat);
1310
1311 stObj->pt = st_texture_create(st,
1312 gl_target_to_pipe(stObj->base.Target),
1313 firstImageFormat,
1314 stObj->lastLevel,
1315 ptWidth,
1316 ptHeight,
1317 ptDepth,
1318 ptLayers,
1319 bindings);
1320
1321 if (!stObj->pt) {
1322 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1323 return GL_FALSE;
1324 }
1325 }
1326
1327 /* Pull in any images not in the object's texture:
1328 */
1329 for (face = 0; face < nr_faces; face++) {
1330 GLuint level;
1331 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1332 struct st_texture_image *stImage =
1333 st_texture_image(stObj->base.Image[face][level]);
1334
1335 /* Need to import images in main memory or held in other textures.
1336 */
1337 if (stImage && stObj->pt != stImage->pt) {
1338 if (level == 0 ||
1339 (stImage->base.Width == u_minify(stObj->width0, level) &&
1340 stImage->base.Height == u_minify(stObj->height0, level) &&
1341 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1342 /* src image fits expected dest mipmap level size */
1343 copy_image_data_to_texture(st, stObj, level, stImage);
1344 }
1345 }
1346 }
1347 }
1348
1349 return GL_TRUE;
1350 }
1351
1352
1353 /**
1354 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1355 * for a whole mipmap stack.
1356 */
1357 static GLboolean
1358 st_AllocTextureStorage(struct gl_context *ctx,
1359 struct gl_texture_object *texObj,
1360 GLsizei levels, GLsizei width,
1361 GLsizei height, GLsizei depth)
1362 {
1363 const GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1364 struct st_context *st = st_context(ctx);
1365 struct st_texture_object *stObj = st_texture_object(texObj);
1366 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1367 enum pipe_format fmt;
1368 GLint level;
1369
1370 assert(levels > 0);
1371
1372 /* Save the level=0 dimensions */
1373 stObj->width0 = width;
1374 stObj->height0 = height;
1375 stObj->depth0 = depth;
1376 stObj->lastLevel = levels - 1;
1377
1378 fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
1379
1380 bindings = default_bindings(st, fmt);
1381
1382 st_gl_texture_dims_to_pipe_dims(texObj->Target,
1383 width, height, depth,
1384 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1385
1386 stObj->pt = st_texture_create(st,
1387 gl_target_to_pipe(texObj->Target),
1388 fmt,
1389 levels,
1390 ptWidth,
1391 ptHeight,
1392 ptDepth,
1393 ptLayers,
1394 bindings);
1395 if (!stObj->pt)
1396 return GL_FALSE;
1397
1398 /* Set image resource pointers */
1399 for (level = 0; level < levels; level++) {
1400 GLuint face;
1401 for (face = 0; face < numFaces; face++) {
1402 struct st_texture_image *stImage =
1403 st_texture_image(texObj->Image[face][level]);
1404 pipe_resource_reference(&stImage->pt, stObj->pt);
1405 }
1406 }
1407
1408 return GL_TRUE;
1409 }
1410
1411
1412
1413 void
1414 st_init_texture_functions(struct dd_function_table *functions)
1415 {
1416 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1417 functions->TexImage1D = st_TexImage1D;
1418 functions->TexImage2D = st_TexImage2D;
1419 functions->TexImage3D = st_TexImage3D;
1420 functions->TexSubImage1D = _mesa_store_texsubimage1d;
1421 functions->TexSubImage2D = _mesa_store_texsubimage2d;
1422 functions->TexSubImage3D = _mesa_store_texsubimage3d;
1423 functions->CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
1424 functions->CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
1425 functions->CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
1426 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1427 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1428 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1429 functions->GenerateMipmap = st_generate_mipmap;
1430
1431 functions->GetTexImage = st_GetTexImage;
1432
1433 /* compressed texture functions */
1434 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1435 functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1436
1437 functions->NewTextureObject = st_NewTextureObject;
1438 functions->NewTextureImage = st_NewTextureImage;
1439 functions->DeleteTextureImage = st_DeleteTextureImage;
1440 functions->DeleteTexture = st_DeleteTextureObject;
1441 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1442 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1443 functions->MapTextureImage = st_MapTextureImage;
1444 functions->UnmapTextureImage = st_UnmapTextureImage;
1445
1446 /* XXX Temporary until we can query pipe's texture sizes */
1447 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1448
1449 functions->AllocTextureStorage = st_AllocTextureStorage;
1450 }