⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convert.h

📁 Lido PXA270平台开发板的最新BSP,包括源代码
💻 H
字号:
/******************************************************************************
<module>
* Name         : Convert.h
* Title        : Conversion related types structures and macros
* Author(s)    : Imagination Technologies
* Created      : 2 March 2004
*
* Copyright    : 2004 by Imagination Technologies Limited.
*                All rights reserved.  No part of this software, either
*                material or conceptual may be copied or distributed,
*                transmitted, transcribed, stored in a retrieval system
*                or translated into any human or computer language in any
*                form by any means, electronic, mechanical, manual or
*                other-wise, or disclosed to third parties without the
*                express written permission of Imagination Technologies
*                Limited, Unit 8, HomePark Industrial Estate,
*                King's Langley, Hertfordshire, WD4 8LZ, U.K.
*
* Description  : Tools for converting and managing fixed\floating point data
*
* Platform     : Windows CE
*
</module>

 $Log: convert.h $
*********************************************************************************/
#if !defined(_CONVERT_H_)
#define _CONVERT_H_

#pragma warning (disable:4244)/* convertion from float to 32 integer type - possible loss of data */
/*****************************************************************************
 Types
*****************************************************************************/
typedef signed int IMG_FIXED;

/*****************************************************************************
 Conversion definitions
*****************************************************************************/
#define MAX_FIXED			((IMG_FIXED)0x7FFFFFFF)
#define MIN_FIXED			((IMG_FIXED)0x80000000)
#define MAX_FIXED_AS_I64	((__int64)0x000000007FFFFFFF)
#define MIN_FIXED_AS_I64	((__int64)0xFFFFFFFF80000000)
#define FLOAT_PVE_INF		0x7F800000
#define FLOAT_NVE_INF		0xFF800000
#define SHIFT				16
#define SCALE				(1 << SHIFT)
#define	SHIFT_PR2NORM		4
#define SHIFT_PR			20

/*****************************************************************************
 LFX2FL - 64 bit fixed (48:16) point to float
*****************************************************************************/
__inline float LFX2FL(__int64 fx)
{
	IMG_FLOAT fTemp = fx;
	IMG_UINT32 ui32Temp = (((*((IMG_UINT32*) &fTemp) & 0x7F800000) - 0x08000000) | 
							((*((IMG_UINT32*) (&fTemp))) & ~(0x7F800000)));

	return ((fx == 0) ? 0 :*((IMG_FLOAT*)(&ui32Temp)));
}
/*****************************************************************************
 FL2LFX - Float to 64 bit fixed point (48:16)
*****************************************************************************/
__inline __int64 FL2LFX(IMG_FLOAT f)
{
	IMG_UINT32 ui32Temp;

	if(*((DWORD*)(&f)) == FLOAT_PVE_INF)
	{
		return MAX_FIXED_AS_I64;
	}
	else if(*((DWORD*)(&f)) == FLOAT_NVE_INF)
	{
		return MIN_FIXED_AS_I64;
	}
	else
	{
		ui32Temp = (((*((IMG_UINT32*) (&f))) & 0x7F800000)  + 0x08000000)  | 
				   ((*((IMG_UINT32*) (&f))) & ~(0x7F800000));
		return ((__int64) *((IMG_FLOAT*) &ui32Temp));
	}
}
/*****************************************************************************
 FX2FL - 32 bit fixed point (16:16) to float
*****************************************************************************/
__inline float FX2FL(IMG_INT32 fx)
{
	IMG_FLOAT fTemp = fx;
	IMG_UINT32 ui32Temp = (((*((IMG_UINT32*) &fTemp) & 0x7F800000) - 0x08000000) | 
							((*((IMG_UINT32*) (&fTemp))) & ~(0x7F800000)));

	return ((fx == 0) ? 0 :*((IMG_FLOAT*)(&ui32Temp)));
}
/*****************************************************************************
 FL2FX - Float to 32 bit fixed point (16:16)
*****************************************************************************/
__inline IMG_INT32 FL2FX(IMG_FLOAT f)
{
	IMG_UINT32 ui32Temp;

	if(*((DWORD*)(&f)) == FLOAT_PVE_INF)
	{
		return MAX_FIXED;
	}
	else if(*((DWORD*)(&f)) == FLOAT_NVE_INF)
	{
		return MIN_FIXED;
	}
	else
	{
		ui32Temp = (((*((IMG_UINT32*) (&f))) & 0x7F800000)  + 0x08000000)  | 
					((*((IMG_UINT32*) (&f))) & ~(0x7F800000));
		return ((IMG_INT32) *((IMG_FLOAT*) &ui32Temp));
	}
}
/*****************************************************************************
 FX2FL_PR - 32 bit high precision fixed point (12:20) to float
*****************************************************************************/
__inline float FX2FL_PR(IMG_INT32 fx)
{
	IMG_FLOAT fTemp = fx;
	IMG_UINT32 ui32Temp = (((*((IMG_UINT32*) &fTemp) & 0x7F800000) - 0x0A000000) | 
							((*((IMG_UINT32*) (&fTemp))) & ~(0x7F800000)));

	return ((fx == 0) ? 0 :*((IMG_FLOAT*)(&ui32Temp)));
}
/*****************************************************************************
 FL2FX_PR - Float to high precision 32 bit fixed point (12:20)
*****************************************************************************/
__inline IMG_INT32 FL2FX_PR(IMG_FLOAT f)
{
	IMG_UINT32 ui32Temp;

	if(*((DWORD*)(&f)) == FLOAT_PVE_INF)
	{
		return MAX_FIXED;
	}
	else if(*((DWORD*)(&f)) == FLOAT_NVE_INF)
	{
		return MIN_FIXED;
	}
	else
	{
		ui32Temp = (((*((IMG_UINT32*) (&f))) & 0x7F800000)  + 0x0A000000)  | 
				    ((*((IMG_UINT32*) (&f))) & ~(0x7F800000));
	}
	return ((IMG_INT32) *((IMG_FLOAT*) &ui32Temp));
}
/*****************************************************************************/

#define LONG_TO_FLOAT(x)	(* ((float*)( & x)))
#define FLOAT_TO_LONG(x)	(* ((IMG_UINT32 *)( & x)))
#define TO_ULONG(x)			(* ((IMG_UINT32 *)( & x)))
#define TO_FLOAT(x)			(* ((float*)( & x)))

/*****************************************************************************
 HW_PRN_713 fix 
*****************************************************************************/
#if (!defined (D3DM_NATIVE_FLOAT)) && defined (FIX_HW_PRN_721)
#define MIN_FIXED_PLUS_ONE	((IMG_FIXED)0x80010000)
#define CLAMP_LOWER(x) ((x == MIN_FIXED) ? MIN_FIXED_PLUS_ONE : x)
#else
#define CLAMP_LOWER(x) (x)
#endif
/*****************************************************************************/

/*****************************************************************************
 Format dependant definitions
*****************************************************************************/
#if defined (D3DM_NATIVE_FLOAT)

	#define FL2NTV(x)		(x)
	#define FX2NTV(x)		FX2FL(x)
	#define FLPTR2NTV(x)	(*((float*)(x)))
	#define FXPTR2NTV(x)	FX2FL(*(IMG_FIXED *)(x))
	#define NTV2FL(x)		(x)
	#define NTV2FX(x)		FL2FX(x)
	#define AS_ULONG(x)		((IMG_UINT32)x)
	#define D3DM_FL2NTV(x)	(* (float *)(&x))
	#define D3DM_FX2NTV(x)	FX2FL(x)
	#define LONG_AS_NTV(x)	((float)(x))
	#define TO_NTV(x)		(*(float*)(&x))
	
	typedef float			NTV_TYPE;		

	#define D3DM_Zero		0.0f
	#define D3DM_One		1.0f
	#define D3DM_Two		2.0f
	#define	D3DM_OneHundred	100.0f
	#define D3DM_E			2.7182817f
	#define D3DM_Half		0.5f
	#define NTV_MAX			max
	#define NTV_MIN			min
	#define NTV_ABS			fabs

	#define AS_ULONG_RND(x)	((IMG_UINT32)(x + D3DM_Half))

	#define	PRNTV2FL(x)		NTV2FL(x)
	#define	PRNTV2FX(x)		NTV2FX(x)
#else
	#define		FL2NTV(x)		FL2FX(x)
	#define		FX2NTV(x)		(x)
	#define		NTV2FL(x)		FX2FL(x)
	#define		FLPTR2NTV(x)	FL2FX(*((float *)x))
	#define		FXPTR2NTV(x)	(* (IMG_FIXED *)(x))
	#define		NTV2FX(x)		(x)
	#define		AS_ULONG(x)		((IMG_UINT32)((x) >> SHIFT))
	#define		D3DM_FL2NTV(x)	FL2FX((* (float *)(&x)))
	#define		D3DM_FX2NTV(x)	(x)
	#define		LONG_AS_NTV(x)	((IMG_FIXED)((IMG_FIXED)(x)) << SHIFT)
	#define		TO_NTV(x)		(*(IMG_FIXED*)(&x))

	typedef		IMG_FIXED		NTV_TYPE;		
				
	#define		D3DM_Zero		0
	#define		D3DM_One		(1		<< SHIFT)
	#define		D3DM_Two		(2		<< SHIFT)
	#define		D3DM_OneHundred	(100	<< SHIFT)
	#define		D3DM_E			FL2FX(2.7182817f)
	#define		D3DM_Half		FL2FX(0.5f)
	#define 	NTV_MAX(a,b)	(a > b ? a : b)
	#define		NTV_MIN(a,b)	(a < b ? a : b)
	#define		NTV_ABS			abs

	#define		AS_ULONG_RND(x)	((IMG_UINT32)((x + D3DM_Half) >> SHIFT))

	/* 12.20 Precision stuff */
	#define		PRNTV2FL(x)			FX2FL_PR(x)
	#define		PRNTV2FX(x)			(x >> SHIFT_PR2NORM)
#endif


/*****************************************************************************
 Structs
*****************************************************************************/


typedef struct _PVR_44_MATRIX_
{
	union
	{
		struct 
		{

		  NTV_TYPE  _11, _12, _13, _14;
		  NTV_TYPE  _21, _22, _23, _24;
		  NTV_TYPE  _31, _32, _33, _34;
		  NTV_TYPE  _41, _42, _43, _44;
		};
		NTV_TYPE m[4][4];
	};

} PVR_44_MATRIX;


typedef struct _PVR_COLORVALUE_
{
	NTV_TYPE	r;
	NTV_TYPE	g;
	NTV_TYPE	b;
	NTV_TYPE	a;
	IMG_UINT32	ui32Colour;
} PVR_COLORVALUE;


typedef struct _PVR_VECTOR2_
{
	NTV_TYPE x;
	NTV_TYPE y;
} PVR_VECTOR2;


typedef struct _PVR_VECTOR3_
{
	NTV_TYPE x;
	NTV_TYPE y;
	NTV_TYPE z;
} PVR_VECTOR3;


typedef struct _PVR_VECTOR4_
{
	NTV_TYPE x;
	NTV_TYPE y;
	NTV_TYPE z;
	NTV_TYPE w;
} PVR_VECTOR4;

typedef struct _PVR_TEXVECTOR_
{
	NTV_TYPE	x;
	NTV_TYPE	y;
	NTV_TYPE	z;
	IMG_UINT32	ui32NumOut;
} PVR_TEXVECTOR;


typedef struct _PVR_LIGHT_
{
    D3DMLIGHTTYPE	Type;
	PVR_COLORVALUE  Diffuse;
	PVR_COLORVALUE  Specular;
	PVR_COLORVALUE  Ambient;
	PVR_VECTOR4     Position;
	PVR_VECTOR4     Direction;
	PVR_VECTOR4     EyePosition;
	PVR_VECTOR4     EyeDirection;
    NTV_TYPE        Range;
    NTV_TYPE        Attenuation0;
    NTV_TYPE        Attenuation1;
    NTV_TYPE        Attenuation2;
} PVR_LIGHT;


typedef struct _PVR_MATERIAL_
{
    PVR_COLORVALUE  Diffuse;
    PVR_COLORVALUE  Ambient;
    PVR_COLORVALUE  Specular;
    NTV_TYPE       Power;
} PVR_MATERIAL;


/*****************************************************************************
 Conversion Functions
*****************************************************************************/


/* Convert D3D Matrix to native format */
__inline void PVR44ConvertD3DMatrix(D3DMMATRIX* pInMatrix, 
									PVR_44_MATRIX *pOut, 
									IMG_BOOL bIsFixed)
{
	if (bIsFixed)
	{
		pOut->_11 = D3DM_FX2NTV(pInMatrix->_11);
		pOut->_12 = D3DM_FX2NTV(pInMatrix->_12);
		pOut->_13 = D3DM_FX2NTV(pInMatrix->_13);
		pOut->_14 = D3DM_FX2NTV(pInMatrix->_14);
		pOut->_21 = D3DM_FX2NTV(pInMatrix->_21);
		pOut->_22 = D3DM_FX2NTV(pInMatrix->_22);
		pOut->_23 = D3DM_FX2NTV(pInMatrix->_23);
		pOut->_24 = D3DM_FX2NTV(pInMatrix->_24);
		pOut->_31 = D3DM_FX2NTV(pInMatrix->_31);
		pOut->_32 = D3DM_FX2NTV(pInMatrix->_32); 
		pOut->_33 = D3DM_FX2NTV(pInMatrix->_33); 
		pOut->_34 = D3DM_FX2NTV(pInMatrix->_34);
		pOut->_41 = D3DM_FX2NTV(pInMatrix->_41);
		pOut->_42 = D3DM_FX2NTV(pInMatrix->_42);
		pOut->_43 = D3DM_FX2NTV(pInMatrix->_43);
		pOut->_44 = D3DM_FX2NTV(pInMatrix->_44);
	}
	else
	{
		pOut->_11 = D3DM_FL2NTV(pInMatrix->_11);
		pOut->_12 = D3DM_FL2NTV(pInMatrix->_12);
		pOut->_13 = D3DM_FL2NTV(pInMatrix->_13);
		pOut->_14 = D3DM_FL2NTV(pInMatrix->_14);
		pOut->_21 = D3DM_FL2NTV(pInMatrix->_21);
		pOut->_22 = D3DM_FL2NTV(pInMatrix->_22);
		pOut->_23 = D3DM_FL2NTV(pInMatrix->_23);
		pOut->_24 = D3DM_FL2NTV(pInMatrix->_24);
		pOut->_31 = D3DM_FL2NTV(pInMatrix->_31);
		pOut->_32 = D3DM_FL2NTV(pInMatrix->_32); 
		pOut->_33 = D3DM_FL2NTV(pInMatrix->_33); 
		pOut->_34 = D3DM_FL2NTV(pInMatrix->_34);
		pOut->_41 = D3DM_FL2NTV(pInMatrix->_41);
		pOut->_42 = D3DM_FL2NTV(pInMatrix->_42);
		pOut->_43 = D3DM_FL2NTV(pInMatrix->_43);
		pOut->_44 = D3DM_FL2NTV(pInMatrix->_44);
	}
}


/* Convert D3D Colour value to native format */
__inline void PVRConvertD3DColourVal(D3DMCOLORVALUE *pInValue, 
									 PVR_COLORVALUE *pOut, 
									 IMG_BOOL		bIsFixed)
{
	if (bIsFixed)
	{
		pOut->r = D3DM_FX2NTV(pInValue->r);
		pOut->g = D3DM_FX2NTV(pInValue->g);
		pOut->b = D3DM_FX2NTV(pInValue->b);
		pOut->a = D3DM_FX2NTV(pInValue->a);
	}
	else
	{
		pOut->r = D3DM_FL2NTV(pInValue->r);
		pOut->g = D3DM_FL2NTV(pInValue->g);
		pOut->b = D3DM_FL2NTV(pInValue->b);
		pOut->a = D3DM_FL2NTV(pInValue->a);
	}
}


/* Convert D3D Vector to native format */
__inline void PVRConvertD3DVector(D3DMVECTOR *pInVector, 
								  PVR_VECTOR4 *pOut,
								  IMG_BOOL	  bIsFixed)
{
	if (bIsFixed)
	{
		pOut->x = D3DM_FX2NTV(pInVector->x);
		pOut->y = D3DM_FX2NTV(pInVector->y);
		pOut->z = D3DM_FX2NTV(pInVector->z);
	}
	else
	{
		pOut->x = D3DM_FL2NTV(pInVector->x);
		pOut->y = D3DM_FL2NTV(pInVector->y);
		pOut->z = D3DM_FL2NTV(pInVector->z);
	}
}


/* Convert D3D Light to native format */
__inline void PVRCopyConvertD3DLight(D3DMLIGHT *pIn, 
									 PVR_LIGHT *pOut, 
									 IMG_BOOL bIsFixed)
{
    pOut->Type			= pIn->Type;

	/* The following elements are always passed in as floats */
    pOut->Attenuation0	= FL2NTV(pIn->Attenuation0);
    pOut->Attenuation1	= FL2NTV(pIn->Attenuation1);
    pOut->Attenuation2	= FL2NTV(pIn->Attenuation2);
    pOut->Range			= FL2NTV(pIn->Range);

	/* Convert to native if required */
	PVRConvertD3DColourVal(&pIn->Diffuse, &pOut->Diffuse, bIsFixed);
	PVRConvertD3DColourVal(&pIn->Specular, &pOut->Specular, bIsFixed);
	PVRConvertD3DColourVal(&pIn->Ambient, &pOut->Ambient, bIsFixed);
	PVRConvertD3DVector(&pIn->Position, &pOut->Position, bIsFixed);
	PVRConvertD3DVector(&pIn->Direction, &pOut->Direction, bIsFixed);

	pOut->Position.w = D3DM_One;
}


/* Convert D3D Material to native format */
__inline void PVRCopyConvertD3DMaterial(D3DMMATERIAL* pIn, 
										PVR_MATERIAL* pOut, 
										IMG_BOOL bIsFixed)
{
	/* The following element is always passed in as a float */
    pOut->Power	= FL2NTV(pIn->Power);

	PVRConvertD3DColourVal(&pIn->Diffuse, &pOut->Diffuse, bIsFixed);
	PVRConvertD3DColourVal(&pIn->Specular, &pOut->Specular, bIsFixed);
	PVRConvertD3DColourVal(&pIn->Ambient, &pOut->Ambient, bIsFixed);
}

#endif /* #if !defined(_CONVERT_H_) */
/*****************************************************************************
 End of file (Convert.h)
*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -