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