Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[SlimDX] Resource managment quesion


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
5 replies to this topic

#1 user88   Members   -  Reputation: 268

Like
0Likes
Like

Posted 16 May 2012 - 09:07 AM

Hi everyone,

please examine a code snippet below:
var form = new Form();
form.Width = 640;
form.Height = 480;
var pp = new PresentParameters();
pp.DeviceWindowHandle = form.Handle;
pp.BackBufferWidth = form.Width;
pp.BackBufferHeight = form.Height;
		  
var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, pp);
var swapChain = new SwapChain(device, pp);
Surface surface = swapChain.GetBackBuffer(0);
swapChain.Dispose();
Why the surface resource is still alive even after disposing swap chain? My expectation is that the surface is a part of the swapChain and it should be disposed after the swap chain has been disposed..

Is this pseudo code correct for case with multiple swap chains, i mean is it normal situation when the back buffer surface disposes in each frame?
Device m_device;
void RenderFrame(SwapChain swapChain)
{
	Surface surface = swapChain.GetBackBuffer(0);
	m_device.SetRenderTarget(0, surface);
		  
	// Render 3D scene
	surface.Dispose();
}


Sponsor:

#2 DJTN   Members   -  Reputation: 207

Like
0Likes
Like

Posted 16 May 2012 - 09:49 AM

You can use PIX to determine what is happening. I've seen this before but with GDNet. Microsoft says you have to switch to windowed mode before disposing of the swap chain. SetFullscreenState(false, NULL);

The only other thing I can think of is a garbage collection issue.

Run it through PIX to narrow down the problem.

#3 Washu   Senior Moderators   -  Reputation: 5359

Like
0Likes
Like

Posted 16 May 2012 - 11:07 AM

In your first code dump you acquire the Surface (Surface surface = swapChain.GetBackBuffer(0);) but don't dispose of it. In COM, whenever you acquire ownership of a handle to an object the reference count is incremented. Now, SlimDX does some trickery in the background to make this easier for you to manage, but in general if you call "GetSomething" in slimdx then you should call "Dispose" on the reference returned when you're done with it.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#4 user88   Members   -  Reputation: 268

Like
0Likes
Like

Posted 17 May 2012 - 01:44 AM

Hello, thanks for answers.

I made some experiment in PIX. My test console application is:
static void Main(string[] args)
{
	var mainForm = new Form();
	mainForm.Width = 640;
	mainForm.Height = 480;
	mainForm.Show();
	var pp = new PresentParameters();
	pp.Windowed = true;
	pp.DeviceWindowHandle = mainForm.Handle;
	pp.BackBufferWidth = mainForm.Width;
	pp.BackBufferHeight = mainForm.Height;
	var direct3D = new Direct3D();
	var device = new Device(direct3D, 0, DeviceType.Hardware, mainForm.Handle, CreateFlags.HardwareVertexProcessing, pp);
	SwapChain swapChain = device.GetSwapChain(0);
	for (int i = 0; i < 3; i++)
	{
		RenderFrame(device, swapChain);
	}
	swapChain.Dispose();
	device.Dispose();
	direct3D.Dispose();
}
private static void RenderFrame(Device device, SwapChain swapChain)
{
	Surface surface = swapChain.GetBackBuffer(0);
	device.SetRenderTarget(0, surface);
	device.BeginScene();
	device.Clear(ClearFlags.Target, new Color4(1f, 0f, 0f), 0, 1);
	device.EndScene();
	surface.Dispose();
	swapChain.Present(Present.None);
}

and i have got next calls in PIX
24 Frame 2 31695410 2 3027324   330.3
25 <0x08CB65A8> IDirect3DSwapChain9::GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, 0x0019ED3C --> 0x08CB6610) 31706260	
26 <0x08CA95E0> IDirect3DDevice9::SetRenderTarget(0x00000000, 0x08CB6610) 31717988	
27 <0x08CA95E0> IDirect3DDevice9::BeginScene() 31722974	
28 <0x08CA95E0> IDirect3DDevice9::Clear(0x00000000, NULL, 0x00000001, D3DCOLOR_ARGB(0xff,0xff,0x00,0x00), 0.000f, 0x00000001) 31741448	
29 <0x08CA95E0> IDirect3DDevice9::EndScene() 31748778	
30 <0x08CB6610> IDirect3DSurface9::Release() 31753470	
31 <0x08CB65A8> IDirect3DSwapChain9::Present(NULL, NULL, NULL, NULL, 0x00000000) 34130700	

What is interesting that the resource <0x08CB6610> is still alive even after call
30 <0x08CB6610> IDirect3DSurface9::Release() 31753470
. So as i see surface.Dispose() actually doesn't free the surface.

#5 Washu   Senior Moderators   -  Reputation: 5359

Like
1Likes
Like

Posted 17 May 2012 - 02:56 AM

Dispose calls release. That does not mean that it will free the object, just that if the REFERENCE COUNT is 0 it will free the object.

The back buffer is used by the swap chain. The swap chain maintains a reference to the back buffer. This is just one of many possible references to the back buffer that might exist.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#6 user88   Members   -  Reputation: 268

Like
0Likes
Like

Posted 17 May 2012 - 03:26 AM

Thanks Washu,

your last post give an understanding of that. The questions to this topic are closed..




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