Messing around with the velocity buffer

This commit is contained in:
samcake 2017-08-16 18:29:59 -07:00
parent 55a6c27eb6
commit 800e4cb4d7
6 changed files with 118 additions and 32 deletions

View file

@ -597,7 +597,50 @@ GLTexelFormat GLTexelFormat::evalGLTexelFormat(const Element& dstFormat, const E
case gpu::RGB:
case gpu::RGBA:
case gpu::XY:
texel.internalFormat = GL_RG8;
switch (dstFormat.getType()) {
case gpu::UINT32:
texel.internalFormat = GL_RG32UI;
break;
case gpu::INT32:
texel.internalFormat = GL_RG32I;
break;
case gpu::FLOAT:
texel.internalFormat = GL_RG32F;
break;
case gpu::UINT16:
texel.internalFormat = GL_RG16UI;
break;
case gpu::INT16:
texel.internalFormat = GL_RG16I;
break;
case gpu::NUINT16:
texel.internalFormat = GL_RG16;
break;
case gpu::NINT16:
texel.internalFormat = GL_RG16_SNORM;
break;
case gpu::HALF:
texel.type = GL_FLOAT;
texel.internalFormat = GL_RG16F;
break;
case gpu::UINT8:
texel.internalFormat = GL_RG8UI;
break;
case gpu::INT8:
texel.internalFormat = GL_RG8I;
break;
case gpu::NUINT8:
texel.internalFormat = GL_RG8;
break;
case gpu::NINT8:
texel.internalFormat = GL_RG8_SNORM;
break;
case gpu::NUINT32:
case gpu::NINT32:
case gpu::COMPRESSED:
case gpu::NUM_TYPES: // quiet compiler
Q_UNREACHABLE();
}
break;
default:
qCWarning(gpugllogging) << "Unknown combination of texel format";

View file

@ -175,10 +175,20 @@ void Antialiasing::run(const render::RenderContextPointer& renderContext, const
#include "fxaa_blend_frag.h"
const int AntialiasingPass_FrameTransformSlot = 0;
const int AntialiasingPass_HistoryMapSlot = 0;
const int AntialiasingPass_SourceMapSlot = 1;
const int AntialiasingPass_VelocityMapSlot = 2;
Antialiasing::Antialiasing() {
}
Antialiasing::~Antialiasing() {
_antialiasingBuffer[0].reset();
_antialiasingBuffer[1].reset();
_antialiasingTexture[0].reset();
_antialiasingTexture[1].reset();
}
const gpu::PipelinePointer& Antialiasing::getAntialiasingPipeline() {
@ -190,15 +200,15 @@ const gpu::PipelinePointer& Antialiasing::getAntialiasingPipeline() {
gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps);
gpu::Shader::BindingSet slotBindings;
// slotBindings.insert(gpu::Shader::Binding(std::string("historyTexture"), 0));
slotBindings.insert(gpu::Shader::Binding(std::string("colorTexture"), 0));
slotBindings.insert(gpu::Shader::Binding(std::string("historyMap"), AntialiasingPass_HistoryMapSlot));
slotBindings.insert(gpu::Shader::Binding(std::string("colorMap"), AntialiasingPass_SourceMapSlot));
slotBindings.insert(gpu::Shader::Binding(std::string("velocityMap"), AntialiasingPass_VelocityMapSlot));
gpu::Shader::makeProgram(*program, slotBindings);
gpu::StatePointer state = gpu::StatePointer(new gpu::State());
PrepareStencil::testMask(*state);
state->setBlendFunction(true, gpu::State::BlendArg::SRC_ALPHA, gpu::State::BlendOp::BLEND_OP_ADD, gpu::State::BlendArg::INV_SRC_ALPHA);
// Good to go add the brand new pipeline
_antialiasingPipeline = gpu::Pipeline::create(program, state);
@ -219,7 +229,7 @@ const gpu::PipelinePointer& Antialiasing::getBlendPipeline() {
gpu::Shader::makeProgram(*program, slotBindings);
gpu::StatePointer state = gpu::StatePointer(new gpu::State());
PrepareStencil::testMask(*state);
// PrepareStencil::testMask(*state);
// Good to go add the brand new pipeline
@ -234,38 +244,51 @@ void Antialiasing::configure(const Config& config) {
}
void Antialiasing::run(const render::RenderContextPointer& renderContext, const gpu::FramebufferPointer& sourceBuffer) {
void Antialiasing::run(const render::RenderContextPointer& renderContext, const Inputs& inputs) {
assert(renderContext->args);
assert(renderContext->args->hasViewFrustum());
RenderArgs* args = renderContext->args;
auto& deferredFrameTransform = inputs.get0();
auto& sourceBuffer = inputs.get1();
auto& velocityBuffer = inputs.get2();
int width = sourceBuffer->getWidth();
int height = sourceBuffer->getHeight();
if (_antialiasingBuffer) {
if (_antialiasingBuffer->getSize() != uvec2(width, height)) {// || (sourceBuffer && (_antialiasingBuffer->getRenderBuffer(1) != sourceBuffer->getRenderBuffer(0)))) {
_antialiasingBuffer.reset();
if (_antialiasingBuffer[0]) {
if (_antialiasingBuffer[0]->getSize() != uvec2(width, height)) {// || (sourceBuffer && (_antialiasingBuffer->getRenderBuffer(1) != sourceBuffer->getRenderBuffer(0)))) {
_antialiasingBuffer[0].reset();
_antialiasingBuffer[1].reset();
_antialiasingTexture[0].reset();
_antialiasingTexture[1].reset();
}
}
if (!_antialiasingBuffer) {
if (!_antialiasingBuffer[0]) {
// Link the antialiasing FBO to texture
_antialiasingBuffer = gpu::FramebufferPointer(gpu::Framebuffer::create("antialiasing"));
auto format = gpu::Element::COLOR_SRGBA_32; // DependencyManager::get<FramebufferCache>()->getLightingTexture()->getTexelFormat();
auto defaultSampler = gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_POINT);
_antialiasingTexture = gpu::Texture::createRenderBuffer(format, width, height, gpu::Texture::SINGLE_MIP, defaultSampler);
_antialiasingBuffer->setRenderBuffer(0, _antialiasingTexture);
for (int i = 0; i < 2; i++) {
_antialiasingBuffer[i] = gpu::FramebufferPointer(gpu::Framebuffer::create("antialiasing"));
auto format = gpu::Element::COLOR_SRGBA_32; // DependencyManager::get<FramebufferCache>()->getLightingTexture()->getTexelFormat();
auto defaultSampler = gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_POINT);
_antialiasingTexture[i] = gpu::Texture::createRenderBuffer(format, width, height, gpu::Texture::SINGLE_MIP, defaultSampler);
_antialiasingBuffer[i]->setRenderBuffer(0, _antialiasingTexture[i]);
}
}
int currentFrame = (_currentFrame++) % 2;
int prevFrame = (currentFrame + 1) % 2;
gpu::doInBatch(args->_context, [&](gpu::Batch& batch) {
batch.enableStereo(false);
batch.setViewportTransform(args->_viewport);
// TAA step
getAntialiasingPipeline();
batch.setResourceTexture(0, sourceBuffer->getRenderBuffer(0));
batch.setFramebuffer(_antialiasingBuffer);
batch.setResourceTexture(AntialiasingPass_HistoryMapSlot, _antialiasingTexture[prevFrame]);
batch.setResourceTexture(AntialiasingPass_SourceMapSlot, sourceBuffer->getRenderBuffer(0));
batch.setResourceTexture(AntialiasingPass_VelocityMapSlot, velocityBuffer->getVelocityTexture());
batch.setFramebuffer(_antialiasingBuffer[currentFrame]);
batch.setPipeline(getAntialiasingPipeline());
batch.setUniformBuffer(0, _params._buffer);
@ -273,7 +296,7 @@ void Antialiasing::run(const render::RenderContextPointer& renderContext, const
batch.draw(gpu::TRIANGLE_STRIP, 4);
// Blend step
batch.setResourceTexture(0, _antialiasingTexture);
batch.setResourceTexture(0, _antialiasingTexture[currentFrame]);
batch.setFramebuffer(sourceBuffer);
batch.setPipeline(getBlendPipeline());
batch.draw(gpu::TRIANGLE_STRIP, 4);

View file

@ -15,6 +15,8 @@
#include <DependencyManager.h>
#include "render/DrawTask.h"
#include "DeferredFrameTransform.h"
#include "VelocityBufferPass.h"
class AntialiasingConfig : public render::Job::Config {
Q_OBJECT
@ -43,13 +45,14 @@ using TAAParamsBuffer = gpu::StructBuffer<TAAParams>;
class Antialiasing {
public:
using Inputs = render::VaryingSet3<DeferredFrameTransformPointer, gpu::FramebufferPointer, VelocityFramebufferPointer>;
using Config = AntialiasingConfig;
using JobModel = render::Job::ModelI<Antialiasing, gpu::FramebufferPointer, Config>;
using JobModel = render::Job::ModelI<Antialiasing, Inputs, Config>;
Antialiasing();
~Antialiasing();
void configure(const Config& config);
void run(const render::RenderContextPointer& renderContext, const gpu::FramebufferPointer& sourceBuffer);
void run(const render::RenderContextPointer& renderContext, const Inputs& inputs);
const gpu::PipelinePointer& getAntialiasingPipeline();
const gpu::PipelinePointer& getBlendPipeline();
@ -62,14 +65,14 @@ private:
// Uniforms for AA
gpu::int32 _texcoordOffsetLoc;
gpu::FramebufferPointer _antialiasingBuffer;
gpu::TexturePointer _antialiasingTexture;
gpu::FramebufferPointer _antialiasingBuffer[2];
gpu::TexturePointer _antialiasingTexture[2];
gpu::PipelinePointer _antialiasingPipeline;
gpu::PipelinePointer _blendPipeline;
TAAParamsBuffer _params;
int _currentFrame{ 0 };
};
/*

View file

@ -226,7 +226,9 @@ void RenderDeferredTask::build(JobModel& task, const render::Varying& input, ren
// AA job to be revisited
task.addJob<Antialiasing>("Antialiasing", primaryFramebuffer);
const auto antialiasingInputs = Antialiasing::Inputs(deferredFrameTransform, primaryFramebuffer, velocityBuffer).asVarying();
task.addJob<Antialiasing>("Antialiasing", antialiasingInputs);
// Draw 2DWeb non AA
const auto nonAAOverlaysInputs = DrawOverlay3D::Inputs(nonAAOverlays, lightingModel).asVarying();

View file

@ -18,8 +18,9 @@ precision mediump float;
precision mediump int;
#endif
uniform sampler2D colorTexture;
//uniform sampler2D historyTexture;
uniform sampler2D colorMap;
uniform sampler2D historyMap;
uniform sampler2D velocityMap;
in vec2 varTexCoord0;
layout(location = 0) out vec4 outFragColor;
@ -37,12 +38,26 @@ layout(std140) uniform taaParamsBuffer {
};
void main() {
outFragColor = vec4(texture(colorTexture, varTexCoord0).xyz, params.blend);
vec3 currentColor = texture(colorMap, varTexCoord0).xyz;
vec2 rawVelocity = texture(velocityMap, varTexCoord0).xy;
vec2 velocity = rawVelocity;
vec2 prevTexCoord = varTexCoord0 - velocity;
vec3 prevColor = texture(historyMap, prevTexCoord).xyz;
vec3 newColor = mix(currentColor, prevColor, params.blend);
outFragColor = vec4(newColor, 1.0);
// outFragColor = vec4(varTexCoord0, 0.0, 1.0);
if (abs(varTexCoord0.x - params.debugX) < (1.0 / 2048.0)) {
outFragColor.rgb = vec3(1.0, 1.0, 0.0);
outFragColor.rgb = vec3(1.0, 1.0, 1.0);
}
if (varTexCoord0.x < params.debugX) {
outFragColor.a = 1.0;
outFragColor = vec4(prevColor, 1.0);
/*if (dot(velocity, velocity) > 0.0001) {
outFragColor = vec4(rawVelocity, 0.0, 1.0);
}*/
}
}

View file

@ -28,7 +28,7 @@ void main(void) {
float Zdb = texelFetch(depthMap, ivec2(gl_FragCoord.xy), 0).x;
float Zeye = -evalZeyeFromZdb(Zdb);
if (Zeye <= -getPosLinearDepthFar()) {
outFragColor = vec4(0.0, 0.0, 0.0, 0.0);
outFragColor = vec4(0.5, 0.5, 0.0, 0.0);
return;
}
@ -40,5 +40,5 @@ void main(void) {
vec4 prevClipPos = (frameTransform._projection[stereoSide.x] * vec4(prevEyePos, 1.0));
vec2 prevUV = 0.5 * (prevClipPos.xy / prevClipPos.w) + vec2(0.5);
outFragColor = vec4(texcoordPos - prevUV, 0.0, 0.0);
outFragColor = vec4( ((texcoordPos - prevUV)), 0.0, 0.0);
}