D2D + D3D11 help me to make them work together

Started by
19 comments, last by BlackJoker 11 years, 1 month ago

Thanks a lot to all of you for your help! Everything seems work fine.

Advertisement

I faced with another problem. Now D3D scene overlap D2D text in some cases. How to make D2D text always on top of D3D scene?

Could someone help me with this question?

Are you drawing text directly to the backbuffer with D2D? If so, its not using the depth buffer. You could try drawing the text last, or drawing the text to a texture first, and then drawing a quad using that texture.

Are you drawing text directly to the backbuffer with D2D? If so, its not using the depth buffer. You could try drawing the text last, or drawing the text to a texture first, and then drawing a quad using that texture.

Yes, I am drawing directly to backbuffer. If I will draw to a texture will this texture be always on top of D3D scene independently of when I am drawing it?

And one more question. Rendered text displaying not clear enough. I use antialiasing like this:




d2dRenderTarget->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE);
	D2D1_ANTIALIAS_MODE anti;
	anti = D2D1_ANTIALIAS_MODE_PER_PRIMITIVE;
	d2dRenderTarget->SetAntialiasMode(anti);

but text look like diffused (see attachment).

How to fix this and make text clear?

Yes, I am drawing directly to backbuffer. If I will draw to a texture will this texture be always on top of D3D scene independently of when I am drawing it?

Here's a better explanation:

If you're just overlaying text on a scene, easiest would be to just draw it last. If for some reason you can't/don't want to draw all the text last, you could draw the text to a texture, and then draw a quad with that texture. You'll still need to draw that quad last, though, and you'd probably want to disable depth testing for that. (Note that this is also a bit less efficient because of the additional memory used by the texture, and overdraw.)

How to fix this and make text clear?

Not sure. If I have time, I'll see how it looks on my system later. Could you post the code you used to create the font?

Not sure. If I have time, I'll see how it looks on my system later. Could you post the code you used to create the font?

Yes, here is my coe for that - it is just copy from yours






swapChain->GetBuffer(0, IID_PPV_ARGS(&BackBuffer11));

	d3d11_Device->CreateRenderTargetView(BackBuffer11, NULL, &d3d11_RenderTargetView);
	d3d11_DeviceContext->OMSetRenderTargets(1, &d3d11_RenderTargetView, NULL);

	options.debugLevel = D2D1_DEBUG_LEVEL_ERROR;
	D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, options, &d2dfactory);

	swapChain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackbuffer));
	D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
		D2D1_RENDER_TARGET_TYPE_DEFAULT,
		D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED));
	d2dfactory->CreateDxgiSurfaceRenderTarget(dxgiBackbuffer, props, &d2dRenderTarget);
	dxgiBackbuffer->Release();

	DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(DWriteFactory), (IUnknown**)(&DWriteFactory));

	DWriteFactory->CreateTextFormat(
		L"Cambria",
		NULL,
		DWRITE_FONT_WEIGHT_NORMAL,
		DWRITE_FONT_STYLE_NORMAL,
		DWRITE_FONT_STRETCH_NORMAL,
		14,
		L"",
		&textFormat);

	d2dRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &Brush);

and then I DrawText with this fuction:

In this code I don`t use antialiasing, but there is no difference between using and not using it.






void D3D11::RenderText(WCHAR* text)
{
	d2dRenderTarget->BeginDraw();
	DWriteFactory->CreateTextLayout(text, wcslen(text), textFormat, 1000, 10, &textLayout);

	D2D1_POINT_2F origin;
	origin.x = 0;
	origin.y = 0;
	D2D1_DRAW_TEXT_OPTIONS op= D2D1_DRAW_TEXT_OPTIONS_NONE;
	d2dRenderTarget->DrawTextLayout(origin,textLayout,Brush, op);
	DWRITE_TEXT_METRICS textMetrix;
	//???????? ?????????? ? ???????????? ??????
	textLayout->GetMetrics(&textMetrix);
	//d2dRenderTarget->DrawText(text, wcslen(text), textFormat, D2D1::RectF(0, 0, 800, 600), Brush);
	d2dRenderTarget->EndDraw();
	textLayout->Release();
	textLayout = NULL;
}

Ok, so two things:

First, it looks like you are using a font that you don't have. Cambria is a serif font, but the font in your screenshot is sans-serif, so I think it is failing to find the font you specified and uses a fallback instead.

Second, everything looks correct when I draw text with my code (see attachment). How are you sizing your window and backbuffer? If you're passing the same dimensions to CreateWindow and D3D11CreateDeviceAndSwapChain, that could be the problem because CreateWindow includes the size of the border in the dimensions. So then you end up with a window that is smaller than your backbuffer, and everything gets scaled and looks horrible.

You should use AdjustWindowRect to take the size of the border into account, passing the same style used to create the window (ex. WS_OVERLAPPEDWINDOW). Then when calling CreateWindow, you should pass rect.right - rect.left as the width, and rect.bottom - rect.top as the height, because AdjustWindowRect might make the left and top values negative. Relevant snippet from my code above:


RECT r = {0, 0, 800, 600};
AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, false);

HWND hWnd = CreateWindow(
	(LPCSTR)a,
	"Vector",
	WS_OVERLAPPEDWINDOW,
	CW_USEDEFAULT,
	CW_USEDEFAULT,
	r.right - r.left,
	r.bottom - r.top,
	NULL,
	NULL,
	hInstance,
	NULL);

AdjustWindowRect

jrh2365, your advice helped. Thanks a lot, but 1 question left (I hope one).

I setup patch fro Win7 you wrote to fix interop D2D with D3D and now it works, but when I exit application, it crashes in "crt0dat.c" in




void __cdecl __crtExitProcess (
        int status
        )
{
        __crtCorExitProcess(status);

        /*
         * Either mscoree.dll isn't loaded,
         * or CorExitProcess isn't exported from mscoree.dll,
         * or CorExitProcess returned (should never happen).
         * Just call ExitProcess.
         */

        ExitProcess(status);
}

here in the last line - ExitProcess(status)

If I comment the code for initialization of D2D, then no crashes occured. On Win 8 no crashes at all. Do you know how to fix that?

This topic is closed to new replies.

Advertisement