// 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 #include // 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 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); };