1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// metal-command-queue.cpp
#include "metal-command-queue.h"
#include "metal-command-buffer.h"
#include "metal-fence.h"
#include "metal-util.h"
namespace gfx
{
using namespace Slang;
namespace metal
{
ICommandQueue* CommandQueueImpl::getInterface(const Guid& guid)
{
if (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_ICommandQueue)
return static_cast<ICommandQueue*>(this);
return nullptr;
}
CommandQueueImpl::~CommandQueueImpl() {}
void CommandQueueImpl::init(DeviceImpl* device, NS::SharedPtr<MTL::CommandQueue> commandQueue)
{
m_device = device;
m_commandQueue = commandQueue;
}
void CommandQueueImpl::waitOnHost()
{
// TODO implement
}
Result CommandQueueImpl::getNativeHandle(InteropHandle* outHandle)
{
outHandle->api = InteropHandleAPI::Metal;
outHandle->handleValue = reinterpret_cast<intptr_t>(m_commandQueue.get());
return SLANG_OK;
}
const CommandQueueImpl::Desc& CommandQueueImpl::getDesc()
{
return m_desc;
}
Result CommandQueueImpl::waitForFenceValuesOnDevice(
GfxCount fenceCount,
IFence** fences,
uint64_t* waitValues)
{
for (GfxCount i = 0; i < fenceCount; ++i)
{
FenceWaitInfo waitInfo;
waitInfo.fence = static_cast<FenceImpl*>(fences[i]);
waitInfo.waitValue = waitValues[i];
m_pendingWaitFences.add(waitInfo);
}
return SLANG_OK;
}
void CommandQueueImpl::queueSubmitImpl(
uint32_t count,
ICommandBuffer* const* commandBuffers,
IFence* fence,
uint64_t valueToSignal)
{
// If there are any pending wait fences, encode them to a new command buffer.
// Metal ensures that command buffers are executed in the order they are committed.
if (m_pendingWaitFences.getCount() > 0)
{
MTL::CommandBuffer* commandBuffer = m_commandQueue->commandBuffer();
for (const auto& fenceInfo : m_pendingWaitFences)
{
commandBuffer->encodeWait(fenceInfo.fence->m_event.get(), fenceInfo.waitValue);
}
commandBuffer->commit();
m_pendingWaitFences.clear();
}
for (uint32_t i = 0; i < count; ++i)
{
CommandBufferImpl* cmdBufImpl = static_cast<CommandBufferImpl*>(commandBuffers[i]);
// If this is the last command buffer and a fence is provided, signal the fence.
if (i == count - 1 && fence != nullptr)
{
cmdBufImpl->m_commandBuffer->encodeSignalEvent(
static_cast<FenceImpl*>(fence)->m_event.get(),
valueToSignal);
}
cmdBufImpl->m_commandBuffer->commit();
}
// If there are no command buffers to submit, but a fence is provided, signal the fence.
if (count == 0 && fence != nullptr)
{
MTL::CommandBuffer* commandBuffer = m_commandQueue->commandBuffer();
commandBuffer->encodeSignalEvent(
static_cast<FenceImpl*>(fence)->m_event.get(),
valueToSignal);
commandBuffer->commit();
}
}
void CommandQueueImpl::executeCommandBuffers(
GfxCount count,
ICommandBuffer* const* commandBuffers,
IFence* fence,
uint64_t valueToSignal)
{
AUTORELEASEPOOL
if (count == 0 && fence == nullptr)
{
return;
}
queueSubmitImpl(count, commandBuffers, fence, valueToSignal);
}
} // namespace metal
} // namespace gfx
|