debug.c

来自「Lido PXA270平台开发板的最新BSP,包括源代码」· C语言 代码 · 共 2,713 行 · 第 1/5 页

C
2,713
字号
/******************************************************************************
<module>
* Name         : PVR_Debug.c
* Title        : Debug functionality
* 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  :  Debug functionality
*
* Platform     : Windows CE
*
</module>

 $Log: debug.c $
********************************************************************************/

#include "context.h"

#if defined(DEBUG) || defined(D3DM_RETAIL_DBGOUT)

INSTANTIATE_D3DM_ZONES

/*****************************************************************************
 FUNCTION	: D3DMDebugPrintf
    
 PURPOSE	: Outputs error messages

 RETURNS	: PSTR		- The decoded header type
*****************************************************************************/
IMG_VOID D3DMDebugPrintf(IMG_UINT32			uDebugLevel,
						 const IMG_CHAR*	pszFilepath,
						 IMG_UINT32			uLine,
						 const IMG_CHAR*	pszFormat,
						 ...)
{
	static IMG_CHAR szMessage[PVR_MAX_DEBUG_MESSAGE_LEN+1];
	IMG_CHAR* pszEndOfMessage = IMG_NULL;
	const IMG_CHAR* pszFilename = IMG_NULL;
	const IMG_CHAR* pszCurrentChar;
	va_list ArgList;

	if(uDebugLevel)
	{
		strncpy(szMessage, "D3DM: ", PVR_MAX_DEBUG_MESSAGE_LEN);

		pszEndOfMessage = &szMessage[strlen(szMessage)];

		va_start(ArgList, pszFormat);
		_vsnprintf( pszEndOfMessage, (PVR_MAX_DEBUG_MESSAGE_LEN - strlen(szMessage)), pszFormat, ArgList);
//		vsprintf(pszEndOfMessage, pszFormat, ArgList);
		va_end(ArgList);

		pszFilename = pszFilepath;
		pszCurrentChar = pszFilepath;

		/* Strip the path from the filename */
		while(*pszCurrentChar != '\0')
		{
			if((((*pszCurrentChar) == '\\') || ((*pszCurrentChar) == '/')))
			{
				pszFilename = pszCurrentChar + 1;
			}
			++pszCurrentChar;
		}

		pszEndOfMessage = &szMessage[strlen(szMessage)];

		sprintf(pszEndOfMessage, " (%u,%s)\n", (unsigned int)uLine, pszFilename);

#ifndef UNICODE
		OutputDebugString(szMessage);
#else
		{
			TCHAR szUnicodeMessage[PVR_MAX_DEBUG_MESSAGE_LEN+1];
			ASCIITOTSTR( szUnicodeMessage, szMessage, PVR_MAX_DEBUG_MESSAGE_LEN );
			OutputDebugString(szUnicodeMessage);
		}
#endif
	}
}
#endif /*defined(DEBUG) || defined(D3DM_RETAIL_DBGOUT) */

#if defined(DEBUG)
/*****************************************************************************
 FUNCTION	: DBGOutputVertexData
    
 PURPOSE	: Outputs vertex data to stream

 PARAMETERS	: dwHeader	- The header word to decode
  
 RETURNS	: PSTR		- The decoded header type
*****************************************************************************/
IMG_VOID DBGOutputVertexData(PVR_NATIVE_VERTEX* psVertex, 
							 IMG_UINT32			ui32VertexCount, 
							 IMG_UINT32			ui32PrimIndex)
{
	TCHAR		szOut[MAX_PATH];
	IMG_UINT32	ui32VertexProcessed = 0;

	ZeroMemory(szOut, MAX_PATH);
	_stprintf(szOut, TEXT("Current Prim Index  - %u"), ui32PrimIndex);
	OutputDebugString(szOut);

	while(ui32VertexProcessed++ < ui32VertexCount)
	{
		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("**** Vertex #%u ****"), ui32VertexProcessed);
		OutputDebugString(szOut);

		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("Window - X - %f"), NTV2FL(psVertex->sWindow.x));
		OutputDebugString(szOut);

		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("Window - Y - %f"), NTV2FL(psVertex->sWindow.y));
		OutputDebugString(szOut);

		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("Window - Z - %f"), NTV2FL(psVertex->sWindow.z));
		OutputDebugString(szOut);

		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("Window - W - %f"), NTV2FL(psVertex->sWindow.w));
		OutputDebugString(szOut);

		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("Texture 0 - X - %f"), NTV2FL(psVertex->sTextures[0].x));
		OutputDebugString(szOut);

		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("Texture 0 - Y - %f"), NTV2FL(psVertex->sTextures[0].y));
		OutputDebugString(szOut);

		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("Texture 0 - Z - %f"), NTV2FL(psVertex->sTextures[0].z));
		OutputDebugString(szOut);

		ZeroMemory(szOut, MAX_PATH);
		_stprintf(szOut, TEXT("********************"), ui32VertexProcessed);
		OutputDebugString(szOut);

		psVertex++;
	}
}

#pragma pack(push, 1)

typedef struct
{
	BITMAPFILEHEADER	bmfHeader;
	BITMAPINFOHEADER	bmiHeader;
}BMP_HEADER, *PBMP_HEADER;

#pragma pack(pop)
 /******************************************************************************
 * Function Name: Replicate
 *
 * Inputs       : ULONG ulData, ulMask, ulShift, ulCount
 * Outputs      : -
 * Returns      : ULONG ulData
 * Globals Used : -
 *
 * Description  : Given a mask will relicate data shifted by n bit m times
 * Pre-condition:
 *****************************************************************************/
ULONG Replicate(ULONG ulData, ULONG ulMask, ULONG ulShift, int nCount)
{
#ifndef JUST_SHIFT
	ULONG ulBits;
	int   nCnt;

	ulBits = ulData & ulMask;
	ulData = 0;

	for (nCnt=0; nCnt <= nCount; nCnt++)
	{
		ulData = ulData | (ulBits >> (ulShift * nCnt));
	}
#else
	ulData &= ulMask;
#endif

	return ulData;
}
/***********************************************************************************
 Function Name	: CreateBmp
 Inputs			: 
 Outputs		: 
 Returns		: 
 Globals Used	: 
 Description	: 
************************************************************************************/
BOOL CreateBmp(LPCTSTR pwszFileName,
			   PVOID pvData,
			   ULONG ulX,
			   ULONG ulY,
			   ULONG ulStride,
			   ULONG ulBpp,
			   D3DMFORMAT eDstFmt,
			   RGBQUAD *prgbqPalette)
{
	HANDLE		hFile;
	BMP_HEADER	sBmpHeader;
	ULONG		ulBytesWritten;
	ULONG		ulXIdx;
	LONG		lYIdx;	
	RGBTRIPLE *	psLineStore;
	ULONG		ulNumEntries = 0;
	ULONG		ulPaletteSize = 0;
	ULONG		ulImageSize = 0;
	ULONG		ulLineSize = abs(ulStride);
	
	/* First alloc enough room for one line */
	if ((psLineStore = (RGBTRIPLE *)D3DMAllocate((ulX * sizeof(RGBTRIPLE) + sizeof(ULONG)))) == NULL)
	{
		//DPFERROR((L"CreateBmp : Alloc line store"));
		return (FALSE);
	}

	/* Open file */
	hFile = CreateFile(pwszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, 0);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		//DPFERROR((L"CreateBmp : Failed to create file %s", pwszFileName));
		D3DMFree(psLineStore);
		return FALSE;
	}

	/* Write BMP header */	
	if (sizeof(BMP_HEADER) != 54)
	{
		sBmpHeader.bmfHeader.bfType = sizeof(BMP_HEADER);
		DebugBreak();
	}

	if (ulBpp <= 8)
	{
		ulNumEntries = 1<<ulBpp;
		ulPaletteSize = ulNumEntries * sizeof(RGBQUAD);
		ulImageSize = ulLineSize * ulY;
	}
	else
	{
		ulImageSize = (ulX * ulY * sizeof(RGBTRIPLE));
	}	

	/* File Header */
	sBmpHeader.bmfHeader.bfType      = 0x4D42;		/* "BM" */
	sBmpHeader.bmfHeader.bfSize      = sizeof(BMP_HEADER) + ulImageSize + ulPaletteSize;
	sBmpHeader.bmfHeader.bfReserved1 = 0;
	sBmpHeader.bmfHeader.bfReserved2 = 0;
	sBmpHeader.bmfHeader.bfOffBits   = sizeof(BMP_HEADER) + ulPaletteSize;

	/* Info Header */
	sBmpHeader.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
	sBmpHeader.bmiHeader.biWidth         = ulX; 
	sBmpHeader.bmiHeader.biHeight        = ulY; 
	sBmpHeader.bmiHeader.biPlanes        = 1; 
	sBmpHeader.bmiHeader.biBitCount      = (BYTE)((ulBpp <= 8) ? ulBpp : 24);
	sBmpHeader.bmiHeader.biCompression   = BI_RGB;
	sBmpHeader.bmiHeader.biSizeImage     = ulImageSize; 
	sBmpHeader.bmiHeader.biXPelsPerMeter = 0; 
	sBmpHeader.bmiHeader.biYPelsPerMeter = 0; 
	sBmpHeader.bmiHeader.biClrUsed       = 0; 
	sBmpHeader.bmiHeader.biClrImportant  = 0;
	
	if (ulBpp == 2)
	{
		/* dump as 4Bpp as there is not bmp format for 2Bpp */
		ULONG ulNewImgSize = ulImageSize * 2;
		ULONG ulNewPalSize = 16 * sizeof(RGBQUAD);

		sBmpHeader.bmfHeader.bfSize      = sizeof(BMP_HEADER) + ulNewImgSize + ulNewPalSize;
		sBmpHeader.bmfHeader.bfOffBits   = sizeof(BMP_HEADER) + ulNewPalSize;
		sBmpHeader.bmiHeader.biBitCount  = 4;
		sBmpHeader.bmiHeader.biSizeImage = ulNewImgSize; 
	}

	if (WriteFile(hFile, &sBmpHeader, sizeof(BMP_HEADER), &ulBytesWritten, NULL) == FALSE)
	{
		//DPFERROR((L"CreateBmp : Failed to write BMP header"));
		D3DMFree(psLineStore);
		CloseHandle(hFile);
		return (FALSE);
	}

	if (ulNumEntries)
	{
		PBYTE pbyData;
		ULONG ulNewLineSize;

		ASSERT (prgbqPalette != NULL);

		if (WriteFile(hFile, prgbqPalette, ulPaletteSize, &ulBytesWritten, NULL) == FALSE)
		{
			//DPFERROR((L"CreateBmp : Failed to write palette"));
			D3DMFree(psLineStore);
			CloseHandle(hFile);
			return (FALSE);
		}

		/* move to last line in bitmap */
		pbyData = (PBYTE)pvData + ((ulY - 1) * ulStride);

		ulNewLineSize = ((((ulX + 1) >> 1) + 3) & 0xFFFFFFFC);

		/* special case 2Bpp as there is no bmp format available */
		if (ulBpp == 2)
		{
			int nCnt;
			ULONG ulBlack = 0;

			/* pad out palette entries to 16 */
			for (nCnt = 0; nCnt < 12; nCnt++)
			{
				if (WriteFile(hFile, &ulBlack, sizeof(RGBQUAD), &ulBytesWritten, NULL) == FALSE)
				{
					//DPFERROR((L"CreateBmp : Failed to write palette"));
					D3DMFree(psLineStore);
					CloseHandle(hFile);
					return (FALSE);
				}
			}

			while (ulY--)
			{
				PBYTE pbyPtr = pbyData;
				WORD *pwWritePix = (WORD *)psLineStore;

				for (nCnt = 0; nCnt < (int)ulNewLineSize; nCnt += 2)
				{
					BYTE byTemp = *pbyPtr++;

					/* 0xE4 -> 0x2301 - appears to be right */
					*pwWritePix++ = ((byTemp & 0x03) << 8) |
									((byTemp & 0x0C) << 10) |
									((byTemp & 0x30) >> 4) |
									((byTemp & 0xC0) >> 2);
			  	}

				if (WriteFile(hFile, psLineStore, ulNewLineSize, &ulBytesWritten, NULL) == FALSE)
				{
					//DPFERROR((L"CreateBmp : Failed to write bitmap bits"));
					D3DMFree(psLineStore);
					CloseHandle(hFile);
					return (FALSE);
				}

				pbyData -= ulStride;
			}
		}
		else
		{
			/* write bimap bits unprocessed */
			while (ulY--)
			{
				memset (psLineStore, 0, ulNewLineSize);
				memcpy (psLineStore, pbyData, ulLineSize);

				if (WriteFile(hFile, psLineStore, ulNewLineSize, &ulBytesWritten, NULL) == FALSE)
				{
					//DPFERROR((L"CreateBmp : Failed to write bitmap bits"));
					D3DMFree(psLineStore);
					CloseHandle(hFile);
					return (FALSE);
				}

				pbyData -= ulStride;
			}
		}
	}
	else
	{	
		/* Now do each row */
		for (lYIdx = ulY-1; lYIdx >= 0; lYIdx--)
		{
			PVOID	   pvCurPix=(PVOID)((PBYTE)pvData + (lYIdx * ulStride));
			ULONG	   ulLinPadding;		
			RGBTRIPLE *psWritePix = psLineStore;

			for (ulXIdx=0; ulXIdx < ulX; ulXIdx++, psWritePix++)
			{
				switch (ulBpp)
				{
					case 16 :
					{
						PWORD	pwPix=(PWORD)pvCurPix;
						
						psWritePix->rgbtBlue  = (BYTE)Replicate((*pwPix << 3), 0xF8, 5, 1);
						psWritePix->rgbtGreen = (BYTE)Replicate((*pwPix >> 3), 0xFC, 6, 1);
						psWritePix->rgbtRed   = (BYTE)Replicate((*pwPix >> 8), 0xF8, 5, 1);
						pvCurPix = (PVOID)(((PWORD)pvCurPix)+1);
						break;
					}
					case 24 :
					{
						PBYTE	pbyPix=(PBYTE)pvCurPix;
						
						psWritePix->rgbtBlue  = *pbyPix;
						psWritePix->rgbtGreen = *(pbyPix+1);
						psWritePix->rgbtRed   = *(pbyPix+2);
						pvCurPix=(PVOID)(((PBYTE)pvCurPix)+3);
						break;
					}
					case 32 :
					{
						PBYTE	pbyPix = (PBYTE)pvCurPix;
						
						psWritePix->rgbtBlue  = *pbyPix;
						psWritePix->rgbtGreen = *(pbyPix+1);
						psWritePix->rgbtRed   = *(pbyPix+2);
						pvCurPix=(PVOID)(((PBYTE)pvCurPix)+4);
						break;
					}
					default :
					{
						//DPFINFO((L"CreateBmp : a bit depth of %ld is not supported", ulBpp));
						DebugBreak();
						D3DMFree(psLineStore);
						CloseHandle(hFile);
						return (FALSE);
					}
				}
			}

			/* Each line of pixels needs to be a multiple DWORD in size */
			if (((ulX * sizeof(RGBTRIPLE)) % 4) != 0)
			{
				ulLinPadding = sizeof(ULONG) - ((ulX * sizeof(RGBTRIPLE)) % 4);
			}
			else
			{
				ulLinPadding = 0;
			}
			
			/* Now write out line of pixels and padding */
			if (WriteFile(hFile, psLineStore, (ulX * sizeof(RGBTRIPLE)) + ulLinPadding, &ulBytesWritten, NULL) == FALSE)
			{
				//DPFERROR((L"CreateBmp : Failed to write pixel"));
				D3DMFree(psLineStore);
				CloseHandle(hFile);
				return (FALSE);
			}
		}
	}

	/* Close file */
	CloseHandle(hFile);

	/* Free mem */
	D3DMFree(psLineStore);

	return (TRUE);
}
/*****************************************************************************
 FUNCTION	: DBGDecodeTACSBlockType
    
 PURPOSE	: Decodes the block-type from a TA control-stream block header

 PARAMETERS	: dwHeader	- The header word to decode
  
 RETURNS	: PSTR		- The decoded header type
*****************************************************************************/
PSTR DBGDecodeTACSBlockType(LPD3DM_CONTEXT psContext, DWORD dwHeader)
{
	DWORD	dwBlockType;
	PSTR	pszBlockName;

	dwBlockType = dwHeader & (~MBX1_TAOBJTYPE_TYPECLRMASK);
	switch (dwBlockType)
	{
		case MBX1_TAOBJTYPE_STATE:
		{
			pszBlockName = "state";
			break;
		}

		case MBX1_TAOBJTYPE_VERTEXFACESTRIP:
		{
			pszBlockName = "tri-strip";
			break;
		}

		case MBX1_TAOBJTYPE_VERTEXFACEFAN:
		{
			pszBlockName = "tri-fan";
			break;
		}

		case MBX1_TAOBJTYPE_VERTEXLINESTRIP:
		{
			pszBlockName = "line-strip";
			break;
		}

		case MBX1_TAOBJTYPE_VERTEXFACELIST:
		{
			pszBlockName = "tri-list";
			break;
		}

		case MBX1_TAOBJTYPE_VGP_CONTROL:
		{
			pszBlockName = "VGP-control";
			break;
		}

		case MBX1_TAOBJTYPE_VGP_CODE:
		{
			pszBlockName = "VGP-code";
			break;
		}

		case MBX1_TAOBJTYPE_VGP_CONSTANTS:
		{
			pszBlockName = "VGP-constants";
			break;
		}

		case MBX1_TAOBJTYPE_POINTS:
		{
			pszBlockName = "point-list";
			break;
		}

		case MBX1_TAOBJTYPE_STREAMEND:
		{
			pszBlockName = "stream-end";
			break;
		}

		default:

⌨️ 快捷键说明

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