private void StartDesktopDuplicator(int adapterId, int outputId)
        {
            var adapter = factory.GetAdapter1(adapterId);

            D3D11.D3D11CreateDevice(adapter, DriverType.Unknown, DeviceCreationFlags.None, s_featureLevels, out device);

            var output  = adapter.GetOutput(outputId);
            var output1 = output.QueryInterface <IDXGIOutput1>();

            var bounds = output1.Description.DesktopCoordinates;
            var width  = bounds.Right - bounds.Left;
            var height = bounds.Bottom - bounds.Top;

            var textureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width / 2,
                Height            = height / 2,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = Usage.Staging
            };

            stagingTexture = device.CreateTexture2D(textureDesc);

            var smallerTextureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.None,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.GenerateMips,
                MipLevels         = 4,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = Usage.Default
            };

            smallerTexture     = device.CreateTexture2D(smallerTextureDesc);
            smallerTextureView = device.CreateShaderResourceView(smallerTexture);

            duplication = output1.DuplicateOutput(device);
        }
Example #2
0
        void CreateFontsTexture()
        {
            var   io = ImGui.GetIO();
            byte *pixels;
            int   width, height;

            io.Fonts.GetTexDataAsRGBA32(out pixels, out width, out height);

            var texDesc = new Texture2DDescription
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.R8G8B8A8_UNorm,
                SampleDescription = new SampleDescription {
                    Count = 1
                },
                Usage          = Vortice.Direct3D11.Usage.Default,
                BindFlags      = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None
            };

            var subResource = new SubresourceData
            {
                DataPointer = (IntPtr)pixels,
                Pitch       = texDesc.Width * 4,
                SlicePitch  = 0
            };

            var texture = device.CreateTexture2D(texDesc, new[] { subResource });

            var resViewDesc = new ShaderResourceViewDescription
            {
                Format        = Format.R8G8B8A8_UNorm,
                ViewDimension = ShaderResourceViewDimension.Texture2D,
                Texture2D     = new Texture2DShaderResourceView {
                    MipLevels = texDesc.MipLevels, MostDetailedMip = 0
                }
            };

            fontTextureView = device.CreateShaderResourceView(texture, resViewDesc);
            texture.Release();

            io.Fonts.TexID = RegisterTexture(fontTextureView);

            var samplerDesc = new SamplerDescription
            {
                Filter             = Filter.MinMagMipLinear,
                AddressU           = TextureAddressMode.Wrap,
                AddressV           = TextureAddressMode.Wrap,
                AddressW           = TextureAddressMode.Wrap,
                MipLODBias         = 0f,
                ComparisonFunction = ComparisonFunction.Always,
                MinLOD             = 0f,
                MaxLOD             = 0f
            };

            fontSampler = device.CreateSamplerState(samplerDesc);
        }
Example #3
0
        public D3D11Texture(ID3D11Device device, ref TextureDescription description)
        {
            _device     = device;
            Width       = description.Width;
            Height      = description.Height;
            Depth       = description.Depth;
            MipLevels   = description.MipLevels;
            ArrayLayers = description.ArrayLayers;
            Format      = description.Format;
            Usage       = description.Usage;
            Type        = description.Type;
            SampleCount = description.SampleCount;

            DxgiFormat = D3D11Formats.ToDxgiFormat(
                description.Format,
                (description.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil);
            TypelessDxgiFormat = D3D11Formats.GetTypelessFormat(DxgiFormat);

            CpuAccessFlags      cpuFlags      = CpuAccessFlags.None;
            Usage               resourceUsage = Vortice.Direct3D11.Usage.Default;
            BindFlags           bindFlags     = BindFlags.None;
            ResourceOptionFlags optionFlags   = ResourceOptionFlags.None;

            if ((description.Usage & TextureUsage.RenderTarget) == TextureUsage.RenderTarget)
            {
                bindFlags |= BindFlags.RenderTarget;
            }
            if ((description.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil)
            {
                bindFlags |= BindFlags.DepthStencil;
            }
            if ((description.Usage & TextureUsage.Sampled) == TextureUsage.Sampled)
            {
                bindFlags |= BindFlags.ShaderResource;
            }
            if ((description.Usage & TextureUsage.Storage) == TextureUsage.Storage)
            {
                bindFlags |= BindFlags.UnorderedAccess;
            }
            if ((description.Usage & TextureUsage.Staging) == TextureUsage.Staging)
            {
                cpuFlags      = CpuAccessFlags.Read | CpuAccessFlags.Write;
                resourceUsage = Vortice.Direct3D11.Usage.Staging;
            }

            if ((description.Usage & TextureUsage.GenerateMipmaps) != 0)
            {
                bindFlags   |= BindFlags.RenderTarget | BindFlags.ShaderResource;
                optionFlags |= ResourceOptionFlags.GenerateMips;
            }

            int arraySize = (int)description.ArrayLayers;

            if ((description.Usage & TextureUsage.Cubemap) == TextureUsage.Cubemap)
            {
                optionFlags = ResourceOptionFlags.TextureCube;
                arraySize  *= 6;
            }

            int roundedWidth  = (int)description.Width;
            int roundedHeight = (int)description.Height;

            if (FormatHelpers.IsCompressedFormat(description.Format))
            {
                roundedWidth  = ((roundedWidth + 3) / 4) * 4;
                roundedHeight = ((roundedHeight + 3) / 4) * 4;
            }

            if (Type == TextureType.Texture1D)
            {
                Texture1DDescription desc1D = new Texture1DDescription()
                {
                    Width          = roundedWidth,
                    MipLevels      = (int)description.MipLevels,
                    ArraySize      = arraySize,
                    Format         = TypelessDxgiFormat,
                    BindFlags      = bindFlags,
                    CpuAccessFlags = cpuFlags,
                    Usage          = resourceUsage,
                    OptionFlags    = optionFlags,
                };

                DeviceTexture = device.CreateTexture1D(desc1D);
            }
            else if (Type == TextureType.Texture2D)
            {
                Texture2DDescription deviceDescription = new Texture2DDescription()
                {
                    Width             = roundedWidth,
                    Height            = roundedHeight,
                    MipLevels         = (int)description.MipLevels,
                    ArraySize         = arraySize,
                    Format            = TypelessDxgiFormat,
                    BindFlags         = bindFlags,
                    CpuAccessFlags    = cpuFlags,
                    Usage             = resourceUsage,
                    SampleDescription = new Vortice.DXGI.SampleDescription((int)FormatHelpers.GetSampleCountUInt32(SampleCount), 0),
                    OptionFlags       = optionFlags,
                };

                DeviceTexture = device.CreateTexture2D(deviceDescription);
            }
            else
            {
                Debug.Assert(Type == TextureType.Texture3D);
                Texture3DDescription desc3D = new Texture3DDescription()
                {
                    Width          = roundedWidth,
                    Height         = roundedHeight,
                    Depth          = (int)description.Depth,
                    MipLevels      = (int)description.MipLevels,
                    Format         = TypelessDxgiFormat,
                    BindFlags      = bindFlags,
                    CpuAccessFlags = cpuFlags,
                    Usage          = resourceUsage,
                    OptionFlags    = optionFlags,
                };

                DeviceTexture = device.CreateTexture3D(desc3D);
            }
        }