st/mesa: fix strides in (de)compress_image() functions
[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 "st_context.h"
29 #include "st_format.h"
30 #include "st_texture.h"
31 #include "st_cb_fbo.h"
32 #include "st_inlines.h"
33 #include "main/enums.h"
34
35 #undef Elements /* fix re-defined macro warning */
36
37 #include "pipe/p_state.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "util/u_inlines.h"
41 #include "util/u_format.h"
42 #include "util/u_rect.h"
43 #include "util/u_math.h"
44
45
46 #define DBG if(0) printf
47
48 #if 0
49 static GLenum
50 target_to_target(GLenum target)
51 {
52 switch (target) {
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
55 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
56 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
57 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
58 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
59 return GL_TEXTURE_CUBE_MAP_ARB;
60 default:
61 return target;
62 }
63 }
64 #endif
65
66
67 /**
68 * Allocate a new pipe_resource object
69 * width0, height0, depth0 are the dimensions of the level 0 image
70 * (the highest resolution). last_level indicates how many mipmap levels
71 * to allocate storage for. For non-mipmapped textures, this will be zero.
72 */
73 struct pipe_resource *
74 st_texture_create(struct st_context *st,
75 enum pipe_texture_target target,
76 enum pipe_format format,
77 GLuint last_level,
78 GLuint width0,
79 GLuint height0,
80 GLuint depth0,
81 GLuint bind )
82 {
83 struct pipe_resource pt, *newtex;
84 struct pipe_screen *screen = st->pipe->screen;
85
86 assert(target <= PIPE_TEXTURE_CUBE);
87
88 DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
89 _mesa_lookup_enum_by_nr(target),
90 _mesa_lookup_enum_by_nr(format), last_level);
91
92 assert(format);
93 assert(screen->is_format_supported(screen, format, target,
94 PIPE_BIND_SAMPLER_VIEW, 0));
95
96 memset(&pt, 0, sizeof(pt));
97 pt.target = target;
98 pt.format = format;
99 pt.last_level = last_level;
100 pt.width0 = width0;
101 pt.height0 = height0;
102 pt.depth0 = depth0;
103 pt.usage = PIPE_USAGE_DEFAULT;
104 pt.bind = bind;
105 pt.flags = 0;
106
107 newtex = screen->resource_create(screen, &pt);
108
109 assert(!newtex || pipe_is_referenced(&newtex->reference));
110
111 return newtex;
112 }
113
114
115 /**
116 * Check if a texture image can be pulled into a unified mipmap texture.
117 */
118 GLboolean
119 st_texture_match_image(const struct pipe_resource *pt,
120 const struct gl_texture_image *image,
121 GLuint face, GLuint level)
122 {
123 /* Images with borders are never pulled into mipmap textures.
124 */
125 if (image->Border)
126 return GL_FALSE;
127
128 /* Check if this image's format matches the established texture's format.
129 */
130 if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format)
131 return GL_FALSE;
132
133 /* Test if this image's size matches what's expected in the
134 * established texture.
135 */
136 if (image->Width != u_minify(pt->width0, level) ||
137 image->Height != u_minify(pt->height0, level) ||
138 image->Depth != u_minify(pt->depth0, level))
139 return GL_FALSE;
140
141 return GL_TRUE;
142 }
143
144
145 #if 000
146 /* Although we use the image_offset[] array to store relative offsets
147 * to cube faces, Mesa doesn't know anything about this and expects
148 * each cube face to be treated as a separate image.
149 *
150 * These functions present that view to mesa:
151 */
152 const GLuint *
153 st_texture_depth_offsets(struct pipe_resource *pt, GLuint level)
154 {
155 static const GLuint zero = 0;
156
157 if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1)
158 return &zero;
159 else
160 return pt->level[level].image_offset;
161 }
162
163
164 /**
165 * Return the offset to the given mipmap texture image within the
166 * texture memory buffer, in bytes.
167 */
168 GLuint
169 st_texture_image_offset(const struct pipe_resource * pt,
170 GLuint face, GLuint level)
171 {
172 if (pt->target == PIPE_TEXTURE_CUBE)
173 return (pt->level[level].level_offset +
174 pt->level[level].image_offset[face] * pt->cpp);
175 else
176 return pt->level[level].level_offset;
177 }
178 #endif
179
180
181 /**
182 * Map a teximage in a mipmap texture.
183 * \param row_stride returns row stride in bytes
184 * \param image_stride returns image stride in bytes (for 3D textures).
185 * \return address of mapping
186 */
187 GLubyte *
188 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
189 GLuint zoffset, enum pipe_transfer_usage usage,
190 GLuint x, GLuint y, GLuint w, GLuint h)
191 {
192 struct pipe_context *pipe = st->pipe;
193 struct pipe_resource *pt = stImage->pt;
194
195 DBG("%s \n", __FUNCTION__);
196
197 stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face,
198 stImage->level, zoffset,
199 usage, x, y, w, h);
200
201 if (stImage->transfer)
202 return pipe_transfer_map(pipe, stImage->transfer);
203 else
204 return NULL;
205 }
206
207
208 void
209 st_texture_image_unmap(struct st_context *st,
210 struct st_texture_image *stImage)
211 {
212 struct pipe_context *pipe = st->pipe;
213
214 DBG("%s\n", __FUNCTION__);
215
216 pipe_transfer_unmap(pipe, stImage->transfer);
217
218 pipe->transfer_destroy(pipe, stImage->transfer);
219 }
220
221
222
223 /**
224 * Upload data to a rectangular sub-region. Lots of choices how to do this:
225 *
226 * - memcpy by span to current destination
227 * - upload data as new buffer and blit
228 *
229 * Currently always memcpy.
230 */
231 static void
232 st_surface_data(struct pipe_context *pipe,
233 struct pipe_transfer *dst,
234 unsigned dstx, unsigned dsty,
235 const void *src, unsigned src_stride,
236 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
237 {
238 void *map = pipe_transfer_map(pipe, dst);
239
240 assert(dst->resource);
241 util_copy_rect(map,
242 dst->resource->format,
243 dst->stride,
244 dstx, dsty,
245 width, height,
246 src, src_stride,
247 srcx, srcy);
248
249 pipe_transfer_unmap(pipe, dst);
250 }
251
252
253 /* Upload data for a particular image.
254 */
255 void
256 st_texture_image_data(struct st_context *st,
257 struct pipe_resource *dst,
258 GLuint face,
259 GLuint level,
260 void *src,
261 GLuint src_row_stride, GLuint src_image_stride)
262 {
263 struct pipe_context *pipe = st->pipe;
264 GLuint depth = u_minify(dst->depth0, level);
265 GLuint i;
266 const GLubyte *srcUB = src;
267 struct pipe_transfer *dst_transfer;
268
269 DBG("%s\n", __FUNCTION__);
270
271 for (i = 0; i < depth; i++) {
272 dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i,
273 PIPE_TRANSFER_WRITE, 0, 0,
274 u_minify(dst->width0, level),
275 u_minify(dst->height0, level));
276
277 st_surface_data(pipe, dst_transfer,
278 0, 0, /* dstx, dsty */
279 srcUB,
280 src_row_stride,
281 0, 0, /* source x, y */
282 u_minify(dst->width0, level),
283 u_minify(dst->height0, level)); /* width, height */
284
285 pipe->transfer_destroy(pipe, dst_transfer);
286
287 srcUB += src_image_stride;
288 }
289 }
290
291
292 /* Copy mipmap image between textures
293 */
294 void
295 st_texture_image_copy(struct pipe_context *pipe,
296 struct pipe_resource *dst, GLuint dstLevel,
297 struct pipe_resource *src,
298 GLuint face)
299 {
300 struct pipe_screen *screen = pipe->screen;
301 GLuint width = u_minify(dst->width0, dstLevel);
302 GLuint height = u_minify(dst->height0, dstLevel);
303 GLuint depth = u_minify(dst->depth0, dstLevel);
304 struct pipe_surface *src_surface;
305 struct pipe_surface *dst_surface;
306 GLuint i;
307
308 for (i = 0; i < depth; i++) {
309 GLuint srcLevel;
310
311 /* find src texture level of needed size */
312 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
313 if (u_minify(src->width0, srcLevel) == width &&
314 u_minify(src->height0, srcLevel) == height) {
315 break;
316 }
317 }
318 assert(u_minify(src->width0, srcLevel) == width);
319 assert(u_minify(src->height0, srcLevel) == height);
320
321 #if 0
322 {
323 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
324 PIPE_BUFFER_USAGE_CPU_READ);
325 ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ);
326 map += src_surface->width * src_surface->height * 4 / 2;
327 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
328 __FUNCTION__,
329 map[0], map[1], map[2], map[3],
330 src, srcLevel, dst, dstLevel);
331
332 screen->surface_unmap(screen, src_surface);
333 pipe_surface_reference(&src_surface, NULL);
334 }
335 #endif
336
337 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
338 PIPE_BIND_BLIT_DESTINATION);
339
340 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
341 PIPE_BIND_BLIT_SOURCE);
342
343 pipe->surface_copy(pipe,
344 dst_surface,
345 0, 0, /* destX, Y */
346 src_surface,
347 0, 0, /* srcX, Y */
348 width, height);
349
350 pipe_surface_reference(&src_surface, NULL);
351 pipe_surface_reference(&dst_surface, NULL);
352 }
353 }
354
355
356 void
357 st_teximage_flush_before_map(struct st_context *st,
358 struct pipe_resource *pt,
359 unsigned int face,
360 unsigned int level,
361 enum pipe_transfer_usage usage)
362 {
363 struct pipe_context *pipe = st->pipe;
364 unsigned referenced =
365 pipe->is_resource_referenced(pipe, pt, face, level);
366
367 if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) ||
368 (usage & PIPE_TRANSFER_WRITE)))
369 st->pipe->flush(st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
370 }