DirectX 小型应用程序像地狱一样消耗资源

DirectX small application consuming resources like hell

本文关键字:一样 资源 应用程序 小型 地狱 DirectX      更新时间:2023-10-16

我正在尝试学习一些DirectX API,目前,我只有一个WINAPI窗口和一个简单的渲染函数,可以在整个屏幕上显示位图。我的赢机功能:

LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device
// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
                    int nCmdShow) {
     hInst = hInstance;
     // Initialize the window
     if (!initWindow(hInstance)) return false;
     // called after creating the window
     if (!initDirect3D()) return false;
     // main message loop:
     MSG msg;
     ZeroMemory( &msg, sizeof( msg ) );
     while( msg.message != WM_QUIT) {
         if( PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
             TranslateMessage ( &msg );
             DispatchMessage ( &msg );
         } else {
             render();
         }
     }
     // Release the device and the Direct3D object
     if( pd3dDevice != NULL ) pd3dDevice->Release( );
     if( pD3D != NULL ) pD3D->Release( );
     return (int) msg.wParam;
}

这是我的渲染函数:

void render(void) {
    IDirect3DSurface9* surface;
    pd3dDevice->CreateOffscreenPlainSurface(640, // the width of the surface to create
        480, // the height of the surface to create
        D3DFMT_X8R8G8B8, // the surface format
        D3DPOOL_DEFAULT, // the memory pool to use
        &surface, // holds the resulting surface
        NULL); // reserved
    D3DXLoadSurfaceFromFile(surface, NULL, NULL, L"test.bmp", NULL, D3DX_DEFAULT, 0, NULL);
    // This will hold the back buffer
    IDirect3DSurface9* backbuffer = NULL;
    // Check to make sure you have a valid Direct3D device
    if( NULL == pd3dDevice ) return;// Clear the back buffer to a blue color
    pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
    // Get the back buffer
    pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer );
    // Copy the offscreen surface to the back buffer
    // Note the use of NULL values for the source and destination RECTs
    // This ensures a copy of the entire surface to the back buffer
    pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE );
    // Present the back buffer contents to the display
    pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

因此,主要问题是当启用(取消注释)渲染功能时,应用程序使用的内存在一秒钟内变为 400 - 600 Mb。现在,如果我禁用(评论)WinMain 的行,内存将保留在 5 Mb 中,但处理器会发疯,应用程序使用大约 50% 的内存。因此,看起来WinMain()使用大量处理器并render()大量内存。为什么?我忘记了什么?

谢谢!

pd3dDevice->CreateOffscreenPlainSurface(640, // the width of the surface to create
        480, // the height of the surface to create
        D3DFMT_X8R8G8B8, // the surface format
        D3DPOOL_DEFAULT, // the memory pool to use
        &surface, // holds the resulting surface
        NULL); // reserved
    D3DXLoadSurfaceFromFile(surface, NULL, NULL, L"test.bmp", NULL, D3DX_DEFAULT, 0, NULL);

调用这段创建资源的代码是无数次的,但你没有发布它。它在渲染函数中,必须每秒至少调用 60 次(如果它缺乏与垂直回溯的同步,它可以每秒调用数千次,这给您带来了巨大的问题)。这意味着您将指向曲面的指针更改为具有相同图像的新曲面内存块,并丢失旧(未释放)的地址。因此,它会导致内存泄漏。

将该代码转移到应用程序的初始化函数中,而不是在呈现函数中。(并确保你管理它!停止使用它时,调用 release 函数以降低 COM 对象的引用计数,以便系统可以声明内存(再次可用))

编辑:这也需要移动到应用程序的初始化,这里只需要一次

IDirect3DSurface9* backbuffer = NULL;
    // Check to make sure you have a valid Direct3D device
    if( NULL == pd3dDevice ) return;// Clear the back buffer to a blue color
    pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
    // Get the back buffer
    pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer );
    // Copy the offscreen surface to the back buffer
    // Note the use of NULL values for the source and destination RECTs
    // This ensures a copy of the entire surface to the back buffer
    pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE );
    // Present the back buffer contents to the display

处理器使用率为 50% 主要是由于频繁调用 while 循环无限次。

前任。

while(TRUE)
{
}

这将经常检查,因为您指示处理器在条件为真时集中精力。 如果您使用的是 4 核处理器,其中一个处理器将完全专注于此过程,就我而言,我使用了 4 核,因此我的 CPU 使用率为 25%。 我认为您使用的是 2 核处理器。

while(TRUE)
{
Sleep(1);
}

这将解决您的处理器问题。