// This file is part of the 64k demo project. // It implements the concrete effects used in the demo. #include "gpu/demo_effects.h" #include "gpu/gpu.h" #include #include #include #include // Helper to create a standard post-processing pipeline static WGPURenderPipeline create_post_process_pipeline(WGPUDevice device, WGPUTextureFormat format, const char* shader_code) { WGPUShaderModuleDescriptor shader_desc = {}; WGPUShaderSourceWGSL wgsl_src = {}; wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; wgsl_src.code = str_view(shader_code); shader_desc.nextInChain = &wgsl_src.chain; WGPUShaderModule shader_module = wgpuDeviceCreateShaderModule(device, &shader_desc); WGPUBindGroupLayoutEntry bgl_entries[3] = {}; bgl_entries[0].binding = 0; bgl_entries[0].visibility = WGPUShaderStage_Fragment; bgl_entries[0].sampler.type = WGPUSamplerBindingType_Filtering; bgl_entries[1].binding = 1; bgl_entries[1].visibility = WGPUShaderStage_Fragment; bgl_entries[1].texture.sampleType = WGPUTextureSampleType_Float; bgl_entries[1].texture.viewDimension = WGPUTextureViewDimension_2D; bgl_entries[2].binding = 2; bgl_entries[2].visibility = WGPUShaderStage_Fragment; bgl_entries[2].buffer.type = WGPUBufferBindingType_Uniform; WGPUBindGroupLayoutDescriptor bgl_desc = {}; bgl_desc.entryCount = 3; bgl_desc.entries = bgl_entries; WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc); WGPUPipelineLayoutDescriptor pl_desc = {}; pl_desc.bindGroupLayoutCount = 1; pl_desc.bindGroupLayouts = &bgl; WGPUPipelineLayout pl = wgpuDeviceCreatePipelineLayout(device, &pl_desc); WGPUColorTargetState color_target = {}; color_target.format = format; color_target.writeMask = WGPUColorWriteMask_All; WGPUFragmentState fragment_state = {}; fragment_state.module = shader_module; fragment_state.entryPoint = str_view("fs_main"); fragment_state.targetCount = 1; fragment_state.targets = &color_target; WGPURenderPipelineDescriptor pipeline_desc = {}; pipeline_desc.layout = pl; pipeline_desc.vertex.module = shader_module; pipeline_desc.vertex.entryPoint = str_view("vs_main"); pipeline_desc.fragment = &fragment_state; pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList; pipeline_desc.multisample.count = 1; pipeline_desc.multisample.mask = 0xFFFFFFFF; return wgpuDeviceCreateRenderPipeline(device, &pipeline_desc); } // --- SHADERS --- const char* main_shader_wgsl = R"( struct Uniforms { audio_peak: f32, aspect_ratio: f32, time: f32, }; @group(0) @binding(0) var uniforms: Uniforms; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { let PI = 3.14159265; let num_sides = 7.0; let scale = 0.5 + 0.3 * uniforms.audio_peak; let tri_idx = f32(i / 3u); let sub_idx = i % 3u; if (sub_idx == 0u) { return vec4(0.0, 0.0, 0.0, 1.0); } let angle = (tri_idx + f32(sub_idx - 1u)) * 2.0 * PI / num_sides + uniforms.time * 0.5; return vec4(scale * cos(angle) / uniforms.aspect_ratio, scale * sin(angle), 0.0, 1.0); } @fragment fn fs_main() -> @location(0) vec4 { let h = uniforms.time * 2.0 + uniforms.audio_peak * 3.0; let r = sin(h) * 0.5 + 0.5; let g = sin(h + 2.0) * 0.9 + 0.3; let b = sin(h + 4.0) * 0.5 + 0.5; let boost = uniforms.audio_peak * 0.5; return vec4(r + boost, g + boost, b + boost, 1.0); })"; const char* particle_compute_wgsl = R"( struct Particle { pos: vec4, vel: vec4, rot: vec4, color: vec4, }; struct Uniforms { audio_peak: f32, aspect_ratio: f32, time: f32, beat: f32, }; @group(0) @binding(0) var particles: array; @group(0) @binding(1) var uniforms: Uniforms; @compute @workgroup_size(64) fn main(@builtin(global_invocation_id) id: vec3) { let i = id.x; if (i >= arrayLength(&particles)) { return; } var p = particles[i]; let new_pos = p.pos.xyz + p.vel.xyz * 0.016; p.pos = vec4(new_pos, p.pos.w); p.vel.y = p.vel.y - 0.01 * (1.0 + uniforms.audio_peak * 5.0); p.rot.x = p.rot.x + p.rot.y * 0.016; if (p.pos.y < -1.5) { p.pos.y = 1.5; p.pos.x = (f32(i % 100u) / 50.0) - 1.0 + (uniforms.audio_peak * 0.5); p.vel.y = 0.0; } particles[i] = p; })"; const char* particle_render_wgsl = R"( struct Particle { pos: vec4, vel: vec4, rot: vec4, color: vec4, }; struct Uniforms { audio_peak: f32, aspect_ratio: f32, time: f32, beat: f32, }; @group(0) @binding(0) var particles: array; @group(0) @binding(1) var uniforms: Uniforms; struct VSOut { @builtin(position) pos: vec4, @location(0) color: vec4, }; @vertex fn vs_main(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VSOut { let p = particles[ii]; let size = 0.02 + p.pos.z * 0.01 + uniforms.audio_peak * 0.02; var offsets = array, 6>( vec2(-1, -1), vec2(1, -1), vec2(-1, 1), vec2(-1, 1), vec2(1, -1), vec2(1, 1) ); let offset = offsets[vi]; let c = cos(p.rot.x); let s = sin(p.rot.x); let rotated_offset = vec2(offset.x * c - offset.y * s, offset.x * s + offset.y * c); let pos = vec2(p.pos.x + rotated_offset.x * size / uniforms.aspect_ratio, p.pos.y + rotated_offset.y * size); return VSOut(vec4(pos, 0.0, 1.0), p.color * (0.5 + 0.5 * uniforms.audio_peak)); } @fragment fn fs_main(@location(0) color: vec4) -> @location(0) vec4 { return color; } )"; const char* passthrough_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { var pos = array, 3>( vec2(-1, -1), vec2(3, -1), vec2(-1, 3) ); return vec4(pos[i], 0.0, 1.0); } @fragment fn fs_main(@builtin(position) p: vec4) -> @location(0) vec4 { return textureSample(txt, smplr, p.xy / vec2(1280.0, 720.0)); })"; const char* ellipse_shader_wgsl = R"( struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(0) var uniforms: Uniforms; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { var pos = array, 3>( vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0) ); return vec4(pos[i], 0.0, 1.0); } fn sdEllipse(p: vec2, ab: vec2) -> f32 { var p_abs = abs(p); if (p_abs.x > p_abs.y) { p_abs = vec2(p_abs.y, p_abs.x); } let l = ab.y * ab.y - ab.x * ab.x; let m = ab.x * p_abs.x / l; let n = ab.y * p_abs.y / l; let m2 = m * m; let n2 = n * n; let c = (m2 + n2 - 1.0) / 3.0; let c3 = c * c * c; let d = c3 + m2 * n2; let g = m + m * n2; var co: f32; if (d < 0.0) { let h = acos((c3 + m2 * n2 * 2.0) / c3) / 3.0; let s = cos(h); let t = sin(h) * sqrt(3.0); co = (sqrt(-c * (s + t * 2.0) + m2) + sign(l) * sqrt(-c * (s - t * 2.0) + m2) + abs(g) / (sqrt(-c * (s + t * 2.0) + m2) * sqrt(-c * (s - t * 2.0) + m2)) - m) / 2.0; } else { let h = 2.0 * m * n * sqrt(d); let s = sign(c3 + m2 * n2 + h) * pow(abs(c3 + m2 * n2 + h), 1.0 / 3.0); let u = sign(c3 + m2 * n2 - h) * pow(abs(c3 + m2 * n2 - h), 1.0 / 3.0); let rx = -s - u + m2 * 2.0; let ry = (s - u) * sqrt(3.0); co = (ry / sqrt(sqrt(rx * rx + ry * ry) - rx) + 2.0 * g / sqrt(rx * rx + ry * ry) - m) / 2.0; } let si = sqrt(max(0.0, 1.0 - co * co)); return length(p_abs - vec2(ab.x * co, ab.y * si)) * sign(p_abs.y * ab.x * co - p_abs.x * ab.y * si); } @fragment fn fs_main(@builtin(position) p: vec4) -> @location(0) vec4 { let uv = (p.xy / vec2(1280.0, 720.0) - 0.5) * 2.0; let movement = vec2(sin(uniforms.time * 0.7), cos(uniforms.time * 0.5)); let d = sdEllipse((uv * vec2(uniforms.aspect_ratio, 1.0)) - movement, vec2(0.5, 0.3) * (1.0 + uniforms.beat * 0.2)); return mix(vec4(0.2, 0.8, 0.4, 1.0), vec4(0.0), smoothstep(0.0, 0.01, d)); })"; const char* particle_spray_compute_wgsl = R"( struct Particle { pos: vec4, vel: vec4, rot: vec4, color: vec4, }; struct Uniforms { intensity: f32, aspect_ratio: f32, time: f32, beat: f32, }; @group(0) @binding(0) var particles: array; @group(0) @binding(1) var uniforms: Uniforms; fn hash(p: f32) -> f32 { return fract(sin(p) * 43758.5453); } @compute @workgroup_size(64) fn main(@builtin(global_invocation_id) id: vec3) { let i = id.x; if (i >= arrayLength(&particles)) { return; } var p = particles[i]; if (p.pos.w <= 0.0) { let r = hash(f32(i) + uniforms.time); let angle = r * 6.28318; p.pos = vec4(0.0, 0.0, 0.0, 1.0); p.vel = vec4(cos(angle), sin(angle), 0.0, 0.0) * (0.5 + hash(r) * 0.5) * (1.0 + uniforms.intensity * 2.0); p.color = vec4(hash(r + 0.1), hash(r + 0.2), 1.0, 1.0); } let new_pos = p.pos.xyz + p.vel.xyz * 0.016; p.pos = vec4(new_pos, p.pos.w - 0.01 * (1.0 + uniforms.beat)); p.vel.y = p.vel.y - 0.01; particles[i] = p; })"; const char* gaussian_blur_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d; struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(2) var uniforms: Uniforms; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { var pos = array, 3>( vec2(-1, -1), vec2(3, -1), vec2(-1, 3) ); return vec4(pos[i], 0.0, 1.0); } @fragment fn fs_main(@builtin(position) p: vec4) -> @location(0) vec4 { let uv = p.xy / vec2(1280.0, 720.0); var res = vec4(0.0); let size = 5.0 * uniforms.intensity; for (var x: f32 = -2.0; x <= 2.0; x += 1.0) { for (var y: f32 = -2.0; y <= 2.0; y += 1.0) { res += textureSample(txt, smplr, uv + vec2(x, y) * size / 1280.0); } } return res / 25.0; })"; const char* solarize_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d; struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(2) var uniforms: Uniforms; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { var pos = array, 3>( vec2(-1, -1), vec2(3, -1), vec2(-1, 3) ); return vec4(pos[i], 0.0, 1.0); } @fragment fn fs_main(@builtin(position) p: vec4) -> @location(0) vec4 { let uv = p.xy / vec2(1280.0, 720.0); var col = textureSample(txt, smplr, uv); let thr = 0.5 + 0.3 * sin(uniforms.time); if (col.r < thr) { col.r = 1.0 - col.r; } if (col.g < thr) { col.g = 1.0 - col.g; } if (col.b < thr) { col.b = 1.0 - col.b; } return col; })"; const char* distort_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d; struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(2) var uniforms: Uniforms; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { var pos = array, 3>( vec2(-1, -1), vec2(3, -1), vec2(-1, 3) ); return vec4(pos[i], 0.0, 1.0); } @fragment fn fs_main(@builtin(position) p: vec4) -> @location(0) vec4 { let uv = p.xy / vec2(1280.0, 720.0); let dist = 0.1 * uniforms.intensity * sin(uv.y * 20.0 + uniforms.time * 5.0); return textureSample(txt, smplr, uv + vec2(dist, 0.0)); })"; const char* chroma_aberration_shader_wgsl = R"( @group(0) @binding(0) var smplr: sampler; @group(0) @binding(1) var txt: texture_2d; struct Uniforms { time: f32, beat: f32, intensity: f32, aspect_ratio: f32, }; @group(0) @binding(2) var uniforms: Uniforms; @vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { var pos = array, 3>( vec2(-1, -1), vec2(3, -1), vec2(-1, 3) ); return vec4(pos[i], 0.0, 1.0); } @fragment fn fs_main(@builtin(position) p: vec4) -> @location(0) vec4 { let uv = p.xy / vec2(1280.0, 720.0); let off = 0.02 * uniforms.intensity; let r = textureSample(txt, smplr, uv + vec2(off, 0.0)).r; let g = textureSample(txt, smplr, uv).g; let b = textureSample(txt, smplr, uv - vec2(off, 0.0)).b; return vec4(r, g, b, 1.0); })"; // --- HeptagonEffect --- HeptagonEffect::HeptagonEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) : queue_(queue) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); ResourceBinding bindings[] = {{uniforms_, WGPUBufferBindingType_Uniform}}; pass_ = gpu_create_render_pass(device, format, main_shader_wgsl, bindings, 1); pass_.vertex_count = 21; } void HeptagonEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { struct { float p, a, t, d; } u = {i, a, t, 0.0f}; wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u)); wgpuRenderPassEncoderSetPipeline(pass, pass_.pipeline); wgpuRenderPassEncoderSetBindGroup(pass, 0, pass_.bind_group, 0, nullptr); wgpuRenderPassEncoderDraw(pass, pass_.vertex_count, 1, 0, 0); } // --- ParticlesEffect --- ParticlesEffect::ParticlesEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) : queue_(queue) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); std::vector init_p(NUM_PARTICLES); particles_buffer_ = gpu_create_buffer( device, sizeof(Particle) * NUM_PARTICLES, WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex, init_p.data()); ResourceBinding cb[] = {{particles_buffer_, WGPUBufferBindingType_Storage}, {uniforms_, WGPUBufferBindingType_Uniform}}; compute_pass_ = gpu_create_compute_pass(device, particle_compute_wgsl, cb, 2); compute_pass_.workgroup_size_x = (NUM_PARTICLES + 63) / 64; ResourceBinding rb[] = { {particles_buffer_, WGPUBufferBindingType_ReadOnlyStorage}, {uniforms_, WGPUBufferBindingType_Uniform}}; render_pass_ = gpu_create_render_pass(device, format, particle_render_wgsl, rb, 2); render_pass_.vertex_count = 6; render_pass_.instance_count = NUM_PARTICLES; } void ParticlesEffect::compute(WGPUCommandEncoder e, float t, float b, float i, float a) { struct { float p, a, t, d; } u = {i, a, t, 0.0f}; wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u)); WGPUComputePassEncoder pass = wgpuCommandEncoderBeginComputePass(e, nullptr); wgpuComputePassEncoderSetPipeline(pass, compute_pass_.pipeline); wgpuComputePassEncoderSetBindGroup(pass, 0, compute_pass_.bind_group, 0, nullptr); wgpuComputePassEncoderDispatchWorkgroups(pass, compute_pass_.workgroup_size_x, 1, 1); wgpuComputePassEncoderEnd(pass); } void ParticlesEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { wgpuRenderPassEncoderSetPipeline(pass, render_pass_.pipeline); wgpuRenderPassEncoderSetBindGroup(pass, 0, render_pass_.bind_group, 0, nullptr); wgpuRenderPassEncoderDraw(pass, render_pass_.vertex_count, render_pass_.instance_count, 0, 0); } // --- PostProcess Implementation Helper --- static void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline, WGPUBindGroup* bind_group, WGPUTextureView input_view, GpuBuffer uniforms) { if (*bind_group) wgpuBindGroupRelease(*bind_group); WGPUBindGroupLayout bgl = wgpuRenderPipelineGetBindGroupLayout(pipeline, 0); WGPUSamplerDescriptor sd = {}; sd.magFilter = WGPUFilterMode_Linear; sd.minFilter = WGPUFilterMode_Linear; sd.maxAnisotropy = 1; WGPUSampler sampler = wgpuDeviceCreateSampler(device, &sd); WGPUBindGroupEntry bge[3] = {}; bge[0].binding = 0; bge[0].sampler = sampler; bge[1].binding = 1; bge[1].textureView = input_view; bge[2].binding = 2; bge[2].buffer = uniforms.buffer; bge[2].size = uniforms.size; WGPUBindGroupDescriptor bgd = { .layout = bgl, .entryCount = 3, .entries = bge}; *bind_group = wgpuDeviceCreateBindGroup(device, &bgd); } // --- PassthroughEffect --- PassthroughEffect::PassthroughEffect(WGPUDevice device, WGPUTextureFormat format) : device_(device) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); pipeline_ = create_post_process_pipeline(device, format, passthrough_shader_wgsl); } void PassthroughEffect::update_bind_group(WGPUTextureView input_view) { pp_update_bind_group(device_, pipeline_, &bind_group_, input_view, uniforms_); } // --- MovingEllipseEffect --- MovingEllipseEffect::MovingEllipseEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) : queue_(queue) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); ResourceBinding bindings[] = {{uniforms_, WGPUBufferBindingType_Uniform}}; pass_ = gpu_create_render_pass(device, format, ellipse_shader_wgsl, bindings, 1); pass_.vertex_count = 3; } void MovingEllipseEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { struct { float t, b, i, a; } u = {t, b, i, a}; wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u)); wgpuRenderPassEncoderSetPipeline(pass, pass_.pipeline); wgpuRenderPassEncoderSetBindGroup(pass, 0, pass_.bind_group, 0, nullptr); wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); } // --- ParticleSprayEffect --- ParticleSprayEffect::ParticleSprayEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) : queue_(queue) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); std::vector init_p(NUM_PARTICLES); 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()); ResourceBinding cb[] = {{particles_buffer_, WGPUBufferBindingType_Storage}, {uniforms_, WGPUBufferBindingType_Uniform}}; compute_pass_ = gpu_create_compute_pass(device, particle_spray_compute_wgsl, cb, 2); compute_pass_.workgroup_size_x = (NUM_PARTICLES + 63) / 64; ResourceBinding rb[] = { {particles_buffer_, WGPUBufferBindingType_ReadOnlyStorage}, {uniforms_, WGPUBufferBindingType_Uniform}}; render_pass_ = gpu_create_render_pass(device, format, particle_render_wgsl, rb, 2); render_pass_.vertex_count = 6; render_pass_.instance_count = NUM_PARTICLES; } void ParticleSprayEffect::compute(WGPUCommandEncoder e, float t, float b, float i, float a) { struct { float i, a, t, b; } u = {i, a, t, b}; wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u)); WGPUComputePassEncoder pass = wgpuCommandEncoderBeginComputePass(e, nullptr); wgpuComputePassEncoderSetPipeline(pass, compute_pass_.pipeline); wgpuComputePassEncoderSetBindGroup(pass, 0, compute_pass_.bind_group, 0, nullptr); wgpuComputePassEncoderDispatchWorkgroups(pass, compute_pass_.workgroup_size_x, 1, 1); wgpuComputePassEncoderEnd(pass); } void ParticleSprayEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { wgpuRenderPassEncoderSetPipeline(pass, render_pass_.pipeline); wgpuRenderPassEncoderSetBindGroup(pass, 0, render_pass_.bind_group, 0, nullptr); wgpuRenderPassEncoderDraw(pass, 6, NUM_PARTICLES, 0, 0); } // --- GaussianBlurEffect --- GaussianBlurEffect::GaussianBlurEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) : device_(device), queue_(queue) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); pipeline_ = create_post_process_pipeline(device, format, gaussian_blur_shader_wgsl); } void GaussianBlurEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { struct { float t, b, i, a; } u = {t, b, i, a}; wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u)); PostProcessEffect::render(pass, t, b, i, a); } void GaussianBlurEffect::update_bind_group(WGPUTextureView v) { pp_update_bind_group(device_, pipeline_, &bind_group_, v, uniforms_); } // --- SolarizeEffect --- SolarizeEffect::SolarizeEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) : device_(device), queue_(queue) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); pipeline_ = create_post_process_pipeline(device, format, solarize_shader_wgsl); } void SolarizeEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { struct { float t, b, i, a; } u = {t, b, i, a}; wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u)); PostProcessEffect::render(pass, t, b, i, a); } void SolarizeEffect::update_bind_group(WGPUTextureView v) { pp_update_bind_group(device_, pipeline_, &bind_group_, v, uniforms_); } // --- DistortEffect --- DistortEffect::DistortEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) : device_(device), queue_(queue) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); pipeline_ = create_post_process_pipeline(device, format, distort_shader_wgsl); } void DistortEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { struct { float t, b, i, a; } u = {t, b, i, a}; wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u)); PostProcessEffect::render(pass, t, b, i, a); } void DistortEffect::update_bind_group(WGPUTextureView v) { pp_update_bind_group(device_, pipeline_, &bind_group_, v, uniforms_); } // --- ChromaAberrationEffect --- ChromaAberrationEffect::ChromaAberrationEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) : device_(device), queue_(queue) { uniforms_ = gpu_create_buffer(device, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); pipeline_ = create_post_process_pipeline(device, format, chroma_aberration_shader_wgsl); } void ChromaAberrationEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { struct { float t, b, i, a; } u = {t, b, i, a}; wgpuQueueWriteBuffer(queue_, uniforms_.buffer, 0, &u, sizeof(u)); PostProcessEffect::render(pass, t, b, i, a); } void ChromaAberrationEffect::update_bind_group(WGPUTextureView v) { pp_update_bind_group(device_, pipeline_, &bind_group_, v, uniforms_); }