st/mesa: use transfer_inline_write for memcpy TexSubImage path
[mesa.git] / src / mesa / state_tracker / st_cb_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <stdio.h>
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/format_utils.h"
34 #include "main/glformats.h"
35 #include "main/image.h"
36 #include "main/imports.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/pack.h"
40 #include "main/pbo.h"
41 #include "main/pixeltransfer.h"
42 #include "main/texcompress.h"
43 #include "main/texcompress_etc.h"
44 #include "main/texgetimage.h"
45 #include "main/teximage.h"
46 #include "main/texobj.h"
47 #include "main/texstore.h"
48
49 #include "state_tracker/st_debug.h"
50 #include "state_tracker/st_context.h"
51 #include "state_tracker/st_cb_fbo.h"
52 #include "state_tracker/st_cb_flush.h"
53 #include "state_tracker/st_cb_texture.h"
54 #include "state_tracker/st_cb_bufferobjects.h"
55 #include "state_tracker/st_format.h"
56 #include "state_tracker/st_texture.h"
57 #include "state_tracker/st_gen_mipmap.h"
58 #include "state_tracker/st_atom.h"
59
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "util/u_inlines.h"
63 #include "util/u_upload_mgr.h"
64 #include "pipe/p_shader_tokens.h"
65 #include "util/u_tile.h"
66 #include "util/u_format.h"
67 #include "util/u_surface.h"
68 #include "util/u_sampler.h"
69 #include "util/u_math.h"
70 #include "util/u_box.h"
71 #include "util/u_simple_shaders.h"
72 #include "cso_cache/cso_context.h"
73 #include "tgsi/tgsi_ureg.h"
74
75 #define DBG if (0) printf
76
77
78 enum pipe_texture_target
79 gl_target_to_pipe(GLenum target)
80 {
81 switch (target) {
82 case GL_TEXTURE_1D:
83 case GL_PROXY_TEXTURE_1D:
84 return PIPE_TEXTURE_1D;
85 case GL_TEXTURE_2D:
86 case GL_PROXY_TEXTURE_2D:
87 case GL_TEXTURE_EXTERNAL_OES:
88 case GL_TEXTURE_2D_MULTISAMPLE:
89 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
90 return PIPE_TEXTURE_2D;
91 case GL_TEXTURE_RECTANGLE_NV:
92 case GL_PROXY_TEXTURE_RECTANGLE_NV:
93 return PIPE_TEXTURE_RECT;
94 case GL_TEXTURE_3D:
95 case GL_PROXY_TEXTURE_3D:
96 return PIPE_TEXTURE_3D;
97 case GL_TEXTURE_CUBE_MAP_ARB:
98 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
99 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
100 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
101 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
102 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
103 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
104 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
105 return PIPE_TEXTURE_CUBE;
106 case GL_TEXTURE_1D_ARRAY_EXT:
107 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
108 return PIPE_TEXTURE_1D_ARRAY;
109 case GL_TEXTURE_2D_ARRAY_EXT:
110 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
111 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
112 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
113 return PIPE_TEXTURE_2D_ARRAY;
114 case GL_TEXTURE_BUFFER:
115 return PIPE_BUFFER;
116 case GL_TEXTURE_CUBE_MAP_ARRAY:
117 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
118 return PIPE_TEXTURE_CUBE_ARRAY;
119 default:
120 assert(0);
121 return 0;
122 }
123 }
124
125
126 /** called via ctx->Driver.NewTextureImage() */
127 static struct gl_texture_image *
128 st_NewTextureImage(struct gl_context * ctx)
129 {
130 DBG("%s\n", __func__);
131 (void) ctx;
132 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
133 }
134
135
136 /** called via ctx->Driver.DeleteTextureImage() */
137 static void
138 st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
139 {
140 /* nothing special (yet) for st_texture_image */
141 _mesa_delete_texture_image(ctx, img);
142 }
143
144
145 /** called via ctx->Driver.NewTextureObject() */
146 static struct gl_texture_object *
147 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
148 {
149 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
150
151 DBG("%s\n", __func__);
152 _mesa_initialize_texture_object(ctx, &obj->base, name, target);
153
154 return &obj->base;
155 }
156
157 /** called via ctx->Driver.DeleteTextureObject() */
158 static void
159 st_DeleteTextureObject(struct gl_context *ctx,
160 struct gl_texture_object *texObj)
161 {
162 struct st_context *st = st_context(ctx);
163 struct st_texture_object *stObj = st_texture_object(texObj);
164
165 pipe_resource_reference(&stObj->pt, NULL);
166 st_texture_release_all_sampler_views(st, stObj);
167 st_texture_free_sampler_views(stObj);
168 _mesa_delete_texture_object(ctx, texObj);
169 }
170
171
172 /** called via ctx->Driver.FreeTextureImageBuffer() */
173 static void
174 st_FreeTextureImageBuffer(struct gl_context *ctx,
175 struct gl_texture_image *texImage)
176 {
177 struct st_texture_image *stImage = st_texture_image(texImage);
178
179 DBG("%s\n", __func__);
180
181 if (stImage->pt) {
182 pipe_resource_reference(&stImage->pt, NULL);
183 }
184
185 free(stImage->transfer);
186 stImage->transfer = NULL;
187 stImage->num_transfers = 0;
188 }
189
190
191 /** called via ctx->Driver.MapTextureImage() */
192 static void
193 st_MapTextureImage(struct gl_context *ctx,
194 struct gl_texture_image *texImage,
195 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
196 GLbitfield mode,
197 GLubyte **mapOut, GLint *rowStrideOut)
198 {
199 struct st_context *st = st_context(ctx);
200 struct st_texture_image *stImage = st_texture_image(texImage);
201 unsigned pipeMode;
202 GLubyte *map;
203 struct pipe_transfer *transfer;
204
205 pipeMode = 0x0;
206 if (mode & GL_MAP_READ_BIT)
207 pipeMode |= PIPE_TRANSFER_READ;
208 if (mode & GL_MAP_WRITE_BIT)
209 pipeMode |= PIPE_TRANSFER_WRITE;
210 if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
211 pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
212
213 map = st_texture_image_map(st, stImage, pipeMode, x, y, slice, w, h, 1,
214 &transfer);
215 if (map) {
216 if ((_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
217 (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)) {
218 /* ETC isn't supported by gallium and it's represented
219 * by uncompressed formats. Only write transfers with precompressed
220 * data are supported by ES3, which makes this really simple.
221 *
222 * Just create a temporary storage where the ETC texture will
223 * be stored. It will be decompressed in the Unmap function.
224 */
225 unsigned z = transfer->box.z;
226 struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
227
228 itransfer->temp_data =
229 malloc(_mesa_format_image_size(texImage->TexFormat, w, h, 1));
230 itransfer->temp_stride =
231 _mesa_format_row_stride(texImage->TexFormat, w);
232 itransfer->map = map;
233
234 *mapOut = itransfer->temp_data;
235 *rowStrideOut = itransfer->temp_stride;
236 }
237 else {
238 /* supported mapping */
239 *mapOut = map;
240 *rowStrideOut = transfer->stride;
241 }
242 }
243 else {
244 *mapOut = NULL;
245 *rowStrideOut = 0;
246 }
247 }
248
249
250 /** called via ctx->Driver.UnmapTextureImage() */
251 static void
252 st_UnmapTextureImage(struct gl_context *ctx,
253 struct gl_texture_image *texImage,
254 GLuint slice)
255 {
256 struct st_context *st = st_context(ctx);
257 struct st_texture_image *stImage = st_texture_image(texImage);
258
259 if ((_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
260 (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)) {
261 /* Decompress the ETC texture to the mapped one. */
262 unsigned z = slice + stImage->base.Face;
263 struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
264 struct pipe_transfer *transfer = itransfer->transfer;
265
266 assert(z == transfer->box.z);
267
268 if (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8) {
269 _mesa_etc1_unpack_rgba8888(itransfer->map, transfer->stride,
270 itransfer->temp_data,
271 itransfer->temp_stride,
272 transfer->box.width, transfer->box.height);
273 }
274 else {
275 _mesa_unpack_etc2_format(itransfer->map, transfer->stride,
276 itransfer->temp_data, itransfer->temp_stride,
277 transfer->box.width, transfer->box.height,
278 texImage->TexFormat);
279 }
280
281 free(itransfer->temp_data);
282 itransfer->temp_data = NULL;
283 itransfer->temp_stride = 0;
284 itransfer->map = 0;
285 }
286
287 st_texture_image_unmap(st, stImage, slice);
288 }
289
290
291 /**
292 * Return default texture resource binding bitmask for the given format.
293 */
294 static GLuint
295 default_bindings(struct st_context *st, enum pipe_format format)
296 {
297 struct pipe_screen *screen = st->pipe->screen;
298 const unsigned target = PIPE_TEXTURE_2D;
299 unsigned bindings;
300
301 if (util_format_is_depth_or_stencil(format))
302 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
303 else
304 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
305
306 if (screen->is_format_supported(screen, format, target, 0, bindings))
307 return bindings;
308 else {
309 /* Try non-sRGB. */
310 format = util_format_linear(format);
311
312 if (screen->is_format_supported(screen, format, target, 0, bindings))
313 return bindings;
314 else
315 return PIPE_BIND_SAMPLER_VIEW;
316 }
317 }
318
319
320 /**
321 * Given the size of a mipmap image, try to compute the size of the level=0
322 * mipmap image.
323 *
324 * Note that this isn't always accurate for odd-sized, non-POW textures.
325 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
326 *
327 * \return GL_TRUE for success, GL_FALSE for failure
328 */
329 static GLboolean
330 guess_base_level_size(GLenum target,
331 GLuint width, GLuint height, GLuint depth, GLuint level,
332 GLuint *width0, GLuint *height0, GLuint *depth0)
333 {
334 assert(width >= 1);
335 assert(height >= 1);
336 assert(depth >= 1);
337
338 if (level > 0) {
339 /* Guess the size of the base level.
340 * Depending on the image's size, we can't always make a guess here.
341 */
342 switch (target) {
343 case GL_TEXTURE_1D:
344 case GL_TEXTURE_1D_ARRAY:
345 width <<= level;
346 break;
347
348 case GL_TEXTURE_2D:
349 case GL_TEXTURE_2D_ARRAY:
350 /* We can't make a good guess here, because the base level dimensions
351 * can be non-square.
352 */
353 if (width == 1 || height == 1) {
354 return GL_FALSE;
355 }
356 width <<= level;
357 height <<= level;
358 break;
359
360 case GL_TEXTURE_CUBE_MAP:
361 case GL_TEXTURE_CUBE_MAP_ARRAY:
362 width <<= level;
363 height <<= level;
364 break;
365
366 case GL_TEXTURE_3D:
367 /* We can't make a good guess here, because the base level dimensions
368 * can be non-cube.
369 */
370 if (width == 1 || height == 1 || depth == 1) {
371 return GL_FALSE;
372 }
373 width <<= level;
374 height <<= level;
375 depth <<= level;
376 break;
377
378 case GL_TEXTURE_RECTANGLE:
379 break;
380
381 default:
382 assert(0);
383 }
384 }
385
386 *width0 = width;
387 *height0 = height;
388 *depth0 = depth;
389
390 return GL_TRUE;
391 }
392
393
394 /**
395 * Try to determine whether we should allocate memory for a full texture
396 * mipmap. The problem is when we get a glTexImage(level=0) call, we
397 * can't immediately know if other mipmap levels are coming next. Here
398 * we try to guess whether to allocate memory for a mipmap or just the
399 * 0th level.
400 *
401 * If we guess incorrectly here we'll later reallocate the right amount of
402 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
403 *
404 * \param stObj the texture object we're going to allocate memory for.
405 * \param stImage describes the incoming image which we need to store.
406 */
407 static boolean
408 allocate_full_mipmap(const struct st_texture_object *stObj,
409 const struct st_texture_image *stImage)
410 {
411 switch (stObj->base.Target) {
412 case GL_TEXTURE_RECTANGLE_NV:
413 case GL_TEXTURE_BUFFER:
414 case GL_TEXTURE_EXTERNAL_OES:
415 case GL_TEXTURE_2D_MULTISAMPLE:
416 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
417 /* these texture types cannot be mipmapped */
418 return FALSE;
419 }
420
421 if (stImage->base.Level > 0 || stObj->base.GenerateMipmap)
422 return TRUE;
423
424 if (stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
425 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT)
426 /* depth/stencil textures are seldom mipmapped */
427 return FALSE;
428
429 if (stObj->base.BaseLevel == 0 && stObj->base.MaxLevel == 0)
430 return FALSE;
431
432 if (stObj->base.Sampler.MinFilter == GL_NEAREST ||
433 stObj->base.Sampler.MinFilter == GL_LINEAR)
434 /* not a mipmap minification filter */
435 return FALSE;
436
437 if (stObj->base.Target == GL_TEXTURE_3D)
438 /* 3D textures are seldom mipmapped */
439 return FALSE;
440
441 return TRUE;
442 }
443
444
445 /**
446 * Try to allocate a pipe_resource object for the given st_texture_object.
447 *
448 * We use the given st_texture_image as a clue to determine the size of the
449 * mipmap image at level=0.
450 *
451 * \return GL_TRUE for success, GL_FALSE if out of memory.
452 */
453 static GLboolean
454 guess_and_alloc_texture(struct st_context *st,
455 struct st_texture_object *stObj,
456 const struct st_texture_image *stImage)
457 {
458 GLuint lastLevel, width, height, depth;
459 GLuint bindings;
460 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
461 enum pipe_format fmt;
462
463 DBG("%s\n", __func__);
464
465 assert(!stObj->pt);
466
467 if (!guess_base_level_size(stObj->base.Target,
468 stImage->base.Width2,
469 stImage->base.Height2,
470 stImage->base.Depth2,
471 stImage->base.Level,
472 &width, &height, &depth)) {
473 /* we can't determine the image size at level=0 */
474 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
475 /* this is not an out of memory error */
476 return GL_TRUE;
477 }
478
479 /* At this point, (width x height x depth) is the expected size of
480 * the level=0 mipmap image.
481 */
482
483 /* Guess a reasonable value for lastLevel. With OpenGL we have no
484 * idea how many mipmap levels will be in a texture until we start
485 * to render with it. Make an educated guess here but be prepared
486 * to re-allocating a texture buffer with space for more (or fewer)
487 * mipmap levels later.
488 */
489 if (allocate_full_mipmap(stObj, stImage)) {
490 /* alloc space for a full mipmap */
491 lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
492 width, height, depth) - 1;
493 }
494 else {
495 /* only alloc space for a single mipmap level */
496 lastLevel = 0;
497 }
498
499 /* Save the level=0 dimensions */
500 stObj->width0 = width;
501 stObj->height0 = height;
502 stObj->depth0 = depth;
503
504 fmt = st_mesa_format_to_pipe_format(st, stImage->base.TexFormat);
505
506 bindings = default_bindings(st, fmt);
507
508 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
509 width, height, depth,
510 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
511
512 stObj->pt = st_texture_create(st,
513 gl_target_to_pipe(stObj->base.Target),
514 fmt,
515 lastLevel,
516 ptWidth,
517 ptHeight,
518 ptDepth,
519 ptLayers, 0,
520 bindings);
521
522 stObj->lastLevel = lastLevel;
523
524 DBG("%s returning %d\n", __func__, (stObj->pt != NULL));
525
526 return stObj->pt != NULL;
527 }
528
529
530 /**
531 * Called via ctx->Driver.AllocTextureImageBuffer().
532 * If the texture object/buffer already has space for the indicated image,
533 * we're done. Otherwise, allocate memory for the new texture image.
534 */
535 static GLboolean
536 st_AllocTextureImageBuffer(struct gl_context *ctx,
537 struct gl_texture_image *texImage)
538 {
539 struct st_context *st = st_context(ctx);
540 struct st_texture_image *stImage = st_texture_image(texImage);
541 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
542 const GLuint level = texImage->Level;
543 GLuint width = texImage->Width;
544 GLuint height = texImage->Height;
545 GLuint depth = texImage->Depth;
546
547 DBG("%s\n", __func__);
548
549 assert(!stImage->pt); /* xxx this might be wrong */
550
551 /* Look if the parent texture object has space for this image */
552 if (stObj->pt &&
553 level <= stObj->pt->last_level &&
554 st_texture_match_image(st, stObj->pt, texImage)) {
555 /* this image will fit in the existing texture object's memory */
556 pipe_resource_reference(&stImage->pt, stObj->pt);
557 return GL_TRUE;
558 }
559
560 /* The parent texture object does not have space for this image */
561
562 pipe_resource_reference(&stObj->pt, NULL);
563 st_texture_release_all_sampler_views(st, stObj);
564
565 if (!guess_and_alloc_texture(st, stObj, stImage)) {
566 /* Probably out of memory.
567 * Try flushing any pending rendering, then retry.
568 */
569 st_finish(st);
570 if (!guess_and_alloc_texture(st, stObj, stImage)) {
571 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
572 return GL_FALSE;
573 }
574 }
575
576 if (stObj->pt &&
577 st_texture_match_image(st, stObj->pt, texImage)) {
578 /* The image will live in the object's mipmap memory */
579 pipe_resource_reference(&stImage->pt, stObj->pt);
580 assert(stImage->pt);
581 return GL_TRUE;
582 }
583 else {
584 /* Create a new, temporary texture/resource/buffer to hold this
585 * one texture image. Note that when we later access this image
586 * (either for mapping or copying) we'll want to always specify
587 * mipmap level=0, even if the image represents some other mipmap
588 * level.
589 */
590 enum pipe_format format =
591 st_mesa_format_to_pipe_format(st, texImage->TexFormat);
592 GLuint bindings = default_bindings(st, format);
593 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
594
595 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
596 width, height, depth,
597 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
598
599 stImage->pt = st_texture_create(st,
600 gl_target_to_pipe(stObj->base.Target),
601 format,
602 0, /* lastLevel */
603 ptWidth,
604 ptHeight,
605 ptDepth,
606 ptLayers, 0,
607 bindings);
608 return stImage->pt != NULL;
609 }
610 }
611
612
613 /**
614 * Preparation prior to glTexImage. Basically check the 'surface_based'
615 * field and switch to a "normal" tex image if necessary.
616 */
617 static void
618 prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
619 GLenum format, GLenum type)
620 {
621 struct gl_texture_object *texObj = texImage->TexObject;
622 struct st_texture_object *stObj = st_texture_object(texObj);
623
624 /* switch to "normal" */
625 if (stObj->surface_based) {
626 const GLenum target = texObj->Target;
627 const GLuint level = texImage->Level;
628 mesa_format texFormat;
629
630 _mesa_clear_texture_object(ctx, texObj);
631 pipe_resource_reference(&stObj->pt, NULL);
632
633 /* oops, need to init this image again */
634 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
635 texImage->InternalFormat, format,
636 type);
637
638 _mesa_init_teximage_fields(ctx, texImage,
639 texImage->Width, texImage->Height,
640 texImage->Depth, texImage->Border,
641 texImage->InternalFormat, texFormat);
642
643 stObj->surface_based = GL_FALSE;
644 }
645 }
646
647
648 /**
649 * Return a writemask for the gallium blit. The parameters can be base
650 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
651 */
652 unsigned
653 st_get_blit_mask(GLenum srcFormat, GLenum dstFormat)
654 {
655 switch (dstFormat) {
656 case GL_DEPTH_STENCIL:
657 switch (srcFormat) {
658 case GL_DEPTH_STENCIL:
659 return PIPE_MASK_ZS;
660 case GL_DEPTH_COMPONENT:
661 return PIPE_MASK_Z;
662 case GL_STENCIL_INDEX:
663 return PIPE_MASK_S;
664 default:
665 assert(0);
666 return 0;
667 }
668
669 case GL_DEPTH_COMPONENT:
670 switch (srcFormat) {
671 case GL_DEPTH_STENCIL:
672 case GL_DEPTH_COMPONENT:
673 return PIPE_MASK_Z;
674 default:
675 assert(0);
676 return 0;
677 }
678
679 case GL_STENCIL_INDEX:
680 switch (srcFormat) {
681 case GL_STENCIL_INDEX:
682 return PIPE_MASK_S;
683 default:
684 assert(0);
685 return 0;
686 }
687
688 default:
689 return PIPE_MASK_RGBA;
690 }
691 }
692
693 void
694 st_init_pbo_upload(struct st_context *st)
695 {
696 struct pipe_context *pipe = st->pipe;
697 struct pipe_screen *screen = pipe->screen;
698
699 st->pbo_upload.enabled =
700 screen->get_param(screen, PIPE_CAP_TEXTURE_BUFFER_OBJECTS) &&
701 screen->get_param(screen, PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT) >= 1 &&
702 screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_INTEGERS);
703 if (!st->pbo_upload.enabled)
704 return;
705
706 st->pbo_upload.rgba_only =
707 screen->get_param(screen, PIPE_CAP_BUFFER_SAMPLER_VIEW_RGBA_ONLY);
708
709 if (screen->get_param(screen, PIPE_CAP_TGSI_INSTANCEID)) {
710 if (screen->get_param(screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT)) {
711 st->pbo_upload.upload_layers = true;
712 } else if (screen->get_param(screen, PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES) >= 3) {
713 st->pbo_upload.upload_layers = true;
714 st->pbo_upload.use_gs = true;
715 }
716 }
717
718 /* Blend state */
719 memset(&st->pbo_upload.blend, 0, sizeof(struct pipe_blend_state));
720 st->pbo_upload.blend.rt[0].colormask = PIPE_MASK_RGBA;
721
722 /* Rasterizer state */
723 memset(&st->pbo_upload.raster, 0, sizeof(struct pipe_rasterizer_state));
724 st->pbo_upload.raster.half_pixel_center = 1;
725 }
726
727 void
728 st_destroy_pbo_upload(struct st_context *st)
729 {
730 if (st->pbo_upload.fs) {
731 cso_delete_fragment_shader(st->cso_context, st->pbo_upload.fs);
732 st->pbo_upload.fs = NULL;
733 }
734
735 if (st->pbo_upload.gs) {
736 cso_delete_geometry_shader(st->cso_context, st->pbo_upload.gs);
737 st->pbo_upload.gs = NULL;
738 }
739
740 if (st->pbo_upload.vs) {
741 cso_delete_vertex_shader(st->cso_context, st->pbo_upload.vs);
742 st->pbo_upload.vs = NULL;
743 }
744 }
745
746 /**
747 * Converts format to a format with the same components, types
748 * and sizes, but with the components in RGBA order.
749 */
750 static enum pipe_format
751 unswizzle_format(enum pipe_format format)
752 {
753 switch (format)
754 {
755 case PIPE_FORMAT_B8G8R8A8_UNORM:
756 case PIPE_FORMAT_A8R8G8B8_UNORM:
757 case PIPE_FORMAT_A8B8G8R8_UNORM:
758 return PIPE_FORMAT_R8G8B8A8_UNORM;
759
760 case PIPE_FORMAT_B10G10R10A2_UNORM:
761 return PIPE_FORMAT_R10G10B10A2_UNORM;
762
763 case PIPE_FORMAT_B10G10R10A2_SNORM:
764 return PIPE_FORMAT_R10G10B10A2_SNORM;
765
766 case PIPE_FORMAT_B10G10R10A2_UINT:
767 return PIPE_FORMAT_R10G10B10A2_UINT;
768
769 default:
770 return format;
771 }
772 }
773
774 /**
775 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
776 */
777 static enum pipe_format
778 alpha_to_red(enum pipe_format format)
779 {
780 switch (format)
781 {
782 case PIPE_FORMAT_A8_UNORM:
783 return PIPE_FORMAT_R8_UNORM;
784 case PIPE_FORMAT_A8_SNORM:
785 return PIPE_FORMAT_R8_SNORM;
786 case PIPE_FORMAT_A8_UINT:
787 return PIPE_FORMAT_R8_UINT;
788 case PIPE_FORMAT_A8_SINT:
789 return PIPE_FORMAT_R8_SINT;
790
791 case PIPE_FORMAT_A16_UNORM:
792 return PIPE_FORMAT_R16_UNORM;
793 case PIPE_FORMAT_A16_SNORM:
794 return PIPE_FORMAT_R16_SNORM;
795 case PIPE_FORMAT_A16_UINT:
796 return PIPE_FORMAT_R16_UINT;
797 case PIPE_FORMAT_A16_SINT:
798 return PIPE_FORMAT_R16_SINT;
799 case PIPE_FORMAT_A16_FLOAT:
800 return PIPE_FORMAT_R16_FLOAT;
801
802 case PIPE_FORMAT_A32_UINT:
803 return PIPE_FORMAT_R32_UINT;
804 case PIPE_FORMAT_A32_SINT:
805 return PIPE_FORMAT_R32_SINT;
806 case PIPE_FORMAT_A32_FLOAT:
807 return PIPE_FORMAT_R32_FLOAT;
808
809 default:
810 return format;
811 }
812 }
813
814 /**
815 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
816 */
817 static enum pipe_format
818 red_alpha_to_red_green(enum pipe_format format)
819 {
820 switch (format)
821 {
822 case PIPE_FORMAT_R8A8_UNORM:
823 return PIPE_FORMAT_R8G8_UNORM;
824 case PIPE_FORMAT_R8A8_SNORM:
825 return PIPE_FORMAT_R8G8_SNORM;
826 case PIPE_FORMAT_R8A8_UINT:
827 return PIPE_FORMAT_R8G8_UINT;
828 case PIPE_FORMAT_R8A8_SINT:
829 return PIPE_FORMAT_R8G8_SINT;
830
831 case PIPE_FORMAT_R16A16_UNORM:
832 return PIPE_FORMAT_R16G16_UNORM;
833 case PIPE_FORMAT_R16A16_SNORM:
834 return PIPE_FORMAT_R16G16_SNORM;
835 case PIPE_FORMAT_R16A16_UINT:
836 return PIPE_FORMAT_R16G16_UINT;
837 case PIPE_FORMAT_R16A16_SINT:
838 return PIPE_FORMAT_R16G16_SINT;
839 case PIPE_FORMAT_R16A16_FLOAT:
840 return PIPE_FORMAT_R16G16_FLOAT;
841
842 case PIPE_FORMAT_R32A32_UINT:
843 return PIPE_FORMAT_R32G32_UINT;
844 case PIPE_FORMAT_R32A32_SINT:
845 return PIPE_FORMAT_R32G32_SINT;
846 case PIPE_FORMAT_R32A32_FLOAT:
847 return PIPE_FORMAT_R32G32_FLOAT;
848
849 default:
850 return format;
851 }
852 }
853
854 /**
855 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
856 */
857 static enum pipe_format
858 luminance_alpha_to_red_green(enum pipe_format format)
859 {
860 switch (format)
861 {
862 case PIPE_FORMAT_L8A8_UNORM:
863 return PIPE_FORMAT_R8G8_UNORM;
864 case PIPE_FORMAT_L8A8_SNORM:
865 return PIPE_FORMAT_R8G8_SNORM;
866 case PIPE_FORMAT_L8A8_UINT:
867 return PIPE_FORMAT_R8G8_UINT;
868 case PIPE_FORMAT_L8A8_SINT:
869 return PIPE_FORMAT_R8G8_SINT;
870
871 case PIPE_FORMAT_L16A16_UNORM:
872 return PIPE_FORMAT_R16G16_UNORM;
873 case PIPE_FORMAT_L16A16_SNORM:
874 return PIPE_FORMAT_R16G16_SNORM;
875 case PIPE_FORMAT_L16A16_UINT:
876 return PIPE_FORMAT_R16G16_UINT;
877 case PIPE_FORMAT_L16A16_SINT:
878 return PIPE_FORMAT_R16G16_SINT;
879 case PIPE_FORMAT_L16A16_FLOAT:
880 return PIPE_FORMAT_R16G16_FLOAT;
881
882 case PIPE_FORMAT_L32A32_UINT:
883 return PIPE_FORMAT_R32G32_UINT;
884 case PIPE_FORMAT_L32A32_SINT:
885 return PIPE_FORMAT_R32G32_SINT;
886 case PIPE_FORMAT_L32A32_FLOAT:
887 return PIPE_FORMAT_R32G32_FLOAT;
888
889 default:
890 return format;
891 }
892 }
893
894 /**
895 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
896 */
897 static bool
898 format_is_alpha(enum pipe_format format)
899 {
900 const struct util_format_description *desc = util_format_description(format);
901
902 if (desc->nr_channels == 1 &&
903 desc->swizzle[0] == PIPE_SWIZZLE_0 &&
904 desc->swizzle[1] == PIPE_SWIZZLE_0 &&
905 desc->swizzle[2] == PIPE_SWIZZLE_0 &&
906 desc->swizzle[3] == PIPE_SWIZZLE_X)
907 return true;
908
909 return false;
910 }
911
912 /**
913 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
914 */
915 static bool
916 format_is_red(enum pipe_format format)
917 {
918 const struct util_format_description *desc = util_format_description(format);
919
920 if (desc->nr_channels == 1 &&
921 desc->swizzle[0] == PIPE_SWIZZLE_X &&
922 desc->swizzle[1] == PIPE_SWIZZLE_0 &&
923 desc->swizzle[2] == PIPE_SWIZZLE_0 &&
924 desc->swizzle[3] == PIPE_SWIZZLE_1)
925 return true;
926
927 return false;
928 }
929
930
931 /**
932 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
933 */
934 static bool
935 format_is_luminance(enum pipe_format format)
936 {
937 const struct util_format_description *desc = util_format_description(format);
938
939 if (desc->nr_channels == 1 &&
940 desc->swizzle[0] == PIPE_SWIZZLE_X &&
941 desc->swizzle[1] == PIPE_SWIZZLE_X &&
942 desc->swizzle[2] == PIPE_SWIZZLE_X &&
943 desc->swizzle[3] == PIPE_SWIZZLE_1)
944 return true;
945
946 return false;
947 }
948
949 /**
950 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
951 */
952 static bool
953 format_is_red_alpha(enum pipe_format format)
954 {
955 const struct util_format_description *desc = util_format_description(format);
956
957 if (desc->nr_channels == 2 &&
958 desc->swizzle[0] == PIPE_SWIZZLE_X &&
959 desc->swizzle[1] == PIPE_SWIZZLE_0 &&
960 desc->swizzle[2] == PIPE_SWIZZLE_0 &&
961 desc->swizzle[3] == PIPE_SWIZZLE_Y)
962 return true;
963
964 return false;
965 }
966
967 static bool
968 format_is_swizzled_rgba(enum pipe_format format)
969 {
970 const struct util_format_description *desc = util_format_description(format);
971
972 if ((desc->swizzle[0] == TGSI_SWIZZLE_X || desc->swizzle[0] == PIPE_SWIZZLE_0) &&
973 (desc->swizzle[1] == TGSI_SWIZZLE_Y || desc->swizzle[1] == PIPE_SWIZZLE_0) &&
974 (desc->swizzle[2] == TGSI_SWIZZLE_Z || desc->swizzle[2] == PIPE_SWIZZLE_0) &&
975 (desc->swizzle[3] == TGSI_SWIZZLE_W || desc->swizzle[3] == PIPE_SWIZZLE_1))
976 return false;
977
978 return true;
979 }
980
981 struct format_table
982 {
983 unsigned char swizzle[4];
984 enum pipe_format format;
985 };
986
987 static const struct format_table table_8888_unorm[] = {
988 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM },
989 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM },
990 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM },
991 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM }
992 };
993
994 static const struct format_table table_1010102_unorm[] = {
995 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM },
996 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM }
997 };
998
999 static const struct format_table table_1010102_snorm[] = {
1000 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM },
1001 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM }
1002 };
1003
1004 static const struct format_table table_1010102_uint[] = {
1005 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT },
1006 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT }
1007 };
1008
1009 static enum pipe_format
1010 swizzle_format(enum pipe_format format, const int * const swizzle)
1011 {
1012 unsigned i;
1013
1014 switch (format) {
1015 case PIPE_FORMAT_R8G8B8A8_UNORM:
1016 case PIPE_FORMAT_B8G8R8A8_UNORM:
1017 case PIPE_FORMAT_A8R8G8B8_UNORM:
1018 case PIPE_FORMAT_A8B8G8R8_UNORM:
1019 for (i = 0; i < ARRAY_SIZE(table_8888_unorm); i++) {
1020 if (swizzle[0] == table_8888_unorm[i].swizzle[0] &&
1021 swizzle[1] == table_8888_unorm[i].swizzle[1] &&
1022 swizzle[2] == table_8888_unorm[i].swizzle[2] &&
1023 swizzle[3] == table_8888_unorm[i].swizzle[3])
1024 return table_8888_unorm[i].format;
1025 }
1026 break;
1027
1028 case PIPE_FORMAT_R10G10B10A2_UNORM:
1029 case PIPE_FORMAT_B10G10R10A2_UNORM:
1030 for (i = 0; i < ARRAY_SIZE(table_1010102_unorm); i++) {
1031 if (swizzle[0] == table_1010102_unorm[i].swizzle[0] &&
1032 swizzle[1] == table_1010102_unorm[i].swizzle[1] &&
1033 swizzle[2] == table_1010102_unorm[i].swizzle[2] &&
1034 swizzle[3] == table_1010102_unorm[i].swizzle[3])
1035 return table_1010102_unorm[i].format;
1036 }
1037 break;
1038
1039 case PIPE_FORMAT_R10G10B10A2_SNORM:
1040 case PIPE_FORMAT_B10G10R10A2_SNORM:
1041 for (i = 0; i < ARRAY_SIZE(table_1010102_snorm); i++) {
1042 if (swizzle[0] == table_1010102_snorm[i].swizzle[0] &&
1043 swizzle[1] == table_1010102_snorm[i].swizzle[1] &&
1044 swizzle[2] == table_1010102_snorm[i].swizzle[2] &&
1045 swizzle[3] == table_1010102_snorm[i].swizzle[3])
1046 return table_1010102_snorm[i].format;
1047 }
1048 break;
1049
1050 case PIPE_FORMAT_R10G10B10A2_UINT:
1051 case PIPE_FORMAT_B10G10R10A2_UINT:
1052 for (i = 0; i < ARRAY_SIZE(table_1010102_uint); i++) {
1053 if (swizzle[0] == table_1010102_uint[i].swizzle[0] &&
1054 swizzle[1] == table_1010102_uint[i].swizzle[1] &&
1055 swizzle[2] == table_1010102_uint[i].swizzle[2] &&
1056 swizzle[3] == table_1010102_uint[i].swizzle[3])
1057 return table_1010102_uint[i].format;
1058 }
1059 break;
1060
1061 default:
1062 break;
1063 }
1064
1065 return PIPE_FORMAT_NONE;
1066 }
1067
1068 static bool
1069 reinterpret_formats(enum pipe_format *src_format, enum pipe_format *dst_format)
1070 {
1071 enum pipe_format src = *src_format;
1072 enum pipe_format dst = *dst_format;
1073
1074 /* Note: dst_format has already been transformed from luminance/intensity
1075 * to red when this function is called. The source format will never
1076 * be an intensity format, because GL_INTENSITY is not a legal value
1077 * for the format parameter in glTex(Sub)Image(). */
1078
1079 if (format_is_alpha(src)) {
1080 if (!format_is_alpha(dst))
1081 return false;
1082
1083 src = alpha_to_red(src);
1084 dst = alpha_to_red(dst);
1085 } else if (format_is_luminance(src)) {
1086 if (!format_is_red(dst) && !format_is_red_alpha(dst))
1087 return false;
1088
1089 src = util_format_luminance_to_red(src);
1090 } else if (util_format_is_luminance_alpha(src)) {
1091 src = luminance_alpha_to_red_green(src);
1092
1093 if (format_is_red_alpha(dst)) {
1094 dst = red_alpha_to_red_green(dst);
1095 } else if (!format_is_red(dst))
1096 return false;
1097 } else if (format_is_swizzled_rgba(src)) {
1098 const struct util_format_description *src_desc = util_format_description(src);
1099 const struct util_format_description *dst_desc = util_format_description(dst);
1100 int swizzle[4];
1101 unsigned i;
1102
1103 /* Make sure the format is an RGBA and not an RGBX format */
1104 if (src_desc->nr_channels != 4 || src_desc->swizzle[3] == PIPE_SWIZZLE_1)
1105 return false;
1106
1107 if (dst_desc->nr_channels != 4 || dst_desc->swizzle[3] == PIPE_SWIZZLE_1)
1108 return false;
1109
1110 for (i = 0; i < 4; i++)
1111 swizzle[i] = dst_desc->swizzle[src_desc->swizzle[i]];
1112
1113 dst = swizzle_format(dst, swizzle);
1114 if (dst == PIPE_FORMAT_NONE)
1115 return false;
1116
1117 src = unswizzle_format(src);
1118 }
1119
1120 *src_format = src;
1121 *dst_format = dst;
1122 return true;
1123 }
1124
1125 static void *
1126 create_pbo_upload_vs(struct st_context *st)
1127 {
1128 struct ureg_program *ureg;
1129 struct ureg_src in_pos;
1130 struct ureg_src in_instanceid;
1131 struct ureg_dst out_pos;
1132 struct ureg_dst out_layer;
1133
1134 ureg = ureg_create(PIPE_SHADER_VERTEX);
1135 if (!ureg)
1136 return NULL;
1137
1138 in_pos = ureg_DECL_vs_input(ureg, TGSI_SEMANTIC_POSITION);
1139
1140 out_pos = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
1141
1142 if (st->pbo_upload.upload_layers) {
1143 in_instanceid = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_INSTANCEID, 0);
1144
1145 if (!st->pbo_upload.use_gs)
1146 out_layer = ureg_DECL_output(ureg, TGSI_SEMANTIC_LAYER, 0);
1147 }
1148
1149 /* out_pos = in_pos */
1150 ureg_MOV(ureg, out_pos, in_pos);
1151
1152 if (st->pbo_upload.upload_layers) {
1153 if (st->pbo_upload.use_gs) {
1154 /* out_pos.z = i2f(gl_InstanceID) */
1155 ureg_I2F(ureg, ureg_writemask(out_pos, TGSI_WRITEMASK_Z),
1156 ureg_scalar(in_instanceid, TGSI_SWIZZLE_X));
1157 } else {
1158 /* out_layer = gl_InstanceID */
1159 ureg_MOV(ureg, out_layer, in_instanceid);
1160 }
1161 }
1162
1163 ureg_END(ureg);
1164
1165 return ureg_create_shader_and_destroy(ureg, st->pipe);
1166 }
1167
1168 static void *
1169 create_pbo_upload_gs(struct st_context *st)
1170 {
1171 static const int zero = 0;
1172 struct ureg_program *ureg;
1173 struct ureg_dst out_pos;
1174 struct ureg_dst out_layer;
1175 struct ureg_src in_pos;
1176 struct ureg_src imm;
1177 unsigned i;
1178
1179 ureg = ureg_create(PIPE_SHADER_GEOMETRY);
1180 if (!ureg)
1181 return NULL;
1182
1183 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM, PIPE_PRIM_TRIANGLES);
1184 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM, PIPE_PRIM_TRIANGLE_STRIP);
1185 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES, 3);
1186
1187 out_pos = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
1188 out_layer = ureg_DECL_output(ureg, TGSI_SEMANTIC_LAYER, 0);
1189
1190 in_pos = ureg_DECL_input(ureg, TGSI_SEMANTIC_POSITION, 0, 0, 1);
1191
1192 imm = ureg_DECL_immediate_int(ureg, &zero, 1);
1193
1194 for (i = 0; i < 3; ++i) {
1195 struct ureg_src in_pos_vertex = ureg_src_dimension(in_pos, i);
1196
1197 /* out_pos = in_pos[i] */
1198 ureg_MOV(ureg, out_pos, in_pos_vertex);
1199
1200 /* out_layer.x = f2i(in_pos[i].z) */
1201 ureg_F2I(ureg, ureg_writemask(out_layer, TGSI_WRITEMASK_X),
1202 ureg_scalar(in_pos_vertex, TGSI_SWIZZLE_Z));
1203
1204 ureg_EMIT(ureg, ureg_scalar(imm, TGSI_SWIZZLE_X));
1205 }
1206
1207 ureg_END(ureg);
1208
1209 return ureg_create_shader_and_destroy(ureg, st->pipe);
1210 }
1211
1212 static void *
1213 create_pbo_upload_fs(struct st_context *st)
1214 {
1215 struct pipe_context *pipe = st->pipe;
1216 struct pipe_screen *screen = pipe->screen;
1217 struct ureg_program *ureg;
1218 struct ureg_dst out;
1219 struct ureg_src sampler;
1220 struct ureg_src pos;
1221 struct ureg_src layer;
1222 struct ureg_src const0;
1223 struct ureg_dst temp0;
1224
1225 ureg = ureg_create(PIPE_SHADER_FRAGMENT);
1226 if (!ureg)
1227 return NULL;
1228
1229 out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
1230 sampler = ureg_DECL_sampler(ureg, 0);
1231 if (screen->get_param(screen, PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL)) {
1232 pos = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_POSITION, 0);
1233 } else {
1234 pos = ureg_DECL_fs_input(ureg, TGSI_SEMANTIC_POSITION, 0,
1235 TGSI_INTERPOLATE_LINEAR);
1236 }
1237 if (st->pbo_upload.upload_layers) {
1238 layer = ureg_DECL_fs_input(ureg, TGSI_SEMANTIC_LAYER, 0,
1239 TGSI_INTERPOLATE_CONSTANT);
1240 }
1241 const0 = ureg_DECL_constant(ureg, 0);
1242 temp0 = ureg_DECL_temporary(ureg);
1243
1244 /* Note: const0 = [ -xoffset + skip_pixels, -yoffset, stride, image_height ] */
1245
1246 /* temp0.xy = f2i(temp0.xy) */
1247 ureg_F2I(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_XY),
1248 ureg_swizzle(pos,
1249 TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y,
1250 TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y));
1251
1252 /* temp0.xy = temp0.xy + const0.xy */
1253 ureg_UADD(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_XY),
1254 ureg_swizzle(ureg_src(temp0),
1255 TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y,
1256 TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y),
1257 ureg_swizzle(const0,
1258 TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y,
1259 TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y));
1260
1261 /* temp0.x = const0.z * temp0.y + temp0.x */
1262 ureg_UMAD(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_X),
1263 ureg_scalar(const0, TGSI_SWIZZLE_Z),
1264 ureg_scalar(ureg_src(temp0), TGSI_SWIZZLE_Y),
1265 ureg_scalar(ureg_src(temp0), TGSI_SWIZZLE_X));
1266
1267 if (st->pbo_upload.upload_layers) {
1268 /* temp0.x = const0.w * layer + temp0.x */
1269 ureg_UMAD(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_X),
1270 ureg_scalar(const0, TGSI_SWIZZLE_W),
1271 ureg_scalar(layer, TGSI_SWIZZLE_X),
1272 ureg_scalar(ureg_src(temp0), TGSI_SWIZZLE_X));
1273 }
1274
1275 /* temp0.w = 0 */
1276 ureg_MOV(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_W), ureg_imm1u(ureg, 0));
1277
1278 /* out = txf(sampler, temp0.x) */
1279 ureg_TXF(ureg, out, TGSI_TEXTURE_BUFFER, ureg_src(temp0), sampler);
1280
1281 ureg_release_temporary(ureg, temp0);
1282
1283 ureg_END(ureg);
1284
1285 return ureg_create_shader_and_destroy(ureg, pipe);
1286 }
1287
1288 static bool
1289 try_pbo_upload_common(struct gl_context *ctx,
1290 struct pipe_surface *surface,
1291 int xoffset, int yoffset,
1292 unsigned upload_width, unsigned upload_height,
1293 struct pipe_resource *buffer,
1294 enum pipe_format src_format,
1295 intptr_t buf_offset,
1296 unsigned bytes_per_pixel,
1297 unsigned stride,
1298 unsigned image_height)
1299 {
1300 struct st_context *st = st_context(ctx);
1301 struct cso_context *cso = st->cso_context;
1302 struct pipe_context *pipe = st->pipe;
1303 unsigned depth = surface->u.tex.last_layer - surface->u.tex.first_layer + 1;
1304 unsigned skip_pixels = 0;
1305 bool success = false;
1306
1307 /* Check alignment. */
1308 {
1309 unsigned ofs = (buf_offset * bytes_per_pixel) % ctx->Const.TextureBufferOffsetAlignment;
1310 if (ofs != 0) {
1311 if (ofs % bytes_per_pixel != 0)
1312 return false;
1313
1314 skip_pixels = ofs / bytes_per_pixel;
1315 buf_offset -= skip_pixels;
1316 }
1317 }
1318
1319 /* Create the shaders */
1320 if (!st->pbo_upload.vs) {
1321 st->pbo_upload.vs = create_pbo_upload_vs(st);
1322 if (!st->pbo_upload.vs)
1323 return false;
1324 }
1325
1326 if (depth != 1 && st->pbo_upload.use_gs && !st->pbo_upload.gs) {
1327 st->pbo_upload.gs = create_pbo_upload_gs(st);
1328 if (!st->pbo_upload.gs)
1329 return false;
1330 }
1331
1332 if (!st->pbo_upload.fs) {
1333 st->pbo_upload.fs = create_pbo_upload_fs(st);
1334 if (!st->pbo_upload.fs)
1335 return false;
1336 }
1337
1338 cso_save_state(cso, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
1339 CSO_BIT_FRAGMENT_SAMPLERS |
1340 CSO_BIT_VERTEX_ELEMENTS |
1341 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
1342 CSO_BIT_FRAMEBUFFER |
1343 CSO_BIT_VIEWPORT |
1344 CSO_BIT_BLEND |
1345 CSO_BIT_DEPTH_STENCIL_ALPHA |
1346 CSO_BIT_RASTERIZER |
1347 CSO_BIT_STREAM_OUTPUTS |
1348 CSO_BIT_PAUSE_QUERIES |
1349 CSO_BITS_ALL_SHADERS));
1350 cso_save_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
1351
1352
1353 /* Set up the sampler_view */
1354 {
1355 unsigned first_element = buf_offset;
1356 unsigned last_element = buf_offset + skip_pixels + upload_width - 1
1357 + (upload_height - 1 + (depth - 1) * image_height) * stride;
1358 struct pipe_sampler_view templ;
1359 struct pipe_sampler_view *sampler_view;
1360 struct pipe_sampler_state sampler = {0};
1361 const struct pipe_sampler_state *samplers[1] = {&sampler};
1362
1363 /* This should be ensured by Mesa before calling our callbacks */
1364 assert((last_element + 1) * bytes_per_pixel <= buffer->width0);
1365
1366 if (last_element - first_element > ctx->Const.MaxTextureBufferSize - 1)
1367 goto fail;
1368
1369 memset(&templ, 0, sizeof(templ));
1370 templ.target = PIPE_BUFFER;
1371 templ.format = src_format;
1372 templ.u.buf.first_element = first_element;
1373 templ.u.buf.last_element = last_element;
1374 templ.swizzle_r = PIPE_SWIZZLE_X;
1375 templ.swizzle_g = PIPE_SWIZZLE_Y;
1376 templ.swizzle_b = PIPE_SWIZZLE_Z;
1377 templ.swizzle_a = PIPE_SWIZZLE_W;
1378
1379 sampler_view = pipe->create_sampler_view(pipe, buffer, &templ);
1380 if (sampler_view == NULL)
1381 goto fail;
1382
1383 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
1384
1385 pipe_sampler_view_reference(&sampler_view, NULL);
1386
1387 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
1388 }
1389
1390 /* Upload vertices */
1391 {
1392 struct pipe_vertex_buffer vbo;
1393 struct pipe_vertex_element velem;
1394
1395 float x0 = (float) xoffset / surface->width * 2.0f - 1.0f;
1396 float y0 = (float) yoffset / surface->height * 2.0f - 1.0f;
1397 float x1 = (float) (xoffset + upload_width) / surface->width * 2.0f - 1.0f;
1398 float y1 = (float) (yoffset + upload_height) / surface->height * 2.0f - 1.0f;
1399
1400 float *verts = NULL;
1401
1402 vbo.user_buffer = NULL;
1403 vbo.buffer = NULL;
1404 vbo.stride = 2 * sizeof(float);
1405
1406 u_upload_alloc(st->uploader, 0, 8 * sizeof(float), 4,
1407 &vbo.buffer_offset, &vbo.buffer, (void **) &verts);
1408 if (!verts)
1409 goto fail;
1410
1411 verts[0] = x0;
1412 verts[1] = y0;
1413 verts[2] = x0;
1414 verts[3] = y1;
1415 verts[4] = x1;
1416 verts[5] = y0;
1417 verts[6] = x1;
1418 verts[7] = y1;
1419
1420 u_upload_unmap(st->uploader);
1421
1422 velem.src_offset = 0;
1423 velem.instance_divisor = 0;
1424 velem.vertex_buffer_index = cso_get_aux_vertex_buffer_slot(cso);
1425 velem.src_format = PIPE_FORMAT_R32G32_FLOAT;
1426
1427 cso_set_vertex_elements(cso, 1, &velem);
1428
1429 cso_set_vertex_buffers(cso, velem.vertex_buffer_index, 1, &vbo);
1430
1431 pipe_resource_reference(&vbo.buffer, NULL);
1432 }
1433
1434 /* Upload constants */
1435 /* Note: the user buffer must be valid until draw time */
1436 struct {
1437 int32_t xoffset;
1438 int32_t yoffset;
1439 int32_t stride;
1440 int32_t image_size;
1441 } constants;
1442
1443 {
1444 struct pipe_constant_buffer cb;
1445
1446 constants.xoffset = -xoffset + skip_pixels;
1447 constants.yoffset = -yoffset;
1448 constants.stride = stride;
1449 constants.image_size = stride * image_height;
1450
1451 if (st->constbuf_uploader) {
1452 cb.buffer = NULL;
1453 cb.user_buffer = NULL;
1454 u_upload_data(st->constbuf_uploader, 0, sizeof(constants),
1455 ctx->Const.UniformBufferOffsetAlignment,
1456 &constants, &cb.buffer_offset, &cb.buffer);
1457 if (!cb.buffer)
1458 goto fail;
1459
1460 u_upload_unmap(st->constbuf_uploader);
1461 } else {
1462 cb.buffer = NULL;
1463 cb.user_buffer = &constants;
1464 cb.buffer_offset = 0;
1465 }
1466 cb.buffer_size = sizeof(constants);
1467
1468 cso_set_constant_buffer(cso, PIPE_SHADER_FRAGMENT, 0, &cb);
1469
1470 pipe_resource_reference(&cb.buffer, NULL);
1471 }
1472
1473 /* Framebuffer_state */
1474 {
1475 struct pipe_framebuffer_state fb;
1476 memset(&fb, 0, sizeof(fb));
1477 fb.width = surface->width;
1478 fb.height = surface->height;
1479 fb.nr_cbufs = 1;
1480 pipe_surface_reference(&fb.cbufs[0], surface);
1481
1482 cso_set_framebuffer(cso, &fb);
1483
1484 pipe_surface_reference(&fb.cbufs[0], NULL);
1485 }
1486
1487 cso_set_viewport_dims(cso, surface->width, surface->height, FALSE);
1488
1489 /* Blend state */
1490 cso_set_blend(cso, &st->pbo_upload.blend);
1491
1492 /* Depth/stencil/alpha state */
1493 {
1494 struct pipe_depth_stencil_alpha_state dsa;
1495 memset(&dsa, 0, sizeof(dsa));
1496 cso_set_depth_stencil_alpha(cso, &dsa);
1497 }
1498
1499 /* Rasterizer state */
1500 cso_set_rasterizer(cso, &st->pbo_upload.raster);
1501
1502 /* Set up the shaders */
1503 cso_set_vertex_shader_handle(cso, st->pbo_upload.vs);
1504
1505 cso_set_geometry_shader_handle(cso, depth != 1 ? st->pbo_upload.gs : NULL);
1506
1507 cso_set_tessctrl_shader_handle(cso, NULL);
1508
1509 cso_set_tesseval_shader_handle(cso, NULL);
1510
1511 cso_set_fragment_shader_handle(cso, st->pbo_upload.fs);
1512
1513 /* Disable stream output */
1514 cso_set_stream_outputs(cso, 0, NULL, 0);
1515
1516 if (depth == 1) {
1517 cso_draw_arrays(cso, PIPE_PRIM_TRIANGLE_STRIP, 0, 4);
1518 } else {
1519 cso_draw_arrays_instanced(cso, PIPE_PRIM_TRIANGLE_STRIP,
1520 0, 4, 0, depth);
1521 }
1522
1523 success = true;
1524
1525 fail:
1526 cso_restore_state(cso);
1527 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
1528
1529 return success;
1530 }
1531
1532 static bool
1533 try_pbo_upload(struct gl_context *ctx, GLuint dims,
1534 struct gl_texture_image *texImage,
1535 GLenum format, GLenum type,
1536 enum pipe_format dst_format,
1537 GLint xoffset, GLint yoffset, GLint zoffset,
1538 GLint width, GLint height, GLint depth,
1539 const void *pixels,
1540 const struct gl_pixelstore_attrib *unpack)
1541 {
1542 struct st_context *st = st_context(ctx);
1543 struct st_texture_image *stImage = st_texture_image(texImage);
1544 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1545 struct pipe_resource *texture = stImage->pt;
1546 struct pipe_context *pipe = st->pipe;
1547 struct pipe_screen *screen = pipe->screen;
1548 struct pipe_surface *surface = NULL;
1549 enum pipe_format src_format;
1550 const struct util_format_description *desc;
1551 GLenum gl_target = texImage->TexObject->Target;
1552 intptr_t buf_offset;
1553 unsigned bytes_per_pixel;
1554 unsigned stride, image_height;
1555 bool success;
1556
1557 if (!st->pbo_upload.enabled)
1558 return false;
1559
1560 /* From now on, we need the gallium representation of dimensions. */
1561 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1562 depth = height;
1563 height = 1;
1564 zoffset = yoffset;
1565 yoffset = 0;
1566 image_height = 1;
1567 } else {
1568 image_height = unpack->ImageHeight > 0 ? unpack->ImageHeight : height;
1569 }
1570
1571 if (depth != 1 && !st->pbo_upload.upload_layers)
1572 return false;
1573
1574 /* Choose the source format. Initially, we do so without checking driver
1575 * support at all because of the remapping we later perform and because
1576 * at least the Radeon driver actually supports some formats for texture
1577 * buffers which it doesn't support for regular textures. */
1578 src_format = st_choose_matching_format(st, 0, format, type, unpack->SwapBytes);
1579 if (!src_format) {
1580 return false;
1581 }
1582
1583 src_format = util_format_linear(src_format);
1584 desc = util_format_description(src_format);
1585
1586 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1587 return false;
1588
1589 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
1590 return false;
1591
1592 if (st->pbo_upload.rgba_only) {
1593 enum pipe_format orig_dst_format = dst_format;
1594
1595 if (!reinterpret_formats(&src_format, &dst_format)) {
1596 return false;
1597 }
1598
1599 if (dst_format != orig_dst_format &&
1600 !screen->is_format_supported(screen, dst_format, PIPE_TEXTURE_2D, 0,
1601 PIPE_BIND_RENDER_TARGET)) {
1602 return false;
1603 }
1604 }
1605
1606 if (!src_format ||
1607 !screen->is_format_supported(screen, src_format, PIPE_BUFFER, 0,
1608 PIPE_BIND_SAMPLER_VIEW)) {
1609 return false;
1610 }
1611
1612 /* Check if the offset satisfies the alignment requirements */
1613 buf_offset = (intptr_t) pixels;
1614 bytes_per_pixel = desc->block.bits / 8;
1615
1616 if (buf_offset % bytes_per_pixel) {
1617 return false;
1618 }
1619
1620 /* Convert to texels */
1621 buf_offset = buf_offset / bytes_per_pixel;
1622
1623 /* Compute the stride, taking unpack->Alignment into account */
1624 {
1625 unsigned pixels_per_row = unpack->RowLength > 0 ?
1626 unpack->RowLength : width;
1627 unsigned bytes_per_row = pixels_per_row * bytes_per_pixel;
1628 unsigned remainder = bytes_per_row % unpack->Alignment;
1629 unsigned offset_rows;
1630
1631 if (remainder > 0)
1632 bytes_per_row += (unpack->Alignment - remainder);
1633
1634 if (bytes_per_row % bytes_per_pixel) {
1635 return false;
1636 }
1637
1638 stride = bytes_per_row / bytes_per_pixel;
1639
1640 offset_rows = unpack->SkipRows;
1641 if (dims == 3)
1642 offset_rows += image_height * unpack->SkipImages;
1643
1644 buf_offset += unpack->SkipPixels + stride * offset_rows;
1645 }
1646
1647 /* Set up the surface */
1648 {
1649 unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
1650 unsigned max_layer = util_max_layer(texture, level);
1651
1652 zoffset += texImage->Face + texImage->TexObject->MinLayer;
1653
1654 struct pipe_surface templ;
1655 memset(&templ, 0, sizeof(templ));
1656 templ.format = dst_format;
1657 templ.u.tex.level = level;
1658 templ.u.tex.first_layer = MIN2(zoffset, max_layer);
1659 templ.u.tex.last_layer = MIN2(zoffset + depth - 1, max_layer);
1660
1661 surface = pipe->create_surface(pipe, texture, &templ);
1662 if (!surface)
1663 return false;
1664 }
1665
1666 success = try_pbo_upload_common(ctx, surface,
1667 xoffset, yoffset, width, height,
1668 st_buffer_object(unpack->BufferObj)->buffer,
1669 src_format,
1670 buf_offset,
1671 bytes_per_pixel, stride, image_height);
1672
1673 pipe_surface_reference(&surface, NULL);
1674
1675 return success;
1676 }
1677
1678 static void
1679 st_TexSubImage(struct gl_context *ctx, GLuint dims,
1680 struct gl_texture_image *texImage,
1681 GLint xoffset, GLint yoffset, GLint zoffset,
1682 GLint width, GLint height, GLint depth,
1683 GLenum format, GLenum type, const void *pixels,
1684 const struct gl_pixelstore_attrib *unpack)
1685 {
1686 struct st_context *st = st_context(ctx);
1687 struct st_texture_image *stImage = st_texture_image(texImage);
1688 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1689 struct pipe_context *pipe = st->pipe;
1690 struct pipe_screen *screen = pipe->screen;
1691 struct pipe_resource *dst = stImage->pt;
1692 struct pipe_resource *src = NULL;
1693 struct pipe_resource src_templ;
1694 struct pipe_transfer *transfer;
1695 struct pipe_blit_info blit;
1696 enum pipe_format src_format, dst_format;
1697 mesa_format mesa_src_format;
1698 GLenum gl_target = texImage->TexObject->Target;
1699 unsigned bind;
1700 GLubyte *map;
1701 unsigned dstz = texImage->Face + texImage->TexObject->MinLayer;
1702 unsigned dst_level = 0;
1703
1704 if (stObj->pt == stImage->pt)
1705 dst_level = texImage->TexObject->MinLevel + texImage->Level;
1706
1707 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
1708 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
1709
1710 if (!dst)
1711 goto fallback;
1712
1713 /* Try transfer_inline_write, which should be the fastest memcpy path. */
1714 if (pixels &&
1715 !_mesa_is_bufferobj(unpack->BufferObj) &&
1716 _mesa_texstore_can_use_memcpy(ctx, texImage->_BaseFormat,
1717 texImage->TexFormat, format, type,
1718 unpack)) {
1719 struct pipe_box box;
1720 unsigned stride, layer_stride;
1721 void *data;
1722
1723 stride = _mesa_image_row_stride(unpack, width, format, type);
1724 layer_stride = _mesa_image_image_stride(unpack, width, height, format,
1725 type);
1726 data = _mesa_image_address(dims, unpack, pixels, width, height, format,
1727 type, 0, 0, 0);
1728
1729 /* Convert to Gallium coordinates. */
1730 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1731 zoffset = yoffset;
1732 yoffset = 0;
1733 depth = height;
1734 height = 1;
1735 layer_stride = stride;
1736 }
1737
1738 u_box_3d(xoffset, yoffset, zoffset + dstz, width, height, depth, &box);
1739 pipe->transfer_inline_write(pipe, dst, dst_level, 0,
1740 &box, data, stride, layer_stride);
1741 return;
1742 }
1743
1744 if (!st->prefer_blit_based_texture_transfer) {
1745 goto fallback;
1746 }
1747
1748 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1749 * blit implementation in some drivers. */
1750 if (format == GL_DEPTH_STENCIL) {
1751 goto fallback;
1752 }
1753
1754 /* If the base internal format and the texture format don't match,
1755 * we can't use blit-based TexSubImage. */
1756 if (texImage->_BaseFormat !=
1757 _mesa_get_format_base_format(texImage->TexFormat)) {
1758 goto fallback;
1759 }
1760
1761
1762 /* See if the destination format is supported. */
1763 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
1764 bind = PIPE_BIND_DEPTH_STENCIL;
1765 else
1766 bind = PIPE_BIND_RENDER_TARGET;
1767
1768 /* For luminance and intensity, only the red channel is stored
1769 * in the destination. */
1770 dst_format = util_format_linear(dst->format);
1771 dst_format = util_format_luminance_to_red(dst_format);
1772 dst_format = util_format_intensity_to_red(dst_format);
1773
1774 if (!dst_format ||
1775 !screen->is_format_supported(screen, dst_format, dst->target,
1776 dst->nr_samples, bind)) {
1777 goto fallback;
1778 }
1779
1780 if (_mesa_is_bufferobj(unpack->BufferObj)) {
1781 if (try_pbo_upload(ctx, dims, texImage, format, type, dst_format,
1782 xoffset, yoffset, zoffset,
1783 width, height, depth, pixels, unpack))
1784 return;
1785 }
1786
1787 /* See if the texture format already matches the format and type,
1788 * in which case the memcpy-based fast path will likely be used and
1789 * we don't have to blit. */
1790 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
1791 type, unpack->SwapBytes, NULL)) {
1792 goto fallback;
1793 }
1794
1795 /* Choose the source format. */
1796 src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
1797 format, type, unpack->SwapBytes);
1798 if (!src_format) {
1799 goto fallback;
1800 }
1801
1802 mesa_src_format = st_pipe_format_to_mesa_format(src_format);
1803
1804 /* There is no reason to do this if we cannot use memcpy for the temporary
1805 * source texture at least. This also takes transfer ops into account,
1806 * etc. */
1807 if (!_mesa_texstore_can_use_memcpy(ctx,
1808 _mesa_get_format_base_format(mesa_src_format),
1809 mesa_src_format, format, type, unpack)) {
1810 goto fallback;
1811 }
1812
1813 /* TexSubImage only sets a single cubemap face. */
1814 if (gl_target == GL_TEXTURE_CUBE_MAP) {
1815 gl_target = GL_TEXTURE_2D;
1816 }
1817 /* TexSubImage can specify subsets of cube map array faces
1818 * so we need to upload via 2D array instead */
1819 if (gl_target == GL_TEXTURE_CUBE_MAP_ARRAY) {
1820 gl_target = GL_TEXTURE_2D_ARRAY;
1821 }
1822
1823 /* Initialize the source texture description. */
1824 memset(&src_templ, 0, sizeof(src_templ));
1825 src_templ.target = gl_target_to_pipe(gl_target);
1826 src_templ.format = src_format;
1827 src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
1828 src_templ.usage = PIPE_USAGE_STAGING;
1829
1830 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
1831 &src_templ.width0, &src_templ.height0,
1832 &src_templ.depth0, &src_templ.array_size);
1833
1834 /* Check for NPOT texture support. */
1835 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
1836 (!util_is_power_of_two(src_templ.width0) ||
1837 !util_is_power_of_two(src_templ.height0) ||
1838 !util_is_power_of_two(src_templ.depth0))) {
1839 goto fallback;
1840 }
1841
1842 /* Create the source texture. */
1843 src = screen->resource_create(screen, &src_templ);
1844 if (!src) {
1845 goto fallback;
1846 }
1847
1848 /* Map source pixels. */
1849 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
1850 format, type, pixels, unpack,
1851 "glTexSubImage");
1852 if (!pixels) {
1853 /* This is a GL error. */
1854 pipe_resource_reference(&src, NULL);
1855 return;
1856 }
1857
1858 /* From now on, we need the gallium representation of dimensions. */
1859 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1860 zoffset = yoffset;
1861 yoffset = 0;
1862 depth = height;
1863 height = 1;
1864 }
1865
1866 map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
1867 width, height, depth, &transfer);
1868 if (!map) {
1869 _mesa_unmap_teximage_pbo(ctx, unpack);
1870 pipe_resource_reference(&src, NULL);
1871 goto fallback;
1872 }
1873
1874 /* Upload pixels (just memcpy). */
1875 {
1876 const uint bytesPerRow = width * util_format_get_blocksize(src_format);
1877 GLuint row, slice;
1878
1879 for (slice = 0; slice < (unsigned) depth; slice++) {
1880 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1881 /* 1D array textures.
1882 * We need to convert gallium coords to GL coords.
1883 */
1884 void *src = _mesa_image_address2d(unpack, pixels,
1885 width, depth, format,
1886 type, slice, 0);
1887 memcpy(map, src, bytesPerRow);
1888 }
1889 else {
1890 ubyte *slice_map = map;
1891
1892 for (row = 0; row < (unsigned) height; row++) {
1893 void *src = _mesa_image_address(dims, unpack, pixels,
1894 width, height, format,
1895 type, slice, row, 0);
1896 memcpy(slice_map, src, bytesPerRow);
1897 slice_map += transfer->stride;
1898 }
1899 }
1900 map += transfer->layer_stride;
1901 }
1902 }
1903
1904 pipe_transfer_unmap(pipe, transfer);
1905 _mesa_unmap_teximage_pbo(ctx, unpack);
1906
1907 /* Blit. */
1908 memset(&blit, 0, sizeof(blit));
1909 blit.src.resource = src;
1910 blit.src.level = 0;
1911 blit.src.format = src_format;
1912 blit.dst.resource = dst;
1913 blit.dst.level = dst_level;
1914 blit.dst.format = dst_format;
1915 blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
1916 blit.dst.box.x = xoffset;
1917 blit.dst.box.y = yoffset;
1918 blit.dst.box.z = zoffset + dstz;
1919 blit.src.box.width = blit.dst.box.width = width;
1920 blit.src.box.height = blit.dst.box.height = height;
1921 blit.src.box.depth = blit.dst.box.depth = depth;
1922 blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
1923 blit.filter = PIPE_TEX_FILTER_NEAREST;
1924 blit.scissor_enable = FALSE;
1925
1926 st->pipe->blit(st->pipe, &blit);
1927
1928 pipe_resource_reference(&src, NULL);
1929 return;
1930
1931 fallback:
1932 _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
1933 width, height, depth, format, type, pixels,
1934 unpack);
1935 }
1936
1937 static void
1938 st_TexImage(struct gl_context * ctx, GLuint dims,
1939 struct gl_texture_image *texImage,
1940 GLenum format, GLenum type, const void *pixels,
1941 const struct gl_pixelstore_attrib *unpack)
1942 {
1943 assert(dims == 1 || dims == 2 || dims == 3);
1944
1945 prep_teximage(ctx, texImage, format, type);
1946
1947 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
1948 return;
1949
1950 /* allocate storage for texture data */
1951 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
1952 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
1953 return;
1954 }
1955
1956 st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
1957 texImage->Width, texImage->Height, texImage->Depth,
1958 format, type, pixels, unpack);
1959 }
1960
1961
1962 static void
1963 st_CompressedTexSubImage(struct gl_context *ctx, GLuint dims,
1964 struct gl_texture_image *texImage,
1965 GLint x, GLint y, GLint z,
1966 GLsizei w, GLsizei h, GLsizei d,
1967 GLenum format, GLsizei imageSize, const void *data)
1968 {
1969 struct st_context *st = st_context(ctx);
1970 struct st_texture_image *stImage = st_texture_image(texImage);
1971 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1972 struct pipe_resource *texture = stImage->pt;
1973 struct pipe_context *pipe = st->pipe;
1974 struct pipe_screen *screen = pipe->screen;
1975 struct pipe_resource *dst = stImage->pt;
1976 struct pipe_surface *surface = NULL;
1977 struct compressed_pixelstore store;
1978 enum pipe_format copy_format;
1979 unsigned bytes_per_block;
1980 unsigned bw, bh;
1981 intptr_t buf_offset;
1982 bool success = false;
1983
1984 /* Check basic pre-conditions for PBO upload */
1985 if (!st->prefer_blit_based_texture_transfer) {
1986 goto fallback;
1987 }
1988
1989 if (!_mesa_is_bufferobj(ctx->Unpack.BufferObj))
1990 goto fallback;
1991
1992 if ((_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
1993 (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)) {
1994 /* ETC isn't supported and is represented by uncompressed formats. */
1995 goto fallback;
1996 }
1997
1998 if (!dst) {
1999 goto fallback;
2000 }
2001
2002 if (!st->pbo_upload.enabled ||
2003 !screen->get_param(screen, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS)) {
2004 goto fallback;
2005 }
2006
2007 /* Choose the pipe format for the upload. */
2008 bytes_per_block = util_format_get_blocksize(dst->format);
2009 bw = util_format_get_blockwidth(dst->format);
2010 bh = util_format_get_blockheight(dst->format);
2011
2012 switch (bytes_per_block) {
2013 case 8:
2014 copy_format = PIPE_FORMAT_R16G16B16A16_UINT;
2015 break;
2016 case 16:
2017 copy_format = PIPE_FORMAT_R32G32B32A32_UINT;
2018 break;
2019 default:
2020 goto fallback;
2021 }
2022
2023 if (!screen->is_format_supported(screen, copy_format, PIPE_BUFFER, 0,
2024 PIPE_BIND_SAMPLER_VIEW)) {
2025 goto fallback;
2026 }
2027
2028 if (!screen->is_format_supported(screen, copy_format, dst->target,
2029 dst->nr_samples, PIPE_BIND_RENDER_TARGET)) {
2030 goto fallback;
2031 }
2032
2033 /* Interpret the pixelstore settings. */
2034 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat, w, h, d,
2035 &ctx->Unpack, &store);
2036 assert(store.CopyBytesPerRow % bytes_per_block == 0);
2037 assert(store.SkipBytes % bytes_per_block == 0);
2038
2039 /* Compute the offset into the buffer */
2040 buf_offset = (intptr_t)data + store.SkipBytes;
2041
2042 if (buf_offset % bytes_per_block) {
2043 goto fallback;
2044 }
2045
2046 buf_offset = buf_offset / bytes_per_block;
2047
2048 /* Set up the surface. */
2049 {
2050 unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
2051 unsigned max_layer = util_max_layer(texture, level);
2052
2053 z += texImage->Face + texImage->TexObject->MinLayer;
2054
2055 struct pipe_surface templ;
2056 memset(&templ, 0, sizeof(templ));
2057 templ.format = copy_format;
2058 templ.u.tex.level = level;
2059 templ.u.tex.first_layer = MIN2(z, max_layer);
2060 templ.u.tex.last_layer = MIN2(z + d - 1, max_layer);
2061
2062 surface = pipe->create_surface(pipe, texture, &templ);
2063 if (!surface)
2064 goto fallback;
2065 }
2066
2067 success = try_pbo_upload_common(ctx, surface,
2068 x / bw, y / bh,
2069 store.CopyBytesPerRow / bytes_per_block,
2070 store.CopyRowsPerSlice,
2071 st_buffer_object(ctx->Unpack.BufferObj)->buffer,
2072 copy_format,
2073 buf_offset,
2074 bytes_per_block,
2075 store.TotalBytesPerRow / bytes_per_block,
2076 store.TotalRowsPerSlice);
2077
2078 pipe_surface_reference(&surface, NULL);
2079
2080 if (success)
2081 return;
2082
2083 fallback:
2084 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
2085 x, y, z, w, h, d,
2086 format, imageSize, data);
2087 }
2088
2089 static void
2090 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
2091 struct gl_texture_image *texImage,
2092 GLsizei imageSize, const void *data)
2093 {
2094 prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
2095
2096 /* only 2D and 3D compressed images are supported at this time */
2097 if (dims == 1) {
2098 _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
2099 return;
2100 }
2101
2102 /* This is pretty simple, because unlike the general texstore path we don't
2103 * have to worry about the usual image unpacking or image transfer
2104 * operations.
2105 */
2106 assert(texImage);
2107 assert(texImage->Width > 0);
2108 assert(texImage->Height > 0);
2109 assert(texImage->Depth > 0);
2110
2111 /* allocate storage for texture data */
2112 if (!st_AllocTextureImageBuffer(ctx, texImage)) {
2113 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
2114 return;
2115 }
2116
2117 st_CompressedTexSubImage(ctx, dims, texImage,
2118 0, 0, 0,
2119 texImage->Width, texImage->Height, texImage->Depth,
2120 texImage->TexFormat,
2121 imageSize, data);
2122 }
2123
2124
2125
2126
2127 /**
2128 * Called via ctx->Driver.GetTexSubImage()
2129 *
2130 * This uses a blit to copy the texture to a texture format which matches
2131 * the format and type combo and then a fast read-back is done using memcpy.
2132 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
2133 * a format which matches the swizzling.
2134 *
2135 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
2136 *
2137 * NOTE: Drivers usually do a blit to convert between tiled and linear
2138 * texture layouts during texture uploads/downloads, so the blit
2139 * we do here should be free in such cases.
2140 */
2141 static void
2142 st_GetTexSubImage(struct gl_context * ctx,
2143 GLint xoffset, GLint yoffset, GLint zoffset,
2144 GLsizei width, GLsizei height, GLint depth,
2145 GLenum format, GLenum type, void * pixels,
2146 struct gl_texture_image *texImage)
2147 {
2148 struct st_context *st = st_context(ctx);
2149 struct pipe_context *pipe = st->pipe;
2150 struct pipe_screen *screen = pipe->screen;
2151 struct st_texture_image *stImage = st_texture_image(texImage);
2152 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
2153 struct pipe_resource *src = stObj->pt;
2154 struct pipe_resource *dst = NULL;
2155 struct pipe_resource dst_templ;
2156 enum pipe_format dst_format, src_format;
2157 mesa_format mesa_format;
2158 GLenum gl_target = texImage->TexObject->Target;
2159 enum pipe_texture_target pipe_target;
2160 struct pipe_blit_info blit;
2161 unsigned bind = PIPE_BIND_TRANSFER_READ;
2162 struct pipe_transfer *tex_xfer;
2163 ubyte *map = NULL;
2164 boolean done = FALSE;
2165
2166 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
2167 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
2168
2169 if (!st->prefer_blit_based_texture_transfer &&
2170 !_mesa_is_format_compressed(texImage->TexFormat)) {
2171 /* Try to avoid the fallback if we're doing texture decompression here */
2172 goto fallback;
2173 }
2174
2175 /* Handle non-finalized textures. */
2176 if (!stImage->pt || stImage->pt != stObj->pt || !src) {
2177 goto fallback;
2178 }
2179
2180 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
2181 * due to an incomplete stencil blit implementation in some drivers. */
2182 if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
2183 goto fallback;
2184 }
2185
2186 /* If the base internal format and the texture format don't match, we have
2187 * to fall back to _mesa_GetTexImage_sw. */
2188 if (texImage->_BaseFormat !=
2189 _mesa_get_format_base_format(texImage->TexFormat)) {
2190 goto fallback;
2191 }
2192
2193 /* See if the texture format already matches the format and type,
2194 * in which case the memcpy-based fast path will be used. */
2195 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
2196 type, ctx->Pack.SwapBytes, NULL)) {
2197 goto fallback;
2198 }
2199
2200 /* Convert the source format to what is expected by GetTexImage
2201 * and see if it's supported.
2202 *
2203 * This only applies to glGetTexImage:
2204 * - Luminance must be returned as (L,0,0,1).
2205 * - Luminance alpha must be returned as (L,0,0,A).
2206 * - Intensity must be returned as (I,0,0,1)
2207 */
2208 if (stObj->surface_based)
2209 src_format = util_format_linear(stObj->surface_format);
2210 else
2211 src_format = util_format_linear(src->format);
2212 src_format = util_format_luminance_to_red(src_format);
2213 src_format = util_format_intensity_to_red(src_format);
2214
2215 if (!src_format ||
2216 !screen->is_format_supported(screen, src_format, src->target,
2217 src->nr_samples,
2218 PIPE_BIND_SAMPLER_VIEW)) {
2219 goto fallback;
2220 }
2221
2222 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
2223 bind |= PIPE_BIND_DEPTH_STENCIL;
2224 else
2225 bind |= PIPE_BIND_RENDER_TARGET;
2226
2227 /* GetTexImage only returns a single face for cubemaps. */
2228 if (gl_target == GL_TEXTURE_CUBE_MAP) {
2229 gl_target = GL_TEXTURE_2D;
2230 }
2231 pipe_target = gl_target_to_pipe(gl_target);
2232
2233 /* Choose the destination format by finding the best match
2234 * for the format+type combo. */
2235 dst_format = st_choose_matching_format(st, bind, format, type,
2236 ctx->Pack.SwapBytes);
2237
2238 if (dst_format == PIPE_FORMAT_NONE) {
2239 GLenum dst_glformat;
2240
2241 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
2242 * where decompression with a blit is always preferred. */
2243 if (!util_format_is_compressed(src->format)) {
2244 goto fallback;
2245 }
2246
2247 /* Set the appropriate format for the decompressed texture.
2248 * Luminance and sRGB formats shouldn't appear here.*/
2249 switch (src_format) {
2250 case PIPE_FORMAT_DXT1_RGB:
2251 case PIPE_FORMAT_DXT1_RGBA:
2252 case PIPE_FORMAT_DXT3_RGBA:
2253 case PIPE_FORMAT_DXT5_RGBA:
2254 case PIPE_FORMAT_RGTC1_UNORM:
2255 case PIPE_FORMAT_RGTC2_UNORM:
2256 case PIPE_FORMAT_ETC1_RGB8:
2257 case PIPE_FORMAT_BPTC_RGBA_UNORM:
2258 dst_glformat = GL_RGBA8;
2259 break;
2260 case PIPE_FORMAT_RGTC1_SNORM:
2261 case PIPE_FORMAT_RGTC2_SNORM:
2262 if (!ctx->Extensions.EXT_texture_snorm)
2263 goto fallback;
2264 dst_glformat = GL_RGBA8_SNORM;
2265 break;
2266 case PIPE_FORMAT_BPTC_RGB_FLOAT:
2267 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
2268 if (!ctx->Extensions.ARB_texture_float)
2269 goto fallback;
2270 dst_glformat = GL_RGBA32F;
2271 break;
2272 default:
2273 assert(0);
2274 goto fallback;
2275 }
2276
2277 dst_format = st_choose_format(st, dst_glformat, format, type,
2278 pipe_target, 0, bind, FALSE);
2279
2280 if (dst_format == PIPE_FORMAT_NONE) {
2281 /* unable to get an rgba format!?! */
2282 goto fallback;
2283 }
2284 }
2285
2286 /* create the destination texture of size (width X height X depth) */
2287 memset(&dst_templ, 0, sizeof(dst_templ));
2288 dst_templ.target = pipe_target;
2289 dst_templ.format = dst_format;
2290 dst_templ.bind = bind;
2291 dst_templ.usage = PIPE_USAGE_STAGING;
2292
2293 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
2294 &dst_templ.width0, &dst_templ.height0,
2295 &dst_templ.depth0, &dst_templ.array_size);
2296
2297 dst = screen->resource_create(screen, &dst_templ);
2298 if (!dst) {
2299 goto fallback;
2300 }
2301
2302 /* From now on, we need the gallium representation of dimensions. */
2303 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2304 zoffset = yoffset;
2305 yoffset = 0;
2306 depth = height;
2307 height = 1;
2308 }
2309
2310 assert(texImage->Face == 0 ||
2311 texImage->TexObject->MinLayer == 0 ||
2312 zoffset == 0);
2313
2314 memset(&blit, 0, sizeof(blit));
2315 blit.src.resource = src;
2316 blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
2317 blit.src.format = src_format;
2318 blit.dst.resource = dst;
2319 blit.dst.level = 0;
2320 blit.dst.format = dst->format;
2321 blit.src.box.x = xoffset;
2322 blit.dst.box.x = 0;
2323 blit.src.box.y = yoffset;
2324 blit.dst.box.y = 0;
2325 blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
2326 blit.dst.box.z = 0;
2327 blit.src.box.width = blit.dst.box.width = width;
2328 blit.src.box.height = blit.dst.box.height = height;
2329 blit.src.box.depth = blit.dst.box.depth = depth;
2330 blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
2331 blit.filter = PIPE_TEX_FILTER_NEAREST;
2332 blit.scissor_enable = FALSE;
2333
2334 /* blit/render/decompress */
2335 st->pipe->blit(st->pipe, &blit);
2336
2337 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
2338
2339 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
2340 0, 0, 0, width, height, depth, &tex_xfer);
2341 if (!map) {
2342 goto end;
2343 }
2344
2345 mesa_format = st_pipe_format_to_mesa_format(dst_format);
2346
2347 /* copy/pack data into user buffer */
2348 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
2349 ctx->Pack.SwapBytes, NULL)) {
2350 /* memcpy */
2351 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
2352 GLuint row, slice;
2353
2354 for (slice = 0; slice < depth; slice++) {
2355 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2356 /* 1D array textures.
2357 * We need to convert gallium coords to GL coords.
2358 */
2359 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2360 width, depth, format,
2361 type, 0, slice, 0);
2362 memcpy(dest, map, bytesPerRow);
2363 }
2364 else {
2365 ubyte *slice_map = map;
2366
2367 for (row = 0; row < height; row++) {
2368 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2369 width, height, format,
2370 type, slice, row, 0);
2371 memcpy(dest, slice_map, bytesPerRow);
2372 slice_map += tex_xfer->stride;
2373 }
2374 }
2375 map += tex_xfer->layer_stride;
2376 }
2377 }
2378 else {
2379 /* format translation via floats */
2380 GLuint row, slice;
2381 GLfloat *rgba;
2382 uint32_t dstMesaFormat;
2383 int dstStride, srcStride;
2384
2385 assert(util_format_is_compressed(src->format));
2386
2387 rgba = malloc(width * 4 * sizeof(GLfloat));
2388 if (!rgba) {
2389 goto end;
2390 }
2391
2392 if (ST_DEBUG & DEBUG_FALLBACK)
2393 debug_printf("%s: fallback format translation\n", __func__);
2394
2395 dstMesaFormat = _mesa_format_from_format_and_type(format, type);
2396 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
2397 srcStride = 4 * width * sizeof(GLfloat);
2398 for (slice = 0; slice < depth; slice++) {
2399 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2400 /* 1D array textures.
2401 * We need to convert gallium coords to GL coords.
2402 */
2403 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2404 width, depth, format,
2405 type, 0, slice, 0);
2406
2407 /* get float[4] rgba row from surface */
2408 pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, 1,
2409 dst_format, rgba);
2410
2411 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2412 rgba, RGBA32_FLOAT, srcStride,
2413 width, 1, NULL);
2414 }
2415 else {
2416 for (row = 0; row < height; row++) {
2417 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2418 width, height, format,
2419 type, slice, row, 0);
2420
2421 /* get float[4] rgba row from surface */
2422 pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1,
2423 dst_format, rgba);
2424
2425 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2426 rgba, RGBA32_FLOAT, srcStride,
2427 width, 1, NULL);
2428 }
2429 }
2430 map += tex_xfer->layer_stride;
2431 }
2432
2433 free(rgba);
2434 }
2435 done = TRUE;
2436
2437 end:
2438 if (map)
2439 pipe_transfer_unmap(pipe, tex_xfer);
2440
2441 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
2442 pipe_resource_reference(&dst, NULL);
2443
2444 fallback:
2445 if (!done) {
2446 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
2447 width, height, depth,
2448 format, type, pixels, texImage);
2449 }
2450 }
2451
2452
2453 /**
2454 * Do a CopyTexSubImage operation using a read transfer from the source,
2455 * a write transfer to the destination and get_tile()/put_tile() to access
2456 * the pixels/texels.
2457 *
2458 * Note: srcY=0=TOP of renderbuffer
2459 */
2460 static void
2461 fallback_copy_texsubimage(struct gl_context *ctx,
2462 struct st_renderbuffer *strb,
2463 struct st_texture_image *stImage,
2464 GLenum baseFormat,
2465 GLint destX, GLint destY, GLint slice,
2466 GLint srcX, GLint srcY,
2467 GLsizei width, GLsizei height)
2468 {
2469 struct st_context *st = st_context(ctx);
2470 struct pipe_context *pipe = st->pipe;
2471 struct pipe_transfer *src_trans;
2472 GLubyte *texDest;
2473 enum pipe_transfer_usage transfer_usage;
2474 void *map;
2475 unsigned dst_width = width;
2476 unsigned dst_height = height;
2477 unsigned dst_depth = 1;
2478 struct pipe_transfer *transfer;
2479
2480 if (ST_DEBUG & DEBUG_FALLBACK)
2481 debug_printf("%s: fallback processing\n", __func__);
2482
2483 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2484 srcY = strb->Base.Height - srcY - height;
2485 }
2486
2487 map = pipe_transfer_map(pipe,
2488 strb->texture,
2489 strb->surface->u.tex.level,
2490 strb->surface->u.tex.first_layer,
2491 PIPE_TRANSFER_READ,
2492 srcX, srcY,
2493 width, height, &src_trans);
2494
2495 if ((baseFormat == GL_DEPTH_COMPONENT ||
2496 baseFormat == GL_DEPTH_STENCIL) &&
2497 util_format_is_depth_and_stencil(stImage->pt->format))
2498 transfer_usage = PIPE_TRANSFER_READ_WRITE;
2499 else
2500 transfer_usage = PIPE_TRANSFER_WRITE;
2501
2502 texDest = st_texture_image_map(st, stImage, transfer_usage,
2503 destX, destY, slice,
2504 dst_width, dst_height, dst_depth,
2505 &transfer);
2506
2507 if (baseFormat == GL_DEPTH_COMPONENT ||
2508 baseFormat == GL_DEPTH_STENCIL) {
2509 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
2510 ctx->Pixel.DepthBias != 0.0F);
2511 GLint row, yStep;
2512 uint *data;
2513
2514 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2515 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2516 srcY = height - 1;
2517 yStep = -1;
2518 }
2519 else {
2520 srcY = 0;
2521 yStep = 1;
2522 }
2523
2524 data = malloc(width * sizeof(uint));
2525
2526 if (data) {
2527 /* To avoid a large temp memory allocation, do copy row by row */
2528 for (row = 0; row < height; row++, srcY += yStep) {
2529 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
2530 if (scaleOrBias) {
2531 _mesa_scale_and_bias_depth_uint(ctx, width, data);
2532 }
2533
2534 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2535 pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
2536 0, 0, width, 1, data);
2537 }
2538 else {
2539 pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
2540 }
2541 }
2542 }
2543 else {
2544 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
2545 }
2546
2547 free(data);
2548 }
2549 else {
2550 /* RGBA format */
2551 GLfloat *tempSrc =
2552 malloc(width * height * 4 * sizeof(GLfloat));
2553
2554 if (tempSrc && texDest) {
2555 const GLint dims = 2;
2556 GLint dstRowStride;
2557 struct gl_texture_image *texImage = &stImage->base;
2558 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
2559
2560 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2561 unpack.Invert = GL_TRUE;
2562 }
2563
2564 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2565 dstRowStride = transfer->layer_stride;
2566 }
2567 else {
2568 dstRowStride = transfer->stride;
2569 }
2570
2571 /* get float/RGBA image from framebuffer */
2572 /* XXX this usually involves a lot of int/float conversion.
2573 * try to avoid that someday.
2574 */
2575 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
2576 util_format_linear(strb->texture->format),
2577 tempSrc);
2578
2579 /* Store into texture memory.
2580 * Note that this does some special things such as pixel transfer
2581 * ops and format conversion. In particular, if the dest tex format
2582 * is actually RGBA but the user created the texture as GL_RGB we
2583 * need to fill-in/override the alpha channel with 1.0.
2584 */
2585 _mesa_texstore(ctx, dims,
2586 texImage->_BaseFormat,
2587 texImage->TexFormat,
2588 dstRowStride,
2589 &texDest,
2590 width, height, 1,
2591 GL_RGBA, GL_FLOAT, tempSrc, /* src */
2592 &unpack);
2593 }
2594 else {
2595 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
2596 }
2597
2598 free(tempSrc);
2599 }
2600
2601 st_texture_image_unmap(st, stImage, slice);
2602 pipe->transfer_unmap(pipe, src_trans);
2603 }
2604
2605
2606 /**
2607 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2608 * Note that the region to copy has already been clipped so we know we
2609 * won't read from outside the source renderbuffer's bounds.
2610 *
2611 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2612 */
2613 static void
2614 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2615 struct gl_texture_image *texImage,
2616 GLint destX, GLint destY, GLint slice,
2617 struct gl_renderbuffer *rb,
2618 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
2619 {
2620 struct st_texture_image *stImage = st_texture_image(texImage);
2621 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
2622 struct st_renderbuffer *strb = st_renderbuffer(rb);
2623 struct st_context *st = st_context(ctx);
2624 struct pipe_context *pipe = st->pipe;
2625 struct pipe_screen *screen = pipe->screen;
2626 struct pipe_blit_info blit;
2627 enum pipe_format dst_format;
2628 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
2629 unsigned bind;
2630 GLint srcY0, srcY1;
2631
2632 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
2633 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
2634
2635 if (!strb || !strb->surface || !stImage->pt) {
2636 debug_printf("%s: null strb or stImage\n", __func__);
2637 return;
2638 }
2639
2640 if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
2641 texImage->TexFormat)) {
2642 goto fallback;
2643 }
2644
2645 /* The base internal format must match the mesa format, so make sure
2646 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2647 */
2648 if (texImage->_BaseFormat !=
2649 _mesa_get_format_base_format(texImage->TexFormat) ||
2650 rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
2651 goto fallback;
2652 }
2653
2654 /* Choose the destination format to match the TexImage behavior. */
2655 dst_format = util_format_linear(stImage->pt->format);
2656 dst_format = util_format_luminance_to_red(dst_format);
2657 dst_format = util_format_intensity_to_red(dst_format);
2658
2659 /* See if the destination format is supported. */
2660 if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
2661 texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
2662 bind = PIPE_BIND_DEPTH_STENCIL;
2663 }
2664 else {
2665 bind = PIPE_BIND_RENDER_TARGET;
2666 }
2667
2668 if (!dst_format ||
2669 !screen->is_format_supported(screen, dst_format, stImage->pt->target,
2670 stImage->pt->nr_samples, bind)) {
2671 goto fallback;
2672 }
2673
2674 /* Y flipping for the main framebuffer. */
2675 if (do_flip) {
2676 srcY1 = strb->Base.Height - srcY - height;
2677 srcY0 = srcY1 + height;
2678 }
2679 else {
2680 srcY0 = srcY;
2681 srcY1 = srcY0 + height;
2682 }
2683
2684 /* Blit the texture.
2685 * This supports flipping, format conversions, and downsampling.
2686 */
2687 memset(&blit, 0, sizeof(blit));
2688 blit.src.resource = strb->texture;
2689 blit.src.format = util_format_linear(strb->surface->format);
2690 blit.src.level = strb->surface->u.tex.level;
2691 blit.src.box.x = srcX;
2692 blit.src.box.y = srcY0;
2693 blit.src.box.z = strb->surface->u.tex.first_layer;
2694 blit.src.box.width = width;
2695 blit.src.box.height = srcY1 - srcY0;
2696 blit.src.box.depth = 1;
2697 blit.dst.resource = stImage->pt;
2698 blit.dst.format = dst_format;
2699 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level + texImage->TexObject->MinLevel;
2700 blit.dst.box.x = destX;
2701 blit.dst.box.y = destY;
2702 blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
2703 blit.dst.box.width = width;
2704 blit.dst.box.height = height;
2705 blit.dst.box.depth = 1;
2706 blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
2707 blit.filter = PIPE_TEX_FILTER_NEAREST;
2708 pipe->blit(pipe, &blit);
2709 return;
2710
2711 fallback:
2712 /* software fallback */
2713 fallback_copy_texsubimage(ctx,
2714 strb, stImage, texImage->_BaseFormat,
2715 destX, destY, slice,
2716 srcX, srcY, width, height);
2717 }
2718
2719
2720 /**
2721 * Copy image data from stImage into the texture object 'stObj' at level
2722 * 'dstLevel'.
2723 */
2724 static void
2725 copy_image_data_to_texture(struct st_context *st,
2726 struct st_texture_object *stObj,
2727 GLuint dstLevel,
2728 struct st_texture_image *stImage)
2729 {
2730 /* debug checks */
2731 {
2732 const struct gl_texture_image *dstImage =
2733 stObj->base.Image[stImage->base.Face][dstLevel];
2734 assert(dstImage);
2735 assert(dstImage->Width == stImage->base.Width);
2736 assert(dstImage->Height == stImage->base.Height);
2737 assert(dstImage->Depth == stImage->base.Depth);
2738 }
2739
2740 if (stImage->pt) {
2741 /* Copy potentially with the blitter:
2742 */
2743 GLuint src_level;
2744 if (stImage->pt->last_level == 0)
2745 src_level = 0;
2746 else
2747 src_level = stImage->base.Level;
2748
2749 assert(src_level <= stImage->pt->last_level);
2750 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
2751 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
2752 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
2753 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
2754 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
2755 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
2756
2757 st_texture_image_copy(st->pipe,
2758 stObj->pt, dstLevel, /* dest texture, level */
2759 stImage->pt, src_level, /* src texture, level */
2760 stImage->base.Face);
2761
2762 pipe_resource_reference(&stImage->pt, NULL);
2763 }
2764 pipe_resource_reference(&stImage->pt, stObj->pt);
2765 }
2766
2767
2768 /**
2769 * Called during state validation. When this function is finished,
2770 * the texture object should be ready for rendering.
2771 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2772 */
2773 GLboolean
2774 st_finalize_texture(struct gl_context *ctx,
2775 struct pipe_context *pipe,
2776 struct gl_texture_object *tObj)
2777 {
2778 struct st_context *st = st_context(ctx);
2779 struct st_texture_object *stObj = st_texture_object(tObj);
2780 const GLuint nr_faces = _mesa_num_tex_faces(stObj->base.Target);
2781 GLuint face;
2782 const struct st_texture_image *firstImage;
2783 enum pipe_format firstImageFormat;
2784 GLuint ptWidth, ptHeight, ptDepth, ptLayers, ptNumSamples;
2785
2786 if (tObj->Immutable)
2787 return GL_TRUE;
2788
2789 if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
2790 /* The texture is complete and we know exactly how many mipmap levels
2791 * are present/needed. This is conditional because we may be called
2792 * from the st_generate_mipmap() function when the texture object is
2793 * incomplete. In that case, we'll have set stObj->lastLevel before
2794 * we get here.
2795 */
2796 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
2797 stObj->base.Sampler.MinFilter == GL_NEAREST)
2798 stObj->lastLevel = stObj->base.BaseLevel;
2799 else
2800 stObj->lastLevel = stObj->base._MaxLevel;
2801 }
2802
2803 if (tObj->Target == GL_TEXTURE_BUFFER) {
2804 struct st_buffer_object *st_obj = st_buffer_object(tObj->BufferObject);
2805
2806 if (!st_obj) {
2807 pipe_resource_reference(&stObj->pt, NULL);
2808 st_texture_release_all_sampler_views(st, stObj);
2809 return GL_TRUE;
2810 }
2811
2812 if (st_obj->buffer != stObj->pt) {
2813 pipe_resource_reference(&stObj->pt, st_obj->buffer);
2814 st_texture_release_all_sampler_views(st, stObj);
2815 stObj->width0 = stObj->pt->width0 / _mesa_get_format_bytes(tObj->_BufferObjectFormat);
2816 stObj->height0 = 1;
2817 stObj->depth0 = 1;
2818 }
2819 return GL_TRUE;
2820
2821 }
2822
2823 firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
2824 assert(firstImage);
2825
2826 /* If both firstImage and stObj point to a texture which can contain
2827 * all active images, favour firstImage. Note that because of the
2828 * completeness requirement, we know that the image dimensions
2829 * will match.
2830 */
2831 if (firstImage->pt &&
2832 firstImage->pt != stObj->pt &&
2833 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
2834 pipe_resource_reference(&stObj->pt, firstImage->pt);
2835 st_texture_release_all_sampler_views(st, stObj);
2836 }
2837
2838 /* If this texture comes from a window system, there is nothing else to do. */
2839 if (stObj->surface_based) {
2840 return GL_TRUE;
2841 }
2842
2843 /* Find gallium format for the Mesa texture */
2844 firstImageFormat =
2845 st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
2846
2847 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2848 {
2849 GLuint width, height, depth;
2850 if (!guess_base_level_size(stObj->base.Target,
2851 firstImage->base.Width2,
2852 firstImage->base.Height2,
2853 firstImage->base.Depth2,
2854 firstImage->base.Level,
2855 &width, &height, &depth)) {
2856 width = stObj->width0;
2857 height = stObj->height0;
2858 depth = stObj->depth0;
2859 } else {
2860 /* The width/height/depth may have been previously reset in
2861 * guess_and_alloc_texture. */
2862 stObj->width0 = width;
2863 stObj->height0 = height;
2864 stObj->depth0 = depth;
2865 }
2866 /* convert GL dims to Gallium dims */
2867 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
2868 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
2869 ptNumSamples = firstImage->base.NumSamples;
2870 }
2871
2872 /* If we already have a gallium texture, check that it matches the texture
2873 * object's format, target, size, num_levels, etc.
2874 */
2875 if (stObj->pt) {
2876 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
2877 stObj->pt->format != firstImageFormat ||
2878 stObj->pt->last_level < stObj->lastLevel ||
2879 stObj->pt->width0 != ptWidth ||
2880 stObj->pt->height0 != ptHeight ||
2881 stObj->pt->depth0 != ptDepth ||
2882 stObj->pt->nr_samples != ptNumSamples ||
2883 stObj->pt->array_size != ptLayers)
2884 {
2885 /* The gallium texture does not match the Mesa texture so delete the
2886 * gallium texture now. We'll make a new one below.
2887 */
2888 pipe_resource_reference(&stObj->pt, NULL);
2889 st_texture_release_all_sampler_views(st, stObj);
2890 st->dirty.st |= ST_NEW_FRAMEBUFFER;
2891 }
2892 }
2893
2894 /* May need to create a new gallium texture:
2895 */
2896 if (!stObj->pt) {
2897 GLuint bindings = default_bindings(st, firstImageFormat);
2898
2899 stObj->pt = st_texture_create(st,
2900 gl_target_to_pipe(stObj->base.Target),
2901 firstImageFormat,
2902 stObj->lastLevel,
2903 ptWidth,
2904 ptHeight,
2905 ptDepth,
2906 ptLayers, ptNumSamples,
2907 bindings);
2908
2909 if (!stObj->pt) {
2910 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
2911 return GL_FALSE;
2912 }
2913 }
2914
2915 /* Pull in any images not in the object's texture:
2916 */
2917 for (face = 0; face < nr_faces; face++) {
2918 GLuint level;
2919 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
2920 struct st_texture_image *stImage =
2921 st_texture_image(stObj->base.Image[face][level]);
2922
2923 /* Need to import images in main memory or held in other textures.
2924 */
2925 if (stImage && stObj->pt != stImage->pt) {
2926 GLuint height = stObj->height0;
2927 GLuint depth = stObj->depth0;
2928
2929 if (stObj->base.Target != GL_TEXTURE_1D_ARRAY)
2930 height = u_minify(height, level);
2931 if (stObj->base.Target == GL_TEXTURE_3D)
2932 depth = u_minify(depth, level);
2933
2934 if (level == 0 ||
2935 (stImage->base.Width == u_minify(stObj->width0, level) &&
2936 stImage->base.Height == height &&
2937 stImage->base.Depth == depth)) {
2938 /* src image fits expected dest mipmap level size */
2939 copy_image_data_to_texture(st, stObj, level, stImage);
2940 }
2941 }
2942 }
2943 }
2944
2945 return GL_TRUE;
2946 }
2947
2948
2949 /**
2950 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2951 * for a whole mipmap stack.
2952 */
2953 static GLboolean
2954 st_AllocTextureStorage(struct gl_context *ctx,
2955 struct gl_texture_object *texObj,
2956 GLsizei levels, GLsizei width,
2957 GLsizei height, GLsizei depth)
2958 {
2959 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
2960 struct gl_texture_image *texImage = texObj->Image[0][0];
2961 struct st_context *st = st_context(ctx);
2962 struct st_texture_object *stObj = st_texture_object(texObj);
2963 struct pipe_screen *screen = st->pipe->screen;
2964 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
2965 enum pipe_format fmt;
2966 GLint level;
2967 GLuint num_samples = texImage->NumSamples;
2968
2969 assert(levels > 0);
2970
2971 /* Save the level=0 dimensions */
2972 stObj->width0 = width;
2973 stObj->height0 = height;
2974 stObj->depth0 = depth;
2975 stObj->lastLevel = levels - 1;
2976
2977 fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
2978
2979 bindings = default_bindings(st, fmt);
2980
2981 /* Raise the sample count if the requested one is unsupported. */
2982 if (num_samples > 1) {
2983 boolean found = FALSE;
2984
2985 for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
2986 if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
2987 num_samples,
2988 PIPE_BIND_SAMPLER_VIEW)) {
2989 /* Update the sample count in gl_texture_image as well. */
2990 texImage->NumSamples = num_samples;
2991 found = TRUE;
2992 break;
2993 }
2994 }
2995
2996 if (!found) {
2997 return GL_FALSE;
2998 }
2999 }
3000
3001 st_gl_texture_dims_to_pipe_dims(texObj->Target,
3002 width, height, depth,
3003 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
3004
3005 stObj->pt = st_texture_create(st,
3006 gl_target_to_pipe(texObj->Target),
3007 fmt,
3008 levels - 1,
3009 ptWidth,
3010 ptHeight,
3011 ptDepth,
3012 ptLayers, num_samples,
3013 bindings);
3014 if (!stObj->pt)
3015 return GL_FALSE;
3016
3017 /* Set image resource pointers */
3018 for (level = 0; level < levels; level++) {
3019 GLuint face;
3020 for (face = 0; face < numFaces; face++) {
3021 struct st_texture_image *stImage =
3022 st_texture_image(texObj->Image[face][level]);
3023 pipe_resource_reference(&stImage->pt, stObj->pt);
3024 }
3025 }
3026
3027 return GL_TRUE;
3028 }
3029
3030
3031 static GLboolean
3032 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
3033 GLint level, mesa_format format,
3034 GLint width, GLint height,
3035 GLint depth, GLint border)
3036 {
3037 struct st_context *st = st_context(ctx);
3038 struct pipe_context *pipe = st->pipe;
3039
3040 if (width == 0 || height == 0 || depth == 0) {
3041 /* zero-sized images are legal, and always fit! */
3042 return GL_TRUE;
3043 }
3044
3045 if (pipe->screen->can_create_resource) {
3046 /* Ask the gallium driver if the texture is too large */
3047 struct gl_texture_object *texObj =
3048 _mesa_get_current_tex_object(ctx, target);
3049 struct pipe_resource pt;
3050
3051 /* Setup the pipe_resource object
3052 */
3053 memset(&pt, 0, sizeof(pt));
3054
3055 pt.target = gl_target_to_pipe(target);
3056 pt.format = st_mesa_format_to_pipe_format(st, format);
3057
3058 st_gl_texture_dims_to_pipe_dims(target,
3059 width, height, depth,
3060 &pt.width0, &pt.height0,
3061 &pt.depth0, &pt.array_size);
3062
3063 if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
3064 texObj->Sampler.MinFilter == GL_NEAREST)) {
3065 /* assume just one mipmap level */
3066 pt.last_level = 0;
3067 }
3068 else {
3069 /* assume a full set of mipmaps */
3070 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
3071 }
3072
3073 return pipe->screen->can_create_resource(pipe->screen, &pt);
3074 }
3075 else {
3076 /* Use core Mesa fallback */
3077 return _mesa_test_proxy_teximage(ctx, target, level, format,
3078 width, height, depth, border);
3079 }
3080 }
3081
3082 static GLboolean
3083 st_TextureView(struct gl_context *ctx,
3084 struct gl_texture_object *texObj,
3085 struct gl_texture_object *origTexObj)
3086 {
3087 struct st_texture_object *orig = st_texture_object(origTexObj);
3088 struct st_texture_object *tex = st_texture_object(texObj);
3089 struct gl_texture_image *image = texObj->Image[0][0];
3090
3091 const int numFaces = _mesa_num_tex_faces(texObj->Target);
3092 const int numLevels = texObj->NumLevels;
3093
3094 int face;
3095 int level;
3096
3097 pipe_resource_reference(&tex->pt, orig->pt);
3098
3099 /* Set image resource pointers */
3100 for (level = 0; level < numLevels; level++) {
3101 for (face = 0; face < numFaces; face++) {
3102 struct st_texture_image *stImage =
3103 st_texture_image(texObj->Image[face][level]);
3104 pipe_resource_reference(&stImage->pt, tex->pt);
3105 }
3106 }
3107
3108 tex->surface_based = GL_TRUE;
3109 tex->surface_format =
3110 st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
3111
3112 tex->width0 = image->Width;
3113 tex->height0 = image->Height;
3114 tex->depth0 = image->Depth;
3115 tex->lastLevel = numLevels - 1;
3116
3117 return GL_TRUE;
3118 }
3119
3120 static void
3121 st_ClearTexSubImage(struct gl_context *ctx,
3122 struct gl_texture_image *texImage,
3123 GLint xoffset, GLint yoffset, GLint zoffset,
3124 GLsizei width, GLsizei height, GLsizei depth,
3125 const void *clearValue)
3126 {
3127 static const char zeros[16] = {0};
3128 struct st_texture_image *stImage = st_texture_image(texImage);
3129 struct pipe_resource *pt = stImage->pt;
3130 struct st_context *st = st_context(ctx);
3131 struct pipe_context *pipe = st->pipe;
3132 unsigned level = texImage->Level;
3133 struct pipe_box box;
3134
3135 if (!pt)
3136 return;
3137
3138 u_box_3d(xoffset, yoffset, zoffset + texImage->Face,
3139 width, height, depth, &box);
3140 if (texImage->TexObject->Immutable) {
3141 level += texImage->TexObject->MinLevel;
3142 box.z += texImage->TexObject->MinLayer;
3143 }
3144
3145 pipe->clear_texture(pipe, pt, level, &box, clearValue ? clearValue : zeros);
3146 }
3147
3148 void
3149 st_init_texture_functions(struct dd_function_table *functions)
3150 {
3151 functions->ChooseTextureFormat = st_ChooseTextureFormat;
3152 functions->QueryInternalFormat = st_QueryInternalFormat;
3153 functions->TexImage = st_TexImage;
3154 functions->TexSubImage = st_TexSubImage;
3155 functions->CompressedTexSubImage = st_CompressedTexSubImage;
3156 functions->CopyTexSubImage = st_CopyTexSubImage;
3157 functions->GenerateMipmap = st_generate_mipmap;
3158
3159 functions->GetTexSubImage = st_GetTexSubImage;
3160
3161 /* compressed texture functions */
3162 functions->CompressedTexImage = st_CompressedTexImage;
3163 functions->GetCompressedTexSubImage = _mesa_GetCompressedTexSubImage_sw;
3164
3165 functions->NewTextureObject = st_NewTextureObject;
3166 functions->NewTextureImage = st_NewTextureImage;
3167 functions->DeleteTextureImage = st_DeleteTextureImage;
3168 functions->DeleteTexture = st_DeleteTextureObject;
3169 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
3170 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
3171 functions->MapTextureImage = st_MapTextureImage;
3172 functions->UnmapTextureImage = st_UnmapTextureImage;
3173
3174 /* XXX Temporary until we can query pipe's texture sizes */
3175 functions->TestProxyTexImage = st_TestProxyTexImage;
3176
3177 functions->AllocTextureStorage = st_AllocTextureStorage;
3178 functions->TextureView = st_TextureView;
3179 functions->ClearTexSubImage = st_ClearTexSubImage;
3180 }