Merge pull request #6033 from samcake/hobbes

FIxing the backgroung leaking in the rear view mirror and moving from Float32 to 24 bits Depth buffer
This commit is contained in:
Brad Hefta-Gaub 2015-10-09 08:36:52 -07:00
commit e2e086ef55
10 changed files with 44 additions and 58 deletions

View file

@ -205,6 +205,13 @@ void Batch::setViewportTransform(const Vec4i& viewport) {
_params.push_back(cacheData(sizeof(Vec4i), &viewport));
}
void Batch::setDepthRangeTransform(float nearDepth, float farDepth) {
ADD_COMMAND(setDepthRangeTransform);
_params.push_back(farDepth);
_params.push_back(nearDepth);
}
void Batch::setPipeline(const PipelinePointer& pipeline) {
ADD_COMMAND(setPipeline);

View file

@ -187,6 +187,7 @@ public:
void setProjectionTransform(const Mat4& proj);
// Viewport is xy = low left corner in framebuffer, zw = width height of the viewport, expressed in pixels
void setViewportTransform(const Vec4i& viewport);
void setDepthRangeTransform(float nearDepth, float farDepth);
// Pipeline Stage
void setPipeline(const PipelinePointer& pipeline);
@ -285,6 +286,7 @@ public:
COMMAND_setViewTransform,
COMMAND_setProjectionTransform,
COMMAND_setViewportTransform,
COMMAND_setDepthRangeTransform,
COMMAND_setPipeline,
COMMAND_setStateBlendFactor,

View file

@ -14,11 +14,12 @@
out vec2 varTexCoord0;
void main(void) {
const float depth = 1.0;
const vec4 UNIT_QUAD[4] = vec4[4](
vec4(-1.0, -1.0, 0.0, 1.0),
vec4(1.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4(1.0, 1.0, 0.0, 1.0)
vec4(-1.0, -1.0, depth, 1.0),
vec4(1.0, -1.0, depth, 1.0),
vec4(-1.0, 1.0, depth, 1.0),
vec4(1.0, 1.0, depth, 1.0)
);
vec4 pos = UNIT_QUAD[gl_VertexID];

View file

@ -35,6 +35,7 @@ GLBackend::CommandCall GLBackend::_commandCalls[Batch::NUM_COMMANDS] =
(&::gpu::GLBackend::do_setViewTransform),
(&::gpu::GLBackend::do_setProjectionTransform),
(&::gpu::GLBackend::do_setViewportTransform),
(&::gpu::GLBackend::do_setDepthRangeTransform),
(&::gpu::GLBackend::do_setPipeline),
(&::gpu::GLBackend::do_setStateBlendFactor),

View file

@ -314,6 +314,7 @@ protected:
void do_setViewTransform(Batch& batch, uint32 paramOffset);
void do_setProjectionTransform(Batch& batch, uint32 paramOffset);
void do_setViewportTransform(Batch& batch, uint32 paramOffset);
void do_setDepthRangeTransform(Batch& batch, uint32 paramOffset);
void initTransform();
void killTransform();
@ -339,6 +340,7 @@ protected:
Transform _view;
Mat4 _projection;
Vec4i _viewport{ 0, 0, 1, 1 };
Vec2 _depthRange{ 0.0f, 1.0f };
bool _invalidModel{true};
bool _invalidView{false};
bool _invalidProj{false};

View file

@ -45,10 +45,21 @@ void GLBackend::do_setViewportTransform(Batch& batch, uint32 paramOffset) {
glViewport(vp.x, vp.y, vp.z, vp.w);
// The Viewport is tagged invalid because the CameraTransformUBO is not up to date and willl need update on next drawcall
// The Viewport is tagged invalid because the CameraTransformUBO is not up to date and will need update on next drawcall
_transform._invalidViewport = true;
}
void GLBackend::do_setDepthRangeTransform(Batch& batch, uint32 paramOffset) {
Vec2 depthRange(batch._params[paramOffset + 0]._float, batch._params[paramOffset + 1]._float);
if ((depthRange.x != _transform._depthRange.x) || (depthRange.y != _transform._depthRange.y)) {
_transform._depthRange = depthRange;
glDepthRangef(depthRange.x, depthRange.y);
}
}
void GLBackend::initTransform() {
glGenBuffers(1, &_transform._objectBuffer);
glGenBuffers(1, &_transform._cameraBuffer);
@ -75,6 +86,8 @@ void GLBackend::syncTransformStateCache() {
glGetIntegerv(GL_VIEWPORT, (GLint*) &_transform._viewport);
glGetFloatv(GL_DEPTH_RANGE, (GLfloat*)&_transform._depthRange);
Mat4 modelView;
auto modelViewInv = glm::inverse(modelView);
_transform._view.evalFromRawMatrix(modelViewInv);

View file

@ -35,9 +35,7 @@ void FramebufferCache::setFrameBufferSize(QSize frameBufferSize) {
_frameBufferSize = frameBufferSize;
_primaryFramebufferFull.reset();
_primaryFramebufferDepthColor.reset();
_primaryFramebufferStencilColor.reset();
_primaryDepthTexture.reset();
_primaryStencilTexture.reset();
_primaryColorTexture.reset();
_primaryNormalTexture.reset();
_primarySpecularTexture.reset();
@ -49,7 +47,6 @@ void FramebufferCache::setFrameBufferSize(QSize frameBufferSize) {
void FramebufferCache::createPrimaryFramebuffer() {
_primaryFramebufferFull = gpu::FramebufferPointer(gpu::Framebuffer::create());
_primaryFramebufferDepthColor = gpu::FramebufferPointer(gpu::Framebuffer::create());
_primaryFramebufferStencilColor = gpu::FramebufferPointer(gpu::Framebuffer::create());
auto colorFormat = gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA);
auto width = _frameBufferSize.width();
@ -66,20 +63,14 @@ void FramebufferCache::createPrimaryFramebuffer() {
_primaryFramebufferDepthColor->setRenderBuffer(0, _primaryColorTexture);
_primaryFramebufferStencilColor->setRenderBuffer(0, _primaryColorTexture);
auto depthFormat = gpu::Element(gpu::SCALAR, gpu::FLOAT, gpu::DEPTH);
// auto depthFormat = gpu::Element(gpu::SCALAR, gpu::FLOAT, gpu::DEPTH);
auto depthFormat = gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::DEPTH_STENCIL); // Depth24_Stencil8 texel format
_primaryDepthTexture = gpu::TexturePointer(gpu::Texture::create2D(depthFormat, width, height, defaultSampler));
auto stencilFormat = gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::DEPTH_STENCIL); // Depth24_Stencil8 texel format
_primaryStencilTexture = gpu::TexturePointer(gpu::Texture::create2D(stencilFormat, width, height, defaultSampler));
_primaryFramebufferFull->setDepthStencilBuffer(_primaryDepthTexture, depthFormat);
_primaryFramebufferDepthColor->setDepthStencilBuffer(_primaryDepthTexture, depthFormat);
_primaryFramebufferStencilColor->setDepthStencilBuffer(_primaryStencilTexture, stencilFormat);
_selfieFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create());
auto tex = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width * 0.5, height * 0.5, defaultSampler));
_selfieFramebuffer->setRenderBuffer(0, tex);
@ -99,13 +90,6 @@ gpu::FramebufferPointer FramebufferCache::getPrimaryFramebufferDepthColor() {
return _primaryFramebufferDepthColor;
}
gpu::FramebufferPointer FramebufferCache::getPrimaryFramebufferStencilColor() {
if (!_primaryFramebufferStencilColor) {
createPrimaryFramebuffer();
}
return _primaryFramebufferStencilColor;
}
gpu::TexturePointer FramebufferCache::getPrimaryDepthTexture() {
if (!_primaryDepthTexture) {
createPrimaryFramebuffer();
@ -113,13 +97,6 @@ gpu::TexturePointer FramebufferCache::getPrimaryDepthTexture() {
return _primaryDepthTexture;
}
gpu::TexturePointer FramebufferCache::getPrimaryStencilTexture() {
if (!_primaryStencilTexture) {
createPrimaryFramebuffer();
}
return _primaryStencilTexture;
}
gpu::TexturePointer FramebufferCache::getPrimaryColorTexture() {
if (!_primaryColorTexture) {
createPrimaryFramebuffer();

View file

@ -31,10 +31,8 @@ public:
/// used for scene rendering.
gpu::FramebufferPointer getPrimaryFramebuffer();
gpu::FramebufferPointer getPrimaryFramebufferDepthColor();
gpu::FramebufferPointer getPrimaryFramebufferStencilColor();
gpu::TexturePointer getPrimaryDepthTexture();
gpu::TexturePointer getPrimaryStencilTexture();
gpu::TexturePointer getPrimaryColorTexture();
gpu::TexturePointer getPrimaryNormalTexture();
gpu::TexturePointer getPrimarySpecularTexture();
@ -60,9 +58,8 @@ private:
gpu::FramebufferPointer _primaryFramebufferFull;
gpu::FramebufferPointer _primaryFramebufferDepthColor;
gpu::FramebufferPointer _primaryFramebufferStencilColor;
gpu::TexturePointer _primaryDepthTexture;
gpu::TexturePointer _primaryStencilTexture;
gpu::TexturePointer _primaryColorTexture;
gpu::TexturePointer _primaryNormalTexture;
gpu::TexturePointer _primarySpecularTexture;

View file

@ -37,22 +37,17 @@ void SetupDeferred::run(const SceneContextPointer& sceneContext, const RenderCon
RenderArgs* args = renderContext->args;
gpu::doInBatch(args->_context, [=](gpu::Batch& batch) {
auto primaryFboStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferStencilColor();
auto primaryFbo = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferDepthColor();
batch.enableStereo(false);
batch.setViewportTransform(args->_viewport);
batch.setStateScissorRect(args->_viewport);
batch.setFramebuffer(primaryFboStencil);
batch.clearFramebuffer(
gpu::Framebuffer::BUFFER_STENCIL,
vec4(vec3(0), 1), 1.0, 0.0, true);
batch.setFramebuffer(primaryFbo);
batch.clearFramebuffer(
gpu::Framebuffer::BUFFER_COLOR0 |
gpu::Framebuffer::BUFFER_DEPTH,
gpu::Framebuffer::BUFFER_DEPTH |
gpu::Framebuffer::BUFFER_STENCIL,
vec4(vec3(0), 1), 1.0, 0.0, true);
});
}
@ -313,7 +308,8 @@ const gpu::PipelinePointer& DrawStencilDeferred::getOpaquePipeline() {
gpu::Shader::makeProgram((*program));
auto state = std::make_shared<gpu::State>();
state->setStencilTest(true, 0xFF, gpu::State::StencilTest(STENCIL_OPAQUE, 0xFF, gpu::ALWAYS, gpu::State::STENCIL_OP_REPLACE, gpu::State::STENCIL_OP_REPLACE, gpu::State::STENCIL_OP_REPLACE));
state->setDepthTest(true, false, gpu::LESS_EQUAL);
state->setStencilTest(true, 0xFF, gpu::State::StencilTest(STENCIL_OPAQUE, 0xFF, gpu::ALWAYS, gpu::State::STENCIL_OP_REPLACE, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_REPLACE));
state->setColorWriteMask(0);
_opaquePipeline.reset(gpu::Pipeline::create(program, state));
@ -330,9 +326,8 @@ void DrawStencilDeferred::run(const SceneContextPointer& sceneContext, const Ren
doInBatch(args->_context, [=](gpu::Batch& batch) {
args->_batch = &batch;
auto primaryFboColorDepthStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferStencilColor();
auto primaryDepth = DependencyManager::get<FramebufferCache>()->getPrimaryDepthTexture();
auto primaryFboColorDepthStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferDepthColor();
batch.enableStereo(false);
batch.setFramebuffer(primaryFboColorDepthStencil);
@ -340,7 +335,6 @@ void DrawStencilDeferred::run(const SceneContextPointer& sceneContext, const Ren
batch.setStateScissorRect(args->_viewport);
batch.setPipeline(getOpaquePipeline());
batch.setResourceTexture(0, primaryDepth);
batch.draw(gpu::TRIANGLE_STRIP, 4);
batch.setResourceTexture(0, nullptr);
@ -367,12 +361,12 @@ void DrawBackgroundDeferred::run(const SceneContextPointer& sceneContext, const
doInBatch(args->_context, [=](gpu::Batch& batch) {
args->_batch = &batch;
auto primaryFboColorStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferStencilColor();
auto primaryFboColorDepthStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferDepthColor();
auto primaryFboFull = DependencyManager::get<FramebufferCache>()->getPrimaryFramebuffer();
batch.enableSkybox(true);
batch.setFramebuffer(primaryFboColorStencil);
batch.setFramebuffer(primaryFboColorDepthStencil);
batch.setViewportTransform(args->_viewport);
batch.setStateScissorRect(args->_viewport);

View file

@ -12,13 +12,5 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
in vec2 varTexCoord0;
uniform sampler2D depthTexture;
void main(void) {
float depth = texture(depthTexture, varTexCoord0.xy).r;
if (depth >= 1.0) {
discard;
}
}