Committing in .
[mesa.git] / src / mesa / main / texutil_tmp.h
1 /* $Id: texutil_tmp.h,v 1.9 2002/02/21 15:12:31 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.0.2
6 *
7 * Copyright (C) 1999-2002 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 teximage code is untested and most definitely broken...
32 */
33
34
35 #define DST_TEXEL_BYTES (4 / DST_TEXELS_PER_DWORD)
36 #define DST_ROW_BYTES (convert->width * DST_TEXEL_BYTES)
37 #define DST_ROW_STRIDE (convert->dstImageWidth * DST_TEXEL_BYTES)
38 #define DST_IMG_STRIDE (convert->dstImageWidth * \
39 convert->dstImageHeight * DST_TEXEL_BYTES)
40
41
42 /* =============================================================
43 * PRE: No pixelstore attribs, width == dstImageWidth.
44 */
45 static GLboolean
46 TAG(texsubimage2d)( struct gl_texture_convert *convert )
47 {
48 const GLubyte *src = (const GLubyte *)convert->srcImage;
49 GLuint *dst = (GLuint *)((GLubyte *)convert->dstImage +
50 (convert->yoffset * convert->dstImageWidth +
51 convert->xoffset) * DST_TEXEL_BYTES);
52
53 #if DEBUG_TEXUTIL
54 fprintf( stderr, __FUNCTION__ "\n" );
55 #endif
56
57 #ifdef CONVERT_DIRECT
58 MEMCPY( dst, src, convert->height * DST_ROW_BYTES );
59 #else
60 {
61 const GLint texels = convert->width * convert->height;
62 const GLint dwords = texels / DST_TEXELS_PER_DWORD;
63 const GLint leftover = texels - dwords * DST_TEXELS_PER_DWORD;
64 GLint i;
65 for ( i = 0 ; i < dwords ; i++ ) {
66 CONVERT_TEXEL_DWORD( *dst++, src );
67 src += SRC_TEXEL_BYTES * DST_TEXELS_PER_DWORD;
68 }
69 for ( i = 0; i < leftover; i++ ) {
70 CONVERT_TEXEL( *dst++, src );
71 src += SRC_TEXEL_BYTES;
72 }
73 }
74 #endif
75
76 return GL_TRUE;
77 }
78
79 /* PRE: As above, height == dstImageHeight also.
80 */
81 static GLboolean
82 TAG(texsubimage3d)( struct gl_texture_convert *convert )
83 {
84 const GLubyte *src = (const GLubyte *)convert->srcImage;
85 GLuint *dst = (GLuint *)((GLubyte *)convert->dstImage +
86 ((convert->zoffset * convert->height +
87 convert->yoffset) * convert->width +
88 convert->xoffset) * DST_TEXEL_BYTES);
89 #if DEBUG_TEXUTIL
90 fprintf( stderr, __FUNCTION__ "\n" );
91 #endif
92
93 #ifdef CONVERT_DIRECT
94 MEMCPY( dst, src, convert->depth * convert->height * DST_ROW_BYTES );
95 #else
96 {
97 const GLint texels = convert->width * convert->height * convert->depth;
98 const GLint dwords = texels / DST_TEXELS_PER_DWORD;
99 const GLint leftover = texels - dwords * DST_TEXELS_PER_DWORD;
100 GLint i;
101 for ( i = 0 ; i < dwords ; i++ ) {
102 CONVERT_TEXEL_DWORD( *dst++, src );
103 src += SRC_TEXEL_BYTES * DST_TEXELS_PER_DWORD;
104 }
105 for ( i = 0; i < leftover; i++ ) {
106 CONVERT_TEXEL( *dst++, src );
107 src += SRC_TEXEL_BYTES;
108 }
109 }
110 #endif
111
112 return GL_TRUE;
113 }
114
115
116
117 /* =============================================================
118 * PRE: No pixelstore attribs, width != dstImageWidth.
119 */
120 static GLboolean
121 TAG(texsubimage2d_stride)( struct gl_texture_convert *convert )
122 {
123 const GLubyte *src = (const GLubyte *)convert->srcImage;
124 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
125 (convert->yoffset * convert->dstImageWidth +
126 convert->xoffset) * DST_TEXEL_BYTES);
127 GLint adjust;
128 GLint row, col;
129
130 adjust = convert->dstImageWidth - convert->width;
131
132 #if DEBUG_TEXUTIL
133 fprintf( stderr, __FUNCTION__ ":\n" );
134 fprintf( stderr, " x=%d y=%d w=%d h=%d s=%d\n",
135 convert->xoffset, convert->yoffset, convert->width,
136 convert->height, convert->dstImageWidth );
137 fprintf( stderr, " adjust=%d\n", adjust );
138 #endif
139
140 for ( row = 0 ; row < convert->height ; row++ ) {
141 for ( col = 0 ; col < convert->width ; col++ ) {
142 CONVERT_TEXEL( *dst++, src );
143 src += SRC_TEXEL_BYTES;
144 }
145 dst += adjust;
146 }
147
148 return GL_TRUE;
149 }
150
151 /* PRE: As above, or height != dstImageHeight also.
152 */
153 static GLboolean
154 TAG(texsubimage3d_stride)( struct gl_texture_convert *convert )
155 {
156 const GLubyte *src = (const GLubyte *)convert->srcImage;
157 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
158 ((convert->zoffset * convert->dstImageHeight +
159 convert->yoffset) * convert->dstImageWidth +
160 convert->xoffset) * DST_TEXEL_BYTES);
161 GLint adjust;
162 GLint row, col, img;
163
164 adjust = convert->dstImageWidth - convert->width;
165
166 #if DEBUG_TEXUTIL
167 fprintf( stderr, __FUNCTION__ ":\n" );
168 fprintf( stderr, " x=%d y=%d w=%d h=%d s=%d\n",
169 convert->xoffset, convert->yoffset, convert->width,
170 convert->height, convert->dstImageWidth );
171 fprintf( stderr, " adjust=%d\n", adjust );
172 #endif
173
174 for ( img = 0 ; img < convert->depth ; img++ ) {
175 for ( row = 0 ; row < convert->height ; row++ ) {
176 for ( col = 0 ; col < convert->width ; col++ ) {
177 CONVERT_TEXEL( *dst++, src );
178 src += SRC_TEXEL_BYTES;
179 }
180 dst += adjust;
181 }
182 /* FIXME: ... */
183 }
184
185 return GL_TRUE;
186 }
187
188
189
190 /* =============================================================
191 * PRE: Require pixelstore attribs, width == dstImageWidth.
192 */
193 static GLboolean
194 TAG(texsubimage2d_unpack)( struct gl_texture_convert *convert )
195 {
196 const GLubyte *src = (const GLubyte *)
197 _mesa_image_address( convert->unpacking, convert->srcImage,
198 convert->width, convert->height,
199 convert->format, convert->type, 0, 0, 0 );
200 const GLint srcRowStride =
201 _mesa_image_row_stride( convert->unpacking, convert->width,
202 convert->format, convert->type );
203 GLint row, col;
204
205 #if DEBUG_TEXUTIL
206 fprintf( stderr, __FUNCTION__ "\n" );
207 #endif
208
209 if (convert->width & (DST_TEXELS_PER_DWORD - 1)) {
210 /* Can't use dword conversion (i.e. when width = 1 and texels/dword = 2
211 * or width = 2 and texels/dword = 4).
212 */
213 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
214 (convert->yoffset * convert->width +
215 convert->xoffset) * DST_TEXEL_BYTES);
216 for ( row = 0 ; row < convert->height ; row++ ) {
217 const GLubyte *srcRow = src;
218 for ( col = 0; col < convert->width; col++ ) {
219 CONVERT_TEXEL(*dst, src);
220 src += SRC_TEXEL_BYTES;
221 }
222 src = srcRow + srcRowStride;
223 }
224 }
225 else {
226 /* the common case */
227 GLuint *dst = (GLuint *)((GLubyte *)convert->dstImage +
228 (convert->yoffset * convert->width +
229 convert->xoffset) * DST_TEXEL_BYTES);
230 for ( row = 0 ; row < convert->height ; row++ ) {
231 #ifdef CONVERT_DIRECT
232 MEMCPY( dst, src, DST_ROW_STRIDE );
233 src += srcRowStride;
234 dst = (GLuint *)((GLubyte *)dst + DST_ROW_STRIDE);
235 #else
236 const GLubyte *srcRow = src;
237 for ( col = convert->width / DST_TEXELS_PER_DWORD ; col ; col-- ) {
238 CONVERT_TEXEL_DWORD( *dst++, src );
239 src += SRC_TEXEL_BYTES * DST_TEXELS_PER_DWORD;
240 }
241 src = srcRow + srcRowStride;
242 #endif
243 }
244 }
245
246 return GL_TRUE;
247 }
248
249 /* PRE: as above, height == dstImageHeight also.
250 */
251 static GLboolean
252 TAG(texsubimage3d_unpack)( struct gl_texture_convert *convert )
253 {
254 const GLubyte *src = (const GLubyte *)
255 _mesa_image_address( convert->unpacking, convert->srcImage,
256 convert->width, convert->height,
257 convert->format, convert->type, 0, 0, 0 );
258 const GLint srcImgStride = (const GLubyte *)
259 _mesa_image_address( convert->unpacking, convert->srcImage,
260 convert->width, convert->height,
261 convert->format, convert->type, 1, 0, 0 ) - src;
262 const GLint srcRowStride =
263 _mesa_image_row_stride( convert->unpacking, convert->width,
264 convert->format, convert->type );
265 GLint row, col, img;
266
267 #if DEBUG_TEXUTIL
268 fprintf( stderr, __FUNCTION__ "\n" );
269 #endif
270
271 if (convert->width & (DST_TEXELS_PER_DWORD - 1)) {
272 /* Can't use dword conversion (i.e. when width = 1 and texels/dword = 2
273 * or width = 2 and texels/dword = 4).
274 */
275 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
276 ((convert->zoffset * convert->height +
277 convert->yoffset) * convert->width +
278 convert->xoffset) * DST_TEXEL_BYTES);
279 for ( img = 0 ; img < convert->depth ; img++ ) {
280 const GLubyte *srcImage = src;
281 for ( row = 0 ; row < convert->height ; row++ ) {
282 const GLubyte *srcRow = src;
283 for ( col = 0; col < convert->width; col++ ) {
284 CONVERT_TEXEL(*dst, src);
285 src += SRC_TEXEL_BYTES;
286 }
287 src = srcRow + srcRowStride;
288 }
289 src = srcImage + srcImgStride;
290 }
291 }
292 else {
293 /* the common case */
294 GLuint *dst = (GLuint *)((GLubyte *)convert->dstImage +
295 ((convert->zoffset * convert->height +
296 convert->yoffset) * convert->width +
297 convert->xoffset) * DST_TEXEL_BYTES);
298 for ( img = 0 ; img < convert->depth ; img++ ) {
299 const GLubyte *srcImage = src;
300 for ( row = 0 ; row < convert->height ; row++ ) {
301 #ifdef CONVERT_DIRECT
302 MEMCPY( dst, src, DST_ROW_STRIDE );
303 src += srcRowStride;
304 dst = (GLuint *)((GLubyte *)dst + DST_ROW_STRIDE);
305 #else
306 const GLubyte *srcRow = src;
307 for ( col = convert->width / DST_TEXELS_PER_DWORD ; col ; col-- ) {
308 CONVERT_TEXEL_DWORD( *dst++, src );
309 src += SRC_TEXEL_BYTES * DST_TEXELS_PER_DWORD;
310 }
311 src = srcRow + srcRowStride;
312 #endif
313 }
314 src = srcImage + srcImgStride;
315 }
316 }
317
318 return GL_TRUE;
319 }
320
321
322
323 /* =============================================================
324 * PRE: Require pixelstore attribs, width != dstImageWidth.
325 */
326 static GLboolean
327 TAG(texsubimage2d_stride_unpack)( struct gl_texture_convert *convert )
328 {
329 const GLubyte *src = (const GLubyte *)
330 _mesa_image_address( convert->unpacking, convert->srcImage,
331 convert->width, convert->height,
332 convert->format, convert->type, 0, 0, 0 );
333 const GLint srcRowStride =
334 _mesa_image_row_stride( convert->unpacking, convert->width,
335 convert->format, convert->type );
336 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
337 (convert->yoffset * convert->dstImageWidth +
338 convert->xoffset) * DST_TEXEL_BYTES);
339 GLint adjust;
340 GLint row, col;
341 (void) col;
342
343 adjust = convert->dstImageWidth - convert->width;
344
345 #if DEBUG_TEXUTIL
346 fprintf( stderr, __FUNCTION__ ":\n" );
347 fprintf( stderr, " x=%d y=%d w=%d h=%d s=%d\n",
348 convert->xoffset, convert->yoffset, convert->width,
349 convert->height, convert->dstImageWidth );
350 fprintf( stderr, " adjust=%d\n", adjust );
351 #endif
352
353 for ( row = 0 ; row < convert->height ; row++ ) {
354 #ifdef CONVERT_DIRECT
355 MEMCPY( dst, src, DST_ROW_BYTES );
356 src += srcRowStride;
357 dst += convert->dstImageWidth;
358 #else
359 const GLubyte *srcRow = src;
360 for ( col = 0 ; col < convert->width ; col++ ) {
361 CONVERT_TEXEL( *dst++, src );
362 src += SRC_TEXEL_BYTES;
363 }
364 src = srcRow + srcRowStride;
365 dst += adjust;
366 #endif
367 }
368
369 return GL_TRUE;
370 }
371
372 /* PRE: As above, or height != dstImageHeight also.
373 */
374 static GLboolean
375 TAG(texsubimage3d_stride_unpack)( struct gl_texture_convert *convert )
376 {
377 const GLubyte *src = (const GLubyte *)
378 _mesa_image_address( convert->unpacking, convert->srcImage,
379 convert->width, convert->height,
380 convert->format, convert->type, 0, 0, 0 );
381 const GLint srcImgStride = (const GLubyte *)
382 _mesa_image_address( convert->unpacking, convert->srcImage,
383 convert->width, convert->height,
384 convert->format, convert->type, 1, 0, 0 ) - src;
385 const GLint srcRowStride =
386 _mesa_image_row_stride( convert->unpacking, convert->width,
387 convert->format, convert->type );
388 DST_TYPE *dst = (DST_TYPE *)((GLubyte *)convert->dstImage +
389 ((convert->zoffset * convert->dstImageHeight +
390 convert->yoffset) * convert->dstImageWidth +
391 convert->xoffset) * DST_TEXEL_BYTES);
392 GLint adjust;
393 GLint row, col, img;
394 (void) col;
395
396 adjust = convert->dstImageWidth - convert->width;
397
398 #if DEBUG_TEXUTIL
399 fprintf( stderr, __FUNCTION__ ":\n" );
400 fprintf( stderr, " x=%d y=%d w=%d h=%d s=%d\n",
401 convert->xoffset, convert->yoffset, convert->width,
402 convert->height, convert->dstImageWidth );
403 fprintf( stderr, " adjust=%d\n", adjust );
404 #endif
405
406 for ( img = 0 ; img < convert->depth ; img++ ) {
407 const GLubyte *srcImage = src;
408 for ( row = 0 ; row < convert->height ; row++ ) {
409 #ifdef CONVERT_DIRECT
410 MEMCPY( dst, src, DST_ROW_BYTES );
411 src += srcRowStride;
412 dst += convert->dstImageWidth;
413 #else
414 const GLubyte *srcRow = src;
415 for ( col = 0 ; col < convert->width ; col++ ) {
416 CONVERT_TEXEL( *dst++, src );
417 src += SRC_TEXEL_BYTES;
418 }
419 src = srcRow + srcRowStride;
420 dst += adjust;
421 #endif
422 }
423 src = srcImage + srcImgStride;
424 }
425
426 return GL_TRUE;
427 }
428
429
430
431 static convert_func TAG(texsubimage2d_tab)[] = {
432 TAG(texsubimage2d),
433 TAG(texsubimage2d_stride),
434 TAG(texsubimage2d_unpack),
435 TAG(texsubimage2d_stride_unpack),
436 };
437
438 static convert_func TAG(texsubimage3d_tab)[] = {
439 TAG(texsubimage3d),
440 TAG(texsubimage3d_stride),
441 TAG(texsubimage3d_unpack),
442 TAG(texsubimage3d_stride_unpack),
443 };
444
445
446 #ifndef PRESERVE_DST_TYPE
447 #undef DST_TYPE
448 #undef DST_TEXELS_PER_DWORD
449 #endif
450
451 #undef SRC_TEXEL_BYTES
452 #undef DST_TEXEL_BYTES
453 #undef DST_ROW_BYTES
454 #undef DST_ROW_STRIDE
455
456 #undef CONVERT_TEXEL
457 #undef CONVERT_TEXEL_DWORD
458 #undef CONVERT_DIRECT
459
460 #undef TAG
461
462 #undef PRESERVE_DST_TYPE