summaryrefslogtreecommitdiff
path: root/src/gpu/effects/chroma_aberration_effect.cc
blob: 3e953e3e437cd37b3e9ee0a2a525a8b5fa123502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// This file is part of the 64k demo project.
// It implements the ChromaAberrationEffect with parameterization.

#include "gpu/demo_effects.h"
#include "gpu/effects/post_process_helper.h"
#include "gpu/gpu.h"

// --- ChromaAberrationEffect ---

// Backward compatibility constructor (delegates to parameterized constructor)
ChromaAberrationEffect::ChromaAberrationEffect(const GpuContext& ctx)
    : ChromaAberrationEffect(ctx, ChromaAberrationParams{}) {
}

// Parameterized constructor
ChromaAberrationEffect::ChromaAberrationEffect(
    const GpuContext& ctx, const ChromaAberrationParams& params)
    : PostProcessEffect(ctx), params_(params) {
  pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format,
                                           chroma_aberration_shader_wgsl);
  uniforms_.init(ctx_.device);
}

void ChromaAberrationEffect::render(WGPURenderPassEncoder pass, float time,
                                    float beat, float intensity,
                                    float aspect_ratio) {
  // Update uniforms with current state and parameters
  const ChromaUniforms u = {.time = time,
                            .beat = beat,
                            .intensity = intensity,
                            .aspect_ratio = aspect_ratio,
                            .width = (float)width_,
                            .height = (float)height_,
                            .offset_scale = params_.offset_scale,
                            .angle = params_.angle};
  uniforms_.update(ctx_.queue, u);

  wgpuRenderPassEncoderSetPipeline(pass, pipeline_);
  wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr);
  wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
}

void ChromaAberrationEffect::update_bind_group(WGPUTextureView input_view) {
  pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view,
                       uniforms_.get());
}