Flecko1

Hi,

I'm wondering if LoadGraphicsContent(true) is ever called by XNA (rather than client code) except for when the game is first started. The reason I care is that if I have to reload, for example, a Texture that a few different objects are still using, I have to make sure that they use the new Texture reference rather than their old one, which is now dead.

Basically: am I guaranteed that LoadGraphicsContent(true) is never called except when the application first starts up and when I call it myself It appears that's how it's supposed to be, but from the documentation I'm not 100% sure.

Thanks,
Max


Re: XNA Framework When is LoadGraphicsContent(true) called?

Shawn Hargreaves - MSFT

This will be called whenever the graphics device needed to be totally recreated, rather than just reset. This can happen if you drag a windowed game to a different monitor on a multimon setup.





Re: XNA Framework When is LoadGraphicsContent(true) called?

Flecko1

Ok, thanks for your response. So to be clear...I've been using a scheme since MDX where I create a structure such as

struct TextureSupplier {
public Texture Texture;
}

So if I want to give a bunch of objects a Texture, I create a TextureSupplier in the Game constructor, assign the Texture to the TextureSupplier whenever LoadGraphicsContent(true) is called, and give the TextureSupplier to any objects that need the Texture. That way, they can be guaranteed that their reference to the Texture is live, even if the device is totally recreated - the TextureSupplier is basically just a really clunky pointer.

The problem is that this is inelegant and adds indirection to common operations while the program is running normally, even though it's only necessary to handle a rare case where performance is not a big issue. How do other people solve this Does anyone know a good way to provide a guarantee of live references to resources that offloads the extra cost to device recreation time




Re: XNA Framework When is LoadGraphicsContent(true) called?

Jon Watte

Yes, that is the way to do it. It is not necessarily inelegant (because you can hang other functionality on the TextureSupplier, if you want), but it is somewhat cumbersome. What's most annoying is that they could just have done this within the Texture2D implementation in XNA, so that device re-set and device re-creation didn't differ. As it is now, resets are handled by the runtime, but re-creates are handled by you.

In my non-managed code, I actually only create DEFAULT pool data, to save on system RAM, and then handle reset as a re-create. That leads to simpler code, and is robust even on drivers that have occasional bugs with reset.

What's even more annoying is that, when you assign textures to meshes, you assign Texture2D, not your TextureSupplier, so you also have to keep track of the relation of what meshes (or MeshSuppliers) use what textures (or TextureSuppliers).

It may be possible to go into the ContentManager and make it create your own Texturesubclass for Texture assets. You'd have to poke around among the type demarshallers to do that, though, and I don't think it exposes enough public/protected members to actually succed in that approach.






Re: XNA Framework When is LoadGraphicsContent(true) called?

Fluxtah

Is there an easy way to force the graphics device to be recreated, just for testing purposes.



Re: XNA Framework When is LoadGraphicsContent(true) called?

Nick Gravelyn

You can reset the device by minimizing the window and bringing it back up again.




Re: XNA Framework When is LoadGraphicsContent(true) called?

Fluxtah

Hi, thanks, but I did not ask how to reset the device, I asked how to recreate it.

The topic of this post was answered along the lines of - LoadAllGraphicsContent(true) is called when the device is re-created.

So I want to know how to force a recreate, not a reset, to force this behaviour.





Re: XNA Framework When is LoadGraphicsContent(true) called?

Flecko1

I haven't tried this so I don't know if it would work, but I suspect you could do this by changing the display the device is supposed to be on. As it says above, if you drag a game from one monitor to another in a multimonitor setup that would recreate the device, so perhaps even if you don't have that kind of hardware you could "move" the device to the monitor it's already on to trigger a recreate.




Re: XNA Framework When is LoadGraphicsContent(true) called?

Bill Reiss

If you switch to full screen mode and then alt-tab to switch to another process and back again the device should be recreated




Re: XNA Framework When is LoadGraphicsContent(true) called?

Fluxtah

If you switch to full screen mode and then alt-tab to switch to another process and back again the device should be recreated

I tried this method but was not sure if the device was recreated, I have a breakpoint inside LoadGraphicsContent, inside the if block where loadAllContent is evaluated, it enters the block on start up, but does not when I try to recreate the device using this method mentioned :(





Re: XNA Framework When is LoadGraphicsContent(true) called?

Bill Reiss

Sorry the alt-tab trick that used to work in Beta 2, seems like maybe that was a bug at the time.