Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GraphicsResourceBase.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 System.Runtime.InteropServices;
6 using SharpDX;
7 using SharpDX.Direct3D;
8 using SharpDX.Direct3D11;
9 using SharpDX.DXGI;
10 
11 using SiliconStudio.Core;
12 
13 namespace SiliconStudio.Paradox.Graphics
14 {
15  /// <summary>
16  /// GraphicsResource class
17  /// </summary>
18  public abstract partial class GraphicsResourceBase
19  {
20  protected internal SharpDX.Direct3D11.DeviceChild _nativeDeviceChild;
21 
22  protected internal SharpDX.Direct3D11.Resource NativeResource;
23 
24  private void Initialize()
25  {
26  if (GraphicsDevice != null)
27  NativeDevice = GraphicsDevice.NativeDevice;
28  }
29 
30  /// <summary>
31  /// Gets or sets the device child.
32  /// </summary>
33  /// <value>The device child.</value>
34  protected internal SharpDX.Direct3D11.DeviceChild NativeDeviceChild
35  {
36  get
37  {
38  return _nativeDeviceChild;
39  }
40  set
41  {
42  if (_nativeDeviceChild != null) throw new ArgumentException(string.Format(FrameworkResources.GraphicsResourceAlreadySet, "DeviceChild"), "value");
43  _nativeDeviceChild = value;
44 
45  if (_nativeDeviceChild is SharpDX.Direct3D11.Resource)
46  NativeResource = (SharpDX.Direct3D11.Resource)_nativeDeviceChild;
47 
48  // Associate PrivateData to this DeviceResource
49  SetDebugName(GraphicsDevice, _nativeDeviceChild, Name);
50  }
51  }
52 
53  protected virtual void DestroyImpl()
54  {
55  if (_nativeDeviceChild != null)
56  {
57  ((IUnknown)_nativeDeviceChild).Release();
58  _nativeDeviceChild = null;
59  NativeResource = null;
60  }
61  }
62 
63  /// <summary>
64  /// Associates the private data to the device child, useful to get the name in PIX debugger.
65  /// </summary>
66  internal static void SetDebugName(GraphicsDevice graphicsDevice, SharpDX.Direct3D11.DeviceChild deviceChild, string name)
67  {
68  if (graphicsDevice.IsDebugMode && deviceChild != null)
69  {
70  IntPtr namePtr = SharpDX.Utilities.StringToHGlobalAnsi(name);
71  deviceChild.SetPrivateData(CommonGuid.DebugObjectName, name.Length, namePtr);
72  // TODO Should PrivateData be deallocated now or keep a reference to it?
73  }
74  }
75 
76  /// <summary>
77  /// Associates the private data to the device child, useful to get the name in PIX debugger.
78  /// </summary>
79  internal static void SetDebugName(GraphicsDevice graphicsDevice, DXGIObject dxgiObject, string name)
80  {
81  if (graphicsDevice.IsDebugMode)
82  {
83  IntPtr namePtr = SharpDX.Utilities.StringToHGlobalAnsi(name);
84  dxgiObject.SetPrivateData(CommonGuid.DebugObjectName, name.Length, namePtr);
85  }
86  }
87 
88  /// <summary>
89  /// Called when graphics device has been detected to be internally destroyed.
90  /// </summary>
91  protected internal virtual void OnDestroyed()
92  {
93  NativeDevice = null;
94  }
95 
96  /// <summary>
97  /// Called when graphics device has been recreated.
98  /// </summary>
99  /// <returns>True if item transitionned to a <see cref="GraphicsResourceLifetimeState.Active"/> state.</returns>
100  protected internal virtual bool OnRecreate()
101  {
102  NativeDevice = GraphicsDevice.NativeDevice;
103  return false;
104  }
105 
106  protected SharpDX.Direct3D11.Device NativeDevice
107  {
108  get;
109  private set;
110  }
111 
112  /// <summary>
113  /// Gets the cpu access flags from resource usage.
114  /// </summary>
115  /// <param name="usage">The usage.</param>
116  /// <returns></returns>
117  internal static SharpDX.Direct3D11.CpuAccessFlags GetCpuAccessFlagsFromUsage(GraphicsResourceUsage usage)
118  {
119  switch (usage)
120  {
121  case GraphicsResourceUsage.Dynamic:
122  return SharpDX.Direct3D11.CpuAccessFlags.Write;
123  case GraphicsResourceUsage.Staging:
124  return SharpDX.Direct3D11.CpuAccessFlags.Read;
125  }
126  return SharpDX.Direct3D11.CpuAccessFlags.None;
127  }
128  }
129 }
130 
131 #endif
GraphicsResourceUsage
Identifies expected resource use during rendering. The usage directly reflects whether a resource is ...