示例#1
0
        /// <summary>
        /// Function to load a texture from a file.
        /// </summary>
        /// <param name="graphics">The graphics interface that will own the texture.</param>
        /// <param name="filePath">The path to the file.</param>
        /// <param name="codec">The codec that is used to decode the the data in the stream.</param>
        /// <param name="options">[Optional] Options used to further define the texture.</param>
        /// <returns>A new <see cref="GorgonTexture3DView"/></returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/>, <paramref name="filePath"/>, or the <paramref name="codec"/> parameter is <b>null</b>.</exception>
        /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="filePath"/> parameter is empty.</exception>
        /// <remarks>
        /// <para>
        /// This will load an <see cref="IGorgonImage"/> from a file on disk and put it into a <see cref="GorgonTexture3D"/> object and return a <see cref="GorgonTexture3DView"/>.
        /// </para>
        /// <para>
        /// If specified, the <paramref name="options"/>parameter will define how Gorgon and shaders should handle the texture.  The <see cref="GorgonTextureLoadOptions"/> type contains the following:
        /// <list type="bullet">
        ///		<item>
        ///			<term>Binding</term>
        ///			<description>When defined, will indicate the <see cref="TextureBinding"/> that defines how the texture will be bound to the graphics pipeline. If it is omitted, then the binding will be
        ///         <see cref="TextureBinding.ShaderResource"/>.</description>
        ///		</item>
        ///		<item>
        ///			<term>Usage</term>
        ///			<description>When defined, will indicate the preferred usage for the texture. If it is omitted, then the usage will be set to <see cref="ResourceUsage.Default"/>.</description>
        ///		</item>
        ///		<item>
        ///			<term>Multisample info</term>
        ///			<description>When defined (i.e. not <b>null</b>), defines the multisampling to apply to the texture. If omitted, then the default is <see cref="GorgonMultisampleInfo.NoMultiSampling"/>.</description>
        ///		</item>
        /// </list>
        /// </para>
        /// <para>
        /// Since the <see cref="GorgonTexture3D"/> created by this method is linked to the <see cref="GorgonTexture3DView"/> returned, disposal of either one will dispose of the other on your behalf. If
        /// the user created a <see cref="GorgonTexture3DView"/> from the <see cref="GorgonTexture3D.GetShaderResourceView"/> method on the <see cref="GorgonTexture3D"/>, 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>
        /// </remarks>
        public static GorgonTexture3DView FromFile(GorgonGraphics graphics, string filePath, IGorgonImageCodec codec, GorgonTextureLoadOptions options = null)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException(nameof(graphics));
            }

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

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentEmptyException(nameof(filePath));
            }

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

            using (IGorgonImage image = codec.LoadFromFile(filePath))
            {
                GorgonTexture3D     texture = image.ToTexture3D(graphics, options);
                GorgonTexture3DView view    = texture.GetShaderResourceView();
                view.OwnsResource = true;
                return(view);
            }
        }
示例#2
0
        public void Test3DStagingFail()
        {
            GorgonTexture3D texture = null;

            try
            {
                texture = _framework.Graphics.Textures.CreateTexture("Test3D",
                                                                     new GorgonTexture3DSettings
                {
                    Width    = 256,
                    Height   = 256,
                    Depth    = 64,
                    Format   = BufferFormat.R8G8B8A8,
                    MipCount = 1,
                    AllowUnorderedAccessViews = false,
                    Usage = BufferUsage.Staging
                });
                texture.GetShaderView(BufferFormat.R8G8B8A8_Int);
            }
            finally
            {
                if (texture != null)
                {
                    texture.Dispose();
                }
            }
        }
        /// <summary>Function to dispose any texture resources.</summary>
        protected override void OnDestroyTexture()
        {
            _texture?.Dispose();
            _textureView?.Dispose();

            _texture     = null;
            _textureView = null;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="GorgonRenderTarget3DView" /> class.
 /// </summary>
 /// <param name="texture">The render target texture to bind.</param>
 /// <param name="format">The format of the render target view.</param>
 /// <param name="formatInfo">The format information.</param>
 /// <param name="mipSlice">The mip slice to use in the view.</param>
 /// <param name="startDepthSlice">The first depth slice to use in the view.</param>
 /// <param name="depthSliceCount">The number of depth slices to use in the view.</param>
 internal GorgonRenderTarget3DView(GorgonTexture3D texture, BufferFormat format, GorgonFormatInfo formatInfo, int mipSlice, int startDepthSlice, int depthSliceCount)
     : base(texture, format, formatInfo)
 {
     Texture         = texture;
     MipSlice        = mipSlice;
     StartDepthSlice = startDepthSlice;
     DepthSliceCount = depthSliceCount;
     MipWidth        = (Width >> MipSlice).Max(1);
     MipHeight       = (Height >> MipSlice).Max(1);
     Bounds          = new DX.Rectangle(0, 0, Width, Height);
 }
示例#5
0
        /// <summary>
        /// Function to create a new texture 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 texture.</param>
        /// <param name="initialData">[Optional] Initial data used to populate the texture.</param>
        /// <returns>A new <see cref="GorgonTexture3DView"/>.</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="GorgonTexture3D"/> and a <see cref="GorgonTexture3DView"/> as a single object that users can use to apply a texture as a shader
        /// resource. This helps simplify creation of a texture by executing some prerequisite steps on behalf of the user.
        /// </para>
        /// <para>
        /// Since the <see cref="GorgonTexture3D"/> created by this method is linked to the <see cref="GorgonTexture3DView"/> returned, disposal of either one will dispose of the other on your behalf. If
        /// the user created a <see cref="GorgonTexture3DView"/> from the <see cref="GorgonTexture3D.GetShaderResourceView"/> method on the <see cref="GorgonTexture3D"/>, 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>
        /// If an <paramref name="initialData"/> image is provided, and the width/height/depth is not the same as the values in the <paramref name="info"/> parameter, then the image data will be cropped to
        /// match the values in the <paramref name="info"/> parameter. Things like array count, and mip levels will still be taken from the <paramref name="initialData"/> image parameter.
        /// </para>
        /// </remarks>
        /// <seealso cref="GorgonTexture3D"/>
        public static GorgonTexture3DView CreateTexture(GorgonGraphics graphics, IGorgonTexture3DInfo info, IGorgonImage initialData = null)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException(nameof(graphics));
            }

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

            var newInfo = new GorgonTexture3DInfo(info)
            {
                Usage   = info.Usage == ResourceUsage.Staging ? ResourceUsage.Default : info.Usage,
                Binding = (info.Binding & TextureBinding.ShaderResource) != TextureBinding.ShaderResource
                                            ? (info.Binding | TextureBinding.ShaderResource)
                                            : info.Binding
            };

            if (initialData != null)
            {
                if ((initialData.Width < info.Width) ||
                    (initialData.Height < info.Height) ||
                    (initialData.Depth < info.Depth))
                {
                    initialData = initialData.Expand(info.Width, info.Height, info.Depth);
                }

                if ((initialData.Width > info.Width) ||
                    (initialData.Height > info.Height) ||
                    (initialData.Depth > info.Depth))
                {
                    initialData = initialData.Crop(new DX.Rectangle(0, 0, info.Width, info.Height), info.Depth);
                }
            }

            GorgonTexture3D texture = initialData == null
                                          ? new GorgonTexture3D(graphics, newInfo)
                                          : initialData.ToTexture3D(graphics,
                                                                    new GorgonTextureLoadOptions
            {
                Usage   = newInfo.Usage,
                Binding = newInfo.Binding,
                Name    = newInfo.Name
            });

            GorgonTexture3DView result = texture.GetShaderResourceView();

            result.OwnsResource = true;

            return(result);
        }
        /// <summary>Function to create the texture for the view.</summary>
        /// <param name="graphics">The graphics interface used to create the texture.</param>
        /// <param name="imageData">The image data to create the texture from.</param>
        /// <param name="name">The name of the texture.</param>
        protected override void OnCreateTexture(GorgonGraphics graphics, IGorgonImage imageData, string name)
        {
            _texture = imageData.ToTexture3D(graphics, new GorgonTextureLoadOptions
            {
                Name    = name,
                Binding = TextureBinding.ShaderResource,
                Usage   = ResourceUsage.Immutable
            });

            _textureView = _texture.GetShaderResourceView();
            _volRenderer.AssignTexture(_textureView);
        }
示例#7
0
        /// <summary>
        /// Function to create a new texture that is bindable to the GPU as an unordered access resource.
        /// </summary>
        /// <param name="graphics">The graphics interface to use when creating the target.</param>
        /// <param name="info">The information about the texture.</param>
        /// <param name="initialData">[Optional] Initial data used to populate the texture.</param>
        /// <returns>A new <see cref="GorgonTexture3DReadWriteView"/>.</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="GorgonTexture3D"/> and a <see cref="GorgonTexture3DReadWriteView"/> as a single object that users can use to apply a texture as an unordered
        /// access resource. This helps simplify creation of a texture by executing some prerequisite steps on behalf of the user.
        /// </para>
        /// <para>
        /// Since the <see cref="GorgonTexture3D"/> created by this method is linked to the <see cref="GorgonTexture3DReadWriteView"/> returned, disposal of either one will dispose of the other on your behalf. If
        /// the user created a <see cref="GorgonTexture3DReadWriteView"/> from the <see cref="GorgonTexture3D.GetReadWriteView"/> method on the <see cref="GorgonTexture3D"/>, 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>
        /// If an <paramref name="initialData"/> image is provided, and the width/height/depth is not the same as the values in the <paramref name="info"/> parameter, then the image data will be cropped to
        /// match the values in the <paramref name="info"/> parameter. Things like array count, and mip levels will still be taken from the <paramref name="initialData"/> image parameter.
        /// </para>
        /// </remarks>
        /// <seealso cref="GorgonTexture3D"/>
        public static GorgonTexture3DReadWriteView CreateTexture(GorgonGraphics graphics, IGorgonTexture3DInfo info, IGorgonImage initialData = null)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException(nameof(graphics));
            }

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

            var newInfo = new GorgonTexture3DInfo(info)
            {
                Usage   = info.Usage == ResourceUsage.Staging ? ResourceUsage.Default : info.Usage,
                Binding = (((info.Binding & TextureBinding.ReadWriteView) != TextureBinding.ReadWriteView)
                                             ? (info.Binding | TextureBinding.ReadWriteView)
                                             : info.Binding) & ~TextureBinding.DepthStencil // There's now way we can build a depth/stencil from this method.
            };

            if (initialData != null)
            {
                if ((initialData.Width > info.Width) ||
                    (initialData.Height > info.Height) ||
                    (initialData.Depth > info.Depth))
                {
                    initialData = initialData.Expand(info.Width, info.Height, info.Depth);
                }

                if ((initialData.Width < info.Width) ||
                    (initialData.Height < info.Height) ||
                    (initialData.Depth < info.Depth))
                {
                    initialData = initialData.Crop(new DX.Rectangle(0, 0, info.Width, info.Height), info.Depth);
                }
            }

            GorgonTexture3D texture = initialData == null
                                          ? new GorgonTexture3D(graphics, newInfo)
                                          : initialData.ToTexture3D(graphics,
                                                                    new GorgonTextureLoadOptions
            {
                Usage   = newInfo.Usage,
                Binding = newInfo.Binding,
                Name    = newInfo.Name
            });

            GorgonTexture3DReadWriteView result = texture.GetReadWriteView();

            result.OwnsResource = true;

            return(result);
        }
示例#8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GorgonTexture3DView"/> class.
 /// </summary>
 /// <param name="texture">The <see cref="GorgonTexture3D"/> being viewed.</param>
 /// <param name="format">The format for the view.</param>
 /// <param name="formatInfo">The information about the view format.</param>
 /// <param name="firstMipLevel">The first mip level to view.</param>
 /// <param name="mipCount">The number of mip levels to view.</param>
 /// <exception cref="ArgumentNullException">Thrown when the <paramref name="texture"/>, or the <paramref name="formatInfo"/> parameter is <b>null</b>.</exception>
 internal GorgonTexture3DView(GorgonTexture3D texture,
                              BufferFormat format,
                              GorgonFormatInfo formatInfo,
                              int firstMipLevel,
                              int mipCount)
     : base(texture)
 {
     FormatInformation = formatInfo ?? throw new ArgumentNullException(nameof(formatInfo));
     Format            = format;
     Texture           = texture;
     MipSlice          = firstMipLevel;
     MipCount          = mipCount;
 }
示例#9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GorgonTexture3DReadWriteView"/> class.
 /// </summary>
 /// <param name="texture">The texture to view.</param>
 /// <param name="format">The format for the view.</param>
 /// <param name="formatInfo">Information about the format.</param>
 /// <param name="firstMipLevel">The first mip level to view.</param>
 /// <param name="startDepthSlice">The first depth slice to view.</param>
 /// <param name="depthSliceCount">The number of depth slices to view.</param>
 /// <exception cref="ArgumentNullException">Thrown when the <paramref name="texture"/>, or the <paramref name="formatInfo"/> parameter is <b>null</b>.</exception>
 internal GorgonTexture3DReadWriteView(GorgonTexture3D texture,
                                       BufferFormat format,
                                       GorgonFormatInfo formatInfo,
                                       int firstMipLevel,
                                       int startDepthSlice,
                                       int depthSliceCount)
     : base(texture)
 {
     FormatInformation = formatInfo ?? throw new ArgumentNullException(nameof(formatInfo));
     Format            = format;
     Texture           = texture;
     Bounds            = new DX.Rectangle(0, 0, Width, Height);
     MipSlice          = firstMipLevel;
     StartDepthSlice   = startDepthSlice;
     DepthSliceCount   = depthSliceCount;
 }
示例#10
0
        /// <summary>
        /// Function to load a texture from a <see cref="Stream"/>.
        /// </summary>
        /// <param name="graphics">The graphics interface that will own the texture.</param>
        /// <param name="stream">The stream containing the texture image data.</param>
        /// <param name="codec">The codec that is used to decode the the data in the stream.</param>
        /// <param name="size">[Optional] The size of the image in the stream, in bytes.</param>
        /// <param name="options">[Optional] Options used to further define the texture.</param>
        /// <returns>A new <see cref="GorgonTexture3DReadWriteView"/></returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/>, <paramref name="stream"/>, or the <paramref name="codec"/> parameter is <b>null</b>.</exception>
        /// <exception cref="IOException">Thrown if the <paramref name="stream"/> is write only.</exception>
        /// <exception cref="EndOfStreamException">Thrown if reading the image would move beyond the end of the <paramref name="stream"/>.</exception>
        /// <remarks>
        /// <para>
        /// This will load an <see cref="IGorgonImage"/> from a <paramref name="stream"/> and put it into a <see cref="GorgonTexture3D"/> object and return a <see cref="GorgonTexture3DReadWriteView"/>.
        /// </para>
        /// <para>
        /// If the <paramref name="size"/> option is specified, then the method will read from the stream up to that number of bytes, so it is up to the user to provide an accurate size. If it is omitted
        /// then the <c>stream length - stream position</c> is used as the total size.
        /// </para>
        /// <para>
        /// If specified, the <paramref name="options"/>parameter will define how Gorgon and shaders should handle the texture.  The <see cref="GorgonTextureLoadOptions"/> type contains the following:
        /// <list type="bullet">
        ///		<item>
        ///			<term>Binding</term>
        ///			<description>When defined, will indicate the <see cref="TextureBinding"/> that defines how the texture will be bound to the graphics pipeline. If it is omitted, then the binding will be
        ///         <see cref="TextureBinding.ShaderResource"/>.</description>
        ///		</item>
        ///		<item>
        ///			<term>Usage</term>
        ///			<description>When defined, will indicate the preferred usage for the texture. If it is omitted, then the usage will be set to <see cref="ResourceUsage.Default"/>.</description>
        ///		</item>
        ///		<item>
        ///			<term>Multisample info</term>
        ///			<description>When defined (i.e. not <b>null</b>), defines the multisampling to apply to the texture. If omitted, then the default is <see cref="GorgonMultisampleInfo.NoMultiSampling"/>.</description>
        ///		</item>
        /// </list>
        /// </para>
        /// <para>
        /// Since the <see cref="GorgonTexture3D"/> created by this method is linked to the <see cref="GorgonTexture3DReadWriteView"/> returned, disposal of either one will dispose of the other on your behalf. If
        /// the user created a <see cref="GorgonTexture3DReadWriteView"/> from the <see cref="GorgonTexture3D.GetShaderResourceView"/> method on the <see cref="GorgonTexture3D"/>, 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>
        /// </remarks>
        public static GorgonTexture3DReadWriteView FromStream(GorgonGraphics graphics,
                                                              Stream stream,
                                                              IGorgonImageCodec codec,
                                                              long?size = null,
                                                              GorgonTextureLoadOptions options = null)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException(nameof(graphics));
            }

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

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

            if (!stream.CanRead)
            {
                throw new IOException(Resources.GORGFX_ERR_STREAM_WRITE_ONLY);
            }

            if (size == null)
            {
                size = stream.Length - stream.Position;
            }

            if ((stream.Length - stream.Position) < size)
            {
                throw new EndOfStreamException();
            }

            using (IGorgonImage image = codec.LoadFromStream(stream, size))
            {
                GorgonTexture3D texture           = image.ToTexture3D(graphics, options);
                GorgonTexture3DReadWriteView view = texture.GetReadWriteView();
                view.OwnsResource = true;
                return(view);
            }
        }
示例#11
0
        /// <summary>
        /// Function to create a new render target 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 texture.</param>
        /// <returns>A new <see cref="GorgonRenderTarget3DView"/>.</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="GorgonTexture3D"/> and a <see cref="GorgonRenderTarget3DView"/> as a single object that users can use to apply a render target 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="GorgonTexture3D"/> created by this method is linked to the <see cref="GorgonRenderTarget3DView"/> returned, disposal of either one will dispose of the other on your behalf.
        /// If the user created a <see cref="GorgonRenderTarget3DView"/> from the <see cref="GorgonTexture3D.GetRenderTargetView"/> method on the <see cref="GorgonTexture3D"/>, 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>
        /// </remarks>
        /// <seealso cref="GorgonTexture3D"/>
        public static GorgonRenderTarget3DView CreateRenderTarget(GorgonGraphics graphics, IGorgonTexture3DInfo info)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException(nameof(graphics));
            }

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

            TextureBinding binding = TextureBinding.RenderTarget;

            if ((info.Binding & TextureBinding.ShaderResource) == TextureBinding.ShaderResource)
            {
                binding |= TextureBinding.ShaderResource;
            }

            if ((info.Binding & TextureBinding.ShaderResource) == TextureBinding.ReadWriteView)
            {
                binding |= TextureBinding.ReadWriteView;
            }

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

            var texture = new GorgonTexture3D(graphics, newInfo);
            GorgonRenderTarget3DView result = texture.GetRenderTargetView();

            result.OwnsResource = true;

            return(result);
        }
示例#12
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public override void Dispose()
 {
     Texture = null;
     base.Dispose();
 }
示例#13
0
        public void TestAutoSRV()
        {
            GorgonTexture1D _1D = null;
            GorgonTexture2D _2D = null;
            GorgonTexture3D _3D = null;

            try
            {
                _1D = _framework.Graphics.Textures.CreateTexture("Test1D",
                                                                 new GorgonTexture1DSettings
                {
                    Width                     = 256,
                    ArrayCount                = 1,
                    Format                    = BufferFormat.R8_UIntNormal,
                    MipCount                  = 1,
                    ShaderViewFormat          = BufferFormat.Unknown,
                    AllowUnorderedAccessViews = false,
                    Usage                     = BufferUsage.Default
                });

                Assert.IsNotNull((GorgonShaderView)_1D);
                Assert.AreEqual(_1D.Settings.Format, ((GorgonShaderView)_1D).Format);

                _2D = _framework.Graphics.Textures.CreateTexture("Test2D",
                                                                 new GorgonTexture2DSettings
                {
                    Width                     = 256,
                    Height                    = 256,
                    ArrayCount                = 1,
                    Format                    = BufferFormat.R8G8B8A8_UIntNormal,
                    MipCount                  = 1,
                    ShaderViewFormat          = BufferFormat.Unknown,
                    AllowUnorderedAccessViews = false,
                    Usage                     = BufferUsage.Default
                });

                Assert.IsNotNull((GorgonShaderView)_2D);
                Assert.AreEqual(_2D.Settings.Format, ((GorgonShaderView)_2D).Format);

                _3D = _framework.Graphics.Textures.CreateTexture("Test3D",
                                                                 new GorgonTexture3DSettings
                {
                    Width                     = 256,
                    Height                    = 256,
                    Depth                     = 64,
                    Format                    = BufferFormat.R8G8B8A8_UIntNormal,
                    MipCount                  = 1,
                    ShaderViewFormat          = BufferFormat.Unknown,
                    AllowUnorderedAccessViews = false,
                    Usage                     = BufferUsage.Default
                });

                Assert.IsNotNull((GorgonShaderView)_3D);
                Assert.AreEqual(_3D.Settings.Format, ((GorgonShaderView)_3D).Format);
            }
            finally
            {
                if (_1D != null)
                {
                    _1D.Dispose();
                }

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

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