Dahlia icon

Active2024 – now

Dahlia

A cross-platform C++20 graphics engine with OpenGL, Vulkan and Metal backends.

  • C++20
  • Vulkan
  • Metal
  • OpenGL
  • SPIR-V
  • SPIRV-Cross
  • Dear ImGui
  • msdfgen
  • CMake
The same DahliaInfo build on all three backends, each with its renderer, shading language and compute self-test.
3 Graphics backends
17k Lines of engine C++
43 Commits
2 Apps built on it

One graphics API in front of three backends. Shaders are written once in GLSL, compiled to SPIR-V and translated at load time, so application code is the same whichever backend runs it.

Dahlia is the engine underneath my other graphical projects. It exposes one rendering API with buffers, pipelines, bind groups, command lists and framebuffers, and implements it three times:

Backend Version Platforms
OpenGL 4.1 Everywhere (always built)
Vulkan 1.3 Wherever a Vulkan SDK or loader is found
Metal 3 macOS

Every backend follows OpenGL's conventions, so application code doesn't change when the backend does. The demo app, DahliaInfo, takes --metal, --vulkan or --opengl and reports what it is running on.

Write shaders once

Shaders are Vulkan-style GLSL (#version 450), compiled offline to SPIR-V with glslc. At load time, SPIRV-Cross translates them to whatever the active backend needs:

How Dahlia's shaders reach each backend GLSL 450 source is compiled by glslc at build time into SPIR-V. At load time Vulkan uses the SPIR-V directly, while SPIRV-Cross translates it to MSL for Metal and to GLSL 4.10 for OpenGL. BUILD TIME LOAD TIME GLSL 450 source.vert .frag .comp glslc SPIR-Vportable bytecode used directly SPIRV-Cross SPIRV-Cross Vulkan 1.3SPIR-V MetalMSL 2.4 OpenGL 4.1GLSL 4.10

Compute on Vulkan and Metal

Compute shaders, storage buffers and storage images are available on Vulkan and Metal. On OpenGL 4.1, which has no compute, GPULimits::supportsCompute is false, so code can check before it dispatches. Barriers are inserted automatically: draws and dispatches recorded after a dispatch see its writes.

auto buffer = Buffer::Create({ count * sizeof(uint32_t), BufferUsage::Storage });
auto layout = BindGroupLayout::Create({ { 0, ShaderResourceType::StorageBuffer, ShaderStage::Compute } });
auto group  = BindGroup::Create(layout, { BindGroupEntry::StorageBuffer(0, buffer) });

ComputePipelineDesc desc;
desc.computeShader    = ShaderModule::Create(ShaderStage::Compute, spirv);
desc.bindGroupLayouts = { layout };
auto pipeline = ComputePipeline::Create(desc);

cmd.BindPipeline(*pipeline);
cmd.BindGroup(0, *group);
cmd.Dispatch((count + pipeline->GetWorkgroupSize().x - 1) / pipeline->GetWorkgroupSize().x);

Libraries and command-line tools can create a headless context, with no window, GLFW, surface or swapchain. dhERI runs its GPU integral kernels this way.

Precision where it matters

Apple GPUs have no double, and Metal compiles with fast math by default. Dahlia exposes ShaderCompileFlags::PreciseMath, which compiles with MTLMathModeSafe, so error-free transformations such as double-float arithmetic survive the compiler. Vulkan devices with shaderFloat64 get native fp64.

Text, MSAA and the rest

  • Text. Glyph atlases are built from any FreeType font as multi-channel signed distance fields with msdfgen. One atlas at about 48 px per em stays sharp at every size, and text is drawn in a single instanced call with kerning, wrapping and alignment.
  • MSAA. Framebuffers render into hidden multisampled attachments (2×, 4× or 8×) and resolve automatically when the render pass ends.
  • UI and scenes. Dear ImGui (docking branch), 2D and 3D cameras, and a layer stack.