From afe2e5e8a9ee56aef0fd52a7e8ada6cae92c5df1 Mon Sep 17 00:00:00 2001 From: Pablo Amorin Date: Wed, 14 Nov 2018 17:40:28 -0300 Subject: [PATCH 1/2] [BUGFIX] Consecutive Masked Sprites use only the first's mask #138 --- oxygine/src/oxygine/STDRenderDelegate.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/oxygine/src/oxygine/STDRenderDelegate.cpp b/oxygine/src/oxygine/STDRenderDelegate.cpp index 13c283ff8..cc3f192bd 100644 --- a/oxygine/src/oxygine/STDRenderDelegate.cpp +++ b/oxygine/src/oxygine/STDRenderDelegate.cpp @@ -156,15 +156,14 @@ namespace oxygine ShaderProgramChangedHook hook; hook.hook = [&]() { - IVideoDriver::instance->setUniform("clip_mask", clipMask); IVideoDriver::instance->setUniform("msk", msk, 4); - }; renderer->pushShaderSetHook(&hook); renderer->setBaseShaderFlags(baseShaderFlags); + hook.hook(); sprite->Sprite::render(parentRS); @@ -236,4 +235,4 @@ namespace oxygine { } -} \ No newline at end of file +} From 74fb326fd2b626c9ec8c99a6203472733fe4169b Mon Sep 17 00:00:00 2001 From: Pablo Amorin Date: Wed, 6 Mar 2019 18:21:58 -0300 Subject: [PATCH 2/2] [Fix] Masked Sprites dont restore masks --- oxygine/src/oxygine/STDRenderDelegate.cpp | 54 +++++++++-------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/oxygine/src/oxygine/STDRenderDelegate.cpp b/oxygine/src/oxygine/STDRenderDelegate.cpp index cc3f192bd..99267724b 100644 --- a/oxygine/src/oxygine/STDRenderDelegate.cpp +++ b/oxygine/src/oxygine/STDRenderDelegate.cpp @@ -89,7 +89,8 @@ namespace oxygine driver->setScissorRect(scissorEnabled ? &scissorRect : 0); } } - +static std::list clipMask_stack; +static std::list msk_stack; void STDRenderDelegate::render(MaskedSprite* sprite, const RenderState& parentRS) { spSprite maskSprite = sprite->getMask(); @@ -109,6 +110,7 @@ namespace oxygine Material::null->apply(); + Vector3 msk[4]; Transform world = maskSprite->computeGlobalTransform(); RectF maskDest = maskSprite->getDestRect(); @@ -118,12 +120,16 @@ namespace oxygine bool useR = sprite->getUseRChannel(); bool rchannel = useR ? true : (df.alpha ? true : false); spNativeTexture maskTexture = useR ? df.base : (df.alpha ? df.alpha : df.base); - STDRenderer* renderer = STDRenderer::getCurrent(); + int sflags = renderer->getBaseShaderFlags(); + int baseShaderFlags = sflags; + baseShaderFlags |= UberShaderProgram::MASK; + if (rchannel) + baseShaderFlags |= UberShaderProgram::MASK_R_CHANNEL; -#if 1 + spNativeTexture oldMask = rsCache().getTexture(UberShaderProgram::SAMPLER_MASK); ClipUV clipUV = ClipUV( world.transform(maskDest.getLeftTop()), world.transform(maskDest.getRightTop()), @@ -135,22 +141,14 @@ namespace oxygine Vector2 v(1.0f / maskTexture->getWidth(), 1.0f / maskTexture->getHeight()); maskSrc.expand(v, v); - - - int sflags = renderer->getBaseShaderFlags(); - int baseShaderFlags = sflags; - - baseShaderFlags |= UberShaderProgram::MASK; - if (rchannel) - baseShaderFlags |= UberShaderProgram::MASK_R_CHANNEL; - - Vector3 msk[4]; - clipUV.get(msk); Vector4 clipMask = Vector4(maskSrc.getLeft(), maskSrc.getTop(), maskSrc.getRight(), maskSrc.getBottom()); - rsCache().setTexture(UberShaderProgram::SAMPLER_MASK, maskTexture); + clipMask_stack.push_back(clipMask); + msk_stack.push_back(clipUV); + + rsCache().setTexture(UberShaderProgram::SAMPLER_MASK, maskTexture); ShaderProgramChangedHook hook; @@ -171,24 +169,16 @@ namespace oxygine renderer->popShaderSetHook(); renderer->setBaseShaderFlags(sflags); -#else - - MaskedRenderer mr(maskTexture, maskSrc, maskDest, world, rchannel, renderer->getDriver()); - renderer->swapVerticesData(mr); - - mr.setViewProj(renderer->getViewProjection()); - mr.begin(); - - RenderState rs = parentRS; - sprite->Sprite::render(rs); - mr.end(); - - Material::null->apply(); - - renderer->swapVerticesData(mr); - renderer->begin(); -#endif + if (oldMask) rsCache().setTexture(UberShaderProgram::SAMPLER_MASK,oldMask); + clipMask_stack.pop_back(); + if (!clipMask_stack.empty()) + IVideoDriver::instance->setUniform("clip_mask", clipMask_stack.back()); + msk_stack.pop_back(); + if (!msk_stack.empty()) { + msk_stack.back().get(msk); + IVideoDriver::instance->setUniform("msk", msk, 4); + } } void STDRenderDelegate::doRender(Sprite* sprite, const RenderState& rs)