Skip to content

Commit

Permalink
Expand pre-warming
Browse files Browse the repository at this point in the history
  • Loading branch information
EusthEnoptEron committed Jul 25, 2021
1 parent 200c844 commit e9c48c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enables loading (and uploading to GPU) textures asynchronously using compute shaders.

This package has yet to be tested thoroughly.

## Quickstart

Here's how you could create a texture:
Expand All @@ -16,6 +18,12 @@ var texture = await AsyncTextureLoader.Instance.LoadTextureAsync(image);

See [Quickstart](#quickstart) for basic usage. The platform has to support compute shaders for this to work.


### Pre-Warming

The first time the compute shader is used, a hiccup can occur in some cases. To prevent this,
call `AsyncTextureLoader.Prewarm()` somewhere you don't care about hiccups.

### Advanced Usage

Take a look at the implementation of `LoadTextureAsync()` to get a good overview of the inner workings:
Expand Down Expand Up @@ -77,16 +85,8 @@ Naturally, this step is superfluous if you use `AsyncTextureLoader.UploadDataAsy

### Mips

Currently, mipmaps are generated by default using `RenderTexture.GenerateMips()`.

If you don't need mips or want to customize the amount of them, you have the option to do so
in `AsyncTextureLoader.AcquireTextureAsync()` like so:

```c#
var image = await DecodeImageAsync(input);
var texture = await AcquireTextureAsync(image.Width, image.Height, mipCount: 0);
await UploadDataAsync(texture, image);
```
Currently, mipmaps are generated by default using `RenderTexture.GenerateMips()`, possibly causing a hiccup. The methods
of `AsyncTextureLoader` have options to control the number of mips to be generated.

## Background

Expand Down
22 changes: 17 additions & 5 deletions Runtime/AsyncTextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,23 @@ public void Prewarm()
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public void Prewarm(int width, int height)
public async void Prewarm(int width, int height)
{
if (_computeShader == null)
{
InitialComputeBufferSize = width * height;
Init();
CreateComputeBuffer(InitialComputeBufferSize);

var rt = await AcquireTextureAsync(1, 1, 0, true);
try
{
await UploadDataAsync(rt, 1, 1, new byte[4]);
}
finally
{
RenderTexture.ReleaseTemporary(rt);
}
}
}

Expand Down Expand Up @@ -395,13 +405,14 @@ public async Task UploadDataAsync(RenderTexture texture, int xOffset, int yOffse
/// Asynchronously loads a texture. Will automatically fall back to the blocking approach if compute shaders are not supported.
/// </summary>
/// <param name="input"></param>
/// <param name="mipCount"></param>
/// <returns></returns>
public async Task<Texture> LoadTextureAsync(Stream input)
public async Task<Texture> LoadTextureAsync(Stream input, int mipCount = -1)
{
if (SystemInfo.supportsComputeShaders)
{
var image = await DecodeImageAsync(input);
var texture = await AcquireTextureAsync(image.Width, image.Height);
var texture = await AcquireTextureAsync(image.Width, image.Height, mipCount);
await UploadDataAsync(texture, 0, 0, image.Width, image.Height, 0, image.Data);
return texture;
}
Expand All @@ -420,10 +431,11 @@ public async Task<Texture> LoadTextureAsync(Stream input)
/// Asynchronously loads a texture. Will automatically fall back to the blocking approach if compute shaders are not supported.
/// </summary>
/// <param name="bytes"></param>
/// <param name="mipCount"></param>
/// <returns></returns>
public Task<Texture> LoadTextureAsync(byte[] bytes)
public Task<Texture> LoadTextureAsync(byte[] bytes, int mipCount = -1)
{
return LoadTextureAsync(new MemoryStream(bytes));
return LoadTextureAsync(new MemoryStream(bytes), mipCount);
}

#endregion
Expand Down

0 comments on commit e9c48c7

Please sign in to comment.