diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/ContouredLayersRunner.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/ContouredLayersRunner.cs index 87a93399..74480aa4 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/ContouredLayersRunner.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/ContouredLayersRunner.cs @@ -25,14 +25,17 @@ public sealed class ContouredLayersRunner : IShaderRunner /// public bool TryExecute(IReadWriteTexture2D texture, TimeSpan timespan, object? parameter) { - if (this.texture is null) + if (this.texture is null || + this.texture.GraphicsDevice != texture.GraphicsDevice) { string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.png"); - this.texture = GraphicsDevice.Default.LoadReadOnlyTexture2D(filename); + this.texture?.Dispose(); + + this.texture = texture.GraphicsDevice.LoadReadOnlyTexture2D(filename); } - GraphicsDevice.Default.ForEach(texture, new ContouredLayers((float)timespan.TotalSeconds, this.texture)); + texture.GraphicsDevice.ForEach(texture, new ContouredLayers((float)timespan.TotalSeconds, this.texture)); return true; } diff --git a/src/ComputeSharp.UI/ShaderRunner{T}.cs b/src/ComputeSharp.UI/ShaderRunner{T}.cs index 98b84b35..967e8713 100644 --- a/src/ComputeSharp.UI/ShaderRunner{T}.cs +++ b/src/ComputeSharp.UI/ShaderRunner{T}.cs @@ -61,11 +61,11 @@ public sealed class ShaderRunner : IShaderRunner { if (this.shaderFactory is Func shaderFactory) { - GraphicsDevice.Default.ForEach(texture, this.shaderFactory(time)); + texture.GraphicsDevice.ForEach(texture, this.shaderFactory(time)); } else { - GraphicsDevice.Default.ForEach(texture, this.statefulShaderFactory!(time, parameter)); + texture.GraphicsDevice.ForEach(texture, this.statefulShaderFactory!(time, parameter)); } return true; diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 06992782..e15f3cea 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -24,7 +24,7 @@ namespace ComputeSharp.Resources; /// A representing a typed buffer stored on GPU memory. /// /// The type of items stored on the buffer. -public unsafe abstract class Buffer : NativeObject +public unsafe abstract class Buffer : NativeObject, IGraphicsResource where T : unmanaged { #if NET6_0_OR_GREATER @@ -122,9 +122,7 @@ public unsafe abstract class Buffer : NativeObject this.d3D12Resource.Get()->SetName(this); } - /// - /// Gets the associated with the current instance. - /// + /// public GraphicsDevice GraphicsDevice { get; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 3b828db5..2e5f35b1 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -24,7 +24,7 @@ namespace ComputeSharp.Resources; /// A representing a typed 2D texture stored on GPU memory. /// /// The type of items stored on the texture. -public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper.IGraphicsResource +public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { #if NET6_0_OR_GREATER @@ -132,9 +132,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper this.d3D12Resource.Get()->SetName(this); } - /// - /// Gets the associated with the current instance. - /// + /// public GraphicsDevice GraphicsDevice { get; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 55d913df..ddfda4af 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -24,7 +24,7 @@ namespace ComputeSharp.Resources; /// A representing a typed 3D texture stored on GPU memory. /// /// The type of items stored on the texture. -public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper.IGraphicsResource +public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { #if NET6_0_OR_GREATER @@ -137,9 +137,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper this.d3D12Resource.Get()->SetName(this); } - /// - /// Gets the associated with the current instance. - /// + /// public GraphicsDevice GraphicsDevice { get; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 04bb5bfd..b9478f5f 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -15,7 +15,7 @@ namespace ComputeSharp.Resources; /// A representing a typed buffer stored on CPU memory, that can be used to transfer data to/from the GPU. /// /// The type of items stored on the buffer. -public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner +public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource, IMemoryOwner where T : unmanaged { #if NET6_0_OR_GREATER @@ -68,9 +68,7 @@ public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner this.d3D12Resource.Get()->SetName(this); } - /// - /// Gets the associated with the current instance. - /// + /// public GraphicsDevice GraphicsDevice { get; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index dd94a193..24d274b2 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -15,7 +15,7 @@ namespace ComputeSharp.Resources; /// A representing a typed 2D texture stored on on CPU memory, that can be used to transfer data to/from the GPU. /// /// The type of items stored on the texture. -public unsafe abstract class TransferTexture2D : NativeObject +public unsafe abstract class TransferTexture2D : NativeObject, IGraphicsResource where T : unmanaged { #if NET6_0_OR_GREATER @@ -84,9 +84,7 @@ public unsafe abstract class TransferTexture2D : NativeObject this.d3D12Resource.Get()->SetName(this); } - /// - /// Gets the associated with the current instance. - /// + /// public GraphicsDevice GraphicsDevice { get; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 20c7adb9..c939db0a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -15,7 +15,7 @@ namespace ComputeSharp.Resources; /// A representing a typed 3D texture stored on on CPU memory, that can be used to transfer data to/from the GPU. /// /// The type of items stored on the texture. -public unsafe abstract class TransferTexture3D : NativeObject +public unsafe abstract class TransferTexture3D : NativeObject, IGraphicsResource where T : unmanaged { #if NET6_0_OR_GREATER @@ -87,9 +87,7 @@ public unsafe abstract class TransferTexture3D : NativeObject this.d3D12Resource.Get()->SetName(this); } - /// - /// Gets the associated with the current instance. - /// + /// public GraphicsDevice GraphicsDevice { get; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Interfaces/IGraphicsResource.cs b/src/ComputeSharp/Graphics/Resources/Interfaces/IGraphicsResource.cs new file mode 100644 index 00000000..b55614d9 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/Interfaces/IGraphicsResource.cs @@ -0,0 +1,12 @@ +namespace ComputeSharp; + +/// +/// An interface representing a graphics resource associated to a given instance. +/// +public interface IGraphicsResource +{ + /// + /// Gets the instance associated with the current resource. + /// + GraphicsDevice GraphicsDevice { get; } +} diff --git a/src/ComputeSharp/Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs b/src/ComputeSharp/Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs index 085a81c8..367d8dd1 100644 --- a/src/ComputeSharp/Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs @@ -5,7 +5,7 @@ /// This interface can only be used to wrap instances. /// /// The type of normalized pixels used on the GPU side. -public interface IReadOnlyTexture2D +public interface IReadOnlyTexture2D : IGraphicsResource where TPixel : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs b/src/ComputeSharp/Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs index 04625bd5..7a92ae62 100644 --- a/src/ComputeSharp/Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs @@ -5,7 +5,7 @@ /// This interface can only be used to wrap instances. /// /// The type of normalized pixels used on the GPU side. -public interface IReadOnlyTexture3D +public interface IReadOnlyTexture3D : IGraphicsResource where TPixel : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs b/src/ComputeSharp/Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs index aaa76334..e9938395 100644 --- a/src/ComputeSharp/Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs @@ -5,7 +5,7 @@ /// This interface can only be used to wrap instances. /// /// The type of normalized pixels used on the GPU side. -public interface IReadWriteTexture2D +public interface IReadWriteTexture2D : IGraphicsResource where TPixel : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs b/src/ComputeSharp/Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs index af43c3dc..746fec0d 100644 --- a/src/ComputeSharp/Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs @@ -5,7 +5,7 @@ /// This interface can only be used to wrap instances. /// /// The type of normalized pixels used on the GPU side. -public interface IReadWriteTexture3D +public interface IReadWriteTexture3D : IGraphicsResource where TPixel : unmanaged { ///