using NativeEngine; namespace Sandbox; /// /// Essentially wraps a couple of textures that we're going to render to. The color texture and the depth texture. /// public sealed partial class RenderTarget : IDisposable { /// /// Is this currently loaned out (Active) /// internal bool Loaned { get; set; } /// /// The amount of time since this texture was last used /// internal int FramesSinceUsed { get; set; } /// /// The hash of the parameters used to create this /// internal int CreationHash { get; set; } /// /// Width of the render target /// public int Width { get; internal set; } /// /// Height of the render target /// public int Height { get; internal set; } /// /// The target colour texture /// public Texture ColorTarget { get; internal set; } /// /// The target depth texture /// public Texture DepthTarget { get; internal set; } // Private - Only way to get a valid render target should be with RenderTarget.From private RenderTarget() { } /// /// Stop using this texture, return it to the pool /// public void Dispose() { if ( !Loaned ) return; Return( this ); } /// /// Destroy this buffer. It shouldn't be used anymore after this. /// internal void Destroy() { //Log.Info( $"Freeing RT {this}" ); ColorTarget?.Dispose(); ColorTarget = null; DepthTarget?.Dispose(); DepthTarget = null; } public override string ToString() { return $"RenderTarget_{Width}x{Height}"; } /// /// Create a render target from these textures /// public static RenderTarget From( Texture color, Texture depth = null ) { if ( color is not null && !color.IsRenderTarget ) { throw new ArgumentException( "Texture was not created as a render target (Texture.CreateRenderTarget)", nameof( color ) ); } if ( depth is not null && !depth.IsRenderTarget ) { throw new ArgumentException( "Texture was not created as a render target (Texture.CreateRenderTarget)", nameof( depth ) ); } return new RenderTarget { ColorTarget = color, DepthTarget = depth, Width = color.Width, Height = color.Height, }; } // Can't use operator overloads because of the implicit conversion internal Rendering.SceneViewRenderTargetHandle ToColorHandle( ISceneView view ) { return view.FindOrCreateRenderTarget( CreationHash.ToString(), ColorTarget.native, 0 ); } internal Rendering.SceneViewRenderTargetHandle ToDepthHandle( ISceneView view ) { return view.FindOrCreateRenderTarget( DepthTarget.GetHashCode().ToString(), DepthTarget.native, 1 ); } }