Resizing buffers -> Out of memory

Started by
13 comments, last by backstep 9 years, 9 months ago

Hello

After the loss of the d3d11 path in my engine i am currently reimplementing all the features.Right now im facing some issues when resizing my buffers which i never encountered before. The code im using for resize events (on WM_SIZE) is:


	void GxDeviceD3D11::onResize(uint32 w, uint32 h) {
		if (!mIsInitialized) {
			return;
		}

		mTargetHeight = h;
		mTargetWidth = w;
		mViewport.Height = static_cast<float>(mTargetHeight);
		mViewport.Width = static_cast<float>(mTargetWidth);

		mContext->OMSetRenderTargets(0, nullptr, nullptr);

		mMainRenderTargetView->Release();
		mMainDepthBufferView->Release();

		assert(SUCCEEDED(mSwapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0)));

		ID3D11Texture2D* backBuffer = nullptr;
		assert(SUCCEEDED(mSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer))));

		assert(SUCCEEDED(mDevice->CreateRenderTargetView(backBuffer, nullptr, &mMainRenderTargetView)));

		backBuffer->Release();

		mDepthBufferDesc.Height = mTargetHeight;
		mDepthBufferDesc.Width = mTargetWidth;

		mMainDepthTexture->Release();
		assert(SUCCEEDED(mDevice->CreateTexture2D(&mDepthBufferDesc, nullptr, &mMainDepthTexture)));

		memset(&mDepthViewDesc, 0, sizeof(mDepthViewDesc));

		mDepthViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
		mDepthViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;

		assert(SUCCEEDED(mDevice->CreateDepthStencilView(mMainDepthTexture, &mDepthViewDesc, &mMainDepthBufferView)));

		mContext->OMSetRenderTargets(1, &mMainRenderTargetView, mMainDepthBufferView);

		mContext->RSSetViewports(1, &mViewport);
	}

And the error i get:


D3D11 ERROR: ID3D11Device::RemoveDevice: Device removal has been triggered for the following reason (E_OUTOFMEMORY: The application tried to use more adapter memory than the Device can simultaneously accommodate. As a result, the TDR (Timeout Detection and Recovery) mechanism has been triggered. The current Device Context was executing commands when exhaustion occurred. The application needs to make less aggressive use of the display memory, perhaps by leveraging ClearState to ensure large Resources do not stay bound to the pipeline, or by leveraging information, such as DXGI_ADAPTER_DESC::DedicatedVideoMemory.). [ EXECUTION ERROR #378: DEVICE_REMOVAL_PROCESS_AT_FAULT]

Neither is the windows size very big nor are the values wrong.

If i comment out the ResizeBuffers and let the depth buffer its old size everything works fine, but as soon as im using ResizeBuffers and resize the window (when i let go of the window) it first halts for a while and then prints the error. If i only resize it a little it freezes for a second or two and then continues, but if i resize it more it freezes longer until i get the error message stated above. What could be the reason for that?

Greetings

Plerion

Advertisement

I can't speak to why you're getting the error. I suspect it is related to releasing/resizing/etc. hundreds of times. If you call OnResize for every WM_SIZE message, that can be hundreds of times for a single frame resize drag. For small resizes, you're getting maybe only 10 or 15 calls, and you recover. For longer drags, maybe 100s of WM_SIZE messages. You only need to resize the buffers when:

a. You maximize the window (a single WM_SIZE message)

b. You restore the window from a maximized state (a single WM_SIZE message)

c. A WM_EXITSIZEMOVE message is received. That's sent only after the drag is complete. Here "drag" means either a drag-resize, or completion of moving the entire window via the title bar.

Try changing your message procedure something like this to see if it relieves the condition:



LRESULT YourEngine::MsgProc(...)
{
   static bool wasMaximized = false; // ASSUMING you don't start maximized!
   ...
    case WM_SIZE: // resize only if maximized, or restored from maximized - no WM_EXITSIZEMOVE sent in those cases
        if (wParam == SIZE_MAXIMIZED)
        {
            OnResize(...);
            wasMaximized = true;
        }
        else
        {
            if (wasMaximized) OnResize(...);
            wasMaximized = false;
        }
        return DefWindowProc(hWnd, message, wParam, lParam); // Windows may still need to play with the frame
    case WM_EXITSIZEMOVE: // the drag is complete.
        OnResize(...);
        break; // or return 0;

For a very minor efficiency, your OnResize should check if the window was, indeed, resized. I.e., the client rect size differs from the backbuffer size. If you move the window with a title bar drag, no need to resize.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

My concern is that not using WM_SIZE to constantly resize the window during drags will hide the underlying issue, which is most likely a resource leak.
He should switch to WM_EXITSIZEMOVE eventually, but the actual bug will still be there. It will just take more and more window resizes to trigger it.

I am currently using WM_SIZE for all of my sizing basically as a method to prove my system, and I can say that if he is not leaking resources it won’t cause any problems, even if dragging the window corner around causes literally thousands of back-buffer resizes.

Basically, Plerion, you have a resource leak, and you need to fix it.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

L. Spiro's correct. Responding to every WM_SIZE should not cause problems. The code I posted may (temporarily) relieve the symptoms, it won't cure the disease.

If you have a resource leak, you should be able to gain more info by creating a debug device [ hr = g_pd3dDevice->QueryInterface(IID_PPV_ARGS(&pDebug)); ] and call ReportLiveDeviceObjects(D3D11_RLDO_DETAIL) just before a resize move starts (WM_ENTERSIZEMOVE) and just after it completes (WM_EXITSIZEMOVE). "Before" and "after" objects should be the same. Those calls will provide the type of objects which will help point in the right direction.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I don't respond directly to WM_SIZE. What I do instead is just set a flag that indicates "hey, on the next frame we're drawing we'd really like to change the display mode/resize buffers/etc". Then when the next frame runs I actually do the work and then clear the flag. That way I can selectively suspend drawing without getting unwanted WM_SIZE actions, and I'm absolutely certain that only the most recent WM_SIZE is going to have any effect.

Regarding the resource leak, a possible cause is that D3D11 holds references for all Set commands, so before you resize it's not a bad idea to issue a ID3D11DeviceContext::ClearState call. (This is also a great way of confirming that your state change handling is sufficiently robust.)

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Hello all

Thanks for your replies. Ive checked again, nothing i create in the resize function is not released. As the error also appeared when all i had in the application was a texture, a pixel shader, a vertex shader, a 4 element vertex buffer and a 6 element index buffer i wasnt sure where to look for the memory leak (ClearState didnt change anything). As i understood resizing in d3d11 i dont have to release and recreate those like in d3d9.

Now i checked mhagains first suggestion and changed onResize to:


void onResize(uint32 w, uint32 h) { mTargetHeight = h; mTargetWidth = w; mResizeRequested = true; }

and added a new function doResize which mainly does what has been in onResize before and after Present i call doResize. Right now the problem is gone, tho im now a bit worried if i just "covered" the real problem.

Greetings

Plerion


before and after Present i call doResize

Why?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

That was a typo, sorry. After Present i call doResize.

I have also double checked, the shader resource view from the texture gets released.


After Present i call doResize.

Why? Do you really need to change the buffer size (or check if it needs to be changed) every frame?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

never mind

This topic is closed to new replies.

Advertisement