Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Texture3D.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 3D frontend to <see cref="SharpDX.Direct3D11.Texture3D"/>.
35  /// </summary>
36  [DataConverter(AutoGenerate = false, ContentReference = true, DataType = false)]
37  public partial class Texture3D : 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 Texture3D(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 Texture3D(this.GraphicsDevice, this.Description.ToStagingDescription());
60  }
61 
62  /// <summary>
63  /// Creates a new texture from a <see cref="Direct3D11.Texture3D"/>.
64  /// </summary>
65  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
66  /// <param name="texture">The native texture <see cref="Direct3D11.Texture3D"/>.</param>
67  /// <returns>
68  /// A new instance of <see cref="Texture3D"/> class.
69  /// </returns>
70  /// <unmanaged-short>ID3D11Device::CreateTexture3D</unmanaged-short>
71  public static Texture3D New(GraphicsDevice device, TextureDescription texture)
72  {
73  return new Texture3D(device, texture);
74  }
75 
76  /// <summary>
77  /// Creates a new <see cref="Texture3D"/> with a single mipmap.
78  /// </summary>
79  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
80  /// <param name="width">The width.</param>
81  /// <param name="height">The height.</param>
82  /// <param name="depth">The depth.</param>
83  /// <param name="format">Describes the format to use.</param>
84  /// <param name="usage">The usage.</param>
85  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
86  /// <returns>
87  /// A new instance of <see cref="Texture3D"/> class.
88  /// </returns>
89  public static Texture3D New(GraphicsDevice device, int width, int height, int depth, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
90  {
91  return New(device, width, height, depth, false, format, textureFlags, usage);
92  }
93 
94  /// <summary>
95  /// Creates a new <see cref="Texture3D"/>.
96  /// </summary>
97  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
98  /// <param name="width">The width.</param>
99  /// <param name="height">The height.</param>
100  /// <param name="depth">The depth.</param>
101  /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param>
102  /// <param name="format">Describes the format to use.</param>
103  /// <param name="usage">The usage.</param>
104  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
105  /// <returns>
106  /// A new instance of <see cref="Texture3D"/> class.
107  /// </returns>
108  public static Texture3D New(GraphicsDevice device, int width, int height, int depth, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
109  {
110  return new Texture3D(device, NewDescription(width, height, depth, format, textureFlags, mipCount, usage));
111  }
112 
113  /// <summary>
114  /// Creates a new <see cref="Texture3D" /> with texture data for the firs map.
115  /// </summary>
116  /// <typeparam name="T">Type of the data to upload to the texture</typeparam>
117  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
118  /// <param name="width">The width.</param>
119  /// <param name="height">The height.</param>
120  /// <param name="depth">The depth.</param>
121  /// <param name="format">Describes the format to use.</param>
122  /// <param name="usage">The usage.</param>
123  /// <param name="textureData">The texture data, width * height * depth datas </param>
124  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
125  /// <returns>A new instance of <see cref="Texture3D" /> class.</returns>
126  /// <remarks>
127  /// The first dimension of mipMapTextures describes the number of is an array ot Texture3D Array
128  /// </remarks>
129  public unsafe static Texture3D New<T>(GraphicsDevice device, int width, int height, int depth, PixelFormat format, T[] textureData, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable) where T : struct
130  {
131  return New(device, width, height, depth, 1, format, new[] { GetDataBox(format, width, height, depth, textureData, (IntPtr)Interop.Fixed(textureData)) }, textureFlags, usage);
132  }
133 
134  /// <summary>
135  /// Creates a new <see cref="Texture3D"/>.
136  /// </summary>
137  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
138  /// <param name="width">The width.</param>
139  /// <param name="height">The height.</param>
140  /// <param name="depth">The depth.</param>
141  /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param>
142  /// <param name="format">Describes the format to use.</param>
143  /// <param name="usage">The usage.</param>
144  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
145  /// <returns>
146  /// A new instance of <see cref="Texture3D"/> class.
147  /// </returns>
148  public static Texture3D New(GraphicsDevice device, int width, int height, int depth, MipMapCount mipCount, PixelFormat format, DataBox[] textureData, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
149  {
150  // TODO Add check for number of texture datas according to width/height/depth/mipCount.
151  return new Texture3D(device, NewDescription(width, height, depth, format, textureFlags, mipCount, usage), textureData);
152  }
153 
154  /// <summary>
155  /// Creates a new <see cref="Texture3D" /> directly from an <see cref="Image"/>.
156  /// </summary>
157  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
158  /// <param name="image">An image in CPU memory.</param>
159  /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
160  /// <param name="usage">The usage.</param>
161  /// <returns>A new instance of <see cref="Texture3D" /> class.</returns>
162  public static new Texture3D New(GraphicsDevice device, Image image, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
163  {
164  if (image == null) throw new ArgumentNullException("image");
165  if (image.Description.Dimension != TextureDimension.Texture3D)
166  throw new ArgumentException("Invalid image. Must be 3D", "image");
167 
168  return new Texture3D(device, CreateTextureDescriptionFromImage(image, textureFlags, usage), image.ToDataBox());
169  }
170 
171  /// <summary>
172  /// Loads a 3D texture from a stream.
173  /// </summary>
174  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
175  /// <param name="stream">The stream to load the texture from.</param>
176  /// <param name="textureFlags">True to load the texture with unordered access enabled. Default is false.</param>
177  /// <param name="usage">Usage of the resource. Default is <see cref="GraphicsResourceUsage.Immutable"/> </param>
178  /// <exception cref="ArgumentException">If the texture is not of type 3D</exception>
179  /// <returns>A texture</returns>
180  public static new Texture3D Load(GraphicsDevice device, Stream stream, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
181  {
182  var texture = Texture.Load(device, stream, textureFlags, usage);
183  if (!(texture is Texture3D))
184  throw new ArgumentException(string.Format("Texture is not type of [Texture3D] but [{0}]", texture.GetType().Name));
185  return (Texture3D)texture;
186  }
187 
188  protected static TextureDescription NewDescription(int width, int height, int depth, PixelFormat format, TextureFlags flags, int mipCount, GraphicsResourceUsage usage)
189  {
190  var desc = new TextureDescription()
191  {
192  Width = width,
193  Height = height,
194  Depth = depth,
195  Flags = flags,
196  Format = format,
197  MipLevels = CalculateMipMapCount(mipCount, width, height, depth),
198  Usage = GetUsageWithFlags(usage, flags),
199  ArraySize = 1,
200  Dimension = TextureDimension.Texture3D,
201  Level = MSAALevel.None
202  };
203 
204  return desc;
205  }
206  }
207 }
static TextureDescription NewDescription(int width, int height, int depth, PixelFormat format, TextureFlags flags, int mipCount, GraphicsResourceUsage usage)
Definition: Texture3D.cs:188
override Texture ToStaging()
Return an equivalent staging texture CPU read-writable from this instance.
Definition: Texture3D.cs:57
static new Texture3D Load(GraphicsDevice device, Stream stream, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Loads a 3D texture from a stream.
Definition: Texture3D.cs:180
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.
static Texture3D New(GraphicsDevice device, int width, int height, int depth, PixelFormat format, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Texture3D with a single mipmap.
Definition: Texture3D.cs:89
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
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.
override Texture Clone()
Makes a copy of this texture.
Definition: Texture3D.cs:48
A Texture 3D frontend to SharpDX.Direct3D11.Texture3D.
Definition: Texture3D.cs:37
static new Texture3D New(GraphicsDevice device, Image image, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Creates a new Texture3D directly from an Image.
Definition: Texture3D.cs:162
static Texture3D New(GraphicsDevice device, int width, int height, int depth, MipMapCount mipCount, PixelFormat format, DataBox[] textureData, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Texture3D.
Definition: Texture3D.cs:148
DataBox[] ToDataBox()
Gets the databox from this image.
Definition: Image.cs:295
Provides access to data organized in 3D.
Definition: DataBox.cs:12
static Texture3D New(GraphicsDevice device, int width, int height, int depth, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Texture3D.
Definition: Texture3D.cs:108
_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 Texture3D New(GraphicsDevice device, TextureDescription texture)
Creates a new texture from a Direct3D11.Texture3D.
Definition: Texture3D.cs:71
TextureDimension Dimension
The dimension of a texture.
Base class for texture resources.
Definition: Texture.cs:38