[PR #3654] [MERGED] Screenshots on OpenGL draw the frame where it can be read back #5127

Closed
opened 2026-11-03 05:10:25 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/3654
Author: @d3grando
Created: 10/29/2026
Status: ✅ Merged
Merged: 10/29/2026
Merged by: @levzzz5154

Base: main ← Head: fix-3510


📝 Commits (1)

  • 675298e Screenshots on OpenGL draw the frame where it can be read back

📊 Changes

2 files changed (+268 additions, -38 deletions)

View changed files

📝 crates/horizon-client-render/src/gfx.rs (+55 -38)
➕ crates/horizon-client-render/src/gfx/readback.rs (+213 -0)

📄 Description

🤖 Opened by d3grando's AI agent (Claude Code, Claude Opus 5.5), not by d3grando themselves.

What and why

Fixes #3510

On the OpenGL backend, taking a screenshot crashed the game with the panic from the report:

wgpu error: Validation Error
Caused by:
    Usage flags TextureUsages(RENDER_ATTACHMENT) of Texture with '<Surface Texture>' label do not contain required usage flags TextureUsages(COPY_SRC)

Root cause. wgpu's GL surfaces only offer RENDER_ATTACHMENT (wgpu-hal gles surface_capabilities). Renderer::new already adds COPY_SRC only when the caps allow it, and the menu blur is skipped without it. But the screenshot path in draw_frame only checked the format's block size, then copy_frame copied the swapchain image into the readback buffer anyway. write_screenshot checked for COPY_SRC too, but only after the copy had been encoded, so the frame's encoder.finish() hit the validation error first. In draw_frame, this copy is the only one that reads from the swapchain image.

Fix. When a frame is read back and the swapchain image can't be copied from, the frame is drawn into a texture that can (gfx/readback.rs, Readback::target). It's read back from there, then drawn onto the swapchain image with wgpu::util::TextureBlitter (Readback::present), so the window still shows that frame. The texture is made once and reused while the window keeps its size. Every other frame, and every surface that has COPY_SRC (Vulkan, DX12, Metal, offscreen), still draws straight into the swapchain, so nothing changes there. I removed the COPY_SRC refusal in write_screenshot, since every path now has a readable source. copy_frame and padded_row became free functions so the test can drive the same copy.

About "automatic": on current main I couldn't find any screenshot Horizon takes by itself. Vanilla's world icon (GameRenderer.tryTakeScreenshotIfNeeded) isn't ported. The frame readbacks that reach this code are F2, horizon-ctl screenshot and the screenshot control request. The reporter's Nightly (9b772b2) also carries #2911, whose world icon is that automatic readback (ScreenshotRequest { size: Some([64, 64]) } right after the level is drawn). It goes through the same self.screenshot request and the same copy, so it panicked the same way; with this in, its request also gets the readable frame, since Readback::target looks at self.screenshot when the frame starts. #3417 (another world-icon PR, with its own world_icon field) would need to pass that flag to Readback::target as well.

Not in this PR: the menu blur still skips itself on surfaces without COPY_SRC, as before. It could reuse the same texture later.

Vanilla reference

None for the rendering itself: this is about Horizon's wgpu backend. Vanilla's Screenshot.takeScreenshot reads back its own main render target, which is always readable. Horizon renders straight into the swapchain, so it needs the extra texture on surfaces that can't be read back.

Tests

New gfx::readback tests on a real wgpu device. screenshot_of_a_surface_without_copy_src uses a swapchain-like texture with no COPY_SRC. It runs the renderer's sequence (target, draw, copy_frame, present) twice inside a validation error scope, then checks the pixel read back and the pixel that reached the "swapchain". frames_draw_straight_to_a_readable_surface checks that readable surfaces, and frames with no screenshot, draw straight to the swapchain with no extra texture.

Before, with only the code part reverted (target always drawing to the swapchain, as main does):

test gfx::readback::tests::frames_draw_straight_to_a_readable_surface ... ok
---- gfx::readback::tests::screenshot_of_a_surface_without_copy_src stdout ----
thread 'gfx::readback::tests::screenshot_of_a_surface_without_copy_src' panicked at crates\horizon-client-render\src\gfx\readback.rs:183:17:
Validation Error

Caused by:
  In a CommandEncoder
    Usage flags TextureUsages(TEXTURE_BINDING | RENDER_ATTACHMENT) of Texture with '' label do not contain required usage flags TextureUsages(COPY_SRC)

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 512 filtered out

After:

test gfx::readback::tests::screenshot_of_a_surface_without_copy_src ... ok
test gfx::readback::tests::frames_draw_straight_to_a_readable_surface ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 512 filtered out

In game

Windows 11, RTX 5080, WGPU_BACKEND=gl, so the renderer runs on GL just like the reporter's Mesa fallback (status reports "backend":"Gl"). I used a creative world and horizon-ctl screenshot once the world was ready.

  • main: the game died on the first screenshot. latest.log has Handling wgpu errors as fatal by default, then Horizon crashed: panicked at ... wgpu_core.rs:2678:18: wgpu error: Validation Error ... Usage flags TextureUsages(RENDER_ATTACHMENT) of Texture with '<Surface Texture>' label do not contain required usage flags TextureUsages(COPY_SRC), the same message as the report.
  • this branch: two screenshots in a row succeeded ({"height":720,"ok":true,"width":1280}), with the view turned between them. The PNGs show the world the right way up with the right colours, and the game kept running with no errors in the log.

Self-check

cargo fmt --all -- --check                                                   # clean
cargo clippy -p horizon-client-render --all-targets -- -D warnings           # clean
cargo test --profile fast -p horizon-client-render --lib gfx::readback       # 2 passed
cargo build --profile fast -p horizon-client --features mods --bins          # ok

(tools/pr_ready.py doesn't run on Windows, so I ran its steps by hand.)

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/3654 **Author:** [@d3grando](https://github.com/d3grando) **Created:** 10/29/2026 **Status:** ✅ Merged **Merged:** 10/29/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `fix-3510` --- ### 📝 Commits (1) - [`675298e`](https://github.com/horizon-rust/horizon/commit/675298eac2b87f6dbd6cc5090c4dc3fa950e9656) Screenshots on OpenGL draw the frame where it can be read back ### 📊 Changes **2 files changed** (+268 additions, -38 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client-render/src/gfx.rs` (+55 -38) ➕ `crates/horizon-client-render/src/gfx/readback.rs` (+213 -0) </details> ### 📄 Description > 🤖 Opened by d3grando's AI agent (Claude Code, Claude Opus 5.5), not by d3grando themselves. ## What and why Fixes #3510 On the OpenGL backend, taking a screenshot crashed the game with the panic from the report: ``` wgpu error: Validation Error Caused by: Usage flags TextureUsages(RENDER_ATTACHMENT) of Texture with '<Surface Texture>' label do not contain required usage flags TextureUsages(COPY_SRC) ``` **Root cause.** wgpu's GL surfaces only offer `RENDER_ATTACHMENT` (wgpu-hal `gles` `surface_capabilities`). `Renderer::new` already adds `COPY_SRC` only when the caps allow it, and the menu blur is skipped without it. But the screenshot path in `draw_frame` only checked the format's block size, then `copy_frame` copied the swapchain image into the readback buffer anyway. `write_screenshot` checked for `COPY_SRC` too, but only after the copy had been encoded, so the frame's `encoder.finish()` hit the validation error first. In `draw_frame`, this copy is the only one that reads from the swapchain image. **Fix.** When a frame is read back and the swapchain image can't be copied from, the frame is drawn into a texture that can (`gfx/readback.rs`, `Readback::target`). It's read back from there, then drawn onto the swapchain image with `wgpu::util::TextureBlitter` (`Readback::present`), so the window still shows that frame. The texture is made once and reused while the window keeps its size. Every other frame, and every surface that has `COPY_SRC` (Vulkan, DX12, Metal, offscreen), still draws straight into the swapchain, so nothing changes there. I removed the `COPY_SRC` refusal in `write_screenshot`, since every path now has a readable source. `copy_frame` and `padded_row` became free functions so the test can drive the same copy. About "automatic": on current main I couldn't find any screenshot Horizon takes by itself. Vanilla's world icon (`GameRenderer.tryTakeScreenshotIfNeeded`) isn't ported. The frame readbacks that reach this code are F2, `horizon-ctl screenshot` and the screenshot control request. The reporter's Nightly (9b772b2) also carries #2911, whose world icon is that automatic readback (`ScreenshotRequest { size: Some([64, 64]) }` right after the level is drawn). It goes through the same `self.screenshot` request and the same copy, so it panicked the same way; with this in, its request also gets the readable frame, since `Readback::target` looks at `self.screenshot` when the frame starts. #3417 (another world-icon PR, with its own `world_icon` field) would need to pass that flag to `Readback::target` as well. Not in this PR: the menu blur still skips itself on surfaces without `COPY_SRC`, as before. It could reuse the same texture later. ## Vanilla reference None for the rendering itself: this is about Horizon's wgpu backend. Vanilla's `Screenshot.takeScreenshot` reads back its own main render target, which is always readable. Horizon renders straight into the swapchain, so it needs the extra texture on surfaces that can't be read back. ## Tests New `gfx::readback` tests on a real wgpu device. `screenshot_of_a_surface_without_copy_src` uses a swapchain-like texture with no `COPY_SRC`. It runs the renderer's sequence (`target`, draw, `copy_frame`, `present`) twice inside a validation error scope, then checks the pixel read back and the pixel that reached the "swapchain". `frames_draw_straight_to_a_readable_surface` checks that readable surfaces, and frames with no screenshot, draw straight to the swapchain with no extra texture. Before, with only the code part reverted (`target` always drawing to the swapchain, as main does): ``` test gfx::readback::tests::frames_draw_straight_to_a_readable_surface ... ok ---- gfx::readback::tests::screenshot_of_a_surface_without_copy_src stdout ---- thread 'gfx::readback::tests::screenshot_of_a_surface_without_copy_src' panicked at crates\horizon-client-render\src\gfx\readback.rs:183:17: Validation Error Caused by: In a CommandEncoder Usage flags TextureUsages(TEXTURE_BINDING | RENDER_ATTACHMENT) of Texture with '' label do not contain required usage flags TextureUsages(COPY_SRC) test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 512 filtered out ``` After: ``` test gfx::readback::tests::screenshot_of_a_surface_without_copy_src ... ok test gfx::readback::tests::frames_draw_straight_to_a_readable_surface ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 512 filtered out ``` ## In game Windows 11, RTX 5080, `WGPU_BACKEND=gl`, so the renderer runs on GL just like the reporter's Mesa fallback (`status` reports `"backend":"Gl"`). I used a creative world and `horizon-ctl screenshot` once the world was ready. - **main:** the game died on the first screenshot. `latest.log` has `Handling wgpu errors as fatal by default`, then `Horizon crashed: panicked at ... wgpu_core.rs:2678:18: wgpu error: Validation Error ... Usage flags TextureUsages(RENDER_ATTACHMENT) of Texture with '<Surface Texture>' label do not contain required usage flags TextureUsages(COPY_SRC)`, the same message as the report. - **this branch:** two screenshots in a row succeeded (`{"height":720,"ok":true,"width":1280}`), with the view turned between them. The PNGs show the world the right way up with the right colours, and the game kept running with no errors in the log. ## Self-check ``` cargo fmt --all -- --check # clean cargo clippy -p horizon-client-render --all-targets -- -D warnings # clean cargo test --profile fast -p horizon-client-render --lib gfx::readback # 2 passed cargo build --profile fast -p horizon-client --features mods --bins # ok ``` (`tools/pr_ready.py` doesn't run on Windows, so I ran its steps by hand.) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 05:10:25 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#5127
No description provided.