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,
65 struct pipe_resource pt, *newtex;
66 struct pipe_screen *screen = st->pipe->screen;
68 assert(target < PIPE_MAX_TEXTURE_TYPES);
72 if (target == PIPE_TEXTURE_CUBE)
75 DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
76 _mesa_lookup_enum_by_nr(target),
77 _mesa_lookup_enum_by_nr(format), last_level);
80 assert(screen->is_format_supported(screen, format, target, 0,
81 PIPE_BIND_SAMPLER_VIEW));
83 memset(&pt, 0, sizeof(pt));
86 pt.last_level = last_level;
90 pt.array_size = (target == PIPE_TEXTURE_CUBE ? 6 : layers);
91 pt.usage = PIPE_USAGE_DEFAULT;
95 newtex = screen->resource_create(screen, &pt);
97 assert(!newtex || pipe_is_referenced(&newtex->reference));
104 * In OpenGL the number of 1D array texture layers is the "height" and
105 * the number of 2D array texture layers is the "depth". In Gallium the
106 * number of layers in an array texture is a separate 'array_size' field.
107 * This function converts dimensions from the former to the later.
110 st_gl_texture_dims_to_pipe_dims(GLenum texture,
121 assert(heightIn == 1);
122 assert(depthIn == 1);
128 case GL_TEXTURE_1D_ARRAY:
129 assert(depthIn == 1);
133 *layersOut = heightIn;
136 case GL_TEXTURE_RECTANGLE:
137 assert(depthIn == 1);
139 *heightOut = heightIn;
143 case GL_TEXTURE_CUBE_MAP:
144 assert(depthIn == 1);
146 *heightOut = heightIn;
150 case GL_TEXTURE_2D_ARRAY:
152 *heightOut = heightIn;
154 *layersOut = depthIn;
157 assert(0 && "Unexpected texture in st_gl_texture_dims_to_pipe_dims()");
161 *heightOut = heightIn;
170 * Check if a texture image can be pulled into a unified mipmap texture.
173 st_texture_match_image(const struct pipe_resource *pt,
174 const struct gl_texture_image *image,
175 GLuint face, GLuint level)
177 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
179 /* Images with borders are never pulled into mipmap textures.
184 /* Check if this image's format matches the established texture's format.
186 if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format)
189 st_gl_texture_dims_to_pipe_dims(image->TexObject->Target,
190 image->Width, image->Height, image->Depth,
191 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
193 /* Test if this image's size matches what's expected in the
194 * established texture.
196 if (ptWidth != u_minify(pt->width0, level) ||
197 ptHeight != u_minify(pt->height0, level) ||
198 ptDepth != u_minify(pt->depth0, level) ||
199 ptLayers != pt->array_size)
207 * Map a texture image and return the address for a particular 2D face/slice/
208 * layer. The stImage indicates the cube face and mipmap level. The slice
209 * of the 3D texture is passed in 'zoffset'.
210 * \param usage one of the PIPE_TRANSFER_x values
211 * \param x, y, w, h the region of interest of the 2D image.
212 * \return address of mapping or NULL if any error
215 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
216 GLuint zoffset, enum pipe_transfer_usage usage,
217 GLuint x, GLuint y, GLuint w, GLuint h)
219 struct pipe_context *pipe = st->pipe;
220 struct pipe_resource *pt = stImage->pt;
222 DBG("%s \n", __FUNCTION__);
224 stImage->transfer = pipe_get_transfer(st->pipe, pt, stImage->level,
225 stImage->face + zoffset,
228 if (stImage->transfer)
229 return pipe_transfer_map(pipe, stImage->transfer);
236 st_texture_image_unmap(struct st_context *st,
237 struct st_texture_image *stImage)
239 struct pipe_context *pipe = st->pipe;
241 DBG("%s\n", __FUNCTION__);
243 pipe_transfer_unmap(pipe, stImage->transfer);
245 pipe->transfer_destroy(pipe, stImage->transfer);
251 * Upload data to a rectangular sub-region. Lots of choices how to do this:
253 * - memcpy by span to current destination
254 * - upload data as new buffer and blit
256 * Currently always memcpy.
259 st_surface_data(struct pipe_context *pipe,
260 struct pipe_transfer *dst,
261 unsigned dstx, unsigned dsty,
262 const void *src, unsigned src_stride,
263 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
265 void *map = pipe_transfer_map(pipe, dst);
267 assert(dst->resource);
269 dst->resource->format,
276 pipe_transfer_unmap(pipe, dst);
280 /* Upload data for a particular image.
283 st_texture_image_data(struct st_context *st,
284 struct pipe_resource *dst,
288 GLuint src_row_stride, GLuint src_image_stride)
290 struct pipe_context *pipe = st->pipe;
292 const GLubyte *srcUB = src;
293 struct pipe_transfer *dst_transfer;
296 if (dst->target == PIPE_TEXTURE_1D_ARRAY ||
297 dst->target == PIPE_TEXTURE_2D_ARRAY)
298 layers = dst->array_size;
300 layers = u_minify(dst->depth0, level);
302 DBG("%s\n", __FUNCTION__);
304 for (i = 0; i < layers; i++) {
305 dst_transfer = pipe_get_transfer(st->pipe, dst, level, face + i,
306 PIPE_TRANSFER_WRITE, 0, 0,
307 u_minify(dst->width0, level),
308 u_minify(dst->height0, level));
310 st_surface_data(pipe, dst_transfer,
311 0, 0, /* dstx, dsty */
314 0, 0, /* source x, y */
315 u_minify(dst->width0, level),
316 u_minify(dst->height0, level)); /* width, height */
318 pipe->transfer_destroy(pipe, dst_transfer);
320 srcUB += src_image_stride;
326 * For debug only: get/print center pixel in the src resource.
329 print_center_pixel(struct pipe_context *pipe, struct pipe_resource *src)
331 struct pipe_transfer *xfer;
332 struct pipe_box region;
335 region.x = src->width0 / 2;
336 region.y = src->height0 / 2;
342 xfer = pipe->get_transfer(pipe, src, 0, PIPE_TRANSFER_READ, ®ion);
343 map = pipe->transfer_map(pipe, xfer);
345 printf("center pixel: %d %d %d %d\n", map[0], map[1], map[2], map[3]);
347 pipe->transfer_unmap(pipe, xfer);
348 pipe->transfer_destroy(pipe, xfer);
353 * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'.
354 * This is used to copy mipmap images from one texture buffer to another.
355 * This typically happens when our initial guess at the total texture size
356 * is incorrect (see the guess_and_alloc_texture() function).
359 st_texture_image_copy(struct pipe_context *pipe,
360 struct pipe_resource *dst, GLuint dstLevel,
361 struct pipe_resource *src, GLuint srcLevel,
364 GLuint width = u_minify(dst->width0, dstLevel);
365 GLuint height = u_minify(dst->height0, dstLevel);
366 GLuint depth = u_minify(dst->depth0, dstLevel);
367 struct pipe_box src_box;
370 if (u_minify(src->width0, srcLevel) != width ||
371 u_minify(src->height0, srcLevel) != height ||
372 u_minify(src->depth0, srcLevel) != depth) {
373 /* The source image size doesn't match the destination image size.
374 * This can happen in some degenerate situations such as rendering to a
375 * cube map face which was set up with mismatched texture sizes.
382 src_box.width = width;
383 src_box.height = height;
385 /* Loop over 3D image slices */
386 /* could (and probably should) use "true" 3d box here -
387 but drivers can't quite handle it yet */
388 for (i = face; i < face + depth; i++) {
392 print_center_pixel(pipe, src);
395 pipe->resource_copy_region(pipe,
398 0, 0, i,/* destX, Y, Z */