Skip to content

Commit

Permalink
!refactor: swapchain & backbuffer implementation
Browse files Browse the repository at this point in the history
MetalFX spatial upscaling is broken in some cases, particually if the swapchain is created with sRGB format
no ci
  • Loading branch information
3Shain committed Nov 23, 2024
1 parent a2749bb commit 9612b55
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 610 deletions.
9 changes: 2 additions & 7 deletions src/d3d11/d3d11_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
#include "d3d11_device.hpp"
#include "d3d11_device_child.hpp"

#include "Metal/MTLCommandBuffer.hpp"

DEFINE_COM_INTERFACE("3a3f085a-d0fe-4324-b0ae-fe04de18571c",
IMTLD3D11DeviceContext)
: public ID3D11DeviceContext4 {

virtual void STDMETHODCALLTYPE FlushInternal(
std::function<void(MTL::CommandBuffer *)> && beforeCommit,
std::function<void(void)> && onFinished, bool present_) = 0;

virtual void WaitUntilGPUIdle() = 0;

virtual void Commit() = 0;
};

namespace dxmt {
Expand Down
7 changes: 0 additions & 7 deletions src/d3d11/d3d11_context_def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,13 +693,6 @@ class MTLD3D11DeferredContext : public DeferredContextBase {
}
}

void
FlushInternal(
std::function<void(MTL::CommandBuffer *)> &&beforeCommit, std::function<void(void)> &&onFinished, bool present_
) final {
// nop
}

HRESULT
GetData(ID3D11Asynchronous *pAsync, void *pData, UINT DataSize, UINT GetDataFlags) override {
return E_FAIL;
Expand Down
19 changes: 2 additions & 17 deletions src/d3d11/d3d11_context_imm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,22 +652,6 @@ class MTLD3D11ImmediateContext : public ImmediateContextBase {
}
}

void
FlushInternal(
std::function<void(MTL::CommandBuffer *)> &&beforeCommit, std::function<void(void)> &&onFinished, bool present_
) final {
if (InvalidateCurrentPass(true)) {
D3D11_ASSERT(0 && "unexpected commit");
}
cmd_queue.CurrentChunk()->emit([bc = std::move(beforeCommit), _ = DestructorWrapper(
[of = std::move(onFinished)]() { of(); }, nullptr
)](CommandChunk::context &ctx) { bc(ctx.cmdbuf); });
Commit();
if (present_) {
cmd_queue.PresentBoundary();
}
}

HRESULT
GetData(ID3D11Asynchronous *pAsync, void *pData, UINT DataSize, UINT GetDataFlags) override {
if (!pAsync || (DataSize && !pData))
Expand Down Expand Up @@ -808,7 +792,8 @@ class MTLD3D11ImmediateContext : public ImmediateContextBase {
virtual void
WaitUntilGPUIdle() override {
uint64_t seq = cmd_queue.CurrentSeqId();
FlushInternal([](auto) {}, []() {}, false);
if(!InvalidateCurrentPass())
Commit();
cmd_queue.WaitCPUFence(seq);
};

Expand Down
20 changes: 9 additions & 11 deletions src/d3d11/d3d11_context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,16 +577,16 @@ template <typename ContextInternalState> class MTLD3D11DeviceContextImplBase : p
case D3D11_UAV_DIMENSION_BUFFER: {
if (desc.Buffer.Flags & D3D11_BUFFER_UAV_FLAG_RAW) {
EmitComputeCommandChk<true>([buffer = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearBufferUint(encoder, buffer.buffer(), buffer.offset(), buffer.width() >> 2, value);
ctx.queue->emulated_cmd.ClearBufferUint(encoder, buffer.buffer(), buffer.offset(), buffer.width() >> 2, value);
});
} else {
if (desc.Format == DXGI_FORMAT_UNKNOWN) {
EmitComputeCommandChk<true>([buffer = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearBufferUint(encoder, buffer.buffer(), buffer.offset(), buffer.width() >> 2, value);
ctx.queue->emulated_cmd.ClearBufferUint(encoder, buffer.buffer(), buffer.offset(), buffer.width() >> 2, value);
});
} else {
EmitComputeCommandChk<true>([tex = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearTextureBufferUint(encoder, tex.texture(&ctx), value);
ctx.queue->emulated_cmd.ClearTextureBufferUint(encoder, tex.texture(&ctx), value);
});
}
}
Expand All @@ -600,15 +600,15 @@ template <typename ContextInternalState> class MTLD3D11DeviceContextImplBase : p
break;
case D3D11_UAV_DIMENSION_TEXTURE2D:
EmitComputeCommandChk<true>([tex = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearTexture2DUint(encoder, tex.texture(&ctx), value);
ctx.queue->emulated_cmd.ClearTexture2DUint(encoder, tex.texture(&ctx), value);
});
break;
case D3D11_UAV_DIMENSION_TEXTURE2DARRAY:
D3D11_ASSERT(0 && "tex2darr clear");
break;
case D3D11_UAV_DIMENSION_TEXTURE3D:
EmitComputeCommandChk<true>([tex = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearTexture3DUint(encoder, tex.texture(&ctx), value);
ctx.queue->emulated_cmd.ClearTexture3DUint(encoder, tex.texture(&ctx), value);
});
break;
}
Expand All @@ -628,18 +628,18 @@ template <typename ContextInternalState> class MTLD3D11DeviceContextImplBase : p
case D3D11_UAV_DIMENSION_BUFFER: {
if (desc.Buffer.Flags & D3D11_BUFFER_UAV_FLAG_RAW) {
EmitComputeCommandChk<true>([buffer = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearBufferFloat(encoder, buffer.buffer(), buffer.offset(), buffer.width() >> 2, value);
ctx.queue->emulated_cmd.ClearBufferFloat(encoder, buffer.buffer(), buffer.offset(), buffer.width() >> 2, value);
});
} else {
if (desc.Format == DXGI_FORMAT_UNKNOWN) {
EmitComputeCommandChk<true>([buffer = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearBufferFloat(
ctx.queue->emulated_cmd.ClearBufferFloat(
encoder, buffer.buffer(), buffer.offset(), buffer.width() >> 2, value
);
});
} else {
EmitComputeCommandChk<true>([tex = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearTextureBufferFloat(encoder, tex.texture(&ctx), value);
ctx.queue->emulated_cmd.ClearTextureBufferFloat(encoder, tex.texture(&ctx), value);
});
}
}
Expand All @@ -653,7 +653,7 @@ template <typename ContextInternalState> class MTLD3D11DeviceContextImplBase : p
break;
case D3D11_UAV_DIMENSION_TEXTURE2D:
EmitComputeCommandChk<true>([tex = Use(bindable), value](auto encoder, auto &ctx) {
ctx.queue->clear_cmd.ClearTexture2DFloat(encoder, tex.texture(&ctx), value);
ctx.queue->emulated_cmd.ClearTexture2DFloat(encoder, tex.texture(&ctx), value);
});
break;
case D3D11_UAV_DIMENSION_TEXTURE2DARRAY:
Expand Down Expand Up @@ -2187,8 +2187,6 @@ template <typename ContextInternalState> class MTLD3D11DeviceContextImplBase : p

void UpdateVertexBuffer();

virtual void Commit() = 0;

/**
Valid transition:
* -> Idle
Expand Down
Loading

0 comments on commit 9612b55

Please sign in to comment.