2
0

D3D11Image.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Interop;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using SharpDX.Direct3D9;
  6. using Texture = SharpDX.Direct3D9.Texture;
  7. namespace WpfInteropSample
  8. {
  9. /// <summary>
  10. /// Wraps the <see cref="D3DImage"/> to make it compatible with Direct3D 11.
  11. /// </summary>
  12. /// <remarks>
  13. /// The <see cref="D3D11Image"/> should be disposed if no longer needed!
  14. /// </remarks>
  15. internal class D3D11Image : D3DImage, IDisposable
  16. {
  17. #region Fields
  18. // Use a Direct3D 9 device for interoperability. The device is shared by
  19. // all D3D11Images.
  20. private static D3D9 _d3D9;
  21. private static int _referenceCount;
  22. private static readonly object _d3d9Lock = new object();
  23. private bool _disposed;
  24. private Texture _backBuffer;
  25. #endregion
  26. #region Creation & Cleanup
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="D3D11Image"/> class.
  29. /// </summary>
  30. public D3D11Image()
  31. {
  32. InitializeD3D9();
  33. }
  34. /// <summary>
  35. /// Releases unmanaged resources before an instance of the <see cref="D3D11Image"/> class is
  36. /// reclaimed by garbage collection.
  37. /// </summary>
  38. /// <remarks>
  39. /// This method releases unmanaged resources by calling the virtual <see cref="Dispose(bool)"/>
  40. /// method, passing in <see langword="false"/>.
  41. /// </remarks>
  42. ~D3D11Image()
  43. {
  44. Dispose(false);
  45. }
  46. /// <summary>
  47. /// Releases all resources used by an instance of the <see cref="D3D11Image"/> class.
  48. /// </summary>
  49. /// <remarks>
  50. /// This method calls the virtual <see cref="Dispose(bool)"/> method, passing in
  51. /// <see langword="true"/>, and then suppresses finalization of the instance.
  52. /// </remarks>
  53. public void Dispose()
  54. {
  55. Dispose(true);
  56. GC.SuppressFinalize(this);
  57. }
  58. /// <summary>
  59. /// Releases the unmanaged resources used by an instance of the <see cref="D3D11Image"/> class
  60. /// and optionally releases the managed resources.
  61. /// </summary>
  62. /// <param name="disposing">
  63. /// <see langword="true"/> to release both managed and unmanaged resources;
  64. /// <see langword="false"/> to release only unmanaged resources.
  65. /// </param>
  66. protected virtual void Dispose(bool disposing)
  67. {
  68. if (!_disposed)
  69. {
  70. if (disposing)
  71. {
  72. // Dispose managed resources.
  73. SetBackBuffer(null);
  74. if (_backBuffer != null)
  75. {
  76. _backBuffer.Dispose();
  77. _backBuffer = null;
  78. }
  79. }
  80. // Release unmanaged resources.
  81. UninitializeD3D9();
  82. _disposed = true;
  83. }
  84. }
  85. #endregion
  86. #region Methods
  87. /// <summary>
  88. /// Initializes the Direct3D 9 device.
  89. /// </summary>
  90. private static void InitializeD3D9()
  91. {
  92. lock (_d3d9Lock)
  93. {
  94. _referenceCount++;
  95. if (_referenceCount == 1)
  96. _d3D9 = new D3D9();
  97. }
  98. }
  99. /// <summary>
  100. /// Un-initializes the Direct3D 9 device, if no longer needed.
  101. /// </summary>
  102. private static void UninitializeD3D9()
  103. {
  104. lock (_d3d9Lock)
  105. {
  106. _referenceCount--;
  107. if (_referenceCount == 0)
  108. {
  109. _d3D9.Dispose();
  110. _d3D9 = null;
  111. }
  112. }
  113. }
  114. private void ThrowIfDisposed()
  115. {
  116. if (_disposed)
  117. throw new ObjectDisposedException(GetType().FullName);
  118. }
  119. /// <summary>
  120. /// Invalidates the front buffer. (Needs to be called when the back buffer has changed.)
  121. /// </summary>
  122. public void Invalidate()
  123. {
  124. ThrowIfDisposed();
  125. if (_backBuffer != null)
  126. {
  127. Lock();
  128. AddDirtyRect(new Int32Rect(0, 0, PixelWidth, PixelHeight));
  129. Unlock();
  130. }
  131. }
  132. /// <summary>
  133. /// Sets the back buffer of the <see cref="D3D11Image"/>.
  134. /// </summary>
  135. /// <param name="texture">The Direct3D 11 texture to be used as the back buffer.</param>
  136. public void SetBackBuffer(Texture2D texture)
  137. {
  138. ThrowIfDisposed();
  139. var previousBackBuffer = _backBuffer;
  140. // Create shared texture on Direct3D 9 device.
  141. _backBuffer = _d3D9.GetSharedTexture(texture);
  142. if (_backBuffer != null)
  143. {
  144. // Set texture as new back buffer.
  145. using (Surface surface = _backBuffer.GetSurfaceLevel(0))
  146. {
  147. Lock();
  148. SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer);
  149. Unlock();
  150. }
  151. }
  152. else
  153. {
  154. // Reset back buffer.
  155. Lock();
  156. SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero);
  157. Unlock();
  158. }
  159. if (previousBackBuffer != null)
  160. previousBackBuffer.Dispose();
  161. }
  162. #endregion
  163. }
  164. }