-
Notifications
You must be signed in to change notification settings - Fork 65
feat(🚧): add fence for shared texture memory #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
212159d
feat: shared texture fences
pepperoni505 471e5a4
chore: add GPUSharedFence spec
pepperoni505 6d730ed
Merge branch 'main' into feat/fences
pepperoni505 bf1872a
Merge remote-tracking branch 'upstream/main' into feat/fences
pepperoni505 072bd0d
fix(android): dup the exported sync-fd in GPUSharedFence.export()
pepperoni505 c8f451d
Apply suggestions from code review
wcandillon b802870
Merge branch 'main' into feat/fences
wcandillon cc65ec9
Merge branch 'main' into feat/fences
wcandillon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #include "GPUSharedFence.h" | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
|
|
||
| #if defined(__ANDROID__) | ||
| #include <fcntl.h> | ||
| #endif | ||
|
|
||
| namespace rnwgpu { | ||
|
|
||
| namespace { | ||
|
|
||
| // Kebab-case names matching the shared-fence-* feature strings (see Unions.h / | ||
| // GPUFeatures.h). | ||
| std::string sharedFenceTypeToString(wgpu::SharedFenceType type) { | ||
| switch (type) { | ||
| case wgpu::SharedFenceType::MTLSharedEvent: | ||
| return "mtl-shared-event"; | ||
| case wgpu::SharedFenceType::SyncFD: | ||
| return "sync-fd"; | ||
| case wgpu::SharedFenceType::VkSemaphoreOpaqueFD: | ||
| return "vk-semaphore-opaque-fd"; | ||
| case wgpu::SharedFenceType::VkSemaphoreZirconHandle: | ||
| return "vk-semaphore-zircon-handle"; | ||
| case wgpu::SharedFenceType::DXGISharedHandle: | ||
| return "dxgi-shared-handle"; | ||
| case wgpu::SharedFenceType::EGLSync: | ||
| return "egl-sync"; | ||
| default: | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| jsi::Value GPUSharedFence::exportInfo(jsi::Runtime &runtime, const jsi::Value &, | ||
| const jsi::Value *, size_t) { | ||
| wgpu::SharedFenceExportInfo info{}; | ||
| uint64_t handle = 0; | ||
|
|
||
| #if defined(__APPLE__) | ||
| // Apple: the handle is an id<MTLSharedEvent> pointer. | ||
| wgpu::SharedFenceMTLSharedEventExportInfo mtlInfo{}; | ||
| info.nextInChain = &mtlInfo; | ||
| _instance.ExportInfo(&info); | ||
| handle = reinterpret_cast<uint64_t>(mtlInfo.sharedEvent); | ||
| #elif defined(__ANDROID__) | ||
| // Android: the handle is an OS file descriptor (sync_fd). Dawn's ExportInfo returns a BORROWED fd — it is | ||
| // owned by the SharedFence and closed when the fence is destroyed. This exported handle is documented as | ||
| // caller-owned (the caller must close() it), so dup() it. Without the dup the same fd is closed twice — | ||
| // once by the caller and once by Dawn on fence destruction — tripping Android's fdsan (double-close abort). | ||
| wgpu::SharedFenceSyncFDExportInfo fdInfo{}; | ||
| info.nextInChain = &fdInfo; | ||
| _instance.ExportInfo(&info); | ||
| int exportedFd = | ||
| fdInfo.handle >= 0 ? ::fcntl(fdInfo.handle, F_DUPFD_CLOEXEC, 0) : fdInfo.handle; | ||
| handle = static_cast<uint64_t>(static_cast<uint32_t>(exportedFd)); | ||
| #else | ||
| // react-native-webgpu only targets Apple (Metal) and Android (Vulkan). On any | ||
| // other platform there is no native handle convention to expose, so fail loudly | ||
| // rather than handing back a meaningless handle of 0. | ||
| throw jsi::JSError(runtime, | ||
| "GPUSharedFence::export(): unsupported platform (only " | ||
| "Apple/Metal and Android/Vulkan are supported)"); | ||
| #endif | ||
|
|
||
| jsi::Object result(runtime); | ||
| result.setProperty( | ||
| runtime, "type", | ||
| jsi::String::createFromUtf8(runtime, sharedFenceTypeToString(info.type))); | ||
| result.setProperty(runtime, "handle", | ||
| jsi::BigInt::fromUint64(runtime, handle)); | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace rnwgpu |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "NativeObject.h" | ||
|
|
||
| #include "webgpu/webgpu_cpp.h" | ||
|
|
||
| namespace rnwgpu { | ||
|
|
||
| namespace jsi = facebook::jsi; | ||
|
|
||
| // Wraps a wgpu::SharedFence: a native GPU sync primitive (id<MTLSharedEvent> on | ||
| // Apple, sync-fd / VkSemaphore on Android). | ||
| class GPUSharedFence : public NativeObject<GPUSharedFence> { | ||
| public: | ||
| static constexpr const char *CLASS_NAME = "GPUSharedFence"; | ||
|
|
||
| explicit GPUSharedFence(wgpu::SharedFence instance, std::string label) | ||
| : NativeObject(CLASS_NAME), _instance(std::move(instance)), | ||
| _label(std::move(label)) {} | ||
|
|
||
| public: | ||
| std::string getBrand() { return CLASS_NAME; } | ||
|
|
||
| // export() -> { type, handle }: exposes the native handle (as a BigInt) so | ||
| // app code can wait on or signal the fence. The caller owns the returned | ||
| // handle (e.g. an exported sync-fd must be close()d). | ||
| jsi::Value exportInfo(jsi::Runtime &runtime, const jsi::Value &thisVal, | ||
| const jsi::Value *args, size_t count); | ||
|
|
||
| std::string getLabel() { return _label; } | ||
| void setLabel(const std::string &label) { | ||
| _label = label; | ||
| _instance.SetLabel(_label.c_str()); | ||
| } | ||
|
|
||
| static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) { | ||
| installGetter(runtime, prototype, "__brand", &GPUSharedFence::getBrand); | ||
| installMethod(runtime, prototype, "export", &GPUSharedFence::exportInfo); | ||
| installGetterSetter(runtime, prototype, "label", &GPUSharedFence::getLabel, | ||
| &GPUSharedFence::setLabel); | ||
| } | ||
|
|
||
| inline wgpu::SharedFence get() { return _instance; } | ||
|
|
||
| private: | ||
| wgpu::SharedFence _instance; | ||
| std::string _label; | ||
| }; | ||
|
|
||
| } // namespace rnwgpu |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.