summaryrefslogtreecommitdiff
path: root/src/tests/gpu/test_sequence_v2.cc
blob: 54b544e44d79d5d6312e7d9d6ec5187097711167 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Test file for Sequence v2 system
// Phase 1: Foundation tests (NodeRegistry, SequenceV2 base class)

#include "gpu/sequence_v2.h"
#include "gpu/effect_v2.h"
#include "tests/common/webgpu_test_fixture.h"
#include <cassert>
#include <cstdio>

// Simple test effect for DAG execution
class TestEffectV2 : public EffectV2 {
 public:
  TestEffectV2(const GpuContext& ctx, const std::vector<std::string>& inputs,
               const std::vector<std::string>& outputs)
      : EffectV2(ctx, inputs, outputs), render_called_(false) {
  }

  void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params,
              NodeRegistry& nodes) override {
    (void)encoder;
    (void)params;
    (void)nodes;
    render_called_ = true;
  }

  bool was_render_called() const {
    return render_called_;
  }

 private:
  bool render_called_;
};

// Test: NodeRegistry basic allocation
void test_node_registry_basic() {
  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stderr, "Skipping test_node_registry_basic (no GPU)\n");
    return;
  }

  NodeRegistry registry(fixture.ctx().device, 1280, 720);

  // Declare node
  registry.declare_node("test_node", NodeType::U8X4_NORM, 1280, 720);

  // Verify node exists
  assert(registry.has_node("test_node"));

  // Get view (should not crash)
  WGPUTextureView view = registry.get_view("test_node");
  assert(view != nullptr);

  printf("PASS: NodeRegistry basic allocation\n");
}

// Test: NodeRegistry aliased nodes (ping-pong optimization)
void test_node_registry_aliased() {
  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stderr, "Skipping test_node_registry_aliased (no GPU)\n");
    return;
  }

  NodeRegistry registry(fixture.ctx().device, 1280, 720);

  // Declare backing node
  registry.declare_node("frame_a", NodeType::U8X4_NORM, 1280, 720);

  // Declare aliased node
  registry.declare_aliased_node("frame_b", "frame_a");

  // Both should resolve to same view
  WGPUTextureView view_a = registry.get_view("frame_a");
  WGPUTextureView view_b = registry.get_view("frame_b");
  assert(view_a == view_b);

  printf("PASS: NodeRegistry aliased nodes\n");
}

// Test: NodeRegistry multi-output views
void test_node_registry_multi_output() {
  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stderr, "Skipping test_node_registry_multi_output (no GPU)\n");
    return;
  }

  NodeRegistry registry(fixture.ctx().device, 1280, 720);

  // Declare multiple nodes
  registry.declare_node("output1", NodeType::U8X4_NORM, 1280, 720);
  registry.declare_node("output2", NodeType::F32X4, 1280, 720);

  // Get multiple views
  std::vector<std::string> names = {"output1", "output2"};
  std::vector<WGPUTextureView> views = registry.get_output_views(names);

  assert(views.size() == 2);
  assert(views[0] != nullptr);
  assert(views[1] != nullptr);

  printf("PASS: NodeRegistry multi-output views\n");
}

// Test: SequenceV2 default preprocess
void test_sequence_v2_preprocess() {
  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stderr, "Skipping test_sequence_v2_preprocess (no GPU)\n");
    return;
  }

  SequenceV2 seq(fixture.ctx(), 1280, 720);

  // Call preprocess with test values
  seq.preprocess(1.0f, 4.0f, 0.5f, 0.8f);

  // No crash = success (params updated internally)
  printf("PASS: SequenceV2 preprocess\n");
}

// Test: SequenceV2 DAG execution
void test_sequence_v2_dag_execution() {
  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stderr, "Skipping test_sequence_v2_dag_execution (no GPU)\n");
    return;
  }

  // Create sequence
  class TestSequence : public SequenceV2 {
   public:
    TestSequence(const GpuContext& ctx)
        : SequenceV2(ctx, 1280, 720),
          effect1_(std::make_shared<TestEffectV2>(ctx, std::vector<std::string>{"source"},
                                                   std::vector<std::string>{"temp"})),
          effect2_(std::make_shared<TestEffectV2>(ctx, std::vector<std::string>{"temp"},
                                                   std::vector<std::string>{"sink"})) {
      // Build DAG (2 effects in sequence)
      effect_dag_.push_back(
          {effect1_, {"source"}, {"temp"}, 0});
      effect_dag_.push_back(
          {effect2_, {"temp"}, {"sink"}, 1});
    }

    std::shared_ptr<TestEffectV2> effect1_;
    std::shared_ptr<TestEffectV2> effect2_;
  };

  TestSequence seq(fixture.ctx());

  // Create command encoder
  WGPUCommandEncoderDescriptor enc_desc = {};
  WGPUCommandEncoder encoder =
      wgpuDeviceCreateCommandEncoder(fixture.ctx().device, &enc_desc);

  // Execute DAG
  seq.render_effects(encoder);

  // Verify both effects called
  assert(seq.effect1_->was_render_called());
  assert(seq.effect2_->was_render_called());

  // Cleanup
  WGPUCommandBuffer cmd = wgpuCommandEncoderFinish(encoder, nullptr);
  wgpuCommandBufferRelease(cmd);
  wgpuCommandEncoderRelease(encoder);

  printf("PASS: SequenceV2 DAG execution\n");
}

int main() {
  printf("Running Sequence v2 tests...\n");

  test_node_registry_basic();
  test_node_registry_aliased();
  test_node_registry_multi_output();
  test_sequence_v2_preprocess();
  test_sequence_v2_dag_execution();

  printf("All Sequence v2 tests passed!\n");
  return 0;
}