Esempio n. 1
0
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pd3d = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice. Since we are now
    // using more complex geometry, we will create a device with a zbuffer.
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

	if( SUCCEEDED(g_pd3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, false, D3DMULTISAMPLE_4_SAMPLES, NULL)))
	{
		d3dpp.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES;
	}
	else if( SUCCEEDED(g_pd3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, false, D3DMULTISAMPLE_2_SAMPLES, NULL)))
	{
		d3dpp.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES;
	}
	else
	{
		d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
	}

    // Create the D3DDevice
    if( FAILED( g_pd3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_HARDWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Turn on the zbuffer
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
	g_pd3dDevice->SetRenderState( D3DRS_MULTISAMPLEANTIALIAS, TRUE);
    return S_OK;
}
Esempio n. 2
0
void setAA(D3DPRESENT_PARAMETERS& d3dpp) {
	DWORD qual;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	for (int i = D3DMULTISAMPLE_16_SAMPLES; i > D3DMULTISAMPLE_NONMASKABLE; i--) {
		if (SUCCEEDED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, TRUE, (D3DMULTISAMPLE_TYPE)i, &qual))) {
			d3dpp.MultiSampleType = (D3DMULTISAMPLE_TYPE)i;
			d3dpp.MultiSampleQuality = qual - 1;
			return;
		}
	}
}
Esempio n. 3
0
D3DDevice::D3DDevice(HWND hwnd, LPDIRECT3D9 d3d, size_t width, size_t height,
        bool fullscreen)
{
    HRESULT ret = 0;
    D3DPRESENT_PARAMETERS settings;
    D3DDISPLAYMODE dmode;

    ZeroMemory(&settings, sizeof(D3DPRESENT_PARAMETERS));

    ret = d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dmode);
    if(FAILED(ret))
    {
        return;
    }

    settings.BackBufferWidth = width;
    settings.BackBufferHeight = height;
    settings.BackBufferFormat = fullscreen ? dmode.Format : D3DFMT_UNKNOWN;
    settings.BackBufferCount = 1;
    //settings.hDeviceWindow = hwnd;
    settings.Windowed = !fullscreen;
    settings.SwapEffect = D3DSWAPEFFECT_DISCARD;

    if(SUCCEEDED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, 
                    D3DDEVTYPE_HAL, settings.BackBufferFormat, !fullscreen,
                    D3DMULTISAMPLE_2_SAMPLES, NULL)))
    {
        settings.MultiSampleQuality = D3DMULTISAMPLE_2_SAMPLES;
    }
    else
    {
        settings.MultiSampleQuality = D3DMULTISAMPLE_NONE;
    }

    settings.EnableAutoDepthStencil = false;
    settings.AutoDepthStencilFormat = D3DFMT_D16;
    settings.FullScreen_RefreshRateInHz = 0;
    settings.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //0;
    settings.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;

    ret = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING, &settings, &m_device);

    if(FAILED(ret))
    {
        m_device = NULL;
        /* TODO */
        return;
    }

    /* copy settings */
    memcpy(&m_settings, &settings, sizeof(settings));
}
Esempio n. 4
0
//-----------------------------------------------------------------------------
// Desc: 初始化Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    HRESULT hr = S_OK;

    //创建Direct3D对象, 该对象用来创建Direct3D设备对象
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    //设置D3DPRESENT_PARAMETERS结构, 准备创建Direct3D设备对象
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed         = TRUE;
    d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.MultiSampleType  = D3DMULTISAMPLE_4_SAMPLES;

    //创建Direct3D设备对象
    if(FAILED(g_pD3D->CheckDeviceMultiSampleType (D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL
              ,D3DFMT_X8R8G8B8,FALSE,D3DMULTISAMPLE_4_SAMPLES,NULL)))
    {
        MessageBox(hWnd, L"硬件不支持多采样反锯齿!\n采用参考设备!"
                   , L"AntiAlisa", 0);
        hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                   &d3dpp, &g_pd3dDevice );
    }
    else
    {
        hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                   &d3dpp, &g_pd3dDevice );
    }

    //在此设置整个程序运行期间不变换的渲染状态

    return hr;
}
Esempio n. 5
0
HRESULT HookIDirect3D9::CheckDeviceMultiSampleType(LPVOID _this, UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType,DWORD* pQualityLevels)
{
	LOG_API();
	return pD3D->CheckDeviceMultiSampleType(Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType, pQualityLevels);
}
Esempio n. 6
0
bool InitialiseDirectX()
{
    const D3DFORMAT backBufferFormat = D3DFMT_D16;
    const D3DFORMAT textureFormat = D3DFMT_A8R8G8B8;

    if(FAILED(d3d = Direct3DCreate9(D3D_SDK_VERSION)))
    {
        ShowMessageBox("Direct3D interface creation has failed");
        return false;
    }

    // Build present params
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    D3DMULTISAMPLE_TYPE antiAliasingLvl;
    bool antiAliasing = false;
    if(SUCCEEDED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, 
        D3DDEVTYPE_HAL, textureFormat, true, D3DMULTISAMPLE_2_SAMPLES, nullptr)))
    {
        d3dpp.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES;
        antiAliasingLvl = D3DMULTISAMPLE_2_SAMPLES;
        antiAliasing = true;
    }

    d3dpp.hDeviceWindow = hWnd;
    d3dpp.Windowed = true;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = textureFormat;
    d3dpp.BackBufferWidth = WINDOW_WIDTH; 
    d3dpp.BackBufferHeight = WINDOW_HEIGHT;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = backBufferFormat; 

    if(FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
        D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3ddev)))
    {
        ShowMessageBox("Direct3D interface creation has failed");
        return false;
    }

    // Create Z-buffer
    if(FAILED(d3ddev->CreateDepthStencilSurface(WINDOW_WIDTH, WINDOW_HEIGHT, backBufferFormat,
        antiAliasing ? antiAliasingLvl : D3DMULTISAMPLE_NONE, NULL, TRUE, &backBuffer, NULL)))
    {
        ShowMessageBox("Z-buffer creation has failed");
        return false;
    }
    d3ddev->SetRenderTarget(0,backBuffer);

    // Set render states
    d3ddev->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, antiAliasing);

    // Check shader capabilities
    D3DCAPS9 caps;
    d3ddev->GetDeviceCaps(&caps);

    // Check for vertex shader version 2.0 support.
    if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0)) 
    {
        ShowMessageBox("Shader model 2.0 or higher is required");
        return false;
    }

    return true;
}
Esempio n. 7
0
void Graphics::init(int windowId, int depthBufferBits, int stencilBufferBits) {
	if (!hasWindow()) return;

	hWnd = (HWND)System::windowHandle(windowId);
	long style = GetWindowLong(hWnd, GWL_STYLE);

	resizable = false;

	if ((style & WS_SIZEBOX) != 0) {
		resizable = true;
	}

	if ((style & WS_MAXIMIZEBOX) != 0) {
		resizable = true;
	}

	// TODO (DK) just setup the primary window for now and ignore secondaries
	//	-this should probably be implemented via swap chain for real at a later time
	//  -http://www.mvps.org/directx/articles/rendering_to_multiple_windows.htm
	if (windowId > 0) {
		return;
	}

#ifdef SYS_WINDOWS
	// TODO (DK) convert depthBufferBits + stencilBufferBits to: d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = (!fullscreen) ? TRUE : FALSE;

	if (resizable) {
		d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
		d3dpp.BackBufferCount = 1;
		d3dpp.BackBufferWidth = Kore::System::desktopWidth();
		d3dpp.BackBufferHeight = Kore::System::desktopHeight();
	}
	else {
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.BackBufferCount = 2;
		d3dpp.BackBufferWidth = System::windowWidth(windowId);
		d3dpp.BackBufferHeight = System::windowHeight(windowId);
	}

	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
	// d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
	if (antialiasingSamples() > 1) {
		for (int samples = min(antialiasingSamples(), 16); samples > 1; --samples) {
			if (SUCCEEDED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_A8R8G8B8, FALSE, (D3DMULTISAMPLE_TYPE)samples, nullptr))) {
				d3dpp.MultiSampleType = (D3DMULTISAMPLE_TYPE)samples;
				break;
			}
		}
	}
#endif

#ifdef SYS_XBOX360
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	XVIDEO_MODE VideoMode;
	XGetVideoMode(&VideoMode);
	// g_bWidescreen = VideoMode.fIsWideScreen;
	d3dpp.BackBufferWidth = min(VideoMode.dwDisplayWidth, 1280);
	d3dpp.BackBufferHeight = min(VideoMode.dwDisplayHeight, 720);
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
#endif

#ifdef SYS_XBOX360
	d3d->CreateDevice(0, D3DDEVTYPE_HAL, nullptr, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device);
#else
	if (!SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device)))
		d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device);
// d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device);
#endif

#ifdef SYS_WINDOWS
	if (System::hasShowWindowFlag(/*windowId*/)) {
		ShowWindow(hWnd, SW_SHOWDEFAULT);
		UpdateWindow(hWnd);
	}
#endif

	initDeviceStates();

#ifdef SYS_WINDOWS
	if (fullscreen) {
		// hz = d3dpp.FullScreen_RefreshRateInHz;
		D3DDISPLAYMODE mode;
		device->GetDisplayMode(0, &mode);
		hz = mode.RefreshRate;
	}
	if (!fullscreen || hz == 0) {
		DEVMODE devMode;
		EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
		hz = devMode.dmDisplayFrequency;
	}
#endif
#ifdef SYS_XBOX360
	hz = 60;
#endif
	// vsync = d3dpp.PresentationInterval != D3DPRESENT_INTERVAL_IMMEDIATE;

	System::ticks test1 = Kore::System::timestamp();
	for (int i = 0; i < 3; ++i) swapBuffers(windowId);
	System::ticks test2 = Kore::System::timestamp();
	if (test2 - test1 < (1.0 / hz) * System::frequency()) {
		vsync = false;
	}
	else
		vsync = true;

#ifdef USE_SHADER
	vertexShader = new Shader("standard", SHADER_VERTEX);
	pixelShader = new Shader("standard", SHADER_FRAGMENT);

	vertexShader->set();
	pixelShader->set();

	setFragmentBool(L"lighting", false);
#endif

	_width = System::windowWidth(windowId);
	_height = System::windowHeight(windowId);

	System::makeCurrent(windowId);
}
Esempio n. 8
0
void Graphics::init() {
	if (!hasWindow()) return;

	hWnd = (HWND)System::createWindow();

	d3d = Direct3DCreate9(D3D_SDK_VERSION);
	//if (!d3d) throw Exception("Could not initialize Direct3D9");

#ifdef SYS_WINDOWS
	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = (!fullscreen) ? TRUE : FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferCount = 2;
	d3dpp.BackBufferWidth = Application::the()->width();
	d3dpp.BackBufferHeight = Application::the()->height();
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
	//d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
	if (antialiasingSamples() > 1) {
		for (int samples = min(antialiasingSamples(), 16); samples > 1; --samples) {
			if (SUCCEEDED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_A8R8G8B8, FALSE, (D3DMULTISAMPLE_TYPE)samples, nullptr))) {
				d3dpp.MultiSampleType = (D3DMULTISAMPLE_TYPE)samples;
				break;
			}
		}
	}
#endif

#ifdef SYS_XBOX360
	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory( &d3dpp, sizeof(d3dpp) );
	XVIDEO_MODE VideoMode;
	XGetVideoMode( &VideoMode );
	//g_bWidescreen = VideoMode.fIsWideScreen;
	d3dpp.BackBufferWidth        = min(VideoMode.dwDisplayWidth, 1280);
	d3dpp.BackBufferHeight       = min(VideoMode.dwDisplayHeight, 720);
	d3dpp.BackBufferFormat       = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount        = 1;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
	d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
	d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_ONE;
#endif
	
#ifdef SYS_XBOX360
	d3d->CreateDevice(0, D3DDEVTYPE_HAL, nullptr, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device);
#else
	if (!SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device)))
		d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device);
	//d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device);
#endif

#ifdef SYS_WINDOWS
	if (Application::the()->showWindow()) {
		ShowWindow(hWnd, SW_SHOWDEFAULT);
		UpdateWindow(hWnd);
	}
#endif

	initDeviceStates();

#ifdef SYS_WINDOWS
	if (fullscreen) {
		//hz = d3dpp.FullScreen_RefreshRateInHz;
		D3DDISPLAYMODE mode;
		device->GetDisplayMode(0, &mode);
		hz = mode.RefreshRate;
	}
	if (!fullscreen || hz == 0) {
		DEVMODE devMode;
		EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
		hz = devMode.dmDisplayFrequency;
	}
#endif
#ifdef SYS_XBOX360
	hz = 60;
#endif
	//vsync = d3dpp.PresentationInterval != D3DPRESENT_INTERVAL_IMMEDIATE;

	System::ticks test1 = Kore::System::timestamp();
	for (int i = 0; i < 3; ++i) swapBuffers();
	System::ticks test2 = Kore::System::timestamp();
	if (test2 - test1 < (1.0 / hz) * System::frequency()) {
		vsync = false;
	}
	else vsync = true;
	
#ifdef USE_SHADER
	vertexShader = new Shader("standard", SHADER_VERTEX);
	pixelShader = new Shader("standard", SHADER_FRAGMENT);

	vertexShader->set();
	pixelShader->set();

	setFragmentBool(L"lighting", false);
#endif
}