Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / when is instancing better than cloning?

Author
Message
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Apr 2013 01:01 Edited at: 27th Apr 2013 01:02
I've been testing the use of instancing and cloning for objects like trees and was surprised to find that there seemed to be little to choose between them in terms of FPS. The same applied when I used a shader to render the trees. I was expecting instancing to give noticeably higher FPS than cloning - or is it simply a matter of memory usage and initial set up time?

Can anyone suggest guidelines for deciding when instancing is better than cloning?
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 27th Apr 2013 01:22
If the objects will not change instancing is better. If you need the objects to have different textures/shaders/animations then cloning is better.

I live for video games! (And beers, and football, and cars!)
See what I live for here: [url]http:\\www.TeamDefiant.co.uk[/url]
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Apr 2013 01:37
But why use instancing? I can't see a difference in performance.

Quote: "If you need the objects to have different textures/shaders/animations then cloning is better."


Better - or essential? [Actually not quite essential if you are prepared to use a complicated texture atlas/shader solution. ]
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 27th Apr 2013 01:51
Quote: "Given a scene that contains many objects that use the same geometry, you can draw many instances of that geometry at different orientations, sizes, colors, and so on with dramatically better performance by reducing the amount of data you need to supply to the renderer. - DirectX 9 MSDN.com"


That should be the difference, but if is not then something is going wrong or possibly the terms are out of date, perhaps the latest hardware simply renders cloned objects just as fast as instanced ones. An analogy of this with the latest hardware rendering 3D more quickly than 2D, which wasn't always the case; perhaps your test would not have been the same 3 years ago; but this is just a guess.

It could be the number of primitive draw calls that's behind the lag and not the meshes themselves.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Apr 2013 01:59
Yes, that used to be my understanding. I've tested a simple program on two machines and get virtually identical fps for instancing and cloning on each machine - although one machine is much faster of course.

Of course there could be a silly error in my code ...

This is what I used:

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 27th Apr 2013 03:00
From what I understand, instanced objects use far less memory.

TheComet


Level 91 Forumer - 9600 health, 666'666 keystroke power (*2 coffee)
Abilities: sophisticated troll, rage
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 27th Apr 2013 08:08
In DBPro the benefit of instancing over cloning is that it uses less memory. There is another technique possible in DX10+ I believe called hardware instancing which can increase performance, but that's not what DBPro instancing does.

[b]
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 27th Apr 2013 08:55
Doesn't it cut load times?

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 27th Apr 2013 08:58
That too since it doesn't have to make a copy.

[b]
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Apr 2013 12:20
@Diggsey

Thanks. That's the conclusion I was coming to and explains what I was seeing.

So, if memory usage and loading times aren't problems then I guess cloning is better since you have more control over individual objects.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 27th Apr 2013 13:48
So Microsofts DirectX 9 geometry instancing is not the same thing as DBP's instancing?

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Apr 2013 14:49
Sounds like it. Like you, I assumed it was till now.
Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 28th Apr 2013 06:16
Well it is to an extent, because it's sharing the same geometry data to render multiple objects in different places and orientations. Where normally to render two objects, you'd need two sets of the geometry data, you only need one in both definitions of "instancing" that you're talking about.

"everyone forgets a semi-colon sometimes." - Phaelax
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Apr 2013 13:13
I thought cloning made a second copy of the source object whereas instancing doesn't make a copy. Cloning is faster than loading a new object but uses the same amount of memory if I recall correctly unlike instancing which only uses the memory required by the source object (plus a bit of "housekeeping" stuff I guess).

I could be wrong though.
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 28th Apr 2013 13:56
Quote: "I thought cloning made a second copy of the source object whereas instancing doesn't make a copy."

Yes.

I live for video games! (And beers, and football, and cars!)
See what I live for here: [url]http:\\www.TeamDefiant.co.uk[/url]
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 28th Apr 2013 15:17 Edited at: 28th Apr 2013 15:20
Quote: "Cloning is faster than loading a new object but uses the same amount of memory if I recall correctly unlike instancing which only uses the memory required by the source object (plus a bit of "housekeeping" stuff I guess)."

Yes it's faster than loading a new object since you skip file I/O. If I have situations where I need a particular object loaded often (like characters). I create an object cache. What this means is I load a single copy of the object, and when ever that object is needed the pathname is checked and the object is cloned instead. It's pretty easy to setup using wrapper functions for the load object command. This allows you to create a simple system that tracks cached objects, and smartly converts load object into clone object where applicable without spaghetti code.

pcRaider
19
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 28th Apr 2013 15:23 Edited at: 28th Apr 2013 15:26






INSTANCE fps is good.
and LIMB is faster.

windowsXp
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 28th Apr 2013 15:29
You should change them so that the load time is measured and pasted to the screen with the frame rate.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Apr 2013 18:27
@pcRaider

That's very interesting and seems consistent with results I vaguely recall getting ages ago with DBPro spheres [just checked - yes I get the same result with spheres].

The results I'm getting now are with high poly trees made using Evolved's Tree It utility. There's nothing special about those (except they look nice ) so I wonder what the real issue is here? I get no difference between the three methods using the tree. Any idea why DBPro objects should be handled differently?

Here's the modified code I've just tested (the model is attached):



Option 1 takes a long time to load on my laptop but once the objects are loaded the fps is the same in each case.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd May 2013 21:26
A quick test using an animated boned object suggests instancing is much faster. This brings me back to the original question:

Quote: "Can anyone suggest guidelines for deciding when instancing is better than cloning?"


A clear definitive answer would be appreciated.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 4th May 2013 00:16
Quote: "A quick test using an animated boned object suggests instancing is much faster."


This is because by default the animation is done on the CPU, so with instanced objects that all share the same animated mesh you have to do only a small fraction of the work.

If you switch to use "fastbone.fx" instead I think you will find little difference between instanced or cloned objects since the work will be repeated for each instance on the GPU in both cases.

[b]
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th May 2013 00:46
But what about DBPro cubes and spheres? Why is instancing so much better than cloning in those cases compared to other static objects like trees?

What general rule can we rely on in order to decide whether to use cloning or instancing in any given case?
Aaron Miller
20
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 11th May 2013 03:02
Always use instancing until you are required to use cloning.

Instancing:
+ Less memory usage
+ Future updates may make instancing faster by ordering transformations properly
- Can't change the object's general appearance (easily)

Cloning:
+ Can change the object's general appearance and properties easily
- More memory usage
- Changes will introduce more state changes

I don't think DBPro takes advantage of DX9 instancing currently. Should there be an improvement, instancing will be better. If there isn't, and you're not seeing a performance increase, can you see a reason to use cloning if the geometry doesn't need to be changed? I don't. I see a bigger increase in memory usage than is actually necessary.

NOTE: This is going off of my assumptions. Am I wrong anywhere here?

Web - Tweets
“I'm going to punch DXGI in the face. Repeatedly.” ~Aras Pranckevicius
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th May 2013 13:11
Quote: "Always use instancing until you are required to use cloning."


I agree.
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 12th May 2013 16:20 Edited at: 12th May 2013 19:04
I haven't used instancing in the past due to hearing about its limitations. I guess I'll ask a few questions while we are on the subject. My perspective is that I am populating game worlds with hundreds of same or similar objects. Some are animated, some have texture cycling animation effects.

How do builtin dbp lights behave with instanced objects? Does each instance have light calculated and displayed, or is lighting calculated for the source object only.

How do shaders work on instanced objects? Can they have separate shaders. If the source objects shader applies to the instances, how would shader lighting work or would lighting be calculated based on the source objects position and look all wrong.

What happens when the source object is deleted? Assume I'm loading hundreds of instances in a completely automated fashion from level files created with a level editor. If I decide to make some of these instances destructible do I need to keep track of the source object in some convoluted management scheme. If deleting the source deletes all instances, then replacing objects with broken versions would delete half the level if I delete and replace the source object.

Can instances be created from referencing other instances? Or do I have to keep track of which one is the source here too.

Do instanced objects disappear when the source object is hidden?

Do instanced objects disappear when the source object is excluded?

Do instanced objects disappear when the source object is culled either by camera range or by being out of frame?



Thanks for any info. I like to work out in my head an overall code structure. Right now my level loading fully automates cloning and texture sharing. Btw texture sharing was huge and dropped the game from 1gb of ram to about 200-300mb and saved a ton of load time. Cloning cut level loading in about half. But a small speed boost would be nice if instancing isn't too annoying to use.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th May 2013 02:34
Quote: "How do builtin dbp lights behave with instanced objects? Does each instance have light calculated and displayed, or is lighting calculated for the source object only."


As far as I can tell, lighting uses the instance copy to calculate lighting.

Quote: "How do shaders work on instanced objects? Can they have separate shaders. If the source objects shader applies to the instances, how would shader lighting work or would lighting be calculated based on the source objects position and look all wrong."


The shader has to be applied to the source object as do any textures but uses the information about the copy for things like position and scaling and lighting.

Quote: "What happens when the source object is deleted? Assume I'm loading hundreds of instances in a completely automated fashion from level files created with a level editor. If I decide to make some of these instances destructible do I need to keep track of the source object in some convoluted management scheme. If deleting the source deletes all instances, then replacing objects with broken versions would delete half the level if I delete and replace the source object."


I believe the source object must exist since it's information is stored once and re-used. You should be able to write a simple test demo yourself.

Quote: "Can instances be created from referencing other instances? Or do I have to keep track of which one is the source here too."


I doubt you can use instances as source objects. Again, you could try a simple test.

Quote: "Do instanced objects disappear when the source object is hidden?"


No.

Quote: "Do instanced objects disappear when the source object is excluded?"


I think they do. You can certainly exclude the instances to speed up culling when necessary though.

Quote: "Do instanced objects disappear when the source object is culled either by camera range or by being out of frame?"


No. However I believe there's a problem with animations that's been reported somewhere.

Quote: "But a small speed boost would be nice if instancing isn't too annoying to use."


My experience suggests that instancing works fine for static objects like trees (and you can position, rotate and scale the instances separately which gives the impression of many different objects). However, except in the case of builtin DBPro objects, I haven't noticed a speed boost. There can be a big difference in memory requirements though. I've just tested a tree demo with about 16000 copies of trees - the cloned version crashed somewhere between 8000 - 16000 copies whereas the instanced version had no problems (apart from FPS of course).

Hope this helps.

Don't be afraid to do simple experiments - they can help a lot.
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 13th May 2013 03:43 Edited at: 13th May 2013 04:00
Quote: "Don't be afraid to do simple experiments - they can help a lot. "


Good advice. Although I'm not liking these details. Exclude Object limitations mean I'd have to setup a convoluted management system where I'd have to track all source objects with at least 1 instanced object. I'd have to have culling routines hide and not exclude them. Any objects with animation would have to be cloned and not instanced.

In some situations it would be a benefit. Potentially hundreds of instanced objects with merely one hidden source object not completely culled off screen. Then again I could end up with hundreds of hidden and not properly culled source objects on screen. It's been said that there is no frame rate advantage to instanced objects.

To me this seems like a potential savings in load time, that can at times hurt frame rate. With the addition of added code complexity.


Edit:I think this Instancing is best for procedurally generating grass, or some other real-time generated effect like dense particles.
It's faster loading than cloning so it does better live, and in these limited situations you can ensure that you don't end up with hundreds of un-cullable objects slowing the game.


Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th May 2013 12:03
Would you really want or need hundreds of source objects for static things like plants and rocks?

Quote: "To me this seems like a potential savings in load time"


Don't forget the memory savings you get with instanced objects.

Quote: "It's been said that there is no frame rate advantage to instanced objects."


Except for certain (all?) builtin DBPro objects for some as yet unexplained reason. [I'm still waiting for Diggsey to come back with an explanation for that one. Hint, hint. ]
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 14th May 2013 10:16 Edited at: 14th May 2013 11:05
Quote: "Would you really want or need hundreds of source objects for static things like plants and rocks?"

It's not just plants and rocks. Wall pieces of all sorts, furniture, signs, interactive objects, sky backdrop objects, architecture, debris, and a vast variety of other objects meant to detail the game world. Additionally, they all have varying textures.

A level may have any variety combination of these things. In my specific situation I'd end up with a ton of un-cullable objects dragging down on the FPS. I would save some memory and loading would be a bit faster. FPS is a bigger issue for me at the moment, despite the memory and loading speed gains being favorable.

The limiting factor here is that you essentially can't cull sourced objects. It's a deal breaker.



In a situation where you are using particles, you know its going to be one "source object" and a ton of instances. You also need to load them all while the gameplay is happening. So it makes sense to use instancing.

A lot of games have vast outdoor environments, where storing every blade of grass in a level file becomes prohibitive. So small vegetation like grass is generated real-time around the player. Instancing also works here for the same reason as particles.

Thanks for the info, I think that this will help with making more detailed effects.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th May 2013 13:16
Quote: "Additionally, they all have varying textures."


In those cases you probably need to use cloning.

I guess a mixture of the two is what's needed - i.e. use instancing where possible (and beneficial of course) and cloning otherwise.

For things like walls made from DBPro objects you might get the performance boost from instancing as well - but the texturing issue could kill that option.

Quote: "The limiting factor here is that you essentially can't cull sourced objects. It's a deal breaker."


You can still hide them though. Actually, I've just checked that and it seems you can exclude source objects without affecting the instances after all. I must have been thinking of an earlier, bugged, test of mine.
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 14th May 2013 22:37
Quote: "You can still hide them though. Actually, I've just checked that and it seems you can exclude source objects without affecting the instances after all. I must have been thinking of an earlier, bugged, test of mine."

Sold.

Login to post a reply

Server time is: 2026-07-07 19:35:57
Your offset time is: 2026-07-07 19:35:57