summaryrefslogtreecommitdiff
path: root/src/gpu/effect.cc
blob: 040b52357dcabe6104b35c758a743d9aaefcbd57 (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
233
234
235
236
237
238
239
240
241
// This file is part of the 64k demo project.
// It implements the Sequence management logic.

#include "effect.h"
#include <algorithm>
#include <cstdio>

// --- Sequence Implementation ---

void Sequence::init(MainSequence *demo) {
  for (auto &item : items_) {
    if (!item.effect->is_initialized) {
      item.effect->init(demo);
      item.effect->is_initialized = true;
    }
  }
}

void Sequence::add_effect(std::shared_ptr<Effect> effect, float start_time,
                          float end_time, int priority) {
  items_.push_back({effect, start_time, end_time, priority, false});
  is_sorted_ = false;
}

void Sequence::sort_items() {
  if (is_sorted_) return;
  // Sort by priority ascending (0 draws first, 100 draws on top)
  std::sort(items_.begin(), items_.end(),
            [](const SequenceItem &a, const SequenceItem &b) {
              return a.priority < b.priority;
            });
  is_sorted_ = true;
}

void Sequence::update_active_list(float seq_time) {
  for (auto &item : items_) {
    bool should_be_active =
        (seq_time >= item.start_time && seq_time < item.end_time);

    if (should_be_active) {
      if (!item.active) {
        item.effect->start();
        item.active = true;
      }
    } else {
      if (item.active) {
        item.effect->end();
        item.active = false;
      }
    }
  }
}

void Sequence::dispatch_compute(WGPUCommandEncoder encoder, float seq_time,
                                float beat, float intensity,
                                float aspect_ratio) {
  sort_items();
  for (auto &item : items_) {
    if (item.active) {
      item.effect->compute(encoder, seq_time - item.start_time, beat, intensity,
                           aspect_ratio);
    }
  }
}

void Sequence::dispatch_render(WGPURenderPassEncoder pass, float seq_time,
                               float beat, float intensity,
                               float aspect_ratio) {
  sort_items(); // Should be sorted already but safe to check
  for (auto &item : items_) {
    if (item.active) {
      item.effect->render(pass, seq_time - item.start_time, beat, intensity,
                          aspect_ratio);
    }
  }
}

void Sequence::reset() {
  for (auto &item : items_) {
    if (item.active) {
      item.effect->end();
      item.active = false;
    }
  }
}

// --- MainSequence Implementation ---

void MainSequence::init(WGPUDevice d, WGPUQueue q, WGPUTextureFormat f) {
  device = d;
  queue = q;
  format = f;
  
  for (auto &entry : sequences_) {
    entry.seq->init(this);
  }
}

void MainSequence::add_sequence(std::shared_ptr<Sequence> seq, float start_time, int priority) {
  sequences_.push_back({seq, start_time, priority});
  // Sort sequences by priority
  std::sort(sequences_.begin(), sequences_.end(),
            [](const ActiveSequence &a, const ActiveSequence &b) {
              return a.priority < b.priority;
            });
}

void MainSequence::render_frame(float global_time, float beat, float peak,
                                float aspect_ratio, WGPUSurface surface) {
  WGPUSurfaceTexture surface_texture;
  wgpuSurfaceGetCurrentTexture(surface, &surface_texture);
  
#if defined(DEMO_CROSS_COMPILE_WIN32)
  #define STATUS_OPTIMAL WGPUSurfaceGetCurrentTextureStatus_Success
  #define STATUS_SUBOPTIMAL WGPUSurfaceGetCurrentTextureStatus_Success
#else
  #define STATUS_OPTIMAL WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal
  #define STATUS_SUBOPTIMAL WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal
#endif

  if (surface_texture.status != STATUS_OPTIMAL &&
      surface_texture.status != STATUS_SUBOPTIMAL) {
    return;
  }

  WGPUTextureView view = wgpuTextureCreateView(surface_texture.texture, nullptr);

  WGPUCommandEncoderDescriptor encoder_desc = {};
  WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, &encoder_desc);

  // 1. Update & Compute Phase
  for (auto &entry : sequences_) {
    // Check if sequence is active (start_time <= global_time)
    // We assume sequences run until end of demo or have internal end? 
    // User said "Sequence ... overlap". Implicitly they might have duration but here we just check start.
    // Let's assume they are active if time >= start.
    // Effects inside sequence handle duration.
    if (global_time >= entry.start_time) {
        float seq_time = global_time - entry.start_time;
        entry.seq->update_active_list(seq_time);
        
        // Pass generic aspect ratio 16:9 for compute? 
        // Or wait for render. Particles compute uses it.
        // We can get it from surface texture size if we want?
        // Let's pass 1.777f for now or fetch.
        // gpu_draw used to pass it. We need it here.
        // Wait, render_frame doesn't take aspect_ratio. gpu_draw did.
        // I should add aspect_ratio to render_frame or calculate it from surface.
    }
  }
  
  for (auto &entry : sequences_) {
    if (global_time >= entry.start_time) {
        entry.seq->dispatch_compute(encoder, global_time - entry.start_time, beat, peak, aspect_ratio);
    }
  }

  // 2. Render Phase
  {
    WGPURenderPassColorAttachment color_attachment = {};
    color_attachment.view = view;
    color_attachment.loadOp = WGPULoadOp_Clear;
    color_attachment.storeOp = WGPUStoreOp_Store;
    
    // Clear color logic could be dynamic or part of a "BackgroundEffect"?
    // For now hardcode.
    float flash = peak * 0.2f;
    color_attachment.clearValue = {0.05 + flash, 0.1 + flash, 0.2 + flash, 1.0};
    
#if !defined(DEMO_CROSS_COMPILE_WIN32)
    color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
#endif

    WGPURenderPassDescriptor render_pass_desc = {};
    render_pass_desc.colorAttachmentCount = 1;
    render_pass_desc.colorAttachments = &color_attachment;

    WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &render_pass_desc);

    for (auto &entry : sequences_) {
        if (global_time >= entry.start_time) {
            entry.seq->dispatch_render(pass, global_time - entry.start_time, beat, peak, aspect_ratio);
        }
    }

    wgpuRenderPassEncoderEnd(pass);
  }

  WGPUCommandBufferDescriptor cmd_desc = {};
  WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, &cmd_desc);
  wgpuQueueSubmit(queue, 1, &commands);
  wgpuSurfacePresent(surface);

  wgpuTextureViewRelease(view);
  wgpuTextureRelease(surface_texture.texture);
}

void MainSequence::simulate_until(float target_time, float step_rate) {
#ifndef STRIP_ALL
  // Assuming 128 BPM as per main.cc.
  // Ideally this should be passed in or shared.
  const float bpm = 128.0f;
  const float aspect_ratio = 16.0f / 9.0f; // Dummy aspect

  for (float t = 0.0f; t < target_time; t += step_rate) {
    WGPUCommandEncoderDescriptor encoder_desc = {};
    WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, &encoder_desc);

    float beat = fmodf(t * bpm / 60.0f, 1.0f);

    // Update active lists
    for (auto &entry : sequences_) {
      if (t >= entry.start_time) {
        entry.seq->update_active_list(t - entry.start_time);
      }
    }

    // Dispatch compute
    for (auto &entry : sequences_) {
      if (t >= entry.start_time) {
        // peak = 0.0f during simulation (no audio analysis)
        entry.seq->dispatch_compute(encoder, t - entry.start_time, beat, 0.0f, aspect_ratio);
      }
    }

    WGPUCommandBufferDescriptor cmd_desc = {};
    WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, &cmd_desc);
    wgpuQueueSubmit(queue, 1, &commands);
  }
#else
  (void)target_time;
  (void)step_rate;
#endif
}

void MainSequence::shutdown() {
    for (auto &entry : sequences_) {
        entry.seq->reset();
    }
    sequences_.clear();
}