Jump to content
  • Advertisement
Sign in to follow this  
Tank202

DirectX11 and Direct2D interoperability problem

This topic is 2037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there,

 

I'm wokring on a DEMO that uses both DirectX11 and Direct2D for rendering. However I got stuck trying to initialize render target. I simply can't initialize it.

Could you help me solve this puzzle.

 

Here is the full code of the program:

#include <Eagle2D.h>
#include <d3d11_2.h>
#include <d2d1_2.h>
#include <DxErr.h>

#include <iostream>
using namespace std;

#pragma comment (lib, "eagle2d.lib")
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d2d1.lib")

using namespace Eagle2D;

int main()
{
	IE2DDevice* Device = CreateEagle2DDevice();
	IE2DWindow* window = Device->CreateWindow();

	ID3D11Device* device;
	ID3D11DeviceContext* deviceContext;
	IDXGISwapChain* swapChain;
	ID3D11RenderTargetView* backBuffer;
	IDXGIDevice* xDevice;
	IDXGISurface* xSurface;

	ID2D1Factory1* dFactory;
	ID2D1Device* dDevice;
	ID2D1DeviceContext* dDeviceContext;
	ID2D1RenderTarget* dRenderTarget;

	DXGI_SWAP_CHAIN_DESC scd;
	ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
	scd.BufferCount = 1;
	scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	scd.BufferDesc.Width = window->GetSize().GetX();
	scd.BufferDesc.Height = window->GetSize().GetY();
	scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	scd.OutputWindow = window->_GetWin32HWND();
	scd.SampleDesc.Count = 1;
	scd.Windowed = TRUE;
	scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

	D3D_FEATURE_LEVEL fl = D3D_FEATURE_LEVEL_10_0;

	D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE,
		NULL, D3D11_CREATE_DEVICE_BGRA_SUPPORT, &fl, 1, D3D11_SDK_VERSION, &scd,
		&swapChain, &device, NULL, &deviceContext);
	device->QueryInterface(&xDevice);

	ID3D11Texture2D* pBackBuffer;
	swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D),
		(LPVOID*)&pBackBuffer);

	device->CreateRenderTargetView(pBackBuffer, NULL,
		&backBuffer);
	pBackBuffer->Release();

	deviceContext->OMSetRenderTargets(1, &backBuffer,
		NULL);

	D3D11_VIEWPORT viewport;
	ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	viewport.Width = window->GetSize().GetX();
	viewport.Height = window->GetSize().GetY();

	deviceContext->RSSetViewports(1, &viewport);
	//ID3D11Texture2D* buffer;
	swapChain->GetBuffer(0, __uuidof(IDXGISurface), (LPVOID*)&xSurface);
	//buffer->QueryInterface(&xSurface);



	D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &dFactory);
	dFactory->CreateDevice(xDevice, &dDevice);
	if(FAILED(dFactory->CreateDxgiSurfaceRenderTarget(xSurface, D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_HARDWARE,
		D2D1::PixelFormat(), 96, 96, D2D1_RENDER_TARGET_USAGE_NONE, D2D1_FEATURE_LEVEL_10), &dRenderTarget))) cout << "doh";
	dDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &dDeviceContext);

	while(Device->Run())
	{
		dDeviceContext->BeginDraw();
		dDeviceContext->Clear(D2D1::ColorF(0, 1, 0, 0.5));
		dDeviceContext->EndDraw();

		dRenderTarget->BeginDraw();
		dRenderTarget->Clear();
		dRenderTarget->EndDraw();

		swapChain->Present(0, 0);
	}
}

//If you want to try this program, you will need to change Eagle2D window creation to win32 window.

Edited by Tank202

Share this post


Link to post
Share on other sites
Advertisement

Found what was wrong... I had to intialize render target with specific pixel format... In my case that was D2D1::PixelFormat(DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)

 

Hope this helps for people like me :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!