Panda3D
 All Classes Functions Variables Enumerations
zbuffer.h
1 #ifndef _tgl_zbuffer_h_
2 #define _tgl_zbuffer_h_
3 
4 /*
5  * Z buffer
6  */
7 
8 #include "zfeatures.h"
9 #include "pbitops.h"
10 #include "srgb_tables.h"
11 
12 typedef unsigned int ZPOINT;
13 #define ZB_Z_BITS 20
14 #define ZB_POINT_Z_FRAC_BITS 10 // These must add to < 32.
15 
16 /* The number of fractional bits below the S and T texture coords.
17  The more we have, the more precise the texel calculation will be
18  when we zoom into small details of a texture; but the greater
19  chance we might overflow our 32-bit integer when texcoords get
20  large. This also limits our greatest texture size (its T dimension
21  cannot exceed this number of bits).*/
22 #define ZB_POINT_ST_FRAC_BITS 12
23 
24 /* This is the theoretical max number of bits we have available to
25  shift down to achieve each next mipmap level, based on the size of
26  a 32-bit int. We need to preallocate mipmap arrays of this size. */
27 #define MAX_MIPMAP_LEVELS (32 - ZB_POINT_ST_FRAC_BITS + 1)
28 
29 /* Returns the index within a texture level for the given (s, t) texel. */
30 #define ZB_TEXEL(texture_level, s, t) \
31  ((((t) & (texture_level).t_mask) >> (texture_level).t_shift) | \
32  (((s) & (texture_level).s_mask) >> (texture_level).s_shift))
33 
34 #define ZB_LOOKUP_TEXTURE_NEAREST(texture_def, s, t) \
35  (texture_def)->levels[0].pixmap[ZB_TEXEL((texture_def)->levels[0], s, t)]
36 
37 #define ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level) \
38  (texture_def)->levels[(level)].pixmap[ZB_TEXEL((texture_def)->levels[(level)], s, t)]
39 
40 /* A special abs() function which doesn't require any branching
41  instructions. Might not work on some exotic hardware. */
42 
43 /* Also doesn't appear to be any faster in practice. Guess gcc is
44  already doing the right thing. Is msvc? */
45 //#define FAST_ABS(v) (((v) ^ ((v) >> (sizeof(v) * 8 - 1))) - ((v) >> (sizeof(v) * 8 - 1)))
46 
47 #define DO_CALC_MIPMAP_LEVEL(mipmap_level, mipmap_dx, dsdx, dtdx) \
48  { \
49  (mipmap_dx) = ((unsigned int)abs(dsdx) + (unsigned int)abs(dtdx)); \
50  (mipmap_level) = get_next_higher_bit((mipmap_dx) >> ZB_POINT_ST_FRAC_BITS); \
51  (mipmap_dx) &= ((1 << (((mipmap_level) - 1) + ZB_POINT_ST_FRAC_BITS)) - 1); \
52  }
53 
54 #define ZB_POINT_RED_MIN 0x0000
55 #define ZB_POINT_RED_MAX 0xffff
56 #define ZB_POINT_GREEN_MIN 0x0000
57 #define ZB_POINT_GREEN_MAX 0xffff
58 #define ZB_POINT_BLUE_MIN 0x0000
59 #define ZB_POINT_BLUE_MAX 0xffff
60 #define ZB_POINT_ALPHA_MIN 0x0000
61 #define ZB_POINT_ALPHA_MAX 0xffff
62 
63 /* display modes */
64 #define ZB_MODE_5R6G5B 1 /* true color 16 bits */
65 #define ZB_MODE_INDEX 2 /* color index 8 bits */
66 #define ZB_MODE_RGBA 3 /* 32 bit rgba mode */
67 #define ZB_MODE_RGB24 4 /* 24 bit rgb mode */
68 #define ZB_NB_COLORS 225 /* number of colors for 8 bit display */
69 
70 #define RGB_TO_PIXEL(r,g,b) \
71  ((((unsigned int)(r) << 8) & 0xff0000) | ((unsigned int)(g) & 0xff00) | ((unsigned int)(b) >> 8))
72 #define RGBA_TO_PIXEL(r,g,b,a) \
73  ((((unsigned int)(a) << 16) & 0xff000000) | (((unsigned int)(r) << 8) & 0xff0000) | ((unsigned int)(g) & 0xff00) | ((unsigned int)(b) >> 8))
74 #define RGBA8_TO_PIXEL(r,g,b,a) \
75  ((((unsigned int)(a) << 24) & 0xff000000) | (((unsigned int)(r) << 16) & 0xff0000) | (((unsigned int)(g) << 8) & 0xff00) | (unsigned int)(b))
76 
77 #define SRGB_TO_PIXEL(r,g,b) \
78  ((encode_sRGB[(unsigned int)(r) >> 4] << 16) | (encode_sRGB[(unsigned int)(g) >> 4] << 8) | (encode_sRGB[(unsigned int)(b) >> 4]))
79 #define SRGBA_TO_PIXEL(r,g,b,a) \
80  ((((unsigned int)(a) << 16) & 0xff000000) | (encode_sRGB[(unsigned int)(r) >> 4] << 16) | (encode_sRGB[(unsigned int)(g) >> 4] << 8) | (encode_sRGB[(unsigned int)(b) >> 4]))
81 
82 #define PIXEL_R(p) (((unsigned int)(p) & 0xff0000) >> 8)
83 #define PIXEL_G(p) ((unsigned int)(p) & 0xff00)
84 #define PIXEL_B(p) (((unsigned int)(p) & 0x00ff) << 8)
85 #define PIXEL_A(p) (((unsigned int)(p) & 0xff000000) >> 16)
86 #define PIXEL_SR(p) (decode_sRGB[((unsigned int)(p) & 0xff0000) >> 16])
87 #define PIXEL_SG(p) (decode_sRGB[((unsigned int)(p) & 0xff00) >> 8])
88 #define PIXEL_SB(p) (decode_sRGB[((unsigned int)(p) & 0x00ff)])
89 typedef unsigned int PIXEL;
90 #define PSZB 4
91 #define PSZSH 5
92 
93 // Returns an unsigned product of c1 * c2
94 #define PCOMPONENT_MULT(c1, c2) \
95  ((((unsigned int)(c1) * (unsigned int)(c2))) >> 16)
96 #define PCOMPONENT_MULT3(c1, c2, c3) \
97  PCOMPONENT_MULT(c1, PCOMPONENT_MULT(c2, c3))
98 #define PCOMPONENT_MULT4(c1, c2, c3, c4) \
99  PCOMPONENT_MULT(PCOMPONENT_MULT(c1, c2), PCOMPONENT_MULT(c3, c4))
100 
101 // Returns a signed product of c1 * c2, where c1 is initially signed.
102 // We leave 2 bits on the top to differentiate between c1 < 0 and c1 >
103 // 0xffff; the result has the same sign.
104 #define PALPHA_MULT(c1, c2) \
105  (((int)(((int)(c1) >> 2) * (unsigned int)(c2))) >> 14)
106 
107 #define PCOMPONENT_BLEND(c1, c2, a2) \
108  ((((unsigned int)(c1) * ((unsigned int)0xffff - (unsigned int)(a2)) + (unsigned int)(c2) * (unsigned int)(a2))) >> 16)
109 
110 #define _BLEND_RGB(r1, g1, b1, r2, g2, b2, a2) \
111  RGBA_TO_PIXEL(PCOMPONENT_BLEND(r1, r2, a2), \
112  PCOMPONENT_BLEND(g1, g2, a2), \
113  PCOMPONENT_BLEND(b1, b2, a2), \
114  a2)
115 
116 #define _BLEND_SRGB(r1, g1, b1, r2, g2, b2, a2) \
117  SRGBA_TO_PIXEL(PCOMPONENT_BLEND(r1, r2, a2), \
118  PCOMPONENT_BLEND(g1, g2, a2), \
119  PCOMPONENT_BLEND(b1, b2, a2), \
120  a2)
121 
122 #define PIXEL_BLEND_RGB(rgb, r, g, b, a) \
123  _BLEND_RGB(PIXEL_R(rgb), PIXEL_G(rgb), PIXEL_B(rgb), r, g, b, a)
124 
125 #define PIXEL_BLEND_SRGB(rgb, r, g, b, a) \
126  _BLEND_SRGB(PIXEL_SR(rgb), PIXEL_SG(rgb), PIXEL_SB(rgb), r, g, b, a)
127 
128 
129 typedef struct {
130  PIXEL *pixmap;
131  unsigned int s_mask, s_shift, t_mask, t_shift;
132 } ZTextureLevel;
133 
134 typedef struct ZBuffer ZBuffer;
135 typedef struct ZBufferPoint ZBufferPoint;
136 typedef struct ZTextureDef ZTextureDef;
137 
138 typedef void (*ZB_fillTriangleFunc)(ZBuffer *, ZBufferPoint *, ZBufferPoint *, ZBufferPoint *);
139 
140 typedef void (*ZB_storePixelFunc)(ZBuffer *zb, PIXEL &result, int r, int g, int b, int a);
141 
142 typedef PIXEL (*ZB_lookupTextureFunc)(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
143 
144 typedef int (*ZB_texWrapFunc)(int coord, int max_coord);
145 
146 struct ZTextureDef {
147  ZTextureLevel *levels;
148  ZB_lookupTextureFunc tex_minfilter_func;
149  ZB_lookupTextureFunc tex_magfilter_func;
150  ZB_lookupTextureFunc tex_minfilter_func_impl;
151  ZB_lookupTextureFunc tex_magfilter_func_impl;
152  ZB_texWrapFunc tex_wrap_u_func;
153  ZB_texWrapFunc tex_wrap_v_func;
154  int s_max, t_max;
155  PIXEL border_color;
156 };
157 
158 struct ZBuffer {
159  int xsize,ysize;
160  int linesize; /* line size, in bytes */
161  int mode;
162 
163  ZPOINT *zbuf;
164  PIXEL *pbuf;
165  int frame_buffer_allocated;
166 
167  int nb_colors;
168  unsigned char *dctable;
169  int *ctable;
170  ZTextureDef current_textures[MAX_TEXTURE_STAGES];
171  int reference_alpha;
172  int blend_r, blend_g, blend_b, blend_a;
173  ZB_storePixelFunc store_pix_func;
174 };
175 
176 struct ZBufferPoint {
177  int x,y,z; /* integer coordinates in the zbuffer */
178  int s,t; /* coordinates for the mapping */
179  int r,g,b,a; /* color indexes */
180 
181  PN_stdfloat sz,tz; /* temporary coordinates for mapping */
182 
183  int sa, ta; /* mapping coordinates for optional second texture stage */
184  PN_stdfloat sza,tza;
185 
186  int sb, tb; /* mapping coordinates for optional third texture stage */
187  PN_stdfloat szb,tzb;
188 };
189 
190 /* zbuffer.c */
191 
192 #ifdef DO_PSTATS
193 extern int pixel_count_white_untextured;
194 extern int pixel_count_flat_untextured;
195 extern int pixel_count_smooth_untextured;
196 extern int pixel_count_white_textured;
197 extern int pixel_count_flat_textured;
198 extern int pixel_count_smooth_textured;
199 extern int pixel_count_white_perspective;
200 extern int pixel_count_flat_perspective;
201 extern int pixel_count_smooth_perspective;
202 extern int pixel_count_smooth_multitex2;
203 extern int pixel_count_smooth_multitex3;
204 
205 #define COUNT_PIXELS(pixel_count, p0, p1, p2) \
206  (pixel_count) += abs((p0)->x * ((p1)->y - (p2)->y) + (p1)->x * ((p2)->y - (p0)->y) + (p2)->x * ((p0)->y - (p1)->y)) / 2
207 
208 #else
209 
210 #define COUNT_PIXELS(pixel_count, p0, p1, p2)
211 
212 #endif // DO_PSTATS
213 
214 ZBuffer *ZB_open(int xsize,int ysize,int mode,
215  int nb_colors,
216  unsigned char *color_indexes,
217  unsigned int *color_table,
218  void *frame_buffer);
219 
220 
221 void ZB_close(ZBuffer *zb);
222 
223 void ZB_resize(ZBuffer *zb,void *frame_buffer,int xsize,int ysize);
224 void ZB_clear(ZBuffer *zb, int clear_z, ZPOINT z, int clear_color, PIXEL color);
225 void ZB_clear_viewport(ZBuffer * zb, int clear_z, ZPOINT z, int clear_color, PIXEL color,
226  int xmin, int ymin, int xsize, int ysize);
227 
228 PIXEL lookup_texture_nearest(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
229 PIXEL lookup_texture_bilinear(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
230 PIXEL lookup_texture_mipmap_nearest(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
231 PIXEL lookup_texture_mipmap_linear(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
232 PIXEL lookup_texture_mipmap_bilinear(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
233 PIXEL lookup_texture_mipmap_trilinear(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
234 
235 PIXEL apply_wrap_general_minfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
236 PIXEL apply_wrap_general_magfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
237 
238 PIXEL apply_wrap_border_color_minfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
239 PIXEL apply_wrap_border_color_magfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
240 
241 PIXEL apply_wrap_clamp_minfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
242 PIXEL apply_wrap_clamp_magfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
243 
244 int texcoord_clamp(int coord, int max_coord);
245 int texcoord_repeat(int coord, int max_coord);
246 int texcoord_mirror(int coord, int max_coord);
247 int texcoord_mirror_once(int coord, int max_coord);
248 
249 /* linesize is in BYTES */
250 void ZB_copyFrameBuffer(const ZBuffer *zb,void *buf,int linesize);
251 void ZB_copyFrameBufferNoAlpha(const ZBuffer *zb,void *buf,int linesize);
252 void ZB_zoomFrameBuffer(ZBuffer *dest, int dest_xmin, int dest_ymin,
253  int dest_xsize, int dest_ysize,
254  const ZBuffer *source, int source_xmin, int source_ymin,
255  int source_xsize, int source_ysize);
256 
257 /* zdither.c */
258 
259 void ZB_initDither(ZBuffer *zb,int nb_colors,
260  unsigned char *color_indexes,int *color_table);
261 void ZB_closeDither(ZBuffer *zb);
262 void ZB_ditherFrameBuffer(ZBuffer *zb,unsigned char *dest,
263  int linesize);
264 
265 /* zline.c */
266 
267 void ZB_plot(ZBuffer *zb,ZBufferPoint *p);
268 void ZB_line(ZBuffer *zb,ZBufferPoint *p1,ZBufferPoint *p2);
269 void ZB_line_z(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2);
270 
271 
272 /* memory.c */
273 void gl_free(void *p);
274 void *gl_malloc(int size);
275 void *gl_zalloc(int size);
276 
277 #endif /* _tgl_zbuffer_h_ */