I use DirecreatectX11.
So want to create ID2D1Device1.
But 'CreateDevice' function return Error.
hr = m_d2dFactory->CreateDevice(dxgiDevice, &m_d2dDevice);
Error Message : "One or more arguments are invalid."
I can understand the reason.
I use Visual Studio2015, C++, platform 8.1.
I add the sample code.
please let me know.
Is there any sample code use ID2D1Device1?
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace DX
{
// Provides an interface for an application that owns DeviceResources to be notified of the device being lost or created.
interface IDeviceNotify
{
virtual void OnDeviceLost() = 0;
virtual void OnDeviceRestored() = 0;
};
// Controls all the DirectX device resources.
class DeviceResources
{
public:
DeviceResources();
void SetWindow(HWND);
void SetLogicalSize(CSize logicalSize);
void SetCurrentOrientation();
void SetDpi(float dpi);
void ValidateDevice();
void HandleDeviceLost();
void RegisterDeviceNotify(IDeviceNotify* deviceNotify);
void Trim();
void Present();
void WaitOnSwapChain();
void OnMessage(MSG);
// Device Accessors.
CSize GetOutputSize() const { return m_outputSize; }
CSize GetLogicalSize() const { return m_logicalSize; }
// D3D Accessors.
ID3D11Device2* GetD3DDevice() { return (ID3D11Device2*)&m_d3dDevice; }
ID3D11DeviceContext2* GetD3DDeviceContext() { return (ID3D11DeviceContext2*)&m_d3dContext; }
IDXGISwapChain1* GetSwapChain() { return (IDXGISwapChain1*)&m_swapChain; }
D3D_FEATURE_LEVEL GetDeviceFeatureLevel() { return m_d3dFeatureLevel; }
ID3D11RenderTargetView* GetBackBufferRenderTargetView() { return (ID3D11RenderTargetView*)&m_d3dRenderTargetView; }
ID3D11DepthStencilView* GetDepthStencilView() { return (ID3D11DepthStencilView *)&m_d3dDepthStencilView; }
D3D11_VIEWPORT GetScreenViewport() { return m_screenViewport; }
void Render(CSR_CharacterRender*);
void ReleaseDeviceResources();
void SetLatency();
HWND GetHWND() { return m_hWindow; }
void GetRotateMat(D3DXVECTOR3 fRotate);
ID3D11Device* m_d3dDeviceOrg = nullptr;
ID3D11DeviceContext* m_d3dContextOrg = nullptr;
CSR_CharacterRender m_oCharacterRender;
LARGE_INTEGER m_fTime, m_fRatio;
CPoint m_nClickPos;
private:
void CreateDeviceIndependentResources();
void CreateDeviceResources();
void CreateWindowSizeDependentResources();
// Direct3D objects.
ID3D11Device2* m_d3dDevice = nullptr;
ID2D1Device1* m_d2dDevice = nullptr;
ID3D11DeviceContext2* m_d3dContext = nullptr;
IDXGISwapChain1* m_swapChain = nullptr;
IDXGISwapChain2* m_swapChain2 = nullptr;
// Direct3D rendering objects. Required for 3D.
ID3D11RenderTargetView* m_d3dRenderTargetView = nullptr;
ID3D11DepthStencilView* m_d3dDepthStencilView = nullptr;
D3D11_VIEWPORT m_screenViewport;
// Direct2D drawing components.
ID2D1Factory2* m_d2dFactory=nullptr;
// DirectWrite drawing components.
IDWriteFactory2* m_dwriteFactory = nullptr;
IDWriteTextFormat* m_pTextFormat = nullptr;
ID2D1SolidColorBrush* m_FPSBrush = nullptr;
// Cached reference to the Window.
HWND m_hWindow;
// Cached device properties.
D3D_FEATURE_LEVEL m_d3dFeatureLevel;
CSize m_d3dRenderTargetSize;
CSize m_outputSize;
CSize m_logicalSize;
// Used by the WaitOnSwapChain method.
HANDLE m_frameLatencyWaitableObject;
// The IDeviceNotify can be held directly as it owns the DeviceResources.
IDeviceNotify* m_deviceNotify=nullptr;
FLOAT m_pBackColor[4];
};
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void
DX::DeviceResources::CreateDeviceIndependentResources()
{
// Initialize Direct2D resources.
D2D1_FACTORY_OPTIONS options;
ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));
#if defined(_DEBUG)
// If the project is in a debug build, enable Direct2D debugging via SDK Layers.
options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
#endif
// Initialize the Direct2D Factory.
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory2), &options, (void**)&m_d2dFactory);
// Initialize the DirectWrite Factory.
IUnknown* pWriteFactory = nullptr;
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory2), (IUnknown**)&pWriteFactory);
m_dwriteFactory = (IDWriteFactory2*)pWriteFactory;
}
void
DX::DeviceResources::CreateDeviceResources()
{
// This flag adds support for surfaces with a different color channel ordering
// than the API default. It is required for compatibility with Direct2D.
//UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
UINT creationFlags = 0;
#if defined(_DEBUG)
//if (DX::SdkLayersAvailable())
{
// If the project is in a debug build, enable debugging via SDK Layers with this flag.
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
}
#endif
// This array defines the set of DirectX hardware feature levels this app will support.
// Note the ordering should be preserved.
// Don't forget to declare your application's minimum required feature level in its
// description. All applications are assumed to support 9.1 unless otherwise stated.
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
};
// Create the Direct3D 11 API device object and a corresponding context.
ID3D11Device* device=nullptr;
ID3D11DeviceContext* context = nullptr;
HRESULT hr = D3D11CreateDevice(
nullptr, // Specify nullptr to use the default adapter.
D3D_DRIVER_TYPE_HARDWARE, // Create a device using the hardware graphics driver.
0, // Should be 0 unless the driver is D3D_DRIVER_TYPE_HARDWARE.
creationFlags, // Set debug and Direct2D compatibility flags.
featureLevels, // List of feature levels this app can support.
ARRAYSIZE(featureLevels), // Size of the list above.
D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
&device, // Returns the Direct3D device created.
&m_d3dFeatureLevel, // Returns feature level of device created.
&context // Returns the device immediate context.
);
if (FAILED(hr))
{
// If the initialization fails, fall back to the WARP device.
// For more information on WARP, see:
D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, 0, creationFlags, featureLevels, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION, &device, &m_d3dFeatureLevel, &context);
}
// Store pointers to the Direct3D 11.1 API device and immediate context.
m_d3dDeviceOrg = device;
m_d3dContextOrg = context;
hr = device->QueryInterface(__uuidof(ID3D11Device2), reinterpret_cast<void**>(&m_d3dDevice));
hr = context->QueryInterface(__uuidof(ID3D11DeviceContext2), reinterpret_cast<void**>(&m_d3dContext));
D3D11_RASTERIZER_DESC drd = {
D3D11_FILL_SOLID, //D3D11_FILL_MODE FillMode;
D3D11_CULL_BACK,//D3D11_CULL_MODE CullMode;
FALSE, //BOOL FrontCounterClockwise;
0, //INT DepthBias;
0.0f,//FLOAT DepthBiasClamp;
0.0f,//FLOAT SlopeScaledDepthBias;
TRUE,//BOOL DepthClipEnable;
FALSE,//BOOL ScissorEnable;
TRUE,//BOOL MultisampleEnable;
FALSE//BOOL AntialiasedLineEnable;
};
ID3D11RasterizerState* pRS = NULL;
hr = m_d3dDeviceOrg->CreateRasterizerState(&drd, &pRS);
if (hr==S_OK)
{
context->RSSetState(pRS);
}
IDXGIDevice3* dxgiDevice = nullptr;
hr = m_d3dDevice->QueryInterface(__uuidof(IDXGIDevice3), reinterpret_cast<void**>(&dxgiDevice));
hr = m_d2dFactory->CreateDevice(dxgiDevice, &m_d2dDevice);
}
////////////////////////////////////////////////////////////////////////////////////////////////////