Exemplo n.º 1
0
        /// <summary>
        /// Creates the depth stencil.
        /// </summary>
        /// <returns>D3D11.DepthStencilView.</returns>
        /// <exception cref="System.Exception"></exception>
        private D3D11.DepthStencilView CreateDepthStencil()
        {
            if (swapChain == null || d3dDevice == null)
            {
                throw new Exception(MethodBase.GetCurrentMethod().Name + "Device or SwapChain is null");
            }

            var DepthStencilTextureDesc = new D3D11.Texture2DDescription
            {
                Format            = DXGI.Format.D16_UNorm,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = swapChain.Description.SampleDescription,
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            using (D3D11.Texture2D backBuffer = new D3D11.Texture2D(d3dDevice, DepthStencilTextureDesc))
            {
                return(new D3D11.DepthStencilView(d3dDevice, backBuffer));
            }
        }
Exemplo n.º 2
0
        public SoftwareRasterizerCore(Renderer renderer)
        {
            rasterizerMode = RasterizerMode.Both;
            outputMode     = OutputMode.Color;

            this.renderer = renderer;

            D3D11.Texture2DDescription texture2DDescription = new D3D11.Texture2DDescription
            {
                CpuAccessFlags    = D3D11.CpuAccessFlags.Write,
                BindFlags         = D3D11.BindFlags.None,
                Format            = DXGI.Format.R8G8B8A8_UNorm,
                Width             = renderer.width,
                Height            = renderer.height,
                OptionFlags       = D3D11.ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = D3D11.ResourceUsage.Staging
            };

            backbufferBitmap = new Bitmap(renderer.width, renderer.height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            depthbufferBitmap = new Bitmap(renderer.width, renderer.height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            screenTexture = new D3D11.Texture2D(renderer.device, texture2DDescription);
        }
Exemplo n.º 3
0
        public D3D11.Texture2D GenerateCubemap(D3D11.Device device, Shader shader, int size, D3D11.Texture2D texture, int mipLevels, int mip)
        {
            Format format = Format.B8G8R8A8_UNorm;

            D3D11.Texture2DDescription renderTextureDesc = GetTexture2DDescription(size, format, mipLevels);

            D3D11.SamplerState sampler = GetSamplerState(device);

            D3D11.Texture2D RenderTexture = new D3D11.Texture2D(device, renderTextureDesc);

            D3D11.RenderTargetView[] skyRTVs = CreateRTVArray(device, format, RenderTexture, mip);

            perFaceBuffer = new D3D11.Buffer(device, SharpDX.Utilities.SizeOf <KDX_SKYBOX_ENV_RENDERER_BUFFER>(),
                                             D3D11.ResourceUsage.Default,
                                             D3D11.BindFlags.ConstantBuffer,
                                             D3D11.CpuAccessFlags.None,
                                             D3D11.ResourceOptionFlags.None, 0);


            //D3D11.DeviceContext deviceContext = device.ImmediateContext;

            SetupContext(device, size, shader, sampler, texture);
            Render(device, skyRTVs);


            return(RenderTexture);
        }
        /// <summary>
        /// Creates a staging texture which enables copying data from gpu to cpu memory.
        /// </summary>
        /// <param name="device">Graphics device.</param>
        /// <param name="size">The size of generated texture.</param>
        public D3D11.Texture2D CreateStagingTexture(D3D11.Device device, GDI.Size size)
        {
            CheckTexture(ref _copyHelperTextureStaging, size);

            if (_copyHelperTextureStaging != null)
            {
                return(_copyHelperTextureStaging);
            }

            //For handling of staging resource see
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ff476259(v=vs.85).aspx

            D3D11.Texture2DDescription textureDescription = new D3D11.Texture2DDescription();
            textureDescription.Width             = size.Width;
            textureDescription.Height            = size.Height;
            textureDescription.MipLevels         = 1;
            textureDescription.ArraySize         = 1;
            textureDescription.Format            = DXGI.Format.B8G8R8A8_UNorm;
            textureDescription.Usage             = D3D11.ResourceUsage.Staging;
            textureDescription.SampleDescription = new DXGI.SampleDescription(1, 0);
            textureDescription.BindFlags         = D3D11.BindFlags.None;
            textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.Read;
            textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;

            return(new D3D11.Texture2D(device, textureDescription));
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        private void InitializeBackBuffer(D2D.DeviceContext deviceContext, SharpDX.Size2F size)
        {
            this.backBitmap?.Dispose();

            Size2 pixelSize = Helpers.GetPixelSize(size, this.Factory.DesktopDpi);

            var p = new D2D.BitmapProperties1(
                new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
                this.Factory.DesktopDpi.Width,
                this.Factory.DesktopDpi.Height,
                D2D.BitmapOptions.Target);

            var desc = new D3D11.Texture2DDescription()
            {
                ArraySize         = 1,
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                MipLevels         = 1,
                OptionFlags       = D3D11.ResourceOptionFlags.Shared,
                Usage             = D3D11.ResourceUsage.Default,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Width             = pixelSize.Width,
                Height            = pixelSize.Height,
            };

            using (var buffer = new D3D11.Texture2D(this.Device, desc))
                using (var surface = buffer.QueryInterface <DXGI.Surface>())
                {
                    this.backBitmap = new D2D.Bitmap1(this.DeviceContext, surface, p);
                }

            this.DeviceContext.Target = this.backBitmap;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Function to initialize the texture with optional initial data.
        /// </summary>
        /// <param name="initialData">Data used to populate the image.</param>
        protected override void OnInitialize(GorgonImageData initialData)
        {
            var desc = new D3D.Texture2DDescription
            {
                ArraySize         = Settings.ArrayCount,
                Format            = (GI.Format)Settings.Format,
                Width             = Settings.Width,
                Height            = Settings.Height,
                MipLevels         = Settings.MipCount,
                BindFlags         = GetBindFlags(false, false),
                Usage             = (D3D.ResourceUsage)Settings.Usage,
                OptionFlags       = Settings.IsTextureCube ? D3D.ResourceOptionFlags.TextureCube : D3D.ResourceOptionFlags.None,
                SampleDescription = GorgonMultisampling.Convert(Settings.Multisampling)
            };

            switch (Settings.Usage)
            {
            case BufferUsage.Staging:
                desc.CpuAccessFlags = D3D.CpuAccessFlags.Read | D3D.CpuAccessFlags.Write;
                break;

            case BufferUsage.Dynamic:
                desc.CpuAccessFlags = D3D.CpuAccessFlags.Write;
                break;

            default:
                desc.CpuAccessFlags = D3D.CpuAccessFlags.None;
                break;
            }

            D3DResource = initialData != null
                                                          ? new D3D.Texture2D(Graphics.D3DDevice, desc, initialData.Buffers.DataBoxes)
                                      : new D3D.Texture2D(Graphics.D3DDevice, desc);
        }
        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);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Function to initialize the texture from a swap chain.
        /// </summary>
        /// <param name="swapChain">The swap chain used to initialize the texture.</param>
        internal void InitializeSwapChain(GorgonSwapChain swapChain)
        {
            if (D3DResource != null)
            {
                CleanUpResource();
            }

            D3DResource = D3D.Resource.FromSwapChain <D3D.Texture2D>(swapChain.GISwapChain, 0);
            D3D.Texture2DDescription desc = ((D3D.Texture2D)D3DResource).Description;

            Settings.Width            = desc.Width;
            Settings.Height           = desc.Height;
            Settings.ArrayCount       = desc.ArraySize;
            Settings.Format           = (BufferFormat)desc.Format;
            Settings.MipCount         = desc.MipLevels;
            Settings.ShaderViewFormat = (swapChain.Settings.Flags & SwapChainUsageFlags.AllowShaderView) ==
                                        SwapChainUsageFlags.AllowShaderView
                                                            ? swapChain.Settings.Format
                                                            : BufferFormat.Unknown;
            Settings.AllowUnorderedAccessViews = (desc.BindFlags & D3D.BindFlags.UnorderedAccess) == D3D.BindFlags.UnorderedAccess;
            Settings.Multisampling             = new GorgonMultisampling(desc.SampleDescription.Count, desc.SampleDescription.Quality);
            Settings.IsTextureCube             = (desc.OptionFlags & D3D.ResourceOptionFlags.TextureCube) ==
                                                 D3D.ResourceOptionFlags.TextureCube;
            Settings.DepthStencilFormat = swapChain.Settings.DepthStencilFormat;
            Settings.TextureFormat      = swapChain.Settings.Format;

            _swapChain = swapChain;

#if DEBUG
            Graphics.Output.ValidateRenderTargetSettings(Settings);
#endif

            GorgonRenderStatistics.TextureCount++;
            GorgonRenderStatistics.TextureSize += SizeInBytes;
            GorgonRenderStatistics.RenderTargetCount++;
            GorgonRenderStatistics.RenderTargetSize += SizeInBytes * swapChain.Settings.BufferCount;

            // Set default viewport.
            Viewport = new GorgonViewport(0, 0, Settings.Width, Settings.Height, 0.0f, 1.0f);

            // Re-initialize any released resource views.
            InitializeResourceViews();

            CreateDepthStencilBuffer();

            if (DepthStencilBuffer != null)
            {
                DepthStencilBuffer.SwapChain = swapChain;
            }

            // Create the default render target view.
            _defaultRenderTargetView = GetRenderTargetView(Settings.Format, 0, 0, 1);

            if ((swapChain.Settings.Flags & SwapChainUsageFlags.AllowShaderView) == SwapChainUsageFlags.AllowShaderView)
            {
                CreateDefaultResourceView();
            }
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        private void CreateResources()
        {
            this.backBuffer           = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0);
            this.backBuffer.DebugName = "BackBuffer";

            using (var surface = this.backBuffer.QueryInterface <DXGI.Surface>())
            {
                var properties = new D2D.BitmapProperties(
                    new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
                    this.factory2d.DesktopDpi.Width,
                    this.factory2d.DesktopDpi.Height);

                this.backBitmap = new D2D.Bitmap(this.deviceContext2D, surface, properties);

                if (this.renderBitmap != null)
                {
                    this.backBitmap.CopyFromBitmap(this.renderBitmap);
                }
            }

            if (this.renderBitmap != null)
            {
                this.renderBitmap.Dispose();
                this.renderBuffer.Dispose();
            }

            var p = new D2D.BitmapProperties1(
                new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
                this.factory2d.DesktopDpi.Width,
                this.factory2d.DesktopDpi.Height,
                D2D.BitmapOptions.Target);

            var pixelSize = Helpers.GetPixelSize(this.backBitmap.Size, this.factory2d.DesktopDpi);

            var desc = new D3D11.Texture2DDescription()
            {
                ArraySize         = 1,
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                MipLevels         = 1,
                OptionFlags       = D3D11.ResourceOptionFlags.Shared,
                Usage             = D3D11.ResourceUsage.Default,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Width             = pixelSize.Width,
                Height            = pixelSize.Height,
            };

            this.renderBuffer = new D3D11.Texture2D(this.device, desc);
            using (var surface = this.renderBuffer.QueryInterface <DXGI.Surface>())
            {
                this.renderBitmap = new D2D.Bitmap1(this.deviceContext2D, surface, p);
            }

            this.renderBitmap.CopyFromBitmap(this.backBitmap);
        }
Exemplo n.º 12
0
        public void ConstructRenderAndResource(double width, double height)
        {
            float dpiX, dpiY;

            this.GetDpi(out dpiX, out dpiY);
            D2D.RenderTargetProperties prop = new D2D.RenderTargetProperties(
                D2D.RenderTargetType.Default,
                new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
                dpiX,
                dpiY,
                D2D.RenderTargetUsage.None,
                D2D.FeatureLevel.Level_DEFAULT);

            D3D11.Texture2DDescription desc = new D3D11.Texture2DDescription();
            desc.Width             = (int)width;
            desc.Height            = (int)height;
            desc.MipLevels         = 1;
            desc.ArraySize         = 1;
            desc.Format            = DXGI.Format.B8G8R8A8_UNorm;
            desc.SampleDescription = new DXGI.SampleDescription(1, 0);
            desc.Usage             = D3D11.ResourceUsage.Default;
            desc.BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource;
            desc.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
            desc.OptionFlags       = D3D11.ResourceOptionFlags.Shared;
            this.d3d11Texture      = new D3D11.Texture2D(this.device, desc);

            this.surface = this.d3d11Texture.QueryInterface <DXGI.Surface>();

            DXGI.Resource resource = this.d3d11Texture.QueryInterface <DXGI.Resource>();
            IntPtr        handel   = resource.SharedHandle;

            D3D9.Texture texture = new D3D9.Texture(
                this.device9,
                this.d3d11Texture.Description.Width,
                this.d3d11Texture.Description.Height,
                1,
                D3D9.Usage.RenderTarget,
                D3D9.Format.A8R8G8B8,
                D3D9.Pool.Default,
                ref handel);
            this.surface9 = texture.GetSurfaceLevel(0);
            resource.Dispose();
            texture.Dispose();

            D2D.BitmapProperties bmpProp = new D2D.BitmapProperties();
            bmpProp.DpiX        = dpiX;
            bmpProp.DpiY        = dpiY;
            bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied);
            this.bmpd2d         = new D2D.Bitmap(this.render, this.surface, bmpProp);
            this.cachedBitMap   = new D2D.Bitmap(this.render, new Size2((int)width, (int)height), bmpProp);
            this.hasCache       = false;

            this.render.Target = this.bmpd2d;

            this.renderSize = new Size(width, height);
        }
            //---------------------------------------------------------------------------------------------------------
            /// <summary>
            /// Создание текстуры Direct3D11 для ренденинга
            /// </summary>
            //---------------------------------------------------------------------------------------------------------
            private void CreateAndBindTargets()
            {
                mD3DSurface.SetRenderTarget(null);

                XDisposer.SafeDispose(ref mD2DRenderTarget);
                XDisposer.SafeDispose(ref mD3DRenderTarget);

                XDisposer.SafeDispose(ref XDirect2DManager.mD2DDevice);
                XDisposer.SafeDispose(ref XDirect2DManager.mD2DFactory);
                XDisposer.SafeDispose(ref XDirect2DManager.mD2DWriteFactory);
                XDisposer.SafeDispose(ref XDirect2DManager.mD2DImagingFactory);

                var width  = (Int32)Math.Max(ActualWidth, mWidthRenderTargetDip);
                var height = (Int32)Math.Max(ActualHeight, mHeightRenderTargetDip);

                var colordesc = new Direct3D11.Texture2DDescription
                {
                    BindFlags         = Direct3D11.BindFlags.RenderTarget | Direct3D11.BindFlags.ShaderResource,
                    Format            = DXGI.Format.B8G8R8A8_UNorm,
                    Width             = width,
                    Height            = height,
                    MipLevels         = 1,
                    SampleDescription = new DXGI.SampleDescription(1, 0),
                    Usage             = Direct3D11.ResourceUsage.Default,
                    OptionFlags       = Direct3D11.ResourceOptionFlags.Shared,
                    CpuAccessFlags    = Direct3D11.CpuAccessFlags.None,
                    ArraySize         = 1
                };

                // Создаем текстуру Direct3D11
                mD3DRenderTarget = new Direct3D11.Texture2D(mD3DDevice, colordesc);
                var surface = mD3DRenderTarget.QueryInterface <DXGI.Surface>();

                // Фабрика ресурсов DirectD2
                XDirect2DManager.mD2DFactory = new Direct2D.Factory();
                //XNativeD2DFactory.mD2DDevice = new Direct2D.Device(XNativeD2DFactory.mD2DFactory, XNativeD2DFactory.mDXGIDevice);
                XDirect2DManager.mD2DWriteFactory   = new DirectWrite.Factory(DirectWrite.FactoryType.Shared);
                XDirect2DManager.mD2DImagingFactory = new ImagingFactory();

                // Создаем поверхность отображения DirectD2
                var render_target_properties = new Direct2D.RenderTargetProperties(new Direct2D.PixelFormat(DXGI.Format.Unknown, Direct2D.AlphaMode.Premultiplied));

                render_target_properties.Type  = Direct2D.RenderTargetType.Default;
                render_target_properties.Usage = Direct2D.RenderTargetUsage.None;
                mD2DRenderTarget = new Direct2D.RenderTarget(XDirect2DManager.mD2DFactory, surface, render_target_properties);

                mD3DSurface.SetRenderTarget(mD3DRenderTarget);

                // Определяем область отображения
                mD3DDevice.ImmediateContext.Rasterizer.SetViewport(0, 0, width, height, 0.0f, 1.0f);

                // Сохраняем
                XDirect2DManager.mD2DRenderTarget = mD2DRenderTarget;
            }
Exemplo n.º 14
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);
            }
        }
Exemplo n.º 15
0
        public void CreateRenderTarget(
            D3D11.Device device,
            D3D11.Texture2DDescription textureDesc)
        {
            renderTarget     = new D3D11.Texture2D(device, textureDesc);
            renderTargetSwap = new D3D11.Texture2D(device, textureDesc);

            defaultRenderViews.RTV     = new D3D11.RenderTargetView(device, renderTarget);
            defaultRenderViews.RT_SRVT = new D3D11.ShaderResourceView(device, renderTarget);

            swapRenderViews.RTV     = new D3D11.RenderTargetView(device, renderTarget);
            swapRenderViews.RT_SRVT = new D3D11.ShaderResourceView(device, renderTarget);
        }
Exemplo n.º 16
0
        public void CreateDepthStencil(
            D3D11.Device device,
            D3D11.Texture2DDescription textureDesc,
            D3D11.DepthStencilViewDescription viewDesc,
            D3D11.ShaderResourceViewDescription resourceViewDesc)
        {
            depthStencil     = new D3D11.Texture2D(device, textureDesc);
            depthStencilSwap = new D3D11.Texture2D(device, textureDesc);


            defaultRenderViews.DSV = new D3D11.DepthStencilView(device, depthStencil, viewDesc);
            swapRenderViews.DSV    = new D3D11.DepthStencilView(device, depthStencil, viewDesc);

            defaultRenderViews.DS_SRVT = new D3D11.ShaderResourceView(device, depthStencil, resourceViewDesc);
            swapRenderViews.DS_SRVT    = new D3D11.ShaderResourceView(device, depthStencil, resourceViewDesc);
        }
Exemplo n.º 17
0
        // https://gamedev.stackexchange.com/questions/75461/how-do-i-set-up-a-depth-buffer-in-sharpdx
        // https://docs.microsoft.com/en-us/windows/desktop/direct3d11/d3d10-graphics-programming-guide-depth-stencil
        private void EnableDepthTest()
        {
            if (depthStencilView != null)
            {
                depthStencilView.Dispose();
            }

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

            // Create the depth stencil description
            depthTextureDesc = new D3D11.Texture2DDescription
            {
                Format            = Format.D16_UNorm,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = this.Width,
                Height            = this.Height,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            // Create the depth stencil view
            using (var depthTex = new D3D11.Texture2D(d3dDevice, depthTextureDesc))
            {
                depthStencilView = new D3D11.DepthStencilView(d3dDevice, depthTex);
            }

            // Create the depth stencil state description
            depthStencilStateDesc = new D3D11.DepthStencilStateDescription();
            depthStencilStateDesc.IsDepthEnabled   = true;
            depthStencilStateDesc.DepthWriteMask   = D3D11.DepthWriteMask.All;
            depthStencilStateDesc.DepthComparison  = D3D11.Comparison.Less;
            depthStencilStateDesc.IsStencilEnabled = false;

            // Create the depth stencil state
            depthStencilState = new D3D11.DepthStencilState(d3dDevice, depthStencilStateDesc);

            // Update the context
            d3dDeviceContext.OutputMerger.SetTargets(depthStencilView, renderTargetView);
            d3dDeviceContext.OutputMerger.SetDepthStencilState(depthStencilState);
        }
Exemplo n.º 18
0
        public override void OnResize()
        {
            base.OnResize();
            //Since reflection and refraction quality depends on Screen Size, the maps are needed to be recreated on a resize.

            Util.ReleaseCom(ref reflectRenderTargetView);
            Util.ReleaseCom(ref reflectResourceView);
            Util.ReleaseCom(ref reflectText);

            Util.ReleaseCom(ref refractRenderTargetView);
            Util.ReleaseCom(ref refractResourceView);
            Util.ReleaseCom(ref refractText);

            Util.ReleaseCom(ref positionMapRenderTargetView);
            Util.ReleaseCom(ref positionMapResourceView);
            Util.ReleaseCom(ref positionMapText);

            // Recalculate perspective matrix
            _proj = Matrix.PerspectiveFovLH(0.25f * MathF.PI, AspectRatio, 1.0f, 1000.0f);

            D3D11.Texture2DDescription tdesc = new D3D11.Texture2DDescription();
            tdesc.Format    = DXGI.Format.R32G32B32A32_Float;
            tdesc.MipLevels = 1;
            tdesc.Width     = ClientWidth;
            tdesc.Height    = ClientHeight;
            tdesc.ArraySize = 1;
            tdesc.SampleDescription.Count = 1;
            tdesc.Usage          = D3D11.ResourceUsage.Default;
            tdesc.BindFlags      = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource;
            tdesc.CpuAccessFlags = D3D11.CpuAccessFlags.None;

            //Recreate refraction map
            refractText             = new D3D11.Texture2D(Device, tdesc);
            refractRenderTargetView = new D3D11.RenderTargetView(Device, refractText);
            refractResourceView     = new D3D11.ShaderResourceView(Device, refractText);

            //recreate reflection map
            reflectText             = new D3D11.Texture2D(Device, tdesc);
            reflectRenderTargetView = new D3D11.RenderTargetView(Device, reflectText);
            reflectResourceView     = new D3D11.ShaderResourceView(Device, reflectText);

            //recreate position map
            positionMapText             = new D3D11.Texture2D(Device, tdesc);
            positionMapRenderTargetView = new D3D11.RenderTargetView(Device, positionMapText);
            positionMapResourceView     = new D3D11.ShaderResourceView(Device, positionMapText);
        }
Exemplo n.º 19
0
        protected override SharpDX.Direct3D11.Texture2D CreateRenderTarget(int width, int height)
        {
            var desc = new SharpDX.Direct3D11.Texture2DDescription {
                ArraySize         = 1,
                BindFlags         = SharpDX.Direct3D11.BindFlags.RenderTarget,
                CpuAccessFlags    = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format            = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                Height            = height,
                Width             = width,
                MipLevels         = 1,
                OptionFlags       = SharpDX.Direct3D11.ResourceOptionFlags.Shared,
                Usage             = SharpDX.Direct3D11.ResourceUsage.Default,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0)
            };

            return(new SharpDX.Direct3D11.Texture2D(Device, desc));
        }
Exemplo n.º 20
0
        public void Resize(int width, int height)
        {
            ResolutionX = width; ResolutionY = height;
            MainCamera.renderTargetView?.Dispose();
            MainCamera.depthStencilView?.Dispose();
            D2DTarget?.Dispose();
            D2DContext?.Dispose();

            MainCamera.AspectRatio = width / (float)height;

            swapChain.ResizeBuffers(swapChain.Description.BufferCount, width, height, DXGI.Format.Unknown, DXGI.SwapChainFlags.None);

            D2DContext = new D2D1.DeviceContext(D2DDevice, D2D1.DeviceContextOptions.None);
            using (DXGI.Surface surface = swapChain.GetBackBuffer <DXGI.Surface>(0))
                D2DTarget = new D2D1.Bitmap1(D2DContext, surface,
                                             new D2D1.BitmapProperties1(new D2D1.PixelFormat(DXGI.Format.R8G8B8A8_UNorm, D2D1.AlphaMode.Premultiplied),
                                                                        D2DFactory.DesktopDpi.Height, D2DFactory.DesktopDpi.Width, D2D1.BitmapOptions.CannotDraw | D2D1.BitmapOptions.Target)
                                             );
            D2DContext.Target = D2DTarget;

            // render target
            using (D3D11.Texture2D backBuffer = swapChain.GetBackBuffer <D3D11.Texture2D>(0))
                MainCamera.renderTargetView = new D3D11.RenderTargetView(Device, backBuffer);

            // depth buffer
            D3D11.Texture2DDescription depthDescription = new D3D11.Texture2DDescription()
            {
                Format            = DXGI.Format.D32_Float,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new DXGI.SampleDescription(SampleCount, SampleQuality),
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };
            using (D3D11.Texture2D depthTexture = new D3D11.Texture2D(Device, depthDescription))
                MainCamera.depthStencilView = new D3D11.DepthStencilView(Device, depthTexture);

            // viewport
            Viewport = new Viewport(0, 0, width, height);
            Context.Rasterizer.SetViewport(Viewport);
        }
Exemplo n.º 21
0
        internal Texture2D AllocateTextureReturnSurface(Device device, Size2F drawingSize)
        {
            var desc = new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm, //  D24_UNorm_S8_UInt,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = (int)drawingSize.Width,
                Height            = (int)drawingSize.Height,
                Usage             = ResourceUsage.Default,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags         = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource,
            };

            desc.Usage = ResourceUsage.Default;
            var tex2D = new Texture2D(device, desc);

            return(tex2D);
        }
        /// <summary>
        /// Creates a render target texture with the given width and height.
        /// </summary>
        /// <param name="device">Graphics device.</param>
        /// <param name="width">Width of generated texture.</param>
        /// <param name="height">Height of generated texture.</param>
        public D3D11.Texture2D CreateRenderTargetTexture(D3D11.Device device, int width, int height)
        {
            var textureDescription = new D3D11.Texture2DDescription();

            if (TryEnableAntialiasing)
            {
                var maxCount = 1;
                for (var i = 0; i < 32; i++)
                {
                    if (device.CheckMultisampleQualityLevels(DXGI.Format.B8G8R8A8_UNorm, i) > 0)
                    {
                        maxCount = i;
                    }
                }

                textureDescription.Width             = width;
                textureDescription.Height            = height;
                textureDescription.MipLevels         = 1;
                textureDescription.ArraySize         = 1;
                textureDescription.Format            = DXGI.Format.B8G8R8A8_UNorm;
                textureDescription.Usage             = D3D11.ResourceUsage.Default;
                textureDescription.SampleDescription = new DXGI.SampleDescription(maxCount, 0);
                textureDescription.BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
            }
            else
            {
                textureDescription.Width             = width;
                textureDescription.Height            = height;
                textureDescription.MipLevels         = 1;
                textureDescription.ArraySize         = 1;
                textureDescription.Format            = DXGI.Format.B8G8R8A8_UNorm;
                textureDescription.Usage             = D3D11.ResourceUsage.Default;
                textureDescription.SampleDescription = new DXGI.SampleDescription(1, 0);
                textureDescription.BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
            }

            return(new D3D11.Texture2D(device, textureDescription));
        }
Exemplo n.º 23
0
        public IRenderTarget CreateRenderTarget()
        {
            var width  = m_Window.ClientRectangle.Width;
            var height = m_Window.ClientRectangle.Height;

            var texDesc = new D3D11.Texture2DDescription {
                ArraySize         = 1,
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(m_NumSamples, 0),
                Width             = width,
                Height            = height,
            };

            var texture      = new D3D11.Texture2D(Device, texDesc);
            var renderTarget = new D3D11.RenderTargetView(Device, texture);

            return(new SharpDXRenderTarget(this, texture, width, height, renderTarget));
        }
Exemplo n.º 24
0
        public static D3D.Texture2D CreateDepthStencilBuffer(this D3D.Device device, int width, int height, DXGI.SampleDescription sampleDescription, out D3D.DepthStencilView view)
        {
            var desc = new D3D.Texture2DDescription
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = DXGI.Format.D24_UNorm_S8_UInt,
                SampleDescription = sampleDescription,
                Usage             = D3D.ResourceUsage.Default,
                BindFlags         = D3D.BindFlags.DepthStencil,
                CpuAccessFlags    = D3D.CpuAccessFlags.None,
                OptionFlags       = D3D.ResourceOptionFlags.None,
            };
            var depthStencilBuffer = new D3D.Texture2D(device, desc);

            view = new D3D.DepthStencilView(device, depthStencilBuffer);
            return(depthStencilBuffer);
        }
        /// <summary>
        /// Creates a render target texture with the given width and height.
        /// </summary>
        /// <param name="device">Graphics device.</param>
        /// <param name="width">Width of generated texture.</param>
        /// <param name="height">Height of generated texture.</param>
        public D3D11.Texture2D CreateRenderTargetTexture(D3D11.Device device, int width, int height)
        {
            var textureDescription = new D3D11.Texture2DDescription();

            if (TryEnableAntialiasing)
            {
                var maxCount = 1;
                for (var i = 0; i < 32; i++)
                {
                    if (device.CheckMultisampleQualityLevels(DXGI.Format.B8G8R8A8_UNorm, i) > 0)
                        maxCount = i;
                }

                textureDescription.Width = width;
                textureDescription.Height = height;
                textureDescription.MipLevels = 1;
                textureDescription.ArraySize = 1;
                textureDescription.Format = DXGI.Format.B8G8R8A8_UNorm;
                textureDescription.Usage = D3D11.ResourceUsage.Default;
                textureDescription.SampleDescription = new DXGI.SampleDescription(maxCount, 0);
                textureDescription.BindFlags = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.None;
                textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None;
            }
            else
            {
                textureDescription.Width = width;
                textureDescription.Height = height;
                textureDescription.MipLevels = 1;
                textureDescription.ArraySize = 1;
                textureDescription.Format = DXGI.Format.B8G8R8A8_UNorm;
                textureDescription.Usage = D3D11.ResourceUsage.Default;
                textureDescription.SampleDescription = new DXGI.SampleDescription(1, 0);
                textureDescription.BindFlags = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.None;
                textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None;
            }

            return new D3D11.Texture2D(device, textureDescription);
        }
Exemplo n.º 26
0
        /// <summary>
        /// Function to initialize the texture with optional initial data.
        /// </summary>
        /// <param name="initialData">Data used to populate the image.</param>
        protected override void OnInitialize(GorgonImageData initialData)
        {
            if ((Settings.Format != BufferFormat.Unknown) && (Settings.TextureFormat == BufferFormat.Unknown))
            {
                Settings.TextureFormat = Settings.Format;
            }

            var desc = new D3D.Texture2DDescription
            {
                ArraySize         = Settings.ArrayCount,
                Format            = (GI.Format)Settings.TextureFormat,
                Width             = Settings.Width,
                Height            = Settings.Height,
                MipLevels         = Settings.MipCount,
                BindFlags         = GetBindFlags(false, true),
                Usage             = D3D.ResourceUsage.Default,
                CpuAccessFlags    = D3D.CpuAccessFlags.None,
                OptionFlags       = D3D.ResourceOptionFlags.None,
                SampleDescription = GorgonMultisampling.Convert(Settings.Multisampling)
            };

            Gorgon.Log.Print("{0} {1}: Creating 2D render target texture...", LoggingLevel.Verbose, GetType().Name, Name);

            // Create the texture.
            D3DResource = initialData != null
                                                          ? new D3D.Texture2D(Graphics.D3DDevice, desc, initialData.Buffers.DataBoxes)
                                                          : new D3D.Texture2D(Graphics.D3DDevice, desc);

            // Create the default render target view.
            _defaultRenderTargetView = GetRenderTargetView(Settings.Format, 0, 0, 1);

            GorgonRenderStatistics.RenderTargetCount++;
            GorgonRenderStatistics.RenderTargetSize += SizeInBytes;

            CreateDepthStencilBuffer();

            // Set default viewport.
            Viewport = new GorgonViewport(0, 0, Settings.Width, Settings.Height, 0.0f, 1.0f);
        }
Exemplo n.º 27
0
        public static Direct3D11.Texture2D CreateTexture2D(this Direct3D11.Device device,
                                                           int w, int h,
                                                           Direct3D11.BindFlags flags = Direct3D11.BindFlags.RenderTarget | Direct3D11.BindFlags.ShaderResource,
                                                           Format format = Format.B8G8R8A8_UNorm,
                                                           Direct3D11.ResourceOptionFlags options = Direct3D11.ResourceOptionFlags.Shared)
        {
            var colordesc = new Direct3D11.Texture2DDescription
            {
                BindFlags         = flags,
                Format            = format,
                Width             = w,
                Height            = h,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Direct3D11.ResourceUsage.Default,
                OptionFlags       = options,
                CpuAccessFlags    = Direct3D11.CpuAccessFlags.None,
                ArraySize         = 1
            };

            return(new Direct3D11.Texture2D(device, colordesc));
        }
Exemplo n.º 28
0
        private void CreateAndBindTargets()
        {
            var width  = Math.Max((int)ActualWidth, 100);
            var height = Math.Max((int)ActualHeight, 100);

            var renderDesc = new D3D11.Texture2DDescription
            {
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Usage             = D3D11.ResourceUsage.Default,
                OptionFlags       = D3D11.ResourceOptionFlags.Shared,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                ArraySize         = 1
            };

            var device = new D3D11.Device(DriverType.Hardware, D3D11.DeviceCreationFlags.BgraSupport);

            var renderTarget = new D3D11.Texture2D(device, renderDesc);

            var surface = renderTarget.QueryInterface <DXGI.Surface>();

            var d2DFactory = new D2D.Factory();

            var renderTargetProperties =
                new D2D.RenderTargetProperties(new D2D.PixelFormat(DXGI.Format.Unknown, D2D.AlphaMode.Premultiplied));

            _d2DRenderTarget = new D2D.RenderTarget(d2DFactory, surface, renderTargetProperties);

            SetRenderTarget(renderTarget);

            device.ImmediateContext.Rasterizer.SetViewport(0, 0, (int)ActualWidth, (int)ActualHeight);

            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }
Exemplo n.º 29
0
 protected override SharpDX.Direct3D11.Texture2D CreateRenderTarget(int width, int height)
 {
     var desc = new SharpDX.Direct3D11.Texture2DDescription {
         ArraySize = 1,
         BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget,
         CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
         Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
         Height = height,
         Width = width,
         MipLevels = 1,
         OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.Shared,
         Usage = SharpDX.Direct3D11.ResourceUsage.Default,
         SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0)
     };
     return new SharpDX.Direct3D11.Texture2D(Device, desc);
 }
Exemplo n.º 30
0
        /// <summary>
        /// Gets a copy of 2D texture data, specifying a mipmap level, source rectangle, start index, and number of elements.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="level"></param>
        /// <param name="rect"></param>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <param name="elementCount"></param>
        public void GetData <T>(int level, Rectangle?rect, T[] data, int startIndex, int elementCount) where T : struct
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentException("data cannot be null");
            }

            if (data.Length < startIndex + elementCount)
            {
                throw new ArgumentException("The data passed has a length of " + data.Length + " but " + elementCount + " pixels have been requested.");
            }

            if (rect.HasValue)
            {
                throw new NotImplementedException("Set 'rect' parameter to null.");
            }


            var mipWidth  = MathUtil.Clamp(Width >> level, 1, Width);
            var mipHeight = MathUtil.Clamp(Width >> level, 1, Height);


            // Create a temp staging resource for copying the data.
            //
            // TODO: We should probably be pooling these staging resources
            // and not creating a new one each time.
            //
            var desc = new SharpDX.Direct3D11.Texture2DDescription();

            desc.Width                     = mipWidth;
            desc.Height                    = mipHeight;
            desc.MipLevels                 = 1;
            desc.ArraySize                 = 1;
            desc.Format                    = Converter.Convert(Format);
            desc.BindFlags                 = D3D.BindFlags.None;
            desc.CpuAccessFlags            = D3D.CpuAccessFlags.Read;
            desc.SampleDescription.Count   = 1;
            desc.SampleDescription.Quality = 0;
            desc.Usage                     = D3D.ResourceUsage.Staging;
            desc.OptionFlags               = D3D.ResourceOptionFlags.None;


            var d3dContext = device.DeviceContext;

            using (var stagingTex = new D3D.Texture2D(device.Device, desc)) {
                lock (d3dContext) {
                    //
                    // Copy the data from the GPU to the staging texture.
                    //
                    int elementsInRow;
                    int rows;

                    if (rect.HasValue)
                    {
                        elementsInRow = rect.Value.Width;
                        rows          = rect.Value.Height;

                        var region = new D3D.ResourceRegion(rect.Value.Left, rect.Value.Top, 0, rect.Value.Right, rect.Value.Bottom, 1);

                        d3dContext.CopySubresourceRegion(tex2D, level, region, stagingTex, 0, 0, 0, 0);
                    }
                    else
                    {
                        elementsInRow = mipWidth;
                        rows          = mipHeight;

                        d3dContext.CopySubresourceRegion(tex2D, level, null, stagingTex, 0, 0, 0, 0);
                    }


                    // Copy the data to the array :
                    DataStream stream;
                    var        databox = d3dContext.MapSubresource(stagingTex, 0, D3D.MapMode.Read, D3D.MapFlags.None, out stream);

                    // Some drivers may add pitch to rows.
                    // We need to copy each row separatly and skip trailing zeros.
                    var currentIndex = startIndex;
                    var elementSize  = Marshal.SizeOf(typeof(T));

                    for (var row = 0; row < rows; row++)
                    {
                        stream.ReadRange(data, currentIndex, elementsInRow);
                        stream.Seek(databox.RowPitch - (elementSize * elementsInRow), SeekOrigin.Current);
                        currentIndex += elementsInRow;
                    }
                    stream.Dispose();
                }
            }
        }
Exemplo n.º 31
0
        internal Texture2D AllocateTextureReturnSurface(Device device,  Size2F drawingSize)
        {
            var desc = new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, //  D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = (int)drawingSize.Width,
                Height = (int)drawingSize.Height,
                Usage = ResourceUsage.Default,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource,
            };

            desc.Usage = ResourceUsage.Default;
            var tex2D = new Texture2D(device, desc);
            return tex2D;
        }
Exemplo n.º 32
0
        public static D3D.Texture2D CreateDepthStencilBuffer(this D3D.Device device, int width, int height, DXGI.SampleDescription sampleDescription, out D3D.DepthStencilView view)
        {
            var desc = new D3D.Texture2DDescription
            {
                Width = width,
                Height = height,
                MipLevels = 1,
                ArraySize = 1,
                Format = DXGI.Format.D24_UNorm_S8_UInt,
                SampleDescription = sampleDescription,
                Usage = D3D.ResourceUsage.Default,
                BindFlags = D3D.BindFlags.DepthStencil,
                CpuAccessFlags = D3D.CpuAccessFlags.None,
                OptionFlags = D3D.ResourceOptionFlags.None,
            };
            var depthStencilBuffer = new D3D.Texture2D(device, desc);

            view = new D3D.DepthStencilView(device, depthStencilBuffer);
            return depthStencilBuffer;
        }
        /// <summary>
        /// Creates a staging texture which enables copying data from gpu to cpu memory.
        /// </summary>
        /// <param name="device">Graphics device.</param>
        /// <param name="size">The size of generated texture.</param>
        public D3D11.Texture2D CreateStagingTexture(D3D11.Device device, GDI.Size size)
        {
            CheckTexture(ref _copyHelperTextureStaging, size);
            
            if (_copyHelperTextureStaging != null)
                return _copyHelperTextureStaging;
            
            //For handling of staging resource see
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ff476259(v=vs.85).aspx

            D3D11.Texture2DDescription textureDescription = new D3D11.Texture2DDescription();
            textureDescription.Width = size.Width;
            textureDescription.Height = size.Height;
            textureDescription.MipLevels = 1;
            textureDescription.ArraySize = 1;
            textureDescription.Format = DXGI.Format.B8G8R8A8_UNorm;
            textureDescription.Usage = D3D11.ResourceUsage.Staging;
            textureDescription.SampleDescription = new DXGI.SampleDescription(1, 0);
            textureDescription.BindFlags = D3D11.BindFlags.None;
            textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.Read;
            textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None;

            return new D3D11.Texture2D(device, textureDescription);
        }
Exemplo n.º 34
0
            public Texture(MetroGraphicsDevice dev, int width, int height)
            {
                this.dev = dev;

                var format = new D3D11.Texture2DDescription
                {
                    Format = DXGI.Format.R8G8B8A8_UNorm,
                    Width = width,
                    Height = height,
                    ArraySize = 1,
                    MipLevels = 1,
                    BindFlags = D3D11.BindFlags.ShaderResource,
                    SampleDescription = new DXGI.SampleDescription { Count = 1 }
                };

                // Allocate a 2-D surface as the depth/stencil buffer.
                tex = new D3D11.Texture2D(dev._device, format);
            }
Exemplo n.º 35
0
 public VideoFrameBuffer(D3D11.Device device3D, D3D11.Texture2DDescription textureDescription)
 {
     Texture = new D3D11.Texture2D(device3D, textureDescription);
 }
Exemplo n.º 36
0
        private void InitializeDeviceResources(DXGI.SwapChain swapChain)
        {
            backbufferTexture = swapChain.GetBackBuffer <D3D11.Texture2D>(0);
            backbufferRTV     = new D3D11.RenderTargetView(device, backbufferTexture);

            width  = backbufferTexture.Description.Width;
            height = backbufferTexture.Description.Height;

            D3D11.Texture2DDescription sceneTextureDesc = new D3D11.Texture2DDescription
            {
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                Format            = DXGI.Format.R8G8B8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = D3D11.ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = D3D11.ResourceUsage.Default
            };

            sceneTexture = new D3D11.Texture2D(device, sceneTextureDesc);
            sceneRTV     = new D3D11.RenderTargetView(device, sceneTexture);
            sceneSRV     = new D3D11.ShaderResourceView(device, sceneTexture);

            var depthBufferDesc = new D3D11.Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = DXGI.Format.R32_Typeless,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            using (var depthStencilBufferTexture = new D3D11.Texture2D(device, depthBufferDesc))
            {
                var depthStencilViewDesc = new D3D11.DepthStencilViewDescription()
                {
                    Format    = DXGI.Format.D32_Float,
                    Dimension = D3D11.DepthStencilViewDimension.Texture2D,
                    Texture2D = { MipSlice = 0 }
                };

                depthDSV = new D3D11.DepthStencilView(device, depthStencilBufferTexture, depthStencilViewDesc);

                var shaderResourceViewDesc = new D3D11.ShaderResourceViewDescription()
                {
                    Format    = DXGI.Format.R32_Float,
                    Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                    Texture2D = { MipLevels = 1, MostDetailedMip = 0 }
                };

                depthSRV = new D3D11.ShaderResourceView(device, depthStencilBufferTexture, shaderResourceViewDesc);
            }

            var depthStencilDesc = new D3D11.DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,
                DepthWriteMask   = D3D11.DepthWriteMask.All,
                DepthComparison  = D3D11.Comparison.Less,
                IsStencilEnabled = false,
                StencilReadMask  = 0xFF,
                StencilWriteMask = 0xFF,

                FrontFace = new D3D11.DepthStencilOperationDescription()
                {
                    FailOperation      = D3D11.StencilOperation.Keep,
                    DepthFailOperation = D3D11.StencilOperation.Keep,
                    PassOperation      = D3D11.StencilOperation.Keep,
                    Comparison         = D3D11.Comparison.Always
                },

                BackFace = new D3D11.DepthStencilOperationDescription()
                {
                    FailOperation      = D3D11.StencilOperation.Keep,
                    DepthFailOperation = D3D11.StencilOperation.Keep,
                    PassOperation      = D3D11.StencilOperation.Keep,
                    Comparison         = D3D11.Comparison.Always
                }
            };

            depthStencilState = new D3D11.DepthStencilState(device, depthStencilDesc);

            var rasterDesc = new D3D11.RasterizerStateDescription()
            {
                IsAntialiasedLineEnabled = false,
                CullMode                = D3D11.CullMode.Back,
                DepthBias               = 0,
                DepthBiasClamp          = 0.0f,
                IsDepthClipEnabled      = true,
                FillMode                = D3D11.FillMode.Solid,
                IsFrontCounterClockwise = false,
                IsMultisampleEnabled    = false,
                IsScissorEnabled        = false,
                SlopeScaledDepthBias    = 0.0f
            };

            rasterizerState = new D3D11.RasterizerState(device, rasterDesc);
        }
Exemplo n.º 37
0
        // Allocate all memory resources that change on a window SizeChanged event.
        protected virtual void CreateWindowSizeDependentResources()
        {
            _windowBounds = _window.Bounds;

            if (_swapChain != null)
            {
                _swapChain.ResizeBuffers(2, 0, 0, DXGI.Format.B8G8R8A8_UNorm, 0);
            }
            else
            {
                var swapChainDesc = new DXGI.SwapChainDescription1()
                {
                    Width = 0,
                    Height = 0,
                    Format = DXGI.Format.B8G8R8A8_UNorm,
                    Stereo = false,
                    Usage = DXGI.Usage.RenderTargetOutput,
                    BufferCount = 2,
                    Scaling = DXGI.Scaling.None,
                    SwapEffect = DXGI.SwapEffect.FlipSequential,
                    SampleDescription = new DXGI.SampleDescription { Count = 1, Quality = 0 },
                    Flags = 0
                };

                _swapChain = _window.CreateSwapChain(_device, ref swapChainDesc);

                // gotta figure out some reasonable way of doing this
                // dxgiDevice.MaximumFrameLatency = 1;

                D3D11.Texture2D backBuffer = _swapChain.GetBackBuffer<D3D11.Texture2D>(0);

                _renderTargetView = new D3D11.RenderTargetView(_device, backBuffer);

                // Cache the rendertarget dimensions in our helper class for convenient use.
                _renderTargetSize.Width = backBuffer.Description.Width;
                _renderTargetSize.Height = backBuffer.Description.Height;

                // Create a descriptor for the depth/stencil buffer.
                var depthStencilDesc = new D3D11.Texture2DDescription
                {
                    Format = DXGI.Format.D24_UNorm_S8_UInt,
                    Width = backBuffer.Description.Width,
                    Height = backBuffer.Description.Height,
                    ArraySize = 1,
                    MipLevels = 1,
                    BindFlags = D3D11.BindFlags.DepthStencil,
                    SampleDescription = new DXGI.SampleDescription { Count = 1 }
                };

                // Allocate a 2-D surface as the depth/stencil buffer.
                var depthStencil = new D3D11.Texture2D(_device, depthStencilDesc);

                // Create a DepthStencil view on this surface to use on bind.
                _depthStencilView = new D3D11.DepthStencilView(_device, depthStencil,
                    new D3D11.DepthStencilViewDescription { Dimension = D3D11.DepthStencilViewDimension.Texture2D });

                // Create a viewport descriptor of the full window size.
                var viewPort = new D3D11.Viewport
                {
                    TopLeftX = 0.0f,
                    TopLeftY = 0.0f,
                    Width = backBuffer.Description.Width,
                    Height = backBuffer.Description.Height
                };

                // Set the current viewport using the descriptor.
                _deviceContext.Rasterizer.SetViewports(viewPort);
            }
        }
Exemplo n.º 38
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;
        }