I am trying to define texture2d objects as shared. The reason of why I am trying to do is, I made an xna user control and my project loads unlimited (as much as the user wants) my XNA User Control. And all of the textures are same. I don't want to load textures for everyone of them (think how much ram size it will use if I do it)

The real problem is:

cmMaps = New ContentManager(Services, "Maps")

As you see, ContentManager news ServiceProvider for initialization. When I look at the codes where Services variant modified, I see this lines:

GraphicsDeviceService = GraphicsDeviceService.AddRef(Handle, ClientSize.Width, ClientSize.Height)
m_services.AddService(Of IGraphicsDeviceService)(GraphicsDeviceService)

Why I can't define global Texture2D objects. Why is it specific for the window or control that the XNA tries to draw on?

Is it possible to define em as shared variant and able to use it in every XNA UserControl?

Note: I took UserControl source codes from this page: http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1

  • \$\begingroup\$ I'm pretty sure XNA already caches Texture2D objects, and loading them twice doesn't consume double the RAM of loading it once. Did you try profiling this with, say, 100-1000 instances of your class? \$\endgroup\$
    – ashes999
    Aug 24, 2013 at 15:39

1 Answer 1


The ContentManager class has built in functionality to only load one instant of the texture2d to the GPU memory. After the first

Texture2d myTexture = myContentManager.Load<Texture2d>(myFileName); 

any other loading of myFileName through myContentManager will be a reference to the first, not a separate instance of the texture data.

so as long as all the different userControls are passed a reference to the same Services container... and that Services container is storing a ContentManager reference... and you use that ContentManager to load all your textures in any or all your userControls, then they will all share the same GPU instance of your texture.

  • \$\begingroup\$ Are you sure? The texture is associated to a graphicsdevice on creation... I don't know how affects this... but I think that different controls will have different graphicsdevice... I fear that it can be dangerous... \$\endgroup\$
    – Blau
    Aug 24, 2013 at 16:21
  • \$\begingroup\$ Yeah, my fear is same too. I will load 200+ usercontrol to my project. I wish Steve is right. \$\endgroup\$
    – turksauron
    Aug 24, 2013 at 17:14
  • \$\begingroup\$ In the winforms sample, the graphics device is a singleton. The second control that is created uses a reference to the graphics device that was created when the first control was made. But be careful, the first one sets the dimensions of the back buffer. So if the 2nd controls is bigger, you have to adjust the presentation params. \$\endgroup\$
    – Steve H
    Aug 24, 2013 at 21:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .