using System; using RefreshCS; namespace MoonWorks.Graphics { /// /// A render target is a structure that wraps a texture so that it can be rendered to. /// public class RenderTarget : GraphicsResource { public TextureSlice TextureSlice { get; } public TextureFormat Format => TextureSlice.Texture.Format; protected override Action QueueDestroyFunction => Refresh.Refresh_QueueDestroyRenderTarget; /// /// Creates a render target backed by a texture. /// /// An initialized GraphicsDevice. /// The width of the render target. /// The height of the render target. /// The format of the render target. /// Whether the render target can be used by a sampler. /// The multisample count of the render target. /// The mip level of the render target. /// public static RenderTarget CreateBackedRenderTarget( GraphicsDevice device, uint width, uint height, TextureFormat format, bool canBeSampled, SampleCount sampleCount = SampleCount.One, uint levelCount = 1 ) { TextureUsageFlags flags = 0; if ( format == TextureFormat.D16 || format == TextureFormat.D32 || format == TextureFormat.D16S8 || format == TextureFormat.D32S8 ) { flags |= TextureUsageFlags.DepthStencilTarget; } else { flags |= TextureUsageFlags.ColorTarget; } if (canBeSampled) { flags |= TextureUsageFlags.Sampler; } var texture = Texture.CreateTexture2D( device, width, height, format, flags, sampleCount, levelCount ); return new RenderTarget(device, new TextureSlice(texture), sampleCount); } /// /// Creates a render target using a texture slice and an optional sample count. /// /// An initialized GraphicsDevice. /// The texture slice that will be rendered to. /// The desired multisample count of the render target. public RenderTarget( GraphicsDevice device, in TextureSlice textureSlice, SampleCount sampleCount = SampleCount.One ) : base(device) { Handle = Refresh.Refresh_CreateRenderTarget( device.Handle, textureSlice.ToRefreshTextureSlice(), (Refresh.SampleCount) sampleCount ); TextureSlice = textureSlice; } } }