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