Skip to content

05 Native Layer

Tagin T edited this page Nov 24, 2026 · 2 revisions

English | 中文

05 The Native Vulkan Layer

Initialisation

Render_Init (exposed as Vulkan_InitEx on Windows; MoltenVK on macOS) creates, in order:

instance → surface → physical device → logical device
  → swapchain → image views → render pass
  → graphics pipelines → framebuffers
  → command pool / buffers → sync objects
  → vertex / mesh / texture buffers

Key constants:

  • swapchain format: B8G8R8A8_UNORM
  • MSAA: 8× on Windows, 4× on macOS/MoltenVK (a Metal limit)
  • depth: D32_SFLOAT
  • output: 1920×1080, 60 fps

Render pass

The scene pass has three attachments:

# Content Notes
0 MSAA colour multisampled colour target
1 resolve target resolved into the swapchain image (PRESENT_SRC)
2 depth depth testing for meshes

The painter pipeline never writes depth; the mesh pipeline does. That is what lets "mesh first, painter second" keep both painter coverage semantics and per-pixel mesh occlusion.

Five pipelines

Pipeline Vertex layout Depth Used for
solid painter x,y,z,r,g,b,a (7) test, no write rect/circle/line/polygon/bezier/text
textured x,y,z,u,v,alpha (6) test, no write ImageMobject, camera frames
mesh x,y,z,r,g,b,a (7) test + write unlit MeshBatch
lit mesh x,y,z,nx,ny,nz,r,g,b,a (10) test + write meshes with lit=True
post full-screen triangle none bloom/tonemap

Solid and textured z come from the Python paint unit's depth; mesh z is normalised camera-space depth; lit mesh also carries normals and material.

Recording one frame

Render_DrawScene(...)         # after Python has submitted its commands
  ├─ replay the CMD_* list
  ├─ PushVertex / BuildVerticesFromImageQuad
  ├─ upload solid / mesh / lit / image vertices
  └─ Tex_EndFrame()

Vulkan_Tick
  └─ Render_DrawFrame
       ├─ acquire image
       ├─ RecordCommandBuffer
       │    ├─ vkCmdBeginRenderPass
       │    ├─ mesh / lit mesh (depth write)
       │    ├─ solid painter
       │    ├─ Tex_RecordInterleaved (images inserted at their submission point)
       │    ├─ vkCmdEndRenderPass
       │    ├─ Post_Record (optional)
       │    └─ readback copy (optional)
       ├─ vkQueueSubmit
       └─ vkQueuePresentKHR

Render_DrawScene only builds vertices and commands; the actual GPU draw happens in RecordCommandBuffer. All geometry can therefore be gathered first and submitted once.

Vertex buffers and synchronisation

  • g_vertices: solid painter, 7 floats/vertex.
  • g_mesh_vertices / g_mesh_lit_vertices: mesh, 7/10 floats/vertex.
  • g_tex_vertices: images, 6 floats/vertex.
  • These host-visible buffers are shared across frames; before writing, Render_WaitAllInFlight() waits for every in-flight fence so the previous frame's GPU is not still reading them.

MAX_VERTICES is large (4M), but exceeding it still drops vertices, so text/curve-heavy scenes should watch diagnostics such as RtmTest_VertexCount.

Why images are "interleaved"

Painter and images are separate pipelines, but submission order must survive. Tex_RecordInterleaved records how many solid vertices precede each image quad:

draw solid[0..n1) → bind texture → draw image 1
draw solid[n1..n2) → bind texture → draw image 2
...

Otherwise an image drawn last would cover text and shapes that were submitted after it.

Post-processing (optional)

Post_Record runs after the scene pass and before readback/present:

  1. copy the swapchain into a sampleable texture;
  2. half-resolution soft-knee bright-pass extraction;
  3. horizontal + vertical Gaussian blur;
  4. scene + bloom * strength, multiplied by exposure, with optional ACES/Reinhard;
  5. write back into the swapchain.

With post-processing off, nothing is recorded here and the image is identical to a build without the feature.

Readback

Screenshots and recording share one readback path:

  • Vulkan_RequestReadback flags the next frame to copy before present;
  • Platform_RecordReadbackCopy (Windows) or MoltenVK's two-stage path copies the swapchain into a host-visible staging buffer;
  • Python SaveScreenshotRaw / SaveScreenshotRawBGRA fetches the pixels;
  • the row stride may be padded, so encoding/PNG writing must honour row_bytes.

Readback happens before present because the presentation engine may repurpose the image afterwards.

macOS differences

  • MoltenVK maps Vulkan onto Metal;
  • the swapchain is a CAMetalLayer drawable, unreadable after present, so readback must complete in the same command buffer;
  • MSAA is capped at 4×;
  • libMoltenVK must be installed at runtime (brew install molten-vk).

Key files

  • native/vulkan_init.c: instance/device/swapchain/render pass/pipelines
  • native/vulkan_draw.c: command replay, vertex upload, command buffer recording, present
  • native/vulkan_texture.c: image textures and interleaved draws
  • native/vulkan_post.c: bloom/tonemap
  • native/platform.c / native/platform_mac.m: platform entry points, readback

Clone this wiki locally