From 353465baba10a2af46e8263f2be9237f18e2d244 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Thu, 26 Dec 2024 16:55:40 -0300 Subject: [PATCH 1/3] Add TextureApplyAsyncHandle.Reinitialize This method should be called after reinitializing a Texture2D to ensure the buffer is still valid, avoiding crashes --- Runtime/Internal/TextureAsyncApplier.cs | 13 ++++++++++++ Runtime/TextureApplyAsyncHandle.cs | 28 +++++++++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Runtime/Internal/TextureAsyncApplier.cs b/Runtime/Internal/TextureAsyncApplier.cs index 4d1ae9e..67157a4 100644 --- a/Runtime/Internal/TextureAsyncApplier.cs +++ b/Runtime/Internal/TextureAsyncApplier.cs @@ -70,6 +70,19 @@ public static void Unregister(TextureApplyAsyncHandle handle) } } + public static bool IsRegistered(TextureApplyAsyncHandle handle) + { + return _applyHandlesEveryFrame.Contains(handle) || _applyHandlesThisFrame.Contains(handle); + } + + internal static void MarkDirty(TextureApplyAsyncHandle handle) + { + if (!_isCommandBufferDirty) + { + _isCommandBufferDirty = IsRegistered(handle); + } + } + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void InitializeOnLoad() { diff --git a/Runtime/TextureApplyAsyncHandle.cs b/Runtime/TextureApplyAsyncHandle.cs index c31a69e..a2150dc 100644 --- a/Runtime/TextureApplyAsyncHandle.cs +++ b/Runtime/TextureApplyAsyncHandle.cs @@ -15,16 +15,8 @@ public class TextureApplyAsyncHandle : IDisposable public TextureApplyAsyncHandle(Texture2D texture) { - if (texture == null) - { - throw new ArgumentNullException(nameof(texture)); - } - Texture = texture; - unsafe - { - Id = NativeBridge.RegisterHandle((IntPtr) Texture.GetRawTextureData().GetUnsafeReadOnlyPtr()); - } + Reinitialize(); } ~TextureApplyAsyncHandle() @@ -47,6 +39,24 @@ public void CancelUpdates() TextureAsyncApplier.Unregister(this); } + public void Reinitialize() + { + if (Texture == null) + { + throw new ArgumentNullException(nameof(Texture)); + } + + if (Id != 0) + { + NativeBridge.UnregisterHandle(Id); + } + unsafe + { + Id = NativeBridge.RegisterHandle((IntPtr) Texture.GetRawTextureData().GetUnsafeReadOnlyPtr()); + } + TextureAsyncApplier.MarkDirty(this); + } + public void FillCommandBuffer(CommandBuffer commandBuffer) { if (IsValid) From 7430e04b8e67abd397f5862b17e99dc5bdad1238 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Thu, 26 Dec 2024 16:57:19 -0300 Subject: [PATCH 2/3] Use reinitialize on PlasmaColorJob sample --- .../PlasmaTextureAsyncUpdater.cs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Samples~/PlasmaColorJob/PlasmaTextureAsyncUpdater.cs b/Samples~/PlasmaColorJob/PlasmaTextureAsyncUpdater.cs index 4110a9c..dd51720 100644 --- a/Samples~/PlasmaColorJob/PlasmaTextureAsyncUpdater.cs +++ b/Samples~/PlasmaColorJob/PlasmaTextureAsyncUpdater.cs @@ -25,15 +25,23 @@ void OnEnable() { if (_texture == null) { - _texture = new Texture2D(width, height, TextureFormat.RGBA32, false, false); + _texture = new Texture2D(width, height, TextureFormat.RGBA32, false, false) + { + name = name + }; + rawImage.texture = _texture; + } + else + { + _texture.Reinitialize(width, height, TextureFormat.RGBA32, false); + _texture.Apply(); } - rawImage.texture = _texture; _textureApplyAsyncHandle = new TextureApplyAsyncHandle(_texture); } void OnDisable() { - _textureApplyAsyncHandle?.Dispose(); + _textureApplyAsyncHandle.Dispose(); } void OnDestroy() @@ -41,6 +49,18 @@ void OnDestroy() Destroy(_texture); } +#if UNITY_EDITOR + void OnValidate() + { + if (isActiveAndEnabled && _texture) + { + _texture.Reinitialize(width, height, TextureFormat.RGBA32, false); + _texture.Apply(); + _textureApplyAsyncHandle.Reinitialize(); + } + } +#endif + void Update() { NativeArray pixels = _texture.GetPixelData(0); From db6408e1664a9192744cdd20906b5cdaebd10558 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Thu, 26 Dec 2024 17:01:18 -0300 Subject: [PATCH 3/3] Mention Reinitialize on README --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8d9b099..6e739a5 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,14 @@ textureApplyAsyncHandle.ScheduleUpdateOnce(); // Works for both one-shot and every frame updates. textureApplyAsyncHandle.CancelUpdates(); -// 6. Dispose of the `TextureApplyAsyncHandle` when not needed +// 6. If you ever Reinitialize your Texture, make +// sure to Reinitialize your TextureApplyAsyncHandle +// or your app may crash!!! +myTexture.Reinitialize(width, height); +myTexture.Apply(); +textureApplyAsyncHandle.Reinitialize(); + +// 7. Dispose of the `TextureApplyAsyncHandle` when not needed // anymore, e.g. inside a component's `OnDestroy`. textureApplyAsyncHandle.Dispose(); -``` \ No newline at end of file +```