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