Improved handling of disposed stuff

This commit is contained in:
Darth Affe 2023-12-15 10:21:38 +01:00
parent 6cd523d6ee
commit 1c01ab0dae
3 changed files with 46 additions and 1 deletions

View File

@ -12,12 +12,22 @@ public sealed unsafe class Image : IDisposable
#region Properties & Fields #region Properties & Fields
private bool _disposed;
private readonly byte* _imagePtr; private readonly byte* _imagePtr;
public int Width { get; } public int Width { get; }
public int Height { get; } public int Height { get; }
public ReadOnlySpan<byte> Data => new(_imagePtr, Width * Height * BPP); public ReadOnlySpan<byte> Data
{
get
{
ObjectDisposedException.ThrowIf(_disposed, this);
return new ReadOnlySpan<byte>(_imagePtr, Width * Height * BPP);
}
}
#endregion #endregion
@ -38,9 +48,12 @@ public sealed unsafe class Image : IDisposable
public void Dispose() public void Dispose()
{ {
if (_disposed) return;
Native.stable_diffusion_free_buffer(_imagePtr); Native.stable_diffusion_free_buffer(_imagePtr);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
_disposed = true;
} }
#endregion #endregion

View File

@ -7,6 +7,8 @@ public sealed unsafe class StableDiffusionModel : IDisposable
{ {
#region Properties & Fields #region Properties & Fields
private bool _disposed;
private readonly string _modelPath; private readonly string _modelPath;
private readonly ModelParameter _parameter; private readonly ModelParameter _parameter;
@ -41,12 +43,16 @@ public sealed unsafe class StableDiffusionModel : IDisposable
public Image TextToImage(string prompt, StableDiffusionParameter parameter) public Image TextToImage(string prompt, StableDiffusionParameter parameter)
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
byte* result = Native.stable_diffusion_predict_image(_ctx, parameter.ParamPtr, prompt); byte* result = Native.stable_diffusion_predict_image(_ctx, parameter.ParamPtr, prompt);
return new Image(result, parameter.Width, parameter.Height); return new Image(result, parameter.Width, parameter.Height);
} }
public Image ImageToImage(string prompt, Span<byte> image, StableDiffusionParameter parameter) public Image ImageToImage(string prompt, Span<byte> image, StableDiffusionParameter parameter)
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
fixed (byte* imagePtr = image) fixed (byte* imagePtr = image)
{ {
byte* result = Native.stable_diffusion_image_predict_image(_ctx, parameter.ParamPtr, imagePtr, prompt); byte* result = Native.stable_diffusion_image_predict_image(_ctx, parameter.ParamPtr, imagePtr, prompt);
@ -56,9 +62,12 @@ public sealed unsafe class StableDiffusionModel : IDisposable
public void Dispose() public void Dispose()
{ {
if (_disposed) return;
Native.stable_diffusion_free(_ctx); Native.stable_diffusion_free(_ctx);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
_disposed = true;
} }
public static string GetSystemInfo() => Native.stable_diffusion_get_system_info(); public static string GetSystemInfo() => Native.stable_diffusion_get_system_info();

View File

@ -6,6 +6,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
{ {
#region Properties & Fields #region Properties & Fields
private bool _disposed;
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type #pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
internal readonly Native.stable_diffusion_full_params* ParamPtr; internal readonly Native.stable_diffusion_full_params* ParamPtr;
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
@ -16,6 +18,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _negativePrompt; get => _negativePrompt;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_negativePrompt = value; _negativePrompt = value;
Native.stable_diffusion_full_params_set_negative_prompt(ParamPtr, _negativePrompt); Native.stable_diffusion_full_params_set_negative_prompt(ParamPtr, _negativePrompt);
} }
@ -27,6 +31,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _cfgScale; get => _cfgScale;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_cfgScale = value; _cfgScale = value;
Native.stable_diffusion_full_params_set_cfg_scale(ParamPtr, _cfgScale); Native.stable_diffusion_full_params_set_cfg_scale(ParamPtr, _cfgScale);
} }
@ -38,6 +44,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _width; get => _width;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_width = value; _width = value;
Native.stable_diffusion_full_params_set_width(ParamPtr, _width); Native.stable_diffusion_full_params_set_width(ParamPtr, _width);
} }
@ -49,6 +57,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _height; get => _height;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_height = value; _height = value;
Native.stable_diffusion_full_params_set_height(ParamPtr, _height); Native.stable_diffusion_full_params_set_height(ParamPtr, _height);
} }
@ -60,6 +70,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _sampleMethod; get => _sampleMethod;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_sampleMethod = value; _sampleMethod = value;
Native.stable_diffusion_full_params_set_sample_method(ParamPtr, _sampleMethod.GetNativeName() ?? "EULER_A"); Native.stable_diffusion_full_params_set_sample_method(ParamPtr, _sampleMethod.GetNativeName() ?? "EULER_A");
} }
@ -71,6 +83,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _sampleSteps; get => _sampleSteps;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_sampleSteps = value; _sampleSteps = value;
Native.stable_diffusion_full_params_set_sample_steps(ParamPtr, _sampleSteps); Native.stable_diffusion_full_params_set_sample_steps(ParamPtr, _sampleSteps);
} }
@ -82,6 +96,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _seed; get => _seed;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_seed = value; _seed = value;
Native.stable_diffusion_full_params_set_seed(ParamPtr, _seed); Native.stable_diffusion_full_params_set_seed(ParamPtr, _seed);
} }
@ -93,6 +109,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _batchCount; get => _batchCount;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_batchCount = value; _batchCount = value;
Native.stable_diffusion_full_params_set_batch_count(ParamPtr, _batchCount); Native.stable_diffusion_full_params_set_batch_count(ParamPtr, _batchCount);
} }
@ -104,6 +122,8 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
get => _strength; get => _strength;
set set
{ {
ObjectDisposedException.ThrowIf(_disposed, this);
_strength = value; _strength = value;
Native.stable_diffusion_full_params_set_strength(ParamPtr, _strength); Native.stable_diffusion_full_params_set_strength(ParamPtr, _strength);
} }
@ -138,9 +158,12 @@ public sealed unsafe class StableDiffusionParameter : IDisposable
public void Dispose() public void Dispose()
{ {
if (_disposed) return;
Native.stable_diffusion_free_full_params(ParamPtr); Native.stable_diffusion_free_full_params(ParamPtr);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
_disposed = true;
} }
#endregion #endregion