1.首先定义一个DX操作类

     using System;
using SlimDX;
using SlimDX.Direct3D9;
using System.Windows.Interop;
using System.Windows.Media; public class DX
{
private enum DirectXStatus
{
Available,
Unavailable_RemoteSession,
Unavailable_LowTier,
Unavailable_MissingDirectX,
Unavailable_Unknown
}; public static Device Device { get; private set; }
public static bool Available { get { return DX.Device != null; } }// = false; private static DX _dx;
private static DirectXStatus _status = DirectXStatus.Unavailable_Unknown;
private static string _statusMessage = ""; [System.Runtime.InteropServices.DllImport("user32")]
private static extern int GetSystemMetrics(int smIndex);
private const int SM_REMOTESESSION = 0x1000; // device settings
private const Format _adapterFormat = Format.X8R8G8B8;
private const Format _backbufferFormat = Format.A8R8G8B8;
private const Format _depthStencilFormat = Format.D16;
private static CreateFlags _createFlags = CreateFlags.Multithreaded | CreateFlags.FpuPreserve; private Direct3D _d3d; private DX()
{
initD3D();
if (_d3d != null)
initDevice();
//if (!DX.Available)
// MessageBox.Show("DirectX硬件加速不可用!\n\n" + _statusMessage, "", MessageBoxButton.OK, MessageBoxImage.Warning);
} ~DX()
{
if (DX.Device != null)
if (!DX.Device.Disposed)
DX.Device.Dispose();
if (_d3d != null)
if (!_d3d.Disposed)
_d3d.Dispose();
} public static void Init()
{
if (_dx == null)
_dx = new DX();
} private void initD3D()
{
if (_d3d != null)
return; _status = DirectXStatus.Unavailable_Unknown; //// assume that we can't run at all under terminal services
if (GetSystemMetrics(SM_REMOTESESSION) != )
{
_status = DirectXStatus.Unavailable_RemoteSession;
return;
} int renderingTier = (RenderCapability.Tier >> );
if (renderingTier < )
{
_status = DirectXStatus.Unavailable_LowTier;
_statusMessage = "low tier";
return;//注意:发现某些集成显卡,在这里出去!!
} try
{
_d3d = new Direct3DEx();
}
catch
{
try
{
_d3d = new Direct3D();
}
catch (Direct3DX9NotFoundException dfe)
{
_status = DirectXStatus.Unavailable_MissingDirectX;
_statusMessage = "Direct3DX9 Not Found\n" + dfe.Message;
return;
}
catch (Exception e)
{
_status = DirectXStatus.Unavailable_Unknown;
_statusMessage = e.Message;
return;
}
} bool ok;
Result result; ok = _d3d.CheckDeviceType(, DeviceType.Hardware, _adapterFormat, _backbufferFormat, true, out result);
if (!ok)
{
//Debug.WriteLine("*** failed to CheckDeviceType");
//MessageBox.Show("Failed to CheckDeviceType");
return;
} ok = _d3d.CheckDepthStencilMatch(, DeviceType.Hardware, _adapterFormat, _backbufferFormat, _depthStencilFormat, out result);
if (!ok)
{
//Debug.WriteLine("*** failed to CheckDepthStencilMatch");
_statusMessage = "Failed to CheckDepthStencilMatch";
return;
} Capabilities deviceCaps = _d3d.GetDeviceCaps(, DeviceType.Hardware);
if ((deviceCaps.DeviceCaps & DeviceCaps.HWTransformAndLight) != )
_createFlags |= CreateFlags.HardwareVertexProcessing;
else
_createFlags |= CreateFlags.SoftwareVertexProcessing; _status = DirectXStatus.Available;
} private void initDevice()
{
if (_status != DirectXStatus.Available)
return; HwndSource hwnd = new HwndSource(, , , , , , , "SlimDX_Wnd", IntPtr.Zero);
PresentParameters pp = new PresentParameters();
//pp.SwapEffect = SwapEffect.Copy;
//pp.DeviceWindowHandle = hwnd.Handle;
pp.Windowed = true;
pp.PresentFlags = PresentFlags.Video;
pp.SwapEffect = SwapEffect.Discard;
//pp.BackBufferCount = 1;
//pp.BackBufferWidth = 320;
//pp.BackBufferHeight = 240;
//pp.BackBufferFormat = _backbufferFormat;
//pp.AutoDepthStencilFormat = _depthStencilFormat;
try
{
DeviceType deviceType = DeviceType.Hardware;
if (_d3d is Direct3DEx)
DX.Device = new DeviceEx((Direct3DEx)_d3d, , deviceType, hwnd.Handle, _createFlags, pp);
else
DX.Device = new Device(_d3d, , deviceType, hwnd.Handle, _createFlags, pp);
}
catch (Exception ex)
{
//Debug.WriteLine("Exception in Direct3DReset " + ex.StackTrace);
//Debug.WriteLine("Exception in Direct3DReset " + ex.Message);
}
}
}

2.定义准备显卡硬件,和释放显卡硬件方法

定义一些变量

       /// <summary>
/// 离屏表面
/// </summary>
private Surface _offscrn;
/// <summary>
/// 交换链
/// </summary>
private SwapChain _swapChain;
private D3DImage _d3dImage = null;
      /// <summary>
/// 准备DirectX显卡硬件
/// </summary>
private bool prepareHardware(VideoFormat videoFormat, int videoWidth, int videoHeight)//, VideoFormat videoFormat)
{
if (!DX.Available)
return true; try
{
SlimDX.Direct3D9.Format format = SlimDX.Direct3D9.Format.A8R8G8B8;
if (videoFormat == VideoFormat.Yuv420)
format = (SlimDX.Direct3D9.Format)0x32315659;
if (_offscrn != null)
if (videoWidth == _offscrn.Description.Width && videoHeight == _offscrn.Description.Height && _offscrn.Description.Format == format)
return true; releaseHardware();
_offscrn = Surface.CreateOffscreenPlain(DX.Device, videoWidth, videoHeight, format, Pool.Default);
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.PresentFlags = PresentFlags.Video;
pp.SwapEffect = SwapEffect.Discard;
pp.BackBufferCount = ;
pp.BackBufferWidth = videoWidth;
pp.BackBufferHeight = videoHeight;
_swapChain = new SwapChain(DX.Device, pp);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 释放DirectX显卡硬件
/// </summary>
private void releaseHardware()
{
if (!DX.Available)
return;
if (_offscrn != null)
if (!_offscrn.Disposed)
_offscrn.Dispose();
_offscrn = null;
if (_swapChain != null)
if (!_swapChain.Disposed)
_swapChain.Dispose();
_swapChain = null;
}

3.

 private void drawFrame(VideoFormat videoFormat, int width, int height, IntPtr Y, IntPtr U, IntPtr V)
{
if (!prepareHardware(videoFormat, width, height))
return;
if (_swapChain == null)
return; DataRectangle dr = _offscrn.LockRectangle(LockFlags.None);//在离屏表面上锁定一个矩形
drawYuv420(width, height, Y, U, V, dr.Data.DataPointer, dr.Pitch);//DataPointer 内部指针指向当前流的存储备份; Pitch 两个连续的行之间的数据的字节数
_offscrn.UnlockRectangle();//解锁矩形
using (Surface bb = _swapChain.GetBackBuffer())//从交换链中检索一个后台缓冲区
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(, , bb.Description.Width, bb.Description.Height);
_swapChain.Device.StretchRectangle(_offscrn, rect, bb, rect, TextureFilter.None);//将后台缓冲区的内容交换到前台缓冲区
_swapChain.Device.Present();//呈现后台缓冲区序列中下一个后台缓冲区的内容 _d3dImage.Lock();
_d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, bb.ComPointer);
_d3dImage.AddDirtyRect(new Int32Rect(, , _d3dImage.PixelWidth, _d3dImage.PixelHeight));
_d3dImage.Unlock();
}
} private void drawYuv420(int width, int height, IntPtr Y, IntPtr U, IntPtr V, IntPtr dest, int pitch)
{
IntPtr py = dest;
IntPtr pv = py + (pitch * height);
IntPtr pu = pv + ((pitch * height) / );
int w2 = width / , pitch2 = pitch / ;
for (int y = ; y < height; y++)
{
CopyMemory(py, Y + y * width, (uint)width);
py += pitch;
if ((y & ) != )
continue;
int offset = y / * w2;
CopyMemory(pu, U + offset, (uint)w2);
CopyMemory(pv, V + offset, (uint)w2);
pu += pitch2;
pv += pitch2;
}
}
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
private static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length);

最新文章

  1. 在output 子句和 scope_identity() 混合使用的时候的注意事项
  2. mySql 远程连接(is not allowed to connect to this MySQL server)
  3. vim(5)vim下wimrc的配置,解决中文乱码问题
  4. Linux命令学习手册-printf命令(转)
  5. CSS的魔法和魅力
  6. F5 刷新功能
  7. HW3.17
  8. 【宽搜】【并查集】Vijos P1015 十字绣
  9. USB CCID协议和PC/SC标准
  10. javaScript 设计模式系列之二:适配器模式
  11. ps删除或覆盖内容
  12. Android 4.4以上使用HttpURLConnection底层使用OkHttp实现的源码分析
  13. MYSQL数据库学习六 索引的操作
  14. 关于如何在Visual Studio上仿真调试安卓的U3D应用
  15. ucos之互斥信号量及优先级反转
  16. Unity3d如何profile模拟器
  17. Jsoup(三)-- Jsoup使用选择器语法查找DOM元素
  18. C# 调用Delphi dll
  19. Vue源码学习(零):内部原理解析
  20. Directive Controller And Link Timing In AngularJS

热门文章

  1. :before与:after伪类的应用
  2. 在js传递参数中含加号(+)的处理方式
  3. 转载: jQuery事件委托( bind() \ live() \ delegate()) [委托 和 绑定的故事]
  4. 学习memcached的一个网站
  5. Android中focusable属性的妙用——底层按钮的实现
  6. LeetCode_Unique Binary Search Trees II
  7. DHTMLEdit
  8. PowerShell 远程管理之 about_Remote_Troubleshooting
  9. CF 567C Geometric Progression
  10. Unable to resolve target &#39;android-14&#39; 解决办法