Skip to content

Commit

Permalink
Cleaner sync interval for D3D
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisd1100 committed Dec 18, 2023
1 parent b858b4f commit 61436ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/gfx/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void sync_set_interval(struct sync *sync, uint32_t interval)
static uint32_t sync_next_interval(struct sync *sync)
{
if (sync->interval == 0)
return 1;
return 0;

uint32_t rem = sync->rem + sync->interval;
uint32_t r = (uint32_t) (rem / 100.0);
Expand Down
14 changes: 9 additions & 5 deletions src/windows/gfx/d3d11-ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ GFX_CTX_PROTOTYPES(_d3d11_)

struct d3d11_ctx {
HWND hwnd;
bool vsync;
struct sync sync;
uint32_t width;
uint32_t height;
Expand Down Expand Up @@ -195,7 +194,9 @@ struct gfx_ctx *mty_d3d11_ctx_create(void *native_window, bool vsync)
{
struct d3d11_ctx *ctx = MTY_Alloc(1, sizeof(struct d3d11_ctx));
ctx->hwnd = (HWND) native_window;
ctx->vsync = vsync;

if (vsync)
sync_set_interval(&ctx->sync, 1);

d3d11_ctx_get_size(ctx, &ctx->width, &ctx->height);

Expand Down Expand Up @@ -307,10 +308,13 @@ void mty_d3d11_ctx_present(struct gfx_ctx *gfx_ctx)
struct d3d11_ctx *ctx = (struct d3d11_ctx *) gfx_ctx;

if (ctx->back_buffer) {
bool tearing = !ctx->vsync && (ctx->flags & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING);
UINT interval = sync_next_interval(&ctx->sync);
UINT flags = interval > 0 ? 0 : DXGI_PRESENT_ALLOW_TEARING;

UINT interval = tearing ? 0 : sync_next_interval(&ctx->sync);
UINT flags = tearing ? DXGI_PRESENT_ALLOW_TEARING : 0;
if (interval == 0 && !(ctx->flags & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING)) {
interval = 1;
flags = 0;
}

HRESULT e = IDXGISwapChain2_Present(ctx->swap_chain2, interval, flags);

Expand Down
9 changes: 5 additions & 4 deletions src/windows/gfx/d3d12-ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ struct d3d12_ctx_buffer {

struct d3d12_ctx {
HWND hwnd;
bool vsync;
struct sync sync;
uint32_t width;
uint32_t height;
Expand Down Expand Up @@ -284,7 +283,9 @@ struct gfx_ctx *mty_d3d12_ctx_create(void *native_window, bool vsync)
bool r = true;

ctx->hwnd = (HWND) native_window;
ctx->vsync = vsync;

if (vsync)
sync_set_interval(&ctx->sync, 1);

d3d12_ctx_get_size(ctx, &ctx->width, &ctx->height);

Expand Down Expand Up @@ -440,8 +441,8 @@ void mty_d3d12_ctx_present(struct gfx_ctx *gfx_ctx)
ID3D12CommandQueue_ExecuteCommandLists(core->cq, 1, &cl);
ID3D12CommandList_Release(cl);

UINT interval = ctx->vsync ? sync_next_interval(&ctx->sync) : 0;
UINT flags = ctx->vsync ? 0 : DXGI_PRESENT_ALLOW_TEARING;
UINT interval = sync_next_interval(&ctx->sync);
UINT flags = interval > 0 ? 0 : DXGI_PRESENT_ALLOW_TEARING;

e = IDXGISwapChain3_Present(core->swap_chain3, interval, flags);

Expand Down

0 comments on commit 61436ed

Please sign in to comment.