|
| 1 | +using Unity.Burst; |
| 2 | +using Unity.Collections; |
| 3 | +using Unity.Jobs; |
| 4 | +using Unity.Mathematics; |
| 5 | +using static Unity.Mathematics.math; |
| 6 | + |
| 7 | +public partial class AsyncImageLoader { |
| 8 | + interface IMipmapJob<TChannel> : IJobParallelFor where TChannel : struct { |
| 9 | + int2 InputSize { get; set; } |
| 10 | + int2 OutputSize { get; set; } |
| 11 | + NativeSlice<TChannel> InputChannel { get; set; } |
| 12 | + NativeSlice<TChannel> OutputChannel { get; set; } |
| 13 | + } |
| 14 | + |
| 15 | + [BurstCompile(CompileSynchronously = true)] |
| 16 | + struct ByteChannelMipmapJob : IMipmapJob<byte> { |
| 17 | + public int2 InputSize { get; set; } |
| 18 | + public int2 OutputSize { get; set; } |
| 19 | + |
| 20 | + public NativeSlice<byte> InputChannel { get => _inputChannel; set => _inputChannel = value; } |
| 21 | + public NativeSlice<byte> OutputChannel { get => _outputChannel; set => _outputChannel = value; } |
| 22 | + |
| 23 | + [ReadOnly] NativeSlice<byte> _inputChannel; |
| 24 | + [WriteOnly] NativeSlice<byte> _outputChannel; |
| 25 | + |
| 26 | + public void Execute(int outputIndex) { |
| 27 | + var outputPosition = int2( |
| 28 | + outputIndex % OutputSize.x, |
| 29 | + outputIndex / OutputSize.x |
| 30 | + ); |
| 31 | + var offset = int2(0); |
| 32 | + var total = 0u; |
| 33 | + |
| 34 | + for (offset.y = 0; offset.y < 2; offset.y++) { |
| 35 | + for (offset.x = 0; offset.x < 2; offset.x++) { |
| 36 | + var inputPosition = min(mad(2, outputPosition, offset), InputSize - 1); |
| 37 | + var inputIndex = mad(InputSize.x, inputPosition.y, inputPosition.x); |
| 38 | + total += _inputChannel[inputIndex]; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + _outputChannel[outputIndex] = (byte)(total >> 2); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments