/// <summary>
        /// Creates a <see cref="RenderTargetView"/> with the specified width, height, and <see cref="Format"/>.
        /// The render target is also bound as a shader resource (BindFlags.ShaderResource) and a <see cref="ShaderResourceView"/> is created.
        /// </summary>
        /// <param name="device">The current <see cref="Device"/> being used.</param>
        /// <param name="width">The width of the <see cref="RenderTargetView"/> being created.</param>
        /// <param name="height">The height of the <see cref="RenderTargetView"/> being created.</param>
        /// <param name="format">
        /// The <see cref="Format"/> of the <see cref="RenderTargetView"/> being created.  The device is queried for support of the given format.
        /// If the current hardware doesn't support the format, then a <see cref="NotSupportedException"/> is thrown.
        /// </param>
        /// <param name="targetTexture">The newly created <see cref="Texture2D"/> that is the backing resource for the <see cref="RenderTargetView"/>.</param>
        /// <param name="shaderView">The newly created <see cref="ShaderResourceView"/> that can be used to bind the render target as input to a shader.</param>
        /// <returns>The newly created <see cref="RenderTargetView"/> object.</returns>
        public static RenderTargetView CreateRenderTarget(Device device, int width, int height, Format format, out Texture2D targetTexture, out ShaderResourceView shaderView)
        {
            FormatSupport support = device.CheckFormatSupport(format);

            if ((support & FormatSupport.RenderTarget) != FormatSupport.RenderTarget)
            {
                throw new NotSupportedException("The given format (" + format.ToString() + ") is not supported as a RenderTarget on this GPU.");
            }

            targetTexture = CreateTexture2D(device, width, height, format, BindFlags.RenderTarget | BindFlags.ShaderResource);

            shaderView = new ShaderResourceView(device, targetTexture);

            // Is this necessary?  It seems to work fine without it.
            //RenderTargetViewDescription rtViewDesc = new RenderTargetViewDescription();
            //rtViewDesc.ArraySize = 1;
            //rtViewDesc.DepthSliceCount = 1;
            //rtViewDesc.Dimension = RenderTargetViewDimension.Texture2D;
            //rtViewDesc.ElementOffset = 0;
            //rtViewDesc.ElementWidth = 0;
            //rtViewDesc.FirstArraySlice = 0;
            //rtViewDesc.FirstDepthSlice = 0;
            //rtViewDesc.Format = format;
            //rtViewDesc.MipSlice = 0;

            return(new RenderTargetView(device, targetTexture));
        }
        /// <summary>
        /// Creates an <see cref="UnorderedAccessView"/> with the specified width, height, and <see cref="Format"/>.
        /// </summary>
        /// <param name="device">The current <see cref="Device"/> being used.</param>
        /// <param name="width">The width of the <see cref="UnorderedAccessView"/> being created.</param>
        /// <param name="height">The height of the <see cref="UnorderedAccessView"/> being created.</param>
        /// <param name="format">
        /// The <see cref="Format"/> of the <see cref="UnorderedAccessView"/> being created.  The device is queried for support of the given format.
        /// If the current hardware doesn't support the format, then a <see cref="NotSupportedException"/> is thrown.
        /// </param>
        /// <param name="unorderedAccessViewTexture">The newly created <see cref="Texture2D"/> that is the backing resource for the <see cref="UnorderedAccessView"/>.</param>
        /// <returns>The newly created <see cref="UnorderedAccessView"/> object.</returns>
        public static UnorderedAccessView CreateUnorderedAccessView(Device device, int width, int height, Format format, out Texture2D unorderedAccessViewTexture)
        {
            FormatSupport support = device.CheckFormatSupport(format);

            if ((support & FormatSupport.UnorderedAccessView) != FormatSupport.UnorderedAccessView)
            {
                throw new NotSupportedException("The given format (" + format.ToString() + ") is not supported as an UnorderedAccessView on this GPU.");
            }

            unorderedAccessViewTexture = CreateTexture2D(device, width, height, format, BindFlags.UnorderedAccess);

            // Is this necessary?
            //UnorderedAccessViewDescription uavViewDesc = new UnorderedAccessViewDescription();
            //uavViewDesc.ArraySize = 1;
            //uavViewDesc.Dimension = DepthStencilViewDimension.Texture2D;
            //uavViewDesc.FirstArraySlice = 0;
            //uavViewDesc.Flags = DepthStencilViewFlags.None;
            //uavViewDesc.Format = format;
            //uavViewDesc.MipSlice = 0;

            return(new UnorderedAccessView(device, unorderedAccessViewTexture));
        }
        /// <summary>
        /// Creates a <see cref="DepthStencilView"/> with the specified width, height, and <see cref="Format"/>.
        /// </summary>
        /// <param name="device">The current <see cref="Device"/> being used.</param>
        /// <param name="width">The width of the <see cref="DepthStencilView"/> being created.</param>
        /// <param name="height">The height of the <see cref="DepthStencilView"/> being created.</param>
        /// <param name="format">
        /// The <see cref="Format"/> of the <see cref="DepthStencilView"/> being created.  The device is queried for support of the given format.
        /// If the current hardware doesn't support the format, then a <see cref="NotSupportedException"/> is thrown.
        /// </param>
        /// <param name="depthStencilTexture">The newly created <see cref="Texture2D"/> that is the backing resource for the <see cref="DepthStencilView"/>.</param>
        /// <returns>The newly created <see cref="DepthStencilView"/> object.</returns>
        public static DepthStencilView CreateDepthStencilView(Device device, int width, int height, Format format, out Texture2D depthStencilTexture)
        {
            FormatSupport support = device.CheckFormatSupport(format);

            if ((support & FormatSupport.DepthStencil) != FormatSupport.DepthStencil)
            {
                throw new NotSupportedException("The given format (" + format.ToString() + ") is not supported as a DepthStencil on this GPU.");
            }

            depthStencilTexture = CreateTexture2D(device, width, height, format, BindFlags.DepthStencil);

            // Is this necessary?  It seems to work fine without it.
            //DepthStencilViewDescription dsViewDesc = new DepthStencilViewDescription();
            //dsViewDesc.ArraySize = 1;
            //dsViewDesc.Dimension = DepthStencilViewDimension.Texture2D;
            //dsViewDesc.FirstArraySlice = 0;
            //dsViewDesc.Flags = DepthStencilViewFlags.None;
            //dsViewDesc.Format = format;
            //dsViewDesc.MipSlice = 0;

            return(new DepthStencilView(device, depthStencilTexture));
        }
        /// <summary>
        /// Creates a <see cref="Texture2D"/> with the specified width, height, <see cref="Format"/>, and <see cref="BindFlags"/>.
        /// It uses default values of CpuAccessFlags.None, ResourceOptionFlags.None, and ResourceUsage.Default
        /// </summary>
        /// <param name="device">The current <see cref="Device"/> being used.</param>
        /// <param name="width">The width of the <see cref="Texture2D"/> being created.</param>
        /// <param name="height">The height of the <see cref="Texture2D"/> being created.</param>
        /// <param name="format">
        /// The <see cref="Format"/> of the <see cref="Texture2D"/> being created.  The device is queried for support of the given format.
        /// If the current hardware doesn't support the format, then a <see cref="NotSupportedException"/> is thrown.
        /// </param>
        /// <param name="bindFlags">The <see cref="BindFlags"/> of the <see cref="Texture2D"/> being created.</param>
        /// <returns>The newly created <see cref="Texture2D"/> object.</returns>
        public static Texture2D CreateTexture2D(Device device, int width, int height, Format format, BindFlags bindFlags)
        {
            FormatSupport support = device.CheckFormatSupport(format);

            if ((support & FormatSupport.Texture2D) != FormatSupport.Texture2D)
            {
                throw new NotSupportedException("The given format (" + format.ToString() + ") is not supported as a Texture2D on this GPU.");
            }

            Texture2DDescription texDesc = new Texture2DDescription();

            texDesc.ArraySize         = 1;
            texDesc.BindFlags         = bindFlags;
            texDesc.CpuAccessFlags    = CpuAccessFlags.None;
            texDesc.Format            = format;
            texDesc.Height            = height;
            texDesc.MipLevels         = 1;
            texDesc.OptionFlags       = ResourceOptionFlags.None;
            texDesc.SampleDescription = new SampleDescription(1, 0);
            texDesc.Usage             = ResourceUsage.Default;
            texDesc.Width             = width;

            return(new Texture2D(device, texDesc));
        }
Example #5
0
 /// <summary>
 /// Checks if a format is supported for a specific usage
 /// </summary>
 /// <param name="dev">Device to check</param>
 /// <param name="usage">Desired format usage</param>
 /// <param name="format">Desired format</param>
 /// <returns>true if format supported, false otherwise</returns>
 public bool IsSupported(Device dev, FormatSupport usage, Format format)
 {
     FormatSupport support = dev.CheckFormatSupport(format);
     return (support | usage) == support;
 }
Example #6
0
        /// <summary>
        /// Checks if a format is supported for a specific usage
        /// </summary>
        /// <param name="dev">Device to check</param>
        /// <param name="usage">Desired format usage</param>
        /// <param name="format">Desired format</param>
        /// <returns>true if format supported, false otherwise</returns>
        public bool IsSupported(Device dev, FormatSupport usage, Format format)
        {
            FormatSupport support = dev.CheckFormatSupport(format);

            return((support | usage) == support);
        }