2

I need to communicate some constantly changing data to my pixel shader. I have a texture2d that I am passing to my pixel shader via a texture parameter. Before I call the shader I need to update the data in the texture.

        emittingPositions.SetData(emittingPositionsBuffer); //Set the data on the texture
        animationEffect.Parameters["emittersMap"].SetValue(emittingPositions); //Tell the shader about the texture data
        //go on to do the actual drawing calls to use the pixel shader

The problem is that when I do this I get an exception:

"You may not call SetData on a resource while it is actively set on the GraphicsDevice. Unset it from the device before calling SetData."

How do I "unset it from the device"? Or should I be taking a different approach here?

Mr Bell
  • 9,228
  • 18
  • 84
  • 134

1 Answers1

5

The first texture is set in the GraphicsDevice.Textures array with the index 0.

so you have to do this:

 GraphicsDevice.Textures[0] = null;
Blau
  • 5,742
  • 1
  • 18
  • 27
  • How do you know you're unsetting the right texture? What if I pass multiple textures to the graphics device via SetData -- how do I know which index I need to use to unset a specific texture? – BrainSlugs83 May 03 '14 at 23:16
  • That's really weird -- upon further experimentation -- Texture 0 is all that ever needs to be set to null -- regardless of which texture you actually need to update. Why is that? – BrainSlugs83 May 03 '14 at 23:24
  • 1
    Of course it depends on the shader you are using, the default xna provided shader is BasicEffect shader, and it uses only one texture in the register s0, that is the first texture. You can create your own shaders and use other registers (s1, s2, ...) for your textures to avoid collisions – Blau May 04 '14 at 01:12
  • I'm using my own shaders with multiple `extern Texture2D`s, and multiple `extern sampler2D`s; regardless of which texture I use, setting `Textures[0] = null;` makes it so I can `SetData` on any of them (otherwise, I get the exception noted above if I call `SetData` on any of them) -- I guess Textures is a collection of collections? Why is there more than one textures collection? And how do you choose which collection your textures go into? Where can I learn more about this? – BrainSlugs83 May 04 '14 at 03:30