Repository navigation
05 Native Layer
English | 中文
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
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.
| 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.
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.
-
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.
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_Record runs after the scene pass and before readback/present:
- copy the swapchain into a sampleable texture;
- half-resolution soft-knee bright-pass extraction;
- horizontal + vertical Gaussian blur;
-
scene + bloom * strength, multiplied by exposure, with optional ACES/Reinhard; - write back into the swapchain.
With post-processing off, nothing is recorded here and the image is identical to a build without the feature.
Screenshots and recording share one readback path:
-
Vulkan_RequestReadbackflags 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/SaveScreenshotRawBGRAfetches 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.
- MoltenVK maps Vulkan onto Metal;
- the swapchain is a
CAMetalLayerdrawable, unreadable after present, so readback must complete in the same command buffer; - MSAA is capped at 4×;
-
libMoltenVKmust be installed at runtime (brew install molten-vk).
-
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