Putting together the stencil buffer for opaque vs background and using it for the backgroud render items

This commit is contained in:
samcake 2015-09-30 15:54:03 -07:00
parent e78f1521ab
commit 75e858cd13
10 changed files with 44 additions and 39 deletions

View file

@ -141,6 +141,7 @@ void Stars::render(RenderArgs* renderArgs, float alpha) {
auto state = gpu::StatePointer(new gpu::State()); auto state = gpu::StatePointer(new gpu::State());
// enable decal blend // enable decal blend
state->setDepthTest(gpu::State::DepthTest(false)); state->setDepthTest(gpu::State::DepthTest(false));
state->setStencilTest(true, 0xFF, gpu::State::StencilTest(0, 0xFF, gpu::EQUAL, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP));
state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA); state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA);
_gridPipeline.reset(gpu::Pipeline::create(program, state)); _gridPipeline.reset(gpu::Pipeline::create(program, state));
} }
@ -152,6 +153,7 @@ void Stars::render(RenderArgs* renderArgs, float alpha) {
auto state = gpu::StatePointer(new gpu::State()); auto state = gpu::StatePointer(new gpu::State());
// enable decal blend // enable decal blend
state->setDepthTest(gpu::State::DepthTest(false)); state->setDepthTest(gpu::State::DepthTest(false));
state->setStencilTest(true, 0xFF, gpu::State::StencilTest(0, 0xFF, gpu::EQUAL, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP));
state->setAntialiasedLineEnable(true); // line smoothing also smooth points state->setAntialiasedLineEnable(true); // line smoothing also smooth points
state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA); state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA);
_starsPipeline.reset(gpu::Pipeline::create(program, state)); _starsPipeline.reset(gpu::Pipeline::create(program, state));

View file

@ -655,11 +655,12 @@ void GLBackend::do_setStateStencil(State::StencilActivation activation, State::S
GL_INCR, GL_INCR,
GL_DECR }; GL_DECR };
glStencilFuncSeparate(GL_FRONT, STENCIL_OPS[frontTest.getFailOp()], STENCIL_OPS[frontTest.getPassOp()], STENCIL_OPS[frontTest.getDepthFailOp()]); glStencilOpSeparate(GL_FRONT, STENCIL_OPS[frontTest.getFailOp()], STENCIL_OPS[frontTest.getPassOp()], STENCIL_OPS[frontTest.getDepthFailOp()]);
glStencilFuncSeparate(GL_FRONT, GL_COMPARISON_FUNCTIONS[frontTest.getFunction()], frontTest.getReference(), frontTest.getReadMask()); glStencilFuncSeparate(GL_FRONT, GL_COMPARISON_FUNCTIONS[frontTest.getFunction()], frontTest.getReference(), frontTest.getReadMask());
glStencilFuncSeparate(GL_BACK, STENCIL_OPS[backTest.getFailOp()], STENCIL_OPS[backTest.getPassOp()], STENCIL_OPS[backTest.getDepthFailOp()]); glStencilOpSeparate(GL_BACK, STENCIL_OPS[backTest.getFailOp()], STENCIL_OPS[backTest.getPassOp()], STENCIL_OPS[backTest.getDepthFailOp()]);
glStencilFuncSeparate(GL_BACK, GL_COMPARISON_FUNCTIONS[backTest.getFunction()], backTest.getReference(), backTest.getReadMask()); glStencilFuncSeparate(GL_BACK, GL_COMPARISON_FUNCTIONS[backTest.getFunction()], backTest.getReference(), backTest.getReadMask());
} else { } else {
glDisable(GL_STENCIL_TEST); glDisable(GL_STENCIL_TEST);
} }

View file

@ -143,11 +143,11 @@ public:
static const int PASS_OP_OFFSET = 12; static const int PASS_OP_OFFSET = 12;
uint16 _functionAndOperations; uint16 _functionAndOperations;
uint8 _reference = 0; int8 _reference = 0;
uint8 _readMask = 0xff; uint8 _readMask = 0xff;
public: public:
StencilTest(uint8 reference = 0, uint8 readMask =0xFF, ComparisonFunction func = ALWAYS, StencilOp failOp = STENCIL_OP_KEEP, StencilOp depthFailOp = STENCIL_OP_KEEP, StencilOp passOp = STENCIL_OP_KEEP) : StencilTest(int8 reference = 0, uint8 readMask =0xFF, ComparisonFunction func = ALWAYS, StencilOp failOp = STENCIL_OP_KEEP, StencilOp depthFailOp = STENCIL_OP_KEEP, StencilOp passOp = STENCIL_OP_KEEP) :
_functionAndOperations(func | (failOp << FAIL_OP_OFFSET) | (depthFailOp << DEPTH_FAIL_OP_OFFSET) | (passOp << PASS_OP_OFFSET)), _functionAndOperations(func | (failOp << FAIL_OP_OFFSET) | (depthFailOp << DEPTH_FAIL_OP_OFFSET) | (passOp << PASS_OP_OFFSET)),
_reference(reference), _readMask(readMask) _reference(reference), _readMask(readMask)
{} {}
@ -157,7 +157,7 @@ public:
StencilOp getDepthFailOp() const { return StencilOp((_functionAndOperations & DEPTH_FAIL_OP_MASK) >> DEPTH_FAIL_OP_OFFSET); } StencilOp getDepthFailOp() const { return StencilOp((_functionAndOperations & DEPTH_FAIL_OP_MASK) >> DEPTH_FAIL_OP_OFFSET); }
StencilOp getPassOp() const { return StencilOp((_functionAndOperations & PASS_OP_MASK) >> PASS_OP_OFFSET); } StencilOp getPassOp() const { return StencilOp((_functionAndOperations & PASS_OP_MASK) >> PASS_OP_OFFSET); }
uint8 getReference() const { return _reference; } int8 getReference() const { return _reference; }
uint8 getReadMask() const { return _readMask; } uint8 getReadMask() const { return _readMask; }
int32 getRaw() const { return *(reinterpret_cast<const int32*>(this)); } int32 getRaw() const { return *(reinterpret_cast<const int32*>(this)); }

View file

@ -89,6 +89,7 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky
} }
auto skyState = std::make_shared<gpu::State>(); auto skyState = std::make_shared<gpu::State>();
skyState->setStencilTest(true, 0xFF, gpu::State::StencilTest(0, 0xFF, gpu::EQUAL, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP));
thePipeline = gpu::PipelinePointer(gpu::Pipeline::create(skyShader, skyState)); thePipeline = gpu::PipelinePointer(gpu::Pipeline::create(skyShader, skyState));

View file

@ -32,7 +32,8 @@ void ProceduralSkybox::setProcedural(const ProceduralPointer& procedural) {
if (_procedural) { if (_procedural) {
_procedural->_vertexSource = ProceduralSkybox_vert; _procedural->_vertexSource = ProceduralSkybox_vert;
_procedural->_fragmentSource = ProceduralSkybox_frag; _procedural->_fragmentSource = ProceduralSkybox_frag;
// No pipeline state customization // Adjust the pipeline state for background using the stencil test
_procedural->_state->setStencilTest(true, 0xFF, gpu::State::StencilTest(0, 0xFF, gpu::EQUAL, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP));
} }
} }

View file

@ -63,7 +63,8 @@ void Environment::setupAtmosphereProgram(const char* vertSource, const char* fra
state->setCullMode(gpu::State::CULL_NONE); state->setCullMode(gpu::State::CULL_NONE);
// state->setDepthTest(false); // state->setDepthTest(false);
state->setDepthTest(true, false, gpu::LESS_EQUAL); // state->setDepthTest(true, false, gpu::LESS_EQUAL);
state->setStencilTest(true, 0xFF, gpu::State::StencilTest(0, 0xFF, gpu::EQUAL, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP));
state->setBlendFunction(true, state->setBlendFunction(true,
gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA,
gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);

View file

@ -35,7 +35,7 @@ void FramebufferCache::setFrameBufferSize(QSize frameBufferSize) {
_frameBufferSize = frameBufferSize; _frameBufferSize = frameBufferSize;
_primaryFramebufferFull.reset(); _primaryFramebufferFull.reset();
_primaryFramebufferDepthColor.reset(); _primaryFramebufferDepthColor.reset();
_primaryFramebufferDepthStencilColor.reset(); _primaryFramebufferStencilColor.reset();
_primaryDepthTexture.reset(); _primaryDepthTexture.reset();
_primaryStencilTexture.reset(); _primaryStencilTexture.reset();
_primaryColorTexture.reset(); _primaryColorTexture.reset();
@ -49,7 +49,7 @@ void FramebufferCache::setFrameBufferSize(QSize frameBufferSize) {
void FramebufferCache::createPrimaryFramebuffer() { void FramebufferCache::createPrimaryFramebuffer() {
_primaryFramebufferFull = gpu::FramebufferPointer(gpu::Framebuffer::create()); _primaryFramebufferFull = gpu::FramebufferPointer(gpu::Framebuffer::create());
_primaryFramebufferDepthColor = gpu::FramebufferPointer(gpu::Framebuffer::create()); _primaryFramebufferDepthColor = gpu::FramebufferPointer(gpu::Framebuffer::create());
_primaryFramebufferDepthStencilColor = gpu::FramebufferPointer(gpu::Framebuffer::create()); _primaryFramebufferStencilColor = gpu::FramebufferPointer(gpu::Framebuffer::create());
auto colorFormat = gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA); auto colorFormat = gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA);
auto width = _frameBufferSize.width(); auto width = _frameBufferSize.width();
@ -66,7 +66,7 @@ void FramebufferCache::createPrimaryFramebuffer() {
_primaryFramebufferDepthColor->setRenderBuffer(0, _primaryColorTexture); _primaryFramebufferDepthColor->setRenderBuffer(0, _primaryColorTexture);
_primaryFramebufferDepthStencilColor->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);
_primaryDepthTexture = gpu::TexturePointer(gpu::Texture::create2D(depthFormat, width, height, defaultSampler)); _primaryDepthTexture = gpu::TexturePointer(gpu::Texture::create2D(depthFormat, width, height, defaultSampler));
@ -78,7 +78,7 @@ void FramebufferCache::createPrimaryFramebuffer() {
_primaryFramebufferDepthColor->setDepthStencilBuffer(_primaryDepthTexture, depthFormat); _primaryFramebufferDepthColor->setDepthStencilBuffer(_primaryDepthTexture, depthFormat);
_primaryFramebufferDepthStencilColor->setDepthStencilBuffer(_primaryStencilTexture, stencilFormat); _primaryFramebufferStencilColor->setDepthStencilBuffer(_primaryStencilTexture, stencilFormat);
_selfieFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create()); _selfieFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create());
auto tex = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width * 0.5, height * 0.5, defaultSampler)); auto tex = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width * 0.5, height * 0.5, defaultSampler));
@ -99,11 +99,11 @@ gpu::FramebufferPointer FramebufferCache::getPrimaryFramebufferDepthColor() {
return _primaryFramebufferDepthColor; return _primaryFramebufferDepthColor;
} }
gpu::FramebufferPointer FramebufferCache::getPrimaryFramebufferDepthStencilColor() { gpu::FramebufferPointer FramebufferCache::getPrimaryFramebufferStencilColor() {
if (!_primaryFramebufferDepthStencilColor) { if (!_primaryFramebufferStencilColor) {
createPrimaryFramebuffer(); createPrimaryFramebuffer();
} }
return _primaryFramebufferDepthStencilColor; return _primaryFramebufferStencilColor;
} }
gpu::TexturePointer FramebufferCache::getPrimaryDepthTexture() { gpu::TexturePointer FramebufferCache::getPrimaryDepthTexture() {

View file

@ -31,7 +31,7 @@ public:
/// used for scene rendering. /// used for scene rendering.
gpu::FramebufferPointer getPrimaryFramebuffer(); gpu::FramebufferPointer getPrimaryFramebuffer();
gpu::FramebufferPointer getPrimaryFramebufferDepthColor(); gpu::FramebufferPointer getPrimaryFramebufferDepthColor();
gpu::FramebufferPointer getPrimaryFramebufferDepthStencilColor(); gpu::FramebufferPointer getPrimaryFramebufferStencilColor();
gpu::TexturePointer getPrimaryDepthTexture(); gpu::TexturePointer getPrimaryDepthTexture();
gpu::TexturePointer getPrimaryStencilTexture(); gpu::TexturePointer getPrimaryStencilTexture();
@ -60,7 +60,7 @@ private:
gpu::FramebufferPointer _primaryFramebufferFull; gpu::FramebufferPointer _primaryFramebufferFull;
gpu::FramebufferPointer _primaryFramebufferDepthColor; gpu::FramebufferPointer _primaryFramebufferDepthColor;
gpu::FramebufferPointer _primaryFramebufferDepthStencilColor; gpu::FramebufferPointer _primaryFramebufferStencilColor;
gpu::TexturePointer _primaryDepthTexture; gpu::TexturePointer _primaryDepthTexture;
gpu::TexturePointer _primaryStencilTexture; gpu::TexturePointer _primaryStencilTexture;
gpu::TexturePointer _primaryColorTexture; gpu::TexturePointer _primaryColorTexture;

View file

@ -301,12 +301,18 @@ void DrawOverlay3D::run(const SceneContextPointer& sceneContext, const RenderCon
gpu::PipelinePointer DrawStencilDeferred::_opaquePipeline; gpu::PipelinePointer DrawStencilDeferred::_opaquePipeline;
const gpu::PipelinePointer& DrawStencilDeferred::getOpaquePipeline() { const gpu::PipelinePointer& DrawStencilDeferred::getOpaquePipeline() {
if (!_opaquePipeline) { if (!_opaquePipeline) {
const gpu::int8 STENCIL_OPAQUE = 1;
auto vs = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS(); auto vs = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS();
auto ps = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(drawOpaqueStencil_frag))); auto ps = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(drawOpaqueStencil_frag)));
auto program = gpu::ShaderPointer(gpu::Shader::createProgram(vs, ps)); auto program = gpu::ShaderPointer(gpu::Shader::createProgram(vs, ps));
gpu::Shader::makeProgram((*program));
auto state = std::make_shared<gpu::State>(); auto state = std::make_shared<gpu::State>();
state->setDepthTest(true, true, gpu::LESS_EQUAL); 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->setStencilTest(false, 0xFF, gpu::State::StencilTest(STENCIL_OPAQUE, 0xFF, gpu::ALWAYS, gpu::State::STENCIL_OP_INCR, gpu::State::STENCIL_OP_INCR, gpu::State::STENCIL_OP_INCR));
state->setColorWriteMask(0);
_opaquePipeline.reset(gpu::Pipeline::create(program, state)); _opaquePipeline.reset(gpu::Pipeline::create(program, state));
} }
@ -322,23 +328,20 @@ void DrawStencilDeferred::run(const SceneContextPointer& sceneContext, const Ren
doInBatch(args->_context, [=](gpu::Batch& batch) { doInBatch(args->_context, [=](gpu::Batch& batch) {
args->_batch = &batch; args->_batch = &batch;
auto primaryFboColorDepthStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferDepthStencilColor(); auto primaryFboColorDepthStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferStencilColor();
auto primaryFboFull = DependencyManager::get<FramebufferCache>()->getPrimaryFramebuffer(); auto primaryFboFull = DependencyManager::get<FramebufferCache>()->getPrimaryFramebuffer();
auto primaryDepth = DependencyManager::get<FramebufferCache>()->getPrimaryDepthTexture(); auto primaryDepth = DependencyManager::get<FramebufferCache>()->getPrimaryDepthTexture();
batch.enableStereo(false);
batch.setFramebuffer(primaryFboColorDepthStencil); batch.setFramebuffer(primaryFboColorDepthStencil);
batch.clearStencilFramebuffer(0, true);
batch.enableStereo(false);
batch.setViewportTransform(args->_viewport); batch.setViewportTransform(args->_viewport);
batch.setStateScissorRect(args->_viewport); batch.setStateScissorRect(args->_viewport);
glm::mat4 projMat; Transform modelMat;
Transform viewMat; batch.setModelTransform(modelMat);
args->_viewFrustum->evalProjectionMatrix(projMat);
args->_viewFrustum->evalViewTransform(viewMat);
batch.setProjectionTransform(projMat);
batch.setViewTransform(viewMat);
batch.setPipeline(getOpaquePipeline()); batch.setPipeline(getOpaquePipeline());
batch.setResourceTexture(0, primaryDepth); batch.setResourceTexture(0, primaryDepth);
@ -369,13 +372,13 @@ void DrawBackgroundDeferred::run(const SceneContextPointer& sceneContext, const
doInBatch(args->_context, [=](gpu::Batch& batch) { doInBatch(args->_context, [=](gpu::Batch& batch) {
args->_batch = &batch; args->_batch = &batch;
// auto primaryFboColorDepthStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferDepthStencilColor(); auto primaryFboColorStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferStencilColor();
auto primaryFboColorDepthStencil = DependencyManager::get<FramebufferCache>()->getPrimaryFramebufferDepthColor();
auto primaryFboFull = DependencyManager::get<FramebufferCache>()->getPrimaryFramebuffer(); auto primaryFboFull = DependencyManager::get<FramebufferCache>()->getPrimaryFramebuffer();
batch.setFramebuffer(primaryFboColorDepthStencil);
batch.enableSkybox(true); batch.enableSkybox(true);
batch.setFramebuffer(primaryFboColorStencil);
batch.setViewportTransform(args->_viewport); batch.setViewportTransform(args->_viewport);
batch.setStateScissorRect(args->_viewport); batch.setStateScissorRect(args->_viewport);

View file

@ -12,22 +12,18 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
in vec2 varTexCoord0; in vec2 varTexCoord0;
out vec4 outFragColor; out vec4 outFragColor;
uniform sampler2D depthTexture; uniform sampler2D depthTexture;
void main(void) { void main(void) {
// outFragColor = vec4(varTexCoord0, 0.0, 1.0);
float depth = texture(depthTexture, varTexCoord0).r; float depth = texture(depthTexture, varTexCoord0.xy).r;
outFragColor = vec4(1.0, 0.0, 0.0, 1.0);
if (depth >= 0.9) { if (depth < 1.0) {
outFragColor = vec4(1.0, 0.0, 0.0, 1.0); outFragColor = vec4(0.0, 1.0, 0.0, 1.0);
} else { } else {
discard; discard;
} }