diff --git a/data/pipelines/lumix.d.lua b/data/pipelines/lumix.d.lua index b1960e6663..fbcd02043d 100644 --- a/data/pipelines/lumix.d.lua +++ b/data/pipelines/lumix.d.lua @@ -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 @@ -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() : () @@ -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 \ No newline at end of file diff --git a/data/pipelines/main.pln b/data/pipelines/main.pln index f5f9467192..844b7f117c 100644 --- a/data/pipelines/main.pln +++ b/data/pipelines/main.pln @@ -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, @@ -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" } @@ -465,9 +465,9 @@ 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) @@ -475,16 +475,16 @@ local function TAA(res, gbuffer) 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, @@ -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 @@ -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 @@ -587,10 +587,6 @@ main = function() end end - if not fsr2_enable then - TAA(res, gbuffer) - end - render2D() if APP ~= nil then @@ -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) diff --git a/src/renderer/draw_stream.cpp b/src/renderer/draw_stream.cpp index f4c2942deb..8ffa8e6474 100644 --- a/src/renderer/draw_stream.cpp +++ b/src/renderer/draw_stream.cpp @@ -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 { @@ -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); @@ -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); @@ -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: diff --git a/src/renderer/draw_stream.h b/src/renderer/draw_stream.h index c43a3d8cb5..3d53ad3569 100644 --- a/src/renderer/draw_stream.h +++ b/src/renderer/draw_stream.h @@ -63,6 +63,7 @@ struct DrawStream { void readTexture(gpu::TextureHandle texture, u32 mip, Span 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); diff --git a/src/renderer/gpu/gpu.h b/src/renderer/gpu/gpu.h index a65e34e179..2865f54380 100644 --- a/src/renderer/gpu/gpu.h +++ b/src/renderer/gpu/gpu.h @@ -328,6 +328,7 @@ void copy(BufferHandle dst, BufferHandle src, u32 dst_offset, u32 src_offset, u3 void readTexture(TextureHandle texture, u32 mip, Span 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); diff --git a/src/renderer/pipeline.cpp b/src/renderer/pipeline.cpp index 2c484dbd67..b11434f92d 100644 --- a/src/renderer/pipeline.cpp +++ b/src/renderer/pipeline.cpp @@ -34,10 +34,6 @@ #include "terrain.h" #include "texture.h" -#ifdef LUMIX_FSR2 - #include "../plugins/dx/src/fsr2.h" -#endif - namespace Lumix { @@ -790,6 +786,10 @@ struct PipelineImpl final : Pipeline ~PipelineImpl() { + for (RenderPlugin* plugin : m_renderer.getPlugins()) { + plugin->pipelineDestroyed(*this); + } + DrawStream& stream = m_renderer.getEndFrameDrawStream(); for (gpu::TextureHandle t : m_textures) stream.destroy(t); for (gpu::BufferHandle b : m_buffers) stream.destroy(b); @@ -1192,11 +1192,9 @@ struct PipelineImpl final : Pipeline m_renderer.waitCanSetup(); clearBuffers(); - bool pixel_jitter = false; - LuaWrapper::getOptionalField(m_lua_state, -1, "PIXEL_JITTER", &pixel_jitter); m_viewport.pixel_offset = Vec2(0); - if (pixel_jitter) { + if (m_is_pixel_jitter_enabled) { m_viewport.pixel_offset.x = (halton(m_renderer.frameNumber() % 8 + 1, 2) * 2 - 1) / m_viewport.w; m_viewport.pixel_offset.y = (halton(m_renderer.frameNumber() % 8 + 1, 3) * 2 - 1) / m_viewport.h; } @@ -1537,7 +1535,8 @@ struct PipelineImpl final : Pipeline for (Renderbuffer& rb : m_renderbuffers) { if (!rb.handle) { rb.frame_counter = 0; - rb.handle = m_renderer.createTexture(size.x, size.y, 1, desc.format, desc.flags, Renderer::MemRef(), desc.debug_name); + StaticString<128> name(desc.debug_name, " ", u32(&rb - m_renderbuffers.begin())); + rb.handle = m_renderer.createTexture(size.x, size.y, 1, desc.format, desc.flags, Renderer::MemRef(), name); rb.flags = desc.flags; rb.format = desc.format; rb.size = size; @@ -1552,6 +1551,8 @@ struct PipelineImpl final : Pipeline } rb.frame_counter = 0; res.renderbuffer = u32(&rb - m_renderbuffers.begin()); + StaticString<128> name(desc.debug_name, " ", u32(&rb - m_renderbuffers.begin())); + m_renderer.getDrawStream().setDebugName(rb.handle, name); return res; } @@ -2212,6 +2213,18 @@ struct PipelineImpl final : Pipeline } } + bool renderAA(PipelineTexture color, PipelineTexture velocity, PipelineTexture depth, PipelineTexture output) { + PROFILE_FUNCTION(); + for (RenderPlugin* plugin : m_renderer.getPlugins()) { + if (plugin->renderAA(*this, toHandle(color), toHandle(velocity), toHandle(depth), toHandle(output))) return true; + } + return false; + } + + void enablePixelJitter(bool enable) override { + m_is_pixel_jitter_enabled = enable; + } + Matrix getShadowMatrix(const PointLight& light, u32 atlas_idx) { Matrix prj; prj.setPerspective(light.fov, 1, 0.1f, light.range, true); @@ -3429,14 +3442,14 @@ struct PipelineImpl final : Pipeline CameraParamsHandle getShadowCameraParams(i32 slice) { return (CameraParamsHandle)CameraParamsEnum::SHADOW0 + slice; } - void setRenderTargets(Span renderbuffers, gpu::TextureHandle ds, bool readonly_ds, bool srgb) { + void setRenderTargets(Span renderbuffers, gpu::TextureHandle ds, bool readonly_ds, bool srgb, IVec2 viewport_size) { gpu::FramebufferFlags flags = srgb ? gpu::FramebufferFlags::SRGB : gpu::FramebufferFlags::NONE; if (readonly_ds) { flags = flags | gpu::FramebufferFlags::READONLY_DEPTH_STENCIL; } DrawStream& stream = m_renderer.getDrawStream(); stream.setFramebuffer(renderbuffers.begin(), renderbuffers.length(), ds, flags); - stream.viewport(0, 0, m_viewport.w, m_viewport.h); + stream.viewport(0, 0, viewport_size.x, viewport_size.y); } static int setRenderTargets(lua_State* L, bool has_ds, bool readonly_ds) { @@ -3450,9 +3463,14 @@ struct PipelineImpl final : Pipeline return 0; } + IVec2 size; + size.x = pipeline->m_viewport.w; + size.y = pipeline->m_viewport.h; + for(u32 i = 0; i < rb_count; ++i) { const i32 rb_idx = pipeline->toRenderbufferIdx(L, i + 1); rbs[i] = pipeline->m_renderbuffers[rb_idx].handle; + size = pipeline->m_renderbuffers[rb_idx].size; } gpu::TextureHandle ds = gpu::INVALID_TEXTURE; @@ -3461,7 +3479,7 @@ struct PipelineImpl final : Pipeline ds = pipeline->m_renderbuffers[ds_idx].handle; } - pipeline->setRenderTargets(Span(rbs, rb_count), ds, readonly_ds, true); + pipeline->setRenderTargets(Span(rbs, rb_count), ds, readonly_ds, true, size); return 0; } @@ -3942,38 +3960,6 @@ struct PipelineImpl final : Pipeline 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(); - gpu::FSR2DispatchParams params; - params.jitter_x = m_viewport.pixel_offset.x; - params.jitter_y = m_viewport.pixel_offset.y; - params.time_delta = m_renderer.getEngine().getLastTimeDelta() * 1000; - params.render_width = m_viewport.w; - params.render_height = m_viewport.h; - params.near_plane = m_viewport.near; - params.fov = m_viewport.fov; - params.color = toHandle(color); - params.depth = toHandle(depth); - params.motion_vectors = toHandle(motion_vectors); - params.output = toHandle(output); - beginBlock("FSR2"); - stream.pushLambda([this, params](){ - 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_display_size.x, m_display_size.y, m_allocator); - m_fsr2_size = s; - } - gpu::fsr2Dispatch(*m_fsr2, params); - }); - endBlock(); - } - #endif - void registerLuaAPI(lua_State* L) { lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env); @@ -4010,12 +3996,10 @@ struct PipelineImpl final : Pipeline REGISTER_FUNCTION(createTexture3D); REGISTER_FUNCTION(dispatch); REGISTER_FUNCTION(drawArray); + REGISTER_FUNCTION(enablePixelJitter); REGISTER_FUNCTION(endBlock); REGISTER_FUNCTION(environmentCastShadows); REGISTER_FUNCTION(executeCustomCommand); - #ifdef LUMIX_FSR2 - REGISTER_FUNCTION(fsr2Dispatch); - #endif REGISTER_FUNCTION(getCameraParams); REGISTER_FUNCTION(getShadowCameraParams); REGISTER_FUNCTION(getRenderToDisplayRatio); @@ -4030,6 +4014,7 @@ struct PipelineImpl final : Pipeline REGISTER_FUNCTION(renderTerrains); REGISTER_FUNCTION(renderOpaque); REGISTER_FUNCTION(renderTransparent); + REGISTER_FUNCTION(renderAA); REGISTER_FUNCTION(renderUI); REGISTER_FUNCTION(saveRenderbuffer); REGISTER_FUNCTION(setOutput); @@ -4098,6 +4083,8 @@ struct PipelineImpl final : Pipeline } void setIndirectLightMultiplier(float value) override { m_indirect_light_multiplier = value; } + + IVec2 getDisplaySize() const override { return m_display_size; } IAllocator& m_allocator; Renderer& m_renderer; @@ -4113,6 +4100,7 @@ struct PipelineImpl final : Pipeline Array> m_views; jobs::Signal m_buckets_ready; Viewport m_viewport; + bool m_is_pixel_jitter_enabled = false; Viewport m_prev_viewport; IVec2 m_display_size; float m_render_to_display_scale = 1; @@ -4153,10 +4141,6 @@ struct PipelineImpl final : Pipeline Buffer refl_probes; } m_cluster_buffers; Viewport m_shadow_camera_viewports[4]; - #ifdef LUMIX_FSR2 - Lumix::gpu::FSR2Context* m_fsr2 = nullptr; - IVec2 m_fsr2_size = IVec2(0); - #endif }; diff --git a/src/renderer/pipeline.h b/src/renderer/pipeline.h index ab14006cf9..aac9ad4bcf 100644 --- a/src/renderer/pipeline.h +++ b/src/renderer/pipeline.h @@ -81,6 +81,8 @@ struct LUMIX_RENDERER_API Pipeline { virtual Viewport getViewport() = 0; virtual void define(const char* define, bool enable) = 0; virtual void setIndirectLightMultiplier(float value) = 0; + virtual IVec2 getDisplaySize() const = 0; + virtual void enablePixelJitter(bool enable) = 0; virtual Draw2D& getDraw2D() = 0; virtual void clearDraw2D() = 0; diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 7c1dff926e..a37f5ff7b2 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -28,7 +28,6 @@ #include "renderer/terrain.h" #include "renderer/texture.h" - namespace Lumix { diff --git a/src/renderer/renderer.h b/src/renderer/renderer.h index fa67938346..cce7015f66 100644 --- a/src/renderer/renderer.h +++ b/src/renderer/renderer.h @@ -16,9 +16,14 @@ namespace Lumix { enum class AttributeSemantic : u8; struct RenderPlugin { + virtual ~RenderPlugin() {} + // all `RenderPlugin` functions are called during execution of every `Pipeline` virtual void renderUI(struct Pipeline& pipeline) {} virtual void renderOpaque(Pipeline& pipeline) {} virtual void renderTransparent(Pipeline& pipeline) {} + // returns true if AA run and builtin TAA should not run + virtual bool renderAA(Pipeline& pipeline, gpu::TextureHandle color, gpu::TextureHandle velocity, gpu::TextureHandle depth, gpu::TextureHandle output) { return false; } + virtual void pipelineDestroyed(Pipeline& pipeline) {} }; struct DrawStream;