summaryrefslogtreecommitdiff
path: root/src/tests/test_shader_compilation.cc
blob: 8b3b5f55d57b4ce070ddc7479bf41c6489aae60d (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
// This file is part of the 64k demo project.
// It validates that all production shaders compile successfully with WebGPU.
// This catches issues like:
// - Invalid WGSL syntax (e.g., undefined functions like inverse())
// - Missing binding declarations
// - Type mismatches

#include "generated/assets.h"
#include "gpu/effects/shader_composer.h"
#include "gpu/effects/shaders.h"
#include "platform.h"
#include <cassert>
#include <cstdio>
#include <cstring>
#include <string>

static WGPUDevice g_device = nullptr;

// Initialize minimal WebGPU for shader compilation testing
static bool init_wgpu() {
  WGPUInstance instance = wgpuCreateInstance(nullptr);
  if (!instance) {
    fprintf(stderr, "Failed to create WGPU instance.\n");
    return false;
  }

  WGPURequestAdapterOptions adapter_opts = {};
  adapter_opts.powerPreference = WGPUPowerPreference_HighPerformance;

  WGPUAdapter adapter = nullptr;

#if defined(DEMO_CROSS_COMPILE_WIN32)
  auto on_adapter = [](WGPURequestAdapterStatus status, WGPUAdapter a,
                       const char* message, void* userdata) {
    if (status == WGPURequestAdapterStatus_Success) {
      *(WGPUAdapter*)userdata = a;
    }
  };
  wgpuInstanceRequestAdapter(instance, &adapter_opts, on_adapter, &adapter);
#else
  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

  // Try to wait for adapter (may not work on all platforms)
  for (int i = 0; i < 100 && !adapter; ++i) {
    wgpuInstanceProcessEvents(instance);
  }

  if (!adapter) {
    fprintf(stderr,
            "Warning: Could not get WGPU adapter (GPU compilation tests "
            "skipped)\n");
    return false;
  }

  WGPUDeviceDescriptor device_desc = {};

#if defined(DEMO_CROSS_COMPILE_WIN32)
  auto on_device = [](WGPURequestDeviceStatus status, WGPUDevice d,
                      const char* message, void* userdata) {
    if (status == WGPURequestDeviceStatus_Success) {
      *(WGPUDevice*)userdata = d;
    }
  };
  wgpuAdapterRequestDevice(adapter, &device_desc, on_device, &g_device);
#else
  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 = &g_device;
  wgpuAdapterRequestDevice(adapter, &device_desc, device_cb);
#endif

  // Try to wait for device (may not work on all platforms)
  for (int i = 0; i < 100 && !g_device; ++i) {
    wgpuInstanceProcessEvents(instance);
  }

  if (!g_device) {
    fprintf(stderr,
            "Warning: Could not get WGPU device (GPU compilation tests "
            "skipped)\n");
    return false;
  }

  return true;
}

// Test shader compilation
static bool test_shader_compilation(const char* name,
                                     const char* shader_code) {
  printf("Testing compilation: %s...\n", name);

  if (!g_device) {
    printf("SKIPPED: %s (no GPU device)\n", name);
    return true;  // Not a failure, just skipped
  }

#if defined(DEMO_CROSS_COMPILE_WIN32)
  WGPUShaderModuleWGSLDescriptor wgsl_desc = {};
  wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
  wgsl_desc.code = shader_code;
  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);
  WGPUShaderModuleDescriptor shader_desc = {};
  shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#endif

  WGPUShaderModule shader_module =
      wgpuDeviceCreateShaderModule(g_device, &shader_desc);

  if (!shader_module) {
    printf("FAILED: %s - shader compilation failed!\n", name);
    return false;
  }

  wgpuShaderModuleRelease(shader_module);
  printf("PASSED: %s\n", name);
  return true;
}

// Test composed shader with different modes
static bool test_composed_shader(const char* base_name, AssetId asset_id,
                                  bool with_bvh) {
  const char* mode_name = with_bvh ? "BVH" : "Linear";
  char test_name[128];
  snprintf(test_name, sizeof(test_name), "%s (%s mode)", base_name, mode_name);

  const char* shader_asset = (const char*)GetAsset(asset_id);
  std::string main_code = shader_asset;

  ShaderComposer::CompositionMap composition_map;
  if (with_bvh) {
    composition_map["render/scene_query_mode"] = "render/scene_query_bvh";
  } else {
    composition_map["render/scene_query_mode"] = "render/scene_query_linear";
  }

  std::string composed_shader =
      ShaderComposer::Get().Compose({}, main_code, composition_map);

  return test_shader_compilation(test_name, composed_shader.c_str());
}

int main() {
  printf("===========================================\n");
  printf("Shader Compilation Test Suite\n");
  printf("===========================================\n\n");

  bool gpu_available = init_wgpu();
  if (!gpu_available) {
    printf(
        "Note: GPU not available - running composition-only tests\n\n");
  }

  // Initialize shader composer
  InitShaderComposer();

  bool all_passed = true;

  // Test 1: Simple shaders that don't need composition
  printf("\n--- Test 1: Simple Shaders ---\n");
  all_passed &= test_shader_compilation(
      "Passthrough", (const char*)GetAsset(AssetId::ASSET_SHADER_PASSTHROUGH));
  all_passed &= test_shader_compilation(
      "Ellipse", (const char*)GetAsset(AssetId::ASSET_SHADER_ELLIPSE));
  all_passed &= test_shader_compilation(
      "Gaussian Blur",
      (const char*)GetAsset(AssetId::ASSET_SHADER_GAUSSIAN_BLUR));
  all_passed &= test_shader_compilation(
      "Solarize", (const char*)GetAsset(AssetId::ASSET_SHADER_SOLARIZE));

  // Test 2: Composed shaders (both BVH and Linear modes)
  printf("\n--- Test 2: Composed Shaders (BVH Mode) ---\n");
  all_passed &= test_composed_shader("Renderer 3D",
                                      AssetId::ASSET_SHADER_RENDERER_3D, true);
  all_passed &=
      test_composed_shader("Mesh Render", AssetId::ASSET_SHADER_MESH, true);

  printf("\n--- Test 3: Composed Shaders (Linear Mode) ---\n");
  all_passed &= test_composed_shader("Renderer 3D",
                                      AssetId::ASSET_SHADER_RENDERER_3D, false);
  all_passed &=
      test_composed_shader("Mesh Render", AssetId::ASSET_SHADER_MESH, false);

  // Test 3: Compute shaders
  printf("\n--- Test 4: Compute Shaders ---\n");
  all_passed &= test_shader_compilation(
      "Particle Compute",
      (const char*)GetAsset(AssetId::ASSET_SHADER_PARTICLE_COMPUTE));
  all_passed &= test_shader_compilation(
      "Particle Spray Compute",
      (const char*)GetAsset(AssetId::ASSET_SHADER_PARTICLE_SPRAY_COMPUTE));

  printf("\n===========================================\n");
  if (all_passed) {
    printf("All shader compilation tests PASSED ✓\n");
  } else {
    printf("Some shader compilation tests FAILED ✗\n");
  }
  printf("===========================================\n");

  if (g_device) {
    wgpuDeviceRelease(g_device);
  }

  return all_passed ? 0 : 1;
}