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