3D Modeling
From TargetWiki
Contents |
Minimizing State Changes is the Path to Good Performance
States, Passes and Techniques
opengl is a state machine renderer. Before some 3D geometry is rendered, 'state' needs to be set to tell opengl how to render it. The most important part of state as far as a 3D artist is concerned is:
- textures
- shader
- lighting (ie ambient, diffuse, emissive, specular colours, plus specular intensity, etc)
There are lots of other kinds of state as well that artists tend not to use those, but in tw2 they can, like:
- blending mode,
- face culling,
- depth testing,
- colour masking,
- alpha testing, etc.
State information gets bundled into a Pass.
Passes get bundled into a Technique, which are just text files.
The most efficient technique is a single pass so there are no state changes.
Multiple 3D objects can have the same exact technique, which means there is no state change. TW2 can detect this and realize efficiencies where the techniques are identical.
Each 3D object should only have one technique, otherwise it will be drawn in batches with state changes, thus decreasing performance.
Multipass rendering changes a lot of states with each pass, thus it is a performance killer. Avoid this if possible.
The best way to share textures is with texture atlasing . This is more efficient than multiple textures used by the a single 3D object, even for VRAM swapping if the video card has very little VRAM.
Transparent Objects are different.
In general, avoid transparency when you can. It is needed for some things, but keep in mind its effects on performance.
Transparent objects have many more state changes because to work correctly, they must be drawn 'far away' first, and 'close' last.
Try dividing objects with transparent elements into two separate parts, one with an opaque technique and one with a transparent technique. And use the same technique for each transparent part of the 3D Object
Minimizing the total number of unique techniques is the real goal.
References
IRC Log of the original conversation
This is a starting place to document various knowledge bits that 3D modelers and mod teams will need to know to develop and optimize their models for TW 2.
Getting the Best Performance From Your 3D Model
(Sylvan needed a tree object to test with)
- yak: i say give hm an 800 poly one, and see what he does with it.
- sylvan: for testing, i don't care. 500-5000. something like that. most importantly, just one material pass!
- not a bunch of different textures for different parts of the tree (that just murders performance)
- for planes it's not as important, since there aren't nearly as many of them
- but minimizing state change is still a good path to performance
Shared Texture Files... Bad? Texture Atlasing: Good
- yak: we have some buildings that use a common set of textures... is that "bad"?
- sylvan: a MUCH better way to do it is called texture atlasing where you have one big texture, and all your models reference it. but it needs to really be the same technique, not just the same texture. so in tw2, that means all the opengl state: shader, textures, and any other state you put in the technique
Transparency and Opacity Issues
- sylvan:another VERY important thing for performance is transparency
- transparent parts should be their own models with their own technique
- don't draw opaque stuff with a transparent technique because it's 'easier', that kills state sorting
- opaque stuff is sorted by state, transparent stuff is sorted by distance. stuff that's sorted by state draws MUCH faster
OpenGL States
(NB: TW2 uses OpenGL)
- sylvan: opengl is a state machine renderer
- before some 3D geometry is rendered, 'state' needs to be set to tell opengl how to render it
- the most important part of state as far as a 3D artist is concerned is:
- textures
- shader
- lighting (ie ambient, diffuse, emissive, specular colours, plus specular intensity, etc)
- there's lots of other kinds of state as well like
- blending mode,
- face culling,
- depth testing,
- colour masking,
- alpha testing, etc
- artists don't tend to use those, but in tw2 they can
- all that state gets bundled up in what's called a pass
- passes get bundled up into what's called a technique
Multipass Rendering is a Performance Killer
- tw2 can do multipass rendering, if it's required...but don't do it if you can possibly avoid it!
- it's a performance killer, since each pass is a bunch of state changing
OpenGL States and Technique Files in TW 2
- yak: when you say don't do it, how is the artist controlling that? Is it just whether or not they included a shader and lighting, etc?
- sylvan: no - they will actually write techniques. in text files. so...
- the most efficient technique has a single pass, so that there are no state changes while drawing an object
- similarly, if multiple 3D objects have the *same exact* technique (in terms of contents - tw2 can detect if a technique specified in file A is the same as a technique in file B), then there is NO state change
- that is:
- the technique is set (state change)
- object A is drawn
- object B is drawn - with no state change!
- this is VERY efficient. really, staggeringly efficient
- so if all your buildings use the same technique, but just use different parts of the One Big Ass Texture, then they can all be drawn, every building on screen, with no state changes
- yak: does every part of a plane (3d part) need it's own technique text file?
- sylvan: that's what you DON'T want.
- more than one technique per object means the object has to be drawn in batches, with state changes.
- use a single technique for the whole object.
Big Huge Shared Texture: Any Issues?
- yak: any issues of VRAM on vid cards and the size of the mega texture we'd use?
- sylvan: not really. having a single large texture [i.e., a texture atlas] is more efficient even for VRAM swapping if the card has very little VRAM
How to Handle Transparent Objects
- sylvan: now...this all applies to opaque objects only
- raff: like in trees?
- sylvan:yes, trees are opaque. transparent objects have to drawn 'far away' first and 'close' last, so that transparency works correctly
- that means transparent objects have a LOT more state changes
- so, if you have an object that is partially opaque and partially transparent, *sometimes* it's a win to separate it into two parts, one with an opaque technique and one with a transparent technique.
- but, in general, avoid transparency when you can.
- i know it's needed for some things, and that's ok. it won't ruin things, but it's something to keep in mind.
- yak: we're still going to use transparency on plane windows, no?
- sylvan: of course! use it where it's needed!
- don't take this as a dictum to not use transparency
- yak: so those parts, use a different techni... file?
- sylvan: well, it doesn't have to be a separate file, but yeah, a separate technique
- raff: yak: but you want to use the *same* technique for each transparent part of the plane... right sick?
- sylvan: raff: yes!
- sylvan: raff: minimizing the total number of unique techniques is the real goal
- and remember, if a technique is specified in two different places, but has the same contents, that's ok!
- tw2 will magically figure out it's really the same technique
- raff: it would be slick for any modeller to reference to a standard set of techniques... kinda like a library?
- because usually you would not need that many different ones
- sylvan: not really, raff, since a different texture makes a different technique
- but sharing shaders is a very good thing
