diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/audio/audio.cc | 15 | ||||
| -rw-r--r-- | src/audio/audio.h | 4 | ||||
| -rw-r--r-- | src/audio/dct.h | 4 | ||||
| -rw-r--r-- | src/audio/fdct.cc | 2 | ||||
| -rw-r--r-- | src/audio/gen.cc | 41 | ||||
| -rw-r--r-- | src/audio/gen.h | 14 | ||||
| -rw-r--r-- | src/audio/idct.cc | 2 | ||||
| -rw-r--r-- | src/audio/synth.cc | 41 | ||||
| -rw-r--r-- | src/audio/synth.h | 10 | ||||
| -rw-r--r-- | src/audio/window.cc | 2 | ||||
| -rw-r--r-- | src/audio/window.h | 2 | ||||
| -rw-r--r-- | src/generated/timeline.cc | 2 | ||||
| -rw-r--r-- | src/gpu/demo_effects.cc | 30 | ||||
| -rw-r--r-- | src/gpu/demo_effects.h | 40 | ||||
| -rw-r--r-- | src/gpu/effect.cc | 75 | ||||
| -rw-r--r-- | src/gpu/effect.h | 51 | ||||
| -rw-r--r-- | src/gpu/gpu.cc | 106 | ||||
| -rw-r--r-- | src/gpu/gpu.h | 38 | ||||
| -rw-r--r-- | src/main.cc | 51 | ||||
| -rw-r--r-- | src/platform.cc | 28 | ||||
| -rw-r--r-- | src/platform.h | 2 | ||||
| -rw-r--r-- | src/tests/test_assets.cc | 12 | ||||
| -rw-r--r-- | src/tests/test_maths.cc | 19 | ||||
| -rw-r--r-- | src/tests/test_sequence.cc | 37 | ||||
| -rw-r--r-- | src/tests/test_spectool.cc | 8 | ||||
| -rw-r--r-- | src/util/asset_manager.cc | 12 | ||||
| -rw-r--r-- | src/util/asset_manager.h | 6 | ||||
| -rw-r--r-- | src/util/math.h | 4 | ||||
| -rw-r--r-- | src/util/mini_math.h | 111 |
29 files changed, 436 insertions, 333 deletions
diff --git a/src/audio/audio.cc b/src/audio/audio.cc index b482e64..e4abbf8 100644 --- a/src/audio/audio.cc +++ b/src/audio/audio.cc @@ -4,12 +4,12 @@ #include "audio.h" -#ifndef DEMO_BUILD_TOOLS +#if !defined(DEMO_BUILD_TOOLS) #define MA_NO_FLAC #define MA_NO_ENCODING #define MA_NO_MP3 #define MA_NO_WAV -#endif +#endif /* !defined(DEMO_BUILD_TOOLS) */ #define MINIAUDIO_IMPLEMENTATION #include "miniaudio.h" #include "synth.h" @@ -18,10 +18,10 @@ static ma_device g_device; -void audio_data_callback(ma_device *pDevice, void *pOutput, const void *pInput, +void audio_data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) { (void)pInput; - float *fOutput = (float *)pOutput; + float* fOutput = (float*)pOutput; synth_render(fOutput, (int)frameCount); } @@ -48,7 +48,7 @@ void audio_start() { } } -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) void audio_render_silent(float duration_sec) { const int sample_rate = 32000; const int chunk_size = 512; @@ -62,9 +62,10 @@ void audio_render_silent(float duration_sec) { total_frames -= frames_to_render; } } -#endif +#endif /* !defined(STRIP_ALL) */ -void audio_update() {} +void audio_update() { +} void audio_shutdown() { ma_device_stop(&g_device); diff --git a/src/audio/audio.h b/src/audio/audio.h index 1fb53b6..24db18f 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -6,8 +6,8 @@ void audio_init(); void audio_start(); // Starts the audio device callback -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) void audio_render_silent(float duration_sec); // Fast-forwards audio state -#endif +#endif /* !defined(STRIP_ALL) */ void audio_update(); void audio_shutdown(); diff --git a/src/audio/dct.h b/src/audio/dct.h index b6b7126..ee3e9b3 100644 --- a/src/audio/dct.h +++ b/src/audio/dct.h @@ -7,5 +7,5 @@ #define DCT_SIZE 512 // Forward declarations -void fdct_512(const float *input, float *output); -void idct_512(const float *input, float *output); +void fdct_512(const float* input, float* output); +void idct_512(const float* input, float* output); diff --git a/src/audio/fdct.cc b/src/audio/fdct.cc index 3541e9c..78973a3 100644 --- a/src/audio/fdct.cc +++ b/src/audio/fdct.cc @@ -5,7 +5,7 @@ #include "dct.h" #include <math.h> -void fdct_512(const float *input, float *output) { +void fdct_512(const float* input, float* output) { const float PI = 3.14159265358979323846f; for (int k = 0; k < DCT_SIZE; ++k) { float sum = 0.0f; diff --git a/src/audio/gen.cc b/src/audio/gen.cc index ddc4fa6..148fc68 100644 --- a/src/audio/gen.cc +++ b/src/audio/gen.cc @@ -9,10 +9,11 @@ #include <stdlib.h> #include <string.h> -std::vector<float> generate_note_spectrogram(const NoteParams ¶ms, - int *out_num_frames) { +std::vector<float> generate_note_spectrogram(const NoteParams& params, + int* out_num_frames) { int num_frames = (int)(params.duration_sec * 32000.0f / DCT_SIZE); - if (num_frames < 1) num_frames = 1; + if (num_frames < 1) + num_frames = 1; *out_num_frames = num_frames; std::vector<float> spec_data(num_frames * DCT_SIZE, 0.0f); @@ -32,7 +33,9 @@ std::vector<float> generate_note_spectrogram(const NoteParams ¶ms, // Envelope (Simple Attack) float env = 1.0f; - if (t < params.attack_sec) { env = t / params.attack_sec; } + if (t < params.attack_sec) { + env = t / params.attack_sec; + } // Vibrato float vib = sinf(t * params.vibrato_rate * 2.0f * 3.14159f) * @@ -58,7 +61,9 @@ std::vector<float> generate_note_spectrogram(const NoteParams ¶ms, } // Apply window - for (int i = 0; i < DCT_SIZE; ++i) { pcm_chunk[i] *= window[i]; } + for (int i = 0; i < DCT_SIZE; ++i) { + pcm_chunk[i] *= window[i]; + } // Apply FDCT float dct_chunk[DCT_SIZE]; @@ -73,10 +78,11 @@ std::vector<float> generate_note_spectrogram(const NoteParams ¶ms, return spec_data; } -void paste_spectrogram(std::vector<float> &dest_data, int *dest_num_frames, - const std::vector<float> &src_data, int src_num_frames, +void paste_spectrogram(std::vector<float>& dest_data, int* dest_num_frames, + const std::vector<float>& src_data, int src_num_frames, int frame_offset) { - if (src_num_frames <= 0) return; + if (src_num_frames <= 0) + return; int needed_frames = frame_offset + src_num_frames; if (needed_frames > *dest_num_frames) { @@ -86,7 +92,8 @@ void paste_spectrogram(std::vector<float> &dest_data, int *dest_num_frames, for (int f = 0; f < src_num_frames; ++f) { int dst_frame_idx = frame_offset + f; - if (dst_frame_idx < 0) continue; + if (dst_frame_idx < 0) + continue; for (int i = 0; i < DCT_SIZE; ++i) { dest_data[dst_frame_idx * DCT_SIZE + i] += src_data[f * DCT_SIZE + i]; @@ -94,7 +101,7 @@ void paste_spectrogram(std::vector<float> &dest_data, int *dest_num_frames, } } -void apply_spectral_noise(std::vector<float> &data, int num_frames, +void apply_spectral_noise(std::vector<float>& data, int num_frames, float amount) { for (int f = 0; f < num_frames; ++f) { for (int i = 0; i < DCT_SIZE; ++i) { @@ -104,11 +111,13 @@ void apply_spectral_noise(std::vector<float> &data, int num_frames, } } -void apply_spectral_lowpass(std::vector<float> &data, int num_frames, +void apply_spectral_lowpass(std::vector<float>& data, int num_frames, float cutoff_ratio) { int cutoff_bin = (int)(cutoff_ratio * DCT_SIZE); - if (cutoff_bin < 0) cutoff_bin = 0; - if (cutoff_bin >= DCT_SIZE) return; + if (cutoff_bin < 0) + cutoff_bin = 0; + if (cutoff_bin >= DCT_SIZE) + return; for (int f = 0; f < num_frames; ++f) { for (int i = cutoff_bin; i < DCT_SIZE; ++i) { @@ -117,11 +126,13 @@ void apply_spectral_lowpass(std::vector<float> &data, int num_frames, } } -void apply_spectral_comb(std::vector<float> &data, int num_frames, +void apply_spectral_comb(std::vector<float>& data, int num_frames, float period_bins, float depth) { for (int i = 0; i < DCT_SIZE; ++i) { float mod = 1.0f - depth * (0.5f + 0.5f * cosf((float)i / period_bins * 6.28318f)); - for (int f = 0; f < num_frames; ++f) { data[f * DCT_SIZE + i] *= mod; } + for (int f = 0; f < num_frames; ++f) { + data[f * DCT_SIZE + i] *= mod; + } } } diff --git a/src/audio/gen.h b/src/audio/gen.h index f490115..94f4ee3 100644 --- a/src/audio/gen.h +++ b/src/audio/gen.h @@ -20,19 +20,19 @@ struct NoteParams { }; // Generates a single note into a new spectrogram buffer -std::vector<float> generate_note_spectrogram(const NoteParams ¶ms, - int *out_num_frames); +std::vector<float> generate_note_spectrogram(const NoteParams& params, + int* out_num_frames); // Pastes a source spectrogram into a destination spectrogram at a given frame // offset Expands destination if necessary -void paste_spectrogram(std::vector<float> &dest_data, int *dest_num_frames, - const std::vector<float> &src_data, int src_num_frames, +void paste_spectrogram(std::vector<float>& dest_data, int* dest_num_frames, + const std::vector<float>& src_data, int src_num_frames, int frame_offset); // Post-processing effects -void apply_spectral_noise(std::vector<float> &data, int num_frames, +void apply_spectral_noise(std::vector<float>& data, int num_frames, float amount); -void apply_spectral_lowpass(std::vector<float> &data, int num_frames, +void apply_spectral_lowpass(std::vector<float>& data, int num_frames, float cutoff_ratio); -void apply_spectral_comb(std::vector<float> &data, int num_frames, +void apply_spectral_comb(std::vector<float>& data, int num_frames, float period_bins, float depth); diff --git a/src/audio/idct.cc b/src/audio/idct.cc index 6235950..f8e8769 100644 --- a/src/audio/idct.cc +++ b/src/audio/idct.cc @@ -5,7 +5,7 @@ #include "dct.h" #include <math.h> -void idct_512(const float *input, float *output) { +void idct_512(const float* input, float* output) { const float PI = 3.14159265358979323846f; for (int n = 0; n < DCT_SIZE; ++n) { float sum = input[0] / 2.0f; diff --git a/src/audio/synth.cc b/src/audio/synth.cc index b4aaf53..db5a96c 100644 --- a/src/audio/synth.cc +++ b/src/audio/synth.cc @@ -23,12 +23,12 @@ struct Voice { float time_domain_buffer[DCT_SIZE]; int buffer_pos; - const volatile float *active_spectral_data; + const volatile float* active_spectral_data; }; static struct { Spectrogram spectrograms[MAX_SPECTROGRAMS]; - const volatile float *active_spectrogram_data[MAX_SPECTROGRAMS]; + const volatile float* active_spectrogram_data[MAX_SPECTROGRAMS]; bool spectrogram_registered[MAX_SPECTROGRAMS]; } g_synth_data; @@ -49,7 +49,7 @@ void synth_shutdown() { // Nothing to do here since we are not allocating memory } -int synth_register_spectrogram(const Spectrogram *spec) { +int synth_register_spectrogram(const Spectrogram* spec) { for (int i = 0; i < MAX_SPECTROGRAMS; ++i) { if (!g_synth_data.spectrogram_registered[i]) { g_synth_data.spectrograms[i] = *spec; @@ -67,19 +67,19 @@ void synth_unregister_spectrogram(int spectrogram_id) { } } -float *synth_begin_update(int spectrogram_id) { +float* synth_begin_update(int spectrogram_id) { if (spectrogram_id < 0 || spectrogram_id >= MAX_SPECTROGRAMS || !g_synth_data.spectrogram_registered[spectrogram_id]) { return nullptr; } - const volatile float *active_ptr = + const volatile float* active_ptr = g_synth_data.active_spectrogram_data[spectrogram_id]; if (active_ptr == g_synth_data.spectrograms[spectrogram_id].spectral_data_a) { - return (float *)(g_synth_data.spectrograms[spectrogram_id].spectral_data_b); + return (float*)(g_synth_data.spectrograms[spectrogram_id].spectral_data_b); } else { - return (float *)(g_synth_data.spectrograms[spectrogram_id].spectral_data_a); + return (float*)(g_synth_data.spectrograms[spectrogram_id].spectral_data_a); } } @@ -89,9 +89,9 @@ void synth_commit_update(int spectrogram_id) { return; } - const volatile float *old_active_ptr = + const volatile float* old_active_ptr = g_synth_data.active_spectrogram_data[spectrogram_id]; - const float *new_active_ptr = + const float* new_active_ptr = (old_active_ptr == g_synth_data.spectrograms[spectrogram_id].spectral_data_a) ? g_synth_data.spectrograms[spectrogram_id].spectral_data_b @@ -99,7 +99,7 @@ void synth_commit_update(int spectrogram_id) { // Atomic swap using GCC/Clang builtins for thread safety __atomic_store_n( - (const float **)&g_synth_data.active_spectrogram_data[spectrogram_id], + (const float**)&g_synth_data.active_spectrogram_data[spectrogram_id], new_active_ptr, __ATOMIC_RELEASE); } @@ -111,7 +111,7 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) { for (int i = 0; i < MAX_VOICES; ++i) { if (!g_voices[i].active) { - Voice &v = g_voices[i]; + Voice& v = g_voices[i]; v.active = true; v.spectrogram_id = spectrogram_id; v.volume = volume; @@ -132,7 +132,7 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) { } } -void synth_render(float *output_buffer, int num_frames) { +void synth_render(float* output_buffer, int num_frames) { // Use the pre-calculated window // float window[WINDOW_SIZE]; // hamming_window_512(window); @@ -145,8 +145,9 @@ void synth_render(float *output_buffer, int num_frames) { float right_sample = 0.0f; for (int v_idx = 0; v_idx < MAX_VOICES; ++v_idx) { - Voice &v = g_voices[v_idx]; - if (!v.active) continue; + Voice& v = g_voices[v_idx]; + if (!v.active) + continue; if (v.buffer_pos >= DCT_SIZE) { if (v.current_spectral_frame >= v.total_spectral_frames) { @@ -158,7 +159,7 @@ void synth_render(float *output_buffer, int num_frames) { v.active_spectral_data = g_synth_data.active_spectrogram_data[v.spectrogram_id]; - const float *spectral_frame = (const float *)v.active_spectral_data + + const float* spectral_frame = (const float*)v.active_spectral_data + (v.current_spectral_frame * DCT_SIZE); float windowed_frame[DCT_SIZE]; @@ -170,7 +171,7 @@ void synth_render(float *output_buffer, int num_frames) { idct_512(windowed_frame, v.time_domain_buffer); v.buffer_pos = 0; - v.current_spectral_frame++; + ++v.current_spectral_frame; } float voice_sample = v.time_domain_buffer[v.buffer_pos] * v.volume; @@ -192,9 +193,13 @@ void synth_render(float *output_buffer, int num_frames) { int synth_get_active_voice_count() { int count = 0; for (int i = 0; i < MAX_VOICES; ++i) { - if (g_voices[i].active) { count++; } + if (g_voices[i].active) { + ++count; + } } return count; } -float synth_get_output_peak() { return g_current_output_peak; } +float synth_get_output_peak() { + return g_current_output_peak; +} diff --git a/src/audio/synth.h b/src/audio/synth.h index 45a9410..a0720f2 100644 --- a/src/audio/synth.h +++ b/src/audio/synth.h @@ -11,8 +11,8 @@ #define MAX_SPECTROGRAMS 8 struct Spectrogram { - const float *spectral_data_a; // Front buffer - const float *spectral_data_b; // Back buffer (for double-buffering) + const float* spectral_data_a; // Front buffer + const float* spectral_data_b; // Back buffer (for double-buffering) int num_frames; }; @@ -20,15 +20,15 @@ void synth_init(); void synth_shutdown(); // Register a spectrogram for playback. Returns an ID or -1. -int synth_register_spectrogram(const Spectrogram *spec); +int synth_register_spectrogram(const Spectrogram* spec); void synth_unregister_spectrogram(int spectrogram_id); // Double-buffering API for thread-safe updates -float *synth_begin_update(int spectrogram_id); +float* synth_begin_update(int spectrogram_id); void synth_commit_update(int spectrogram_id); void synth_trigger_voice(int spectrogram_id, float volume, float pan); -void synth_render(float *output_buffer, int num_frames); +void synth_render(float* output_buffer, int num_frames); int synth_get_active_voice_count(); float synth_get_output_peak(); diff --git a/src/audio/window.cc b/src/audio/window.cc index e5dfdec..b68c747 100644 --- a/src/audio/window.cc +++ b/src/audio/window.cc @@ -5,7 +5,7 @@ #include "window.h" #include <math.h> -void hamming_window_512(float *window) { +void hamming_window_512(float* window) { const float PI = 3.14159265358979323846f; for (int i = 0; i < WINDOW_SIZE; ++i) { window[i] = diff --git a/src/audio/window.h b/src/audio/window.h index 99ac209..c3b583a 100644 --- a/src/audio/window.h +++ b/src/audio/window.h @@ -6,4 +6,4 @@ #define WINDOW_SIZE 512 -void hamming_window_512(float *window); +void hamming_window_512(float* window); diff --git a/src/generated/timeline.cc b/src/generated/timeline.cc index 2e060fb..486d0a6 100644 --- a/src/generated/timeline.cc +++ b/src/generated/timeline.cc @@ -2,7 +2,7 @@ #include "gpu/demo_effects.h" #include "gpu/effect.h" -void LoadTimeline(MainSequence &main_seq, WGPUDevice device, WGPUQueue queue, +void LoadTimeline(MainSequence& main_seq, WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) { { auto seq = std::make_shared<Sequence>(); diff --git a/src/gpu/demo_effects.cc b/src/gpu/demo_effects.cc index 77fb152..dbf5be8 100644 --- a/src/gpu/demo_effects.cc +++ b/src/gpu/demo_effects.cc @@ -11,7 +11,7 @@ // Helper to create a standard post-processing pipeline static WGPURenderPipeline create_post_process_pipeline(WGPUDevice device, WGPUTextureFormat format, - const char *shader_code) { + const char* shader_code) { WGPUShaderModuleDescriptor shader_desc = {}; WGPUShaderSourceWGSL wgsl_src = {}; wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; @@ -66,7 +66,7 @@ create_post_process_pipeline(WGPUDevice device, WGPUTextureFormat format, // --- SHADERS --- -const char *main_shader_wgsl = R"( +const char* main_shader_wgsl = R"( struct Uniforms { audio_peak: f32, aspect_ratio: f32, time: f32, }; @group(0) @binding(0) var<uniform> uniforms: Uniforms; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { @@ -84,7 +84,7 @@ struct Uniforms { audio_peak: f32, aspect_ratio: f32, time: f32, }; return vec4<f32>(r+boost,g+boost,b+boost, 1.0); })"; -const char *particle_compute_wgsl = R"( +const char* particle_compute_wgsl = R"( struct Particle { pos: vec4<f32>, vel: vec4<f32>, rot: vec4<f32>, color: vec4<f32>, }; struct Uniforms { audio_peak: f32, aspect_ratio: f32, time: f32, beat: f32, }; @group(0) @binding(0) var<storage, read_write> particles: array<Particle>; @@ -105,7 +105,7 @@ fn main(@builtin(global_invocation_id) id: vec3<u32>) { particles[i] = p; })"; -const char *particle_render_wgsl = R"( +const char* particle_render_wgsl = R"( struct Particle { pos: vec4<f32>, vel: vec4<f32>, rot: vec4<f32>, color: vec4<f32>, }; struct Uniforms { audio_peak: f32, aspect_ratio: f32, time: f32, beat: f32, }; @group(0) @binding(0) var<storage, read> particles: array<Particle>; @@ -124,7 +124,7 @@ struct VSOut { @builtin(position) pos: vec4<f32>, @location(0) color: vec4<f32>, @fragment fn fs_main(@location(0) color: vec4<f32>) -> @location(0) vec4<f32> { return color; } )"; -const char *passthrough_shader_wgsl = R"( +const char* passthrough_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d<f32>; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { @@ -135,7 +135,7 @@ const char *passthrough_shader_wgsl = R"( return textureSample(txt, smplr, p.xy / vec2<f32>(1280.0, 720.0)); })"; -const char *ellipse_shader_wgsl = R"( +const char* ellipse_shader_wgsl = R"( struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(0) var<uniform> uniforms: Uniforms; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { @@ -158,7 +158,7 @@ fn sdEllipse(p: vec2<f32>, ab: vec2<f32>) -> f32 { return mix(vec4<f32>(0.2, 0.8, 0.4, 1.0), vec4<f32>(0.0), smoothstep(0.0, 0.01, d)); })"; -const char *particle_spray_compute_wgsl = R"( +const char* particle_spray_compute_wgsl = R"( struct Particle { pos: vec4<f32>, vel: vec4<f32>, rot: vec4<f32>, color: vec4<f32>, }; struct Uniforms { intensity: f32, aspect_ratio: f32, time: f32, beat: f32, }; @group(0) @binding(0) var<storage, read_write> particles: array<Particle>; @@ -180,7 +180,7 @@ fn main(@builtin(global_invocation_id) id: vec3<u32>) { particles[i] = p; })"; -const char *gaussian_blur_shader_wgsl = R"( +const char* gaussian_blur_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d<f32>; struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(2) var<uniform> uniforms: Uniforms; @@ -199,7 +199,7 @@ struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; return res / 25.0; })"; -const char *solarize_shader_wgsl = R"( +const char* solarize_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d<f32>; struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(2) var<uniform> uniforms: Uniforms; @@ -216,7 +216,7 @@ struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; return col; })"; -const char *distort_shader_wgsl = R"( +const char* distort_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d<f32>; struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(2) var<uniform> uniforms: Uniforms; @@ -230,7 +230,7 @@ struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; return textureSample(txt, smplr, uv + vec2<f32>(dist, 0.0)); })"; -const char *chroma_aberration_shader_wgsl = R"( +const char* chroma_aberration_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d<f32>; struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(2) var<uniform> uniforms: Uniforms; @@ -317,10 +317,11 @@ void ParticlesEffect::render(WGPURenderPassEncoder pass, float t, float b, // --- PostProcess Implementation Helper --- static void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline, - WGPUBindGroup *bind_group, + WGPUBindGroup* bind_group, WGPUTextureView input_view, GpuBuffer uniforms) { - if (*bind_group) wgpuBindGroupRelease(*bind_group); + if (*bind_group) + wgpuBindGroupRelease(*bind_group); WGPUBindGroupLayout bgl = wgpuRenderPipelineGetBindGroupLayout(pipeline, 0); WGPUSamplerDescriptor sd = {}; sd.magFilter = WGPUFilterMode_Linear; @@ -385,7 +386,8 @@ ParticleSprayEffect::ParticleSprayEffect(WGPUDevice device, WGPUQueue queue, gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); std::vector<Particle> init_p(NUM_PARTICLES); - for (auto &p : init_p) p.pos[3] = 0.0f; + for (Particle& p : init_p) + p.pos[3] = 0.0f; particles_buffer_ = gpu_create_buffer( device, sizeof(Particle) * NUM_PARTICLES, WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex, init_p.data()); diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h index 5db710e..160ce65 100644 --- a/src/gpu/demo_effects.h +++ b/src/gpu/demo_effects.h @@ -16,26 +16,26 @@ struct Particle { }; class HeptagonEffect : public Effect { -public: + public: HeptagonEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; -private: + private: WGPUQueue queue_; RenderPass pass_; GpuBuffer uniforms_; }; class ParticlesEffect : public Effect { -public: + public: ParticlesEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void compute(WGPUCommandEncoder encoder, float time, float beat, float intensity, float aspect_ratio) override; void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; -private: + private: WGPUQueue queue_; ComputePass compute_pass_; RenderPass render_pass_; @@ -44,30 +44,30 @@ private: }; class PassthroughEffect : public PostProcessEffect { -public: + public: PassthroughEffect(WGPUDevice device, WGPUTextureFormat format); void update_bind_group(WGPUTextureView input_view) override; -private: + private: WGPUDevice device_; GpuBuffer uniforms_; }; class MovingEllipseEffect : public Effect { -public: + public: MovingEllipseEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; -private: + private: WGPUQueue queue_; RenderPass pass_; GpuBuffer uniforms_; }; class ParticleSprayEffect : public Effect { -public: + public: ParticleSprayEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void compute(WGPUCommandEncoder encoder, float time, float beat, @@ -75,7 +75,7 @@ public: void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; -private: + private: WGPUQueue queue_; ComputePass compute_pass_; RenderPass render_pass_; @@ -84,59 +84,59 @@ private: }; class GaussianBlurEffect : public PostProcessEffect { -public: + public: GaussianBlurEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; -private: + private: WGPUDevice device_; WGPUQueue queue_; GpuBuffer uniforms_; }; class SolarizeEffect : public PostProcessEffect { -public: + public: SolarizeEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; -private: + private: WGPUDevice device_; WGPUQueue queue_; GpuBuffer uniforms_; }; class DistortEffect : public PostProcessEffect { -public: + public: DistortEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; -private: + private: WGPUDevice device_; WGPUQueue queue_; GpuBuffer uniforms_; }; class ChromaAberrationEffect : public PostProcessEffect { -public: + public: ChromaAberrationEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; -private: + private: WGPUDevice device_; WGPUQueue queue_; GpuBuffer uniforms_; }; // Auto-generated function to populate the timeline -void LoadTimeline(MainSequence &main_seq, WGPUDevice device, WGPUQueue queue, - WGPUTextureFormat format);
\ No newline at end of file +void LoadTimeline(MainSequence& main_seq, WGPUDevice device, WGPUQueue queue, + WGPUTextureFormat format); diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc index 86390a9..64bd053 100644 --- a/src/gpu/effect.cc +++ b/src/gpu/effect.cc @@ -19,8 +19,8 @@ void PostProcessEffect::render(WGPURenderPassEncoder pass, float, float, float, } // --- Sequence Implementation --- -void Sequence::init(MainSequence *demo) { - for (auto &item : items_) { +void Sequence::init(MainSequence* demo) { + for (SequenceItem& item : items_) { if (!item.effect->is_initialized) { item.effect->init(demo); item.effect->is_initialized = true; @@ -35,16 +35,17 @@ void Sequence::add_effect(std::shared_ptr<Effect> effect, float start_time, } void Sequence::sort_items() { - if (is_sorted_) return; + if (is_sorted_) + return; std::sort(items_.begin(), items_.end(), - [](const SequenceItem &a, const SequenceItem &b) { + [](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_) { + for (SequenceItem& item : items_) { bool should_be_active = (seq_time >= item.start_time && seq_time < item.end_time); @@ -59,10 +60,10 @@ void Sequence::update_active_list(float seq_time) { } void Sequence::collect_active_effects( - std::vector<SequenceItem *> &scene_effects, - std::vector<SequenceItem *> &post_effects) { + std::vector<SequenceItem*>& scene_effects, + std::vector<SequenceItem*>& post_effects) { sort_items(); - for (auto &item : items_) { + for (SequenceItem& item : items_) { if (item.active) { if (item.effect->is_post_process()) { post_effects.push_back(&item); @@ -74,7 +75,7 @@ void Sequence::collect_active_effects( } void Sequence::reset() { - for (auto &item : items_) { + for (SequenceItem& item : items_) { if (item.active) { item.effect->end(); item.active = false; @@ -129,14 +130,16 @@ void MainSequence::init(WGPUDevice d, WGPUQueue q, WGPUTextureFormat f, create_framebuffers(width, height); passthrough_effect_ = std::make_unique<PassthroughEffect>(device, format); - for (auto &entry : sequences_) { entry.seq->init(this); } + for (ActiveSequence& 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) { + [](const ActiveSequence& a, const ActiveSequence& b) { return a.priority < b.priority; }); } @@ -145,9 +148,9 @@ 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_) { + std::vector<SequenceItem*> scene_effects; + std::vector<SequenceItem*> post_effects; + for (ActiveSequence& entry : sequences_) { if (global_time >= entry.start_time) { float seq_time = global_time - entry.start_time; entry.seq->update_active_list(seq_time); @@ -155,16 +158,16 @@ void MainSequence::render_frame(float global_time, float beat, float peak, } } std::sort(scene_effects.begin(), scene_effects.end(), - [](const SequenceItem *a, const SequenceItem *b) { + [](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) { + [](const SequenceItem* a, const SequenceItem* b) { return a->priority < b->priority; }); // 1. Compute - for (const auto &item : scene_effects) { + for (const SequenceItem* item : scene_effects) { item->effect->compute(encoder, global_time - item->start_time, beat, peak, aspect_ratio); } @@ -178,12 +181,12 @@ void MainSequence::render_frame(float global_time, float beat, float peak, scene_attachment.clearValue = {0, 0, 0, 1}; #if !defined(DEMO_CROSS_COMPILE_WIN32) scene_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; -#endif +#endif /* !defined(DEMO_CROSS_COMPILE_WIN32) */ WGPURenderPassDescriptor scene_desc = {.colorAttachmentCount = 1, .colorAttachments = &scene_attachment}; WGPURenderPassEncoder scene_pass = wgpuCommandEncoderBeginRenderPass(encoder, &scene_desc); - for (const auto &item : scene_effects) { + for (const SequenceItem* item : scene_effects) { item->effect->render(scene_pass, global_time - item->start_time, beat, peak, aspect_ratio); } @@ -205,7 +208,7 @@ void MainSequence::render_frame(float global_time, float beat, float peak, final_attachment.storeOp = WGPUStoreOp_Store; #if !defined(DEMO_CROSS_COMPILE_WIN32) final_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; -#endif +#endif /* !defined(DEMO_CROSS_COMPILE_WIN32) */ WGPURenderPassDescriptor final_desc = { .colorAttachmentCount = 1, .colorAttachments = &final_attachment}; WGPURenderPassEncoder final_pass = @@ -228,8 +231,8 @@ void MainSequence::render_frame(float global_time, float beat, float peak, : framebuffer_view_a_); } - PostProcessEffect *pp = - static_cast<PostProcessEffect *>(post_effects[i]->effect.get()); + PostProcessEffect* pp = + (PostProcessEffect*)(post_effects[i]->effect.get()); pp->update_bind_group(current_input); WGPURenderPassColorAttachment pp_attachment = {}; @@ -239,7 +242,7 @@ void MainSequence::render_frame(float global_time, float beat, float peak, pp_attachment.storeOp = WGPUStoreOp_Store; #if !defined(DEMO_CROSS_COMPILE_WIN32) pp_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; -#endif +#endif /* !defined(DEMO_CROSS_COMPILE_WIN32) */ WGPURenderPassDescriptor pp_desc = {.colorAttachmentCount = 1, .colorAttachments = &pp_attachment}; WGPURenderPassEncoder pp_pass = @@ -262,14 +265,20 @@ void MainSequence::render_frame(float global_time, float beat, float peak, } 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(); } + 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 (ActiveSequence& entry : sequences_) { + entry.seq->reset(); + } } -#ifndef STRIP_ALL +#if !defined(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; @@ -277,14 +286,14 @@ void MainSequence::simulate_until(float target_time, float 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_) { + std::vector<SequenceItem*> scene_effects, post_effects; + for (ActiveSequence& 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) { + for (const SequenceItem* item : scene_effects) { item->effect->compute(encoder, t - item->start_time, beat, 0.0f, aspect_ratio); } @@ -292,4 +301,4 @@ void MainSequence::simulate_until(float target_time, float step_rate) { wgpuQueueSubmit(queue, 1, &commands); } } -#endif +#endif /* !defined(STRIP_ALL) */ diff --git a/src/gpu/effect.h b/src/gpu/effect.h index 0b791a3..ee90fa4 100644 --- a/src/gpu/effect.h +++ b/src/gpu/effect.h @@ -7,16 +7,19 @@ #include <webgpu/webgpu.h> #else #include <webgpu.h> -#endif +#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ class MainSequence; class PostProcessEffect; class Effect { -public: + public: virtual ~Effect() = default; - virtual void init(MainSequence *demo) { (void)demo; } - virtual void start() {} + virtual void init(MainSequence* demo) { + (void)demo; + } + virtual void start() { + } virtual void compute(WGPUCommandEncoder encoder, float time, float beat, float intensity, float aspect_ratio) { (void)encoder; @@ -27,20 +30,26 @@ public: } virtual void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) = 0; - virtual void end() {} + virtual void end() { + } bool is_initialized = false; - virtual bool is_post_process() const { return false; } + virtual bool is_post_process() const { + return false; + } }; class PostProcessEffect : public Effect { -public: - bool is_post_process() const override { return true; } - void compute(WGPUCommandEncoder, float, float, float, float) override {} + public: + bool is_post_process() const override { + return true; + } + void compute(WGPUCommandEncoder, float, float, float, float) override { + } void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; virtual void update_bind_group(WGPUTextureView input_view) = 0; -protected: + protected: WGPURenderPipeline pipeline_ = nullptr; WGPUBindGroup bind_group_ = nullptr; }; @@ -54,24 +63,24 @@ struct SequenceItem { }; class Sequence { -public: + public: int priority = 0; // Render order of this sequence (higher = later/top) - void init(MainSequence *demo); + void init(MainSequence* demo); void add_effect(std::shared_ptr<Effect> effect, float start_time, float end_time, int priority = 0); void update_active_list(float seq_time); - void collect_active_effects(std::vector<SequenceItem *> &scene_effects, - std::vector<SequenceItem *> &post_effects); + void collect_active_effects(std::vector<SequenceItem*>& scene_effects, + std::vector<SequenceItem*>& post_effects); void reset(); -private: + private: std::vector<SequenceItem> items_; bool is_sorted_ = false; void sort_items(); }; class MainSequence { -public: + public: MainSequence(); ~MainSequence(); WGPUDevice device; @@ -86,11 +95,11 @@ public: float aspect_ratio, WGPUSurface surface); void shutdown(); -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) void simulate_until(float target_time, float step_rate); -#endif +#endif /* !defined(STRIP_ALL) */ -private: + private: struct ActiveSequence { std::shared_ptr<Sequence> seq; float start_time; @@ -103,10 +112,10 @@ private: WGPUTexture framebuffer_b_ = nullptr; WGPUTextureView framebuffer_view_b_ = nullptr; -public: // Made public for testing + public: // Made public for testing void init_test(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); -private: // Restore private access for other members + private: // Restore private access for other members std::unique_ptr<PostProcessEffect> passthrough_effect_; void create_framebuffers(int width, int height); diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc index 8618280..a097efa 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -16,9 +16,9 @@ #include <cstring> #include <vector> -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) #include <iostream> -#endif +#endif /* !defined(STRIP_ALL) */ // --- WebGPU Headers & Compatibility --- #if defined(DEMO_CROSS_COMPILE_WIN32) @@ -28,7 +28,7 @@ #define WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal \ WGPUSurfaceGetCurrentTextureStatus_Success #define WGPUCallbackMode_WaitAnyOnly 0 -static void wgpuInstanceWaitAny(WGPUInstance instance, size_t, void *, +static void wgpuInstanceWaitAny(WGPUInstance instance, size_t, void*, uint64_t) { wgpuInstanceProcessEvents(instance); } @@ -40,7 +40,7 @@ static void set_error_callback(WGPUDevice device, WGPUUncapturedErrorCallback callback) { // Handled in descriptor for new API. } -#endif +#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ static WGPUInstance g_instance = nullptr; static WGPUAdapter g_adapter = nullptr; @@ -54,7 +54,7 @@ static MainSequence g_main_sequence; // --- Helper Functions --- GpuBuffer gpu_create_buffer(WGPUDevice device, size_t size, uint32_t usage, - const void *data) { + const void* data) { WGPUBufferDescriptor desc = {}; desc.label = label_view("GpuBuffer"); desc.usage = (WGPUBufferUsage)usage; // Cast for C++ strictness with enums @@ -64,7 +64,7 @@ GpuBuffer gpu_create_buffer(WGPUDevice device, size_t size, uint32_t usage, WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &desc); if (data) { - void *ptr = wgpuBufferGetMappedRange(buffer, 0, size); + void* ptr = wgpuBufferGetMappedRange(buffer, 0, size); memcpy(ptr, data, size); wgpuBufferUnmap(buffer); } @@ -73,8 +73,8 @@ GpuBuffer gpu_create_buffer(WGPUDevice device, size_t size, uint32_t usage, } RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, - const char *shader_code, - ResourceBinding *bindings, int num_bindings) { + const char* shader_code, + ResourceBinding* bindings, int num_bindings) { RenderPass pass = {}; // Create Shader Module @@ -160,8 +160,8 @@ RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, return pass; } -ComputePass gpu_create_compute_pass(WGPUDevice device, const char *shader_code, - ResourceBinding *bindings, +ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code, + ResourceBinding* bindings, int num_bindings) { ComputePass pass = {}; @@ -220,98 +220,98 @@ ComputePass gpu_create_compute_pass(WGPUDevice device, const char *shader_code, // --- Main Init/Draw --- -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) #if defined(DEMO_CROSS_COMPILE_WIN32) static void handle_request_adapter(WGPURequestAdapterStatus status, - WGPUAdapter adapter, const char *message, - void *userdata) { + WGPUAdapter adapter, const char* message, + void* userdata) { if (status == WGPURequestAdapterStatus_Success) { - *((WGPUAdapter *)userdata) = adapter; + *((WGPUAdapter*)userdata) = adapter; } else { printf("Request adapter failed: %s\n", message ? message : "Unknown"); } } static void handle_request_device(WGPURequestDeviceStatus status, - WGPUDevice device, const char *message, - void *userdata) { + WGPUDevice device, const char* message, + void* userdata) { if (status == WGPURequestDeviceStatus_Success) { - *((WGPUDevice *)userdata) = device; + *((WGPUDevice*)userdata) = device; } else { printf("Request device failed: %s\n", message ? message : "Unknown"); } } -static void handle_device_error(WGPUErrorType type, const char *message, - void *userdata) { +static void handle_device_error(WGPUErrorType type, const char* message, + void* userdata) { printf("WebGPU Error: %s\n", message ? message : "Unknown"); } #else static void handle_request_adapter(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message, - void *userdata, void *userdata2) { + void* userdata, void* userdata2) { (void)userdata2; if (status == WGPURequestAdapterStatus_Success) { - *((WGPUAdapter *)userdata) = adapter; + *((WGPUAdapter*)userdata) = adapter; } else { printf("Request adapter failed: %.*s\n", (int)message.length, message.data); } } static void handle_request_device(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, - void *userdata, void *userdata2) { + void* userdata, void* userdata2) { (void)userdata2; if (status == WGPURequestDeviceStatus_Success) { - *((WGPUDevice *)userdata) = device; + *((WGPUDevice*)userdata) = device; } else { printf("Request device failed: %.*s\n", (int)message.length, message.data); } } -static void handle_device_error(const WGPUDevice *device, WGPUErrorType type, - WGPUStringView message, void *userdata, - void *userdata2) { +static void handle_device_error(const WGPUDevice* device, WGPUErrorType type, + WGPUStringView message, void* userdata, + void* userdata2) { (void)device; (void)userdata; (void)userdata2; printf("WebGPU Error: %.*s\n", (int)message.length, message.data); } -#endif +#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ #else // STRIP_ALL versions #if defined(DEMO_CROSS_COMPILE_WIN32) static void handle_request_adapter(WGPURequestAdapterStatus status, - WGPUAdapter adapter, const char *message, - void *userdata) { + WGPUAdapter adapter, const char* message, + void* userdata) { if (status == WGPURequestAdapterStatus_Success) { - *((WGPUAdapter *)userdata) = adapter; + *((WGPUAdapter*)userdata) = adapter; } } static void handle_request_device(WGPURequestDeviceStatus status, - WGPUDevice device, const char *message, - void *userdata) { + WGPUDevice device, const char* message, + void* userdata) { if (status == WGPURequestDeviceStatus_Success) { - *((WGPUDevice *)userdata) = device; + *((WGPUDevice*)userdata) = device; } } #else static void handle_request_adapter(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message, - void *userdata, void *userdata2) { + void* userdata, void* userdata2) { (void)userdata2; if (status == WGPURequestAdapterStatus_Success) { - *((WGPUAdapter *)userdata) = adapter; + *((WGPUAdapter*)userdata) = adapter; } } static void handle_request_device(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, - void *userdata, void *userdata2) { + void* userdata, void* userdata2) { (void)userdata2; if (status == WGPURequestDeviceStatus_Success) { - *((WGPUDevice *)userdata) = device; + *((WGPUDevice*)userdata) = device; } } -#endif -#endif +#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ +#endif /* !defined(STRIP_ALL) */ -void gpu_init(GLFWwindow *window, int width, int height) { +void gpu_init(GLFWwindow* window, int width, int height) { g_instance = wgpuCreateInstance(nullptr); g_surface = platform_create_wgpu_surface(g_instance); @@ -328,15 +328,16 @@ void gpu_init(GLFWwindow *window, int width, int height) { adapter_cb.callback = handle_request_adapter; adapter_cb.userdata1 = &g_adapter; wgpuInstanceRequestAdapter(g_instance, &adapter_opts, adapter_cb); -#endif - while (!g_adapter) wgpuInstanceWaitAny(g_instance, 0, nullptr, 0); +#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ + while (!g_adapter) + wgpuInstanceWaitAny(g_instance, 0, nullptr, 0); WGPUDeviceDescriptor device_desc = {}; -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) #if !defined(DEMO_CROSS_COMPILE_WIN32) device_desc.uncapturedErrorCallbackInfo.callback = handle_device_error; -#endif -#endif +#endif /* !defined(DEMO_CROSS_COMPILE_WIN32) */ +#endif /* !defined(STRIP_ALL) */ #if defined(DEMO_CROSS_COMPILE_WIN32) wgpuAdapterRequestDevice(g_adapter, &device_desc, handle_request_device, @@ -347,12 +348,13 @@ void gpu_init(GLFWwindow *window, int width, int height) { device_cb.callback = handle_request_device; device_cb.userdata1 = &g_device; wgpuAdapterRequestDevice(g_adapter, &device_desc, device_cb); -#endif - while (!g_device) wgpuInstanceWaitAny(g_instance, 0, nullptr, 0); +#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ + while (!g_device) + wgpuInstanceWaitAny(g_instance, 0, nullptr, 0); #if defined(DEMO_CROSS_COMPILE_WIN32) && !defined(STRIP_ALL) set_error_callback(g_device, handle_device_error); -#endif +#endif /* defined(DEMO_CROSS_COMPILE_WIN32) && !defined(STRIP_ALL) */ g_queue = wgpuDeviceGetQueue(g_device); @@ -378,10 +380,12 @@ void gpu_draw(float audio_peak, float aspect_ratio, float time, float beat) { g_main_sequence.render_frame(time, beat, audio_peak, aspect_ratio, g_surface); } -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) void gpu_simulate_until(float time) { g_main_sequence.simulate_until(time, 1.0f / 60.0f); } -#endif +#endif /* !defined(STRIP_ALL) */ -void gpu_shutdown() { g_main_sequence.shutdown(); }
\ No newline at end of file +void gpu_shutdown() { + g_main_sequence.shutdown(); +} diff --git a/src/gpu/gpu.h b/src/gpu/gpu.h index 062b822..8814cbc 100644 --- a/src/gpu/gpu.h +++ b/src/gpu/gpu.h @@ -16,9 +16,13 @@ #include <webgpu/wgpu.h> -static inline const char *str_view(const char *str) { return str; } +static inline const char* str_view(const char* str) { + return str; +} -static inline const char *label_view(const char *str) { return str; } +static inline const char* label_view(const char* str) { + return str; +} #define WGPUSType_ShaderSourceWGSL WGPUSType_ShaderModuleWGSLDescriptor @@ -40,17 +44,19 @@ using WGPUShaderSourceWGSL = WGPUShaderModuleWGSLDescriptor; #include <wgpu.h> -static inline WGPUStringView str_view(const char *str) { - if (!str) return {nullptr, 0}; +static inline WGPUStringView str_view(const char* str) { + if (!str) + return {nullptr, 0}; return {str, strlen(str)}; } -static inline WGPUStringView label_view(const char *str) { +static inline WGPUStringView label_view(const char* str) { -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) - if (!str) return {nullptr, 0}; + if (!str) + return {nullptr, 0}; return {str, strlen(str)}; @@ -60,10 +66,10 @@ static inline WGPUStringView label_view(const char *str) { return {nullptr, 0}; -#endif +#endif /* !defined(STRIP_ALL) */ } -#endif +#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ struct GLFWwindow; @@ -90,11 +96,11 @@ struct RenderPass { uint32_t instance_count; }; -void gpu_init(GLFWwindow *window, int width, int height); +void gpu_init(GLFWwindow* window, int width, int height); void gpu_draw(float audio_peak, float aspect_ratio, float time, float beat); -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) void gpu_simulate_until(float time); -#endif +#endif /* !defined(STRIP_ALL) */ void gpu_shutdown(); // Helper functions (exposed for internal/future use) @@ -105,12 +111,12 @@ struct ResourceBinding { }; GpuBuffer gpu_create_buffer(WGPUDevice device, size_t size, uint32_t usage, - const void *data = nullptr); -ComputePass gpu_create_compute_pass(WGPUDevice device, const char *shader_code, - ResourceBinding *bindings, + const void* data = nullptr); +ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code, + ResourceBinding* bindings, int num_bindings); RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, // Needed for render pipeline - const char *shader_code, ResourceBinding *bindings, + const char* shader_code, ResourceBinding* bindings, int num_bindings); diff --git a/src/main.cc b/src/main.cc index f63c803..8cedfce 100644 --- a/src/main.cc +++ b/src/main.cc @@ -28,11 +28,12 @@ struct SpecHeader { int register_spec_asset(AssetId id) { size_t size; - const uint8_t *data = GetAsset(id, &size); - if (!data || size < sizeof(SpecHeader)) return -1; + const uint8_t* data = GetAsset(id, &size); + if (!data || size < sizeof(SpecHeader)) + return -1; - const SpecHeader *header = (const SpecHeader *)data; - const float *spectral_data = (const float *)(data + sizeof(SpecHeader)); + const SpecHeader* header = (const SpecHeader*)data; + const float* spectral_data = (const float*)(data + sizeof(SpecHeader)); Spectrogram spec; spec.spectral_data_a = spectral_data; @@ -42,8 +43,8 @@ int register_spec_asset(AssetId id) { return synth_register_spectrogram(&spec); } -static float *g_spec_buffer_a[SPEC_FRAMES * DCT_SIZE] = {0}; -static float *g_spec_buffer_b[SPEC_FRAMES * DCT_SIZE] = {0}; +static float* g_spec_buffer_a[SPEC_FRAMES * DCT_SIZE] = {0}; +static float* g_spec_buffer_b[SPEC_FRAMES * DCT_SIZE] = {0}; // Global storage for the melody to ensure it persists std::vector<float> g_melody_data; @@ -61,11 +62,13 @@ int generate_melody() { srand(12345); // Fixed seed for reproducibility for (int i = 0; i < 128; ++i) { - if (i % 4 == 0) continue; // Rest on beat 1 of every bar + if (i % 4 == 0) + continue; // Rest on beat 1 of every bar NoteParams params = {}; params.base_freq = notes[rand() % num_notes]; - if (rand() % 4 == 0) params.base_freq *= 2.0f; // Occasional octave up + if (rand() % 4 == 0) + params.base_freq *= 2.0f; // Occasional octave up params.duration_sec = (rand() % 2 == 0) ? 0.2f : 0.4f; params.amplitude = 0.4f; @@ -107,33 +110,35 @@ int generate_melody() { return synth_register_spectrogram(&spec); } -float *generate_tone(float *buffer, float freq) { +float* generate_tone(float* buffer, float freq) { if (buffer == nullptr) { - buffer = (float *)calloc(SPEC_FRAMES * DCT_SIZE, sizeof(float)); + buffer = (float*)calloc(SPEC_FRAMES * DCT_SIZE, sizeof(float)); } else { memset(buffer, 0, SPEC_FRAMES * DCT_SIZE * sizeof(float)); } for (int frame = 0; frame < SPEC_FRAMES; ++frame) { - float *spec_frame = buffer + frame * DCT_SIZE; + float* spec_frame = buffer + frame * DCT_SIZE; float amplitude = 1000. * powf(1.0f - (float)frame / SPEC_FRAMES, 2.0f); int bin = (int)(freq / (32000.0f / 2.0f) * DCT_SIZE); - if (bin > 0 && bin < DCT_SIZE) { spec_frame[bin] = amplitude; } + if (bin > 0 && bin < DCT_SIZE) { + spec_frame[bin] = amplitude; + } } return buffer; } -int main(int argc, char **argv) { +int main(int argc, char** argv) { bool fullscreen_enabled = false; double seek_time = 0.0; -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--fullscreen") == 0) { fullscreen_enabled = true; } else if (strcmp(argv[i], "--seek") == 0 && i + 1 < argc) { seek_time = atof(argv[i + 1]); - i++; + ++i; } } #else @@ -154,8 +159,8 @@ int main(int argc, char **argv) { int hihat_id = register_spec_asset(AssetId::ASSET_HIHAT_1); // Still keep the dynamic tone for bass - const float *g_spec_buffer_a = generate_tone(nullptr, 110.0f); // A2 - const float *g_spec_buffer_b = generate_tone(nullptr, 110.0f); + const float* g_spec_buffer_a = generate_tone(nullptr, 110.0f); // A2 + const float* g_spec_buffer_b = generate_tone(nullptr, 110.0f); const Spectrogram bass_spec = {g_spec_buffer_a, g_spec_buffer_b, SPEC_FRAMES}; int bass_id = synth_register_spectrogram(&bass_spec); @@ -183,11 +188,13 @@ int main(int argc, char **argv) { } // Hihat on every offbeat - if (step % 2 == 1) { synth_trigger_voice(hihat_id, 0.5f, 0.3f); } + if (step % 2 == 1) { + synth_trigger_voice(hihat_id, 0.5f, 0.3f); + } // Bass pattern if (step % 4 == 0) { - float *back_buffer = synth_begin_update(bass_id); + float* back_buffer = synth_begin_update(bass_id); if (back_buffer) { float bass_freq = (step < 8) ? 110.0f : 164.82f; // A3 then E3 generate_tone(back_buffer, bass_freq); @@ -200,7 +207,7 @@ int main(int argc, char **argv) { } }; -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) if (seek_time > 0.0) { printf("Seeking to %.2f seconds...\n", seek_time); @@ -215,7 +222,7 @@ int main(int argc, char **argv) { // Simulate Visuals gpu_simulate_until((float)seek_time); } -#endif +#endif /* !defined(STRIP_ALL) */ // Start real audio audio_start(); @@ -244,4 +251,4 @@ int main(int argc, char **argv) { gpu_shutdown(); platform_shutdown(); return 0; -}
\ No newline at end of file +} diff --git a/src/platform.cc b/src/platform.cc index 04bbf09..ff45964 100644 --- a/src/platform.cc +++ b/src/platform.cc @@ -7,11 +7,11 @@ #include <GLFW/glfw3.h> -static GLFWwindow *window = nullptr; +static GLFWwindow* window = nullptr; static int windowed_x, windowed_y, windowed_w, windowed_h; static bool g_is_fullscreen = false; -static void glfw_key_callback(GLFWwindow *cb_window, int key, int scancode, +static void glfw_key_callback(GLFWwindow* cb_window, int key, int scancode, int action, int mods) { if (action == GLFW_PRESS) { if (key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) { @@ -34,8 +34,8 @@ void platform_init_window(bool fullscreen) { glfwGetWindowPos(window, &windowed_x, &windowed_y); glfwGetWindowSize(window, &windowed_w, &windowed_h); - GLFWmonitor *monitor = glfwGetPrimaryMonitor(); - const GLFWvidmode *mode = glfwGetVideoMode(monitor); + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); + const GLFWvidmode* mode = glfwGetVideoMode(monitor); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); } @@ -46,9 +46,13 @@ void platform_shutdown() { glfwTerminate(); } -void platform_poll() { glfwPollEvents(); } +void platform_poll() { + glfwPollEvents(); +} -bool platform_should_close() { return glfwWindowShouldClose(window); } +bool platform_should_close() { + return glfwWindowShouldClose(window); +} void platform_toggle_fullscreen() { g_is_fullscreen = !g_is_fullscreen; @@ -56,8 +60,8 @@ void platform_toggle_fullscreen() { glfwGetWindowPos(window, &windowed_x, &windowed_y); glfwGetWindowSize(window, &windowed_w, &windowed_h); - GLFWmonitor *monitor = glfwGetPrimaryMonitor(); - const GLFWvidmode *mode = glfwGetVideoMode(monitor); + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); + const GLFWvidmode* mode = glfwGetVideoMode(monitor); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); } else { @@ -66,9 +70,13 @@ void platform_toggle_fullscreen() { } } -GLFWwindow *platform_get_window() { return window; } +GLFWwindow* platform_get_window() { + return window; +} -double platform_get_time() { return glfwGetTime(); } +double platform_get_time() { + return glfwGetTime(); +} WGPUSurface platform_create_wgpu_surface(WGPUInstance instance) { return glfwCreateWindowWGPUSurface(instance, window); diff --git a/src/platform.h b/src/platform.h index 31505ab..9cfaf00 100644 --- a/src/platform.h +++ b/src/platform.h @@ -15,7 +15,7 @@ bool platform_should_close(); void platform_toggle_fullscreen(); -GLFWwindow *platform_get_window(); +GLFWwindow* platform_get_window(); double platform_get_time(); WGPUSurface platform_create_wgpu_surface(WGPUInstance instance); diff --git a/src/tests/test_assets.cc b/src/tests/test_assets.cc index ff1cae2..e8b6318 100644 --- a/src/tests/test_assets.cc +++ b/src/tests/test_assets.cc @@ -2,11 +2,11 @@ // It tests the asset manager's ability to retrieve packed data. // Verifies data integrity and size reporting. -#ifdef USE_TEST_ASSETS +#if defined(USE_TEST_ASSETS) #include "generated/test_assets.h" #else #include "generated/assets.h" -#endif +#endif /* defined(USE_TEST_ASSETS) */ #include <assert.h> #include <stdio.h> @@ -16,18 +16,18 @@ int main() { printf("Running AssetManager test...\n"); size_t size = 0; - const uint8_t *data = GetAsset(AssetId::ASSET_TEST_ASSET, &size); + const uint8_t* data = GetAsset(AssetId::ASSET_TEST_ASSET, &size); assert(data != nullptr); assert(size > 0); - const char *expected_prefix = "This is a test asset file."; - if (strncmp((const char *)data, expected_prefix, strlen(expected_prefix)) == + const char* expected_prefix = "This is a test asset file."; + if (strncmp((const char*)data, expected_prefix, strlen(expected_prefix)) == 0) { printf("Asset content verification: SUCCESS\n"); } else { printf("Asset content verification: FAILED\n"); - printf("Got: %.*s\n", (int)size, (const char *)data); + printf("Got: %.*s\n", (int)size, (const char*)data); return 1; } diff --git a/src/tests/test_maths.cc b/src/tests/test_maths.cc index e7a686c..d9bc4d1 100644 --- a/src/tests/test_maths.cc +++ b/src/tests/test_maths.cc @@ -9,7 +9,9 @@ #include <vector> // Checks if two floats are approximately equal -bool near(float a, float b, float e = 0.001f) { return std::abs(a - b) < e; } +bool near(float a, float b, float e = 0.001f) { + return std::abs(a - b) < e; +} // Generic test runner for any vector type (vec2, vec3, vec4) template <typename T> void test_vector_ops(int n) { @@ -22,16 +24,19 @@ template <typename T> void test_vector_ops(int n) { // Add T c = a + b; - for (int i = 0; i < n; ++i) assert(near(c[i], (float)(i + 1) + 10.0f)); + for (int i = 0; i < n; ++i) + assert(near(c[i], (float)(i + 1) + 10.0f)); // Scale T s = a * 2.0f; - for (int i = 0; i < n; ++i) assert(near(s[i], (float)(i + 1) * 2.0f)); + for (int i = 0; i < n; ++i) + assert(near(s[i], (float)(i + 1) * 2.0f)); // Dot Product // vec3(1,2,3) . vec3(1,2,3) = 1+4+9 = 14 float expected_dot = 0; - for (int i = 0; i < n; ++i) expected_dot += a[i] * a[i]; + for (int i = 0; i < n; ++i) + expected_dot += a[i] * a[i]; assert(near(T::dot(a, a), expected_dot)); // Norm (Length) @@ -43,7 +48,8 @@ template <typename T> void test_vector_ops(int n) { // Lerp T l = lerp(a, b, 0.3f); - for (int i = 0; i < n; ++i) assert(near(l[i], .7 * (i + 1) + .3 * 10.0f)); + for (int i = 0; i < n; ++i) + assert(near(l[i], .7 * (i + 1) + .3 * 10.0f)); } // Specific test for padding alignment in vec3 @@ -127,7 +133,8 @@ void test_spring() { std::cout << "Testing Spring..." << std::endl; float p = 0, v = 0; // Simulate approx 1 sec with 0.5s smooth time - for (int i = 0; i < 60; ++i) spring::solve(p, v, 10.0f, 0.5f, 0.016f); + for (int i = 0; i < 60; ++i) + spring::solve(p, v, 10.0f, 0.5f, 0.016f); assert(p > 8.5f); // Test vector spring diff --git a/src/tests/test_sequence.cc b/src/tests/test_sequence.cc index 363846b..6b04302 100644 --- a/src/tests/test_sequence.cc +++ b/src/tests/test_sequence.cc @@ -19,23 +19,26 @@ static WGPURenderPassEncoder dummy_render_pass_encoder = // --- Dummy Effect for Tracking --- class DummyEffect : public Effect { -public: + public: int init_calls = 0; int start_calls = 0; int render_calls = 0; int end_calls = 0; bool is_pp = false; - DummyEffect(bool post_process = false) : is_pp(post_process) {} + DummyEffect(bool post_process = false) : is_pp(post_process) { + } - void init(MainSequence *demo) override { - init_calls++; + void init(MainSequence* demo) override { + ++init_calls; (void)demo; } - void start() override { start_calls++; } + void start() override { + ++start_calls; + } void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override { - render_calls++; + ++render_calls; (void)pass; (void)time; (void)beat; @@ -50,13 +53,17 @@ public: (void)intensity; (void)aspect_ratio; } - void end() override { end_calls++; } - bool is_post_process() const override { return is_pp; } + void end() override { + ++end_calls; + } + bool is_post_process() const override { + return is_pp; + } }; // --- Dummy PostProcessEffect for Tracking (unused in simplified tests) --- class DummyPostProcessEffect : public PostProcessEffect { -public: + public: int init_calls = 0; int render_calls = 0; int update_bind_group_calls = 0; @@ -66,13 +73,13 @@ public: (void)format; } - void init(MainSequence *demo) override { - init_calls++; + void init(MainSequence* demo) override { + ++init_calls; (void)demo; } void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override { - render_calls++; + ++render_calls; (void)pass; (void)time; (void)beat; @@ -80,7 +87,7 @@ public: (void)aspect_ratio; } void update_bind_group(WGPUTextureView input_view) override { - update_bind_group_calls++; + ++update_bind_group_calls; (void)input_view; } }; @@ -133,7 +140,7 @@ void test_effect_lifecycle() { } void test_simulate_until() { -#ifndef STRIP_ALL +#if !defined(STRIP_ALL) printf(" test_simulate_until...\n"); MainSequence main_seq; main_seq.init_test(dummy_device, dummy_queue, dummy_format); @@ -158,7 +165,7 @@ void test_simulate_until() { assert(effect1->end_calls == 1); // Should end #else printf(" test_simulate_until (skipped in STRIP_ALL build)...\n"); -#endif +#endif /* !defined(STRIP_ALL) */ } int main() { diff --git a/src/tests/test_spectool.cc b/src/tests/test_spectool.cc index b9270ed..37a74b7 100644 --- a/src/tests/test_spectool.cc +++ b/src/tests/test_spectool.cc @@ -20,7 +20,7 @@ struct SpecHeader { int32_t num_frames; }; -void generate_test_wav(const char *path, int duration_seconds) { +void generate_test_wav(const char* path, int duration_seconds) { ma_encoder_config config = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 1, 32000); ma_encoder encoder; @@ -40,8 +40,8 @@ void generate_test_wav(const char *path, int duration_seconds) { } int main() { - const char *test_wav = "test_input.wav"; - const char *test_spec = "test_output.spec"; + const char* test_wav = "test_input.wav"; + const char* test_spec = "test_output.spec"; printf("Generating test WAV...\n"); generate_test_wav(test_wav, 1); @@ -54,7 +54,7 @@ int main() { assert(ret == 0); printf("Verifying .spec file...\n"); - FILE *f = fopen(test_spec, "rb"); + FILE* f = fopen(test_spec, "rb"); assert(f != NULL); SpecHeader header; diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc index de3934b..30295b9 100644 --- a/src/util/asset_manager.cc +++ b/src/util/asset_manager.cc @@ -9,19 +9,21 @@ extern const AssetRecord g_assets[]; extern const size_t g_assets_count; -const uint8_t *GetAsset(AssetId asset_id, size_t *out_size) { +const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) { uint16_t index = (uint16_t)asset_id; if (index >= g_assets_count) { - if (out_size) *out_size = 0; + if (out_size) + *out_size = 0; return nullptr; } - const AssetRecord &record = g_assets[index]; - if (out_size) *out_size = record.size; + const AssetRecord& record = g_assets[index]; + if (out_size) + *out_size = record.size; return record.data; } -void DropAsset(AssetId asset_id, const uint8_t *asset) { +void DropAsset(AssetId asset_id, const uint8_t* asset) { (void)asset_id; (void)asset; // Implementation for lazy decompression will go here diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h index 1b5e510..54d0144 100644 --- a/src/util/asset_manager.h +++ b/src/util/asset_manager.h @@ -10,10 +10,10 @@ enum class AssetId : uint16_t; struct AssetRecord { - const uint8_t *data; + const uint8_t* data; size_t size; }; // Generic interface -const uint8_t *GetAsset(AssetId asset_id, size_t *out_size = nullptr); -void DropAsset(AssetId asset_id, const uint8_t *asset); +const uint8_t* GetAsset(AssetId asset_id, size_t* out_size = nullptr); +void DropAsset(AssetId asset_id, const uint8_t* asset); diff --git a/src/util/math.h b/src/util/math.h index 8aa2c7a..f565b76 100644 --- a/src/util/math.h +++ b/src/util/math.h @@ -4,6 +4,6 @@ #pragma once -#ifndef M_PI +#if !defined(M_PI) #define M_PI 3.14159265358979323846 -#endif +#endif /* !defined(M_PI) */ diff --git a/src/util/mini_math.h b/src/util/mini_math.h index d3c9bc5..7314933 100644 --- a/src/util/mini_math.h +++ b/src/util/mini_math.h @@ -18,26 +18,33 @@ // T: Class Name (e.g., vec3) // N: Number of active components for math (e.g., 3) #define VEC_OPERATORS(T, N) \ - float &operator[](int i) { return v[i]; } \ - const float &operator[](int i) const { return v[i]; } \ - T &operator+=(const T &r) { \ - for (int i = 0; i < N; ++i) v[i] += r.v[i]; \ + float& operator[](int i) { \ + return v[i]; \ + } \ + const float& operator[](int i) const { \ + return v[i]; \ + } \ + T& operator+=(const T& r) { \ + for (int i = 0; i < N; ++i) \ + v[i] += r.v[i]; \ return *this; \ } \ - T &operator-=(const T &r) { \ - for (int i = 0; i < N; ++i) v[i] -= r.v[i]; \ + T& operator-=(const T& r) { \ + for (int i = 0; i < N; ++i) \ + v[i] -= r.v[i]; \ return *this; \ } \ - T &operator*=(float s) { \ - for (int i = 0; i < N; ++i) v[i] *= s; \ + T& operator*=(float s) { \ + for (int i = 0; i < N; ++i) \ + v[i] *= s; \ return *this; \ } \ - T operator+(const T &r) const { \ + T operator+(const T& r) const { \ T res(*this); \ res += r; \ return res; \ } \ - T operator-(const T &r) const { \ + T operator-(const T& r) const { \ T res(*this); \ res -= r; \ return res; \ @@ -49,24 +56,34 @@ } \ T operator-() const { \ T res; \ - for (int i = 0; i < N; ++i) res.v[i] = -v[i]; \ + for (int i = 0; i < N; ++i) \ + res.v[i] = -v[i]; \ return res; \ } \ - static float dot(const T &a, const T &b) { \ + static float dot(const T& a, const T& b) { \ float s = 0; \ - for (int i = 0; i < N; ++i) s += a.v[i] * b.v[i]; \ + for (int i = 0; i < N; ++i) \ + s += a.v[i] * b.v[i]; \ return s; \ } \ - float dot(const T &a) const { return dot(*this, a); } \ - float norm() const { return std::sqrt(dot(*this, *this)); } \ - float len() const { return norm(); } \ + float dot(const T& a) const { \ + return dot(*this, a); \ + } \ + float norm() const { \ + return std::sqrt(dot(*this, *this)); \ + } \ + float len() const { \ + return norm(); \ + } \ float inv_norm() const { \ float l2 = dot(*this, *this); \ return l2 > 0 ? 1.0f / std::sqrt(l2) : 0; \ } \ - T normalize() const { return (*this) * inv_norm(); } + T normalize() const { \ + return (*this) * inv_norm(); \ + } -#ifdef USE_VEC2 +#if defined(USE_VEC2) struct vec2 { union { struct { @@ -74,12 +91,13 @@ struct vec2 { }; float v[2]; }; - vec2(float x = 0, float y = 0) : x(x), y(y) {} + vec2(float x = 0, float y = 0) : x(x), y(y) { + } VEC_OPERATORS(vec2, 2) }; -#endif +#endif /* defined(USE_VEC2) */ -#ifdef USE_VEC3 +#if defined(USE_VEC3) struct vec3 { union { struct { @@ -88,7 +106,8 @@ struct vec3 { }; // _ is padding for 16-byte alignment float v[4]; // Size 4 to match alignment }; - vec3(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z), _(0) {} + vec3(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z), _(0) { + } VEC_OPERATORS(vec3, 3) // Operators only touch x,y,z (indices 0,1,2) static vec3 cross(vec3 a, vec3 b) { @@ -96,9 +115,9 @@ struct vec3 { a.x * b.y - a.y * b.x}; } }; -#endif +#endif /* defined(USE_VEC3) */ -#ifdef USE_VEC4 +#if defined(USE_VEC4) struct vec4 { union { struct { @@ -107,12 +126,13 @@ struct vec4 { float v[4]; }; vec4(float x = 0, float y = 0, float z = 0, float w = 0) - : x(x), y(y), z(z), w(w) {} + : x(x), y(y), z(z), w(w) { + } VEC_OPERATORS(vec4, 4) }; -#endif +#endif /* defined(USE_VEC4) */ -#ifdef USE_MAT4 +#if defined(USE_MAT4) struct mat4 { float m[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; // Identity (Column-Major) @@ -148,9 +168,9 @@ struct mat4 { return res; } }; -#endif +#endif /* defined(USE_MAT4) */ -#ifdef USE_QUAT +#if defined(USE_QUAT) struct quat { union { struct { @@ -159,10 +179,11 @@ struct quat { float v[4]; }; quat(float x = 0, float y = 0, float z = 0, float w = 1) - : x(x), y(y), z(z), w(w) {} + : x(x), y(y), z(z), w(w) { + } VEC_OPERATORS(quat, 4) - quat operator*(const quat &q) const { + quat operator*(const quat& q) const { return {w * q.x + x * q.w + y * q.z - z * q.y, w * q.y - x * q.z + y * q.w + z * q.x, w * q.z + x * q.y - y * q.x + z * q.w, @@ -177,7 +198,8 @@ struct quat { static quat from_to(vec3 a, vec3 b) { float d = vec3::dot(a, b); vec3 axis = vec3::cross(a, b); - if (d < -0.9999f) return {0, 1, 0, 0}; + if (d < -0.9999f) + return {0, 1, 0, 0}; float s = std::sqrt((1.0f + d) * 2.0f), inv_s = 1.0f / s; return {axis.x * inv_s, axis.y * inv_s, axis.z * inv_s, s * 0.5f}; } @@ -237,22 +259,25 @@ inline quat slerp(quat a, quat b, float t) { } if (d > 0.9995f) { // Linear fall-back quat r; - for (int i = 0; i < 4; ++i) r.v[i] = a.v[i] + (b.v[i] - a.v[i]) * t; + for (int i = 0; i < 4; ++i) + r.v[i] = a.v[i] + (b.v[i] - a.v[i]) * t; return r; } float th0 = std::acos(d), th = th0 * t, s0 = std::sin(th0), s1 = std::sin(th) / s0, s2 = std::sin(th0 - th) / s0; return a * s2 + b * s1; } -#endif +#endif /* defined(USE_QUAT) */ -template <typename T> inline T lerp(const T &a, const T &b, float t) { +template <typename T> inline T lerp(const T& a, const T& b, float t) { return a + (b - a) * t; } -#ifdef USE_EASING +#if defined(USE_EASING) namespace ease { -inline float out_cubic(float t) { return 1.0f - std::pow(1.0f - t, 3.0f); } +inline float out_cubic(float t) { + return 1.0f - std::pow(1.0f - t, 3.0f); +} inline float in_out_quad(float t) { return t < 0.5f ? 2.0f * t * t : 1.0f - std::pow(-2.0f * t + 2.0f, 2.0f) / 2.0f; @@ -260,13 +285,13 @@ inline float in_out_quad(float t) { inline float out_expo(float t) { return t == 1.0f ? 1.0f : 1.0f - std::pow(2.0f, -10.0f * t); } -} -#endif +} // namespace ease +#endif /* defined(USE_EASING) */ -#ifdef USE_SPRING +#if defined(USE_SPRING) namespace spring { template <typename T> -void solve(T ¤t, T &velocity, const T &target, float smooth_time, +void solve(T& current, T& velocity, const T& target, float smooth_time, float dt) { float omega = 2.0f / smooth_time; float x = omega * dt; @@ -276,5 +301,5 @@ void solve(T ¤t, T &velocity, const T &target, float smooth_time, velocity = (velocity - temp * omega) * exp; current = target + (change + temp) * exp; } -} -#endif +} // namespace spring +#endif /* defined(USE_SPRING) */ |
