0 out of 1 rated this helpful - Rate this topic

Texture2D.SetData Generic Method (Int32, Nullable<Rectangle>, T[], Int32, Int32, SetDataOptions)

Copies array data to the texture.

Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

public void SetData<T> (
         int level,
         Nullable<Rectangle> rect,
         T[] data,
         int startIndex,
         int elementCount,
         SetDataOptions options
) where T : ValueType

Type Parameters

T
The type of data to set.

Parameters

level
The mipmap level where the data will be placed.
rect
The section of the texture where the data will be placed. null indicates the data will be copied over the entire texture.
data
The array of data to copy. If rect is null, the number of elements in the array must be equal to the size of the texture, which is Width × Height; otherwise, the number of elements in the array should equal the size of the rectangle specified.
startIndex
The index of the element in the array at which to start copying.
elementCount
The number of elements to copy.
options
Option that specifies whether existing data in the buffer will be kept after this operation.
Exception typeCondition
InvalidOperationException The size of the data passed in is too large or too small for this resource.
An InvalidOperationException is thrown if an attempt is made to modify (for example, calls to the SetData method) a resource that is currently set on a graphics device.
Bb198829.caution(en-us,XNAGameStudio.31).gifCaution

Here is a tip for rendering objects within the Draw method of an Xbox 360 game. Do not use SetData when writing data to vertex buffers, index buffers, and textures. This method may lead to graphics corruption or crashes.

This is because, in cases where the size of the back buffer and depth-stencil buffer exceed the size of the Xbox 360 10 MB of embedded memory (EDRAM), predicated tiling is utilized on this platform to compensate for the additional memory requirements. Predicated tiling is a process by which scene rendering is performed multiple times on subsections of the final render target dimensions.

When predicated tiling has been triggered, the drawing commands contained in the Draw function are not submitted until Present is called. (Note that Draw implicitly calls Present at the end of this method.) In this case, these resources are not available for modification until the GPU is finished with presenting the entire frame.

This example demonstrates the use of the rect argument to set a single pixel in a texture.

texture2D.SetData<Color>(
    0,
    new Rectangle(randomX, randomY, 1, 1),
    new Color[] { Color.White },
    0,
    1,
    SetDataOptions.None
    );
Xbox 360, Windows XP SP2, Windows Vista, Zune
Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.