Finally finished TAA

This commit is contained in:
antopilo
2024-09-25 22:37:31 -04:00
parent 31c8a2e598
commit 102ccc530b
11 changed files with 34952 additions and 415 deletions

View File

@@ -1279,6 +1279,33 @@ namespace Nuake {
}
END_COLLAPSE_HEADER()
BEGIN_COLLAPSE_HEADER(TAA)
if (ImGui::BeginTable("EnvTableTAA", 3, ImGuiTableFlags_BordersInner | ImGuiTableFlags_SizingStretchProp))
{
ImGui::TableSetupColumn("name", 0, 0.3f);
ImGui::TableSetupColumn("set", 0, 0.6f);
ImGui::TableSetupColumn("reset", 0, 0.1f);
ImGui::TableNextColumn();
{
auto& sceneRenderer = Engine::GetCurrentScene()->m_SceneRenderer;
// Title
ImGui::Text("TAA Factor");
ImGui::TableNextColumn();
ImGui::SliderFloat("##TAAFactor", &sceneRenderer->TAAFactor, 0.0f, 1.0f );
ImGui::TableNextColumn();
// Reset button
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0));
std::string resetVolumetric = ICON_FA_UNDO + std::string("##resetTAAFactor");
if (ImGui::Button(resetVolumetric.c_str())) sceneRenderer->TAAFactor = 0.6f;
ImGui::PopStyleColor();
}
ImGui::EndTable();
}
END_COLLAPSE_HEADER()
BEGIN_COLLAPSE_HEADER(BLOOM)
if (ImGui::BeginTable("BloomTable", 3, ImGuiTableFlags_BordersInner | ImGuiTableFlags_SizingStretchProp))
{

View File

@@ -67,7 +67,7 @@ namespace Nuake
Matrix4 Camera::GetPerspective()
{
//TODO: Add perspective options
m_Perspective = glm::perspectiveFov(glm::radians(Fov), 9.0f * AspectRatio, 9.0f, 0.001f, 500.0f);
m_Perspective = glm::perspectiveFov(glm::radians(Fov), 9.0f * AspectRatio, 9.0f, 0.001f, 400.0f);
return m_Perspective;
}

View File

@@ -43,7 +43,7 @@ namespace Nuake {
private:
static const int CSM_SPLIT_AMOUNT = 4;
const float CSM_NEAR_CLIP = 0.001f;
const float CSM_FAR_CLIP = 500.0f;
const float CSM_FAR_CLIP = 400.0f;
const float CSM_CLIP_RANGE = CSM_FAR_CLIP - CSM_NEAR_CLIP;
const float mCascadeNearPlaneOffset = 0.0;

View File

@@ -58,7 +58,7 @@ namespace Nuake
mGBuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RGBA), GL_COLOR_ATTACHMENT2); // Material + unlit
mGBuffer->SetTexture(entityTexture, GL_COLOR_ATTACHMENT3); // Entity ID
mGBuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RED, GL_R16F, GL_FLOAT), GL_COLOR_ATTACHMENT4); // Emissive
mGBuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RGB, GL_RGB16F, GL_FLOAT), GL_COLOR_ATTACHMENT5); // Velocity
mGBuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RG, GL_RG16F, GL_FLOAT), GL_COLOR_ATTACHMENT5); // Velocity
mShadingBuffer = CreateScope<FrameBuffer>(true, defaultResolution);
mShadingBuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RGB, GL_RGB16F, GL_FLOAT));
@@ -88,6 +88,9 @@ namespace Nuake
mDisplayMotionVector = CreateScope<FrameBuffer>(false, defaultResolution);
mDisplayMotionVector->SetTexture(CreateRef<Texture>(defaultResolution, GL_RGB), GL_COLOR_ATTACHMENT0);
mPreviousFrame = CreateScope<FrameBuffer>(false, defaultResolution);
mPreviousFrame->SetTexture(CreateRef<Texture>(defaultResolution, GL_RGBA), GL_COLOR_ATTACHMENT0);
// Generate debug meshes
std::vector<Vertex> lineVertices
{
@@ -180,17 +183,19 @@ namespace Nuake
if (framebufferResolution != GetGBuffer().GetSize())
{
UpdateJitterOffsets({ 1600, 1200 });
UpdateJitterOffsets(framebuffer.GetSize());
}
if (renderUI)
{
mGBuffer->QueueResize(framebufferResolution);
}
GBufferPass(scene);
if (framebuffer.GetSize() != GetGBuffer().GetSize())
if (mPreviousFrame->GetSize() != GetGBuffer().GetSize())
{
mPreviousFrame->QueueResize(framebufferResolution);
// UpdateJitterOffsets(framebuffer.GetSize());
}
@@ -218,7 +223,6 @@ namespace Nuake
if (!entity.IsValid())
continue;
Logger::Log("Setting previous pos of : " + entity.GetComponent<NameComponent>().Name);
if (mesh.ModelResource && visibility.Visible)
{
transform.PreviousTransform = mProjection * mView * transform.GetGlobalTransform();
@@ -246,7 +250,8 @@ namespace Nuake
{
mShadingBuffer->QueueResize(framebufferResolution);
}
ShadingPass(scene);
ShadingPass(scene, framebuffer.GetTexture(GL_COLOR_ATTACHMENT0));
// Blit depth buffer
glBindFramebuffer(GL_READ_FRAMEBUFFER, mGBuffer->GetRenderID());
@@ -333,11 +338,29 @@ namespace Nuake
shader->SetUniform("u_Exposure", sceneEnv->Exposure);
shader->SetUniform("u_Gamma", sceneEnv->Gamma);
Ref<Texture> previousTexture = mPreviousFrame->GetTexture(GL_COLOR_ATTACHMENT0);
previousTexture->SetMagnificationFilter(SamplerFilter::LINEAR);
previousTexture->SetMinificationFilter(SamplerFilter::LINEAR);
previousTexture->SetWrapping(SamplerWrapping::CLAMP_TO_EDGE);
shader->SetUniform("u_PreviousFrame", previousTexture.get(), 4);
shader->SetUniform("u_VelocityFrame", GetGBuffer().GetTexture(GL_COLOR_ATTACHMENT5).get(), 7);
shader->SetUniform("u_TAAFactor", TAAFactor);
shader->SetUniform("u_Source", finalOutput.get());
Renderer::DrawQuad();
}
mToneMapBuffer->Unbind();
mPreviousFrame->Bind();
{
RenderCommand::Clear();
Shader* shader = ShaderManager::GetShader("Resources/Shaders/copy.shader");
shader->Bind();
shader->SetUniform("u_Source", mToneMapBuffer->GetTexture(GL_COLOR_ATTACHMENT0).get(), 1);
Renderer::DrawQuad();
}
mPreviousFrame->Unbind();
ProjectSettings projectSettings = Engine::GetProject()->Settings;
mOutlineBuffer->QueueResize(framebufferResolution);
@@ -1126,7 +1149,7 @@ namespace Nuake
RenderCommand::Enable(RendererEnum::BLENDING);
}
void SceneRenderer::ShadingPass(Scene& scene)
void SceneRenderer::ShadingPass(Scene& scene, Ref<Texture> previousFrame)
{
ZoneScoped;
@@ -1159,6 +1182,8 @@ namespace Nuake
shadingShader->SetUniform("u_AmbientTerm", environment->AmbientTerm);
shadingShader->SetUniform("m_SSAO", scene.GetEnvironment()->mSSAO->GetOuput()->GetTexture().get(), 9);
Ref<Environment> env = scene.GetEnvironment();
struct LightDistance

View File

@@ -85,6 +85,8 @@ namespace Nuake
mTempModels.clear();
}
float TAAFactor = 0.6f;
int mOutlineEntityID = 0;
private:
Matrix4 mProjection, mView;
@@ -101,6 +103,7 @@ namespace Nuake
Scope<FrameBuffer> mDisplayDepthBuffer;
Scope<FrameBuffer> mDisplayMotionVector;
Scope<FrameBuffer> mTempFrameBuffer;
Scope<FrameBuffer> mPreviousFrame;
// Shapes
Ref<BoxGizmo> mBoxGizmo;
@@ -120,7 +123,7 @@ namespace Nuake
void ShadowPass(Scene& scene);
void GBufferPass(Scene& scene);
void ShadingPass(Scene& scene);
void ShadingPass(Scene& scene, Ref<Texture> previousFrame);
void PostProcessPass(const Scene& scene);
void DebugRendererPass(Scene& scene);

File diff suppressed because it is too large Load Diff

View File

@@ -8,6 +8,9 @@ namespace Nuake {
extern const std::string Resources_default_layout_ini_path;
extern unsigned int Resources_default_layout_ini_len;
extern unsigned char Resources_default_layout_ini[];
extern const std::string Resources_resources_aps_path;
extern unsigned int Resources_resources_aps_len;
extern unsigned char Resources_resources_aps[];
extern const std::string Resources_resources_rc_path;
extern unsigned int Resources_resources_rc_len;
extern unsigned char Resources_resources_rc[];

View File

@@ -73,6 +73,7 @@ uniform DirectionalLight u_DirectionalLight;
uniform int u_DisableSSAO = 0;
uniform float u_AmbientTerm;
uniform sampler2D u_PreviousFrame;
// Converts depth to World space coords.
vec3 WorldPosFromDepth(float depth) {

View File

@@ -103,7 +103,7 @@ float arrow(vec2 p, vec2 v) {
}
vec2 field(vec2 pos) {
return texture(u_Source, UV).xy * 2.0 - 1.0;
return texture(u_Source, UV).xy;
}
void main()

View File

@@ -1,6 +1,8 @@
#shader vertex
#version 440 core
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec2 UVPosition;
layout(location = 2) in vec3 Normal;
@@ -14,13 +16,13 @@ uniform mat4 u_PreviousViewModel;
uniform vec2 u_Jitter;
out vec4 FragPos;
out vec4 PreviousFragPos;
out mat3 TBN;
out vec2 UV;
out mat4 Inv_Proj;
out mat4 Inv_View;
out mat4 view;
out vec3 o_Tangent;
out vec4 PreviousFragPos;
void main()
{
@@ -41,15 +43,22 @@ void main()
o_Tangent = Tangent;
vec4 clipPosition = u_Projection * u_View * u_Model * vec4(VertexPosition, 1.0f);
FragPos = clipPosition;
gl_Position = clipPosition;
FragPos = clipPosition; // Push unjittered to not affect velocity buffer
// Now we can jitter.
vec4 newClipPosition = clipPosition + vec4(u_Jitter.x, u_Jitter.y, 0, 0) * clipPosition.w;
gl_Position = newClipPosition;
PreviousFragPos = u_PreviousViewModel * vec4(VertexPosition, 1.0f);
}
#shader fragment
#version 440 core
precision highp float;
layout(location = 0) out vec4 gAlbedo;
layout(location = 1) out vec4 gNormal;
layout(location = 2) out vec4 gMaterial;
@@ -151,6 +160,14 @@ void main()
gEntityID = int(u_EntityID);
gEmissive = u_Emissive;
vec4 positionDifference = FragPos - PreviousFragPos;
gVelocity = vec4((positionDifference.x + 1.0) / 2.0, (positionDifference.y + 1.0) / 2.0, 0.0f, 1);
vec4 fragPos = FragPos / FragPos.w;
fragPos.xy = (fragPos.xy + 1.0) / 2.0;
fragPos.y = 1.0 - fragPos.y;
vec4 previousFragPos = PreviousFragPos / PreviousFragPos.w;
previousFragPos.xy = (previousFragPos.xy + 1.0) / 2.0;
previousFragPos.y = 1.0 - previousFragPos.y;
vec2 positionDifference = (fragPos).xy - (previousFragPos).xy;
gVelocity = vec4(positionDifference.x, positionDifference.y, 0.0, 1.0);
}

View File

@@ -18,7 +18,9 @@ void main()
uniform sampler2D u_Source;
uniform float u_Gamma;
uniform float u_Exposure;
uniform float u_TAAFactor;
uniform sampler2D u_PreviousFrame;
uniform sampler2D u_VelocityFrame;
in vec2 UV;
@@ -43,5 +45,25 @@ void main()
// gamma correct
color = pow(mapped, vec3(u_Gamma));
// TAA
vec2 velocity = texture(u_VelocityFrame, UV).rg;
vec2 previousPixelPos = UV - velocity;
vec3 historyColor = texture(u_PreviousFrame, previousPixelPos).rgb;
// Sample the neighboring pixels for clamping
vec3 NearColor0 = textureOffset(u_Source, UV, ivec2(1, 0)).rgb;
vec3 NearColor1 = textureOffset(u_Source, UV, ivec2(0, 1)).rgb;
vec3 NearColor2 = textureOffset(u_Source, UV, ivec2(-1, 0)).rgb;
vec3 NearColor3 = textureOffset(u_Source, UV, ivec2(0, -1)).rgb;
// Calculate the min and max colors
vec3 BoxMin = min(color, min(NearColor0, min(NearColor1, min(NearColor2, NearColor3))));
vec3 BoxMax = max(color, max(NearColor0, max(NearColor1, max(NearColor2, NearColor3))));
// Clamp the history color
historyColor = clamp(historyColor, BoxMin, BoxMax);
color = mix(color, historyColor, u_TAAFactor);
FragColor = vec4(color, 1.0);
}