void FindCompatibleFormat(int adapter) { for (int i = 0; i < viewFormats.Length; i++) { viewFormat = viewFormats[i]; if (d3d.CheckDeviceType(adapter, DeviceType.Hardware, viewFormat, viewFormat, true)) { break; } if (i == viewFormats.Length - 1) { throw new InvalidOperationException("Unable to create a back buffer with sufficient precision."); } } for (int i = 0; i < depthFormats.Length; i++) { depthFormat = depthFormats[i]; if (d3d.CheckDepthStencilMatch(adapter, DeviceType.Hardware, viewFormat, viewFormat, depthFormat)) { break; } if (i == depthFormats.Length - 1) { throw new InvalidOperationException("Unable to create a depth buffer with sufficient precision."); } } }
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 }); }
/// <summary> /// Initializes the Direct3D objects and sets the Available flag /// </summary> private void InitializeDirect3D() { DirectXStatus = DirectXStatus.Unavailable_Unknown; ReleaseDevice(); ReleaseDirect3D(); // assume that we can't run at all under terminal services //if (GetSystemMetrics(SM_REMOTESESSION) != 0) //{ // DirectXStatus = DirectXStatus.Unavailable_RemoteSession; // return; //} //int renderingTier = (RenderCapability.Tier >> 16); //if (renderingTier < 2) //{ //DirectXStatus = DirectXStatus.Unavailable_LowTier; //return; //} #if USE_XP_MODE _direct3D = new Direct3D(); UseDeviceEx = false; #else try { direct3DEx = new Direct3DEx(); UseDeviceEx = true; } catch { try { direct3D = new Direct3D(); UseDeviceEx = false; } catch (Direct3DX9NotFoundException) { DirectXStatus = DirectXStatus.Unavailable_MissingDirectX; return; } catch { DirectXStatus = DirectXStatus.Unavailable_Unknown; return; } } #endif bool ok; Result result; ok = Direct3D.CheckDeviceType(0, DeviceType.Hardware, adapterFormat, backbufferFormat, true, out result); if (!ok) { //const int D3DERR_NOTAVAILABLE = -2005530518; //if (result.Code == D3DERR_NOTAVAILABLE) //{ // ReleaseDirect3D(); // Available = Status.Unavailable_NotReady; // return; //} ReleaseDirect3D(); return; } ok = Direct3D.CheckDepthStencilMatch(0, DeviceType.Hardware, adapterFormat, backbufferFormat, depthStencilFormat, out result); if (!ok) { ReleaseDirect3D(); return; } Capabilities deviceCaps = Direct3D.GetDeviceCaps(0, DeviceType.Hardware); if ((deviceCaps.DeviceCaps & DeviceCaps.HWTransformAndLight) != 0) { createFlags |= CreateFlags.HardwareVertexProcessing; } else { createFlags |= CreateFlags.SoftwareVertexProcessing; } DirectXStatus = DirectXStatus.Available; return; }
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); }