Skip to content

Commit

Permalink
AA plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Sep 22, 2023
1 parent 5fa70a7 commit fa9841a
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 79 deletions.
4 changes: 4 additions & 0 deletions data/pipelines/lumix.d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ declare function cull(cp : CameraParams, ... : any) : any
declare function dispatch(shader : Shader, x : number, y : number, z : number) : ()
declare function drawArray(indices_offset : number, indices_count : number, shader : Shader, textures : {RenderBuffer}, rs : any, define : string?) : ()
declare function drawcallUniforms(...: number) : ()
declare function enablePixelJitter(enable : boolean) : ()
declare function endBlock() : ()
declare function environmentCastShadows() : boolean
declare function getCameraParams() : CameraParams
Expand All @@ -45,6 +46,7 @@ declare function keepRenderbufferAlive(rb : RenderBuffer) : ()
declare function pass(cp : CameraParams) : ()
declare function preloadShader(path: string) : Shader
declare function render2D() : ()
declare function renderAA(color : RenderBuffer, velocity : RenderBuffer, depth : RenderBuffer, output : RenderBuffer) : boolean
declare function renderBucket(bucket : Bucket) : ()
declare function renderDebugShapes() : ()
declare function renderGizmos() : ()
Expand Down Expand Up @@ -82,6 +84,8 @@ declare CLEAR_DEPTH : number
declare CLEAR_COLOR : number
declare viewport_w : number
declare viewport_h : number
declare display_w : number
declare display_h : number
declare Renderer : any
declare enabled : boolean
declare fsr2Dispatch : any
41 changes: 17 additions & 24 deletions data/pipelines/main.pln
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ local taa_history = -1
local render_grass = true
local render_impostors = true
local render_terrain = true
local fsr2_enable = false

type GBuffer = {
A : RenderBuffer,
Expand Down Expand Up @@ -62,8 +61,9 @@ 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, display_size = true }
local taa_desc = createRenderbufferDesc { format = "rgba16", debug_name = "taa_tmp", compute_write = true, display_size = true }
local taa_history_desc = createRenderbufferDesc { format = "rgba16", debug_name = "taa_history", compute_write = true, display_size = true }
local taa_tmp_desc = createRenderbufferDesc { format = "rgba16", debug_name = "taa_tmp", compute_write = true, display_size = true }
local taa_desc = createRenderbufferDesc { format = "rgba16", debug_name = "taa", 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 @@ -465,26 +465,26 @@ local function render_preview()
setOutput(output)
end

local function TAA(res, gbuffer)
local function TAA(hdr_buffer : RenderBuffer, velocity : RenderBuffer, depth : RenderBuffer, output : RenderBuffer)
enablePixelJitter(taa_enabled);
if taa_enabled then
PIXEL_JITTER = true
beginBlock("taa")
if taa_history == -1 then
taa_history = createRenderbuffer(taa_history_desc)
setRenderTargets(taa_history)
clear(CLEAR_ALL, 1, 1, 1, 1, 0)
end

local taa_tmp = createRenderbuffer(taa_desc)

setRenderTargets()

local taa_tmp = createRenderbuffer(taa_tmp_desc)

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

setRenderTargets(res)
setRenderTargets(output)
drawcallUniforms(
0, 0, 1, 1,
1, 0, 0, 0,
Expand All @@ -494,20 +494,18 @@ local function TAA(res, gbuffer)
0, 0, 0, 0
)

-- TODO get rid of this step
-- TODO textured_quad_shader does unnecessary computations
drawArray(0, 3, textured_quad_shader
, { taa_tmp }
, empty_state)

taa_history = taa_tmp

keepRenderbufferAlive(taa_history)
endBlock()
end
end

main = function()
PIXEL_JITTER = false
if PREVIEW then
render_preview()
return
Expand All @@ -533,12 +531,14 @@ main = function()
cubemap_sky:postprocess(getfenv(1), hdr_buffer, gbuffer, shadowmap)
atmo:postprocess(getfenv(1), hdr_buffer, gbuffer, shadowmap)

local res
if fsr2Dispatch and fsr2_enable then
PIXEL_JITTER = true
res = createRenderbuffer(taa_desc)
fsr2Dispatch(hdr_buffer, gbuffer.DS, gbuffer.D, res)
local res
if taa_enabled then
res = createRenderbuffer(taa_desc)
if not renderAA(hdr_buffer, gbuffer.D, gbuffer.DS, res) then
TAA(hdr_buffer, gbuffer.D, gbuffer.DS, res)
end
else
enablePixelJitter(false)
res = hdr_buffer
end

Expand Down Expand Up @@ -587,10 +587,6 @@ main = function()
end
end

if not fsr2_enable then
TAA(res, gbuffer)
end

render2D()

if APP ~= nil then
Expand Down Expand Up @@ -659,9 +655,6 @@ onGUI = function()
_, debug_shadow_buf = ImGui.Checkbox("GBuffer shadow", debug_shadow_buf)
_, debug_clusters = ImGui.Checkbox("Clusters", debug_clusters)
_, taa_enabled = ImGui.Checkbox("TAA", taa_enabled)
if fsr2Dispatch then
_, fsr2_enable = ImGui.Checkbox("FSR2", fsr2_enable)
end
_, render_grass = ImGui.Checkbox("Grass", render_grass)
_, render_impostors = ImGui.Checkbox("Impostors", render_impostors)
_, render_terrain = ImGui.Checkbox("Terrain", render_terrain)
Expand Down
23 changes: 19 additions & 4 deletions src/renderer/draw_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ enum class DrawStream::Instruction : u8 {
SUBSTREAM,
BEGIN_PROFILE_BLOCK,
END_PROFILE_BLOCK,
USER_ALLOC
USER_ALLOC,
SET_TEXTURE_DEBUG_NAME
};

namespace {
Expand Down Expand Up @@ -365,6 +366,15 @@ void DrawStream::createTexture(gpu::TextureHandle handle, u32 w, u32 h, u32 dept
WRITE_ARRAY(debug_name, len);
}

void DrawStream::setDebugName(gpu::TextureHandle texture, const char* debug_name) {
const u32 len = stringLength(debug_name) + 1;
u8* data = alloc(sizeof(Instruction) + sizeof(texture) + len + sizeof(len));
WRITE_CONST(Instruction::SET_TEXTURE_DEBUG_NAME);
WRITE(texture);
WRITE(len);
WRITE_ARRAY(debug_name, len);
}

DrawStream& DrawStream::createSubstream() {
u8* data = alloc(sizeof(Instruction) + sizeof(DrawStream));
WRITE_CONST(Instruction::SUBSTREAM);
Expand Down Expand Up @@ -835,6 +845,14 @@ void DrawStream::run() {
gpu::scissor(vec.x, vec.y, vec.z, vec.w);
break;
}
case Instruction::SET_TEXTURE_DEBUG_NAME: {
READ(gpu::TextureHandle, texture);
READ(u32, len);
const char* debug_name = (const char*)ptr;
ptr += len;
gpu::setDebugName(texture, debug_name);
break;
}
case Instruction::CREATE_TEXTURE: {
READ(CreateTextureData, data);
READ(u32, len);
Expand Down Expand Up @@ -961,9 +979,6 @@ void DrawStream::run() {
gpu::viewport(vec.x, vec.y, vec.z, vec.w);
break;
}
default:
ASSERT(false);
goto next_page;
}
}
next_page:
Expand Down
1 change: 1 addition & 0 deletions src/renderer/draw_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct DrawStream {

void readTexture(gpu::TextureHandle texture, u32 mip, Span<u8> buf);
void generateMipmaps(gpu::TextureHandle texture);
void setDebugName(gpu::TextureHandle texture, const char* debug_name);

void update(gpu::TextureHandle texture, u32 mip, u32 x, u32 y, u32 z, u32 w, u32 h, gpu::TextureFormat format, const void* buf, u32 size);
void update(gpu::BufferHandle buffer, const void* data, size_t size);
Expand Down
1 change: 1 addition & 0 deletions src/renderer/gpu/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ void copy(BufferHandle dst, BufferHandle src, u32 dst_offset, u32 src_offset, u3

void readTexture(TextureHandle texture, u32 mip, Span<u8> buf);
void generateMipmaps(TextureHandle texture);
void setDebugName(TextureHandle texture, const char* debug_name);

void update(TextureHandle texture, u32 mip, u32 x, u32 y, u32 z, u32 w, u32 h, TextureFormat format, const void* buf, u32 size);
void update(BufferHandle buffer, const void* data, size_t size);
Expand Down
Loading

0 comments on commit fa9841a

Please sign in to comment.