2007-12-14 07:13:21 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS ReactX
|
|
|
|
* FILE: dll/directx/d3d9/d3d9_helpers.c
|
|
|
|
* PURPOSE: d3d9.dll helper functions
|
|
|
|
* PROGRAMERS: Gregor Brunmar <gregor (dot) brunmar (at) home (dot) se>
|
|
|
|
*/
|
|
|
|
|
2008-01-13 11:45:31 +00:00
|
|
|
#include <d3d9.h>
|
2007-12-14 07:13:21 +00:00
|
|
|
#include "d3d9_helpers.h"
|
2007-12-17 21:22:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ddraw.h>
|
2007-12-20 21:18:42 +00:00
|
|
|
#include <debug.h>
|
2007-12-14 07:13:21 +00:00
|
|
|
|
2007-12-20 21:18:42 +00:00
|
|
|
#define MEM_ALIGNMENT 0x20
|
2007-12-14 07:13:21 +00:00
|
|
|
|
2007-12-29 15:19:13 +00:00
|
|
|
static LPCSTR D3D9_DebugRegPath = "Software\\Microsoft\\Direct3D";
|
2007-12-14 07:13:21 +00:00
|
|
|
|
|
|
|
BOOL ReadRegistryValue(IN DWORD ValueType, IN LPCSTR ValueName, OUT LPBYTE DataBuffer, IN OUT LPDWORD DataBufferSize)
|
|
|
|
{
|
|
|
|
HKEY hKey;
|
|
|
|
DWORD Type;
|
|
|
|
LONG Ret;
|
|
|
|
|
2008-02-04 21:33:12 +00:00
|
|
|
if (ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE, D3D9_DebugRegPath, 0, KEY_QUERY_VALUE, &hKey))
|
2007-12-14 07:13:21 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2008-02-04 21:33:12 +00:00
|
|
|
Ret = RegQueryValueExA(hKey, ValueName, 0, &Type, DataBuffer, DataBufferSize);
|
2007-12-14 07:13:21 +00:00
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
|
|
|
|
if (ERROR_SUCCESS != Ret)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type != ValueType)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-17 21:22:40 +00:00
|
|
|
|
2008-08-21 02:34:28 +00:00
|
|
|
HRESULT SafeFormatString(OUT LPSTR Buffer, IN DWORD BufferSize, IN LPCSTR FormatString, ... )
|
2008-08-19 15:42:23 +00:00
|
|
|
{
|
|
|
|
DWORD BytesWritten;
|
|
|
|
va_list vargs;
|
|
|
|
|
|
|
|
if (BufferSize == 0)
|
|
|
|
return DDERR_INVALIDPARAMS;
|
|
|
|
|
|
|
|
va_start(vargs, FormatString);
|
|
|
|
BytesWritten = _vsnprintf(Buffer, BufferSize-1, FormatString, vargs);
|
|
|
|
|
|
|
|
if (BytesWritten < BufferSize)
|
|
|
|
return DDERR_GENERIC;
|
|
|
|
|
|
|
|
Buffer[BufferSize-1] = '\0';
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT SafeCopyString(OUT LPSTR Dst, IN DWORD DstSize, IN LPCSTR Src)
|
|
|
|
{
|
|
|
|
HRESULT hr = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if (Dst == NULL || DstSize == 0 || Src == NULL)
|
|
|
|
return DDERR_INVALIDPARAMS;
|
|
|
|
|
|
|
|
while (*Src != '\0' && DstSize > 0)
|
|
|
|
{
|
|
|
|
*Dst++ = *Src++;
|
|
|
|
--DstSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DstSize == 0)
|
|
|
|
{
|
|
|
|
--Dst;
|
|
|
|
hr = DDERR_GENERIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT SafeAppendString(IN OUT LPSTR Dst, IN DWORD DstSize, IN LPCSTR Src)
|
|
|
|
{
|
2008-08-23 15:01:19 +00:00
|
|
|
size_t CurrentDstLength;
|
2008-08-19 15:42:23 +00:00
|
|
|
|
|
|
|
if (Dst == NULL || DstSize == 0)
|
|
|
|
return DDERR_INVALIDPARAMS;
|
|
|
|
|
|
|
|
CurrentDstLength = strlen(Dst);
|
|
|
|
|
|
|
|
return SafeCopyString(Dst + CurrentDstLength, DstSize - CurrentDstLength, Src);
|
|
|
|
}
|
|
|
|
|
2007-12-20 21:18:42 +00:00
|
|
|
HRESULT AlignedAlloc(IN OUT LPVOID *ppObject, IN SIZE_T dwSize)
|
|
|
|
{
|
2009-12-15 15:16:01 +00:00
|
|
|
ULONG_PTR AddressOffset;
|
2007-12-20 21:18:42 +00:00
|
|
|
ULONG AlignedMask = MEM_ALIGNMENT - 1;
|
|
|
|
CHAR *AlignedPtr;
|
|
|
|
ULONG_PTR *AlignedOffsetPtr;
|
|
|
|
|
|
|
|
if (ppObject == 0)
|
|
|
|
return DDERR_INVALIDPARAMS;
|
|
|
|
|
|
|
|
if (dwSize == 0)
|
|
|
|
{
|
|
|
|
*ppObject = NULL;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwSize += MEM_ALIGNMENT;
|
|
|
|
|
2008-03-02 14:12:24 +00:00
|
|
|
AlignedPtr = (CHAR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
|
2007-12-20 21:18:42 +00:00
|
|
|
|
|
|
|
if (AlignedPtr == 0)
|
|
|
|
return DDERR_OUTOFMEMORY;
|
|
|
|
|
2009-12-15 15:16:01 +00:00
|
|
|
AddressOffset = MEM_ALIGNMENT - ((ULONG_PTR)AlignedPtr & AlignedMask);
|
2007-12-20 21:18:42 +00:00
|
|
|
|
|
|
|
AlignedPtr += AddressOffset;
|
|
|
|
|
|
|
|
AlignedOffsetPtr = (ULONG_PTR *)(AlignedPtr - sizeof(ULONG));
|
|
|
|
*AlignedOffsetPtr = AddressOffset;
|
|
|
|
|
|
|
|
*ppObject = (ULONG_PTR *)AlignedPtr;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID AlignedFree(IN OUT LPVOID pObject)
|
|
|
|
{
|
|
|
|
CHAR *NonAlignedPtr = pObject;
|
|
|
|
ULONG_PTR *AlignedPtr = pObject;
|
|
|
|
|
|
|
|
if (pObject == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NonAlignedPtr -= *(AlignedPtr - 1);
|
|
|
|
|
2008-03-02 14:12:24 +00:00
|
|
|
HeapFree(GetProcessHeap(), 0, NonAlignedPtr);
|
2007-12-20 21:18:42 +00:00
|
|
|
}
|