summaryrefslogtreecommitdiff
path: root/src/tests/test_gpu_procedural.cc
blob: f1bade0b1d98a6bcfc2fdb29149565176b9a8a11 (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
// 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;
}