在 Direct3D9 中启用抗锯齿(多样本渲染目标)

发布于 2024-08-22 13:13:28 字数 2054 浏览 3 评论 0原文

我正在尝试在 D3D9 应用程序中启用 AA,但不确定如何正确设置曲面。到目前为止,我有:

IDirect3DDevice9* m_pd3dDevice;
IDirect3DSurface9* screen;
IDirect3DSurface9* msaasurf;
D3DPRESENT_PARAMETERS m_presentationParameters;

初始化:

m_presentationParameters.Windowed = TRUE;
m_presentationParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_presentationParameters.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES;
m_presentationParameters.MultiSampleQuality = 0;
m_presentationParameters.BackBufferFormat = D3DFMT_UNKNOWN;
m_presentationParameters.EnableAutoDepthStencil = TRUE;
m_presentationParameters.AutoDepthStencilFormat = D3DFMT_D16;
m_presentationParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

// create d3d device
m_pD3D->CreateDevice(
  D3DADAPTER_DEFAULT, 
  D3DDEVTYPE_HAL, 
  hWnd,
  D3DCREATE_HARDWARE_VERTEXPROCESSING,
  &m_presentationParameters, &m_pd3dDevice 
)


// save screen surface
m_pd3dDevice->GetRenderTarget(0, &screen);
D3DSURFACE_DESC desc;
screen->GetDesc(&desc);

// Create multisample render target
m_pd3dDevice->CreateRenderTarget(
    800, 600, 
    D3DFMT_A8R8G8B8,
    desc.MultiSampleType, desc.MultiSampleQuality,
    false,
    &msaasurf,
    NULL
  );

然后,对于每一帧:

// render to multisample surface
m_pd3dDevice->SetRenderTarget(0, msaasurf);

m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );
m_pd3dDevice->BeginScene();

// render stuff here

m_pd3dDevice->EndScene();

m_pd3dDevice->SetRenderTarget(0, screen);

// get back buffer
IDirect3DSurface9* backBuffer = NULL;
m_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);

// copy rendertarget to backbuffer
m_pd3dDevice->StretchRect(msaasurf, NULL, backBuffer, NULL, D3DTEXF_NONE);
backBuffer->Release();

// Present the backbuffer contents to the display
m_pd3dDevice->Present(NULL, NULL, NULL, NULL);

但是,我的屏幕上没有出现任何内容(全黑)。没有发生错误(我检查所有 d3d 调用的返回值)。我做错了什么?

I am trying to enable AA in a D3D9 application, but am not sure how to set up the surfaces correctly. So far, I have:

IDirect3DDevice9* m_pd3dDevice;
IDirect3DSurface9* screen;
IDirect3DSurface9* msaasurf;
D3DPRESENT_PARAMETERS m_presentationParameters;

Initialization:

m_presentationParameters.Windowed = TRUE;
m_presentationParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_presentationParameters.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES;
m_presentationParameters.MultiSampleQuality = 0;
m_presentationParameters.BackBufferFormat = D3DFMT_UNKNOWN;
m_presentationParameters.EnableAutoDepthStencil = TRUE;
m_presentationParameters.AutoDepthStencilFormat = D3DFMT_D16;
m_presentationParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

// create d3d device
m_pD3D->CreateDevice(
  D3DADAPTER_DEFAULT, 
  D3DDEVTYPE_HAL, 
  hWnd,
  D3DCREATE_HARDWARE_VERTEXPROCESSING,
  &m_presentationParameters, &m_pd3dDevice 
)


// save screen surface
m_pd3dDevice->GetRenderTarget(0, &screen);
D3DSURFACE_DESC desc;
screen->GetDesc(&desc);

// Create multisample render target
m_pd3dDevice->CreateRenderTarget(
    800, 600, 
    D3DFMT_A8R8G8B8,
    desc.MultiSampleType, desc.MultiSampleQuality,
    false,
    &msaasurf,
    NULL
  );

And then, for each frame:

// render to multisample surface
m_pd3dDevice->SetRenderTarget(0, msaasurf);

m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );
m_pd3dDevice->BeginScene();

// render stuff here

m_pd3dDevice->EndScene();

m_pd3dDevice->SetRenderTarget(0, screen);

// get back buffer
IDirect3DSurface9* backBuffer = NULL;
m_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);

// copy rendertarget to backbuffer
m_pd3dDevice->StretchRect(msaasurf, NULL, backBuffer, NULL, D3DTEXF_NONE);
backBuffer->Release();

// Present the backbuffer contents to the display
m_pd3dDevice->Present(NULL, NULL, NULL, NULL);

However, nothing is appearing on my screen (all black). No errors are occuring (I check the return value of all d3d calls). What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2024-08-29 13:13:28

您不需要额外的表面,可以直接渲染到多重采样后缓冲区。对我来说,像这样使用 StretchRect() 的唯一原因是获取场景的非多重采样副本以用于后处理(因为多重采样渲染目标是不良纹理,因此您需要场景数据在已解析的纹理中)。如果您想这样做,则无需为后缓冲区指定多重采样。那么用于渲染场景的多重采样渲染目标就足够了。

You don't need the extra surface, you can render directly to the multisampled backbuffer. For me, the only reason to use StretchRect() like this is to get a non-multisampled copy of the scene for use with postprocessing (because multisampled render targets are bad textures, so you need the scene data in a resolved texture). If you want to do this, you don't need to specify multisampling for the backbuffer. A multisampled render target to render the scene to is sufficient then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文