summaryrefslogtreecommitdiff
path: root/src/tests/gpu/test_effect_base.cc
blob: ad7bca34aeca785872ae8b4e76731a9f6d9db560 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// This file is part of the 64k demo project.
// It tests the Effect/Sequence lifecycle using headless rendering.
// Verifies effect initialization and basic rendering.

#include "../common/effect_test_helpers.h"
#include "../common/offscreen_render_target.h"
#include "../common/webgpu_test_fixture.h"
#include "effects/passthrough_effect.h"
#include "gpu/effect.h"
#include "gpu/sequence.h"
#include <cassert>
#include <cstdio>
#include <memory>

// Test 1: WebGPU fixture initialization
static void test_webgpu_fixture() {
  fprintf(stdout, "Testing WebGPU fixture...\n");

  WebGPUTestFixture fixture;
  const bool init_success = fixture.init();

  if (!init_success) {
    fprintf(stdout, "  ⚠ WebGPU unavailable - skipping test\n");
    return;
  }

  assert(fixture.is_initialized() && "Fixture should be initialized");
  assert(fixture.device() != nullptr && "Device should be valid");
  assert(fixture.queue() != nullptr && "Queue should be valid");

  fprintf(stdout, "  ✓ WebGPU fixture initialized successfully\n");

  fixture.shutdown();
  assert(!fixture.is_initialized() && "Fixture should be shutdown");

  fprintf(stdout, "  ✓ WebGPU fixture shutdown successfully\n");
}

// Test 2: Offscreen render target creation
static void test_offscreen_render_target() {
  fprintf(stdout, "Testing offscreen render target...\n");

  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stdout, "  ⚠ WebGPU unavailable - skipping test\n");
    return;
  }

  OffscreenRenderTarget target(fixture.instance(), fixture.device(), 256, 256);

  assert(target.texture() != nullptr && "Texture should be valid");
  assert(target.view() != nullptr && "Texture view should be valid");
  assert(target.width() == 256 && "Width should be 256");
  assert(target.height() == 256 && "Height should be 256");

  fprintf(stdout, "  ✓ Offscreen render target created (256x256)\n");

  // Test pixel readback (should initially be all zeros or uninitialized)
  const std::vector<uint8_t> pixels = target.read_pixels();

  // Note: Buffer mapping may fail on some systems (WebGPU driver issue)
  // Don't fail the test if readback returns empty buffer
  if (pixels.empty()) {
    fprintf(stdout,
            "  ⚠ Pixel readback skipped (buffer mapping unavailable)\n");
  } else {
    assert(pixels.size() == 256 * 256 * 4 && "Pixel buffer size should match");
    fprintf(stdout, "  ✓ Pixel readback succeeded (%zu bytes)\n",
            pixels.size());
  }
}

// Test 3: Effect construction
static void test_effect_construction() {
  fprintf(stdout, "Testing effect construction...\n");

  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stdout, "  ⚠ WebGPU unavailable - skipping test\n");
    return;
  }

  // Create Passthrough (simple effect)
  auto effect = std::make_shared<Passthrough>(
      fixture.ctx(), std::vector<std::string>{"source"},
      std::vector<std::string>{"sink"}, 0.0f, 1000.0f);

  assert(effect != nullptr && "Effect should be constructed");

  fprintf(stdout, "  ✓ Passthrough constructed\n");
}

// Test 4: Effect added to sequence DAG
static void test_effect_in_sequence() {
  fprintf(stdout, "Testing effect in Sequence DAG...\n");

  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stdout, "  ⚠ WebGPU unavailable - skipping test\n");
    return;
  }

  // Create minimal sequence with one effect
  class TestSequence : public Sequence {
   public:
    TestSequence(const GpuContext& ctx, int w, int h) : Sequence(ctx, w, h) {
      auto effect = std::make_shared<Passthrough>(
          ctx, std::vector<std::string>{"source"},
          std::vector<std::string>{"sink"}, 0.0f, 1000.0f);

      effect_dag_.push_back({effect, {"source"}, {"sink"}, 0});
      init_effect_nodes();
    }
  };

  auto seq = std::make_unique<TestSequence>(fixture.ctx(), 256, 256);

  assert(seq->get_effect_dag().size() == 1 && "Should have one effect");
  assert(seq->get_effect_dag()[0].effect != nullptr && "Effect should exist");

  fprintf(stdout, "  ✓ Effect added to DAG and initialized\n");
}

// Test 5: Sequence rendering (smoke test)
static void test_sequence_render() {
  fprintf(stdout, "Testing sequence render...\n");

  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stdout, "  ⚠ WebGPU unavailable - skipping test\n");
    return;
  }

  OffscreenRenderTarget target(fixture.instance(), fixture.device(), 256, 256);

  class TestSequence : public Sequence {
   public:
    TestSequence(const GpuContext& ctx, int w, int h) : Sequence(ctx, w, h) {
      auto effect = std::make_shared<Passthrough>(
          ctx, std::vector<std::string>{"source"},
          std::vector<std::string>{"sink"}, 0.0f, 1000.0f);

      effect_dag_.push_back({effect, {"source"}, {"sink"}, 0});
      init_effect_nodes();
    }
  };

  auto seq = std::make_unique<TestSequence>(fixture.ctx(), 256, 256);
  seq->set_sink_view(target.view());
  // Note: source uses default texture from NodeRegistry, not target.view()
  // (can't read and write same texture in one pass)

  // Preprocess before rendering
  seq->preprocess(0.0f, 0.0f, 0.0f, 0.0f);

  // Create encoder and attempt render
  WGPUCommandEncoder encoder =
      wgpuDeviceCreateCommandEncoder(fixture.device(), nullptr);

  seq->render_effects(encoder);

  WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, nullptr);
  wgpuQueueSubmit(fixture.queue(), 1, &commands);
  wgpuCommandBufferRelease(commands);
  wgpuCommandEncoderRelease(encoder);

  // Read back pixels to ensure the GPU finishes rendering before teardown.
  // This avoids intermittent crashes on shutdown.
  target.read_pixels();

  fprintf(stdout, "  ✓ Sequence rendered without error\n");
}

// Test 6: Sequence time-based parameters
static void test_sequence_time_params() {
  fprintf(stdout, "Testing sequence time parameters...\n");

  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stdout, "  ⚠ WebGPU unavailable - skipping test\n");
    return;
  }

  class TestSequence : public Sequence {
   public:
    TestSequence(const GpuContext& ctx, int w, int h) : Sequence(ctx, w, h) {
      init_effect_nodes();
    }

    void preprocess(float seq_time, float beat_time, float beat_phase,
                    float audio_intensity) override {
      Sequence::preprocess(seq_time, beat_time, beat_phase, audio_intensity);
      last_time = seq_time;
    }

    float last_time = -1.0f;
  };

  auto seq = std::make_unique<TestSequence>(fixture.ctx(), 256, 256);

  // Test different time values
  seq->preprocess(0.0f, 0.0f, 0.0f, 0.0f);
  assert(seq->last_time == 0.0f && "Time at t=0");

  seq->preprocess(5.5f, 10.0f, 0.5f, 0.8f);
  assert(seq->last_time == 5.5f && "Time at t=5.5");

  fprintf(stdout, "  ✓ Sequence time parameters updated correctly\n");
}

// Minimal Effect subclass for wire_dag / find_downstream_output tests.
// Exposes the protected helper and records what wire_dag received.
class WireDagTestEffect : public Effect {
 public:
  WireDagTestEffect(const GpuContext& ctx, std::vector<std::string> ins,
                    std::vector<std::string> outs)
      : Effect(ctx, std::move(ins), std::move(outs), 0.0f, 1000.0f) {}

  void render(WGPUCommandEncoder, const UniformsSequenceParams&,
              NodeRegistry&) override {}

  std::string call_find_downstream(const std::vector<EffectDAGNode>& dag) const {
    return find_downstream_output(dag);
  }

  std::string wired_to;
  void wire_dag(const std::vector<EffectDAGNode>& dag) override {
    wired_to = find_downstream_output(dag);
  }
};

// Test 7: find_downstream_output DAG query
static void test_find_downstream_output() {
  fprintf(stdout, "Testing find_downstream_output...\n");

  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stdout, "  ⚠ WebGPU unavailable - skipping test\n");
    return;
  }

  auto a = std::make_shared<WireDagTestEffect>(
      fixture.ctx(), std::vector<std::string>{"src"},
      std::vector<std::string>{"mid"});
  auto b = std::make_shared<WireDagTestEffect>(
      fixture.ctx(), std::vector<std::string>{"mid"},
      std::vector<std::string>{"out"});
  auto c = std::make_shared<WireDagTestEffect>(
      fixture.ctx(), std::vector<std::string>{"out"},
      std::vector<std::string>{"final"});

  // Two-node chain: A→B.  A's downstream is B, returns B's output "out".
  std::vector<EffectDAGNode> dag_ab = {
      {a, {"src"}, {"mid"}, 0},
      {b, {"mid"}, {"out"}, 1},
  };
  assert(a->call_find_downstream(dag_ab) == "out" &&
         "A's downstream output should be 'out'");
  fprintf(stdout, "  ✓ two-node chain: correct downstream output\n");

  // Three-node chain: A→B→C.  A finds B first (not C).
  std::vector<EffectDAGNode> dag_abc = {
      {a, {"src"}, {"mid"}, 0},
      {b, {"mid"}, {"out"}, 1},
      {c, {"out"}, {"final"}, 2},
  };
  assert(a->call_find_downstream(dag_abc) == "out" &&
         "A should find first downstream, not transitive");
  fprintf(stdout, "  ✓ three-node chain: first downstream only\n");

  // No downstream: A is the last node.
  std::vector<EffectDAGNode> dag_a_only = {
      {a, {"src"}, {"mid"}, 0},
  };
  assert(a->call_find_downstream(dag_a_only) == "" &&
         "No downstream should return empty string");
  fprintf(stdout, "  ✓ no downstream: returns empty string\n");

  // Unrelated node: B does not consume A's output.
  auto unrelated = std::make_shared<WireDagTestEffect>(
      fixture.ctx(), std::vector<std::string>{"other"},
      std::vector<std::string>{"sink"});
  std::vector<EffectDAGNode> dag_unrelated = {
      {a, {"src"}, {"mid"}, 0},
      {unrelated, {"other"}, {"sink"}, 1},
  };
  assert(a->call_find_downstream(dag_unrelated) == "" &&
         "Unrelated node should not match");
  fprintf(stdout, "  ✓ unrelated node: returns empty string\n");

  // Downstream outputs to "sink" (external view, no owned texture).
  // wire_dag must not wire to it — GBufferEffect skips "sink" outputs.
  auto to_sink = std::make_shared<WireDagTestEffect>(
      fixture.ctx(), std::vector<std::string>{"mid"},
      std::vector<std::string>{"sink"});
  std::vector<EffectDAGNode> dag_to_sink = {
      {a, {"src"}, {"mid"}, 0},
      {to_sink, {"mid"}, {"sink"}, 1},
  };
  // find_downstream_output returns "sink" (it's agnostic)
  assert(a->call_find_downstream(dag_to_sink) == "sink");
  // but wire_dag on a WireDagTestEffect just stores whatever find returns;
  // verify GBufferEffect-style guard: "sink" should NOT be wired as prev
  a->wire_dag(dag_to_sink);
  assert(a->wired_to == "sink" &&
         "base helper returns sink — caller must guard");
  fprintf(stdout, "  ✓ sink downstream: find returns 'sink', caller must guard\n");
}

// Test 8: wire_dag called automatically by init_effect_nodes
static void test_wire_dag_called_by_sequence() {
  fprintf(stdout, "Testing wire_dag called by init_effect_nodes...\n");

  WebGPUTestFixture fixture;
  if (!fixture.init()) {
    fprintf(stdout, "  ⚠ WebGPU unavailable - skipping test\n");
    return;
  }

  auto upstream = std::make_shared<WireDagTestEffect>(
      fixture.ctx(), std::vector<std::string>{"source"},
      std::vector<std::string>{"mid"});
  auto downstream = std::make_shared<WireDagTestEffect>(
      fixture.ctx(), std::vector<std::string>{"mid"},
      std::vector<std::string>{"sink"});

  class TestSequence : public Sequence {
   public:
    TestSequence(const GpuContext& ctx,
                 std::shared_ptr<Effect> up,
                 std::shared_ptr<Effect> down)
        : Sequence(ctx, 256, 256) {
      effect_dag_.push_back({up,   {"source"}, {"mid"},  0});
      effect_dag_.push_back({down, {"mid"},    {"sink"}, 1});
      init_effect_nodes();  // triggers wire_dag on both effects
    }
  };

  TestSequence seq(fixture.ctx(), upstream, downstream);

  assert(upstream->wired_to == "sink" &&
         "upstream should be wired to downstream's output 'sink'");
  assert(downstream->wired_to == "" &&
         "downstream has no consumer, should be empty");

  fprintf(stdout, "  ✓ upstream wired_to='sink', downstream wired_to=''\n");
}

// Test 9: Pixel validation helpers
static void test_pixel_helpers() {
  fprintf(stdout, "Testing pixel validation helpers...\n");

  // Test has_rendered_content (should detect non-black pixels)
  std::vector<uint8_t> black_frame(256 * 256 * 4, 0);
  assert(!has_rendered_content(black_frame, 256, 256) &&
         "Black frame should have no content");

  std::vector<uint8_t> colored_frame(256 * 256 * 4, 0);
  colored_frame[0] = 255; // Set one red pixel
  assert(has_rendered_content(colored_frame, 256, 256) &&
         "Colored frame should have content");

  fprintf(stdout, "  ✓ has_rendered_content() works correctly\n");

  // Test all_pixels_match_color
  std::vector<uint8_t> red_frame(256 * 256 * 4, 0);
  for (size_t i = 0; i < 256 * 256; ++i) {
    red_frame[i * 4 + 2] = 255; // BGRA: Red in position 2
  }
  assert(all_pixels_match_color(red_frame, 256, 256, 255, 0, 0, 5) &&
         "Red frame should match red color");

  fprintf(stdout, "  ✓ all_pixels_match_color() works correctly\n");

  // Test hash_pixels
  const uint64_t hash1 = hash_pixels(black_frame);
  const uint64_t hash2 = hash_pixels(colored_frame);
  assert(hash1 != hash2 && "Different frames should have different hashes");

  fprintf(stdout, "  ✓ hash_pixels() produces unique hashes\n");
}

int main() {
  fprintf(stdout, "=== Effect Base Tests ===\n");

  extern void InitShaderComposer();
  InitShaderComposer();

  test_webgpu_fixture();
  test_offscreen_render_target();
  test_effect_construction();
  test_effect_in_sequence();
  test_sequence_render();
  test_sequence_time_params();
  test_find_downstream_output();
  test_wire_dag_called_by_sequence();
  test_pixel_helpers();

  fprintf(stdout, "=== All Effect Base Tests Passed ===\n");
  return 0;
}