Ejemplo n.º 1
0
        private static D3d.Device CreateDevice(System.Windows.Forms.Control renderWindow,
                                               D3d.PresentParameters presentParams,
                                               D3d.CreateFlags createFlags,
                                               Settings deviceSettings)
        {
            try
            {
                D3d.Device d3dDevice = new D3d.Device(deviceSettings.adapterIndex,
                                                      deviceSettings.deviceType,
                                                      renderWindow,
                                                      createFlags,
                                                      presentParams);
                return(d3dDevice);
            }
            catch (D3d.DeviceLostException e)
            {
                log.Warning("Unable to create Direct3D device: {0}", e.Message);
            }
            catch (D3d.InvalidCallException e)
            {
                log.Warning("Unable to create Direct3D device: {0}", e.Message);
            }
            catch (D3d.NotAvailableException e)
            {
                log.Warning("Unable to create Direct3D device: {0}", e.Message);
            }
            catch (D3d.OutOfVideoMemoryException e)
            {
                log.Warning("Unable to create Direct3D device: {0}", e.Message);
            }

            return(null);
        }
Ejemplo n.º 2
0
        public static D3d.Device    CreateDevice(Settings deviceSettings,
                                                 System.Windows.Forms.Control renderWindow)
        {
            EnumerateAdapters();

            // First try full hardware acceleration.
            D3d.PresentParameters presentParams = deviceSettings.GetPresentParameters(renderWindow);
            D3d.CreateFlags       createFlags   = deviceSettings.GetCreateFlags(true, true);

            D3d.Device d3dDevice = CreateDevice(renderWindow, presentParams, createFlags, deviceSettings);

            if (d3dDevice == null)
            {
                log.Warning("Pure device not available.");
                createFlags = deviceSettings.GetCreateFlags(false, true);

                d3dDevice = CreateDevice(renderWindow, presentParams, createFlags, deviceSettings);

                if (d3dDevice == null)
                {
                    log.Warning("Hardware vertex processing not available.");
                    createFlags = deviceSettings.GetCreateFlags(false, false);

                    d3dDevice = CreateDevice(renderWindow, presentParams, createFlags, deviceSettings);
                }
            }

            return(d3dDevice);
        }
Ejemplo n.º 3
0
        internal void Initialize(MDX1_DisplayWindow window)
        {
            if (mInitialized)
            {
                return;
            }

            if (window.RenderTarget.TopLevelControl == null)
            {
                throw new ArgumentException("The specified render target does not have a Form object yet.  " +
                                            "It's TopLevelControl property is null.  You may not create DisplayWindow objects before " +
                                            "the control to render to is added to the Form.");
            }

            mInitialized = true;

            // ok, create D3D device
            PresentParameters present = CreateWindowedPresentParameters(window, 0, 0);

            present.BackBufferWidth  = 1;
            present.BackBufferHeight = 1;

            DeviceType dtype = DeviceType.Hardware;

            int adapterOrdinal = Direct3D.Manager.Adapters.Default.Adapter;

            Direct3D.Caps        caps  = Direct3D.Manager.GetDeviceCaps(adapterOrdinal, Direct3D.DeviceType.Hardware);
            Direct3D.CreateFlags flags = Direct3D.CreateFlags.SoftwareVertexProcessing;

            // Is there support for hardware vertex processing? If so, replace
            // software vertex processing.
            if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
            {
                flags = Direct3D.CreateFlags.HardwareVertexProcessing;
            }

            // Does the device support a pure device?
            if (caps.DeviceCaps.SupportsPureDevice)
            {
                flags |= Direct3D.CreateFlags.PureDevice;
            }

            Device device = new Device
                                (0, dtype, window.RenderTarget.TopLevelControl.Handle,
                                flags, present);

            device.DeviceLost  += new EventHandler(mDevice_DeviceLost);
            device.DeviceReset += new EventHandler(mDevice_DeviceReset);

            mDevice = new D3DDevice(device);

            // create primitive objects
            mLine = new Direct3D.Line(device);

            //CreateSurfaceVB();
        }
Ejemplo n.º 4
0
 public static D3D.CreateFlags GetDeviceCreateFlags(int adpNum, D3D.DeviceType type)
 {
     DebugOnly.ConsoleWrite("Getting device creation flags...");
     D3D.Caps        caps  = D3D.Manager.GetDeviceCaps(adpNum, type);
     D3D.CreateFlags flags = D3D.CreateFlags.SoftwareVertexProcessing;
     if (caps.DeviceCaps.SupportsHardwareRasterization && caps.DeviceCaps.SupportsHardwareTransformAndLight)
     {
         flags = D3D.CreateFlags.HardwareVertexProcessing;
         if (caps.DeviceCaps.SupportsPureDevice)
         {
             flags |= D3D.CreateFlags.PureDevice;
         }
     }
     DebugOnly.ConsoleWrite("Found flags: " + flags.ToString());
     return(flags);
 }
Ejemplo n.º 5
0
        public D3d.CreateFlags          GetCreateFlags(bool pure, bool hardware)
        {
            D3d.CreateFlags createFlags = (D3d.CreateFlags) 0;

            if (pure)
            {
                createFlags |= D3d.CreateFlags.PureDevice;
            }

            if (hardware)
            {
                createFlags |= D3d.CreateFlags.HardwareVertexProcessing;
            }
            else
            {
                createFlags |= D3d.CreateFlags.SoftwareVertexProcessing;
            }

            return(createFlags);
        }
Ejemplo n.º 6
0
        public void InitDevice(D3D.DeviceType forcedDevice, D3D.CreateFlags forcedFlags)
        {
            if (this._dev != null)
            {
                this.Dispose(true);
            }

            try
            {
                D3D.PresentParameters pprms = new Microsoft.DirectX.Direct3D.PresentParameters();
                pprms.Windowed = this._pprmWindowed;
                if (!this._pprmWindowed)
                {
                    pprms.BackBufferWidth           = this._pprmBkBfrW;
                    pprms.BackBufferHeight          = this._pprmBkBfrH;
                    pprms.BackBufferFormat          = this._pprmBkBfrFmt;
                    pprms.FullScreenRefreshRateInHz = this._pprmRefreshRt;
                }
                pprms.AutoDepthStencilFormat = this._pprmDepthStencilFormat;
                pprms.SwapEffect             = this._pprmSwapEffect;
                pprms.PresentationInterval   = this._pprmIntvl;
                pprms.PresentFlag            = this._pprmPrsFlg;
                pprms.MultiSample            = this._pprmMsType;
                pprms.MultiSampleQuality     = 0;
                pprms.BackBufferCount        = this._pprmBkBfrCnt;

                this._dev                 = new D3D.Device(this._adpNum, forcedDevice, this._hWnd, forcedFlags, pprms);
                this._dev.DeviceReset    += new EventHandler(this.dev_onDeviceReset);
                this._dev.DeviceLost     += new EventHandler(this.dev_onDeviceLost);
                this._dev.DeviceResizing += new System.ComponentModel.CancelEventHandler(this.dev_onDeviceResizing);
                this._disposed            = false;
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 7
0
 public void InitDevice()
 {
     D3D.DeviceType  devType = DxGraphics.GetDeviceType(this._adpNum);
     D3D.CreateFlags flags   = DxGraphics.GetDeviceCreateFlags(this._adpNum, devType);
     this.InitDevice(devType, flags);
 }
Ejemplo n.º 8
0
        public ScreenAccess()
        {
            try
            {
                Direct3D.PresentParameters presentParams = new Direct3D.PresentParameters();
                presentParams.Windowed               = true;
                presentParams.SwapEffect             = Direct3D.SwapEffect.Discard;
                presentParams.EnableAutoDepthStencil = true;
                presentParams.AutoDepthStencilFormat = Direct3D.DepthFormat.D16;

                Direct3D.Caps        hardware = Direct3D.Manager.GetDeviceCaps(0, Direct3D.DeviceType.Hardware);
                Direct3D.CreateFlags flags    = Direct3D.CreateFlags.SoftwareVertexProcessing;

                // Search for the highest possible shader support and define the device.
                if (hardware.VertexShaderVersion >= new Version(2, 0))
                {
                    if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
                    {
                        flags = Direct3D.CreateFlags.HardwareVertexProcessing;
                    }

                    if (hardware.DeviceCaps.SupportsPureDevice)
                    {
                        flags |= Direct3D.CreateFlags.PureDevice;
                    }

                    if (hardware.PixelShaderVersion >= new Version(3, 0))
                    {
                        CardShader = GameEngine.ShaderLevel.Pixel_3_0;
                    }
                    else if (hardware.PixelShaderVersion >= new Version(2, 2))
                    {
                        CardShader = GameEngine.ShaderLevel.Pixel_2_b;
                    }
                    else if (hardware.PixelShaderVersion >= new Version(2, 0))
                    {
                        CardShader = GameEngine.ShaderLevel.Pixel_2_0;
                    }
                    else if (hardware.PixelShaderVersion >= new Version(1, 4))
                    {
                        CardShader = GameEngine.ShaderLevel.Pixel_1_4;
                    }
                    else
                    {
                        CardShader = GameEngine.ShaderLevel.NoShaders;
                    }

                    device = new Direct3D.Device(0, Direct3D.DeviceType.Hardware, this, flags, presentParams);
                }
                else if (hardware.VertexShaderVersion >= new Version(1, 1))
                {
                    if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
                    {
                        flags = Direct3D.CreateFlags.HardwareVertexProcessing;
                    }

                    if (hardware.DeviceCaps.SupportsPureDevice)
                    {
                        flags |= Direct3D.CreateFlags.PureDevice;
                    }

                    if (hardware.PixelShaderVersion >= new Version(1, 4))
                    {
                        CardShader = GameEngine.ShaderLevel.Pixel_1_4;
                    }
                    else
                    {
                        CardShader = GameEngine.ShaderLevel.NoShaders;
                    }

                    device = new Direct3D.Device(0, Direct3D.DeviceType.Hardware, this, flags, presentParams);
                }
                //TODO: test this on shit cards and fake cards to see if they can use DeviceType.Hardware
                else
                {
                    CardShader = GameEngine.ShaderLevel.NoShaders;
                    device     = new Direct3D.Device(0, Direct3D.DeviceType.Hardware, this,
                                                     Direct3D.CreateFlags.SoftwareVertexProcessing, presentParams);
                }

                //Setup Alpha Blending
                this.device.RenderState.AlphaBlendOperation = Direct3D.BlendOperation.Add;
                this.device.RenderState.AlphaTestEnable     = true;
                this.device.RenderState.ReferenceAlpha      = 0x08;
                this.device.RenderState.AlphaFunction       = Direct3D.Compare.GreaterEqual;

                this.device.SetTextureStageState(0, Direct3D.TextureStageStates.AlphaOperation, true);

                //Get the best texture filtering available from the card
                if (this.device.DeviceCaps.TextureFilterCaps.SupportsMinifyAnisotropic == true &&
                    this.device.DeviceCaps.TextureFilterCaps.SupportsMagnifyAnisotropic == true)
                {
                    this.PreferedTextureFilter = Direct3D.TextureFilter.Anisotropic;
                }
                else if (this.device.DeviceCaps.TextureFilterCaps.SupportsMinifyLinear == true &&
                         this.device.DeviceCaps.TextureFilterCaps.SupportsMagnifyLinear == true)
                {
                    this.PreferedTextureFilter = Direct3D.TextureFilter.Linear;
                }
                else
                {
                    this.PreferedTextureFilter = Direct3D.TextureFilter.None;
                }

                //Add window events to hardware screen device
                this.DeligateEventsToDevices();
            }
            catch (Exception err)
            {
                throw new Exception(err.StackTrace);
            }
        }