summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gpu/gpu.cc21
-rw-r--r--src/gpu/gpu.h2
-rw-r--r--src/main.cc9
3 files changed, 24 insertions, 8 deletions
diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc
index 9eb2437..707ffe2 100644
--- a/src/gpu/gpu.cc
+++ b/src/gpu/gpu.cc
@@ -7,6 +7,7 @@
#include <algorithm>
#include <cassert>
+#include <cstdint>
#include <cstring>
#include <vector>
@@ -86,6 +87,7 @@ static void handle_device_error(WGPUDevice const *device, WGPUErrorType type,
const char *shader_wgsl_code = R"(
struct Uniforms {
audio_peak : f32,
+ aspect_ratio: f32,
};
@group(0) @binding(0) var<uniform> uniforms : Uniforms;
@@ -108,7 +110,7 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> @builtin(position) vec4<
let i = tri_idx + f32(sub_idx - 1u);
let angle = i * 2.0 * PI / num_sides;
- let x = scale * cos(angle);
+ let x = scale * cos(angle) / uniforms.aspect_ratio;
let y = scale * sin(angle);
return vec4<f32>(x, y, 0.0, 1.0);
@@ -179,14 +181,14 @@ void gpu_init(GLFWwindow *window) {
WGPUBufferDescriptor uniform_buffer_desc = {};
uniform_buffer_desc.label = str_view("Uniform Buffer");
uniform_buffer_desc.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst;
- uniform_buffer_desc.size = sizeof(float);
+ uniform_buffer_desc.size = sizeof(float) * 2;
g_uniform_buffer = wgpuDeviceCreateBuffer(g_device, &uniform_buffer_desc);
WGPUBindGroupLayoutEntry bgl_entry = {};
bgl_entry.binding = 0;
bgl_entry.visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment;
bgl_entry.buffer.type = WGPUBufferBindingType_Uniform;
- bgl_entry.buffer.minBindingSize = sizeof(float);
+ bgl_entry.buffer.minBindingSize = sizeof(float) * 2;
WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.entryCount = 1;
@@ -197,7 +199,7 @@ void gpu_init(GLFWwindow *window) {
WGPUBindGroupEntry bg_entry = {};
bg_entry.binding = 0;
bg_entry.buffer = g_uniform_buffer;
- bg_entry.size = sizeof(float);
+ bg_entry.size = sizeof(float) * 2;
WGPUBindGroupDescriptor bg_desc = {};
bg_desc.layout = bind_group_layout;
@@ -236,6 +238,7 @@ void gpu_init(GLFWwindow *window) {
glfwGetFramebufferSize(window, &width, &height);
g_config.device = g_device;
g_config.format = swap_chain_format;
+ g_config.usage = WGPUTextureUsage_RenderAttachment;
g_config.width = width;
g_config.height = height;
g_config.presentMode = WGPUPresentMode_Fifo;
@@ -244,7 +247,7 @@ void gpu_init(GLFWwindow *window) {
wgpuSurfaceConfigure(g_surface, &g_config);
}
-void gpu_draw(float audio_peak) {
+void gpu_draw(float audio_peak, float aspect_ratio) {
WGPUSurfaceTexture surface_texture;
wgpuSurfaceGetCurrentTexture(g_surface, &surface_texture);
if (surface_texture.status != WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal &&
@@ -253,7 +256,11 @@ void gpu_draw(float audio_peak) {
WGPUTextureView view = wgpuTextureCreateView(surface_texture.texture, nullptr);
- wgpuQueueWriteBuffer(g_queue, g_uniform_buffer, 0, &audio_peak, sizeof(float));
+ struct {
+ float audio_peak;
+ float aspect_ratio;
+ } uniforms = {audio_peak, aspect_ratio};
+ wgpuQueueWriteBuffer(g_queue, g_uniform_buffer, 0, &uniforms, sizeof(uniforms));
WGPUCommandEncoderDescriptor encoder_desc = {};
WGPUCommandEncoder encoder =
@@ -261,9 +268,11 @@ void gpu_draw(float audio_peak) {
WGPURenderPassColorAttachment color_attachment = {};
color_attachment.view = view;
+ color_attachment.resolveTarget = nullptr;
color_attachment.loadOp = WGPULoadOp_Clear;
color_attachment.storeOp = WGPUStoreOp_Store;
color_attachment.clearValue = {0.1, 0.2, 0.3, 1.0};
+ color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
WGPURenderPassDescriptor render_pass_desc = {};
render_pass_desc.colorAttachmentCount = 1;
diff --git a/src/gpu/gpu.h b/src/gpu/gpu.h
index 127ca42..87d4b9b 100644
--- a/src/gpu/gpu.h
+++ b/src/gpu/gpu.h
@@ -2,5 +2,5 @@
struct GLFWwindow;
void gpu_init(GLFWwindow *window);
-void gpu_draw(float audio_peak);
+void gpu_draw(float audio_peak, float aspect_ratio);
void gpu_shutdown();
diff --git a/src/main.cc b/src/main.cc
index ef48f21..e46468f 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -3,7 +3,9 @@
#include "gpu/gpu.h"
#include "platform.h"
#include "util/math.h"
+#include <GLFW/glfw3.h>
#include <math.h>
+#include <stdio.h>
#include <string.h>
#define DEMO_BPM 120.0f
@@ -75,7 +77,12 @@ int main(int argc, char **argv) {
}
}
- gpu_draw(synth_get_output_peak());
+ int width, height;
+ glfwGetFramebufferSize(platform_get_window(), &width, &height);
+ float aspect_ratio = (float)width / (float)height;
+
+ float visual_peak = fminf(synth_get_output_peak() * 80.0f, 1.0f);
+ gpu_draw(visual_peak, aspect_ratio);
audio_update();
}