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