platform-glfw-d3d12
Garnet GLFW Direct3D 12 host
require "garnet-platform-glfw-d3d12" provides Garnet::Platform::D3D12GLFWHost.open. It creates a GLFW window with no client API (GLFW_CLIENT_API = GLFW_NO_API), takes its HWND from glfwGetWin32Window, and presents through a DXGI flip-model swapchain (DXGI_SWAP_EFFECT_FLIP_DISCARD) with resize handling and vsync on or off.
The host never creates a Direct3D 12 device. The code that renders (the Garnet Direct3D 12 renderer, or an application using d3d12.cr directly) passes its device and direct command queue to create_presenter, just as platform-glfw-vulkan hands its window surface to the Vulkan renderer.
Installation
dependencies:
garnet-platform-glfw-d3d12:
github: garnet-engine/platform-glfw-d3d12
branch: main
Windows 10 or 11 (x64) only, with the MSVC toolchain Crystal uses (Visual Studio 2022 Build Tools with the C++ workload and a Windows SDK) and the prebuilt GLFW 3.4 binaries: add the lib-vc2022 directory of glfw-3.4.bin.WIN64.zip to CRYSTAL_LIBRARY_PATH (the glfw shard links glfw3dll.lib) and to PATH, or copy glfw3.dll next to the executable. Direct3D 12, DXGI and d3dcompiler_47.dll ship with Windows.
On other platforms the shard compiles, so dependents can be type-checked, but opening a host raises Garnet::Platform::D3D12HostError.
Usage
require "garnet-platform-glfw-d3d12"
Garnet::Platform::D3D12GLFWHost.open(1280, 720, "Game") do |host|
factory = D3D12::Factory.create
device = D3D12::Device.create(factory.default_adapter)
queue = device.create_command_queue
presenter = host.create_presenter(device, queue, buffer_count: 3)
host.vsync = false # Present(0) with tearing where supported
while host.poll_events
next unless host.begin_frame # resizes with the window; false while minimised
# Record: back buffer Present -> RenderTarget, draw into
# presenter.render_target_view, RenderTarget -> Present; execute on queue.
host.present
end
end
D3D12Presenter owns the swapchain, its back buffers, one render target view per buffer (optionally with a different view_format, such as the sRGB variant) and a fence. begin_frame resizes the buffers when the framebuffer changed (after waiting for the GPU) and waits until the current back buffer is free; present presents with sync interval 1, or 0 plus DXGI_PRESENT_ALLOW_TEARING when vsync is off and tearing is supported, and signals the fence. capture(list) records a readback copy of the back buffer; read it with D3D12FrameCapture#rgba after wait_idle.
The presenter may be created on a render thread (the HWND is captured on the main thread when the host opens). Window calls and event polling stay on the main thread. Closing the host closes the presenter first; the device and queue stay with their owner.
The host also reports framebuffer_size, logical_size, focused?, iconified? and closed?, and offers wait_events, matching the other GLFW hosts. Feed input with Garnet::Platform::GLFWInput from garnet-platform-glfw/input, which works with any GLFW window.
Example
examples/triangle.cr draws a spinning triangle with d3d12.cr directly:
shards install
$env:GARNET_D3D12_WARP = "1" # optional: WARP software adapter
$env:GARNET_EXAMPLE_FRAMES = "30" # optional: stop after 30 frames
$env:GARNET_CAPTURE = "shots\triangle.png" # optional: write the last frame
crystal run examples/triangle.cr
GARNET_VSYNC=0 presents without waiting for vertical blank.
CI
The Windows job builds the example, presents 30 frames on WARP, reads the last back buffer back into a PNG, checks its pixels (.github/scripts/check_capture.py), uploads it as an artifact and runs the host spec (hidden window, presenting, capture, resize, vsync). The Linux job type-checks the host and cross-compiles the example and spec for x86_64-pc-windows-msvc; lint runs Ameba.
Contributing
- Fork it and create a branch.
- Keep
crystal tool format --checkandamebaclean. - Open a pull request.
License
MIT; see LICENSE.
platform-glfw-d3d12
- 0
- 0
- 0
- 0
- 2
- about 1 hour ago
- December 3, 2026
MIT License
Thu, 03 Dec 2026 12:16:38 GMT