public static void Initialize()
        {
            var endScenePointer = IntPtr.Zero;
            var resetPointer    = IntPtr.Zero;

            using (var d3d = new SlimDX.Direct3D9.Direct3D())
            {
                using (
                    var tmpDevice = new Device(d3d, 0, DeviceType.Hardware, IntPtr.Zero,
                                               CreateFlags.HardwareVertexProcessing,
                                               new PresentParameters {
                    BackBufferWidth = 1, BackBufferHeight = 1
                }))
                {
                    endScenePointer = Manager.Memory.GetObjectVtableFunction(tmpDevice.ComPointer, VMT_ENDSCENE);
                    resetPointer    = Manager.Memory.GetObjectVtableFunction(tmpDevice.ComPointer, VMT_RESET);
                }
            }

            _endSceneDelegate = Manager.Memory.RegisterDelegate <Direct3D9EndScene>(endScenePointer);
            _endSceneHook     = Manager.Memory.Detours.CreateAndApply(_endSceneDelegate, new Direct3D9EndScene(EndSceneHook), "D9EndScene");

            _resetDelegate = Manager.Memory.RegisterDelegate <Direct3D9Reset>(resetPointer);
            _resetHook     = Manager.Memory.Detours.CreateAndApply(_resetDelegate, new Direct3D9Reset(ResetHook), "D9Reset");

            // NEEDED FOR DRAWING IN RENDERBACKGROUND
            //_renderBackgroundDelegate = Manager.Memory.RegisterDelegate<Direct3D39RenderBackground>((IntPtr)Pointers.Drawing.RenderBackground);
            //_renderBackgroundHook = Manager.Memory.Detours.CreateAndApply(_renderBackgroundDelegate, new Direct3D39RenderBackground(PrepareRenderState), "RenderBackground");

            Log.WriteLine("EndScene detoured at 0x{0:X}", endScenePointer);
        }
示例#2
0
        /*
		public D3DTextureManager(D3D.Direct3D manager, D3D.Device device)
		{
           
		}
         */
         

		protected override Resource _create( string name, ulong handle, string group, bool isManual, IManualResourceLoader loader, NameValuePairList createParams )
		{
            // temporary hack
            manager = D3DRenderSystem.Direct3D9;
            device = D3DRenderSystem.ActiveD3D9Device;
			return new D3DTexture( this, name, handle, group, isManual, loader, this.device, this.manager );
		}
示例#3
0
        public static void Initialize()
        {
            var endScenePointer = IntPtr.Zero;
            var resetPointer = IntPtr.Zero;
            using (var d3d = new SlimDX.Direct3D9.Direct3D())
            {
                using (
                    var tmpDevice = new Device(d3d, 0, DeviceType.Hardware, IntPtr.Zero,
                                               CreateFlags.HardwareVertexProcessing,
                                               new PresentParameters { BackBufferWidth = 1, BackBufferHeight = 1 }))
                {
                    endScenePointer = Manager.Memory.GetObjectVtableFunction(tmpDevice.ComPointer, VMT_ENDSCENE);
                    resetPointer = Manager.Memory.GetObjectVtableFunction(tmpDevice.ComPointer, VMT_RESET);
                }
            }

            _endSceneDelegate = Manager.Memory.RegisterDelegate<Direct3D9EndScene>(endScenePointer);
            _endSceneHook = Manager.Memory.Detours.CreateAndApply(_endSceneDelegate, new Direct3D9EndScene(EndSceneHook), "D9EndScene");

            //_resetDelegate = Manager.Memory.RegisterDelegate<Direct3D9Reset>(resetPointer);
            //_resetHook = Manager.Memory.Detours.CreateAndApply(_resetDelegate, new Direct3D9Reset(ResetHook), "D9Reset");

            Log.WriteLine("Direct3D9x:");
            Log.WriteLine("\tEndScene: 0x{0:X}", endScenePointer);
            Log.WriteLine("\tReset: 0x{0:X}", resetPointer);
        }
示例#4
0
        public static void Initialize()
        {
            frm = new Form();
            frm.Show();

            d3d = new DX.Direct3D();

            DX.PresentParameters pm = new DX.PresentParameters();
            pm.Windowed = true; pm.AutoDepthStencilFormat = DX.Format.D24S8;

            device = new DX.Device(d3d, 0, DX.DeviceType.Reference, frm.Handle, DX.CreateFlags.SoftwareVertexProcessing, pm);
        }
示例#5
0
 private void CreateNullDevice()
 {
     form = new Form();
     using (var direct3D = new D3D9.Direct3D())
         device9 = new D3D9.Device(direct3D, 0, D3D9.DeviceType.Reference, form.Handle, D3D9.CreateFlags.SoftwareVertexProcessing, new D3D9.PresentParameters
         {
             BackBufferCount = 1,
             BackBufferFormat = D3D9.Format.X8R8G8B8,
             BackBufferHeight = 512,
             BackBufferWidth = 512,
             SwapEffect = D3D9.SwapEffect.Discard,
             Windowed = true
         });
 }
示例#6
0
        public void CreateDevice(bool useRegistry, bool showDialog = true)
        {
            Direct3D = new SlimDX.Direct3D9.Direct3D();
            VideoConfig cfg = VideoConfig.Load(useRegistry, showDialog);

            CurrentConfig = cfg;

            mPresentParams = new PresentParameters()
            {
                AutoDepthStencilFormat = cfg.DepthStencilFormat,
                EnableAutoDepthStencil = true,
                BackBufferFormat       = Format.A8R8G8B8,
                BackBufferHeight       = mRenderWindow.ClientSize.Height,
                BackBufferWidth        = mRenderWindow.ClientSize.Width,
                DeviceWindowHandle     = mRenderWindow.Handle,
                Windowed           = true,
                Multisample        = cfg.Multisampling,
                MultisampleQuality = (int)cfg.MultisampleQuality
            };

            Device = new Device(Direct3D, cfg.Adapter.Adapter, DeviceType.Hardware, mRenderWindow.Handle,
                                CreateFlags.HardwareVertexProcessing,
                                mPresentParams
                                );

            VideoResourceMgr = new VideoResourceManager();

            mActiveCamera = new PerspectiveCamera();

            Device.SetTransform(TransformState.World, Matrix.Identity);
            Device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear);
            Device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Linear);
            Device.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.Linear);

            if (DeviceLoaded != null)
            {
                DeviceLoaded();
            }
        }
示例#7
0
 public ViewportDX(ViewportParams p)
 {
     Params = p;
     D3D    = new DX.Direct3D();
     //Получаем текущий режим экрана
     DX.DisplayMode mode = D3D.GetAdapterDisplayMode(0);
     //Инициализируем параметры
     d3dpp.AutoDepthStencilFormat = DX.Format.D16;
     d3dpp.BackBufferCount        = 1;
     d3dpp.BackBufferFormat       = mode.Format;
     d3dpp.BackBufferHeight       = p.WindowHeight;
     d3dpp.BackBufferWidth        = p.WindowWidth;
     //Куда рисовать
     d3dpp.DeviceWindowHandle = p.ControlHandle;
     //Контроль вертикальной развертки
     if (p.VerticalSync)
     {
         d3dpp.PresentationInterval = DX.PresentInterval.Default;
     }
     else
     {
         d3dpp.PresentationInterval = DX.PresentInterval.Immediate;
     }
     d3dpp.SwapEffect             = DX.SwapEffect.Discard;
     d3dpp.Windowed               = !p.FullScreen;
     d3dpp.EnableAutoDepthStencil = true;
     if (!p.FullScreen && p.VerticalSync)
     {
         d3dpp.SwapEffect = DX.SwapEffect.Copy;
     }
     //Создаем девайс
     Device = new DX.Device(D3D, 0, DX.DeviceType.Hardware, p.ControlHandle, DX.CreateFlags.HardwareVertexProcessing, d3dpp);
     if (Device == null)
     {
         throw new NullReferenceException("Не удалось инициализировать устройство!");
     }
 }
        //public override void ApplySettings()
        //{
        //    if (Settings.RequiresDeviceReset)
        //    {
        //        MarkForReset();
        //        Settings.RequiresDeviceReset = false;
        //    }
        //}

        public override void Create()
        {
            D3D = new SlimDX.Direct3D9.Direct3D();
            InitParams();
#if DEBUG
            int  adapter = 0;
            bool perfHud = false;
#endif

            Application.Log("Listing adapters..");

            for (int i = 0; i < D3D.AdapterCount; i++)
            {
                var res = D3D.GetAdapterIdentifier(i);
                Application.Log("[" + i + "] Adapter: " + res.Description);
#if DEBUG
                if (res.Description.Contains("PerfHUD"))
                {
                    adapter = i;
                    perfHud = true;
                    break;
                }
#endif
            }

            // Multithreaded flag:
            // "If you set that flag in D3D9 or D3D10, then the device will enter a critical section every time you call
            // one of its methods (or a method on any objects created by the device, like a texture or vertex buffer).
            // This makes it safe for you to do whatever you want from multiple threads, but it will end up serializing
            // all of your API calls which will kill performance."
            // http://www.gamedev.net/community/forums/topic.asp?topic_id=533801&whichpage=1&#3450338
            //  * Needed since the physics thread performs mesh intersections which crashes the application if performed on an object being rendered
#if DEBUG
            if (perfHud)
            {
                Application.Log("Using NVidia PerfHUD adapter");
                Device9 = new Device(D3D, adapter, DeviceType.Reference, presentParams.DeviceWindowHandle,
                                     CreateFlags.HardwareVertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded, // see above
                                     presentParams);
            }
            else
            {
#endif
            if (SettingConverters.VertexProcessing == CreateFlags.None)
            {
                Device9 = new Device(D3D, 0, DeviceType.Hardware, presentParams.DeviceWindowHandle,
                                     CreateFlags.HardwareVertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded, // see above
                                     presentParams);
            }
            else
            {
                if (SettingConverters.VertexProcessing == CreateFlags.SoftwareVertexProcessing)
                {
                    Device9 = new Device(D3D, 0, DeviceType.Hardware, presentParams.DeviceWindowHandle,
                                         SettingConverters.VertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded, // see above
                                         presentParams);
                }
                else if (Settings.PureDevice)
                {
                    Device9 = new Device(D3D, 0, DeviceType.Hardware, presentParams.DeviceWindowHandle,
                                         SettingConverters.VertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded | CreateFlags.PureDevice, // see above
                                         presentParams);
                }
                else
                {
                    Device9 = new Device(D3D, 0, DeviceType.Hardware, presentParams.DeviceWindowHandle,
                                         SettingConverters.VertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded, // see above
                                         presentParams);
                }
            }
#if DEBUG
        }
#endif

            markedForReset = false;
        }
        //public override void ApplySettings()
        //{
        //    if (Settings.RequiresDeviceReset)
        //    {
        //        MarkForReset();
        //        Settings.RequiresDeviceReset = false;
        //    }
        //}
        public override void Create()
        {
            D3D = new SlimDX.Direct3D9.Direct3D();
            InitParams();
            #if DEBUG
            int adapter = 0;
            bool perfHud = false;
            #endif

            Application.Log("Listing adapters..");

            for (int i = 0; i < D3D.AdapterCount; i++)
            {
                var res = D3D.GetAdapterIdentifier(i);
                Application.Log("[" + i + "] Adapter: " + res.Description);
            #if DEBUG
                if (res.Description.Contains("PerfHUD"))
                {
                    adapter = i;
                    perfHud = true;
                    break;
                }
            #endif
            }

            // Multithreaded flag:
            // "If you set that flag in D3D9 or D3D10, then the device will enter a critical section every time you call
            // one of its methods (or a method on any objects created by the device, like a texture or vertex buffer).
            // This makes it safe for you to do whatever you want from multiple threads, but it will end up serializing
            // all of your API calls which will kill performance."
            // http://www.gamedev.net/community/forums/topic.asp?topic_id=533801&whichpage=1&#3450338
            //  * Needed since the physics thread performs mesh intersections which crashes the application if performed on an object being rendered
            #if DEBUG
            if (perfHud)
            {
                Application.Log("Using NVidia PerfHUD adapter");
                Device9 = new Device(D3D, adapter, DeviceType.Reference, presentParams.DeviceWindowHandle,
                    CreateFlags.HardwareVertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded,   // see above
                    presentParams);
            }
            else
            {
            #endif
                if (SettingConverters.VertexProcessing == CreateFlags.None)
                {
                    Device9 = new Device(D3D, 0, DeviceType.Hardware, presentParams.DeviceWindowHandle,
                        CreateFlags.HardwareVertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded,    // see above
                        presentParams);
                }
                else
                {
                    if (SettingConverters.VertexProcessing == CreateFlags.SoftwareVertexProcessing)
                    {
                        Device9 = new Device(D3D, 0, DeviceType.Hardware, presentParams.DeviceWindowHandle,
                                 SettingConverters.VertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded,    // see above
                                 presentParams);
                    }
                    else if (Settings.PureDevice)
                    {
                        Device9 = new Device(D3D, 0, DeviceType.Hardware, presentParams.DeviceWindowHandle,
                            SettingConverters.VertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded | CreateFlags.PureDevice,    // see above
                            presentParams);
                    }
                    else
                    {
                        Device9 = new Device(D3D, 0, DeviceType.Hardware, presentParams.DeviceWindowHandle,
                            SettingConverters.VertexProcessing | CreateFlags.FpuPreserve | CreateFlags.Multithreaded,    // see above
                            presentParams);
                    }
                }
            #if DEBUG
            }
            #endif

            markedForReset = false;
        }
示例#10
0
		public D3DTexture( ResourceManager parent, string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader, D3D.Device device, D3D.Direct3D manager )
			: base( parent, name, handle, group, isManual, loader )
		{
            device = D3DRenderSystem.ActiveD3D9Device; // temp hack till 1.7 upgrade
			Debug.Assert( device != null, "Cannot create a texture without a valid D3D Device." );
			this._device = device;
			this._manager = manager;

			InitDevice();
		}
示例#11
0
        /// <summary>
        /// 最上位ウインドウを作成
        /// </summary>
        public MainWindow()
        {
            // コンポーネント初期化
            InitializeComponent();

            // ウインドウが読み込まれたら
            this.Loaded += (sender, e) =>
            {
                // Direct3D作成
                var direct3D = new SlimDX.Direct3D9.Direct3D();

                // デバイスを初期化
                Device device = null;

                // ティーポッドと文字列を初期化
                Mesh teapod     = null;
                Mesh helloWorld = null;

                // バッファーウインドウ作成
                IntPtr handle = new HwndSource(0, 0, 0, 0, 0, "buffer", IntPtr.Zero).Handle;

                // デバイスを作成
                device = new Device(direct3D, 0,
                                    DeviceType.Hardware,
                                    handle,
                                    CreateFlags.HardwareVertexProcessing,
                                    new PresentParameters()
                {
                    BackBufferFormat       = Format.X8R8G8B8,
                    BackBufferCount        = 1,
                    BackBufferWidth        = (int)this.Width,
                    BackBufferHeight       = (int)this.Height,
                    Multisample            = MultisampleType.None,
                    SwapEffect             = SwapEffect.Discard,
                    EnableAutoDepthStencil = true,
                    AutoDepthStencilFormat = Format.D16,
                    PresentFlags           = PresentFlags.DiscardDepthStencil,
                    PresentationInterval   = PresentInterval.Default,
                    Windowed           = true,
                    DeviceWindowHandle = handle
                });

                // ティーポッドと文字列を作成
                teapod     = Mesh.CreateTeapot(device);
                helloWorld = Mesh.CreateText(device, new System.Drawing.Font("Arial", 10), "Hello, world!", 0.001f, 0.001f);

                // 光源描画を有効化
                device.SetRenderState(RenderState.Lighting, true);

                // 光源を設定
                device.SetLight(0, new Light()
                {
                    Type      = LightType.Directional,
                    Diffuse   = Color.White,
                    Ambient   = Color.GhostWhite,
                    Direction = new Vector3(0.0f, -1.0f, 0.0f)
                });

                // 光源を有効化
                device.EnableLight(0, true);


                //射影変換を設定
                device.SetTransform(TransformState.Projection,
                                    Matrix.PerspectiveFovLH((float)(Math.PI / 4),
                                                            (float)(this.Width / this.Height),
                                                            0.1f, 20.0f));

                //ビューを設定
                device.SetTransform(TransformState.View,
                                    Matrix.LookAtLH(new Vector3(3.0f, 2.0f, -3.0f),
                                                    Vector3.Zero,
                                                    new Vector3(0.0f, 1.0f, 0.0f)));

                //マテリアル設定
                device.Material = new Material()
                {
                    Diffuse = new Color4(Color.GhostWhite)
                };

                // 描画される時に描画処理を実行
                CompositionTarget.Rendering += (sender2, e2) =>
                {
                    // 画像をロックしてバックバッファーに設定
                    this.directXImage.Lock();
                    this.directXImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, device.GetBackBuffer(0, 0).ComPointer);

                    // フロントバッファーがあれば
                    if (directXImage.IsFrontBufferAvailable)
                    {
                        // デバイスの現在の状態を確かめて失敗したら
                        if (device.TestCooperativeLevel().IsFailure)
                        {
                            // 例外
                            throw new Direct3D9Exception("デバイスが無効です");
                        }

                        // 全面灰色でクリア
                        device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(0.3f, 0.3f, 0.3f), 1.0f, 0);

                        // シーン開始
                        device.BeginScene();

                        //ティーポットを描画
                        device.SetTransform(TransformState.World,
                                            Matrix.Translation(-2, 0, 5));
                        teapod.DrawSubset(0);

                        //文字列を描画
                        device.SetTransform(TransformState.World,
                                            Matrix.Translation(-4, 0, 0));
                        helloWorld.DrawSubset(0);

                        // シーン終了
                        device.EndScene();

                        // バッファー更新
                        device.Present();
                    }

                    // バックバッファー領域を指定
                    this.directXImage.AddDirtyRect(new Int32Rect(0, 0, directXImage.PixelWidth, directXImage.PixelHeight));

                    // ロック解除
                    this.directXImage.Unlock();
                };

                // イメージのバックバッファーを設定
                directXImage.Lock();
                directXImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, device.GetBackBuffer(0, 0).ComPointer);
                directXImage.Unlock();
            };
        }
示例#12
0
        /// <summary>
        /// 最上位ウインドウを作成
        /// </summary>
        public MainWindow()
        {
            // コンポーネント初期化
            InitializeComponent();

            // ウインドウが読み込まれたら
            this.Loaded += (sender, e) =>
            {
                // Direct3D作成
                var direct3D = new SlimDX.Direct3D9.Direct3D();

                // デバイスを初期化
                Device device = null;

                // ティーポッドと文字列を初期化
                Mesh teapod = null;
                Mesh helloWorld = null;

                // バッファーウインドウ作成
                IntPtr handle = new HwndSource(0, 0, 0, 0, 0, "buffer", IntPtr.Zero).Handle;

                // デバイスを作成
                device = new Device(direct3D, 0,
                    DeviceType.Hardware,
                    handle,
                    CreateFlags.HardwareVertexProcessing,
                    new PresentParameters()
                    {
                        BackBufferFormat = Format.X8R8G8B8,
                        BackBufferCount = 1,
                        BackBufferWidth = (int)this.Width,
                        BackBufferHeight = (int)this.Height,
                        Multisample = MultisampleType.None,
                        SwapEffect = SwapEffect.Discard,
                        EnableAutoDepthStencil = true,
                        AutoDepthStencilFormat = Format.D16,
                        PresentFlags = PresentFlags.DiscardDepthStencil,
                        PresentationInterval = PresentInterval.Default,
                        Windowed = true,
                        DeviceWindowHandle = handle
                    });

                // ティーポッドと文字列を作成
                teapod = Mesh.CreateTeapot(device);
                helloWorld = Mesh.CreateText(device, new System.Drawing.Font("Arial", 10), "Hello, world!", 0.001f, 0.001f);

                // 光源描画を有効化
                device.SetRenderState(RenderState.Lighting, true);

                // 光源を設定
                device.SetLight(0, new Light()
                {
                    Type = LightType.Directional,
                    Diffuse = Color.White,
                    Ambient = Color.GhostWhite,
                    Direction = new Vector3(0.0f, -1.0f, 0.0f)
                });

                // 光源を有効化
                device.EnableLight(0, true);

                //射影変換を設定
                device.SetTransform(TransformState.Projection,
                    Matrix.PerspectiveFovLH((float)(Math.PI / 4),
                    (float)(this.Width / this.Height),
                    0.1f, 20.0f));

                //ビューを設定
                device.SetTransform(TransformState.View,
                    Matrix.LookAtLH(new Vector3(3.0f, 2.0f, -3.0f),
                    Vector3.Zero,
                    new Vector3(0.0f, 1.0f, 0.0f)));

                //マテリアル設定
                device.Material = new Material()
                {
                    Diffuse = new Color4(Color.GhostWhite)
                };

                // 描画される時に描画処理を実行
                CompositionTarget.Rendering += (sender2, e2) =>
                {
                    // 画像をロックしてバックバッファーに設定
                    this.directXImage.Lock();
                    this.directXImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, device.GetBackBuffer(0, 0).ComPointer);

                    // フロントバッファーがあれば
                    if (directXImage.IsFrontBufferAvailable)
                    {
                        // デバイスの現在の状態を確かめて失敗したら
                        if (device.TestCooperativeLevel().IsFailure)
                        {
                            // 例外
                            throw new Direct3D9Exception("デバイスが無効です");
                        }

                        // 全面灰色でクリア
                        device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(0.3f, 0.3f, 0.3f), 1.0f, 0);

                        // シーン開始
                        device.BeginScene();

                        //ティーポットを描画
                        device.SetTransform(TransformState.World,
                            Matrix.Translation(-2, 0, 5));
                        teapod.DrawSubset(0);

                        //文字列を描画
                        device.SetTransform(TransformState.World,
                            Matrix.Translation(-4, 0, 0));
                        helloWorld.DrawSubset(0);

                        // シーン終了
                        device.EndScene();

                        // バッファー更新
                        device.Present();
                    }

                    // バックバッファー領域を指定
                    this.directXImage.AddDirtyRect(new Int32Rect(0, 0, directXImage.PixelWidth, directXImage.PixelHeight));

                    // ロック解除
                    this.directXImage.Unlock();
                };

                // イメージのバックバッファーを設定
                directXImage.Lock();
                directXImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, device.GetBackBuffer(0, 0).ComPointer);
                directXImage.Unlock();
            };
        }