🆕 Answer an Apple GPU's scheduled event signal in arm64 guests
-[MTLCommandBuffer encodeSignalEventScheduled:value:] is SPI an Apple GPU's command buffers answer and the x86 Macs' do not. MPSGraph's Neural Engine region op sends it with no respondsToSelector: check, so an arm64 guest's process that reached it raised an unrecognized selector. An arm64 guest's command buffer now answers it as the M3 Max does (measured, oracle_cmdbuf schedsig): the shared event takes the value as the buffer's queue submits the call, ahead of the buffer's GPU work and scheduling, behind any wait before it on the queue, and only rises. The bundle sets it as the CPU's set does: at the buffer's post where no wait before it is unmet as the process knows it, else at the buffer's scheduling, before its scheduled handlers. After the commit the call sets nothing; a plain event ends the process with Metal's words; nil raises Metal's exception on a buffer that retains its references and fails one that retains none with Metal's error. The x86-64 bundle is unchanged: it still answers no such member.
This commit is contained in:
1 parent
4daf23dd26
commit
de0ac7c079
3 files changed
+420
-2
No files matched your search
@@ -1622,6 +1622,16 @@ constexpr uint64_t kRgfxBarrierRenderStages = MTLRenderStageVertex | MTLRenderSt
|
||||
constexpr uint32_t kRgfxSlotsNamed = 1, kRgfxSlotsFinished = 2, kRgfxSlotsHeld = 4,
|
||||
kRgfxSlotsScheduled = 8;
|
||||
|
||||
#if defined(__arm64__)
|
||||
// A scheduled signal of a command buffer (encodeSignalEventScheduled:value:): the shared event,
|
||||
// the value it takes, and how many of the buffer's waits were encoded before it.
|
||||
struct RgfxScheduledSignal {
|
||||
RgfxSharedEvent *event;
|
||||
uint64_t value;
|
||||
size_t waitsBefore;
|
||||
};
|
||||
#endif
|
||||
|
||||
// A waiting thread's ask of a command buffer's scheduling (RgfxCmdBuffer's _scheduledAsk): a
|
||||
// thread waits for it to be scheduled; its message is on its ring, held by nothing.
|
||||
constexpr uint32_t kRgfxAskWanted = 1, kRgfxAskPosted = 2;
|
||||
@@ -1776,6 +1786,16 @@ enum RgfxStage : unsigned {
|
||||
// finish of the buffer raises each plain event's value as this process knows it.
|
||||
std::vector<std::pair<id, uint64_t>> _eventWaits;
|
||||
std::vector<std::pair<RgfxEvent *, uint64_t>> _plainSignals;
|
||||
#if defined(__arm64__)
|
||||
// Its scheduled signals (encodeSignalEventScheduled:value:), in the order encoded, and how
|
||||
// many of them are set: at its post, those its queue's submission reaches there, the rest at
|
||||
// its scheduling (rgfxSetScheduledSignalsAtPost, rgfxReachScheduled).
|
||||
std::vector<RgfxScheduledSignal> _scheduledSignals;
|
||||
size_t _scheduledSignalsSet;
|
||||
// A scheduled signal of a nil event on a buffer that retains no references: it fails, as
|
||||
// Metal's does (encodeSignalEventScheduled:value:).
|
||||
bool _invalidInput;
|
||||
#endif
|
||||
// Signalled once the queue's write-back queue has written its copies back and retired its
|
||||
// writes, where its take left them to it (rgfxHostDone:); nil otherwise.
|
||||
dispatch_semaphore_t _writtenBack;
|
||||
@@ -2184,6 +2204,11 @@ enum RgfxStage : unsigned {
|
||||
RgfxRegion *writesRegion = _surfaceWritesRegion;
|
||||
std::function<void(uint64_t)> expect;
|
||||
std::function<void()> whileWaiting;
|
||||
#if defined(__arm64__)
|
||||
// The scheduled signals its queue's submission reaches now, before it goes on its ring and
|
||||
// before a wait of it on the same event is looked at.
|
||||
[self rgfxSetScheduledSignalsAtPost];
|
||||
#endif
|
||||
if (_reader != nullptr) {
|
||||
RgfxCmdBuffer *strongSelf = self;
|
||||
RgfxReplyReader *reader = _reader;
|
||||
@@ -2409,6 +2434,22 @@ static NSError *rgfxCommandBufferError(NSInteger code)
|
||||
return [NSError errorWithDomain:MTLCommandBufferErrorDomain code:code userInfo:nil];
|
||||
}
|
||||
|
||||
#if defined(__arm64__)
|
||||
// The NSError Metal gives a command buffer that retains no references, one of whose scheduled
|
||||
// signals names no event: its description, under the code it carries (measured on the M3 Max,
|
||||
// macOS 27.0.1, oracle_cmdbuf schedsig_nil_unretained; an underlying error is not measured).
|
||||
static NSError *rgfxInvalidInputError()
|
||||
{
|
||||
return [NSError
|
||||
errorWithDomain:MTLCommandBufferErrorDomain
|
||||
code:MTLCommandBufferErrorInternal
|
||||
userInfo:@{
|
||||
NSLocalizedDescriptionKey :
|
||||
@"Invalid Input (0000000a:kIOGPUCommandBufferCallbackErrorInvalidInput)"
|
||||
}];
|
||||
}
|
||||
#endif
|
||||
|
||||
// What the host is done with at the buffer's completion, on the thread that took it off the
|
||||
// ring, before its record goes to the queue's completion queue: it is submitted where it was
|
||||
// not yet (its CB_COMPLETE stands for both); its writes of surfaces all in place retire; the
|
||||
@@ -2432,6 +2473,58 @@ static NSError *rgfxCommandBufferError(NSInteger code)
|
||||
return YES;
|
||||
}
|
||||
|
||||
#if defined(__arm64__)
|
||||
// Whether the first `count` events it waits on are at their values, as rgfxWaitsMet reads them.
|
||||
- (BOOL)rgfxWaitsMetBefore:(size_t)count
|
||||
{
|
||||
for (size_t i = 0; i < count && i < _eventWaits.size(); i++) {
|
||||
const auto &wait = _eventWaits[i];
|
||||
uint64_t now = 0;
|
||||
if ([wait.first isKindOfClass:[RgfxSharedEvent class]])
|
||||
now = [(RgfxSharedEvent *)wait.first signaledValue];
|
||||
else if ([wait.first isKindOfClass:[RgfxEvent class]])
|
||||
now = [(RgfxEvent *)wait.first rgfxSignalledValue];
|
||||
if (now < wait.second)
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
// At its post, the scheduled signals its queue's submission reaches there, in the order
|
||||
// encoded: where no buffer posted before it on its queue is held, each whose waits encoded before
|
||||
// it are met as this process knows them (rgfxWaitsMetBefore:). The rest are set at its
|
||||
// scheduling, which then runs on its queue's completion queue, as a scheduled handler's does.
|
||||
// Metal's queue submits a buffer's stream in order: a scheduled signal takes its value as the
|
||||
// submission reaches it, behind every wait before it on the queue, its buffer's own and an
|
||||
// earlier buffer's, and ahead of the buffer's GPU work and of a wait encoded after it (measured
|
||||
// on the M3 Max, macOS 27.0.1, oracle_cmdbuf schedsig: 0.04 to 0.1 ms after the commit, before
|
||||
// GPUStartTime and the scheduled handler, behind a 190 ms buffer of its queue too; behind a wait,
|
||||
// 0.04 to 0.15 ms after the CPU set its event; with a wait on its own event after it, the
|
||||
// buffer runs). One held at its post is set later than Metal's, at its buffer's scheduling,
|
||||
// where a wait after it on the buffer is not yet met.
|
||||
- (void)rgfxSetScheduledSignalsAtPost
|
||||
{
|
||||
if (_scheduledSignals.empty())
|
||||
return;
|
||||
if ([_queue rgfxHeldBuffers] == 0) {
|
||||
while (_scheduledSignalsSet < _scheduledSignals.size() &&
|
||||
[self rgfxWaitsMetBefore:_scheduledSignals[_scheduledSignalsSet].waitsBefore])
|
||||
[self rgfxSetScheduledSignal:_scheduledSignals[_scheduledSignalsSet++]];
|
||||
}
|
||||
if (_scheduledSignalsSet < _scheduledSignals.size()) {
|
||||
_schedulesWhereSeen = false;
|
||||
_deliversWhereSeen = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Sets a scheduled signal's event to its value, as the CPU's set does: the host first, then
|
||||
// this process's listeners and waits; a lower value leaves the event, as Metal's does.
|
||||
- (void)rgfxSetScheduledSignal:(const RgfxScheduledSignal &)signal
|
||||
{
|
||||
[signal.event setSignaledValue:signal.value];
|
||||
}
|
||||
#endif
|
||||
|
||||
// Names its surface accesses, once: its queue among the writers of each view it writes and the
|
||||
// readers of each it only reads, so a present of the surface waits for it and a buffer of
|
||||
// another queue posted after it that uses the surface runs after it where one of the two writes
|
||||
@@ -2613,7 +2706,14 @@ static NSError *rgfxCommandBufferError(NSInteger code)
|
||||
// The resources it kept are out of flight before it is seen complete, where its take did
|
||||
// not do it.
|
||||
_kept.land();
|
||||
// A buffer some of whose inline bytes found no chunk ran without those bindings, so it fails.
|
||||
// A buffer some of whose inline bytes found no chunk ran without those bindings, so it fails;
|
||||
// so does one with a scheduled signal of a nil event, as Metal's does.
|
||||
#if defined(__arm64__)
|
||||
if (msg->status == RGFX_CB_OK && _invalidInput) {
|
||||
_cbError = rgfxInvalidInputError();
|
||||
_status.store(MTLCommandBufferStatusError);
|
||||
} else
|
||||
#endif
|
||||
if (msg->status == RGFX_CB_OK && _uploadsFailed) {
|
||||
// It ran without some of its bindings, so it fails as Metal's does when memory runs out.
|
||||
static char site;
|
||||
@@ -2667,6 +2767,11 @@ static NSError *rgfxCommandBufferError(NSInteger code)
|
||||
return;
|
||||
[self rgfxReachSubmitted];
|
||||
[self rgfxScheduledNamesAccesses];
|
||||
#if defined(__arm64__)
|
||||
// Its scheduled signals its post did not set, before its scheduled handlers run.
|
||||
while (_scheduledSignalsSet < _scheduledSignals.size())
|
||||
[self rgfxSetScheduledSignal:_scheduledSignals[_scheduledSignalsSet++]];
|
||||
#endif
|
||||
MTLCommandBufferStatus committed = MTLCommandBufferStatusCommitted;
|
||||
_status.compare_exchange_strong(committed, MTLCommandBufferStatusScheduled);
|
||||
[self rgfxNoteStage:kRgfxStageSubmitToHW];
|
||||
@@ -3101,6 +3206,43 @@ static NSError *rgfxCommandBufferError(NSInteger code)
|
||||
[self encodeSignalEvent:event value:value];
|
||||
}
|
||||
|
||||
#if defined(__arm64__)
|
||||
// An Apple GPU's command buffer's SPI, which MPSGraph, CMImaging and PhotosensitivityProcessing
|
||||
// send with no respondsToSelector: check; neither x86 Mac's buffers answer it (%spi-neither), so
|
||||
// an x86-64 guest's do not. The shared event takes `value` as its queue's submission reaches the
|
||||
// call, where its GPU work has not begun (rgfxSetScheduledSignalsAtPost), and its value only
|
||||
// rises, as Metal's (measured on the M3 Max, macOS 27.0.1, oracle_cmdbuf schedsig: an event at 5
|
||||
// stays 5 after a scheduled signal of 3; of two, 6 and 8 in either order, 8). After the commit
|
||||
// the call sets nothing. A plain event ends the process with Metal's words, as Metal does; a nil
|
||||
// event raises Metal's exception on a buffer that retains its references, and fails a buffer
|
||||
// that retains none with Metal's error (measured, schedsig_plain, schedsig_late, schedsig_nil,
|
||||
// schedsig_nil_unretained). An event no device of ours made is not carried (rgfxEventOf:).
|
||||
- (void)encodeSignalEventScheduled:(id<MTLEvent>)event value:(uint64_t)value
|
||||
{
|
||||
if ([(id)event isKindOfClass:[RgfxEvent class]]) {
|
||||
NSLog(@"MTLEvent passed to API expecting MTLSharedEvent");
|
||||
abort();
|
||||
}
|
||||
if (event == nil && _retained)
|
||||
@throw [NSException
|
||||
exceptionWithName:NSInvalidArgumentException
|
||||
reason:@"*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil"
|
||||
userInfo:nil];
|
||||
std::lock_guard<std::mutex> hold(_handlersLock);
|
||||
if (_committed)
|
||||
return;
|
||||
if (event == nil) {
|
||||
_invalidInput = true;
|
||||
return;
|
||||
}
|
||||
if (![(id)event isKindOfClass:[RgfxSharedEvent class]]) {
|
||||
[self rgfxEventOf:event op:"encodeSignalEventScheduled" value:value];
|
||||
return;
|
||||
}
|
||||
_scheduledSignals.push_back({(RgfxSharedEvent *)event, value, _eventWaits.size()});
|
||||
}
|
||||
#endif
|
||||
|
||||
// Statistics, as the queue's.
|
||||
- (void)setStatEnabled:(BOOL)enabled
|
||||
{
|
||||
|
||||
@@ -1428,7 +1428,7 @@ encodeDashboardFinalizeForResourceGroup:dashboard:value:forIndex: metal ends the
|
||||
encodeDashboardFinalizeForResourceGroup:dashboard:values:indices:count: metal ends the process with Metal's words, as Metal's base buffer does: not supported on this device (read from code, %cb-base)
|
||||
encodeDashboardTagForResourceGroup: metal ends the process with Metal's words, as Metal's base buffer does: not supported on this device (read from code, %cb-base)
|
||||
encodeSignalEvent:value:agentMask: implemented
|
||||
encodeSignalEventScheduled:value: absent %spi-neither
|
||||
encodeSignalEventScheduled:value: absent %spi-neither (the arm64 bundle answers it, as the M3 Max's buffers do: an Apple GPU's SPI, src/RgfxCommandObjects.mm)
|
||||
encodeWaitForEvent:value:timeout: implemented
|
||||
getListIndex metal 0, as the AMD 5300M's and the Intel 630's buffers answer (measured, %cb-answers)
|
||||
globalTraceObjectID implemented
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
#import "RgfxBuffer.h"
|
||||
#import "RgfxCommandObjects.h"
|
||||
#import "RgfxEvent.h"
|
||||
#import "RgfxFunction.h"
|
||||
#import "RgfxIOSurface.h"
|
||||
#import "RgfxMetalPrivate.h"
|
||||
@@ -116,6 +117,11 @@ RGFX_RESOURCE_SPI
|
||||
- (void)useInternalResidencySets:(const id _Nonnull[_Nonnull])residencySets count:(NSUInteger)count;
|
||||
@end
|
||||
|
||||
// The command-buffer SPI an Apple GPU's buffers answer, and an arm64 guest's.
|
||||
@protocol QueueTestScheduledSignalSPI
|
||||
- (void)encodeSignalEventScheduled:(id<MTLEvent>)event value:(uint64_t)value;
|
||||
@end
|
||||
|
||||
// The command-queue SPI members the queue answers (RgfxCommandQueue, MTLCommandQueueSPI).
|
||||
@protocol QueueTestCommandQueueSPI
|
||||
- (NSUInteger)qosLevel;
|
||||
@@ -1741,8 +1747,16 @@ void test_buffers_let_go_give_their_blocks_back()
|
||||
|
||||
} // namespace
|
||||
|
||||
#if defined(__arm64__)
|
||||
int rgfxQueueDiesScheduledPlain();
|
||||
#endif
|
||||
|
||||
int rgfxQueueDiesCase(const char *name)
|
||||
{
|
||||
#if defined(__arm64__)
|
||||
if (std::strcmp(name, "scheduled-signal-plain") == 0)
|
||||
return rgfxQueueDiesScheduledPlain();
|
||||
#endif
|
||||
signal(SIGABRT, [](int) { _exit(kAborted); });
|
||||
@autoreleasepool {
|
||||
if (std::strcmp(name, "function-handle") == 0) {
|
||||
@@ -1790,6 +1804,21 @@ int rgfxQueueDiesCase(const char *name)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(__arm64__)
|
||||
// A scheduled signal of a plain event, on a buffer not yet committed: it ends the process.
|
||||
int rgfxQueueDiesScheduledPlain()
|
||||
{
|
||||
signal(SIGABRT, [](int) { _exit(kAborted); });
|
||||
@autoreleasepool {
|
||||
Queue q(true);
|
||||
id<MTLCommandBuffer> cb = [q.queue commandBufferWithUnretainedReferences];
|
||||
RgfxEvent *plain = [RgfxEvent eventWithDevice:nil serializer:&q.ser];
|
||||
[(id<QueueTestScheduledSignalSPI>)cb encodeSignalEventScheduled:plain value:1];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
// A command buffer's signal of a shared event that the host posts while the buffer runs, ahead of
|
||||
// its CB_SCHEDULED, reaches the process through the completion thread: a thread waiting on the
|
||||
// CPU for it wakes while the buffer is still Committed, and the value it then sets goes to the
|
||||
@@ -2316,6 +2345,248 @@ void test_residency_sets_are_held_until_let_go()
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(__arm64__)
|
||||
// A shared event of `q`'s, registered with its queue's reader, at `initial`.
|
||||
RgfxSharedEvent *queueEvent(Queue &q, uint64_t initial)
|
||||
{
|
||||
RgfxSharedEvent *ev = [RgfxSharedEvent sharedEventWithInitialValue:initial
|
||||
device:nil
|
||||
serializer:&q.ser];
|
||||
CHECK(ev != nil);
|
||||
[ev rgfxRegisterWithReplyReader:[q.queue sharedReplyReader].get()];
|
||||
return ev;
|
||||
}
|
||||
|
||||
// The values `q`'s control ring carries for `event` (SHARED_EVENT_SET_VALUE), in order; every
|
||||
// message is taken off the ring.
|
||||
std::vector<uint64_t> setsOf(Queue &q, RgfxObjId event)
|
||||
{
|
||||
std::vector<uint64_t> values;
|
||||
RgfxMsgHeader h = {};
|
||||
std::vector<uint8_t> bytes;
|
||||
while (q.control.next(&h, &bytes)) {
|
||||
RgfxSharedEventSetValue m = {};
|
||||
if (h.type != RGFX_CMD_SHARED_EVENT_SET_VALUE || bytes.size() < sizeof(m))
|
||||
continue;
|
||||
std::memcpy(&m, bytes.data(), sizeof(m));
|
||||
if (m.obj_id == event)
|
||||
values.push_back(m.value);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
void scheduleSignal(id<MTLCommandBuffer> cb, id<MTLEvent> event, uint64_t value)
|
||||
{
|
||||
[(id<QueueTestScheduledSignalSPI>)cb encodeSignalEventScheduled:event value:value];
|
||||
}
|
||||
|
||||
// An arm64 guest's command buffer answers an Apple GPU's SPI encodeSignalEventScheduled:value:,
|
||||
// which MPSGraph sends with no respondsToSelector: check. Its shared event takes the value as
|
||||
// the buffer's queue submits it, as Metal's does (measured on the M3 Max, macOS 27.0.1,
|
||||
// oracle_cmdbuf schedsig): at the commit's post, before the buffer's work or its scheduling,
|
||||
// whatever the buffer encoded before the call; the host is told first, as the CPU's set tells
|
||||
// it, then this process's listeners and waits, and the scheduled handler reads the value. The
|
||||
// value only rises: an event at 5 stays 5 after a signal of 3, and of two the higher stays, in
|
||||
// either order. After the commit the call sets nothing.
|
||||
void test_a_scheduled_signal_is_set_as_its_queue_submits_it()
|
||||
{
|
||||
@autoreleasepool {
|
||||
Queue q(true);
|
||||
Driven driven({&q});
|
||||
RgfxSharedEvent *ev = queueEvent(q, 0);
|
||||
id<MTLCommandBuffer> cb = [q.queue commandBuffer];
|
||||
CHECK([(id)cb respondsToSelector:@selector(encodeSignalEventScheduled:value:)]);
|
||||
[[cb blitCommandEncoder] endEncoding];
|
||||
scheduleSignal(cb, ev, 3);
|
||||
std::atomic<uint64_t> inHandler{0};
|
||||
std::atomic<uint64_t> *inHandlerAt = &inHandler;
|
||||
[cb addScheduledHandler:^(id<MTLCommandBuffer> c) {
|
||||
(void)c;
|
||||
inHandlerAt->store(ev.signaledValue);
|
||||
}];
|
||||
dispatch_semaphore_t heard = dispatch_semaphore_create(0);
|
||||
MTLSharedEventListener *listener = [[MTLSharedEventListener alloc] init];
|
||||
[ev notifyListener:listener
|
||||
atValue:3
|
||||
block:^(id<MTLSharedEvent> e, uint64_t v) {
|
||||
(void)e;
|
||||
(void)v;
|
||||
dispatch_semaphore_signal(heard);
|
||||
}];
|
||||
CHECK(ev.signaledValue == 0);
|
||||
CHECK(setsOf(q, ev.rgfxObjId).empty());
|
||||
[cb commit];
|
||||
CHECK(ev.signaledValue == 3);
|
||||
CHECK((setsOf(q, ev.rgfxObjId) == std::vector<uint64_t>{3}));
|
||||
CHECK(waitFor(heard, 5) == 0);
|
||||
CHECK(cb.status == MTLCommandBufferStatusCommitted);
|
||||
const uint64_t seqno = nextSeqno(q);
|
||||
hostScheduled(q, *driven.kext, seqno, cb);
|
||||
[cb waitUntilScheduled];
|
||||
CHECK(inHandler == 3);
|
||||
hostComplete(q, *driven.kext, seqno, cb);
|
||||
[cb waitUntilCompleted];
|
||||
CHECK(cb.status == MTLCommandBufferStatusCompleted);
|
||||
CHECK(ev.signaledValue == 3);
|
||||
|
||||
struct Case {
|
||||
uint64_t initial;
|
||||
std::vector<uint64_t> signals;
|
||||
uint64_t after;
|
||||
};
|
||||
const Case cases[] = {{5, {3}, 5}, {5, {7}, 7}, {5, {6, 8}, 8}, {5, {8, 6}, 8}};
|
||||
for (const Case &c : cases) {
|
||||
RgfxSharedEvent *e = queueEvent(q, c.initial);
|
||||
id<MTLCommandBuffer> b = [q.queue commandBuffer];
|
||||
for (uint64_t v : c.signals)
|
||||
scheduleSignal(b, e, v);
|
||||
[b commit];
|
||||
CHECK(e.signaledValue == c.after);
|
||||
CHECK(setsOf(q, e.rgfxObjId) == c.signals); // the host keeps the higher too
|
||||
const uint64_t s = nextSeqno(q);
|
||||
hostComplete(q, *driven.kext, s, b);
|
||||
[b waitUntilCompleted];
|
||||
}
|
||||
|
||||
RgfxSharedEvent *late = queueEvent(q, 0);
|
||||
id<MTLCommandBuffer> b = [q.queue commandBuffer];
|
||||
[b commit];
|
||||
scheduleSignal(b, late, 1);
|
||||
const uint64_t s = nextSeqno(q);
|
||||
hostComplete(q, *driven.kext, s, b);
|
||||
[b waitUntilCompleted];
|
||||
CHECK(late.signaledValue == 0);
|
||||
CHECK(setsOf(q, late.rgfxObjId).empty());
|
||||
CHECK(b.status == MTLCommandBufferStatusCompleted);
|
||||
}
|
||||
}
|
||||
|
||||
// A scheduled signal behind a wait the process does not know met, its buffer's own encoded
|
||||
// before it or one of an earlier buffer of its queue, is set at its buffer's scheduling, before
|
||||
// the scheduled handlers, as Metal's queue reaches it only once that wait is met (measured on
|
||||
// the M3 Max, macOS 27.0.1, oracle_cmdbuf schedsig: 0.04 to 0.15 ms after the CPU set the
|
||||
// event waited on, before the scheduled handler). One behind a wait met already, or ahead of a
|
||||
// wait, is set at the post; a wait after it on its own event is then met, and the buffer holds
|
||||
// up no later signal of its queue (Metal's buffer runs).
|
||||
void test_a_scheduled_signal_behind_a_wait_is_set_at_its_scheduling()
|
||||
{
|
||||
@autoreleasepool {
|
||||
Queue q(true);
|
||||
Driven driven({&q});
|
||||
|
||||
RgfxSharedEvent *w = queueEvent(q, 0), *e = queueEvent(q, 0);
|
||||
id<MTLCommandBuffer> cb = [q.queue commandBuffer];
|
||||
[cb encodeWaitForEvent:w value:1];
|
||||
scheduleSignal(cb, e, 1);
|
||||
std::atomic<uint64_t> inHandler{0};
|
||||
std::atomic<uint64_t> *inHandlerAt = &inHandler;
|
||||
[cb addScheduledHandler:^(id<MTLCommandBuffer> c) {
|
||||
(void)c;
|
||||
inHandlerAt->store(e.signaledValue);
|
||||
}];
|
||||
[cb commit];
|
||||
const uint64_t s1 = nextSeqno(q);
|
||||
CHECK(e.signaledValue == 0);
|
||||
CHECK(setsOf(q, e.rgfxObjId).empty());
|
||||
w.signaledValue = 1; // the host's wait goes
|
||||
hostScheduled(q, *driven.kext, s1, cb);
|
||||
[cb waitUntilScheduled];
|
||||
CHECK(inHandler == 1);
|
||||
CHECK(e.signaledValue == 1);
|
||||
CHECK((setsOf(q, e.rgfxObjId) == std::vector<uint64_t>{1}));
|
||||
hostComplete(q, *driven.kext, s1, cb);
|
||||
[cb waitUntilCompleted];
|
||||
|
||||
// Behind an earlier buffer of its queue that waits.
|
||||
RgfxSharedEvent *w2 = queueEvent(q, 0), *e2 = queueEvent(q, 0);
|
||||
id<MTLCommandBuffer> a = [q.queue commandBuffer];
|
||||
[a encodeWaitForEvent:w2 value:1];
|
||||
[a commit];
|
||||
const uint64_t sa = nextSeqno(q);
|
||||
id<MTLCommandBuffer> b = [q.queue commandBuffer];
|
||||
scheduleSignal(b, e2, 1);
|
||||
[b commit];
|
||||
const uint64_t sb = nextSeqno(q);
|
||||
CHECK(e2.signaledValue == 0);
|
||||
CHECK(setsOf(q, e2.rgfxObjId).empty());
|
||||
w2.signaledValue = 1;
|
||||
hostScheduled(q, *driven.kext, sa, a);
|
||||
[a waitUntilScheduled];
|
||||
hostScheduled(q, *driven.kext, sb, b);
|
||||
[b waitUntilScheduled];
|
||||
CHECK(e2.signaledValue == 1);
|
||||
CHECK((setsOf(q, e2.rgfxObjId) == std::vector<uint64_t>{1}));
|
||||
hostComplete(q, *driven.kext, sa, a);
|
||||
hostComplete(q, *driven.kext, sb, b);
|
||||
[b waitUntilCompleted];
|
||||
|
||||
// Behind a wait met already; and ahead of a wait on its own event, which it meets.
|
||||
RgfxSharedEvent *met = queueEvent(q, 1), *e3 = queueEvent(q, 0), *own = queueEvent(q, 0);
|
||||
id<MTLCommandBuffer> c = [q.queue commandBuffer];
|
||||
[c encodeWaitForEvent:met value:1];
|
||||
scheduleSignal(c, e3, 1);
|
||||
scheduleSignal(c, own, 2);
|
||||
[c encodeWaitForEvent:own value:2];
|
||||
[c commit];
|
||||
const uint64_t sc = nextSeqno(q);
|
||||
CHECK(e3.signaledValue == 1);
|
||||
CHECK(own.signaledValue == 2);
|
||||
// So the queue holds nothing: a later buffer's signal is set at its post.
|
||||
RgfxSharedEvent *e4 = queueEvent(q, 0);
|
||||
id<MTLCommandBuffer> d = [q.queue commandBuffer];
|
||||
scheduleSignal(d, e4, 1);
|
||||
[d commit];
|
||||
const uint64_t sd = nextSeqno(q);
|
||||
CHECK(e4.signaledValue == 1);
|
||||
hostComplete(q, *driven.kext, sc, c);
|
||||
hostComplete(q, *driven.kext, sd, d);
|
||||
[d waitUntilCompleted];
|
||||
CHECK(c.status == MTLCommandBufferStatusCompleted);
|
||||
}
|
||||
}
|
||||
|
||||
// What the call does with no shared event, as Metal's does (measured on the M3 Max, macOS
|
||||
// 27.0.1, oracle_cmdbuf schedsig_plain, schedsig_nil, schedsig_nil_unretained): a plain event
|
||||
// ends the process with Metal's words; nil raises Metal's NSInvalidArgumentException on a buffer
|
||||
// that retains its references, and fails one that retains none with Metal's error.
|
||||
void test_a_scheduled_signal_takes_only_a_shared_event()
|
||||
{
|
||||
checkDies("scheduled-signal-plain", "MTLEvent passed to API expecting MTLSharedEvent");
|
||||
@autoreleasepool {
|
||||
Queue q(true);
|
||||
Driven driven({&q});
|
||||
id<MTLCommandBuffer> retained = [q.queue commandBuffer];
|
||||
bool raised = false;
|
||||
@try {
|
||||
scheduleSignal(retained, nil, 1);
|
||||
} @catch (NSException *ex) {
|
||||
raised = [ex.name isEqualToString:NSInvalidArgumentException] &&
|
||||
[ex.reason isEqualToString:@"*** -[__NSArrayM insertObject:atIndex:]: object "
|
||||
@"cannot be nil"];
|
||||
}
|
||||
CHECK(raised);
|
||||
[retained commit];
|
||||
const uint64_t sr = nextSeqno(q);
|
||||
hostComplete(q, *driven.kext, sr, retained);
|
||||
[retained waitUntilCompleted];
|
||||
CHECK(retained.status == MTLCommandBufferStatusCompleted);
|
||||
|
||||
id<MTLCommandBuffer> unretained = [q.queue commandBufferWithUnretainedReferences];
|
||||
scheduleSignal(unretained, nil, 1);
|
||||
[unretained commit];
|
||||
const uint64_t su = nextSeqno(q);
|
||||
hostComplete(q, *driven.kext, su, unretained);
|
||||
[unretained waitUntilCompleted];
|
||||
CHECK(unretained.status == MTLCommandBufferStatusError);
|
||||
CHECK([unretained.error.domain isEqualToString:MTLCommandBufferErrorDomain]);
|
||||
CHECK(unretained.error.code == MTLCommandBufferErrorInternal);
|
||||
CHECK([unretained.error.localizedDescription
|
||||
isEqualToString:@"Invalid Input (0000000a:kIOGPUCommandBufferCallbackErrorInvalidInput)"]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
void rgfxQueueTests(const char *self)
|
||||
@@ -2347,4 +2618,9 @@ void rgfxQueueTests(const char *self)
|
||||
#if RgfxMakesResidencySets
|
||||
test_residency_sets_are_held_until_let_go();
|
||||
#endif
|
||||
#if defined(__arm64__)
|
||||
test_a_scheduled_signal_is_set_as_its_queue_submits_it();
|
||||
test_a_scheduled_signal_behind_a_wait_is_set_at_its_scheduling();
|
||||
test_a_scheduled_signal_takes_only_a_shared_event();
|
||||
#endif
|
||||
}
|
||||
Reference in new issue
Block a user