fix setScissorRect crash (#20348)

When part of scissorRect is out of render target viewport, it crash on mac.
Error message like this: -[MTLDebugRenderCommandEncoder setScissorRect:]:2703: failed assertion `(rect.y(568) + rect.height(447))(1015) must be <= render pass height(1000)'
This commit is contained in:
codetypes 2019-11-20 11:03:59 +08:00 committed by minggo
parent 977510f41d
commit d05967b69a
1 changed files with 17 additions and 4 deletions

View File

@ -205,6 +205,10 @@ namespace
return nil; return nil;
} }
} }
inline int clamp(int value, int min, int max) {
return std::min(max, std::max(min, value));
}
} }
CommandBufferMTL::CommandBufferMTL(DeviceMTL* deviceMTL) CommandBufferMTL::CommandBufferMTL(DeviceMTL* deviceMTL)
@ -484,10 +488,19 @@ void CommandBufferMTL::setScissorRect(bool isEnabled, float x, float y, float wi
MTLScissorRect scissorRect; MTLScissorRect scissorRect;
if(isEnabled) if(isEnabled)
{ {
scissorRect.x = x; y = _renderTargetHeight - height - y;
scissorRect.y = _renderTargetHeight - height - y; int minX = clamp((int)x, 0, (int)_renderTargetWidth);
scissorRect.width = width; int minY = clamp((int)y, 0, (int)_renderTargetHeight);
scissorRect.height = height; int maxX = clamp((int)(x + width), 0, (int)_renderTargetWidth);
int maxY = clamp((int)(y + height), 0, (int)_renderTargetHeight);
scissorRect.x = minX;
scissorRect.y = minY;
scissorRect.width = maxX - minX;
scissorRect.height = maxY - minY;
if (scissorRect.width == 0 || scissorRect.height == 0) {
scissorRect.width = 0;
scissorRect.height = 0;
}
} }
else else
{ {