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