Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.