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 st_init_pbo_upload(struct st_context
*st
)
696 struct pipe_context
*pipe
= st
->pipe
;
697 struct pipe_screen
*screen
= pipe
->screen
;
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
)
706 st
->pbo_upload
.rgba_only
=
707 screen
->get_param(screen
, PIPE_CAP_BUFFER_SAMPLER_VIEW_RGBA_ONLY
);
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;
719 memset(&st
->pbo_upload
.blend
, 0, sizeof(struct pipe_blend_state
));
720 st
->pbo_upload
.blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
722 /* Rasterizer state */
723 memset(&st
->pbo_upload
.raster
, 0, sizeof(struct pipe_rasterizer_state
));
724 st
->pbo_upload
.raster
.half_pixel_center
= 1;
728 st_destroy_pbo_upload(struct st_context
*st
)
730 if (st
->pbo_upload
.fs
) {
731 cso_delete_fragment_shader(st
->cso_context
, st
->pbo_upload
.fs
);
732 st
->pbo_upload
.fs
= NULL
;
735 if (st
->pbo_upload
.gs
) {
736 cso_delete_geometry_shader(st
->cso_context
, st
->pbo_upload
.gs
);
737 st
->pbo_upload
.gs
= NULL
;
740 if (st
->pbo_upload
.vs
) {
741 cso_delete_vertex_shader(st
->cso_context
, st
->pbo_upload
.vs
);
742 st
->pbo_upload
.vs
= NULL
;
747 * Converts format to a format with the same components, types
748 * and sizes, but with the components in RGBA order.
750 static enum pipe_format
751 unswizzle_format(enum pipe_format format
)
755 case PIPE_FORMAT_B8G8R8A8_UNORM
:
756 case PIPE_FORMAT_A8R8G8B8_UNORM
:
757 case PIPE_FORMAT_A8B8G8R8_UNORM
:
758 return PIPE_FORMAT_R8G8B8A8_UNORM
;
760 case PIPE_FORMAT_B10G10R10A2_UNORM
:
761 return PIPE_FORMAT_R10G10B10A2_UNORM
;
763 case PIPE_FORMAT_B10G10R10A2_SNORM
:
764 return PIPE_FORMAT_R10G10B10A2_SNORM
;
766 case PIPE_FORMAT_B10G10R10A2_UINT
:
767 return PIPE_FORMAT_R10G10B10A2_UINT
;
775 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
777 static enum pipe_format
778 alpha_to_red(enum pipe_format format
)
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
;
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
;
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
;
815 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
817 static enum pipe_format
818 red_alpha_to_red_green(enum pipe_format format
)
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
;
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
;
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
;
855 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
857 static enum pipe_format
858 luminance_alpha_to_red_green(enum pipe_format format
)
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
;
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
;
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
;
895 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
898 format_is_alpha(enum pipe_format format
)
900 const struct util_format_description
*desc
= util_format_description(format
);
902 if (desc
->nr_channels
== 1 &&
903 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_0
&&
904 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_0
&&
905 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_0
&&
906 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_X
)
913 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
916 format_is_red(enum pipe_format format
)
918 const struct util_format_description
*desc
= util_format_description(format
);
920 if (desc
->nr_channels
== 1 &&
921 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
922 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_0
&&
923 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_0
&&
924 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
)
932 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
935 format_is_luminance(enum pipe_format format
)
937 const struct util_format_description
*desc
= util_format_description(format
);
939 if (desc
->nr_channels
== 1 &&
940 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
941 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
942 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
943 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
)
950 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
953 format_is_red_alpha(enum pipe_format format
)
955 const struct util_format_description
*desc
= util_format_description(format
);
957 if (desc
->nr_channels
== 2 &&
958 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
959 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_0
&&
960 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_0
&&
961 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_Y
)
968 format_is_swizzled_rgba(enum pipe_format format
)
970 const struct util_format_description
*desc
= util_format_description(format
);
972 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_0
) &&
973 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_0
) &&
974 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_0
) &&
975 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
))
983 unsigned char swizzle
[4];
984 enum pipe_format format
;
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
}
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
}
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
}
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
}
1009 static enum pipe_format
1010 swizzle_format(enum pipe_format format
, const int * const swizzle
)
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
;
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
;
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
;
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
;
1065 return PIPE_FORMAT_NONE
;
1069 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1071 enum pipe_format src
= *src_format
;
1072 enum pipe_format dst
= *dst_format
;
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(). */
1079 if (format_is_alpha(src
)) {
1080 if (!format_is_alpha(dst
))
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
))
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
);
1093 if (format_is_red_alpha(dst
)) {
1094 dst
= red_alpha_to_red_green(dst
);
1095 } else if (!format_is_red(dst
))
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
);
1103 /* Make sure the format is an RGBA and not an RGBX format */
1104 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
)
1107 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
)
1110 for (i
= 0; i
< 4; i
++)
1111 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1113 dst
= swizzle_format(dst
, swizzle
);
1114 if (dst
== PIPE_FORMAT_NONE
)
1117 src
= unswizzle_format(src
);
1126 create_pbo_upload_vs(struct st_context
*st
)
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
;
1134 ureg
= ureg_create(TGSI_PROCESSOR_VERTEX
);
1136 in_pos
= ureg_DECL_vs_input(ureg
, TGSI_SEMANTIC_POSITION
);
1138 out_pos
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
1140 if (st
->pbo_upload
.upload_layers
) {
1141 in_instanceid
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_INSTANCEID
, 0);
1143 if (!st
->pbo_upload
.use_gs
)
1144 out_layer
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_LAYER
, 0);
1147 /* out_pos = in_pos */
1148 ureg_MOV(ureg
, out_pos
, in_pos
);
1150 if (st
->pbo_upload
.upload_layers
) {
1151 if (st
->pbo_upload
.use_gs
) {
1152 /* out_pos.z = i2f(gl_InstanceID) */
1153 ureg_I2F(ureg
, ureg_writemask(out_pos
, TGSI_WRITEMASK_Z
),
1154 ureg_scalar(in_instanceid
, TGSI_SWIZZLE_X
));
1156 /* out_layer = gl_InstanceID */
1157 ureg_MOV(ureg
, out_layer
, in_instanceid
);
1163 return ureg_create_shader_and_destroy(ureg
, st
->pipe
);
1167 create_pbo_upload_gs(struct st_context
*st
)
1169 static const int zero
= 0;
1170 struct ureg_program
*ureg
;
1171 struct ureg_dst out_pos
;
1172 struct ureg_dst out_layer
;
1173 struct ureg_src in_pos
;
1174 struct ureg_src imm
;
1177 ureg
= ureg_create(TGSI_PROCESSOR_GEOMETRY
);
1181 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
, PIPE_PRIM_TRIANGLES
);
1182 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
, PIPE_PRIM_TRIANGLE_STRIP
);
1183 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
, 3);
1185 out_pos
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
1186 out_layer
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_LAYER
, 0);
1188 in_pos
= ureg_DECL_input(ureg
, TGSI_SEMANTIC_POSITION
, 0, 0, 1);
1190 imm
= ureg_DECL_immediate_int(ureg
, &zero
, 1);
1192 for (i
= 0; i
< 3; ++i
) {
1193 struct ureg_src in_pos_vertex
= ureg_src_dimension(in_pos
, i
);
1195 /* out_pos = in_pos[i] */
1196 ureg_MOV(ureg
, out_pos
, in_pos_vertex
);
1198 /* out_layer.x = f2i(in_pos[i].z) */
1199 ureg_F2I(ureg
, ureg_writemask(out_layer
, TGSI_WRITEMASK_X
),
1200 ureg_scalar(in_pos_vertex
, TGSI_SWIZZLE_Z
));
1202 ureg_EMIT(ureg
, ureg_scalar(imm
, TGSI_SWIZZLE_X
));
1207 return ureg_create_shader_and_destroy(ureg
, st
->pipe
);
1211 create_pbo_upload_fs(struct st_context
*st
)
1213 struct pipe_context
*pipe
= st
->pipe
;
1214 struct pipe_screen
*screen
= pipe
->screen
;
1215 struct ureg_program
*ureg
;
1216 struct ureg_dst out
;
1217 struct ureg_src sampler
;
1218 struct ureg_src pos
;
1219 struct ureg_src layer
;
1220 struct ureg_src const0
;
1221 struct ureg_dst temp0
;
1223 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
1227 out
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
1228 sampler
= ureg_DECL_sampler(ureg
, 0);
1229 if (screen
->get_param(screen
, PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL
)) {
1230 pos
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_POSITION
, 0);
1232 pos
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_POSITION
, 0,
1233 TGSI_INTERPOLATE_LINEAR
);
1235 if (st
->pbo_upload
.upload_layers
) {
1236 layer
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_LAYER
, 0,
1237 TGSI_INTERPOLATE_CONSTANT
);
1239 const0
= ureg_DECL_constant(ureg
, 0);
1240 temp0
= ureg_DECL_temporary(ureg
);
1242 /* Note: const0 = [ -xoffset + skip_pixels, -yoffset, stride, image_height ] */
1244 /* temp0.xy = f2i(temp0.xy) */
1245 ureg_F2I(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_XY
),
1247 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_Y
,
1248 TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_Y
));
1250 /* temp0.xy = temp0.xy + const0.xy */
1251 ureg_UADD(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_XY
),
1252 ureg_swizzle(ureg_src(temp0
),
1253 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_Y
,
1254 TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_Y
),
1255 ureg_swizzle(const0
,
1256 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_Y
,
1257 TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_Y
));
1259 /* temp0.x = const0.z * temp0.y + temp0.x */
1260 ureg_UMAD(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_X
),
1261 ureg_scalar(const0
, TGSI_SWIZZLE_Z
),
1262 ureg_scalar(ureg_src(temp0
), TGSI_SWIZZLE_Y
),
1263 ureg_scalar(ureg_src(temp0
), TGSI_SWIZZLE_X
));
1265 if (st
->pbo_upload
.upload_layers
) {
1266 /* temp0.x = const0.w * layer + temp0.x */
1267 ureg_UMAD(ureg
, ureg_writemask(temp0
, TGSI_WRITEMASK_X
),
1268 ureg_scalar(const0
, TGSI_SWIZZLE_W
),
1269 ureg_scalar(layer
, TGSI_SWIZZLE_X
),
1270 ureg_scalar(ureg_src(temp0
), TGSI_SWIZZLE_X
));
1273 /* out = txf(sampler, temp0.x) */
1274 ureg_TXF(ureg
, out
, TGSI_TEXTURE_BUFFER
,
1275 ureg_scalar(ureg_src(temp0
), TGSI_SWIZZLE_X
),
1278 ureg_release_temporary(ureg
, temp0
);
1282 return ureg_create_shader_and_destroy(ureg
, pipe
);
1286 try_pbo_upload_common(struct gl_context
*ctx
,
1287 struct pipe_surface
*surface
,
1288 int xoffset
, int yoffset
,
1289 unsigned upload_width
, unsigned upload_height
,
1290 struct pipe_resource
*buffer
,
1291 enum pipe_format src_format
,
1292 intptr_t buf_offset
,
1293 unsigned bytes_per_pixel
,
1295 unsigned image_height
)
1297 struct st_context
*st
= st_context(ctx
);
1298 struct pipe_context
*pipe
= st
->pipe
;
1299 struct pipe_sampler_view
*sampler_view
= NULL
;
1300 unsigned depth
= surface
->u
.tex
.last_layer
- surface
->u
.tex
.first_layer
+ 1;
1301 unsigned skip_pixels
= 0;
1303 /* Check alignment. */
1305 unsigned ofs
= (buf_offset
* bytes_per_pixel
) % ctx
->Const
.TextureBufferOffsetAlignment
;
1307 if (ofs
% bytes_per_pixel
!= 0)
1310 skip_pixels
= ofs
/ bytes_per_pixel
;
1311 buf_offset
-= skip_pixels
;
1315 /* Create the shaders */
1316 if (!st
->pbo_upload
.vs
) {
1317 st
->pbo_upload
.vs
= create_pbo_upload_vs(st
);
1318 if (!st
->pbo_upload
.vs
)
1322 if (depth
!= 1 && st
->pbo_upload
.use_gs
&& !st
->pbo_upload
.gs
) {
1323 st
->pbo_upload
.gs
= create_pbo_upload_gs(st
);
1324 if (!st
->pbo_upload
.gs
)
1328 if (!st
->pbo_upload
.fs
) {
1329 st
->pbo_upload
.fs
= create_pbo_upload_fs(st
);
1330 if (!st
->pbo_upload
.fs
)
1334 /* Set up the sampler_view */
1336 unsigned first_element
= buf_offset
;
1337 unsigned last_element
= buf_offset
+ skip_pixels
+ upload_width
- 1
1338 + (upload_height
- 1 + (depth
- 1) * image_height
) * stride
;
1339 struct pipe_sampler_view templ
;
1341 /* This should be ensured by Mesa before calling our callbacks */
1342 assert((last_element
+ 1) * bytes_per_pixel
<= buffer
->width0
);
1344 if (last_element
- first_element
> ctx
->Const
.MaxTextureBufferSize
- 1)
1347 memset(&templ
, 0, sizeof(templ
));
1348 templ
.format
= src_format
;
1349 templ
.u
.buf
.first_element
= first_element
;
1350 templ
.u
.buf
.last_element
= last_element
;
1351 templ
.swizzle_r
= PIPE_SWIZZLE_RED
;
1352 templ
.swizzle_g
= PIPE_SWIZZLE_GREEN
;
1353 templ
.swizzle_b
= PIPE_SWIZZLE_BLUE
;
1354 templ
.swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1356 sampler_view
= pipe
->create_sampler_view(pipe
, buffer
, &templ
);
1357 if (sampler_view
== NULL
)
1361 /* Begin setting state. This is the point of no return. */
1362 cso_save_fragment_sampler_views(st
->cso_context
);
1363 cso_set_sampler_views(st
->cso_context
, PIPE_SHADER_FRAGMENT
, 1,
1366 /* Framebuffer_state */
1368 struct pipe_framebuffer_state fb
;
1369 memset(&fb
, 0, sizeof(fb
));
1370 fb
.width
= surface
->width
;
1371 fb
.height
= surface
->height
;
1373 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1375 cso_save_framebuffer(st
->cso_context
);
1376 cso_set_framebuffer(st
->cso_context
, &fb
);
1378 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1381 /* Viewport state */
1383 struct pipe_viewport_state vp
;
1384 vp
.scale
[0] = 0.5f
* surface
->width
;
1385 vp
.scale
[1] = 0.5f
* surface
->height
;
1387 vp
.translate
[0] = 0.5f
* surface
->width
;
1388 vp
.translate
[1] = 0.5f
* surface
->height
;
1389 vp
.translate
[2] = 0.0f
;
1391 cso_save_viewport(st
->cso_context
);
1392 cso_set_viewport(st
->cso_context
, &vp
);
1396 cso_save_blend(st
->cso_context
);
1397 cso_set_blend(st
->cso_context
, &st
->pbo_upload
.blend
);
1399 /* Rasterizer state */
1400 cso_save_rasterizer(st
->cso_context
);
1401 cso_set_rasterizer(st
->cso_context
, &st
->pbo_upload
.raster
);
1403 /* Upload vertices */
1405 struct pipe_vertex_buffer vbo
;
1406 struct pipe_vertex_element velem
;
1408 float x0
= (float) xoffset
/ surface
->width
* 2.0f
- 1.0f
;
1409 float y0
= (float) yoffset
/ surface
->height
* 2.0f
- 1.0f
;
1410 float x1
= (float) (xoffset
+ upload_width
) / surface
->width
* 2.0f
- 1.0f
;
1411 float y1
= (float) (yoffset
+ upload_height
) / surface
->height
* 2.0f
- 1.0f
;
1413 float *verts
= NULL
;
1415 vbo
.user_buffer
= NULL
;
1417 vbo
.stride
= 2 * sizeof(float);
1419 u_upload_alloc(st
->uploader
, 0, 8 * sizeof(float), 4,
1420 &vbo
.buffer_offset
, &vbo
.buffer
, (void **) &verts
);
1431 u_upload_unmap(st
->uploader
);
1433 velem
.src_offset
= 0;
1434 velem
.instance_divisor
= 0;
1435 velem
.vertex_buffer_index
= cso_get_aux_vertex_buffer_slot(st
->cso_context
);
1436 velem
.src_format
= PIPE_FORMAT_R32G32_FLOAT
;
1438 cso_save_vertex_elements(st
->cso_context
);
1439 cso_set_vertex_elements(st
->cso_context
, 1, &velem
);
1441 cso_save_aux_vertex_buffer_slot(st
->cso_context
);
1442 cso_set_vertex_buffers(st
->cso_context
, velem
.vertex_buffer_index
,
1446 /* Upload constants */
1448 struct pipe_constant_buffer cb
;
1457 constants
.xoffset
= -xoffset
+ skip_pixels
;
1458 constants
.yoffset
= -yoffset
;
1459 constants
.stride
= stride
;
1460 constants
.image_size
= stride
* image_height
;
1462 if (st
->constbuf_uploader
) {
1464 cb
.user_buffer
= NULL
;
1465 u_upload_data(st
->constbuf_uploader
, 0, sizeof(constants
),
1466 st
->ctx
->Const
.UniformBufferOffsetAlignment
,
1467 &constants
, &cb
.buffer_offset
, &cb
.buffer
);
1468 u_upload_unmap(st
->constbuf_uploader
);
1471 cb
.user_buffer
= &constants
;
1472 cb
.buffer_offset
= 0;
1474 cb
.buffer_size
= sizeof(constants
);
1476 cso_save_constant_buffer_slot0(st
->cso_context
, PIPE_SHADER_FRAGMENT
);
1477 cso_set_constant_buffer(st
->cso_context
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
1480 /* Set up the shaders */
1481 cso_save_vertex_shader(st
->cso_context
);
1482 cso_set_vertex_shader_handle(st
->cso_context
, st
->pbo_upload
.vs
);
1484 cso_save_geometry_shader(st
->cso_context
);
1485 cso_set_geometry_shader_handle(st
->cso_context
,
1486 depth
!= 1 ? st
->pbo_upload
.gs
: NULL
);
1488 cso_save_tessctrl_shader(st
->cso_context
);
1489 cso_set_tessctrl_shader_handle(st
->cso_context
, NULL
);
1491 cso_save_tesseval_shader(st
->cso_context
);
1492 cso_set_tesseval_shader_handle(st
->cso_context
, NULL
);
1494 cso_save_fragment_shader(st
->cso_context
);
1495 cso_set_fragment_shader_handle(st
->cso_context
, st
->pbo_upload
.fs
);
1497 /* Disable stream output */
1498 cso_save_stream_outputs(st
->cso_context
);
1499 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
1502 cso_draw_arrays(st
->cso_context
, PIPE_PRIM_TRIANGLE_STRIP
, 0, 4);
1504 cso_draw_arrays_instanced(st
->cso_context
, PIPE_PRIM_TRIANGLE_STRIP
,
1508 cso_restore_fragment_sampler_views(st
->cso_context
);
1509 cso_restore_framebuffer(st
->cso_context
);
1510 cso_restore_viewport(st
->cso_context
);
1511 cso_restore_blend(st
->cso_context
);
1512 cso_restore_rasterizer(st
->cso_context
);
1513 cso_restore_vertex_elements(st
->cso_context
);
1514 cso_restore_aux_vertex_buffer_slot(st
->cso_context
);
1515 cso_restore_constant_buffer_slot0(st
->cso_context
, PIPE_SHADER_FRAGMENT
);
1516 cso_restore_vertex_shader(st
->cso_context
);
1517 cso_restore_geometry_shader(st
->cso_context
);
1518 cso_restore_tessctrl_shader(st
->cso_context
);
1519 cso_restore_tesseval_shader(st
->cso_context
);
1520 cso_restore_fragment_shader(st
->cso_context
);
1521 cso_restore_stream_outputs(st
->cso_context
);
1523 pipe_sampler_view_reference(&sampler_view
, NULL
);
1529 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1530 struct gl_texture_image
*texImage
,
1531 GLenum format
, GLenum type
,
1532 enum pipe_format dst_format
,
1533 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1534 GLint width
, GLint height
, GLint depth
,
1536 const struct gl_pixelstore_attrib
*unpack
)
1538 struct st_context
*st
= st_context(ctx
);
1539 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1540 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1541 struct pipe_resource
*texture
= stImage
->pt
;
1542 struct pipe_context
*pipe
= st
->pipe
;
1543 struct pipe_screen
*screen
= pipe
->screen
;
1544 struct pipe_surface
*surface
= NULL
;
1545 enum pipe_format src_format
;
1546 const struct util_format_description
*desc
;
1547 GLenum gl_target
= texImage
->TexObject
->Target
;
1548 intptr_t buf_offset
;
1549 unsigned bytes_per_pixel
;
1550 unsigned stride
, image_height
;
1553 if (!st
->pbo_upload
.enabled
)
1556 /* From now on, we need the gallium representation of dimensions. */
1557 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1564 image_height
= unpack
->ImageHeight
> 0 ? unpack
->ImageHeight
: height
;
1567 if (depth
!= 1 && !st
->pbo_upload
.upload_layers
)
1570 /* Choose the source format. Initially, we do so without checking driver
1571 * support at all because of the remapping we later perform and because
1572 * at least the Radeon driver actually supports some formats for texture
1573 * buffers which it doesn't support for regular textures. */
1574 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1579 src_format
= util_format_linear(src_format
);
1580 desc
= util_format_description(src_format
);
1582 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1585 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1588 if (st
->pbo_upload
.rgba_only
) {
1589 enum pipe_format orig_dst_format
= dst_format
;
1591 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1595 if (dst_format
!= orig_dst_format
&&
1596 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1597 PIPE_BIND_RENDER_TARGET
)) {
1603 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1604 PIPE_BIND_SAMPLER_VIEW
)) {
1608 /* Check if the offset satisfies the alignment requirements */
1609 buf_offset
= (intptr_t) pixels
;
1610 bytes_per_pixel
= desc
->block
.bits
/ 8;
1612 if (buf_offset
% bytes_per_pixel
) {
1616 /* Convert to texels */
1617 buf_offset
= buf_offset
/ bytes_per_pixel
;
1619 /* Compute the stride, taking unpack->Alignment into account */
1621 unsigned pixels_per_row
= unpack
->RowLength
> 0 ?
1622 unpack
->RowLength
: width
;
1623 unsigned bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1624 unsigned remainder
= bytes_per_row
% unpack
->Alignment
;
1625 unsigned offset_rows
;
1628 bytes_per_row
+= (unpack
->Alignment
- remainder
);
1630 if (bytes_per_row
% bytes_per_pixel
) {
1634 stride
= bytes_per_row
/ bytes_per_pixel
;
1636 offset_rows
= unpack
->SkipRows
;
1638 offset_rows
+= image_height
* unpack
->SkipImages
;
1640 buf_offset
+= unpack
->SkipPixels
+ stride
* offset_rows
;
1643 /* Set up the surface */
1645 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1646 unsigned max_layer
= util_max_layer(texture
, level
);
1648 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1650 struct pipe_surface templ
;
1651 memset(&templ
, 0, sizeof(templ
));
1652 templ
.format
= dst_format
;
1653 templ
.u
.tex
.level
= level
;
1654 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1655 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1657 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1662 success
= try_pbo_upload_common(ctx
, surface
,
1663 xoffset
, yoffset
, width
, height
,
1664 st_buffer_object(unpack
->BufferObj
)->buffer
,
1667 bytes_per_pixel
, stride
, image_height
);
1669 pipe_surface_reference(&surface
, NULL
);
1675 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1676 struct gl_texture_image
*texImage
,
1677 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1678 GLint width
, GLint height
, GLint depth
,
1679 GLenum format
, GLenum type
, const void *pixels
,
1680 const struct gl_pixelstore_attrib
*unpack
)
1682 struct st_context
*st
= st_context(ctx
);
1683 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1684 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1685 struct pipe_context
*pipe
= st
->pipe
;
1686 struct pipe_screen
*screen
= pipe
->screen
;
1687 struct pipe_resource
*dst
= stImage
->pt
;
1688 struct pipe_resource
*src
= NULL
;
1689 struct pipe_resource src_templ
;
1690 struct pipe_transfer
*transfer
;
1691 struct pipe_blit_info blit
;
1692 enum pipe_format src_format
, dst_format
;
1693 mesa_format mesa_src_format
;
1694 GLenum gl_target
= texImage
->TexObject
->Target
;
1698 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1699 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1701 if (!st
->prefer_blit_based_texture_transfer
) {
1709 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1710 * blit implementation in some drivers. */
1711 if (format
== GL_DEPTH_STENCIL
) {
1715 /* If the base internal format and the texture format don't match,
1716 * we can't use blit-based TexSubImage. */
1717 if (texImage
->_BaseFormat
!=
1718 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1723 /* See if the destination format is supported. */
1724 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1725 bind
= PIPE_BIND_DEPTH_STENCIL
;
1727 bind
= PIPE_BIND_RENDER_TARGET
;
1729 /* For luminance and intensity, only the red channel is stored
1730 * in the destination. */
1731 dst_format
= util_format_linear(dst
->format
);
1732 dst_format
= util_format_luminance_to_red(dst_format
);
1733 dst_format
= util_format_intensity_to_red(dst_format
);
1736 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1737 dst
->nr_samples
, bind
)) {
1741 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1742 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1743 xoffset
, yoffset
, zoffset
,
1744 width
, height
, depth
, pixels
, unpack
))
1748 /* See if the texture format already matches the format and type,
1749 * in which case the memcpy-based fast path will likely be used and
1750 * we don't have to blit. */
1751 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1752 type
, unpack
->SwapBytes
, NULL
)) {
1756 /* Choose the source format. */
1757 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1758 format
, type
, unpack
->SwapBytes
);
1763 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1765 /* There is no reason to do this if we cannot use memcpy for the temporary
1766 * source texture at least. This also takes transfer ops into account,
1768 if (!_mesa_texstore_can_use_memcpy(ctx
,
1769 _mesa_get_format_base_format(mesa_src_format
),
1770 mesa_src_format
, format
, type
, unpack
)) {
1774 /* TexSubImage only sets a single cubemap face. */
1775 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1776 gl_target
= GL_TEXTURE_2D
;
1778 /* TexSubImage can specify subsets of cube map array faces
1779 * so we need to upload via 2D array instead */
1780 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1781 gl_target
= GL_TEXTURE_2D_ARRAY
;
1784 /* Initialize the source texture description. */
1785 memset(&src_templ
, 0, sizeof(src_templ
));
1786 src_templ
.target
= gl_target_to_pipe(gl_target
);
1787 src_templ
.format
= src_format
;
1788 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1789 src_templ
.usage
= PIPE_USAGE_STAGING
;
1791 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1792 &src_templ
.width0
, &src_templ
.height0
,
1793 &src_templ
.depth0
, &src_templ
.array_size
);
1795 /* Check for NPOT texture support. */
1796 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1797 (!util_is_power_of_two(src_templ
.width0
) ||
1798 !util_is_power_of_two(src_templ
.height0
) ||
1799 !util_is_power_of_two(src_templ
.depth0
))) {
1803 /* Create the source texture. */
1804 src
= screen
->resource_create(screen
, &src_templ
);
1809 /* Map source pixels. */
1810 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1811 format
, type
, pixels
, unpack
,
1814 /* This is a GL error. */
1815 pipe_resource_reference(&src
, NULL
);
1819 /* From now on, we need the gallium representation of dimensions. */
1820 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1827 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1828 width
, height
, depth
, &transfer
);
1830 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1831 pipe_resource_reference(&src
, NULL
);
1835 /* Upload pixels (just memcpy). */
1837 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1840 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1841 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1842 /* 1D array textures.
1843 * We need to convert gallium coords to GL coords.
1845 GLvoid
*src
= _mesa_image_address2d(unpack
, pixels
,
1846 width
, depth
, format
,
1848 memcpy(map
, src
, bytesPerRow
);
1851 ubyte
*slice_map
= map
;
1853 for (row
= 0; row
< (unsigned) height
; row
++) {
1854 GLvoid
*src
= _mesa_image_address(dims
, unpack
, pixels
,
1855 width
, height
, format
,
1856 type
, slice
, row
, 0);
1857 memcpy(slice_map
, src
, bytesPerRow
);
1858 slice_map
+= transfer
->stride
;
1861 map
+= transfer
->layer_stride
;
1865 pipe_transfer_unmap(pipe
, transfer
);
1866 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1869 memset(&blit
, 0, sizeof(blit
));
1870 blit
.src
.resource
= src
;
1872 blit
.src
.format
= src_format
;
1873 blit
.dst
.resource
= dst
;
1874 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1875 blit
.dst
.format
= dst_format
;
1876 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1877 blit
.dst
.box
.x
= xoffset
;
1878 blit
.dst
.box
.y
= yoffset
;
1879 blit
.dst
.box
.z
= zoffset
+ texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1880 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1881 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1882 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1883 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1884 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1885 blit
.scissor_enable
= FALSE
;
1887 st
->pipe
->blit(st
->pipe
, &blit
);
1889 pipe_resource_reference(&src
, NULL
);
1893 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1894 width
, height
, depth
, format
, type
, pixels
,
1899 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1900 struct gl_texture_image
*texImage
,
1901 GLenum format
, GLenum type
, const void *pixels
,
1902 const struct gl_pixelstore_attrib
*unpack
)
1904 assert(dims
== 1 || dims
== 2 || dims
== 3);
1906 prep_teximage(ctx
, texImage
, format
, type
);
1908 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1911 /* allocate storage for texture data */
1912 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1913 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1917 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1918 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1919 format
, type
, pixels
, unpack
);
1924 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1925 struct gl_texture_image
*texImage
,
1926 GLint x
, GLint y
, GLint z
,
1927 GLsizei w
, GLsizei h
, GLsizei d
,
1928 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
1930 struct st_context
*st
= st_context(ctx
);
1931 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1932 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1933 struct pipe_resource
*texture
= stImage
->pt
;
1934 struct pipe_context
*pipe
= st
->pipe
;
1935 struct pipe_screen
*screen
= pipe
->screen
;
1936 struct pipe_resource
*dst
= stImage
->pt
;
1937 struct pipe_surface
*surface
= NULL
;
1938 struct compressed_pixelstore store
;
1939 enum pipe_format copy_format
;
1940 unsigned bytes_per_block
;
1942 intptr_t buf_offset
;
1943 bool success
= false;
1945 /* Check basic pre-conditions for PBO upload */
1946 if (!st
->prefer_blit_based_texture_transfer
) {
1950 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1953 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
1954 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
1955 /* ETC isn't supported and is represented by uncompressed formats. */
1963 if (!st
->pbo_upload
.enabled
||
1964 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1968 /* Choose the pipe format for the upload. */
1969 bytes_per_block
= util_format_get_blocksize(dst
->format
);
1970 bw
= util_format_get_blockwidth(dst
->format
);
1971 bh
= util_format_get_blockheight(dst
->format
);
1973 switch (bytes_per_block
) {
1975 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1978 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1984 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1985 PIPE_BIND_SAMPLER_VIEW
)) {
1989 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1990 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1994 /* Interpret the pixelstore settings. */
1995 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1996 &ctx
->Unpack
, &store
);
1997 assert(store
.CopyBytesPerRow
% bytes_per_block
== 0);
1998 assert(store
.SkipBytes
% bytes_per_block
== 0);
2000 /* Compute the offset into the buffer */
2001 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
2003 if (buf_offset
% bytes_per_block
) {
2007 buf_offset
= buf_offset
/ bytes_per_block
;
2009 /* Set up the surface. */
2011 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
2012 unsigned max_layer
= util_max_layer(texture
, level
);
2014 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
2016 struct pipe_surface templ
;
2017 memset(&templ
, 0, sizeof(templ
));
2018 templ
.format
= copy_format
;
2019 templ
.u
.tex
.level
= level
;
2020 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
2021 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
2023 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
2028 success
= try_pbo_upload_common(ctx
, surface
,
2030 store
.CopyBytesPerRow
/ bytes_per_block
,
2031 store
.CopyRowsPerSlice
,
2032 st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
2036 store
.TotalBytesPerRow
/ bytes_per_block
,
2037 store
.TotalRowsPerSlice
);
2039 pipe_surface_reference(&surface
, NULL
);
2045 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
2047 format
, imageSize
, data
);
2051 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
2052 struct gl_texture_image
*texImage
,
2053 GLsizei imageSize
, const GLvoid
*data
)
2055 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
2057 /* only 2D and 3D compressed images are supported at this time */
2059 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
2063 /* This is pretty simple, because unlike the general texstore path we don't
2064 * have to worry about the usual image unpacking or image transfer
2068 assert(texImage
->Width
> 0);
2069 assert(texImage
->Height
> 0);
2070 assert(texImage
->Depth
> 0);
2072 /* allocate storage for texture data */
2073 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
2074 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
2078 st_CompressedTexSubImage(ctx
, dims
, texImage
,
2080 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
2081 texImage
->TexFormat
,
2089 * Called via ctx->Driver.GetTexSubImage()
2091 * This uses a blit to copy the texture to a texture format which matches
2092 * the format and type combo and then a fast read-back is done using memcpy.
2093 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
2094 * a format which matches the swizzling.
2096 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
2098 * NOTE: Drivers usually do a blit to convert between tiled and linear
2099 * texture layouts during texture uploads/downloads, so the blit
2100 * we do here should be free in such cases.
2103 st_GetTexSubImage(struct gl_context
* ctx
,
2104 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2105 GLsizei width
, GLsizei height
, GLint depth
,
2106 GLenum format
, GLenum type
, GLvoid
* pixels
,
2107 struct gl_texture_image
*texImage
)
2109 struct st_context
*st
= st_context(ctx
);
2110 struct pipe_context
*pipe
= st
->pipe
;
2111 struct pipe_screen
*screen
= pipe
->screen
;
2112 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2113 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2114 struct pipe_resource
*src
= stObj
->pt
;
2115 struct pipe_resource
*dst
= NULL
;
2116 struct pipe_resource dst_templ
;
2117 enum pipe_format dst_format
, src_format
;
2118 mesa_format mesa_format
;
2119 GLenum gl_target
= texImage
->TexObject
->Target
;
2120 enum pipe_texture_target pipe_target
;
2121 struct pipe_blit_info blit
;
2122 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
2123 struct pipe_transfer
*tex_xfer
;
2125 boolean done
= FALSE
;
2127 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2128 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2130 if (!st
->prefer_blit_based_texture_transfer
&&
2131 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
2132 /* Try to avoid the fallback if we're doing texture decompression here */
2136 if (!stImage
->pt
|| !src
) {
2140 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
2141 * due to an incomplete stencil blit implementation in some drivers. */
2142 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
2146 /* If the base internal format and the texture format don't match, we have
2147 * to fall back to _mesa_GetTexImage_sw. */
2148 if (texImage
->_BaseFormat
!=
2149 _mesa_get_format_base_format(texImage
->TexFormat
)) {
2153 /* See if the texture format already matches the format and type,
2154 * in which case the memcpy-based fast path will be used. */
2155 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
2156 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
2160 /* Convert the source format to what is expected by GetTexImage
2161 * and see if it's supported.
2163 * This only applies to glGetTexImage:
2164 * - Luminance must be returned as (L,0,0,1).
2165 * - Luminance alpha must be returned as (L,0,0,A).
2166 * - Intensity must be returned as (I,0,0,1)
2168 if (stObj
->surface_based
)
2169 src_format
= util_format_linear(stObj
->surface_format
);
2171 src_format
= util_format_linear(src
->format
);
2172 src_format
= util_format_luminance_to_red(src_format
);
2173 src_format
= util_format_intensity_to_red(src_format
);
2176 !screen
->is_format_supported(screen
, src_format
, src
->target
,
2178 PIPE_BIND_SAMPLER_VIEW
)) {
2182 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
2183 bind
|= PIPE_BIND_DEPTH_STENCIL
;
2185 bind
|= PIPE_BIND_RENDER_TARGET
;
2187 /* GetTexImage only returns a single face for cubemaps. */
2188 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
2189 gl_target
= GL_TEXTURE_2D
;
2191 pipe_target
= gl_target_to_pipe(gl_target
);
2193 /* Choose the destination format by finding the best match
2194 * for the format+type combo. */
2195 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
2196 ctx
->Pack
.SwapBytes
);
2198 if (dst_format
== PIPE_FORMAT_NONE
) {
2199 GLenum dst_glformat
;
2201 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
2202 * where decompression with a blit is always preferred. */
2203 if (!util_format_is_compressed(src
->format
)) {
2207 /* Set the appropriate format for the decompressed texture.
2208 * Luminance and sRGB formats shouldn't appear here.*/
2209 switch (src_format
) {
2210 case PIPE_FORMAT_DXT1_RGB
:
2211 case PIPE_FORMAT_DXT1_RGBA
:
2212 case PIPE_FORMAT_DXT3_RGBA
:
2213 case PIPE_FORMAT_DXT5_RGBA
:
2214 case PIPE_FORMAT_RGTC1_UNORM
:
2215 case PIPE_FORMAT_RGTC2_UNORM
:
2216 case PIPE_FORMAT_ETC1_RGB8
:
2217 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
2218 dst_glformat
= GL_RGBA8
;
2220 case PIPE_FORMAT_RGTC1_SNORM
:
2221 case PIPE_FORMAT_RGTC2_SNORM
:
2222 if (!ctx
->Extensions
.EXT_texture_snorm
)
2224 dst_glformat
= GL_RGBA8_SNORM
;
2226 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
2227 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
2228 if (!ctx
->Extensions
.ARB_texture_float
)
2230 dst_glformat
= GL_RGBA32F
;
2237 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
2238 pipe_target
, 0, bind
, FALSE
);
2240 if (dst_format
== PIPE_FORMAT_NONE
) {
2241 /* unable to get an rgba format!?! */
2246 /* create the destination texture of size (width X height X depth) */
2247 memset(&dst_templ
, 0, sizeof(dst_templ
));
2248 dst_templ
.target
= pipe_target
;
2249 dst_templ
.format
= dst_format
;
2250 dst_templ
.bind
= bind
;
2251 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2253 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2254 &dst_templ
.width0
, &dst_templ
.height0
,
2255 &dst_templ
.depth0
, &dst_templ
.array_size
);
2257 dst
= screen
->resource_create(screen
, &dst_templ
);
2262 /* From now on, we need the gallium representation of dimensions. */
2263 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2270 assert(texImage
->Face
== 0 ||
2271 texImage
->TexObject
->MinLayer
== 0 ||
2274 memset(&blit
, 0, sizeof(blit
));
2275 blit
.src
.resource
= src
;
2276 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2277 blit
.src
.format
= src_format
;
2278 blit
.dst
.resource
= dst
;
2280 blit
.dst
.format
= dst
->format
;
2281 blit
.src
.box
.x
= xoffset
;
2283 blit
.src
.box
.y
= yoffset
;
2285 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2287 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2288 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2289 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2290 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2291 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2292 blit
.scissor_enable
= FALSE
;
2294 /* blit/render/decompress */
2295 st
->pipe
->blit(st
->pipe
, &blit
);
2297 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2299 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2300 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2305 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2307 /* copy/pack data into user buffer */
2308 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2309 ctx
->Pack
.SwapBytes
, NULL
)) {
2311 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2314 for (slice
= 0; slice
< depth
; slice
++) {
2315 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2316 /* 1D array textures.
2317 * We need to convert gallium coords to GL coords.
2319 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2320 width
, depth
, format
,
2322 memcpy(dest
, map
, bytesPerRow
);
2325 ubyte
*slice_map
= map
;
2327 for (row
= 0; row
< height
; row
++) {
2328 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2329 width
, height
, format
,
2330 type
, slice
, row
, 0);
2331 memcpy(dest
, slice_map
, bytesPerRow
);
2332 slice_map
+= tex_xfer
->stride
;
2335 map
+= tex_xfer
->layer_stride
;
2339 /* format translation via floats */
2342 uint32_t dstMesaFormat
;
2343 int dstStride
, srcStride
;
2345 assert(util_format_is_compressed(src
->format
));
2347 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
2352 if (ST_DEBUG
& DEBUG_FALLBACK
)
2353 debug_printf("%s: fallback format translation\n", __func__
);
2355 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2356 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2357 srcStride
= 4 * width
* sizeof(GLfloat
);
2358 for (slice
= 0; slice
< depth
; slice
++) {
2359 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2360 /* 1D array textures.
2361 * We need to convert gallium coords to GL coords.
2363 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2364 width
, depth
, format
,
2367 /* get float[4] rgba row from surface */
2368 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
2371 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2372 rgba
, RGBA32_FLOAT
, srcStride
,
2376 for (row
= 0; row
< height
; row
++) {
2377 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2378 width
, height
, format
,
2379 type
, slice
, row
, 0);
2381 /* get float[4] rgba row from surface */
2382 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
2385 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2386 rgba
, RGBA32_FLOAT
, srcStride
,
2390 map
+= tex_xfer
->layer_stride
;
2399 pipe_transfer_unmap(pipe
, tex_xfer
);
2401 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2402 pipe_resource_reference(&dst
, NULL
);
2406 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2407 width
, height
, depth
,
2408 format
, type
, pixels
, texImage
);
2414 * Do a CopyTexSubImage operation using a read transfer from the source,
2415 * a write transfer to the destination and get_tile()/put_tile() to access
2416 * the pixels/texels.
2418 * Note: srcY=0=TOP of renderbuffer
2421 fallback_copy_texsubimage(struct gl_context
*ctx
,
2422 struct st_renderbuffer
*strb
,
2423 struct st_texture_image
*stImage
,
2425 GLint destX
, GLint destY
, GLint slice
,
2426 GLint srcX
, GLint srcY
,
2427 GLsizei width
, GLsizei height
)
2429 struct st_context
*st
= st_context(ctx
);
2430 struct pipe_context
*pipe
= st
->pipe
;
2431 struct pipe_transfer
*src_trans
;
2433 enum pipe_transfer_usage transfer_usage
;
2435 unsigned dst_width
= width
;
2436 unsigned dst_height
= height
;
2437 unsigned dst_depth
= 1;
2438 struct pipe_transfer
*transfer
;
2440 if (ST_DEBUG
& DEBUG_FALLBACK
)
2441 debug_printf("%s: fallback processing\n", __func__
);
2443 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2444 srcY
= strb
->Base
.Height
- srcY
- height
;
2447 map
= pipe_transfer_map(pipe
,
2449 strb
->surface
->u
.tex
.level
,
2450 strb
->surface
->u
.tex
.first_layer
,
2453 width
, height
, &src_trans
);
2455 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2456 baseFormat
== GL_DEPTH_STENCIL
) &&
2457 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2458 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2460 transfer_usage
= PIPE_TRANSFER_WRITE
;
2462 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2463 destX
, destY
, slice
,
2464 dst_width
, dst_height
, dst_depth
,
2467 if (baseFormat
== GL_DEPTH_COMPONENT
||
2468 baseFormat
== GL_DEPTH_STENCIL
) {
2469 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2470 ctx
->Pixel
.DepthBias
!= 0.0F
);
2474 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2475 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2484 data
= malloc(width
* sizeof(uint
));
2487 /* To avoid a large temp memory allocation, do copy row by row */
2488 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2489 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2491 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2494 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2495 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2496 0, 0, width
, 1, data
);
2499 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2504 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2512 malloc(width
* height
* 4 * sizeof(GLfloat
));
2514 if (tempSrc
&& texDest
) {
2515 const GLint dims
= 2;
2517 struct gl_texture_image
*texImage
= &stImage
->base
;
2518 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2520 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2521 unpack
.Invert
= GL_TRUE
;
2524 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2525 dstRowStride
= transfer
->layer_stride
;
2528 dstRowStride
= transfer
->stride
;
2531 /* get float/RGBA image from framebuffer */
2532 /* XXX this usually involves a lot of int/float conversion.
2533 * try to avoid that someday.
2535 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2536 util_format_linear(strb
->texture
->format
),
2539 /* Store into texture memory.
2540 * Note that this does some special things such as pixel transfer
2541 * ops and format conversion. In particular, if the dest tex format
2542 * is actually RGBA but the user created the texture as GL_RGB we
2543 * need to fill-in/override the alpha channel with 1.0.
2545 _mesa_texstore(ctx
, dims
,
2546 texImage
->_BaseFormat
,
2547 texImage
->TexFormat
,
2551 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2555 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2561 st_texture_image_unmap(st
, stImage
, slice
);
2562 pipe
->transfer_unmap(pipe
, src_trans
);
2567 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2568 * Note that the region to copy has already been clipped so we know we
2569 * won't read from outside the source renderbuffer's bounds.
2571 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2574 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2575 struct gl_texture_image
*texImage
,
2576 GLint destX
, GLint destY
, GLint slice
,
2577 struct gl_renderbuffer
*rb
,
2578 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2580 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2581 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2582 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2583 struct st_context
*st
= st_context(ctx
);
2584 struct pipe_context
*pipe
= st
->pipe
;
2585 struct pipe_screen
*screen
= pipe
->screen
;
2586 struct pipe_blit_info blit
;
2587 enum pipe_format dst_format
;
2588 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2592 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2593 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2595 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2596 debug_printf("%s: null strb or stImage\n", __func__
);
2600 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2601 texImage
->TexFormat
)) {
2605 /* The base internal format must match the mesa format, so make sure
2606 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2608 if (texImage
->_BaseFormat
!=
2609 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2610 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2614 /* Choose the destination format to match the TexImage behavior. */
2615 dst_format
= util_format_linear(stImage
->pt
->format
);
2616 dst_format
= util_format_luminance_to_red(dst_format
);
2617 dst_format
= util_format_intensity_to_red(dst_format
);
2619 /* See if the destination format is supported. */
2620 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2621 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2622 bind
= PIPE_BIND_DEPTH_STENCIL
;
2625 bind
= PIPE_BIND_RENDER_TARGET
;
2629 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2630 stImage
->pt
->nr_samples
, bind
)) {
2634 /* Y flipping for the main framebuffer. */
2636 srcY1
= strb
->Base
.Height
- srcY
- height
;
2637 srcY0
= srcY1
+ height
;
2641 srcY1
= srcY0
+ height
;
2644 /* Blit the texture.
2645 * This supports flipping, format conversions, and downsampling.
2647 memset(&blit
, 0, sizeof(blit
));
2648 blit
.src
.resource
= strb
->texture
;
2649 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2650 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2651 blit
.src
.box
.x
= srcX
;
2652 blit
.src
.box
.y
= srcY0
;
2653 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2654 blit
.src
.box
.width
= width
;
2655 blit
.src
.box
.height
= srcY1
- srcY0
;
2656 blit
.src
.box
.depth
= 1;
2657 blit
.dst
.resource
= stImage
->pt
;
2658 blit
.dst
.format
= dst_format
;
2659 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2660 blit
.dst
.box
.x
= destX
;
2661 blit
.dst
.box
.y
= destY
;
2662 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2663 blit
.dst
.box
.width
= width
;
2664 blit
.dst
.box
.height
= height
;
2665 blit
.dst
.box
.depth
= 1;
2666 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2667 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2668 pipe
->blit(pipe
, &blit
);
2672 /* software fallback */
2673 fallback_copy_texsubimage(ctx
,
2674 strb
, stImage
, texImage
->_BaseFormat
,
2675 destX
, destY
, slice
,
2676 srcX
, srcY
, width
, height
);
2681 * Copy image data from stImage into the texture object 'stObj' at level
2685 copy_image_data_to_texture(struct st_context
*st
,
2686 struct st_texture_object
*stObj
,
2688 struct st_texture_image
*stImage
)
2692 const struct gl_texture_image
*dstImage
=
2693 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2695 assert(dstImage
->Width
== stImage
->base
.Width
);
2696 assert(dstImage
->Height
== stImage
->base
.Height
);
2697 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2701 /* Copy potentially with the blitter:
2704 if (stImage
->pt
->last_level
== 0)
2707 src_level
= stImage
->base
.Level
;
2709 assert(src_level
<= stImage
->pt
->last_level
);
2710 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2711 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2712 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2713 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2714 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2715 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2717 st_texture_image_copy(st
->pipe
,
2718 stObj
->pt
, dstLevel
, /* dest texture, level */
2719 stImage
->pt
, src_level
, /* src texture, level */
2720 stImage
->base
.Face
);
2722 pipe_resource_reference(&stImage
->pt
, NULL
);
2724 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2729 * Called during state validation. When this function is finished,
2730 * the texture object should be ready for rendering.
2731 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2734 st_finalize_texture(struct gl_context
*ctx
,
2735 struct pipe_context
*pipe
,
2736 struct gl_texture_object
*tObj
)
2738 struct st_context
*st
= st_context(ctx
);
2739 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2740 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
2742 const struct st_texture_image
*firstImage
;
2743 enum pipe_format firstImageFormat
;
2744 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2746 if (tObj
->Immutable
)
2749 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2750 /* The texture is complete and we know exactly how many mipmap levels
2751 * are present/needed. This is conditional because we may be called
2752 * from the st_generate_mipmap() function when the texture object is
2753 * incomplete. In that case, we'll have set stObj->lastLevel before
2756 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2757 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2758 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2760 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2763 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2764 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2767 pipe_resource_reference(&stObj
->pt
, NULL
);
2768 st_texture_release_all_sampler_views(st
, stObj
);
2772 if (st_obj
->buffer
!= stObj
->pt
) {
2773 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2774 st_texture_release_all_sampler_views(st
, stObj
);
2775 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
2783 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
2786 /* If both firstImage and stObj point to a texture which can contain
2787 * all active images, favour firstImage. Note that because of the
2788 * completeness requirement, we know that the image dimensions
2791 if (firstImage
->pt
&&
2792 firstImage
->pt
!= stObj
->pt
&&
2793 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2794 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2795 st_texture_release_all_sampler_views(st
, stObj
);
2798 /* If this texture comes from a window system, there is nothing else to do. */
2799 if (stObj
->surface_based
) {
2803 /* Find gallium format for the Mesa texture */
2805 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2807 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2809 GLuint width
, height
, depth
;
2810 if (!guess_base_level_size(stObj
->base
.Target
,
2811 firstImage
->base
.Width2
,
2812 firstImage
->base
.Height2
,
2813 firstImage
->base
.Depth2
,
2814 firstImage
->base
.Level
,
2815 &width
, &height
, &depth
)) {
2816 width
= stObj
->width0
;
2817 height
= stObj
->height0
;
2818 depth
= stObj
->depth0
;
2820 /* The width/height/depth may have been previously reset in
2821 * guess_and_alloc_texture. */
2822 stObj
->width0
= width
;
2823 stObj
->height0
= height
;
2824 stObj
->depth0
= depth
;
2826 /* convert GL dims to Gallium dims */
2827 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
2828 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2829 ptNumSamples
= firstImage
->base
.NumSamples
;
2832 /* If we already have a gallium texture, check that it matches the texture
2833 * object's format, target, size, num_levels, etc.
2836 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2837 stObj
->pt
->format
!= firstImageFormat
||
2838 stObj
->pt
->last_level
< stObj
->lastLevel
||
2839 stObj
->pt
->width0
!= ptWidth
||
2840 stObj
->pt
->height0
!= ptHeight
||
2841 stObj
->pt
->depth0
!= ptDepth
||
2842 stObj
->pt
->nr_samples
!= ptNumSamples
||
2843 stObj
->pt
->array_size
!= ptLayers
)
2845 /* The gallium texture does not match the Mesa texture so delete the
2846 * gallium texture now. We'll make a new one below.
2848 pipe_resource_reference(&stObj
->pt
, NULL
);
2849 st_texture_release_all_sampler_views(st
, stObj
);
2850 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
2854 /* May need to create a new gallium texture:
2857 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2859 stObj
->pt
= st_texture_create(st
,
2860 gl_target_to_pipe(stObj
->base
.Target
),
2866 ptLayers
, ptNumSamples
,
2870 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2875 /* Pull in any images not in the object's texture:
2877 for (face
= 0; face
< nr_faces
; face
++) {
2879 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2880 struct st_texture_image
*stImage
=
2881 st_texture_image(stObj
->base
.Image
[face
][level
]);
2883 /* Need to import images in main memory or held in other textures.
2885 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2887 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
2888 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
2889 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
2890 /* src image fits expected dest mipmap level size */
2891 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2902 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2903 * for a whole mipmap stack.
2906 st_AllocTextureStorage(struct gl_context
*ctx
,
2907 struct gl_texture_object
*texObj
,
2908 GLsizei levels
, GLsizei width
,
2909 GLsizei height
, GLsizei depth
)
2911 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2912 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2913 struct st_context
*st
= st_context(ctx
);
2914 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2915 struct pipe_screen
*screen
= st
->pipe
->screen
;
2916 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
2917 enum pipe_format fmt
;
2919 GLuint num_samples
= texImage
->NumSamples
;
2923 /* Save the level=0 dimensions */
2924 stObj
->width0
= width
;
2925 stObj
->height0
= height
;
2926 stObj
->depth0
= depth
;
2927 stObj
->lastLevel
= levels
- 1;
2929 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2931 bindings
= default_bindings(st
, fmt
);
2933 /* Raise the sample count if the requested one is unsupported. */
2934 if (num_samples
> 1) {
2935 boolean found
= FALSE
;
2937 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2938 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2940 PIPE_BIND_SAMPLER_VIEW
)) {
2941 /* Update the sample count in gl_texture_image as well. */
2942 texImage
->NumSamples
= num_samples
;
2953 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2954 width
, height
, depth
,
2955 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2957 stObj
->pt
= st_texture_create(st
,
2958 gl_target_to_pipe(texObj
->Target
),
2964 ptLayers
, num_samples
,
2969 /* Set image resource pointers */
2970 for (level
= 0; level
< levels
; level
++) {
2972 for (face
= 0; face
< numFaces
; face
++) {
2973 struct st_texture_image
*stImage
=
2974 st_texture_image(texObj
->Image
[face
][level
]);
2975 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2984 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2985 GLint level
, mesa_format format
,
2986 GLint width
, GLint height
,
2987 GLint depth
, GLint border
)
2989 struct st_context
*st
= st_context(ctx
);
2990 struct pipe_context
*pipe
= st
->pipe
;
2992 if (width
== 0 || height
== 0 || depth
== 0) {
2993 /* zero-sized images are legal, and always fit! */
2997 if (pipe
->screen
->can_create_resource
) {
2998 /* Ask the gallium driver if the texture is too large */
2999 struct gl_texture_object
*texObj
=
3000 _mesa_get_current_tex_object(ctx
, target
);
3001 struct pipe_resource pt
;
3003 /* Setup the pipe_resource object
3005 memset(&pt
, 0, sizeof(pt
));
3007 pt
.target
= gl_target_to_pipe(target
);
3008 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
3010 st_gl_texture_dims_to_pipe_dims(target
,
3011 width
, height
, depth
,
3012 &pt
.width0
, &pt
.height0
,
3013 &pt
.depth0
, &pt
.array_size
);
3015 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
3016 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
3017 /* assume just one mipmap level */
3021 /* assume a full set of mipmaps */
3022 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
3025 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
3028 /* Use core Mesa fallback */
3029 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
3030 width
, height
, depth
, border
);
3035 st_TextureView(struct gl_context
*ctx
,
3036 struct gl_texture_object
*texObj
,
3037 struct gl_texture_object
*origTexObj
)
3039 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
3040 struct st_texture_object
*tex
= st_texture_object(texObj
);
3041 struct gl_texture_image
*image
= texObj
->Image
[0][0];
3043 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
3044 const int numLevels
= texObj
->NumLevels
;
3049 pipe_resource_reference(&tex
->pt
, orig
->pt
);
3051 /* Set image resource pointers */
3052 for (level
= 0; level
< numLevels
; level
++) {
3053 for (face
= 0; face
< numFaces
; face
++) {
3054 struct st_texture_image
*stImage
=
3055 st_texture_image(texObj
->Image
[face
][level
]);
3056 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
3060 tex
->surface_based
= GL_TRUE
;
3061 tex
->surface_format
=
3062 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
3064 tex
->width0
= image
->Width
;
3065 tex
->height0
= image
->Height
;
3066 tex
->depth0
= image
->Depth
;
3067 tex
->lastLevel
= numLevels
- 1;
3073 st_ClearTexSubImage(struct gl_context
*ctx
,
3074 struct gl_texture_image
*texImage
,
3075 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3076 GLsizei width
, GLsizei height
, GLsizei depth
,
3077 const GLvoid
*clearValue
)
3079 static const char zeros
[16] = {0};
3080 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3081 struct pipe_resource
*pt
= stImage
->pt
;
3082 struct st_context
*st
= st_context(ctx
);
3083 struct pipe_context
*pipe
= st
->pipe
;
3084 unsigned level
= texImage
->Level
;
3085 struct pipe_box box
;
3090 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3091 width
, height
, depth
, &box
);
3092 if (texImage
->TexObject
->Immutable
) {
3093 level
+= texImage
->TexObject
->MinLevel
;
3094 box
.z
+= texImage
->TexObject
->MinLayer
;
3097 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3101 st_init_texture_functions(struct dd_function_table
*functions
)
3103 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3104 functions
->QuerySamplesForFormat
= st_QuerySamplesForFormat
;
3105 functions
->TexImage
= st_TexImage
;
3106 functions
->TexSubImage
= st_TexSubImage
;
3107 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3108 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3109 functions
->GenerateMipmap
= st_generate_mipmap
;
3111 functions
->GetTexSubImage
= st_GetTexSubImage
;
3113 /* compressed texture functions */
3114 functions
->CompressedTexImage
= st_CompressedTexImage
;
3115 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
3117 functions
->NewTextureObject
= st_NewTextureObject
;
3118 functions
->NewTextureImage
= st_NewTextureImage
;
3119 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3120 functions
->DeleteTexture
= st_DeleteTextureObject
;
3121 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3122 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3123 functions
->MapTextureImage
= st_MapTextureImage
;
3124 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3126 /* XXX Temporary until we can query pipe's texture sizes */
3127 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3129 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3130 functions
->TextureView
= st_TextureView
;
3131 functions
->ClearTexSubImage
= st_ClearTexSubImage
;