summaryrefslogtreecommitdiff
path: root/src/tests/webgpu_test_fixture.cc
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-07 09:46:12 +0100
committerskal <pascal.massimino@gmail.com>2026-02-07 09:46:12 +0100
commit4965202fb03299fc351f20eb2eb13f3fa30f6704 (patch)
tree55cb51b8772faa67f8e6228fa71ef0e15a913672 /src/tests/webgpu_test_fixture.cc
parent514d1b83562cbe63a24e8a53f90cda81f941b608 (diff)
test: Add GPU effects test infrastructure (Phase 1 Foundation)
Creates shared testing utilities for headless GPU effect testing. Enables testing visual effects without windows (CI-friendly). New Test Infrastructure (8 files): - webgpu_test_fixture.{h,cc}: Shared WebGPU initialization * Handles Win32 (old API) vs Native (new callback info structs) * Graceful skip if GPU unavailable * Eliminates 100+ lines of boilerplate per test - offscreen_render_target.{h,cc}: Headless rendering ("frame sink") * Creates offscreen WGPUTexture for rendering without windows * Pixel readback via wgpuBufferMapAsync for validation * 262,144 byte framebuffer (256x256 BGRA8) - effect_test_helpers.{h,cc}: Reusable validation utilities * has_rendered_content(): Detects non-black pixels * all_pixels_match_color(): Color matching with tolerance * hash_pixels(): Deterministic output verification (FNV-1a) - test_effect_base.cc: Comprehensive test suite (7 tests, all passing) * WebGPU fixture lifecycle * Offscreen rendering and pixel readback * Effect construction and initialization * Sequence add_effect and activation logic * Pixel validation helpers Coverage Impact: - GPU test infrastructure: 0% → Foundation ready for Phase 2 - Next: Individual effect tests (FlashEffect, GaussianBlur, etc.) Size Impact: ZERO - All test code wrapped in #if !defined(STRIP_ALL) - Test executables separate from demo64k - No impact on final binary (verified with guards) Test Output: ✓ 7/7 tests passing ✓ WebGPU initialization (adapter + device) ✓ Offscreen render target creation ✓ Pixel readback (262,144 bytes) ✓ Effect initialization via Sequence ✓ Sequence activation logic ✓ Pixel validation helpers Technical Details: - Uses WGPUTexelCopyTextureInfo/BufferInfo (not deprecated ImageCopy*) - Handles WGPURequestAdapterCallbackInfo (native) vs old API (Win32) - Polls wgpuInstanceProcessEvents for async operations - MapAsync uses WGPUMapMode_Read for pixel readback Analysis Document: - GPU_EFFECTS_TEST_ANALYSIS.md: Full roadmap (Phases 1-4, 44 hours) - Phase 1 complete, Phase 2 ready (individual effect tests) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/tests/webgpu_test_fixture.cc')
-rw-r--r--src/tests/webgpu_test_fixture.cc145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/tests/webgpu_test_fixture.cc b/src/tests/webgpu_test_fixture.cc
new file mode 100644
index 0000000..750dea0
--- /dev/null
+++ b/src/tests/webgpu_test_fixture.cc
@@ -0,0 +1,145 @@
+// This file is part of the 64k demo project.
+// It implements shared WebGPU initialization for GPU tests.
+// Provides graceful fallback if GPU unavailable.
+
+#if !defined(STRIP_ALL) // Test code only - zero size impact on final binary
+
+#include "webgpu_test_fixture.h"
+#include <cstdio>
+#include <cstdlib>
+
+WebGPUTestFixture::WebGPUTestFixture() {
+}
+
+WebGPUTestFixture::~WebGPUTestFixture() {
+ shutdown();
+}
+
+bool WebGPUTestFixture::init() {
+ // Create instance
+ const WGPUInstanceDescriptor instance_desc = {};
+ instance_ = wgpuCreateInstance(&instance_desc);
+ if (!instance_) {
+ fprintf(stderr,
+ "WebGPU not available (wgpuCreateInstance failed) - skipping GPU "
+ "test\n");
+ return false;
+ }
+
+ // Request adapter (API differs between Win32 and native)
+ WGPUAdapter adapter = nullptr;
+ const WGPURequestAdapterOptions adapter_opts = {
+ .compatibleSurface = nullptr,
+ .powerPreference = WGPUPowerPreference_HighPerformance,
+ };
+
+#if defined(DEMO_CROSS_COMPILE_WIN32)
+ // Win32: Old callback API (function pointer + userdata)
+ auto on_adapter = [](WGPURequestAdapterStatus status, WGPUAdapter a,
+ const char* message, void* userdata) {
+ if (status == WGPURequestAdapterStatus_Success) {
+ *(WGPUAdapter*)userdata = a;
+ } else if (message) {
+ fprintf(stderr, "Adapter request failed: %s\n", message);
+ }
+ };
+ wgpuInstanceRequestAdapter(instance_, &adapter_opts, on_adapter, &adapter);
+#else
+ // Native: New callback info API
+ auto on_adapter = [](WGPURequestAdapterStatus status, WGPUAdapter a,
+ WGPUStringView message, void* userdata, void* user2) {
+ (void)user2;
+ (void)message;
+ if (status == WGPURequestAdapterStatus_Success) {
+ *(WGPUAdapter*)userdata = a;
+ }
+ };
+ WGPURequestAdapterCallbackInfo adapter_cb = {};
+ adapter_cb.mode = WGPUCallbackMode_WaitAnyOnly;
+ adapter_cb.callback = on_adapter;
+ adapter_cb.userdata1 = &adapter;
+ wgpuInstanceRequestAdapter(instance_, &adapter_opts, adapter_cb);
+#endif
+
+ // Wait for adapter callback
+ for (int i = 0; i < 100 && !adapter; ++i) {
+ wgpuInstanceProcessEvents(instance_);
+ }
+
+ if (!adapter) {
+ fprintf(stderr, "No WebGPU adapter available - skipping GPU test\n");
+ shutdown();
+ return false;
+ }
+
+ adapter_ = adapter;
+
+ // Request device (API differs between Win32 and native)
+ WGPUDevice device = nullptr;
+ const WGPUDeviceDescriptor device_desc = {};
+
+#if defined(DEMO_CROSS_COMPILE_WIN32)
+ // Win32: Old callback API
+ auto on_device = [](WGPURequestDeviceStatus status, WGPUDevice d,
+ const char* message, void* userdata) {
+ if (status == WGPURequestDeviceStatus_Success) {
+ *(WGPUDevice*)userdata = d;
+ } else if (message) {
+ fprintf(stderr, "Device request failed: %s\n", message);
+ }
+ };
+ wgpuAdapterRequestDevice(adapter_, &device_desc, on_device, &device);
+#else
+ // Native: New callback info API
+ auto on_device = [](WGPURequestDeviceStatus status, WGPUDevice d,
+ WGPUStringView message, void* userdata, void* user2) {
+ (void)user2;
+ (void)message;
+ if (status == WGPURequestDeviceStatus_Success) {
+ *(WGPUDevice*)userdata = d;
+ }
+ };
+ WGPURequestDeviceCallbackInfo device_cb = {};
+ device_cb.mode = WGPUCallbackMode_WaitAnyOnly;
+ device_cb.callback = on_device;
+ device_cb.userdata1 = &device;
+ wgpuAdapterRequestDevice(adapter_, &device_desc, device_cb);
+#endif
+
+ // Wait for device callback
+ for (int i = 0; i < 100 && !device; ++i) {
+ wgpuInstanceProcessEvents(instance_);
+ }
+
+ if (!device) {
+ fprintf(stderr, "Failed to create WebGPU device - skipping GPU test\n");
+ shutdown();
+ return false;
+ }
+
+ device_ = device;
+ queue_ = wgpuDeviceGetQueue(device_);
+
+ return true;
+}
+
+void WebGPUTestFixture::shutdown() {
+ if (queue_) {
+ wgpuQueueRelease(queue_);
+ queue_ = nullptr;
+ }
+ if (device_) {
+ wgpuDeviceRelease(device_);
+ device_ = nullptr;
+ }
+ if (adapter_) {
+ wgpuAdapterRelease(adapter_);
+ adapter_ = nullptr;
+ }
+ if (instance_) {
+ wgpuInstanceRelease(instance_);
+ instance_ = nullptr;
+ }
+}
+
+#endif /* !defined(STRIP_ALL) */