mesa: remove unused _mesa_init_teximage_fields() target parameter
[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 * XXX This function and st_TexImage() have some duplicated code. That
419 * can be cleaned up in the future.
420 */
421 static GLboolean
422 st_AllocTextureImageBuffer(struct gl_context *ctx,
423 struct gl_texture_image *texImage,
424 gl_format format, GLsizei width,
425 GLsizei height, GLsizei depth)
426 {
427 struct st_context *st = st_context(ctx);
428 struct st_texture_image *stImage = st_texture_image(texImage);
429 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
430 const GLuint level = texImage->Level;
431
432 DBG("%s\n", __FUNCTION__);
433
434 assert(width > 0);
435 assert(height > 0);
436 assert(depth > 0);
437 assert(!stImage->TexData);
438 assert(!stImage->pt); /* xxx this might be wrong */
439
440 /* Look if the parent texture object has space for this image */
441 if (stObj->pt &&
442 level <= stObj->pt->last_level &&
443 st_texture_match_image(stObj->pt, texImage)) {
444 /* this image will fit in the existing texture object's memory */
445 pipe_resource_reference(&stImage->pt, stObj->pt);
446 return GL_TRUE;
447 }
448
449 /* The parent texture object does not have space for this image */
450
451 pipe_resource_reference(&stObj->pt, NULL);
452 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
453
454 if (!guess_and_alloc_texture(st, stObj, stImage)) {
455 /* Probably out of memory.
456 * Try flushing any pending rendering, then retry.
457 */
458 st_finish(st);
459 if (!guess_and_alloc_texture(st, stObj, stImage)) {
460 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
461 return GL_FALSE;
462 }
463 }
464
465 if (stObj->pt &&
466 st_texture_match_image(stObj->pt, texImage)) {
467 /* The image will live in the object's mipmap memory */
468 pipe_resource_reference(&stImage->pt, stObj->pt);
469 assert(stImage->pt);
470 return GL_TRUE;
471 }
472 else {
473 /* Create a new, temporary texture/resource/buffer to hold this
474 * one texture image.
475 */
476 enum pipe_format format =
477 st_mesa_format_to_pipe_format(texImage->TexFormat);
478 GLuint bindings = default_bindings(st, format);
479 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
480
481 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
482 width, height, depth,
483 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
484
485 stImage->pt = st_texture_create(st,
486 gl_target_to_pipe(stObj->base.Target),
487 format,
488 0, /* lastLevel */
489 ptWidth,
490 ptHeight,
491 ptDepth,
492 ptLayers,
493 bindings);
494 return stImage->pt != NULL;
495 }
496 }
497
498 /**
499 * Do glTexImage1/2/3D().
500 */
501 static void
502 st_TexImage(struct gl_context * ctx,
503 GLint dims,
504 struct gl_texture_image *texImage,
505 GLint internalFormat,
506 GLint width, GLint height, GLint depth,
507 GLint border,
508 GLenum format, GLenum type, const void *pixels,
509 const struct gl_pixelstore_attrib *unpack,
510 GLsizei imageSize, GLboolean compressed_src)
511 {
512 struct st_context *st = st_context(ctx);
513 struct gl_texture_object *texObj = texImage->TexObject;
514 struct st_texture_object *stObj = st_texture_object(texObj);
515 struct st_texture_image *stImage = st_texture_image(texImage);
516 const GLenum target = texObj->Target;
517 const GLuint level = texImage->Level;
518 GLuint dstRowStride = 0;
519 enum pipe_transfer_usage transfer_usage = 0;
520 GLubyte *dstMap;
521
522 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
523 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
524
525 /* switch to "normal" */
526 if (stObj->surface_based) {
527 gl_format texFormat;
528
529 _mesa_clear_texture_object(ctx, texObj);
530 pipe_resource_reference(&stObj->pt, NULL);
531
532 /* oops, need to init this image again */
533 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
534 internalFormat, format, type);
535
536 _mesa_init_teximage_fields(ctx, texImage,
537 width, height, depth, border,
538 internalFormat, texFormat);
539
540 stObj->surface_based = GL_FALSE;
541 }
542
543 assert(texImage->Width == width);
544 assert(texImage->Height == height);
545 assert(texImage->Depth == depth);
546
547 /* Release the reference to a potentially orphaned buffer.
548 * Release any old malloced memory.
549 */
550 if (stImage->pt) {
551 pipe_resource_reference(&stImage->pt, NULL);
552 assert(!stImage->TexData);
553 }
554 else if (stImage->TexData) {
555 _mesa_align_free(stImage->TexData);
556 }
557
558 /*
559 * See if the new image is somehow incompatible with the existing
560 * mipmap. If so, free the old mipmap.
561 */
562 if (stObj->pt) {
563 if (level > (GLint) stObj->pt->last_level ||
564 !st_texture_match_image(stObj->pt, &stImage->base)) {
565 DBG("release it\n");
566 pipe_resource_reference(&stObj->pt, NULL);
567 assert(!stObj->pt);
568 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
569 }
570 }
571
572 if (width == 0 || height == 0 || depth == 0) {
573 /* stop after freeing old image */
574 return;
575 }
576
577 if (!stObj->pt) {
578 if (!guess_and_alloc_texture(st, stObj, stImage)) {
579 /* Probably out of memory.
580 * Try flushing any pending rendering, then retry.
581 */
582 st_finish(st);
583 if (!guess_and_alloc_texture(st, stObj, stImage)) {
584 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
585 return;
586 }
587 }
588 }
589
590 assert(!stImage->pt);
591
592 /* Check if this texture image can live inside the texture object's buffer.
593 * If so, store the image there. Otherwise the image will temporarily live
594 * in its own buffer.
595 */
596 if (stObj->pt &&
597 st_texture_match_image(stObj->pt, &stImage->base)) {
598
599 pipe_resource_reference(&stImage->pt, stObj->pt);
600 assert(stImage->pt);
601 }
602
603 if (!stImage->pt)
604 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
605
606 /* Pixel data may come from regular user memory or a PBO. For the later,
607 * do bounds checking and map the PBO to read pixels data from it.
608 *
609 * XXX we should try to use a GPU-accelerated path to copy the image data
610 * from the PBO to the texture.
611 */
612 if (compressed_src) {
613 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
614 unpack,
615 "glCompressedTexImage");
616 }
617 else {
618 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
619 format, type,
620 pixels, unpack, "glTexImage");
621 }
622
623 /* for a 1D array upload the image as a series of layer with height = 1 */
624 if (target == GL_TEXTURE_1D_ARRAY) {
625 depth = height;
626 height = 1;
627 }
628
629 /*
630 * Prepare to store the texture data. Either map the gallium texture buffer
631 * memory or malloc space for it.
632 */
633 if (stImage->pt) {
634 if (!pixels) {
635 /* We've allocated texture resource, but have no pixel data - all done. */
636 goto done;
637 }
638
639 /* Store the image in the gallium transfer object */
640 if (format == GL_DEPTH_COMPONENT &&
641 util_format_is_depth_and_stencil(stImage->pt->format))
642 transfer_usage = PIPE_TRANSFER_READ_WRITE;
643 else
644 transfer_usage = PIPE_TRANSFER_WRITE;
645
646 dstMap = st_texture_image_map(st, stImage, 0,
647 transfer_usage, 0, 0, width, height);
648 if(stImage->transfer)
649 dstRowStride = stImage->transfer->stride;
650 }
651 else {
652 /* Allocate regular memory and store the image there temporarily. */
653 GLuint imageSize = _mesa_format_image_size(texImage->TexFormat,
654 width, height, depth);
655 dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
656
657 stImage->TexData = _mesa_align_malloc(imageSize, 16);
658 dstMap = stImage->TexData;
659 }
660
661 if (!dstMap) {
662 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
663 return;
664 }
665
666 if (!pixels) {
667 /* We've allocated texture memory, but have no pixel data - all done. */
668 goto done;
669 }
670
671 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
672 width, height, depth, width, dstRowStride);
673
674 /* Copy user texture image into the mapped texture buffer.
675 */
676 if (compressed_src) {
677 const GLuint srcRowStride =
678 _mesa_format_row_stride(texImage->TexFormat, width);
679 if (dstRowStride == srcRowStride) {
680 memcpy(dstMap, pixels, imageSize);
681 }
682 else {
683 GLubyte *dst = dstMap;
684 const char *src = pixels;
685 GLuint i, bw, bh, lines;
686 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
687 lines = (height + bh - 1) / bh;
688
689 for (i = 0; i < lines; ++i) {
690 memcpy(dst, src, srcRowStride);
691 dst += dstRowStride;
692 src += srcRowStride;
693 }
694 }
695 }
696 else {
697 const GLuint srcImageStride =
698 _mesa_image_image_stride(unpack, width, height, format, type);
699 GLint i;
700 const GLubyte *src = (const GLubyte *) pixels;
701
702 for (i = 0; i < depth; i++) {
703 if (!_mesa_texstore(ctx, dims,
704 texImage->_BaseFormat,
705 texImage->TexFormat,
706 dstRowStride,
707 (GLubyte **) &dstMap, /* dstSlice */
708 width, height, 1,
709 format, type, src, unpack)) {
710 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
711 }
712
713 if (stImage->pt && i + 1 < depth) {
714 /* unmap this slice */
715 st_texture_image_unmap(st, stImage);
716 /* map next slice of 3D texture */
717 dstMap = st_texture_image_map(st, stImage, i + 1,
718 transfer_usage, 0, 0,
719 width, height);
720 src += srcImageStride;
721 }
722 }
723 }
724
725 done:
726 _mesa_unmap_teximage_pbo(ctx, unpack);
727
728 if (stImage->pt && stImage->transfer) {
729 st_texture_image_unmap(st, stImage);
730 }
731 }
732
733
734 static void
735 st_TexImage3D(struct gl_context * ctx,
736 struct gl_texture_image *texImage,
737 GLint internalFormat,
738 GLint width, GLint height, GLint depth,
739 GLint border,
740 GLenum format, GLenum type, const void *pixels,
741 const struct gl_pixelstore_attrib *unpack)
742 {
743 st_TexImage(ctx, 3, texImage, internalFormat, width, height, depth, border,
744 format, type, pixels, unpack, 0, GL_FALSE);
745 }
746
747
748 static void
749 st_TexImage2D(struct gl_context * ctx,
750 struct gl_texture_image *texImage,
751 GLint internalFormat,
752 GLint width, GLint height, GLint border,
753 GLenum format, GLenum type, const void *pixels,
754 const struct gl_pixelstore_attrib *unpack)
755 {
756 st_TexImage(ctx, 2, texImage, internalFormat, width, height, 1, border,
757 format, type, pixels, unpack, 0, GL_FALSE);
758 }
759
760
761 static void
762 st_TexImage1D(struct gl_context * ctx,
763 struct gl_texture_image *texImage,
764 GLint internalFormat,
765 GLint width, GLint border,
766 GLenum format, GLenum type, const void *pixels,
767 const struct gl_pixelstore_attrib *unpack)
768 {
769 st_TexImage(ctx, 1, texImage, internalFormat, width, 1, 1, border,
770 format, type, pixels, unpack, 0, GL_FALSE);
771 }
772
773
774 static void
775 st_CompressedTexImage2D(struct gl_context *ctx,
776 struct gl_texture_image *texImage,
777 GLint internalFormat,
778 GLint width, GLint height, GLint border,
779 GLsizei imageSize, const GLvoid *data)
780 {
781 st_TexImage(ctx, 2, texImage, internalFormat, width, height, 1, border,
782 0, 0, data, &ctx->Unpack, imageSize, GL_TRUE);
783 }
784
785
786
787 /**
788 * glGetTexImage() helper: decompress a compressed texture by rendering
789 * a textured quad. Store the results in the user's buffer.
790 */
791 static void
792 decompress_with_blit(struct gl_context * ctx,
793 GLenum format, GLenum type, GLvoid *pixels,
794 struct gl_texture_image *texImage)
795 {
796 struct st_context *st = st_context(ctx);
797 struct pipe_context *pipe = st->pipe;
798 struct st_texture_image *stImage = st_texture_image(texImage);
799 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
800 struct pipe_sampler_view *src_view =
801 st_get_texture_sampler_view(stObj, pipe);
802 const GLuint width = texImage->Width;
803 const GLuint height = texImage->Height;
804 struct pipe_surface *dst_surface;
805 struct pipe_resource *dst_texture;
806 struct pipe_transfer *tex_xfer;
807 unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
808 PIPE_BIND_TRANSFER_READ);
809
810 /* create temp / dest surface */
811 if (!util_create_rgba_surface(pipe, width, height, bind,
812 &dst_texture, &dst_surface)) {
813 _mesa_problem(ctx, "util_create_rgba_surface() failed "
814 "in decompress_with_blit()");
815 return;
816 }
817
818 /* Disable conditional rendering. */
819 if (st->render_condition) {
820 pipe->render_condition(pipe, NULL, 0);
821 }
822
823 /* Choose the source mipmap level */
824 src_view->u.tex.first_level = src_view->u.tex.last_level = texImage->Level;
825
826 /* blit/render/decompress */
827 util_blit_pixels_tex(st->blit,
828 src_view, /* pipe_resource (src) */
829 0, 0, /* src x0, y0 */
830 width, height, /* src x1, y1 */
831 dst_surface, /* pipe_surface (dst) */
832 0, 0, /* dst x0, y0 */
833 width, height, /* dst x1, y1 */
834 0.0, /* z */
835 PIPE_TEX_MIPFILTER_NEAREST);
836
837 /* Restore conditional rendering state. */
838 if (st->render_condition) {
839 pipe->render_condition(pipe, st->render_condition,
840 st->condition_mode);
841 }
842
843 /* map the dst_surface so we can read from it */
844 tex_xfer = pipe_get_transfer(pipe,
845 dst_texture, 0, 0,
846 PIPE_TRANSFER_READ,
847 0, 0, width, height);
848
849 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
850
851 /* copy/pack data into user buffer */
852 if (st_equal_formats(stImage->pt->format, format, type)) {
853 /* memcpy */
854 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
855 ubyte *map = pipe_transfer_map(pipe, tex_xfer);
856 GLuint row;
857 for (row = 0; row < height; row++) {
858 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
859 height, format, type, row, 0);
860 memcpy(dest, map, bytesPerRow);
861 map += tex_xfer->stride;
862 }
863 pipe_transfer_unmap(pipe, tex_xfer);
864 }
865 else {
866 /* format translation via floats */
867 GLuint row;
868 enum pipe_format pformat = util_format_linear(dst_texture->format);
869 for (row = 0; row < height; row++) {
870 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
871 GLfloat rgba[4 * MAX_WIDTH];
872 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
873 height, format, type, row, 0);
874
875 if (ST_DEBUG & DEBUG_FALLBACK)
876 debug_printf("%s: fallback format translation\n", __FUNCTION__);
877
878 /* get float[4] rgba row from surface */
879 pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
880 pformat, rgba);
881
882 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
883 type, dest, &ctx->Pack, transferOps);
884 }
885 }
886
887 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
888
889 pipe->transfer_destroy(pipe, tex_xfer);
890
891 /* destroy the temp / dest surface */
892 util_destroy_rgba_surface(dst_texture, dst_surface);
893 }
894
895
896
897 /**
898 * Called via ctx->Driver.GetTexImage()
899 */
900 static void
901 st_GetTexImage(struct gl_context * ctx,
902 GLenum format, GLenum type, GLvoid * pixels,
903 struct gl_texture_image *texImage)
904 {
905 struct st_texture_image *stImage = st_texture_image(texImage);
906
907 if (stImage->pt && util_format_is_s3tc(stImage->pt->format)) {
908 /* Need to decompress the texture.
909 * We'll do this by rendering a textured quad (which is hopefully
910 * faster than using the fallback code in texcompress.c).
911 * Note that we only expect RGBA formats (no Z/depth formats).
912 */
913 decompress_with_blit(ctx, format, type, pixels, texImage);
914 }
915 else {
916 _mesa_get_teximage(ctx, format, type, pixels, texImage);
917 }
918 }
919
920
921 static void
922 st_CompressedTexSubImage1D(struct gl_context *ctx,
923 struct gl_texture_image *texImage,
924 GLint xoffset, GLsizei width,
925 GLenum format,
926 GLsizei imageSize, const GLvoid *data)
927 {
928 assert(0);
929 }
930
931
932 static void
933 st_CompressedTexSubImage2D(struct gl_context *ctx,
934 struct gl_texture_image *texImage,
935 GLint xoffset, GLint yoffset,
936 GLsizei width, GLint height,
937 GLenum format,
938 GLsizei imageSize, const GLvoid *data)
939 {
940 struct st_context *st = st_context(ctx);
941 struct st_texture_image *stImage = st_texture_image(texImage);
942 int srcBlockStride;
943 int dstBlockStride;
944 int y;
945 enum pipe_format pformat;
946 GLubyte *dstMap;
947
948 if (stImage->pt) {
949 pformat = stImage->pt->format;
950
951 dstMap = st_texture_image_map(st, stImage, 0,
952 PIPE_TRANSFER_WRITE,
953 xoffset, yoffset,
954 width, height);
955
956 srcBlockStride = util_format_get_stride(pformat, width);
957 dstBlockStride = stImage->transfer->stride;
958 } else {
959 assert(stImage->pt);
960 /* TODO find good values for block and strides */
961 /* TODO also adjust texImage->data for yoffset/xoffset */
962 return;
963 }
964
965 if (!dstMap) {
966 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage");
967 return;
968 }
969
970 assert(xoffset % util_format_get_blockwidth(pformat) == 0);
971 assert(yoffset % util_format_get_blockheight(pformat) == 0);
972
973 for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
974 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
975 const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
976 char *dst = (char *) dstMap + dstBlockStride * util_format_get_nblocksy(pformat, y);
977 memcpy(dst, src, util_format_get_stride(pformat, width));
978 }
979
980 if (stImage->pt && stImage->transfer) {
981 st_texture_image_unmap(st, stImage);
982 }
983 }
984
985
986 static void
987 st_CompressedTexSubImage3D(struct gl_context *ctx,
988 struct gl_texture_image *texImage,
989 GLint xoffset, GLint yoffset, GLint zoffset,
990 GLsizei width, GLint height, GLint depth,
991 GLenum format,
992 GLsizei imageSize, const GLvoid *data)
993 {
994 assert(0);
995 }
996
997
998
999 /**
1000 * Do a CopyTexSubImage operation using a read transfer from the source,
1001 * a write transfer to the destination and get_tile()/put_tile() to access
1002 * the pixels/texels.
1003 *
1004 * Note: srcY=0=TOP of renderbuffer
1005 */
1006 static void
1007 fallback_copy_texsubimage(struct gl_context *ctx, GLenum target, GLint level,
1008 struct st_renderbuffer *strb,
1009 struct st_texture_image *stImage,
1010 GLenum baseFormat,
1011 GLint destX, GLint destY, GLint destZ,
1012 GLint srcX, GLint srcY,
1013 GLsizei width, GLsizei height)
1014 {
1015 struct st_context *st = st_context(ctx);
1016 struct pipe_context *pipe = st->pipe;
1017 struct pipe_transfer *src_trans;
1018 GLvoid *texDest;
1019 enum pipe_transfer_usage transfer_usage;
1020
1021 if (ST_DEBUG & DEBUG_FALLBACK)
1022 debug_printf("%s: fallback processing\n", __FUNCTION__);
1023
1024 assert(width <= MAX_WIDTH);
1025
1026 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1027 srcY = strb->Base.Height - srcY - height;
1028 }
1029
1030 src_trans = pipe_get_transfer(pipe,
1031 strb->texture,
1032 strb->rtt_level,
1033 strb->rtt_face + strb->rtt_slice,
1034 PIPE_TRANSFER_READ,
1035 srcX, srcY,
1036 width, height);
1037
1038 if ((baseFormat == GL_DEPTH_COMPONENT ||
1039 baseFormat == GL_DEPTH_STENCIL) &&
1040 util_format_is_depth_and_stencil(stImage->pt->format))
1041 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1042 else
1043 transfer_usage = PIPE_TRANSFER_WRITE;
1044
1045 /* XXX this used to ignore destZ param */
1046 texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
1047 destX, destY, width, height);
1048
1049 if (baseFormat == GL_DEPTH_COMPONENT ||
1050 baseFormat == GL_DEPTH_STENCIL) {
1051 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1052 ctx->Pixel.DepthBias != 0.0F);
1053 GLint row, yStep;
1054
1055 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1056 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1057 srcY = height - 1;
1058 yStep = -1;
1059 }
1060 else {
1061 srcY = 0;
1062 yStep = 1;
1063 }
1064
1065 /* To avoid a large temp memory allocation, do copy row by row */
1066 for (row = 0; row < height; row++, srcY += yStep) {
1067 uint data[MAX_WIDTH];
1068 pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1069 if (scaleOrBias) {
1070 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1071 }
1072 pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1073 }
1074 }
1075 else {
1076 /* RGBA format */
1077 GLfloat *tempSrc =
1078 (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1079
1080 if (tempSrc && texDest) {
1081 const GLint dims = 2;
1082 const GLint dstRowStride = stImage->transfer->stride;
1083 struct gl_texture_image *texImage = &stImage->base;
1084 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1085
1086 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1087 unpack.Invert = GL_TRUE;
1088 }
1089
1090 /* get float/RGBA image from framebuffer */
1091 /* XXX this usually involves a lot of int/float conversion.
1092 * try to avoid that someday.
1093 */
1094 pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
1095 util_format_linear(strb->texture->format),
1096 tempSrc);
1097
1098 /* Store into texture memory.
1099 * Note that this does some special things such as pixel transfer
1100 * ops and format conversion. In particular, if the dest tex format
1101 * is actually RGBA but the user created the texture as GL_RGB we
1102 * need to fill-in/override the alpha channel with 1.0.
1103 */
1104 _mesa_texstore(ctx, dims,
1105 texImage->_BaseFormat,
1106 texImage->TexFormat,
1107 dstRowStride,
1108 (GLubyte **) &texDest,
1109 width, height, 1,
1110 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1111 &unpack);
1112 }
1113 else {
1114 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1115 }
1116
1117 if (tempSrc)
1118 free(tempSrc);
1119 }
1120
1121 st_texture_image_unmap(st, stImage);
1122 pipe->transfer_destroy(pipe, src_trans);
1123 }
1124
1125
1126
1127 /**
1128 * If the format of the src renderbuffer and the format of the dest
1129 * texture are compatible (in terms of blitting), return a TGSI writemask
1130 * to be used during the blit.
1131 * If the src/dest are incompatible, return 0.
1132 */
1133 static unsigned
1134 compatible_src_dst_formats(struct gl_context *ctx,
1135 const struct gl_renderbuffer *src,
1136 const struct gl_texture_image *dst)
1137 {
1138 /* Get logical base formats for the src and dest.
1139 * That is, use the user-requested formats and not the actual, device-
1140 * chosen formats.
1141 * For example, the user may have requested an A8 texture but the
1142 * driver may actually be using an RGBA texture format. When we
1143 * copy/blit to that texture, we only want to copy the Alpha channel
1144 * and not the RGB channels.
1145 *
1146 * Similarly, when the src FBO was created an RGB format may have been
1147 * requested but the driver actually chose an RGBA format. In that case,
1148 * we don't want to copy the undefined Alpha channel to the dest texture
1149 * (it should be 1.0).
1150 */
1151 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1152 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1153
1154 /**
1155 * XXX when we have red-only and red/green renderbuffers we'll need
1156 * to add more cases here (or implement a general-purpose routine that
1157 * queries the existance of the R,G,B,A channels in the src and dest).
1158 */
1159 if (srcFormat == dstFormat) {
1160 /* This is the same as matching_base_formats, which should
1161 * always pass, as it did previously.
1162 */
1163 return TGSI_WRITEMASK_XYZW;
1164 }
1165 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1166 /* Make sure that A in the dest is 1. The actual src format
1167 * may be RGBA and have undefined A values.
1168 */
1169 return TGSI_WRITEMASK_XYZ;
1170 }
1171 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1172 /* Make sure that A in the dest is 1. The actual dst format
1173 * may be RGBA and will need A=1 to provide proper alpha values
1174 * when sampled later.
1175 */
1176 return TGSI_WRITEMASK_XYZ;
1177 }
1178 else {
1179 if (ST_DEBUG & DEBUG_FALLBACK)
1180 debug_printf("%s failed for src %s, dst %s\n",
1181 __FUNCTION__,
1182 _mesa_lookup_enum_by_nr(srcFormat),
1183 _mesa_lookup_enum_by_nr(dstFormat));
1184
1185 /* Otherwise fail.
1186 */
1187 return 0;
1188 }
1189 }
1190
1191
1192
1193 /**
1194 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1195 * Note that the region to copy has already been clipped so we know we
1196 * won't read from outside the source renderbuffer's bounds.
1197 *
1198 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1199 */
1200 static void
1201 st_copy_texsubimage(struct gl_context *ctx,
1202 GLenum target, GLint level,
1203 GLint destX, GLint destY, GLint destZ,
1204 GLint srcX, GLint srcY,
1205 GLsizei width, GLsizei height)
1206 {
1207 struct gl_texture_unit *texUnit =
1208 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1209 struct gl_texture_object *texObj =
1210 _mesa_select_tex_object(ctx, texUnit, target);
1211 struct gl_texture_image *texImage =
1212 _mesa_select_tex_image(ctx, texObj, target, level);
1213 struct st_texture_image *stImage = st_texture_image(texImage);
1214 const GLenum texBaseFormat = texImage->_BaseFormat;
1215 struct gl_framebuffer *fb = ctx->ReadBuffer;
1216 struct st_renderbuffer *strb;
1217 struct st_context *st = st_context(ctx);
1218 struct pipe_context *pipe = st->pipe;
1219 struct pipe_screen *screen = pipe->screen;
1220 enum pipe_format dest_format, src_format;
1221 GLboolean matching_base_formats;
1222 GLuint format_writemask, sample_count;
1223 struct pipe_surface *dest_surface = NULL;
1224 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1225 struct pipe_surface surf_tmpl;
1226 unsigned int dst_usage;
1227 GLint srcY0, srcY1;
1228
1229 /* make sure finalize_textures has been called?
1230 */
1231 if (0) st_validate_state(st);
1232
1233 /* determine if copying depth or color data */
1234 if (texBaseFormat == GL_DEPTH_COMPONENT ||
1235 texBaseFormat == GL_DEPTH_STENCIL) {
1236 strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
1237 }
1238 else {
1239 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1240 strb = st_renderbuffer(fb->_ColorReadBuffer);
1241 }
1242
1243 if (!strb || !strb->surface || !stImage->pt) {
1244 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1245 return;
1246 }
1247
1248 sample_count = strb->surface->texture->nr_samples;
1249 /* I believe this would be legal, presumably would need to do a resolve
1250 for color, and for depth/stencil spec says to just use one of the
1251 depth/stencil samples per pixel? Need some transfer clarifications. */
1252 assert(sample_count < 2);
1253
1254 assert(strb);
1255 assert(strb->surface);
1256 assert(stImage->pt);
1257
1258 src_format = strb->surface->format;
1259 dest_format = stImage->pt->format;
1260
1261 /*
1262 * Determine if the src framebuffer and dest texture have the same
1263 * base format. We need this to detect a case such as the framebuffer
1264 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1265 * texture format stores RGBA we need to set A=1 (overriding the
1266 * framebuffer's alpha values). We can't do that with the blit or
1267 * textured-quad paths.
1268 */
1269 matching_base_formats =
1270 (_mesa_get_format_base_format(strb->Base.Format) ==
1271 _mesa_get_format_base_format(texImage->TexFormat));
1272
1273 if (ctx->_ImageTransferState) {
1274 goto fallback;
1275 }
1276
1277 if (matching_base_formats &&
1278 src_format == dest_format &&
1279 !do_flip) {
1280 /* use surface_copy() / blit */
1281 struct pipe_box src_box;
1282 u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1283 width, height, &src_box);
1284
1285 /* for resource_copy_region(), y=0=top, always */
1286 pipe->resource_copy_region(pipe,
1287 /* dest */
1288 stImage->pt,
1289 stImage->base.Level,
1290 destX, destY, destZ + stImage->base.Face,
1291 /* src */
1292 strb->texture,
1293 strb->surface->u.tex.level,
1294 &src_box);
1295 return;
1296 }
1297
1298 if (texBaseFormat == GL_DEPTH_STENCIL) {
1299 goto fallback;
1300 }
1301
1302 if (texBaseFormat == GL_DEPTH_COMPONENT) {
1303 format_writemask = TGSI_WRITEMASK_XYZW;
1304 dst_usage = PIPE_BIND_DEPTH_STENCIL;
1305 }
1306 else {
1307 format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1308 dst_usage = PIPE_BIND_RENDER_TARGET;
1309 }
1310
1311 if (!format_writemask ||
1312 !screen->is_format_supported(screen, src_format,
1313 PIPE_TEXTURE_2D, sample_count,
1314 PIPE_BIND_SAMPLER_VIEW) ||
1315 !screen->is_format_supported(screen, dest_format,
1316 PIPE_TEXTURE_2D, 0,
1317 dst_usage)) {
1318 goto fallback;
1319 }
1320
1321 if (do_flip) {
1322 srcY1 = strb->Base.Height - srcY - height;
1323 srcY0 = srcY1 + height;
1324 }
1325 else {
1326 srcY0 = srcY;
1327 srcY1 = srcY0 + height;
1328 }
1329
1330 /* Disable conditional rendering. */
1331 if (st->render_condition) {
1332 pipe->render_condition(pipe, NULL, 0);
1333 }
1334
1335 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1336 surf_tmpl.format = util_format_linear(stImage->pt->format);
1337 surf_tmpl.usage = dst_usage;
1338 surf_tmpl.u.tex.level = stImage->base.Level;
1339 surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ;
1340 surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ;
1341
1342 dest_surface = pipe->create_surface(pipe, stImage->pt,
1343 &surf_tmpl);
1344 util_blit_pixels_writemask(st->blit,
1345 strb->texture,
1346 strb->surface->u.tex.level,
1347 srcX, srcY0,
1348 srcX + width, srcY1,
1349 strb->surface->u.tex.first_layer,
1350 dest_surface,
1351 destX, destY,
1352 destX + width, destY + height,
1353 0.0, PIPE_TEX_MIPFILTER_NEAREST,
1354 format_writemask);
1355 pipe_surface_reference(&dest_surface, NULL);
1356
1357 /* Restore conditional rendering state. */
1358 if (st->render_condition) {
1359 pipe->render_condition(pipe, st->render_condition,
1360 st->condition_mode);
1361 }
1362
1363 return;
1364
1365 fallback:
1366 /* software fallback */
1367 fallback_copy_texsubimage(ctx, target, level,
1368 strb, stImage, texBaseFormat,
1369 destX, destY, destZ,
1370 srcX, srcY, width, height);
1371 }
1372
1373
1374
1375 static void
1376 st_CopyTexSubImage1D(struct gl_context * ctx, GLenum target, GLint level,
1377 GLint xoffset, GLint x, GLint y, GLsizei width)
1378 {
1379 const GLint yoffset = 0, zoffset = 0;
1380 const GLsizei height = 1;
1381 st_copy_texsubimage(ctx, target, level,
1382 xoffset, yoffset, zoffset, /* destX,Y,Z */
1383 x, y, width, height); /* src X, Y, size */
1384 }
1385
1386
1387 static void
1388 st_CopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
1389 GLint xoffset, GLint yoffset,
1390 GLint x, GLint y, GLsizei width, GLsizei height)
1391 {
1392 const GLint zoffset = 0;
1393 st_copy_texsubimage(ctx, target, level,
1394 xoffset, yoffset, zoffset, /* destX,Y,Z */
1395 x, y, width, height); /* src X, Y, size */
1396 }
1397
1398
1399 static void
1400 st_CopyTexSubImage3D(struct gl_context * ctx, GLenum target, GLint level,
1401 GLint xoffset, GLint yoffset, GLint zoffset,
1402 GLint x, GLint y, GLsizei width, GLsizei height)
1403 {
1404 st_copy_texsubimage(ctx, target, level,
1405 xoffset, yoffset, zoffset, /* destX,Y,Z */
1406 x, y, width, height); /* src X, Y, size */
1407 }
1408
1409
1410 /**
1411 * Copy image data from stImage into the texture object 'stObj' at level
1412 * 'dstLevel'.
1413 */
1414 static void
1415 copy_image_data_to_texture(struct st_context *st,
1416 struct st_texture_object *stObj,
1417 GLuint dstLevel,
1418 struct st_texture_image *stImage)
1419 {
1420 /* debug checks */
1421 {
1422 const struct gl_texture_image *dstImage =
1423 stObj->base.Image[stImage->base.Face][dstLevel];
1424 assert(dstImage);
1425 assert(dstImage->Width == stImage->base.Width);
1426 assert(dstImage->Height == stImage->base.Height);
1427 assert(dstImage->Depth == stImage->base.Depth);
1428 }
1429
1430 if (stImage->pt) {
1431 /* Copy potentially with the blitter:
1432 */
1433 st_texture_image_copy(st->pipe,
1434 stObj->pt, dstLevel, /* dest texture, level */
1435 stImage->pt, stImage->base.Level, /* src texture, level */
1436 stImage->base.Face);
1437
1438 pipe_resource_reference(&stImage->pt, NULL);
1439 }
1440 else if (stImage->TexData) {
1441 /* Copy from malloc'd memory */
1442 /* XXX this should be re-examined/tested with a compressed format */
1443 GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1444 GLuint srcRowStride = stImage->base.Width * blockSize;
1445 GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1446 st_texture_image_data(st,
1447 stObj->pt,
1448 stImage->base.Face,
1449 dstLevel,
1450 stImage->TexData,
1451 srcRowStride,
1452 srcSliceStride);
1453 _mesa_align_free(stImage->TexData);
1454 stImage->TexData = NULL;
1455 }
1456
1457 pipe_resource_reference(&stImage->pt, stObj->pt);
1458 }
1459
1460
1461 /**
1462 * Called during state validation. When this function is finished,
1463 * the texture object should be ready for rendering.
1464 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1465 */
1466 GLboolean
1467 st_finalize_texture(struct gl_context *ctx,
1468 struct pipe_context *pipe,
1469 struct gl_texture_object *tObj)
1470 {
1471 struct st_context *st = st_context(ctx);
1472 struct st_texture_object *stObj = st_texture_object(tObj);
1473 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1474 GLuint face;
1475 struct st_texture_image *firstImage;
1476 enum pipe_format firstImageFormat;
1477 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1478
1479 if (stObj->base._Complete) {
1480 /* The texture is complete and we know exactly how many mipmap levels
1481 * are present/needed. This is conditional because we may be called
1482 * from the st_generate_mipmap() function when the texture object is
1483 * incomplete. In that case, we'll have set stObj->lastLevel before
1484 * we get here.
1485 */
1486 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1487 stObj->base.Sampler.MinFilter == GL_NEAREST)
1488 stObj->lastLevel = stObj->base.BaseLevel;
1489 else
1490 stObj->lastLevel = stObj->base._MaxLevel;
1491 }
1492
1493 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1494 assert(firstImage);
1495
1496 /* If both firstImage and stObj point to a texture which can contain
1497 * all active images, favour firstImage. Note that because of the
1498 * completeness requirement, we know that the image dimensions
1499 * will match.
1500 */
1501 if (firstImage->pt &&
1502 firstImage->pt != stObj->pt &&
1503 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1504 pipe_resource_reference(&stObj->pt, firstImage->pt);
1505 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1506 }
1507
1508 /* Find gallium format for the Mesa texture */
1509 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1510
1511 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1512 {
1513 GLuint width, height, depth;
1514 if (!guess_base_level_size(stObj->base.Target,
1515 firstImage->base.Width2,
1516 firstImage->base.Height2,
1517 firstImage->base.Depth2,
1518 firstImage->base.Level,
1519 &width, &height, &depth)) {
1520 width = stObj->width0;
1521 height = stObj->height0;
1522 depth = stObj->depth0;
1523 }
1524 /* convert GL dims to Gallium dims */
1525 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1526 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1527 }
1528
1529 /* If we already have a gallium texture, check that it matches the texture
1530 * object's format, target, size, num_levels, etc.
1531 */
1532 if (stObj->pt) {
1533 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1534 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1535 stObj->pt->last_level < stObj->lastLevel ||
1536 stObj->pt->width0 != ptWidth ||
1537 stObj->pt->height0 != ptHeight ||
1538 stObj->pt->depth0 != ptDepth ||
1539 stObj->pt->array_size != ptLayers)
1540 {
1541 /* The gallium texture does not match the Mesa texture so delete the
1542 * gallium texture now. We'll make a new one below.
1543 */
1544 pipe_resource_reference(&stObj->pt, NULL);
1545 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1546 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1547 }
1548 }
1549
1550 /* May need to create a new gallium texture:
1551 */
1552 if (!stObj->pt) {
1553 GLuint bindings = default_bindings(st, firstImageFormat);
1554
1555 stObj->pt = st_texture_create(st,
1556 gl_target_to_pipe(stObj->base.Target),
1557 firstImageFormat,
1558 stObj->lastLevel,
1559 ptWidth,
1560 ptHeight,
1561 ptDepth,
1562 ptLayers,
1563 bindings);
1564
1565 if (!stObj->pt) {
1566 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1567 return GL_FALSE;
1568 }
1569 }
1570
1571 /* Pull in any images not in the object's texture:
1572 */
1573 for (face = 0; face < nr_faces; face++) {
1574 GLuint level;
1575 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1576 struct st_texture_image *stImage =
1577 st_texture_image(stObj->base.Image[face][level]);
1578
1579 /* Need to import images in main memory or held in other textures.
1580 */
1581 if (stImage && stObj->pt != stImage->pt) {
1582 if (level == 0 ||
1583 (stImage->base.Width == u_minify(stObj->width0, level) &&
1584 stImage->base.Height == u_minify(stObj->height0, level) &&
1585 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1586 /* src image fits expected dest mipmap level size */
1587 copy_image_data_to_texture(st, stObj, level, stImage);
1588 }
1589 }
1590 }
1591 }
1592
1593 return GL_TRUE;
1594 }
1595
1596
1597 /**
1598 * Returns pointer to a default/dummy texture.
1599 * This is typically used when the current shader has tex/sample instructions
1600 * but the user has not provided a (any) texture(s).
1601 */
1602 struct gl_texture_object *
1603 st_get_default_texture(struct st_context *st)
1604 {
1605 if (!st->default_texture) {
1606 static const GLenum target = GL_TEXTURE_2D;
1607 GLubyte pixels[16][16][4];
1608 struct gl_texture_object *texObj;
1609 struct gl_texture_image *texImg;
1610 GLuint i, j;
1611
1612 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1613 * when attempting to sample incomplete textures.
1614 */
1615 for (i = 0; i < 16; i++) {
1616 for (j = 0; j < 16; j++) {
1617 pixels[i][j][0] = 0;
1618 pixels[i][j][1] = 0;
1619 pixels[i][j][2] = 0;
1620 pixels[i][j][3] = 255;
1621 }
1622 }
1623
1624 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1625
1626 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1627
1628 _mesa_init_teximage_fields(st->ctx, texImg,
1629 16, 16, 1, 0, /* w, h, d, border */
1630 GL_RGBA, MESA_FORMAT_RGBA8888);
1631
1632 st_TexImage(st->ctx, 2,
1633 texImg,
1634 GL_RGBA, /* level, intformat */
1635 16, 16, 1, 0, /* w, h, d, border */
1636 GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1637 &st->ctx->DefaultPacking,
1638 0, GL_FALSE);
1639
1640 texObj->Sampler.MinFilter = GL_NEAREST;
1641 texObj->Sampler.MagFilter = GL_NEAREST;
1642 texObj->_Complete = GL_TRUE;
1643
1644 st->default_texture = texObj;
1645 }
1646 return st->default_texture;
1647 }
1648
1649
1650 /**
1651 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1652 * for a whole mipmap stack.
1653 */
1654 static GLboolean
1655 st_AllocTextureStorage(struct gl_context *ctx,
1656 struct gl_texture_object *texObj,
1657 GLsizei levels, GLsizei width,
1658 GLsizei height, GLsizei depth)
1659 {
1660 const GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1661 struct st_context *st = st_context(ctx);
1662 struct st_texture_object *stObj = st_texture_object(texObj);
1663 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1664 enum pipe_format fmt;
1665 GLint level;
1666
1667 assert(levels > 0);
1668
1669 /* Save the level=0 dimensions */
1670 stObj->width0 = width;
1671 stObj->height0 = height;
1672 stObj->depth0 = depth;
1673 stObj->lastLevel = levels - 1;
1674
1675 fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
1676
1677 bindings = default_bindings(st, fmt);
1678
1679 st_gl_texture_dims_to_pipe_dims(texObj->Target,
1680 width, height, depth,
1681 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1682
1683 stObj->pt = st_texture_create(st,
1684 gl_target_to_pipe(texObj->Target),
1685 fmt,
1686 levels,
1687 ptWidth,
1688 ptHeight,
1689 ptDepth,
1690 ptLayers,
1691 bindings);
1692 if (!stObj->pt)
1693 return GL_FALSE;
1694
1695 /* Set image resource pointers */
1696 for (level = 0; level < levels; level++) {
1697 GLuint face;
1698 for (face = 0; face < numFaces; face++) {
1699 struct st_texture_image *stImage =
1700 st_texture_image(texObj->Image[face][level]);
1701 pipe_resource_reference(&stImage->pt, stObj->pt);
1702 }
1703 }
1704
1705 return GL_TRUE;
1706 }
1707
1708
1709
1710 void
1711 st_init_texture_functions(struct dd_function_table *functions)
1712 {
1713 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1714 functions->TexImage1D = st_TexImage1D;
1715 functions->TexImage2D = st_TexImage2D;
1716 functions->TexImage3D = st_TexImage3D;
1717 functions->TexSubImage1D = _mesa_store_texsubimage1d;
1718 functions->TexSubImage2D = _mesa_store_texsubimage2d;
1719 functions->TexSubImage3D = _mesa_store_texsubimage3d;
1720 functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
1721 functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
1722 functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
1723 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1724 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1725 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1726 functions->GenerateMipmap = st_generate_mipmap;
1727
1728 functions->GetTexImage = st_GetTexImage;
1729
1730 /* compressed texture functions */
1731 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1732 functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1733
1734 functions->NewTextureObject = st_NewTextureObject;
1735 functions->NewTextureImage = st_NewTextureImage;
1736 functions->DeleteTextureImage = st_DeleteTextureImage;
1737 functions->DeleteTexture = st_DeleteTextureObject;
1738 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1739 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1740 functions->MapTextureImage = st_MapTextureImage;
1741 functions->UnmapTextureImage = st_UnmapTextureImage;
1742
1743 /* XXX Temporary until we can query pipe's texture sizes */
1744 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1745
1746 functions->AllocTextureStorage = st_AllocTextureStorage;
1747 }