Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Texture1D.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 
25 using SharpDX.DXGI;
26 using SharpDX.Direct3D;
27 using SharpDX.Direct3D11;
28 
29 namespace SiliconStudio.Paradox.Graphics
30 {
31  /// <summary>
32  /// A Texture 1D frontend to <see cref="SharpDX.Direct3D11.Texture1D"/>.
33  /// </summary>
34  public partial class Texture1D
35  {
36  protected internal readonly SharpDX.Direct3D11.Texture1D Resource;
37  protected internal SharpDX.DXGI.Surface dxgiSurface;
38  protected internal readonly Texture1DDescription NativeDescription;
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="Texture1DBase" /> class.
42  /// </summary>
43  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
44  /// <param name="description1D">The description.</param>
45  protected internal Texture1D(GraphicsDevice device, TextureDescription description1D, DataBox[] dataBox = null) : base(device, description1D, ViewType.Full, 0, 0)
46  {
47  NativeDescription = ConvertToNativeDescription(description1D);
48  Resource = new SharpDX.Direct3D11.Texture1D(device.NativeDevice, NativeDescription, ConvertDataBoxes(dataBox));
49  NativeDeviceChild = Resource;
50  NativeShaderResourceView = GetShaderResourceView(ViewType, ArraySlice, MipLevel);
51  NativeUnorderedAccessView = GetUnorderedAccessView(ArraySlice, MipLevel);
52  }
53 
54  /// <summary>
55  /// Initializes a new instance of the <see cref="Texture1DBase" /> class.
56  /// </summary>
57  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
58  /// <param name="description1D">The description.</param>
59  protected internal Texture1D(GraphicsDevice device, Texture1D texture, ViewType viewType, int arraySlice, int mipMapSlice, PixelFormat viewFormat = PixelFormat.None) : base(device, texture, viewType, arraySlice, mipMapSlice, viewFormat)
60  {
61  // Copy the device child, but don't use NativeDeviceChild, as it is registering it for disposing.
62  _nativeDeviceChild = texture._nativeDeviceChild;
63  Resource = texture.Resource;
64  NativeDescription = texture.NativeDescription;
65  dxgiSurface = texture.dxgiSurface;
66  NativeShaderResourceView = GetShaderResourceView(ViewType, ArraySlice, MipLevel);
67  NativeUnorderedAccessView = GetUnorderedAccessView(ArraySlice, MipLevel);
68  }
69 
70  public override Texture ToTexture(ViewType viewType, int arraySlice, int mipMapSlice)
71  {
72  return new Texture1D(GraphicsDevice, this, viewType, arraySlice, mipMapSlice);
73  }
74 
75  internal override ShaderResourceView GetShaderResourceView(ViewType viewType, int arrayOrDepthSlice, int mipIndex)
76  {
77  if ((this.NativeDescription.BindFlags & BindFlags.ShaderResource) == 0)
78  return null;
79 
80  int arrayCount;
81  int mipCount;
82  GetViewSliceBounds(viewType, ref arrayOrDepthSlice, ref mipIndex, out arrayCount, out mipCount);
83 
84  // Create the view
85  var srvDescription = new ShaderResourceViewDescription() { Format = (Format)this.Description.Format };
86 
87  // Initialize for texture arrays or texture cube
88  if (this.Description.ArraySize > 1)
89  {
90  // Else regular Texture1D
91  srvDescription.Dimension = ShaderResourceViewDimension.Texture1DArray;
92  srvDescription.Texture1DArray.ArraySize = arrayCount;
93  srvDescription.Texture1DArray.FirstArraySlice = arrayOrDepthSlice;
94  srvDescription.Texture1DArray.MipLevels = mipCount;
95  srvDescription.Texture1DArray.MostDetailedMip = mipIndex;
96  }
97  else
98  {
99  srvDescription.Dimension = ShaderResourceViewDimension.Texture1D;
100  srvDescription.Texture1D.MipLevels = mipCount;
101  srvDescription.Texture1D.MostDetailedMip = mipIndex;
102  }
103 
104  return new ShaderResourceView(this.GraphicsDevice.NativeDevice, this.Resource, srvDescription);
105  }
106 
107  internal override UnorderedAccessView GetUnorderedAccessView(int arrayOrDepthSlice, int mipIndex)
108  {
109  if ((this.NativeDescription.BindFlags & BindFlags.UnorderedAccess) == 0)
110  return null;
111 
112  int arrayCount;
113  int mipCount;
114  GetViewSliceBounds(ViewType.Single, ref arrayOrDepthSlice, ref mipIndex, out arrayCount, out mipCount);
115 
116  var uavDescription = new UnorderedAccessViewDescription() {
117  Format = (Format)this.Description.Format,
118  Dimension = this.Description.ArraySize > 1 ? UnorderedAccessViewDimension.Texture1DArray : UnorderedAccessViewDimension.Texture1D
119  };
120 
121  if (this.Description.ArraySize > 1)
122  {
123  uavDescription.Texture1DArray.ArraySize = arrayCount;
124  uavDescription.Texture1DArray.FirstArraySlice = arrayOrDepthSlice;
125  uavDescription.Texture1DArray.MipSlice = mipIndex;
126  }
127  else
128  {
129  uavDescription.Texture1D.MipSlice = mipIndex;
130  }
131 
132  return new UnorderedAccessView(GraphicsDevice.NativeDevice, Resource, uavDescription);
133  }
134 
135  internal override RenderTargetView GetRenderTargetView(ViewType viewType, int arrayOrDepthSlice, int mipIndex)
136  {
137  if ((this.NativeDescription.BindFlags & BindFlags.RenderTarget) == 0)
138  return null;
139 
140  if (viewType == ViewType.MipBand)
141  throw new NotSupportedException("ViewSlice.MipBand is not supported for render targets");
142 
143  int arrayCount;
144  int mipCount;
145  GetViewSliceBounds(viewType, ref arrayOrDepthSlice, ref mipIndex, out arrayCount, out mipCount);
146 
147  // Create the render target view
148  var rtvDescription = new RenderTargetViewDescription() { Format = this.NativeDescription.Format };
149 
150  if (this.Description.ArraySize > 1)
151  {
152  rtvDescription.Dimension = RenderTargetViewDimension.Texture1DArray;
153  rtvDescription.Texture1DArray.ArraySize = arrayCount;
154  rtvDescription.Texture1DArray.FirstArraySlice = arrayOrDepthSlice;
155  rtvDescription.Texture1DArray.MipSlice = mipIndex;
156  }
157  else
158  {
159  rtvDescription.Dimension = RenderTargetViewDimension.Texture1D;
160  rtvDescription.Texture1D.MipSlice = mipIndex;
161  }
162 
163  return new RenderTargetView(GraphicsDevice.NativeDevice, Resource, rtvDescription);
164  }
165 
166  protected static Texture1DDescription ConvertToNativeDescription(TextureDescription description)
167  {
168  var desc = new Texture1DDescription()
169  {
170  Width = description.Width,
171  ArraySize = 1,
172  BindFlags = GetBindFlagsFromTextureFlags(description.Flags),
173  Format = (Format)description.Format,
174  MipLevels = description.MipLevels,
175  Usage = (ResourceUsage)description.Usage,
176  CpuAccessFlags = GetCpuAccessFlagsFromUsage(description.Usage),
177  OptionFlags = ResourceOptionFlags.None
178  };
179  return desc;
180  }
181  }
182 }
183 #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 1D.
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32