Init & Cleanup
Initialization Sequence
The init(int width, int height, const char* title) method performs the full boot sequence:
glfwInit()-- initialize window systemglfwCreateWindow()-- create window withGLFW_NO_API(no OpenGL)- Set callbacks:
framebufferResizeCallback,scrollCallback createInstance()-- Vulkan instance with MoltenVK portability extensionscreateSurface()-- window surface via GLFWpickPhysicalDevice()-- select first suitable GPUcreateLogicalDevice()-- create device + graphics/present queuescreateSwapchain()-- prefer B8G8R8A8_SRGB + MAILBOX present modecreateImageViews()-- one image view per swapchain imagecreateRenderPass()-- color + depth attachments, single subpasscreateDescriptorSetLayout()-- set 0: UBO (binding 0) + LightUBO (binding 1)createMaterialDescriptorSetLayout()-- set 1: COMBINED_IMAGE_SAMPLER (binding 0)createGraphicsPipeline()-- 3D pipeline: depth test, backface cull, no blendcreateCommandPool()-- resettable command bufferscreateDepthResources()-- depth image + view (D32_SFLOAT preferred)createFramebuffers()-- one per swapchain image (color + depth)createUniformBuffers()-- view/proj UBO + light UBO, per frame-in-flight (2), persistently mappedcreateDescriptorPool()-- UBO descriptorscreateDescriptorSets()-- bind UBO + light buffers to descriptor setscreateTextureSampler()-- LINEAR filter, REPEAT wrapcreateDefaultTexture()-- 1x1 white RGBA pixelcreateMaterialDescriptorPool()-- up to 64 material descriptorscreateMaterial(defaultTextureView_)-- default material (id 0)createCommandBuffers()-- one per frame-in-flightcreateSyncObjects()-- 2 imageAvailable semaphores, 2 renderFinished semaphores, 2 fences (pre-signaled)createUIDescriptorSetLayout()-- COMBINED_IMAGE_SAMPLER for font atlascreateUIPipeline()-- no depth, alpha blending, no cullingcreateUIVertexBuffers()-- host-visible, persistently mapped, 4096 max vertices per framecreateFontResources()-- load TTF via stb_truetype -> 512x512 R8_UNORM atlas (or 1x1 placeholder)createUIDescriptorPool()+createUIDescriptorSets()-- bind font atlas
Cleanup Order
cleanup() tears down in reverse dependency order:
vkDeviceWaitIdle()-- wait for all GPU workcleanupUIResources()-- UI pipeline, font atlas, UI vertex buffers, UI descriptorscleanupMaterialResources()-- material textures, sampler, descriptor pool, layout, default texturecleanupSwapchain()-- depth resources, framebuffers, image views, swapchain- Uniform buffers + light buffers (per frame)
- Sync objects (semaphores + fences)
- Geometry buffers (vertex + index)
- Descriptor pool + layout
- Graphics pipeline + pipeline layout
- Render pass
- Command pool
- Logical device
- Surface
- Instance
- GLFW window + terminate
Swapchain Recreation
On window resize (or VK_ERROR_OUT_OF_DATE_KHR):
- Wait for zero-size framebuffer to resolve (minimized window)
vkDeviceWaitIdle()cleanupSwapchain()-- destroy depth, framebuffers, image views, old swapchaincreateSwapchain()->createImageViews()->createDepthResources()->createFramebuffers()
Where to Edit
Adding new GPU resources to init/cleanup: Add the create call in init() after any resources it depends on. Add the matching destroy call in cleanup() before destroying its dependencies. If the resource depends on swapchain extent, also add recreate logic to recreateSwapchain().