Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RenderTarget.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.Direct3D11;
7 using SiliconStudio.Core.ReferenceCounting;
9 
10 namespace SiliconStudio.Paradox.Graphics
11 {
12  /// <summary>
13  /// A renderable Texture2D.
14  /// </summary>
15  public partial class RenderTarget
16  {
17  private SharpDX.Direct3D11.RenderTargetView _renderTargetView;
18 
19  /// <summary>
20  /// Create a RenderTarget from a texture.
21  /// </summary>
22  /// <param name="device">The device.</param>
23  /// <param name="texture">The texture.</param>
24  /// <param name="viewType">The view type.</param>
25  /// <param name="arraySlice">The index of the array slice.</param>
26  /// <param name="mipSlice">The index of the mip slice.</param>
27  /// <param name="viewFormat">The pixel format.</param>
28  internal RenderTarget(GraphicsDevice device, Texture texture, ViewType viewType, int arraySlice, int mipSlice, PixelFormat viewFormat = PixelFormat.None)
29  : base(device)
30  {
31  _nativeDeviceChild = texture.NativeDeviceChild;
32  Description = texture.Description;
33 
34  NativeRenderTargetView = texture.GetRenderTargetView(viewType, arraySlice, mipSlice);
35 
36  Width = Math.Max(1, Description.Width >> mipSlice);
37  Height = Math.Max(1, Description.Height >> mipSlice);
38 
39  ViewType = viewType;
40  ArraySlice = arraySlice;
41  MipLevel = mipSlice;
42  ViewFormat = viewFormat == PixelFormat.None ? Description.Format : viewFormat;
43 
44  Texture = texture;
45  Texture.AddReferenceInternal();
46  }
47 
48  /// <summary>
49  /// Create a RenderTarget from a texture.
50  /// </summary>
51  /// <param name="device">The device.</param>
52  /// <param name="texture">The texture.</param>
53  /// <param name="viewType">The view type.</param>
54  /// <param name="arraySlice">The index of the array slice.</param>
55  /// <param name="mipSlice">The index of the mip slice.</param>
56  /// <param name="view">The render target view.</param>
57  internal RenderTarget(GraphicsDevice device, Texture texture, ViewType viewType, int arraySlice, int mipSlice, RenderTargetView view)
58  : base(device)
59  {
60  _nativeDeviceChild = texture.NativeDeviceChild; //._nativeDeviceChild;
61  Description = texture.Description;
62 
63  NativeRenderTargetView = view;
64 
65  Width = Math.Max(1, Description.Width >> mipSlice);
66  Height = Math.Max(1, Description.Height >> mipSlice);
67 
68  var viewFormat = view.Description.Format;
69 
70  ViewType = viewType;
71  ArraySlice = arraySlice;
72  MipLevel = mipSlice;
73  ViewFormat = (PixelFormat)viewFormat;
74 
75  Texture = texture;
76  }
77 
78  protected override void DestroyImpl()
79  {
80  // Do not release _nativeDeviceChild, as it is owned by the texture.
81  // _renderTargetView should keep it alive anyway.
82  //base.DestroyImpl();
83  _nativeDeviceChild = null;
84  Utilities.Dispose(ref _renderTargetView);
85  }
86 
87  /// <inheritdoc/>
88  protected internal override void OnDestroyed()
89  {
90  base.OnDestroyed();
91  DestroyImpl();
92  }
93 
94  /// <inheritdoc/>
95  protected internal override bool OnRecreate()
96  {
97  // Dependency: wait for underlying texture to be recreated first
98  if (Texture.LifetimeState != GraphicsResourceLifetimeState.Active)
99  return false;
100 
101  base.OnRecreate();
102  _nativeDeviceChild = Texture.NativeDeviceChild;
103  NativeRenderTargetView = Texture.GetRenderTargetView(ViewType, ArraySlice, MipLevel);
104 
105  return true;
106  }
107 
108  /// <summary>
109  /// Gets or sets the RenderTargetView attached to this GraphicsResource.
110  /// Note that only Texture2D, Texture3D, RenderTarget2D, RenderTarget3D, DepthStencil are using this ShaderResourceView
111  /// </summary>
112  /// <value>The device child.</value>
113  internal SharpDX.Direct3D11.RenderTargetView NativeRenderTargetView
114  {
115  get
116  {
117  return _renderTargetView;
118  }
119  set
120  {
121  if (_renderTargetView != null) throw new ArgumentException(string.Format(FrameworkResources.GraphicsResourceAlreadySet, "RenderTargetView"), "value");
122  _renderTargetView = value;
123 
124  // Associate PrivateData to this DeviceResource
125  SetDebugName(GraphicsDevice, _renderTargetView, "View " + Name);
126  }
127  }
128  }
129 }
130 
131 #endif
SiliconStudio.Core.Utilities Utilities
Definition: Texture.cs:29
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...
GraphicsResourceLifetimeState
Describes the lifetime state of a graphics resource.
Creates a render target buffer.
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32