Example #1
0
        protected void InitializeRendering(GraphicsDevice graphicsDevice)
        {
            // Get our initial render target size
            // The scaling factor doesn't seem to affect the image quality. There is no golden value for this as stated in the SDK documentation
            var renderTargetSize = HMD.GetDefaultRenderTargetSize(1.5f);

            // Create the render target texture that is shown on the HMD
            RenderTarget = RenderTarget2D.New(graphicsDevice, renderTargetSize.Width, renderTargetSize.Height, new MipMapCount(1), PixelFormat.R8G8B8A8.UNorm);

            // The created texture can have a different size due to hardware limitations so the render target size needs to be updated
            renderTargetSize.Width  = RenderTarget.Width;
            renderTargetSize.Height = RenderTarget.Height;

            DepthStencilBuffer = DepthStencilBuffer.New(graphicsDevice, renderTargetSize.Width, renderTargetSize.Height, DepthFormat.Depth32, true);

            // Compute the virtual cameras' viewports
            EyeViewport[0] = new Rect(0, 0, renderTargetSize.Width / 2, renderTargetSize.Height);
            EyeViewport[1] = new Rect((renderTargetSize.Width + 1) / 2, 0, EyeViewport[0].Width, EyeViewport[0].Height);

            // Create HMD's eye texture data
            EyeTextureData = new D3D11TextureData[2];
            EyeTextureData[0].Header.API            = RenderAPIType.D3D11;
            EyeTextureData[0].Header.TextureSize    = renderTargetSize;
            EyeTextureData[0].Header.RenderViewport = EyeViewport[0];
            EyeTextureData[0].pTexture = ((SharpDX.Direct3D11.Texture2D)RenderTarget).NativePointer;
            EyeTextureData[0].pSRView  = ((ShaderResourceView)RenderTarget).NativePointer;
            // Right eye holds the same values except for the viewport
            EyeTextureData[1] = EyeTextureData[0];
            EyeTextureData[1].Header.RenderViewport = EyeViewport[1];
        }
        public override void Initialize()
        {
            var device = DeviceService.DirectXDevice;

            if (outputRule == OutputRule.NewRenderTarget)
            {
                renderTarget           = ToDispose(RenderTarget2D.New(device, textureDescription));
                renderTarget.DebugName = string.Format("RT2D_{0}", Name);
                var depthStencilDesc = new Texture2DDescription()
                {
                    ArraySize         = 1,
                    MipLevels         = 1,
                    BindFlags         = BindFlags.DepthStencil,
                    Format            = device.DepthStencilBuffer.Format,
                    Width             = textureDescription.Width,
                    Height            = textureDescription.Height,
                    SampleDescription = textureDescription.SampleDescription,
                    CpuAccessFlags    = CpuAccessFlags.None,
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.None
                };

                depthStencil = ToDispose(DepthStencilBuffer.New(device, depthStencilDesc));
            }

            Output = renderTarget;
        }
Example #3
0
        public override void Process(ITimeService time)
        {
            IsEnabled = false;
            var device             = Services.GetService <IGraphicsDeviceService>().DirectXDevice;
            var cScreenshot        = Entities.First().GetComponent <ScreenshotComponent>();
            var backBuffer         = device.BackBuffer;
            var textureDescription = new Texture2DDescription()
            {
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = backBuffer.Format.Value,
                Height            = backBuffer.Height,
                Usage             = ResourceUsage.Default,
                Width             = backBuffer.Width,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None
            };

            renderTarget = RenderTarget2D.New(device, textureDescription);

            var depthStencilDesc = new Texture2DDescription()
            {
                ArraySize         = 1,
                MipLevels         = 1,
                BindFlags         = BindFlags.DepthStencil,
                Format            = device.DepthStencilBuffer.Format.Value,
                Width             = textureDescription.Width,
                Height            = textureDescription.Height,
                SampleDescription = textureDescription.SampleDescription,
                CpuAccessFlags    = CpuAccessFlags.None,
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.None
            };

            depthStencil = DepthStencilBuffer.New(device, depthStencilDesc);


            ((DeviceContext)device).ResolveSubresource(backBuffer, 0, renderTarget, 0, textureDescription.Format);
            renderTarget.Initialize();
            depthStencil.Initialize();

            device.SetRenderTargets(depthStencil, renderTarget);
            renderTarget.Save("test.png", ImageFileType.Png);

            renderTarget.Dispose();
            depthStencil.Dispose();
            IsEnabled = false;
        }
Example #4
0
        public WarpSceneRenderer(Scene scene, int width, int height)
        {
            _scene       = scene;
            _width       = width;
            _height      = height;
            _aspectRatio = width / (float)height;

            _device = GraphicsDevice.New(DriverType.Warp, DeviceCreationFlags.None, FeatureLevel.Level_10_1);

            var serviceProvider = new ServiceProvider();

            serviceProvider.AddService <IGraphicsDeviceService>(new GraphicsDeviceService(_device));

            _contentManager = new ContentManager(serviceProvider);
            _contentManager.Resolvers.Add(new ContentResolver());

            var viewport = new Viewport(0, 0, _width, _height);

            _device.SetViewports(viewport);

            const MSAALevel msaaLevel = MSAALevel.None;

            _depthStencilTexture = DepthStencilBuffer.New(_device, _width, _height, msaaLevel, DepthFormat.Depth24Stencil8);
            _renderTexture       = RenderTarget2D.New(_device, _width, _height, msaaLevel, PixelFormat.R8G8B8A8.UNorm);

            Options = new RenderOptions();

            _effect = new BasicEffect(_device);
            _effect.EnableDefaultLighting();

            _inputLayout = VertexInputLayout.New(0, typeof(VertexPositionNormalTexture));
            _device.SetVertexInputLayout(_inputLayout);

            _meshes = new List <WarpMesh>();
            foreach (Mesh mesh in _scene.Meshes)
            {
                if (!mesh.Positions.Any())
                {
                    continue;
                }

                var warpMesh = new WarpMesh(_device, mesh);
                _meshes.Add(warpMesh);

                warpMesh.Initialize(_contentManager);
            }
        }
Example #5
0
        /// <summary>
        /// Creates the depth stencil buffer.
        /// </summary>
        protected virtual void CreateDepthStencilBuffer()
        {
            // If no depth stencil buffer, just return
            if (Description.DepthStencilFormat == DepthFormat.None)
            {
                return;
            }

            // Creates the depth stencil buffer.
            DepthStencilBuffer =
                ToDispose(DepthStencilBuffer.New(DirectXDevice,
                                                 Description.BackBufferWidth,
                                                 Description.BackBufferHeight,
                                                 Description.MultiSampleCount,
                                                 Description.DepthStencilFormat,
                                                 Description.DepthBufferShaderResource));
        }
Example #6
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();
        }