fix for gl_ztrick bug (Ove Kaaven)
[mesa.git] / src / mesa / main / texutil_tmp.h
1 /* $Id: texutil_tmp.h,v 1.3 2001/03/18 13:34:24 gareth Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Author:
27 * Gareth Hughes <gareth@valinux.com>
28 */
29
30 /*
31 * NOTE: All 3D code is untested and most definitely broken...
32 */
33
34 #define DST_TEXEL_BYTES (4 / DST_TEXELS_PER_DWORD)
35 #define DST_ROW_WIDTH (convert->width * DST_TEXEL_BYTES)
36 #define DST_ROW_STRIDE (convert->imageWidth * DST_TEXEL_BYTES)
37 #define DST_IMG_STRIDE (convert->imageWidth * \
38 convert->imageHeight * DST_TEXEL_BYTES)
39
40
41 /* ================================================================
42 * PRE: No pixelstore attribs, width == imageWidth.
43 */
44 static GLboolean
45 TAG(texsubimage2d)( struct gl_texture_convert *convert )
46 {
47 const GLubyte *src = (const GLubyte *)convert->srcImage;
48 GLuint *dst = (GLuint *)((GLubyte *)convert->dstImage +
49 (convert->yoffset * convert->imageWidth +
50 convert->xoffset) * DST_TEXEL_BYTES);
51 GLint dwords, i;
52 (void) dwords; (void) i;
53
54 if ( DBG )
55 fprintf( stderr, __FUNCTION__ "\n" );
56
57 #ifdef CONVERT_DIRECT
58 MEMCPY( dst, src, convert->height * DST_ROW_WIDTH );
59 #else
60 dwords = (convert->width * convert->height +
61 DST_TEXELS_PER_DWORD - 1) / DST_TEXELS_PER_DWORD;
62
63 for ( i = 0 ; i < dwords ; i++ ) {
64 *dst++ = CONVERT_TEXEL_DWORD( src );
65 src += SRC_TEXEL_BYTES * DST_TEXELS_PER_DWORD;
66 }
67 #endif
68
69 return GL_TRUE;
70 }
71
72 /* PRE: As above, height == imageHeight also.
73 */
74 static GLboolean
75 TAG(texsubimage3d)( struct gl_texture_convert *convert )
76 {
77 const GLubyte *src = (const GLubyte *)convert->srcImage;
78 GLuint *dst = (GLuint *)((GLubyte *)convert->dstImage +
79 ((convert->zoffset * convert->height +
80 convert->yoffset) * convert->width +
81 convert->xoffset) * DST_TEXEL_BYTES);
82 GLint dwords, i;
83 (void) dwords; (void) i;
84
85 if ( DBG )
86 fprintf( stderr, __FUNCTION__ "\n" );
87
88 #ifdef CONVERT_DIRECT
89 MEMCPY( dst, src, convert->depth * convert->height * DST_ROW_WIDTH );
90 #else
91 dwords = (convert->width * convert->height * convert->depth +
92 DST_TEXELS_PER_DWORD - 1) / DST_TEXELS_PER_DWORD;
93
94 for ( i = 0 ; i < dwords ; i++ ) {
95 *dst++ = CONVERT_TEXEL_DWORD( src );
96 src += SRC_TEXEL_BYTES * DST_TEXELS_PER_DWORD;
97 }
98 #endif
99
100 return GL_TRUE;
101 }
102
103
104
105 /* ================================================================
106 * PRE: No pixelstore attribs, width != imageWidth.
107 */
108 static GLboolean
109 TAG(texsubimage2d_stride)( struct gl_texture_convert *convert )
110 {
111 const GLubyte *src = (const GLubyte *)convert->srcImage;
112 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
113 (convert->yoffset * convert->imageWidth +
114 convert->xoffset) * DST_TEXEL_BYTES);
115 GLint adjust;
116 GLint row, col;
117
118 adjust = convert->imageWidth - convert->width;
119
120 if ( DBG ) {
121 fprintf( stderr, __FUNCTION__ ":\n" );
122 fprintf( stderr, " x=%d y=%d w=%d h=%d s=%d\n",
123 convert->xoffset, convert->yoffset, convert->width,
124 convert->height, convert->imageWidth );
125 fprintf( stderr, " adjust=%d\n", adjust );
126 }
127
128 for ( row = 0 ; row < convert->height ; row++ ) {
129 for ( col = 0 ; col < convert->width ; col++ ) {
130 *dst++ = CONVERT_TEXEL( src );
131 src += SRC_TEXEL_BYTES;
132 }
133 dst += adjust;
134 }
135
136 return GL_TRUE;
137 }
138
139 /* PRE: As above, or height != imageHeight also.
140 */
141 static GLboolean
142 TAG(texsubimage3d_stride)( struct gl_texture_convert *convert )
143 {
144 const GLubyte *src = (const GLubyte *)convert->srcImage;
145 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
146 ((convert->zoffset * convert->imageHeight +
147 convert->yoffset) * convert->imageWidth +
148 convert->xoffset) * DST_TEXEL_BYTES);
149 GLint adjust;
150 GLint row, col, img;
151
152 adjust = convert->imageWidth - convert->width;
153
154 if ( DBG ) {
155 fprintf( stderr, __FUNCTION__ ":\n" );
156 fprintf( stderr, " x=%d y=%d w=%d h=%d s=%d\n",
157 convert->xoffset, convert->yoffset, convert->width,
158 convert->height, convert->imageWidth );
159 fprintf( stderr, " adjust=%d\n", adjust );
160 }
161
162 for ( img = 0 ; img < convert->depth ; img++ ) {
163 for ( row = 0 ; row < convert->height ; row++ ) {
164 for ( col = 0 ; col < convert->width ; col++ ) {
165 *dst++ = CONVERT_TEXEL( src );
166 src += SRC_TEXEL_BYTES;
167 }
168 dst += adjust;
169 }
170 /* FIXME: ... */
171 }
172
173 return GL_TRUE;
174 }
175
176
177
178 /* ================================================================
179 * PRE: Require pixelstore attribs, width == imageWidth.
180 */
181 static GLboolean
182 TAG(texsubimage2d_pack)( struct gl_texture_convert *convert )
183 {
184 const GLubyte *src = (const GLubyte *)
185 _mesa_image_address( convert->packing, convert->srcImage,
186 convert->width, convert->height,
187 convert->format, convert->type, 0, 0, 0 );
188 const GLint srcRowStride =
189 _mesa_image_row_stride( convert->packing, convert->width,
190 convert->format, convert->type );
191 GLuint *dst = (GLuint *)((GLubyte *)convert->dstImage +
192 (convert->yoffset * convert->width +
193 convert->xoffset) * DST_TEXEL_BYTES);
194 GLint width;
195 GLint row, col;
196 (void) col;
197
198 if ( DBG )
199 fprintf( stderr, __FUNCTION__ "\n" );
200
201 width = ((convert->width + DST_TEXELS_PER_DWORD - 1)
202 & ~(DST_TEXELS_PER_DWORD - 1));
203
204 for ( row = 0 ; row < convert->height ; row++ ) {
205 #ifdef CONVERT_DIRECT
206 MEMCPY( dst, src, DST_ROW_STRIDE );
207 src += srcRowStride;
208 dst = (GLuint *)((GLubyte *)dst + DST_ROW_STRIDE);
209 #else
210 const GLubyte *srcRow = src;
211 for ( col = width / DST_TEXELS_PER_DWORD ; col ; col-- ) {
212 *dst++ = CONVERT_TEXEL_DWORD( src );
213 src += SRC_TEXEL_BYTES * DST_TEXELS_PER_DWORD;
214 }
215 src = srcRow + srcRowStride;
216 #endif
217 }
218
219 return GL_TRUE;
220 }
221
222 /* PRE: as above, height == imageHeight also.
223 */
224 static GLboolean
225 TAG(texsubimage3d_pack)( struct gl_texture_convert *convert )
226 {
227 const GLubyte *src = (const GLubyte *)
228 _mesa_image_address( convert->packing, convert->srcImage,
229 convert->width, convert->height,
230 convert->format, convert->type, 0, 0, 0 );
231 const GLint srcRowStride =
232 _mesa_image_row_stride( convert->packing, convert->width,
233 convert->format, convert->type );
234 GLuint *dst = (GLuint *)((GLubyte *)convert->dstImage +
235 ((convert->zoffset * convert->height +
236 convert->yoffset) * convert->width +
237 convert->xoffset) * DST_TEXEL_BYTES);
238 GLint width;
239 GLint row, col, img;
240 (void) col;
241
242 if ( DBG )
243 fprintf( stderr, __FUNCTION__ "\n" );
244
245 width = ((convert->width + DST_TEXELS_PER_DWORD - 1)
246 & ~(DST_TEXELS_PER_DWORD - 1));
247
248 for ( img = 0 ; img < convert->depth ; img++ ) {
249 for ( row = 0 ; row < convert->height ; row++ ) {
250 #ifdef CONVERT_DIRECT
251 MEMCPY( dst, src, DST_ROW_STRIDE );
252 src += srcRowStride;
253 dst = (GLuint *)((GLubyte *)dst + DST_ROW_STRIDE);
254 #else
255 const GLubyte *srcRow = src;
256 for ( col = width / DST_TEXELS_PER_DWORD ; col ; col-- ) {
257 *dst++ = CONVERT_TEXEL_DWORD( src );
258 src += SRC_TEXEL_BYTES * DST_TEXELS_PER_DWORD;
259 }
260 src = srcRow + srcRowStride;
261 #endif
262 }
263 }
264
265 return GL_TRUE;
266 }
267
268
269
270 /* ================================================================
271 * PRE: Require pixelstore attribs, width != imageWidth.
272 */
273 static GLboolean
274 TAG(texsubimage2d_stride_pack)( struct gl_texture_convert *convert )
275 {
276 const GLubyte *src = (const GLubyte *)
277 _mesa_image_address( convert->packing, convert->srcImage,
278 convert->width, convert->height,
279 convert->format, convert->type, 0, 0, 0 );
280 const GLint srcRowStride =
281 _mesa_image_row_stride( convert->packing, convert->width,
282 convert->format, convert->type );
283 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
284 (convert->yoffset * convert->imageWidth +
285 convert->xoffset) * DST_TEXEL_BYTES);
286 GLint adjust;
287 GLint row, col;
288 (void) col;
289
290 adjust = convert->imageWidth - convert->width;
291
292 if ( DBG ) {
293 fprintf( stderr, __FUNCTION__ ":\n" );
294 fprintf( stderr, " x=%d y=%d w=%d h=%d s=%d\n",
295 convert->xoffset, convert->yoffset, convert->width,
296 convert->height, convert->imageWidth );
297 fprintf( stderr, " adjust=%d\n", adjust );
298 }
299
300 for ( row = 0 ; row < convert->height ; row++ ) {
301 #ifdef CONVERT_DIRECT
302 MEMCPY( dst, src, DST_ROW_WIDTH );
303 src += srcRowStride;
304 dst += convert->imageWidth;
305 #else
306 const GLubyte *srcRow = src;
307 for ( col = 0 ; col < convert->width ; col++ ) {
308 *dst++ = CONVERT_TEXEL( src );
309 src += SRC_TEXEL_BYTES;
310 }
311 src = srcRow + srcRowStride;
312 dst += adjust;
313 #endif
314 }
315
316 return GL_TRUE;
317 }
318
319 /* PRE: As above, or height != imageHeight also.
320 */
321 static GLboolean
322 TAG(texsubimage3d_stride_pack)( struct gl_texture_convert *convert )
323 {
324 const GLubyte *src = (const GLubyte *)
325 _mesa_image_address( convert->packing, convert->srcImage,
326 convert->width, convert->height,
327 convert->format, convert->type, 0, 0, 0 );
328 const GLint srcRowStride =
329 _mesa_image_row_stride( convert->packing, convert->width,
330 convert->format, convert->type );
331 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
332 ((convert->zoffset * convert->imageHeight +
333 convert->yoffset) * convert->imageWidth +
334 convert->xoffset) * DST_TEXEL_BYTES);
335 GLint adjust;
336 GLint row, col, img;
337 (void) col;
338
339 adjust = convert->imageWidth - convert->width;
340
341 if ( DBG ) {
342 fprintf( stderr, __FUNCTION__ ":\n" );
343 fprintf( stderr, " x=%d y=%d w=%d h=%d s=%d\n",
344 convert->xoffset, convert->yoffset, convert->width,
345 convert->height, convert->imageWidth );
346 fprintf( stderr, " adjust=%d\n", adjust );
347 }
348
349 for ( img = 0 ; img < convert->depth ; img++ ) {
350 for ( row = 0 ; row < convert->height ; row++ ) {
351 #ifdef CONVERT_DIRECT
352 MEMCPY( dst, src, DST_ROW_WIDTH );
353 src += srcRowStride;
354 dst += convert->imageWidth;
355 #else
356 const GLubyte *srcRow = src;
357 for ( col = 0 ; col < convert->width ; col++ ) {
358 *dst++ = CONVERT_TEXEL( src );
359 src += SRC_TEXEL_BYTES;
360 }
361 src = srcRow + srcRowStride;
362 dst += adjust;
363 #endif
364 }
365 /* FIXME: ... */
366 }
367
368 return GL_TRUE;
369 }
370
371
372
373 static convert_func TAG(texsubimage2d_tab)[] = {
374 TAG(texsubimage2d),
375 TAG(texsubimage2d_stride),
376 TAG(texsubimage2d_pack),
377 TAG(texsubimage2d_stride_pack),
378 };
379
380 static convert_func TAG(texsubimage3d_tab)[] = {
381 TAG(texsubimage3d),
382 TAG(texsubimage3d_stride),
383 TAG(texsubimage3d_pack),
384 TAG(texsubimage3d_stride_pack),
385 };
386
387
388 #ifndef PRESERVE_DST_TYPE
389 #undef DST_TYPE
390 #undef DST_TEXELS_PER_DWORD
391 #endif
392
393 #undef SRC_TEXEL_BYTES
394 #undef DST_TEXEL_BYTES
395 #undef DST_ROW_WIDTH
396 #undef DST_ROW_STRIDE
397
398 #undef CONVERT_TEXEL
399 #undef CONVERT_TEXEL_DWORD
400 #undef CONVERT_DIRECT
401
402 #undef TAG
403
404 #undef PRESERVE_DST_TYPE