3 * Z buffer: 16 bits Z / 16 bits color
12 ZBuffer
*ZB_open(int xsize
, int ysize
, int mode
,
14 unsigned char *color_indexes
,
21 zb
= gl_malloc(sizeof(ZBuffer
));
28 zb
->linesize
= (xsize
* PSZB
+ 3) & ~3;
31 #ifdef TGL_FEATURE_8_BITS
33 ZB_initDither(zb
, nb_colors
, color_indexes
, color_table
);
36 #ifdef TGL_FEATURE_32_BITS
39 #ifdef TGL_FEATURE_24_BITS
49 size
= zb
->xsize
* zb
->ysize
* sizeof(unsigned short);
51 zb
->zbuf
= gl_malloc(size
);
55 if (frame_buffer
== NULL
) {
56 zb
->pbuf
= gl_malloc(zb
->ysize
* zb
->linesize
);
57 if (zb
->pbuf
== NULL
) {
61 zb
->frame_buffer_allocated
= 1;
63 zb
->frame_buffer_allocated
= 0;
64 zb
->pbuf
= frame_buffer
;
67 zb
->current_texture
= NULL
;
75 void ZB_close(ZBuffer
* zb
)
77 #ifdef TGL_FEATURE_8_BITS
78 if (zb
->mode
== ZB_MODE_INDEX
)
82 if (zb
->frame_buffer_allocated
)
89 void ZB_resize(ZBuffer
* zb
, void *frame_buffer
, int xsize
, int ysize
)
93 /* xsize must be a multiple of 4 */
98 zb
->linesize
= (xsize
* PSZB
+ 3) & ~3;
100 size
= zb
->xsize
* zb
->ysize
* sizeof(unsigned short);
103 zb
->zbuf
= gl_malloc(size
);
105 if (zb
->frame_buffer_allocated
)
108 if (frame_buffer
== NULL
) {
109 zb
->pbuf
= gl_malloc(zb
->ysize
* zb
->linesize
);
110 zb
->frame_buffer_allocated
= 1;
112 zb
->pbuf
= frame_buffer
;
113 zb
->frame_buffer_allocated
= 0;
117 static void ZB_copyBuffer(ZBuffer
* zb
,
127 n
= zb
->xsize
* PSZB
;
128 for (y
= 0; y
< zb
->ysize
; y
++) {
131 q
= (PIXEL
*) ((char *) q
+ zb
->linesize
);
135 #if TGL_FEATURE_RENDER_BITS == 16
139 #ifdef TGL_FEATURE_32_BITS
141 #define RGB16_TO_RGB32(p0,p1,v)\
143 unsigned int g,b,gb;\
144 g = (v & 0x07E007E0) << 5;\
145 b = (v & 0x001F001F) << 3;\
147 p0 = (gb & 0x0000FFFF) | ((v & 0x0000F800) << 8);\
148 p1 = (gb >> 16) | ((v & 0xF8000000) >> 8);\
151 static void ZB_copyFrameBufferRGB32(ZBuffer
* zb
,
156 unsigned int *p
, *p1
, v
, w0
, w1
;
160 p1
= (unsigned int *) buf
;
162 for (y
= 0; y
< zb
->ysize
; y
++) {
166 v
= *(unsigned int *) q
;
167 #if BYTE_ORDER == BIG_ENDIAN
168 RGB16_TO_RGB32(w1
, w0
, v
);
170 RGB16_TO_RGB32(w0
, w1
, v
);
175 v
= *(unsigned int *) (q
+ 2);
176 #if BYTE_ORDER == BIG_ENDIAN
177 RGB16_TO_RGB32(w1
, w0
, v
);
179 RGB16_TO_RGB32(w0
, w1
, v
);
194 /* 24 bit packed pixel handling */
196 #ifdef TGL_FEATURE_24_BITS
198 /* order: RGBR GBRG BRGB */
200 /* XXX: packed pixel 24 bit support not tested */
201 /* XXX: big endian case not optimised */
203 #if BYTE_ORDER == BIG_ENDIAN
205 #define RGB16_TO_RGB24(p0,p1,p2,v1,v2)\
207 unsigned int r1,g1,b1,gb1,g2,b2,gb2;\
208 v1 = (v1 << 16) | (v1 >> 16);\
209 v2 = (v2 << 16) | (v2 >> 16);\
210 r1 = (v1 & 0xF800F800);\
211 g1 = (v1 & 0x07E007E0) << 5;\
212 b1 = (v1 & 0x001F001F) << 3;\
214 p0 = ((gb1 & 0x0000FFFF) << 8) | (r1 << 16) | (r1 >> 24);\
215 g2 = (v2 & 0x07E007E0) << 5;\
216 b2 = (v2 & 0x001F001F) << 3;\
218 p1 = (gb1 & 0xFFFF0000) | (v2 & 0xF800) | ((gb2 >> 8) & 0xff);\
219 p2 = (gb2 << 24) | ((v2 & 0xF8000000) >> 8) | (gb2 >> 16);\
224 #define RGB16_TO_RGB24(p0,p1,p2,v1,v2)\
226 unsigned int r1,g1,b1,gb1,g2,b2,gb2;\
227 r1 = (v1 & 0xF800F800);\
228 g1 = (v1 & 0x07E007E0) << 5;\
229 b1 = (v1 & 0x001F001F) << 3;\
231 p0 = ((gb1 & 0x0000FFFF) << 8) | (r1 << 16) | (r1 >> 24);\
232 g2 = (v2 & 0x07E007E0) << 5;\
233 b2 = (v2 & 0x001F001F) << 3;\
235 p1 = (gb1 & 0xFFFF0000) | (v2 & 0xF800) | ((gb2 >> 8) & 0xff);\
236 p2 = (gb2 << 24) | ((v2 & 0xF8000000) >> 8) | (gb2 >> 16);\
241 static void ZB_copyFrameBufferRGB24(ZBuffer
* zb
,
246 unsigned int *p
, *p1
, w0
, w1
, w2
, v0
, v1
;
250 p1
= (unsigned int *) buf
;
251 linesize
= linesize
* 3;
253 for (y
= 0; y
< zb
->ysize
; y
++) {
257 v0
= *(unsigned int *) q
;
258 v1
= *(unsigned int *) (q
+ 2);
259 RGB16_TO_RGB24(w0
, w1
, w2
, v0
, v1
);
268 p1
= (char *)p1
+ linesize
;
274 void ZB_copyFrameBuffer(ZBuffer
* zb
, void *buf
,
278 #ifdef TGL_FEATURE_8_BITS
280 ZB_ditherFrameBuffer(zb
, buf
, linesize
>> 1);
283 #ifdef TGL_FEATURE_16_BITS
285 ZB_copyBuffer(zb
, buf
, linesize
);
288 #ifdef TGL_FEATURE_32_BITS
290 ZB_copyFrameBufferRGB32(zb
, buf
, linesize
>> 1);
293 #ifdef TGL_FEATURE_24_BITS
295 ZB_copyFrameBufferRGB24(zb
, buf
, linesize
>> 1);
303 #endif /* TGL_FEATURE_RENDER_BITS == 16 */
305 #if TGL_FEATURE_RENDER_BITS == 24
307 #define RGB24_TO_RGB16(r, g, b) \
308 ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
310 /* XXX: not optimized */
311 static void ZB_copyFrameBuffer5R6G5B(ZBuffer
* zb
,
312 void *buf
, int linesize
)
315 unsigned short *p
, *p1
;
319 p1
= (unsigned short *) buf
;
321 for (y
= 0; y
< zb
->ysize
; y
++) {
325 p
[0] = RGB24_TO_RGB16(q
[0], q
[1], q
[2]);
326 p
[1] = RGB24_TO_RGB16(q
[3], q
[4], q
[5]);
327 p
[2] = RGB24_TO_RGB16(q
[6], q
[7], q
[8]);
328 p
[3] = RGB24_TO_RGB16(q
[9], q
[10], q
[11]);
329 q
= (PIXEL
*)((char *)q
+ 4 * PSZB
);
332 p1
= (unsigned short *)((char *)p1
+ linesize
);
336 void ZB_copyFrameBuffer(ZBuffer
* zb
, void *buf
,
340 #ifdef TGL_FEATURE_16_BITS
342 ZB_copyFrameBuffer5R6G5B(zb
, buf
, linesize
);
345 #ifdef TGL_FEATURE_24_BITS
347 ZB_copyBuffer(zb
, buf
, linesize
);
355 #endif /* TGL_FEATURE_RENDER_BITS == 24 */
357 #if TGL_FEATURE_RENDER_BITS == 32
359 #define RGB32_TO_RGB16(v) \
360 (((v >> 8) & 0xf800) | (((v) >> 5) & 0x07e0) | (((v) & 0xff) >> 3))
362 /* XXX: not optimized */
363 static void ZB_copyFrameBuffer5R6G5B(ZBuffer
* zb
,
364 void *buf
, int linesize
)
367 unsigned short *p
, *p1
;
371 p1
= (unsigned short *) buf
;
373 for (y
= 0; y
< zb
->ysize
; y
++) {
377 p
[0] = RGB32_TO_RGB16(q
[0]);
378 p
[1] = RGB32_TO_RGB16(q
[1]);
379 p
[2] = RGB32_TO_RGB16(q
[2]);
380 p
[3] = RGB32_TO_RGB16(q
[3]);
384 p1
= (unsigned short *)((char *)p1
+ linesize
);
388 void ZB_copyFrameBuffer(ZBuffer
* zb
, void *buf
,
392 #ifdef TGL_FEATURE_16_BITS
394 ZB_copyFrameBuffer5R6G5B(zb
, buf
, linesize
);
397 #ifdef TGL_FEATURE_32_BITS
399 ZB_copyBuffer(zb
, buf
, linesize
);
407 #endif /* TGL_FEATURE_RENDER_BITS == 32 */
411 * adr must be aligned on an 'int'
413 void memset_s(void *adr
, int val
, int count
)
420 v
= val
| (val
<< 16);
423 for (i
= 0; i
< n
; i
++) {
431 q
= (unsigned short *) p
;
433 for (i
= 0; i
< n
; i
++)
437 void memset_l(void *adr
, int val
, int count
)
445 for (i
= 0; i
< n
; i
++) {
454 for (i
= 0; i
< n
; i
++)
458 /* count must be a multiple of 4 and >= 4 */
459 void memset_RGB24(void *adr
,int r
, int v
, int b
,long count
)
462 register long v1
,v2
,v3
,*pt
=(long *)(adr
);
463 unsigned char *p
,R
=(unsigned char)r
,V
=(unsigned char)v
,B
=(unsigned char)b
;
465 p
=(unsigned char *)adr
;
489 void ZB_clear(ZBuffer
* zb
, int clear_z
, int z
,
490 int clear_color
, int r
, int g
, int b
)
492 #if TGL_FEATURE_RENDER_BITS != 24
499 memset_s(zb
->zbuf
, z
, zb
->xsize
* zb
->ysize
);
503 for (y
= 0; y
< zb
->ysize
; y
++) {
504 #if TGL_FEATURE_RENDER_BITS == 15 || TGL_FEATURE_RENDER_BITS == 16
505 color
= RGB_TO_PIXEL(r
, g
, b
);
506 memset_s(pp
, color
, zb
->xsize
);
507 #elif TGL_FEATURE_RENDER_BITS == 32
508 color
= RGB_TO_PIXEL(r
, g
, b
);
509 memset_l(pp
, color
, zb
->xsize
);
510 #elif TGL_FEATURE_RENDER_BITS == 24
511 memset_RGB24(pp
,r
>>8,g
>>8,b
>>8,zb
->xsize
);
515 pp
= (PIXEL
*) ((char *) pp
+ zb
->linesize
);