Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Texture3D.Direct3D.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 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_DIRECT3D
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 using System;
24 using System.IO;
25 
26 using SharpDX.DXGI;
27 using SharpDX.Direct3D;
28 using SharpDX.Direct3D11;
29 using SharpDX.IO;
30 
31 namespace SiliconStudio.Paradox.Graphics
32 {
33  /// <summary>
34  /// A Texture 3D frontend to <see cref="SharpDX.Direct3D11.Texture3D"/>.
35  /// </summary>
36  public partial class Texture3D
37  {
38  protected readonly SharpDX.Direct3D11.Texture3D Resource;
39  private SharpDX.DXGI.Surface dxgiSurface;
40  protected internal readonly Texture3DDescription NativeDescription;
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="Texture3DBase" /> class.
44  /// </summary>
45  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
46  /// <param name="description3D">The description.</param>
47  /// <param name="dataRectangles">A variable-length parameters list containing data rectangles.</param>
48  protected internal Texture3D(GraphicsDevice device, TextureDescription description3D, DataBox[] dataBoxes = null) : base(device, description3D, ViewType.Full, 0, 0)
49  {
50  NativeDescription = ConvertToNativeDescription(description3D);
51  Resource = new SharpDX.Direct3D11.Texture3D(device.NativeDevice, NativeDescription, ConvertDataBoxes(dataBoxes));
52  NativeDeviceChild = Resource;
53  NativeShaderResourceView = GetShaderResourceView(ViewType, ArraySlice, MipLevel);
54  NativeUnorderedAccessView = GetUnorderedAccessView(ArraySlice, MipLevel);
55  }
56 
57  /// <summary>
58  /// Specialised constructor for use only by derived classes.
59  /// </summary>
60  /// <param name="device">The device.</param>
61  /// <param name="texture">The texture.</param>
62  protected internal Texture3D(GraphicsDevice device, Texture3D texture, ViewType viewType, int arraySlice, int mipMapSlice, PixelFormat viewFormat = PixelFormat.None) : base(device, texture, viewType, arraySlice, mipMapSlice, viewFormat)
63  {
64  // Copy the device child, but don't use NativeDeviceChild, as it is registering it for disposing.
65  _nativeDeviceChild = texture._nativeDeviceChild;
66  Resource = texture.Resource;
67  NativeDescription = texture.NativeDescription;
68  dxgiSurface = texture.dxgiSurface;
69  NativeShaderResourceView = GetShaderResourceView(ViewType, ArraySlice, MipLevel);
70  NativeUnorderedAccessView = GetUnorderedAccessView(ArraySlice, MipLevel);
71  }
72 
73  public override Texture ToTexture(ViewType viewType, int arraySlice, int mipMapSlice)
74  {
75  return new Texture3D(GraphicsDevice, this, viewType, arraySlice, mipMapSlice);
76  }
77 
78  internal override ShaderResourceView GetShaderResourceView(ViewType viewType, int arrayOrDepthSlice, int mipIndex)
79  {
80  if ((this.NativeDescription.BindFlags & BindFlags.ShaderResource) == 0)
81  return null;
82 
83  int arrayCount;
84  int mipCount;
85  GetViewSliceBounds(viewType, ref arrayOrDepthSlice, ref mipIndex, out arrayCount, out mipCount);
86 
87  // Create the view
88  var srvDescription = new ShaderResourceViewDescription {
89  Format = (Format)this.Description.Format,
90  Dimension = ShaderResourceViewDimension.Texture3D,
91  Texture3D = {
92  MipLevels = mipCount,
93  MostDetailedMip = mipIndex
94  }
95  };
96 
97  return new ShaderResourceView(this.GraphicsDevice.NativeDevice, this.Resource, srvDescription);
98  }
99 
100  internal override UnorderedAccessView GetUnorderedAccessView(int zSlice, int mipIndex)
101  {
102  if ((this.NativeDescription.BindFlags & BindFlags.UnorderedAccess) == 0)
103  return null;
104 
105  int sliceCount;
106  int mipCount;
107  GetViewSliceBounds(ViewType.Single, ref zSlice, ref mipIndex, out sliceCount, out mipCount);
108 
109  var uavIndex = GetViewIndex(ViewType.Single, zSlice, mipIndex);
110 
111  var uavDescription = new UnorderedAccessViewDescription() {
112  Format = (Format)this.Description.Format,
113  Dimension = UnorderedAccessViewDimension.Texture3D,
114  Texture3D = {
115  FirstWSlice = zSlice,
116  MipSlice = mipIndex,
117  WSize = sliceCount
118  }
119  };
120 
121  return new UnorderedAccessView(GraphicsDevice.NativeDevice, Resource, uavDescription);
122  }
123 
124  protected static Texture3DDescription ConvertToNativeDescription(TextureDescription description)
125  {
126  var desc = new Texture3DDescription()
127  {
128  Width = description.Width,
129  Height = description.Height,
130  Depth = description.Depth,
131  BindFlags = GetBindFlagsFromTextureFlags(description.Flags),
132  Format = (Format)description.Format,
133  MipLevels = description.MipLevels,
134  Usage = (ResourceUsage)description.Usage,
135  CpuAccessFlags = GetCpuAccessFlagsFromUsage(description.Usage),
136  OptionFlags = ResourceOptionFlags.None
137  };
138  return desc;
139  }
140 
141  internal override RenderTargetView GetRenderTargetView(ViewType viewType, int arrayOrDepthSlice, int mipIndex)
142  {
143  if ((this.NativeDescription.BindFlags & BindFlags.RenderTarget) == 0)
144  return null;
145 
146  if (viewType == ViewType.MipBand)
147  throw new NotSupportedException("ViewSlice.MipBand is not supported for render targets");
148 
149  int arrayCount;
150  int mipCount;
151  GetViewSliceBounds(viewType, ref arrayOrDepthSlice, ref mipIndex, out arrayCount, out mipCount);
152 
153  // Create the render target view
154  var rtvDescription = new RenderTargetViewDescription()
155  {
156  Format = this.NativeDescription.Format,
157  Dimension = RenderTargetViewDimension.Texture3D,
158  Texture3D =
159  {
160  DepthSliceCount = arrayCount,
161  FirstDepthSlice = arrayOrDepthSlice,
162  MipSlice = mipIndex,
163  }
164  };
165 
166  return new RenderTargetView(GraphicsDevice.NativeDevice, Resource, rtvDescription);
167  }
168  }
169 }
170 #endif
ViewType
Defines how a view is selected from a resource.
Definition: ViewType.cs:31
Same as Deferred mode, except sprites are sorted by texture prior to drawing. This can improve perfor...
Gets a texture view for the whole texture for all mips/arrays dimensions.
The texture dimension is 3D.
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32