public override bool Resize(int width, int height, Format format, Rational?refreshRate = null)
        {
            if (!base.Resize(width, height, format, refreshRate))
            {
                return(false);
            }

            string backBufferName = backBuffer.DebugName;

            RemoveAndDispose(ref backBuffer);

            swapChain.ResizeBuffers(BufferCount, width, height, format, Description.Flags);

            // Recreate the back buffer
            backBuffer           = ToDispose(RenderTarget2D.New(DirectXDevice, swapChain.GetBackBuffer <Texture2D>(0)));
            backBuffer.DebugName = backBufferName;
            // Reinit the Viewport
            DefaultViewport = new ViewportF(0, 0, backBuffer.Width, backBuffer.Height);

            return(true);
        }
Exemplo n.º 2
0
        public override GraphicsDevice CreateDevice(GraphicsDeviceInformation deviceInformation)
        {
            var gameWindowBackgroundXaml = gameWindow as GameWindowPhoneBackgroundXaml;

            if (gameWindowBackgroundXaml != null)
            {
                // We don't have anything else than the GraphicsDevice created for the XAML so return it directly.
                return(gameWindowBackgroundXaml.EnsureDevice());
            }

            // Else this is a DrawingSruface
            var device = GraphicsDevice.New(deviceInformation.Adapter, deviceInformation.DeviceCreationFlags, deviceInformation.GraphicsProfile);

            var renderTargetDesc = new Texture2DDescription
            {
                Format            = Format.B8G8R8A8_UNorm,
                Width             = (int)deviceInformation.PresentationParameters.BackBufferWidth,
                Height            = (int)deviceInformation.PresentationParameters.BackBufferHeight,
                ArraySize         = 1,
                MipLevels         = 1,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Usage             = ResourceUsage.Default,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.SharedKeyedmutex | ResourceOptionFlags.SharedNthandle,
                SampleDescription = new DXGI.SampleDescription(1, 0)
            };

            var backBuffer = RenderTarget2D.New(device, renderTargetDesc);

            var graphicsPresenter = new RenderTargetGraphicsPresenter(device, backBuffer, deviceInformation.PresentationParameters.DepthStencilFormat, true);

            device.Presenter = graphicsPresenter;

            var gameWindowXaml = (GameWindowPhoneXaml)gameWindow;

            gameWindowXaml.CreateSynchronizedTexture(backBuffer);

            return(device);
        }
Exemplo n.º 3
0
        private void HandleDeviceChangeEnd(object sender, EventArgs e)
        {
            var device = _graphicsDeviceManager.GraphicsDevice;

            var swapChain = (SwapChain)device.Presenter.NativePresenter;
            var d         = swapChain.Description;

            var d3DDevice = (SharpDX.Direct3D11.Device)device;

            using (var dxgiDevice = d3DDevice.QueryInterface <Device3>())
            {
                var adapter = dxgiDevice.Adapter;
                var factory = adapter.GetParent <Factory2>();

                var description = new SwapChainDescription1
                {
                    Width             = d.ModeDescription.Width,
                    Height            = d.ModeDescription.Height,
                    Format            = d.ModeDescription.Format,
                    Stereo            = false,
                    SampleDescription = new SampleDescription(1, 0),
                    Usage             = Usage.RenderTargetOutput,
                    BufferCount       = 2,
                    SwapEffect        = SwapEffect.FlipSequential,
                    Flags             = SwapChainFlags.ForegroundLayer,
                    Scaling           = Scaling.None,
                    AlphaMode         = AlphaMode.Premultiplied
                };

                // create the foreground swap chain for the core window
                using (var comWindow = new ComObject(Window.NativeWindow))
                    _foregroundChain = new SwapChain1(factory, (SharpDX.Direct3D11.Device)device, comWindow, ref description);

                // recreate the foreground render target
                using (var backBuffer = _foregroundChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                    _foregroundRenderTarget = RenderTarget2D.New(device, backBuffer);
            }
        }
        public void Render(VideoBuffer videoBuffer)
        {
            if (GraphicsDevice == null)
            {
                return;
            }

            if (RenderTarget2D == null)
            {
                RenderTarget2D = RenderTarget2D.New(GraphicsDevice, videoBuffer.Width, videoBuffer.Height, PixelFormat.B8G8R8A8.UNorm);
            }
            else if (RenderTarget2D != null && (RenderTarget2D.Width != videoBuffer.Width || RenderTarget2D.Height != videoBuffer.Height))
            {
                RenderTarget2D.Dispose();
                RenderTarget2D = null;
                RenderTarget2D = RenderTarget2D.New(GraphicsDevice, videoBuffer.Width, videoBuffer.Height, PixelFormat.B8G8R8A8.UNorm);
            }

            if (RenderTarget2D != null)
            {
                var x    = 0;
                var data = videoBuffer.Plane.Data;
                var argb = new int[videoBuffer.Width * videoBuffer.Height];
                for (var i = 0; i < argb.Length; i++)
                {
                    var r = data[x++];
                    var g = data[x++];
                    var b = data[x++];
                    var a = data[x++];
                    argb[i] = ((a & 0xFF) << 24) |
                              ((r & 0xFF) << 16) |
                              ((g & 0xFF) << 8) |
                              ((b & 0xFF) << 0);
                }

                RenderTarget2D.SetData <int>(argb);
            }
        }
Exemplo n.º 5
0
        public Atmosphere()
        {
            GraphicsDevice device = Engine.GraphicsContext.Device;

            transmittanceT = RenderTarget2D.New(device, TRANSMITTANCE_W, TRANSMITTANCE_H, PixelFormat.R16G16B16A16.Float);
            irradianceT    = RenderTarget2D.New(device, SKY_W, SKY_H, PixelFormat.R16G16B16A16.Float);
            inscatterT     = RenderTarget3D.New(device, RES_MU_S * RES_NU, RES_MU, RES_R, PixelFormat.R16G16B16A16.Float);
            deltaET        = RenderTarget2D.New(device, SKY_W, SKY_H, PixelFormat.R16G16B16A16.Float);
            deltaSRT       = RenderTarget3D.New(device, RES_MU_S * RES_NU, RES_MU, RES_R, PixelFormat.R16G16B16A16.Float);
            deltaSMT       = RenderTarget3D.New(device, RES_MU_S * RES_NU, RES_MU, RES_R, PixelFormat.R16G16B16A16.Float);
            deltaJT        = RenderTarget3D.New(device, RES_MU_S * RES_NU, RES_MU, RES_R, PixelFormat.R16G16B16A16.Float);

            atmosphere     = EffectLoader.Load(@"World/Atmosphere/Shaders/atmosphere.fx");
            copyInscatter1 = EffectLoader.Load(@"World/Atmosphere/Shaders/copyInscatter1.fx");
            copyInscatterN = EffectLoader.Load(@"World/Atmosphere/Shaders/copyInscatterN.fx");
            copyIrradiance = EffectLoader.Load(@"World/Atmosphere/Shaders/copyIrradiance.fx");
            inscatter1     = EffectLoader.Load(@"World/Atmosphere/Shaders/inscatter1.fx");
            inscatterN     = EffectLoader.Load(@"World/Atmosphere/Shaders/inscatterN.fx");
            inscatterS     = EffectLoader.Load(@"World/Atmosphere/Shaders/inscatterS.fx");
            irradiance1    = EffectLoader.Load(@"World/Atmosphere/Shaders/irradiance1.fx");
            irradianceN    = EffectLoader.Load(@"World/Atmosphere/Shaders/irradianceN.fx");
            transmittance  = EffectLoader.Load(@"World/Atmosphere/Shaders/transmittance.fx");

            QuadVert[] verts = new QuadVert[6]
            {
                new QuadVert(-1, -1, 0),
                new QuadVert(-1, 1, 0),
                new QuadVert(1, 1, 0),
                new QuadVert(1, 1, 0),
                new QuadVert(1, -1, 0),
                new QuadVert(-1, -1, 0)
            };

            vb = SharpDX.Toolkit.Graphics.Buffer.New <QuadVert>(device, verts, BufferFlags.VertexBuffer);

            PreProcess();
        }
Exemplo n.º 6
0
        public GraphicsDeviceService()
        {
            System.Windows.Forms.Control control = new System.Windows.Forms.Panel();

            deviceInformation = new GraphicsDeviceInformation()
            {
                GraphicsProfile        = FeatureLevel.Level_11_0,
                DeviceCreationFlags    = SharpDX.Direct3D11.DeviceCreationFlags.Debug,
                PresentationParameters = new PresentationParameters()
                {
                    BackBufferFormat          = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    DepthStencilFormat        = DepthFormat.Depth32,
                    DepthBufferShaderResource = true,
                    BackBufferWidth           = 1920,
                    BackBufferHeight          = 1080,
                    IsFullScreen       = false,
                    RenderTargetUsage  = SharpDX.DXGI.Usage.RenderTargetOutput,
                    MultiSampleCount   = MSAALevel.None,
                    DeviceWindowHandle = control
                }
            };

            FindAdapter();

            graphicsDevice = GraphicsDevice.New(deviceInformation.Adapter, deviceInformation.DeviceCreationFlags);

            renderTarget             = RenderTarget2D.New(graphicsDevice, 1920, 1080, PixelFormat.B8G8R8A8.UNorm);
            graphicsDevice.Presenter = new RenderTargetGraphicsPresenter(graphicsDevice, renderTarget, DepthFormat.Depth32, false);

            dxDevice  = (Device)typeof(GraphicsDevice).GetField("Device", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GraphicsDevice);
            dxContext = (DeviceContext)typeof(GraphicsDevice).GetField("Context", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GraphicsDevice);

            if (DeviceCreated != null)
            {
                DeviceCreated(this, EventArgs.Empty);
            }
        }
        protected override void LoadContent()
        {
            _spriteBatch    = ToDisposeContent(new SpriteBatch(GraphicsDevice));
            _depthRectangle = new Rectangle(0, 0, RoomAliveToolkit.Kinect2Calibration.depthImageWidth, RoomAliveToolkit.Kinect2Calibration.depthImageHeight);

            int depthWidth  = RoomAliveToolkit.Kinect2Calibration.depthImageWidth;
            int depthHeight = RoomAliveToolkit.Kinect2Calibration.depthImageHeight;

            _roomAliveEffect = Content.Load <Effect>("Holographic");
            _fromUintEffect  = Content.Load <Effect>("FromUint");
            _gaussianEffect  = Content.Load <Effect>("BilateralFilter");

            _colorSamplerState = ToDisposeContent(SharpDX.Toolkit.Graphics.SamplerState.New(this.GraphicsDevice, new SamplerStateDescription()
            {
                Filter             = Filter.MinMagMipLinear,
                AddressU           = TextureAddressMode.Border,
                AddressV           = TextureAddressMode.Border,
                AddressW           = TextureAddressMode.Border,
                BorderColor        = new SharpDX.Color4(0.5f, 0.5f, 0.5f, 1.0f),
                ComparisonFunction = Comparison.Never,
                MaximumAnisotropy  = 16,
                MipLodBias         = 0,
                MinimumLod         = -float.MaxValue,
                MaximumLod         = float.MaxValue
            }));

            List <VertexPosition> vertices = new List <VertexPosition>();
            List <int>            indices  = new List <int>();

            // vertex buffer
            if (_camera.calibration != null)
            {
                var table = _camera.calibration.ComputeDepthFrameToCameraSpaceTable();
                for (int i = 0; i < depthHeight; i++)
                {
                    for (int j = 0; j < depthWidth; j++)
                    {
                        var point = table[RoomAliveToolkit.Kinect2Calibration.depthImageWidth * i + j];

                        vertices.Add(new VertexPosition(new SharpDX.Vector4(point.X, point.Y, j, i)));
                    }
                }


                for (int i = 0; i < depthHeight - 1; i++)
                {
                    for (int j = 0; j < depthWidth - 1; j++)
                    {
                        int baseIndex = depthWidth * i + j;
                        indices.Add(baseIndex);
                        indices.Add(baseIndex + depthWidth + 1);
                        indices.Add(baseIndex + 1);

                        indices.Add(baseIndex);
                        indices.Add(baseIndex + depthWidth);
                        indices.Add(baseIndex + depthWidth + 1);
                    }
                }

                _vertexArray = vertices.ToArray();
                _indexArray  = indices.ToArray();

                // build the plane geometry of the specified size and subdivision segments
                _geometry = ToDisposeContent(new GeometricPrimitive <VertexPosition>(GraphicsDevice, _vertexArray, _indexArray, true));
            }
            else
            {
                Console.WriteLine("Camera '{0}' is missing calibration", _camera.name);
                Visible = false;
                Enabled = false;
            }

            if (!string.IsNullOrEmpty(_colorImageFilePath))
            {
                _tex2DColorImage1 = SharpDX.Toolkit.Graphics.Texture2D.Load(GraphicsDevice, _colorImageFilePath, TextureFlags.ShaderResource, ResourceUsage.Dynamic);
                _tex2DColorImage2 = SharpDX.Toolkit.Graphics.Texture2D.Load(GraphicsDevice, _colorImageFilePath, TextureFlags.ShaderResource, ResourceUsage.Dynamic);
            }

            _imagingFactory = new SharpDX.WIC.ImagingFactory();
            if (!string.IsNullOrEmpty(_depthImageFilePath))
            {
                var depthImage = new RoomAliveToolkit.ShortImage(RoomAliveToolkit.Kinect2Calibration.depthImageWidth, RoomAliveToolkit.Kinect2Calibration.depthImageHeight);
                RoomAliveToolkit.ProjectorCameraEnsemble.LoadFromTiff(_imagingFactory, depthImage, _depthImageFilePath);

                UpdateDepthImage(this.GraphicsDevice, depthImage.DataIntPtr);
            }

            var floatDepthImageTextureDesc = new Texture2DDescription()
            {
                Width             = depthWidth,
                Height            = depthHeight,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.R32_Float,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
            };

            _floatDepthImageFinal   = ToDisposeContent(RenderTarget2D.New(GraphicsDevice, floatDepthImageTextureDesc));
            _floatDepthImageSubpass = ToDisposeContent(RenderTarget2D.New(GraphicsDevice, floatDepthImageTextureDesc));

            base.LoadContent();
        }
Exemplo n.º 8
0
        protected override void Initialize()
        {
            if (Thread.CurrentThread.Name == null)
            {
                Thread.CurrentThread.Name = "TelepresenceSystem thread";
            }
            //            INIT_TP_window();
            //            INIT_TP_renderTarget();
            //            INIT_TP_eyeTextureRendering();
            //            INIT_TP_d3d11();
            //            INIT_TP_hmd();

            //            base.Initialize();

            //            INIT_txu();
            //            //INIT_TP_text();

            // Modify the title of the window
            Window.Title = "RiftGame";

            // Attach HMD to window
            var control = (System.Windows.Forms.Control)Window.NativeWindow;

            hmd.AttachToWindow(control.Handle);

            // Create our render target
            var renderTargetSize = hmd.GetDefaultRenderTargetSize(1.5f);

            renderTarget = RenderTarget2D.New(GraphicsDevice, renderTargetSize.Width, renderTargetSize.Height,
                                              new MipMapCount(1), PixelFormat.B8G8R8X8.UNorm, TextureFlags.RenderTarget | TextureFlags.ShaderResource);
            //    new MipMapCount(1), PixelFormat.R8G8B8A8.UNorm, TextureFlags.RenderTarget | TextureFlags.ShaderResource);
            renderTargetView   = (RenderTargetView)renderTarget;
            renderTargetSRView = (ShaderResourceView)renderTarget;

            // Create a depth stencil buffer for our render target
            depthStencilBuffer = DepthStencilBuffer.New(GraphicsDevice, renderTargetSize.Width, renderTargetSize.Height, DepthFormat.Depth32, true);

            // Adjust render target size if there were any hardware limitations
            renderTargetSize.Width  = renderTarget.Width;
            renderTargetSize.Height = renderTarget.Height;

            // The viewport sizes are re-computed in case renderTargetSize changed
            eyeRenderViewport    = new Rect[2];
            eyeRenderViewport[0] = new Rect(0, 0, renderTargetSize.Width / 2, renderTargetSize.Height);
            eyeRenderViewport[1] = new Rect((renderTargetSize.Width + 1) / 2, 0, eyeRenderViewport[0].Width, eyeRenderViewport[0].Height);

            // Create our eye texture data
            eyeTexture = new D3D11TextureData[2];
            eyeTexture[0].Header.API            = RenderAPIType.D3D11;
            eyeTexture[0].Header.TextureSize    = renderTargetSize;
            eyeTexture[0].Header.RenderViewport = eyeRenderViewport[0];
            eyeTexture[0].pTexture = ((SharpDX.Direct3D11.Texture2D)renderTarget).NativePointer;
            eyeTexture[0].pSRView  = renderTargetSRView.NativePointer;

            // Right eye uses the same texture, but different rendering viewport
            eyeTexture[1] = eyeTexture[0];
            eyeTexture[1].Header.RenderViewport = eyeRenderViewport[1];

            // Configure d3d11
            var             device   = (SharpDX.Direct3D11.Device)GraphicsDevice;
            D3D11ConfigData d3d11cfg = new D3D11ConfigData();

            d3d11cfg.Header.API            = RenderAPIType.D3D11;
            d3d11cfg.Header.BackBufferSize = hmd.Resolution;
            d3d11cfg.Header.Multisample    = 1;
            d3d11cfg.pDevice        = device.NativePointer;
            d3d11cfg.pDeviceContext = device.ImmediateContext.NativePointer;
            d3d11cfg.pBackBufferRT  = ((RenderTargetView)GraphicsDevice.BackBuffer).NativePointer;
            d3d11cfg.pSwapChain     = ((SharpDX.DXGI.SwapChain)GraphicsDevice.Presenter.NativePresenter).NativePointer;

            // Configure rendering
            eyeRenderDesc = new EyeRenderDesc[2];
            if (!hmd.ConfigureRendering(d3d11cfg, DistortionCapabilities.Chromatic | DistortionCapabilities.TimeWarp, hmd.DefaultEyeFov, eyeRenderDesc))
            //if (!hmd.ConfigureRendering(d3d11cfg, DistortionCapabilities.None, hmd.DefaultEyeFov, eyeRenderDesc))
            //if (!hmd.ConfigureRendering(d3d11cfg, DistortionCapabilities.TimeWarp, hmd.DefaultEyeFov, eyeRenderDesc))
            {
                throw new Exception("Failed to configure rendering");
            }

            // Set enabled capabilities
            hmd.EnabledCaps = HMDCapabilities.LowPersistence | HMDCapabilities.DynamicPrediction;

            // Configure tracking
            hmd.ConfigureTracking(TrackingCapabilities.Orientation | TrackingCapabilities.Position | TrackingCapabilities.MagYawCorrection, TrackingCapabilities.None);

            // Dismiss the Heatlh and Safety Window
            hmd.DismissHSWDisplay();

            // Get HMD output
            var adapter   = (Adapter)GraphicsDevice.Adapter;
            var hmdOutput = adapter.Outputs.FirstOrDefault(o => hmd.DeviceName.StartsWith(o.Description.DeviceName, StringComparison.OrdinalIgnoreCase));

            if (hmdOutput != null)
            {
                // Set game to fullscreen on rift
                var swapChain   = (SwapChain)GraphicsDevice.Presenter.NativePresenter;
                var description = swapChain.Description.ModeDescription;
                swapChain.ResizeTarget(ref description);
                swapChain.SetFullscreenState(true, hmdOutput);
            }

            //GraphicsDevice.SetRasterizerState(GraphicsDevice.RasterizerStates.CullBack);
            GraphicsDevice.SetRasterizerState(GraphicsDevice.RasterizerStates.CullFront);


            base.Initialize();
        }