gallium: replace some ordinary assignments with pipe_reference_texture()
[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 "enums.h"
32
33 #include "pipe/p_state.h"
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "pipe/p_inlines.h"
37 #include "pipe/p_util.h"
38 #include "pipe/p_inlines.h"
39 #include "pipe/p_winsys.h"
40
41
42 #define DBG if(0) printf
43
44 #if 0
45 static GLenum
46 target_to_target(GLenum target)
47 {
48 switch (target) {
49 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
50 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
51 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
52 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
55 return GL_TEXTURE_CUBE_MAP_ARB;
56 default:
57 return target;
58 }
59 }
60 #endif
61
62
63 /**
64 * Allocate a new pipe_texture object
65 * width0, height0, depth0 are the dimensions of the level 0 image
66 * (the highest resolution). last_level indicates how many mipmap levels
67 * to allocate storage for. For non-mipmapped textures, this will be zero.
68 */
69 struct pipe_texture *
70 st_texture_create(struct st_context *st,
71 enum pipe_texture_target target,
72 enum pipe_format format,
73 GLuint last_level,
74 GLuint width0,
75 GLuint height0,
76 GLuint depth0,
77 GLuint compress_byte)
78 {
79 struct pipe_texture pt, *newtex;
80
81 assert(target <= PIPE_TEXTURE_CUBE);
82
83 DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
84 _mesa_lookup_enum_by_nr(target),
85 _mesa_lookup_enum_by_nr(format), last_level);
86
87 assert(format);
88
89 memset(&pt, 0, sizeof(pt));
90 pt.target = target;
91 pt.format = format;
92 pt.last_level = last_level;
93 pt.width[0] = width0;
94 pt.height[0] = height0;
95 pt.depth[0] = depth0;
96 pt.compressed = compress_byte ? 1 : 0;
97 pt.cpp = pt.compressed ? compress_byte : st_sizeof_format(format);
98
99 newtex = st->pipe->texture_create(st->pipe, &pt);
100
101 assert(!newtex || newtex->refcount == 1);
102
103 return newtex;
104 }
105
106
107 /**
108 * Check if a texture image be pulled into a unified mipmap texture.
109 * This mirrors the completeness test in a lot of ways.
110 *
111 * Not sure whether I want to pass gl_texture_image here.
112 */
113 GLboolean
114 st_texture_match_image(const struct pipe_texture *pt,
115 const struct gl_texture_image *image,
116 GLuint face, GLuint level)
117 {
118 /* Images with borders are never pulled into mipmap textures.
119 */
120 if (image->Border)
121 return GL_FALSE;
122
123 if (st_mesa_format_to_pipe_format(image->TexFormat->MesaFormat) != pt->format ||
124 image->IsCompressed != pt->compressed)
125 return GL_FALSE;
126
127 /* Test image dimensions against the base level image adjusted for
128 * minification. This will also catch images not present in the
129 * texture, changed targets, etc.
130 */
131 if (image->Width != pt->width[level] ||
132 image->Height != pt->height[level] ||
133 image->Depth != pt->depth[level])
134 return GL_FALSE;
135
136 return GL_TRUE;
137 }
138
139
140 #if 000
141 /* Although we use the image_offset[] array to store relative offsets
142 * to cube faces, Mesa doesn't know anything about this and expects
143 * each cube face to be treated as a separate image.
144 *
145 * These functions present that view to mesa:
146 */
147 const GLuint *
148 st_texture_depth_offsets(struct pipe_texture *pt, GLuint level)
149 {
150 static const GLuint zero = 0;
151
152 if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1)
153 return &zero;
154 else
155 return pt->level[level].image_offset;
156 }
157
158
159 /**
160 * Return the offset to the given mipmap texture image within the
161 * texture memory buffer, in bytes.
162 */
163 GLuint
164 st_texture_image_offset(const struct pipe_texture * pt,
165 GLuint face, GLuint level)
166 {
167 if (pt->target == PIPE_TEXTURE_CUBE)
168 return (pt->level[level].level_offset +
169 pt->level[level].image_offset[face] * pt->cpp);
170 else
171 return pt->level[level].level_offset;
172 }
173 #endif
174
175
176 /**
177 * Map a teximage in a mipmap texture.
178 * \param row_stride returns row stride in bytes
179 * \param image_stride returns image stride in bytes (for 3D textures).
180 * \return address of mapping
181 */
182 GLubyte *
183 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
184 GLuint zoffset)
185 {
186 struct pipe_texture *pt = stImage->pt;
187 DBG("%s \n", __FUNCTION__);
188
189 stImage->surface = st->pipe->get_tex_surface(st->pipe, pt, stImage->face,
190 stImage->level, zoffset);
191
192 return pipe_surface_map(stImage->surface);
193 }
194
195
196 void
197 st_texture_image_unmap(struct st_texture_image *stImage)
198 {
199 DBG("%s\n", __FUNCTION__);
200
201 pipe_surface_unmap(stImage->surface);
202
203 pipe_surface_reference(&stImage->surface, NULL);
204 }
205
206
207
208 /**
209 * Upload data to a rectangular sub-region. Lots of choices how to do this:
210 *
211 * - memcpy by span to current destination
212 * - upload data as new buffer and blit
213 *
214 * Currently always memcpy.
215 */
216 static void
217 st_surface_data(struct pipe_context *pipe,
218 struct pipe_surface *dst,
219 unsigned dstx, unsigned dsty,
220 const void *src, unsigned src_pitch,
221 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
222 {
223 pipe_copy_rect(pipe_surface_map(dst),
224 dst->cpp,
225 dst->pitch,
226 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
227
228 pipe_surface_unmap(dst);
229 }
230
231
232 /* Upload data for a particular image.
233 */
234 void
235 st_texture_image_data(struct pipe_context *pipe,
236 struct pipe_texture *dst,
237 GLuint face,
238 GLuint level,
239 void *src,
240 GLuint src_row_pitch, GLuint src_image_pitch)
241 {
242 GLuint depth = dst->depth[level];
243 GLuint i;
244 GLuint height = 0;
245 const GLubyte *srcUB = src;
246 struct pipe_surface *dst_surface;
247
248 DBG("%s\n", __FUNCTION__);
249 for (i = 0; i < depth; i++) {
250 height = dst->height[level];
251 if(dst->compressed)
252 height /= 4;
253
254 dst_surface = pipe->get_tex_surface(pipe, dst, face, level, i);
255
256 st_surface_data(pipe, dst_surface,
257 0, 0, /* dstx, dsty */
258 srcUB,
259 src_row_pitch,
260 0, 0, /* source x, y */
261 dst->width[level], height); /* width, height */
262
263 pipe_surface_reference(&dst_surface, NULL);
264
265 srcUB += src_image_pitch * dst->cpp;
266 }
267 }
268
269
270 /* Copy mipmap image between textures
271 */
272 void
273 st_texture_image_copy(struct pipe_context *pipe,
274 struct pipe_texture *dst, GLuint dstLevel,
275 struct pipe_texture *src,
276 GLuint face)
277 {
278 GLuint width = dst->width[dstLevel];
279 GLuint height = dst->height[dstLevel];
280 GLuint depth = dst->depth[dstLevel];
281 struct pipe_surface *src_surface;
282 struct pipe_surface *dst_surface;
283 GLuint i;
284
285 /* XXX this is a hack */
286 if (dst->compressed)
287 height /= 4;
288
289 for (i = 0; i < depth; i++) {
290 GLuint srcLevel;
291
292 /* find src texture level of needed size */
293 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
294 if (src->width[srcLevel] == width &&
295 src->height[srcLevel] == height) {
296 break;
297 }
298 }
299 assert(src->width[srcLevel] == width);
300 assert(src->height[srcLevel] == height);
301
302 dst_surface = pipe->get_tex_surface(pipe, dst, face, dstLevel, i);
303 src_surface = pipe->get_tex_surface(pipe, src, face, srcLevel, i);
304
305 pipe->surface_copy(pipe,
306 FALSE,
307 dst_surface,
308 0, 0, /* destX, Y */
309 src_surface,
310 0, 0, /* srcX, Y */
311 width, height);
312
313 pipe_surface_reference(&dst_surface, NULL);
314 pipe_surface_reference(&src_surface, NULL);
315 }
316 }