Commit e81e2b02 authored by Dario Seyb's avatar Dario Seyb

saving normals and motion in the same buffer

parent 02e09673
......@@ -42,8 +42,7 @@ uniform mat4 uPrevViewProjectionMatrix;
out vec4 oColor;
out vec4 oEmissive;
out vec3 oNormal;
out vec3 oMotion;
out vec4 oNormalMotion;
out vec4 oSpecularSmoothness;
float rand(vec2 co){
......@@ -95,13 +94,12 @@ void main()
vec4 thisPosition = uViewProjectionMatrix * vec4(inPosition, 1);
vec4 prevPosition = uPrevViewProjectionMatrix * uPrevModelMatrix * uInverseModelMatrix * vec4(inPosition, 1);
vec3 prevFragCoord = prevPosition.xyz/prevPosition.w;
vec3 thisFragCoord = thisPosition.xyz/thisPosition.w;
vec2 prevFragCoord = prevPosition.xy/prevPosition.w;
vec2 thisFragCoord = thisPosition.xy/thisPosition.w;
oMotion = (thisFragCoord-prevFragCoord)*0.5;
oMotion.z = thisPosition.z;
oColor = color;
oEmissive = emissive();
oSpecularSmoothness = specularSmoothness();
oNormal = normalize(normal());
oNormalMotion.xy = normalize(normal()).xy;
oNormalMotion.zw = (thisFragCoord-prevFragCoord)*0.5;
}
......@@ -2,7 +2,7 @@
uniform sampler2D uSamplerColor;
uniform sampler2D uSamplerEmissive;
uniform sampler2D uSamplerNormal;
uniform sampler2D uSamplerNormalMotion;
uniform sampler2D uSamplerMotion;
uniform sampler2D uSamplerDepth;
uniform sampler2D uSamplerSpecularSmoothness;
......@@ -77,7 +77,7 @@ void main()
vec4 albedo = texture(uSamplerColor, vTexCoord);
vec4 specularSmoothness = texture(uSamplerSpecularSmoothness, vTexCoord);
vec4 emissive = texture(uSamplerEmissive, vTexCoord);
vec3 normal = texture(uSamplerNormal, vTexCoord).rgb;
vec3 normal = unpackNormal(texture(uSamplerNormalMotion, vTexCoord));
float depth = unpackDepth(texture(uSamplerDepth, vTexCoord).r);
......
......@@ -2,7 +2,8 @@
uniform sampler2D uSamplerColor;
uniform sampler2D uSamplerHistory;
uniform sampler2D uSamplerMotion;
uniform sampler2D uSamplerNormalMotion;
uniform sampler2D uSamplerDepth;
in vec2 vTexCoord;
......@@ -18,15 +19,18 @@ const vec2 SAMPLE_OFFSETS[4] = vec2[] (
void main()
{
vec3 motion = texture(uSamplerMotion, vTexCoord).xyz;
vec2 motion = texture(uSamplerNormalMotion, vTexCoord).zw;
float depth = texture(uSamplerDepth, vTexCoord).x;
for(int i = 0; i < 4; i++) {
vec3 motionSample = texture(uSamplerMotion, vTexCoord + SAMPLE_OFFSETS[i]/vec2(1280, 720)).xyz;
if(motionSample.z < motion.z) {
motion = motionSample;
vec2 sampleCoord = vTexCoord + SAMPLE_OFFSETS[i]/vec2(1280, 720);
float depthSample = texture(uSamplerDepth, sampleCoord).x;
if(depthSample < depth) {
motion = texture(uSamplerNormalMotion, sampleCoord).zw;
depth = depthSample;
}
}
vec2 samplePos = vTexCoord-motion.xy;
vec4 history = texture(uSamplerHistory, vTexCoord);
......
#define M_PI 3.1415926535897932384626433832795
vec3 unpackNormal(vec4 norm) {
return norm.xyz * 2 - vec3(1, 1, 1);
}
vec3 unpackNormal(vec4 normalMotion) {
return normalize(vec3(normalMotion.xy, sqrt(1.0 - normalMotion.x*normalMotion.x - normalMotion.y*normalMotion.y)));
}
\ No newline at end of file
......@@ -61,8 +61,7 @@ private:
SharedTexture2D m_colorBuffer;
SharedTexture2D m_emissiveBuffer;
SharedTexture2D m_normalBuffer;
SharedTexture2D m_motionBuffer;
SharedTexture2D m_normalMotionBuffer;
SharedTexture2D m_depthBuffer;
SharedTexture2D m_specularBuffer;
......
......@@ -48,16 +48,14 @@ bool RendererSystem::startup() {
m_colorBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA16F);
m_emissiveBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA16F);
m_normalBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA16F);
m_motionBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA16F);
m_normalMotionBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA16F);
m_depthBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_DEPTH24_STENCIL8);
m_specularBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA16F);
m_gBufferObject = SharedFrameBufferObject(new FrameBufferObject());
m_gBufferObject->attachColorTexture("oColor", m_colorBuffer);
m_gBufferObject->attachColorTexture("oEmissive", m_emissiveBuffer);
m_gBufferObject->attachColorTexture("oNormal", m_normalBuffer);
m_gBufferObject->attachColorTexture("oMotion", m_motionBuffer);
m_gBufferObject->attachColorTexture("oNormalMotion", m_normalMotionBuffer);
m_gBufferObject->attachColorTexture("oSpecularSmoothness", m_specularBuffer);
m_gBufferObject->setDepthTexture(m_depthBuffer);
m_gBufferObject->validate(); // always a good idea
......@@ -173,8 +171,7 @@ bool RendererSystem::startup() {
ImGui::Begin("GBuffer", 0, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Image((void*)m_colorBuffer->getObjectName(), glm::vec2{ 1280, 720 } *0.2f, { 0, 1 }, { 1, 0 });
ImGui::Image((void*)m_emissiveBuffer->getObjectName(), glm::vec2{ 1280, 720 } *0.2f, { 0, 1 }, { 1, 0 });
ImGui::Image((void*)m_normalBuffer->getObjectName(), glm::vec2{ 1280, 720 } *0.2f, { 0, 1 }, { 1, 0 });
ImGui::Image((void*)m_motionBuffer->getObjectName(), glm::vec2{ 1280, 720 } *0.2f, { 0, 1 }, { 1, 0 });
ImGui::Image((void*)m_normalMotionBuffer->getObjectName(), glm::vec2{ 1280, 720 } *0.2f, { 0, 1 }, { 1, 0 });
ImGui::Image((void*)m_specularBuffer->getObjectName(), glm::vec2{ 1280, 720 } *0.2f, { 0, 1 }, { 1, 0 });
ImGui::End();
......@@ -448,10 +445,9 @@ void RendererSystem::render(RenderPass& pass, double interp, double totalTime) {
m_deferredCombineProgram->use();
m_deferredCombineProgram->setTexture("uSamplerColor", m_colorBuffer, 0);
m_deferredCombineProgram->setTexture("uSamplerEmissive", m_emissiveBuffer, 1);
m_deferredCombineProgram->setTexture("uSamplerNormal", m_normalBuffer, 2);
m_deferredCombineProgram->setTexture("uSamplerMotion", m_motionBuffer, 3);
m_deferredCombineProgram->setTexture("uSamplerDepth", m_depthBuffer, 4);
m_deferredCombineProgram->setTexture("uSamplerSpecularSmoothness", m_specularBuffer, 5);
m_deferredCombineProgram->setTexture("uSamplerNormalMotion", m_normalMotionBuffer, 2);
m_deferredCombineProgram->setTexture("uSamplerDepth", m_depthBuffer, 3);
m_deferredCombineProgram->setTexture("uSamplerSpecularSmoothness", m_specularBuffer, 4);
m_deferredCombineProgram->setUniform("uNear", cam->near);
m_deferredCombineProgram->setUniform("uFar", cam->far);
......@@ -474,7 +470,7 @@ void RendererSystem::render(RenderPass& pass, double interp, double totalTime) {
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", m_dummyShadowMap, 6);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", m_dummyShadowMap, 5);
for (size_t i = 0; i < pass.submittedLights.size(); i++) {
auto light = pass.submittedLights[i];
......@@ -492,8 +488,7 @@ void RendererSystem::render(RenderPass& pass, double interp, double totalTime) {
if (hasShadow) {
m_deferredCombineProgram->setUniform("uLightProjMatrix",
biasMatrix * light.projMatrix);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", light.shadowMap,
6);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", light.shadowMap, 5);
auto size = light.shadowMap->getSize();
m_deferredCombineProgram->setUniform(
......@@ -583,7 +578,8 @@ void RendererSystem::render(RenderPass& pass, double interp, double totalTime) {
"uSamplerHistory", pass.txaaHistory->getColorAttachments()[0].texture,
1);
m_txaaProg->setTexture("uSamplerMotion", m_motionBuffer, 2);
m_txaaProg->setTexture("uSamplerNormalMotion", m_normalMotionBuffer, 2);
m_txaaProg->setTexture("uSamplerDepth", m_depthBuffer, 3);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // create 2 triangles (defined in shader) with no attributes
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment