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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// This file is part of the 64k demo project.
// It implements the Sequence management logic.
#include "effect.h"
#include "gpu/demo_effects.h"
#include "gpu/gpu.h"
#include <algorithm>
#include <cstdio>
#include <vector>
// --- PostProcessEffect ---
void PostProcessEffect::render(WGPURenderPassEncoder pass, float, float, float,
float) {
if (pipeline_ && bind_group_) {
wgpuRenderPassEncoderSetPipeline(pass, pipeline_);
wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr);
wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); // Fullscreen triangle
}
}
// --- 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;
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 && !item.active) {
item.effect->start();
item.active = true;
} else if (!should_be_active && item.active) {
item.effect->end();
item.active = false;
}
}
}
void Sequence::collect_active_effects(
std::vector<SequenceItem *> &scene_effects,
std::vector<SequenceItem *> &post_effects) {
sort_items();
for (auto &item : items_) {
if (item.active) {
if (item.effect->is_post_process()) {
post_effects.push_back(&item);
} else {
scene_effects.push_back(&item);
}
}
}
}
void Sequence::reset() {
for (auto &item : items_) {
if (item.active) {
item.effect->end();
item.active = false;
}
}
}
// --- MainSequence Implementation ---
MainSequence::MainSequence() = default;
MainSequence::~MainSequence() = default;
void MainSequence::create_framebuffers(int width, int height) {
WGPUTextureDescriptor desc = {};
desc.usage =
WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding;
desc.dimension = WGPUTextureDimension_2D;
desc.size = {(uint32_t)width, (uint32_t)height, 1};
desc.format = format;
desc.mipLevelCount = 1;
desc.sampleCount = 1;
framebuffer_a_ = wgpuDeviceCreateTexture(device, &desc);
framebuffer_b_ = wgpuDeviceCreateTexture(device, &desc);
WGPUTextureViewDescriptor view_desc = {};
view_desc.dimension = WGPUTextureViewDimension_2D;
view_desc.format = format;
view_desc.mipLevelCount = 1;
view_desc.arrayLayerCount = 1;
framebuffer_view_a_ = wgpuTextureCreateView(framebuffer_a_, &view_desc);
framebuffer_view_b_ = wgpuTextureCreateView(framebuffer_b_, &view_desc);
}
void MainSequence::init(WGPUDevice d, WGPUQueue q, WGPUTextureFormat f,
int width, int height) {
device = d;
queue = q;
format = f;
create_framebuffers(width, height);
passthrough_effect_ = std::make_unique<PassthroughEffect>(device, format);
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});
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) {
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
std::vector<SequenceItem *> scene_effects;
std::vector<SequenceItem *> post_effects;
for (auto &entry : sequences_) {
if (global_time >= entry.start_time) {
float seq_time = global_time - entry.start_time;
entry.seq->update_active_list(seq_time);
entry.seq->collect_active_effects(scene_effects, post_effects);
}
}
std::sort(scene_effects.begin(), scene_effects.end(),
[](const SequenceItem *a, const SequenceItem *b) {
return a->priority < b->priority;
});
std::sort(post_effects.begin(), post_effects.end(),
[](const SequenceItem *a, const SequenceItem *b) {
return a->priority < b->priority;
});
// 1. Compute
for (const auto &item : scene_effects) {
item->effect->compute(encoder, global_time - item->start_time, beat, peak,
aspect_ratio);
}
// 2. Scene Pass (to A)
WGPURenderPassColorAttachment scene_attachment = {};
scene_attachment.view = framebuffer_view_a_;
scene_attachment.loadOp = WGPULoadOp_Clear;
scene_attachment.storeOp = WGPUStoreOp_Store;
scene_attachment.clearValue = {0, 0, 0, 1};
#if !defined(DEMO_CROSS_COMPILE_WIN32)
scene_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
#endif
WGPURenderPassDescriptor scene_desc = {.colorAttachmentCount = 1,
.colorAttachments = &scene_attachment};
WGPURenderPassEncoder scene_pass =
wgpuCommandEncoderBeginRenderPass(encoder, &scene_desc);
for (const auto &item : scene_effects) {
item->effect->render(scene_pass, global_time - item->start_time, beat, peak,
aspect_ratio);
}
wgpuRenderPassEncoderEnd(scene_pass);
// 3. Post Chain
if (post_effects.empty()) {
WGPUSurfaceTexture st;
wgpuSurfaceGetCurrentTexture(surface, &st);
WGPUTextureView final_view = wgpuTextureCreateView(st.texture, nullptr);
passthrough_effect_->update_bind_group(framebuffer_view_a_);
WGPURenderPassColorAttachment final_attachment = {
.view = final_view, .loadOp = WGPULoadOp_Load, .storeOp = WGPUStoreOp_Store};
WGPURenderPassDescriptor final_desc = {.colorAttachmentCount = 1,
.colorAttachments =
&final_attachment};
WGPURenderPassEncoder final_pass =
wgpuCommandEncoderBeginRenderPass(encoder, &final_desc);
passthrough_effect_->render(final_pass, 0, 0, 0, aspect_ratio);
wgpuRenderPassEncoderEnd(final_pass);
wgpuTextureViewRelease(final_view);
wgpuSurfacePresent(surface);
wgpuTextureRelease(st.texture);
} else {
WGPUTextureView current_input = framebuffer_view_a_;
for (size_t i = 0; i < post_effects.size(); ++i) {
bool is_last = (i == post_effects.size() - 1);
WGPUSurfaceTexture st;
if (is_last)
wgpuSurfaceGetCurrentTexture(surface, &st);
WGPUTextureView current_output =
is_last ? wgpuTextureCreateView(st.texture, nullptr)
: (current_input == framebuffer_view_a_ ? framebuffer_view_b_
: framebuffer_view_a_);
PostProcessEffect *pp =
static_cast<PostProcessEffect *>(post_effects[i]->effect.get());
pp->update_bind_group(current_input);
WGPURenderPassColorAttachment pp_attachment = {
.view = current_output, .loadOp = WGPULoadOp_Load, .storeOp = WGPUStoreOp_Store};
WGPURenderPassDescriptor pp_desc = {.colorAttachmentCount = 1,
.colorAttachments = &pp_attachment};
WGPURenderPassEncoder pp_pass =
wgpuCommandEncoderBeginRenderPass(encoder, &pp_desc);
pp->render(pp_pass, global_time - post_effects[i]->start_time, beat, peak,
aspect_ratio);
wgpuRenderPassEncoderEnd(pp_pass);
if (is_last) {
wgpuTextureViewRelease(current_output);
wgpuSurfacePresent(surface);
wgpuTextureRelease(st.texture);
}
current_input = current_output;
}
}
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, nullptr);
wgpuQueueSubmit(queue, 1, &commands);
}
void MainSequence::shutdown() {
if (framebuffer_view_a_) wgpuTextureViewRelease(framebuffer_view_a_);
if (framebuffer_a_) wgpuTextureRelease(framebuffer_a_);
if (framebuffer_view_b_) wgpuTextureViewRelease(framebuffer_view_b_);
if (framebuffer_b_) wgpuTextureRelease(framebuffer_b_);
for (auto &entry : sequences_) {
entry.seq->reset();
}
}
#ifndef STRIP_ALL
void MainSequence::simulate_until(float target_time, float step_rate) {
const float bpm = 128.0f;
const float aspect_ratio = 16.0f / 9.0f;
for (float t = 0.0f; t < target_time; t += step_rate) {
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
float beat = fmodf(t * bpm / 60.0f, 1.0f);
std::vector<SequenceItem *> scene_effects, post_effects;
for (auto &entry : sequences_) {
if (t >= entry.start_time) {
entry.seq->update_active_list(t - entry.start_time);
entry.seq->collect_active_effects(scene_effects, post_effects);
}
}
for (const auto &item : scene_effects) {
item->effect->compute(encoder, t - item->start_time, beat, 0.0f, aspect_ratio);
}
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, nullptr);
wgpuQueueSubmit(queue, 1, &commands);
}
}
#endif
|