Skip to content

Commit f4018da

Browse files
blaztinnblaztomazicO7
authored andcommitted
fix(metal): clear only bound textures
`clearQuad()` can be operating on framebuffers that don't have all the color/depth/stencil buffers attached. For example, if there is no stencil buffer in the framebuffer, the Metal driver asserts with: ``` validateDepthStencilState: failed assertion `MTLDepthStencilDescriptor uses frontFaceStencil but MTLRenderPassDescriptor has a nil stencilAttachment texture' ``` Copy the logic from `setFrameBuffer()` to check which buffers we can clear. Using this info set the pipeline state that is valid for currently bound buffers.
1 parent 61c770b commit f4018da

File tree

1 file changed

+80
-11
lines changed

1 file changed

+80
-11
lines changed

src/renderer_mtl.mm

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,20 +1712,89 @@ void commit(UniformBuffer& _uniformBuffer)
17121712

17131713
void clearQuad(ClearQuad& _clearQuad, const Rect& /*_rect*/, const Clear& _clear, const float _palette[][4])
17141714
{
1715+
bool clear_color;
1716+
bool clear_depth;
1717+
bool clear_stencil;
1718+
1719+
if (!isValid(m_fbh) || m_frameBuffers[m_fbh.idx].m_swapChain)
1720+
{
1721+
SwapChainMtl* swapChain =
1722+
!isValid(m_fbh) ?
1723+
m_mainFrameBuffer.m_swapChain :
1724+
m_frameBuffers[m_fbh.idx].m_swapChain;
1725+
if (NULL != swapChain->m_backBufferColorMsaa)
1726+
{
1727+
clear_color = swapChain->m_backBufferColorMsaa != NULL;
1728+
}
1729+
else
1730+
{
1731+
clear_color = (NULL != m_screenshotTarget ? m_screenshotTarget.m_obj : swapChain->currentDrawable().texture) != NULL;
1732+
}
1733+
1734+
clear_depth = swapChain->m_backBufferDepth != NULL;
1735+
clear_stencil = swapChain->m_backBufferStencil != NULL;
1736+
}
1737+
else
1738+
{
1739+
FrameBufferMtl& frameBuffer = m_frameBuffers[m_fbh.idx];
1740+
1741+
clear_color = false;
1742+
for (uint32_t ii = 0; ii < frameBuffer.m_num; ++ii)
1743+
{
1744+
const TextureMtl& texture = m_textures[frameBuffer.m_colorHandle[ii].idx];
1745+
clear_color = (texture.m_ptrMsaa ? texture.m_ptrMsaa : texture.m_ptr) != NULL;
1746+
if (clear_color)
1747+
{
1748+
break;
1749+
}
1750+
}
1751+
1752+
if (isValid(frameBuffer.m_depthHandle))
1753+
{
1754+
const TextureMtl& texture = m_textures[frameBuffer.m_depthHandle.idx];
1755+
clear_depth = (texture.m_ptrMsaa ? texture.m_ptrMsaa : texture.m_ptr) != NULL;
1756+
clear_stencil = texture.m_ptrStencil != NULL;
1757+
1758+
if (texture.m_textureFormat == TextureFormat::D24S8)
1759+
{
1760+
if (texture.m_ptr.pixelFormat() == 255 /* Depth24Unorm_Stencil8 */
1761+
|| texture.m_ptr.pixelFormat() == 260 /* Depth32Float_Stencil8 */)
1762+
{
1763+
clear_stencil = clear_depth;
1764+
}
1765+
else
1766+
{
1767+
clear_stencil = (texture.m_ptrMsaa ? texture.m_ptrMsaa : texture.m_ptrStencil) != NULL;
1768+
}
1769+
}
1770+
}
1771+
else
1772+
{
1773+
clear_depth = false;
1774+
clear_stencil = false;
1775+
}
1776+
}
1777+
17151778
uint64_t state = 0;
1716-
state |= _clear.m_flags & BGFX_CLEAR_COLOR ? BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A : 0;
1717-
state |= _clear.m_flags & BGFX_CLEAR_DEPTH ? BGFX_STATE_DEPTH_TEST_ALWAYS|BGFX_STATE_WRITE_Z : 0;
1779+
if (clear_color) {
1780+
state |= _clear.m_flags & BGFX_CLEAR_COLOR ? BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A : 0;
1781+
}
1782+
if (clear_depth) {
1783+
state |= _clear.m_flags & BGFX_CLEAR_DEPTH ? BGFX_STATE_DEPTH_TEST_ALWAYS|BGFX_STATE_WRITE_Z : 0;
1784+
}
17181785

17191786
uint64_t stencil = 0;
1720-
stencil |= _clear.m_flags & BGFX_CLEAR_STENCIL ? 0
1721-
| BGFX_STENCIL_TEST_ALWAYS
1722-
| BGFX_STENCIL_FUNC_REF(_clear.m_stencil)
1723-
| BGFX_STENCIL_FUNC_RMASK(0xff)
1724-
| BGFX_STENCIL_OP_FAIL_S_REPLACE
1725-
| BGFX_STENCIL_OP_FAIL_Z_REPLACE
1726-
| BGFX_STENCIL_OP_PASS_Z_REPLACE
1727-
: 0
1728-
;
1787+
if (clear_stencil) {
1788+
stencil |= _clear.m_flags & BGFX_CLEAR_STENCIL ? 0
1789+
| BGFX_STENCIL_TEST_ALWAYS
1790+
| BGFX_STENCIL_FUNC_REF(_clear.m_stencil)
1791+
| BGFX_STENCIL_FUNC_RMASK(0xff)
1792+
| BGFX_STENCIL_OP_FAIL_S_REPLACE
1793+
| BGFX_STENCIL_OP_FAIL_Z_REPLACE
1794+
| BGFX_STENCIL_OP_PASS_Z_REPLACE
1795+
: 0
1796+
;
1797+
}
17291798

17301799
setDepthStencilState(state, stencil);
17311800

0 commit comments

Comments
 (0)