be450084ed8ca0c450659cc32f8101109fafea1e
[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
49 /**
50 * Allocate a new pipe_resource object
51 * width0, height0, depth0 are the dimensions of the level 0 image
52 * (the highest resolution). last_level indicates how many mipmap levels
53 * to allocate storage for. For non-mipmapped textures, this will be zero.
54 */
55 struct pipe_resource *
56 st_texture_create(struct st_context *st,
57 enum pipe_texture_target target,
58 enum pipe_format format,
59 GLuint last_level,
60 GLuint width0,
61 GLuint height0,
62 GLuint depth0,
63 GLuint bind )
64 {
65 struct pipe_resource pt, *newtex;
66 struct pipe_screen *screen = st->pipe->screen;
67
68 assert(target <= PIPE_TEXTURE_CUBE);
69
70 DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
71 _mesa_lookup_enum_by_nr(target),
72 _mesa_lookup_enum_by_nr(format), last_level);
73
74 assert(format);
75 assert(screen->is_format_supported(screen, format, target,
76 PIPE_BIND_SAMPLER_VIEW, 0));
77
78 memset(&pt, 0, sizeof(pt));
79 pt.target = target;
80 pt.format = format;
81 pt.last_level = last_level;
82 pt.width0 = width0;
83 pt.height0 = height0;
84 pt.depth0 = depth0;
85 pt.usage = PIPE_USAGE_DEFAULT;
86 pt.bind = bind;
87 pt.flags = 0;
88
89 newtex = screen->resource_create(screen, &pt);
90
91 assert(!newtex || pipe_is_referenced(&newtex->reference));
92
93 return newtex;
94 }
95
96
97 /**
98 * Check if a texture image can be pulled into a unified mipmap texture.
99 */
100 GLboolean
101 st_texture_match_image(const struct pipe_resource *pt,
102 const struct gl_texture_image *image,
103 GLuint face, GLuint level)
104 {
105 /* Images with borders are never pulled into mipmap textures.
106 */
107 if (image->Border)
108 return GL_FALSE;
109
110 /* Check if this image's format matches the established texture's format.
111 */
112 if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format)
113 return GL_FALSE;
114
115 /* Test if this image's size matches what's expected in the
116 * established texture.
117 */
118 if (image->Width != u_minify(pt->width0, level) ||
119 image->Height != u_minify(pt->height0, level) ||
120 image->Depth != u_minify(pt->depth0, level))
121 return GL_FALSE;
122
123 return GL_TRUE;
124 }
125
126
127 /**
128 * Map a teximage in a mipmap texture.
129 * \param row_stride returns row stride in bytes
130 * \param image_stride returns image stride in bytes (for 3D textures).
131 * \return address of mapping
132 */
133 GLubyte *
134 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
135 GLuint zoffset, enum pipe_transfer_usage usage,
136 GLuint x, GLuint y, GLuint w, GLuint h)
137 {
138 struct pipe_context *pipe = st->pipe;
139 struct pipe_resource *pt = stImage->pt;
140
141 DBG("%s \n", __FUNCTION__);
142
143 stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face,
144 stImage->level, zoffset,
145 usage, x, y, w, h);
146
147 if (stImage->transfer)
148 return pipe_transfer_map(pipe, stImage->transfer);
149 else
150 return NULL;
151 }
152
153
154 void
155 st_texture_image_unmap(struct st_context *st,
156 struct st_texture_image *stImage)
157 {
158 struct pipe_context *pipe = st->pipe;
159
160 DBG("%s\n", __FUNCTION__);
161
162 pipe_transfer_unmap(pipe, stImage->transfer);
163
164 pipe->transfer_destroy(pipe, stImage->transfer);
165 }
166
167
168
169 /**
170 * Upload data to a rectangular sub-region. Lots of choices how to do this:
171 *
172 * - memcpy by span to current destination
173 * - upload data as new buffer and blit
174 *
175 * Currently always memcpy.
176 */
177 static void
178 st_surface_data(struct pipe_context *pipe,
179 struct pipe_transfer *dst,
180 unsigned dstx, unsigned dsty,
181 const void *src, unsigned src_stride,
182 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
183 {
184 void *map = pipe_transfer_map(pipe, dst);
185
186 assert(dst->resource);
187 util_copy_rect(map,
188 dst->resource->format,
189 dst->stride,
190 dstx, dsty,
191 width, height,
192 src, src_stride,
193 srcx, srcy);
194
195 pipe_transfer_unmap(pipe, dst);
196 }
197
198
199 /* Upload data for a particular image.
200 */
201 void
202 st_texture_image_data(struct st_context *st,
203 struct pipe_resource *dst,
204 GLuint face,
205 GLuint level,
206 void *src,
207 GLuint src_row_stride, GLuint src_image_stride)
208 {
209 struct pipe_context *pipe = st->pipe;
210 GLuint depth = u_minify(dst->depth0, level);
211 GLuint i;
212 const GLubyte *srcUB = src;
213 struct pipe_transfer *dst_transfer;
214
215 DBG("%s\n", __FUNCTION__);
216
217 for (i = 0; i < depth; i++) {
218 dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i,
219 PIPE_TRANSFER_WRITE, 0, 0,
220 u_minify(dst->width0, level),
221 u_minify(dst->height0, level));
222
223 st_surface_data(pipe, dst_transfer,
224 0, 0, /* dstx, dsty */
225 srcUB,
226 src_row_stride,
227 0, 0, /* source x, y */
228 u_minify(dst->width0, level),
229 u_minify(dst->height0, level)); /* width, height */
230
231 pipe->transfer_destroy(pipe, dst_transfer);
232
233 srcUB += src_image_stride;
234 }
235 }
236
237
238 /* Copy mipmap image between textures
239 */
240 void
241 st_texture_image_copy(struct pipe_context *pipe,
242 struct pipe_resource *dst, GLuint dstLevel,
243 struct pipe_resource *src,
244 GLuint face)
245 {
246 struct pipe_screen *screen = pipe->screen;
247 GLuint width = u_minify(dst->width0, dstLevel);
248 GLuint height = u_minify(dst->height0, dstLevel);
249 GLuint depth = u_minify(dst->depth0, dstLevel);
250 struct pipe_surface *src_surface;
251 struct pipe_surface *dst_surface;
252 GLuint i;
253
254 assert(src->width0 == dst->width0);
255 assert(src->height0 == dst->height0);
256
257 for (i = 0; i < depth; i++) {
258 GLuint srcLevel;
259
260 /* find src texture level of needed size */
261 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
262 if (u_minify(src->width0, srcLevel) == width &&
263 u_minify(src->height0, srcLevel) == height) {
264 break;
265 }
266 }
267 assert(u_minify(src->width0, srcLevel) == width);
268 assert(u_minify(src->height0, srcLevel) == height);
269
270 #if 0
271 {
272 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
273 PIPE_BUFFER_USAGE_CPU_READ);
274 ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ);
275 map += src_surface->width * src_surface->height * 4 / 2;
276 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
277 __FUNCTION__,
278 map[0], map[1], map[2], map[3],
279 src, srcLevel, dst, dstLevel);
280
281 screen->surface_unmap(screen, src_surface);
282 pipe_surface_reference(&src_surface, NULL);
283 }
284 #endif
285
286 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
287 PIPE_BIND_BLIT_DESTINATION);
288
289 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
290 PIPE_BIND_BLIT_SOURCE);
291
292 pipe->surface_copy(pipe,
293 dst_surface,
294 0, 0, /* destX, Y */
295 src_surface,
296 0, 0, /* srcX, Y */
297 width, height);
298
299 pipe_surface_reference(&src_surface, NULL);
300 pipe_surface_reference(&dst_surface, NULL);
301 }
302 }
303
304
305 void
306 st_teximage_flush_before_map(struct st_context *st,
307 struct pipe_resource *pt,
308 unsigned int face,
309 unsigned int level,
310 enum pipe_transfer_usage usage)
311 {
312 struct pipe_context *pipe = st->pipe;
313 unsigned referenced =
314 pipe->is_resource_referenced(pipe, pt, face, level);
315
316 if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) ||
317 (usage & PIPE_TRANSFER_WRITE)))
318 st->pipe->flush(st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
319 }