OH4

swapchain presentation paths

Choosing a DXGI swapchain presentation path that resizes smoothly.

7E47E57E67E77E87E97EA

7EAB1A · 2026.11.261 session · 2 h

When setting up your renderer (DX11, DX12, Vulkan) on Windows you have a couple of options for DXGI swapchain presentation. But not all of these options are equal if you want to allow for smoothly resizing a windowed view. Choosing the naive route will produce very laggy and bizarre resizing behavior. Everything here assumes a single-threaded renderer.

Windows composites every desktop window through the Desktop Window Manager (DWM). The difference between the three paths is how your backbuffer gets into DWM.

Naive route (laggy)

Use DXGI_SWAP_EFFECT_FLIP_DISCARD. Instead of copying into the window's legacy DWM redirection bitmap, DXGI hands your backbuffer directly to DWM via an independent flip queue (enabling MPO / Independent Flip, HDR, and VRR).

The catch: That flip queue is asynchronous from SetWindowPos. Every ResizeBuffers tears down and re-handshakes the flip queue with DWM (2–4+ ms spikes), while the OS window border moves ahead of the swapchain and exposes pitch-black gutters (DXGI_SCALING_NONE).

Listing 1 · C · 12 lines
DXGI_SWAP_CHAIN_DESC1 desc = {
    .BufferCount = 2,
    .Scaling     = DXGI_SCALING_NONE,
    .SwapEffect  = DXGI_SWAP_EFFECT_FLIP_DISCARD, // independent DWM flip queue
    ...
};
factory->CreateSwapChainForHwnd(device, hwnd, &desc, 0, 0, &swapchain);

// On every WM_PAINT resize:
swapchain->ResizeBuffers(0, w, h, DXGI_FORMAT_UNKNOWN, 0);
...
swapchain->Present(1, 0);

Deprecated, yet smooth (DX11-only)

The legacy D3D11 path (used by raddebugger). On Present, DXGI blits (copies) your backbuffer directly into the window's DWM redirection surface in lockstep with SetWindowPos and WM_PAINT.

Listing 2 · C · 11 lines
DXGI_SWAP_CHAIN_DESC1 desc = {
    .BufferCount = 2,
    .Scaling     = DXGI_SCALING_STRETCH,
    .SwapEffect  = DXGI_SWAP_EFFECT_DISCARD,
    ...
};
factory->CreateSwapChainForHwnd(device, hwnd, &desc, 0, 0, &swapchain);

swapchain->ResizeBuffers(0, w, h, DXGI_FORMAT_UNKNOWN, 0);
...
swapchain->Present(1, 0);

Official Microsoft "right way" (DirectComposition)

Shipped Flip-model desktop apps (Windows Terminal, Zed, Chromium) decouple the swapchain from the HWND by creating the window with WS_EX_NOREDIRECTIONBITMAP and binding a CreateSwapChainForComposition swapchain to a DirectComposition visual tree.

Important catch — DComp alone still hitches: Switching from CreateSwapChainForHwnd to CreateSwapChainForComposition fixes redirection-bitmap gutter artifacts, not flip-model resize stalls. If you still call ResizeBuffers(0, w, h, ...) + Present(1, 0) synchronously inside WM_PAINT, ResizeBuffers still takes 1.6–1.8 ms avg (spikes 10–19 ms) and Present(1, 0) blocks the Win32 modal drag loop waiting on DWM vblank.

How shipped DX12 / DComp apps actually avoid the hitch:

  1. Defer until WM_EXITSIZEMOVE (DX12 game templates, Godot D3D12): zero drag hitching, no live reflow.
  2. Dedicated render thread / GPU process (Chromium, Edge, Windows Terminal): Win32 WndProc never blocks on ResizeBuffers or Present; rendering feeds DirectComposition asynchronously.
  3. Single-threaded live reflow — bucketed over-allocation + SetClip + Present(0, 0): You need both halves together:
    • Over-allocate in buckets (e.g. 512 px) + SetClip: Unlike an HWND swapchain, a composition swapchain bound to an IDCompositionVisual with an identity transform renders at 1:1 physical pixels anchored at (0, 0) — it does not stretch to the window. Set your GPU viewport/scissor and UI coordinates to (clientW, clientH) (zero stretching, zero ImGui/pointer breakage) and clip the visual with visual->SetClip({0, 0, clientW, clientH}). ResizeBuffers drops to 0 calls while dragging inside a bucket (firing only when crossing a bucket boundary, and optionally shrinking once on WM_EXITSIZEMOVE).
    • Present(0, 0) on resize frames (and no per-frame Commit()): Under DWM flip-model, Present(0, 0) (flags 0) is still tear-free — DWM composites on vblank and FLIP_DISCARD drops superseded frames instead of stalling WM_PAINT. dcomp->Commit() is only needed when the visual tree or clip rect changes, not on every Present().

The tax: Works in both D3D11 and DX12 and avoids Blt's steady-state copy, but WS_EX_NOREDIRECTIONBITMAP pushes you toward owning custom chrome (WM_NCCALCSIZE + WM_NCHITTEST + titlebar/buttons) and a backdrop (a DComp solid-color visual or DWMWA_SYSTEMBACKDROP_TYPE behind the swapchain). You also pay extra VRAM for bucketed triple buffers, ~150+ LOC of COM glue (dcomp.h is C++-only), and still no ALLOW_TEARING.

Listing 3 · C · 34 lines
// Window created with WS_EX_NOREDIRECTIONBITMAP (app owns chrome + backdrop)
DXGI_SWAP_CHAIN_DESC1 desc = {
    .Width       = round_up_512(w),
    .Height      = round_up_512(h),
    .BufferCount = 3,
    .Scaling     = DXGI_SCALING_STRETCH,          // required for composition
    .SwapEffect  = DXGI_SWAP_EFFECT_FLIP_DISCARD,
    .AlphaMode   = DXGI_ALPHA_MODE_IGNORE,        // UNSPECIFIED fails
    ...
};
factory->CreateSwapChainForComposition(device, &desc, 0, &swapchain);

// Bind swapchain into a DirectComposition visual tree once (1:1 pixels, clipped to client rect):
DCompositionCreateDevice(dxgi_device, &IID_IDCompositionDevice, &dcomp);
dcomp->CreateTargetForHwnd(hwnd, TRUE, &target);
dcomp->CreateVisual(&visual);
visual->SetContent(swapchain);
visual->SetClip((D2D_RECT_F){ 0, 0, (float)w, (float)h });
target->SetRoot(visual);
dcomp->Commit();

// On resize (WM_PAINT): only ResizeBuffers when exceeding the current 512px bucket
if (w > alloc_w || h > alloc_h) {
    alloc_w = round_up_512(max(w, alloc_w));
    alloc_h = round_up_512(max(h, alloc_h));
    swapchain->ResizeBuffers(0, alloc_w, alloc_h, DXGI_FORMAT_UNKNOWN, 0);
}
// Render with viewport/scissor/UI size = (w, h), then present non-blocking + update clip:
...
swapchain->Present(resized_this_frame ? 0 : 1, 0);
if (resized_this_frame) {
    visual->SetClip((D2D_RECT_F){ 0, 0, (float)w, (float)h });
    dcomp->Commit();
}

Measured comparison (100-step modal resize, Win11 165 Hz)

Ablation on the same WM_PAINT live-resize loop (950..1350 × 620..880, D3D11, Release-Profile):

Path ResizeBuffers (100 steps) Present in WM_PAINT WND_WM_PAINT avg/fr WND_DispatchMsg p50 / p95 / p99 / max
Naive HWND FLIP_DISCARD + Present(1,0) 1.20 ms/fr — 4.89 ms — (WM_PAINT p95 8.45 ms, p99 10.17 ms)
Naive DComp (per-px ResizeBuffers + Present(1,0)) 1.63 ms/fr 1.78 ms/fr 4.29 ms — (Present spikes up to 36.7 ms)
DComp (Present(0,0) only, still per-px ResizeBuffers) 1.69 ms/fr 0.44 ms/fr 3.06 ms — (ResizeBuffers max 10.1 ms)
DComp (512px bucket only, still Present(1,0)) 0.00 ms 4.16 ms/fr 4.97 ms — (Present(1,0) absorbs the savings)
Blt DISCARD (DX11-only) 0.41 ms/fr 0.92 ms/fr 2.17 ms 2.50 / 5.21 / 6.23 / 6.65 ms
DComp Smooth (512px bucket + SetClip + Present(0,0)) 0.00 ms 0.70 ms/fr 1.51 ms 1.97 / 3.66 / 11.04 / 15.10 ms

Takeaway: Neither bucketed over-allocation nor Present(0, 0) fixes DComp resize on its own — combining both beats Blt DISCARD on avg/p95 resize time (1.51 ms vs 2.17 ms) and works on DX12, while Blt DISCARD still has the tightest worst-case tail (6.65 ms max vs 15.10 ms) with zero extra VRAM or COM/chrome machinery.

Minimal example

A complete D3D11 + DirectComposition program in pure C that uses the bucketed over-allocation, SetClip and Present(0, 0) approach above.

Listing 4 · C · 417 lines
/*
	Minimal DirectComposition + D3D11 setup in pure C that supports window
	resizing without constant hitching, unlike more naive setups. Comparable or
	better performance than using DXGI_SWAP_EFFECT_DISCARD.

	Soli Deo gloria

	To compile, run:

		cl /TC /std:c11 /W4 /WX d3d11_dcomp_smooth_resize.c
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <d3d11_1.h>
#include <dxgi1_2.h>
#include <d3dcompiler.h>
#include <stdint.h>

#pragma comment(lib, "user32")
#pragma comment(lib, "d3d11")
#pragma comment(lib, "dxgi")
#pragma comment(lib, "dcomp")
#pragma comment(lib, "d3dcompiler")
#pragma comment(lib, "dxguid")
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")

#define kResizeBucketPx	512u
#define com_release(x)	do { if ( (x) != 0 ) { (x)->lpVtbl->Release(x); (x) = 0; } } while (0)

typedef struct DComp_RectF DComp_RectF;
struct DComp_RectF {
	float	Left;
	float	Top;
	float	Right;
	float	Bottom;
};

typedef struct DComp_Device DComp_Device;
typedef struct DComp_Target DComp_Target;
typedef struct DComp_Visual DComp_Visual;

typedef struct DComp_Device_Vtbl DComp_Device_Vtbl;
struct DComp_Device_Vtbl {
	HRESULT(STDMETHODCALLTYPE *QueryInterface)(DComp_Device*, REFIID, void**);
	ULONG(STDMETHODCALLTYPE *AddRef)(DComp_Device*);
	ULONG(STDMETHODCALLTYPE *Release)(DComp_Device*);
	HRESULT(STDMETHODCALLTYPE *Commit)(DComp_Device*);
	HRESULT(STDMETHODCALLTYPE *WaitForCommitCompletion)(DComp_Device*);
	HRESULT(STDMETHODCALLTYPE *GetFrameStatistics)(DComp_Device*, void*);
	HRESULT(STDMETHODCALLTYPE *CreateTargetForHwnd)(DComp_Device*, HWND, BOOL, DComp_Target**);
	HRESULT(STDMETHODCALLTYPE *CreateVisual)(DComp_Device*, DComp_Visual**);
};
struct DComp_Device {
	const DComp_Device_Vtbl*	lpVtbl;
};

typedef struct DComp_Target_Vtbl DComp_Target_Vtbl;
struct DComp_Target_Vtbl {
	HRESULT(STDMETHODCALLTYPE *QueryInterface)(DComp_Target*, REFIID, void**);
	ULONG(STDMETHODCALLTYPE *AddRef)(DComp_Target*);
	ULONG(STDMETHODCALLTYPE *Release)(DComp_Target*);
	HRESULT(STDMETHODCALLTYPE *SetRoot)(DComp_Target*, DComp_Visual*);
};
struct DComp_Target {
	const DComp_Target_Vtbl*	lpVtbl;
};

typedef struct DComp_Visual_Vtbl DComp_Visual_Vtbl;
struct DComp_Visual_Vtbl {
	HRESULT(STDMETHODCALLTYPE *QueryInterface)(DComp_Visual*, REFIID, void**);
	ULONG(STDMETHODCALLTYPE *AddRef)(DComp_Visual*);
	ULONG(STDMETHODCALLTYPE *Release)(DComp_Visual*);
	HRESULT(STDMETHODCALLTYPE *SetOffsetXAnim)(DComp_Visual*, void*);
	HRESULT(STDMETHODCALLTYPE *SetOffsetX)(DComp_Visual*, float);
	HRESULT(STDMETHODCALLTYPE *SetOffsetYAnim)(DComp_Visual*, void*);
	HRESULT(STDMETHODCALLTYPE *SetOffsetY)(DComp_Visual*, float);
	HRESULT(STDMETHODCALLTYPE *SetTransformObj)(DComp_Visual*, void*);
	HRESULT(STDMETHODCALLTYPE *SetTransformMat)(DComp_Visual*, const void*);
	HRESULT(STDMETHODCALLTYPE *SetTransformParent)(DComp_Visual*, DComp_Visual*);
	HRESULT(STDMETHODCALLTYPE *SetEffect)(DComp_Visual*, void*);
	HRESULT(STDMETHODCALLTYPE *SetBitmapInterpolationMode)(DComp_Visual*, UINT);
	HRESULT(STDMETHODCALLTYPE *SetBorderMode)(DComp_Visual*, UINT);
	HRESULT(STDMETHODCALLTYPE *SetClipObj)(DComp_Visual*, void*);
	HRESULT(STDMETHODCALLTYPE *SetClipRect)(DComp_Visual*, const DComp_RectF*);
	HRESULT(STDMETHODCALLTYPE *SetContent)(DComp_Visual*, IUnknown*);
};
struct DComp_Visual {
	const DComp_Visual_Vtbl*	lpVtbl;
};

extern HRESULT WINAPI DCompositionCreateDevice(IDXGIDevice* dxgi_device, REFIID iid, void** dcomp_device);

static const GUID kIIDDCompDevice = {
	0xC37EA93Au, 0xE7AAu, 0x450Du,
	{ 0xB1u, 0x6Fu, 0x97u, 0x46u, 0xCBu, 0x04u, 0x07u, 0xF3u }
};

static const char g_shader_src[] =
	"cbuffer Params : register(b0) { float2 Size; float2 Pad; };\n"
	"struct VSOut { float4 pos : SV_POSITION; float3 col : COLOR; };\n"
	"VSOut VSMain(uint id : SV_VertexID) {\n"
	"	float2 pts[3] = { float2(0.0, 220.0), float2(190.5, -110.0), float2(-190.5, -110.0) };\n"
	"	float3 col[3] = { float3(0.95, 0.35, 0.30), float3(0.25, 0.85, 0.55), float3(0.30, 0.55, 0.95) };\n"
	"	VSOut o;\n"
	"	o.pos = float4(pts[id] / (Size * 0.5), 0.0, 1.0);\n"
	"	o.col = col[id];\n"
	"	return o;\n"
	"}\n"
	"float4 PSMain(VSOut i) : SV_TARGET {\n"
	"	return float4(i.col, 1.0);\n"
	"}\n";

typedef struct App_State App_State;
struct App_State {
	ID3D11Device*			Device;
	ID3D11DeviceContext*	Context;
	IDXGISwapChain1*		SwapChain;
	ID3D11RenderTargetView*	RTV;
	ID3D11VertexShader*		VertexShader;
	ID3D11PixelShader*		PixelShader;
	ID3D11Buffer*			ParamsCB;
	DComp_Device*			DComp;
	DComp_Target*			Target;
	DComp_Visual*			Visual;
	uint32_t				ClientW;
	uint32_t				ClientH;
	uint32_t				RenderedW;
	uint32_t				RenderedH;
	uint32_t				AllocW;
	uint32_t				AllocH;
	BOOL					InLiveResize;
	BOOL					InRepaint;
	BOOL					Ready;
};

static App_State g_app;

static uint32_t round_bucket(uint32_t v) {
	uint32_t rounded = ( ( v + kResizeBucketPx - 1u ) / kResizeBucketPx ) * kResizeBucketPx;
	return( ( rounded > 0u ) ? rounded : kResizeBucketPx );
}

static void bind_backbuffer(void) {
	com_release(g_app.RTV);
	ID3D11Texture2D* backbuffer = 0;
	if ( FAILED(g_app.SwapChain->lpVtbl->GetBuffer(g_app.SwapChain, 0, &IID_ID3D11Texture2D, (void**)&backbuffer)) ) {
		return;
	}
	D3D11_TEXTURE2D_DESC desc = {0};
	backbuffer->lpVtbl->GetDesc(backbuffer, &desc);
	g_app.AllocW = desc.Width;
	g_app.AllocH = desc.Height;
	g_app.Device->lpVtbl->CreateRenderTargetView(g_app.Device, (ID3D11Resource*)backbuffer, 0, &g_app.RTV);
	com_release(backbuffer);
}

static void resize_swapchain(uint32_t w, uint32_t h) {
	g_app.Context->lpVtbl->OMSetRenderTargets(g_app.Context, 0, 0, 0);
	com_release(g_app.RTV);
	g_app.Context->lpVtbl->Flush(g_app.Context);
	if ( SUCCEEDED(g_app.SwapChain->lpVtbl->ResizeBuffers(g_app.SwapChain, 0, w, h, DXGI_FORMAT_UNKNOWN, 0u)) ) {
		bind_backbuffer();
	}
}

static void render_frame(void) {
	if ( !g_app.Ready || ( g_app.ClientW == 0 ) || ( g_app.ClientH == 0 ) ) {
		return;
	}

	BOOL size_changed = ( g_app.ClientW != g_app.RenderedW ) || ( g_app.ClientH != g_app.RenderedH );
	BOOL resized = FALSE;

	if ( g_app.InLiveResize ) {
		if ( ( g_app.ClientW > g_app.AllocW ) || ( g_app.ClientH > g_app.AllocH ) ) {
			uint32_t bw = round_bucket(( g_app.ClientW > g_app.AllocW ) ? g_app.ClientW : g_app.AllocW);
			uint32_t bh = round_bucket(( g_app.ClientH > g_app.AllocH ) ? g_app.ClientH : g_app.AllocH);
			resize_swapchain(bw, bh);
			resized = TRUE;
		}
	}
	else if ( ( g_app.ClientW != g_app.AllocW ) || ( g_app.ClientH != g_app.AllocH ) ) {
		resize_swapchain(g_app.ClientW, g_app.ClientH);
		resized = TRUE;
	}

	if ( g_app.RTV == 0 ) {
		return;
	}

	g_app.RenderedW = g_app.ClientW;
	g_app.RenderedH = g_app.ClientH;

	const float clear[4] = { 0.08f, 0.09f, 0.11f, 1.0f };
	const float params[4] = { (float)g_app.ClientW, (float)g_app.ClientH, 0.0f, 0.0f };
	D3D11_VIEWPORT vp = { 0.0f, 0.0f, (float)g_app.ClientW, (float)g_app.ClientH, 0.0f, 1.0f };

	g_app.Context->lpVtbl->UpdateSubresource(g_app.Context, (ID3D11Resource*)g_app.ParamsCB, 0, 0, params, 0, 0);
	g_app.Context->lpVtbl->OMSetRenderTargets(g_app.Context, 1, &g_app.RTV, 0);
	g_app.Context->lpVtbl->RSSetViewports(g_app.Context, 1, &vp);
	g_app.Context->lpVtbl->ClearRenderTargetView(g_app.Context, g_app.RTV, clear);
	g_app.Context->lpVtbl->IASetPrimitiveTopology(g_app.Context, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	g_app.Context->lpVtbl->VSSetConstantBuffers(g_app.Context, 0, 1, &g_app.ParamsCB);
	g_app.Context->lpVtbl->VSSetShader(g_app.Context, g_app.VertexShader, 0, 0);
	g_app.Context->lpVtbl->PSSetShader(g_app.Context, g_app.PixelShader, 0, 0);
	g_app.Context->lpVtbl->Draw(g_app.Context, 3, 0);

	UINT sync_interval = ( g_app.InLiveResize && ( size_changed || resized ) ) ? 0u : 1u;
	g_app.SwapChain->lpVtbl->Present(g_app.SwapChain, sync_interval, 0u);

	if ( size_changed || resized ) {
		DComp_RectF clip = { 0.0f, 0.0f, (float)g_app.ClientW, (float)g_app.ClientH };
		g_app.Visual->lpVtbl->SetClipRect(g_app.Visual, &clip);
		g_app.DComp->lpVtbl->Commit(g_app.DComp);
	}
}

static LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
	switch ( msg ) {
		case WM_ERASEBKGND: {
				return( 1 );
			}
		case WM_ENTERSIZEMOVE: {
				g_app.InLiveResize = TRUE;
			}
			break;
		case WM_EXITSIZEMOVE: {
				g_app.InLiveResize = FALSE;
			}
			break;
		case WM_SIZE: {
				g_app.ClientW = (uint32_t)LOWORD(lp);
				g_app.ClientH = (uint32_t)HIWORD(lp);
				return( 0 );
			}
		case WM_PAINT: {
				ValidateRect(hwnd, 0);
				if ( g_app.InLiveResize && !g_app.InRepaint ) {
					g_app.InRepaint = TRUE;
					render_frame();
					g_app.InRepaint = FALSE;
				}
				return( 0 );
			}
		case WM_DESTROY: {
				PostQuitMessage(0);
				return( 0 );
			}
		default: break;
	}
	return( DefWindowProcW(hwnd, msg, wp, lp) );
}

static BOOL init_graphics(HWND hwnd) {
	D3D_FEATURE_LEVEL levels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0 };
	UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_SINGLETHREADED;
	if ( FAILED(D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, flags, levels, 2, D3D11_SDK_VERSION, &g_app.Device, 0, &g_app.Context)) ) {
		return( FALSE );
	}

	IDXGIDevice* dxgi_device = 0;
	IDXGIAdapter* adapter = 0;
	IDXGIFactory2* factory = 0;
	HRESULT hr = g_app.Device->lpVtbl->QueryInterface(g_app.Device, &IID_IDXGIDevice, (void**)&dxgi_device);
	if ( SUCCEEDED(hr) ) {
		hr = dxgi_device->lpVtbl->GetAdapter(dxgi_device, &adapter);
	}
	if ( SUCCEEDED(hr) ) {
		hr = adapter->lpVtbl->GetParent(adapter, &IID_IDXGIFactory2, (void**)&factory);
	}
	if ( SUCCEEDED(hr) ) {
		hr = DCompositionCreateDevice(dxgi_device, &kIIDDCompDevice, (void**)&g_app.DComp);
	}

	DXGI_SWAP_CHAIN_DESC1 sc_desc = {0};
	sc_desc.Width				= ( g_app.ClientW > 0 ) ? g_app.ClientW : 1u;
	sc_desc.Height				= ( g_app.ClientH > 0 ) ? g_app.ClientH : 1u;
	sc_desc.Format				= DXGI_FORMAT_B8G8R8A8_UNORM;
	sc_desc.SampleDesc.Count	= 1;
	sc_desc.BufferUsage			= DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sc_desc.BufferCount			= 3;
	sc_desc.Scaling				= DXGI_SCALING_STRETCH;
	sc_desc.SwapEffect			= DXGI_SWAP_EFFECT_FLIP_DISCARD;
	sc_desc.AlphaMode			= DXGI_ALPHA_MODE_IGNORE;

	if ( SUCCEEDED(hr) ) {
		hr = factory->lpVtbl->CreateSwapChainForComposition(factory, (IUnknown*)g_app.Device, &sc_desc, 0, &g_app.SwapChain);
	}
	if ( SUCCEEDED(hr) ) {
		hr = g_app.DComp->lpVtbl->CreateTargetForHwnd(g_app.DComp, hwnd, TRUE, &g_app.Target);
	}
	if ( SUCCEEDED(hr) ) {
		hr = g_app.DComp->lpVtbl->CreateVisual(g_app.DComp, &g_app.Visual);
	}
	if ( SUCCEEDED(hr) ) {
		DComp_RectF clip = { 0.0f, 0.0f, (float)sc_desc.Width, (float)sc_desc.Height };
		hr = g_app.Visual->lpVtbl->SetContent(g_app.Visual, (IUnknown*)g_app.SwapChain);
		if ( SUCCEEDED(hr) ) {
			hr = g_app.Visual->lpVtbl->SetClipRect(g_app.Visual, &clip);
		}
		if ( SUCCEEDED(hr) ) {
			hr = g_app.Target->lpVtbl->SetRoot(g_app.Target, g_app.Visual);
		}
		if ( SUCCEEDED(hr) ) {
			hr = g_app.DComp->lpVtbl->Commit(g_app.DComp);
		}
	}

	com_release(factory);
	com_release(adapter);
	com_release(dxgi_device);
	if ( FAILED(hr) ) {
		return( FALSE );
	}

	ID3DBlob* vs_blob = 0;
	ID3DBlob* ps_blob = 0;
	hr = D3DCompile(g_shader_src, sizeof(g_shader_src) - 1, 0, 0, 0, "VSMain", "vs_5_0", 0, 0, &vs_blob, 0);
	if ( SUCCEEDED(hr) ) {
		hr = D3DCompile(g_shader_src, sizeof(g_shader_src) - 1, 0, 0, 0, "PSMain", "ps_5_0", 0, 0, &ps_blob, 0);
	}
	if ( SUCCEEDED(hr) ) {
		hr = g_app.Device->lpVtbl->CreateVertexShader(g_app.Device, vs_blob->lpVtbl->GetBufferPointer(vs_blob), vs_blob->lpVtbl->GetBufferSize(vs_blob), 0, &g_app.VertexShader);
	}
	if ( SUCCEEDED(hr) ) {
		hr = g_app.Device->lpVtbl->CreatePixelShader(g_app.Device, ps_blob->lpVtbl->GetBufferPointer(ps_blob), ps_blob->lpVtbl->GetBufferSize(ps_blob), 0, &g_app.PixelShader);
	}
	com_release(vs_blob);
	com_release(ps_blob);
	if ( FAILED(hr) ) {
		return( FALSE );
	}

	D3D11_BUFFER_DESC cb_desc = {0};
	cb_desc.ByteWidth	= 16;
	cb_desc.Usage		= D3D11_USAGE_DEFAULT;
	cb_desc.BindFlags	= D3D11_BIND_CONSTANT_BUFFER;
	if ( FAILED(g_app.Device->lpVtbl->CreateBuffer(g_app.Device, &cb_desc, 0, &g_app.ParamsCB)) ) {
		return( FALSE );
	}

	bind_backbuffer();
	g_app.RenderedW = g_app.ClientW;
	g_app.RenderedH = g_app.ClientH;
	g_app.Ready = TRUE;
	return( g_app.RTV != 0 );
}

int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show) {
	(void)prev;
	(void)cmd;
	SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);

	WNDCLASSEXW wc = { sizeof(wc) };
	wc.style			= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= wnd_proc;
	wc.hInstance		= inst;
	wc.hCursor			= LoadCursorW(0, (LPCWSTR)IDC_ARROW);
	wc.lpszClassName	= L"D3D11DCompTriangle";
	RegisterClassExW(&wc);

	RECT rc = { 0, 0, 960, 600 };
	DWORD style = WS_OVERLAPPEDWINDOW;
	DWORD ex_style = WS_EX_APPWINDOW | WS_EX_NOREDIRECTIONBITMAP;
	AdjustWindowRectEx(&rc, style, FALSE, ex_style);

	HWND hwnd = CreateWindowExW(
		ex_style, wc.lpszClassName, L"D3D11 + DirectComposition",
		style, CW_USEDEFAULT, CW_USEDEFAULT,
		rc.right - rc.left, rc.bottom - rc.top,
		0, 0, inst, 0);

	RECT client = {0};
	GetClientRect(hwnd, &client);
	g_app.ClientW = (uint32_t)( client.right - client.left );
	g_app.ClientH = (uint32_t)( client.bottom - client.top );

	if ( !init_graphics(hwnd) ) {
		return( 1 );
	}

	ShowWindow(hwnd, show);

	MSG msg = {0};
	BOOL running = TRUE;
	while ( running ) {
		while ( PeekMessageW(&msg, 0, 0, 0, PM_REMOVE) ) {
			if ( msg.message == WM_QUIT ) {
				running = FALSE;
				break;
			}
			TranslateMessage(&msg);
			DispatchMessageW(&msg);
		}
		if ( running ) {
			render_frame();
		}
	}

	if ( g_app.Target != 0 ) {
		g_app.Target->lpVtbl->SetRoot(g_app.Target, 0);
		if ( g_app.DComp != 0 ) {
			g_app.DComp->lpVtbl->Commit(g_app.DComp);
		}
	}
	com_release(g_app.Visual);
	com_release(g_app.Target);
	com_release(g_app.DComp);
	com_release(g_app.ParamsCB);
	com_release(g_app.PixelShader);
	com_release(g_app.VertexShader);
	com_release(g_app.RTV);
	com_release(g_app.SwapChain);
	com_release(g_app.Context);
	com_release(g_app.Device);
	return( (int)msg.wParam );
}