Plan-2026-11-24-editor-step2-offscreen-views

Chuck Smith edited this page 2026-11-25 01:11:24 +00:00 · 1 commit

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Editor step 2: offscreen views and theme v1 — Implementation Plan

For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (- [ ]) syntax for tracking.

Goal: Render a surface without a window into a texture that ImGui shows (the editor's viewports), read its pixels back for thumbnails, and give the editor UI its first real look (Inter + Lucide icons, spacing, a quiet dark palette).

Architecture: An offscreen surface is a normal graphics::Surface whose final target is its own RGBA16F texture instead of a swapchain image: same gpass targets, light culling, camera and UI pass, no acquire or present, and the texture ends each frame readable by shaders. UiDrawCommand::surface_id lets the UI pass sample a surface's current texture (looked up at record time). Readback copies the texture into a CPU buffer at the end of the frame and calls back from a later render_frame() once that frame's fence passed. The theme lives in EditorUI (Editor/UI/Theme.*) with the fonts downloaded and copied next to the programs.

Tech Stack: C++23, Vulkan + D3D12, Dear ImGui 1.93.0-docking, Inter 4.1, Lucide 1.79.0, IconFontCppHeaders, doctest, CMake + MSBuild.

Spec: Docs/Editor.md, section 9 "Step 2, offscreen views and theme v1: design" (and sections 5.3, 5.10).

Global Constraints

  • The user commits. No git commit, no staging. Every "Commit" step is "leave in the working tree".
  • Rules of Docs/Preferences.md: trailing return types, const, braces, designated initializers, PRIMAL_API on engine API other binaries call, logging through primal::log (error() never per frame), no issue numbers in comments, the copyright header on new files, std::expected/[[nodiscard]] for fallible new functions where a caller acts on the reason (a plain bool is fine where it doesn't).
  • Third-party files are downloaded and pinned, never committed; CMake (cmake/PrimalDependencies.cmake) and MSBuild (Editor/UI/ImGui.targets) use the same URLs and SHA-256:
    • Inter: https://github.com/rsms/inter/releases/download/v4.1/Inter-4.1.zip, 9883fdd4a49d4fb66bd8177ba6625ef9a64aa45899767dde3d36aa425756b11e; use extras/ttf/Inter-Regular.ttf, extras/ttf/Inter-SemiBold.ttf, LICENSE.txt (SIL OFL 1.1).
    • Lucide: https://github.com/lucide-icons/lucide/releases/download/1.79.0/lucide-font-1.79.0.zip, f128284e6b30a43cf3b70e191ba27fad4f7677cf4b02d4e2862e1e4b6adb25e3; use lucide-font/lucide.ttf (ISC). The zip has no license file: download https://raw.githubusercontent.com/lucide-icons/lucide/1.79.0/LICENSE (b495047bd93a9b06913511076f504daba17d5bbeb3e0650f3bb53a4220329c57) as fonts/Lucide-LICENSE.txt.
    • IconsLucide.h: https://raw.githubusercontent.com/juliettef/IconFontCppHeaders/210b5a399a64270674560d633638952d1e8d804d/IconsLucide.h, ad2e0fcef96dddadd05abc51ead1ad2966092bba79af3d12dd2e969ea2a59f8d (zlib; all 1,962 codepoints match the 1.79.0 font, checked 2026-11-24).
  • The fonts and their licenses go to x64/<Config>/fonts/ (Inter-Regular.ttf, Inter-SemiBold.ttf, lucide.ttf, Inter-LICENSE.txt, Lucide-LICENSE.txt) at build time, for every program that links EditorUI.
  • Every new source goes in a CMakeLists.txt and the matching .vcxproj (+ .filters).
  • D3D12 and Win32 code is written blind and checked in the VM (Tools/WinVM/winvm.sh: four configurations, GraphicsTests on both backends inside Invoke-Interactive with Start-Process -RedirectStandard*, not a | Out-File pipeline).
  • Verification standard of CLAUDE.md: 0 warnings (clang, GCC, MSVC), clang-tidy 0, UnitTests, GraphicsTests in Debug with validation (and sync validation) clean, ASan/UBSan with CI's noclose + lsan.supp, the EngineTest variant exits 0; the user does the visual check.

Review Focus

  1. A UI command naming an offscreen surface that was removed, or never rendered: a removed one is skipped; a never-rendered one shows black (the texture is cleared and made shader-readable at creation), no validation error. Test in Task 1.
  2. An offscreen surface resized while earlier frames still sample its old texture: the old texture is released through the deferred release; this frame's UI samples the new one (looked up at record time). Test in Task 1.
  3. An offscreen surface sampled by a window's UI in the same frame it renders: it comes earlier in the span; the barrier to shader-read happens before the window's UI pass (sync validation clean). An offscreen surface later in the span than the window that samples it shows last frame's picture, without a hazard. Test in Task 1.
  4. A readback requested and its surface removed (or graphics::shutdown()) before it completes: the callback is never called, the staging memory is freed, validation clean. Test in Task 2.
  5. Fonts missing at run time (a program copied without fonts/): load_fonts() returns false, logs a warning, and ImGui keeps its default font; nothing crashes. Test in Task 4.

Task 1: Offscreen surfaces on Vulkan, shown by the UI pass

Files:

  • Modify: Engine/Graphics/Renderer.h, Engine/Graphics/Renderer.cpp, Engine/Graphics/GraphicsPlatformInterface.h, Engine/Graphics/UiDrawData.h
  • Modify: Engine/Graphics/Vulkan/VulkanSurface.h/.cpp, VulkanCore.cpp (render_frame(), record_surface()), VulkanInterface.cpp, VulkanUiPass.cpp
  • Modify (stub, so Windows keeps building until Task 3): Engine/Graphics/Direct3D12/D3D12Interface.cpp
  • Test: GraphicsTests/OffscreenSurfaceTests.cpp (new; CMake + GraphicsTests.vcxproj)

Interfaces:

  • Produces:

    // Renderer.h
    [[nodiscard]] PRIMAL_API auto create_offscreen_surface(u32 width, u32 height) -> Surface; // invalid on failure
    [[nodiscard]] PRIMAL_API auto is_offscreen_surface(SurfaceId id) -> bool;
    // UiDrawData.h, UiDrawCommand (new field, after vertex_offset)
    id::id_type surface_id{id::invalid_id}; // valid: sample this offscreen surface's picture; texture_id is ignored
    // PlatformInterface::surface
    graphics::Surface (*create_offscreen)(u32, u32);
    bool (*is_offscreen)(SurfaceId);
    
  • Behaviour: the surface's texture is RGBA16F (VK_FORMAT_R16G16B16A16_SFLOAT) with color-attachment, sampled and transfer-src usage and a descriptor in the resource heap. acquire_next_image() always succeeds, the surface is not in the frame's present list and waits on no semaphore. record_surface(): at the start the texture goes shader-read → render target; post-process and the UI pass write it; at the end it goes to shader-read (fragment shader) instead of present. At creation it is cleared to transparent black and left shader-readable. resize() recreates it (old one deferred). The UI pass resolves surface_id to the surface's current descriptor index; a command whose surface isn't valid or isn't offscreen is skipped (log::debug once per frame at most, never an assert: the UI was built before the removal). A window surface naming itself is skipped too. D3D12's interface gets create_offscreen returning an invalid Surface and is_offscreen returning false until Task 3.

  • Step 1: Write the failing tests (suite "offscreen surface", each ends with session.shutdown_and_check()):

    • "offscreen surface: create, render, resize, remove": create 256×128 → is_valid(), width() == 256, height() == 128, is_offscreen_surface(id); render 3 frames with only it in the span; resize(64, 300) → sizes follow; 3 frames; remove.
    • "offscreen surface: shown in a window's UI": a window view and an offscreen surface; each frame the span is {offscreen, window} and the window's UiDrawData has one full-window quad with surface_id = offscreen id; 5 frames; then the offscreen surface is resized every frame for 4 frames.
    • "offscreen surface: sampled before it renders": span {window, offscreen} (window first), 5 frames; and a second offscreen surface never in any span but named by the UI.
    • "offscreen surface: removed while the UI names it": remove it, then 3 frames whose UI still names it.
    • "offscreen surface: a window surface isn't offscreen": CHECK_FALSE(is_offscreen_surface(window id)).
  • Step 2: Run cmake --build --preset linux-debug && x64/Debug/GraphicsTests -ts="offscreen surface" → fails to compile (create_offscreen_surface unknown).

  • Step 3: Implement the interface above (Vulkan + the D3D12 stub).

  • Step 4: Run the suite with PRIMAL_TEST_REQUIRE_VALIDATION=1, then the whole GraphicsTests and UnitTests → pass, validation and sync validation clean.

  • Step 5: Leave in the working tree.

Task 2: Readback on Vulkan

Files:

  • Create: Engine/Graphics/Readback.h/.cpp (shared, no backend code; CMake + Engine.vcxproj + .filters)
  • Modify: Engine/Graphics/Renderer.h/.cpp, GraphicsPlatformInterface.h, Engine/Utilities/MathSimd.h (a thin math::half_to_float(u16) -> f32 forwarder if none exists) + UnitTests/MathTests.cpp, Vulkan core/surface, D3D12Interface.cpp (stub returning false)
  • Test: UnitTests/ReadbackTests.cpp (new; CMake + UnitTests.vcxproj), GraphicsTests/OffscreenSurfaceTests.cpp

Interfaces:

  • Produces:

    // Renderer.h
    struct ReadbackImage
    {
        u32 width{0};
        u32 height{0};
        std::span<const u16> pixels; // RGBA16F half floats, rows tightly packed, top row first; valid during the callback
    };
    using ReadbackCallback = std::function<void(const ReadbackImage &)>;
    // Copies the surface's picture after the next render_frame() that renders it; the callback runs inside a later
    // render_frame() on the calling thread, at most frame_buffer_count frames after. False: not an offscreen surface.
    [[nodiscard]] PRIMAL_API auto request_readback(SurfaceId id, ReadbackCallback callback) -> bool;
    // Readback.h
    [[nodiscard]] PRIMAL_API auto to_rgba8_srgb(const ReadbackImage &image) -> std::vector<u8>; // clamped, sRGB-encoded
    
  • Behaviour: requests on a surface are recorded after its UI pass (texture → transfer-src → copy into a host-visible buffer → shader-read). A surface not rendered in that frame keeps its request for the next frame that renders it. Removing the surface or graphics::shutdown() drops its pending requests without calling them (buffers deferred).

  • Step 1: Write the failing tests:

    • UnitTests "readback: to_rgba8_srgb": a 2×1 image of halves (0.0, 0.5, 1.0, 1.0) and (2.0, -1.0, 0.2140, 0.5) → {0, 188, 255, 255, 255, 0, 128, 128} (±1; 0.2140 linear ≈ 128 sRGB, alpha stays linear).
    • GraphicsTests "offscreen surface: readback of a UI quad": 64×64 offscreen surface; its own UiDrawData has one full-surface quad with a white texture and color 0xff40'80c0 (R = 0xc0, G = 0x80, B = 0x40, A = 0xff); request a readback, render frame_buffer_count + 1 frames → the callback ran exactly once, width == 64, height == 64, and every pixel of to_rgba8_srgb() is (0xc0, 0x80, 0x40, 0xff) ±1.
    • GraphicsTests "offscreen surface: readback dropped with its surface": request, remove the surface before any frame, render 4 frames → callback never ran; validation clean.
    • GraphicsTests "offscreen surface: readback of a window surface is refused": CHECK_FALSE(request_readback(window id, ...)).
  • Step 2: Run UnitTests -ts=readback and GraphicsTests -ts="offscreen surface" → fail to compile.

  • Step 3: Implement.

  • Step 4: Run both suites, the whole UnitTests and GraphicsTests → pass, validation clean.

  • Step 5: Leave in the working tree.

Task 3: D3D12 offscreen surfaces and readback

Files:

  • Modify: Engine/Graphics/Direct3D12/D3D12Surface.h/.cpp, D3D12Core.cpp, D3D12Interface.cpp, D3D12UiPass.cpp

Interfaces:

  • Consumes: Tasks 1-2's API, unchanged. Same behaviour on D3D12: an RGBA16F (DXGI_FORMAT_R16G16B16A16_FLOAT) render texture with an RTV and an SRV in the shader-visible heap; D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ↔ RENDER_TARGET around the frame; no present; readback with CopyTextureRegion into a readback-heap buffer whose row pitch is aligned to D3D12_TEXTURE_DATA_PITCH_ALIGNMENT (the callback gets tightly packed rows: copy them out).

  • Step 1: The Task 1 and 2 GraphicsTests are the tests; run them in the VM with PRIMAL_GRAPHICS=d3d12 before implementing → the offscreen ones fail (invalid surface from the stub).

  • Step 2: Implement (blind on Linux; Windows compile database clang-tidy where it helps).

  • Step 3: VM: four configurations, 0 warnings; GraphicsTests on D3D12 (WARP) and Vulkan (lavapipe) → pass, no debug-layer warnings.

  • Step 4: Leave in the working tree.

Task 4: Theme v1 (fonts, icons, style)

Files:

  • Create: Editor/UI/Theme.h/.cpp (CMake + EditorUI.vcxproj + .filters)
  • Modify: cmake/PrimalDependencies.cmake, Editor/UI/CMakeLists.txt (download, include path for IconsLucide.h, copy to fonts/), Editor/UI/ImGui.targets (the same for MSBuild: download, hash, unzip, copy into $(OutDir)fonts\)
  • Test: UnitTests/EditorUiTests.cpp

Interfaces:

  • Produces:

    namespace primal::editor::ui {
    // Inter-Regular 15 px with Lucide's icons merged (ICON_MIN_LC..ICON_MAX_LC) as the default font, and Inter-SemiBold
    // 15 px (also with icons) as fonts().heading. False (a warning logged): a file is missing; ImGui keeps its font.
    [[nodiscard]] auto load_fonts(const std::filesystem::path &fonts_folder) -> bool;
    struct Fonts { ImFont *regular{nullptr}; ImFont *heading{nullptr}; };
    [[nodiscard]] auto fonts() -> const Fonts &;
    // Sizes below times `scale` (ImGuiStyle::ScaleAllSizes), and the palette.
    auto apply_theme(f32 scale) -> void;
    }
    

    IconsLucide.h is on EditorUI's public include path (CMake: SYSTEM PUBLIC; MSBuild: ExternalIncludePath).

  • Values (scale 1): WindowPadding (10, 10), FramePadding (8, 5), ItemSpacing (8, 6), ItemInnerSpacing (6, 6), IndentSpacing 18, ScrollbarSize 12, GrabMinSize 10; rounding: window/child/frame/popup/grab/tab 4, scrollbar 6; WindowBorderSize 1, FrameBorderSize 0, PopupBorderSize 1; WindowMenuButtonPosition = ImGuiDir_None. Palette (the rest derived from these): text #E4E4E7, text disabled #71717A, window bg #1E1E22, child bg #141417, popup bg #18181B, border #2E2E33, frame bg #27272A (hovered #303036, active #36363D), title bg #18181B (active #1E1E22), accent #4A7BA6 (hovered #5A8BB6, active #3A6A93) for buttons' active state, headers, checkmarks, slider grabs, selected tabs and the docking preview; headers and selection at 45% alpha of the accent; separators #2E2E33; plot lines/histograms stay ImGui's; warnings use #D9A441 (a named constant for later panels).

  • Step 1: Write the failing tests (suite editor_ui, create_textures = false):

    • "theme: Inter and the icons load": load_fonts(<executable folder>/fonts) → true; fonts().regular and heading not null; the regular font's debug name contains "Inter"; after one new_frame()/render(), ImGui::GetFont() is fonts().regular and the font has the glyph 0xe13c (ICON_LC_PLAY) and 'A'.
    • "theme: a missing folder keeps ImGui's font": load_fonts("/nonexistent") → false, a frame still renders.
    • "theme: sizes scale": apply_theme(2.f) → FramePadding == (16, 10), FrameRounding == 8; apply_theme(1.f) on a fresh context → WindowBg equals #1E1E22 (as floats, ±1/255). The executable folder: platform::set_current_directory_to_executable_path().
  • Step 2: Run x64/Debug/UnitTests -ts=editor_ui → fails to compile (Theme.h missing).

  • Step 3: Implement the downloads, the copy step (CMake: a post-build or custom target that EditorUI's consumers depend on, writing into ${PROJECT_SOURCE_DIR}/x64/$<CONFIG>/fonts; MSBuild: a target after the build of each importing project), and Theme.cpp.

  • Step 4: Run the suite, a clean configure (delete the fonts folder first) and build → fonts appear, tests pass; the VM builds and passes the same tests.

  • Step 5: Leave in the working tree.

Task 5: The viewport panel, theme in EDITOR_UI, docs

Files:

  • Modify: Editor/UI/ImGuiBackend.h/.cpp, EngineTest/TestEditorUi.h, UnitTests/EditorUiTests.cpp
  • Modify: CLAUDE.md, Docs/Editor.md (section 9: step 2 done, findings), Docs/LinuxVulkanPort.md (A60: desktop check), issue #13 (comment)

Interfaces:

  • Consumes: Tasks 1-4.

  • Produces:

    // ImGuiBackend.h (includes imgui.h now)
    [[nodiscard]] auto surface_image(graphics::Surface surface) -> ImTextureID; // for ImGui::Image(); bit 63 marks a surface
    

    convert() turns an id with bit 63 into UiDrawCommand{.surface_id = low 32 bits} (texture id invalid).

  • EDITOR_UI: load_fonts(<exe>/fonts) + apply_theme(window.display_scale()) after initialize(); a "Viewport" window (docked by default into the dock space's central node on first use) shows an offscreen surface through ImGui::Image(surface_image(...), size), the surface resized to the window's content region each frame (minimum 1×1); the offscreen surface has its own small UiDrawData (a quad that moves across it over time, so a live picture is visible) and comes first in the span. The "Primal" window gains icons (ICON_LC_MONITOR before the backend, ICON_LC_TIMER before the frame time) and uses fonts().heading for its title line.

  • Step 1: Write the failing test: UnitTests "imgui backend: surface images convert to surface ids": draw ImGui::Image(surface_image(graphics::Surface{7}), {64, 64}) in a window, render() → exactly one command has surface_id == 7, and its texture_id is invalid; every other command's surface_id is invalid.

  • Step 2: Run → fails to compile.

  • Step 3: Implement the backend part, then the EngineTest part.

  • Step 4: Run the unit test; PRIMAL_TEST_FRAMES=300 x64/Debug/EngineTest (preset linux-debug-editor-ui) → exit 0, no validation messages; VM: EDITOR_UI on D3D12 and Vulkan, exit 0, a VM screenshot shows the viewport and the theme. Ask the user for the desktop look (A60).

  • Step 5: Docs and #13 (comment; step 2 ticked after the user's check). Leave in the working tree.

Pages 19

View all

Clone this repository