yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
3.1 KiB109 linesraw
1// d3d12-swap-chain.cpp
2#include "d3d12-swap-chain.h"
3
4#include "d3d12-command-queue.h"
5#include "d3d12-device.h"
6#include "d3d12-texture.h"
7
8namespace gfx
9{
10namespace d3d12
11{
12
13using namespace Slang;
14
15Result SwapchainImpl::init(
16    DeviceImpl* renderer,
17    const ISwapchain::Desc& swapchainDesc,
18    WindowHandle window)
19{
20    m_queue = static_cast<CommandQueueImpl*>(swapchainDesc.queue)->m_d3dQueue;
21    m_dxgiFactory = renderer->m_deviceInfo.m_dxgiFactory;
22    SLANG_RETURN_ON_FAIL(
23        D3DSwapchainBase::init(swapchainDesc, window, DXGI_SWAP_EFFECT_FLIP_DISCARD));
24    SLANG_RETURN_ON_FAIL(renderer->m_device->CreateFence(
25        0,
26        D3D12_FENCE_FLAG_NONE,
27        IID_PPV_ARGS(m_fence.writeRef())));
28
29    SLANG_RETURN_ON_FAIL(m_swapChain->QueryInterface(m_swapChain3.writeRef()));
30    for (GfxIndex i = 0; i < swapchainDesc.imageCount; i++)
31    {
32        m_frameEvents.add(CreateEventEx(
33            nullptr,
34            FALSE,
35            CREATE_EVENT_INITIAL_SET | CREATE_EVENT_MANUAL_RESET,
36            EVENT_ALL_ACCESS));
37    }
38    return SLANG_OK;
39}
40
41Result SwapchainImpl::resize(GfxCount width, GfxCount height)
42{
43    for (auto evt : m_frameEvents)
44        SetEvent(evt);
45    SLANG_RETURN_ON_FAIL(D3DSwapchainBase::resize(width, height));
46    return SLANG_OK;
47}
48
49void SwapchainImpl::createSwapchainBufferImages()
50{
51    m_images.clear();
52
53    for (GfxIndex i = 0; i < m_desc.imageCount; i++)
54    {
55        ComPtr<ID3D12Resource> d3dResource;
56        m_swapChain->GetBuffer(i, IID_PPV_ARGS(d3dResource.writeRef()));
57        ITextureResource::Desc imageDesc = {};
58        imageDesc.allowedStates = ResourceStateSet(
59            ResourceState::Present,
60            ResourceState::RenderTarget,
61            ResourceState::CopyDestination);
62        imageDesc.type = IResource::Type::Texture2D;
63        imageDesc.arraySize = 0;
64        imageDesc.format = m_desc.format;
65        imageDesc.size.width = m_desc.width;
66        imageDesc.size.height = m_desc.height;
67        imageDesc.size.depth = 1;
68        imageDesc.numMipLevels = 1;
69        imageDesc.defaultState = ResourceState::Present;
70        RefPtr<TextureResourceImpl> image = new TextureResourceImpl(imageDesc);
71        image->m_resource.setResource(d3dResource.get());
72        image->m_defaultState = D3D12_RESOURCE_STATE_PRESENT;
73        m_images.add(image);
74    }
75    for (auto evt : m_frameEvents)
76        SetEvent(evt);
77}
78
79int SwapchainImpl::acquireNextImage()
80{
81    auto result = (int)m_swapChain3->GetCurrentBackBufferIndex();
82    WaitForSingleObject(m_frameEvents[result], INFINITE);
83    ResetEvent(m_frameEvents[result]);
84    return result;
85}
86
87Result SwapchainImpl::present()
88{
89    m_fence->SetEventOnCompletion(
90        fenceValue,
91        m_frameEvents[m_swapChain3->GetCurrentBackBufferIndex()]);
92    SLANG_RETURN_ON_FAIL(D3DSwapchainBase::present());
93    fenceValue++;
94    m_queue->Signal(m_fence, fenceValue);
95    return SLANG_OK;
96}
97
98bool SwapchainImpl::isOccluded()
99{
100    return (m_swapChain3->Present(0, DXGI_PRESENT_TEST) == DXGI_STATUS_OCCLUDED);
101}
102
103Result SwapchainImpl::setFullScreenMode(bool mode)
104{
105    return m_swapChain3->SetFullscreenState(mode, nullptr);
106}
107
108} // namespace d3d12
109} // namespace gfx