yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
3.2 KiB122 linesraw
1// d3d12-command-buffer.cpp
2#include "d3d12-command-buffer.h"
3
4#include "d3d12-transient-heap.h"
5
6namespace gfx
7{
8namespace d3d12
9{
10
11using namespace Slang;
12
13// There are a pair of cyclic references between a `TransientResourceHeap` and
14// a `CommandBuffer` created from the heap. We need to break the cycle upon
15// the public reference count of a command buffer dropping to 0.
16
17ICommandBufferD3D12* CommandBufferImpl::getInterface(const Guid& guid)
18{
19    if (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_ICommandBuffer ||
20        guid == GfxGUID::IID_ICommandBufferD3D12)
21        return static_cast<ICommandBufferD3D12*>(this);
22    return nullptr;
23}
24
25Result CommandBufferImpl::getNativeHandle(InteropHandle* handle)
26{
27    handle->api = InteropHandleAPI::D3D12;
28    handle->handleValue = (uint64_t)m_cmdList.get();
29    return SLANG_OK;
30}
31
32void CommandBufferImpl::bindDescriptorHeaps()
33{
34    if (!m_descriptorHeapsBound)
35    {
36        ID3D12DescriptorHeap* heaps[] = {
37            m_transientHeap->getCurrentViewHeap().getHeap(),
38            m_transientHeap->getCurrentSamplerHeap().getHeap(),
39        };
40        m_cmdList->SetDescriptorHeaps(SLANG_COUNT_OF(heaps), heaps);
41        m_descriptorHeapsBound = true;
42    }
43}
44
45void CommandBufferImpl::reinit()
46{
47    invalidateDescriptorHeapBinding();
48    m_rootShaderObject.init(m_renderer);
49}
50
51void CommandBufferImpl::init(
52    DeviceImpl* renderer,
53    ID3D12GraphicsCommandList* d3dCommandList,
54    TransientResourceHeapImpl* transientHeap)
55{
56    m_transientHeap = transientHeap;
57    m_renderer = renderer;
58    m_cmdList = d3dCommandList;
59
60    reinit();
61
62    m_cmdList->QueryInterface<ID3D12GraphicsCommandList6>(m_cmdList6.writeRef());
63    if (m_cmdList6)
64    {
65        m_cmdList4 = m_cmdList6;
66        m_cmdList1 = m_cmdList6;
67        return;
68    }
69#if SLANG_GFX_HAS_DXR_SUPPORT
70    m_cmdList->QueryInterface<ID3D12GraphicsCommandList4>(m_cmdList4.writeRef());
71    if (m_cmdList4)
72    {
73        m_cmdList1 = m_cmdList4;
74        return;
75    }
76#endif
77    m_cmdList->QueryInterface<ID3D12GraphicsCommandList1>(m_cmdList1.writeRef());
78}
79
80void CommandBufferImpl::encodeResourceCommands(IResourceCommandEncoder** outEncoder)
81{
82    m_resourceCommandEncoder.init(this);
83    *outEncoder = &m_resourceCommandEncoder;
84}
85
86void CommandBufferImpl::encodeRenderCommands(
87    IRenderPassLayout* renderPass,
88    IFramebuffer* framebuffer,
89    IRenderCommandEncoder** outEncoder)
90{
91    m_renderCommandEncoder.init(
92        m_renderer,
93        m_transientHeap,
94        this,
95        static_cast<RenderPassLayoutImpl*>(renderPass),
96        static_cast<FramebufferImpl*>(framebuffer));
97    *outEncoder = &m_renderCommandEncoder;
98}
99
100void CommandBufferImpl::encodeComputeCommands(IComputeCommandEncoder** outEncoder)
101{
102    m_computeCommandEncoder.init(m_renderer, m_transientHeap, this);
103    *outEncoder = &m_computeCommandEncoder;
104}
105
106void CommandBufferImpl::encodeRayTracingCommands(IRayTracingCommandEncoder** outEncoder)
107{
108#if SLANG_GFX_HAS_DXR_SUPPORT
109    m_rayTracingCommandEncoder.init(this);
110    *outEncoder = &m_rayTracingCommandEncoder;
111#else
112    *outEncoder = nullptr;
113#endif
114}
115
116void CommandBufferImpl::close()
117{
118    m_cmdList->Close();
119}
120
121} // namespace d3d12
122} // namespace gfx