Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Texture1D.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 1D frontend to <see cref="SharpDX.Direct3D11.Texture1D"/>.
35  /// </summary>
36  [DataConverter(AutoGenerate = false, ContentReference = true, DataType = false)]
37  public partial class Texture1D : Texture
38  {
39  /// <summary>
40  /// Makes a copy of this texture.
41  /// </summary>
42  /// <remarks>
43  /// This method doesn't copy the content of the texture.
44  /// </remarks>
45  /// <returns>
46  /// A copy of this texture.
47  /// </returns>
48  public override Texture Clone()
49  {
50  return new Texture1D(GraphicsDevice, GetCloneableDescription());
51  }
52 
53  /// <summary>
54  /// Return an equivalent staging texture CPU read-writable from this instance.
55  /// </summary>
56  /// <returns></returns>
57  public override Texture ToStaging()
58  {
59  return new Texture1D(this.GraphicsDevice, this.Description.ToStagingDescription());
60  }
61 
62  /// <summary>
63  /// Creates a new <see cref="Texture1D"/> with a single mipmap.
64  /// </summary>
65  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
66  /// <param name="width">The width.</param>
67  /// <param name="format">Describes the format to use.</param>
68  /// <param name="usage">The usage.</param>
69  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
70  /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
71  /// <returns>
72  /// A new instance of <see cref="Texture1D"/> class.
73  /// </returns>
74  public static Texture1D New(GraphicsDevice device, int width, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
75  {
76  return New(device, width, false, format, textureFlags, arraySize, usage);
77  }
78 
79  /// <summary>
80  /// Creates a new <see cref="Texture1D"/>.
81  /// </summary>
82  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
83  /// <param name="width">The width.</param>
84  /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param>
85  /// <param name="format">Describes the format to use.</param>
86  /// <param name="usage">The usage.</param>
87  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
88  /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
89  /// <returns>
90  /// A new instance of <see cref="Texture1D"/> class.
91  /// </returns>
92  public static Texture1D New(GraphicsDevice device, int width, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
93  {
94  return new Texture1D(device, NewDescription(width, format, textureFlags, mipCount, arraySize, usage));
95  }
96 
97  /// <summary>
98  /// Creates a new <see cref="Texture1D" /> with a single level of mipmap.
99  /// </summary>
100  /// <typeparam name="T">Type of the initial data to upload to the texture</typeparam>
101  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
102  /// <param name="width">The width.</param>
103  /// <param name="format">Describes the format to use.</param>
104  /// <param name="usage">The usage.</param>
105  /// <param name="textureData">Texture data. Size of must be equal to sizeof(Format) * width </param>
106  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
107  /// <returns>A new instance of <see cref="Texture1D" /> class.</returns>
108  /// <remarks>
109  /// The first dimension of mipMapTextures describes the number of array (Texture1D Array), second dimension is the mipmap, the third is the texture data for a particular mipmap.
110  /// </remarks>
111  public unsafe static Texture1D New<T>(GraphicsDevice device, int width, PixelFormat format, T[] textureData, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable) where T : struct
112  {
113  return new Texture1D(device, NewDescription(width, format, textureFlags, 1, 1, usage), new[] {GetDataBox(format, width, 1, 1, textureData, (IntPtr)Interop.Fixed(textureData))});
114  }
115 
116  /// <summary>
117  /// Creates a new <see cref="Texture1D" /> with a single level of mipmap.
118  /// </summary>
119  /// <typeparam name="T">Type of the initial data to upload to the texture</typeparam>
120  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
121  /// <param name="width">The width.</param>
122  /// <param name="format">Describes the format to use.</param>
123  /// <param name="usage">The usage.</param>
124  /// <param name="textureData">Texture data. Size of must be equal to sizeof(Format) * width </param>
125  /// <param name="dataPtr">Data ptr</param>
126  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
127  /// <returns>A new instance of <see cref="Texture1D" /> class.</returns>
128  /// <remarks>
129  /// The first dimension of mipMapTextures describes the number of array (Texture1D Array), second dimension is the mipmap, the third is the texture data for a particular mipmap.
130  /// </remarks>
131  public unsafe static Texture1D New(GraphicsDevice device, int width, PixelFormat format, IntPtr dataPtr, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
132  {
133  return new Texture1D(device, NewDescription(width, format, textureFlags, 1, 1, usage), new [] { new DataBox(dataPtr, 0, 0), });
134  }
135  /// <summary>
136  /// Creates a new <see cref="Texture1D" /> directly from an <see cref="Image"/>.
137  /// </summary>
138  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
139  /// <param name="image">An image in CPU memory.</param>
140  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
141  /// <param name="usage">The usage.</param>
142  /// <returns>A new instance of <see cref="Texture1D" /> class.</returns>
143  public static new Texture1D New(GraphicsDevice device, Image image, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
144  {
145  if (image == null) throw new ArgumentNullException("image");
146  if (image.Description.Dimension != TextureDimension.Texture1D)
147  throw new ArgumentException("Invalid image. Must be 1D", "image");
148 
149  return new Texture1D(device, CreateTextureDescriptionFromImage(image, textureFlags, usage), image.ToDataBox());
150  }
151 
152  /// <summary>
153  /// Loads a 1D texture from a stream.
154  /// </summary>
155  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
156  /// <param name="stream">The stream to load the texture from.</param>
157  /// <param name="textureFlags">True to load the texture with unordered access enabled. Default is false.</param>
158  /// <param name="usage">Usage of the resource. Default is <see cref="GraphicsResourceUsage.Immutable"/> </param>
159  /// <exception cref="ArgumentException">If the texture is not of type 1D</exception>
160  /// <returns>A texture</returns>
161  public static new Texture1D Load(GraphicsDevice device, Stream stream, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
162  {
163  var texture = Texture.Load(device, stream, textureFlags, usage);
164  if (!(texture is Texture1D))
165  throw new ArgumentException(string.Format("Texture is not type of [Texture1D] but [{0}]", texture.GetType().Name));
166  return (Texture1D)texture;
167  }
168 
169  protected static TextureDescription NewDescription(int width, PixelFormat format, TextureFlags flags, int mipCount, int arraySize, GraphicsResourceUsage usage)
170  {
171  usage = (flags & TextureFlags.UnorderedAccess) != 0 ? GraphicsResourceUsage.Default : usage;
172  var desc = new TextureDescription()
173  {
174  Dimension = TextureDimension.Texture1D,
175  Width = width,
176  Height = 1,
177  Depth = 1,
178  ArraySize = arraySize,
179  Flags = flags,
180  Format = format,
181  MipLevels = CalculateMipMapCount(mipCount, width),
182  Usage = GetUsageWithFlags(usage, flags),
183  };
184  return desc;
185  }
186  }
187 }
A simple wrapper to specify number of mipmaps. Set to true to specify all mipmaps or sets an integer ...
Definition: MipMapCount.cs:43
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ DXGI_FORMAT _In_ DWORD flags
Definition: DirectXTexP.h:170
Provides method to instantiate an image 1D/2D/3D supporting TextureArray and mipmaps on the CPU or to...
Definition: Image.cs:88
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 Clone()
Makes a copy of this texture.
Definition: Texture1D.cs:48
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.
Flags
Enumeration of the new Assimp's flags.
A Common description for all textures.
A Texture 1D frontend to SharpDX.Direct3D11.Texture1D.
Definition: Texture1D.cs:37
static unsafe Texture1D New(GraphicsDevice device, int width, PixelFormat format, IntPtr dataPtr, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Creates a new Texture1D with a single level of mipmap.
Definition: Texture1D.cs:131
DataBox[] ToDataBox()
Gets the databox from this image.
Definition: Image.cs:295
static Texture1D New(GraphicsDevice device, int width, PixelFormat format, TextureFlags textureFlags=TextureFlags.ShaderResource, int arraySize=1, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Texture1D with a single mipmap.
Definition: Texture1D.cs:74
static Texture1D New(GraphicsDevice device, int width, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags=TextureFlags.ShaderResource, int arraySize=1, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Texture1D.
Definition: Texture1D.cs:92
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
static TextureDescription NewDescription(int width, PixelFormat format, TextureFlags flags, int mipCount, int arraySize, GraphicsResourceUsage usage)
Definition: Texture1D.cs:169
static new Texture1D New(GraphicsDevice device, Image image, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Creates a new Texture1D directly from an Image.
Definition: Texture1D.cs:143
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32
static new Texture1D Load(GraphicsDevice device, Stream stream, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Loads a 1D texture from a stream.
Definition: Texture1D.cs:161
TextureDimension Dimension
The dimension of a texture.
override Texture ToStaging()
Return an equivalent staging texture CPU read-writable from this instance.
Definition: Texture1D.cs:57
Base class for texture resources.
Definition: Texture.cs:38