public ShadowMap( VisionContent vContent, int width, int height, int nearPlane = 1, int farPlane = 200) { _graphicsDevice = vContent.GraphicsDevice; ShadowDepthTarget = RenderTarget2D.New(_graphicsDevice, width, height, PixelFormat.R16G16.Float); DepthStencilTarget = DepthStencilBuffer.New(_graphicsDevice, width, height, DepthFormat.Depth16); _spriteBatch = new SpriteBatch(_graphicsDevice); _shadowBlurEffect = vContent.LoadEffect("Effects/Blur"); _shadowBlurEffect.Parameters["dx"].SetValue(1f/width); _shadowBlurEffect.Parameters["dy"].SetValue(1f/height); _shadowBlurTarg = RenderTarget2D.New(_graphicsDevice, width, height, PixelFormat.R16G16.Float); ShadowNearPlane = nearPlane; ShadowFarPlane = farPlane; Camera = new Camera( new Vector2(width, height), Vector3.Zero, Vector3.Up, ShadowNearPlane, ShadowFarPlane); UpdateProjection(60, 60); }
/// <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(GraphicsDevice, Description.BackBufferWidth, Description.BackBufferHeight, Description.MultiSampleCount, Description.DepthStencilFormat)); }
/// <summary> /// Creates a new texture with the specified generic texture description. /// </summary> /// <param name="graphicsDevice">The graphics device.</param> /// <param name="description">The description.</param> /// <returns>A Texture instance, either a RenderTarget or DepthStencilBuffer or Texture, depending on Binding flags.</returns> public static Texture New(Direct3D11.Device graphicsDevice, TextureDescription description) { if (graphicsDevice == null) { throw new ArgumentNullException("graphicsDevice"); } if ((description.BindFlags & BindFlags.RenderTarget) != 0) { switch (description.Dimension) { case TextureDimension.Texture1D: return(RenderTarget1D.New(graphicsDevice, description)); case TextureDimension.Texture2D: return(RenderTarget2D.New(graphicsDevice, description)); case TextureDimension.Texture3D: return(RenderTarget3D.New(graphicsDevice, description)); case TextureDimension.TextureCube: return(RenderTargetCube.New(graphicsDevice, description)); } } else if ((description.BindFlags & BindFlags.DepthStencil) != 0) { return(DepthStencilBuffer.New(graphicsDevice, description)); } else { switch (description.Dimension) { case TextureDimension.Texture1D: return(Texture1D.New(graphicsDevice, description)); case TextureDimension.Texture2D: return(Texture2D.New(graphicsDevice, description)); case TextureDimension.Texture3D: return(Texture3D.New(graphicsDevice, description)); case TextureDimension.TextureCube: return(TextureCube.New(graphicsDevice, description)); } } return(null); }
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); } }
internal DepthStencilViewSelector(DepthStencilBuffer texture) { this.texture = texture; }
internal DepthStencilViewSelector(DepthStencilBuffer texture) { this.texture = texture; }
protected override void Initialize() { keyMgr.Initialize(); // Modify the title of the window Window.Title = "RiftGame"; // Attach HMD to window var control = (RenderForm)Window.NativeWindow; control.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; control.StartPosition = System.Windows.Forms.FormStartPosition.Manual; control.DesktopLocation = new System.Drawing.Point(0, 0); hmd.AttachToWindow(control.Handle); // Create our render target //var renderTargetSize = hmd.GetDefaultRenderTargetSize(1.5f); var renderTargetSize = hmd.GetDefaultRenderTargetSize(); renderTarget = RenderTarget2D.New(GraphicsDevice, renderTargetSize.Width, renderTargetSize.Height, 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)) { 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.EnableHSWDisplaySDKRender(false); 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) { control.DesktopLocation = new System.Drawing.Point(hmdOutput.Description.DesktopBounds.Left, hmdOutput.Description.DesktopBounds.Top); //Window.BeginScreenDeviceChange(true); // 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); //Window.EndScreenDeviceChange(); } sprite = new SpriteBatch(this.GraphicsDevice); position = new Vector2(hmd.Resolution.Width / 2, hmd.Resolution.Height / 2); capture = OpenCV.Net.Capture.CreateCameraCapture(0); _form = new RenderForm("Normal"); // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(_form.ClientSize.Width, _form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = _form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput | Usage.ShaderInput | Usage.BackBuffer }; // Used for debugging dispose object references // Configuration.EnableObjectTracking = true; // Disable throws on shader compilation errors //Configuration.ThrowOnShaderCompileError = false; // Create Device and SwapChain SharpDX.Direct3D11.Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.None, desc, out _device, out _swapChain); _context = _device.ImmediateContext; _gdevice = GraphicsDevice.New(_device); _renderTarget = RenderTarget2D.New(_gdevice, renderTargetSize.Width, renderTargetSize.Height, new MipMapCount(1), PixelFormat.R8G8B8A8.UNorm, TextureFlags.RenderTarget | TextureFlags.ShaderResource); _depthStencilBuffer = DepthStencilBuffer.New(_gdevice, renderTargetSize.Width, renderTargetSize.Height, DepthFormat.Depth32, true); _sprite = new SpriteBatch(_gdevice); _swapChain.GetParent<Factory>().MakeWindowAssociation(_form.Handle, WindowAssociationFlags.IgnoreAll); var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(_swapChain, 0); // Renderview on the backbuffer _renderView = new RenderTargetView(_device, backBuffer); // Create the depth buffer var depthBuffer = new SharpDX.Direct3D11.Texture2D(_device, new Texture2DDescription() { Format = Format.D32_Float_S8X24_UInt, ArraySize = 1, MipLevels = 1, Width = _form.ClientSize.Width, Height = _form.ClientSize.Height, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }); // Create the depth buffer view _depthView = new DepthStencilView(_device, depthBuffer); model = null; _model = null; base.Initialize(); }