st/mesa: pass layers param to st_texture_create()
[mesa.git] / src / mesa / state_tracker / st_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL 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.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29
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"
35
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"
43
44
45 #define DBG if(0) printf
46
47
48 /**
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.
53 */
54 struct pipe_resource *
55 st_texture_create(struct st_context *st,
56 enum pipe_texture_target target,
57 enum pipe_format format,
58 GLuint last_level,
59 GLuint width0,
60 GLuint height0,
61 GLuint depth0,
62 GLuint layers,
63 GLuint bind )
64 {
65 struct pipe_resource pt, *newtex;
66 struct pipe_screen *screen = st->pipe->screen;
67
68 assert(target < PIPE_MAX_TEXTURE_TYPES);
69 assert(width0 > 0);
70 assert(height0 > 0);
71 assert(depth0 > 0);
72
73 DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
74 _mesa_lookup_enum_by_nr(target),
75 _mesa_lookup_enum_by_nr(format), last_level);
76
77 assert(format);
78 assert(screen->is_format_supported(screen, format, target, 0,
79 PIPE_BIND_SAMPLER_VIEW, 0));
80
81 memset(&pt, 0, sizeof(pt));
82 pt.target = target;
83 pt.format = format;
84 pt.last_level = last_level;
85 pt.width0 = width0;
86 pt.height0 = height0;
87 pt.depth0 = depth0;
88 pt.array_size = (target == PIPE_TEXTURE_CUBE ? 6 : 1);
89 pt.usage = PIPE_USAGE_DEFAULT;
90 pt.bind = bind;
91 pt.flags = 0;
92
93 newtex = screen->resource_create(screen, &pt);
94
95 assert(!newtex || pipe_is_referenced(&newtex->reference));
96
97 return newtex;
98 }
99
100
101 /**
102 * Check if a texture image can be pulled into a unified mipmap texture.
103 */
104 GLboolean
105 st_texture_match_image(const struct pipe_resource *pt,
106 const struct gl_texture_image *image,
107 GLuint face, GLuint level)
108 {
109 /* Images with borders are never pulled into mipmap textures.
110 */
111 if (image->Border)
112 return GL_FALSE;
113
114 /* Check if this image's format matches the established texture's format.
115 */
116 if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format)
117 return GL_FALSE;
118
119 /* Test if this image's size matches what's expected in the
120 * established texture.
121 */
122 if (image->Width != u_minify(pt->width0, level) ||
123 image->Height != u_minify(pt->height0, level) ||
124 image->Depth != u_minify(pt->depth0, level))
125 return GL_FALSE;
126
127 return GL_TRUE;
128 }
129
130
131 /**
132 * Map a texture image and return the address for a particular 2D face/slice/
133 * layer. The stImage indicates the cube face and mipmap level. The slice
134 * of the 3D texture is passed in 'zoffset'.
135 * \param usage one of the PIPE_TRANSFER_x values
136 * \param x, y, w, h the region of interest of the 2D image.
137 * \return address of mapping or NULL if any error
138 */
139 GLubyte *
140 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
141 GLuint zoffset, enum pipe_transfer_usage usage,
142 GLuint x, GLuint y, GLuint w, GLuint h)
143 {
144 struct pipe_context *pipe = st->pipe;
145 struct pipe_resource *pt = stImage->pt;
146
147 DBG("%s \n", __FUNCTION__);
148
149 stImage->transfer = pipe_get_transfer(st->pipe, pt, stImage->level,
150 stImage->face + zoffset,
151 usage, x, y, w, h);
152
153 if (stImage->transfer)
154 return pipe_transfer_map(pipe, stImage->transfer);
155 else
156 return NULL;
157 }
158
159
160 void
161 st_texture_image_unmap(struct st_context *st,
162 struct st_texture_image *stImage)
163 {
164 struct pipe_context *pipe = st->pipe;
165
166 DBG("%s\n", __FUNCTION__);
167
168 pipe_transfer_unmap(pipe, stImage->transfer);
169
170 pipe->transfer_destroy(pipe, stImage->transfer);
171 }
172
173
174
175 /**
176 * Upload data to a rectangular sub-region. Lots of choices how to do this:
177 *
178 * - memcpy by span to current destination
179 * - upload data as new buffer and blit
180 *
181 * Currently always memcpy.
182 */
183 static void
184 st_surface_data(struct pipe_context *pipe,
185 struct pipe_transfer *dst,
186 unsigned dstx, unsigned dsty,
187 const void *src, unsigned src_stride,
188 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
189 {
190 void *map = pipe_transfer_map(pipe, dst);
191
192 assert(dst->resource);
193 util_copy_rect(map,
194 dst->resource->format,
195 dst->stride,
196 dstx, dsty,
197 width, height,
198 src, src_stride,
199 srcx, srcy);
200
201 pipe_transfer_unmap(pipe, dst);
202 }
203
204
205 /* Upload data for a particular image.
206 */
207 void
208 st_texture_image_data(struct st_context *st,
209 struct pipe_resource *dst,
210 GLuint face,
211 GLuint level,
212 void *src,
213 GLuint src_row_stride, GLuint src_image_stride)
214 {
215 struct pipe_context *pipe = st->pipe;
216 GLuint depth = u_minify(dst->depth0, level);
217 GLuint i;
218 const GLubyte *srcUB = src;
219 struct pipe_transfer *dst_transfer;
220
221 DBG("%s\n", __FUNCTION__);
222
223 for (i = 0; i < depth; i++) {
224 dst_transfer = pipe_get_transfer(st->pipe, dst, level, face + i,
225 PIPE_TRANSFER_WRITE, 0, 0,
226 u_minify(dst->width0, level),
227 u_minify(dst->height0, level));
228
229 st_surface_data(pipe, dst_transfer,
230 0, 0, /* dstx, dsty */
231 srcUB,
232 src_row_stride,
233 0, 0, /* source x, y */
234 u_minify(dst->width0, level),
235 u_minify(dst->height0, level)); /* width, height */
236
237 pipe->transfer_destroy(pipe, dst_transfer);
238
239 srcUB += src_image_stride;
240 }
241 }
242
243
244 /**
245 * For debug only: get/print center pixel in the src resource.
246 */
247 static void
248 print_center_pixel(struct pipe_context *pipe, struct pipe_resource *src)
249 {
250 struct pipe_transfer *xfer;
251 struct pipe_box region;
252 ubyte *map;
253
254 region.x = src->width0 / 2;
255 region.y = src->height0 / 2;
256 region.z = 0;
257 region.width = 1;
258 region.height = 1;
259 region.depth = 1;
260
261 xfer = pipe->get_transfer(pipe, src, 0, PIPE_TRANSFER_READ, &region);
262 map = pipe->transfer_map(pipe, xfer);
263
264 printf("center pixel: %d %d %d %d\n", map[0], map[1], map[2], map[3]);
265
266 pipe->transfer_unmap(pipe, xfer);
267 pipe->transfer_destroy(pipe, xfer);
268 }
269
270
271 /**
272 * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'.
273 * This is used to copy mipmap images from one texture buffer to another.
274 * This typically happens when our initial guess at the total texture size
275 * is incorrect (see the guess_and_alloc_texture() function).
276 */
277 void
278 st_texture_image_copy(struct pipe_context *pipe,
279 struct pipe_resource *dst, GLuint dstLevel,
280 struct pipe_resource *src, GLuint srcLevel,
281 GLuint face)
282 {
283 GLuint width = u_minify(dst->width0, dstLevel);
284 GLuint height = u_minify(dst->height0, dstLevel);
285 GLuint depth = u_minify(dst->depth0, dstLevel);
286 struct pipe_box src_box;
287 GLuint i;
288
289 assert(u_minify(src->width0, srcLevel) == width);
290 assert(u_minify(src->height0, srcLevel) == height);
291 assert(u_minify(src->depth0, srcLevel) == depth);
292
293 src_box.x = 0;
294 src_box.y = 0;
295 src_box.width = width;
296 src_box.height = height;
297 src_box.depth = 1;
298 /* Loop over 3D image slices */
299 /* could (and probably should) use "true" 3d box here -
300 but drivers can't quite handle it yet */
301 for (i = face; i < face + depth; i++) {
302 src_box.z = i;
303
304 if (0) {
305 print_center_pixel(pipe, src);
306 }
307
308 pipe->resource_copy_region(pipe,
309 dst,
310 dstLevel,
311 0, 0, i,/* destX, Y, Z */
312 src,
313 srcLevel,
314 &src_box);
315 }
316 }
317