Ejemplo n.º 1
0
        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.");
                }
            }
        }
Ejemplo n.º 2
0
        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 }
            }
        /// <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;
        }
Ejemplo n.º 4
0
		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;
		}
Ejemplo n.º 5
0
        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);
        }