/// <summary>
            /// Creates a staging texture which enables copying data from gpu to cpu memory.
            /// </summary>
            /// <param name="device">Graphics device.</param>
            /// <param name="width">Width of generated texture.</param>
            /// <param name="height">Height of generated texture.</param>
            public static D3D11.ID3D11Texture2D CreateStagingTexture(EngineDevice device, int width, int height)
            {
                device.EnsureNotNull(nameof(device));
                width.EnsurePositiveOrZero(nameof(width));
                height.EnsurePositiveOrZero(nameof(height));

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

                var textureDescription = new D3D11.Texture2DDescription
                {
                    Width             = width,
                    Height            = height,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    Format            = DEFAULT_TEXTURE_FORMAT,
                    Usage             = D3D11.ResourceUsage.Staging,
                    SampleDescription = new SampleDescription(1, 0),
                    BindFlags         = D3D11.BindFlags.None,
                    CpuAccessFlags    = D3D11.CpuAccessFlags.Read,
                    OptionFlags       = D3D11.ResourceOptionFlags.None
                };

                return(device.DeviceD3D11_1.CreateTexture2D(textureDescription));
            }
            /// <summary>
            /// Creates a depth buffer texture with 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>
            /// <param name="gfxConfig">Current graphics configuration.</param>
            public static D3D11.ID3D11Texture2D CreateDepthBufferTexture(EngineDevice device, int width, int height, GraphicsViewConfiguration gfxConfig)
            {
                device.EnsureNotNull(nameof(device));
                width.EnsurePositiveOrZero(nameof(width));
                height.EnsurePositiveOrZero(nameof(height));
                gfxConfig.EnsureNotNull(nameof(gfxConfig));

                var textureDescription = new D3D11.Texture2DDescription();

                if (gfxConfig.AntialiasingEnabled &&
                    device.IsStandardAntialiasingPossible)
                {
                    textureDescription.Width             = width;
                    textureDescription.Height            = height;
                    textureDescription.MipLevels         = 1;
                    textureDescription.ArraySize         = 1;
                    textureDescription.Usage             = D3D11.ResourceUsage.Default;
                    textureDescription.SampleDescription = device.GetSampleDescription(gfxConfig.AntialiasingQuality);
                    textureDescription.BindFlags         = D3D11.BindFlags.DepthStencil;
                    textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                    textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
                }
                else
                {
                    textureDescription.Width             = width;
                    textureDescription.Height            = height;
                    textureDescription.MipLevels         = 1;
                    textureDescription.ArraySize         = 1;
                    textureDescription.Usage             = D3D11.ResourceUsage.Default;
                    textureDescription.SampleDescription = new SampleDescription(1, 0);
                    textureDescription.BindFlags         = D3D11.BindFlags.DepthStencil;
                    textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                    textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
                }

                // Set buffer format
                textureDescription.Format = Format.D32_Float_S8X24_UInt;

                // Create the texture finally
                return(device.DeviceD3D11_1.CreateTexture2D(textureDescription));
            }
            /// <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>
            /// <param name="gfxConfig">The GFX configuration.</param>
            public static D3D11.ID3D11Texture2D CreateRenderTargetTexture(
                EngineDevice device, int width, int height, GraphicsViewConfiguration gfxConfig)
            {
                device.EnsureNotNull(nameof(device));
                width.EnsurePositiveOrZero(nameof(width));
                height.EnsurePositiveOrZero(nameof(height));
                gfxConfig.EnsureNotNull(nameof(gfxConfig));

                var textureDescription = new D3D11.Texture2DDescription();

                if (gfxConfig.AntialiasingEnabled &&
                    device.IsStandardAntialiasingPossible)
                {
                    textureDescription.Width             = width;
                    textureDescription.Height            = height;
                    textureDescription.MipLevels         = 1;
                    textureDescription.ArraySize         = 1;
                    textureDescription.Format            = DEFAULT_TEXTURE_FORMAT;
                    textureDescription.Usage             = D3D11.ResourceUsage.Default;
                    textureDescription.SampleDescription = device.GetSampleDescription(gfxConfig.AntialiasingQuality);
                    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            = DEFAULT_TEXTURE_FORMAT;
                    textureDescription.Usage             = D3D11.ResourceUsage.Default;
                    textureDescription.SampleDescription = new SampleDescription(1, 0);
                    textureDescription.BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                    textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                    textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
                }

                return(device.DeviceD3D11_1.CreateTexture2D(textureDescription));
            }
            /// <summary>
            /// Creates a standard 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>
            /// <param name="format">The format which is used to create the texture.</param>
            public static D3D11.ID3D11Texture2D CreateCpuWritableTexture(EngineDevice device, int width, int height, Format format = DEFAULT_TEXTURE_FORMAT)
            {
                device.EnsureNotNull(nameof(device));
                width.EnsurePositiveOrZero(nameof(width));
                height.EnsurePositiveOrZero(nameof(height));

                var textureDescription = new D3D11.Texture2DDescription
                {
                    Width             = width,
                    Height            = height,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    Format            = format,
                    Usage             = D3D11.ResourceUsage.Dynamic,
                    SampleDescription = new SampleDescription(1, 0),
                    BindFlags         = D3D11.BindFlags.ShaderResource,
                    CpuAccessFlags    = D3D11.CpuAccessFlags.Write,
                    OptionFlags       = D3D11.ResourceOptionFlags.None
                };

                return(device.DeviceD3D11_1.CreateTexture2D(textureDescription));
            }
            /// <summary>
            /// Creates a Direct3D 11 texture that can be shared between more devices.
            /// </summary>
            /// <param name="device">The Direct3D 11 device.</param>
            /// <param name="width">The width of the generated texture.</param>
            /// <param name="height">The height of the generated texture.</param>
            public static D3D11.ID3D11Texture2D CreateSharedTexture(EngineDevice device, int width, int height)
            {
                device.EnsureNotNull(nameof(device));
                width.EnsurePositiveOrZero(nameof(width));
                height.EnsurePositiveOrZero(nameof(height));

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

                return(device.DeviceD3D11_1.CreateTexture2D(textureDescription));
            }
        /// <summary>
        /// Creates a new image based on the given raw image data.
        /// </summary>
        /// <param name="device">Graphics device.</param>
        /// <param name="rawImage">Raw image data.</param>
        internal static D3D11.ID3D11Texture2D CreateTexture(EngineDevice device, SDXTK.Image rawImage)
        {
            var textureDescription = new D3D11.Texture2DDescription
            {
                Width             = rawImage.Description.Width,
                Height            = rawImage.Description.Height,
                MipLevels         = rawImage.Description.MipLevels,
                ArraySize         = rawImage.Description.ArraySize,
                Format            = rawImage.Description.Format,
                Usage             = D3D11.ResourceUsage.Default,
                SampleDescription = new SampleDescription(1, 0),
                BindFlags         = D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            // Special handling for cube textures
            if (rawImage.Description.Dimension == SDXTK.TextureDimension.TextureCube)
            {
                textureDescription.OptionFlags = D3D11.ResourceOptionFlags.TextureCube;
            }

            return(device.DeviceD3D11_1.CreateTexture2D(textureDescription, rawImage.ToD3D11SubresourceData()));
        }
 /// <summary>
 /// Is the given texture multisampled?
 /// </summary>
 internal static bool IsMultisampled(D3D11.Texture2DDescription textureDesc)
 {
     return(textureDesc.SampleDescription.Count > 1 || textureDesc.SampleDescription.Quality > 0);
 }
示例#8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ShaderResourceViewDescription"/> struct.
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="viewDimension"></param>
        /// <param name="format"></param>
        /// <param name="mostDetailedMip"></param>
        /// <param name="mipLevels"></param>
        /// <param name="firstArraySlice"></param>
        /// <param name="arraySize"></param>
        public ShaderResourceViewDescription(
            ID3D11Texture2D texture,
            ShaderResourceViewDimension viewDimension,
            Format format       = Format.Unknown,
            int mostDetailedMip = 0,
            int mipLevels       = -1,
            int firstArraySlice = 0,
            int arraySize       = -1)
            : this()
        {
            ViewDimension = viewDimension;
            if (format == Format.Unknown ||
                (mipLevels == -1 && viewDimension != ShaderResourceViewDimension.Texture2DMultisampled && viewDimension != ShaderResourceViewDimension.Texture2DMultisampledArray) ||
                (arraySize == -1 && (ShaderResourceViewDimension.Texture2DArray == viewDimension || ShaderResourceViewDimension.Texture2DMultisampledArray == viewDimension || ShaderResourceViewDimension.TextureCubeArray == viewDimension)))
            {
                Texture2DDescription textureDesc = texture.Description;
                if (format == Format.Unknown)
                {
                    format = textureDesc.Format;
                }
                if (-1 == mipLevels)
                {
                    mipLevels = textureDesc.MipLevels - mostDetailedMip;
                }
                if (-1 == arraySize)
                {
                    arraySize = textureDesc.ArraySize - firstArraySlice;
                    if (viewDimension == ShaderResourceViewDimension.TextureCubeArray)
                    {
                        arraySize /= 6;
                    }
                }
            }
            Format = format;
            switch (viewDimension)
            {
            case ShaderResourceViewDimension.Texture2D:
                Texture2D.MostDetailedMip = mostDetailedMip;
                Texture2D.MipLevels       = mipLevels;
                break;

            case ShaderResourceViewDimension.Texture2DArray:
                Texture2DArray.MostDetailedMip = mostDetailedMip;
                Texture2DArray.MipLevels       = mipLevels;
                Texture2DArray.FirstArraySlice = firstArraySlice;
                Texture2DArray.ArraySize       = arraySize;
                break;

            case ShaderResourceViewDimension.Texture2DMultisampled:
                break;

            case ShaderResourceViewDimension.Texture2DMultisampledArray:
                Texture2DMSArray.FirstArraySlice = firstArraySlice;
                Texture2DMSArray.ArraySize       = arraySize;
                break;

            case ShaderResourceViewDimension.TextureCube:
                TextureCube.MostDetailedMip = mostDetailedMip;
                TextureCube.MipLevels       = mipLevels;
                break;

            case ShaderResourceViewDimension.TextureCubeArray:
                TextureCubeArray.MostDetailedMip  = mostDetailedMip;
                TextureCubeArray.MipLevels        = mipLevels;
                TextureCubeArray.First2DArrayFace = firstArraySlice;
                TextureCubeArray.NumCubes         = arraySize;
                break;

            default:
                break;
            }
        }