1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 #include "st_context.h"
31 #include "st_format.h"
32 #include "st_texture.h"
33 #include "st_cb_fbo.h"
34 #include "main/enums.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "util/u_inlines.h"
40 #include "util/u_format.h"
41 #include "util/u_rect.h"
42 #include "util/u_math.h"
45 #define DBG if(0) printf
49 * Allocate a new pipe_resource object
50 * width0, height0, depth0 are the dimensions of the level 0 image
51 * (the highest resolution). last_level indicates how many mipmap levels
52 * to allocate storage for. For non-mipmapped textures, this will be zero.
54 struct pipe_resource
*
55 st_texture_create(struct st_context
*st
,
56 enum pipe_texture_target target
,
57 enum pipe_format format
,
64 struct pipe_resource pt
, *newtex
;
65 struct pipe_screen
*screen
= st
->pipe
->screen
;
67 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
72 DBG("%s target %s format %s last_level %d\n", __FUNCTION__
,
73 _mesa_lookup_enum_by_nr(target
),
74 _mesa_lookup_enum_by_nr(format
), last_level
);
77 assert(screen
->is_format_supported(screen
, format
, target
, 0,
78 PIPE_BIND_SAMPLER_VIEW
, 0));
80 memset(&pt
, 0, sizeof(pt
));
83 pt
.last_level
= last_level
;
87 pt
.usage
= PIPE_USAGE_DEFAULT
;
91 newtex
= screen
->resource_create(screen
, &pt
);
93 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
100 * Check if a texture image can be pulled into a unified mipmap texture.
103 st_texture_match_image(const struct pipe_resource
*pt
,
104 const struct gl_texture_image
*image
,
105 GLuint face
, GLuint level
)
107 /* Images with borders are never pulled into mipmap textures.
112 /* Check if this image's format matches the established texture's format.
114 if (st_mesa_format_to_pipe_format(image
->TexFormat
) != pt
->format
)
117 /* Test if this image's size matches what's expected in the
118 * established texture.
120 if (image
->Width
!= u_minify(pt
->width0
, level
) ||
121 image
->Height
!= u_minify(pt
->height0
, level
) ||
122 image
->Depth
!= u_minify(pt
->depth0
, level
))
130 * Map a texture image and return the address for a particular 2D face/slice/
131 * layer. The stImage indicates the cube face and mipmap level. The slice
132 * of the 3D texture is passed in 'zoffset'.
133 * \param usage one of the PIPE_TRANSFER_x values
134 * \param x, y, w, h the region of interest of the 2D image.
135 * \return address of mapping or NULL if any error
138 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
139 GLuint zoffset
, enum pipe_transfer_usage usage
,
140 GLuint x
, GLuint y
, GLuint w
, GLuint h
)
142 struct pipe_context
*pipe
= st
->pipe
;
143 struct pipe_resource
*pt
= stImage
->pt
;
145 DBG("%s \n", __FUNCTION__
);
147 stImage
->transfer
= pipe_get_transfer(st
->pipe
, pt
, stImage
->face
,
148 stImage
->level
, zoffset
,
151 if (stImage
->transfer
)
152 return pipe_transfer_map(pipe
, stImage
->transfer
);
159 st_texture_image_unmap(struct st_context
*st
,
160 struct st_texture_image
*stImage
)
162 struct pipe_context
*pipe
= st
->pipe
;
164 DBG("%s\n", __FUNCTION__
);
166 pipe_transfer_unmap(pipe
, stImage
->transfer
);
168 pipe
->transfer_destroy(pipe
, stImage
->transfer
);
174 * Upload data to a rectangular sub-region. Lots of choices how to do this:
176 * - memcpy by span to current destination
177 * - upload data as new buffer and blit
179 * Currently always memcpy.
182 st_surface_data(struct pipe_context
*pipe
,
183 struct pipe_transfer
*dst
,
184 unsigned dstx
, unsigned dsty
,
185 const void *src
, unsigned src_stride
,
186 unsigned srcx
, unsigned srcy
, unsigned width
, unsigned height
)
188 void *map
= pipe_transfer_map(pipe
, dst
);
190 assert(dst
->resource
);
192 dst
->resource
->format
,
199 pipe_transfer_unmap(pipe
, dst
);
203 /* Upload data for a particular image.
206 st_texture_image_data(struct st_context
*st
,
207 struct pipe_resource
*dst
,
211 GLuint src_row_stride
, GLuint src_image_stride
)
213 struct pipe_context
*pipe
= st
->pipe
;
214 GLuint depth
= u_minify(dst
->depth0
, level
);
216 const GLubyte
*srcUB
= src
;
217 struct pipe_transfer
*dst_transfer
;
219 DBG("%s\n", __FUNCTION__
);
221 for (i
= 0; i
< depth
; i
++) {
222 dst_transfer
= pipe_get_transfer(st
->pipe
, dst
, face
, level
, i
,
223 PIPE_TRANSFER_WRITE
, 0, 0,
224 u_minify(dst
->width0
, level
),
225 u_minify(dst
->height0
, level
));
227 st_surface_data(pipe
, dst_transfer
,
228 0, 0, /* dstx, dsty */
231 0, 0, /* source x, y */
232 u_minify(dst
->width0
, level
),
233 u_minify(dst
->height0
, level
)); /* width, height */
235 pipe
->transfer_destroy(pipe
, dst_transfer
);
237 srcUB
+= src_image_stride
;
243 * For debug only: get/print center pixel in the src resource.
246 print_center_pixel(struct pipe_context
*pipe
, struct pipe_resource
*src
)
248 struct pipe_subresource rect
;
249 struct pipe_transfer
*xfer
;
250 struct pipe_box region
;
256 region
.x
= src
->width0
/ 2;
257 region
.y
= src
->height0
/ 2;
263 xfer
= pipe
->get_transfer(pipe
, src
, rect
, PIPE_TRANSFER_READ
, ®ion
);
264 map
= pipe
->transfer_map(pipe
, xfer
);
266 printf("center pixel: %d %d %d %d\n", map
[0], map
[1], map
[2], map
[3]);
268 pipe
->transfer_unmap(pipe
, xfer
);
269 pipe
->transfer_destroy(pipe
, xfer
);
274 * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'.
275 * This is used to copy mipmap images from one texture buffer to another.
276 * This typically happens when our initial guess at the total texture size
277 * is incorrect (see the guess_and_alloc_texture() function).
280 st_texture_image_copy(struct pipe_context
*pipe
,
281 struct pipe_resource
*dst
, GLuint dstLevel
,
282 struct pipe_resource
*src
, GLuint srcLevel
,
285 GLuint width
= u_minify(dst
->width0
, dstLevel
);
286 GLuint height
= u_minify(dst
->height0
, dstLevel
);
287 GLuint depth
= u_minify(dst
->depth0
, dstLevel
);
288 struct pipe_subresource dstsub
, srcsub
;
291 assert(u_minify(src
->width0
, srcLevel
) == width
);
292 assert(u_minify(src
->height0
, srcLevel
) == height
);
293 assert(u_minify(src
->depth0
, srcLevel
) == depth
);
296 dstsub
.level
= dstLevel
;
298 srcsub
.level
= srcLevel
;
299 /* Loop over 3D image slices */
300 for (i
= 0; i
< depth
; i
++) {
303 print_center_pixel(pipe
, src
);
306 pipe
->resource_copy_region(pipe
,
309 0, 0, i
,/* destX, Y, Z */
312 0, 0, i
,/* srcX, Y, Z */