2019-09-26 22:48:35 +02:00
|
|
|
radius = 0.2
|
|
|
|
intensity = 1
|
2021-10-03 01:32:55 +02:00
|
|
|
use_temporal = false
|
2022-05-08 09:39:44 +02:00
|
|
|
blur = false
|
|
|
|
current_frame_weight = 0.05
|
2021-10-03 01:32:55 +02:00
|
|
|
local history_buf = -1
|
2019-09-26 22:48:35 +02:00
|
|
|
|
2023-09-14 16:48:57 +02:00
|
|
|
function postprocess(env, phase, hdr_buffer, gbuffer0, gbuffer1, gbuffer2, gbuffer3, gbuffer_depth, shadowmap)
|
2019-09-26 22:48:35 +02:00
|
|
|
if not enabled then return hdr_buffer end
|
2021-02-22 21:19:03 +01:00
|
|
|
if phase ~= "pre_lightpass" then return hdr_buffer end
|
2021-10-03 01:32:55 +02:00
|
|
|
|
|
|
|
env.ssao_shader = env.ssao_shader or env.preloadShader("pipelines/ssao.shd")
|
|
|
|
env.blur_shader = env.blur_shader or env.preloadShader("pipelines/blur.shd")
|
|
|
|
env.ssao_blit_shader = env.ssao_blit_shader or env.preloadShader("pipelines/ssao_blit.shd")
|
|
|
|
env.ssao_resolve_shader = env.ssao_resolve_shader or env.preloadShader("pipelines/ssao_resolve.shd")
|
2022-11-20 14:47:55 +01:00
|
|
|
env.ssao_rb_desc = env.ssao_rb_desc or env.createRenderbufferDesc { rel_size = {0.5, 0.5}, format = "r8", debug_name = "ssao", compute_write = true }
|
2021-10-03 01:32:55 +02:00
|
|
|
|
2021-10-04 22:46:51 +02:00
|
|
|
local w = math.floor(env.viewport_w * 0.5)
|
|
|
|
local h = math.floor(env.viewport_h * 0.5)
|
2022-11-20 14:47:55 +01:00
|
|
|
local ssao_rb = env.createRenderbuffer(env.ssao_rb_desc)
|
2021-10-03 01:32:55 +02:00
|
|
|
|
|
|
|
if use_temporal and history_buf == -1 then
|
2022-11-20 14:47:55 +01:00
|
|
|
history_buf = env.createRenderbuffer(env.ssao_rb_desc)
|
2021-10-03 01:32:55 +02:00
|
|
|
env.setRenderTargets(history_buf)
|
|
|
|
env.clear(env.CLEAR_ALL, 1, 1, 1, 1, 0)
|
|
|
|
end
|
|
|
|
|
2021-02-22 21:19:03 +01:00
|
|
|
env.setRenderTargets()
|
|
|
|
|
2021-10-03 01:32:55 +02:00
|
|
|
env.beginBlock("ssao " .. tostring(w) .. "x" .. tostring(h))
|
|
|
|
env.drawcallUniforms(radius, intensity, w, h)
|
|
|
|
env.bindTextures({gbuffer_depth, gbuffer1}, 0)
|
|
|
|
env.bindImageTexture(ssao_rb, 2)
|
|
|
|
env.dispatch(env.ssao_shader, (w + 15) / 16, (h + 15) / 16, 1)
|
|
|
|
|
2022-11-20 14:47:55 +01:00
|
|
|
env.ssao_blur_rb_desc = env.ssao_blur_rb_desc or env.createRenderbufferDesc { format = "r8", rel_size = {0.5, 0.5}, debug_name = "ssao_blur" }
|
2021-10-03 01:32:55 +02:00
|
|
|
if use_temporal then
|
|
|
|
env.beginBlock("ssao_resolve " .. tostring(w) .. "x" .. tostring(h))
|
2022-05-08 09:39:44 +02:00
|
|
|
env.drawcallUniforms( w, h, 0, 0, current_frame_weight, 0, 0, 0 )
|
2021-10-04 22:31:00 +02:00
|
|
|
env.bindTextures({gbuffer_depth, history_buf}, 0)
|
|
|
|
env.bindImageTexture(ssao_rb, 2)
|
2021-10-03 01:32:55 +02:00
|
|
|
env.dispatch(env.ssao_resolve_shader, (w + 15) / 16, (h + 15) / 16, 1)
|
|
|
|
env.endBlock()
|
2022-05-08 09:39:44 +02:00
|
|
|
if blur then
|
2022-11-20 14:47:55 +01:00
|
|
|
env.blur(ssao_rb, w, h, env.ssao_blur_rb_desc)
|
2022-05-08 09:39:44 +02:00
|
|
|
end
|
2021-10-03 01:32:55 +02:00
|
|
|
else
|
2022-05-08 09:39:44 +02:00
|
|
|
if blur then
|
2022-11-20 14:47:55 +01:00
|
|
|
env.blur(ssao_rb, w, h, env.ssao_blur_rb_desc)
|
2022-05-08 09:39:44 +02:00
|
|
|
end
|
2021-10-03 01:32:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
env.beginBlock("ssao_blit " .. tostring(env.viewport_w) .. "x" .. tostring(env.viewport_h))
|
2021-02-22 21:19:03 +01:00
|
|
|
env.drawcallUniforms( env.viewport_w, env.viewport_h, 0, 0 )
|
|
|
|
env.bindTextures({ssao_rb}, 0)
|
2022-05-05 22:59:45 +02:00
|
|
|
env.bindImageTexture(gbuffer1, 1)
|
2021-02-22 21:19:03 +01:00
|
|
|
env.dispatch(env.ssao_blit_shader, (env.viewport_w + 15) / 16, (env.viewport_h + 15) / 16, 1)
|
2019-09-26 22:48:35 +02:00
|
|
|
env.endBlock()
|
2021-10-04 22:31:00 +02:00
|
|
|
env.endBlock()
|
2021-10-03 01:32:55 +02:00
|
|
|
|
|
|
|
if use_temporal then
|
|
|
|
history_buf = ssao_rb
|
|
|
|
env.keepRenderbufferAlive(history_buf)
|
|
|
|
end
|
2019-09-26 22:48:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function awake()
|
2020-04-07 20:07:00 +02:00
|
|
|
_G["postprocesses"] = _G["postprocesses"] or {}
|
|
|
|
_G["postprocesses"]["ssao"] = postprocess
|
2019-09-26 22:48:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function onDestroy()
|
2020-04-07 20:07:00 +02:00
|
|
|
_G["postprocesses"]["ssao"] = nil
|
2021-04-12 20:02:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function onUnload()
|
|
|
|
onDestroy()
|
|
|
|
end
|