public static DepthStencilViewDescription CreateForTexture1D(int formatID, DepthStencilViewFlags flags, int mipSlice)
 {
     return new DepthStencilViewDescription
     {
         Dimension = DepthStencilViewDimension.Texture1D,
         FormatID = formatID,
         Flags = flags,
         MipSlice = mipSlice
     };
 }
Пример #2
0
    /// <summary>
    /// Initializes a new instance of the <see cref="DepthStencilViewDescription"/> struct.
    /// </summary>
    /// <param name="texture"></param>
    /// <param name="viewDimension"></param>
    /// <param name="format"></param>
    /// <param name="mipSlice"></param>
    /// <param name="firstArraySlice"></param>
    /// <param name="arraySize"></param>
    /// <param name="flags"></param>
    public DepthStencilViewDescription(
        ID3D11Texture2D texture,
        DepthStencilViewDimension viewDimension,
        Format format               = Format.Unknown,
        int mipSlice                = 0,
        int firstArraySlice         = 0,
        int arraySize               = -1,
        DepthStencilViewFlags flags = DepthStencilViewFlags.None) : this()
    {
        ViewDimension = viewDimension;
        Flags         = flags;

        if (format == Format.Unknown ||
            (-1 == arraySize && (DepthStencilViewDimension.Texture2DArray == viewDimension || DepthStencilViewDimension.Texture2DMultisampledArray == viewDimension)))
        {
            var textureDesc = texture.Description;
            if (format == Format.Unknown)
            {
                format = textureDesc.Format;
            }
            if (arraySize == -1)
            {
                arraySize = textureDesc.ArraySize - firstArraySlice;
            }
        }
        Format = format;
        switch (viewDimension)
        {
        case DepthStencilViewDimension.Texture2D:
            Texture2D.MipSlice = mipSlice;
            break;

        case DepthStencilViewDimension.Texture2DArray:
            Texture2DArray.MipSlice        = mipSlice;
            Texture2DArray.FirstArraySlice = firstArraySlice;
            Texture2DArray.ArraySize       = arraySize;
            break;

        case DepthStencilViewDimension.Texture2DMultisampled:
            break;

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

        default:
            break;
        }
    }
Пример #3
0
    /// <summary>
    /// Initializes a new instance of the <see cref="DepthStencilViewDescription"/> struct.
    /// </summary>
    /// <param name="viewDimension">The <see cref="DepthStencilViewDimension"/></param>
    /// <param name="format">The <see cref="DXGI.Format"/> to use or <see cref="Format.Unknown"/>.</param>
    /// <param name="mipSlice">The index of the mipmap level to use mip slice.</param>
    /// <param name="firstArraySlice">The index of the first texture to use in an array of textures.</param>
    /// <param name="arraySize">Number of textures in the array.</param>
    /// <param name="flags"></param>
    public DepthStencilViewDescription(
        DepthStencilViewDimension viewDimension,
        Format format               = Format.Unknown,
        int mipSlice                = 0,
        int firstArraySlice         = 0,
        int arraySize               = -1,
        DepthStencilViewFlags flags = DepthStencilViewFlags.None) : this()
    {
        Format        = format;
        ViewDimension = viewDimension;
        Flags         = flags;
        switch (viewDimension)
        {
        case DepthStencilViewDimension.Texture1D:
            Texture1D.MipSlice = mipSlice;
            break;

        case DepthStencilViewDimension.Texture1DArray:
            Texture1DArray.MipSlice        = mipSlice;
            Texture1DArray.FirstArraySlice = firstArraySlice;
            Texture1DArray.ArraySize       = arraySize;
            break;

        case DepthStencilViewDimension.Texture2D:
            Texture2D.MipSlice = mipSlice;
            break;

        case DepthStencilViewDimension.Texture2DArray:
            Texture2DArray.MipSlice        = mipSlice;
            Texture2DArray.FirstArraySlice = firstArraySlice;
            Texture2DArray.ArraySize       = arraySize;
            break;

        case DepthStencilViewDimension.Texture2DMultisampled:
            break;

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

        default:
            break;
        }
    }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GorgonDepthStencilView"/> class.
        /// </summary>
        /// <param name="resource">The resource to bind to the view.</param>
        /// <param name="format">The format of the view.</param>
        /// <param name="mipSlice">The mip level to use for the view.</param>
        /// <param name="firstArrayIndex">The first array index to use for the view.</param>
        /// <param name="arrayCount">The number of array indices to use for the view.</param>
        /// <param name="flags">Depth/stencil view flags.</param>
        internal GorgonDepthStencilView(GorgonResource resource, BufferFormat format, int mipSlice, int firstArrayIndex, int arrayCount, DepthStencilViewFlags flags)
            : base(resource, format)
        {
            MipSlice        = mipSlice;
            FirstArrayIndex = firstArrayIndex;
            ArrayCount      = arrayCount;
            Flags           = flags;

            switch (resource.ResourceType)
            {
            case ResourceType.Texture1D:
                _depth1D = (GorgonDepthStencil1D)resource;
                break;

            case ResourceType.Texture2D:
                _depth2D = (GorgonDepthStencil2D)resource;
                break;
            }
        }
Пример #5
0
    /// <summary>
    /// Initializes a new instance of the <see cref="DepthStencilViewDescription"/> struct.
    /// </summary>
    /// <param name="texture"></param>
    /// <param name="isArray"></param>
    /// <param name="format"></param>
    /// <param name="mipSlice"></param>
    /// <param name="firstArraySlice"></param>
    /// <param name="arraySize"></param>
    /// <param name="flags"></param>
    public DepthStencilViewDescription(
        ID3D11Texture1D texture,
        bool isArray,
        Format format               = Format.Unknown,
        int mipSlice                = 0,
        int firstArraySlice         = 0,
        int arraySize               = -1,
        DepthStencilViewFlags flags = DepthStencilViewFlags.None) : this()
    {
        ViewDimension = isArray ? DepthStencilViewDimension.Texture1DArray : DepthStencilViewDimension.Texture1D;
        Flags         = flags;
        if (format == Format.Unknown ||
            (arraySize == -1 && DepthStencilViewDimension.Texture1DArray == ViewDimension))
        {
            var textureDesc = texture.Description;
            if (format == Format.Unknown)
            {
                format = textureDesc.Format;
            }
            if (arraySize == -1)
            {
                arraySize = textureDesc.ArraySize - firstArraySlice;
            }
        }

        Format = format;
        switch (ViewDimension)
        {
        case DepthStencilViewDimension.Texture1D:
            Texture1D.MipSlice = mipSlice;
            break;

        case DepthStencilViewDimension.Texture1DArray:
            Texture1DArray.MipSlice        = mipSlice;
            Texture1DArray.FirstArraySlice = firstArraySlice;
            Texture1DArray.ArraySize       = arraySize;
            break;

        default:
            break;
        }
    }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GorgonDepthStencil2DView"/> class.
        /// </summary>
        /// <param name="texture">The resource to bind to the view.</param>
        /// <param name="format">The format of the view.</param>
        /// <param name="formatInfo">Information about the format.</param>
        /// <param name="mipSlice">The mip level to use for the view.</param>
        /// <param name="firstArrayIndex">The first array index to use for the view.</param>
        /// <param name="arrayCount">The number of array indices to use for the view.</param>
        /// <param name="flags">Depth/stencil view flags.</param>
        internal GorgonDepthStencil2DView(GorgonTexture2D texture,
                                          BufferFormat format,
                                          GorgonFormatInfo formatInfo,
                                          int mipSlice,
                                          int firstArrayIndex,
                                          int arrayCount,
                                          DepthStencilViewFlags flags)
            : base(texture)
        {
            Texture           = texture ?? throw new ArgumentNullException(nameof(texture));
            Format            = format;
            FormatInformation = formatInfo ?? throw new ArgumentNullException(nameof(formatInfo));

            MipSlice   = texture.MipLevels <= 0 ? 0 : mipSlice.Max(0).Min(texture.MipLevels - 1);
            ArrayIndex = firstArrayIndex;
            ArrayCount = arrayCount;
            Flags      = flags;
            MipWidth   = (Width >> MipSlice).Max(1);
            MipHeight  = (Height >> MipSlice).Max(1);

            Bounds = new DX.Rectangle(0, 0, Width, Height);
        }
Пример #7
0
        /// <summary>
        /// Function to create/retrieve a depth/stencil view in the cache.
        /// </summary>
        /// <param name="format">Format of the depth/stencil view.</param>
        /// <param name="mipSlice">Mip slice.</param>
        /// <param name="arrayIndex">Array index.</param>
        /// <param name="arrayCount">Array count.</param>
        /// <param name="flags">Flags for the depth/stencil view.</param>
        /// <returns>The cached depth/stencil view.</returns>
        public GorgonDepthStencilView GetDepthStencilView(BufferFormat format,
                                                          int mipSlice,
                                                          int arrayIndex,
                                                          int arrayCount,
                                                          DepthStencilViewFlags flags)
        {
            var key = new ViewKey(format, mipSlice, arrayIndex, arrayCount, (int)flags);

            lock (_syncLock)
            {
                GorgonDepthStencilView result;

                if (_depthViews.TryGetValue(key, out result))
                {
                    return(result);
                }

                switch (_resource.ResourceType)
                {
                case ResourceType.Texture1D:
                case ResourceType.Texture2D:
                    result = new GorgonDepthStencilView(_resource, format, mipSlice, arrayIndex, arrayCount, flags);
                    break;
                }

                // This should never happen.
                if (result == null)
                {
                    throw new GorgonException(GorgonResult.CannotCreate,
                                              string.Format(Resources.GORGFX_IMAGE_TYPE_INVALID, _resource.ResourceType));
                }

                result.Initialize();
                _depthViews.Add(key, result);

                return(result);
            }
        }
Пример #8
0
 IDepthStencilView ITexture1D.ViewAsDepthStencil(int formatID, DepthStencilViewFlags flags, int mipSlice)
 {
     return ViewAsDepthStencil(formatID, flags, mipSlice);
 }
Пример #9
0
        /// <summary>
        /// Function to retrieve a depth/stencil view object.
        /// </summary>
        /// <param name="format">The format of the depth/stencil view.</param>
        /// <param name="mipSlice">Starting mip map for the view.</param>
        /// <param name="arrayStart">Starting array index for the view.</param>
        /// <param name="arrayCount">Array index count for the view.</param>
        /// <param name="flags">Flags to determine how to treat the bound view.</param>
        /// <remarks>Use a depth/stencil view to bind a resource to the pipeline as a depth/stencil buffer.  A depth/stencil view can view a select portion of the texture, and the view <paramref name="format"/> can be used to
        /// cast the format of the texture into another type (as long as the view format has the same bit depth as the depth/stencil format).  For example, a texture with a format of D32_Float could be cast
        /// to R32_Uint, R32_Int or R32_Float formats.
        /// <para>The <paramref name="flags"/> parameter will allow the depth/stencil buffer to be read simultaneously from the depth/stencil view and from a shader view.  It is not normally possible to bind a view of a
        /// resource to 2 parts of the pipeline at the same time.  However, using the flags provided, read-only access may be granted to a part of the resource (depth or stencil) or all of it for all parts of the pipline.
        /// This would bind the depth/stencil as a read-only view and make it a read-only view accessible to shaders. If the flags are not set to None, then the depth/stencil buffer must allow shader access.</para>
        /// <para>Binding to simulatenous views require a video device with a feature level of SM5 or better.</para>
        /// </remarks>
        /// <exception cref="GorgonLibrary.GorgonException">Thrown when the view could not created or retrieved from the internal cache.</exception>
        /// <returns>A texture shader view object.</returns>
        public GorgonDepthStencilView GetDepthStencilView(BufferFormat format, int mipSlice, int arrayStart, int arrayCount, DepthStencilViewFlags flags)
        {
            if (flags == DepthStencilViewFlags.None)
            {
                return(OnGetDepthStencilView(format, mipSlice, arrayStart, arrayCount, flags));
            }

            if (Graphics.VideoDevice.SupportedFeatureLevel < DeviceFeatureLevel.SM5)
            {
                throw new GorgonException(GorgonResult.CannotCreate, string.Format(Resources.GORGFX_REQUIRES_SM, DeviceFeatureLevel.SM5));
            }

            if (!Settings.AllowShaderView)
            {
                throw new GorgonException(GorgonResult.CannotCreate, string.Format(Resources.GORGFX_VIEW_NO_SUPPORT, "GorgonShaderView"));
            }

            return(OnGetDepthStencilView(format, mipSlice, arrayStart, arrayCount, flags));
        }
Пример #10
0
 public IDepthStencilView ViewAsDepthStencilMultisampled(int formatID, DepthStencilViewFlags flags)
 {
     DepthStencilViewDescription viewDesc;
     DepthStencilViewDescription.CreateForTexture2DMultisampled(formatID, flags, out viewDesc);
     return GetDsv(ref viewDesc);
 }
Пример #11
0
 public static DepthStencilViewDescription CreateForTexture1DArray(int formatID, DepthStencilViewFlags flags, int mipSlice, int firstArraySlice, int arraySize)
 {
     return new DepthStencilViewDescription
     {
         Dimension = DepthStencilViewDimension.Texture1DArray,
         FormatID = formatID,
         Flags = flags,
         MipSlice = mipSlice,
         FirstArraySlice = firstArraySlice,
         ArraySize = arraySize
     };
 }
Пример #12
0
 public static void CreateForTexture1D(int formatID, DepthStencilViewFlags flags, int mipSlice, out DepthStencilViewDescription desc)
 {
     desc = new DepthStencilViewDescription
     {
         Dimension = DepthStencilViewDimension.Texture1D,
         FormatID = formatID,
         Flags = flags,
         MipSlice = mipSlice
     };
 }
Пример #13
0
 public static void CreateForTexture2DMultisampledArray(int formatID, DepthStencilViewFlags flags, int firstArraySlice, int arraySize, out DepthStencilViewDescription desc)
 {
     desc = new DepthStencilViewDescription
     {
         Dimension = DepthStencilViewDimension.Texture2DMultisampledArray,
         FormatID = formatID,
         Flags = flags,
         FirstArraySlice = firstArraySlice,
         ArraySize = arraySize
     };
 }
Пример #14
0
 public static void CreateForTexture2DMultisampled(int formatID, DepthStencilViewFlags flags, out DepthStencilViewDescription desc)
 {
     desc = new DepthStencilViewDescription
     {
         Dimension = DepthStencilViewDimension.Texture2DMultisampled,
         FormatID = formatID,
         Flags = flags,
     };
 }
Пример #15
0
 public static DepthStencilViewDescription CreateForTexture2DMultisampled(int formatID, DepthStencilViewFlags flags)
 {
     return new DepthStencilViewDescription
     {
         Dimension = DepthStencilViewDimension.Texture2DMultisampled,
         FormatID = formatID,
         Flags = flags,
     };
 }
Пример #16
0
 IDepthStencilView ITexture1D.ViewAsDepthStencilArray(int formatID, DepthStencilViewFlags flags, int mipSlice, int firstArraySlice, int arraySize)
 {
     return ViewAsDepthStencilArray(formatID, flags, mipSlice, firstArraySlice, arraySize);
 }
Пример #17
0
 public IDepthStencilView ViewAsDepthStencil(int formatID, DepthStencilViewFlags flags, int mipSlice)
 {
     DepthStencilViewDescription viewDesc;
     DepthStencilViewDescription.CreateForTexture2D(formatID, flags, mipSlice, out viewDesc);
     return GetDsv(ref viewDesc);
 }
Пример #18
0
 /// <summary>
 /// Function to create a new <see cref="GorgonDepthStencil2DView"/> for this texture.
 /// </summary>
 /// <param name="format">[Optional] The format for the view.</param>
 /// <param name="flags">[Optional] Flags to define how this view should be accessed by the shader.</param>
 /// <returns>A <see cref="GorgonDepthStencil2DView"/> used to bind the texture as a depth/stencil buffer.</returns>
 /// <exception cref="ArgumentException">Thrown when the <paramref name="format"/> is not supported as a depth/stencil format.</exception>
 /// <exception cref="GorgonException">Thrown when this texture does not have a <see cref="TextureBinding"/> of <see cref="TextureBinding.DepthStencil"/>.
 /// <para>-or-</para>
 /// <para>Thrown when this texture has a <see cref="GorgonGraphicsResource.Usage"/> of <see cref="ResourceUsage.Staging"/>.</para>
 /// <para>-or-</para>
 /// <para>Thrown if this texture has a <see cref="Binding"/> of <see cref="TextureBinding.ShaderResource"/>, but the texture format is not a typeless format.</para>
 /// </exception>
 /// <remarks>
 /// <para>
 /// The depth/stencil views take a <see cref="DepthStencilViewFlags"/> parameter that determine how a shader can access the depth buffer when it is bound to the pipeline for reading. By specifying
 /// a single flag, the depth can write to the opposite plane (e.g. read only depth and write only stencil, write only depth and read only stencil) of the texture. This allows for multiple
 /// depth/stencil views to be bound to the pipeline for reading and writing.
 /// </para>
 /// <para>
 /// If the <see cref="Binding"/> for the texture includes <see cref="TextureBinding.ShaderResource"/>, then the <paramref name="format"/> for the view and the <see cref="Format"/> for the texture
 /// must be specific values.  These values are listed below:
 /// <list type="table">
 ///     <listheader><term>Depth Format</term><term>Texture Format</term></listheader>
 ///     <item><term><see cref="BufferFormat.D32_Float_S8X24_UInt"/></term><term><see cref="BufferFormat.R32G8X24_Typeless"/></term></item>
 ///     <item><term><see cref="BufferFormat.D24_UNorm_S8_UInt"/></term><term><see cref="BufferFormat.R24G8_Typeless"/></term></item>
 ///     <item><term><see cref="BufferFormat.D32_Float"/></term><term><see cref="BufferFormat.R32_Typeless"/></term></item>
 ///     <item><term><see cref="BufferFormat.D16_UNorm"/></term><term><see cref="BufferFormat.R16_Typeless"/></term></item>
 /// </list>
 /// </para>
 /// </remarks>
 public GorgonDepthStencil2DView GetDepthStencilView(BufferFormat format = BufferFormat.Unknown, DepthStencilViewFlags flags = DepthStencilViewFlags.None) => Texture.GetDepthStencilView(format, MipSlice, ArrayIndex, ArrayCount, flags);
Пример #19
0
 public IDepthStencilView ViewAsDepthStencilMultisampledArray(int formatID, DepthStencilViewFlags flags, int firstArraySlice, int arraySize)
 {
     DepthStencilViewDescription viewDesc;
     DepthStencilViewDescription.CreateForTexture2DMultisampledArray(formatID, flags, firstArraySlice, arraySize, out viewDesc);
     return GetDsv(ref viewDesc);
 }
Пример #20
0
        /// <summary>
        /// Function to create a new depth/stencil buffer that is bindable to the GPU.
        /// </summary>
        /// <param name="graphics">The graphics interface to use when creating the target.</param>
        /// <param name="info">The information about the depth/stencil texture.</param>
        /// <param name="viewFlags">[Optional] Flags used to determine if the depth buffer/stencil can be read by the GPU or not.</param>
        /// <returns>A new <see cref="GorgonDepthStencil2DView"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/>, or <paramref name="info"/> parameter is <b>null</b>.</exception>
        /// <remarks>
        /// <para>
        /// This is a convenience method that will create a <see cref="GorgonTexture2D"/> and a <see cref="GorgonDepthStencil2DView"/> as a single object that users can use to apply a depth/stencil texture.
        /// This helps simplify creation of a render target by executing some prerequisite steps on behalf of the user.
        /// </para>
        /// <para>
        /// Since the <see cref="GorgonTexture2D"/> created by this method is linked to the <see cref="GorgonDepthStencil2DView"/> returned, disposal of either one will dispose of the other on your behalf.
        /// If the user created a <see cref="GorgonDepthStencil2DView"/> from the <see cref="GorgonTexture2D.GetRenderTargetView"/> method on the <see cref="GorgonTexture2D"/>, then it's assumed the user
        /// knows what they are doing and will handle the disposal of the texture and view on their own.
        /// </para>
        /// <para>
        /// To make the texture bindable on the GPU as a shader resource view, set the <see cref="IGorgonTexture2DInfo.Binding"/> to include the <see cref="TextureBinding.ShaderResource"/> flag in the value
        /// and set the <paramref name="viewFlags"/> to <see cref="DepthStencilViewFlags.ReadOnlyDepth"/>, <see cref="DepthStencilViewFlags.ReadOnlyStencil"/> or both.
        /// </para>
        /// </remarks>
        /// <seealso cref="GorgonTexture2D"/>
        public static GorgonDepthStencil2DView CreateDepthStencil(GorgonGraphics graphics, IGorgonTexture2DInfo info, DepthStencilViewFlags viewFlags = DepthStencilViewFlags.None)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException(nameof(graphics));
            }

            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            TextureBinding binding = TextureBinding.DepthStencil;

            if ((info.Binding & TextureBinding.ShaderResource) == TextureBinding.ShaderResource)
            {
                if (viewFlags != DepthStencilViewFlags.None)
                {
                    binding |= TextureBinding.ShaderResource;
                }
                else
                {
                    // Do this to notify the user that something is amiss.
                    graphics.Log.Print($"WARNING: Depth Stencil View {info.Name} - Depth/stencil texture has a binding of {TextureBinding.ShaderResource}, but has a view flags of {viewFlags}.  The view will not be bindable to the shader pipeline.",
                                       LoggingLevel.Simple);
                }
            }
            else if (viewFlags != DepthStencilViewFlags.None)
            {
                // Do this to notify the user that something is amiss.
                graphics.Log.Print($"WARNING: Depth Stencil View {info.Name} - Depth/stencil view flag(s) are set to {viewFlags}, but the texture lacks a {TextureBinding.ShaderResource} binding.",
                                   LoggingLevel.Simple);
            }

            var newInfo = new GorgonTexture2DInfo(info)
            {
                // Can't see a reason to use anything other than default for dsvs
                Usage   = ResourceUsage.Default,
                Binding = binding
            };

            BufferFormat depthStencilFormat = newInfo.Format;

            if (((binding & TextureBinding.ShaderResource) == TextureBinding.ShaderResource) &&
                (viewFlags != DepthStencilViewFlags.None))
            {
                switch (newInfo.Format)
                {
                case BufferFormat.R32G8X24_Typeless:
                    depthStencilFormat = BufferFormat.D32_Float_S8X24_UInt;
                    break;

                case BufferFormat.R24G8_Typeless:
                    depthStencilFormat = BufferFormat.D24_UNorm_S8_UInt;
                    break;

                case BufferFormat.R16_Typeless:
                    depthStencilFormat = BufferFormat.D16_UNorm;
                    break;

                case BufferFormat.R32_Typeless:
                    depthStencilFormat = BufferFormat.D32_Float;
                    break;
                }
            }

            var texture = new GorgonTexture2D(graphics, newInfo);
            GorgonDepthStencil2DView result = texture.GetDepthStencilView(depthStencilFormat, flags: viewFlags);

            result.OwnsResource = true;

            return(result);
        }