Пример #1
0
void GraphicsDevice::CreateDeviceAndSwapChain()
{
	//Init swapchain desc
	DXGI_SWAP_CHAIN_DESC swapChainDesc={};
	//Swapchain characteristics
	swapChainDesc.BufferDesc.Width					= m_MainViewportInfo.width;
	swapChainDesc.BufferDesc.Height					= m_MainViewportInfo.height;
	swapChainDesc.BufferDesc.RefreshRate.Numerator	= 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator	= 1;
	swapChainDesc.BufferDesc.Format					= DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.ScanlineOrdering			= DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	swapChainDesc.BufferDesc.Scaling					= DXGI_MODE_SCALING_UNSPECIFIED;
	//Disable anti-aliasing
	swapChainDesc.SampleDesc.Count	= 1;
	swapChainDesc.SampleDesc.Quality	= 0;
	//Swapchain to window
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT;
	swapChainDesc.BufferCount = 1;
	swapChainDesc.OutputWindow = m_hWindow;
	swapChainDesc.Windowed = true;
	swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
	swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

	UINT createDeviceFlags = 0;
	#ifndef NDEBUG
		createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
	#endif

	HR(D3D10CreateDeviceAndSwapChain(0, D3D10_DRIVER_TYPE_HARDWARE, 0, createDeviceFlags, D3D10_SDK_VERSION, &swapChainDesc, &m_pSwapChain, &m_pD3DDevice));
}
	bool CRenderDevice::DeviceAndSwapChain(HRESULT& hr, HWND hWnd)
	{
		RECT rc;
		ZeroMemory( &rc, sizeof(rc) );
		GetClientRect( hWnd, &rc );

		m_ViewWidth = rc.right - rc.left;
		m_ViewHeight = rc.bottom - rc.top;


		DXGI_SWAP_CHAIN_DESC sd;
		ZeroMemory( &sd, sizeof( sd ) );
		sd.BufferCount = 1;
		sd.BufferDesc.Width = m_ViewWidth;             // Target window size
		sd.BufferDesc.Height = m_ViewHeight;           // --"--
		sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // Pixel format of target window
		sd.BufferDesc.RefreshRate.Numerator = 60;          // Refresh rate of monitor
		sd.BufferDesc.RefreshRate.Denominator = 1;         // --"--

		sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
		sd.SampleDesc.Count = 1;
		sd.SampleDesc.Quality = 0;
		sd.OutputWindow = hWnd;                          // Target window
		sd.Windowed = TRUE;                                // Whether to render in a window (TRUE) or go fullscreen (FALSE)
		hr = D3D10CreateDeviceAndSwapChain( NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0,
											D3D10_SDK_VERSION, &sd, &m_pSwapChain, &m_pd3dDevice );
		if( FAILED( hr ) )
		{
			MessageBox( NULL, "NO DEVICE OR SWAP CHAIN", "", NULL );
			return false;
		}

		return true;
	}
Пример #3
0
HRESULT CreateDeviceD3D(HWND hWnd)
{
    // Setup swap chain
    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory(&sd, sizeof(sd));
    sd.BufferCount = 2;
    sd.BufferDesc.Width = 0;
    sd.BufferDesc.Height = 0;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
    sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = hWnd;
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = TRUE;
    sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

    UINT createDeviceFlags = 0;
    //createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
    if (D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, D3D10_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice) != S_OK)
        return E_FAIL;

    CreateRenderTarget();

    return S_OK;
}
Пример #4
0
void Window::initDirect3D()
{
	//Create swap chain.
	DXGI_SWAP_CHAIN_DESC sd;
	sd.BufferDesc.Width  = windowWidth;
	sd.BufferDesc.Height = windowHeight;
	sd.BufferDesc.RefreshRate.Numerator = 60;
	sd.BufferDesc.RefreshRate.Denominator = 1;
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
	sd.SampleDesc.Count   = 1;
	sd.SampleDesc.Quality = 0;
	sd.BufferUsage  = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.BufferCount  = 1;
	sd.OutputWindow = hMainWnd;
	sd.Windowed     = true;
	sd.SwapEffect   = DXGI_SWAP_EFFECT_DISCARD;
	sd.Flags        = 0;


	//Create DirectX device.
	UINT createDeviceFlags = 0;
#if defined(DEBUG) || defined(_DEBUG)  
    createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
#endif
	HR(D3D10CreateDeviceAndSwapChain(0, d3dDriverType, 0, createDeviceFlags, D3D10_SDK_VERSION, &sd, &swapChain, &d3dDevice));


	//Create backbuffer etc, used here to prevents duplication of code
	onResize();
}
Пример #5
0
bool D3DContext::CreateDeviceAndSwapChain(Description description)
{
    unsigned int deviceFlags = 0;
#ifdef DEBUG_MODE
    deviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

    // Create the swap chain description
    description.BackBuffer.Width = (description.BackBuffer.Width == 0) ? mTargetWindow->GetClientWidth() : description.BackBuffer.Width;
    description.BackBuffer.Height = (description.BackBuffer.Height == 0) ? mTargetWindow->GetClientHeight() : description.BackBuffer.Height;

    DXGI_SWAP_CHAIN_DESC scDescription;
    ZeroMemory(&scDescription, sizeof(scDescription));
    scDescription.BufferCount = 1;
    scDescription.BufferDesc.Width = description.BackBuffer.Width;
    scDescription.BufferDesc.Height = description.BackBuffer.Height;
    scDescription.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    scDescription.BufferDesc.RefreshRate.Numerator = 60;
    scDescription.BufferDesc.RefreshRate.Denominator = 1;
    scDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    scDescription.OutputWindow = mTargetWindow->GetHandle();
    scDescription.SampleDesc.Count = 1;
    scDescription.SampleDesc.Quality = 0;
    scDescription.Windowed = !description.Fullscreen;

    // Create the device and the swap chain
    // Try different drivers for Direct3D, hardware first, reference only if that fails.
    D3D10_DRIVER_TYPE driverTypes[] = { D3D10_DRIVER_TYPE_HARDWARE, D3D10_DRIVER_TYPE_REFERENCE };
    unsigned int driverTypeCount = sizeof(driverTypes) / sizeof(driverTypes[0]);

    HRESULT result = S_OK;
    for (unsigned int i = 0; i < driverTypeCount; ++i)
    {
        result = D3D10CreateDeviceAndSwapChain(NULL,
                                               driverTypes[i],
                                               NULL,
                                               deviceFlags,
                                               D3D10_SDK_VERSION,
                                               &scDescription,
                                               &mSwapChain,
                                               &mDevice);

        if (SUCCEEDED(result))
            break;
    }

    if (FAILED(result))
        return false;

    mBackBufferSize.Width = description.BackBuffer.Width;
    mBackBufferSize.Height = description.BackBuffer.Height;

    return true;
}
Пример #6
0
HRESULT CreateDeviceD3D(HWND hWnd)
{
    // Setup swap chain
    DXGI_SWAP_CHAIN_DESC sd;
    {
        ZeroMemory(&sd, sizeof(sd));
        sd.BufferCount = 2;
        sd.BufferDesc.Width = 0;
        sd.BufferDesc.Height = 0;
        sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        sd.BufferDesc.RefreshRate.Numerator = 60;
        sd.BufferDesc.RefreshRate.Denominator = 1;
        sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
        sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
        sd.OutputWindow = hWnd;
        sd.SampleDesc.Count = 1;
        sd.SampleDesc.Quality = 0;
        sd.Windowed = TRUE;
        sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    }

    UINT createDeviceFlags = 0;
    //createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
    if (D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, D3D10_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice) != S_OK)
        return E_FAIL;

    // Setup rasterizer
    {
        D3D10_RASTERIZER_DESC RSDesc;
        memset(&RSDesc, 0, sizeof(D3D10_RASTERIZER_DESC));
        RSDesc.FillMode = D3D10_FILL_SOLID;
        RSDesc.CullMode = D3D10_CULL_NONE;
        RSDesc.FrontCounterClockwise = FALSE;
        RSDesc.DepthBias = 0;
        RSDesc.SlopeScaledDepthBias = 0.0f;
        RSDesc.DepthBiasClamp = 0;
        RSDesc.DepthClipEnable = TRUE;
        RSDesc.ScissorEnable = TRUE;
        RSDesc.AntialiasedLineEnable = FALSE;
        RSDesc.MultisampleEnable = (sd.SampleDesc.Count > 1) ? TRUE : FALSE;

        ID3D10RasterizerState* pRState = NULL;
        g_pd3dDevice->CreateRasterizerState(&RSDesc, &pRState);
        g_pd3dDevice->RSSetState(pRState);
        pRState->Release();
    }

    CreateRenderTarget();

    return S_OK;
}
Пример #7
0
void D3DApp::initDirect3D()
{
  // Fill out a DXGI_SWAP_CHAIN_DESC to describe our swap chain.

  DXGI_SWAP_CHAIN_DESC sd;
  sd.BufferDesc.Width  = mClientWidth;
  sd.BufferDesc.Height = mClientHeight;
  sd.BufferDesc.RefreshRate.Numerator = 60;
  sd.BufferDesc.RefreshRate.Denominator = 1;
  sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

  // Multisampling.
  sd.SampleDesc.Count   = 8;
  sd.SampleDesc.Quality = 0;

  sd.BufferUsage  = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  sd.BufferCount  = 1;
  sd.OutputWindow = mhMainWnd;
  sd.Windowed     = true;
  sd.SwapEffect   = DXGI_SWAP_EFFECT_DISCARD;
  sd.Flags        = 0;



  // Create the device.

  UINT createDeviceFlags = 0;
#if defined(DEBUG) || defined(_DEBUG)
    createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

  HR( D3D10CreateDeviceAndSwapChain(
      0,                 //default adapter
      md3dDriverType,
      0,                 // no software device
      createDeviceFlags,
      D3D10_SDK_VERSION,
      &sd,
      &mSwapChain,
      &md3dDevice) );


  // The remaining steps that need to be carried out for d3d creation
  // also need to be executed every time the window is resized.  So
  // just call the onResize method here to avoid code duplication.

  onResize();
}
Пример #8
0
	bool DirectX10Renderer::CreateSwapChainAndDevice()
	{
		DEBUG_OUT("DirectX10Renderer::CreateSwapChainAndDevice");

		DXGI_SWAP_CHAIN_DESC swapChainDesc;
		ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
	
		//set buffer dimensions and format
		swapChainDesc.BufferCount = 2;
		swapChainDesc.BufferDesc.Width = m_ScreenWidth;
		swapChainDesc.BufferDesc.Height = m_ScreenHeight;
		swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
		swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	
		//set refresh rate
		swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
		swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	
		//sampling settings
		swapChainDesc.SampleDesc.Quality = 0;
		swapChainDesc.SampleDesc.Count = 1;

		//output window handle
		swapChainDesc.OutputWindow = *hWnd;
		swapChainDesc.Windowed = true;    

		//Create the D3D device
		//--------------------------------------------------------------
		HRESULT result = D3D10CreateDeviceAndSwapChain(
			NULL, 
			D3D10_DRIVER_TYPE_HARDWARE,
			NULL, 
			0,
			D3D10_SDK_VERSION,
			&swapChainDesc, 
			&pSwapChain,
			&pD3DDevice
		);
		if(FAILED(result))
		{
			return FatalError("D3D device creation failed");
		}

		return true;
	}
Пример #9
0
bool DX10_Init3d()
{
	HRESULT hr = S_OK;

    RECT rc;
    GetClientRect( g_hWnd, &rc );
    UINT width = rc.right - rc.left;
    UINT height = rc.bottom - rc.top;

    UINT createDeviceFlags = 0;

    D3D10_DRIVER_TYPE driverTypes[] =
    {
        D3D10_DRIVER_TYPE_HARDWARE,
        D3D10_DRIVER_TYPE_REFERENCE,
    };
    UINT numDriverTypes = sizeof( driverTypes ) / sizeof( driverTypes[0] );

    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory( &sd, sizeof( sd ) );
    sd.BufferCount = 1;
    sd.BufferDesc.Width = width;
    sd.BufferDesc.Height = height;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = g_hWnd;
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = TRUE;

    for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
    {
        g_driverType = driverTypes[driverTypeIndex];
        hr = D3D10CreateDeviceAndSwapChain( NULL, g_driverType, NULL, createDeviceFlags,
                                            D3D10_SDK_VERSION, &sd, &g_pSwapChainDX10, &g_pd3dDeviceDX10 );
        if( SUCCEEDED( hr ) )
            break;
    }
    if( FAILED( hr ) )
        return false;

	return true;
}
Пример #10
0
HRESULT QD3D10Widget::createSwapChainAndDevice( int width, int height )
{
    UINT createDeviceFlags = 0;

#ifdef _DEBUG
    createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory( &sd, sizeof( sd ) );
    sd.BufferCount = 1;
    sd.BufferDesc.Width = width;
    sd.BufferDesc.Height = height;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 0;
    sd.BufferDesc.RefreshRate.Denominator = 0;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = winId();
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    sd.Windowed = TRUE;

    D3D10_DRIVER_TYPE driverType = D3D10_DRIVER_TYPE_HARDWARE;

    // create the device and the swap chain
    HRESULT hr = D3D10CreateDeviceAndSwapChain
                 (
                     NULL, // existing DXGI adapter
                     driverType, // driver type
                     NULL, // HMODULE pointing to loaded software rasterizer dll
                     createDeviceFlags, // device creation flags
                     D3D10_SDK_VERSION, // DX sdk version
                     &sd, // swap chain description
                     &m_pSwapChain, // output swap chain
                     &m_pDevice // output device
                 );

    return hr;
}
Пример #11
0
	RenderingSystem::RenderingSystem(Window* Window) : m_Running(true), m_Window(Window)
	{
		HRESULT hr = 0;

		DXGI_SWAP_CHAIN_DESC SwapChainDesc;
		ZeroMemory(&SwapChainDesc, sizeof(SwapChainDesc));

		SwapChainDesc.BufferCount = 1;

		//Set the width and Height of the back buffer
		SwapChainDesc.BufferDesc.Width = m_Window->getWidth();
		SwapChainDesc.BufferDesc.Height = m_Window->getHeight();

		//Set the surface to be 32 bit
		SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

		//Set the refresh rate to be 60 frames per second
		SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
		SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
		SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;

		//Set the output Window
		SwapChainDesc.OutputWindow = m_Window->getWindowHandle();

		//No multisampling at the moment
		SwapChainDesc.SampleDesc.Count = 1;
		SwapChainDesc.SampleDesc.Quality = 0;

		//Windowed mode
		SwapChainDesc.Windowed = TRUE;

		hr = D3D10CreateDeviceAndSwapChain(NULL, //Use the default Adapter
			D3D10_DRIVER_TYPE_HARDWARE, 
			NULL, //Not using DLL for rendering
			D3D10_CREATE_DEVICE_DEBUG, //Allow debug output
			D3D10_SDK_VERSION, //use the current SDK
			&SwapChainDesc,
			&m_SwapChain,
			&m_Device);
	}
Пример #12
0
bool Screen::createSCandDevice()
{
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = width;
	swapChainDesc.BufferDesc.Height = height;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow = rndrWindow;
	swapChainDesc.SampleDesc.Count = settings.getNumSamples();
	swapChainDesc.SampleDesc.Quality = settings.getNumSamples()-1;
	swapChainDesc.Windowed = TRUE;
	HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,
		D3D10_DRIVER_TYPE_HARDWARE, NULL, D3D10_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS | D3D10_CREATE_DEVICE_SINGLETHREADED||D3D10_CREATE_DEVICE_DEBUG,
		D3D10_SDK_VERSION, &swapChainDesc,
		&swapChain, &d3ddev);
	if (FAILED(hr))
	{
		MessageBox(NULL, "Error initializing Direct3D\n\n*This is usually caused by the configuration being unsupported by your graphics card, or your graphics card not supporting DirectX 10.\n\n*Try lowering and/or disabling some settings in the software config.\n\n*This may also have been caused by outdated graphics card drivers. Please make sure your graphics drivers are up to date.\n\n*If you are running a system with Nvidia Optimus, please make sure the software is running on the dedicated GPU.\n\n*Unfortunately, if your graphics card does not support DirectX 10, the only fix for this would be to use one that does.", "Fatal Error", MB_OK);
		return false;
	}
	return true;
}
Пример #13
0
HRESULT Engine::D3DDeviceSetup( UINT width, UINT height, UINT createDeviceFlags )
{
	HRESULT hr = S_OK;

	D3D10_DRIVER_TYPE driverTypes[] =
	{
		D3D10_DRIVER_TYPE_HARDWARE,
		D3D10_DRIVER_TYPE_REFERENCE,
	};
	UINT numDriverTypes = sizeof( driverTypes ) / sizeof( driverTypes[0] );

	DXGI_SWAP_CHAIN_DESC swapChain;
	ZeroMemory( &swapChain, sizeof( swapChain ) );
	swapChain.BufferCount = 1;
	swapChain.BufferDesc.Width = width;
	swapChain.BufferDesc.Height = height;
	swapChain.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
	swapChain.BufferDesc.RefreshRate.Numerator = 60;
	swapChain.BufferDesc.RefreshRate.Denominator = 1;
	swapChain.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChain.OutputWindow = myWindowHandler;
	swapChain.SampleDesc.Count = 1;
	swapChain.SampleDesc.Quality = 0;
	swapChain.Windowed = TRUE;

	for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
	{
		myDriverType = driverTypes[driverTypeIndex];
		hr = D3D10CreateDeviceAndSwapChain( NULL, myDriverType, NULL, createDeviceFlags,
			D3D10_SDK_VERSION, &swapChain, &mySwapChain, &my3DDevice );

		if( SUCCEEDED( hr ) )
			break;
	}

	return hr;
}
Пример #14
0
bool CVideo::Reset(int width, int height, bool fullscreen)
{
	Shutdown();

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
		return false;

	Uint32 flags = SDL_WINDOW_SHOWN;
	if (fullscreen) flags |= SDL_WINDOW_FULLSCREEN;
	window_ = SDL_CreateWindow("Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
	if (!window_)
	{
		SDL_Quit();
		return false;
	}

	SDL_SysWMinfo wminfo;
	SDL_VERSION(&wminfo.version);
	if (!SDL_GetWindowWMInfo(window_, &wminfo))
	{
		SDL_Quit();
		return false;
	}

	hWnd_ = wminfo.info.win.window;
	hInstance_ = (HINSTANCE)GetWindowLong(hWnd_, GWL_HINSTANCE);

	RECT dimensions;
	GetClientRect(hWnd_, &dimensions);

	unsigned int w = dimensions.right - dimensions.left;
	unsigned int h = dimensions.bottom - dimensions.top;

	D3D10_DRIVER_TYPE driverTypes[] =
	{
		D3D10_DRIVER_TYPE_HARDWARE,
		D3D10_DRIVER_TYPE_WARP,
		D3D10_DRIVER_TYPE_REFERENCE
	};

	unsigned int totalDriverTypes = ARRAYSIZE(driverTypes);

	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = w;
	swapChainDesc.BufferDesc.Height = h;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow = hWnd_;
	swapChainDesc.Windowed = true;
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;

	unsigned int creationFlags = 0;

	HRESULT result;
	unsigned int driver = 0;

	for (driver = 0; driver < totalDriverTypes; ++driver)
	{
		result = D3D10CreateDeviceAndSwapChain(0, driverTypes[driver], 0, creationFlags,
			D3D10_SDK_VERSION, &swapChainDesc, &swapChain_, &d3dDevice_);

		if (SUCCEEDED(result))
		{
			driverType_ = driverTypes[driver];
			break;
		}
	}

	if (FAILED(result))
		return false;

	ID3D10Texture2D *backBufferTexture;
	result = swapChain_->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&backBufferTexture);

	if (FAILED(result))
		return false;

	result = d3dDevice_->CreateRenderTargetView(backBufferTexture, 0, &backBufferTarget_);

	if (backBufferTexture)
		backBufferTexture->Release();

	if (FAILED(result))
		return false;

	D3D10_TEXTURE2D_DESC depthTexDesc;
	ZeroMemory(&depthTexDesc, sizeof(depthTexDesc));
	depthTexDesc.Width = w;
	depthTexDesc.Height = h;
	depthTexDesc.MipLevels = 1;
	depthTexDesc.ArraySize = 1;
	depthTexDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	depthTexDesc.SampleDesc.Count = 1;
	depthTexDesc.SampleDesc.Quality = 0;
	depthTexDesc.Usage = D3D10_USAGE_DEFAULT;
	depthTexDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;

	result = d3dDevice_->CreateTexture2D(&depthTexDesc, NULL, &depthTexture_);

	if (FAILED(result))
		return false;

	D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
	ZeroMemory(&descDSV, sizeof(descDSV));
	descDSV.Format = depthTexDesc.Format;
	descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
	descDSV.Texture2D.MipSlice = 0;

	result = d3dDevice_->CreateDepthStencilView(depthTexture_, &descDSV, &depthStencilView_);

	if (FAILED(result))
		return false;

	d3dDevice_->OMSetRenderTargets(1, &backBufferTarget_, depthStencilView_);

	D3D10_VIEWPORT viewport;
	viewport.Height = h;
	viewport.Width = w;
	viewport.MinDepth = 0.0f;
	viewport.MaxDepth = 1.0f;
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;

	d3dDevice_->RSSetViewports(1, &viewport);

	initialized_ = true;
	return true;
}
//----------------------------------------------------------------------------//
bool CEGuiD3D10BaseApplication::initialiseDirect3D(unsigned int width,
    unsigned int height, bool windowed)
{
    HRESULT res;

    // init sqap chain descriptor structure
    DXGI_SWAP_CHAIN_DESC scd;
    ZeroMemory(&scd, sizeof(scd));
    scd.BufferCount = 1;
    scd.BufferDesc.Width = width;
    scd.BufferDesc.Height = height;
    scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    scd.BufferDesc.RefreshRate.Numerator = 60;
    scd.BufferDesc.RefreshRate.Denominator = 1;
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    scd.OutputWindow = pimpl->d_window;
    scd.SampleDesc.Count = 1;
    scd.SampleDesc.Quality = 0;
    scd.Windowed = windowed;

    // initialise main parts of D3D
    res = D3D10CreateDeviceAndSwapChain(0, D3D10_DRIVER_TYPE_HARDWARE,
                                        0, 0, D3D10_SDK_VERSION,
                                        &scd, &pimpl->d_swapChain,
                                        &pimpl->d_device);
    if (SUCCEEDED(res))
    {

        // obtain handle to thr back buffer of the swap chain
        ID3D10Texture2D* back_buffer;
        res = pimpl->d_swapChain->GetBuffer(0, __uuidof(ID3D10Texture2D),
                                            (LPVOID*)&back_buffer);

        if (SUCCEEDED(res))
        {
            ID3D10RenderTargetView* rtview;

            // create render target view using the back buffer
            res = pimpl->d_device->
                CreateRenderTargetView(back_buffer, 0, &rtview);

            // release handle to buffer - we have done all we needed to with it.
            back_buffer->Release();

            if (SUCCEEDED(res))
            {
                // bind the back-buffer render target to get the output.
                pimpl->d_device->
                    OMSetRenderTargets(1, &rtview, 0);

                // set a basic viewport.
                D3D10_VIEWPORT view_port;
                view_port.Width    = width;
                view_port.Height   = height;
                view_port.MinDepth = 0.0f;
                view_port.MaxDepth = 1.0f;
                view_port.TopLeftX = 0;
                view_port.TopLeftY = 0;
                pimpl->d_device->RSSetViewports(1, &view_port);

                // complete window initialisation
                ShowWindow(pimpl->d_window, SW_NORMAL);
                UpdateWindow(pimpl->d_window);

                return true;
            }

            rtview->Release();
        }

        pimpl->d_swapChain->Release();
        pimpl->d_device->Release();
        pimpl->d_swapChain = 0;
        pimpl->d_device = 0;
    }

    MessageBox(0, "Failed to correctly initialise Direct3D 10",
               Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);

    return false;
}
Пример #16
0
bool CDX10Renderer::Initialise(HINSTANCE _hInstance, HWND _hwnd, int _iClientWidth, int _iClientHeight)
{
	m_hAppInst = _hInstance;
	m_hMainWnd = _hwnd;
	m_iTargetWidth = _iClientWidth;
	m_iTargetHeight = _iClientHeight;
	m_iClientWidth = _iClientWidth;
	m_iClientHeight = _iClientHeight;

	// Set up DX swap chain
	DXGI_SWAP_CHAIN_DESC sd;
	sd.BufferDesc.Width  = m_iTargetWidth;
	sd.BufferDesc.Height = m_iTargetHeight;
	sd.BufferDesc.RefreshRate.Numerator = 60;
	sd.BufferDesc.RefreshRate.Denominator = 1;
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

	sd.SampleDesc.Count   = 1;
	sd.SampleDesc.Quality = 0;

	sd.BufferUsage  = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.BufferCount  = 1;
	sd.OutputWindow = m_hMainWnd;
	sd.Windowed     = true;
	sd.SwapEffect   = DXGI_SWAP_EFFECT_DISCARD;
	sd.Flags        = 0;

	// Create DX10 device
	UINT createDeviceFlags = 0;

#if defined(DEBUG) || defined(_DEBUG)  
    createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

	// Create the ID3D10Device and IDXGISwapChain which interfaces
	// with the D3D10CreateDeviceAndSwapChain function
	if(FAILED(D3D10CreateDeviceAndSwapChain(0, //default adapter
											D3D10_DRIVER_TYPE_HARDWARE,
											0,  // no software device
											createDeviceFlags, 
											D3D10_SDK_VERSION,
											&sd,
											&m_pSwapChain,
											&m_pDevice)))
	{
		MessageBoxA(0, "Failed to create DX10 device.", "ERROR", MB_OK);
		return (false);
	}

	// Create raster states. And set an initial state
	D3D10_RASTERIZER_DESC rsDesc;
	ZeroMemory(&rsDesc, sizeof(D3D10_RASTERIZER_DESC));

	// Make backface cull solid and set this as initial state
	rsDesc.FillMode = D3D10_FILL_SOLID;
	rsDesc.CullMode = D3D10_CULL_BACK;
	m_pDevice->CreateRasterizerState(&rsDesc, &m_pDefaultRasterState);
	m_pDevice->RSSetState(m_pDefaultRasterState);

	Resize(m_iClientWidth, m_iClientHeight);

	// Load the effect
	if (FAILED(D3DX10CreateEffectFromFile(L"default.fx",
                                            0,
                                            0,
                                            "fx_4_0",
                                            D3D10_SHADER_ENABLE_STRICTNESS,
                                            0,
                                            m_pDevice,
                                            0, 0,
                                            &m_pDefaultEffect,
                                            0, 0)))
	{
		MessageBoxA(0, "Failed to create DX10 device.", "ERROR", MB_OK);
		return (false);
	}

 
	m_pDefaultTech = m_pDefaultEffect->GetTechniqueByName("DefaultTech");
 
	// Create matrix effect pointers
	m_pWorldMatrixEffectVariable = m_pDefaultEffect->GetVariableByName( "gWorld" )->AsMatrix();
	m_pViewMatrixEffectVariable = m_pDefaultEffect->GetVariableByName( "gView" )->AsMatrix();
	m_pProjectionMatrixEffectVariable = m_pDefaultEffect->GetVariableByName( "gProjection" )->AsMatrix();


	// Vert layout
	D3D10_INPUT_ELEMENT_DESC layout[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
		{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }
	};

	
	UINT numElements = 2;
	D3D10_PASS_DESC passDesc;
	m_pDefaultTech->GetPassByIndex(0)->GetDesc(&passDesc);
 
	if (FAILED(m_pDevice->CreateInputLayout(layout,
											numElements,
											passDesc.pIAInputSignature,
											passDesc.IAInputSignatureSize,
											&m_pDefaultVertexInputLayout ) ) )
	{
		MessageBoxA(0, "Failed to create DX10 device.", "ERROR", MB_OK);
		return (false);
	};
 
	// Set the input layout
	m_pDevice->IASetInputLayout(m_pDefaultVertexInputLayout);

	m_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

	// Set effect values now because they will not change
	D3DXMATRIX world;
	D3DXMatrixIdentity(&world);
	D3DXMatrixTranslation(&world, m_iClientWidth * -0.5f, m_iClientHeight * -0.5f, 0.0f);
	
	D3DXMATRIX rot;
	D3DXMatrixRotationX(&rot, static_cast<float>(D3DX_PI));

	world = world * rot;

		// Set up the view matrix
	//--------------------------------------------------------------
 
	D3DXVECTOR3 eye(0.0f, 0.0f, -1.0f);
	D3DXVECTOR3 view(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
 
	D3DXMATRIX viewMatrix;
	D3DXMatrixLookAtLH( &viewMatrix, &eye, &view, &up);
 
	//Set up projection matrix
	//--------------------------------------------------------------
	D3DXMATRIX projectionMatrix;
	D3DXMatrixOrthoLH(&projectionMatrix, static_cast<float>(m_iClientWidth), static_cast<float>(m_iClientHeight), 0.0f, 1.0f);
	//D3DXMatrixPerspectiveFovLH(&projectionMatrix, (float)D3DX_PI * 0.5f, (float)m_iClientWidth/m_iClientHeight, 0.1f, 100.0f);

	m_pWorldMatrixEffectVariable->SetMatrix(world);
	m_pViewMatrixEffectVariable->SetMatrix(viewMatrix);
	m_pProjectionMatrixEffectVariable->SetMatrix(projectionMatrix);

	D3D10_TECHNIQUE_DESC techDesc;
	m_pDefaultTech->GetDesc(&techDesc);

	for(unsigned int p = 0; p < techDesc.Passes; ++p )
	{
		m_pDefaultTech->GetPassByIndex(p)->Apply(0);
	}
	
	
	//create vertex buffer (space for 100 vertices)
	//---------------------------------------------
 
	UINT numVertices = 100;
 
	D3D10_BUFFER_DESC bd;
	bd.Usage = D3D10_USAGE_DYNAMIC;
	bd.ByteWidth = sizeof( TVertex ) * numVertices; //total size of buffer in bytes
	bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
	bd.MiscFlags = 0;
 
	if ( FAILED( m_pDevice->CreateBuffer( &bd, 0, &m_pVertexBuffer ) ) ) 
	{
		MessageBoxA(0, "Failed to create DX10 device.", "ERROR", MB_OK);
		return (false);
	}

	PushPenColour(TColour(0, 0, 0, 255).Value());
	PushBrushColour(TColour(0, 0, 0, 255).Value());

	// Init font
	D3DX10_FONT_DESC fontDesc;
	fontDesc.Height          = 16;
    fontDesc.Width           = 0;
    fontDesc.Weight          = 0;
    fontDesc.MipLevels       = 1;
    fontDesc.Italic          = false;
    fontDesc.CharSet         = DEFAULT_CHARSET;
    fontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
    fontDesc.Quality         = DEFAULT_QUALITY;
    fontDesc.PitchAndFamily  = DEFAULT_PITCH | FF_DONTCARE;
    wcscpy_s(fontDesc.FaceName, L"Times New Roman");

	D3DX10CreateFontIndirect(m_pDevice, &fontDesc, &m_pFont);

		D3DX10CreateSprite(m_pDevice, 512, &m_pSprite);

	return(true);
}
Пример #17
0
// Create Direct3D device and swap chain
HRESULT DxWidget::InitDevice()
{
	HRESULT hrResult = E_FAIL;
	HRESULT hrRetCode = E_FAIL;

	m_driverType = D3D10_DRIVER_TYPE_NULL;

    UINT createDeviceFlags = 0;

    D3D10_DRIVER_TYPE driverTypes[] =
    {
        D3D10_DRIVER_TYPE_HARDWARE,
        D3D10_DRIVER_TYPE_REFERENCE,
    };
    UINT numDriverTypes = sizeof(driverTypes) / sizeof(driverTypes[0]);

    ZeroMemory(&m_swapChainDesc, sizeof(m_swapChainDesc));
    m_swapChainDesc.BufferCount = 1;
	m_swapChainDesc.BufferDesc.Width = width();
    m_swapChainDesc.BufferDesc.Height = height();
    m_swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    m_swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
    m_swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    m_swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    m_swapChainDesc.OutputWindow = (HWND)winId();
    m_swapChainDesc.SampleDesc.Count = 1;
    m_swapChainDesc.SampleDesc.Quality = 0;
    m_swapChainDesc.Windowed = TRUE;
	
    for(UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
    {
        m_driverType = driverTypes[driverTypeIndex];
        hrRetCode = D3D10CreateDeviceAndSwapChain(NULL, m_driverType, NULL, createDeviceFlags,
			D3D10_SDK_VERSION, &m_swapChainDesc, &m_pSwapChain, &m_pd3dDevice);

        if(SUCCEEDED(hrRetCode))
            break;
    }
	KE_COM_PROCESS_ERROR(hrRetCode);
	KE_PROCESS_ERROR(m_pSwapChain);
	KE_PROCESS_ERROR(m_pd3dDevice);

    // Create a render target view
    ID3D10Texture2D* pBackBuffer;
    hrRetCode = m_pSwapChain->GetBuffer(0, __uuidof( ID3D10Texture2D ), ( LPVOID* )&pBackBuffer);
	KE_COM_PROCESS_ERROR(hrRetCode);
	KE_PROCESS_ERROR(pBackBuffer);

    hrRetCode = m_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &m_pRenderTargetView);
    pBackBuffer->Release();
	KE_COM_PROCESS_ERROR(hrRetCode);
	KE_PROCESS_ERROR(m_pRenderTargetView);
	
    m_pd3dDevice->OMSetRenderTargets(1, &m_pRenderTargetView, NULL);

    // Setup the viewport
    m_viewPort.Width = width();
    m_viewPort.Height = height();
    m_viewPort.MinDepth = 0.0f;
    m_viewPort.MaxDepth = 1.0f;
    m_viewPort.TopLeftX = 0;
    m_viewPort.TopLeftY = 0;
    m_pd3dDevice->RSSetViewports(1, &m_viewPort);

	 // Create the effect
    DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;

    hrRetCode = D3DX10CreateEffectFromFile(L"testqt.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0,
		m_pd3dDevice, NULL, NULL, &m_pEffect, NULL, NULL );
	KE_COM_PROCESS_ERROR(hrRetCode);
	KE_PROCESS_ERROR(m_pEffect);

    // Obtain the technique
    m_pTechnique = m_pEffect->GetTechniqueByName("Render");

    // Define the input layout
    D3D10_INPUT_ELEMENT_DESC layout[] =
    {
        {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
		{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
    };
    UINT numElements = sizeof(layout) / sizeof(layout[0]);

    // Create the input layout
    D3D10_PASS_DESC PassDesc;
    m_pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc);
    hrRetCode = m_pd3dDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature,
		PassDesc.IAInputSignatureSize, &m_pVertexLayout);
    KE_COM_PROCESS_ERROR(hrRetCode);
	KE_PROCESS_ERROR(m_pVertexLayout);

    // Set the input layout
    m_pd3dDevice->IASetInputLayout(m_pVertexLayout);
	// Set primitive topology
    m_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINELIST);

	hrResult = S_OK;
Exit0:
	return hrResult;
}
Пример #18
0
bool ds::Render::Create( HWND Handle, const ds::String& BasicEffect )
{
	g_SwapChain.OutputWindow = Handle;

	RECT Rect;
	GetClientRect(  Handle, &Rect  );
	g_SwapChain.BufferDesc.Width = (  unsigned  ) Rect.right - (  unsigned  ) Rect.left;
	g_SwapChain.BufferDesc.Height = (  unsigned  ) Rect.bottom - (  unsigned  ) Rect.top;

	for( unsigned i = 0; i < 2; i++ )
	{
		if( SUCCEEDED( D3D10CreateDeviceAndSwapChain( 
			nullptr,
			( D3D10_DRIVER_TYPE )i, 
			nullptr,
			0,
			D3D10_SDK_VERSION,
			&g_SwapChain,
			&Swap,
			&Device
			 ) ) )
		{
			if( ( D3D10_DRIVER_TYPE )i == D3D10_DRIVER_TYPE_REFERENCE )
				dsPushMessage( ErrRenderCreateDevice );
			break;
		}
	}

	if( !Device )
	{
		dsPushErrorStatic( ErrRenderCreateDevice );
		return false;
	}

	IDXGIOutput* l_Info;
	unsigned l_ModesCount = 0;
	Swap->GetContainingOutput( &l_Info );
	l_Info->GetDisplayModeList( g_SwapChain.BufferDesc.Format,
		0, &l_ModesCount, nullptr );

	DXGI_MODE_DESC* l_Modes = new DXGI_MODE_DESC[l_ModesCount];
	l_Info->GetDisplayModeList( g_SwapChain.BufferDesc.Format,
		0, &l_ModesCount, l_Modes );	

	for( unsigned i = 0; i < l_ModesCount; i++ )
		VideoModes.push_back( VideoMode( ds::Size( l_Modes[i].Width, l_Modes[i].Height ), l_Modes[i].RefreshRate.Numerator / l_Modes[i].RefreshRate.Denominator ) );

	delete[] l_Modes;
	l_Info->Release( );

	ID3D10Texture2D* l_BackBuffer = nullptr;
	if( FAILED( Swap->GetBuffer( 0, __uuidof( ID3D10Texture2D ), ( LPVOID* )&l_BackBuffer ) ) )
		return false;

	if( FAILED( Device->CreateRenderTargetView( l_BackBuffer, nullptr, &RenderTargetView ) ) )
		return false;

	l_BackBuffer->Release( );

	CreateDepthBuffer( ds::Size( 
		g_SwapChain.BufferDesc.Width,
		g_SwapChain.BufferDesc.Height
		 ) );

	Device->OMSetRenderTargets( 1, &RenderTargetView, DepthBufferEnable ? DepthStencilView : nullptr );
	Device->RSSetViewports( 1, &g_ViewPort );

	SetRenderRect( v4<unsigned>( 0, 0, 
		g_SwapChain.BufferDesc.Width,
		g_SwapChain.BufferDesc.Height ) );

	if( !BasicEffect.empty( ) )
		Effect = new ds::BasicEffect( BasicEffect, "Render" );
	else
		Effect = nullptr;

	SetRenderMode( 1 );

	SetView( v2<unsigned>( 
		g_SwapChain.BufferDesc.Width, g_SwapChain.BufferDesc.Height ), 
		v2<unsigned>( g_ViewPort.TopLeftX, g_ViewPort.TopLeftY ),
		g_ViewPort.MinDepth, g_ViewPort.MaxDepth );
	
	ds::Clock::Reset( );

	return true;
}
HRESULT InitD3D( HWND hWnd )
{
	// Setup a DXGI swap chain descriptor
	DXGI_SWAP_CHAIN_DESC sd;
	ZeroMemory( &sd, sizeof( sd ) );

	sd.BufferCount = 1; // number of buffer
	sd.BufferDesc.Width = 600; // buffer width, can we set it to the screen width?
	sd.BufferDesc.Height = 600; // buffer height, can we set it to the screen height?
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // buffer format, 32 bit color with alpha(RGBA)
	sd.BufferDesc.RefreshRate.Numerator = 60; // refresh rate?
	sd.BufferDesc.RefreshRate.Denominator = 1; // WHAT'S THIS?
	sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // use buffer as render target
	sd.OutputWindow = hWnd; // output window handle
	sd.SampleDesc.Count = 1; // WHAT'S THIS?
	sd.SampleDesc.Quality = 0; // WHAT'S THIS?
	sd.Windowed = TRUE; // full-screen mode

	HRESULT hr;
	UINT flags = D3D10_CREATE_DEVICE_BGRA_SUPPORT;

#if defined( DEBUG ) || defined( _DEBUG )
	flags |= D3D10_CREATE_DEVICE_DEBUG;
#endif 

	// Create device and swap chain
	if (FAILED (hr = D3D10CreateDeviceAndSwapChain( NULL, 
	    D3D10_DRIVER_TYPE_HARDWARE,
		NULL,
		flags,
		D3D10_SDK_VERSION,
		&sd, 
		&g_pSwapChain,
		&g_pd3dDevice)))
	{
		return hr;
	}

	// Create render target and bind the back-buffer
	ID3D10Texture2D* pBackBuffer;

	// Get a pointer to the back buffer
	hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID* )&pBackBuffer);
	if (FAILED(hr))
		return hr;

	// Create a render-target view
	g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);

	// Bind the view
	g_pd3dDevice->OMSetRenderTargets(1, &g_pRenderTargetView, NULL); // WHAT'S OM here mean?

	// Setup the viewport
	D3D10_VIEWPORT vp;
	vp.Width = 640; // this should be similar with the back-buffer width, global it!
	vp.Height = 480;
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;
	g_pd3dDevice->RSSetViewports(1, &vp);

	InitVertexBuffer();
	InitEffects();
		                       
	return S_OK;
}
Пример #20
0
bool DX10App::Init(char* title, int width, int height)
{
	hInstance = GetModuleHandle(0);

	InitVertexStructures();

	mWidth	= width;
	mHeight = height;
	// To create a window we need to describe some of
	// it's characteristics by filling out a WNDCLASS structure
	WNDCLASS wc;

	wc.style			= CS_HREDRAW | CS_VREDRAW;		// Repaints window when resized
	wc.lpfnWndProc		= WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;					// Main application instance
	wc.hIcon			= LoadIcon(0, IDI_APPLICATION);
	wc.hCursor			= LoadCursor(0, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName		= 0;
	wc.lpszClassName	= "Albanien";
	
	// Registers WNDCLASS with windows so that we can create a window based on it
	if(!RegisterClass(&wc))
	{
		MessageBox(0, "RegisterClass failed.", 0, 0);
		return false;
	}

	// Grab all window border values to adjust the window to the size we want
	RECT rect;
	rect.top	= (long)0;
	rect.left	= (long)(0);
	rect.right	= (long)width;
	rect.bottom	= (long)height;

	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);

	hMainWnd = CreateWindow(
				"Albanien",				// Registered WNDCLASS instance to use
				title,					// Window title
				WS_OVERLAPPEDWINDOW,	// Style flags
				200/*CW_USEDEFAULT*/,			// x-coordinate
				200/*CW_USEDEFAULT*/,			// y-coordinate
				rect.right - rect.left,	// Width (adjusted width)
				rect.bottom - rect.top,	// Height (adjusted height)
				0,						// Parent window
				0,						// Menu handle
				hInstance,				// App instance
				0						//
			);

	UINT createDeviceFlags  = 0;
		 createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG; // If debug mode

	D3D10_DRIVER_TYPE driverTypes[] = 
	{
		D3D10_DRIVER_TYPE_HARDWARE,
		D3D10_DRIVER_TYPE_REFERENCE
	};

	UINT numDriverTypes = sizeof(driverTypes) / sizeof(driverTypes[0]);

	DXGI_SWAP_CHAIN_DESC sd;
	ZeroMemory(&sd, sizeof(sd));
	sd.BufferCount							= 1;
	sd.BufferDesc.Width						= width;
	sd.BufferDesc.Height					= height;
	sd.BufferDesc.Format					= DXGI_FORMAT_R8G8B8A8_UNORM;
	sd.BufferDesc.RefreshRate.Numerator		= 60;
	sd.BufferDesc.RefreshRate.Denominator	= 1;
	sd.BufferUsage							= DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.OutputWindow							= hMainWnd;
	sd.SampleDesc.Count						= 1;
	sd.SampleDesc.Quality					= 0;
	sd.Windowed								= TRUE;

	D3D10_DRIVER_TYPE driverType;
	HRESULT hr;
	// i == driverTypeIndex
	for(UINT i = 0; i < numDriverTypes; i++)
	{
		driverType	= driverTypes[i];
		hr			= D3D10CreateDeviceAndSwapChain(
													NULL,
													driverType,
													NULL,
													createDeviceFlags,
													D3D10_SDK_VERSION,
													&sd,
													&mSwapChain,
													&gDevice
													);

		// If first driver type succeeded, break, else try the other one
		if(SUCCEEDED(hr))
			break;
	}
	printf("Device and swap chain created.\n");
	
	mSwapBuffers->setDevice(gDevice);

	//create rastersizer desc
	D3D10_RASTERIZER_DESC rasterDesc;
	rasterDesc.FillMode					= D3D10_FILL_SOLID;
	rasterDesc.CullMode					= D3D10_CULL_BACK;
    rasterDesc.FrontCounterClockwise	= false;
    rasterDesc.DepthBias				= false;
    rasterDesc.DepthBiasClamp			= 0;
    rasterDesc.SlopeScaledDepthBias		= 0;
    rasterDesc.DepthClipEnable			= true;
    rasterDesc.ScissorEnable			= false;
    rasterDesc.MultisampleEnable		= true;
    rasterDesc.AntialiasedLineEnable	= true;
	gDevice->CreateRasterizerState( &rasterDesc, &mSolidMode );

	rasterDesc.FillMode					= D3D10_FILL_WIREFRAME;
	rasterDesc.CullMode					= D3D10_CULL_BACK;
    rasterDesc.FrontCounterClockwise	= false;
    rasterDesc.DepthBias				= false;
    rasterDesc.DepthBiasClamp			= 0;
    rasterDesc.SlopeScaledDepthBias		= 0;
    rasterDesc.DepthClipEnable			= false;
    rasterDesc.ScissorEnable			= false;
    rasterDesc.MultisampleEnable		= false;
    rasterDesc.AntialiasedLineEnable	= false;
	
	gDevice->CreateRasterizerState( &rasterDesc, &mWireframeMode );
	gDevice->RSSetState(mSolidMode);

	mSwapBuffers->createBackBuffer(mSwapChain);
	printf("Render target view created.\n");

	mSwapBuffers->createDepthTexture();

	// Viewport settings
	D3D10_VIEWPORT vp;
	vp.Width	= width;
	vp.Height	= height;
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;
	mSwapBuffers->setViewPort(&vp);

	printf("DX10 initiated.\n\n");
	ShowWindow(hMainWnd, 1);

	return true;
}
bool C_Graphics::initDirect3D(const C_Window& window) {
	// Fill out a DXGI_SWAP_CHAIN_DESC to describe our swap chain.
	DXGI_SWAP_CHAIN_DESC sd;
	ZeroMemory( &sd, sizeof( sd ) );
	sd.BufferDesc.Width  = window.getWidth();
	sd.BufferDesc.Height = window.getHeight();
	sd.BufferDesc.RefreshRate.Numerator = 60;
	sd.BufferDesc.RefreshRate.Denominator = 1;
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

	// No multisampling
	sd.SampleDesc.Count   = 1;
	sd.SampleDesc.Quality = 0;
	sd.BufferUsage  = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.BufferCount  = 1;
	sd.OutputWindow = window.getWindowHandle();
	sd.Windowed     = true;
	sd.SwapEffect   = DXGI_SWAP_EFFECT_DISCARD;
	sd.Flags        = 0;


	// Create the device
	UINT createDeviceFlags = 0;
	//#if defined(DEBUG) || defined(_DEBUG)  
	//	createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
	//#endif
	HRESULT hr =
	D3D10CreateDeviceAndSwapChain(
			0,                 //default adapter
			d_D3DDriverType,
			0,                 // no software device
			createDeviceFlags, 
			D3D10_SDK_VERSION,
			&sd,
			&d_SwapChain,
			&d_D3DDevice);

	if(FAILED(hr)) {
		DEBUGLOG(L"D3DDevice INIT FAILED");
		DXTrace(__FILE__, (DWORD)__LINE__, hr, 0, true);
		return false;
	}

	DEBUGLOG(L"D3DDevice INIT SUCCESSFUL");

	// The remaining steps that need to be carried out for D3D creation
	// also need to be executed every time the window is resized.  So
	// just call the OnResize method here to avoid code duplication.
	onResize(window);

	D3DX10_FONT_DESC fontDesc;
	fontDesc.Height          = 14;
    fontDesc.Width           = 0;
    fontDesc.Weight          = 0;
    fontDesc.MipLevels       = 1;
    fontDesc.Italic          = false;
    fontDesc.CharSet         = DEFAULT_CHARSET;
    fontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
    fontDesc.Quality         = DEFAULT_QUALITY;
    fontDesc.PitchAndFamily  = DEFAULT_PITCH | FF_DONTCARE;
    wcscpy_s(fontDesc.FaceName, L"Times New Roman");

	hr = D3DX10CreateFontIndirect(d_D3DDevice, &fontDesc, &d_Font);

	if(FAILED(hr)) {
		DEBUGLOG(L"Font INIT FAILED");
		return false;
	}

	DEBUGLOG(L"Font INIT SUCCESSFUL");

	// Init the defualt shader
	//d_pDefaultShader = new C_DefaultShader(d_D3DDevice);
	return true;
}
Пример #22
0
bool GSDevice10::Create(HWND hWnd, bool vsync)
{
	if(!__super::Create(hWnd, vsync))
	{
		return false;
	}

	HRESULT hr;

	DXGI_SWAP_CHAIN_DESC scd;
	D3D10_BUFFER_DESC bd;
	D3D10_SAMPLER_DESC sd;
	D3D10_DEPTH_STENCIL_DESC dsd;
    D3D10_RASTERIZER_DESC rd;
	D3D10_BLEND_DESC bsd;

	memset(&scd, 0, sizeof(scd));

	scd.BufferCount = 2;
	scd.BufferDesc.Width = 1;
	scd.BufferDesc.Height = 1;
	scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	//scd.BufferDesc.RefreshRate.Numerator = 60;
	//scd.BufferDesc.RefreshRate.Denominator = 1;
	scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	scd.OutputWindow = hWnd;
	scd.SampleDesc.Count = 1;
	scd.SampleDesc.Quality = 0;
	scd.Windowed = TRUE;

	UINT flags = 0;

#ifdef DEBUG
	flags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

	hr = D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, flags, D3D10_SDK_VERSION, &scd, &m_swapchain, &m_dev);

	if(FAILED(hr)) return false;

	// font
/*
	// TODO: the driver crashes on alt-enter when using a font...

	D3DX10_FONT_DESC fd;
	memset(&fd, 0, sizeof(fd));
	_tcscpy(fd.FaceName, _T("Arial"));
	fd.Height = 20;
	D3DX10CreateFontIndirect(m_dev, &fd, &m_font);
*/
	// convert

	D3D10_INPUT_ELEMENT_DESC il_convert[] =
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
		{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 16, D3D10_INPUT_PER_VERTEX_DATA, 0},
	};

	hr = CompileShader(IDR_CONVERT10_FX, "vs_main", NULL, &m_convert.vs, il_convert, countof(il_convert), &m_convert.il);

	for(int i = 0; i < countof(m_convert.ps); i++)
	{
		CStringA main;
		main.Format("ps_main%d", i);
		hr = CompileShader(IDR_CONVERT10_FX, main, NULL, &m_convert.ps[i]);
	}

	memset(&bd, 0, sizeof(bd));

	bd.Usage = D3D10_USAGE_DEFAULT;
	bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;
	bd.ByteWidth = 4 * sizeof(GSVertexPT1);

	hr = m_dev->CreateBuffer(&bd, NULL, &m_convert.vb);

	memset(&dsd, 0, sizeof(dsd));

	dsd.DepthEnable = false;
	dsd.StencilEnable = false;

	hr = m_dev->CreateDepthStencilState(&dsd, &m_convert.dss);

	memset(&bsd, 0, sizeof(bsd));

	bsd.BlendEnable[0] = false;
	bsd.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;

	hr = m_dev->CreateBlendState(&bsd, &m_convert.bs);

	// merge

	memset(&bd, 0, sizeof(bd));

    bd.ByteWidth = sizeof(MergeConstantBuffer);
    bd.Usage = D3D10_USAGE_DEFAULT;
    bd.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = 0;
    bd.MiscFlags = 0;

    hr = m_dev->CreateBuffer(&bd, NULL, &m_merge.cb);

	for(int i = 0; i < countof(m_merge.ps); i++)
	{
		CStringA main;
		main.Format("ps_main%d", i);
		hr = CompileShader(IDR_MERGE10_FX, main, NULL, &m_merge.ps[i]);
	}

	memset(&bsd, 0, sizeof(bsd));

	bsd.BlendEnable[0] = true;
	bsd.BlendOp = D3D10_BLEND_OP_ADD;
	bsd.SrcBlend = D3D10_BLEND_SRC_ALPHA;
	bsd.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
	bsd.BlendOpAlpha = D3D10_BLEND_OP_ADD;
	bsd.SrcBlendAlpha = D3D10_BLEND_ONE;
	bsd.DestBlendAlpha = D3D10_BLEND_ZERO;
	bsd.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;

	hr = m_dev->CreateBlendState(&bsd, &m_merge.bs);

	// interlace

	memset(&bd, 0, sizeof(bd));

    bd.ByteWidth = sizeof(InterlaceConstantBuffer);
    bd.Usage = D3D10_USAGE_DEFAULT;
    bd.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = 0;
    bd.MiscFlags = 0;

    hr = m_dev->CreateBuffer(&bd, NULL, &m_interlace.cb);

	for(int i = 0; i < countof(m_interlace.ps); i++)
	{
		CStringA main;
		main.Format("ps_main%d", i);
		hr = CompileShader(IDR_INTERLACE10_FX, main, NULL, &m_interlace.ps[i]);
	}

	//

	memset(&rd, 0, sizeof(rd));

	rd.FillMode = D3D10_FILL_SOLID;
	rd.CullMode = D3D10_CULL_NONE;
	rd.FrontCounterClockwise = false;
	rd.DepthBias = false;
	rd.DepthBiasClamp = 0;
	rd.SlopeScaledDepthBias = 0;
	rd.DepthClipEnable = false; // ???
	rd.ScissorEnable = true;
	rd.MultisampleEnable = false;
	rd.AntialiasedLineEnable = false;

	hr = m_dev->CreateRasterizerState(&rd, &m_rs);

	m_dev->RSSetState(m_rs);

	//

	memset(&sd, 0, sizeof(sd));

	sd.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
	sd.AddressU = D3D10_TEXTURE_ADDRESS_CLAMP;
	sd.AddressV = D3D10_TEXTURE_ADDRESS_CLAMP;
	sd.AddressW = D3D10_TEXTURE_ADDRESS_CLAMP;
	sd.MaxLOD = FLT_MAX;
	sd.MaxAnisotropy = 16; 
	sd.ComparisonFunc = D3D10_COMPARISON_NEVER;

	hr = m_dev->CreateSamplerState(&sd, &m_convert.ln);

	sd.Filter = D3D10_FILTER_MIN_MAG_MIP_POINT;

	hr = m_dev->CreateSamplerState(&sd, &m_convert.pt);

	//

	Reset(1, 1, true);

	//
/*
	if(!m_mergefx.Create(this))
	{
		return false;
	}
*/
	//

	return true;
}
Пример #23
0
bool Device_D3D10::initD3D( void )
{
	HRESULT hr = S_OK;

	// `获得窗口大小`
	RECT rc;
	GetClientRect( m_hWnd, &rc );
	UINT width = rc.right - rc.left;
	UINT height = rc.bottom - rc.top;

	UINT createDeviceFlags = 0;
#ifdef _DEBUG
	createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

	// `设置Frontbuffer/Backbuffer大小及格式`
	DXGI_SWAP_CHAIN_DESC sd;
	ZeroMemory( &sd, sizeof(sd) );
	sd.BufferCount = 1;
	sd.BufferDesc.Width = width;
	sd.BufferDesc.Height = height;
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
	sd.BufferDesc.RefreshRate.Numerator = 60;
	sd.BufferDesc.RefreshRate.Denominator = 1;
	sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.OutputWindow = m_hWnd;
	sd.SampleDesc.Count = 1;
	sd.SampleDesc.Quality = 0;
	//sd.SwapEffect = DXGI_SWAP_EFFECT_SEQUENTIAL;
	//sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
	sd.Windowed = TRUE;

	// `打开Frontbuffer/Backbuffer`
	hr = D3D10CreateDeviceAndSwapChain( NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, D3D10_SDK_VERSION, &sd, &m_pSwapChain, &m_pd3dDevice );
	if( FAILED(hr) ) 
		return false;

	// `取出第一个display buffer`
	ID3D10Texture2D *pBuffer = NULL;
	hr = m_pSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), (LPVOID*)&pBuffer );
	if( FAILED(hr) ) 
		return false;
	// `打开一个合乎display buffer格式的RenderTargetView`
	hr = m_pd3dDevice->CreateRenderTargetView( pBuffer, NULL, &m_pRenderTargetView );

	pBuffer->Release();
	if( FAILED( hr ) ) 
		return false;

	// `设置Depth/Stencil buffer的数据格式`
	D3D10_TEXTURE2D_DESC descDepth;
	descDepth.Width = width;
	descDepth.Height = height;
	descDepth.MipLevels = 1;
	descDepth.ArraySize = 1;
	descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	descDepth.SampleDesc.Count = 1;
	descDepth.SampleDesc.Quality = 0;
	descDepth.Usage = D3D10_USAGE_DEFAULT;
	descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
	descDepth.CPUAccessFlags = 0;
	descDepth.MiscFlags = 0;
	// `分配Depth/Stencil buffer的空间`
	hr = m_pd3dDevice->CreateTexture2D( &descDepth, NULL, &m_pDepthStencil );
	if( FAILED(hr) )
		return false;

	// `设置Depth/Stencil View的格式`
	D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
	descDSV.Format = descDepth.Format;
	descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
	descDSV.Texture2D.MipSlice = 0;
	// `打开DepthStencil Buffer的View`
	hr = m_pd3dDevice->CreateDepthStencilView( m_pDepthStencil, &descDSV, &m_pDepthStencilView );
	if( FAILED(hr) )
		return false;

	setDefaultState();

	// `设置绘图的结果会存放在什么地方`
	// RenderTargetView = RGBA Color Buffer resource
	// DepthStencilView = Depth/Stencil Buffer resource
	m_pd3dDevice->OMSetRenderTargets(1, &m_pRenderTargetView, m_pDepthStencilView);

	setDefaultState();

	// `Viewport用来指定3D绘图的画布范围, 在此把Viewport设置成和整个窗口一样大`
	D3D10_VIEWPORT vp;

	vp.Width = width;
	vp.Height = height;
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;

	m_pd3dDevice->RSSetViewports( 1, &vp );

	return true;
}
Пример #24
0
bool DX10Renderer::Initialize(const HWND *pHWND)
{
    //get window dimensions
    RECT rc;
    GetClientRect( *pHWND, &rc );
    UINT width = rc.right - rc.left;
    UINT height = rc.bottom - rc.top;

    /*******************************************************************
    * Create Swap Chain and D3D device
    ********************************************************************/
    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));

    //set buffer dimensions and format
    swapChainDesc.BufferCount = 2;
    swapChainDesc.BufferDesc.Width = width;
    swapChainDesc.BufferDesc.Height = height;
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
    swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    swapChainDesc.SampleDesc.Quality = 0;
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.OutputWindow = *pHWND;
    swapChainDesc.Windowed = true;


    //Create the D3D device
    //--------------------------------------------------------------
    // put the d3d device into debug mode under debug profile
    //--------------------------------------------------------------
    UINT flags = 0;
#if defined(_DEBUG) | defined(DEBUG)
    flags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

    if ( FAILED( D3D10CreateDeviceAndSwapChain(		NULL,
                 D3D10_DRIVER_TYPE_HARDWARE,
                 NULL,
                 flags,
                 D3D10_SDK_VERSION,
                 &swapChainDesc,
                 &pSwapChain,
                 &pD3DDevice ) ) ) return false;

    /*******************************************************************
    * Create Rendering Targets and Depth Stencil
    ********************************************************************/
    //try to get the back buffer
    ID3D10Texture2D* pBackBuffer;
    if ( FAILED( pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &pBackBuffer) ) ) return false;

    //try to create render target view
    if ( FAILED( pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView) ) ) return false;
    pBackBuffer->Release();

    //create depth stencil texture desc
    D3D10_TEXTURE2D_DESC texDesc;
    texDesc.Width = width;
    texDesc.Height = height;
    texDesc.MipLevels = 1;
    texDesc.ArraySize = 1;
    texDesc.Format = DXGI_FORMAT_D32_FLOAT;
    texDesc.SampleDesc.Count = 1;
    texDesc.SampleDesc.Quality = 0;
    texDesc.Usage = D3D10_USAGE_DEFAULT;
    texDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;
    texDesc.CPUAccessFlags = 0;
    texDesc.MiscFlags = 0;

    // Create the depth stencil view desc
    D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
    descDSV.Format = texDesc.Format;
    descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
    descDSV.Texture2D.MipSlice = 0;

    if( FAILED( pD3DDevice->CreateTexture2D( &texDesc, NULL, &pDepthStencil ) ) )  return false;
    if( FAILED( pD3DDevice->CreateDepthStencilView( pDepthStencil, &descDSV, &pDepthStencilView ) ) ) return false;

    /*******************************************************************
    * Create Shadow Map Texture
    ********************************************************************/

    texDesc.Width = width;
    texDesc.Height = height;
    texDesc.Format = DXGI_FORMAT_R32_TYPELESS;
    texDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL | D3D10_BIND_SHADER_RESOURCE;

    D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
    srvDesc.Format = DXGI_FORMAT_R32_FLOAT;
    srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MipLevels = texDesc.MipLevels;
    srvDesc.Texture2D.MostDetailedMip = 0;

    if( FAILED( pD3DDevice->CreateTexture2D( &texDesc, NULL, &pShadowMap ) ) )  return false;
    if( FAILED( pD3DDevice->CreateDepthStencilView( pShadowMap, &descDSV, &pShadowMapDepthView ) ) ) return false;
    if( FAILED( pD3DDevice->CreateShaderResourceView( pShadowMap, &srvDesc, &pShadowMapSRView) ) ) return false;

    /*******************************************************************
    * Set up Viewports
    ********************************************************************/
    viewport.Width = width;
    viewport.Height = height;
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;
    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;

    shadowMapViewport.Width = width;
    shadowMapViewport.Height = height;
    shadowMapViewport.MinDepth = 0.0f;
    shadowMapViewport.MaxDepth = 1.0f;
    shadowMapViewport.TopLeftX = 0;
    shadowMapViewport.TopLeftY = 0;

    return true;
}
Пример #25
0
bool D3DClass::Initialize(int screenWidth, int screenHeight, bool vsync, HWND hwnd, bool fullscreen,
                          float screenDepth, float screenNear)
{
    HRESULT result;
    IDXGIFactory* factory;
    IDXGIAdapter* adapter;
    IDXGIOutput* adapterOutput;
    unsigned int numModes, i, numerator, denominator, stringLength;
    DXGI_MODE_DESC* displayModeList;
    DXGI_ADAPTER_DESC adapterDesc;
    int error;
    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    ID3D10Texture2D* backBufferPtr;
    D3D10_TEXTURE2D_DESC depthBufferDesc;
    D3D10_DEPTH_STENCIL_DESC depthStencilDesc;
    D3D10_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
    D3D10_VIEWPORT viewport;
    float fieldOfView, screenAspect;
    D3D10_RASTERIZER_DESC rasterDesc;


    // Store the vsync setting.
    m_vsync_enabled = vsync;

    // Create a DirectX graphics interface factory.
    result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
    if(FAILED(result))
    {
        return false;
    }

    // Use the factory to create an adapter for the primary graphics interface (video card).
    result = factory->EnumAdapters(0, &adapter);
    if(FAILED(result))
    {
        return false;
    }

    // Enumerate the primary adapter output (monitor).
    result = adapter->EnumOutputs(0, &adapterOutput);
    if(FAILED(result))
    {
        return false;
    }

    // Get the number of modes that fit the DXGI_FORMAT_R8G8B8A8_UNORM display format for the adapter output (monitor).
    result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, NULL);
    if(FAILED(result))
    {
        return false;
    }

    // Create a list to hold all the possible display modes for this monitor/video card combination.
    displayModeList = new DXGI_MODE_DESC[numModes];
    if(!displayModeList)
    {
        return false;
    }

    // Now fill the display mode list structures.
    result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, displayModeList);
    if(FAILED(result))
    {
        return false;
    }

    // Now go through all the display modes and find the one that matches the screen width and height.
    // When a match is found store the numerator and denominator of the refresh rate for that monitor.
    for(i=0; i<numModes; i++)
    {
        if(displayModeList[i].Width == (unsigned int)screenWidth)
        {
            if(displayModeList[i].Height == (unsigned int)screenHeight)
            {
                numerator = displayModeList[i].RefreshRate.Numerator;
                denominator = displayModeList[i].RefreshRate.Denominator;
            }
        }
    }

    // Get the adapter (video card) description.
    result = adapter->GetDesc(&adapterDesc);
    if(FAILED(result))
    {
        return false;
    }

    // Store the dedicated video card memory in megabytes.
    m_videoCardMemory = (int)(adapterDesc.DedicatedVideoMemory / 1024 / 1024);

    // Convert the name of the video card to a character array and store it.
    error = wcstombs_s(&stringLength, m_videoCardDescription, 128, adapterDesc.Description, 128);
    if(error != 0)
    {
        return false;
    }

    // Release the display mode list.
    delete [] displayModeList;
    displayModeList = 0;

    // Release the adapter output.
    adapterOutput->Release();
    adapterOutput = 0;

    // Release the adapter.
    adapter->Release();
    adapter = 0;

    // Release the factory.
    factory->Release();
    factory = 0;

    // Initialize the swap chain description.
    ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));

    // Set to a single back buffer.
    swapChainDesc.BufferCount = 1;

    // Set the width and height of the back buffer.
    swapChainDesc.BufferDesc.Width = screenWidth;
    swapChainDesc.BufferDesc.Height = screenHeight;

    // Set regular 32-bit surface for the back buffer.
    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

    // Set the refresh rate of the back buffer.
    if(m_vsync_enabled)
    {
        swapChainDesc.BufferDesc.RefreshRate.Numerator = numerator;
        swapChainDesc.BufferDesc.RefreshRate.Denominator = denominator;
    }
    else
    {
        swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
        swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    }

    // Set the usage of the back buffer.
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;

    // Set the handle for the window to render to.
    swapChainDesc.OutputWindow = hwnd;

    // Turn multisampling off.
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.SampleDesc.Quality = 0;

    // Set to full screen or windowed mode.
    if(fullscreen)
    {
        swapChainDesc.Windowed = false;
    }
    else
    {
        swapChainDesc.Windowed = true;
    }

    // Set the scan line ordering and scaling to unspecified.
    swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

    // Discard the back buffer contents after presenting.
    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

    // Don't set the advanced flags.
    swapChainDesc.Flags = 0;

    // Create the swap chain and the Direct3D device.
    result = D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0, D3D10_SDK_VERSION,
                                           &swapChainDesc, &m_swapChain, &m_device);
    if(FAILED(result))
    {
        return false;
    }

    // Get the pointer to the back buffer.
    result = m_swapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&backBufferPtr);
    if(FAILED(result))
    {
        return false;
    }

    // Create the render target view with the back buffer pointer.
    result = m_device->CreateRenderTargetView(backBufferPtr, NULL, &m_renderTargetView);
    if(FAILED(result))
    {
        return false;
    }

    // Release pointer to the back buffer as we no longer need it.
    backBufferPtr->Release();
    backBufferPtr = 0;

    // Initialize the description of the depth buffer.
    ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));

    // Set up the description of the depth buffer.
    depthBufferDesc.Width = screenWidth;
    depthBufferDesc.Height = screenHeight;
    depthBufferDesc.MipLevels = 1;
    depthBufferDesc.ArraySize = 1;
    depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    depthBufferDesc.SampleDesc.Count = 1;
    depthBufferDesc.SampleDesc.Quality = 0;
    depthBufferDesc.Usage = D3D10_USAGE_DEFAULT;
    depthBufferDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;
    depthBufferDesc.CPUAccessFlags = 0;
    depthBufferDesc.MiscFlags = 0;

    // Create the texture for the depth buffer using the filled out description.
    result = m_device->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer);
    if(FAILED(result))
    {
        return false;
    }

    // Initialize the description of the stencil state.
    ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));

    // Set up the description of the stencil state.
    depthStencilDesc.DepthEnable = true;
    depthStencilDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
    depthStencilDesc.DepthFunc = D3D10_COMPARISON_LESS;

    depthStencilDesc.StencilEnable = true;
    depthStencilDesc.StencilReadMask = 0xFF;
    depthStencilDesc.StencilWriteMask = 0xFF;

    // Stencil operations if pixel is front-facing.
    depthStencilDesc.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
    depthStencilDesc.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_INCR;
    depthStencilDesc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
    depthStencilDesc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;

    // Stencil operations if pixel is back-facing.
    depthStencilDesc.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
    depthStencilDesc.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_DECR;
    depthStencilDesc.BackFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
    depthStencilDesc.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS;

    // Create the depth stencil state.
    result = m_device->CreateDepthStencilState(&depthStencilDesc, &m_depthStencilState);
    if(FAILED(result))
    {
        return false;
    }

    // Set the depth stencil state on the D3D device.
    m_device->OMSetDepthStencilState(m_depthStencilState, 1);

    // Initialize the depth stencil view.
    ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));

    // Set up the depth stencil view description.
    depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    depthStencilViewDesc.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
    depthStencilViewDesc.Texture2D.MipSlice = 0;

    // Create the depth stencil view.
    result = m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView);
    if(FAILED(result))
    {
        return false;
    }

    // Bind the render target view and depth stencil buffer to the output render pipeline.
    m_device->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView);

    // Setup the raster description which will determine how and what polygons will be drawn.
    rasterDesc.AntialiasedLineEnable = false;
    rasterDesc.CullMode = D3D10_CULL_BACK;
    rasterDesc.DepthBias = 0;
    rasterDesc.DepthBiasClamp = 0.0f;
    rasterDesc.DepthClipEnable = true;
    rasterDesc.FillMode = D3D10_FILL_SOLID;
    rasterDesc.FrontCounterClockwise = false;
    rasterDesc.MultisampleEnable = false;
    rasterDesc.ScissorEnable = false;
    rasterDesc.SlopeScaledDepthBias = 0.0f;

    // Create the rasterizer state from the description we just filled out.
    result = m_device->CreateRasterizerState(&rasterDesc, &m_rasterState);
    if(FAILED(result))
    {
        return false;
    }

    // Now set the rasterizer state.
    m_device->RSSetState(m_rasterState);

    // Setup the viewport for rendering.
    viewport.Width = screenWidth;
    viewport.Height = screenHeight;
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;
    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;

    // Create the viewport.
    m_device->RSSetViewports(1, &viewport);

    // Setup the projection matrix.
    fieldOfView = (float)D3DX_PI / 4.0f;
    screenAspect = (float)screenWidth / (float)screenHeight;

    // Create the projection matrix for 3D rendering.
    D3DXMatrixPerspectiveFovLH(&m_projectionMatrix, fieldOfView, screenAspect, screenNear, screenDepth);

    // Initialize the world matrix to the identity matrix.
    D3DXMatrixIdentity(&m_worldMatrix);

    // Create an orthographic projection matrix for 2D rendering.
    D3DXMatrixOrthoLH(&m_orthoMatrix, (float)screenWidth, (float)screenHeight, screenNear, screenDepth);

    return true;
}
bool DX10_Renderer::InitialiseDeviceAndSwapChain()
{
	// State the Driver to be of type HAL
	m_dx10DriverType = D3D10_DRIVER_TYPE_HARDWARE;

	// Fill out the DXGI Swap Chain Description structure
	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
	swapChainDesc.BufferDesc.Width = m_clientWidth;
	swapChainDesc.BufferDesc.Height = m_clientHeight;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 59;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

	// Multi sampling per pixel
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;

	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.BufferCount = 1;
	swapChainDesc.OutputWindow = m_hWnd;
	swapChainDesc.Windowed = true;
	swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
	swapChainDesc.Flags = 0;

	// Add Debug Info to the flags when run in Debug mode
	UINT createDeviceFlags = 0;
	#if defined(DEBUG) || defined(_DEBUG)  
		createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
	#endif
	
	// Create the Device and Swap Chain
	VALIDATEHR(	D3D10CreateDeviceAndSwapChain(
				0,                 //default adapter
				m_dx10DriverType,
				0,                 // no software device
				createDeviceFlags,
				D3D10_SDK_VERSION,
				&swapChainDesc,
				&m_pDX10SwapChain,
				&m_pDX10Device));

	m_rasterizerDesc.CullMode = D3D10_CULL_BACK; 
	m_rasterizerDesc.FillMode = D3D10_FILL_SOLID;
	m_rasterizerDesc.FrontCounterClockwise = false;
	m_rasterizerDesc.DepthBias = 0;					
	m_rasterizerDesc.DepthBiasClamp = 0;
	m_rasterizerDesc.SlopeScaledDepthBias = 0;
	m_rasterizerDesc.DepthClipEnable = true;
	m_rasterizerDesc.ScissorEnable = false;
	m_rasterizerDesc.MultisampleEnable = false;
	m_rasterizerDesc.AntialiasedLineEnable = false;

	m_pDX10Device->CreateRasterizerState(&m_rasterizerDesc, &m_pRasterizerState);
	m_pDX10Device->RSSetState(m_pRasterizerState);

	// Create the Rasterizer description for culling clockwise when using reflection
	D3D10_RASTERIZER_DESC rasterizerDesc;
	ZeroMemory(&rasterizerDesc, sizeof(rasterizerDesc));
	rasterizerDesc.FillMode = D3D10_FILL_SOLID;
	rasterizerDesc.CullMode = D3D10_CULL_BACK;
	rasterizerDesc.FrontCounterClockwise = true;

	VALIDATEHR(m_pDX10Device->CreateRasterizerState(&rasterizerDesc, &m_pRasterizerState_Reflection));
	
	// Invoke functionality that deals with changing size of the window
	VALIDATE(onResize());

	return true;
}
Пример #27
0
	//=============================================================================================================
	bool CGame10::Initialize()
	{
		if( Graphics )
			return true;
				
		HRESULT hr;
		HWND hwnd;
		bool success = false;
		ID3D10Texture2D* backbuffer;
		D3D10_VIEWPORT vp;

		int width = (int)DisplayMode.Width;
		int height = (int)DisplayMode.Height;

		success = Application.Initialize(width, height, FullScreen);
		dassert(false, "CGame10::Initialize(): Could not initialize application", success);

		hwnd = Application.GetWindowHandle();

		SwapChainDesc.BufferDesc			= DisplayMode;
		SwapChainDesc.BufferDesc.Width		= width;
		SwapChainDesc.BufferDesc.Height		= height;
		SwapChainDesc.BufferCount			= 1;
		SwapChainDesc.BufferUsage			= DXGI_USAGE_RENDER_TARGET_OUTPUT;
		SwapChainDesc.OutputWindow			= hwnd;
		SwapChainDesc.SampleDesc.Count		= 1;
		SwapChainDesc.SampleDesc.Quality	= 0;
		SwapChainDesc.Windowed				= !FullScreen;
		
		UINT flags = 0;

	#ifdef _DEBUG
		flags |= D3D10_CREATE_DEVICE_DEBUG;
	#endif

		hr = D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, flags,
			D3D10_SDK_VERSION, &SwapChainDesc, &SwapChain, &Graphics);

		dnassert(false, "CGame10::Initialize(): Could not create device", FAILED(hr));

		hr = SwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (void**)&backbuffer);
		dnassert(false, "CGame10::Initialize(): Could not get backbuffer", FAILED(hr));

		hr = Graphics->CreateRenderTargetView(backbuffer, NULL, &RenderTargetView);

		backbuffer->Release();
		dnassert(false, "CGame10::Initialize(): Could not create render target view", FAILED(hr));

		// zbuffer
		if( ZBuffer )
		{
			D3D10_TEXTURE2D_DESC depthdesc;
			memset(&depthdesc, 0, sizeof(D3D10_TEXTURE2D_DESC));

			depthdesc.Width					= width;
			depthdesc.Height				= height;
			depthdesc.MipLevels				= 1;
			depthdesc.ArraySize				= 1;
			depthdesc.Format				= DXGI_FORMAT_D32_FLOAT;
			depthdesc.SampleDesc.Count		= 1;
			depthdesc.SampleDesc.Quality	= 0;
			depthdesc.Usage					= D3D10_USAGE_DEFAULT;
			depthdesc.BindFlags				= D3D10_BIND_DEPTH_STENCIL;

			hr = Graphics->CreateTexture2D(&depthdesc, NULL, &DepthStencil);
			dnassert(false, "CGame10::Initialize(): Could not create depth buffer", FAILED(hr));

			D3D10_DEPTH_STENCIL_VIEW_DESC dsv;
			memset(&dsv, 0, sizeof(D3D10_DEPTH_STENCIL_VIEW_DESC));
			
			dsv.Format = depthdesc.Format;
			dsv.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
			dsv.Texture2D.MipSlice = 0;

			hr = Graphics->CreateDepthStencilView(DepthStencil, &dsv, &DepthStencilView);
			dnassert(false, "CGame10::Initialize(): Could not create depth stencil view", FAILED(hr));
		}

		Graphics->OMSetRenderTargets(1, &RenderTargetView, DepthStencilView);

		vp.Width = width;
		vp.Height = height;
		vp.MinDepth = 0.0f;
		vp.MaxDepth = 1.0f;
		vp.TopLeftX = 0;
		vp.TopLeftY = 0;

		Graphics->RSSetViewports(1, &vp);

		// ...

		if( fixedtimestep )
			Sync.synchronize.connect(this, &CGame10::Update);

		return true;
	}
Пример #28
0
void CreateD3DDevice()
{
	RECT ClientRect;
	GetClientRect(g_pHWND, &ClientRect);
	UINT width = ClientRect.right - ClientRect.left;
	UINT height = ClientRect.bottom - ClientRect.top;

	D3D10_DRIVER_TYPE driverTypes[] =
	{
		D3D10_DRIVER_TYPE_HARDWARE,
		D3D10_DRIVER_TYPE_WARP,
		D3D10_DRIVER_TYPE_REFERENCE,
	};
	UINT numDriverTypes = ARRAYSIZE(driverTypes);

	DXGI_SWAP_CHAIN_DESC sd;
	ZeroMemory(&sd, sizeof(sd));
	sd.BufferCount = 1;
	sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.OutputWindow = g_pHWND;
	sd.SampleDesc.Count = 1;
	sd.SampleDesc.Quality = 0;
	sd.Windowed = TRUE;

	sd.BufferDesc.RefreshRate = { 1, 60 };
	sd.BufferDesc.Width = width;
	sd.BufferDesc.Height = height;
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

	HRESULT hr = S_OK;
	for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
	{
		hr = D3D10CreateDeviceAndSwapChain(NULL, driverTypes[driverTypeIndex], NULL, D3D10_CREATE_DEVICE_SINGLETHREADED, 
			D3D10_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice);
		if (SUCCEEDED(hr))
			break;
	}

	if (FAILED(hr))
	{
		OutputDebugString(L"Couldn't create D3D Device for some reason!");
		return;
	}

	// Create a render target view
	ID3D10Texture2D* pBackBuffer = NULL;
	hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
	if (FAILED(hr))
	{
		OutputDebugString(L"Couldn't get back buffer from Swap Chain for some reason!");
		return;
	}

	hr = g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);
	pBackBuffer->Release();
	if (FAILED(hr))
	{
		OutputDebugString(L"Couldn't create render target for some reason!");
		return;
	}

	g_pd3dDevice->OMSetRenderTargets(1, &g_pRenderTargetView, NULL);

	// Setup the viewport
	D3D10_VIEWPORT vp;
	vp.Width = (FLOAT)width;
	vp.Height = (FLOAT)height;
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;
	g_pd3dDevice->RSSetViewports(1, &vp);

}
Пример #29
0
bool initD3DDevice(HWND hWnd)
{
	// Création du device
	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	memset(&swapChainDesc, 0, sizeof(swapChainDesc));
	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = SCREEN_WIDTH;
	swapChainDesc.BufferDesc.Height = SCREEN_HEIGHT;
    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = REFRESHRATE;
    swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow = hWnd;
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.SampleDesc.Quality = 0;
    swapChainDesc.Windowed = true;
	UINT deviceFlags = 0;
	#ifdef _DEBUG
		deviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
	#endif
	if(FAILED(D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, deviceFlags, D3D10_SDK_VERSION, &swapChainDesc, &g_pSwapChain, &g_pD3DDevice)))
		return false;

	// Création de la vue
	ID3D10Texture2D *pBackBuffer;
	if(FAILED(g_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (void **)&pBackBuffer)))
		return false;
	HRESULT res = g_pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);
	pBackBuffer->Release();
	if(FAILED(res))
		return false;
	g_pD3DDevice->OMSetRenderTargets(1, &g_pRenderTargetView, NULL);

	// Initialisation du viewport
	D3D10_VIEWPORT viewport;
	viewport.Width = SCREEN_WIDTH;
	viewport.Height = SCREEN_HEIGHT;
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;
    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;
    g_pD3DDevice->RSSetViewports(1, &viewport);

	// Chargement des shaders
	DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
	#ifdef _DEBUG
		dwShaderFlags |= D3D10_SHADER_DEBUG;
	#endif
	if(FAILED(D3DX10CreateEffectFromFile("raytracing.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0, g_pD3DDevice, NULL, NULL, &g_pEffect, NULL, NULL)))
		return false;

	// Récupération de la technique
	g_pTechnique = g_pEffect->GetTechniqueByName("Render");

	// Création du input layout
	D3D10_INPUT_ELEMENT_DESC layout[] =
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0},
		{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0},
	};
	D3D10_PASS_DESC PassDesc;
	g_pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc);
	if(FAILED(g_pD3DDevice->CreateInputLayout(layout, 2, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout)))
		return false;
	g_pD3DDevice->IASetInputLayout(g_pVertexLayout);

	// Création du vertex buffer
	SimpleVertex vertices[] =
	{
		{D3DXVECTOR3(-1.0f, -1.0f, 0.5f), D3DXVECTOR2(0.0f, 0.0f)},
		{D3DXVECTOR3(-1.0f, 1.0f, 0.5f), D3DXVECTOR2(0.0f, 1.0f)},
		{D3DXVECTOR3(1.0f, -1.0f, 0.5f), D3DXVECTOR2(1.0f, 0.0f)},
		{D3DXVECTOR3(1.0f, 1.0f, 0.5f), D3DXVECTOR2(1.0f, 1.0f)}
	};
	D3D10_BUFFER_DESC bd;
	bd.Usage = D3D10_USAGE_DEFAULT;
	bd.ByteWidth = sizeof(SimpleVertex)*4;
	bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;
	D3D10_SUBRESOURCE_DATA InitData;
	InitData.pSysMem = vertices;
	if(FAILED(g_pD3DDevice->CreateBuffer(&bd, &InitData, &g_pVertexBuffer)))
		return false;
    UINT stride = sizeof(SimpleVertex);
    UINT offset = 0;
    g_pD3DDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);
    g_pD3DDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

	return true;
}
Пример #30
0
bool Init_Device()
{
	//Set up DX swap chain
	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory( &swapChainDesc, sizeof(swapChainDesc) );

	//set buffer dimensions and format
	swapChainDesc.BufferCount = 2;
	swapChainDesc.BufferDesc.Width = GAME_RESOLUTION_X;
	swapChainDesc.BufferDesc.Height = GAME_RESOLUTION_Y;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;

	//set refresh rate
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;

	//sampling settings
	swapChainDesc.SampleDesc.Quality = 0;
	swapChainDesc.SampleDesc.Count = 1;

	//output window handle
	swapChainDesc.OutputWindow = Window_GetHWND();
	swapChainDesc.Windowed = !FULL_SCREEN;

	//Create the D3D device
	if ( FAILED( D3D10CreateDeviceAndSwapChain(	NULL,D3D10_DRIVER_TYPE_HARDWARE,NULL,0,D3D10_SDK_VERSION,&swapChainDesc,&dx_swap_chain,&dx_device) ) )
			return false;

	//try to get the back buffer
	ID3D10Texture2D* pBackBuffer;

	if ( FAILED( dx_swap_chain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer) ) ) return false;

	//try to create render target view
	if ( FAILED( dx_device->CreateRenderTargetView(pBackBuffer, NULL, &dx_render_target) ) ) return false;

	//release the back buffer
	pBackBuffer->Release();

	//set the render target
	dx_device->OMSetRenderTargets(1, &dx_render_target, NULL);

	//create viewport structure
	dx_viewport.Width = GAME_RESOLUTION_X;
	dx_viewport.Height = GAME_RESOLUTION_Y;
	dx_viewport.MinDepth = 0.0f;
	dx_viewport.MaxDepth = 1.0f;
	dx_viewport.TopLeftX = 0;
	dx_viewport.TopLeftY = 0;

	//set the viewport
	dx_device->RSSetViewports(1, &dx_viewport);

	//setup blend state
	D3D10_BLEND_DESC BlendState;
	ZeroMemory(&BlendState, sizeof(D3D10_BLEND_DESC));

	BlendState.BlendEnable[0] = TRUE;

	BlendState.SrcBlend = D3D10_BLEND_SRC_ALPHA;
	BlendState.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
	BlendState.BlendOp = D3D10_BLEND_OP_ADD;

	BlendState.SrcBlendAlpha = D3D10_BLEND_ONE;
	BlendState.DestBlendAlpha = D3D10_BLEND_ONE;
	BlendState.BlendOpAlpha = D3D10_BLEND_OP_ADD;

	BlendState.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;

	dx_device->CreateBlendState(&BlendState, &dx_AlphaBlendState);

	//set blend state
	dx_device->OMSetBlendState(dx_AlphaBlendState, 0, 0xfffffff);

	return true;
}