I was wondering about texture optimization in XNA 4.0.

Will the the contentmanager send the texturedata to the GPU directly when the texture gets loaded or do I send the texture data to the GPU when I declare a texture in my shader.

If that's the case, what happens if I have 5 shaders all using the same texture, does that mean that I send 5 instances of that texture data to the gpu or am I simply telling the GPU what preloaded texture to use?

Or does XNA do the heavy lifting in the background?


1 Answer 1


Pretty much everything in XNA that you can call SetData on stores its data in GPU memory. That is: VertexBuffer, IndexBuffer, Texture2D, etc. Shaders themselves are also stored on the GPU.

They start existing on the GPU as soon as you create them (or ContentManager creates them for you.)

The GPU has "texture samplers". Conceptually these are pointers (references) to a texture in GPU memory. When you set GraphicsDevice.Textures[0] = myTexture (or something sets it for you, like SpriteBatch or BasicEffect), XNA sets sampler 0 on the GPU to point at the memory for "myTexture" (which is already on the GPU). This is so cheap that it's practically a free operation.

So, when you write this in your shader:

sampler myTextureSampler : register(s0);
tex2D(myTextureSampler, uv);

You are saying you want to load pixel data from whatever texture is pointed to by sampler 0.


You must log in to answer this question.

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