Sign in to follow this  
Logikz

Direct3D9: (ERROR) :Invalid bit set in Flags member.

Recommended Posts

Hi, I am learning D3D and am having a strange issue with it trying to call CreateDevice, this is from the Direct3D Debugger: Direct3D9: (ERROR) :Invalid bit set in Flags member. ValidatePresentParameters fails.
#include "stdafx.h"
#include "Graphics.h"

#ifdef _DEBUG
#   pragma comment(lib, "d3dx9d.lib")
#else
#   pragma comment(lib, "d3dx9.lib")
#endif
#pragma comment(lib, "d3d9.lib")


tGraphics::tGraphics():
fD3D9(NULL),
fDevice(NULL)
{}

tGraphics::~tGraphics()
{}

void tGraphics::mRelease()
{	
}

BOOL tGraphics::mInitialize(HWND window, BOOL windowed)
{
	fWindow = window;
	BOOL result = false;
	fD3D9 = Direct3DCreate9( D3D_SDK_VERSION );
	fWindowed = windowed;
	if(fD3D9)
	{
		fD3D9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &fDisplayMode);
		fD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &fCaps);

		DWORD vertexProcessing = 0;
		
		if(fCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
		{
			vertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING;
			
			if(fCaps.DevCaps & D3DDEVCAPS_PUREDEVICE)
			{
				vertexProcessing |= D3DCREATE_PUREDEVICE;
			}
		}
		else
		{
			vertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
		}

		if( mBuildPresentParameters() )
		{
			HRESULT hresult = 0;
                          //fails, don't know why...
			hresult = fD3D9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fWindow, vertexProcessing, &fPresentParams, &fDevice );

			if( SUCCEEDED( hresult ))
			{
				result = true;
			}
		}		
	}
	return result;
}

BOOL tGraphics::mBuildPresentParameters()
{
	BOOL result = false;

	ZeroMemory( &fPresentParams, sizeof(&fPresentParams));
	D3DFORMAT adapterFormat = (fWindowed) ? fDisplayMode.Format : D3DFMT_X8R8G8B8;

	if( SUCCEEDED( fD3D9->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, adapterFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24S8) ) )
	{
		fPresentParams.AutoDepthStencilFormat = D3DFMT_D24X8;
		result = mSetPresentParams();
	}
	else if ( SUCCEEDED( fD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, adapterFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16) ) )
	{
		fPresentParams.AutoDepthStencilFormat = D3DFMT_D16;
		result = mSetPresentParams();
	}

	return result;
}

BOOL tGraphics::mSetPresentParams()
{
	fPresentParams.BackBufferWidth = (fWindowed) ? 0 : fDisplayMode.Width;
	fPresentParams.BackBufferHeight = (fWindowed) ? 0: fDisplayMode.Height;
	fPresentParams.BackBufferCount = 1;
	fPresentParams.BackBufferFormat =  D3DFMT_X8R8G8B8;
	fPresentParams.EnableAutoDepthStencil = true;
	fPresentParams.FullScreen_RefreshRateInHz = (fWindowed) ? 0 : fDisplayMode.RefreshRate;
	fPresentParams.BackBufferCount = 1;
	fPresentParams.MultiSampleType = D3DMULTISAMPLE_NONE;
	fPresentParams.MultiSampleQuality = 0;
	fPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
	fPresentParams.hDeviceWindow = fWindow;
	fPresentParams.Windowed = fWindowed;
	fPresentParams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

	return true;
}

BOOL tGraphics::mReset()
{
	BOOL result = false;
	if(fDevice)
	{
		if( mBuildPresentParameters() )
		{
			fDevice->Reset(&fPresentParams);
			result = true;
		}
	}
	return result;
}

if you need anything else, just let me know.

Share this post


Link to post
Share on other sites
Assuming fPresentParams is a class member of tGraphics, it will be filled with "random" data when tGraphics is instantiated.

I'd assume that since you are not setting the Flags member of fPresentParams to zero, it is filled with some random bits that do not correspond to valid values.

Just add a line to mSetPresentParams() that assigns zero to fPresentParams.Flags.


BOOL tGraphics::mSetPresentParams()
{
fPresentParams.BackBufferWidth = (fWindowed) ? 0 : fDisplayMode.Width;
fPresentParams.BackBufferHeight = (fWindowed) ? 0: fDisplayMode.Height;
fPresentParams.BackBufferCount = 1;
fPresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
fPresentParams.EnableAutoDepthStencil = true;
fPresentParams.FullScreen_RefreshRateInHz = (fWindowed) ? 0 : fDisplayMode.RefreshRate;
fPresentParams.BackBufferCount = 1;
fPresentParams.MultiSampleType = D3DMULTISAMPLE_NONE;
fPresentParams.MultiSampleQuality = 0;
fPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
fPresentParams.hDeviceWindow = fWindow;
fPresentParams.Windowed = fWindowed;
fPresentParams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

fPresentParams.Flags=0;

return true;
}

HTH

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this