st/mesa: add support for ARB_texture_multisample (v3)
[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/bufferobj.h"
29 #include "main/enums.h"
30 #include "main/fbobject.h"
31 #include "main/formats.h"
32 #include "main/image.h"
33 #include "main/imports.h"
34 #include "main/macros.h"
35 #include "main/mipmap.h"
36 #include "main/pack.h"
37 #include "main/pbo.h"
38 #include "main/pixeltransfer.h"
39 #include "main/texcompress.h"
40 #include "main/texgetimage.h"
41 #include "main/teximage.h"
42 #include "main/texobj.h"
43 #include "main/texstore.h"
44
45 #include "state_tracker/st_debug.h"
46 #include "state_tracker/st_context.h"
47 #include "state_tracker/st_cb_fbo.h"
48 #include "state_tracker/st_cb_flush.h"
49 #include "state_tracker/st_cb_texture.h"
50 #include "state_tracker/st_cb_bufferobjects.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_format.h"
62 #include "util/u_surface.h"
63 #include "util/u_sampler.h"
64 #include "util/u_math.h"
65 #include "util/u_box.h"
66
67 #define DBG if (0) printf
68
69
70 enum pipe_texture_target
71 gl_target_to_pipe(GLenum target)
72 {
73 switch (target) {
74 case GL_TEXTURE_1D:
75 case GL_PROXY_TEXTURE_1D:
76 return PIPE_TEXTURE_1D;
77 case GL_TEXTURE_2D:
78 case GL_PROXY_TEXTURE_2D:
79 case GL_TEXTURE_EXTERNAL_OES:
80 case GL_TEXTURE_2D_MULTISAMPLE:
81 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
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 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
104 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
105 return PIPE_TEXTURE_2D_ARRAY;
106 case GL_TEXTURE_BUFFER:
107 return PIPE_BUFFER;
108 case GL_TEXTURE_CUBE_MAP_ARRAY:
109 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
110 return PIPE_TEXTURE_CUBE_ARRAY;
111 default:
112 assert(0);
113 return 0;
114 }
115 }
116
117
118 /** called via ctx->Driver.NewTextureImage() */
119 static struct gl_texture_image *
120 st_NewTextureImage(struct gl_context * ctx)
121 {
122 DBG("%s\n", __FUNCTION__);
123 (void) ctx;
124 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
125 }
126
127
128 /** called via ctx->Driver.DeleteTextureImage() */
129 static void
130 st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
131 {
132 /* nothing special (yet) for st_texture_image */
133 _mesa_delete_texture_image(ctx, img);
134 }
135
136
137 /** called via ctx->Driver.NewTextureObject() */
138 static struct gl_texture_object *
139 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
140 {
141 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
142
143 DBG("%s\n", __FUNCTION__);
144 _mesa_initialize_texture_object(&obj->base, name, target);
145
146 return &obj->base;
147 }
148
149 /** called via ctx->Driver.DeleteTextureObject() */
150 static void
151 st_DeleteTextureObject(struct gl_context *ctx,
152 struct gl_texture_object *texObj)
153 {
154 struct st_context *st = st_context(ctx);
155 struct st_texture_object *stObj = st_texture_object(texObj);
156 if (stObj->pt)
157 pipe_resource_reference(&stObj->pt, NULL);
158 if (stObj->sampler_view) {
159 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
160 }
161 _mesa_delete_texture_object(ctx, texObj);
162 }
163
164
165 /** called via ctx->Driver.FreeTextureImageBuffer() */
166 static void
167 st_FreeTextureImageBuffer(struct gl_context *ctx,
168 struct gl_texture_image *texImage)
169 {
170 struct st_texture_image *stImage = st_texture_image(texImage);
171
172 DBG("%s\n", __FUNCTION__);
173
174 if (stImage->pt) {
175 pipe_resource_reference(&stImage->pt, NULL);
176 }
177
178 if (stImage->TexData) {
179 _mesa_align_free(stImage->TexData);
180 stImage->TexData = NULL;
181 }
182 }
183
184
185 /** called via ctx->Driver.MapTextureImage() */
186 static void
187 st_MapTextureImage(struct gl_context *ctx,
188 struct gl_texture_image *texImage,
189 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
190 GLbitfield mode,
191 GLubyte **mapOut, GLint *rowStrideOut)
192 {
193 struct st_context *st = st_context(ctx);
194 struct st_texture_image *stImage = st_texture_image(texImage);
195 unsigned pipeMode;
196 GLubyte *map;
197
198 pipeMode = 0x0;
199 if (mode & GL_MAP_READ_BIT)
200 pipeMode |= PIPE_TRANSFER_READ;
201 if (mode & GL_MAP_WRITE_BIT)
202 pipeMode |= PIPE_TRANSFER_WRITE;
203 if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
204 pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
205
206 map = st_texture_image_map(st, stImage, pipeMode, x, y, slice, w, h, 1);
207 if (map) {
208 *mapOut = map;
209 *rowStrideOut = stImage->transfer->stride;
210 }
211 else {
212 *mapOut = NULL;
213 *rowStrideOut = 0;
214 }
215 }
216
217
218 /** called via ctx->Driver.UnmapTextureImage() */
219 static void
220 st_UnmapTextureImage(struct gl_context *ctx,
221 struct gl_texture_image *texImage,
222 GLuint slice)
223 {
224 struct st_context *st = st_context(ctx);
225 struct st_texture_image *stImage = st_texture_image(texImage);
226 st_texture_image_unmap(st, stImage);
227 }
228
229
230 /**
231 * Return default texture resource binding bitmask for the given format.
232 */
233 static GLuint
234 default_bindings(struct st_context *st, enum pipe_format format)
235 {
236 struct pipe_screen *screen = st->pipe->screen;
237 const unsigned target = PIPE_TEXTURE_2D;
238 unsigned bindings;
239
240 if (util_format_is_depth_or_stencil(format))
241 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
242 else
243 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
244
245 if (screen->is_format_supported(screen, format, target, 0, bindings))
246 return bindings;
247 else {
248 /* Try non-sRGB. */
249 format = util_format_linear(format);
250
251 if (screen->is_format_supported(screen, format, target, 0, bindings))
252 return bindings;
253 else
254 return PIPE_BIND_SAMPLER_VIEW;
255 }
256 }
257
258
259 /**
260 * Given the size of a mipmap image, try to compute the size of the level=0
261 * mipmap image.
262 *
263 * Note that this isn't always accurate for odd-sized, non-POW textures.
264 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
265 *
266 * \return GL_TRUE for success, GL_FALSE for failure
267 */
268 static GLboolean
269 guess_base_level_size(GLenum target,
270 GLuint width, GLuint height, GLuint depth, GLuint level,
271 GLuint *width0, GLuint *height0, GLuint *depth0)
272 {
273 assert(width >= 1);
274 assert(height >= 1);
275 assert(depth >= 1);
276
277 if (level > 0) {
278 /* Guess the size of the base level.
279 * Depending on the image's size, we can't always make a guess here.
280 */
281 switch (target) {
282 case GL_TEXTURE_1D:
283 case GL_TEXTURE_1D_ARRAY:
284 width <<= level;
285 break;
286
287 case GL_TEXTURE_2D:
288 case GL_TEXTURE_2D_ARRAY:
289 /* We can't make a good guess here, because the base level dimensions
290 * can be non-square.
291 */
292 if (width == 1 || height == 1) {
293 return GL_FALSE;
294 }
295 width <<= level;
296 height <<= level;
297 break;
298
299 case GL_TEXTURE_CUBE_MAP:
300 case GL_TEXTURE_CUBE_MAP_ARRAY:
301 width <<= level;
302 height <<= level;
303 break;
304
305 case GL_TEXTURE_3D:
306 /* We can't make a good guess here, because the base level dimensions
307 * can be non-cube.
308 */
309 if (width == 1 || height == 1 || depth == 1) {
310 return GL_FALSE;
311 }
312 width <<= level;
313 height <<= level;
314 depth <<= level;
315 break;
316
317 case GL_TEXTURE_RECTANGLE:
318 break;
319
320 default:
321 assert(0);
322 }
323 }
324
325 *width0 = width;
326 *height0 = height;
327 *depth0 = depth;
328
329 return GL_TRUE;
330 }
331
332
333 /**
334 * Try to allocate a pipe_resource object for the given st_texture_object.
335 *
336 * We use the given st_texture_image as a clue to determine the size of the
337 * mipmap image at level=0.
338 *
339 * \return GL_TRUE for success, GL_FALSE if out of memory.
340 */
341 static GLboolean
342 guess_and_alloc_texture(struct st_context *st,
343 struct st_texture_object *stObj,
344 const struct st_texture_image *stImage)
345 {
346 GLuint lastLevel, width, height, depth;
347 GLuint bindings;
348 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
349 enum pipe_format fmt;
350
351 DBG("%s\n", __FUNCTION__);
352
353 assert(!stObj->pt);
354
355 if (!guess_base_level_size(stObj->base.Target,
356 stImage->base.Width2,
357 stImage->base.Height2,
358 stImage->base.Depth2,
359 stImage->base.Level,
360 &width, &height, &depth)) {
361 /* we can't determine the image size at level=0 */
362 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
363 /* this is not an out of memory error */
364 return GL_TRUE;
365 }
366
367 /* At this point, (width x height x depth) is the expected size of
368 * the level=0 mipmap image.
369 */
370
371 /* Guess a reasonable value for lastLevel. With OpenGL we have no
372 * idea how many mipmap levels will be in a texture until we start
373 * to render with it. Make an educated guess here but be prepared
374 * to re-allocating a texture buffer with space for more (or fewer)
375 * mipmap levels later.
376 */
377 if ((stObj->base.Sampler.MinFilter == GL_NEAREST ||
378 stObj->base.Sampler.MinFilter == GL_LINEAR ||
379 (stObj->base.BaseLevel == 0 &&
380 stObj->base.MaxLevel == 0) ||
381 stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
382 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
383 !stObj->base.GenerateMipmap &&
384 stImage->base.Level == 0) {
385 /* only alloc space for a single mipmap level */
386 lastLevel = 0;
387 }
388 else {
389 /* alloc space for a full mipmap */
390 lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
391 width, height, depth) - 1;
392 }
393
394 /* Save the level=0 dimensions */
395 stObj->width0 = width;
396 stObj->height0 = height;
397 stObj->depth0 = depth;
398
399 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
400
401 bindings = default_bindings(st, fmt);
402
403 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
404 width, height, depth,
405 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
406
407 stObj->pt = st_texture_create(st,
408 gl_target_to_pipe(stObj->base.Target),
409 fmt,
410 lastLevel,
411 ptWidth,
412 ptHeight,
413 ptDepth,
414 ptLayers, 0,
415 bindings);
416
417 stObj->lastLevel = lastLevel;
418
419 DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
420
421 return stObj->pt != NULL;
422 }
423
424
425 /**
426 * Called via ctx->Driver.AllocTextureImageBuffer().
427 * If the texture object/buffer already has space for the indicated image,
428 * we're done. Otherwise, allocate memory for the new texture image.
429 */
430 static GLboolean
431 st_AllocTextureImageBuffer(struct gl_context *ctx,
432 struct gl_texture_image *texImage)
433 {
434 struct st_context *st = st_context(ctx);
435 struct st_texture_image *stImage = st_texture_image(texImage);
436 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
437 const GLuint level = texImage->Level;
438 GLuint width = texImage->Width;
439 GLuint height = texImage->Height;
440 GLuint depth = texImage->Depth;
441
442 DBG("%s\n", __FUNCTION__);
443
444 assert(!stImage->TexData);
445 assert(!stImage->pt); /* xxx this might be wrong */
446
447 /* Look if the parent texture object has space for this image */
448 if (stObj->pt &&
449 level <= stObj->pt->last_level &&
450 st_texture_match_image(stObj->pt, texImage)) {
451 /* this image will fit in the existing texture object's memory */
452 pipe_resource_reference(&stImage->pt, stObj->pt);
453 return GL_TRUE;
454 }
455
456 /* The parent texture object does not have space for this image */
457
458 pipe_resource_reference(&stObj->pt, NULL);
459 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
460
461 if (!guess_and_alloc_texture(st, stObj, stImage)) {
462 /* Probably out of memory.
463 * Try flushing any pending rendering, then retry.
464 */
465 st_finish(st);
466 if (!guess_and_alloc_texture(st, stObj, stImage)) {
467 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
468 return GL_FALSE;
469 }
470 }
471
472 if (stObj->pt &&
473 st_texture_match_image(stObj->pt, texImage)) {
474 /* The image will live in the object's mipmap memory */
475 pipe_resource_reference(&stImage->pt, stObj->pt);
476 assert(stImage->pt);
477 return GL_TRUE;
478 }
479 else {
480 /* Create a new, temporary texture/resource/buffer to hold this
481 * one texture image. Note that when we later access this image
482 * (either for mapping or copying) we'll want to always specify
483 * mipmap level=0, even if the image represents some other mipmap
484 * level.
485 */
486 enum pipe_format format =
487 st_mesa_format_to_pipe_format(texImage->TexFormat);
488 GLuint bindings = default_bindings(st, format);
489 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
490
491 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
492 width, height, depth,
493 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
494
495 stImage->pt = st_texture_create(st,
496 gl_target_to_pipe(stObj->base.Target),
497 format,
498 0, /* lastLevel */
499 ptWidth,
500 ptHeight,
501 ptDepth,
502 ptLayers, 0,
503 bindings);
504 return stImage->pt != NULL;
505 }
506 }
507
508
509 /**
510 * Preparation prior to glTexImage. Basically check the 'surface_based'
511 * field and switch to a "normal" tex image if necessary.
512 */
513 static void
514 prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
515 GLenum format, GLenum type)
516 {
517 struct gl_texture_object *texObj = texImage->TexObject;
518 struct st_texture_object *stObj = st_texture_object(texObj);
519
520 /* switch to "normal" */
521 if (stObj->surface_based) {
522 const GLenum target = texObj->Target;
523 const GLuint level = texImage->Level;
524 gl_format texFormat;
525
526 _mesa_clear_texture_object(ctx, texObj);
527 pipe_resource_reference(&stObj->pt, NULL);
528
529 /* oops, need to init this image again */
530 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
531 texImage->InternalFormat, format,
532 type);
533
534 _mesa_init_teximage_fields(ctx, texImage,
535 texImage->Width, texImage->Height,
536 texImage->Depth, texImage->Border,
537 texImage->InternalFormat, texFormat);
538
539 stObj->surface_based = GL_FALSE;
540 }
541 }
542
543
544 /**
545 * Return a writemask for the gallium blit. The parameters can be base
546 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
547 */
548 unsigned
549 st_get_blit_mask(GLenum srcFormat, GLenum dstFormat)
550 {
551 switch (dstFormat) {
552 case GL_DEPTH_STENCIL:
553 switch (srcFormat) {
554 case GL_DEPTH_STENCIL:
555 return PIPE_MASK_ZS;
556 case GL_DEPTH_COMPONENT:
557 return PIPE_MASK_Z;
558 case GL_STENCIL_INDEX:
559 return PIPE_MASK_S;
560 default:
561 assert(0);
562 return 0;
563 }
564
565 case GL_DEPTH_COMPONENT:
566 switch (srcFormat) {
567 case GL_DEPTH_STENCIL:
568 case GL_DEPTH_COMPONENT:
569 return PIPE_MASK_Z;
570 default:
571 assert(0);
572 return 0;
573 }
574
575 case GL_STENCIL_INDEX:
576 switch (srcFormat) {
577 case GL_STENCIL_INDEX:
578 return PIPE_MASK_S;
579 default:
580 assert(0);
581 return 0;
582 }
583
584 default:
585 return PIPE_MASK_RGBA;
586 }
587 }
588
589
590 static void
591 st_TexSubImage(struct gl_context *ctx, GLuint dims,
592 struct gl_texture_image *texImage,
593 GLint xoffset, GLint yoffset, GLint zoffset,
594 GLint width, GLint height, GLint depth,
595 GLenum format, GLenum type, const void *pixels,
596 const struct gl_pixelstore_attrib *unpack)
597 {
598 struct st_context *st = st_context(ctx);
599 struct st_texture_image *stImage = st_texture_image(texImage);
600 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
601 struct pipe_context *pipe = st->pipe;
602 struct pipe_screen *screen = pipe->screen;
603 struct pipe_resource *dst = stImage->pt;
604 struct pipe_resource *src = NULL;
605 struct pipe_resource src_templ;
606 struct pipe_transfer *transfer;
607 struct pipe_blit_info blit;
608 enum pipe_format src_format, dst_format;
609 gl_format mesa_src_format;
610 GLenum gl_target = texImage->TexObject->Target;
611 unsigned bind;
612 GLubyte *map;
613
614 if (!st->prefer_blit_based_texture_transfer) {
615 goto fallback;
616 }
617
618 if (!dst) {
619 goto fallback;
620 }
621
622 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
623 * blit implementation in some drivers. */
624 if (format == GL_DEPTH_STENCIL) {
625 goto fallback;
626 }
627
628 /* If the base internal format and the texture format don't match,
629 * we can't use blit-based TexSubImage. */
630 if (texImage->_BaseFormat !=
631 _mesa_get_format_base_format(texImage->TexFormat)) {
632 goto fallback;
633 }
634
635 /* See if the texture format already matches the format and type,
636 * in which case the memcpy-based fast path will likely be used and
637 * we don't have to blit. */
638 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
639 type, unpack->SwapBytes)) {
640 goto fallback;
641 }
642
643 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
644 bind = PIPE_BIND_DEPTH_STENCIL;
645 else
646 bind = PIPE_BIND_RENDER_TARGET;
647
648 /* See if the destination format is supported.
649 * For luminance and intensity, only the red channel is stored there. */
650 dst_format = util_format_linear(dst->format);
651 dst_format = util_format_luminance_to_red(dst_format);
652 dst_format = util_format_intensity_to_red(dst_format);
653
654 if (!dst_format ||
655 !screen->is_format_supported(screen, dst_format, dst->target,
656 dst->nr_samples, bind)) {
657 goto fallback;
658 }
659
660 /* Choose the source format. */
661 src_format = st_choose_matching_format(screen, PIPE_BIND_SAMPLER_VIEW,
662 format, type, unpack->SwapBytes);
663 if (!src_format) {
664 goto fallback;
665 }
666
667 mesa_src_format = st_pipe_format_to_mesa_format(src_format);
668
669 /* There is no reason to do this if we cannot use memcpy for the temporary
670 * source texture at least. This also takes transfer ops into account,
671 * etc. */
672 if (!_mesa_texstore_can_use_memcpy(ctx,
673 _mesa_get_format_base_format(mesa_src_format),
674 mesa_src_format, format, type, unpack)) {
675 goto fallback;
676 }
677
678 /* TexSubImage only sets a single cubemap face. */
679 if (gl_target == GL_TEXTURE_CUBE_MAP) {
680 gl_target = GL_TEXTURE_2D;
681 }
682
683 /* Initialize the source texture description. */
684 memset(&src_templ, 0, sizeof(src_templ));
685 src_templ.target = gl_target_to_pipe(gl_target);
686 src_templ.format = src_format;
687 src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
688 src_templ.usage = PIPE_USAGE_STAGING;
689
690 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
691 &src_templ.width0, &src_templ.height0,
692 &src_templ.depth0, &src_templ.array_size);
693
694 /* Check for NPOT texture support. */
695 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
696 (!util_is_power_of_two(src_templ.width0) ||
697 !util_is_power_of_two(src_templ.height0) ||
698 !util_is_power_of_two(src_templ.depth0))) {
699 goto fallback;
700 }
701
702 /* Create the source texture. */
703 src = screen->resource_create(screen, &src_templ);
704 if (!src) {
705 goto fallback;
706 }
707
708 /* Map source pixels. */
709 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
710 format, type, pixels, unpack,
711 "glTexSubImage");
712 if (!pixels) {
713 /* This is a GL error. */
714 pipe_resource_reference(&src, NULL);
715 return;
716 }
717
718 /* From now on, we need the gallium representation of dimensions. */
719 if (gl_target == GL_TEXTURE_1D_ARRAY) {
720 depth = height;
721 height = 1;
722 }
723
724 map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
725 width, height, depth, &transfer);
726 if (!map) {
727 _mesa_unmap_teximage_pbo(ctx, unpack);
728 pipe_resource_reference(&src, NULL);
729 goto fallback;
730 }
731
732 /* Upload pixels (just memcpy). */
733 {
734 const uint bytesPerRow = width * util_format_get_blocksize(src_format);
735 GLuint row, slice;
736
737 for (slice = 0; slice < depth; slice++) {
738 if (gl_target == GL_TEXTURE_1D_ARRAY) {
739 /* 1D array textures.
740 * We need to convert gallium coords to GL coords.
741 */
742 GLvoid *src = _mesa_image_address3d(unpack, pixels,
743 width, depth, format,
744 type, 0, slice, 0);
745 memcpy(map, src, bytesPerRow);
746 }
747 else {
748 ubyte *slice_map = map;
749
750 for (row = 0; row < height; row++) {
751 GLvoid *src = _mesa_image_address3d(unpack, pixels,
752 width, height, format,
753 type, slice, row, 0);
754 memcpy(slice_map, src, bytesPerRow);
755 slice_map += transfer->stride;
756 }
757 }
758 map += transfer->layer_stride;
759 }
760 }
761
762 pipe_transfer_unmap(pipe, transfer);
763 _mesa_unmap_teximage_pbo(ctx, unpack);
764
765 /* Blit. */
766 blit.src.resource = src;
767 blit.src.level = 0;
768 blit.src.format = src_format;
769 blit.dst.resource = dst;
770 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level;
771 blit.dst.format = dst_format;
772 blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
773 blit.dst.box.x = xoffset;
774 blit.dst.box.y = yoffset;
775 blit.dst.box.z = zoffset + texImage->Face;
776 blit.src.box.width = blit.dst.box.width = width;
777 blit.src.box.height = blit.dst.box.height = height;
778 blit.src.box.depth = blit.dst.box.depth = depth;
779 blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
780 blit.filter = PIPE_TEX_FILTER_NEAREST;
781 blit.scissor_enable = FALSE;
782
783 st->pipe->blit(st->pipe, &blit);
784
785 pipe_resource_reference(&src, NULL);
786 return;
787
788 fallback:
789 _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
790 width, height, depth, format, type, pixels,
791 unpack);
792 }
793
794 static void
795 st_TexImage(struct gl_context * ctx, GLuint dims,
796 struct gl_texture_image *texImage,
797 GLenum format, GLenum type, const void *pixels,
798 const struct gl_pixelstore_attrib *unpack)
799 {
800 assert(dims == 1 || dims == 2 || dims == 3);
801
802 prep_teximage(ctx, texImage, format, type);
803
804 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
805 return;
806
807 /* allocate storage for texture data */
808 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
809 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
810 return;
811 }
812
813 st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
814 texImage->Width, texImage->Height, texImage->Depth,
815 format, type, pixels, unpack);
816 }
817
818
819 static void
820 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
821 struct gl_texture_image *texImage,
822 GLsizei imageSize, const GLvoid *data)
823 {
824 prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
825 _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data);
826 }
827
828
829
830
831 /**
832 * Called via ctx->Driver.GetTexImage()
833 *
834 * This uses a blit to copy the texture to a texture format which matches
835 * the format and type combo and then a fast read-back is done using memcpy.
836 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
837 * a format which matches the swizzling.
838 *
839 * If such a format isn't available, it falls back to _mesa_get_teximage.
840 *
841 * NOTE: Drivers usually do a blit to convert between tiled and linear
842 * texture layouts during texture uploads/downloads, so the blit
843 * we do here should be free in such cases.
844 */
845 static void
846 st_GetTexImage(struct gl_context * ctx,
847 GLenum format, GLenum type, GLvoid * pixels,
848 struct gl_texture_image *texImage)
849 {
850 struct st_context *st = st_context(ctx);
851 struct pipe_context *pipe = st->pipe;
852 struct pipe_screen *screen = pipe->screen;
853 GLuint width = texImage->Width;
854 GLuint height = texImage->Height;
855 GLuint depth = texImage->Depth;
856 struct st_texture_image *stImage = st_texture_image(texImage);
857 struct pipe_resource *src = st_texture_object(texImage->TexObject)->pt;
858 struct pipe_resource *dst = NULL;
859 struct pipe_resource dst_templ;
860 enum pipe_format dst_format, src_format;
861 gl_format mesa_format;
862 GLenum gl_target = texImage->TexObject->Target;
863 enum pipe_texture_target pipe_target;
864 struct pipe_blit_info blit;
865 unsigned bind = PIPE_BIND_TRANSFER_READ;
866 struct pipe_transfer *tex_xfer;
867 ubyte *map = NULL;
868 boolean done = FALSE;
869
870 if (!st->prefer_blit_based_texture_transfer) {
871 goto fallback;
872 }
873
874 if (!stImage->pt || !src) {
875 goto fallback;
876 }
877
878 /* XXX Fallback to _mesa_get_teximage for depth-stencil formats
879 * due to an incomplete stencil blit implementation in some drivers. */
880 if (format == GL_DEPTH_STENCIL) {
881 goto fallback;
882 }
883
884 /* If the base internal format and the texture format don't match, we have
885 * to fall back to _mesa_get_teximage. */
886 if (texImage->_BaseFormat !=
887 _mesa_get_format_base_format(texImage->TexFormat)) {
888 goto fallback;
889 }
890
891 /* See if the texture format already matches the format and type,
892 * in which case the memcpy-based fast path will be used. */
893 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
894 type, ctx->Pack.SwapBytes)) {
895 goto fallback;
896 }
897
898 /* Convert the source format to what is expected by GetTexImage
899 * and see if it's supported.
900 *
901 * This only applies to glGetTexImage:
902 * - Luminance must be returned as (L,0,0,1).
903 * - Luminance alpha must be returned as (L,0,0,A).
904 * - Intensity must be returned as (I,0,0,1)
905 */
906 src_format = util_format_linear(src->format);
907 src_format = util_format_luminance_to_red(src_format);
908 src_format = util_format_intensity_to_red(src_format);
909
910 if (!src_format ||
911 !screen->is_format_supported(screen, src_format, src->target,
912 src->nr_samples,
913 PIPE_BIND_SAMPLER_VIEW)) {
914 goto fallback;
915 }
916
917 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
918 bind |= PIPE_BIND_DEPTH_STENCIL;
919 else
920 bind |= PIPE_BIND_RENDER_TARGET;
921
922 /* GetTexImage only returns a single face for cubemaps. */
923 if (gl_target == GL_TEXTURE_CUBE_MAP) {
924 gl_target = GL_TEXTURE_2D;
925 }
926 pipe_target = gl_target_to_pipe(gl_target);
927
928 /* Choose the destination format by finding the best match
929 * for the format+type combo. */
930 dst_format = st_choose_matching_format(screen, bind, format, type,
931 ctx->Pack.SwapBytes);
932
933 if (dst_format == PIPE_FORMAT_NONE) {
934 GLenum dst_glformat;
935
936 /* Fall back to _mesa_get_teximage except for compressed formats,
937 * where decompression with a blit is always preferred. */
938 if (!util_format_is_compressed(src->format)) {
939 goto fallback;
940 }
941
942 /* Set the appropriate format for the decompressed texture.
943 * Luminance and sRGB formats shouldn't appear here.*/
944 switch (src_format) {
945 case PIPE_FORMAT_DXT1_RGB:
946 case PIPE_FORMAT_DXT1_RGBA:
947 case PIPE_FORMAT_DXT3_RGBA:
948 case PIPE_FORMAT_DXT5_RGBA:
949 case PIPE_FORMAT_RGTC1_UNORM:
950 case PIPE_FORMAT_RGTC2_UNORM:
951 case PIPE_FORMAT_ETC1_RGB8:
952 dst_glformat = GL_RGBA8;
953 break;
954 case PIPE_FORMAT_RGTC1_SNORM:
955 case PIPE_FORMAT_RGTC2_SNORM:
956 if (!ctx->Extensions.EXT_texture_snorm)
957 goto fallback;
958 dst_glformat = GL_RGBA8_SNORM;
959 break;
960 /* TODO: for BPTC_*FLOAT, set RGBA32F and check for ARB_texture_float */
961 default:
962 assert(0);
963 goto fallback;
964 }
965
966 dst_format = st_choose_format(st, dst_glformat, format, type,
967 pipe_target, 0, bind, FALSE);
968
969 if (dst_format == PIPE_FORMAT_NONE) {
970 /* unable to get an rgba format!?! */
971 goto fallback;
972 }
973 }
974
975 /* create the destination texture */
976 memset(&dst_templ, 0, sizeof(dst_templ));
977 dst_templ.target = pipe_target;
978 dst_templ.format = dst_format;
979 dst_templ.bind = bind;
980 dst_templ.usage = PIPE_USAGE_STAGING;
981
982 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
983 &dst_templ.width0, &dst_templ.height0,
984 &dst_templ.depth0, &dst_templ.array_size);
985
986 dst = screen->resource_create(screen, &dst_templ);
987 if (!dst) {
988 goto fallback;
989 }
990
991 /* From now on, we need the gallium representation of dimensions. */
992 if (gl_target == GL_TEXTURE_1D_ARRAY) {
993 depth = height;
994 height = 1;
995 }
996
997 blit.src.resource = src;
998 blit.src.level = texImage->Level;
999 blit.src.format = src_format;
1000 blit.dst.resource = dst;
1001 blit.dst.level = 0;
1002 blit.dst.format = dst->format;
1003 blit.src.box.x = blit.dst.box.x = 0;
1004 blit.src.box.y = blit.dst.box.y = 0;
1005 blit.src.box.z = texImage->Face;
1006 blit.dst.box.z = 0;
1007 blit.src.box.width = blit.dst.box.width = width;
1008 blit.src.box.height = blit.dst.box.height = height;
1009 blit.src.box.depth = blit.dst.box.depth = depth;
1010 blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
1011 blit.filter = PIPE_TEX_FILTER_NEAREST;
1012 blit.scissor_enable = FALSE;
1013
1014 /* blit/render/decompress */
1015 st->pipe->blit(st->pipe, &blit);
1016
1017 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
1018
1019 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
1020 0, 0, 0, width, height, depth, &tex_xfer);
1021 if (!map) {
1022 goto end;
1023 }
1024
1025 mesa_format = st_pipe_format_to_mesa_format(dst_format);
1026
1027 /* copy/pack data into user buffer */
1028 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
1029 ctx->Pack.SwapBytes)) {
1030 /* memcpy */
1031 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
1032 GLuint row, slice;
1033
1034 for (slice = 0; slice < depth; slice++) {
1035 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1036 /* 1D array textures.
1037 * We need to convert gallium coords to GL coords.
1038 */
1039 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1040 width, depth, format,
1041 type, 0, slice, 0);
1042 memcpy(dest, map, bytesPerRow);
1043 }
1044 else {
1045 ubyte *slice_map = map;
1046
1047 for (row = 0; row < height; row++) {
1048 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1049 width, height, format,
1050 type, slice, row, 0);
1051 memcpy(dest, slice_map, bytesPerRow);
1052 slice_map += tex_xfer->stride;
1053 }
1054 }
1055 map += tex_xfer->layer_stride;
1056 }
1057 }
1058 else {
1059 /* format translation via floats */
1060 GLuint row, slice;
1061 GLfloat *rgba;
1062
1063 assert(util_format_is_compressed(src->format));
1064
1065 rgba = malloc(width * 4 * sizeof(GLfloat));
1066 if (!rgba) {
1067 goto end;
1068 }
1069
1070 if (ST_DEBUG & DEBUG_FALLBACK)
1071 debug_printf("%s: fallback format translation\n", __FUNCTION__);
1072
1073 for (slice = 0; slice < depth; slice++) {
1074 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1075 /* 1D array textures.
1076 * We need to convert gallium coords to GL coords.
1077 */
1078 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1079 width, depth, format,
1080 type, 0, slice, 0);
1081
1082 /* get float[4] rgba row from surface */
1083 pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, 1,
1084 dst_format, rgba);
1085
1086 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
1087 type, dest, &ctx->Pack, 0);
1088 }
1089 else {
1090 for (row = 0; row < height; row++) {
1091 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1092 width, height, format,
1093 type, slice, row, 0);
1094
1095 /* get float[4] rgba row from surface */
1096 pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1,
1097 dst_format, rgba);
1098
1099 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
1100 type, dest, &ctx->Pack, 0);
1101 }
1102 }
1103 map += tex_xfer->layer_stride;
1104 }
1105
1106 free(rgba);
1107 }
1108 done = TRUE;
1109
1110 end:
1111 if (map)
1112 pipe_transfer_unmap(pipe, tex_xfer);
1113
1114 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
1115 pipe_resource_reference(&dst, NULL);
1116
1117 fallback:
1118 if (!done) {
1119 _mesa_get_teximage(ctx, format, type, pixels, texImage);
1120 }
1121 }
1122
1123
1124 /**
1125 * Do a CopyTexSubImage operation using a read transfer from the source,
1126 * a write transfer to the destination and get_tile()/put_tile() to access
1127 * the pixels/texels.
1128 *
1129 * Note: srcY=0=TOP of renderbuffer
1130 */
1131 static void
1132 fallback_copy_texsubimage(struct gl_context *ctx,
1133 struct st_renderbuffer *strb,
1134 struct st_texture_image *stImage,
1135 GLenum baseFormat,
1136 GLint destX, GLint destY, GLint destZ,
1137 GLint srcX, GLint srcY,
1138 GLsizei width, GLsizei height)
1139 {
1140 struct st_context *st = st_context(ctx);
1141 struct pipe_context *pipe = st->pipe;
1142 struct pipe_transfer *src_trans;
1143 GLubyte *texDest;
1144 enum pipe_transfer_usage transfer_usage;
1145 void *map;
1146 unsigned dst_width = width;
1147 unsigned dst_height = height;
1148 unsigned dst_depth = 1;
1149
1150 if (ST_DEBUG & DEBUG_FALLBACK)
1151 debug_printf("%s: fallback processing\n", __FUNCTION__);
1152
1153 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1154 srcY = strb->Base.Height - srcY - height;
1155 }
1156
1157 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
1158 /* Move y/height to z/depth for 1D array textures. */
1159 destZ = destY;
1160 destY = 0;
1161 dst_depth = dst_height;
1162 dst_height = 1;
1163 }
1164
1165 map = pipe_transfer_map(pipe,
1166 strb->texture,
1167 strb->rtt_level,
1168 strb->rtt_face + strb->rtt_slice,
1169 PIPE_TRANSFER_READ,
1170 srcX, srcY,
1171 width, height, &src_trans);
1172
1173 if ((baseFormat == GL_DEPTH_COMPONENT ||
1174 baseFormat == GL_DEPTH_STENCIL) &&
1175 util_format_is_depth_and_stencil(stImage->pt->format))
1176 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1177 else
1178 transfer_usage = PIPE_TRANSFER_WRITE;
1179
1180 texDest = st_texture_image_map(st, stImage, transfer_usage,
1181 destX, destY, destZ,
1182 dst_width, dst_height, dst_depth);
1183
1184 if (baseFormat == GL_DEPTH_COMPONENT ||
1185 baseFormat == GL_DEPTH_STENCIL) {
1186 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1187 ctx->Pixel.DepthBias != 0.0F);
1188 GLint row, yStep;
1189 uint *data;
1190
1191 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1192 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1193 srcY = height - 1;
1194 yStep = -1;
1195 }
1196 else {
1197 srcY = 0;
1198 yStep = 1;
1199 }
1200
1201 data = malloc(width * sizeof(uint));
1202
1203 if (data) {
1204 /* To avoid a large temp memory allocation, do copy row by row */
1205 for (row = 0; row < height; row++, srcY += yStep) {
1206 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
1207 if (scaleOrBias) {
1208 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1209 }
1210
1211 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
1212 pipe_put_tile_z(stImage->transfer,
1213 texDest + row*stImage->transfer->layer_stride,
1214 0, 0, width, 1, data);
1215 }
1216 else {
1217 pipe_put_tile_z(stImage->transfer, texDest, 0, row, width, 1,
1218 data);
1219 }
1220 }
1221 }
1222 else {
1223 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
1224 }
1225
1226 free(data);
1227 }
1228 else {
1229 /* RGBA format */
1230 GLfloat *tempSrc =
1231 malloc(width * height * 4 * sizeof(GLfloat));
1232
1233 if (tempSrc && texDest) {
1234 const GLint dims = 2;
1235 GLint dstRowStride;
1236 struct gl_texture_image *texImage = &stImage->base;
1237 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1238
1239 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1240 unpack.Invert = GL_TRUE;
1241 }
1242
1243 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
1244 dstRowStride = stImage->transfer->layer_stride;
1245 }
1246 else {
1247 dstRowStride = stImage->transfer->stride;
1248 }
1249
1250 /* get float/RGBA image from framebuffer */
1251 /* XXX this usually involves a lot of int/float conversion.
1252 * try to avoid that someday.
1253 */
1254 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
1255 util_format_linear(strb->texture->format),
1256 tempSrc);
1257
1258 /* Store into texture memory.
1259 * Note that this does some special things such as pixel transfer
1260 * ops and format conversion. In particular, if the dest tex format
1261 * is actually RGBA but the user created the texture as GL_RGB we
1262 * need to fill-in/override the alpha channel with 1.0.
1263 */
1264 _mesa_texstore(ctx, dims,
1265 texImage->_BaseFormat,
1266 texImage->TexFormat,
1267 dstRowStride,
1268 &texDest,
1269 width, height, 1,
1270 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1271 &unpack);
1272 }
1273 else {
1274 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1275 }
1276
1277 free(tempSrc);
1278 }
1279
1280 st_texture_image_unmap(st, stImage);
1281 pipe->transfer_unmap(pipe, src_trans);
1282 }
1283
1284
1285 /**
1286 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1287 * Note that the region to copy has already been clipped so we know we
1288 * won't read from outside the source renderbuffer's bounds.
1289 *
1290 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1291 */
1292 static void
1293 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
1294 struct gl_texture_image *texImage,
1295 GLint destX, GLint destY, GLint destZ,
1296 struct gl_renderbuffer *rb,
1297 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
1298 {
1299 struct st_texture_image *stImage = st_texture_image(texImage);
1300 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1301 struct st_renderbuffer *strb = st_renderbuffer(rb);
1302 struct st_context *st = st_context(ctx);
1303 struct pipe_context *pipe = st->pipe;
1304 struct pipe_screen *screen = pipe->screen;
1305 struct pipe_blit_info blit;
1306 enum pipe_format dst_format;
1307 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1308 unsigned bind;
1309 GLint srcY0, srcY1, yStep;
1310
1311 if (!strb || !strb->surface || !stImage->pt) {
1312 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1313 return;
1314 }
1315
1316 if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
1317 texImage->TexFormat)) {
1318 goto fallback;
1319 }
1320
1321 /* The base internal format must match the mesa format, so make sure
1322 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1323 */
1324 if (texImage->_BaseFormat !=
1325 _mesa_get_format_base_format(texImage->TexFormat) ||
1326 rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
1327 goto fallback;
1328 }
1329
1330 /* Choose the destination format to match the TexImage behavior. */
1331 dst_format = util_format_linear(stImage->pt->format);
1332 dst_format = util_format_luminance_to_red(dst_format);
1333 dst_format = util_format_intensity_to_red(dst_format);
1334
1335 /* See if the destination format is supported. */
1336 if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
1337 texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
1338 bind = PIPE_BIND_DEPTH_STENCIL;
1339 }
1340 else {
1341 bind = PIPE_BIND_RENDER_TARGET;
1342 }
1343
1344 if (!dst_format ||
1345 !screen->is_format_supported(screen, dst_format, stImage->pt->target,
1346 stImage->pt->nr_samples, bind)) {
1347 goto fallback;
1348 }
1349
1350 /* Y flipping for the main framebuffer. */
1351 if (do_flip) {
1352 srcY1 = strb->Base.Height - srcY - height;
1353 srcY0 = srcY1 + height;
1354 yStep = -1;
1355 }
1356 else {
1357 srcY0 = srcY;
1358 srcY1 = srcY0 + height;
1359 yStep = 1;
1360 }
1361
1362 /* Blit the texture.
1363 * This supports flipping, format conversions, and downsampling.
1364 */
1365 memset(&blit, 0, sizeof(blit));
1366 blit.src.resource = strb->texture;
1367 blit.src.format = util_format_linear(strb->surface->format);
1368 blit.src.level = strb->surface->u.tex.level;
1369 blit.src.box.x = srcX;
1370 blit.src.box.y = srcY0;
1371 blit.src.box.z = strb->surface->u.tex.first_layer;
1372 blit.src.box.width = width;
1373 blit.src.box.height = srcY1 - srcY0;
1374 blit.src.box.depth = 1;
1375 blit.dst.resource = stImage->pt;
1376 blit.dst.format = dst_format;
1377 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level;
1378 blit.dst.box.x = destX;
1379 blit.dst.box.y = destY;
1380 blit.dst.box.z = stImage->base.Face + destZ;
1381 blit.dst.box.width = width;
1382 blit.dst.box.height = height;
1383 blit.dst.box.depth = 1;
1384 blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
1385 blit.filter = PIPE_TEX_FILTER_NEAREST;
1386
1387 /* 1D array textures need special treatment.
1388 * Blit rows from the source to layers in the destination. */
1389 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
1390 int y, layer;
1391
1392 for (y = srcY0, layer = 0; layer < height; y += yStep, layer++) {
1393 blit.src.box.y = y;
1394 blit.src.box.height = 1;
1395 blit.dst.box.y = 0;
1396 blit.dst.box.height = 1;
1397 blit.dst.box.z = destY + layer;
1398
1399 pipe->blit(pipe, &blit);
1400 }
1401 }
1402 else {
1403 /* All the other texture targets. */
1404 pipe->blit(pipe, &blit);
1405 }
1406 return;
1407
1408 fallback:
1409 /* software fallback */
1410 fallback_copy_texsubimage(ctx,
1411 strb, stImage, texImage->_BaseFormat,
1412 destX, destY, destZ,
1413 srcX, srcY, width, height);
1414 }
1415
1416
1417 /**
1418 * Copy image data from stImage into the texture object 'stObj' at level
1419 * 'dstLevel'.
1420 */
1421 static void
1422 copy_image_data_to_texture(struct st_context *st,
1423 struct st_texture_object *stObj,
1424 GLuint dstLevel,
1425 struct st_texture_image *stImage)
1426 {
1427 /* debug checks */
1428 {
1429 const struct gl_texture_image *dstImage =
1430 stObj->base.Image[stImage->base.Face][dstLevel];
1431 assert(dstImage);
1432 assert(dstImage->Width == stImage->base.Width);
1433 assert(dstImage->Height == stImage->base.Height);
1434 assert(dstImage->Depth == stImage->base.Depth);
1435 }
1436
1437 if (stImage->pt) {
1438 /* Copy potentially with the blitter:
1439 */
1440 GLuint src_level;
1441 if (stImage->pt->last_level == 0)
1442 src_level = 0;
1443 else
1444 src_level = stImage->base.Level;
1445
1446 assert(src_level <= stImage->pt->last_level);
1447 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
1448 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
1449 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
1450 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
1451 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
1452 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
1453
1454 st_texture_image_copy(st->pipe,
1455 stObj->pt, dstLevel, /* dest texture, level */
1456 stImage->pt, src_level, /* src texture, level */
1457 stImage->base.Face);
1458
1459 pipe_resource_reference(&stImage->pt, NULL);
1460 }
1461 else if (stImage->TexData) {
1462 /* Copy from malloc'd memory */
1463 /* XXX this should be re-examined/tested with a compressed format */
1464 GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1465 GLuint srcRowStride = stImage->base.Width * blockSize;
1466 GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1467 st_texture_image_data(st,
1468 stObj->pt,
1469 stImage->base.Face,
1470 dstLevel,
1471 stImage->TexData,
1472 srcRowStride,
1473 srcSliceStride);
1474 _mesa_align_free(stImage->TexData);
1475 stImage->TexData = NULL;
1476 }
1477
1478 pipe_resource_reference(&stImage->pt, stObj->pt);
1479 }
1480
1481
1482 /**
1483 * Called during state validation. When this function is finished,
1484 * the texture object should be ready for rendering.
1485 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1486 */
1487 GLboolean
1488 st_finalize_texture(struct gl_context *ctx,
1489 struct pipe_context *pipe,
1490 struct gl_texture_object *tObj)
1491 {
1492 struct st_context *st = st_context(ctx);
1493 struct st_texture_object *stObj = st_texture_object(tObj);
1494 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1495 GLuint face;
1496 struct st_texture_image *firstImage;
1497 enum pipe_format firstImageFormat;
1498 GLuint ptWidth, ptHeight, ptDepth, ptLayers, ptNumSamples;
1499
1500 if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
1501 /* The texture is complete and we know exactly how many mipmap levels
1502 * are present/needed. This is conditional because we may be called
1503 * from the st_generate_mipmap() function when the texture object is
1504 * incomplete. In that case, we'll have set stObj->lastLevel before
1505 * we get here.
1506 */
1507 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1508 stObj->base.Sampler.MinFilter == GL_NEAREST)
1509 stObj->lastLevel = stObj->base.BaseLevel;
1510 else
1511 stObj->lastLevel = stObj->base._MaxLevel;
1512 }
1513
1514 if (tObj->Target == GL_TEXTURE_BUFFER) {
1515 struct st_buffer_object *st_obj = st_buffer_object(tObj->BufferObject);
1516
1517 if (st_obj->buffer != stObj->pt) {
1518 pipe_resource_reference(&stObj->pt, st_obj->buffer);
1519 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1520 stObj->width0 = stObj->pt->width0 / _mesa_get_format_bytes(tObj->_BufferObjectFormat);
1521 stObj->height0 = 1;
1522 stObj->depth0 = 1;
1523 }
1524 return GL_TRUE;
1525
1526 }
1527
1528 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1529 assert(firstImage);
1530
1531 /* If both firstImage and stObj point to a texture which can contain
1532 * all active images, favour firstImage. Note that because of the
1533 * completeness requirement, we know that the image dimensions
1534 * will match.
1535 */
1536 if (firstImage->pt &&
1537 firstImage->pt != stObj->pt &&
1538 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1539 pipe_resource_reference(&stObj->pt, firstImage->pt);
1540 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1541 }
1542
1543 /* Find gallium format for the Mesa texture */
1544 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1545
1546 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1547 {
1548 GLuint width, height, depth;
1549 if (!guess_base_level_size(stObj->base.Target,
1550 firstImage->base.Width2,
1551 firstImage->base.Height2,
1552 firstImage->base.Depth2,
1553 firstImage->base.Level,
1554 &width, &height, &depth)) {
1555 width = stObj->width0;
1556 height = stObj->height0;
1557 depth = stObj->depth0;
1558 }
1559 /* convert GL dims to Gallium dims */
1560 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1561 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1562 ptNumSamples = firstImage->base.NumSamples;
1563 }
1564
1565 /* If we already have a gallium texture, check that it matches the texture
1566 * object's format, target, size, num_levels, etc.
1567 */
1568 if (stObj->pt) {
1569 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1570 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1571 stObj->pt->last_level < stObj->lastLevel ||
1572 stObj->pt->width0 != ptWidth ||
1573 stObj->pt->height0 != ptHeight ||
1574 stObj->pt->depth0 != ptDepth ||
1575 stObj->pt->nr_samples != ptNumSamples ||
1576 stObj->pt->array_size != ptLayers)
1577 {
1578 /* The gallium texture does not match the Mesa texture so delete the
1579 * gallium texture now. We'll make a new one below.
1580 */
1581 pipe_resource_reference(&stObj->pt, NULL);
1582 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1583 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1584 }
1585 }
1586
1587 /* May need to create a new gallium texture:
1588 */
1589 if (!stObj->pt) {
1590 GLuint bindings = default_bindings(st, firstImageFormat);
1591
1592 stObj->pt = st_texture_create(st,
1593 gl_target_to_pipe(stObj->base.Target),
1594 firstImageFormat,
1595 stObj->lastLevel,
1596 ptWidth,
1597 ptHeight,
1598 ptDepth,
1599 ptLayers, ptNumSamples,
1600 bindings);
1601
1602 if (!stObj->pt) {
1603 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1604 return GL_FALSE;
1605 }
1606 }
1607
1608 /* Pull in any images not in the object's texture:
1609 */
1610 for (face = 0; face < nr_faces; face++) {
1611 GLuint level;
1612 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1613 struct st_texture_image *stImage =
1614 st_texture_image(stObj->base.Image[face][level]);
1615
1616 /* Need to import images in main memory or held in other textures.
1617 */
1618 if (stImage && stObj->pt != stImage->pt) {
1619 if (level == 0 ||
1620 (stImage->base.Width == u_minify(stObj->width0, level) &&
1621 stImage->base.Height == u_minify(stObj->height0, level) &&
1622 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1623 /* src image fits expected dest mipmap level size */
1624 copy_image_data_to_texture(st, stObj, level, stImage);
1625 }
1626 }
1627 }
1628 }
1629
1630 return GL_TRUE;
1631 }
1632
1633
1634 /**
1635 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1636 * for a whole mipmap stack.
1637 */
1638 static GLboolean
1639 st_AllocTextureStorage(struct gl_context *ctx,
1640 struct gl_texture_object *texObj,
1641 GLsizei levels, GLsizei width,
1642 GLsizei height, GLsizei depth)
1643 {
1644 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1645 struct gl_texture_image *texImage = texObj->Image[0][0];
1646 struct st_context *st = st_context(ctx);
1647 struct st_texture_object *stObj = st_texture_object(texObj);
1648 struct pipe_screen *screen = st->pipe->screen;
1649 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1650 enum pipe_format fmt;
1651 GLint level;
1652 int num_samples = texImage->NumSamples;
1653
1654 assert(levels > 0);
1655
1656 /* Save the level=0 dimensions */
1657 stObj->width0 = width;
1658 stObj->height0 = height;
1659 stObj->depth0 = depth;
1660 stObj->lastLevel = levels - 1;
1661
1662 fmt = st_mesa_format_to_pipe_format(texImage->TexFormat);
1663
1664 bindings = default_bindings(st, fmt);
1665
1666 /* Raise the sample count if the requested one is unsupported. */
1667 if (num_samples > 1) {
1668 boolean found = FALSE;
1669
1670 for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
1671 if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
1672 num_samples,
1673 PIPE_BIND_SAMPLER_VIEW)) {
1674 /* Update the sample count in gl_texture_image as well. */
1675 texImage->NumSamples = num_samples;
1676 found = TRUE;
1677 break;
1678 }
1679 }
1680
1681 if (!found) {
1682 return GL_FALSE;
1683 }
1684 }
1685
1686 st_gl_texture_dims_to_pipe_dims(texObj->Target,
1687 width, height, depth,
1688 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1689
1690 stObj->pt = st_texture_create(st,
1691 gl_target_to_pipe(texObj->Target),
1692 fmt,
1693 levels - 1,
1694 ptWidth,
1695 ptHeight,
1696 ptDepth,
1697 ptLayers, num_samples,
1698 bindings);
1699 if (!stObj->pt)
1700 return GL_FALSE;
1701
1702 /* Set image resource pointers */
1703 for (level = 0; level < levels; level++) {
1704 GLuint face;
1705 for (face = 0; face < numFaces; face++) {
1706 struct st_texture_image *stImage =
1707 st_texture_image(texObj->Image[face][level]);
1708 pipe_resource_reference(&stImage->pt, stObj->pt);
1709 }
1710 }
1711
1712 return GL_TRUE;
1713 }
1714
1715
1716 static GLboolean
1717 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
1718 GLint level, gl_format format,
1719 GLint width, GLint height,
1720 GLint depth, GLint border)
1721 {
1722 struct st_context *st = st_context(ctx);
1723 struct pipe_context *pipe = st->pipe;
1724
1725 if (width == 0 || height == 0 || depth == 0) {
1726 /* zero-sized images are legal, and always fit! */
1727 return GL_TRUE;
1728 }
1729
1730 if (pipe->screen->can_create_resource) {
1731 /* Ask the gallium driver if the texture is too large */
1732 struct gl_texture_object *texObj =
1733 _mesa_get_current_tex_object(ctx, target);
1734 struct pipe_resource pt;
1735
1736 /* Setup the pipe_resource object
1737 */
1738 memset(&pt, 0, sizeof(pt));
1739
1740 pt.target = gl_target_to_pipe(target);
1741 pt.format = st_mesa_format_to_pipe_format(format);
1742
1743 st_gl_texture_dims_to_pipe_dims(target,
1744 width, height, depth,
1745 &pt.width0, &pt.height0,
1746 &pt.depth0, &pt.array_size);
1747
1748 if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
1749 texObj->Sampler.MinFilter == GL_NEAREST)) {
1750 /* assume just one mipmap level */
1751 pt.last_level = 0;
1752 }
1753 else {
1754 /* assume a full set of mipmaps */
1755 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
1756 }
1757
1758 return pipe->screen->can_create_resource(pipe->screen, &pt);
1759 }
1760 else {
1761 /* Use core Mesa fallback */
1762 return _mesa_test_proxy_teximage(ctx, target, level, format,
1763 width, height, depth, border);
1764 }
1765 }
1766
1767
1768 void
1769 st_init_texture_functions(struct dd_function_table *functions)
1770 {
1771 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1772 functions->QuerySamplesForFormat = st_QuerySamplesForFormat;
1773 functions->TexImage = st_TexImage;
1774 functions->TexSubImage = st_TexSubImage;
1775 functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage;
1776 functions->CopyTexSubImage = st_CopyTexSubImage;
1777 functions->GenerateMipmap = st_generate_mipmap;
1778
1779 functions->GetTexImage = st_GetTexImage;
1780
1781 /* compressed texture functions */
1782 functions->CompressedTexImage = st_CompressedTexImage;
1783 functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1784
1785 functions->NewTextureObject = st_NewTextureObject;
1786 functions->NewTextureImage = st_NewTextureImage;
1787 functions->DeleteTextureImage = st_DeleteTextureImage;
1788 functions->DeleteTexture = st_DeleteTextureObject;
1789 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1790 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1791 functions->MapTextureImage = st_MapTextureImage;
1792 functions->UnmapTextureImage = st_UnmapTextureImage;
1793
1794 /* XXX Temporary until we can query pipe's texture sizes */
1795 functions->TestProxyTexImage = st_TestProxyTexImage;
1796
1797 functions->AllocTextureStorage = st_AllocTextureStorage;
1798 }