Advertisement
Guest User

Untitled

a guest
Nov 9th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Jobs;
  5. using Unity.Collections;
  6. using Unity.Burst;
  7.  
  8. using UnityEngine.UI;
  9. public class TestParallel : MonoBehaviour
  10. {
  11. private Texture2D _inputTexture; // Texture that we are reading, should have read/write enabled set to true and Compression set to None
  12. private RawImage rawImage;
  13. private Texture2D _outputTexture;
  14.  
  15. private NativeArray<byte> inputArray;
  16. private NativeArray<byte> outputArray;
  17.  
  18. public ZEDToOpenCVRetriever imageRetriever;
  19.  
  20. private NativeArray<byte> historyArray;
  21. private NativeArray<byte> averageArray;
  22.  
  23. private int _history_scan_depth;
  24. private int _history_capacity;
  25. private int _data_delta;
  26. private int _layer;
  27. private const int width = 1280;
  28. private const int height = 720;
  29. private int history_capacity = 3;
  30. private int layer_size = width * height * 6;
  31. private int layer = 0;
  32. public int history_scan_depth = 50;
  33. public int data_delta = 50;
  34. private void Awake()
  35. {
  36. if (!imageRetriever) imageRetriever = ZEDToOpenCVRetriever.GetInstance();
  37. imageRetriever.OnImageUpdated_LeftGrayscale += ImageUpdated;
  38.  
  39. // Copy original texture to a new Texture2D for testing
  40. _outputTexture = new Texture2D(1280, 720);
  41. // Graphics.CopyTexture(_inputTexture, _outputTexture);
  42.  
  43. // Get the underlying NativeArrays which can be used by the jobs system
  44.  
  45.  
  46. // Assign outputtexture to a rawimage
  47. // rawImage.texture = _outputTexture;
  48. historyArray = new NativeArray<byte>(history_capacity * layer_size, Allocator.Persistent);
  49. averageArray = new NativeArray<byte>(layer_size, Allocator.Persistent);
  50. outputArray = new NativeArray<byte>(layer_size, Allocator.Persistent);
  51.  
  52. }
  53.  
  54. private void OnDestroy()
  55. {
  56. historyArray.Dispose(); // We need to manually dispose our NativeArrays. The arrays retrieved from the Textures are disposed by the textures so we don't care here
  57. averageArray.Dispose();
  58. outputArray.Dispose();
  59. }
  60.  
  61. private void Update()
  62. {
  63.  
  64. }
  65.  
  66. [BurstCompile] // <-- For this you need to add the Burst package using the Unity package manager, this attribute is completely optional but it improves performance by a lot (like 10 times faster)
  67. public struct ConvertJobParallel : IJobParallelFor
  68. {
  69. [ReadOnly("input_texture")] public NativeArray<byte> input_texture;
  70.  
  71. public NativeArray<byte> output;
  72. [NativeDisableParallelForRestriction] public NativeArray<byte> history;
  73. [NativeDisableParallelForRestriction] public NativeArray<byte> average;
  74. public int _history_scan_depth;
  75. public int _history_capacity;
  76. public int _layer_size;
  77. public int _data_delta;
  78. public int _layer;
  79. // This gets called by the job system. index ranges between 0 and the 'arrayLength' specified in the first parameter of Schedule()
  80. public void Execute(int index)
  81. {
  82. // This is simply a demo test, it swaps the red and green channels, here you put your logic
  83.  
  84. }
  85. }
  86.  
  87.  
  88. private void ImageUpdated(ref Texture2D zedTextureDepth)
  89. {
  90.  
  91. inputArray = zedTextureDepth.GetRawTextureData<byte>(); // Could use GetPixelData instead when working with mipmaps
  92.  
  93. var job = new ConvertJobParallel()
  94. {
  95. input_texture = inputArray,
  96. output = outputArray,
  97. history = historyArray,
  98. average = averageArray,
  99. _history_scan_depth = history_scan_depth,
  100. _history_capacity = history_capacity,
  101. _layer_size = layer_size,
  102. _data_delta = data_delta,
  103. _layer = layer
  104. };
  105.  
  106. JobHandle handle1 = job.Schedule(inputArray.Length, 100); // The 100 here is a magic number, it is basically a number that says how many items (Color32 in our example) are handled in one 'batch' where each batch can run async on any thread
  107. JobHandle.ScheduleBatchedJobs(); // Start running (all) our previously scheduled jobs
  108. handle1.Complete(); // Wait on the main thread fot this job to complete
  109. //_outputTexture.Apply(); // Copy changes to gpu
  110.  
  111. }
  112. }
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement