summaryrefslogtreecommitdiff
path: root/src/tests/gpu/test_gpu_composite.cc
blob: e5ac7889605eb78f4b06c7a229c05bffcc1c9401 (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
// 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