예제 #1
0
HRESULT DXApp::onResize(int width, int height) {
	if (!_dxDev)
		return S_FALSE;

	// Release render target and depth-stencil view
	ID3D11RenderTargetView *nullRTV = NULL;
	_dxImmedDC->OMSetRenderTargets(1, &nullRTV, NULL);
	
	if (_depthStencilView) {
		_depthStencilView->Release();
		_depthStencilView = NULL;
	}

	if (_renderTargetView) {
		_renderTargetView->Release();
		_renderTargetView = NULL;
	}

	// Resize swap chain
	_swapChainDesc.BufferDesc.Width = width;
	_swapChainDesc.BufferDesc.Height = height;
	if (_swapChain) 
		_swapChain->ResizeBuffers(_swapChainDesc.BufferCount, _swapChainDesc.BufferDesc.Width, 
			_swapChainDesc.BufferDesc.Height, _swapChainDesc.BufferDesc.Format, _swapChainDesc.Flags);

	// Re-create a depth-stencil view
	_depthStencilDesc.Width = _swapChainDesc.BufferDesc.Width;
	_depthStencilDesc.Height = _swapChainDesc.BufferDesc.Height;

	createDepthStencilView();

	// Re-create a render target 
	createRenderTargetView();

	// Bind the views to the output
	_dxImmedDC->OMSetRenderTargets(1, &_renderTargetView, _depthStencilView);

	// Setup viewport
	setupViewport();

	return S_OK;
}
예제 #2
0
// Create Direct3D device and swap chain
HRESULT DXApp::initialiseDxDevice() {
	// Get window size
	RECT rc;
	GetClientRect(_hWnd, &rc);
	UINT width = rc.right - rc.left;
	UINT height = rc.bottom - rc.top;

	// Create D3D11 device and swap chain
	UINT createDeviceFlags = 0;
#ifdef _DEBUG
	//createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
	ZeroMemory(&_swapChainDesc, sizeof(_swapChainDesc));
	_swapChainDesc.BufferDesc.Width = width;
	_swapChainDesc.BufferDesc.Height = height;
	_swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	_swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	_swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; // DXGI_FORMAT_R8G8B8A8_UNORM; // 
	_swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
	_swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;

	// No multisampling
	_swapChainDesc.SampleDesc.Count = 1;
	_swapChainDesc.SampleDesc.Quality = 0;

	_swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	_swapChainDesc.BufferCount = 1;
	_swapChainDesc.OutputWindow = _hWnd;
	_swapChainDesc.Windowed = true;
	_swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
	_swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

	// Try to create a hardware accelerated device
	const D3D_FEATURE_LEVEL featureLevel[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0 };
	HRESULT hr = D3D11CreateDeviceAndSwapChain(
		NULL, // Primary graphics adapter
		D3D_DRIVER_TYPE_HARDWARE, // Use graphics hardware
		NULL, // Use hardware rasteriser
		createDeviceFlags, // Debug flags
		featureLevel, // Feature level
		2, // Elements in the feature level
		D3D11_SDK_VERSION, // SDK version
		&_swapChainDesc, // Description of the swap chain
		&_swapChain, // Returns the created swap chain
		&_dxDev, // Returns the created device
		NULL, // Returns feature level
		&_dxImmedDC // Returns the Device Context 
		);

	if (FAILED(hr)) {
		// If failed, try to create a reference device
		D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_REFERENCE, NULL, createDeviceFlags, 
			NULL, 0, D3D11_SDK_VERSION, &_swapChainDesc, &_swapChain, &_dxDev, NULL, &_dxImmedDC);
		
		MessageBox(_hWnd, L"No DX10 hardware acceleration found.\nSwitching to REFERENCE driver (very slow).", L"Warning", MB_OK|MB_ICONWARNING);
	}


	// Create a depth - stencil view
	_depthStencilDesc.Width = width;
	_depthStencilDesc.Height = height;
	_depthStencilDesc.MipLevels = 1;
	_depthStencilDesc.ArraySize = 1;
	_depthStencilDesc.Format = DXGI_FORMAT_D16_UNORM;
	_depthStencilDesc.SampleDesc = _swapChainDesc.SampleDesc;
	_depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
	_depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
	_depthStencilDesc.CPUAccessFlags = 0;
	_depthStencilDesc.MiscFlags = 0;

	createDepthStencilView();

	// Create a render target view
	createRenderTargetView();

	// Bind the views to the output
	_dxImmedDC->OMSetRenderTargets(1, &_renderTargetView, _depthStencilView);

	// Setup viewport
	setupViewport();

	return S_OK;
}
예제 #3
0
bool D3D11Context::startup (void* hwnd)
{
    UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

#ifdef _DEBUG
    flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    D3D_FEATURE_LEVEL featureLevels[] =
    {
        D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0,
    };

    if (m_failed (::D3D11CreateDevice (nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, featureLevels, numElementsInArray (featureLevels), D3D11_SDK_VERSION, device, &featureLevel, contextIM)))
    {
        if (m_failed (::D3D11CreateDevice (nullptr, D3D_DRIVER_TYPE_WARP, nullptr, flags, featureLevels, numElementsInArray (featureLevels), D3D11_SDK_VERSION, device, &featureLevel, contextIM)))
            return false;
    }

    Hold<IDXGIDevice2> dxgiDevice;
    if (m_failed (device.as (dxgiDevice)))
        return false;

    Hold<IDXGIAdapter> dxgiAdapter;
    if (m_failed (dxgiDevice->GetAdapter (dxgiAdapter)))
        return false;

    Hold<IDXGIFactory2> dxgiFactory;
    if (m_failed (dxgiAdapter->GetParent (__uuidof (IDXGIFactory2), dxgiFactory)))
        return false;

    DXGI_SWAP_CHAIN_DESC1 desc;

    desc.Width = 0;
    desc.Height = 0;
    desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    desc.Stereo = false;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    desc.BufferCount = 2;
    desc.Scaling = DXGI_SCALING_STRETCH; // DXGI_SCALING_NONE is not supported on Windows7
    desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    desc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
    desc.Flags = 0;

    if (m_failed (dxgiFactory->CreateSwapChainForHwnd (device, (HWND)hwnd, &desc, nullptr, nullptr, swapchain)))
        return false;

    Hold<ID3D11Texture2D> backBuf;
    if (m_failed (swapchain->GetBuffer (0, __uuidof (ID3D11Texture2D), backBuf)))
        return false;

    if (m_failed (device->CreateRenderTargetView (backBuf, nullptr, backBufRTView)))
        return false;

    RECT rect;
    ::GetClientRect ((HWND)hwnd, &rect);
    if (m_isnull (depthBuf.set (createTexture2DRT (rect.right - rect.left, rect.bottom - rect.top, 1, DXGI_FORMAT_R32_TYPELESS, DXGI_FORMAT_D32_FLOAT))))
        return false;

    if (m_isnull (depthBufDSView.set (createDepthStencilView (depthBuf, 0, DXGI_FORMAT_D32_FLOAT))))
        return false;

    if (m_isnull (depthBufSRView.set (createShaderResourceView (depthBuf, DXGI_FORMAT_R32_FLOAT))))
        return false;

    // common sampler states
    {
        D3D11_SAMPLER_DESC _ = CD3D11_SAMPLER_DESC (D3D11_DEFAULT);
        _.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
        _.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
        _.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
        _.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
        if (m_isnull (sampWrapLinear.set (createSamplerState (_))))
            return false;
    }
    {
        D3D11_SAMPLER_DESC _ = CD3D11_SAMPLER_DESC (D3D11_DEFAULT);
        _.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
        _.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
        _.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
        _.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
        if (m_isnull (sampWrapPoint.set (createSamplerState (_))))
            return false;
    }
    {
        D3D11_SAMPLER_DESC _ = CD3D11_SAMPLER_DESC (D3D11_DEFAULT);
        _.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
        _.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
        _.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
        _.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
        if (m_isnull (sampClampLinear.set (createSamplerState (_))))
            return false;
    }
    {
        D3D11_SAMPLER_DESC _ = CD3D11_SAMPLER_DESC (D3D11_DEFAULT);
        _.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
        _.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
        _.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
        _.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
        if (m_isnull (sampClampPoint.set (createSamplerState (_))))
            return false;
    }

    // common rasterizer state
    {
        D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT);
        _.CullMode = D3D11_CULL_NONE;
        _.FrontCounterClockwise = FALSE;
        if (m_isnull (rastCullNone.set (createRasterizerState (_))))
            return false;
    }

    {
        D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT);
        _.CullMode = D3D11_CULL_FRONT;
        _.FrontCounterClockwise = FALSE;
        if (m_isnull (rastCWCullFront.set (createRasterizerState (_))))
            return false;
    }

    {
        D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT);
        _.CullMode = D3D11_CULL_BACK;
        _.FrontCounterClockwise = FALSE;
        if (m_isnull (rastCWCullBack.set (createRasterizerState (_))))
            return false;
    }

    {
        D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT);
        _.CullMode = D3D11_CULL_FRONT;
        _.FrontCounterClockwise = TRUE;
        if (m_isnull (rastCCWCullFront.set (createRasterizerState (_))))
            return false;
    }

    {
        D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT);
        _.CullMode = D3D11_CULL_BACK;
        _.FrontCounterClockwise = TRUE;
        if (m_isnull (rastCCWCullBack.set (createRasterizerState (_))))
            return false;
    }

    // common depth stencil state
    {
        D3D11_DEPTH_STENCIL_DESC _ = CD3D11_DEPTH_STENCIL_DESC (D3D11_DEFAULT);
        _.DepthEnable = TRUE;
        _.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
        if (m_isnull (depthTestOnWriteOn.set (createDepthStencilState (_))))
            return false;
    }

    {
        D3D11_DEPTH_STENCIL_DESC _ = CD3D11_DEPTH_STENCIL_DESC (D3D11_DEFAULT);
        _.DepthEnable = TRUE;
        _.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
        if (m_isnull (depthTestOnWriteOff.set (createDepthStencilState (_))))
            return false;
    }

    {
        D3D11_DEPTH_STENCIL_DESC _ = CD3D11_DEPTH_STENCIL_DESC (D3D11_DEFAULT);
        _.DepthEnable = FALSE;
        _.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
        if (m_isnull (depthTestOffWriteOn.set (createDepthStencilState (_))))
            return false;
    }

    {
        D3D11_DEPTH_STENCIL_DESC _ = CD3D11_DEPTH_STENCIL_DESC (D3D11_DEFAULT);
        _.DepthEnable = FALSE;
        _.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
        if (m_isnull (depthTestOffWriteOff.set (createDepthStencilState (_))))
            return false;
    }

    return true;
}