Skip to content

Commit

Permalink
fallback dx swapchain interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxwellGengYF committed Dec 19, 2024
1 parent 63e2587 commit 9ff40a6
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/backends/dx/DXApi/LCCmdBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,10 @@ void LCCmdBuffer::Present(
{
std::lock_guard lck{mtx};
tracker.listType = alloc->Type();
swapchain->frameIndex = swapchain->swapChain->GetCurrentBackBufferIndex();
// swapchain->frameIndex = swapchain->swapChain->GetCurrentBackBufferIndex();
auto &&rt = &swapchain->m_renderTargets[swapchain->frameIndex];
swapchain->frameIndex += 1;
swapchain->frameIndex %= swapchain->frameCount;
auto cb = alloc->GetBuffer();
auto bd = cb->Build();
auto cmdList = cb->CmdList();
Expand Down
13 changes: 7 additions & 6 deletions src/backends/dx/DXApi/LCSwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LCSwapChain::LCSwapChain(
uint backBufferCount)
: Resource(device), vsync(vsync) {
this->format = format;
auto frameCount = backBufferCount + 1;
frameCount = backBufferCount + 1;
vstd::push_back_func(
m_renderTargets,
frameCount,
Expand All @@ -40,19 +40,19 @@ LCSwapChain::LCSwapChain(
nullptr,
nullptr,
&localSwap));
swapChain = DxPtr(static_cast<IDXGISwapChain3*>(localSwap), true);

swapChain = DxPtr(localSwap, true);
}
for (uint32_t n = 0; n < frameCount; n++) {
ThrowIfFailed(swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n].rt)));
}
if (!vsync)
swapChain->SetMaximumFrameLatency(backBufferCount * 2);
// if (!vsync)
// swapChain->SetMaximumFrameLatency(backBufferCount * 2);
}
LCSwapChain::LCSwapChain(
PixelStorage &storage,
Device *device,
IDXGISwapChain4 *swapChain,
IDXGISwapChain1 *swapChain,
bool vsync)
: Resource(device),
swapChain(swapChain, false),
Expand All @@ -63,6 +63,7 @@ LCSwapChain::LCSwapChain(
m_renderTargets,
swapChainDesc.BufferCount,
[&] { return device; });
frameCount = swapChainDesc.BufferCount;
for (uint32_t n = 0; n < swapChainDesc.BufferCount; n++) {
ThrowIfFailed(swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n].rt)));
}
Expand Down
5 changes: 3 additions & 2 deletions src/backends/dx/DXApi/LCSwapChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ namespace lc::dx {
class LCSwapChain : public Resource {
public:
vstd::vector<SwapChain> m_renderTargets;
DxPtr<IDXGISwapChain3> swapChain;
DxPtr<IDXGISwapChain1> swapChain;
uint64 frameIndex = 0;
uint64 frameCount = 0;
DXGI_FORMAT format;
bool vsync;
Tag GetTag() const override { return Tag::SwapChain; }
Expand All @@ -27,7 +28,7 @@ class LCSwapChain : public Resource {
LCSwapChain(
PixelStorage& storage,
Device* device,
IDXGISwapChain4* swapChain,
IDXGISwapChain1* swapChain,
bool vsync);
};
}// namespace lc::dx
2 changes: 1 addition & 1 deletion src/backends/dx/DXApi/ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ SwapchainCreationInfo DxNativeResourceExt::register_external_swapchain(
auto res = new LCSwapChain(
info.storage,
dx_device,
reinterpret_cast<IDXGISwapChain4 *>(swapchain_ptr),
reinterpret_cast<IDXGISwapChain1 *>(swapchain_ptr),
vsync);
info.handle = reinterpret_cast<uint64_t>(res);
info.native_handle = swapchain_ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/backends/dx/DXRuntime/CommandAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void CommandAllocator::Execute(
}
}
}
void CommandAllocator::ExecuteAndPresent(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex, IDXGISwapChain1 *swapchain, bool vsync) {
void CommandAllocator::ExecuteAndPresent(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex, IDXGISwapChain *swapchain, bool vsync) {
auto present = [&]() {
if (vsync) {
ThrowIfFailed(swapchain->Present(1, 0));
Expand Down
2 changes: 1 addition & 1 deletion src/backends/dx/DXRuntime/CommandAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CommandAllocator final : public vstd::IOperatorNewBase {
~CommandAllocator();
CommandBuffer *GetBuffer() const;
void Execute(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex);
void ExecuteAndPresent(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex, IDXGISwapChain1 *swapchain, bool vsync);
void ExecuteAndPresent(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex, IDXGISwapChain *swapchain, bool vsync);
void Complete(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex);
DefaultBuffer const *AllocateScratchBuffer(size_t targetSize);
BufferView GetTempReadbackBuffer(uint64 size, size_t align = 0);
Expand Down
2 changes: 1 addition & 1 deletion src/backends/dx/DXRuntime/CommandQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void CommandQueue::ExecuteEmptyCallbacks(AllocatorPtr &&alloc, vstd::vector<vstd
waitCv.notify_one();
}

void CommandQueue::ExecuteAndPresent(AllocatorPtr &&alloc, IDXGISwapChain1 *swapChain, bool vsync) {
void CommandQueue::ExecuteAndPresent(AllocatorPtr &&alloc, IDXGISwapChain *swapChain, bool vsync) {
auto curFrame = ++lastFrame;
alloc->ExecuteAndPresent(this, cmdFence.Get(), curFrame, swapChain, vsync);
mtx.lock();
Expand Down
2 changes: 1 addition & 1 deletion src/backends/dx/DXRuntime/CommandQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CommandQueue : vstd::IOperatorNewBase {
void ExecuteCallbacks(AllocatorPtr &&alloc, vstd::vector<vstd::function<void()>> &&callbacks);
void ExecuteEmpty(AllocatorPtr &&alloc);
void ExecuteEmptyCallbacks(AllocatorPtr &&alloc, vstd::vector<vstd::function<void()>> &&callbacks);
void ExecuteAndPresent(AllocatorPtr &&alloc, IDXGISwapChain1 *swapChain, bool vsync);
void ExecuteAndPresent(AllocatorPtr &&alloc, IDXGISwapChain *swapChain, bool vsync);
void Complete(uint64 fence);
void Complete();
void ForceSync(
Expand Down
2 changes: 1 addition & 1 deletion src/ext/imgui

0 comments on commit 9ff40a6

Please sign in to comment.