diff options
| author | skal <pascal.massimino@gmail.com> | 2026-01-28 02:51:21 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-01-28 02:51:21 +0100 |
| commit | 295626c3d527613c93fa63e2b1f079704600a98e (patch) | |
| tree | 978da6b066bb1ff69df4abb02887bc940e88afe1 | |
| parent | 96bfc344ce9098681ff739cd7ed0a1d1f8ba54df (diff) | |
build: Finalize WebGPU integration and platform fixes
Includes correct CMake configuration for GLFW native access, Objective-C++ compilation for the helper library on macOS, and applies clang-format to modified sources.
| -rw-r--r-- | CMakeLists.txt | 18 | ||||
| -rw-r--r-- | src/audio/synth.cc | 11 | ||||
| -rw-r--r-- | src/gpu/gpu.cc | 35 | ||||
| -rw-r--r-- | src/main.cc | 7 | ||||
| -rw-r--r-- | src/platform.cc | 16 |
5 files changed, 60 insertions, 27 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1238a8a..69e51c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,18 @@ cmake_minimum_required(VERSION 3.16) project(demo64k LANGUAGES C CXX) +if(APPLE) + add_definitions(-DGLFW_EXPOSE_NATIVE_COCOA) +elseif(WIN32) + add_definitions(-DGLFW_EXPOSE_NATIVE_WIN32) +elseif(UNIX) + if(DEFINED CMAKE_USE_WAYLAND) + add_definitions(-DGLFW_EXPOSE_NATIVE_WAYLAND) + else() + add_definitions(-DGLFW_EXPOSE_NATIVE_X11) + endif() +endif() + option(DEMO_SIZE_OPT "Enable size optimization flags" OFF) option(DEMO_STRIP_ALL "Strip all unnecessary code for final build" OFF) @@ -29,7 +41,11 @@ set(DEMO_LIBS glfw ${WGPU_LIBRARY}) # Platform-specific dependencies if (APPLE) - set_source_files_properties(src/platform.cc PROPERTIES COMPILE_FLAGS "-x objective-c++") + set_source_files_properties( + src/platform.cc + third_party/glfw3webgpu/glfw3webgpu.c + PROPERTIES COMPILE_FLAGS "-x objective-c++" + ) list(APPEND DEMO_LIBS "-framework Metal" "-framework Foundation" diff --git a/src/audio/synth.cc b/src/audio/synth.cc index 77a0d05..a4f3b7a 100644 --- a/src/audio/synth.cc +++ b/src/audio/synth.cc @@ -26,7 +26,8 @@ static struct { } g_synth_data; static Voice g_voices[MAX_VOICES]; -static volatile float g_current_output_peak = 0.0f; // Global peak for visualization +static volatile float g_current_output_peak = + 0.0f; // Global peak for visualization void synth_init() { memset(&g_synth_data, 0, sizeof(g_synth_data)); @@ -172,8 +173,8 @@ void synth_render(float *output_buffer, int num_frames) { output_buffer[i * 2 + 1] = right_sample; // Update the peak with the new max (attack) - g_current_output_peak = - fmaxf(g_current_output_peak, fmaxf(fabsf(left_sample), fabsf(right_sample))); + g_current_output_peak = fmaxf( + g_current_output_peak, fmaxf(fabsf(left_sample), fabsf(right_sample))); } } @@ -187,4 +188,6 @@ int synth_get_active_voice_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/gpu/gpu.cc b/src/gpu/gpu.cc index 79498e3..b87c47f 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -28,7 +28,8 @@ static WGPUBindGroup g_bind_group = nullptr; static WGPUStringView label_view(const char *str) { #ifndef STRIP_ALL - if (!str) return {nullptr, 0}; + if (!str) + return {nullptr, 0}; return {str, strlen(str)}; #else (void)str; @@ -37,7 +38,8 @@ static WGPUStringView label_view(const char *str) { } static WGPUStringView str_view(const char *str) { - if (!str) return {nullptr, 0}; + if (!str) + return {nullptr, 0}; return {str, strlen(str)}; } @@ -142,10 +144,9 @@ void gpu_init(GLFWwindow *window) { adapter_opts.compatibleSurface = g_surface; adapter_opts.powerPreference = WGPUPowerPreference_HighPerformance; - wgpuInstanceRequestAdapter( - g_instance, &adapter_opts, - {nullptr, WGPUCallbackMode_WaitAnyOnly, handle_request_adapter, &g_adapter, - nullptr}); + wgpuInstanceRequestAdapter(g_instance, &adapter_opts, + {nullptr, WGPUCallbackMode_WaitAnyOnly, + handle_request_adapter, &g_adapter, nullptr}); while (!g_adapter) { wgpuInstanceWaitAny(g_instance, 0, nullptr, 0); @@ -157,10 +158,9 @@ void gpu_init(GLFWwindow *window) { device_desc.uncapturedErrorCallbackInfo.callback = handle_device_error; #endif - wgpuAdapterRequestDevice( - g_adapter, &device_desc, - {nullptr, WGPUCallbackMode_WaitAnyOnly, handle_request_device, &g_device, - nullptr}); + wgpuAdapterRequestDevice(g_adapter, &device_desc, + {nullptr, WGPUCallbackMode_WaitAnyOnly, + handle_request_device, &g_device, nullptr}); while (!g_device) { wgpuInstanceWaitAny(g_instance, 0, nullptr, 0); @@ -259,17 +259,21 @@ void gpu_init(GLFWwindow *window) { void gpu_draw(float audio_peak, float aspect_ratio) { WGPUSurfaceTexture surface_texture; wgpuSurfaceGetCurrentTexture(g_surface, &surface_texture); - if (surface_texture.status != WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal && - surface_texture.status != WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal) + if (surface_texture.status != + WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal && + surface_texture.status != + WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal) return; - WGPUTextureView view = wgpuTextureCreateView(surface_texture.texture, nullptr); + WGPUTextureView view = + wgpuTextureCreateView(surface_texture.texture, nullptr); struct { float audio_peak; float aspect_ratio; } uniforms = {audio_peak, aspect_ratio}; - wgpuQueueWriteBuffer(g_queue, g_uniform_buffer, 0, &uniforms, sizeof(uniforms)); + wgpuQueueWriteBuffer(g_queue, g_uniform_buffer, 0, &uniforms, + sizeof(uniforms)); WGPUCommandEncoderDescriptor encoder_desc = {}; encoder_desc.label = label_view("Command Encoder"); @@ -308,4 +312,5 @@ void gpu_draw(float audio_peak, float aspect_ratio) { wgpuTextureRelease(surface_texture.texture); } -void gpu_shutdown() {}
\ No newline at end of file +void gpu_shutdown() { +}
\ No newline at end of file diff --git a/src/main.cc b/src/main.cc index e46468f..31e1c58 100644 --- a/src/main.cc +++ b/src/main.cc @@ -49,7 +49,7 @@ int main(int argc, char **argv) { audio_init(); generate_tone(g_spec_buffer_a, 440.0f); // A4 - generate_tone(g_spec_buffer_b, 0.0f); // A5 + generate_tone(g_spec_buffer_b, 0.0f); // A5 const Spectrogram spec = {g_spec_buffer_a, g_spec_buffer_b, SPEC_FRAMES}; int tone_id = synth_register_spectrogram(&spec); @@ -71,10 +71,11 @@ int main(int argc, char **argv) { // Time to update the sound! float *back_buffer = synth_begin_update(tone_id); if (back_buffer) { - generate_tone(back_buffer, (beat_count % 8) == 4 ? 220.0f : 480.f); // A3 + generate_tone(back_buffer, + (beat_count % 8) == 4 ? 220.0f : 480.f); // A3 synth_commit_update(tone_id); } - } + } } int width, height; diff --git a/src/platform.cc b/src/platform.cc index b0ec86b..2b6af44 100644 --- a/src/platform.cc +++ b/src/platform.cc @@ -42,9 +42,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; @@ -62,9 +66,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); |
