   [XNA] Dynamic Textures
  I'm currently trying to updated a texture each frame (only if certain conditions are met) as follows:Vector4[] textureArray = new Vector[width * height];texture.GetData(textureArray);// Manipulate Texture Datatexture.SetData(textureArray);However, when the call to SetData occurs I get the following error:"The operation was aborted. You may not modify a resource that has been set on a device, or after it has been used within a tiling bracket."I've tried this with texture as both a Texture2D and ResolveTexture2D object (someone suggested this as a fix), I've also tried using device.Textures[0] = null before calling this piece of code, but that doesn't work either.I want to eventually move this onto the GPU with render targets, but for now I want a working CPU solution as I have other things that need completing before moving this to the GPU.Thanks for any help in advance.   
  I had this issue myself a while ago. I ended up doing something similar to the recommendations here to maintain a working 'copy' of the texture in a Color structure to perform collision tests on and updates. I then used this in my SetData() call, thus avoiding the need to GetData().   ;  I saw that topic during my Google searching, however I get the exception when I call SetData, not GetData.Unless I missed something, their only advice was was "device.Textures[0] = null" for when SetData is causing problems. Please correct me if I'm wrong.GetData seems quite happy to grab the information from the texture even if I've just drawn with it.I know it's the SetData that is the problem, as commenting out the SetData line allows the code to run flawlessly (with the array updating each frame), however that obviously defeats the point as I need the information in a texture not an array.Although, you did mention in your post that you used a Color structure, is there any reason why I'd be getting problems with a Vector4 rather than Color? Essentially they are the same thing, right? Or is XNA doing something behind the scenes?   ;  Okay so that error can happen for one of two reasons:-You're in the middle of a predicated tiling bracket-The device is still using the TextureWe need to figure out which of these is the case.  The first we can figure out pretty easily, since it's dependent on the size and format of the render target you're rendering to.  What is the size, SurfaceFormat, and MultiSampleType?  The second is a little more difficult.  It's not as simple as setting Textures[0] to null, since you have to remember that commands you send to the GPU don't happen synchronously.  Instead the GPU typically runs a frame or two behind the CPU, and your commands will get queued up.  One way to try to get around this is to double or triple-buffer your writes by having multiple textures that you write to, and then rotate through them each frame.  The more ideal solution is to use a render target texture and do all of your modifications as GPU operations, but that's not always practical.   ;  I don't have my code in front of me right now, but from memory.texture in my example above is a R32F format Texture2D object, which as far as I know doesn't have the properties you listed above.This is then used in my vertex shader as part of a VTF operation in my draw code.I've already got this implemented in C++ and C# using a locked rectangle. From a previous question I asked here I thought the Get/SetData methods in XNA essential achieved the same thing.What I'm trying to achieve is toroidial updates of multiple (I'd say a max of 10) textures every frame. This is fairly simple by reading it out into an array. However I don't even know how I'd begin to do it within a pixel shader, would it even be efficient to do this on the GPU?I'll post the full manipulation code as soon as I can (about 2 hours) to hopefully make what I'm up to clearer.   ;  I've found a temporary solution (which is clearly the wrong one), which is to recreate my texture before setting data on it.IE.pClipmap[Level].ElevationMap = new Texture2D(device, TexSize, TexSize, 1, TextureUsage.None, SurfaceFormat.Vector4);  pClipmap[Level].ElevationMap.SetData<Vector4>(pClipBuffer, 0, pClipBuffer.Length, SetDataOptions.Discard);Obviously this is not the right way to do it, and it causes huge CPU overhead whilst running my code, but hopefully it should help in figuring out whats is wrong. I'm thinking it's based around what you were saying earlier with the GPU lagging behind the CPU and I really need to get this on the GPU ASAP to stop this.I've been doing some testing of my manipulation code, and it doesn't do anything it shouldn't. However I could still use some help in converting it to HLSL for use on the GPU.I understand the kind of variables I should be sending, but how do I deal with the loop and reading the 3x3 height array for each pixel?void FillClipBuffer(short Level, iVec2 Start, iVec2 End, iVec2 Dir)   {  // Start = upper left  // End = lower right  float po = (float)Math.Pow(2.0f, (float)Level);  int modTexSize = TexSize - 1;  // Texture Space : (j & modTexSize, k & modTexSize)   // World Space : ( pClipmap[Level].ClipShift.x + j * po, pClipmap[Level].ClipShift.y + k * po )  // TODO : Best choice ?!  // Pool Default + Dynamic + 1 Lock or  // Pool Managed + 4 Locks or  // Pool Default + Pool SysMem + 4 Locks + UpdateTexture ...  int mapWidth = pClipmap[Level].ElevationMap.Width;  int mapHeight = pClipmap[Level].ElevationMap.Height;  Vector4[] pClipBuffer = new Vector4[mapWidth * mapHeight];  pClipmap[Level].ElevationMap.GetData<Vector4>(pClipBuffer);  float[,] height = new float[3, 3];  // TODO: Optimize  // Split Y  int SY = Dir.X > 0 ? End.X : Start.X;  // First -  // Process blocks of 2x2 pixels  for (int k = Start.Y; k < End.Y; ++k, ++k) for (int j = SY; j < SY + TexSize; ++j, ++j) {// Get 3x3 height values for (int i = 0; i < 3; ++i)    for (int l = 0; l < 3; ++l)   height[i, l] = GetWorldHeight(  pClipmap[Level].ClipShift.X + ((j + l) * po),  pClipmap[Level].ClipShift.Y + (((Dir.Y > 0 ? 1 : 0) * po) * TexSize) + ((k + i) * po));// Current levelpClipBuffer[(k & modTexSize) * TexSize + (j & modTexSize)].X = height[0, 0];pClipBuffer[((k + 1) & modTexSize) * TexSize + (j & modTexSize)].X = height[1, 0];pClipBuffer[(k & modTexSize) * TexSize + ((j + 1) & modTexSize)].X = height[0, 1];pClipBuffer[((k + 1) & modTexSize) * TexSize + ((j + 1) & modTexSize)].X = height[1, 1];// Morphed values for transition between levels pClipBuffer[(k & modTexSize) * TexSize + (j & modTexSize)].Y = height[0, 0];pClipBuffer[((k + 1) & modTexSize) * TexSize + (j & modTexSize)].Y = avg(height[0, 0], height[2, 0]);pClipBuffer[(k & modTexSize) * TexSize + ((j + 1) & modTexSize)].Y = avg(height[0, 0], height[0, 2]);pClipBuffer[((k + 1) & modTexSize) * TexSize + ((j + 1) & modTexSize)].Y = avg(height[0, 0], height[2, 2]); }  // Second |  // Prevent overwriting -  int SXb = Dir.Y > 0 ? End.Y : Start.Y + (End.Y - Start.Y);  int SXe = Dir.Y > 0 ? End.Y - (End.Y - Start.Y) : Start.Y;  for (int k = SXb; k < SXe + TexSize; ++k, ++k) for (int j = Start.X; j < End.X; ++j, ++j) {for (int i = 0; i < 3; ++i)    for (int l = 0; l < 3; ++l)   height[i, l] = GetWorldHeight(  pClipmap[Level].ClipShift.X + (((Dir.X > 0 ? 1 : 0) * po) * TexSize) + ((j + l) * po),  pClipmap[Level].ClipShift.Y + ((k + i) * po));// Current levelpClipBuffer[(k & modTexSize) * TexSize + (j & modTexSize)].X = height[0, 0];pClipBuffer[((k + 1) & modTexSize) * TexSize + (j & modTexSize)].X = height[1, 0];pClipBuffer[(k & modTexSize) * TexSize + ((j + 1) & modTexSize)].X = height[0, 1];pClipBuffer[((k + 1) & modTexSize) * TexSize + ((j + 1) & modTexSize)].X = height[1, 1];// Morphed values for transition between levelspClipBuffer[(k & modTexSize) * TexSize + (j & modTexSize)].Y = height[0, 0];pClipBuffer[((k + 1) & modTexSize) * TexSize + (j & modTexSize)].Y = avg(height[0, 0], height[2, 0]);pClipBuffer[(k & modTexSize) * TexSize + ((j + 1) & modTexSize)].Y = avg(height[0, 0], height[0, 2]);pClipBuffer[((k + 1) & modTexSize) * TexSize + ((j + 1) & modTexSize)].Y = avg(height[0, 0], height[2, 2]); }  pClipmap[Level].ElevationMap.SetData<Vector4>(pClipBuffer, 0, pClipBuffer.Length, SetDataOptions.Discard);   };  I've been optomising other parts of my draw code and it seems that recreating the textures each frame is not a massive overhead. However, it just doesn't sit right with me, it's clearly the wrong way to go about this.Is there no way to sync up my SetData calls with the GPU? I'd imagine not, but worth an ask. Why doesn't it work the same way as using a locked rectangle in DirectX?