Merge branch 'gles2-2'
[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 /**
146 * Map a teximage in a mipmap texture.
147 * \param row_stride returns row stride in bytes
148 * \param image_stride returns image stride in bytes (for 3D textures).
149 * \return address of mapping
150 */
151 GLubyte *
152 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
153 GLuint zoffset, enum pipe_transfer_usage usage,
154 GLuint x, GLuint y, GLuint w, GLuint h)
155 {
156 struct pipe_context *pipe = st->pipe;
157 struct pipe_resource *pt = stImage->pt;
158
159 DBG("%s \n", __FUNCTION__);
160
161 stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face,
162 stImage->level, zoffset,
163 usage, x, y, w, h);
164
165 if (stImage->transfer)
166 return pipe_transfer_map(pipe, stImage->transfer);
167 else
168 return NULL;
169 }
170
171
172 void
173 st_texture_image_unmap(struct st_context *st,
174 struct st_texture_image *stImage)
175 {
176 struct pipe_context *pipe = st->pipe;
177
178 DBG("%s\n", __FUNCTION__);
179
180 pipe_transfer_unmap(pipe, stImage->transfer);
181
182 pipe->transfer_destroy(pipe, stImage->transfer);
183 }
184
185
186
187 /**
188 * Upload data to a rectangular sub-region. Lots of choices how to do this:
189 *
190 * - memcpy by span to current destination
191 * - upload data as new buffer and blit
192 *
193 * Currently always memcpy.
194 */
195 static void
196 st_surface_data(struct pipe_context *pipe,
197 struct pipe_transfer *dst,
198 unsigned dstx, unsigned dsty,
199 const void *src, unsigned src_stride,
200 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
201 {
202 void *map = pipe_transfer_map(pipe, dst);
203
204 assert(dst->resource);
205 util_copy_rect(map,
206 dst->resource->format,
207 dst->stride,
208 dstx, dsty,
209 width, height,
210 src, src_stride,
211 srcx, srcy);
212
213 pipe_transfer_unmap(pipe, dst);
214 }
215
216
217 /* Upload data for a particular image.
218 */
219 void
220 st_texture_image_data(struct st_context *st,
221 struct pipe_resource *dst,
222 GLuint face,
223 GLuint level,
224 void *src,
225 GLuint src_row_stride, GLuint src_image_stride)
226 {
227 struct pipe_context *pipe = st->pipe;
228 GLuint depth = u_minify(dst->depth0, level);
229 GLuint i;
230 const GLubyte *srcUB = src;
231 struct pipe_transfer *dst_transfer;
232
233 DBG("%s\n", __FUNCTION__);
234
235 for (i = 0; i < depth; i++) {
236 dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i,
237 PIPE_TRANSFER_WRITE, 0, 0,
238 u_minify(dst->width0, level),
239 u_minify(dst->height0, level));
240
241 st_surface_data(pipe, dst_transfer,
242 0, 0, /* dstx, dsty */
243 srcUB,
244 src_row_stride,
245 0, 0, /* source x, y */
246 u_minify(dst->width0, level),
247 u_minify(dst->height0, level)); /* width, height */
248
249 pipe->transfer_destroy(pipe, dst_transfer);
250
251 srcUB += src_image_stride;
252 }
253 }
254
255
256 /* Copy mipmap image between textures
257 */
258 void
259 st_texture_image_copy(struct pipe_context *pipe,
260 struct pipe_resource *dst, GLuint dstLevel,
261 struct pipe_resource *src,
262 GLuint face)
263 {
264 struct pipe_screen *screen = pipe->screen;
265 GLuint width = u_minify(dst->width0, dstLevel);
266 GLuint height = u_minify(dst->height0, dstLevel);
267 GLuint depth = u_minify(dst->depth0, dstLevel);
268 struct pipe_surface *src_surface;
269 struct pipe_surface *dst_surface;
270 GLuint i;
271
272 assert(src->width0 == dst->width0);
273 assert(src->height0 == dst->height0);
274
275 for (i = 0; i < depth; i++) {
276 GLuint srcLevel;
277
278 /* find src texture level of needed size */
279 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
280 if (u_minify(src->width0, srcLevel) == width &&
281 u_minify(src->height0, srcLevel) == height) {
282 break;
283 }
284 }
285 assert(u_minify(src->width0, srcLevel) == width);
286 assert(u_minify(src->height0, srcLevel) == height);
287
288 #if 0
289 {
290 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
291 PIPE_BUFFER_USAGE_CPU_READ);
292 ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ);
293 map += src_surface->width * src_surface->height * 4 / 2;
294 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
295 __FUNCTION__,
296 map[0], map[1], map[2], map[3],
297 src, srcLevel, dst, dstLevel);
298
299 screen->surface_unmap(screen, src_surface);
300 pipe_surface_reference(&src_surface, NULL);
301 }
302 #endif
303
304 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
305 PIPE_BIND_BLIT_DESTINATION);
306
307 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
308 PIPE_BIND_BLIT_SOURCE);
309
310 pipe->surface_copy(pipe,
311 dst_surface,
312 0, 0, /* destX, Y */
313 src_surface,
314 0, 0, /* srcX, Y */
315 width, height);
316
317 pipe_surface_reference(&src_surface, NULL);
318 pipe_surface_reference(&dst_surface, NULL);
319 }
320 }
321
322
323 void
324 st_teximage_flush_before_map(struct st_context *st,
325 struct pipe_resource *pt,
326 unsigned int face,
327 unsigned int level,
328 enum pipe_transfer_usage usage)
329 {
330 struct pipe_context *pipe = st->pipe;
331 unsigned referenced =
332 pipe->is_resource_referenced(pipe, pt, face, level);
333
334 if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) ||
335 (usage & PIPE_TRANSFER_WRITE)))
336 st->pipe->flush(st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
337 }