macros.h 4.03 KB

/* macros.h */

/* mem_span rendering modes */
#define RENDER_OPAQUE 0
#define RENDER_TRANSP 1
#define RENDER_DECAL 2
#define RENDER_TEXEDGE 3
#define RENDER_INTERPEN 4


#define clamp( a, b, c) ((b > c) ? c : ((b < a) ? a : b))

/* divide fraction precision */
#define DIV_RECP_FRAC 0x7FFFFFFF

/* coordinate precision into setup */
#define SU_XY_FRAC_SHFT 2
#define SU_XY_FRAC_ONE (1<<SU_XY_FRAC_SHFT)
#define SU_XY_FRAC_MASK (SU_XY_FRAC_ONE-1)
#define float_sux( a) ((float)a*(float)1.0/SU_XY_FRAC_ONE)

/* texture coordinate perspective correction */
#define TC_PERSP_SHFT 15
#define TC_PERSP_ONE (1<<TC_PERSP_SHFT)

/* plane equation coefficients during setup */
#define SU_P_FRAC_SHFT 20
#define SU_P_FRAC_ONE (1<<SU_P_FRAC_SHFT)

/* edge walk fixed point precision out of setup */
/* X coordinate */
#define EW_X_FRAC_SHFT 15
#define EW_X_FRAC_ONE (1<<EW_X_FRAC_SHFT)
#define EW_X_FRAC_MASK (EW_X_FRAC_ONE-1)
#define EW_X_SUBPIX_MASK (SU_XY_FRAC_MASK<<(EW_X_FRAC_SHFT-SU_XY_FRAC_SHFT))
#define float_ewx( a, b) ((float)a+b*(float)1.0/EW_X_FRAC_ONE)

/* attributes */
#define EW_A_FRAC_SHFT EW_X_FRAC_SHFT
#define EW_A_FRAC_ONE (1<<EW_A_FRAC_SHFT)
#define EW_A_FRAC_MASK (EW_A_FRAC_ONE-1)
#define fix_ewa( a) ((a)<<EW_A_FRAC_SHFT)
#define int_ewa( a) ((a)>>EW_A_FRAC_SHFT)
#define float_ewa( a) ((float)a*(float)1.0/EW_A_FRAC_ONE)

/* color */
#define ALPHA_MAX 255
#define COLOR_MAX ALPHA_MAX
#define ALPHA_SCALE ((float)1.0/(ALPHA_MAX+1))
#define ALPHA_ROUND ((float)0.5)
#define clamp_color( a) clamp( 0, a, COLOR_MAX)
#define pack_color( r, g, b) (r | (g << 8) | (b << 16))
#define pack_colora( r, g, b, a) (r | (g << 8) | (b << 16) | (a << 24))
#define unpack_color( c, r, g, b) \
	(r = c & 0xff, g = (c >> 8) & 0xff, b = (c >> 16) & 0xff)
#define unpack_colora( c, r, g, b, a) \
	(r = c & 0xff, g = (c >> 8) & 0xff, b = (c >> 16) & 0xff, \
	a = (c >> 24) & 0xff)
#define unpack_colora4( c, r, g, b, a) \
	(r = c & 0xf, g = (c >> 4) & 0xf, b = (c >> 8) & 0xf, \
	a = (c >> 12) & 0xf), r |= r << 4, g |= g << 4, b |= b << 4, a |= a << 4
#define pack_colora4( r, g, b, a) ( ((r&0xf0) >> 4) | (g&0xf0) | \
		((b&0xf0) << 4) | ((a&0xf0) << 8))
#define blend_color( a, b, c) \
	(a + (int)((float)(b - a)*(float)c*ALPHA_SCALE))
#define combine_color( c1, c2, m1, c3) \
	(c3 + (int)((float)(c1 - c2)*(float)m1*ALPHA_SCALE))
#define times_alpha( a, b) \
	(int)((float)a*b*ALPHA_SCALE+ALPHA_ROUND)

/* texture */
/* input texture coordinates */
#define TC_FRAC_SHFT 12		/* vertex texture coord 0 to 1 */
#define TC_FRAC_ONE (1<<TC_FRAC_SHFT)
#define TC_FRAC_MAX (TC_FRAC_ONE-1)

/* texture image coordinates */
#define TI_FRAC_SHFT 5		/* texture image fractional bits */
#define TI_FRAC_ONE (1<<TI_FRAC_SHFT)
#define TI_FRAC_MASK (TI_FRAC_ONE-1)

/* convert input texture coords to texture image coords with frac */
#define tc_image( a, b) ((int)( (float)a* (float)b * \
	(float)TI_FRAC_ONE/TC_FRAC_ONE ))

#define filter_color( x, y, a) \
	(int)(x + (y-x) * (float)a*((float)1.0/TI_FRAC_ONE))

/* texture coordinate perspective divide interval */
#define TC_DIV_SHFT 3
#define TC_DIV_STEP (1<<TC_DIV_SHFT)

/* antialiasing subpixels */
#define AA_SP_BITS 2
#define AA_SP_SHFT (1<<AA_SP_BITS)
#define AA_SP_MASK ((1<<AA_SP_SHFT)-1)
#define AA_SP_COUNT AA_SP_SHFT
#define AA_SP_CENTER (1<<(AA_SP_COUNT*AA_SP_COUNT-1))
#define AA_SP_HALF 0xA5A5

/*
 *  DEFINE COMBINE MUX SELECTS
 */

#define SHADE_ENABLE	0x8000

#define SEL_TEX_CLR_1	0x2000
#define SEL_PRIM_CLR_1	0x1000
#define SEL_ENV_CLR_1	0x0000

#define SEL_TEX_CLR_2	0x0200
#define SEL_PRIM_CLR_2	0x0100
#define SEL_NO_CLR_2	0x0000

#define SEL_TEXC_MULT	0x0060
#define SEL_PRIMC_MULT	0x0050
#define SEL_ENVC_MULT	0x0040
#define SEL_TEXA_MULT	0x0030
#define SEL_PRIMA_MULT	0x0020
#define SEL_ENVA_MULT	0x0010
#define SEL_ONE_MULT	0x0000

#define SEL_TEX_CLR_3	0x0003
#define SEL_PRIM_CLR_3	0x0002
#define SEL_ENV_CLR_3	0x0001
#define SEL_NO_CLR_3	0x0000

#define DEFAULT_COMBINE (SHADE_ENABLE | SEL_TEX_CLR_1 | SEL_PRIM_CLR_2 | \
			SEL_TEXA_MULT | SEL_PRIM_CLR_3)

#define DISABLE_TEXTURE (SHADE_ENABLE | SEL_PRIM_CLR_1 | SEL_NO_CLR_2 | \
			SEL_ONE_MULT | SEL_NO_CLR_3)