8000 GitHub - arminkz/VKE: a library to make a Vulkan programmer's life easier · GitHub
[go: up one dir, main page]

Skip to content
arminkzPublic

About

a library to make a Vulkan programmer's life easier

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VKE

The Vulkan boilerplate shared by Aquarium, SolarSystem, MassSpring, vkRadix and RayTracer, extracted into one library.

Everything lives in namespace vke. Nothing in the five projects has been modified yet — this repository is the library only.

#include "vke/vke.h"

vke::WindowConfig config;
config.title = "My App";
config.createRenderer = [](auto ctx, auto swapChain) {
    return std::make_unique<MyScene>(std::move(ctx), std::move(swapChain));
};

vke::Window window(config); // throws std::runtime_error on failure
window.startRenderingLoop();

Modules

Each layer is a CMake option, so a headless compute consumer links the core without pulling in glm, SDL3 or ImGui.

Option Default Contents Extra dependencies
(always) — VulkanContext, VulkanHelper, Buffer, DescriptorSetLayout, DescriptorSet, ComputePipeline, ShaderSource, AssetPath, Singleton Vulkan, spdlog
VKE_WITH_GRAPHICS ON SwapChain, RenderPass, FrameBuffer, GraphicsPipeline, Texture2D, TextureCubemap, TextureSampler, StorageImage stb_image
VKE_WITH_GEOMETRY ON VertexTraits, Vertex, HostMesh, DeviceMesh, MeshFactory, ObjLoader, cameras glm, tinyobjloader
VKE_WITH_APP ON Window, FramePresenter, Renderer, GUI SDL3, Dear ImGui
VKE_WITH_RAYTRACING OFF VulkanRT, RayTracingPipeline, ShaderBindingTable, BLAS, TLAS —

vke/vke.h includes whatever is enabled; the module macros (VKE_WITH_*) are public compile definitions, so your own code can branch on them too.

Building

A standalone checkout builds the library and examples with no extra flags. The Vulkan SDK (with glslc or glslangValidator) is the only prerequisite:

cmake -S . -B build
cmake --build build -j
./build/examples/vke_example_vertex_layouts

The library does not vendor its dependencies. Each one is resolved in order:

  1. a target the enclosing build already defines (spdlog::spdlog, SDL3::SDL3, …)
  2. a local checkout under VKE_EXTERNAL_DIR, which is expected to contain spdlog/, glm/, SDL/, imgui/, stb/ and tinyobjloader/. Override any of them individually with VKE_SPDLOG_DIR, VKE_GLM_DIR, VKE_SDL_DIR, VKE_IMGUI_DIR, VKE_STB_DIR and VKE_TINYOBJ_DIR
  3. an installed package (spdlog, glm and SDL3 only)
  4. a pinned, hash-checked archive downloaded into build/_deps. Turn this off with -DVKE_FETCH_DEPENDENCIES=OFF for offline builds

So a project that already has everything under external/ never downloads anything:

cmake -S . -B build -DVKE_EXTERNAL_DIR=/path/to/project/external

Consuming it from a project:

add_subdirectory(../VKE VKE)
target_link_libraries(MyApp PRIVATE vke::vke)

# Compiles shaders/*.vert|frag to <build>/shaders/*.spv and
# defines VKE_SHADER_DIR for the target.
vke_compile_shaders(MyApp SHADERS ${CMAKE_CURRENT_SOURCE_DIR}/shaders/scene.vert ...)

Headless / compute-only (the vkRadix case):

set(VKE_WITH_GRAPHICS   OFF CACHE BOOL "" FORCE)
set(VKE_WITH_GEOMETRY   OFF CACHE BOOL "" FORCE)
set(VKE_WITH_APP        OFF CACHE BOOL "" FORCE)
add_subdirectory(../VKE VKE)

Examples

Built by default in a standalone build (VKE_BUILD_EXAMPLES).

  • vertex_layouts — no window, no GPU. Instantiates the header-only geometry templates against three different vertex layouts and asserts the derived vertex-input descriptions. This is the library's smoke test: nothing in the library itself instantiates those templates.
  • spinning_mesh — a full windowed application: procedural mesh, turntable camera, per-frame uniform buffers, ImGui panel. The reference for what a project's own scene class should look like after migrating.

Design notes

These are the places where the library deliberately differs from the code it was extracted from. They are also the things that need attention when migrating.

VulkanContext is configuration-driven

vkRadix had already generalised this; the library adopts its shape. There is no longer an SDL dependency in the context — the caller supplies a surface via a callback, so the same class serves windowed and headless use.

auto config = vke::VulkanContextConfig::forGraphics("My App");
config.instanceExtensions = /* SDL_Vulkan_GetInstanceExtensions */;
config.createSurface = [window](VkInstance i) { /* SDL_Vulkan_CreateSurface */ }
8A62
;
config.deviceExtensions.push_back(VK_KHR_SOME_EXTENSION_NAME);

Device extensions double as the physical-device suitability filter, so adding one automatically excludes GPUs that lack it. Queue requirements, descriptor pool sizes, the feature pNext chain and an onDeviceCreated hook are all config fields — the ray tracer wires vke::rt::loadFunctions into that hook rather than the context knowing anything about ray tracing.

vke::Window fills all of this in for the common windowed case; use WindowConfig::configureContext to adjust it.

FramePresenter takes a renderer factory

It used to construct the scene class by name (new BoidScene(...)), which is the one thing that made the file un-shareable. It now takes a RendererFactory callback, invoked once the swapchain exists.

Renderer's input handlers are no longer pure virtual, so a scene overrides only what it uses. MAX_FRAMES_IN_FLIGHT became FramePresenterConfig::framesInFlight (default 2).

Meshes are templated on the vertex type

The five projects disagree about what a vertex is — the rasterising ones carry position, colour, UV, normal and tangent; the ray tracer packs position and normal to 16 bytes. Rather than pick one, HostMesh<V>, DeviceMesh<V>, MeshFactory and ObjLoader work off whichever of five canonical members a struct declares:

member type shader location
pos glm::vec3 0
color glm::vec4 1
texCoord glm::vec2 2
normal glm::vec3 3
tangent glm::vec3 4

Locations are fixed, not packed: a vertex with only pos and normal gets attributes at locations 0 and 3, so adding a member to a struct never silently renumbers the others out from under your shaders. Members you don't declare are skipped by the generators and produce no vertex attribute. Extra members (padding, custom data) are ignored.

struct MyVertex { glm::vec3 pos; float pad0; glm::vec3 normal; float pad1; };

auto mesh = vke::MeshFactory::createSphereMesh<MyVertex>(1.0f, 48, 32);

vke::PipelineParams params;
vke::useVertexType<MyVertex>(params);   // fills in binding + attributes

vke::Vertex is the stock five-member struct, and vke::convertMesh<Dst>(src) translates between layouts.

Descriptors: layouts are separate, sets are updatable

The library keeps vkRadix's split API rather than the one-shot DescriptorSet(ctx, {Descriptor...}) form that four projects use. A DescriptorSetLayout is a shareable object several sets allocate from, and DescriptorSet::update() can be called repeatedly — which is what per-frame uniforms and post-resize storage image rebinds actually need.

auto layout = std::make_shared<vke::DescriptorSetLayout>(ctx,
    std::vector<vke::DescriptorSetLayout::Binding>{
        { 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT },
    });

vke::DescriptorSet set(ctx, layout);
set.update({ vke::DescriptorWrite{ 0, buffer->getDescriptorInfo() } });

Acceleration structures are written by handle; update() builds the pNext chain itself.

This is the largest migration cost. Every descriptor call site in Aquarium, SolarSystem, MassSpring and RayTracer has to be rewritten.

Shaders: file path or embedded blob

ShaderSource is implicitly constructible from a path or from a SpirvCode view, so both pipeline classes accept either and a project can move to build-time-embedded shaders without touching call sites.

AssetPath root is set at runtime

It used to read an ASSET_DIR compile definition, which would now be captured when the library is compiled rather than the application. Set it explicitly from the application, where the macro means what you expect:

VKE_ASSET_PATH_INIT();                          // uses the app's ASSET_DIR
vke::AssetPath::getInstance()->setRoot(dir);    // or set it directly

Behaviour changes worth knowing

  • BLAS::getDeviceAddress() returns the acceleration structure's address from vkGetAccelerationStructureDeviceAddressKHR. The original returned the backing buffer's address; the two usually coincide, but nothing guarantees it. TLAS instances must reference the former.
  • Texture2D's data constructor set the sampler's maxLod from its mipLevels parameter, so the default of 0 disabled mipmapping. It now uses the resolved level count.
  • SwapChain::firstTimeCreation was a static shared across all instances; it is now a member, so the creation log appears once per swapchain.
  • Buffer adds VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT itself when asked for a device address, instead of relying on the caller to remember.
  • TLAS::update() rejects a changed instance count rather than corrupting the refit.
  • RenderPass and GraphicsPipeline take their params by const reference; PipelineParams no longer defaults its vertex input to a specific Vertex.

Migration order

The projects are not modified yet. When they are, roughly:

  1. vkRadix — smallest surface, core-only, and its VulkanContext is already the one the library adopted. Mostly a namespace and include-path change.
  2. SolarSystem or MassSpring — a straightforward graphics app; the real work is the descriptor rewrite and deleting the duplicated files.
  3. Aquarium — same, plus compute pipelines.
  4. RayTracer — the ray tracing module, plus deciding whether its scene keeps its packed vertex (it can: name it in the templates) or moves to vke::Vertex.

Per project the deletions are src/vulkan/, src/core/, src/gui/, src/geometry/, src/utils/ and the camera files — roughly 3,000 lines each.

About

a library to make a Vulkan programmer's life easier

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0