예제 #1
0
        private void InitializeDeviceResources()
        {
            ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm);

            // Descriptor for the swap chain
            SwapChainDescription swapChainDesc = new SwapChainDescription()
            {
                ModeDescription   = backBufferDesc,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = renderForm.Handle,
                IsWindowed        = true
            };

            // Create device and swap chain
            D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain);
            d3dDeviceContext = d3dDevice.ImmediateContext;

            // Create render target view for back buffer
            using (D3D11.Texture2D backBuffer = swapChain.GetBackBuffer <D3D11.Texture2D>(0))
            {
                renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer);
            }
        }
        public static SharpDX.Direct3D11.Texture2D InitializeComposeTexture(
            SharpDX.Direct3D11.Device sharpDxD3dDevice,
            SizeInt32 size)
        {
            var description = new SharpDX.Direct3D11.Texture2DDescription
            {
                Width             = size.Width,
                Height            = size.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage          = SharpDX.Direct3D11.ResourceUsage.Default,
                BindFlags      = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None
            };
            var composeTexture = new SharpDX.Direct3D11.Texture2D(sharpDxD3dDevice, description);


            using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(sharpDxD3dDevice, composeTexture))
            {
                sharpDxD3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1));
            }

            return(composeTexture);
        }
예제 #3
0
        //startup settings
        private void InitializeDeviceResources()
        {
            //params,FPS,buffer pixel format
            ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(30, 1), Format.R8G8B8A8_UNorm);

            SwapChainDescription swapChainDesc = new SwapChainDescription()
            {
                ModeDescription   = backBufferDesc,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = renderForm.Handle,
                IsWindowed        = true
            };

            //GPU,Special flag,descriptor for swap chain,holders
            D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain);

            //Getting device context
            d3dDeviceContext = d3dDevice.ImmediateContext;

            using (D3D11.Texture2D backBuffer = swapChain.GetBackBuffer <D3D11.Texture2D>(0))
            {
                renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer);
            }
        }
예제 #4
0
        /// <summary>
        /// Function to perform initialization of the view.
        /// </summary>
        protected override void OnInitialize()
        {
            D3D.RenderTargetViewDescription desc = default(D3D.RenderTargetViewDescription);

            desc.Dimension = D3D.RenderTargetViewDimension.Unknown;

            switch (Resource.ResourceType)
            {
            case ResourceType.Texture1D:
                desc = GetDesc1D();
                break;

            case ResourceType.Texture2D:
                desc = GetDesc2D();
                break;

            case ResourceType.Texture3D:
                desc = GetDesc3D();
                break;
            }

            if (desc.Dimension == D3D.RenderTargetViewDimension.Unknown)
            {
                throw new GorgonException(GorgonResult.CannotCreate, Resources.GORGFX_VIEW_CANNOT_BIND_UNKNOWN_RESOURCE);
            }

            D3DView = new D3D.RenderTargetView(Resource.Graphics.D3DDevice, Resource.D3DResource, desc)
            {
                DebugName = string.Format("{0} '{1}' Render Target View", Resource.ResourceType, Resource.Name)
            };
        }
예제 #5
0
 internal MyBackbuffer(SharpDX.Direct3D11.Resource swapChainBB)
 {
     m_resource = swapChainBB;
     m_resource.DebugName = m_debugName;
     m_rtv = new RenderTargetView(MyRender11.Device, swapChainBB);
     m_rtv.DebugName = m_debugName;
 }
예제 #6
0
        /// <summary>
        /// Create all view resources.
        /// </summary>
        Tuple <D3D11.Texture2D, D3D11.RenderTargetView, D3D11.Texture2D, D3D11.DepthStencilView, SharpDX.Mathematics.Interop.RawViewportF, Size2, DpiScaling> IRenderLoopHost.OnRenderLoop_CreateViewResources(EngineDevice device)
        {
            //Get references to current render device
            m_device        = device.DeviceD3D11_1;
            m_deviceContext = m_device.ImmediateContext;

            // Create swapchain and dummy form
            m_swapChain = GraphicsHelper.CreateSwapChainForFullScreen(
                m_dummyForm,
                m_targetOutput, m_targetOutputMode,
                device, m_renderLoop.ViewConfiguration);

            // Take width and height out of the render target
            m_renderTarget     = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(m_swapChain, 0);
            m_renderTargetView = new D3D11.RenderTargetView(m_device, m_renderTarget);

            //Create the depth buffer
            m_renderTargetDepth     = GraphicsHelper.CreateDepthBufferTexture(device, m_targetOutputMode.PixelWidth, m_targetOutputMode.PixelHeight, m_renderLoop.ViewConfiguration);
            m_renderTargetDepthView = new D3D11.DepthStencilView(m_device, m_renderTargetDepth);

            //Define the viewport for rendering
            SharpDX.Mathematics.Interop.RawViewportF viewPort = GraphicsHelper.CreateDefaultViewport(m_targetOutputMode.PixelWidth, m_targetOutputMode.PixelHeight);

            //Return all generated objects
            return(Tuple.Create(m_renderTarget, m_renderTargetView, m_renderTargetDepth, m_renderTargetDepthView, viewPort, new Size2(m_targetOutputMode.PixelWidth, m_targetOutputMode.PixelHeight), DpiScaling.Default));
        }
예제 #7
0
        public RenderTexture(Device device, Vector2I screenSize)
        {
            var textureDesc = new Texture2DDescription()
            {
                Width = screenSize.X,
                Height = screenSize.Y,
                MipLevels = 1,
                ArraySize = 1,
                Format = Format.R32G32B32A32_Float,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None
            };

            _renderTargetTexture = new Texture2D(device, textureDesc);

            _renderTargetView = new RenderTargetView(device, _renderTargetTexture,
                new RenderTargetViewDescription
                {
                    Format = textureDesc.Format,
                    Dimension = RenderTargetViewDimension.Texture2D,
                    Texture2D = {MipSlice = 0},
                });

            // Create the render target view.
            ShaderResourceView = new ShaderResourceView(device, _renderTargetTexture,
                new ShaderResourceViewDescription
                {
                    Format = textureDesc.Format,
                    Dimension = ShaderResourceViewDimension.Texture2D,
                    Texture2D = { MipLevels = 1, MostDetailedMip = 0 },
                });
        }
예제 #8
0
 internal RenderTarget2D(GraphicsDevice device, Direct3D11.Texture2D texture, RenderTargetView renderTargetView = null, bool pureRenderTarget = false)
     : base(device.MainDevice, texture)
 {
     this.pureRenderTarget = pureRenderTarget;
     this.customRenderTargetView = renderTargetView;
     Initialize(Resource);
 }
 internal void ReleaseDevices()
 {
     IsRendererSuppressed = true;
     RenderTarget.Dispose();
     Backbuffer.Dispose();
     RenderTargetSurface.Dispose();
     RenderTargetView.Dispose();
     D2DDeviceContext.Dispose();
     D2DDevice.Dispose();
     D2DFactory.Dispose();
     DXGIDevice.Dispose();
     D3DDevice.Dispose();
     D3DDefaultDevice.Dispose();
     SwapChain.Dispose();
     SwapChain           = null;
     RenderTarget        = null;
     RenderTargetSurface = null;
     Backbuffer          = null;
     RenderTargetView    = null;
     D2DDeviceContext    = null;
     D2DFactory          = null;
     D2DDevice           = null;
     DXGIDevice          = null;
     D3DDevice           = null;
     D3DDefaultDevice    = null;
 }
예제 #10
0
 /// <summary>
 /// Инициализирует объекты связанные с графическим устройство - Девайс его контекст и Свапчейн
 /// </summary>
 private void InitializeDeviceResources()
 {
     //Создаем объектное преставление нашего GPU, его контекст и класс который будет менят местами буфферы в которые рисует наша GPU
     DX11.Device.CreateWithSwapChain(
         SharpDX.Direct3D.DriverType.Hardware,
         DX11.DeviceCreationFlags.None | DX11.DeviceCreationFlags.BgraSupport,
         new[] { SharpDX.Direct3D.FeatureLevel.Level_11_0 },
         new SwapChainDescription()
     {
         ModeDescription = new ModeDescription(
             _renderForm.ClientSize.Width,
             _renderForm.ClientSize.Height,
             new Rational(60, 1),
             Format.R8G8B8A8_UNorm),
         SampleDescription = new SampleDescription(4, 0),
         Usage             = Usage.BackBuffer | Usage.RenderTargetOutput,
         BufferCount       = 2,
         OutputHandle      = _renderForm.Handle,
         IsWindowed        = true,
         SwapEffect        = SwapEffect.Discard,
         Flags             = SwapChainFlags.None
     },
         out _dx11Device,
         out _swapChain);
     //Игноровать все события видновс
     _factory = _swapChain.GetParent <Factory>();
     _factory.MakeWindowAssociation(_renderForm.Handle, WindowAssociationFlags.IgnoreAll);
     // Создаем буффер и вьюшку глубины
     using (var _depthBuffer = new DX11.Texture2D(
                _dx11Device,
                new DX11.Texture2DDescription()
     {
         Format = Format.D32_Float_S8X24_UInt,
         ArraySize = 1,
         MipLevels = 1,
         Width = _renderForm.ClientSize.Width,
         Height = _renderForm.ClientSize.Height,
         SampleDescription = _swapChain.Description.SampleDescription,
         Usage = DX11.ResourceUsage.Default,
         BindFlags = DX11.BindFlags.DepthStencil,
         CpuAccessFlags = DX11.CpuAccessFlags.None,
         OptionFlags = DX11.ResourceOptionFlags.None
     }))
         _depthView = new DX11.DepthStencilView(_dx11Device, _depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription()
         {
             Dimension = (SwapChain.Description.SampleDescription.Count > 1 ||
                          SwapChain.Description.SampleDescription.Quality > 0) ?
                         DX11.DepthStencilViewDimension.Texture2DMultisampled :
                         DX11.DepthStencilViewDimension.Texture2D,
             Flags = DX11.DepthStencilViewFlags.None
         });
     //Создаем буффер и вьюшку для рисования
     using (DX11.Texture2D backBuffer = _swapChain.GetBackBuffer <DX11.Texture2D>(0))
         _renderView = new DX11.RenderTargetView(_dx11Device, backBuffer);
     //Создаем контекст нашего GPU
     _dx11DeviceContext = _dx11Device.ImmediateContext;
     //Устанавливаем размер конечной картинки
     _dx11DeviceContext.Rasterizer.SetViewport(0, 0, _renderForm.ClientSize.Width, _renderForm.ClientSize.Height);
     _dx11DeviceContext.OutputMerger.SetTargets(_depthView, _renderView);
 }
예제 #11
0
        private void SwapChainPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // Check if resources have been initialized.
            if (isDXInitialized)
            {
                Size2 newSize = RenderSizeToPixelSize(e.NewSize);

                // If the requested swap chain is bigger than the current one,
                if (newSize.Width > swapChain.Description1.Width || newSize.Height > swapChain.Description1.Height)
                {
                    // Destroy resources.
                    Utilities.Dispose(ref this.backBufferView);
                    Utilities.Dispose(ref this.backBufferTexture);

                    // Resize swap chain while conserving format and flags.
                    swapChain.ResizeBuffers(swapChain.Description.BufferCount, (int)e.NewSize.Width, (int)e.NewSize.Height, swapChain.Description1.Format, swapChain.Description1.Flags);

                    // Recreate resources.
                    this.backBufferTexture = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0);
                    this.backBufferView    = new D3D11.RenderTargetView(this.device, this.backBufferTexture);
                }

                // Set source size propery
                swapChain.SourceSize = newSize;
            }
        }
예제 #12
0
        public Renderer(IntPtr swapChainPtr, OverlayConfig overlayConfig)
        {
            OverlayConfig = overlayConfig;

            Overlays = new List<IOverlay>();

            var swapChain = (SwapChain) swapChainPtr;
            Device = swapChain.GetDevice<Device>();
            Texture2D = swapChain.GetBackBuffer<Texture2D>(0);
            try
            {
                DeviceContext = new DeviceContext(Device);
            }
            catch (SharpDXException)
            {
                DeviceContext = Device.ImmediateContext;
            }
            TargetView = new RenderTargetView(Device, Texture2D);
            if (DeferredContext)
            {
                DeviceContext.Rasterizer.SetViewports(new ViewportF(0, 0, Texture2D.Description.Width, Texture2D.Description.Height, 0, 1));
                DeviceContext.OutputMerger.SetTargets(TargetView);
            }
            SpriteEngine = new DXSpriteRenderer(Device, DeviceContext);
            SpriteEngine?.Initialize();

            InitializeElementResources();
        }
예제 #13
0
		private void InitializeDeviceResources()
		{
			ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm);
			
			// Descriptor for the swap chain
			SwapChainDescription swapChainDesc = new SwapChainDescription()
			{
				ModeDescription = backBufferDesc,
				SampleDescription = new SampleDescription(1, 0),
				Usage = Usage.RenderTargetOutput,
				BufferCount = 1,
				OutputHandle = renderForm.Handle,
				IsWindowed = true
			};

			// Create device and swap chain
			D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain);
			d3dDeviceContext = d3dDevice.ImmediateContext;

			// Create render target view for back buffer
			using(D3D11.Texture2D backBuffer = swapChain.GetBackBuffer<D3D11.Texture2D>(0))
			{
				renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer);
			}

			// Set back buffer as current render target view
			d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView);
		}
예제 #14
0
        public void Dispose()
        {
            _closedEvent.Set();
            _framePool?.Dispose();
            _session?.Dispose();
            if (_captureItem != null)
            {
                _captureItem.Closed -= OnClosed;
            }
            _captureItem = null;
            _device      = null;
            _d3dDevice   = null;
            _composeTexture?.Dispose();
            _composeTexture = null;
            _composeRenderTargetView?.Dispose();
            _composeRenderTargetView = null;
            _currentFrame?.Dispose();

            //_session?.Dispose();
            //_swapChain?.Dispose();

            //_swapChain = null;
            //_framePool = null;
            //_session = null;
            //_captureItem = null;
        }
예제 #15
0
        private void InitializeDeviceResources()
        {
            ModeDescription backBufferDesc = new ModeDescription(Size.X, Size.Y, new Rational(60, 1), Format.R8G8B8A8_UNorm);

            SwapChainDescription swapChainDesc = new SwapChainDescription()
            {
                ModeDescription   = backBufferDesc,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 2,
                IsWindowed        = true,
                OutputHandle      = Form.Handle
            };

            viewport = new Viewport(0, 0, Size.X, Size.Y);

            D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.Debug, swapChainDesc, out device, out swapChain);

            deviceContext = device.ImmediateContext;

            using (D3D11.Texture2D backBuffer = swapChain.GetBackBuffer <D3D11.Texture2D>(0))
            {
                view = new D3D11.RenderTargetView(device, backBuffer);
            }

            deviceContext.OutputMerger.SetRenderTargets(view);

            deviceContext.Rasterizer.SetViewport(viewport);

            vertexBuffer          = D3D11.Buffer.Create <Vertex>(device, D3D11.BindFlags.VertexBuffer, new Vertex[1]);
            lastVertexArrayLength = 1;
        }
예제 #16
0
        void InitializeSharedBackBuffer(IntPtr resourcePtr)
        {
            // convert native pointer to DXGI shared resource
            Resource resource = CppObject.FromPointer <Resource>(resourcePtr).QueryInterface <Resource>();

            // convert shared resource to D3D11 Texture
            D3D11.Texture2D sharedBackbuffer = device.OpenSharedResource <D3D11.Texture2D>(resource.SharedHandle);

            // release reference
            resource.Dispose();

            // use D3D11 Texture as render target
            D3D11.RenderTargetViewDescription desc = new D3D11.RenderTargetViewDescription();
            desc.Format             = Format.B8G8R8A8_UNorm;
            desc.Dimension          = D3D11.RenderTargetViewDimension.Texture2D;
            desc.Texture2D.MipSlice = 0;

            renderTargetView = new D3D11.RenderTargetView(device, sharedBackbuffer, desc);
            deviceContext.OutputMerger.SetRenderTargets(renderTargetView);

            // release reference
            sharedBackbuffer.Dispose();

            // setup viewport
            Size size = Utils.WpfSizeToPixels(ImageGrid);

            deviceContext.Rasterizer.SetViewport(new Viewport(0, 0, (int)size.Width, (int)size.Height, 0.0f, 1.0f));
        }
예제 #17
0
        /// <summary>
        /// Resize backbuffer the specified width and height
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <exception cref="System.Exception">
        /// </exception>
        public void Resize(int width, int height)
        {
            if (d3dDevice == null || swapChain == null || d3dContext == null)
            {
                throw new Exception(MethodBase.GetCurrentMethod().Name + "Device, DeviceContext or SwapChain is null");
            }

            if (width <= 0 || height <= 0)
            {
                throw new Exception(MethodBase.GetCurrentMethod().Name + " Width or height less than or equal to zero");
            }

            this.width  = width;
            this.height = height;

            if (d3dRenderTarget != null)
            {
                d3dContext.OutputMerger.SetRenderTargets(null, (D3D11.RenderTargetView)null);
                d3dRenderTarget.Dispose();
                d3dRenderTarget = null;
            }

            swapChain.ResizeBuffers(1,
                                    width, height,
                                    swapChain.Description.ModeDescription.Format,
                                    swapChain.Description.Flags);

            d3dRenderTarget = CreateRenderTarget();
            d3dDepthStencil = CreateDepthStencil();

            System.Diagnostics.Debug.WriteLine(MethodBase.GetCurrentMethod().Name + " Buffers resized " + width + ":" + height);

            viewport = new Viewport(0, 0, width, height);
        }
예제 #18
0
        public static D3D.Texture2D CreateSurface(this D3D.Device device, int width, int height, DXGI.SampleDescription sampleDescription, out D3D.RenderTargetView renderTarget)
        {
            var desc = new D3D.Texture2DDescription
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = DXGI.Format.R8G8B8A8_UNorm,
                SampleDescription = sampleDescription,
                Usage             = D3D.ResourceUsage.Default,
                BindFlags         = D3D.BindFlags.RenderTarget | D3D.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D.CpuAccessFlags.None,
                OptionFlags       = D3D.ResourceOptionFlags.Shared,
            };
            var surface = new D3D.Texture2D(device, desc);

            var targetDesc = new D3D.RenderTargetViewDescription
            {
                Format    = desc.Format,
                Dimension = desc.SampleDescription.Count == 1
                        ? D3D.RenderTargetViewDimension.Texture2D
                        : D3D.RenderTargetViewDimension.Texture2DMultisampled,
            };

            renderTarget = new D3D.RenderTargetView(device, surface, targetDesc);

            return(surface);
        }
 /// <summary>
 /// Disposes all loaded view resources.
 /// </summary>
 private void OnRenderLoopDisposeViewResources(EngineDevice engineDevice)
 {
     m_renderTargetDepth = GraphicsHelper.DisposeObject(m_renderTargetDepth);
     m_depthBuffer       = GraphicsHelper.DisposeObject(m_depthBuffer);
     m_renderTargetView  = GraphicsHelper.DisposeObject(m_renderTargetView);
     m_backBuffer        = GraphicsHelper.DisposeObject(m_backBuffer);
 }
예제 #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RenderTargets"/> struct.
 /// </summary>
 /// <param name="colorBuffer">The color buffer.</param>
 /// <param name="depthStencilBuffer">The depth stencil buffer.</param>
 /// <param name="objectIDBuffer">The object identifier buffer.</param>
 public RenderTargets(D3D11.RenderTargetView colorBuffer, D3D11.DepthStencilView depthStencilBuffer, D3D11.RenderTargetView objectIDBuffer)
 {
     this.ColorBuffer        = colorBuffer;
     this.DepthStencilBuffer = depthStencilBuffer;
     this.ObjectIDBuffer     = objectIDBuffer;
     this.NormalDepthBuffer  = null;
 }
예제 #21
0
        public void Draw(Device device, DeviceContext context, RenderTargetView renderTargetView)
        {
            var deviceChanged = _device != device || _context != context;
            _device = device;
            _context = context;

            if (deviceChanged)
            {
                DrawingSurfaceState.Device = _device;
                DrawingSurfaceState.Context = _context;
                DrawingSurfaceState.RenderTargetView = renderTargetView;
            }

            if (!_game.Initialized)
            {
                // Start running the game.
                _game.Run(GameRunBehavior.Asynchronous);
            }
            else if (deviceChanged)
            {
                _game.GraphicsDevice.Initialize();

                Microsoft.Xna.Framework.Content.ContentManager.ReloadGraphicsContent();

                // DeviceReset events
                _game.graphicsDeviceManager.OnDeviceReset(EventArgs.Empty);
                _game.GraphicsDevice.OnDeviceReset();
            }

            _game.GraphicsDevice.UpdateTarget(renderTargetView);
            _game.GraphicsDevice.ResetRenderTargets();
            _game.Tick();

            _host.RequestAdditionalFrame();
        }
예제 #22
0
 public RenderViews(D3D11.RenderTargetView rtv, D3D11.DepthStencilView dsv,
                    D3D11.ShaderResourceView rtv_srvt, D3D11.ShaderResourceView dsv_srvt)
     : this(rtv, dsv)
 {
     this.RT_SRVT = rtv_srvt;
     this.DS_SRVT = dsv_srvt;
 }
예제 #23
0
        public void Dispose()
        {
            if (_rtv != null)
            {
                _rtv.Dispose();
            }
            _rtv = null;

            if (_swap != null)
            {
                _swap.Dispose();
            }
            _swap = null;

            if (ToolkitDevice != null)
            {
                ToolkitDevice.Dispose();
            }
            ToolkitDevice = null;

            if (_context != null)
            {
                _context.Dispose();
            }
            _context = null;

            if (_device != null)
            {
                _device.Dispose();
            }
            _device = null;

            _width  = 0;
            _height = 0;
        }
예제 #24
0
        public void ApplyPostEffect(
            D3D11.ShaderResourceView sourceRenderTarget,
            D3D11.ShaderResourceView sourceDepthStencil,
            D3D11.RenderTargetView destRenderTarget,
            PostProcessing.PostEffect effect)
        {
            D3D11.DeviceContext deviceContext = deviceResources.DeviceContext;
            // No rendering directly to DepthStencil
            deviceContext.OutputMerger.SetRenderTargets(null, destRenderTarget);

            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
            deviceContext.InputAssembler.InputLayout       = effect.Shader.InputLayout;

            deviceContext.VertexShader.Set(effect.Shader.VertexShader);
            deviceContext.PixelShader.Set(effect.Shader.PixelShader);

            deviceContext.PixelShader.SetShaderResource(0, sourceRenderTarget);
            deviceContext.PixelShader.SetShaderResource(1, sourceDepthStencil);

            deviceContext.PixelShader.SetSampler(0, sampler);

            deviceContext.Draw(4, 0);

            // Reset
            deviceContext.OutputMerger.SetRenderTargets(
                renderTargetHandler.GetDepthStencilView(),
                renderTargetHandler.GetRenderTargetView()
                );
        }
예제 #25
0
        protected void InitializeDeviceResources()
        {
            ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm);

            SwapChainDescription swapChainDesc = new SwapChainDescription()
            {
                ModeDescription   = backBufferDesc,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = renderForm.Handle,
                IsWindowed        = true
            };

            D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain);
            d3dDeviceContext = d3dDevice.ImmediateContext;

            viewport = new Viewport(0, 0, Width, Height);
            d3dDeviceContext.Rasterizer.SetViewport(viewport);

            using (D3D11.Texture2D backBuffer = swapChain.GetBackBuffer <D3D11.Texture2D>(0))
            {
                renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer);
            }
        }
 internal static void DisplayHistogram(RenderTargetView rtv)
 {
     RC.Context.PixelShader.SetShaderResource(0, m_histogram.ShaderView);
     RC.Context.PixelShader.Set(m_drawHistogram);
     RC.Context.OutputMerger.SetRenderTargets(rtv);
     MyScreenPass.DrawFullscreenQuad(new MyViewport(64, 64, 512, 64));
 }
예제 #27
0
	    RenderTargetView IRenderTarget.GetRenderTargetView(int arraySlice)
	    {
            if (arraySlice >= Depth)
                throw new ArgumentOutOfRangeException("The arraySlice is out of range for this Texture3D.");

            // Dispose the previous target.
	        if (_currentSlice != arraySlice && _renderTargetView != null)
	        {
	            _renderTargetView.Dispose();
	            _renderTargetView = null;
	        }

            // Create the new target view interface.
	        if (_renderTargetView == null)
	        {
	            _currentSlice = arraySlice;

	            var desc = new RenderTargetViewDescription
	            {
	                Format = SharpDXHelper.ToFormat(_format),
	                Dimension = RenderTargetViewDimension.Texture3D,
	                Texture3D =
	                    {
	                        DepthSliceCount = -1,
	                        FirstDepthSlice = arraySlice,
	                        MipSlice = 0,
	                    }
	            };

	            _renderTargetView = new RenderTargetView(GraphicsDevice._d3dDevice, GetTexture(), desc);
	        }

	        return _renderTargetView;
	    }
예제 #28
0
        public void Apply()
        {
            if (Native == null || mDepthView == null)
                throw new InvalidOperationException("Cannot bind render target before its initialized");

            mOldRenderTarget = mContext.Context.OutputMerger.GetRenderTargets(1, out mOldDepthView)[0];
            mContext.Context.OutputMerger.SetRenderTargets(mDepthView, Native);
        }
예제 #29
0
        public override void Draw(Device device, DeviceContext context, RenderTargetView renderTargetView)
        {
            // We just clear the render target view
            context.ClearRenderTargetView(renderTargetView, Color.CornflowerBlue);

            // Ask the DrawingSurface to call us back
            host.RequestAdditionalFrame();
        }
예제 #30
0
파일: Filter.cs 프로젝트: romanchom/Luna
 public virtual void Apply(DeviceContext context, ShaderResourceView source, RenderTargetView destination, float scale = 1f, float offset = 0f)
 {
     context.OutputMerger.SetTargets(destination);
     context.PixelShader.SetShaderResource(0, source);
     Texture2D tex2D = destination.ResourceAs<Texture2D>();
     context.Rasterizer.SetViewport(0f, 0f, (float)tex2D.Description.Width, (float)tex2D.Description.Height, 0f, 1f);
     tex2D.Dispose();
     ScreenQuad.Draw(context, scale, offset);
 }
예제 #31
0
 internal override void Release()
 {
     if (m_RTV != null)
     {
         m_RTV.Dispose();
         m_RTV = null;
     }
     base.Release();
 }
 /// <summary>
 /// Disposes all loaded view resources.
 /// </summary>
 void IRenderLoopHost.OnRenderLoop_DisposeViewResources(EngineDevice engineDevice)
 {
     m_renderTargetDepth      = GraphicsHelper.DisposeObject(m_renderTargetDepth);
     m_depthBuffer            = GraphicsHelper.DisposeObject(m_depthBuffer);
     m_renderTargetView       = GraphicsHelper.DisposeObject(m_renderTargetView);
     m_backBuffer             = GraphicsHelper.DisposeObject(m_backBuffer);
     m_backBufferMultisampled = GraphicsHelper.DisposeObject(m_backBufferMultisampled);
     m_swapChain = GraphicsHelper.DisposeObject(m_swapChain);
 }
 /// <summary>	
 /// Get references to the render targets that are available to the {{output-merger stage}}.	
 /// </summary>	
 /// <remarks>	
 /// Any returned interfaces will have their reference count incremented by one. Applications should call {{IUnknown::Release}} on the returned interfaces when they are no longer needed to avoid memory leaks. 	
 /// </remarks>	
 /// <param name="numViews">Number of render targets to retrieve. </param>
 /// <returns>an array of render targets views (see <see cref="SharpDX.Direct3D11.RenderTargetView"/>) to be filled with the render targets from the device.</returns>
 /// <unmanaged>void OMGetRenderTargets([In] int NumViews,[Out, Buffer, Optional] ID3D10RenderTargetView** ppRenderTargetViews,[Out, Optional] ID3D10DepthStencilView** ppDepthStencilView)</unmanaged>
 public SharpDX.Direct3D11.RenderTargetView[] GetRenderTargets(int numViews)
 {
     var renderTargets = new RenderTargetView[numViews];
     DepthStencilView depthStencilView;
     GetRenderTargets(numViews, renderTargets, out depthStencilView);
     if (depthStencilView != null)
         depthStencilView.Dispose();
     return renderTargets;
 }
예제 #34
0
        private void InitializeComposeTexture(SizeInt32 size)
        {
            var description = new SharpDX.Direct3D11.Texture2DDescription {
                Width             = size.Width,
                Height            = size.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage          = SharpDX.Direct3D11.ResourceUsage.Default,
                BindFlags      = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None
            };
            var tardescription = new SharpDX.Direct3D11.Texture2DDescription {
                Width             = 1920,
                Height            = 1080,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage          = SharpDX.Direct3D11.ResourceUsage.Default,
                BindFlags      = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None
            };

            _tarComposeTexture = new SharpDX.Direct3D11.Texture2D(_d3dDevice, tardescription);

            _composeTexture          = new SharpDX.Direct3D11.Texture2D(_d3dDevice, description);
            _composeRenderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, _composeTexture);

            byte[] data = new byte[1920 * 1080 * 4];
            for (int i = 0; i < 1920 * 1080 * 4; i++)
            {
                if ((i + 1) % 4 == 0)
                {
                    data[i] = 255;
                }
                else
                {
                    data[i] = 255;
                }
            }
            DataStream    s    = DataStream.Create(data, true, true);
            DataRectangle rect = new DataRectangle(s.DataPointer, 1920 * 4);

            _blankComposeTexture = new SharpDX.Direct3D11.Texture2D(_d3dDevice, tardescription, rect);
        }
        internal static void DisplayHistogram(RenderTargetView rtv, ShaderResourceView avgLumSrv)
        {
            RC.DeviceContext.PixelShader.SetShaderResources(0, m_histogram.ShaderView, avgLumSrv);
            RC.DeviceContext.PixelShader.Set(m_drawHistogram);
            RC.DeviceContext.OutputMerger.SetRenderTargets(rtv);
            MyScreenPass.DrawFullscreenQuad(new MyViewport(64, 64, 512, 64));

            RC.DeviceContext.PixelShader.Set(m_drawTonemapping);
            MyScreenPass.DrawFullscreenQuad(new MyViewport(64, 128, 512, 64));
        }
예제 #36
0
        public Bitmap CaptureAndProcess(ModernCaptureItemDescription itemDescription)
        {
            // Assuming old texture is already disposed
            description = itemDescription;

            // Initialize DirectX context (for the canvas)
            textureSDRImage = new D3D11.Texture2D(d3dDevice, new D3D11.Texture2DDescription
            {
                Width             = description.CanvasRect.Width,
                Height            = description.CanvasRect.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = DXGI.Format.B8G8R8A8_UNorm_SRgb,
                Usage             = D3D11.ResourceUsage.Default,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                BindFlags         = D3D11.BindFlags.RenderTarget,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None,
            });
            rtvSdrTexture = new D3D11.RenderTargetView(d3dDevice, textureSDRImage);

            d3dContext.Rasterizer.SetViewport(new Viewport(0, 0, description.CanvasRect.Width, description.CanvasRect.Height));
            d3dContext.OutputMerger.SetRenderTargets(rtvSdrTexture);
            d3dContext.ClearRenderTargetView(rtvSdrTexture, new SharpDX.Mathematics.Interop.RawColor4 {
                A = 0.0f, B = 0.0f, G = 0.0f, R = 0.0f
            });

            foreach (var item in description.Regions)
            {
                using (var session = new ModernCaptureMonitorSession(wrtD3D11Device, item))
                {
                    Direct3D11CaptureFrame f;
                    currentSession = session;
                    session.Session.StartCapture();
                    while ((f = session.FramePool.TryGetNextFrame()) == null)
                    {
                        Thread.Sleep(1);
                    }
                    ProcessFrame(f);
                    f.Dispose();
                }
            }

            // Process final 8-bit bitmap
            var gdiPlusBitmap = new Bitmap(DumpAndSaveImage());

            rtvSdrTexture.Dispose();
            textureSDRImage.Dispose();
            description = null;

#if DEBUG
            System.Diagnostics.Debug.WriteLine(SharpDX.Diagnostics.ObjectTracker.ReportActiveObjects());
#endif
            return(gdiPlusBitmap);
        }
예제 #37
0
        /// <summary>
        /// Disposes all loaded view resources.
        /// </summary>
        void IRenderLoopHost.OnRenderLoop_DisposeViewResources(EngineDevice device)
        {
            m_renderTargetDepthView    = GraphicsHelper.DisposeObject(m_renderTargetDepthView);
            m_renderTargetDepth        = GraphicsHelper.DisposeObject(m_renderTargetDepth);
            m_renderTargetView         = GraphicsHelper.DisposeObject(m_renderTargetView);
            m_renderTarget             = GraphicsHelper.DisposeObject(m_renderTarget);
            m_copyHelperTextureStaging = GraphicsHelper.DisposeObject(m_copyHelperTextureStaging);

            m_device        = null;
            m_deviceContext = null;
        }
예제 #38
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="rtv"></param>
 internal RenderTargetSurface( RenderTargetView rtv, UnorderedAccessView uav, Resource resource, int subresource, ColorFormat format, int width, int height, int sampleCount )
 {
     Width			=	width;
     Height			=	height;
     Format			=	format;
     SampleCount		=	sampleCount;
     RTV				=	rtv;
     UAV				=	uav;
     Resource		=	resource;
     Subresource		=	subresource;
 }
예제 #39
0
        internal static void Run(RenderTargetView renderTarget, RenderTargetView intermediateRenderTarget, ShaderResourceView intermediateResourceView, ShaderResourceView initialResourceView,
                                 int maxOffset = 5, MyBlurDensityFunctionType densityFunctionType = MyBlurDensityFunctionType.Gaussian, float WeightParameter = 1.5f,
                                 DepthStencilState depthStencilState = null, int stencilRef = 0x0, bool copyOnStencilFail = false,
                                 float depthDiscardThreshold = 0.0f, MyViewport? viewport = null)
        {
            ProfilerShort.Begin("MyBlur.Run");
            MyGpuProfiler.IC_BeginBlock("MyBlur.Run");
            Debug.Assert(initialResourceView != null);
            Debug.Assert(intermediateResourceView != null);
            Debug.Assert(intermediateRenderTarget != null);
            Debug.Assert(renderTarget != null);

            int shaderKey = InitShaders(densityFunctionType, maxOffset, copyOnStencilFail, depthDiscardThreshold);

            RC.DeviceContext.PixelShader.SetConstantBuffer(5, m_blurConstantBuffer);

            BlurConstants constants = new BlurConstants
            {
                DistributionWeight = WeightParameter,
                StencilRef = stencilRef,
            };
            var mapping = MyMapping.MapDiscard(m_blurConstantBuffer);
            mapping.WriteAndPosition(ref constants);
            mapping.Unmap();

            // Horizontal pass
            MyRender11.DeviceContext.ClearRenderTargetView(intermediateRenderTarget, Color4.White);
            RC.DeviceContext.OutputMerger.SetTargets(intermediateRenderTarget);
            RC.SetDS(depthStencilState, stencilRef);
            RC.DeviceContext.PixelShader.SetShaderResource(0, MyGBuffer.Main.DepthStencil.m_SRV_depth);
            RC.DeviceContext.PixelShader.SetShaderResource(4, MyGBuffer.Main.DepthStencil.m_SRV_stencil);
            RC.DeviceContext.PixelShader.SetShaderResource(5, initialResourceView);
            RC.SetPS(m_blurShaders[shaderKey].Item1);
            MyScreenPass.DrawFullscreenQuad(viewport);
            RC.DeviceContext.PixelShader.SetShaderResource(5, null);

            // Vertical pass
            MyRender11.DeviceContext.ClearRenderTargetView(renderTarget, Color4.White);
            RC.DeviceContext.OutputMerger.SetTargets(renderTarget);
            RC.SetDS(depthStencilState, stencilRef);
            RC.DeviceContext.PixelShader.SetShaderResource(0, MyGBuffer.Main.DepthStencil.m_SRV_depth);
            RC.DeviceContext.PixelShader.SetShaderResource(4, MyGBuffer.Main.DepthStencil.m_SRV_stencil);
            RC.DeviceContext.PixelShader.SetShaderResource(5, intermediateResourceView);
            RC.SetPS(m_blurShaders[shaderKey].Item2);
            MyScreenPass.DrawFullscreenQuad(viewport);

            RC.DeviceContext.PixelShader.SetShaderResource(0, null);
            RC.DeviceContext.PixelShader.SetShaderResource(4, null);
            RC.DeviceContext.PixelShader.SetShaderResource(5, null);

            MyGpuProfiler.IC_EndBlock();
            ProfilerShort.End();
        }
예제 #40
0
 public Texture(PPDDevice device, SharpDX.Direct3D11.Texture2D texture, bool pa, bool isRenderTarget)
 {
     this.device         = device;
     this.pa             = pa;
     _Texture            = texture;
     _ShaderResourceView = new ShaderResourceView((SharpDX.Direct3D11.Device)((PPDFramework.DX11.PPDDevice)device).Device, texture);
     Surface             = new Surface();
     if (isRenderTarget)
     {
         _RenderTargetView = new RenderTargetView((SharpDX.Direct3D11.Device)((PPDFramework.DX11.PPDDevice)device).Device, texture);
     }
 }
예제 #41
0
        public void CreateResources(D3D11.Device device, int sampleCount, int sampleQuality, int width, int height)
        {
            FieldOfView = width / (float)height;
            // render target
            D3D11.Texture2DDescription targetTextureDesc = new D3D11.Texture2DDescription()
            {
                Format            = DXGI.Format.R8G8B8A8_UNorm,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new DXGI.SampleDescription(sampleCount, sampleQuality),
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };
            using (D3D11.Texture2D target = new D3D11.Texture2D(device, targetTextureDesc)) {
                renderTargetResource = new D3D11.ShaderResourceView(device, target);
                renderTargetView     = new D3D11.RenderTargetView(device, target);
            }

            // depth buffer
            D3D11.Texture2DDescription depthTextureDesc = new D3D11.Texture2DDescription()
            {
                Format            = DXGI.Format.R32_Typeless,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new DXGI.SampleDescription(sampleCount, sampleQuality),
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };
            D3D11.DepthStencilViewDescription depthViewDesc = new D3D11.DepthStencilViewDescription()
            {
                Flags     = D3D11.DepthStencilViewFlags.None,
                Dimension = D3D11.DepthStencilViewDimension.Texture2D,
                Format    = DXGI.Format.D32_Float,
            };
            D3D11.ShaderResourceViewDescription depthResourceDesc = new D3D11.ShaderResourceViewDescription()
            {
                Format    = DXGI.Format.R32_Float,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D
            };
            depthResourceDesc.Texture2D.MipLevels = 1;
            using (D3D11.Texture2D depthTexture = new D3D11.Texture2D(device, depthTextureDesc)) {
                depthStencilView     = new D3D11.DepthStencilView(device, depthTexture, depthViewDesc);
                depthStencilResource = new D3D11.ShaderResourceView(device, depthTexture, depthResourceDesc);
            }
        }
예제 #42
0
 private void InitializeBackBuffer()
 {
     // Set the back buffer as the render target view
     using (D3D11.Texture2D backBuffer = swapChain.GetBackBuffer <D3D11.Texture2D>(0))
     {
         if (renderTargetView != null)
         {
             renderTargetView.Dispose();
         }
         renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer);
     }
 }
예제 #43
0
        /// <summary>
        /// Disposes all loaded view resources.
        /// </summary>
        void IRenderLoopHost.OnRenderLoop_DisposeViewResources(EngineDevice device)
        {
            m_factory             = null;
            m_renderDevice        = null;
            m_renderDeviceContext = null;

            m_renderTargetDepth = GraphicsHelper.DisposeObject(m_renderTargetDepth);
            m_depthBuffer       = GraphicsHelper.DisposeObject(m_depthBuffer);
            m_renderTarget      = GraphicsHelper.DisposeObject(m_renderTarget);
            m_backBuffer        = GraphicsHelper.DisposeObject(m_backBuffer);
            m_swapChain         = GraphicsHelper.DisposeObject(m_swapChain);
        }
예제 #44
0
        public void Update(Device device, DeviceContext context, RenderTargetView renderTargetView)
        {
            if (device != _device)
            {
                _device = device;
                CreateDeviceResources();
            }

            _deviceContext = context;
            _renderTargetview = renderTargetView;

            CreateWindowSizeDependentResources();
        }
예제 #45
0
 internal void Release()
 {
     if (m_rtv != null)
     {
         m_rtv.Dispose();
         m_rtv = null;
     }
     if (m_resource != null)
     {
         m_resource.Dispose();
         m_resource = null;
     }
 }
예제 #46
0
        public RenderTargetDirect2D(RenderTargetView renderTargetView11,
                                    SharpDX.Direct3D10.RenderTargetView renderTargetView10,
                                    SharpDX.Direct2D1.RenderTarget context,
                                    Configuration.CreationSettings settings)
            : base(null, null)
        {
            RenderTargetView11 = renderTargetView11;
            RenderTargetView10 = renderTargetView10;
            RenderTargetClearSettings = settings.RenderTargetClearSettings;
            Context = context;

            Device10Mutex = renderTargetView10.Resource.QueryInterface<KeyedMutex>();
            Device11Mutex = renderTargetView11.Resource.QueryInterface<KeyedMutex>();
        }
예제 #47
0
        public void InitializeDeviceResources()
        {
            ModeDescription backBufferDesc = new ModeDescription(1600, 900, new Rational(60, 1), Format.B8G8R8A8_UNorm);

            SwapChainDescription swapChainDesc = new SwapChainDescription()
            {
                ModeDescription   = backBufferDesc,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = this.Handle,
                IsWindowed        = true
            };

            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug;

            //SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain);
            SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, creationFlags, swapChainDesc, out d3dDevice, out swapChain);
            d3dDeviceContext = d3dDevice.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>();

            using (SharpDX.Direct3D11.Texture2D backBuffer = swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
            {
                renderTargetView = new SharpDX.Direct3D11.RenderTargetView(d3dDevice, backBuffer);
            }

            System.Drawing.Graphics g = this.CreateGraphics();

            SharpDX.Direct2D1.Factory d2dFactory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded, SharpDX.Direct2D1.DebugLevel.None);

            // Create Direct2D device
            var dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device>();

            //d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice);
            d2dDevice  = new SharpDX.Direct2D1.Device(dxgiDevice);
            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);
            //d2dContext.PrimitiveBlend = PrimitiveBlend.SourceOver;

            BitmapProperties1 properties = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Ignore),
                                                                 g.DpiX, g.DpiY, BitmapOptions.Target | BitmapOptions.CannotDraw);

            Surface backBuffer2D = swapChain.GetBackBuffer <Surface>(0);

            //new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)
            SharpDX.Direct2D1.RenderTargetProperties rtp = new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Ignore));
            d2dRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, backBuffer2D, rtp);
            d2dTarget       = new Bitmap1(d2dContext, backBuffer2D, properties);

            d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView);
        }
예제 #48
0
        /// <summary>
        /// Function to clean up the render target view.
        /// </summary>
        protected override void OnCleanUp()
        {
            if (D3DView == null)
            {
                return;
            }

            Resource.Graphics.Output.Unbind(this);

            Gorgon.Log.Print("Destroying render target view for {0}.",
                             LoggingLevel.Verbose,
                             Resource.Name);
            D3DView.Dispose();
            D3DView = null;
        }
예제 #49
0
        private void RetrieveSetBuffers()
        {
            if (_swap == null)
            {
                throw new NullReferenceException("SwapChain is null");
            }

            var bb = _swap.GetBackBuffer <D3D11.Texture2D>(0);

            _rtv = new D3D11.RenderTargetView(_device, bb);
            bb.Dispose();

            _context.OutputMerger.SetRenderTargets(_rtv);
            _context.Rasterizer.SetViewport(0, 0, _width, _height);
        }
예제 #50
0
        public void Update(Device device, DeviceContext context, RenderTargetView renderTargetView)
        {
            bool isNewDevice = false;
            if (device != _device)
            {
                _device = device;
                CreateDeviceResources();
                isNewDevice = true;
            }
            _deviceContext.ClearState();

            _deviceContext = context;
            _renderTargetview = renderTargetView;

            CreateWindowSizeDependentResources(isNewDevice);
        }
예제 #51
0
        public bool Initialise(Device device, Texture2D renderTarget)
        {
            Debug.Assert(!_initialised);
            if (_initialising)
                return false;

            _initialising = true;
            
            try
            {

                _device = device;
                _renderTarget = renderTarget;
                try
                {
                    // TODO: determine if any benefit to using deferred context here
                    _deviceContext = new DeviceContext(_device);
                }
                catch (SharpDXException)
                {
                    _deviceContext = _device.ImmediateContext;
                }

                _renderTargetView = new RenderTargetView(_device, _renderTarget);

                if (DeferredContext)
                {
                    _deviceContext.Rasterizer.SetViewports(new ViewportF(0, 0, _renderTarget.Description.Width, _renderTarget.Description.Height, 0, 1));
                    _deviceContext.OutputMerger.SetTargets(_renderTargetView);
                }

                _spriteEngine = new DXSprite(_device, _deviceContext);
                if (!_spriteEngine.Initialize())
                    return false;

                // Initialise any resources required for overlay elements
                IntialiseElementResources();

                _initialised = true;
                return true;
            }
            finally
            {
                _initialising = false;
            }
        }
예제 #52
0
        public RenderTargetDirect2D CreateDirect2DRenderTarget(RenderTarget.Configuration.CreationSettings settings)
        {
            var description = new Texture2DDescription
                                  {
                                      Width = settings.Dimensions.Width,
                                      Height = settings.Dimensions.Height,
                                      MipLevels = 1,
                                      ArraySize = 1,
                                      Format = Format.B8G8R8A8_UNorm,
                                      SampleDescription = settings.SampleDescription,
                                      Usage = ResourceUsage.Default,
                                      BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                                      CpuAccessFlags = CpuAccessFlags.None,
                                      OptionFlags = ResourceOptionFlags.SharedKeyedmutex
                                  };

            var texture2D = new Texture2D(_deviceManager.Device, description);
            //using
            var resource = texture2D;
            {
                var sharedResource = resource.QueryInterface<SharpDX.DXGI.Resource>();
                var textureD3D10 = _deviceManager.Device10.OpenSharedResource<SharpDX.Direct3D10.Texture2D>(sharedResource.SharedHandle);

                var renderTargetProperties = new RenderTargetProperties
                              {
                                  MinLevel = FeatureLevel.Level_10,
                                  Type = RenderTargetType.Hardware,
                                  PixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)
                              };

                var surface = textureD3D10.AsSurface();
                var context = new SharpDX.Direct2D1.RenderTarget(_deviceManager.Direct2DFactory, surface, renderTargetProperties)
                                  {
                                      AntialiasMode = AntialiasMode.PerPrimitive,
                                      TextAntialiasMode = TextAntialiasMode.Cleartype
                                  };

                var renderTargetView11 = new RenderTargetView(_deviceManager.Device, resource);
                var renderTargetView10 = new SharpDX.Direct3D10.RenderTargetView(_deviceManager.Device10, textureD3D10);

                var renderTarget2D = new RenderTargetDirect2D(renderTargetView11, renderTargetView10, context, settings);
                _renderTargets.Add(renderTarget2D);

                return renderTarget2D;
            }
        }
예제 #53
0
        public DXImageSource(GraphicsDevice graphics, int width, int height)
        {
            if (width < 10) width = 10;
            if (height < 10) height = 10;

            _renderTarget = RenderTarget2D.New(graphics, width, height, SharpDX.Toolkit.Graphics.PixelFormat.B8G8R8A8.UNorm);
            _renderTargetView = new RenderTargetView(graphics, (SharpDX.Toolkit.Graphics.GraphicsResource)_renderTarget);

            SharpDX.Direct3D11.Texture2D depthBuffer = new SharpDX.Direct3D11.Texture2D(graphics, new Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize=1,
                MipLevels=0,
                Width = width,
                Height = height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1,0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil
            });

            //_depthStencilBuffer = DepthStencilBuffer.New(graphics,width,height,DepthFormat.Depth24Stencil8);
            _depthStencilView = new DepthStencilView(graphics, depthBuffer);

            Texture2DDescription description = new Texture2DDescription()
            {
                Width = width,
                Height = height,
                MipLevels = 1,
                ArraySize = 1,
                Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                BindFlags = BindFlags.None,
                CpuAccessFlags = CpuAccessFlags.Read,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count = 1,
                    Quality = 0
                },
                Usage = ResourceUsage.Staging,
                OptionFlags = ResourceOptionFlags.None
            };
            _stagingTexture = SharpDX.Toolkit.Graphics.Texture2D.New(graphics, description);

            _buffer = new byte[width * height * 4];
            _writeableBitmap = new WriteableBitmap(
                width, height, 96, 96, PixelFormats.Bgr32, null);
        }
예제 #54
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode)
                return;

            // create swap chain, rendertarget
            var swapChainDesc = new SwapChainDescription()
            {
                BufferCount = 1,
                Usage = Usage.RenderTargetOutput,
                OutputHandle = videoPanel1.Handle,
                IsWindowed = true,
                ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                Flags = SwapChainFlags.AllowModeSwitch,
                SwapEffect = SwapEffect.Discard,
                SampleDescription = new SampleDescription(1, 0),
            };

            swapChain = new SwapChain(factory, device, swapChainDesc);

            // render target
            renderTarget = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
            renderTargetView = new RenderTargetView(device, renderTarget);

            // depth buffer
            var depthBufferDesc = new Texture2DDescription()
            {
                Width = videoPanel1.Width,
                Height = videoPanel1.Height,
                MipLevels = 1,
                ArraySize = 1,
                Format = Format.D32_Float, // necessary?
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.DepthStencil,
                CpuAccessFlags = CpuAccessFlags.None
            };
            depthStencil = new Texture2D(device, depthBufferDesc);
            depthStencilView = new DepthStencilView(device, depthStencil);

            // viewport
            viewport = new Viewport(0, 0, videoPanel1.Width, videoPanel1.Height, 0f, 1f);
        }
예제 #55
0
        protected override void OnSurfaceInvalidated(object sender, EventArgs e) {
            var swapChain = D3DApp11.I.SwapChain;
            var clientSize = D3DApp11.I.ControlWindow.ClientSize;
            var device = D3DApp11.I.D3DDevice;
            var immediateContext = D3DApp11.I.ImmediateContext;

            // Dispose all previous allocated resources
            Utilities.Dispose(ref _backBuffer);
            Utilities.Dispose(ref _renderView);
            Utilities.Dispose(ref _depthBuffer);
            Utilities.Dispose(ref _depthView);

            // Resize the backbuffer
            swapChain.ResizeBuffers(_swapChainDescription.BufferCount, clientSize.Width, clientSize.Height, Format.Unknown, SwapChainFlags.None);
            // Get the backbuffer from the swapchain
            _backBuffer = Resource.FromSwapChain<Texture2D>(swapChain, 0);

            // Renderview on the backbuffer
            _renderView = new RenderTargetView(device, _backBuffer);

            // Create the depth buffer
            _depthBuffer = new Texture2D(device, new Texture2DDescription() {
                Format = Format.D32_Float_S8X24_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = clientSize.Width,
                Height = 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);

            // Setup targets and viewport for rendering
            _viewport = new Viewport(0, 0, clientSize.Width, clientSize.Height, 0.0f, 1.0f);
            immediateContext.Rasterizer.SetViewport(_viewport);
            immediateContext.OutputMerger.SetTargets(_depthView, _renderView);

            base.OnSurfaceInvalidated(sender, e);
        }
예제 #56
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="device">Device</param>
        /// <param name="size">Cube Size</param>
        /// <param name="format">Color Format</param>
        public SharpCubeTarget(SharpDevice device, int size, Format format)
        {
            Device = device;
            Size = size;

            Texture2D target = new Texture2D(device.Device, new Texture2DDescription()
            {
                Format = format,
                Width = size,
                Height = size,
                ArraySize = 6,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.TextureCube,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
            });

            _target = new RenderTargetView(device.Device, target);

            _resource = new ShaderResourceView(device.Device, target);

            ComObject.Dispose(ref target);

            var _zbufferTexture = new Texture2D(Device.Device, new Texture2DDescription()
            {
                Format = Format.D16_UNorm,
                ArraySize = 6,
                MipLevels = 1,
                Width = size,
                Height = size,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.DepthStencil,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.TextureCube
            });

            // Create the depth buffer view
            _zbuffer = new DepthStencilView(Device.Device, _zbufferTexture);
            ComObject.Dispose(ref _zbufferTexture);
        }
        private void GenerateIfRequired()
        {
            if (_renderTargetView != null)
                return;

            // Create a view interface on the rendertarget to use on bind.
            _renderTargetView = new RenderTargetView(GraphicsDevice._d3dDevice, GetTexture());

            // If we don't need a depth buffer then we're done.
            if (DepthStencilFormat == DepthFormat.None)
                return;

            // Setup the multisampling description.
            var multisampleDesc = new SharpDX.DXGI.SampleDescription(1, 0);
            if (MultiSampleCount > 1)
            {
                multisampleDesc.Count = MultiSampleCount;
                multisampleDesc.Quality = (int)StandardMultisampleQualityLevels.StandardMultisamplePattern;
            }

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D surface as the depth/stencil buffer.
            // Create a DepthStencil view on this surface to use on bind.
            using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(GraphicsDevice._d3dDevice, new Texture2DDescription
            {
                Format = SharpDXHelper.ToFormat(DepthStencilFormat),
                ArraySize = 1,
                MipLevels = 1,
                Width = width,
                Height = height,
                SampleDescription = multisampleDesc,
                BindFlags = BindFlags.DepthStencil,
            }))
            {
                // Create the view for binding to the device.
                _depthStencilView = new DepthStencilView(GraphicsDevice._d3dDevice, depthBuffer,
                    new DepthStencilViewDescription()
                    {
                        Format = SharpDXHelper.ToFormat(DepthStencilFormat),
                        Dimension = DepthStencilViewDimension.Texture2D
                    });
            }
        }
예제 #58
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="device">Device</param>
        /// <param name="width">Width</param>
        /// <param name="height">Height</param>
        /// <param name="format">Format</param>
        public SharpRenderTarget(SharpDevice device, int width, int height, Format format)
        {
            Device = device;
            Height = height;
            Width = width;

            Texture2D target = new Texture2D(device.Device, new Texture2DDescription()
            {
                Format = format,
                Width = width,
                Height = height,
                ArraySize = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
            });

            _target = new RenderTargetView(device.Device, target);
            _resource = new ShaderResourceView(device.Device, target);
             target.Dispose();

            var _zbufferTexture = new Texture2D(Device.Device, new Texture2DDescription()
            {
                Format = Format.D16_UNorm,
                ArraySize = 1,
                MipLevels = 1,
                Width = width,
                Height = height,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.DepthStencil,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None
            });

            // Create the depth buffer view
            _zbuffer = new DepthStencilView(Device.Device, _zbufferTexture);
            _zbufferTexture.Dispose();
        }
 public virtual void Render(DeviceContext deviceContext, ShaderResourceView inputRV, RenderTargetView renderTargetView)
 {
     deviceContext.InputAssembler.SetVertexBuffers(0, vertexBufferBinding);
     deviceContext.InputAssembler.InputLayout = null;
     deviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleStrip;
     deviceContext.OutputMerger.SetTargets(renderTargetView);
     deviceContext.Rasterizer.State = rasterizerState;
     deviceContext.Rasterizer.SetViewport(viewport);
     deviceContext.VertexShader.SetShaderResource(0, null); // TODO: this should be done by the depthAndColorVS
     deviceContext.VertexShader.Set(vertexShader);
     deviceContext.GeometryShader.Set(null);
     deviceContext.PixelShader.Set(pixelShader);
     deviceContext.PixelShader.SetShaderResource(0, inputRV);
     if (constantBuffer != null)
         deviceContext.PixelShader.SetConstantBuffer(0, constantBuffer);
     deviceContext.Draw(4, 0);
     RenderTargetView nullRTV = null;
     deviceContext.OutputMerger.SetTargets(nullRTV);
     deviceContext.PixelShader.SetShaderResource(0, null);
 }
예제 #60
0
 public void OnDeviceInit()
 {
     {
         ShaderResourceViewDescription desc = new ShaderResourceViewDescription();
         desc.Format = m_format;
         desc.Dimension = ShaderResourceViewDimension.Texture2D;
         desc.Texture2D.MipLevels = 1;
         desc.Texture2D.MostDetailedMip = 0;
         m_srv = new ShaderResourceView(MyRender11.Device, m_owner.Resource, desc);
         m_srv.DebugName = m_owner.Name;
     }
     {
         RenderTargetViewDescription desc = new RenderTargetViewDescription();
         desc.Format = m_format;
         desc.Dimension = RenderTargetViewDimension.Texture2D;
         desc.Texture2D.MipSlice = 0;
         m_rtv = new RenderTargetView(MyRender11.Device, m_owner.Resource, desc);
         m_rtv.DebugName = m_owner.Name;
     }
 }