Nominom / BCnEncoder.NET

Cross-platform texture encoding libary for .NET. With support for BC1-3/DXT, BC4-5/RGTC and BC6-7/BPTC compression. Outputs files in ktx or dds formats.
The Unlicense
108 stars 16 forks source link

Async API #16

Closed onepiecefreak3 closed 3 years ago

onepiecefreak3 commented 3 years ago

As discussed in issue #15 an async api would benefit consuming programs of this library to design non-blocking UI's while decoding and encoding processes are running. Currently all classes seem to be thread-safe already, so it would just suffice to offer Async methods that wrap starting the async operation. This needs further investigation though.

onepiecefreak3 commented 3 years ago

As per microsoft's documentation on Span and Memory, it seems to be acknowledged that Span should be used for synchronous API's, while Memory is the way to go for asynchronous API's. Due to how Span and Memory are implemented, it is not possible to use Span for the conversion from synchronous to asynchronous anyways. Therefore, methods that currently take in a ReadOnlySpan as the data, will get an Async overload that takes in a ReadOnlyMemory instead. Calling upon the Task.Factory I will pass the content of ReadOnlyMemory.Span.

Example:

public Task<Image<Rgba32>> DecodeRawAsync(ReadOnlyMemory<byte> input, CompressionFormat format, int pixelWidth, int pixelHeight)
{
    return Task.Factory.StartNew(() => DecodeRaw(input.Span, format, pixelWidth, pixelHeight));
}

Documentation: https://docs.microsoft.com/en-us/dotnet/standard/memory-and-spans/memory-t-usage-guidelines

onepiecefreak3 commented 3 years ago

Since the PR is merged, the Async API is ready to use.