diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-09 20:27:04 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-09 20:27:04 +0100 |
| commit | eff8d43479e7704df65fae2a80eefa787213f502 (patch) | |
| tree | 76f2fb8fe8d3db2c15179449df2cf12f7f54e0bf /src/tests/common/webgpu_test_fixture.cc | |
| parent | 12378b1b7e9091ba59895b4360b2fa959180a56a (diff) | |
refactor: Reorganize tests into subsystem subdirectories
Restructured test suite for better organization and targeted testing:
**Structure:**
- src/tests/audio/ - 15 audio system tests
- src/tests/gpu/ - 12 GPU/shader tests
- src/tests/3d/ - 6 3D rendering tests
- src/tests/assets/ - 2 asset system tests
- src/tests/util/ - 3 utility tests
- src/tests/common/ - 3 shared test helpers
- src/tests/scripts/ - 2 bash test scripts (moved conceptually, not physically)
**CMake changes:**
- Updated add_demo_test macro to accept LABEL parameter
- Applied CTest labels to all 36 tests for subsystem filtering
- Updated all test file paths in CMakeLists.txt
- Fixed common helper paths (webgpu_test_fixture, etc.)
- Added custom targets for subsystem testing:
- run_audio_tests, run_gpu_tests, run_3d_tests
- run_assets_tests, run_util_tests, run_all_tests
**Include path updates:**
- Fixed relative includes in GPU tests to reference ../common/
**Documentation:**
- Updated doc/HOWTO.md with subsystem test commands
- Updated doc/CONTRIBUTING.md with new test organization
- Updated scripts/check_all.sh to reflect new structure
**Verification:**
- All 36 tests passing (100%)
- ctest -L <subsystem> filters work correctly
- make run_<subsystem>_tests targets functional
- scripts/check_all.sh passes
Backward compatible: make test and ctest continue to work unchanged.
handoff(Gemini): Test reorganization complete. 36/36 tests passing.
Diffstat (limited to 'src/tests/common/webgpu_test_fixture.cc')
| -rw-r--r-- | src/tests/common/webgpu_test_fixture.cc | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/tests/common/webgpu_test_fixture.cc b/src/tests/common/webgpu_test_fixture.cc new file mode 100644 index 0000000..afb7ce3 --- /dev/null +++ b/src/tests/common/webgpu_test_fixture.cc @@ -0,0 +1,141 @@ +// This file is part of the 64k demo project. +// It implements shared WebGPU initialization for GPU tests. +// Provides graceful fallback if GPU unavailable. + +#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; + } +} |
