summaryrefslogtreecommitdiff
path: root/src/effects/heptagon_effect.cc
blob: 1bc9d0658fea8882c39f58135ffb35f64679500e (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Heptagon effect v2 implementation

#include "effects/heptagon_effect.h"
#include "util/fatal_error.h"
#include "gpu/gpu.h"
#include "gpu/post_process_helper.h"
#include "gpu/shaders.h"

Heptagon::Heptagon(const GpuContext& ctx,
                                   const std::vector<std::string>& inputs,
                                   const std::vector<std::string>& outputs)
    : Effect(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr), sampler_(nullptr) {
  // Headless mode: skip GPU resource creation (compiled out in STRIP_ALL)
  HEADLESS_RETURN_IF_NULL(ctx_.device);

  // Init uniforms
  uniforms_buffer_.init(ctx_.device);

  // Create pipeline (standard v2 post-process, no depth)
  pipeline_ = create_post_process_pipeline(ctx_.device, WGPUTextureFormat_RGBA8Unorm,
                                           heptagon_v2_shader_wgsl);

  // Create dummy sampler (scene effects don't use texture input)
  WGPUSamplerDescriptor sampler_desc = {};
  sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge;
  sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge;
  sampler_desc.magFilter = WGPUFilterMode_Nearest;
  sampler_desc.minFilter = WGPUFilterMode_Nearest;
  sampler_desc.maxAnisotropy = 1;
  sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc);

  // Create 1×1 dummy texture
  WGPUTextureDescriptor tex_desc = {};
  tex_desc.size = {1, 1, 1};
  tex_desc.format = WGPUTextureFormat_RGBA8Unorm;
  tex_desc.usage = WGPUTextureUsage_TextureBinding;
  tex_desc.dimension = WGPUTextureDimension_2D;
  tex_desc.mipLevelCount = 1;
  tex_desc.sampleCount = 1;
  dummy_texture_ = wgpuDeviceCreateTexture(ctx_.device, &tex_desc);
  dummy_texture_view_ = wgpuTextureCreateView(dummy_texture_, nullptr);
}

Heptagon::~Heptagon() {
  if (bind_group_) wgpuBindGroupRelease(bind_group_);
  if (pipeline_) wgpuRenderPipelineRelease(pipeline_);
  if (sampler_) wgpuSamplerRelease(sampler_);
  if (dummy_texture_view_) wgpuTextureViewRelease(dummy_texture_view_);
  if (dummy_texture_) wgpuTextureRelease(dummy_texture_);
}

void Heptagon::render(WGPUCommandEncoder encoder,
                              const UniformsSequenceParams& params,
                              NodeRegistry& nodes) {
  // Get output view (scene effects typically write to output, ignore input)
  WGPUTextureView output_view = nodes.get_view(output_nodes_[0]);

  // Update uniforms
  uniforms_buffer_.update(ctx_.queue, params);

  // Create bind group (use dummy texture for scene effect)
  pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, dummy_texture_view_,
                       uniforms_buffer_.get(), {nullptr, 0});

  // Render pass
  WGPURenderPassColorAttachment color_attachment = {};
  color_attachment.view = output_view;
  color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
  color_attachment.loadOp = WGPULoadOp_Clear;
  color_attachment.storeOp = WGPUStoreOp_Store;
  color_attachment.clearValue = {0.0, 0.0, 0.0, 1.0};

  WGPURenderPassDescriptor pass_desc = {};
  pass_desc.colorAttachmentCount = 1;
  pass_desc.colorAttachments = &color_attachment;

  WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc);
  wgpuRenderPassEncoderSetPipeline(pass, pipeline_);
  wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr);
  wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);  // Fullscreen triangle
  wgpuRenderPassEncoderEnd(pass);
  wgpuRenderPassEncoderRelease(pass);
}