summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/test_3d_render.cc39
-rw-r--r--src/tests/test_demo_effects.cc3
-rw-r--r--src/tests/test_effect_base.cc3
-rw-r--r--src/tests/test_file_watcher.cc63
-rw-r--r--src/tests/test_gpu_composite.cc124
-rw-r--r--src/tests/test_gpu_procedural.cc117
-rw-r--r--src/tests/test_post_process_helper.cc8
-rw-r--r--src/tests/test_shader_compilation.cc7
8 files changed, 344 insertions, 20 deletions
diff --git a/src/tests/test_3d_render.cc b/src/tests/test_3d_render.cc
index fa13a43..eee46ba 100644
--- a/src/tests/test_3d_render.cc
+++ b/src/tests/test_3d_render.cc
@@ -220,25 +220,36 @@ int main(int argc, char** argv) {
g_renderer.resize(platform_state.width, platform_state.height);
g_textures.init(g_device, g_queue);
- ProceduralTextureDef noise_def;
- noise_def.width = 256;
- noise_def.height = 256;
- noise_def.gen_func = gen_periodic_noise;
- noise_def.params.push_back(1234.0f);
- noise_def.params.push_back(16.0f);
- g_textures.create_procedural_texture("noise", noise_def);
+ // GPU Noise texture (replaces CPU procedural)
+ GpuProceduralParams noise_params = {};
+ noise_params.width = 256;
+ noise_params.height = 256;
+ float noise_vals[2] = {1234.0f, 16.0f};
+ noise_params.params = noise_vals;
+ noise_params.num_params = 2;
+ g_textures.create_gpu_noise_texture("noise", noise_params);
g_renderer.set_noise_texture(g_textures.get_texture_view("noise"));
- ProceduralTextureDef sky_def;
- sky_def.width = 512;
- sky_def.height = 256;
- sky_def.gen_func = procedural::gen_perlin;
- sky_def.params = {42.0f, 4.0f, 1.0f, 0.5f, 6.0f};
- g_textures.create_procedural_texture("sky", sky_def);
-
+ // GPU Perlin texture for sky (replaces CPU procedural)
+ GpuProceduralParams sky_params = {};
+ sky_params.width = 512;
+ sky_params.height = 256;
+ float sky_vals[5] = {42.0f, 4.0f, 1.0f, 0.5f, 6.0f};
+ sky_params.params = sky_vals;
+ sky_params.num_params = 5;
+ g_textures.create_gpu_perlin_texture("sky", sky_params);
g_renderer.set_sky_texture(g_textures.get_texture_view("sky"));
+ // GPU Grid texture (new!)
+ GpuProceduralParams grid_params = {};
+ grid_params.width = 256;
+ grid_params.height = 256;
+ float grid_vals[2] = {32.0f, 2.0f}; // grid_size, thickness
+ grid_params.params = grid_vals;
+ grid_params.num_params = 2;
+ g_textures.create_gpu_grid_texture("grid", grid_params);
+
setup_scene();
g_camera.position = vec3(0, 5, 10);
diff --git a/src/tests/test_demo_effects.cc b/src/tests/test_demo_effects.cc
index d0163c2..0d2b09a 100644
--- a/src/tests/test_demo_effects.cc
+++ b/src/tests/test_demo_effects.cc
@@ -197,6 +197,9 @@ static void test_effect_type_classification() {
int main() {
fprintf(stdout, "=== Demo Effects Tests ===\n");
+ extern void InitShaderComposer();
+ InitShaderComposer();
+
test_post_process_effects();
test_scene_effects();
test_effect_type_classification();
diff --git a/src/tests/test_effect_base.cc b/src/tests/test_effect_base.cc
index e280e05..612e9da 100644
--- a/src/tests/test_effect_base.cc
+++ b/src/tests/test_effect_base.cc
@@ -249,6 +249,9 @@ static void test_pixel_helpers() {
int main() {
fprintf(stdout, "=== Effect Base Tests ===\n");
+ extern void InitShaderComposer();
+ InitShaderComposer();
+
test_webgpu_fixture();
test_offscreen_render_target();
test_effect_construction();
diff --git a/src/tests/test_file_watcher.cc b/src/tests/test_file_watcher.cc
new file mode 100644
index 0000000..ac13afd
--- /dev/null
+++ b/src/tests/test_file_watcher.cc
@@ -0,0 +1,63 @@
+// test_file_watcher.cc - Unit tests for file change detection
+
+#include "util/file_watcher.h"
+#include <cstdio>
+#include <fstream>
+#include <unistd.h>
+
+#if !defined(STRIP_ALL)
+
+int main() {
+ // Create a temporary test file
+ const char* test_file = "/tmp/test_watcher_file.txt";
+ {
+ std::ofstream f(test_file);
+ f << "initial content\n";
+ }
+
+ FileWatcher watcher;
+ watcher.add_file(test_file);
+
+ // Initial check - no changes yet
+ bool changed = watcher.check_changes();
+ if (changed) {
+ fprintf(stderr, "FAIL: Expected no changes on first check\n");
+ return 1;
+ }
+
+ // Sleep to ensure mtime changes (some filesystems have 1s granularity)
+ sleep(1);
+
+ // Modify the file
+ {
+ std::ofstream f(test_file, std::ios::app);
+ f << "modified\n";
+ }
+
+ // Check for changes
+ changed = watcher.check_changes();
+ if (!changed) {
+ fprintf(stderr, "FAIL: Expected changes after file modification\n");
+ return 1;
+ }
+
+ // Reset and check again - should be no changes
+ watcher.reset();
+ changed = watcher.check_changes();
+ if (changed) {
+ fprintf(stderr, "FAIL: Expected no changes after reset\n");
+ return 1;
+ }
+
+ printf("PASS: FileWatcher tests\n");
+ return 0;
+}
+
+#else
+
+int main() {
+ printf("SKIP: FileWatcher tests (STRIP_ALL build)\n");
+ return 0;
+}
+
+#endif
diff --git a/src/tests/test_gpu_composite.cc b/src/tests/test_gpu_composite.cc
new file mode 100644
index 0000000..e5ac788
--- /dev/null
+++ b/src/tests/test_gpu_composite.cc
@@ -0,0 +1,124 @@
+// This file is part of the 64k demo project.
+// Tests GPU composite texture generation (Phase 4).
+
+#include "gpu/gpu.h"
+#include "gpu/texture_manager.h"
+#include "platform/platform.h"
+#include <cstdint>
+#include <cstdio>
+#include <vector>
+
+#if !defined(STRIP_GPU_COMPOSITE)
+
+int main() {
+ printf("GPU Composite Test: Starting...\n");
+
+ // Initialize GPU
+ PlatformState platform = platform_init(false, 256, 256);
+ if (!platform.window) {
+ fprintf(stderr, "Error: Failed to create window\n");
+ return 1;
+ }
+
+ gpu_init(&platform);
+ const GpuContext* ctx = gpu_get_context();
+
+ extern void InitShaderComposer();
+ InitShaderComposer();
+
+ TextureManager tex_mgr;
+ tex_mgr.init(ctx->device, ctx->queue);
+
+ // Create base textures
+ float noise_params_a[2] = {1234.0f, 4.0f};
+ GpuProceduralParams noise_a = {256, 256, noise_params_a, 2};
+ tex_mgr.create_gpu_noise_texture("noise_a", noise_a);
+
+ float noise_params_b[2] = {5678.0f, 8.0f};
+ GpuProceduralParams noise_b = {256, 256, noise_params_b, 2};
+ tex_mgr.create_gpu_noise_texture("noise_b", noise_b);
+
+ float grid_params[2] = {32.0f, 2.0f};
+ GpuProceduralParams grid = {256, 256, grid_params, 2};
+ tex_mgr.create_gpu_grid_texture("grid", grid);
+
+ printf("SUCCESS: Base textures created (noise_a, noise_b, grid)\n");
+
+ // Test blend composite
+ extern const char* gen_blend_compute_wgsl;
+ struct {
+ uint32_t width, height;
+ float blend_factor, _pad0;
+ } blend_uni = {256, 256, 0.5f, 0.0f};
+
+ std::vector<std::string> blend_inputs = {"noise_a", "noise_b"};
+ tex_mgr.create_gpu_composite_texture("blended", "gen_blend",
+ gen_blend_compute_wgsl, &blend_uni,
+ sizeof(blend_uni), 256, 256, blend_inputs);
+
+ WGPUTextureView blended_view = tex_mgr.get_texture_view("blended");
+ if (!blended_view) {
+ fprintf(stderr, "Error: Blended texture not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+ printf("SUCCESS: Blend composite created (noise_a + noise_b)\n");
+
+ // Test mask composite
+ extern const char* gen_mask_compute_wgsl;
+ struct {
+ uint32_t width, height;
+ } mask_uni = {256, 256};
+
+ std::vector<std::string> mask_inputs = {"noise_a", "grid"};
+ tex_mgr.create_gpu_composite_texture("masked", "gen_mask", gen_mask_compute_wgsl,
+ &mask_uni, sizeof(mask_uni), 256, 256,
+ mask_inputs);
+
+ WGPUTextureView masked_view = tex_mgr.get_texture_view("masked");
+ if (!masked_view) {
+ fprintf(stderr, "Error: Masked texture not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+ printf("SUCCESS: Mask composite created (noise_a * grid)\n");
+
+ // Test multi-stage composite (composite of composite)
+ struct {
+ uint32_t width, height;
+ float blend_factor, _pad0;
+ } blend2_uni = {256, 256, 0.7f, 0.0f};
+
+ std::vector<std::string> blend2_inputs = {"blended", "masked"};
+ tex_mgr.create_gpu_composite_texture("final", "gen_blend",
+ gen_blend_compute_wgsl, &blend2_uni,
+ sizeof(blend2_uni), 256, 256, blend2_inputs);
+
+ WGPUTextureView final_view = tex_mgr.get_texture_view("final");
+ if (!final_view) {
+ fprintf(stderr, "Error: Multi-stage composite not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+ printf("SUCCESS: Multi-stage composite (composite of composites)\n");
+
+ // Cleanup
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ platform_shutdown(&platform);
+
+ printf("All GPU composite tests passed!\n");
+ return 0;
+}
+
+#else
+
+int main() {
+ printf("GPU Composite Test: SKIPPED (STRIP_GPU_COMPOSITE defined)\n");
+ return 0;
+}
+
+#endif
diff --git a/src/tests/test_gpu_procedural.cc b/src/tests/test_gpu_procedural.cc
new file mode 100644
index 0000000..f1bade0
--- /dev/null
+++ b/src/tests/test_gpu_procedural.cc
@@ -0,0 +1,117 @@
+// This file is part of the 64k demo project.
+// Tests GPU procedural texture generation.
+
+#include "gpu/gpu.h"
+#include "gpu/texture_manager.h"
+#include "platform/platform.h"
+#include <cstdio>
+
+int main() {
+ printf("GPU Procedural Test: Starting...\n");
+
+ // Minimal GPU initialization for testing
+ PlatformState platform = platform_init(false, 256, 256);
+ if (!platform.window) {
+ fprintf(stderr, "Error: Failed to create window\n");
+ return 1;
+ }
+
+ gpu_init(&platform);
+ const GpuContext* ctx = gpu_get_context();
+
+ // Initialize shader composer (needed for #include resolution)
+ extern void InitShaderComposer();
+ InitShaderComposer();
+
+ // Create TextureManager
+ TextureManager tex_mgr;
+ tex_mgr.init(ctx->device, ctx->queue);
+
+ // Test GPU noise generation
+ GpuProceduralParams params = {};
+ params.width = 256;
+ params.height = 256;
+ float proc_params[2] = {0.0f, 4.0f}; // seed, frequency
+ params.params = proc_params;
+ params.num_params = 2;
+
+ tex_mgr.create_gpu_noise_texture("test_noise", params);
+
+ // Verify texture exists
+ WGPUTextureView view = tex_mgr.get_texture_view("test_noise");
+ if (!view) {
+ fprintf(stderr, "Error: GPU noise texture not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+ printf("SUCCESS: GPU noise texture created (256x256)\n");
+
+ // Test pipeline caching (create second noise texture)
+ tex_mgr.create_gpu_noise_texture("test_noise_2", params);
+ WGPUTextureView view2 = tex_mgr.get_texture_view("test_noise_2");
+ if (!view2) {
+ fprintf(stderr, "Error: Second GPU noise texture not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+ printf("SUCCESS: Pipeline caching works (second noise texture)\n");
+
+ // Test GPU perlin generation
+ float perlin_params[5] = {42.0f, 4.0f, 1.0f, 0.5f, 6.0f};
+ GpuProceduralParams perlin = {512, 256, perlin_params, 5};
+ tex_mgr.create_gpu_perlin_texture("test_perlin", perlin);
+ WGPUTextureView perlin_view = tex_mgr.get_texture_view("test_perlin");
+ if (!perlin_view) {
+ fprintf(stderr, "Error: GPU perlin texture not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+ printf("SUCCESS: GPU perlin texture created (512x256)\n");
+
+ // Test GPU grid generation
+ float grid_params[2] = {32.0f, 2.0f};
+ GpuProceduralParams grid = {256, 256, grid_params, 2};
+ tex_mgr.create_gpu_grid_texture("test_grid", grid);
+ WGPUTextureView grid_view = tex_mgr.get_texture_view("test_grid");
+ if (!grid_view) {
+ fprintf(stderr, "Error: GPU grid texture not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+ printf("SUCCESS: GPU grid texture created (256x256)\n");
+
+ // Test multiple pipelines coexist
+ printf("SUCCESS: All three GPU generators work (unified pipeline system)\n");
+
+ // Test variable-size textures
+ float noise_small[2] = {999.0f, 8.0f};
+ GpuProceduralParams small = {128, 64, noise_small, 2};
+ tex_mgr.create_gpu_noise_texture("noise_128x64", small);
+ if (!tex_mgr.get_texture_view("noise_128x64")) {
+ fprintf(stderr, "Error: Variable-size texture (128x64) not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+
+ float noise_large[2] = {777.0f, 2.0f};
+ GpuProceduralParams large = {1024, 512, noise_large, 2};
+ tex_mgr.create_gpu_noise_texture("noise_1024x512", large);
+ if (!tex_mgr.get_texture_view("noise_1024x512")) {
+ fprintf(stderr, "Error: Variable-size texture (1024x512) not created\n");
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ return 1;
+ }
+ printf("SUCCESS: Variable-size textures work (128x64, 1024x512)\n");
+
+ // Cleanup
+ tex_mgr.shutdown();
+ gpu_shutdown();
+ platform_shutdown(&platform);
+ return 0;
+}
diff --git a/src/tests/test_post_process_helper.cc b/src/tests/test_post_process_helper.cc
index 104bbc3..36d193e 100644
--- a/src/tests/test_post_process_helper.cc
+++ b/src/tests/test_post_process_helper.cc
@@ -182,14 +182,14 @@ static void test_bind_group_update() {
// Create initial bind group
WGPUBindGroup bind_group = nullptr;
- pp_update_bind_group(fixture.device(), pipeline, &bind_group, view1,
- uniforms, dummy_effect_params_buffer);
+ pp_update_bind_group(fixture.device(), pipeline, &bind_group, view1, uniforms,
+ dummy_effect_params_buffer);
assert(bind_group != nullptr && "Initial bind group should be created");
fprintf(stdout, " ✓ Initial bind group created\n");
// Update bind group (should release old and create new)
- pp_update_bind_group(fixture.device(), pipeline, &bind_group, view2,
- uniforms, dummy_effect_params_buffer);
+ pp_update_bind_group(fixture.device(), pipeline, &bind_group, view2, uniforms,
+ dummy_effect_params_buffer);
assert(bind_group != nullptr && "Updated bind group should be created");
fprintf(stdout, " ✓ Bind group updated successfully\n");
diff --git a/src/tests/test_shader_compilation.cc b/src/tests/test_shader_compilation.cc
index e2c0adc..a322e8a 100644
--- a/src/tests/test_shader_compilation.cc
+++ b/src/tests/test_shader_compilation.cc
@@ -115,16 +115,19 @@ static bool test_shader_compilation(const char* name, const char* shader_code) {
return true; // Not a failure, just skipped
}
+ // Compose shader to resolve #include directives
+ std::string composed_shader = ShaderComposer::Get().Compose({}, shader_code);
+
#if defined(DEMO_CROSS_COMPILE_WIN32)
WGPUShaderModuleWGSLDescriptor wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
- wgsl_desc.code = shader_code;
+ wgsl_desc.code = composed_shader.c_str();
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#else
WGPUShaderSourceWGSL wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
- wgsl_desc.code = str_view(shader_code);
+ wgsl_desc.code = str_view(composed_shader.c_str());
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#endif