Removing uniforms from antialiasing

This commit is contained in:
Brad Davis 2018-08-24 10:35:51 -07:00
parent 07fba89f0f
commit a4cd56532e
3 changed files with 53 additions and 76 deletions

View file

@ -29,7 +29,6 @@
namespace ru { namespace ru {
using render_utils::slot::uniform::Uniform;
using render_utils::slot::texture::Texture; using render_utils::slot::texture::Texture;
using render_utils::slot::buffer::Buffer; using render_utils::slot::buffer::Buffer;
} }
@ -39,13 +38,7 @@ namespace gr {
using graphics::slot::buffer::Buffer; using graphics::slot::buffer::Buffer;
} }
#define ANTIALIASING_USE_TAA 1
#if !ANTIALIASING_USE_TAA #if !ANTIALIASING_USE_TAA
#include "fxaa_vert.h"
#include "fxaa_frag.h"
#include "fxaa_blend_frag.h"
Antialiasing::Antialiasing() { Antialiasing::Antialiasing() {
_geometryId = DependencyManager::get<GeometryCache>()->allocateID(); _geometryId = DependencyManager::get<GeometryCache>()->allocateID();
@ -58,30 +51,9 @@ Antialiasing::~Antialiasing() {
} }
} }
const gpu::PipelinePointer& Antialiasing::getAntialiasingPipeline(RenderArgs* args) { const gpu::PipelinePointer& Antialiasing::getAntialiasingPipeline() {
int width = args->_viewport.z;
int height = args->_viewport.w;
if (_antialiasingBuffer && _antialiasingBuffer->getSize() != uvec2(width, height)) {
_antialiasingBuffer.reset();
}
if (!_antialiasingBuffer) {
// Link the antialiasing FBO to texture
_antialiasingBuffer = gpu::FramebufferPointer(gpu::Framebuffer::create("antialiasing"));
auto format = gpu::Element::COLOR_SRGBA_32;
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);
}
if (!_antialiasingPipeline) { if (!_antialiasingPipeline) {
auto vs = fxaa_vert::getShader(); gpu::ShaderPointer program = gpu::Shader::createProgram(shader::render_utils::program::fxaa);
auto ps = fxaa_frag::getShader();
gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps);
_texcoordOffsetLoc = program->getUniforms().findLocation("texcoordOffset");
gpu::StatePointer state = gpu::StatePointer(new gpu::State()); gpu::StatePointer state = gpu::StatePointer(new gpu::State());
state->setDepthTest(false, false, gpu::LESS_EQUAL); state->setDepthTest(false, false, gpu::LESS_EQUAL);
@ -96,9 +68,7 @@ const gpu::PipelinePointer& Antialiasing::getAntialiasingPipeline(RenderArgs* ar
const gpu::PipelinePointer& Antialiasing::getBlendPipeline() { const gpu::PipelinePointer& Antialiasing::getBlendPipeline() {
if (!_blendPipeline) { if (!_blendPipeline) {
auto vs = fxaa_vert::getShader(); gpu::ShaderPointer program = gpu::Shader::createProgram(shader::render_utils::program::fxaa_blend);
auto ps = fxaa_blend_frag::getShader();
gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps);
gpu::StatePointer state = gpu::StatePointer(new gpu::State()); gpu::StatePointer state = gpu::StatePointer(new gpu::State());
state->setDepthTest(false, false, gpu::LESS_EQUAL); state->setDepthTest(false, false, gpu::LESS_EQUAL);
PrepareStencil::testNoAA(*state); PrepareStencil::testNoAA(*state);
@ -119,13 +89,30 @@ void Antialiasing::run(const render::RenderContextPointer& renderContext, const
batch.enableStereo(false); batch.enableStereo(false);
batch.setViewportTransform(args->_viewport); batch.setViewportTransform(args->_viewport);
// FIXME: NEED to simplify that code to avoid all the GeometryCahce call, this is purely pixel manipulation if (!_paramsBuffer) {
float fbWidth = renderContext->args->_viewport.z; _paramsBuffer = std::make_shared<gpu::Buffer>(sizeof(glm::vec2), nullptr);
float fbHeight = renderContext->args->_viewport.w; }
// float sMin = args->_viewport.x / fbWidth;
// float sWidth = args->_viewport.z / fbWidth; {
// float tMin = args->_viewport.y / fbHeight; int width = args->_viewport.z;
// float tHeight = args->_viewport.w / fbHeight; int height = args->_viewport.w;
if (_antialiasingBuffer && _antialiasingBuffer->getSize() != uvec2(width, height)) {
_antialiasingBuffer.reset();
}
if (!_antialiasingBuffer) {
// Link the antialiasing FBO to texture
_antialiasingBuffer = gpu::FramebufferPointer(gpu::Framebuffer::create("antialiasing"));
auto format = gpu::Element::COLOR_SRGBA_32;
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);
glm::vec2 fbExtent { args->_viewport.z, args->_viewport.w };
glm::vec2 inverseFbExtent = 1.0f / fbExtent;
_paramsBuffer->setSubData(0, inverseFbExtent);
}
}
glm::mat4 projMat; glm::mat4 projMat;
Transform viewMat; Transform viewMat;
@ -136,40 +123,18 @@ void Antialiasing::run(const render::RenderContextPointer& renderContext, const
batch.setModelTransform(Transform()); batch.setModelTransform(Transform());
// FXAA step // FXAA step
auto pipeline = getAntialiasingPipeline(renderContext->args); auto pipeline = getAntialiasingPipeline();
batch.setResourceTexture(0, sourceBuffer->getRenderBuffer(0)); batch.setResourceTexture(0, sourceBuffer->getRenderBuffer(0));
batch.setFramebuffer(_antialiasingBuffer); batch.setFramebuffer(_antialiasingBuffer);
batch.setPipeline(pipeline); batch.setPipeline(pipeline);
batch.setUniformBuffer(0, _paramsBuffer);
// initialize the view-space unpacking uniforms using frustum data batch.draw(gpu::TRIANGLE_STRIP, 4);
float left, right, bottom, top, nearVal, farVal;
glm::vec4 nearClipPlane, farClipPlane;
args->getViewFrustum().computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane);
// float depthScale = (farVal - nearVal) / farVal;
// float nearScale = -1.0f / nearVal;
// float depthTexCoordScaleS = (right - left) * nearScale / sWidth;
// float depthTexCoordScaleT = (top - bottom) * nearScale / tHeight;
// float depthTexCoordOffsetS = left * nearScale - sMin * depthTexCoordScaleS;
// float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT;
batch._glUniform2f(_texcoordOffsetLoc, 1.0f / fbWidth, 1.0f / fbHeight);
glm::vec4 color(0.0f, 0.0f, 0.0f, 1.0f);
glm::vec2 bottomLeft(-1.0f, -1.0f);
glm::vec2 topRight(1.0f, 1.0f);
glm::vec2 texCoordTopLeft(0.0f, 0.0f);
glm::vec2 texCoordBottomRight(1.0f, 1.0f);
DependencyManager::get<GeometryCache>()->renderQuad(batch, bottomLeft, topRight, texCoordTopLeft, texCoordBottomRight, color, _geometryId);
// Blend step // Blend step
batch.setResourceTexture(0, _antialiasingTexture); batch.setResourceTexture(0, _antialiasingTexture);
batch.setFramebuffer(sourceBuffer); batch.setFramebuffer(sourceBuffer);
batch.setPipeline(getBlendPipeline()); batch.setPipeline(getBlendPipeline());
batch.draw(gpu::TRIANGLE_STRIP, 4);
DependencyManager::get<GeometryCache>()->renderQuad(batch, bottomLeft, topRight, texCoordTopLeft, texCoordBottomRight, color, _geometryId);
}); });
} }
#else #else
@ -314,7 +279,11 @@ void Antialiasing::run(const render::RenderContextPointer& renderContext, const
// Must match the bindg point in the fxaa_blend.slf shader // Must match the bindg point in the fxaa_blend.slf shader
batch.setResourceFramebufferSwapChainTexture(0, _antialiasingBuffers, 1); batch.setResourceFramebufferSwapChainTexture(0, _antialiasingBuffers, 1);
// Disable sharpen if FXAA // Disable sharpen if FXAA
batch._glUniform1f(ru::Uniform::TaaSharpenIntensity, _sharpen * _params.get().regionInfo.z); if (!_blendParamsBuffer) {
_blendParamsBuffer = std::make_shared<gpu::Buffer>(sizeof(float), nullptr);
}
_blendParamsBuffer->setSubData(0, _sharpen * _params.get().regionInfo.z);
batch.setUniformBuffer(0, _blendParamsBuffer);
} }
batch.draw(gpu::TRIANGLE_STRIP, 4); batch.draw(gpu::TRIANGLE_STRIP, 4);
batch.advance(_antialiasingBuffers); batch.advance(_antialiasingBuffers);

View file

@ -134,6 +134,10 @@ signals:
#define SET_BIT(bitfield, bitIndex, value) bitfield = ((bitfield) & ~(1 << (bitIndex))) | ((value) << (bitIndex)) #define SET_BIT(bitfield, bitIndex, value) bitfield = ((bitfield) & ~(1 << (bitIndex))) | ((value) << (bitIndex))
#define GET_BIT(bitfield, bitIndex) ((bitfield) & (1 << (bitIndex))) #define GET_BIT(bitfield, bitIndex) ((bitfield) & (1 << (bitIndex)))
#define ANTIALIASING_USE_TAA 1
#if ANTIALIASING_USE_TAA
struct TAAParams { struct TAAParams {
float nope{ 0.0f }; float nope{ 0.0f };
float blend{ 0.15f }; float blend{ 0.15f };
@ -186,7 +190,7 @@ private:
gpu::FramebufferSwapChainPointer _antialiasingBuffers; gpu::FramebufferSwapChainPointer _antialiasingBuffers;
gpu::TexturePointer _antialiasingTextures[2]; gpu::TexturePointer _antialiasingTextures[2];
gpu::BufferPointer _blendParamsBuffer;
gpu::PipelinePointer _antialiasingPipeline; gpu::PipelinePointer _antialiasingPipeline;
gpu::PipelinePointer _blendPipeline; gpu::PipelinePointer _blendPipeline;
gpu::PipelinePointer _debugBlendPipeline; gpu::PipelinePointer _debugBlendPipeline;
@ -197,7 +201,7 @@ private:
}; };
/* #else
class AntiAliasingConfig : public render::Job::Config { class AntiAliasingConfig : public render::Job::Config {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool enabled MEMBER enabled) Q_PROPERTY(bool enabled MEMBER enabled)
@ -219,18 +223,15 @@ public:
const gpu::PipelinePointer& getBlendPipeline(); const gpu::PipelinePointer& getBlendPipeline();
private: private:
// Uniforms for AA
gpu::int32 _texcoordOffsetLoc;
gpu::FramebufferPointer _antialiasingBuffer; gpu::FramebufferPointer _antialiasingBuffer;
gpu::TexturePointer _antialiasingTexture; gpu::TexturePointer _antialiasingTexture;
gpu::BufferPointer _paramsBuffer;
gpu::PipelinePointer _antialiasingPipeline; gpu::PipelinePointer _antialiasingPipeline;
gpu::PipelinePointer _blendPipeline; gpu::PipelinePointer _blendPipeline;
int _geometryId { 0 }; int _geometryId { 0 };
}; };
*/ #endif
#endif // hifi_AntialiasingEffect_h #endif // hifi_AntialiasingEffect_h

View file

@ -18,7 +18,14 @@ layout(location=0) in vec2 varTexCoord0;
layout(location=0) out vec4 outFragColor; layout(location=0) out vec4 outFragColor;
layout(binding=0) uniform sampler2D colorTexture; layout(binding=0) uniform sampler2D colorTexture;
layout(location=GPU_UNIFORM_EXTRA0) uniform float sharpenIntensity;
struct FxaaBlendParams {
float sharpenIntensity;
};
layout(binding=0) uniform fxaaBlendParamsBuffer {
FxaaBlendParams params;
};
void main(void) { void main(void) {
vec4 pixels[9]; vec4 pixels[9];
@ -39,5 +46,5 @@ void main(void) {
vec4 minColor = max(vec4(0), pixels[4]-vec4(0.5)); vec4 minColor = max(vec4(0), pixels[4]-vec4(0.5));
vec4 maxColor = pixels[4]+vec4(0.5); vec4 maxColor = pixels[4]+vec4(0.5);
outFragColor = clamp(pixels[4] + sharpenedPixel * sharpenIntensity, minColor, maxColor); outFragColor = clamp(pixels[4] + sharpenedPixel * params.sharpenIntensity, minColor, maxColor);
} }