|
| 1 | +using Unity.Burst; |
| 2 | +using Unity.Collections; |
| 3 | +using Unity.Collections.LowLevel.Unsafe; |
| 4 | +using Unity.Jobs; |
| 5 | + |
| 6 | +public partial class AsyncImageLoader { |
| 7 | + [BurstCompile(CompileSynchronously = true)] |
| 8 | + struct TransferImageToTextureJob : IJobParallelFor { |
| 9 | + public int bytesPerLine; |
| 10 | + public int bytesPerScanline; |
| 11 | + |
| 12 | + [NativeDisableUnsafePtrRestriction] |
| 13 | + public System.IntPtr bitsPtr; |
| 14 | + |
| 15 | + [WriteOnly] public NativeSlice<byte> textureData; |
| 16 | + |
| 17 | + public unsafe void Execute(int rowIndex) { |
| 18 | + UnsafeUtility.MemCpy( |
| 19 | + (byte*)textureData.GetUnsafePtr() + rowIndex * bytesPerLine, |
| 20 | + (bitsPtr + rowIndex * bytesPerScanline).ToPointer(), |
| 21 | + bytesPerLine |
| 22 | + ); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + [BurstCompile(CompileSynchronously = true)] |
| 27 | + struct TransferBGR24ImageToRGB24TextureJob : IJobParallelFor { |
| 28 | + public int bytesPerScanline; |
| 29 | + public int width; |
| 30 | + |
| 31 | + [NativeDisableUnsafePtrRestriction] |
| 32 | + public System.IntPtr bitsPtr; |
| 33 | + |
| 34 | + [WriteOnly] public NativeSlice<byte> textureData; |
| 35 | + |
| 36 | + public unsafe void Execute(int rowIndex) { |
| 37 | + var rowData = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<RGB24Pixel>( |
| 38 | + (RGB24Pixel*)textureData.GetUnsafePtr() + rowIndex * width, sizeof(RGB24Pixel), width |
| 39 | + ); |
| 40 | + var rowBits = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<RGB24Pixel>( |
| 41 | + (bitsPtr + rowIndex * bytesPerScanline).ToPointer(), sizeof(RGB24Pixel), width |
| 42 | + ); |
| 43 | + |
| 44 | + rowData.SliceWithStride<byte>(0).CopyFrom(rowBits.SliceWithStride<byte>(2)); |
| 45 | + rowData.SliceWithStride<byte>(1).CopyFrom(rowBits.SliceWithStride<byte>(1)); |
| 46 | + rowData.SliceWithStride<byte>(2).CopyFrom(rowBits.SliceWithStride<byte>(0)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + [BurstCompile(CompileSynchronously = true)] |
| 51 | + struct TransferBGRA32ImageToRGBA32TextureJob : IJobParallelFor { |
| 52 | + public int bytesPerScanline; |
| 53 | + public int width; |
| 54 | + |
| 55 | + [NativeDisableUnsafePtrRestriction] |
| 56 | + public System.IntPtr bitsPtr; |
| 57 | + |
| 58 | + [WriteOnly] public NativeSlice<byte> textureData; |
| 59 | + |
| 60 | + public unsafe void Execute(int rowIndex) { |
| 61 | + var rowData = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<RGBA32Pixel>( |
| 62 | + (RGBA32Pixel*)textureData.GetUnsafePtr() + rowIndex * width, sizeof(RGBA32Pixel), width |
| 63 | + ); |
| 64 | + var rowBits = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<RGBA32Pixel>( |
| 65 | + (bitsPtr + rowIndex * bytesPerScanline).ToPointer(), sizeof(RGBA32Pixel), width |
| 66 | + ); |
| 67 | + |
| 68 | + rowData.SliceWithStride<byte>(0).CopyFrom(rowBits.SliceWithStride<byte>(2)); |
| 69 | + rowData.SliceWithStride<byte>(1).CopyFrom(rowBits.SliceWithStride<byte>(1)); |
| 70 | + rowData.SliceWithStride<byte>(2).CopyFrom(rowBits.SliceWithStride<byte>(0)); |
| 71 | + rowData.SliceWithStride<byte>(3).CopyFrom(rowBits.SliceWithStride<byte>(3)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + [BurstCompile(CompileSynchronously = true)] |
| 76 | + struct TransferRGBFloatImageToRGBAFloatTextureJob : IJobParallelFor { |
| 77 | + public int bytesPerScanline; |
| 78 | + public int width; |
| 79 | + |
| 80 | + [NativeDisableUnsafePtrRestriction] |
| 81 | + public System.IntPtr bitsPtr; |
| 82 | + |
| 83 | + [WriteOnly] public NativeSlice<byte> textureData; |
| 84 | + |
| 85 | + public unsafe void Execute(int rowIndex) { |
| 86 | + var rowData = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<RGBAFloatPixel>( |
| 87 | + (RGBAFloatPixel*)textureData.GetUnsafePtr() + rowIndex * width, sizeof(RGBAFloatPixel), width |
| 88 | + ); |
| 89 | + var rowAlphaData = rowData.SliceWithStride<float>(3 * sizeof(float)); |
| 90 | + |
| 91 | + UnsafeUtility.MemCpyStride( |
| 92 | + rowData.GetUnsafePtr(), rowData.Stride, |
| 93 | + (bitsPtr + rowIndex * bytesPerScanline).ToPointer(), 3 * sizeof(float), |
| 94 | + 3 * sizeof(float), width |
| 95 | + ); |
| 96 | + |
| 97 | + for (var i = 0; i < rowAlphaData.Length; i++) rowAlphaData[i] = 1f; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments