summaryrefslogtreecommitdiff
path: root/src/tests/offscreen_render_target.h
blob: 4163ec14594238777b825eb75947b3414559a697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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);
};