What is the purpose of the Warm() methods?

Dec 28, 2009 at 5:18 AM
Edited Dec 28, 2009 at 5:28 AM

I noticed several classes have a Warm(Application application, GraphicsDevice state) method. The documentation for Warm(...) says "Preload (warm) the resource before its first use" but I am not sure what this means. By looking at the code it seems like the Warm methods are being used as content loading methods. They also add a reference to the application, letting it know that there is a graphics resource declared. But why not use the LoadContent() methods instead then? (Also, I have noticed that some of the LoadContent() methods call Warm which in turn call LoadContent...). The only logical use I could find was to "warm" render targets before use because of the XNA bug that throws an error if u try to use a render target before it has been set as the current render target.


It would be awesome if someone could expand on what they are for and why they are necessary.


Dec 28, 2009 at 12:31 PM
Edited Dec 28, 2009 at 12:32 PM

Hi. You are half way there. You are right, XNA does have a number of bugs related to the valid points you initalise objects (such as render targets).
The way xen is built, any resource that has a Warm() method will only get created the first time it's used. This way, when you create a DrawTarget, or a vertex buffer, etc, then the actual graphics resource (under the hood) isn't created straight away.

So if you create a DrawTargetTexture and never use it (ie, Draw it), then the actual render target is never created. The resources aren't wasted.
However, the flip side of this, is that if you call GetTexture() on that draw target, without having drawn it first, then it will fail because the resource hasn't been created.

Which is where Warm comes in. A call to Warm will create the resources, if they haven't been created yet.

As you say, XNA is picky about where and how you can create these resources. In general, any time you have a DrawState (ie, you are in the main present loop) you usually can create a resource. So Warm() requires the DrawState is passed in, this restricts where and when you can call Warm() to only the times when it's valid to do so (with some very small exceptions).

Which is also where GetTexture(DrawState) comes in - this internally just calls Warm() before returning the texture.

The times when you'd call Warm() are either implicitly in the GetTexture case, or explicitly for the case when resources don't get used straight away - and you want to avoid a hitch or frame rate spike when they first are used. (ie, you'd call warm as part of LoadContent - to make sure the resources are created before starting the game)

Does that make sense? :-)

Dec 28, 2009 at 7:47 PM

Thanks, it makes perfect sense now.