Skip to content

Commit

Permalink
pipeline upsampling
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Sep 22, 2023
1 parent ef3e2c6 commit 5fa70a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
5 changes: 4 additions & 1 deletion data/pipelines/lumix.d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ declare function drawcallUniforms(...: number) : ()
declare function endBlock() : ()
declare function environmentCastShadows() : boolean
declare function getCameraParams() : CameraParams
declare function getRenderToDisplayRatio() : number
declare function getShadowCameraParams(slice : number) : CameraParams
declare function keepRenderbufferAlive(rb : RenderBuffer) : ()
declare function pass(cp : CameraParams) : ()
Expand All @@ -59,6 +60,7 @@ declare function setOutput(rb : RenderBuffer) : ()
declare function setRenderTargets(...: RenderBuffer) : ()
declare function setRenderTargetsDS(...: RenderBuffer) : ()
declare function setRenderTargetsReadonlyDS(...: RenderBuffer) : ()
declare function setRenderToDisplayRatio(ratio: number) : ()
declare function viewport(x : number, y : number, w : number, h : number) : ()


Expand All @@ -81,4 +83,5 @@ declare CLEAR_COLOR : number
declare viewport_w : number
declare viewport_h : number
declare Renderer : any
declare enabled : boolean
declare enabled : boolean
declare fsr2Dispatch : any
17 changes: 12 additions & 5 deletions data/pipelines/main.pln
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ local shadowmap_1x1 = createRenderbufferDesc { size = {1, 1}, format = "depth32"
local shadowmap_desc = createRenderbufferDesc { size = { 4096, 1024 }, format = "depth32", debug_name = "shadowmap" }
local selection_mask_desc = createRenderbufferDesc { format = "depth32", debug_name = "selection_depthbuffer" }
local baked_shadowmap_desc = createRenderbufferDesc { format = "depth32", debug_name = "shadowmap_depth" }
local taa_history_desc = createRenderbufferDesc { format = "rgba16", debug_name = "taa", compute_write = true }
local taa_desc = createRenderbufferDesc { format = "rgba16", debug_name = "taa_tmp", compute_write = true }
local taa_history_desc = createRenderbufferDesc { format = "rgba16", debug_name = "taa", compute_write = true, display_size = true }
local taa_desc = createRenderbufferDesc { format = "rgba16", debug_name = "taa_tmp", compute_write = true, display_size = true }
local icon_ds_desc = createRenderbufferDesc { format = "depth24stencil8", debug_name = "icon_ds" }
local water_color_copy_desc = createRenderbufferDesc { format = "r11g11b10f", debug_name = "hdr_copy" }
local hdr_rb_desc = createRenderbufferDesc { format = if PROBE ~= nil then "rgba32f" else "rgba16f", debug_name = "hdr" }
Expand Down Expand Up @@ -479,10 +479,10 @@ local function TAA(res, gbuffer)

setRenderTargets()

drawcallUniforms(viewport_w, viewport_h, 0, 0)
drawcallUniforms(display_w, display_h, 0, 0)
bindTextures({taa_history, res, gbuffer.D}, 0)
bindImageTexture(taa_tmp, 3)
dispatch(taa_shader, (viewport_w + 15) / 16, (viewport_h + 15) / 16, 1)
dispatch(taa_shader, (display_w + 15) / 16, (display_h + 15) / 16, 1)

setRenderTargets(res)
drawcallUniforms(
Expand Down Expand Up @@ -666,7 +666,14 @@ onGUI = function()
_, render_impostors = ImGui.Checkbox("Impostors", render_impostors)
_, render_terrain = ImGui.Checkbox("Terrain", render_terrain)
_, enable_icons = ImGui.Checkbox("Icons", enable_icons)
local changed, lod_mul = ImGui.DragFloat("LOD multiplier", Renderer.getLODMultiplier())
local changed, upsample = ImGui.DragFloat("Downsample", getRenderToDisplayRatio())
if changed then
if upsample < 1 then upsample = 1 end
setRenderToDisplayRatio(upsample)
end

local lod_mul
changed, lod_mul = ImGui.DragFloat("LOD multiplier", Renderer.getLODMultiplier())
if changed then
Renderer.setLODMultiplier(lod_mul)
end
Expand Down
43 changes: 36 additions & 7 deletions src/renderer/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ struct PipelineImpl final : Pipeline
struct RenderbufferDesc {
enum Type {
FIXED,
RELATIVE
RELATIVE,
DISPLAY_SIZE
};
Type type = FIXED;
IVec2 fixed_size;
Expand Down Expand Up @@ -985,6 +986,10 @@ struct PipelineImpl final : Pipeline
void setViewport(const Viewport& viewport) override
{
m_viewport = viewport;
m_display_size.x = m_viewport.w;
m_display_size.y = m_viewport.h;
m_viewport.w = i32(m_viewport.w / m_render_to_display_scale);
m_viewport.h = i32(m_viewport.h / m_render_to_display_scale);
if (m_first_set_viewport) {
m_prev_viewport = viewport;
m_first_set_viewport = false;
Expand Down Expand Up @@ -1112,6 +1117,8 @@ struct PipelineImpl final : Pipeline
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
LuaWrapper::setField(m_lua_state, -1, "viewport_w", m_viewport.w);
LuaWrapper::setField(m_lua_state, -1, "viewport_h", m_viewport.h);
LuaWrapper::setField(m_lua_state, -1, "display_w", m_display_size.x);
LuaWrapper::setField(m_lua_state, -1, "display_h", m_display_size.y);
lua_getfield(m_lua_state, -1, "main_shadowmap");
if (lua_type(m_lua_state, -1) != LUA_TFUNCTION) {
lua_pop(m_lua_state, 2);
Expand Down Expand Up @@ -1139,6 +1146,8 @@ struct PipelineImpl final : Pipeline
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
LuaWrapper::setField(m_lua_state, -1, "viewport_w", m_viewport.w);
LuaWrapper::setField(m_lua_state, -1, "viewport_h", m_viewport.h);
LuaWrapper::setField(m_lua_state, -1, "display_w", m_display_size.x);
LuaWrapper::setField(m_lua_state, -1, "display_h", m_display_size.y);
lua_pop(m_lua_state, 1);
return true;
}
Expand Down Expand Up @@ -1259,6 +1268,8 @@ struct PipelineImpl final : Pipeline
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
LuaWrapper::setField(m_lua_state, -1, "viewport_w", m_viewport.w);
LuaWrapper::setField(m_lua_state, -1, "viewport_h", m_viewport.h);
LuaWrapper::setField(m_lua_state, -1, "display_w", m_display_size.x);
LuaWrapper::setField(m_lua_state, -1, "display_h", m_display_size.y);
lua_getfield(m_lua_state, -1, "main");
bool has_main = true;
if (lua_type(m_lua_state, -1) != LUA_TFUNCTION) {
Expand Down Expand Up @@ -1517,7 +1528,12 @@ struct PipelineImpl final : Pipeline
res.type = PipelineTexture::RENDERBUFFER;

const RenderbufferDesc& desc = m_renderbuffer_descs[desc_handle];
const IVec2 size = desc.type == RenderbufferDesc::FIXED ? desc.fixed_size : IVec2(i32(desc.rel_size.x * m_viewport.w), i32(desc.rel_size.y * m_viewport.h));
IVec2 size;
switch (desc.type) {
case RenderbufferDesc::FIXED: size = desc.fixed_size; break;
case RenderbufferDesc::RELATIVE: size = IVec2(i32(desc.rel_size.x * m_viewport.w), i32(desc.rel_size.y * m_viewport.h)); break;
case RenderbufferDesc::DISPLAY_SIZE: size = m_display_size; break;
}
for (Renderbuffer& rb : m_renderbuffers) {
if (!rb.handle) {
rb.frame_counter = 0;
Expand Down Expand Up @@ -1561,10 +1577,16 @@ struct PipelineImpl final : Pipeline
bool compute_write = false;
RenderbufferDesc::Type type = RenderbufferDesc::FIXED;
if (!LuaWrapper::getOptionalField(L, 1, "size", &fixed_size)) {
type = RenderbufferDesc::RELATIVE;
if (!LuaWrapper::getOptionalField(L, 1, "rel_size", &rel_size)) {
rel_size = Vec2(1, 1);
bool display_size = false;
if (LuaWrapper::getOptionalField(L, 1, "display_size", &display_size) && display_size) {
type = RenderbufferDesc::DISPLAY_SIZE;
}
else {
rel_size = Vec2(1, 1);
}
}
type = RenderbufferDesc::RELATIVE;
}
else {
type = RenderbufferDesc::FIXED;
Expand Down Expand Up @@ -3917,9 +3939,12 @@ struct PipelineImpl final : Pipeline
stream.freeMemory(mem.data, m_renderer.getAllocator());
}

float getRenderToDisplayRatio() const { return m_render_to_display_scale; }
void setRenderToDisplayRatio(float scale) { m_render_to_display_scale = scale; }

#ifdef LUMIX_FSR2
void fsr2Dispatch(PipelineTexture color, PipelineTexture depth, PipelineTexture motion_vectors, PipelineTexture output) {
DrawStream& stream = m_renderer.getDrawStream();
DrawStream& stream = m_renderer.getDrawStream();
gpu::FSR2DispatchParams params;
params.jitter_x = m_viewport.pixel_offset.x;
params.jitter_y = m_viewport.pixel_offset.y;
Expand All @@ -3934,13 +3959,13 @@ struct PipelineImpl final : Pipeline
params.output = toHandle(output);
beginBlock("FSR2");
stream.pushLambda([this, params](){
IVec2 s(m_viewport.w, m_viewport.h);
IVec2 s(m_display_size.x, m_display_size.y);
if (m_fsr2 && m_fsr2_size != s) {
gpu::fsr2Shutdown(*m_fsr2);
m_fsr2 = nullptr;
}
if (!m_fsr2) {
m_fsr2 = gpu::fsr2Init(m_viewport.w, m_viewport.h, m_allocator);
m_fsr2 = gpu::fsr2Init(m_display_size.x, m_display_size.y, m_allocator);
m_fsr2_size = s;
}
gpu::fsr2Dispatch(*m_fsr2, params);
Expand Down Expand Up @@ -3993,6 +4018,7 @@ struct PipelineImpl final : Pipeline
#endif
REGISTER_FUNCTION(getCameraParams);
REGISTER_FUNCTION(getShadowCameraParams);
REGISTER_FUNCTION(getRenderToDisplayRatio);
REGISTER_FUNCTION(keepRenderbufferAlive);
REGISTER_FUNCTION(pass);
REGISTER_FUNCTION(preloadShader);
Expand All @@ -4007,6 +4033,7 @@ struct PipelineImpl final : Pipeline
REGISTER_FUNCTION(renderUI);
REGISTER_FUNCTION(saveRenderbuffer);
REGISTER_FUNCTION(setOutput);
REGISTER_FUNCTION(setRenderToDisplayRatio);
REGISTER_FUNCTION(viewport);

lua_pushinteger(L, -2); lua_setfield(L, -2, "SHADOW_ATLAS");
Expand Down Expand Up @@ -4087,6 +4114,8 @@ struct PipelineImpl final : Pipeline
jobs::Signal m_buckets_ready;
Viewport m_viewport;
Viewport m_prev_viewport;
IVec2 m_display_size;
float m_render_to_display_scale = 1;
float m_indirect_light_multiplier = 1;
bool m_first_set_viewport = true;
int m_output;
Expand Down

0 comments on commit 5fa70a7

Please sign in to comment.