Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Texture2D.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2012 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 
24 using System;
25 using System.IO;
26 
27 using SiliconStudio.Core.Serialization.Converters;
28 using SiliconStudio.Paradox.Games;
29 using SiliconStudio.Core;
30 
31 namespace SiliconStudio.Paradox.Graphics
32 {
33  /// <summary>
34  /// A Texture 2D frontend to <see cref="SharpDX.Direct3D11.Texture2D"/>.
35  /// </summary>
36  [DataConverter(AutoGenerate = false, ContentReference = true, DataType = false)]
37  public partial class Texture2D : Texture2DBase
38  {
39  protected Texture2D()
40  {
41  }
42 
43  /// <summary>
44  /// Makes a copy of this texture.
45  /// </summary>
46  /// <remarks>
47  /// This method doesn't copy the content of the texture.
48  /// </remarks>
49  /// <returns>
50  /// A copy of this texture.
51  /// </returns>
52  public override Texture Clone()
53  {
54  return new Texture2D(GraphicsDevice, GetCloneableDescription());
55  }
56 
57  /// <summary>
58  /// Return an equivalent staging texture CPU read-writable from this instance.
59  /// </summary>
60  /// <returns></returns>
61  public override Texture ToStaging()
62  {
63  return new Texture2D(this.GraphicsDevice, this.Description.ToStagingDescription());
64  }
65 
66  /// <summary>
67  /// Creates a new texture from a <see cref="Direct3D11.Texture2D"/>.
68  /// </summary>
69  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
70  /// <param name="texture">The native texture <see cref="Direct3D11.Texture2D"/>.</param>
71  /// <returns>
72  /// A new instance of <see cref="Texture2D"/> class.
73  /// </returns>
74  public static Texture2D New(GraphicsDevice device, Texture2D texture)
75  {
76  return new Texture2D(device, texture);
77  }
78 
79  /// <summary>
80  /// Creates a new texture from a <see cref="Direct3D11.Texture2D"/>.
81  /// </summary>
82  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
83  /// <param name="texture">The native texture <see cref="Direct3D11.Texture2D"/>.</param>
84  /// <returns>
85  /// A new instance of <see cref="Texture2D"/> class.
86  /// </returns>
87  public static Texture2D New(GraphicsDevice device, TextureDescription textureDescription)
88  {
89  return new Texture2D(device, textureDescription);
90  }
91 
92  /// <summary>
93  /// Creates a new <see cref="Texture2D" /> with a single mipmap.
94  /// </summary>
95  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
96  /// <param name="width">The width.</param>
97  /// <param name="height">The height.</param>
98  /// <param name="format">Describes the format to use.</param>
99  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
100  /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
101  /// <param name="usage">The usage.</param>
102  /// <returns>A new instance of <see cref="Texture2D" /> class.</returns>
103  public static Texture2D New(GraphicsDevice device, int width, int height, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
104  {
105  return New(device, width, height, false, format, textureFlags, arraySize, usage);
106  }
107 
108  /// <summary>
109  /// Creates a new <see cref="Texture2D" />.
110  /// </summary>
111  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
112  /// <param name="width">The width.</param>
113  /// <param name="height">The height.</param>
114  /// <param name="format">Describes the format to use.</param>
115  /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param>
116  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
117  /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
118  /// <param name="usage">The usage.</param>
119  /// <returns>A new instance of <see cref="Texture2D" /> class.</returns>
120  public static Texture2D New(GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
121  {
122  return new Texture2D(device, NewDescription(width, height, format, textureFlags, mipCount, arraySize, usage));
123  }
124 
125  /// <summary>
126  /// Creates a new <see cref="Texture2D" /> with a single level of mipmap.
127  /// </summary>
128  /// <typeparam name="T">Type of the pixel data to upload to the texture.</typeparam>
129  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
130  /// <param name="width">The width.</param>
131  /// <param name="height">The height.</param>
132  /// <param name="format">Describes the format to use.</param>
133  /// <param name="usage">The usage.</param>
134  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
135  /// <param name="textureData">The texture data for a single mipmap and a single array slice. See remarks</param>
136  /// <returns>A new instance of <see cref="Texture2D" /> class.</returns>
137  /// <remarks>
138  /// Each value in textureData is a pixel in the destination texture.
139  /// </remarks>
140  public unsafe static Texture2D New<T>(GraphicsDevice device, int width, int height, PixelFormat format, T[] textureData, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable) where T : struct
141  {
142  return New(device, width, height, 1, format, new []{ GetDataBox(format, width, height, 1, textureData, (IntPtr)Interop.Fixed(textureData)) }, textureFlags, 1, usage);
143  }
144 
145  /// <summary>
146  /// Creates a new <see cref="Texture2D" />.
147  /// </summary>
148  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
149  /// <param name="width">The width.</param>
150  /// <param name="height">The height.</param>
151  /// <param name="format">Describes the format to use.</param>
152  /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param>
153  /// <param name="textureData">Texture datas through an array of <see cref="DataBox"/> </param>
154  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
155  /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
156  /// <param name="usage">The usage.</param>
157  /// <returns>A new instance of <see cref="Texture2D" /> class.</returns>
158  public static Texture2D New(GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, DataBox[] textureData, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
159  {
160  return new Texture2D(device, NewDescription(width, height, format, textureFlags, mipCount, arraySize, usage), textureData);
161  }
162 
163  /// <summary>
164  /// Creates a new <see cref="Texture2D" /> directly from an <see cref="Image"/>.
165  /// </summary>
166  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
167  /// <param name="image">An image in CPU memory.</param>
168  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
169  /// <param name="usage">The usage.</param>
170  /// <returns>A new instance of <see cref="Texture2D" /> class.</returns>
171  public static new Texture2D New(GraphicsDevice device, Image image, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
172  {
173  if (image == null) throw new ArgumentNullException("image");
174  if (image.Description.Dimension != TextureDimension.Texture2D)
175  throw new ArgumentException("Invalid image. Must be 2D", "image");
176 
177  return new Texture2D(device, CreateTextureDescriptionFromImage(image, textureFlags, usage), image.ToDataBox());
178  }
179 
180  /// <summary>
181  /// Loads a 2D texture from a stream.
182  /// </summary>
183  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
184  /// <param name="stream">The stream to load the texture from.</param>
185  /// <param name="textureFlags">True to load the texture with unordered access enabled. Default is false.</param>
186  /// <param name="usage">Usage of the resource. Default is <see cref="GraphicsResourceUsage.Immutable"/> </param>
187  /// <exception cref="ArgumentException">If the texture is not of type 2D</exception>
188  /// <returns>A texture</returns>
189  public static new Texture2D Load(GraphicsDevice device, Stream stream, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
190  {
191  var texture = Texture.Load(device, stream, textureFlags, usage);
192  if (!(texture is Texture2D))
193  throw new ArgumentException(string.Format("Texture is not type of [Texture2D] but [{0}]", texture.GetType().Name));
194  return (Texture2D)texture;
195  }
196  }
197 }
static new Texture2D New(GraphicsDevice device, Image image, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Creates a new Texture2D directly from an Image.
Definition: Texture2D.cs:171
A simple wrapper to specify number of mipmaps. Set to true to specify all mipmaps or sets an integer ...
Definition: MipMapCount.cs:43
Provides method to instantiate an image 1D/2D/3D supporting TextureArray and mipmaps on the CPU or to...
Definition: Image.cs:88
static Texture2D New(GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags=TextureFlags.ShaderResource, int arraySize=1, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Texture2D.
Definition: Texture2D.cs:120
Base class for converters to/from a data type.
GraphicsResourceUsage
Identifies expected resource use during rendering. The usage directly reflects whether a resource is ...
ImageDescription Description
Description of this image.
Definition: Image.cs:137
override Texture ToStaging()
Return an equivalent staging texture CPU read-writable from this instance.
Definition: Texture2D.cs:61
TextureDimension
Defines the dimension of a texture.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
A Common description for all textures.
static Texture2D New(GraphicsDevice device, TextureDescription textureDescription)
Creates a new texture from a Direct3D11.Texture2D.
Definition: Texture2D.cs:87
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
static Texture2D New(GraphicsDevice device, int width, int height, PixelFormat format, TextureFlags textureFlags=TextureFlags.ShaderResource, int arraySize=1, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Texture2D with a single mipmap.
Definition: Texture2D.cs:103
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
static Texture2D New(GraphicsDevice device, Texture2D texture)
Creates a new texture from a Direct3D11.Texture2D.
Definition: Texture2D.cs:74
override Texture Clone()
Makes a copy of this texture.
Definition: Texture2D.cs:52
DataBox[] ToDataBox()
Gets the databox from this image.
Definition: Image.cs:295
Provides access to data organized in 3D.
Definition: DataBox.cs:12
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32
static new Texture2D Load(GraphicsDevice device, Stream stream, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Loads a 2D texture from a stream.
Definition: Texture2D.cs:189
static Texture2D New(GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, DataBox[] textureData, TextureFlags textureFlags=TextureFlags.ShaderResource, int arraySize=1, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Texture2D.
Definition: Texture2D.cs:158
TextureDimension Dimension
The dimension of a texture.
Base class for texture resources.
Definition: Texture.cs:38