Exemplo n.º 1
0
 private void InitializeDevice()
 {
     device = new Dx11.Device(Dx11.DriverType.Hardware, Dx11.DeviceCreationFlags.None);
     for (int i = 0; i < 8; i++)
     {
         qual = device.CheckMultisampleQualityLevels(Dxgi.Format.R8G8B8A8_UNorm, i);
         if (qual > 0)
         {
             count = i;
         }
     }
     factor    = new Dxgi.Factory();
     swapChain = new Dxgi.SwapChain(factor, device,
                                    new Dxgi.SwapChainDescription()
     {
         BufferCount       = 1, OutputHandle = this.Handle,
         IsWindowed        = true,
         SampleDescription = new Dxgi.SampleDescription()
         {
             Count = count, Quality = qual - 1,
         }, ModeDescription = new Dxgi.ModeDescription()
         {
             Width       = ClientSize.Width, Height = ClientSize.Height,
             RefreshRate = new Rational(60, 1),
             Format      = Dxgi.Format.R8G8B8A8_UNorm,
         }, Usage = Dxgi.Usage.RenderTargetOutput
     });
     InitializeRenderTarget();
     InitializeDepthStencil();
     InitializeViewport();
     LoadContent();
     InitializeInputDevice();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10RenderSystemProvider"/> using the adapter index.
        /// </summary>
        /// <param name="adapterIndex">Index of the adapter to use when creating the underlying device</param>
        public D3D10RenderSystemProvider(int adapterIndex)
        {
            //Create the DXGI Factory
            DXGI.Factory factory = new DXGI.Factory();

            //Create the device
            DXGI.Adapter adapter = factory.GetAdapter(adapterIndex);
            D3D.Device   device  = new D3D.Device(adapter, D3D.DriverType.Hardware, D3D.DeviceCreationFlags.None);

            //Enumerate adapters
            List <IGraphicsAdapter> adapterList = new List <IGraphicsAdapter>();
            int adapterCount = factory.GetAdapterCount();

            for (int i = 0; i < adapterCount; i++)
            {
                adapterList.Add(new D3D10GraphicsAdapter(device, factory, i));
            }
            _adapters = new ReadOnlyList <IGraphicsAdapter>(adapterList);

            //Create the renderer
            _renderer = new D3D10Renderer(factory, device, (D3D10GraphicsAdapter)adapterList[adapterIndex]);

            //Create default content manager
            _content = new ContentManager(new EmbeddedResourceLocator(Tesla.Direct3D10.DefaultContent.ResourceManager));
            _content.UseDefaultContent = false;
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a new instance of <see cref="D3D10Renderer"/>.
 /// </summary>
 /// <param name="factory">The DXGI factory used to create the adapter for the device.</param>
 /// <param name="graphicsDevice">The graphics device created by the render system.</param>
 /// <param name="adapter">The graphics adapter used to create the device.</param>
 internal D3D10Renderer(DXGI.Factory factory, D3D.Device graphicsDevice, D3D10GraphicsAdapter adapter)
 {
     _factory        = factory;
     _graphicsDevice = graphicsDevice;
     _adapter        = adapter;
     CreateHelpers();
 }
Exemplo n.º 4
0
		private void InitalizeGraphics()
		{
			if (Window.RenderCanvasHandle == IntPtr.Zero)
				throw new InvalidOperationException("Window handle cannot be zero");

			SwapChainDescription swapChainDesc = new SwapChainDescription()
			{
				BufferCount = 1,
				Flags = SwapChainFlags.None,
				IsWindowed = true,
				OutputHandle = Window.RenderCanvasHandle,
				SwapEffect = SwapEffect.Discard,
				Usage = Usage.RenderTargetOutput,
				ModeDescription = new ModeDescription()
				{
					Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
					//Format = SlimDX.DXGI.Format.B8G8R8A8_UNorm,
					Width = Window.ClientSize.Width,
					Height = Window.ClientSize.Height,
					RefreshRate = new Rational(60, 1),
					Scaling = DisplayModeScaling.Unspecified,
					ScanlineOrdering = DisplayModeScanlineOrdering.Unspecified
				},
				SampleDescription = new SampleDescription(1, 0)
			};

			var giFactory = new SlimDX.DXGI.Factory();
			var adapter = giFactory.GetAdapter(0);

			Device device;
			SwapChain swapChain;
			Device.CreateWithSwapChain(adapter, DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out device, out swapChain);
			_swapChain = swapChain;
			GraphicsDevice = device;

			// create a view of our render target, which is the backbuffer of the swap chain we just created
			using (var resource = SlimDX.Direct3D10.Resource.FromSwapChain<Texture2D>(swapChain, 0))
			{
				_backBuffer = new RenderTargetView(device, resource);
			}

			// setting a viewport is required if you want to actually see anything
			var viewport = new Viewport(0, 0, Window.ClientSize.Width, Window.ClientSize.Height);
			device.OutputMerger.SetTargets(_backBuffer);
			device.Rasterizer.SetViewports(viewport);

			CreateDepthStencil();
			LoadVisualizationEffect();

			// Allocate a large buffer to write the PhysX visualization vertices into
			// There's more optimized ways of doing this, but for this sample a large buffer will do
			_userPrimitivesBuffer = new SlimDX.Direct3D10.Buffer(GraphicsDevice, VertexPositionColor.SizeInBytes * 50000, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None);

			var elements = new[]
			{
				new InputElement("Position", 0, Format.R32G32B32A32_Float, 0, 0),
				new InputElement("Color", 0, Format.R32G32B32A32_Float, 16, 0)
			};
			_inputLayout = new InputLayout(GraphicsDevice, _visualizationEffect.RenderScenePass0.Description.Signature, elements);
		}
Exemplo n.º 5
0
        internal GameDeviceContext(AppContext context) 
		{
			SwapChain defaultSwapChain;

			SlimDX.Direct3D11.Device.CreateWithSwapChain(SlimDX.Direct3D11.DriverType.Hardware, GameConfiguration.CreationFlags, context[0].SwapChainDescription, out m_Device, out defaultSwapChain);
			
			m_Factory = m_Device.Factory;

			int numberOfAdditionalWindows = m_Factory.GetAdapter(GameConfiguration.AdapterOrdinal).GetOutputCount() - 1;

			if (numberOfAdditionalWindows > 0)
			{
				context.CreateAdditionalForms(numberOfAdditionalWindows);
			}

			context[0].SwapChain = defaultSwapChain; 

			m_Factory.SetWindowAssociation(context[0].Form.Handle, WindowAssociationFlags.IgnoreAll | WindowAssociationFlags.IgnoreAltEnter);
			
			for (int i = 1; i < context.Count; i++)
			{				
				context[i].SwapChain = new SlimDX.DXGI.SwapChain(m_Factory, m_Device, context[i].SwapChainDescription);

				m_Device.Factory.SetWindowAssociation(context[i].Form.Handle, WindowAssociationFlags.IgnoreAll | WindowAssociationFlags.IgnoreAltEnter);
			}

			for (int i = 0; i < context.Count; i++)
			{
				context[i].Form.Location = m_Factory.GetAdapter(GameConfiguration.AdapterOrdinal).GetOutput(i).Description.DesktopBounds.Location; 
			}
        }
 /// <summary>
 /// Creates a new instance of <see cref="D3D10SwapChainImplementation"/>.
 /// </summary>
 /// <param name="renderer">The D3D10 renderer.</param>
 /// <param name="factory">The DXGI factory.</param>
 /// <param name="windowHandle">The window handle.</param>
 /// <param name="presentParams">The presentation parameters to initialize to.</param>
 internal D3D10SwapChainImplementation(D3D10Renderer renderer, DXGI.Factory factory, IntPtr windowHandle, PresentationParameters presentParams)
 {
     _renderer       = renderer;
     _graphicsDevice = _renderer.GraphicsDevice;
     _factory        = factory;
     _resetting      = false;
     _isFullScreen   = false;
     _presentParams  = presentParams;
     _windowHandle   = windowHandle;
     CreateSwapChain(_windowHandle, _presentParams);
     CreateViews();
 }
Exemplo n.º 7
0
        public override void Hook()
        {
            this.DebugMessage("Hook: Begin");

            // Determine method addresses in Direct3D10.Device, and DXGI.SwapChain
            if (_d3d10VTblAddresses == null)
            {
                _d3d10VTblAddresses         = new List <IntPtr>();
                _dxgiSwapChainVTblAddresses = new List <IntPtr>();
                this.DebugMessage("Hook: Before device creation");
                using (SlimDX.DXGI.Factory factory = new SlimDX.DXGI.Factory())
                {
                    using (SlimDX.Direct3D10.Device device = new Device(factory.GetAdapter(0), DriverType.Hardware, DeviceCreationFlags.None))
                    {
                        this.DebugMessage("Hook: Device created");
                        _d3d10VTblAddresses.AddRange(GetVTblAddresses(device.ComPointer, D3D10_DEVICE_METHOD_COUNT));

                        using (SlimDX.Windows.RenderForm renderForm = new SlimDX.Windows.RenderForm())
                        {
                            using (SlimDX.DXGI.SwapChain sc = new SlimDX.DXGI.SwapChain(factory, device, DXGI.CreateSwapChainDescription(renderForm.Handle)))
                            {
                                _dxgiSwapChainVTblAddresses.AddRange(GetVTblAddresses(sc.ComPointer, DXGI.DXGI_SWAPCHAIN_METHOD_COUNT));
                            }
                        }
                    }
                }
            }

            // We will capture the backbuffer here
            DXGISwapChain_PresentHook = LocalHook.Create(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.Present],
                new DXGISwapChain_PresentDelegate(PresentHook),
                this);

            // We will capture target/window resizes here
            DXGISwapChain_ResizeTargetHook = LocalHook.Create(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.ResizeTarget],
                new DXGISwapChain_ResizeTargetDelegate(ResizeTargetHook),
                this);

            /*
             * Don't forget that all hooks will start deactivated...
             * The following ensures that all threads are intercepted:
             * Note: you must do this for each hook.
             */
            DXGISwapChain_PresentHook.ThreadACL.SetExclusiveACL(new Int32[1]);

            DXGISwapChain_ResizeTargetHook.ThreadACL.SetExclusiveACL(new Int32[1]);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="D3D10GraphicsAdapter"/> class.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device.</param>
        /// <param name="factory">The DXGI factory.</param>
        /// <param name="adapterIndex">Index of the adapter.</param>
        internal D3D10GraphicsAdapter(D3D.Device graphicsDevice, DXGI.Factory factory, int adapterIndex)
        {
            _graphicsDevice = graphicsDevice;
            _adapterIndex   = adapterIndex;

            List <DisplayMode> modes = new List <DisplayMode>();

            DXGI.Adapter adapter = factory.GetAdapter(adapterIndex);
            if (adapter != null)
            {
                DXGI.AdapterDescription adDesc = adapter.Description;
                _desc        = adDesc.Description;
                _devId       = adDesc.DeviceId;
                _rev         = adDesc.Revision;
                _subsystemId = adDesc.SubsystemId;
                _vendorId    = adDesc.VendorId;

                DXGI.Output output = adapter.GetOutput(0);
                if (output != null)
                {
                    foreach (SurfaceFormat format in Enum.GetValues(typeof(SurfaceFormat)))
                    {
                        try {
                            System.Collections.ObjectModel.ReadOnlyCollection <DXGI.ModeDescription> modeList = output.GetDisplayModeList(D3D10Helper.ToD3DSurfaceFormat(format), 0);
                            if (modeList == null)
                            {
                                continue;
                            }
                            foreach (DXGI.ModeDescription modeDesc in modeList)
                            {
                                DisplayMode displayMode = new DisplayMode(modeDesc.Width, modeDesc.Height, format);
                                if (!modes.Contains(displayMode))
                                {
                                    modes.Add(displayMode);
                                }
                            }
                        } catch (Exception e) {
                            String s = e.StackTrace;
                            continue;
                        }
                    }
                    _displayModes = new DisplayModeCollection(modes);
                    output.Dispose();
                }
                adapter.Dispose();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Enumerates all the available adapters for the render system. This should
        /// only be used to identify which adapter to use at startup.
        /// </summary>
        /// <returns>List of available adapters</returns>
        public static ReadOnlyList <IGraphicsAdapter> EnumerateAdapters()
        {
            //Create the DXGI Factory
            DXGI.Factory factory = new DXGI.Factory();

            //Create the device
            DXGI.Adapter adapter = factory.GetAdapter(0);
            D3D.Device   device  = new D3D.Device(adapter, D3D.DriverType.Hardware, D3D.DeviceCreationFlags.None);

            //Enumerate adapters
            List <IGraphicsAdapter> adapterList = new List <IGraphicsAdapter>();
            int adapterCount = factory.GetAdapterCount();

            for (int i = 0; i < adapterCount; i++)
            {
                adapterList.Add(new D3D10GraphicsAdapter(device, factory, i));
            }

            factory.Dispose();
            return(new ReadOnlyList <IGraphicsAdapter>(adapterList));
        }
Exemplo n.º 10
0
        private void InitalizeGraphics()
        {
            if (Window.RenderCanvasHandle == IntPtr.Zero)
            {
                throw new InvalidOperationException("Window handle cannot be zero");
            }

            SwapChainDescription swapChainDesc = new SwapChainDescription()
            {
                BufferCount     = 1,
                Flags           = SwapChainFlags.None,
                IsWindowed      = true,
                OutputHandle    = Window.RenderCanvasHandle,
                SwapEffect      = SwapEffect.Discard,
                Usage           = Usage.RenderTargetOutput,
                ModeDescription = new ModeDescription()
                {
                    Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
                    //Format = SlimDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width            = Window.ClientSize.Width,
                    Height           = Window.ClientSize.Height,
                    RefreshRate      = new Rational(60, 1),
                    Scaling          = DisplayModeScaling.Unspecified,
                    ScanlineOrdering = DisplayModeScanlineOrdering.Unspecified
                },
                SampleDescription = new SampleDescription(1, 0)
            };

            var giFactory = new SlimDX.DXGI.Factory();
            var adapter   = giFactory.GetAdapter(0);

            Device    device;
            SwapChain swapChain;

            Device.CreateWithSwapChain(adapter, DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out device, out swapChain);
            _swapChain     = swapChain;
            GraphicsDevice = device;

            // create a view of our render target, which is the backbuffer of the swap chain we just created
            using (var resource = SlimDX.Direct3D10.Resource.FromSwapChain <Texture2D>(swapChain, 0))
            {
                _backBuffer = new RenderTargetView(device, resource);
            }

            // setting a viewport is required if you want to actually see anything
            var viewport = new Viewport(0, 0, Window.ClientSize.Width, Window.ClientSize.Height);

            device.OutputMerger.SetTargets(_backBuffer);
            device.Rasterizer.SetViewports(viewport);

            CreateDepthStencil();
            LoadVisualizationEffect();

            // Allocate a large buffer to write the PhysX visualization vertices into
            // There's more optimized ways of doing this, but for this sample a large buffer will do
            _userPrimitivesBuffer = new SlimDX.Direct3D10.Buffer(GraphicsDevice, VertexPositionColor.SizeInBytes * 50000, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None);

            var elements = new[]
            {
                new InputElement("Position", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("Color", 0, Format.R32G32B32A32_Float, 16, 0)
            };

            _inputLayout = new InputLayout(GraphicsDevice, _visualizationEffect.RenderScenePass0.Description.Signature, elements);
        }
Exemplo n.º 11
0
        public override void Hook()
        {
            this.DebugMessage("Hook: Begin");

            // Determine method addresses in Direct3D10.Device, and DXGI.SwapChain
            if (_d3d10VTblAddresses == null)
            {
                _d3d10VTblAddresses = new List<IntPtr>();
                _dxgiSwapChainVTblAddresses = new List<IntPtr>();
                this.DebugMessage("Hook: Before device creation");
                using (SlimDX.DXGI.Factory factory = new SlimDX.DXGI.Factory())
                {
                    using (SlimDX.Direct3D10.Device device = new Device(factory.GetAdapter(0), DriverType.Hardware, DeviceCreationFlags.None))
                    {
                        this.DebugMessage("Hook: Device created");
                        _d3d10VTblAddresses.AddRange(GetVTblAddresses(device.ComPointer, D3D10_DEVICE_METHOD_COUNT));
                        
                        using (SlimDX.Windows.RenderForm renderForm = new SlimDX.Windows.RenderForm())
                        {
                            using (SlimDX.DXGI.SwapChain sc = new SlimDX.DXGI.SwapChain(factory, device, DXGI.CreateSwapChainDescription(renderForm.Handle)))
                            {
                                _dxgiSwapChainVTblAddresses.AddRange(GetVTblAddresses(sc.ComPointer, DXGI.DXGI_SWAPCHAIN_METHOD_COUNT));
                            }
                        }
                    }
                }
            }

            // We will capture the backbuffer here
            DXGISwapChain_PresentHook = LocalHook.Create(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.Present],
                new DXGISwapChain_PresentDelegate(PresentHook),
                this);

            // We will capture target/window resizes here
            DXGISwapChain_ResizeTargetHook = LocalHook.Create(
                _dxgiSwapChainVTblAddresses[(int)DXGI.DXGISwapChainVTbl.ResizeTarget],
                new DXGISwapChain_ResizeTargetDelegate(ResizeTargetHook),
                this);

            /*
             * Don't forget that all hooks will start deactivated...
             * The following ensures that all threads are intercepted:
             * Note: you must do this for each hook.
             */
            DXGISwapChain_PresentHook.ThreadACL.SetExclusiveACL(new Int32[1]);

            DXGISwapChain_ResizeTargetHook.ThreadACL.SetExclusiveACL(new Int32[1]);
        }