Skip to content

Commit a70113c

Browse files
kaizhangNVcsyonghe
andauthored
Fix metal issue (#6361)
* Fix metal issue - implement waitForFences for metal backend - fix a bug that it misses clear the entryPoints when initializing RootShaderObject * format --------- Co-authored-by: Yong He <yonghe@outlook.com>
1 parent 7435d5d commit a70113c

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

tools/gfx/metal/metal-device.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,16 @@ Result DeviceImpl::waitForFences(
875875
bool waitForAll,
876876
uint64_t timeout)
877877
{
878-
return SLANG_E_NOT_IMPLEMENTED;
878+
// return SLANG_E_NOT_IMPLEMENTED;
879+
for (GfxCount i = 0; i < fenceCount; ++i)
880+
{
881+
FenceImpl* fenceImpl = static_cast<FenceImpl*>(fences[i]);
882+
if (!fenceImpl->waitForFence(fenceValues[i], timeout))
883+
{
884+
return SLANG_FAIL;
885+
}
886+
}
887+
return SLANG_OK;
879888
}
880889

881890
} // namespace metal

tools/gfx/metal/metal-fence.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ Result FenceImpl::init(DeviceImpl* device, const IFence::Desc& desc)
2222
return SLANG_FAIL;
2323
}
2424
m_event->setSignaledValue(desc.initialValue);
25+
26+
m_eventListener = NS::TransferPtr(MTL::SharedEventListener::alloc()->init());
27+
if (!m_eventListener)
28+
{
29+
return SLANG_FAIL;
30+
}
31+
2532
return SLANG_OK;
2633
}
2734

@@ -49,5 +56,34 @@ Result FenceImpl::getNativeHandle(InteropHandle* outNativeHandle)
4956
return SLANG_OK;
5057
}
5158

59+
bool FenceImpl::waitForFence(uint64_t value, uint64_t timeout)
60+
{
61+
// Create a semaphore to synchronize the notification
62+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
63+
64+
// Create and store the notification block
65+
MTL::SharedEventNotificationBlock block = ^(MTL::SharedEvent* event, uint64_t eventValue) {
66+
dispatch_semaphore_signal(semaphore);
67+
};
68+
69+
// Set up notification handler
70+
m_event->notifyListener(m_eventListener.get(), value, block);
71+
72+
// Wait for the notification or timeout
73+
if (timeout & (1LLU << 63))
74+
{
75+
timeout = DISPATCH_TIME_FOREVER;
76+
}
77+
else
78+
{
79+
timeout = dispatch_time(DISPATCH_TIME_NOW, timeout);
80+
}
81+
82+
long result = dispatch_semaphore_wait(semaphore, timeout);
83+
dispatch_release(semaphore);
84+
85+
return result == 0;
86+
}
87+
5288
} // namespace metal
5389
} // namespace gfx

tools/gfx/metal/metal-fence.h

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class FenceImpl : public FenceBase
1616
public:
1717
RefPtr<DeviceImpl> m_device;
1818
NS::SharedPtr<MTL::SharedEvent> m_event;
19+
NS::SharedPtr<MTL::SharedEventListener> m_eventListener;
1920

2021
~FenceImpl();
2122

@@ -29,6 +30,8 @@ class FenceImpl : public FenceBase
2930

3031
virtual SLANG_NO_THROW Result SLANG_MCALL
3132
getNativeHandle(InteropHandle* outNativeHandle) override;
33+
34+
bool waitForFence(uint64_t value, uint64_t timeout);
3235
};
3336

3437
} // namespace metal

tools/gfx/metal/metal-shader-object.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ Result RootShaderObjectImpl::bindAsRoot(BindingContext* context, RootShaderObjec
768768
Result RootShaderObjectImpl::init(IDevice* device, RootShaderObjectLayoutImpl* layout)
769769
{
770770
SLANG_RETURN_ON_FAIL(Super::init(device, layout));
771-
771+
m_entryPoints.clear();
772772
for (auto entryPointInfo : layout->getEntryPoints())
773773
{
774774
RefPtr<ShaderObjectImpl> entryPoint;

0 commit comments

Comments
 (0)