void InitializeDevice() { try { EnsureD3D9(); #if TEST_Direct3D9Ex // 2011.4.26 yyagi // Direct3D9.DeviceExを呼ぶ際(IDirect3D9Ex::CreateDeviceExを呼ぶ際)、 // フルスクリーンモードで初期化する場合はDisplayModeEx(D3DDISPLAYMODEEX *pFullscreenDisplayMode)に // 適切な値を設定する必要あり。 // 一方、ウインドウモードで初期化する場合は、D3DDISPLAYMODEEXをNULLにする必要があるが、 // DisplayModeExがNULL不可と定義されているため、DeviceExのoverloadの中でDisplayModeExを引数に取らないものを // 使う。(DeviceEx側でD3DDISPLAYMODEEXをNULLにしてくれる) // 結局、DeviceExの呼び出しの際に、フルスクリーンかどうかで場合分けが必要となる。 if (CurrentSettings.Direct3D9.PresentParameters.Windowed == false) { DisplayModeEx fullScreenDisplayMode = new DisplayModeEx(); fullScreenDisplayMode.Width = CurrentSettings.Direct3D9.PresentParameters.BackBufferWidth; fullScreenDisplayMode.Height = CurrentSettings.Direct3D9.PresentParameters.BackBufferHeight; fullScreenDisplayMode.RefreshRate = CurrentSettings.Direct3D9.PresentParameters.FullScreenRefreshRateInHertz; fullScreenDisplayMode.Format = CurrentSettings.Direct3D9.PresentParameters.BackBufferFormat; Device = new SlimDX.Direct3D9.DeviceEx(Direct3D9Object, CurrentSettings.Direct3D9.AdapterOrdinal, CurrentSettings.Direct3D9.DeviceType, game.Window.Handle, CurrentSettings.Direct3D9.CreationFlags, CurrentSettings.Direct3D9.PresentParameters, fullScreenDisplayMode); } else { Device = new SlimDX.Direct3D9.DeviceEx(Direct3D9Object, CurrentSettings.Direct3D9.AdapterOrdinal, CurrentSettings.Direct3D9.DeviceType, game.Window.Handle, CurrentSettings.Direct3D9.CreationFlags, CurrentSettings.Direct3D9.PresentParameters); } Device.MaximumFrameLatency = 1; #else Device = new DeviceCache(new SlimDX.Direct3D9.Device(Direct3D9Object, CurrentSettings.Direct3D9.AdapterOrdinal, CurrentSettings.Direct3D9.DeviceType, game.Window.Handle, CurrentSettings.Direct3D9.CreationFlags, CurrentSettings.Direct3D9.PresentParameters)); #endif if (Result.Last == SlimDX.Direct3D9.ResultCode.DeviceLost) { deviceLost = true; return; } #if TEST_Direct3D9Ex Device.MaximumFrameLatency = 1; // yyagi #endif } catch (Exception e) { throw new DeviceCreationException("Could not create graphics device.", e); } PropogateSettings(); UpdateDeviceStats(); game.Initialize(); game.LoadContent(); }
/// <summary> /// Allocates DirectX resources and attaches to render events. /// </summary> private void EnableStats() { Log("Try get Device"); _device = SkinContext.Device; if (_device == null) { return; } _swapChain = _device.GetSwapChain(0); _swapChainEx = new SwapChain9Ex(_swapChain.NativePointer); _line = new Line(_device) { Width = 2.5f, Antialias = true }; _fontSprite = new Sprite(_device); _font = new Font(_device, TEXT_SIZE, 0, FontWeight.Normal, 0, false, FontCharacterSet.Default, FontPrecision.Default, FontQuality.ClearTypeNatural, FontPitchAndFamily.DontCare, "tahoma"); // Get device info _adapterDisplayModeEx = SkinContext.Direct3D.GetAdapterDisplayModeEx(0); Log("Screen height {0} at refresh rate {1} Hz", _adapterDisplayModeEx.Height, _adapterDisplayModeEx.RefreshRate); Log("Attach render events"); SkinContext.DeviceSceneBegin += BeginScene; SkinContext.DeviceSceneEnd += EndScene; _statsEnabled = true; }
private void StartD3D() { if (DX10ImageSource.ActiveClients != 0) { return; } D3DContext = new Direct3DEx(); PresentParameters presentparams = new PresentParameters(); presentparams.Windowed = true; presentparams.SwapEffect = SwapEffect.Discard; presentparams.DeviceWindowHandle = GetDesktopWindow(); presentparams.PresentationInterval = PresentInterval.Default; DisplayModeEx[] mode = new DisplayModeEx[0]; PresentParameters[] presentParams = new PresentParameters[] { presentparams }; DX10ImageSource.D3DDevice = new DeviceEx(D3DContext, 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.FpuPreserve, presentParams, mode); }
void InitD3D9() { if (NumActiveImages == 0) { D3DContext = new Direct3DEx(); PresentParameters presentparams = new PresentParameters(); presentparams.Windowed = settings.IsWindowed; presentparams.SwapEffect = SwapEffect.Discard; presentparams.DeviceWindowHandle = GetDesktopWindow(); presentparams.PresentationInterval = PresentInterval.Immediate; if (!settings.IsWindowed) { presentparams.FullScreenRefreshRateInHertz = 120; presentparams.BackBufferHeight = settings.ScreenHeight; presentparams.BackBufferWidth = settings.ScreenWidth; presentparams.BackBufferFormat = TranslateFormat(settings.Format); DisplayModeEx fullScreen = new DisplayModeEx() { Format = TranslateFormat(settings.Format), Width = settings.ScreenWidth, Height = settings.ScreenHeight, RefreshRate = 120 }; D3DDevice = new DeviceEx(D3DContext, 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.FpuPreserve, presentparams, fullScreen); } else D3DDevice = new DeviceEx(D3DContext, 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.FpuPreserve, presentparams); } }
private void InitDevice() { // Use D3DEx for Vista/Win7+ _is3D9Ex = false; if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6) { _is3D9Ex = true; } DeviceType devType = DeviceType.Hardware; int adapterOrdinal = 0; Direct3D d3d = null; Direct3DEx d3dex = null; if (_is3D9Ex) { try { // Create Ex, fallback if it fails d3dex = new Direct3DEx(); } catch { d3d = new Direct3D(); _is3D9Ex = false; } } else d3d = new Direct3D(); // Look for PerfHUD AdapterCollection adapters = (_is3D9Ex ? d3dex : d3d).Adapters; foreach (AdapterInformation adap in adapters) { if (adap.Details.Description.Contains("PerfH")) { adapterOrdinal = adap.Adapter; devType = DeviceType.Reference; } } foreach (var item in adapters[adapterOrdinal].GetDisplayModes(Format.X8R8G8B8)) { string val = item.Width + "x" + item.Height; bool found = false; for (int i = 0; i < ValidResolutions.Count; i++) { if (ValidResolutions[i].Equals(val)) { found = true; break; } } if(!found) ValidResolutions.Add(val); } // Get resolution Size res = GetResolution(); RenderSize = res; form.ClientSize = res; // Set present parameters _pp = new PresentParameters(); _pp.Windowed = r_fs.Integer==0?true:false; _pp.SwapEffect = SwapEffect.Discard; _pp.EnableAutoDepthStencil = true; _pp.AutoDepthStencilFormat = Format.D24S8; _pp.PresentationInterval = (r_vsync.Integer==1? PresentInterval.One: PresentInterval.Immediate); _pp.Multisample = MultiSampling; _pp.BackBufferWidth = RenderSize.Width; _pp.BackBufferHeight = RenderSize.Height; _pp.BackBufferFormat = Format.X8R8G8B8; _pp.DeviceWindowHandle = form.Handle; // Handle Capabilities Capabilities caps = adapters[adapterOrdinal].GetCaps(devType); CreateFlags createFlags = CreateFlags.SoftwareVertexProcessing; // Got hardare vertex? if ((caps.DeviceCaps & DeviceCaps.HWTransformAndLight) == DeviceCaps.HWTransformAndLight) { createFlags = CreateFlags.HardwareVertexProcessing; // Support pure device? if ((caps.DeviceCaps & DeviceCaps.PureDevice) == DeviceCaps.PureDevice) createFlags |= CreateFlags.PureDevice; } createFlags |= CreateFlags.FpuPreserve; // Create d3d device + behemoth fallback try { if (_is3D9Ex) { if (r_fs.Bool) { DisplayModeEx dsp = new DisplayModeEx(); dsp.Width = _pp.BackBufferWidth; dsp.Height = _pp.BackBufferHeight; dsp.Format = _pp.BackBufferFormat; dsp.RefreshRate = 60; _pp.FullScreenRefreshRateInHertz = 60; //DisplayModeEx dispMode = d3dex.GetAdapterDisplayModeEx(adapterOrdinal); //RenderSize = new Size(dispMode.Width, dispMode.Height); //form.ClientSize = new Size(dispMode.Width, dispMode.Height); //_pp.BackBufferFormat = dispMode.Format; //_pp.BackBufferWidth = dispMode.Width; //_pp.BackBufferHeight = dispMode.Height; //_pp.FullScreenRefreshRateInHertz = dispMode.RefreshRate; device = new DeviceEx(d3dex, adapterOrdinal, devType, form.Handle, createFlags, _pp, dsp); } else device = new DeviceEx(d3dex, adapterOrdinal, devType, form.Handle, createFlags, _pp); } else device = new Device(d3d, adapterOrdinal, devType, form.Handle, createFlags, _pp); } catch (Direct3D9Exception ex) { if (ex.ResultCode == ResultCode.NotAvailable) { // Try again with different settings RenderSize = new Size(800, 600); form.ClientSize = new Size(800, 600); _pp.BackBufferWidth = 800; _pp.BackBufferHeight = 600; _pp.BackBufferCount = 1; _pp.SwapEffect = SwapEffect.Discard; createFlags &= ~(CreateFlags.PureDevice | CreateFlags.HardwareVertexProcessing); createFlags |= CreateFlags.SoftwareVertexProcessing; try { if (_is3D9Ex) { if (r_fs.Bool) { DisplayModeEx dispMode = d3dex.GetAdapterDisplayModeEx(adapterOrdinal); RenderSize = new Size(dispMode.Width, dispMode.Height); form.ClientSize = new Size(dispMode.Width, dispMode.Height); _pp.BackBufferFormat = dispMode.Format; _pp.BackBufferWidth = dispMode.Width; _pp.BackBufferHeight = dispMode.Height; _pp.FullScreenRefreshRateInHertz = dispMode.RefreshRate; device = new DeviceEx(d3dex, adapterOrdinal, devType, form.Handle, createFlags, _pp, dispMode); } else device = new DeviceEx(d3dex, adapterOrdinal, devType, form.Handle, createFlags, _pp); } else device = new Device(d3d, adapterOrdinal, devType, form.Handle, createFlags, _pp); } catch (Exception ex2) { if (_is3D9Ex) { // 3. fallback.. disable ex _is3D9Ex = false; Size ress = GetResolution(); RenderSize = ress; form.ClientSize = ress; _pp.BackBufferWidth = ress.Width; _pp.BackBufferHeight = ress.Height; _pp.BackBufferCount = 1; _pp.SwapEffect = SwapEffect.Discard; d3dex.Dispose(); device = new Device(new Direct3D(), adapterOrdinal, devType, form.Handle, createFlags, _pp); } else throw ex2; } } else throw ex; } // Load main shader if (System.IO.File.Exists(System.Windows.Forms.Application.StartupPath+"/client/render/simple.fx")) { string shaderOutput = null; SlimDX.Configuration.ThrowOnError = false; effect = Effect.FromFile(device, System.Windows.Forms.Application.StartupPath + "/client/render/simple.fx", null, null, null, ShaderFlags.None, null, out shaderOutput); if (shaderOutput != null && shaderOutput != "" && effect == null) { // Shader problem :..( System.Windows.Forms.MessageBox.Show(shaderOutput, "Shader Compilation error :(", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); Shutdown(); return; } SlimDX.Configuration.ThrowOnError = true; } else { System.Console.WriteLine("Could not find shader.."); } // Add fonts if (Fonts.Count == 0) { System.Drawing.Text.PrivateFontCollection col = new System.Drawing.Text.PrivateFontCollection(); col.AddFontFile(System.Windows.Forms.Application.StartupPath + @"\data\gui\Candara.ttf"); // UI Title System.Drawing.Font localFont = new System.Drawing.Font(col.Families[0], 10.5f, System.Drawing.FontStyle.Regular); SlimDX.Direct3D9.Font font = new SlimDX.Direct3D9.Font(Renderer.Instance.device, localFont); Fonts.Add("title", font); // FPS, etc. font = new SlimDX.Direct3D9.Font(device, new System.Drawing.Font("Lucidia Console", 10f, System.Drawing.FontStyle.Regular)); Fonts.Add("diag", font); // Labels and UI elements localFont = new System.Drawing.Font(col.Families[0], 10f, System.Drawing.FontStyle.Regular); font = new SlimDX.Direct3D9.Font(Renderer.Instance.device, localFont); Fonts.Add("label", font); localFont = new System.Drawing.Font(col.Families[0], 15f, System.Drawing.FontStyle.Regular); font = new SlimDX.Direct3D9.Font(Renderer.Instance.device, localFont); Fonts.Add("biglabel", font); localFont = new System.Drawing.Font(col.Families[0], 23f, System.Drawing.FontStyle.Regular); font = new SlimDX.Direct3D9.Font(Renderer.Instance.device, localFont); Fonts.Add("biggerlabel", font); // Textbox col = new System.Drawing.Text.PrivateFontCollection(); col.AddFontFile(System.Windows.Forms.Application.StartupPath + @"\data\gui\dina10px.ttf"); localFont = new System.Drawing.Font(col.Families[0], 12f, FontStyle.Regular); font = new SlimDX.Direct3D9.Font(Renderer.Instance.device, localFont); Fonts.Add("textbox", font); } // Init backbuffers, etc. OnResetDevice(); bloom = new Bloom(this); tonemap = new ToneMap(this); // Init windowing system WindowManager.Instance.Init(device); }
// private bool IsDeviceReady(Device device) // { // var cooplevel = device.TestCooperativeLevel(); // if (cooplevel.Code != ResultCode.Success.Code) // { // return false; // } // return true; // } private int ResetExHook(IntPtr devicePtr, ref PresentParameters presentparameters, DisplayModeEx displayModeEx) { var hresult = Result.Ok.Code; var device = (DeviceEx)devicePtr; try { if (!_hooksStarted) { hresult = Direct3DDeviceEx_ResetExHook.Original(devicePtr, ref presentparameters, displayModeEx); return(hresult); } ClearData(); hresult = Direct3DDeviceEx_ResetExHook.Original(devicePtr, ref presentparameters, displayModeEx); if (_currentDevice != devicePtr) { _hooksStarted = false; _currentDevice = devicePtr; } } catch (Exception ex) { DebugMessage(ex.ToString()); } return(hresult); }
public unsafe void ResetEx(ref PresentParameters presentationParameters, DisplayModeEx fullScreenDisplayMode) { ResetEx(ref presentationParameters, &fullScreenDisplayMode); }
public IDirect3DDevice9Ex CreateDeviceEx(int adapter, DeviceType deviceType, IntPtr focusWindow, CreateFlags createFlags, PresentParameters presentationParameters, DisplayModeEx fullScreenDisplayMode) { return(CreateDeviceEx(adapter, deviceType, focusWindow, (int)createFlags, new[] { presentationParameters }, new[] { fullScreenDisplayMode })); }
private int ResetExHook(IntPtr deviceptr, ref PresentParameters presentparameters, DisplayModeEx displayModeEx) { try { DeviceEx device = (DeviceEx)deviceptr; if (hooksStarted) { ClearD3D9Data(); } device.ResetEx(ref presentparameters, displayModeEx); if (hooksStarted) { if (currentDevice == null && !targetAcquired) { currentDevice = device; targetAcquired = true; hasD3D9Ex = true; } if (currentDevice == device) { SetupD3D9(device); } } } catch (Exception ex) { DebugMessage(ex.ToString()); } finally { } return(Result.Ok.Code); }