Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Resizing buffers -> Out of memory


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 Plerion   Members   -  Reputation: 368

Like
0Likes
Like

Posted 16 July 2014 - 12:13 PM

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



Sponsor:

#2 Buckeye   Crossbones+   -  Reputation: 6147

Like
0Likes
Like

Posted 16 July 2014 - 08:53 PM

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.


Edited by Buckeye, 16 July 2014 - 09:07 PM.

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


#3 L. Spiro   Crossbones+   -  Reputation: 14197

Like
4Likes
Like

Posted 17 July 2014 - 03:59 AM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#4 Buckeye   Crossbones+   -  Reputation: 6147

Like
3Likes
Like

Posted 17 July 2014 - 06:55 AM

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.


Edited by Buckeye, 17 July 2014 - 07:10 AM.

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


#5 mhagain   Crossbones+   -  Reputation: 8269

Like
4Likes
Like

Posted 17 July 2014 - 09:18 AM

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.)


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 Plerion   Members   -  Reputation: 368

Like
0Likes
Like

Posted 17 July 2014 - 09:48 AM

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



#7 Buckeye   Crossbones+   -  Reputation: 6147

Like
0Likes
Like

Posted 17 July 2014 - 02:27 PM


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.


#8 Plerion   Members   -  Reputation: 368

Like
0Likes
Like

Posted 17 July 2014 - 02:31 PM

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

 

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



#9 Buckeye   Crossbones+   -  Reputation: 6147

Like
0Likes
Like

Posted 17 July 2014 - 02:33 PM


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?


Edited by Buckeye, 17 July 2014 - 02:34 PM.

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


#10 Scott Bruno   Members   -  Reputation: 199

Like
0Likes
Like

Posted 17 July 2014 - 02:33 PM

never mind



#11 Plerion   Members   -  Reputation: 368

Like
0Likes
Like

Posted 17 July 2014 - 02:40 PM

 


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?

 

 

Ok, third attempt: After Present if mRequestResize is true i call doResize.



#12 L. Spiro   Crossbones+   -  Reputation: 14197

Like
0Likes
Like

Posted 17 July 2014 - 02:51 PM

So did you fix the memory leak or just hide it?
I would suggest actually fixing it, especially now while it is manageable. If you move on to the next task you will never know what bug is lying in wait underneath all your code. You can never trust your foundation, and thus you can never trust anything you make with it.

It will crash that one time you send it to a company when you apply for a job.
Fix it now. Top priority.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#13 Plerion   Members   -  Reputation: 368

Like
0Likes
Like

Posted 17 July 2014 - 03:22 PM

Hello L. Spiro

 

The leak was hidden, if i move back to instantly resizing it crashes again. I dont know what i can release more, i have the following:

-> release render target view

-> no reference to the Texture2D of the backbuffer is held

-> all shader resource views are released

-> depth buffer view is released

-> depth buffer texture is released

-> ClearState is called

 

Not released are:

-> 1 Vertex & 1 Pixel shader

-> the Texture2D object in the textures (the view is recreated after resize)

-> depth stencil states

-> blend states

-> rasterizer states

-> sampler states

-> 1 Vertex buffer

-> 1 Index buffer

-> 1 Input layout

 

Greetings

Plerion



#14 Plerion   Members   -  Reputation: 368

Like
0Likes
Like

Posted 17 July 2014 - 03:33 PM

A little updated:

Ive switched back to the one resize per frame approach and did a reportliveobjects at entersizemove and exitsizemove. This is what i get after resizing the window for over a minute (several thousand frames):

http://pastebin.com/Ekv4Wg54

 

The only thing odd are the 4500 references on the pixel shader. But the amount of objects seems reasonable.



#15 backstep   Members   -  Reputation: 336

Like
0Likes
Like

Posted 19 July 2014 - 01:22 PM

I had a similar issue with resizing my backbuffer on D3D11, perhaps its the same thing.  Does it only occur when you click and drag to resize your window?

 

Here's a little test to check if it's the same problem I had:

 

1. Open task manager in windows and go to the Performance tab, make you sure can see the current memory committed (e.g. 3.5/12GB or whatever it happens to be).

 

2. Run your app and click and hold to resize the window, slowly move the mouse around without releasing the button and watch the commit amount in task manager.  As you move the mouse around, the commit amount should steadily climb a few GB quite rapidly until your app crashes with E_OUTOFMEMORY.

 

3. Run your app again, this time click and hold to resize the window less than required to crash it, but enough to increase the commit, while watching the memory commit amount, then release the mouse button.  When you release the button the commit amount should drop back to where it began, roughly.

 

If it's the same thing let me know and i'll write a longer post about what was causing it for me.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS