diff options
Diffstat (limited to 'src/tests/common/offscreen_render_target.h')
| -rw-r--r-- | src/tests/common/offscreen_render_target.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/tests/common/offscreen_render_target.h b/src/tests/common/offscreen_render_target.h new file mode 100644 index 0000000..10c12aa --- /dev/null +++ b/src/tests/common/offscreen_render_target.h @@ -0,0 +1,61 @@ +// This file is part of the 64k demo project. +// It provides offscreen rendering without windows (headless testing). +// Enables pixel readback for frame validation in tests. + +#pragma once + +#include "platform/platform.h" +#include <cstdint> +#include <vector> + +// Offscreen render target for headless GPU testing +// Creates a texture that can be rendered to and read back +class OffscreenRenderTarget { + public: + // Create an offscreen render target with specified dimensions + OffscreenRenderTarget( + WGPUInstance instance, WGPUDevice device, int width, int height, + WGPUTextureFormat format = WGPUTextureFormat_BGRA8Unorm); + ~OffscreenRenderTarget(); + + // Accessors + WGPUTexture texture() const { + return texture_; + } + WGPUTextureView view() const { + return view_; + } + int width() const { + return width_; + } + int height() const { + return height_; + } + WGPUTextureFormat format() const { + return format_; + } + + // Read pixels from the render target + // Returns BGRA8 pixel data (width * height * 4 bytes) + std::vector<uint8_t> read_pixels(); + + private: + WGPUInstance instance_; + WGPUDevice device_; + WGPUTexture texture_; + WGPUTextureView view_; + int width_; + int height_; + WGPUTextureFormat format_; + + // Helper: Create staging buffer for readback + WGPUBuffer create_staging_buffer(); + + // Callback state for async buffer mapping + struct MapState { + bool done = false; + WGPUMapAsyncStatus status = WGPUMapAsyncStatus_Unknown; + }; + + static void map_callback(WGPUMapAsyncStatus status, void* userdata); +}; |
