private static bool TryCreateDevice(Direct3D d3D, DeviceSettings deviceSettings, DeviceType deviceType, CreateFlags vertexProcessingFlag, out Device device) { Format adapterformat = d3D.Adapters[0].CurrentDisplayMode.Format; //для теней обязательно нужен stencil buffer. Ищем формат PresentParameters presentParameters = deviceSettings.PresentParameters; foreach (Format stencilformat in new[] { Format.D24S8, Format.D24SingleS8, Format.D24X4S4, Format.D15S1 }) { if (d3D.CheckDeviceFormat(0, deviceType, adapterformat, Usage.DepthStencil, ResourceType.Surface, stencilformat) && d3D.CheckDepthStencilMatch(0, deviceType, adapterformat, adapterformat, stencilformat)) { presentParameters.EnableAutoDepthStencil = true; presentParameters.AutoDepthStencilFormat = stencilformat; break; } } //Выбираем тип multisampling var multiSamplingTypesToCheck = new Dictionary <MultisampleType, int>(); if (deviceSettings.AutoDetermineMultisampleType) { multiSamplingTypesToCheck = new[] { MultisampleType.FourSamples, MultisampleType.TwoSamples, MultisampleType.None } }
private static Format GetSupportedDepthBufferFormat(Direct3D d3d) { var adapterFormat = d3d.Adapters[0].CurrentDisplayMode.Format; var formats = new[] { Format.D32, Format.D32SingleLockable, Format.D24SingleS8, Format.D24S8, Format.D24X8, Format.D16 }; return(formats.First(f => d3d.CheckDeviceFormat(0, DeviceType.Hardware, adapterFormat, Usage.DepthStencil, ResourceType.Surface, f))); }
/// <summary> /// Checks whether a certain depth stencil format is supported by the <see cref="SlimDX.Direct3D9.Device"/> /// </summary> internal static bool TestDepthStencil(int adapter, Format depthFormat) { using (var manager = new Direct3D()) { return(manager.CheckDeviceFormat(adapter, DeviceType.Hardware, Format.X8R8G8B8, Usage.DepthStencil, ResourceType.Surface, depthFormat)); } }
static public void Hook06000005(ref Direct3D d3d, ref Device device, ref PresentParameters param, Control control, bool windowed, Size size, int quality) { fullscreen |= !windowed; d3d = new Direct3D(); int adapter = d3d.Adapters.DefaultAdapter.Adapter; Capabilities deviceCaps = d3d.GetDeviceCaps(adapter, DeviceType.Hardware); DeviceType deviceType = deviceCaps.DeviceType; CreateFlags createFlags = (deviceCaps.VertexShaderVersion >= new Version(2, 0)) ? CreateFlags.HardwareVertexProcessing : CreateFlags.SoftwareVertexProcessing; param = new PresentParameters(); param.SwapEffect = SwapEffect.Discard; DisplayMode currentDisplayMode = d3d.Adapters[adapter].CurrentDisplayMode; param.Windowed = (windowed || !d3d.CheckDeviceType(adapter, DeviceType.Hardware, currentDisplayMode.Format, currentDisplayMode.Format, false)); if (param.Windowed) { param.DeviceWindowHandle = control.Handle; if (!fullscreen) { param.BackBufferWidth = 0; param.BackBufferHeight = 0; } else { param.BackBufferWidth = size.Width; param.BackBufferHeight = size.Height; control.ClientSize = new Size(currentDisplayMode.Width, currentDisplayMode.Height); control.Location = new Point(0, 0); } } else { param.BackBufferFormat = currentDisplayMode.Format; param.BackBufferCount = 1; if (size.Width == 0 || size.Height == 0) { size = new Size(currentDisplayMode.Width, currentDisplayMode.Height); } param.BackBufferWidth = size.Width; param.BackBufferHeight = size.Height; param.PresentFlags = PresentFlags.LockableBackBuffer; control.ClientSize = new Size(currentDisplayMode.Width, currentDisplayMode.Height); control.Location = new Point(0, 0); } if (d3d.CheckDeviceFormat(adapter, DeviceType.Hardware, currentDisplayMode.Format, Usage.DepthStencil, ResourceType.Surface, Format.D24S8)) { param.EnableAutoDepthStencil = true; param.AutoDepthStencilFormat = Format.D24S8; } MultisampleType multisampleType = quality <= 1 ? MultisampleType.None : MultisampleType.NonMaskable; while (multisampleType > MultisampleType.None) { int val; int val2; if (d3d.CheckDeviceMultisampleType(adapter, deviceType, param.BackBufferFormat, param.Windowed, multisampleType, out val) && d3d.CheckDeviceMultisampleType(adapter, deviceType, Format.D24S8, param.Windowed, multisampleType, out val2)) { param.Multisample = multisampleType; if (multisampleType == MultisampleType.NonMaskable) { param.MultisampleQuality = Math.Min(Math.Min(val, val2) - 1, (int)Math.Log(quality, 2) - 1); break; } break; } else { multisampleType--; } } param.PresentationInterval = PresentInterval.One; device = new Device(d3d, adapter, deviceType, control.Handle, createFlags, new PresentParameters[] { param }); }
private static bool TryCreateDevice(Direct3D d3D, DeviceSettings deviceSettings, DeviceType deviceType, CreateFlags vertexProcessingFlag, out Device device) { Format adapterformat = d3D.Adapters[0].CurrentDisplayMode.Format; //для теней обязательно нужен stencil buffer. Ищем формат PresentParameters presentParameters = deviceSettings.PresentParameters; foreach(Format stencilformat in new[] {Format.D24S8, Format.D24SingleS8, Format.D24X4S4, Format.D15S1}) { if(d3D.CheckDeviceFormat(0, deviceType, adapterformat, Usage.DepthStencil, ResourceType.Surface, stencilformat) && d3D.CheckDepthStencilMatch(0, deviceType, adapterformat, adapterformat, stencilformat)) { presentParameters.EnableAutoDepthStencil = true; presentParameters.AutoDepthStencilFormat = stencilformat; break; } } //Выбираем тип multisampling var multiSamplingTypesToCheck = new Dictionary<MultisampleType, int>(); if(deviceSettings.AutoDetermineMultisampleType) multiSamplingTypesToCheck = new[] {MultisampleType.FourSamples, MultisampleType.TwoSamples, MultisampleType.None}.ToDictionary(x => x, x => (int)x); else { MultisampleType multType = deviceSettings.MultisampleType; multiSamplingTypesToCheck.Add(multType, (int)multType); } foreach(var i in multiSamplingTypesToCheck) { presentParameters.Multisample = i.Key; presentParameters.MultisampleQuality = i.Value; try { device = new Device(d3D, 0, deviceType, presentParameters.DeviceWindowHandle, CreateFlags.Multithreaded | vertexProcessingFlag, presentParameters); return true; } catch(Direct3D9Exception) { } } device = null; return false; }
public bool Initialize(RenderForm Form) { direct3D = new Direct3D(); this.form = Form; int adapter = direct3D.AdapterCount - 1; // check window mode if (!direct3D.CheckDeviceType(adapter, DeviceType.Hardware, Format.R5G6B5, Format.R5G6B5, true)) { // not available System.Windows.Forms.MessageBox.Show("window mode not available"); } // fullscreen mode if (!direct3D.CheckDeviceType(adapter, DeviceType.Hardware, Format.R5G6B5, Format.R5G6B5, false)) { // not available System.Windows.Forms.MessageBox.Show("fullscreen not available"); } presentParams = new PresentParameters(); presentParams.BackBufferHeight = Form.ClientRectangle.Height; presentParams.BackBufferWidth = Form.ClientRectangle.Width; presentParams.DeviceWindowHandle = Form.Handle; presentParams.PresentationInterval = PresentInterval.One; presentParams.BackBufferCount = 1; presentParams.BackBufferFormat = Format.R5G6B5; // check back buffer format if (!direct3D.CheckDeviceFormat(adapter, DeviceType.Hardware, presentParams.BackBufferFormat, Usage.RenderTarget, ResourceType.Surface, Format.R5G6B5)) { // not available System.Windows.Forms.MessageBox.Show("back buffer not available"); } // check depth stencil format if (!direct3D.CheckDeviceFormat(adapter, DeviceType.Hardware, presentParams.BackBufferFormat, Usage.DepthStencil, ResourceType.Surface, Format.D16)) { // not available System.Windows.Forms.MessageBox.Show("depth stencil not available"); } // check depth stencil format match if (!direct3D.CheckDepthStencilMatch(adapter, DeviceType.Hardware, presentParams.BackBufferFormat, presentParams.BackBufferFormat, Format.D16)) { // not available System.Windows.Forms.MessageBox.Show("depth stencil match not available"); } presentParams.AutoDepthStencilFormat = Format.D16; presentParams.EnableAutoDepthStencil = true; presentParams.Windowed = !engine.FullScreen; if (!presentParams.Windowed) { presentParams.BackBufferWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; presentParams.BackBufferHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; presentParams.SwapEffect = SwapEffect.Flip; Form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; engine.Resolution = new Vector2(presentParams.BackBufferWidth, presentParams.BackBufferHeight); Log.Info("Fullscreen: yes"); Log.Info("Resolution: " + presentParams.BackBufferWidth + "x" + presentParams.BackBufferHeight); } else { presentParams.SwapEffect = SwapEffect.Discard; Log.Info("Fullscreen: no"); Log.Info("Resolution: " + presentParams.BackBufferWidth + "x" + presentParams.BackBufferHeight); } Log.Info("Virtual resolution: " + engine.GameResolution.x + "x" + engine.GameResolution.y); Log.Info("Scale factor: " + engine.Scale.x.ToString("0.00") + "x" + engine.Scale.y.ToString("0.00")); Log.Info("Texture filter method: " + (engine.UseTextureFilter ? "GaussianQuad" : "NearestPoint")); device = new Device(direct3D, adapter, DeviceType.Hardware, Form.Handle, CreateFlags.SoftwareVertexProcessing | CreateFlags.Multithreaded, presentParams); renderToSurface = new RenderToSurface(device, engine.GameResolution.x * renderScale, engine.GameResolution.y * renderScale, Format.X8R8G8B8); renderToTexture = new Texture(device, engine.GameResolution.x * renderScale, engine.GameResolution.y * renderScale, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default); loadingOverlay = new LoadingOverlay(engine, new Vector2(presentParams.BackBufferWidth, presentParams.BackBufferHeight)); blendOverlay = new BlendOverlay(new Vector2(presentParams.BackBufferWidth, presentParams.BackBufferHeight)); errorOverlay = new ErrorOverlay(engine, new Vector2(presentParams.BackBufferWidth, presentParams.BackBufferHeight)); ReloadGraphicResources(); DeviceReset += new EventHandler(RenderDevice_DeviceReset); DeviceLost += new EventHandler(RenderDevice_DeviceLost); return(true); }