Home | History | Annotate | Download | only in Tools
      1 /*!****************************************************************************
      2 
      3  @file         PVRTFixedPoint.h
      4  @copyright    Copyright (c) Imagination Technologies Limited.
      5  @brief        Set of macros and functions to make fixed-point easier to program.
      6 
      7 ******************************************************************************/
      8 
      9 #ifndef _PVRTFIXEDPOINT_H_
     10 #define _PVRTFIXEDPOINT_H_
     11 
     12 #include "PVRTGlobal.h"
     13 
     14 #if defined(BUILD_OGLES)
     15 	#include "PVRTFixedPointAPI.h"
     16 #else
     17 	#define VERTTYPE PVRTfloat32
     18 	#ifdef PVRT_FIXED_POINT_ENABLE
     19 		#error Build option not supported: PVRT_FIXED_POINT_ENABLE
     20 	#endif
     21 #endif
     22 
     23 /* Fixed-point macros */
     24 #define PVRTF2X(f)		( (int) ( (f)*(65536) ) )
     25 #define PVRTX2F(x)		((float)(x)/65536.0f)
     26 #define PVRTXMUL(a,b)	( (int)( ((PVRTint64)(a)*(b)) / 65536 ) )
     27 #define PVRTXDIV(a,b)	( (int)( (((PVRTint64)(a))<<16)/(b) ) )
     28 #define PVRTABS(a)		((a) <= 0 ? -(a) : (a) )
     29 
     30 /* Define trig table macros */
     31 #include "PVRTMathTable.h"
     32 
     33 /* Useful values */
     34 #define PVRT_PI_OVER_TWOf	(3.1415926535f / 2.0f)
     35 #define PVRT_PIf			(3.1415926535f)
     36 #define PVRT_TWO_PIf		(3.1415926535f * 2.0f)
     37 #define PVRT_ONEf		(1.0f)
     38 
     39 #define PVRT_PI_OVER_TWOx	PVRTF2X(PVRT_PI_OVER_TWOf)
     40 #define PVRT_PIx			PVRTF2X(PVRT_PIf)
     41 #define PVRT_TWO_PIx		PVRTF2X(PVRT_TWO_PIf)
     42 #define PVRT_ONEx			PVRTF2X(PVRT_ONEf)
     43 
     44 /* Fixed-point trig function lookups */
     45 #define PVRTXCOS(x)		(cos_val[(PVRTXMUL(((PVRTXDIV((x)<0? -(x):(x), PVRT_TWO_PIx)) & 0x0000FFFF), (NUM_ENTRIES-1)))])
     46 #define PVRTXSIN(x)		(sin_val[(PVRTXMUL(((PVRTXDIV((x)<0 ? PVRT_PIx-(x):(x), PVRT_TWO_PIx)) & 0x0000FFFF), (NUM_ENTRIES-1)))])
     47 #define PVRTXTAN(x)		( (x)<0 ? -tan_val[(PVRTXMUL(((PVRTXDIV(-(x), PVRT_TWO_PIx)) & 0x0000FFFF), (NUM_ENTRIES-1)))] : tan_val[(PVRTXMUL(((PVRTXDIV(x, PVRT_TWO_PIx)) & 0x0000FFFF), (NUM_ENTRIES-1)))] )
     48 #define PVRTXACOS(x)	(acos_val[PVRTXMUL(((((x) + PVRTF2X(1.0f))>>1) & 0x0000FFFF), (NUM_ENTRIES-1))])
     49 
     50 /* Floating-point trig functions lookups (needed by some tools chains that have problems with real math functions) */
     51 #ifdef USE_TRIGONOMETRIC_LOOKUP_TABLES
     52 
     53 	/* If trig tables are forced ON in non-fixed-point builds then convert fixed-point trig tables results to float */
     54 	#define	PVRTFCOS(x)				PVRTX2F(PVRTXCOS(PVRTF2X(x)))
     55 	#define	PVRTFSIN(x)				PVRTX2F(PVRTXSIN(PVRTF2X(x)))
     56 	#define	PVRTFTAN(x)				PVRTX2F(PVRTXTAN(PVRTF2X(x)))
     57 	#define	PVRTFACOS(x)			PVRTX2F(PVRTXACOS(PVRTF2X(x)))
     58 
     59 #else
     60 
     61 	/* Trig abstraction macros default to normal math trig functions for full float mode */
     62 	#define	PVRTFCOS(x)				((float)cos(x))
     63 	#define	PVRTFSIN(x)				((float)sin(x))
     64 	#define	PVRTFTAN(x)				((float)tan(x))
     65 	#define	PVRTFACOS(x)			((float)acos(x))
     66 
     67 #endif
     68 
     69 
     70 /* Fixed/float macro abstraction */
     71 #ifdef PVRT_FIXED_POINT_ENABLE
     72 
     73 	/* Fixed-point operations, including trig tables */
     74 	#define VERTTYPEMUL(a,b)			PVRTXMUL(a,b)
     75 	#define VERTTYPEDIV(a,b)			PVRTXDIV(a,b)
     76 	#define VERTTYPEABS(a)				PVRTABS(a)
     77 
     78 	#define f2vt(f) 					PVRTF2X(f)
     79 	#define vt2f(x) 					PVRTX2F(x)
     80 
     81 	#define PVRT_PI_OVER_TWO			PVRT_PI_OVER_TWOx
     82 	#define PVRT_PI						PVRT_PIx
     83 	#define PVRT_TWO_PI					PVRT_TWO_PIx
     84 	#define PVRT_ONE					PVRT_ONEx
     85 
     86 	#define	PVRTCOS(x)					PVRTXCOS(x)
     87 	#define	PVRTSIN(x)					PVRTXSIN(x)
     88 	#define	PVRTTAN(x)					PVRTXTAN(x)
     89 	#define	PVRTACOS(x)					PVRTXACOS(x)
     90 
     91 #else
     92 
     93 	/* Floating-point operations */
     94 	#define VERTTYPEMUL(a,b)			( (VERTTYPE)((a)*(b)) )
     95 	#define VERTTYPEDIV(a,b)			( (VERTTYPE)((a)/(b)) )
     96 	#define VERTTYPEABS(a)				( (VERTTYPE)(fabs(a)) )
     97 
     98 	#define f2vt(x)						(x)
     99 	#define vt2f(x)						(x)
    100 
    101 	#define PVRT_PI_OVER_TWO			PVRT_PI_OVER_TWOf
    102 	#define PVRT_PI						PVRT_PIf
    103 	#define PVRT_TWO_PI					PVRT_TWO_PIf
    104 	#define PVRT_ONE					PVRT_ONEf
    105 
    106 	/* If trig tables are forced ON in non-fixed-point builds then convert fixed-point trig tables results to float */
    107 	#define	PVRTCOS(x)					PVRTFCOS(x)
    108 	#define	PVRTSIN(x)					PVRTFSIN(x)
    109 	#define	PVRTTAN(x)					PVRTFTAN(x)
    110 	#define	PVRTACOS(x)					PVRTFACOS(x)
    111 
    112 #endif
    113 
    114 
    115 // Structure Definitions
    116 
    117 /*!***************************************************************************
    118  @struct HeaderStruct_Mesh
    119  @brief  Defines the format of a header-object as exported by the MAX plugin.
    120 *****************************************************************************/
    121 typedef struct {
    122 	unsigned int      nNumVertex;
    123     unsigned int      nNumFaces;
    124     unsigned int      nNumStrips;
    125     unsigned int      nFlags;
    126     unsigned int      nMaterial;
    127     float             fCenter[3];
    128     float             *pVertex;
    129     float             *pUV;
    130     float             *pNormals;
    131     float             *pPackedVertex;
    132     unsigned int      *pVertexColor;
    133     unsigned int      *pVertexMaterial;
    134     unsigned short    *pFaces;
    135     unsigned short    *pStrips;
    136     unsigned short    *pStripLength;
    137     struct
    138     {
    139         unsigned int  nType;
    140         unsigned int  nNumPatches;
    141         unsigned int  nNumVertices;
    142         unsigned int  nNumSubdivisions;
    143         float         *pControlPoints;
    144         float         *pUVs;
    145     } Patch;
    146 }   HeaderStruct_Mesh;
    147 
    148 
    149 #ifdef PVRT_FIXED_POINT_ENABLE
    150 
    151 /*!***************************************************************************
    152  @struct HeaderStruct_Fixed_Mesh
    153  @brief  Defines the format of a header-object as when converted to fixed point.
    154 *****************************************************************************/
    155 typedef struct {
    156 	unsigned int      nNumVertex;
    157 	unsigned int      nNumFaces;
    158 	unsigned int      nNumStrips;
    159 	unsigned int      nFlags;
    160 	unsigned int      nMaterial;
    161 	VERTTYPE          fCenter[3];
    162 	VERTTYPE          *pVertex;
    163 	VERTTYPE          *pUV;
    164 	VERTTYPE          *pNormals;
    165 	VERTTYPE          *pPackedVertex;
    166 	unsigned int      *pVertexColor;
    167 	unsigned int      *pVertexMaterial;
    168 	unsigned short    *pFaces;
    169 	unsigned short    *pStrips;
    170 	unsigned short    *pStripLength;
    171 	struct
    172 	{
    173 		unsigned int  nType;				// for the moment, these are left as floats
    174 		unsigned int  nNumPatches;
    175 		unsigned int  nNumVertices;
    176 		unsigned int  nNumSubdivisions;
    177 		float       *pControlPoints;
    178 		float       *pUVs;
    179 	} Patch;
    180 }   HeaderStruct_Fixed_Mesh;
    181 
    182 	typedef HeaderStruct_Fixed_Mesh HeaderStruct_Mesh_Type;
    183 #else
    184 	typedef HeaderStruct_Mesh HeaderStruct_Mesh_Type;
    185 #endif
    186 
    187 // Function prototypes
    188 
    189 /*!***************************************************************************
    190  @brief         Converts the data exported by MAX to fixed point when used in OpenGL
    191 				ES common-lite profile.
    192  @param[in]		headerObj			Pointer to object structure in the header file
    193  @return		directly usable geometry in fixed or float format as appropriate
    194 *****************************************************************************/
    195 HeaderStruct_Mesh_Type* PVRTLoadHeaderObject(const void *headerObj);
    196 
    197 /*!***************************************************************************
    198  @brief     	Releases memory allocated by LoadHeaderObject when geometry no longer
    199 				needed.
    200  @param[in] 	headerObj			Pointer returned by LoadHeaderObject
    201 *****************************************************************************/
    202 void PVRTUnloadHeaderObject(HeaderStruct_Mesh_Type* headerObj);
    203 
    204 
    205 #endif /* _PVRTFIXEDPOINT_H_ */
    206 
    207 /*****************************************************************************
    208  End of file (PVRTFixedPoint.h)
    209 *****************************************************************************/
    210 
    211