PVRTFixedPoint.h 7.7 KB

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