summaryrefslogtreecommitdiff
path: root/src/gpu/post_process_helper.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/post_process_helper.cc')
-rw-r--r--src/gpu/post_process_helper.cc58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/gpu/post_process_helper.cc b/src/gpu/post_process_helper.cc
new file mode 100644
index 0000000..4ffa9c1
--- /dev/null
+++ b/src/gpu/post_process_helper.cc
@@ -0,0 +1,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/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);
+}