Multisampling in a DirectX 9 application on Windows 7
In an application I try to use multisampling for anti-aliasing on some simple 2D geometry. It was developed originally on Windows XP where I had no problem enabling multisampling for my DirectX device and for any additional swap chains. Now, on Windows 7 the multisampling feature seems to not work at all.
I've extracted a very short example code, that does nothing but displaying a triangle. When I run the program on Windows XP, the edge is anti-aliased, but on Windows 7 it isn't.
void testDX() {
struct CustomVertex {
FLOAT x, y, z, rhw;
DWORD color;
};
CustomVertex vertices[] = {
{0.0f, 0.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
{700.0f, 500.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
开发者_JS百科 {0.0f, 500.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
};
HRESULT hr;
DXWindow window;
window.New(GetDesktopWindow(), "Main Window", 0, 0, 800, 600);
IDirect3D9Ptr d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof d3dpp);
d3dpp.Flags = (D3DPRESENTFLAG_VIDEO | D3DPRESENTFLAG_DEVICECLIP) & ~D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.Windowed = TRUE;
d3dpp.hDeviceWindow = window.GetHandle();
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONMASKABLE;
d3dpp.MultiSampleQuality = 7;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
//d3dpp.BackBufferFormat = m_d3ddm.Format;
d3dpp.BackBufferCount = 0;
//d3dpp.EnableAutoDepthStencil = TRUE;
//d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // D3DFMT_D24X8;
IDirect3DDevice9Ptr device;
hr = d3d->CreateDevice(0, D3DDEVTYPE_HAL, window.GetHandle(), D3DCREATE_FPU_PRESERVE | D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device);
IDirect3DSwapChain9Ptr swapChain;
hr = device->GetSwapChain(0, &swapChain);
hr = device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
while (!window.ShouldQuit()) {
Sleep(50);
IDirect3DSurface9Ptr targetSurface;
hr = swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &targetSurface);
hr = device->SetRenderTarget(0, targetSurface);
hr = device->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0);
hr = device->BeginScene();
hr = device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 1, vertices, sizeof CustomVertex);
hr = device->EndScene();
hr = swapChain->Present(0, 0, (HWND)0, 0, D3DPRESENT_DONOTWAIT);
}
}
I tried to compare the code with the AntiAlias sample that Microsoft ships with its DirectX SDK. And while the anti-aliasing effect works in the example code, I could not find any significant difference (however, the program flow is not very intuitive).
My question is, why does anti-aliasing via multisampling work on Windows XP but not on Windows 7 and what can I do to fix this?
Removing the D3DPRESENTFLAG_VIDEO helped to enable multisampling on Windows 7. It was added because the application uses DirectX to display video. But as the flag is not very well documented and seems to be only a hint for the video driver, I don't know which side effects setting or not setting the flag has. It has no other obvious effect, but maybe there is a performance penalty that I'm not aware of.
精彩评论