summaryrefslogtreecommitdiff
path: root/src/gpu/effects/post_process_helper.cc
blob: 0c339c71a78dd6dfc0e08ac43712126df523adf7 (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
// This file is part of the 64k demo project.
// It implements helper functions for post-processing effects.

#include "post_process_helper.h"
#include "../demo_effects.h"
#include "gpu/gpu.h"
#include "gpu/effects/shader_composer.h"
#include "gpu/bind_group_builder.h"
#include "gpu/sampler_cache.h"
#include "gpu/pipeline_builder.h"
#include <cstring>

// Helper to create a standard post-processing pipeline
WGPURenderPipeline create_post_process_pipeline(WGPUDevice device,
                                                WGPUTextureFormat format,
                                                const char* shader_code) {
  WGPUBindGroupLayout bgl = BindGroupLayoutBuilder()
    .sampler(PP_BINDING_SAMPLER, WGPUShaderStage_Fragment)
    .texture(PP_BINDING_TEXTURE, WGPUShaderStage_Fragment)
    .uniform(PP_BINDING_UNIFORMS, WGPUShaderStage_Vertex | WGPUShaderStage_Fragment)
    .uniform(PP_BINDING_EFFECT_PARAMS, WGPUShaderStage_Fragment)
    .build(device);

  WGPURenderPipeline pipeline = RenderPipelineBuilder(device)
    .shader(shader_code)
    .bind_group_layout(bgl)
    .format(format)
    .build();

  wgpuBindGroupLayoutRelease(bgl);
  return pipeline;
}

// --- PostProcess Implementation Helper ---
static GpuBuffer g_dummy_buffer = {nullptr, 0};

void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline,
                          WGPUBindGroup* bind_group, WGPUTextureView input_view,
                          GpuBuffer uniforms, GpuBuffer effect_params) {
  if (!g_dummy_buffer.buffer) {
    g_dummy_buffer = gpu_create_buffer(device, 32, WGPUBufferUsage_Uniform);
  }

  if (*bind_group)
    wgpuBindGroupRelease(*bind_group);

  WGPUBindGroupLayout bgl = wgpuRenderPipelineGetBindGroupLayout(pipeline, 0);
  WGPUSampler sampler = SamplerCache::Get().get_or_create(device, SamplerCache::linear());

  *bind_group = BindGroupBuilder()
    .sampler(PP_BINDING_SAMPLER, sampler)
    .texture(PP_BINDING_TEXTURE, input_view)
    .buffer(PP_BINDING_UNIFORMS, uniforms.buffer, uniforms.size)
    .buffer(PP_BINDING_EFFECT_PARAMS,
            effect_params.buffer ? effect_params.buffer : g_dummy_buffer.buffer,
            effect_params.buffer ? effect_params.size : g_dummy_buffer.size)
    .build(device, bgl);
}