private void CreateDevice() { if (m_device != null) { return; } var param = new D3DPRESENT_PARAMETERS { Windowed = 1, Flags = ((short)D3DPRESENTFLAG.D3DPRESENTFLAG_VIDEO), BackBufferFormat = D3DFORMAT.D3DFMT_X8R8G8B8, SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_COPY }; /* The COM pointer to our D3D Device */ IntPtr dev; /* Windows Vista runs much more performant with the IDirect3DDevice9Ex */ if (IsVistaOrBetter) { m_d3dEx.CreateDeviceEx(0, D3DDEVTYPE.D3DDEVTYPE_HAL, m_hWnd, CreateFlags.D3DCREATE_SOFTWARE_VERTEXPROCESSING | CreateFlags.D3DCREATE_MULTITHREADED, ref param, IntPtr.Zero, out dev); } else/* Windows XP */ { m_d3d.CreateDevice(0, D3DDEVTYPE.D3DDEVTYPE_HAL, m_hWnd, CreateFlags.D3DCREATE_SOFTWARE_VERTEXPROCESSING | CreateFlags.D3DCREATE_MULTITHREADED, ref param, out dev); } m_device = (IDirect3DDevice9)Marshal.GetObjectForIUnknown(dev); Marshal.Release(dev); }
public override void Initialize() { // Create a new 'window' var window = new Form(); // Grabbed from d3d9.h const uint D3D_SDK_VERSION = 32; // Create the IDirect3D* object. IntPtr direct3D = Direct3DCreate9(D3D_SDK_VERSION); // Make sure it's valid. (Should always be valid....) if (direct3D == IntPtr.Zero) throw new Exception("Failed to create D3D."); // Setup some present params... var d3dpp = new D3DPRESENT_PARAMETERS { Windowed = true, SwapEffect = 1, BackBufferFormat = 0 }; IntPtr device; // CreateDevice is a vfunc of IDirect3D. Hence; why this entire thing only works in process! (Unless you // know of some way to hook funcs from out of process...) // It's the 16th vfunc btw. // Check d3d9.h var createDevice = Pulse.Magic.RegisterDelegate<IDirect3D9_CreateDevice>(Pulse.Magic.GetObjectVtableFunction(direct3D, 16)); // Pass it some vals. You can check d3d9.h for what these actually are.... if (createDevice(direct3D, 0, 1, window.Handle, 0x20, ref d3dpp, out device) < 0) throw new Exception("Failed to create device."); EndScenePointer = Pulse.Magic.GetObjectVtableFunction(device, VMT_ENDSCENE); ResetPointer = Pulse.Magic.GetObjectVtableFunction(device, VMT_RESET); // We now have a valid pointer to the device. We can release the shit we don't need now. :) // Again, the Release() funcs are virtual. Part of the IUnknown interface for COM. // They're the 3rd vfunc. (2nd index) var deviceRelease = Pulse.Magic.RegisterDelegate<D3DVirtVoid>(Pulse.Magic.GetObjectVtableFunction(device, 2)); var d3dRelease = Pulse.Magic.RegisterDelegate<D3DVirtVoid>(Pulse.Magic.GetObjectVtableFunction(direct3D, 2)); // And finally, release the device and d3d object. deviceRelease(device); d3dRelease(direct3D); // Destroy the window... window.Dispose(); // Hook endscene _endSceneDelegate = Pulse.Magic.RegisterDelegate<Direct3D9EndScene>(EndScenePointer); _endSceneHook = Pulse.Magic.Detours.CreateAndApply(_endSceneDelegate, new Direct3D9EndScene(Callback), "D9EndScene"); }
//----------------------------------------------------------------------------- // GetSwapChainPresentParameters // // Given a media type that describes the video format, fills in the // D3DPRESENT_PARAMETERS for creating a swap chain. //----------------------------------------------------------------------------- protected void GetSwapChainPresentParameters(IMFMediaType pType, out D3DPRESENT_PARAMETERS pPP) { pPP = new D3DPRESENT_PARAMETERS(); // Caller holds the object lock. int width = 0, height = 0; int d3dFormat = 0; VideoTypeBuilder pTypeHelper = null; if (m_hwnd == IntPtr.Zero) { throw new COMException("D3DPresentEngine::GetSwapChainPresentParameters", (int)HResult.MF_E_INVALIDREQUEST); } try { // Create the helper object for reading the proposed type. pTypeHelper = new VideoTypeBuilder(pType); // Get some information about the video format. pTypeHelper.GetFrameDimensions(out width, out height); pTypeHelper.GetFourCC(out d3dFormat); pPP.BackBufferWidth = width; pPP.BackBufferHeight = height; pPP.Windowed = true; pPP.SwapEffect = D3DSWAPEFFECT.Copy; pPP.BackBufferFormat = (D3DFORMAT)d3dFormat; pPP.hDeviceWindow = m_hwnd; pPP.Flags = D3DPRESENTFLAG.Video; pPP.PresentationInterval = D3DPRESENT_INTERVAL.Default; D3DDEVICE_CREATION_PARAMETERS dparams; m_pDevice.GetCreationParameters(out dparams); if (dparams.DeviceType != D3DDEVTYPE.HAL) { pPP.Flags |= D3DPRESENTFLAG.LockableBackbuffer; } } catch { } //SafeRelease(pTypeHelper); }
private void CreateDevice() { if (m_device != null) { return; } var param = new D3DPRESENT_PARAMETERS { Windowed = 1, Flags = ((short)D3DPRESENTFLAG.D3DPRESENTFLAG_VIDEO), BackBufferFormat = D3DFORMAT.D3DFMT_X8R8G8B8, SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_COPY }; /* The COM pointer to our D3D Device */ IntPtr dev; /* Windows Vista runs much more performant with the IDirect3DDevice9Ex */ int hr = 0; if (m_d3dEx != null) { hr = m_d3dEx.CreateDeviceEx(0, D3DDEVTYPE.D3DDEVTYPE_HAL, m_hWnd, CreateFlags.D3DCREATE_SOFTWARE_VERTEXPROCESSING | CreateFlags.D3DCREATE_MULTITHREADED, ref param, IntPtr.Zero, out dev); } else/* Windows XP */ { hr = m_d3d.CreateDevice(0, D3DDEVTYPE.D3DDEVTYPE_HAL, m_hWnd, CreateFlags.D3DCREATE_SOFTWARE_VERTEXPROCESSING | CreateFlags.D3DCREATE_MULTITHREADED, ref param, out dev); } if (dev == IntPtr.Zero) { throw new WPFMediaKitException($"Cannot create D3D device ({hr:X}). Do you have D3D acceleration enabled for your graphics card?"); } m_device = (IDirect3DDevice9)Marshal.GetObjectForIUnknown(dev); Marshal.Release(dev); }
// ReSharper restore FieldCanBeMadeReadOnly.Local // ReSharper restore MemberCanBePrivate.Local // ReSharper restore InconsistentNaming private static void Initialize() { _wndProc = WindowProc; var wc = new WNDCLASS { lpszClassName = g_szClassName, lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_wndProc), hInstance = GetModuleHandle(IntPtr.Zero) }; RegisterClass(ref wc); g_hWnd = CreateWindowEx(0, g_szClassName, "Shader Decompiler", WS_OVERLAPPEDWINDOW, 20, 20, 800, 600, (IntPtr)0, (IntPtr)0, GetModuleHandle(IntPtr.Zero), (IntPtr)0); g_D3D = Direct3DCreate9(D3D_SDK_VERSION); g_D3DPresentParameters = new D3DPRESENT_PARAMETERS { AutoDepthStencilFormat = D3DFMT_D24S8, BackBufferCount = 1, BackBufferFormat = D3DFMT_X8R8G8B8, BackBufferWidth = 32, BackBufferHeight = 32, EnableAutoDepthStencil = false, Flags = 0, FullScreen_RefreshRateInHz = 0, hDeviceWindow = g_hWnd, PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE, SwapEffect = D3DSWAPEFFECT_DISCARD, Windowed = true }; fixed(D3DPRESENT_PARAMETERS *pD3DPresentParams = &g_D3DPresentParameters) { fixed(IDirect3DDevice9 **ppD3DDevice = &g_D3DDevice) { IDirect3D9_CreateDevice(g_D3D, 0, D3DDEVTYPE_NULLREF, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, pD3DPresentParams, ppD3DDevice); } } }
private void OnDeviceCheck() { if (_device != null) { return; } _size = _window.Size; _d3dpp = CreatePresentParams(_window.Handle); _monitorId = _window.MonitorId; var adapterId = _window.AdapterId; using (var d3d = new Direct3D9()) { D3DCAPS9 caps; d3d.GetDeviceCaps(adapterId, D3DDEVTYPE.D3DDEVTYPE_HAL, out caps).CheckError(); var flags = D3DCREATE.D3DCREATE_NOWINDOWCHANGES | D3DCREATE.D3DCREATE_FPU_PRESERVE;// CreateFlags.MultiThreaded; if ((caps.DevCaps & D3DDEVCAPS.D3DDEVCAPS_HWTRANSFORMANDLIGHT) != 0) { flags |= D3DCREATE.D3DCREATE_HARDWARE_VERTEXPROCESSING; } else { flags |= D3DCREATE.D3DCREATE_SOFTWARE_VERTEXPROCESSING; } Logger.Debug("Direct3D: create Device, threadId={0}", Thread.CurrentThread.ManagedThreadId); _device = d3d.CreateDevice( adapterId, D3DDEVTYPE.D3DDEVTYPE_HAL, _window.Handle, flags, _d3dpp); } //TODO: check //_device.DeviceResizing += (s, e) => e.Cancel = true; //_device.DeviceReset += Device_OnDeviceReset; OnDeviceInit(); }
private D3DPRESENT_PARAMETERS CreatePresentParams(IntPtr hwnd) { var format = D3DFORMAT.D3DFMT_A8R8G8B8; // mode.Format; var d3dpp = new D3DPRESENT_PARAMETERS(); d3dpp.hDeviceWindow = hwnd; d3dpp.Windowed = true; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL.D3DPRESENT_INTERVAL_ONE;// PresentInterval.One;//vbSync ? PresentInterval.One : PresentInterval.Immediate;//PresentInterval.Default; //make sure you are NOT using flipping if you are in windowed mode. //In windowed mode, you share the current video mode of the applications running. //Unfortunately, you have to use the slower blitting process. d3dpp.SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_DISCARD;//vbSync ? SwapEffect.Flip : SwapEffect.Discard;//SwapEffect.Discard; d3dpp.BackBufferCount = 1; d3dpp.BackBufferFormat = format; d3dpp.BackBufferWidth = _size.Width > 0 ? _size.Width : 1; d3dpp.BackBufferHeight = _size.Height > 0 ? _size.Height : 1; d3dpp.EnableAutoDepthStencil = false; //d3dpp.MultiSample = MultiSampleType.NonMaskable; d3dpp.Flags = D3DPRESENTFLAG.VIDEO; // PresentFlag.DeviceClip == single display mode return(d3dpp); }
//----------------------------------------------------------------------------- // CreateD3DDevice // // Creates the Direct3D device. //----------------------------------------------------------------------------- protected void CreateD3DDevice() { IntPtr hwnd = IntPtr.Zero; IntPtr hMonitor = IntPtr.Zero; int uAdapterID = 0; D3DCREATE vp = 0; D3DCAPS9 ddCaps; IDirect3DDevice9Ex pDevice = null; // Hold the lock because we might be discarding an exisiting device. lock (this) { if ((m_pD3D9 == null) || (m_pDeviceManager == null)) { throw new COMException("D3DPresentEngine::CreateD3DDevice", (int)HResult.MF_E_NOT_INITIALIZED); } hwnd = GetDesktopWindow(); // Note: The presenter creates additional swap chains to present the // video frames. Therefore, it does not use the device's implicit // swap chain, so the size of the back buffer here is 1 x 1. D3DPRESENT_PARAMETERS pp = new D3DPRESENT_PARAMETERS(); pp.BackBufferWidth = 1; pp.BackBufferHeight = 1; pp.Windowed = true; pp.SwapEffect = D3DSWAPEFFECT.Copy; pp.BackBufferFormat = D3DFORMAT.Unknown; pp.hDeviceWindow = hwnd; pp.Flags = D3DPRESENTFLAG.Video; pp.PresentationInterval = D3DPRESENT_INTERVAL.Default; // Find the monitor for this window. if (m_hwnd != IntPtr.Zero) { hMonitor = MonitorFromWindow(m_hwnd, MonitorFlags.DefaultToNearest); // Find the corresponding adapter. FindAdapter(m_pD3D9 as IDirect3D9, hMonitor, out uAdapterID); } // Get the device caps for this adapter. m_pD3D9.GetDeviceCaps(uAdapterID, D3DDEVTYPE.HAL, out ddCaps); if ((ddCaps.DevCaps & D3DDEVCAPS.HWTRANSFORMANDLIGHT) > 0) { vp = D3DCREATE.HardwareVertexProcessing; } else { vp = D3DCREATE.SoftwareVertexProcessing; } // Create the device. m_pD3D9.CreateDeviceEx( uAdapterID, D3DDEVTYPE.HAL, pp.hDeviceWindow, vp | D3DCREATE.NoWindowChanges | D3DCREATE.MultiThreaded | D3DCREATE.FPU_PRESERVE, pp, null, out pDevice ); // Get the adapter display mode. m_pD3D9.GetAdapterDisplayMode(uAdapterID, out m_DisplayMode); // Reset the D3DDeviceManager with the new device m_pDeviceManager.ResetDevice(pDevice, m_DeviceResetToken); if (m_pDevice != pDevice) { SafeRelease(m_pDevice); m_pDevice = pDevice; } //SafeRelease(pDevice); } }
private void MainWindow_Loaded(object sender, RoutedEventArgs e) { var hr = Direct3DCreate9Ex(D3D_SDK_VERSION, out var direct3D9Ex); m_d3dEx = direct3D9Ex; var adapterMonitor = direct3D9Ex.GetAdapterMonitor(0); m_hWnd = GetDesktopWindow(); var param = new D3DPRESENT_PARAMETERS { Windowed = 1, Flags = ((short)D3DPRESENTFLAG.D3DPRESENTFLAG_VIDEO), /* * D3DFMT_R8G8B8:表示一个24位像素,从左开始,8位分配给红色,8位分配给绿色,8位分配给蓝色。 * * D3DFMT_X8R8G8B8:表示一个32位像素,从左开始,8位不用,8位分配给红色,8位分配给绿色,8位分配给蓝色。 * * D3DFMT_A8R8G8B8:表示一个32位像素,从左开始,8位为ALPHA通道,8位分配给红色,8位分配给绿色,8位分配给蓝色。 * * D3DFMT_A16B16G16R16F:表示一个64位浮点像素,从左开始,16位为ALPHA通道,16位分配给蓝色,16位分配给绿色,16位分配给红色。 * * D3DFMT_A32B32G32R32F:表示一个128位浮点像素,从左开始,32位为ALPHA通道,32位分配给蓝色,32位分配给绿色,32位分配给红色。 */ //BackBufferFormat = D3DFORMAT.D3DFMT_X8R8G8B8, //SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_COPY SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_DISCARD, hDeviceWindow = GetDesktopWindow(), // 添加 PresentationInterval = (int)D3D9.PresentInterval.Default, }; /* The COM pointer to our D3D Device */ IntPtr dev; m_d3dEx.CreateDeviceEx(0, D3DDEVTYPE.D3DDEVTYPE_HAL, m_hWnd, Direct3D.CreateFlags.D3DCREATE_HARDWARE_VERTEXPROCESSING | Direct3D.CreateFlags.D3DCREATE_MULTITHREADED | Direct3D.CreateFlags.D3DCREATE_FPU_PRESERVE, ref param, IntPtr.Zero, out dev); m_device = (IDirect3DDevice9)Marshal.GetObjectForIUnknown(dev); // 只是减少引用计数而已,现在换成 m_device 了 Marshal.Release(dev); hr = m_device.TestCooperativeLevel(); var pDevice = dev; D3D11.Texture2D d3d11Texture2D = CreateRenderTarget(); //SetRenderTarget(d3d11Texture2D); var format = TranslateFormat(TranslateFormat(d3d11Texture2D)); var dxgiResource = d3d11Texture2D.QueryInterface <DXGI.Resource>(); var pSharedHandle = dxgiResource.SharedHandle; hr = m_device.CreateTexture(ImageWidth, ImageHeight, 1, 1, format, 0, out m_privateTexture, ref pSharedHandle); hr = m_privateTexture.GetSurfaceLevel(0, out m_privateSurface); var backBuffer = Marshal.GetIUnknownForObject(m_privateSurface); var surface = new D3D9.Surface(backBuffer); var queryInterface = surface.QueryInterface <D3D9.Surface>(); //// 只是减少引用计数而已 //Marshal.Release(backBuffer); //hr = m_device.SetTexture(0, m_privateTexture); var texturePtr = Marshal.GetIUnknownForObject(m_privateTexture); //Marshal.Release(texturePtr); //var byteList = new byte[32 * 10]; //for (int i = 0; i < byteList.Length; i++) //{ // byteList[i] = (byte)i; //} //unsafe //{ // fixed (void* p = byteList) // { // Buffer.MemoryCopy(p, (void*) texturePtr,0,320); // } //} //var d2dFactory = new SharpDX.Direct2D1.Factory(); //Texture2D backBufferTexture2D = new Texture2D(texturePtr); //var d2dRenderTarget = new RenderTarget(d2dFactory, new SharpDX.DXGI.Surface(backBuffer), // new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown,AlphaMode.Premultiplied))); //d2dRenderTarget.BeginDraw(); //d2dRenderTarget.Clear(new RawColor4(1,0,0.5f,1)); //d2dRenderTarget.EndDraw(); D3DImage.Lock(); D3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, backBuffer, true); D3DImage.Unlock(); Render(); string s = "123"; var stringBuilder = new StringBuilder(s); stringBuilder.Replace("%", "%25").Replace("#", "%23"); stringBuilder.Insert(0, "123"); stringBuilder.Insert("123".Length, "#"); }