Hide texture layout details from the state tracker.
[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_util.h"
37 #include "pipe/p_inlines.h"
38 #include "pipe/p_winsys.h"
39
40
41 #define DBG if(0) printf
42
43 #if 0
44 static GLenum
45 target_to_target(GLenum target)
46 {
47 switch (target) {
48 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
49 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
50 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
51 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
52 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
53 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
54 return GL_TEXTURE_CUBE_MAP_ARB;
55 default:
56 return target;
57 }
58 }
59 #endif
60
61 struct pipe_texture *
62 st_texture_create(struct st_context *st,
63 unsigned target,
64 unsigned format,
65 GLenum internal_format,
66 GLuint first_level,
67 GLuint last_level,
68 GLuint width0,
69 GLuint height0,
70 GLuint depth0,
71 GLuint compress_byte)
72 {
73 struct pipe_texture *pt = CALLOC_STRUCT(pipe_texture);
74
75 assert(target <= PIPE_TEXTURE_CUBE);
76
77 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__,
78 _mesa_lookup_enum_by_nr(target),
79 _mesa_lookup_enum_by_nr(internal_format), first_level, last_level);
80
81 if (!pt)
82 return NULL;
83
84 assert(format);
85
86 pt->target = target;
87 pt->format = format;
88 pt->internal_format = internal_format;
89 pt->first_level = first_level;
90 pt->last_level = last_level;
91 pt->width[0] = width0;
92 pt->height[0] = height0;
93 pt->depth[0] = depth0;
94 pt->compressed = compress_byte ? 1 : 0;
95 pt->cpp = pt->compressed ? compress_byte : st_sizeof_format(format);
96 pt->refcount = 1;
97
98 st->pipe->texture_create(st->pipe, &pt);
99
100 return pt;
101 }
102
103
104
105
106 /* Can the image be pulled into a unified mipmap texture. This mirrors
107 * the completeness test in a lot of ways.
108 *
109 * Not sure whether I want to pass gl_texture_image here.
110 */
111 GLboolean
112 st_texture_match_image(struct pipe_texture *pt,
113 struct gl_texture_image *image,
114 GLuint face, GLuint level)
115 {
116 /* Images with borders are never pulled into mipmap textures.
117 */
118 if (image->Border)
119 return GL_FALSE;
120
121 if (image->InternalFormat != pt->internal_format ||
122 image->IsCompressed != pt->compressed)
123 return GL_FALSE;
124
125 /* Test image dimensions against the base level image adjusted for
126 * minification. This will also catch images not present in the
127 * texture, changed targets, etc.
128 */
129 if (image->Width != pt->width[level] ||
130 image->Height != pt->height[level] ||
131 image->Depth != pt->depth[level])
132 return GL_FALSE;
133
134 return GL_TRUE;
135 }
136
137
138 #if 000
139 /* Although we use the image_offset[] array to store relative offsets
140 * to cube faces, Mesa doesn't know anything about this and expects
141 * each cube face to be treated as a separate image.
142 *
143 * These functions present that view to mesa:
144 */
145 const GLuint *
146 st_texture_depth_offsets(struct pipe_texture *pt, GLuint level)
147 {
148 static const GLuint zero = 0;
149
150 if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1)
151 return &zero;
152 else
153 return pt->level[level].image_offset;
154 }
155
156
157 /**
158 * Return the offset to the given mipmap texture image within the
159 * texture memory buffer, in bytes.
160 */
161 GLuint
162 st_texture_image_offset(const struct pipe_texture * pt,
163 GLuint face, GLuint level)
164 {
165 if (pt->target == PIPE_TEXTURE_CUBE)
166 return (pt->level[level].level_offset +
167 pt->level[level].image_offset[face] * pt->cpp);
168 else
169 return pt->level[level].level_offset;
170 }
171 #endif
172
173
174 /**
175 * Map a teximage in a mipmap texture.
176 * \param row_stride returns row stride in bytes
177 * \param image_stride returns image stride in bytes (for 3D textures).
178 * \return address of mapping
179 */
180 GLubyte *
181 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
182 GLuint zoffset)
183 {
184 struct pipe_texture *pt = stImage->pt;
185 DBG("%s \n", __FUNCTION__);
186
187 stImage->surface = st->pipe->get_tex_surface(st->pipe, pt, stImage->face,
188 stImage->level, zoffset);
189
190 (void) st->pipe->region_map(st->pipe, stImage->surface->region);
191
192 return stImage->surface->region->map + stImage->surface->offset;
193 }
194
195 void
196 st_texture_image_unmap(struct st_context *st, struct st_texture_image *stImage)
197 {
198 DBG("%s\n", __FUNCTION__);
199
200 st->pipe->region_unmap(st->pipe, stImage->surface->region);
201
202 pipe_surface_reference(&stImage->surface, NULL);
203 }
204
205
206
207 /* Upload data for a particular image.
208 */
209 void
210 st_texture_image_data(struct pipe_context *pipe,
211 struct pipe_texture *dst,
212 GLuint face,
213 GLuint level,
214 void *src,
215 GLuint src_row_pitch, GLuint src_image_pitch)
216 {
217 GLuint depth = dst->depth[level];
218 GLuint i;
219 GLuint height = 0;
220 const GLubyte *srcUB = src;
221 struct pipe_surface *dst_surface;
222
223 DBG("%s\n", __FUNCTION__);
224 for (i = 0; i < depth; i++) {
225 height = dst->height[level];
226 if(dst->compressed)
227 height /= 4;
228
229 dst_surface = pipe->get_tex_surface(pipe, dst, face, level, i);
230
231 pipe->surface_data(pipe, dst_surface,
232 0, 0, /* dstx, dsty */
233 srcUB,
234 src_row_pitch,
235 0, 0, /* source x, y */
236 dst->width[level], height); /* width, height */
237
238 pipe_surface_reference(&dst_surface, NULL);
239
240 srcUB += src_image_pitch * dst->cpp;
241 }
242 }
243
244 /* Copy mipmap image between textures
245 */
246 void
247 st_texture_image_copy(struct pipe_context *pipe,
248 struct pipe_texture *dst,
249 GLuint face, GLuint level,
250 struct pipe_texture *src)
251 {
252 GLuint width = src->width[level];
253 GLuint height = src->height[level];
254 GLuint depth = src->depth[level];
255 struct pipe_surface *src_surface;
256 struct pipe_surface *dst_surface;
257 GLuint i;
258
259 if (dst->compressed)
260 height /= 4;
261 for (i = 0; i < depth; i++) {
262 dst_surface = pipe->get_tex_surface(pipe, dst, face, level, i);
263 src_surface = pipe->get_tex_surface(pipe, src, face, level, i);
264
265 pipe->surface_copy(pipe,
266 dst_surface,
267 0, 0, /* destX, Y */
268 src_surface,
269 0, 0, /* srcX, Y */
270 width, height);
271
272 pipe_surface_reference(&dst_surface, NULL);
273 pipe_surface_reference(&src_surface, NULL);
274 }
275
276 }