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