Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DepthStencilBuffer.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 using System;
5 using SharpDX;
6 using SharpDX.DXGI;
7 using SharpDX.Direct3D;
8 using SharpDX.Direct3D11;
9 
10 using SiliconStudio.Core;
11 using SiliconStudio.Core.ReferenceCounting;
13 
14 namespace SiliconStudio.Paradox.Graphics
15 {
16  /// <summary>
17  /// Depth stencil buffer
18  /// </summary>
19  public partial class DepthStencilBuffer
20  {
21  internal DepthStencilView NativeDepthStencilView;
22 
23  internal bool HasStencil;
24 
25  private readonly bool isReadOnly;
26 
27  internal DepthStencilBuffer(GraphicsDevice device, Texture2D depthTexture, bool isReadOnly) : base(device)
28  {
29  DescriptionInternal = depthTexture.Description;
30  Texture = depthTexture;
31  Texture.AddReferenceInternal();
32  this.isReadOnly = isReadOnly;
33  InitializeViews(out HasStencil);
34  }
35 
36  public static bool IsReadOnlySupported(GraphicsDevice device)
37  {
38  return device.Features.Profile >= GraphicsProfile.Level_11_0;
39  }
40 
41  protected override void DestroyImpl()
42  {
43  // Do not release NativeDepthStencilView, as it is owned by the texture.
44  // _renderTargetView should keep it alive anyway.
45  //base.DestroyImpl();
46  _nativeDeviceChild = null;
47  Utilities.Dispose(ref NativeDepthStencilView);
48  }
49 
50  /// <inheritdoc/>
51  protected internal override void OnDestroyed()
52  {
53  base.OnDestroyed();
54 
55  DestroyImpl();
56  }
57 
58  /// <inheritdoc/>
59  protected internal override bool OnRecreate()
60  {
61  base.OnRecreate();
62 
63  _nativeDeviceChild = Texture.NativeDeviceChild;
64  InitializeViews(out HasStencil);
65 
66  return true;
67  }
68 
69  private void InitializeViews(out bool hasStencil)
70  {
71  var nativeDescription = ((Texture2D)Texture).NativeDescription;
72 
73  if ((nativeDescription.BindFlags & BindFlags.DepthStencil) == 0)
74  throw new InvalidOperationException();
75 
76  // Check that the format is supported
77  if (ComputeShaderResourceFormat((Format)Texture.Description.Format) == Format.Unknown)
78  throw new NotSupportedException("Depth stencil format not supported");
79 
80  // Setup the HasStencil flag
81  hasStencil = IsStencilFormat(nativeDescription.Format);
82 
83  // Create a Depth stencil view on this texture2D
84  var depthStencilViewDescription = new SharpDX.Direct3D11.DepthStencilViewDescription
85  {
86  Format = ComputeDepthViewFormatFromTextureFormat(nativeDescription.Format),
87  Flags = SharpDX.Direct3D11.DepthStencilViewFlags.None,
88  };
89 
90  if (nativeDescription.ArraySize > 1)
91  {
92  depthStencilViewDescription.Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2DArray;
93  depthStencilViewDescription.Texture2DArray.ArraySize = nativeDescription.ArraySize;
94  depthStencilViewDescription.Texture2DArray.FirstArraySlice = 0;
95  depthStencilViewDescription.Texture2DArray.MipSlice = 0;
96  }
97  else
98  {
99  depthStencilViewDescription.Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D;
100  depthStencilViewDescription.Texture2D.MipSlice = 0;
101  }
102 
103  if (nativeDescription.SampleDescription.Count > 1)
104  depthStencilViewDescription.Dimension = DepthStencilViewDimension.Texture2DMultisampled;
105 
106  if (isReadOnly)
107  {
108  if (!IsReadOnlySupported(GraphicsDevice))
109  throw new NotSupportedException("Cannot instantiate ReadOnly DepthStencilBuffer. Not supported on this device.");
110 
111  // Create a Depth stencil view on this texture2D
112  depthStencilViewDescription.Flags = DepthStencilViewFlags.ReadOnlyDepth;
113  if (HasStencil)
114  depthStencilViewDescription.Flags |= DepthStencilViewFlags.ReadOnlyStencil;
115 
116  // Create the Depth Stencil View
117  NativeDepthStencilView = new SharpDX.Direct3D11.DepthStencilView(GraphicsDevice.NativeDevice, Texture.NativeResource, depthStencilViewDescription);
118  }
119  else
120  {
121  // Create the Depth Stencil View
122  NativeDepthStencilView = new SharpDX.Direct3D11.DepthStencilView(GraphicsDevice.NativeDevice, Texture.NativeResource, depthStencilViewDescription);
123  }
124  }
125 
126  internal static bool IsStencilFormat(Format format)
127  {
128  switch (format)
129  {
130  case Format.R24G8_Typeless:
131  case Format.D24_UNorm_S8_UInt:
132  case Format.R32G8X24_Typeless:
133  case Format.D32_Float_S8X24_UInt:
134  return true;
135  }
136 
137  return false;
138  }
139 
140  internal static Format ComputeShaderResourceFormat(Format format)
141  {
142  Format viewFormat;
143 
144  // Determine TypeLess Format and ShaderResourceView Format
145  switch (format)
146  {
147  case Format.D16_UNorm:
148  viewFormat = SharpDX.DXGI.Format.R16_Float;
149  break;
150  case Format.D32_Float:
151  viewFormat = SharpDX.DXGI.Format.R32_Float;
152  break;
153  case Format.D24_UNorm_S8_UInt:
154  viewFormat = SharpDX.DXGI.Format.R24_UNorm_X8_Typeless;
155  break;
156  case Format.D32_Float_S8X24_UInt:
157  viewFormat = SharpDX.DXGI.Format.R32_Float_X8X24_Typeless;
158  break;
159  default:
160  viewFormat = Format.Unknown;
161  break;
162  }
163 
164  return viewFormat;
165  }
166 
167  internal static Format ComputeDepthViewFormatFromTextureFormat(Format format)
168  {
169  Format viewFormat;
170 
171  switch (format)
172  {
173  case Format.R16_Typeless:
174  case Format.D16_UNorm:
175  viewFormat = Format.D16_UNorm;
176  break;
177  case Format.R32_Typeless:
178  case Format.D32_Float:
179  viewFormat = Format.D32_Float;
180  break;
181  case Format.R24G8_Typeless:
182  case Format.D24_UNorm_S8_UInt:
183  viewFormat = Format.D24_UNorm_S8_UInt;
184  break;
185  case Format.R32G8X24_Typeless:
186  case Format.D32_Float_S8X24_UInt:
187  viewFormat = Format.D32_Float_S8X24_UInt;
188  break;
189  default:
190  throw new NotSupportedException(string.Format("Unsupported depth format [{0}]", format));
191  }
192 
193  return viewFormat;
194  }
195  }
196 }
197 
198 #endif
Flags
Enumeration of the new Assimp's flags.
SiliconStudio.Core.Utilities Utilities
Definition: Texture.cs:29
Same as Deferred mode, except sprites are sorted by texture prior to drawing. This can improve perfor...
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
The texture dimension is 2D.