00001 #ifndef _tgl_zbuffer_h_
00002 #define _tgl_zbuffer_h_
00003
00004
00005
00006
00007
00008 #include "zfeatures.h"
00009 #include "pbitops.h"
00010
00011 typedef unsigned int ZPOINT;
00012 #define ZB_Z_BITS 20
00013 #define ZB_POINT_Z_FRAC_BITS 10 // These must add to < 32.
00014
00015
00016
00017
00018
00019
00020
00021 #define ZB_POINT_ST_FRAC_BITS 12
00022
00023
00024
00025
00026 #define MAX_MIPMAP_LEVELS (32 - ZB_POINT_ST_FRAC_BITS + 1)
00027
00028
00029 #define ZB_TEXEL(texture_level, s, t) \
00030 ((((t) & (texture_level).t_mask) >> (texture_level).t_shift) | \
00031 (((s) & (texture_level).s_mask) >> (texture_level).s_shift))
00032
00033 #define ZB_LOOKUP_TEXTURE_NEAREST(texture_def, s, t) \
00034 (texture_def)->levels[0].pixmap[ZB_TEXEL((texture_def)->levels[0], s, t)]
00035
00036 #define ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level) \
00037 (texture_def)->levels[(level)].pixmap[ZB_TEXEL((texture_def)->levels[(level)], s, t)]
00038
00039
00040
00041
00042
00043
00044
00045
00046 #define DO_CALC_MIPMAP_LEVEL(mipmap_level, mipmap_dx, dsdx, dtdx) \
00047 { \
00048 (mipmap_dx) = ((unsigned int)abs(dsdx) + (unsigned int)abs(dtdx)); \
00049 (mipmap_level) = get_next_higher_bit((mipmap_dx) >> ZB_POINT_ST_FRAC_BITS); \
00050 (mipmap_dx) &= ((1 << (((mipmap_level) - 1) + ZB_POINT_ST_FRAC_BITS)) - 1); \
00051 }
00052
00053 #define ZB_POINT_RED_MIN 0x0000
00054 #define ZB_POINT_RED_MAX 0xffff
00055 #define ZB_POINT_GREEN_MIN 0x0000
00056 #define ZB_POINT_GREEN_MAX 0xffff
00057 #define ZB_POINT_BLUE_MIN 0x0000
00058 #define ZB_POINT_BLUE_MAX 0xffff
00059 #define ZB_POINT_ALPHA_MIN 0x0000
00060 #define ZB_POINT_ALPHA_MAX 0xffff
00061
00062
00063 #define ZB_MODE_5R6G5B 1
00064 #define ZB_MODE_INDEX 2
00065 #define ZB_MODE_RGBA 3
00066 #define ZB_MODE_RGB24 4
00067 #define ZB_NB_COLORS 225
00068
00069 #define RGB_TO_PIXEL(r,g,b) \
00070 ((((unsigned int)(r) << 8) & 0xff0000) | ((unsigned int)(g) & 0xff00) | ((unsigned int)(b) >> 8))
00071 #define RGBA_TO_PIXEL(r,g,b,a) \
00072 ((((unsigned int)(a) << 16) & 0xff000000) | (((unsigned int)(r) << 8) & 0xff0000) | ((unsigned int)(g) & 0xff00) | ((unsigned int)(b) >> 8))
00073 #define RGBA8_TO_PIXEL(r,g,b,a) \
00074 ((((unsigned int)(a) << 24) & 0xff000000) | (((unsigned int)(r) << 16) & 0xff0000) | (((unsigned int)(g) << 8) & 0xff00) | (unsigned int)(b))
00075 #define PIXEL_R(p) (((unsigned int)(p) & 0xff0000) >> 8)
00076 #define PIXEL_G(p) ((unsigned int)(p) & 0xff00)
00077 #define PIXEL_B(p) (((unsigned int)(p) & 0x00ff) << 8)
00078 #define PIXEL_A(p) (((unsigned int)(p) & 0xff000000) >> 16)
00079 typedef unsigned int PIXEL;
00080 #define PSZB 4
00081 #define PSZSH 5
00082
00083
00084 #define PCOMPONENT_MULT(c1, c2) \
00085 ((((unsigned int)(c1) * (unsigned int)(c2))) >> 16)
00086 #define PCOMPONENT_MULT3(c1, c2, c3) \
00087 PCOMPONENT_MULT(c1, PCOMPONENT_MULT(c2, c3))
00088 #define PCOMPONENT_MULT4(c1, c2, c3, c4) \
00089 PCOMPONENT_MULT(PCOMPONENT_MULT(c1, c2), PCOMPONENT_MULT(c3, c4))
00090
00091
00092
00093
00094 #define PALPHA_MULT(c1, c2) \
00095 (((int)(((int)(c1) >> 2) * (unsigned int)(c2))) >> 14)
00096
00097 #define PCOMPONENT_BLEND(c1, c2, a2) \
00098 ((((unsigned int)(c1) * ((unsigned int)0xffff - (unsigned int)(a2)) + (unsigned int)(c2) * (unsigned int)(a2))) >> 16)
00099
00100 #define PIXEL_BLEND(r1, g1, b1, r2, g2, b2, a2) \
00101 RGBA_TO_PIXEL(PCOMPONENT_BLEND(r1, r2, a2), \
00102 PCOMPONENT_BLEND(g1, g2, a2), \
00103 PCOMPONENT_BLEND(b1, b2, a2), \
00104 a2)
00105 #define PIXEL_BLEND_RGB(rgb, r, g, b, a) \
00106 PIXEL_BLEND(PIXEL_R(rgb), PIXEL_G(rgb), PIXEL_B(rgb), r, g, b, a)
00107
00108 typedef struct {
00109 PIXEL *pixmap;
00110 unsigned int s_mask, s_shift, t_mask, t_shift;
00111 } ZTextureLevel;
00112
00113 typedef struct ZBuffer ZBuffer;
00114 typedef struct ZBufferPoint ZBufferPoint;
00115 typedef struct ZTextureDef ZTextureDef;
00116
00117 typedef void (*ZB_fillTriangleFunc)(ZBuffer *, ZBufferPoint *, ZBufferPoint *, ZBufferPoint *);
00118
00119 typedef void (*ZB_storePixelFunc)(ZBuffer *zb, PIXEL &result, int r, int g, int b, int a);
00120
00121 typedef PIXEL (*ZB_lookupTextureFunc)(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00122
00123 typedef int (*ZB_texWrapFunc)(int coord, int max_coord);
00124
00125 struct ZTextureDef {
00126 ZTextureLevel *levels;
00127 ZB_lookupTextureFunc tex_minfilter_func;
00128 ZB_lookupTextureFunc tex_magfilter_func;
00129 ZB_lookupTextureFunc tex_minfilter_func_impl;
00130 ZB_lookupTextureFunc tex_magfilter_func_impl;
00131 ZB_texWrapFunc tex_wrap_u_func;
00132 ZB_texWrapFunc tex_wrap_v_func;
00133 int s_max, t_max;
00134 PIXEL border_color;
00135 };
00136
00137 struct ZBuffer {
00138 int xsize,ysize;
00139 int linesize;
00140 int mode;
00141
00142 ZPOINT *zbuf;
00143 PIXEL *pbuf;
00144 int frame_buffer_allocated;
00145
00146 int nb_colors;
00147 unsigned char *dctable;
00148 int *ctable;
00149 ZTextureDef current_textures[MAX_TEXTURE_STAGES];
00150 int reference_alpha;
00151 int blend_r, blend_g, blend_b, blend_a;
00152 ZB_storePixelFunc store_pix_func;
00153 };
00154
00155 struct ZBufferPoint {
00156 int x,y,z;
00157 int s,t;
00158 int r,g,b,a;
00159
00160 PN_stdfloat sz,tz;
00161
00162 int sa, ta;
00163 PN_stdfloat sza,tza;
00164
00165 int sb, tb;
00166 PN_stdfloat szb,tzb;
00167 };
00168
00169
00170
00171 #ifdef DO_PSTATS
00172 extern int pixel_count_white_untextured;
00173 extern int pixel_count_flat_untextured;
00174 extern int pixel_count_smooth_untextured;
00175 extern int pixel_count_white_textured;
00176 extern int pixel_count_flat_textured;
00177 extern int pixel_count_smooth_textured;
00178 extern int pixel_count_white_perspective;
00179 extern int pixel_count_flat_perspective;
00180 extern int pixel_count_smooth_perspective;
00181 extern int pixel_count_smooth_multitex2;
00182 extern int pixel_count_smooth_multitex3;
00183
00184 #define COUNT_PIXELS(pixel_count, p0, p1, p2) \
00185 (pixel_count) += abs((p0)->x * ((p1)->y - (p2)->y) + (p1)->x * ((p2)->y - (p0)->y) + (p2)->x * ((p0)->y - (p1)->y)) / 2
00186
00187 #else
00188
00189 #define COUNT_PIXELS(pixel_count, p0, p1, p2)
00190
00191 #endif // DO_PSTATS
00192
00193 ZBuffer *ZB_open(int xsize,int ysize,int mode,
00194 int nb_colors,
00195 unsigned char *color_indexes,
00196 unsigned int *color_table,
00197 void *frame_buffer);
00198
00199
00200 void ZB_close(ZBuffer *zb);
00201
00202 void ZB_resize(ZBuffer *zb,void *frame_buffer,int xsize,int ysize);
00203 void ZB_clear(ZBuffer *zb, int clear_z, ZPOINT z,
00204 int clear_color, unsigned int r, unsigned int g, unsigned int b, unsigned int a);
00205 void ZB_clear_viewport(ZBuffer * zb, int clear_z, ZPOINT z,
00206 int clear_color, unsigned int r, unsigned int g, unsigned int b, unsigned int a,
00207 int xmin, int ymin, int xsize, int ysize);
00208
00209 PIXEL lookup_texture_nearest(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00210 PIXEL lookup_texture_bilinear(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00211 PIXEL lookup_texture_mipmap_nearest(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00212 PIXEL lookup_texture_mipmap_linear(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00213 PIXEL lookup_texture_mipmap_bilinear(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00214 PIXEL lookup_texture_mipmap_trilinear(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00215
00216 PIXEL apply_wrap_general_minfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00217 PIXEL apply_wrap_general_magfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00218
00219 PIXEL apply_wrap_border_color_minfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00220 PIXEL apply_wrap_border_color_magfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00221
00222 PIXEL apply_wrap_clamp_minfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00223 PIXEL apply_wrap_clamp_magfilter(ZTextureDef *texture_def, int s, int t, unsigned int level, unsigned int level_dx);
00224
00225 int texcoord_clamp(int coord, int max_coord);
00226 int texcoord_repeat(int coord, int max_coord);
00227 int texcoord_mirror(int coord, int max_coord);
00228 int texcoord_mirror_once(int coord, int max_coord);
00229
00230
00231 void ZB_copyFrameBuffer(const ZBuffer *zb,void *buf,int linesize);
00232 void ZB_copyFrameBufferNoAlpha(const ZBuffer *zb,void *buf,int linesize);
00233 void ZB_zoomFrameBuffer(ZBuffer *dest, int dest_xmin, int dest_ymin,
00234 int dest_xsize, int dest_ysize,
00235 const ZBuffer *source, int source_xmin, int source_ymin,
00236 int source_xsize, int source_ysize);
00237
00238
00239
00240 void ZB_initDither(ZBuffer *zb,int nb_colors,
00241 unsigned char *color_indexes,int *color_table);
00242 void ZB_closeDither(ZBuffer *zb);
00243 void ZB_ditherFrameBuffer(ZBuffer *zb,unsigned char *dest,
00244 int linesize);
00245
00246
00247
00248 void ZB_plot(ZBuffer *zb,ZBufferPoint *p);
00249 void ZB_line(ZBuffer *zb,ZBufferPoint *p1,ZBufferPoint *p2);
00250 void ZB_line_z(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2);
00251
00252
00253
00254 void gl_free(void *p);
00255 void *gl_malloc(int size);
00256 void *gl_zalloc(int size);
00257
00258 #endif