Safe Texture lifetime handling for readback-free encoding#104
Draft
Safe Texture lifetime handling for readback-free encoding#104
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #89
Problem
On macOS, resizing the window during recording may cause a crash (
EXC_BAD_ACCESSinobjc_retain→MetalTexture::try_from_unity_native_texture_ptr). The root cause is thatGetNativeTexturePtr()was called at frame capture time, and the resulting pointer was stored in the pipeline. By the time the native encoder consumed it, theRenderTexturecould have been released and recreated (due to resize), leaving a dangling pointer.On Vulkan (Android), the same class of bug exists but is harder to trigger:
VkImagehas no reference-counting mechanism, so even a successfulretain-equivalent at push time would not protect the handle through to the deferred graphics event.Solution
Defer
GetNativeTexturePtr()to the latest safe moment — immediately beforeCommandBuffer.IssuePluginEventAndDataon the main thread.Before:
After:
Key properties:
GetNativeTexturePtr()is called on the main thread, right beforeExecuteCommandBuffer, so the pointer is always fresh.API changes
TextureHandle(opaque struct inUniEnc.Unity) wrapping a weakGCHandle. ProvidesAlloc(Texture)/Free().VideoEncoderExtensions.UnsafePushUnityFrameAsyncnow takesTextureHandleinstead ofnint.nint-based overloads are marked[Obsolete("...", true)].Rust-side changes
VideoFrame::BlitSourceno longer holds a platform texture (MetalTexture/VulkanTexture). It stores atexture_token: usizeandPhantomData<BlitSourceType>.GraphicsEventIssuer::issue_graphics_eventcallback signature changed toFnOnce(*mut c_void)— receives the native pointer as an argument at execution time.TryFromUnityNativeTexturePointerinside the graphics-event closure, not at push time.unienc_video_encoder_push_blit_sourceno longer callstry_from_unity_native_texture_ptr.