1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
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"
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"
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"
75 #define DBG if (0) printf
78 enum pipe_texture_target
79 gl_target_to_pipe(GLenum target
)
83 case GL_PROXY_TEXTURE_1D
:
84 return PIPE_TEXTURE_1D
;
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
;
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
:
116 case GL_TEXTURE_CUBE_MAP_ARRAY
:
117 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
118 return PIPE_TEXTURE_CUBE_ARRAY
;
126 /** called via ctx->Driver.NewTextureImage() */
127 static struct gl_texture_image
*
128 st_NewTextureImage(struct gl_context
* ctx
)
130 DBG("%s\n", __func__
);
132 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
136 /** called via ctx->Driver.DeleteTextureImage() */
138 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
140 /* nothing special (yet) for st_texture_image */
141 _mesa_delete_texture_image(ctx
, img
);
145 /** called via ctx->Driver.NewTextureObject() */
146 static struct gl_texture_object
*
147 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
149 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
151 DBG("%s\n", __func__
);
152 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
157 /** called via ctx->Driver.DeleteTextureObject() */
159 st_DeleteTextureObject(struct gl_context
*ctx
,
160 struct gl_texture_object
*texObj
)
162 struct st_context
*st
= st_context(ctx
);
163 struct st_texture_object
*stObj
= st_texture_object(texObj
);
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
);
172 /** called via ctx->Driver.FreeTextureImageBuffer() */
174 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
175 struct gl_texture_image
*texImage
)
177 struct st_texture_image
*stImage
= st_texture_image(texImage
);
179 DBG("%s\n", __func__
);
182 pipe_resource_reference(&stImage
->pt
, NULL
);
185 free(stImage
->transfer
);
186 stImage
->transfer
= NULL
;
187 stImage
->num_transfers
= 0;
191 /** called via ctx->Driver.MapTextureImage() */
193 st_MapTextureImage(struct gl_context
*ctx
,
194 struct gl_texture_image
*texImage
,
195 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
197 GLubyte
**mapOut
, GLint
*rowStrideOut
)
199 struct st_context
*st
= st_context(ctx
);
200 struct st_texture_image
*stImage
= st_texture_image(texImage
);
203 struct pipe_transfer
*transfer
;
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
;
213 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
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.
222 * Just create a temporary storage where the ETC texture will
223 * be stored. It will be decompressed in the Unmap function.
225 unsigned z
= transfer
->box
.z
;
226 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
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
;
234 *mapOut
= itransfer
->temp_data
;
235 *rowStrideOut
= itransfer
->temp_stride
;
238 /* supported mapping */
240 *rowStrideOut
= transfer
->stride
;
250 /** called via ctx->Driver.UnmapTextureImage() */
252 st_UnmapTextureImage(struct gl_context
*ctx
,
253 struct gl_texture_image
*texImage
,
256 struct st_context
*st
= st_context(ctx
);
257 struct st_texture_image
*stImage
= st_texture_image(texImage
);
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
;
266 assert(z
== transfer
->box
.z
);
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
);
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
);
281 free(itransfer
->temp_data
);
282 itransfer
->temp_data
= NULL
;
283 itransfer
->temp_stride
= 0;
287 st_texture_image_unmap(st
, stImage
, slice
);
292 * Return default texture resource binding bitmask for the given format.
295 default_bindings(struct st_context
*st
, enum pipe_format format
)
297 struct pipe_screen
*screen
= st
->pipe
->screen
;
298 const unsigned target
= PIPE_TEXTURE_2D
;
301 if (util_format_is_depth_or_stencil(format
))
302 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
304 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
306 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
310 format
= util_format_linear(format
);
312 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
315 return PIPE_BIND_SAMPLER_VIEW
;
321 * Given the size of a mipmap image, try to compute the size of the level=0
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.
327 * \return GL_TRUE for success, GL_FALSE for failure
330 guess_base_level_size(GLenum target
,
331 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
332 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
339 /* Guess the size of the base level.
340 * Depending on the image's size, we can't always make a guess here.
344 case GL_TEXTURE_1D_ARRAY
:
349 case GL_TEXTURE_2D_ARRAY
:
350 /* We can't make a good guess here, because the base level dimensions
353 if (width
== 1 || height
== 1) {
360 case GL_TEXTURE_CUBE_MAP
:
361 case GL_TEXTURE_CUBE_MAP_ARRAY
:
367 /* We can't make a good guess here, because the base level dimensions
370 if (width
== 1 || height
== 1 || depth
== 1) {
378 case GL_TEXTURE_RECTANGLE
:
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
401 * If we guess incorrectly here we'll later reallocate the right amount of
402 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
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.
408 allocate_full_mipmap(const struct st_texture_object
*stObj
,
409 const struct st_texture_image
*stImage
)
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 */
421 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
424 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
425 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
426 /* depth/stencil textures are seldom mipmapped */
429 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
432 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
433 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
434 /* not a mipmap minification filter */
437 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
438 /* 3D textures are seldom mipmapped */
446 * Try to allocate a pipe_resource object for the given st_texture_object.
448 * We use the given st_texture_image as a clue to determine the size of the
449 * mipmap image at level=0.
451 * \return GL_TRUE for success, GL_FALSE if out of memory.
454 guess_and_alloc_texture(struct st_context
*st
,
455 struct st_texture_object
*stObj
,
456 const struct st_texture_image
*stImage
)
458 GLuint lastLevel
, width
, height
, depth
;
460 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
461 enum pipe_format fmt
;
463 DBG("%s\n", __func__
);
467 if (!guess_base_level_size(stObj
->base
.Target
,
468 stImage
->base
.Width2
,
469 stImage
->base
.Height2
,
470 stImage
->base
.Depth2
,
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 */
479 /* At this point, (width x height x depth) is the expected size of
480 * the level=0 mipmap image.
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.
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;
495 /* only alloc space for a single mipmap level */
499 /* Save the level=0 dimensions */
500 stObj
->width0
= width
;
501 stObj
->height0
= height
;
502 stObj
->depth0
= depth
;
504 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
506 bindings
= default_bindings(st
, fmt
);
508 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
509 width
, height
, depth
,
510 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
512 stObj
->pt
= st_texture_create(st
,
513 gl_target_to_pipe(stObj
->base
.Target
),
522 stObj
->lastLevel
= lastLevel
;
524 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
526 return stObj
->pt
!= NULL
;
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.
536 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
537 struct gl_texture_image
*texImage
)
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
;
547 DBG("%s\n", __func__
);
549 assert(!stImage
->pt
); /* xxx this might be wrong */
551 /* Look if the parent texture object has space for this image */
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
);
560 /* The parent texture object does not have space for this image */
562 pipe_resource_reference(&stObj
->pt
, NULL
);
563 st_texture_release_all_sampler_views(st
, stObj
);
565 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
566 /* Probably out of memory.
567 * Try flushing any pending rendering, then retry.
570 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
571 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
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
);
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
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
;
595 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
596 width
, height
, depth
,
597 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
599 stImage
->pt
= st_texture_create(st
,
600 gl_target_to_pipe(stObj
->base
.Target
),
608 return stImage
->pt
!= NULL
;
614 * Preparation prior to glTexImage. Basically check the 'surface_based'
615 * field and switch to a "normal" tex image if necessary.
618 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
619 GLenum format
, GLenum type
)
621 struct gl_texture_object
*texObj
= texImage
->TexObject
;
622 struct st_texture_object
*stObj
= st_texture_object(texObj
);
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
;
630 _mesa_clear_texture_object(ctx
, texObj
);
631 pipe_resource_reference(&stObj
->pt
, NULL
);
633 /* oops, need to init this image again */
634 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
635 texImage
->InternalFormat
, format
,
638 _mesa_init_teximage_fields(ctx
, texImage
,
639 texImage
->Width
, texImage
->Height
,
640 texImage
->Depth
, texImage
->Border
,
641 texImage
->InternalFormat
, texFormat
);
643 stObj
->surface_based
= GL_FALSE
;
649 * Return a writemask for the gallium blit. The parameters can be base
650 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
653 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
656 case GL_DEPTH_STENCIL
:
658 case GL_DEPTH_STENCIL
:
660 case GL_DEPTH_COMPONENT
:
662 case GL_STENCIL_INDEX
:
669 case GL_DEPTH_COMPONENT
:
671 case GL_DEPTH_STENCIL
:
672 case GL_DEPTH_COMPONENT
:
679 case GL_STENCIL_INDEX
:
681 case GL_STENCIL_INDEX
:
689 return PIPE_MASK_RGBA
;
694 * Converts format to a format with the same components, types
695 * and sizes, but with the components in RGBA order.
697 static enum pipe_format
698 unswizzle_format(enum pipe_format format
)
702 case PIPE_FORMAT_B8G8R8A8_UNORM
:
703 case PIPE_FORMAT_A8R8G8B8_UNORM
:
704 case PIPE_FORMAT_A8B8G8R8_UNORM
:
705 return PIPE_FORMAT_R8G8B8A8_UNORM
;
707 case PIPE_FORMAT_B10G10R10A2_UNORM
:
708 return PIPE_FORMAT_R10G10B10A2_UNORM
;
710 case PIPE_FORMAT_B10G10R10A2_SNORM
:
711 return PIPE_FORMAT_R10G10B10A2_SNORM
;
713 case PIPE_FORMAT_B10G10R10A2_UINT
:
714 return PIPE_FORMAT_R10G10B10A2_UINT
;
722 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
724 static enum pipe_format
725 alpha_to_red(enum pipe_format format
)
729 case PIPE_FORMAT_A8_UNORM
:
730 return PIPE_FORMAT_R8_UNORM
;
731 case PIPE_FORMAT_A8_SNORM
:
732 return PIPE_FORMAT_R8_SNORM
;
733 case PIPE_FORMAT_A8_UINT
:
734 return PIPE_FORMAT_R8_UINT
;
735 case PIPE_FORMAT_A8_SINT
:
736 return PIPE_FORMAT_R8_SINT
;
738 case PIPE_FORMAT_A16_UNORM
:
739 return PIPE_FORMAT_R16_UNORM
;
740 case PIPE_FORMAT_A16_SNORM
:
741 return PIPE_FORMAT_R16_SNORM
;
742 case PIPE_FORMAT_A16_UINT
:
743 return PIPE_FORMAT_R16_UINT
;
744 case PIPE_FORMAT_A16_SINT
:
745 return PIPE_FORMAT_R16_SINT
;
746 case PIPE_FORMAT_A16_FLOAT
:
747 return PIPE_FORMAT_R16_FLOAT
;
749 case PIPE_FORMAT_A32_UINT
:
750 return PIPE_FORMAT_R32_UINT
;
751 case PIPE_FORMAT_A32_SINT
:
752 return PIPE_FORMAT_R32_SINT
;
753 case PIPE_FORMAT_A32_FLOAT
:
754 return PIPE_FORMAT_R32_FLOAT
;
762 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
764 static enum pipe_format
765 red_alpha_to_red_green(enum pipe_format format
)
769 case PIPE_FORMAT_R8A8_UNORM
:
770 return PIPE_FORMAT_R8G8_UNORM
;
771 case PIPE_FORMAT_R8A8_SNORM
:
772 return PIPE_FORMAT_R8G8_SNORM
;
773 case PIPE_FORMAT_R8A8_UINT
:
774 return PIPE_FORMAT_R8G8_UINT
;
775 case PIPE_FORMAT_R8A8_SINT
:
776 return PIPE_FORMAT_R8G8_SINT
;
778 case PIPE_FORMAT_R16A16_UNORM
:
779 return PIPE_FORMAT_R16G16_UNORM
;
780 case PIPE_FORMAT_R16A16_SNORM
:
781 return PIPE_FORMAT_R16G16_SNORM
;
782 case PIPE_FORMAT_R16A16_UINT
:
783 return PIPE_FORMAT_R16G16_UINT
;
784 case PIPE_FORMAT_R16A16_SINT
:
785 return PIPE_FORMAT_R16G16_SINT
;
786 case PIPE_FORMAT_R16A16_FLOAT
:
787 return PIPE_FORMAT_R16G16_FLOAT
;
789 case PIPE_FORMAT_R32A32_UINT
:
790 return PIPE_FORMAT_R32G32_UINT
;
791 case PIPE_FORMAT_R32A32_SINT
:
792 return PIPE_FORMAT_R32G32_SINT
;
793 case PIPE_FORMAT_R32A32_FLOAT
:
794 return PIPE_FORMAT_R32G32_FLOAT
;
802 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
804 static enum pipe_format
805 luminance_alpha_to_red_green(enum pipe_format format
)
809 case PIPE_FORMAT_L8A8_UNORM
:
810 return PIPE_FORMAT_R8G8_UNORM
;
811 case PIPE_FORMAT_L8A8_SNORM
:
812 return PIPE_FORMAT_R8G8_SNORM
;
813 case PIPE_FORMAT_L8A8_UINT
:
814 return PIPE_FORMAT_R8G8_UINT
;
815 case PIPE_FORMAT_L8A8_SINT
:
816 return PIPE_FORMAT_R8G8_SINT
;
818 case PIPE_FORMAT_L16A16_UNORM
:
819 return PIPE_FORMAT_R16G16_UNORM
;
820 case PIPE_FORMAT_L16A16_SNORM
:
821 return PIPE_FORMAT_R16G16_SNORM
;
822 case PIPE_FORMAT_L16A16_UINT
:
823 return PIPE_FORMAT_R16G16_UINT
;
824 case PIPE_FORMAT_L16A16_SINT
:
825 return PIPE_FORMAT_R16G16_SINT
;
826 case PIPE_FORMAT_L16A16_FLOAT
:
827 return PIPE_FORMAT_R16G16_FLOAT
;
829 case PIPE_FORMAT_L32A32_UINT
:
830 return PIPE_FORMAT_R32G32_UINT
;
831 case PIPE_FORMAT_L32A32_SINT
:
832 return PIPE_FORMAT_R32G32_SINT
;
833 case PIPE_FORMAT_L32A32_FLOAT
:
834 return PIPE_FORMAT_R32G32_FLOAT
;
842 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
845 format_is_alpha(enum pipe_format format
)
847 const struct util_format_description
*desc
= util_format_description(format
);
849 if (desc
->nr_channels
== 1 &&
850 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
851 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
852 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
853 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
860 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
863 format_is_red(enum pipe_format format
)
865 const struct util_format_description
*desc
= util_format_description(format
);
867 if (desc
->nr_channels
== 1 &&
868 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
869 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
870 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
871 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
879 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
882 format_is_luminance(enum pipe_format format
)
884 const struct util_format_description
*desc
= util_format_description(format
);
886 if (desc
->nr_channels
== 1 &&
887 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
888 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
889 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
890 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
897 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
900 format_is_red_alpha(enum pipe_format format
)
902 const struct util_format_description
*desc
= util_format_description(format
);
904 if (desc
->nr_channels
== 2 &&
905 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
906 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
907 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
908 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
915 format_is_swizzled_rgba(enum pipe_format format
)
917 const struct util_format_description
*desc
= util_format_description(format
);
919 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
920 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
921 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
922 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
930 unsigned char swizzle
[4];
931 enum pipe_format format
;
934 static const struct format_table table_8888_unorm
[] = {
935 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
936 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
937 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
938 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
941 static const struct format_table table_1010102_unorm
[] = {
942 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
943 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
946 static const struct format_table table_1010102_snorm
[] = {
947 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
948 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
951 static const struct format_table table_1010102_uint
[] = {
952 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
953 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
956 static enum pipe_format
957 swizzle_format(enum pipe_format format
, const int * const swizzle
)
962 case PIPE_FORMAT_R8G8B8A8_UNORM
:
963 case PIPE_FORMAT_B8G8R8A8_UNORM
:
964 case PIPE_FORMAT_A8R8G8B8_UNORM
:
965 case PIPE_FORMAT_A8B8G8R8_UNORM
:
966 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
967 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
968 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
969 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
970 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
971 return table_8888_unorm
[i
].format
;
975 case PIPE_FORMAT_R10G10B10A2_UNORM
:
976 case PIPE_FORMAT_B10G10R10A2_UNORM
:
977 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
978 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
979 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
980 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
981 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
982 return table_1010102_unorm
[i
].format
;
986 case PIPE_FORMAT_R10G10B10A2_SNORM
:
987 case PIPE_FORMAT_B10G10R10A2_SNORM
:
988 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
989 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
990 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
991 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
992 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
993 return table_1010102_snorm
[i
].format
;
997 case PIPE_FORMAT_R10G10B10A2_UINT
:
998 case PIPE_FORMAT_B10G10R10A2_UINT
:
999 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1000 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1001 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1002 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1003 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1004 return table_1010102_uint
[i
].format
;
1012 return PIPE_FORMAT_NONE
;
1016 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1018 enum pipe_format src
= *src_format
;
1019 enum pipe_format dst
= *dst_format
;
1021 /* Note: dst_format has already been transformed from luminance/intensity
1022 * to red when this function is called. The source format will never
1023 * be an intensity format, because GL_INTENSITY is not a legal value
1024 * for the format parameter in glTex(Sub)Image(). */
1026 if (format_is_alpha(src
)) {
1027 if (!format_is_alpha(dst
))
1030 src
= alpha_to_red(src
);
1031 dst
= alpha_to_red(dst
);
1032 } else if (format_is_luminance(src
)) {
1033 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1036 src
= util_format_luminance_to_red(src
);
1037 } else if (util_format_is_luminance_alpha(src
)) {
1038 src
= luminance_alpha_to_red_green(src
);
1040 if (format_is_red_alpha(dst
)) {
1041 dst
= red_alpha_to_red_green(dst
);
1042 } else if (!format_is_red(dst
))
1044 } else if (format_is_swizzled_rgba(src
)) {
1045 const struct util_format_description
*src_desc
= util_format_description(src
);
1046 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1050 /* Make sure the format is an RGBA and not an RGBX format */
1051 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1054 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1057 for (i
= 0; i
< 4; i
++)
1058 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1060 dst
= swizzle_format(dst
, swizzle
);
1061 if (dst
== PIPE_FORMAT_NONE
)
1064 src
= unswizzle_format(src
);
1073 create_pbo_upload_vs(struct st_context
*st
)
1075 struct ureg_program
*ureg
;
1076 struct ureg_src in_pos
;
1077 struct ureg_src in_instanceid
;
1078 struct ureg_dst out_pos
;
1079 struct ureg_dst out_layer
;
1081 ureg
= ureg_create(PIPE_SHADER_VERTEX
);
1085 in_pos
= ureg_DECL_vs_input(ureg
, TGSI_SEMANTIC_POSITION
);
1087 out_pos
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
1089 if (st
->pbo_upload
.upload_layers
) {
1090 in_instanceid
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_INSTANCEID
, 0);
1092 if (!st
->pbo_upload
.use_gs
)
1093 out_layer
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_LAYER
, 0);
1096 /* out_pos = in_pos */
1097 ureg_MOV(ureg
, out_pos
, in_pos
);
1099 if (st
->pbo_upload
.upload_layers
) {
1100 if (st
->pbo_upload
.use_gs
) {
1101 /* out_pos.z = i2f(gl_InstanceID) */
1102 ureg_I2F(ureg
, ureg_writemask(out_pos
, TGSI_WRITEMASK_Z
),
1103 ureg_scalar(in_instanceid
, TGSI_SWIZZLE_X
));
1105 /* out_layer = gl_InstanceID */
1106 ureg_MOV(ureg
, out_layer
, in_instanceid
);
1112 return ureg_create_shader_and_destroy(ureg
, st
->pipe
);
1116 create_pbo_upload_gs(struct st_context
*st
)
1118 static const int zero
= 0;
1119 struct ureg_program
*ureg
;
1120 struct ureg_dst out_pos
;
1121 struct ureg_dst out_layer
;
1122 struct ureg_src in_pos
;
1123 struct ureg_src imm
;
1126 ureg
= ureg_create(PIPE_SHADER_GEOMETRY
);
1130 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
, PIPE_PRIM_TRIANGLES
);
1131 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
, PIPE_PRIM_TRIANGLE_STRIP
);
1132 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
, 3);
1134 out_pos
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
1135 out_layer
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_LAYER
, 0);
1137 in_pos
= ureg_DECL_input(ureg
, TGSI_SEMANTIC_POSITION
, 0, 0, 1);
1139 imm
= ureg_DECL_immediate_int(ureg
, &zero
, 1);
1141 for (i
= 0; i
< 3; ++i
) {
1142 struct ureg_src in_pos_vertex
= ureg_src_dimension(in_pos
, i
);
1144 /* out_pos = in_pos[i] */
1145 ureg_MOV(ureg
, out_pos
, in_pos_vertex
);
1147 /* out_layer.x = f2i(in_pos[i].z) */
1148 ureg_F2I(ureg
, ureg_writemask(out_layer
, TGSI_WRITEMASK_X
),
1149 ureg_scalar(in_pos_vertex
, TGSI_SWIZZLE_Z
));
1151 ureg_EMIT(ureg
, ureg_scalar(imm
, TGSI_SWIZZLE_X
));
1156 return ureg_create_shader_and_destroy(ureg
, st
->pipe
);
1160 create_pbo_upload_fs(struct st_context
*st
)
1162 struct pipe_context
*pipe
= st
->pipe
;
1163 struct pipe_screen
*screen
= pipe
->screen
;
1164 struct ureg_program
*ureg
;
1165 struct ureg_dst out
;
1166 struct ureg_src sampler
;
1167 struct ureg_src pos
;
1168 struct ureg_src layer
;
1169 struct ureg_src const0
;
1170 struct ureg_dst temp0
;
1172 ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
1176 out
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
1177 sampler
= ureg_DECL_sampler(ureg
, 0);
1178 if (screen
->get_param(screen
, PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL
)) {
1179 pos
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_POSITION
, 0);
1181 pos
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_POSITION
, 0,
1182 TGSI_INTERPOLATE_LINEAR
);
1184 if (st
->pbo_upload
.upload_layers
) {
1185 layer
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_LAYER
, 0,
1186 TGSI_INTERPOLATE_CONSTANT
);
1188 const0
= ureg_DECL_constant(ureg
, 0);
1189 temp0
= ureg_DECL_temporary(ureg
);
1191 /* Note: const0 = [ -xoffset + skip_pixels, -yoffset, stride, image_height ] */
1193 /* temp0.xy = f2i(temp0.xy) */
1194 ureg_F2I(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_XY
),
1196 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_Y
,
1197 TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_Y
));
1199 /* temp0.xy = temp0.xy + const0.xy */
1200 ureg_UADD(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_XY
),
1201 ureg_swizzle(ureg_src(temp0
),
1202 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_Y
,
1203 TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_Y
),
1204 ureg_swizzle(const0
,
1205 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_Y
,
1206 TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_Y
));
1208 /* temp0.x = const0.z * temp0.y + temp0.x */
1209 ureg_UMAD(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_X
),
1210 ureg_scalar(const0
, TGSI_SWIZZLE_Z
),
1211 ureg_scalar(ureg_src(temp0
), TGSI_SWIZZLE_Y
),
1212 ureg_scalar(ureg_src(temp0
), TGSI_SWIZZLE_X
));
1214 if (st
->pbo_upload
.upload_layers
) {
1215 /* temp0.x = const0.w * layer + temp0.x */
1216 ureg_UMAD(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_X
),
1217 ureg_scalar(const0
, TGSI_SWIZZLE_W
),
1218 ureg_scalar(layer
, TGSI_SWIZZLE_X
),
1219 ureg_scalar(ureg_src(temp0
), TGSI_SWIZZLE_X
));
1223 ureg_MOV(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_W
), ureg_imm1u(ureg
, 0));
1225 /* out = txf(sampler, temp0.x) */
1226 ureg_TXF(ureg
, out
, TGSI_TEXTURE_BUFFER
, ureg_src(temp0
), sampler
);
1228 ureg_release_temporary(ureg
, temp0
);
1232 return ureg_create_shader_and_destroy(ureg
, pipe
);
1236 try_pbo_upload_common(struct gl_context
*ctx
,
1237 struct pipe_surface
*surface
,
1238 int xoffset
, int yoffset
,
1239 unsigned upload_width
, unsigned upload_height
,
1240 struct pipe_resource
*buffer
,
1241 enum pipe_format src_format
,
1242 intptr_t buf_offset
,
1243 unsigned bytes_per_pixel
,
1245 unsigned image_height
)
1247 struct st_context
*st
= st_context(ctx
);
1248 struct cso_context
*cso
= st
->cso_context
;
1249 struct pipe_context
*pipe
= st
->pipe
;
1250 unsigned depth
= surface
->u
.tex
.last_layer
- surface
->u
.tex
.first_layer
+ 1;
1251 unsigned skip_pixels
= 0;
1252 bool success
= false;
1254 /* Check alignment. */
1256 unsigned ofs
= (buf_offset
* bytes_per_pixel
) % ctx
->Const
.TextureBufferOffsetAlignment
;
1258 if (ofs
% bytes_per_pixel
!= 0)
1261 skip_pixels
= ofs
/ bytes_per_pixel
;
1262 buf_offset
-= skip_pixels
;
1266 /* Create the shaders */
1267 if (!st
->pbo_upload
.vs
) {
1268 st
->pbo_upload
.vs
= create_pbo_upload_vs(st
);
1269 if (!st
->pbo_upload
.vs
)
1273 if (depth
!= 1 && st
->pbo_upload
.use_gs
&& !st
->pbo_upload
.gs
) {
1274 st
->pbo_upload
.gs
= create_pbo_upload_gs(st
);
1275 if (!st
->pbo_upload
.gs
)
1279 if (!st
->pbo_upload
.fs
) {
1280 st
->pbo_upload
.fs
= create_pbo_upload_fs(st
);
1281 if (!st
->pbo_upload
.fs
)
1285 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1286 CSO_BIT_FRAGMENT_SAMPLERS
|
1287 CSO_BIT_VERTEX_ELEMENTS
|
1288 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1289 CSO_BIT_FRAMEBUFFER
|
1292 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1293 CSO_BIT_RASTERIZER
|
1294 CSO_BIT_STREAM_OUTPUTS
|
1295 CSO_BIT_PAUSE_QUERIES
|
1296 CSO_BITS_ALL_SHADERS
));
1297 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1300 /* Set up the sampler_view */
1302 unsigned first_element
= buf_offset
;
1303 unsigned last_element
= buf_offset
+ skip_pixels
+ upload_width
- 1
1304 + (upload_height
- 1 + (depth
- 1) * image_height
) * stride
;
1305 struct pipe_sampler_view templ
;
1306 struct pipe_sampler_view
*sampler_view
;
1307 struct pipe_sampler_state sampler
= {0};
1308 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1310 /* This should be ensured by Mesa before calling our callbacks */
1311 assert((last_element
+ 1) * bytes_per_pixel
<= buffer
->width0
);
1313 if (last_element
- first_element
> ctx
->Const
.MaxTextureBufferSize
- 1)
1316 memset(&templ
, 0, sizeof(templ
));
1317 templ
.target
= PIPE_BUFFER
;
1318 templ
.format
= src_format
;
1319 templ
.u
.buf
.first_element
= first_element
;
1320 templ
.u
.buf
.last_element
= last_element
;
1321 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1322 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1323 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1324 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1326 sampler_view
= pipe
->create_sampler_view(pipe
, buffer
, &templ
);
1327 if (sampler_view
== NULL
)
1330 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1332 pipe_sampler_view_reference(&sampler_view
, NULL
);
1334 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1337 /* Upload vertices */
1339 struct pipe_vertex_buffer vbo
;
1340 struct pipe_vertex_element velem
;
1342 float x0
= (float) xoffset
/ surface
->width
* 2.0f
- 1.0f
;
1343 float y0
= (float) yoffset
/ surface
->height
* 2.0f
- 1.0f
;
1344 float x1
= (float) (xoffset
+ upload_width
) / surface
->width
* 2.0f
- 1.0f
;
1345 float y1
= (float) (yoffset
+ upload_height
) / surface
->height
* 2.0f
- 1.0f
;
1347 float *verts
= NULL
;
1349 vbo
.user_buffer
= NULL
;
1351 vbo
.stride
= 2 * sizeof(float);
1353 u_upload_alloc(st
->uploader
, 0, 8 * sizeof(float), 4,
1354 &vbo
.buffer_offset
, &vbo
.buffer
, (void **) &verts
);
1367 u_upload_unmap(st
->uploader
);
1369 velem
.src_offset
= 0;
1370 velem
.instance_divisor
= 0;
1371 velem
.vertex_buffer_index
= cso_get_aux_vertex_buffer_slot(cso
);
1372 velem
.src_format
= PIPE_FORMAT_R32G32_FLOAT
;
1374 cso_set_vertex_elements(cso
, 1, &velem
);
1376 cso_set_vertex_buffers(cso
, velem
.vertex_buffer_index
, 1, &vbo
);
1378 pipe_resource_reference(&vbo
.buffer
, NULL
);
1381 /* Upload constants */
1382 /* Note: the user buffer must be valid until draw time */
1391 struct pipe_constant_buffer cb
;
1393 constants
.xoffset
= -xoffset
+ skip_pixels
;
1394 constants
.yoffset
= -yoffset
;
1395 constants
.stride
= stride
;
1396 constants
.image_size
= stride
* image_height
;
1398 if (st
->constbuf_uploader
) {
1400 cb
.user_buffer
= NULL
;
1401 u_upload_data(st
->constbuf_uploader
, 0, sizeof(constants
),
1402 ctx
->Const
.UniformBufferOffsetAlignment
,
1403 &constants
, &cb
.buffer_offset
, &cb
.buffer
);
1407 u_upload_unmap(st
->constbuf_uploader
);
1410 cb
.user_buffer
= &constants
;
1411 cb
.buffer_offset
= 0;
1413 cb
.buffer_size
= sizeof(constants
);
1415 cso_set_constant_buffer(cso
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
1417 pipe_resource_reference(&cb
.buffer
, NULL
);
1420 /* Framebuffer_state */
1422 struct pipe_framebuffer_state fb
;
1423 memset(&fb
, 0, sizeof(fb
));
1424 fb
.width
= surface
->width
;
1425 fb
.height
= surface
->height
;
1427 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1429 cso_set_framebuffer(cso
, &fb
);
1431 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1434 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1437 cso_set_blend(cso
, &st
->pbo_upload
.blend
);
1439 /* Depth/stencil/alpha state */
1441 struct pipe_depth_stencil_alpha_state dsa
;
1442 memset(&dsa
, 0, sizeof(dsa
));
1443 cso_set_depth_stencil_alpha(cso
, &dsa
);
1446 /* Rasterizer state */
1447 cso_set_rasterizer(cso
, &st
->pbo_upload
.raster
);
1449 /* Set up the shaders */
1450 cso_set_vertex_shader_handle(cso
, st
->pbo_upload
.vs
);
1452 cso_set_geometry_shader_handle(cso
, depth
!= 1 ? st
->pbo_upload
.gs
: NULL
);
1454 cso_set_tessctrl_shader_handle(cso
, NULL
);
1456 cso_set_tesseval_shader_handle(cso
, NULL
);
1458 cso_set_fragment_shader_handle(cso
, st
->pbo_upload
.fs
);
1460 /* Disable stream output */
1461 cso_set_stream_outputs(cso
, 0, NULL
, 0);
1464 cso_draw_arrays(cso
, PIPE_PRIM_TRIANGLE_STRIP
, 0, 4);
1466 cso_draw_arrays_instanced(cso
, PIPE_PRIM_TRIANGLE_STRIP
,
1473 cso_restore_state(cso
);
1474 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1480 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1481 struct gl_texture_image
*texImage
,
1482 GLenum format
, GLenum type
,
1483 enum pipe_format dst_format
,
1484 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1485 GLint width
, GLint height
, GLint depth
,
1487 const struct gl_pixelstore_attrib
*unpack
)
1489 struct st_context
*st
= st_context(ctx
);
1490 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1491 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1492 struct pipe_resource
*texture
= stImage
->pt
;
1493 struct pipe_context
*pipe
= st
->pipe
;
1494 struct pipe_screen
*screen
= pipe
->screen
;
1495 struct pipe_surface
*surface
= NULL
;
1496 enum pipe_format src_format
;
1497 const struct util_format_description
*desc
;
1498 GLenum gl_target
= texImage
->TexObject
->Target
;
1499 intptr_t buf_offset
;
1500 unsigned bytes_per_pixel
;
1501 unsigned stride
, image_height
;
1504 if (!st
->pbo_upload
.enabled
)
1507 /* From now on, we need the gallium representation of dimensions. */
1508 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1515 image_height
= unpack
->ImageHeight
> 0 ? unpack
->ImageHeight
: height
;
1518 if (depth
!= 1 && !st
->pbo_upload
.upload_layers
)
1521 /* Choose the source format. Initially, we do so without checking driver
1522 * support at all because of the remapping we later perform and because
1523 * at least the Radeon driver actually supports some formats for texture
1524 * buffers which it doesn't support for regular textures. */
1525 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1530 src_format
= util_format_linear(src_format
);
1531 desc
= util_format_description(src_format
);
1533 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1536 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1539 if (st
->pbo_upload
.rgba_only
) {
1540 enum pipe_format orig_dst_format
= dst_format
;
1542 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1546 if (dst_format
!= orig_dst_format
&&
1547 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1548 PIPE_BIND_RENDER_TARGET
)) {
1554 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1555 PIPE_BIND_SAMPLER_VIEW
)) {
1559 /* Check if the offset satisfies the alignment requirements */
1560 buf_offset
= (intptr_t) pixels
;
1561 bytes_per_pixel
= desc
->block
.bits
/ 8;
1563 if (buf_offset
% bytes_per_pixel
) {
1567 /* Convert to texels */
1568 buf_offset
= buf_offset
/ bytes_per_pixel
;
1570 /* Compute the stride, taking unpack->Alignment into account */
1572 unsigned pixels_per_row
= unpack
->RowLength
> 0 ?
1573 unpack
->RowLength
: width
;
1574 unsigned bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1575 unsigned remainder
= bytes_per_row
% unpack
->Alignment
;
1576 unsigned offset_rows
;
1579 bytes_per_row
+= (unpack
->Alignment
- remainder
);
1581 if (bytes_per_row
% bytes_per_pixel
) {
1585 stride
= bytes_per_row
/ bytes_per_pixel
;
1587 offset_rows
= unpack
->SkipRows
;
1589 offset_rows
+= image_height
* unpack
->SkipImages
;
1591 buf_offset
+= unpack
->SkipPixels
+ stride
* offset_rows
;
1594 /* Set up the surface */
1596 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1597 unsigned max_layer
= util_max_layer(texture
, level
);
1599 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1601 struct pipe_surface templ
;
1602 memset(&templ
, 0, sizeof(templ
));
1603 templ
.format
= dst_format
;
1604 templ
.u
.tex
.level
= level
;
1605 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1606 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1608 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1613 success
= try_pbo_upload_common(ctx
, surface
,
1614 xoffset
, yoffset
, width
, height
,
1615 st_buffer_object(unpack
->BufferObj
)->buffer
,
1618 bytes_per_pixel
, stride
, image_height
);
1620 pipe_surface_reference(&surface
, NULL
);
1626 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1627 struct gl_texture_image
*texImage
,
1628 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1629 GLint width
, GLint height
, GLint depth
,
1630 GLenum format
, GLenum type
, const void *pixels
,
1631 const struct gl_pixelstore_attrib
*unpack
)
1633 struct st_context
*st
= st_context(ctx
);
1634 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1635 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1636 struct pipe_context
*pipe
= st
->pipe
;
1637 struct pipe_screen
*screen
= pipe
->screen
;
1638 struct pipe_resource
*dst
= stImage
->pt
;
1639 struct pipe_resource
*src
= NULL
;
1640 struct pipe_resource src_templ
;
1641 struct pipe_transfer
*transfer
;
1642 struct pipe_blit_info blit
;
1643 enum pipe_format src_format
, dst_format
;
1644 mesa_format mesa_src_format
;
1645 GLenum gl_target
= texImage
->TexObject
->Target
;
1648 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1649 unsigned dst_level
= 0;
1651 if (stObj
->pt
== stImage
->pt
)
1652 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1654 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1655 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1660 /* Try transfer_inline_write, which should be the fastest memcpy path. */
1662 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1663 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1664 texImage
->TexFormat
, format
, type
,
1666 struct pipe_box box
;
1667 unsigned stride
, layer_stride
;
1670 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1671 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1673 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1676 /* Convert to Gallium coordinates. */
1677 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1682 layer_stride
= stride
;
1685 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1686 pipe
->transfer_inline_write(pipe
, dst
, dst_level
, 0,
1687 &box
, data
, stride
, layer_stride
);
1691 if (!st
->prefer_blit_based_texture_transfer
) {
1695 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1696 * blit implementation in some drivers. */
1697 if (format
== GL_DEPTH_STENCIL
) {
1701 /* If the base internal format and the texture format don't match,
1702 * we can't use blit-based TexSubImage. */
1703 if (texImage
->_BaseFormat
!=
1704 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1709 /* See if the destination format is supported. */
1710 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1711 bind
= PIPE_BIND_DEPTH_STENCIL
;
1713 bind
= PIPE_BIND_RENDER_TARGET
;
1715 /* For luminance and intensity, only the red channel is stored
1716 * in the destination. */
1717 dst_format
= util_format_linear(dst
->format
);
1718 dst_format
= util_format_luminance_to_red(dst_format
);
1719 dst_format
= util_format_intensity_to_red(dst_format
);
1722 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1723 dst
->nr_samples
, bind
)) {
1727 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1728 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1729 xoffset
, yoffset
, zoffset
,
1730 width
, height
, depth
, pixels
, unpack
))
1734 /* See if the texture format already matches the format and type,
1735 * in which case the memcpy-based fast path will likely be used and
1736 * we don't have to blit. */
1737 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1738 type
, unpack
->SwapBytes
, NULL
)) {
1742 /* Choose the source format. */
1743 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1744 format
, type
, unpack
->SwapBytes
);
1749 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1751 /* There is no reason to do this if we cannot use memcpy for the temporary
1752 * source texture at least. This also takes transfer ops into account,
1754 if (!_mesa_texstore_can_use_memcpy(ctx
,
1755 _mesa_get_format_base_format(mesa_src_format
),
1756 mesa_src_format
, format
, type
, unpack
)) {
1760 /* TexSubImage only sets a single cubemap face. */
1761 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1762 gl_target
= GL_TEXTURE_2D
;
1764 /* TexSubImage can specify subsets of cube map array faces
1765 * so we need to upload via 2D array instead */
1766 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1767 gl_target
= GL_TEXTURE_2D_ARRAY
;
1770 /* Initialize the source texture description. */
1771 memset(&src_templ
, 0, sizeof(src_templ
));
1772 src_templ
.target
= gl_target_to_pipe(gl_target
);
1773 src_templ
.format
= src_format
;
1774 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1775 src_templ
.usage
= PIPE_USAGE_STAGING
;
1777 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1778 &src_templ
.width0
, &src_templ
.height0
,
1779 &src_templ
.depth0
, &src_templ
.array_size
);
1781 /* Check for NPOT texture support. */
1782 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1783 (!util_is_power_of_two(src_templ
.width0
) ||
1784 !util_is_power_of_two(src_templ
.height0
) ||
1785 !util_is_power_of_two(src_templ
.depth0
))) {
1789 /* Create the source texture. */
1790 src
= screen
->resource_create(screen
, &src_templ
);
1795 /* Map source pixels. */
1796 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1797 format
, type
, pixels
, unpack
,
1800 /* This is a GL error. */
1801 pipe_resource_reference(&src
, NULL
);
1805 /* From now on, we need the gallium representation of dimensions. */
1806 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1813 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1814 width
, height
, depth
, &transfer
);
1816 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1817 pipe_resource_reference(&src
, NULL
);
1821 /* Upload pixels (just memcpy). */
1823 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1826 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1827 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1828 /* 1D array textures.
1829 * We need to convert gallium coords to GL coords.
1831 void *src
= _mesa_image_address2d(unpack
, pixels
,
1832 width
, depth
, format
,
1834 memcpy(map
, src
, bytesPerRow
);
1837 ubyte
*slice_map
= map
;
1839 for (row
= 0; row
< (unsigned) height
; row
++) {
1840 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1841 width
, height
, format
,
1842 type
, slice
, row
, 0);
1843 memcpy(slice_map
, src
, bytesPerRow
);
1844 slice_map
+= transfer
->stride
;
1847 map
+= transfer
->layer_stride
;
1851 pipe_transfer_unmap(pipe
, transfer
);
1852 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1855 memset(&blit
, 0, sizeof(blit
));
1856 blit
.src
.resource
= src
;
1858 blit
.src
.format
= src_format
;
1859 blit
.dst
.resource
= dst
;
1860 blit
.dst
.level
= dst_level
;
1861 blit
.dst
.format
= dst_format
;
1862 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1863 blit
.dst
.box
.x
= xoffset
;
1864 blit
.dst
.box
.y
= yoffset
;
1865 blit
.dst
.box
.z
= zoffset
+ dstz
;
1866 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1867 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1868 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1869 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1870 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1871 blit
.scissor_enable
= FALSE
;
1873 st
->pipe
->blit(st
->pipe
, &blit
);
1875 pipe_resource_reference(&src
, NULL
);
1879 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1880 width
, height
, depth
, format
, type
, pixels
,
1885 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1886 struct gl_texture_image
*texImage
,
1887 GLenum format
, GLenum type
, const void *pixels
,
1888 const struct gl_pixelstore_attrib
*unpack
)
1890 assert(dims
== 1 || dims
== 2 || dims
== 3);
1892 prep_teximage(ctx
, texImage
, format
, type
);
1894 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1897 /* allocate storage for texture data */
1898 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1899 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1903 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1904 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1905 format
, type
, pixels
, unpack
);
1910 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1911 struct gl_texture_image
*texImage
,
1912 GLint x
, GLint y
, GLint z
,
1913 GLsizei w
, GLsizei h
, GLsizei d
,
1914 GLenum format
, GLsizei imageSize
, const void *data
)
1916 struct st_context
*st
= st_context(ctx
);
1917 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1918 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1919 struct pipe_resource
*texture
= stImage
->pt
;
1920 struct pipe_context
*pipe
= st
->pipe
;
1921 struct pipe_screen
*screen
= pipe
->screen
;
1922 struct pipe_resource
*dst
= stImage
->pt
;
1923 struct pipe_surface
*surface
= NULL
;
1924 struct compressed_pixelstore store
;
1925 enum pipe_format copy_format
;
1926 unsigned bytes_per_block
;
1928 intptr_t buf_offset
;
1929 bool success
= false;
1931 /* Check basic pre-conditions for PBO upload */
1932 if (!st
->prefer_blit_based_texture_transfer
) {
1936 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1939 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
1940 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
1941 /* ETC isn't supported and is represented by uncompressed formats. */
1949 if (!st
->pbo_upload
.enabled
||
1950 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1954 /* Choose the pipe format for the upload. */
1955 bytes_per_block
= util_format_get_blocksize(dst
->format
);
1956 bw
= util_format_get_blockwidth(dst
->format
);
1957 bh
= util_format_get_blockheight(dst
->format
);
1959 switch (bytes_per_block
) {
1961 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1964 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1970 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1971 PIPE_BIND_SAMPLER_VIEW
)) {
1975 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1976 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1980 /* Interpret the pixelstore settings. */
1981 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1982 &ctx
->Unpack
, &store
);
1983 assert(store
.CopyBytesPerRow
% bytes_per_block
== 0);
1984 assert(store
.SkipBytes
% bytes_per_block
== 0);
1986 /* Compute the offset into the buffer */
1987 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1989 if (buf_offset
% bytes_per_block
) {
1993 buf_offset
= buf_offset
/ bytes_per_block
;
1995 /* Set up the surface. */
1997 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1998 unsigned max_layer
= util_max_layer(texture
, level
);
2000 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
2002 struct pipe_surface templ
;
2003 memset(&templ
, 0, sizeof(templ
));
2004 templ
.format
= copy_format
;
2005 templ
.u
.tex
.level
= level
;
2006 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
2007 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
2009 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
2014 success
= try_pbo_upload_common(ctx
, surface
,
2016 store
.CopyBytesPerRow
/ bytes_per_block
,
2017 store
.CopyRowsPerSlice
,
2018 st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
2022 store
.TotalBytesPerRow
/ bytes_per_block
,
2023 store
.TotalRowsPerSlice
);
2025 pipe_surface_reference(&surface
, NULL
);
2031 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
2033 format
, imageSize
, data
);
2037 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
2038 struct gl_texture_image
*texImage
,
2039 GLsizei imageSize
, const void *data
)
2041 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
2043 /* only 2D and 3D compressed images are supported at this time */
2045 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
2049 /* This is pretty simple, because unlike the general texstore path we don't
2050 * have to worry about the usual image unpacking or image transfer
2054 assert(texImage
->Width
> 0);
2055 assert(texImage
->Height
> 0);
2056 assert(texImage
->Depth
> 0);
2058 /* allocate storage for texture data */
2059 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
2060 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
2064 st_CompressedTexSubImage(ctx
, dims
, texImage
,
2066 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
2067 texImage
->TexFormat
,
2075 * Called via ctx->Driver.GetTexSubImage()
2077 * This uses a blit to copy the texture to a texture format which matches
2078 * the format and type combo and then a fast read-back is done using memcpy.
2079 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
2080 * a format which matches the swizzling.
2082 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
2084 * NOTE: Drivers usually do a blit to convert between tiled and linear
2085 * texture layouts during texture uploads/downloads, so the blit
2086 * we do here should be free in such cases.
2089 st_GetTexSubImage(struct gl_context
* ctx
,
2090 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2091 GLsizei width
, GLsizei height
, GLint depth
,
2092 GLenum format
, GLenum type
, void * pixels
,
2093 struct gl_texture_image
*texImage
)
2095 struct st_context
*st
= st_context(ctx
);
2096 struct pipe_context
*pipe
= st
->pipe
;
2097 struct pipe_screen
*screen
= pipe
->screen
;
2098 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2099 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2100 struct pipe_resource
*src
= stObj
->pt
;
2101 struct pipe_resource
*dst
= NULL
;
2102 struct pipe_resource dst_templ
;
2103 enum pipe_format dst_format
, src_format
;
2104 mesa_format mesa_format
;
2105 GLenum gl_target
= texImage
->TexObject
->Target
;
2106 enum pipe_texture_target pipe_target
;
2107 struct pipe_blit_info blit
;
2108 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
2109 struct pipe_transfer
*tex_xfer
;
2111 boolean done
= FALSE
;
2113 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2114 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2116 if (!st
->prefer_blit_based_texture_transfer
&&
2117 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
2118 /* Try to avoid the fallback if we're doing texture decompression here */
2122 /* Handle non-finalized textures. */
2123 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
2127 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
2128 * due to an incomplete stencil blit implementation in some drivers. */
2129 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
2133 /* If the base internal format and the texture format don't match, we have
2134 * to fall back to _mesa_GetTexImage_sw. */
2135 if (texImage
->_BaseFormat
!=
2136 _mesa_get_format_base_format(texImage
->TexFormat
)) {
2140 /* See if the texture format already matches the format and type,
2141 * in which case the memcpy-based fast path will be used. */
2142 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
2143 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
2147 /* Convert the source format to what is expected by GetTexImage
2148 * and see if it's supported.
2150 * This only applies to glGetTexImage:
2151 * - Luminance must be returned as (L,0,0,1).
2152 * - Luminance alpha must be returned as (L,0,0,A).
2153 * - Intensity must be returned as (I,0,0,1)
2155 if (stObj
->surface_based
)
2156 src_format
= util_format_linear(stObj
->surface_format
);
2158 src_format
= util_format_linear(src
->format
);
2159 src_format
= util_format_luminance_to_red(src_format
);
2160 src_format
= util_format_intensity_to_red(src_format
);
2163 !screen
->is_format_supported(screen
, src_format
, src
->target
,
2165 PIPE_BIND_SAMPLER_VIEW
)) {
2169 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
2170 bind
|= PIPE_BIND_DEPTH_STENCIL
;
2172 bind
|= PIPE_BIND_RENDER_TARGET
;
2174 /* GetTexImage only returns a single face for cubemaps. */
2175 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
2176 gl_target
= GL_TEXTURE_2D
;
2178 pipe_target
= gl_target_to_pipe(gl_target
);
2180 /* Choose the destination format by finding the best match
2181 * for the format+type combo. */
2182 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
2183 ctx
->Pack
.SwapBytes
);
2185 if (dst_format
== PIPE_FORMAT_NONE
) {
2186 GLenum dst_glformat
;
2188 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
2189 * where decompression with a blit is always preferred. */
2190 if (!util_format_is_compressed(src
->format
)) {
2194 /* Set the appropriate format for the decompressed texture.
2195 * Luminance and sRGB formats shouldn't appear here.*/
2196 switch (src_format
) {
2197 case PIPE_FORMAT_DXT1_RGB
:
2198 case PIPE_FORMAT_DXT1_RGBA
:
2199 case PIPE_FORMAT_DXT3_RGBA
:
2200 case PIPE_FORMAT_DXT5_RGBA
:
2201 case PIPE_FORMAT_RGTC1_UNORM
:
2202 case PIPE_FORMAT_RGTC2_UNORM
:
2203 case PIPE_FORMAT_ETC1_RGB8
:
2204 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
2205 dst_glformat
= GL_RGBA8
;
2207 case PIPE_FORMAT_RGTC1_SNORM
:
2208 case PIPE_FORMAT_RGTC2_SNORM
:
2209 if (!ctx
->Extensions
.EXT_texture_snorm
)
2211 dst_glformat
= GL_RGBA8_SNORM
;
2213 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
2214 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
2215 if (!ctx
->Extensions
.ARB_texture_float
)
2217 dst_glformat
= GL_RGBA32F
;
2224 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
2225 pipe_target
, 0, bind
, FALSE
);
2227 if (dst_format
== PIPE_FORMAT_NONE
) {
2228 /* unable to get an rgba format!?! */
2233 /* create the destination texture of size (width X height X depth) */
2234 memset(&dst_templ
, 0, sizeof(dst_templ
));
2235 dst_templ
.target
= pipe_target
;
2236 dst_templ
.format
= dst_format
;
2237 dst_templ
.bind
= bind
;
2238 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2240 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2241 &dst_templ
.width0
, &dst_templ
.height0
,
2242 &dst_templ
.depth0
, &dst_templ
.array_size
);
2244 dst
= screen
->resource_create(screen
, &dst_templ
);
2249 /* From now on, we need the gallium representation of dimensions. */
2250 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2257 assert(texImage
->Face
== 0 ||
2258 texImage
->TexObject
->MinLayer
== 0 ||
2261 memset(&blit
, 0, sizeof(blit
));
2262 blit
.src
.resource
= src
;
2263 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2264 blit
.src
.format
= src_format
;
2265 blit
.dst
.resource
= dst
;
2267 blit
.dst
.format
= dst
->format
;
2268 blit
.src
.box
.x
= xoffset
;
2270 blit
.src
.box
.y
= yoffset
;
2272 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2274 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2275 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2276 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2277 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2278 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2279 blit
.scissor_enable
= FALSE
;
2281 /* blit/render/decompress */
2282 st
->pipe
->blit(st
->pipe
, &blit
);
2284 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2286 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2287 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2292 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2294 /* copy/pack data into user buffer */
2295 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2296 ctx
->Pack
.SwapBytes
, NULL
)) {
2298 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2301 for (slice
= 0; slice
< depth
; slice
++) {
2302 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2303 /* 1D array textures.
2304 * We need to convert gallium coords to GL coords.
2306 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2307 width
, depth
, format
,
2309 memcpy(dest
, map
, bytesPerRow
);
2312 ubyte
*slice_map
= map
;
2314 for (row
= 0; row
< height
; row
++) {
2315 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2316 width
, height
, format
,
2317 type
, slice
, row
, 0);
2318 memcpy(dest
, slice_map
, bytesPerRow
);
2319 slice_map
+= tex_xfer
->stride
;
2322 map
+= tex_xfer
->layer_stride
;
2326 /* format translation via floats */
2329 uint32_t dstMesaFormat
;
2330 int dstStride
, srcStride
;
2332 assert(util_format_is_compressed(src
->format
));
2334 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
2339 if (ST_DEBUG
& DEBUG_FALLBACK
)
2340 debug_printf("%s: fallback format translation\n", __func__
);
2342 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2343 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2344 srcStride
= 4 * width
* sizeof(GLfloat
);
2345 for (slice
= 0; slice
< depth
; slice
++) {
2346 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2347 /* 1D array textures.
2348 * We need to convert gallium coords to GL coords.
2350 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2351 width
, depth
, format
,
2354 /* get float[4] rgba row from surface */
2355 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
2358 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2359 rgba
, RGBA32_FLOAT
, srcStride
,
2363 for (row
= 0; row
< height
; row
++) {
2364 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2365 width
, height
, format
,
2366 type
, slice
, row
, 0);
2368 /* get float[4] rgba row from surface */
2369 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
2372 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2373 rgba
, RGBA32_FLOAT
, srcStride
,
2377 map
+= tex_xfer
->layer_stride
;
2386 pipe_transfer_unmap(pipe
, tex_xfer
);
2388 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2389 pipe_resource_reference(&dst
, NULL
);
2393 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2394 width
, height
, depth
,
2395 format
, type
, pixels
, texImage
);
2401 * Do a CopyTexSubImage operation using a read transfer from the source,
2402 * a write transfer to the destination and get_tile()/put_tile() to access
2403 * the pixels/texels.
2405 * Note: srcY=0=TOP of renderbuffer
2408 fallback_copy_texsubimage(struct gl_context
*ctx
,
2409 struct st_renderbuffer
*strb
,
2410 struct st_texture_image
*stImage
,
2412 GLint destX
, GLint destY
, GLint slice
,
2413 GLint srcX
, GLint srcY
,
2414 GLsizei width
, GLsizei height
)
2416 struct st_context
*st
= st_context(ctx
);
2417 struct pipe_context
*pipe
= st
->pipe
;
2418 struct pipe_transfer
*src_trans
;
2420 enum pipe_transfer_usage transfer_usage
;
2422 unsigned dst_width
= width
;
2423 unsigned dst_height
= height
;
2424 unsigned dst_depth
= 1;
2425 struct pipe_transfer
*transfer
;
2427 if (ST_DEBUG
& DEBUG_FALLBACK
)
2428 debug_printf("%s: fallback processing\n", __func__
);
2430 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2431 srcY
= strb
->Base
.Height
- srcY
- height
;
2434 map
= pipe_transfer_map(pipe
,
2436 strb
->surface
->u
.tex
.level
,
2437 strb
->surface
->u
.tex
.first_layer
,
2440 width
, height
, &src_trans
);
2442 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2443 baseFormat
== GL_DEPTH_STENCIL
) &&
2444 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2445 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2447 transfer_usage
= PIPE_TRANSFER_WRITE
;
2449 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2450 destX
, destY
, slice
,
2451 dst_width
, dst_height
, dst_depth
,
2454 if (baseFormat
== GL_DEPTH_COMPONENT
||
2455 baseFormat
== GL_DEPTH_STENCIL
) {
2456 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2457 ctx
->Pixel
.DepthBias
!= 0.0F
);
2461 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2462 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2471 data
= malloc(width
* sizeof(uint
));
2474 /* To avoid a large temp memory allocation, do copy row by row */
2475 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2476 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2478 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2481 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2482 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2483 0, 0, width
, 1, data
);
2486 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2491 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2499 malloc(width
* height
* 4 * sizeof(GLfloat
));
2501 if (tempSrc
&& texDest
) {
2502 const GLint dims
= 2;
2504 struct gl_texture_image
*texImage
= &stImage
->base
;
2505 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2507 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2508 unpack
.Invert
= GL_TRUE
;
2511 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2512 dstRowStride
= transfer
->layer_stride
;
2515 dstRowStride
= transfer
->stride
;
2518 /* get float/RGBA image from framebuffer */
2519 /* XXX this usually involves a lot of int/float conversion.
2520 * try to avoid that someday.
2522 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2523 util_format_linear(strb
->texture
->format
),
2526 /* Store into texture memory.
2527 * Note that this does some special things such as pixel transfer
2528 * ops and format conversion. In particular, if the dest tex format
2529 * is actually RGBA but the user created the texture as GL_RGB we
2530 * need to fill-in/override the alpha channel with 1.0.
2532 _mesa_texstore(ctx
, dims
,
2533 texImage
->_BaseFormat
,
2534 texImage
->TexFormat
,
2538 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2542 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2548 st_texture_image_unmap(st
, stImage
, slice
);
2549 pipe
->transfer_unmap(pipe
, src_trans
);
2554 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2555 * Note that the region to copy has already been clipped so we know we
2556 * won't read from outside the source renderbuffer's bounds.
2558 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2561 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2562 struct gl_texture_image
*texImage
,
2563 GLint destX
, GLint destY
, GLint slice
,
2564 struct gl_renderbuffer
*rb
,
2565 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2567 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2568 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2569 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2570 struct st_context
*st
= st_context(ctx
);
2571 struct pipe_context
*pipe
= st
->pipe
;
2572 struct pipe_screen
*screen
= pipe
->screen
;
2573 struct pipe_blit_info blit
;
2574 enum pipe_format dst_format
;
2575 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2579 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2580 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2582 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2583 debug_printf("%s: null strb or stImage\n", __func__
);
2587 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2588 texImage
->TexFormat
)) {
2592 /* The base internal format must match the mesa format, so make sure
2593 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2595 if (texImage
->_BaseFormat
!=
2596 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2597 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2601 /* Choose the destination format to match the TexImage behavior. */
2602 dst_format
= util_format_linear(stImage
->pt
->format
);
2603 dst_format
= util_format_luminance_to_red(dst_format
);
2604 dst_format
= util_format_intensity_to_red(dst_format
);
2606 /* See if the destination format is supported. */
2607 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2608 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2609 bind
= PIPE_BIND_DEPTH_STENCIL
;
2612 bind
= PIPE_BIND_RENDER_TARGET
;
2616 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2617 stImage
->pt
->nr_samples
, bind
)) {
2621 /* Y flipping for the main framebuffer. */
2623 srcY1
= strb
->Base
.Height
- srcY
- height
;
2624 srcY0
= srcY1
+ height
;
2628 srcY1
= srcY0
+ height
;
2631 /* Blit the texture.
2632 * This supports flipping, format conversions, and downsampling.
2634 memset(&blit
, 0, sizeof(blit
));
2635 blit
.src
.resource
= strb
->texture
;
2636 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2637 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2638 blit
.src
.box
.x
= srcX
;
2639 blit
.src
.box
.y
= srcY0
;
2640 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2641 blit
.src
.box
.width
= width
;
2642 blit
.src
.box
.height
= srcY1
- srcY0
;
2643 blit
.src
.box
.depth
= 1;
2644 blit
.dst
.resource
= stImage
->pt
;
2645 blit
.dst
.format
= dst_format
;
2646 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2647 blit
.dst
.box
.x
= destX
;
2648 blit
.dst
.box
.y
= destY
;
2649 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2650 blit
.dst
.box
.width
= width
;
2651 blit
.dst
.box
.height
= height
;
2652 blit
.dst
.box
.depth
= 1;
2653 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2654 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2655 pipe
->blit(pipe
, &blit
);
2659 /* software fallback */
2660 fallback_copy_texsubimage(ctx
,
2661 strb
, stImage
, texImage
->_BaseFormat
,
2662 destX
, destY
, slice
,
2663 srcX
, srcY
, width
, height
);
2668 * Copy image data from stImage into the texture object 'stObj' at level
2672 copy_image_data_to_texture(struct st_context
*st
,
2673 struct st_texture_object
*stObj
,
2675 struct st_texture_image
*stImage
)
2679 const struct gl_texture_image
*dstImage
=
2680 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2682 assert(dstImage
->Width
== stImage
->base
.Width
);
2683 assert(dstImage
->Height
== stImage
->base
.Height
);
2684 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2688 /* Copy potentially with the blitter:
2691 if (stImage
->pt
->last_level
== 0)
2694 src_level
= stImage
->base
.Level
;
2696 assert(src_level
<= stImage
->pt
->last_level
);
2697 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2698 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2699 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2700 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2701 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2702 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2704 st_texture_image_copy(st
->pipe
,
2705 stObj
->pt
, dstLevel
, /* dest texture, level */
2706 stImage
->pt
, src_level
, /* src texture, level */
2707 stImage
->base
.Face
);
2709 pipe_resource_reference(&stImage
->pt
, NULL
);
2711 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2716 * Called during state validation. When this function is finished,
2717 * the texture object should be ready for rendering.
2718 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2721 st_finalize_texture(struct gl_context
*ctx
,
2722 struct pipe_context
*pipe
,
2723 struct gl_texture_object
*tObj
)
2725 struct st_context
*st
= st_context(ctx
);
2726 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2727 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2729 const struct st_texture_image
*firstImage
;
2730 enum pipe_format firstImageFormat
;
2731 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2733 if (tObj
->Immutable
)
2736 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2737 /* The texture is complete and we know exactly how many mipmap levels
2738 * are present/needed. This is conditional because we may be called
2739 * from the st_generate_mipmap() function when the texture object is
2740 * incomplete. In that case, we'll have set stObj->lastLevel before
2743 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2744 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2745 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2747 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2750 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2751 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2754 pipe_resource_reference(&stObj
->pt
, NULL
);
2755 st_texture_release_all_sampler_views(st
, stObj
);
2759 if (st_obj
->buffer
!= stObj
->pt
) {
2760 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2761 st_texture_release_all_sampler_views(st
, stObj
);
2762 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
2770 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
2773 /* If both firstImage and stObj point to a texture which can contain
2774 * all active images, favour firstImage. Note that because of the
2775 * completeness requirement, we know that the image dimensions
2778 if (firstImage
->pt
&&
2779 firstImage
->pt
!= stObj
->pt
&&
2780 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2781 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2782 st_texture_release_all_sampler_views(st
, stObj
);
2785 /* If this texture comes from a window system, there is nothing else to do. */
2786 if (stObj
->surface_based
) {
2790 /* Find gallium format for the Mesa texture */
2792 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2794 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2796 GLuint width
, height
, depth
;
2797 if (!guess_base_level_size(stObj
->base
.Target
,
2798 firstImage
->base
.Width2
,
2799 firstImage
->base
.Height2
,
2800 firstImage
->base
.Depth2
,
2801 firstImage
->base
.Level
,
2802 &width
, &height
, &depth
)) {
2803 width
= stObj
->width0
;
2804 height
= stObj
->height0
;
2805 depth
= stObj
->depth0
;
2807 /* The width/height/depth may have been previously reset in
2808 * guess_and_alloc_texture. */
2809 stObj
->width0
= width
;
2810 stObj
->height0
= height
;
2811 stObj
->depth0
= depth
;
2813 /* convert GL dims to Gallium dims */
2814 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
2815 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2816 ptNumSamples
= firstImage
->base
.NumSamples
;
2819 /* If we already have a gallium texture, check that it matches the texture
2820 * object's format, target, size, num_levels, etc.
2823 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2824 stObj
->pt
->format
!= firstImageFormat
||
2825 stObj
->pt
->last_level
< stObj
->lastLevel
||
2826 stObj
->pt
->width0
!= ptWidth
||
2827 stObj
->pt
->height0
!= ptHeight
||
2828 stObj
->pt
->depth0
!= ptDepth
||
2829 stObj
->pt
->nr_samples
!= ptNumSamples
||
2830 stObj
->pt
->array_size
!= ptLayers
)
2832 /* The gallium texture does not match the Mesa texture so delete the
2833 * gallium texture now. We'll make a new one below.
2835 pipe_resource_reference(&stObj
->pt
, NULL
);
2836 st_texture_release_all_sampler_views(st
, stObj
);
2837 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
2841 /* May need to create a new gallium texture:
2844 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2846 stObj
->pt
= st_texture_create(st
,
2847 gl_target_to_pipe(stObj
->base
.Target
),
2853 ptLayers
, ptNumSamples
,
2857 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2862 /* Pull in any images not in the object's texture:
2864 for (face
= 0; face
< nr_faces
; face
++) {
2866 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2867 struct st_texture_image
*stImage
=
2868 st_texture_image(stObj
->base
.Image
[face
][level
]);
2870 /* Need to import images in main memory or held in other textures.
2872 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2873 GLuint height
= stObj
->height0
;
2874 GLuint depth
= stObj
->depth0
;
2876 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2877 height
= u_minify(height
, level
);
2878 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2879 depth
= u_minify(depth
, level
);
2882 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
2883 stImage
->base
.Height
== height
&&
2884 stImage
->base
.Depth
== depth
)) {
2885 /* src image fits expected dest mipmap level size */
2886 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2897 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2898 * for a whole mipmap stack.
2901 st_AllocTextureStorage(struct gl_context
*ctx
,
2902 struct gl_texture_object
*texObj
,
2903 GLsizei levels
, GLsizei width
,
2904 GLsizei height
, GLsizei depth
)
2906 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2907 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2908 struct st_context
*st
= st_context(ctx
);
2909 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2910 struct pipe_screen
*screen
= st
->pipe
->screen
;
2911 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
2912 enum pipe_format fmt
;
2914 GLuint num_samples
= texImage
->NumSamples
;
2918 /* Save the level=0 dimensions */
2919 stObj
->width0
= width
;
2920 stObj
->height0
= height
;
2921 stObj
->depth0
= depth
;
2922 stObj
->lastLevel
= levels
- 1;
2924 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2926 bindings
= default_bindings(st
, fmt
);
2928 /* Raise the sample count if the requested one is unsupported. */
2929 if (num_samples
> 1) {
2930 boolean found
= FALSE
;
2932 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2933 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2935 PIPE_BIND_SAMPLER_VIEW
)) {
2936 /* Update the sample count in gl_texture_image as well. */
2937 texImage
->NumSamples
= num_samples
;
2948 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2949 width
, height
, depth
,
2950 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2952 stObj
->pt
= st_texture_create(st
,
2953 gl_target_to_pipe(texObj
->Target
),
2959 ptLayers
, num_samples
,
2964 /* Set image resource pointers */
2965 for (level
= 0; level
< levels
; level
++) {
2967 for (face
= 0; face
< numFaces
; face
++) {
2968 struct st_texture_image
*stImage
=
2969 st_texture_image(texObj
->Image
[face
][level
]);
2970 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2979 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2980 GLint level
, mesa_format format
,
2981 GLint width
, GLint height
,
2982 GLint depth
, GLint border
)
2984 struct st_context
*st
= st_context(ctx
);
2985 struct pipe_context
*pipe
= st
->pipe
;
2987 if (width
== 0 || height
== 0 || depth
== 0) {
2988 /* zero-sized images are legal, and always fit! */
2992 if (pipe
->screen
->can_create_resource
) {
2993 /* Ask the gallium driver if the texture is too large */
2994 struct gl_texture_object
*texObj
=
2995 _mesa_get_current_tex_object(ctx
, target
);
2996 struct pipe_resource pt
;
2998 /* Setup the pipe_resource object
3000 memset(&pt
, 0, sizeof(pt
));
3002 pt
.target
= gl_target_to_pipe(target
);
3003 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
3005 st_gl_texture_dims_to_pipe_dims(target
,
3006 width
, height
, depth
,
3007 &pt
.width0
, &pt
.height0
,
3008 &pt
.depth0
, &pt
.array_size
);
3010 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
3011 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
3012 /* assume just one mipmap level */
3016 /* assume a full set of mipmaps */
3017 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
3020 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
3023 /* Use core Mesa fallback */
3024 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
3025 width
, height
, depth
, border
);
3030 st_TextureView(struct gl_context
*ctx
,
3031 struct gl_texture_object
*texObj
,
3032 struct gl_texture_object
*origTexObj
)
3034 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
3035 struct st_texture_object
*tex
= st_texture_object(texObj
);
3036 struct gl_texture_image
*image
= texObj
->Image
[0][0];
3038 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
3039 const int numLevels
= texObj
->NumLevels
;
3044 pipe_resource_reference(&tex
->pt
, orig
->pt
);
3046 /* Set image resource pointers */
3047 for (level
= 0; level
< numLevels
; level
++) {
3048 for (face
= 0; face
< numFaces
; face
++) {
3049 struct st_texture_image
*stImage
=
3050 st_texture_image(texObj
->Image
[face
][level
]);
3051 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
3055 tex
->surface_based
= GL_TRUE
;
3056 tex
->surface_format
=
3057 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
3059 tex
->width0
= image
->Width
;
3060 tex
->height0
= image
->Height
;
3061 tex
->depth0
= image
->Depth
;
3062 tex
->lastLevel
= numLevels
- 1;
3068 st_ClearTexSubImage(struct gl_context
*ctx
,
3069 struct gl_texture_image
*texImage
,
3070 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3071 GLsizei width
, GLsizei height
, GLsizei depth
,
3072 const void *clearValue
)
3074 static const char zeros
[16] = {0};
3075 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3076 struct pipe_resource
*pt
= stImage
->pt
;
3077 struct st_context
*st
= st_context(ctx
);
3078 struct pipe_context
*pipe
= st
->pipe
;
3079 unsigned level
= texImage
->Level
;
3080 struct pipe_box box
;
3085 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3086 width
, height
, depth
, &box
);
3087 if (texImage
->TexObject
->Immutable
) {
3088 level
+= texImage
->TexObject
->MinLevel
;
3089 box
.z
+= texImage
->TexObject
->MinLayer
;
3092 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3096 st_init_texture_functions(struct dd_function_table
*functions
)
3098 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3099 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3100 functions
->TexImage
= st_TexImage
;
3101 functions
->TexSubImage
= st_TexSubImage
;
3102 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3103 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3104 functions
->GenerateMipmap
= st_generate_mipmap
;
3106 functions
->GetTexSubImage
= st_GetTexSubImage
;
3108 /* compressed texture functions */
3109 functions
->CompressedTexImage
= st_CompressedTexImage
;
3110 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
3112 functions
->NewTextureObject
= st_NewTextureObject
;
3113 functions
->NewTextureImage
= st_NewTextureImage
;
3114 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3115 functions
->DeleteTexture
= st_DeleteTextureObject
;
3116 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3117 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3118 functions
->MapTextureImage
= st_MapTextureImage
;
3119 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3121 /* XXX Temporary until we can query pipe's texture sizes */
3122 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3124 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3125 functions
->TextureView
= st_TextureView
;
3126 functions
->ClearTexSubImage
= st_ClearTexSubImage
;