accumulator blur

This commit is contained in:
2025-12-24 15:55:44 -06:00
parent 971299f062
commit 37c360dc67
3 changed files with 144 additions and 6 deletions

View File

@@ -11,7 +11,6 @@ var png = use('image/png')
var qoi = use('image/qoi')
var gif = use('image/gif')
var aseprite = use('image/aseprite')
var aseprite = use('image/aseprite')
var staef = use('staef')
var res = use('resources')
var geometry = use('geometry')
@@ -33,8 +32,8 @@ var _blit_frag = null
var _threshold_frag = null
var _blur_frag = null
var _mask_frag = null
var _mask_frag = null
var _crt_frag = null
var _accumulator_frag = null
var _text_sdf_frag = null
var _text_msdf_frag = null
@@ -242,6 +241,18 @@ function _load_shaders() {
num_samplers: 1
})
}
var accumulator_frag_code = io.slurp("shaders/msl/accumulator.frag.msl")
if (accumulator_frag_code) {
_accumulator_frag = new gpu_mod.shader(_gpu, {
code: accumulator_frag_code,
stage: "fragment",
format: "msl",
entrypoint: "fragment_main",
num_uniform_buffers: 1,
num_samplers: 2
})
}
return true
}
@@ -566,7 +577,29 @@ function _create_pipelines() {
}]
}
})
}
// Accumulator pipeline
if (_blit_vert && _accumulator_frag) {
_pipelines.accumulator = new gpu_mod.graphics_pipeline(_gpu, {
vertex: _blit_vert,
fragment: _accumulator_frag,
primitive: "triangle",
cull: "none",
face: "counter_clockwise",
fill: "fill",
vertex_buffer_descriptions: [{
slot: 0,
pitch: 16,
input_rate: "vertex"
}],
vertex_attributes: [
{location: 0, buffer_slot: 0, format: "float2", offset: 0},
{location: 1, buffer_slot: 0, format: "float2", offset: 8}
],
target: {
color_targets: [{format: _swapchain_format, blend: {enabled: false}}]
}
})
} }
}
// ========================================================================
@@ -684,7 +717,17 @@ sdl_gpu.get_or_create_target = function(width, height, key) {
_target_pool[pool_key] = []
// Reuse from pool if available
// Simple optimization: Prefer most recently used (LIFO) or just first available
// 1. Check if a target with this exact key already exists
if (key) {
for (var target of _target_pool[pool_key]) {
if (target.key == key) {
target.in_use = true
return target
}
}
}
// 2. Otherwise prefer most recently used (LIFO) or just first available
for (var target of _target_pool[pool_key]) {
if (!target.in_use) {
target.in_use = true
@@ -1608,6 +1651,9 @@ function _do_shader_pass(cmd_buffer, cmd, get_swapchain_tex) {
case 'crt':
pipeline = _pipelines.crt
break
case 'accumulator':
pipeline = _pipelines.accumulator
break
default:
log.console(`sdl_gpu: Unknown shader: ${shader}`)
return
@@ -1684,7 +1730,15 @@ function _do_shader_pass(cmd_buffer, cmd, get_swapchain_tex) {
pass.bind_pipeline(pipeline)
pass.bind_vertex_buffers(0, [{buffer: vb, offset: 0}])
pass.bind_index_buffer({buffer: ib, offset: 0}, 16)
pass.bind_fragment_samplers(0, [{texture: input.texture, sampler: _sampler_linear}])
// Bind samplers
var samplers = [{texture: input.texture, sampler: _sampler_linear}]
if (cmd.extra_inputs) {
for (var extra of cmd.extra_inputs) {
samplers.push({texture: extra.texture, sampler: _sampler_linear})
}
}
pass.bind_fragment_samplers(0, samplers)
if (uniform_data) {
cmd_buffer.push_fragment_uniform_data(0, uniform_data)
@@ -1723,6 +1777,12 @@ function _build_shader_uniforms(shader, uniforms) {
data.wf(0) // padding
data.wf(0) // padding
break
case 'accumulator':
data.wf(uniforms.decay != null ? uniforms.decay : 0.9)
data.wf(0) // padding
data.wf(0) // padding
data.wf(0) // padding
break
default:
return null
}
@@ -1797,4 +1857,4 @@ function _do_composite(cmd_buffer, cmd) {
overlay_pass.end()
}
return sdl_gpu
return sdl_gpu