From e78f1521abb9967886948243c5d3286aef70a5f6 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 30 Sep 2015 09:24:36 -0700 Subject: [PATCH] getting the stencil buffer in place --- libraries/gpu/src/gpu/Framebuffer.cpp | 8 ++- libraries/gpu/src/gpu/Framebuffer.h | 2 + libraries/gpu/src/gpu/GLBackendOutput.cpp | 13 ++++- libraries/gpu/src/gpu/GLBackendTexture.cpp | 49 +++++++++++++------ .../render-utils/src/RenderDeferredTask.cpp | 4 +- .../render-utils/src/drawOpaqueStencil.slf | 6 ++- 6 files changed, 63 insertions(+), 19 deletions(-) diff --git a/libraries/gpu/src/gpu/Framebuffer.cpp b/libraries/gpu/src/gpu/Framebuffer.cpp index 96bd3d3002..d570007b3e 100755 --- a/libraries/gpu/src/gpu/Framebuffer.cpp +++ b/libraries/gpu/src/gpu/Framebuffer.cpp @@ -247,7 +247,13 @@ bool Framebuffer::setDepthStencilBuffer(const TexturePointer& texture, const For _bufferMask = ( _bufferMask & ~BUFFER_DEPTHSTENCIL); if (texture) { - _bufferMask |= BUFFER_DEPTHSTENCIL; + if (format.getSemantic() == gpu::DEPTH) { + _bufferMask |= BUFFER_DEPTH; + } else if (format.getSemantic() == gpu::STENCIL) { + _bufferMask |= BUFFER_STENCIL; + } else if (format.getSemantic() == gpu::DEPTH_STENCIL) { + _bufferMask |= BUFFER_DEPTHSTENCIL; + } } return true; diff --git a/libraries/gpu/src/gpu/Framebuffer.h b/libraries/gpu/src/gpu/Framebuffer.h index 310255af9f..83ff8fbb23 100755 --- a/libraries/gpu/src/gpu/Framebuffer.h +++ b/libraries/gpu/src/gpu/Framebuffer.h @@ -116,6 +116,8 @@ public: bool isEmpty() const { return (_bufferMask == 0); } bool hasColor() const { return (getBufferMask() & BUFFER_COLORS); } bool hasDepthStencil() const { return (getBufferMask() & BUFFER_DEPTHSTENCIL); } + bool hasDepth() const { return (getBufferMask() & BUFFER_DEPTH); } + bool hasStencil() const { return (getBufferMask() & BUFFER_STENCIL); } bool validateTargetCompatibility(const Texture& texture, uint32 subresource = 0) const; diff --git a/libraries/gpu/src/gpu/GLBackendOutput.cpp b/libraries/gpu/src/gpu/GLBackendOutput.cpp index c3f61a67c3..70e4b18b0f 100755 --- a/libraries/gpu/src/gpu/GLBackendOutput.cpp +++ b/libraries/gpu/src/gpu/GLBackendOutput.cpp @@ -100,7 +100,18 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe if (surface) { auto gltexture = GLBackend::syncGPUObject(*surface); if (gltexture) { - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, gltexture->_texture, 0); + GLenum attachement = GL_DEPTH_STENCIL_ATTACHMENT; + if (!framebuffer.hasStencil()) { + attachement = GL_DEPTH_ATTACHMENT; + glFramebufferTexture2D(GL_FRAMEBUFFER, attachement, GL_TEXTURE_2D, gltexture->_texture, 0); + } else if (!framebuffer.hasDepth()) { + attachement = GL_STENCIL_ATTACHMENT; + glFramebufferTexture2D(GL_FRAMEBUFFER, attachement, GL_TEXTURE_2D, gltexture->_texture, 0); + } else { + attachement = GL_DEPTH_STENCIL_ATTACHMENT; + glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachement, GL_RENDERBUFFER, gltexture->_texture); + } + (void) CHECK_GL_ERROR(); } } } diff --git a/libraries/gpu/src/gpu/GLBackendTexture.cpp b/libraries/gpu/src/gpu/GLBackendTexture.cpp index dce5236868..044204934c 100755 --- a/libraries/gpu/src/gpu/GLBackendTexture.cpp +++ b/libraries/gpu/src/gpu/GLBackendTexture.cpp @@ -66,7 +66,9 @@ public: texel.internalFormat = GL_RG; break; case gpu::DEPTH_STENCIL: - texel.internalFormat = GL_DEPTH_STENCIL; + texel.type = GL_UNSIGNED_BYTE; + texel.format = GL_DEPTH_STENCIL; + texel.internalFormat = GL_DEPTH24_STENCIL8; break; default: qCDebug(gpulogging) << "Unknown combination of texel format"; @@ -197,7 +199,9 @@ public: texel.internalFormat = GL_RG; break; case gpu::DEPTH_STENCIL: - texel.internalFormat = GL_DEPTH_STENCIL; + texel.type = GL_UNSIGNED_BYTE; + texel.format = GL_DEPTH_STENCIL; + texel.internalFormat = GL_DEPTH24_STENCIL8; break; default: qCDebug(gpulogging) << "Unknown combination of texel format"; @@ -334,22 +338,37 @@ GLBackend::GLTexture* GLBackend::syncGPUObject(const Texture& texture) { } GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat); - - glTexImage2D(GL_TEXTURE_2D, 0, - texelFormat.internalFormat, texture.getWidth(), texture.getHeight(), 0, - texelFormat.format, texelFormat.type, bytes); - if (bytes && texture.isAutogenerateMips()) { - glGenerateMipmap(GL_TEXTURE_2D); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - }/* else { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - }*/ + auto semantic = texture.getTexelFormat().getSemantic(); + + if (semantic == gpu::DEPTH_STENCIL) { + glBindTexture(GL_TEXTURE_2D, 0); + glDeleteTextures(1, &object->_texture); + + glGenRenderbuffers(1, &object->_texture); + glBindRenderbuffer(GL_RENDERBUFFER, object->_texture); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, texture.getWidth(), texture.getHeight()); + // At this point the mip pixels have been loaded, we can notify + texture.notifyMipFaceGPULoaded(0, 0); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + } else { + glTexImage2D(GL_TEXTURE_2D, 0, + texelFormat.internalFormat, texture.getWidth(), texture.getHeight(), 0, + texelFormat.format, texelFormat.type, bytes); + + if (bytes && texture.isAutogenerateMips()) { + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + }/* else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + }*/ - object->_target = GL_TEXTURE_2D; + object->_target = GL_TEXTURE_2D; - syncSampler(texture.getSampler(), texture.getType(), object); + syncSampler(texture.getSampler(), texture.getType(), object); + } // At this point the mip pixels have been loaded, we can notify texture.notifyMipFaceGPULoaded(0, 0); diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index d21b6377be..71bc41e6b6 100755 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -232,7 +232,9 @@ const gpu::PipelinePointer& DrawOverlay3D::getOpaquePipeline() { auto program = gpu::ShaderPointer(gpu::Shader::createProgram(vs, ps)); auto state = std::make_shared(); - state->setDepthTest(true, true, gpu::LESS_EQUAL); + state->setDepthTest(false); + // additive blending + state->setBlendFunction(true, gpu::State::ONE, gpu::State::BLEND_OP_ADD, gpu::State::ONE); _opaquePipeline.reset(gpu::Pipeline::create(program, state)); } diff --git a/libraries/render-utils/src/drawOpaqueStencil.slf b/libraries/render-utils/src/drawOpaqueStencil.slf index 0634d2d66a..dae3e56c4d 100644 --- a/libraries/render-utils/src/drawOpaqueStencil.slf +++ b/libraries/render-utils/src/drawOpaqueStencil.slf @@ -26,5 +26,9 @@ void main(void) { float depth = texture(depthTexture, varTexCoord0).r; - outFragColor = vec4(vec3(depth), 1.0); + if (depth >= 0.9) { + outFragColor = vec4(1.0, 0.0, 0.0, 1.0); + } else { + discard; + } }