ed37098483e91ec298f67a24714e33e9c9f540f9
[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 if (target == PIPE_TEXTURE_CUBE)
73 assert(layers == 6);
74
75 DBG("%s target %d format %s last_level %d\n", __FUNCTION__,
76 (int) target, util_format_name(format), last_level);
77
78 assert(format);
79 assert(screen->is_format_supported(screen, format, target, 0,
80 PIPE_BIND_SAMPLER_VIEW));
81
82 memset(&pt, 0, sizeof(pt));
83 pt.target = target;
84 pt.format = format;
85 pt.last_level = last_level;
86 pt.width0 = width0;
87 pt.height0 = height0;
88 pt.depth0 = depth0;
89 pt.array_size = (target == PIPE_TEXTURE_CUBE ? 6 : layers);
90 pt.usage = PIPE_USAGE_DEFAULT;
91 pt.bind = bind;
92 pt.flags = 0;
93
94 newtex = screen->resource_create(screen, &pt);
95
96 assert(!newtex || pipe_is_referenced(&newtex->reference));
97
98 return newtex;
99 }
100
101
102 /**
103 * In OpenGL the number of 1D array texture layers is the "height" and
104 * the number of 2D array texture layers is the "depth". In Gallium the
105 * number of layers in an array texture is a separate 'array_size' field.
106 * This function converts dimensions from the former to the later.
107 */
108 void
109 st_gl_texture_dims_to_pipe_dims(GLenum texture,
110 GLuint widthIn,
111 GLuint heightIn,
112 GLuint depthIn,
113 GLuint *widthOut,
114 GLuint *heightOut,
115 GLuint *depthOut,
116 GLuint *layersOut)
117 {
118 switch (texture) {
119 case GL_TEXTURE_1D:
120 case GL_PROXY_TEXTURE_1D:
121 assert(heightIn == 1);
122 assert(depthIn == 1);
123 *widthOut = widthIn;
124 *heightOut = 1;
125 *depthOut = 1;
126 *layersOut = 1;
127 break;
128 case GL_TEXTURE_1D_ARRAY:
129 case GL_PROXY_TEXTURE_1D_ARRAY:
130 assert(depthIn == 1);
131 *widthOut = widthIn;
132 *heightOut = 1;
133 *depthOut = 1;
134 *layersOut = heightIn;
135 break;
136 case GL_TEXTURE_2D:
137 case GL_PROXY_TEXTURE_2D:
138 case GL_TEXTURE_RECTANGLE:
139 case GL_PROXY_TEXTURE_RECTANGLE:
140 case GL_TEXTURE_EXTERNAL_OES:
141 assert(depthIn == 1);
142 *widthOut = widthIn;
143 *heightOut = heightIn;
144 *depthOut = 1;
145 *layersOut = 1;
146 break;
147 case GL_TEXTURE_CUBE_MAP:
148 case GL_PROXY_TEXTURE_CUBE_MAP:
149 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
150 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
151 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
152 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
153 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
154 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
155 assert(depthIn == 1);
156 *widthOut = widthIn;
157 *heightOut = heightIn;
158 *depthOut = 1;
159 *layersOut = 6;
160 break;
161 case GL_TEXTURE_2D_ARRAY:
162 case GL_PROXY_TEXTURE_2D_ARRAY:
163 *widthOut = widthIn;
164 *heightOut = heightIn;
165 *depthOut = 1;
166 *layersOut = depthIn;
167 break;
168 case GL_TEXTURE_CUBE_MAP_ARRAY:
169 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
170 *widthOut = widthIn;
171 *heightOut = heightIn;
172 *depthOut = 1;
173 *layersOut = depthIn;
174 break;
175 default:
176 assert(0 && "Unexpected texture in st_gl_texture_dims_to_pipe_dims()");
177 /* fall-through */
178 case GL_TEXTURE_3D:
179 case GL_PROXY_TEXTURE_3D:
180 *widthOut = widthIn;
181 *heightOut = heightIn;
182 *depthOut = depthIn;
183 *layersOut = 1;
184 break;
185 }
186 }
187
188
189 /**
190 * Check if a texture image can be pulled into a unified mipmap texture.
191 */
192 GLboolean
193 st_texture_match_image(const struct pipe_resource *pt,
194 const struct gl_texture_image *image)
195 {
196 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
197
198 /* Images with borders are never pulled into mipmap textures.
199 */
200 if (image->Border)
201 return GL_FALSE;
202
203 /* Check if this image's format matches the established texture's format.
204 */
205 if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format)
206 return GL_FALSE;
207
208 st_gl_texture_dims_to_pipe_dims(image->TexObject->Target,
209 image->Width, image->Height, image->Depth,
210 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
211
212 /* Test if this image's size matches what's expected in the
213 * established texture.
214 */
215 if (ptWidth != u_minify(pt->width0, image->Level) ||
216 ptHeight != u_minify(pt->height0, image->Level) ||
217 ptDepth != u_minify(pt->depth0, image->Level) ||
218 ptLayers != pt->array_size)
219 return GL_FALSE;
220
221 return GL_TRUE;
222 }
223
224
225 /**
226 * Map a texture image and return the address for a particular 2D face/slice/
227 * layer. The stImage indicates the cube face and mipmap level. The slice
228 * of the 3D texture is passed in 'zoffset'.
229 * \param usage one of the PIPE_TRANSFER_x values
230 * \param x, y, w, h the region of interest of the 2D image.
231 * \return address of mapping or NULL if any error
232 */
233 GLubyte *
234 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
235 enum pipe_transfer_usage usage,
236 GLuint x, GLuint y, GLuint z,
237 GLuint w, GLuint h, GLuint d)
238 {
239 struct st_texture_object *stObj =
240 st_texture_object(stImage->base.TexObject);
241 GLuint level;
242
243 DBG("%s \n", __FUNCTION__);
244
245 if (!stImage->pt)
246 return NULL;
247
248 if (stObj->pt != stImage->pt)
249 level = 0;
250 else
251 level = stImage->base.Level;
252
253 return pipe_transfer_map_3d(st->pipe, stImage->pt, level, usage,
254 x, y, z + stImage->base.Face,
255 w, h, d, &stImage->transfer);
256 }
257
258
259 void
260 st_texture_image_unmap(struct st_context *st,
261 struct st_texture_image *stImage)
262 {
263 struct pipe_context *pipe = st->pipe;
264
265 DBG("%s\n", __FUNCTION__);
266
267 pipe_transfer_unmap(pipe, stImage->transfer);
268 stImage->transfer = NULL;
269 }
270
271
272 /* Upload data for a particular image.
273 */
274 void
275 st_texture_image_data(struct st_context *st,
276 struct pipe_resource *dst,
277 GLuint face,
278 GLuint level,
279 void *src,
280 GLuint src_row_stride, GLuint src_image_stride)
281 {
282 struct pipe_context *pipe = st->pipe;
283 GLuint i;
284 const GLubyte *srcUB = src;
285 GLuint layers;
286
287 if (dst->target == PIPE_TEXTURE_1D_ARRAY ||
288 dst->target == PIPE_TEXTURE_2D_ARRAY ||
289 dst->target == PIPE_TEXTURE_CUBE_ARRAY)
290 layers = dst->array_size;
291 else
292 layers = u_minify(dst->depth0, level);
293
294 DBG("%s\n", __FUNCTION__);
295
296 for (i = 0; i < layers; i++) {
297 struct pipe_box box;
298 u_box_2d_zslice(0, 0, face + i,
299 u_minify(dst->width0, level),
300 u_minify(dst->height0, level),
301 &box);
302
303 pipe->transfer_inline_write(pipe, dst, level, PIPE_TRANSFER_WRITE,
304 &box, srcUB, src_row_stride, 0);
305
306 srcUB += src_image_stride;
307 }
308 }
309
310
311 /**
312 * For debug only: get/print center pixel in the src resource.
313 */
314 static void
315 print_center_pixel(struct pipe_context *pipe, struct pipe_resource *src)
316 {
317 struct pipe_transfer *xfer;
318 struct pipe_box region;
319 ubyte *map;
320
321 region.x = src->width0 / 2;
322 region.y = src->height0 / 2;
323 region.z = 0;
324 region.width = 1;
325 region.height = 1;
326 region.depth = 1;
327
328 map = pipe->transfer_map(pipe, src, 0, PIPE_TRANSFER_READ, &region, &xfer);
329
330 printf("center pixel: %d %d %d %d\n", map[0], map[1], map[2], map[3]);
331
332 pipe->transfer_unmap(pipe, xfer);
333 }
334
335
336 /**
337 * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'.
338 * This is used to copy mipmap images from one texture buffer to another.
339 * This typically happens when our initial guess at the total texture size
340 * is incorrect (see the guess_and_alloc_texture() function).
341 */
342 void
343 st_texture_image_copy(struct pipe_context *pipe,
344 struct pipe_resource *dst, GLuint dstLevel,
345 struct pipe_resource *src, GLuint srcLevel,
346 GLuint face)
347 {
348 GLuint width = u_minify(dst->width0, dstLevel);
349 GLuint height = u_minify(dst->height0, dstLevel);
350 GLuint depth = u_minify(dst->depth0, dstLevel);
351 struct pipe_box src_box;
352 GLuint i;
353
354 if (u_minify(src->width0, srcLevel) != width ||
355 u_minify(src->height0, srcLevel) != height ||
356 u_minify(src->depth0, srcLevel) != depth) {
357 /* The source image size doesn't match the destination image size.
358 * This can happen in some degenerate situations such as rendering to a
359 * cube map face which was set up with mismatched texture sizes.
360 */
361 return;
362 }
363
364 src_box.x = 0;
365 src_box.y = 0;
366 src_box.width = width;
367 src_box.height = height;
368 src_box.depth = 1;
369 /* Loop over 3D image slices */
370 /* could (and probably should) use "true" 3d box here -
371 but drivers can't quite handle it yet */
372 for (i = face; i < face + depth; i++) {
373 src_box.z = i;
374
375 if (0) {
376 print_center_pixel(pipe, src);
377 }
378
379 pipe->resource_copy_region(pipe,
380 dst,
381 dstLevel,
382 0, 0, i,/* destX, Y, Z */
383 src,
384 srcLevel,
385 &src_box);
386 }
387 }
388
389
390 struct pipe_resource *
391 st_create_color_map_texture(struct gl_context *ctx)
392 {
393 struct st_context *st = st_context(ctx);
394 struct pipe_resource *pt;
395 enum pipe_format format;
396 const uint texSize = 256; /* simple, and usually perfect */
397
398 /* find an RGBA texture format */
399 format = st_choose_format(st, GL_RGBA, GL_NONE, GL_NONE,
400 PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW,
401 FALSE);
402
403 /* create texture for color map/table */
404 pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
405 texSize, texSize, 1, 1, PIPE_BIND_SAMPLER_VIEW);
406 return pt;
407 }
408