summaryrefslogtreecommitdiffstats
path: root/tools/gfx/d3d12/d3d12-command-queue.cpp
blob: a856c063324189f35a1fa54fdb5ffdf27d30da81 (plain)
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
// d3d12-command-queue.cpp
#include "d3d12-command-queue.h"

#include "d3d12-command-buffer.h"
#include "d3d12-device.h"
#include "d3d12-fence.h"

namespace gfx
{
namespace d3d12
{

using namespace Slang;

Result CommandQueueImpl::init(DeviceImpl* device, uint32_t queueIndex)
{
    m_queueIndex = queueIndex;
    m_renderer = device;
    m_device = device->m_device;
    D3D12_COMMAND_QUEUE_DESC queueDesc = {};
    queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
    SLANG_RETURN_ON_FAIL(
        m_device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(m_d3dQueue.writeRef())));
    SLANG_RETURN_ON_FAIL(
        m_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(m_fence.writeRef())));
    globalWaitHandle = CreateEventEx(
        nullptr,
        nullptr,
        CREATE_EVENT_INITIAL_SET | CREATE_EVENT_MANUAL_RESET,
        EVENT_ALL_ACCESS);
    return SLANG_OK;
}

CommandQueueImpl::~CommandQueueImpl()
{
    waitOnHost();
    CloseHandle(globalWaitHandle);
    m_renderer->m_queueIndexAllocator.free((int)m_queueIndex, 1);
}

void CommandQueueImpl::executeCommandBuffers(
    GfxCount count,
    ICommandBuffer* const* commandBuffers,
    IFence* fence,
    uint64_t valueToSignal)
{
    ShortList<ID3D12CommandList*> commandLists;
    for (GfxCount i = 0; i < count; i++)
    {
        auto cmdImpl = static_cast<CommandBufferImpl*>(commandBuffers[i]);
        commandLists.add(cmdImpl->m_cmdList);
    }
    if (count > 0)
    {
        m_d3dQueue->ExecuteCommandLists((UINT)count, commandLists.getArrayView().getBuffer());

        m_fenceValue++;

        for (GfxCount i = 0; i < count; i++)
        {
            if (i > 0 && commandBuffers[i] == commandBuffers[i - 1])
                continue;
            auto cmdImpl = static_cast<CommandBufferImpl*>(commandBuffers[i]);
            auto transientHeap = cmdImpl->m_transientHeap;
            auto& waitInfo = transientHeap->getQueueWaitInfo(m_queueIndex);
            waitInfo.waitValue = m_fenceValue;
            waitInfo.fence = m_fence;
            waitInfo.queue = m_d3dQueue;
        }
    }

    if (fence)
    {
        auto fenceImpl = static_cast<FenceImpl*>(fence);
        m_d3dQueue->Signal(fenceImpl->m_fence.get(), valueToSignal);
    }
}

void CommandQueueImpl::waitOnHost()
{
    m_fenceValue++;
    m_d3dQueue->Signal(m_fence, m_fenceValue);
    ResetEvent(globalWaitHandle);
    m_fence->SetEventOnCompletion(m_fenceValue, globalWaitHandle);
    WaitForSingleObject(globalWaitHandle, INFINITE);
}

Result CommandQueueImpl::waitForFenceValuesOnDevice(
    GfxCount fenceCount,
    IFence** fences,
    uint64_t* waitValues)
{
    for (GfxCount i = 0; i < fenceCount; ++i)
    {
        auto fenceImpl = static_cast<FenceImpl*>(fences[i]);
        m_d3dQueue->Wait(fenceImpl->m_fence.get(), waitValues[i]);
    }
    return SLANG_OK;
}

const CommandQueueImpl::Desc& CommandQueueImpl::getDesc()
{
    return m_desc;
}

ICommandQueue* CommandQueueImpl::getInterface(const Guid& guid)
{
    if (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_ICommandQueue)
        return static_cast<ICommandQueue*>(this);
    return nullptr;
}

Result CommandQueueImpl::getNativeHandle(InteropHandle* handle)
{
    handle->api = InteropHandleAPI::D3D12;
    handle->handleValue = (uint64_t)m_d3dQueue.get();
    return SLANG_OK;
}

} // namespace d3d12
} // namespace gfx