DirectX 小型应用程序像地狱一样消耗资源
DirectX small application consuming resources like hell
我正在尝试学习一些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);
}
这将解决您的处理器问题。
相关文章:
- 具有瞬态资源的RAII类
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 允许从 std::map 的密钥窃取资源?
- 参考资源文件VC++中的$(SolutionDir)
- 如何维护资源管理器项目视图中当前可见的项目列表
- 将 std::allocate_shared 与多态资源分配器一起使用
- 使用RAII在给定次数的迭代后重新分配资源
- 资源管理设计模式
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 如何跨平台将二进制资源构建到程序中?
- 为什么在C++中对链表这样做?(像堆叠一样处理它们)
- SetDlgItemInt 不会更改嵌入资源的编辑框
- 堆栈和队列是否像C++中的数组一样传递?
- Klocwork Inside的资源泄漏
- 从存储为 Windows 资源 (c++) 的 png 中获取 png 文件数据
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- C++链接时间资源"allocation"而不定义
- 使用 Bazel 生成 QT 资源文件
- DirectX 小型应用程序像地狱一样消耗资源
- 如何以编程方式发送电子邮件,就像在 Windows 资源管理器中"Send To Mail Recipient"一样?