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