Merge pull request #13251 from hrydgard/fit-viewport-to-scissor

Fit viewport to scissor
This commit is contained in:
Henrik Rydgård 2020-08-05 23:48:29 +02:00 committed by GitHub
commit 937042b3db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 34 deletions

View file

@ -568,7 +568,7 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
renderHeightFactor = renderHeight / 272.0f; renderHeightFactor = renderHeight / 272.0f;
} }
renderX += gstate_c.curRTOffsetX * renderWidthFactor; renderX = gstate_c.curRTOffsetX;
// Scissor // Scissor
int scissorX1 = gstate.getScissorX1(); int scissorX1 = gstate.getScissorX1();
@ -576,8 +576,6 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
int scissorX2 = gstate.getScissorX2() + 1; int scissorX2 = gstate.getScissorX2() + 1;
int scissorY2 = gstate.getScissorY2() + 1; int scissorY2 = gstate.getScissorY2() + 1;
// This is a bit of a hack as the render buffer isn't always that size
// We always scissor on non-buffered so that clears don't spill outside the frame.
out.scissorEnable = true; out.scissorEnable = true;
if (scissorX2 < scissorX1 || scissorY2 < scissorY1) { if (scissorX2 < scissorX1 || scissorY2 < scissorY1) {
out.scissorX = 0; out.scissorX = 0;
@ -585,8 +583,8 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
out.scissorW = 0; out.scissorW = 0;
out.scissorH = 0; out.scissorH = 0;
} else { } else {
out.scissorX = renderX + displayOffsetX + scissorX1 * renderWidthFactor; out.scissorX = (renderX * renderWidthFactor) + displayOffsetX + scissorX1 * renderWidthFactor;
out.scissorY = renderY + displayOffsetY + scissorY1 * renderHeightFactor; out.scissorY = (renderY * renderHeightFactor) + displayOffsetY + scissorY1 * renderHeightFactor;
out.scissorW = (scissorX2 - scissorX1) * renderWidthFactor; out.scissorW = (scissorX2 - scissorX1) * renderWidthFactor;
out.scissorH = (scissorY2 - scissorY1) * renderHeightFactor; out.scissorH = (scissorY2 - scissorY1) * renderHeightFactor;
} }
@ -598,8 +596,8 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
float offsetY = gstate.getOffsetY(); float offsetY = gstate.getOffsetY();
if (throughmode) { if (throughmode) {
out.viewportX = renderX + displayOffsetX; out.viewportX = renderX * renderWidthFactor + displayOffsetX;
out.viewportY = renderY + displayOffsetY; out.viewportY = renderY * renderHeightFactor + displayOffsetY;
out.viewportW = curRTWidth * renderWidthFactor; out.viewportW = curRTWidth * renderWidthFactor;
out.viewportH = curRTHeight * renderHeightFactor; out.viewportH = curRTHeight * renderHeightFactor;
out.depthRangeMin = ToScaledDepthFromIntegerScale(0); out.depthRangeMin = ToScaledDepthFromIntegerScale(0);
@ -629,12 +627,6 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
float vpWidth = fabsf(gstate_c.vpWidth); float vpWidth = fabsf(gstate_c.vpWidth);
float vpHeight = fabsf(gstate_c.vpHeight); float vpHeight = fabsf(gstate_c.vpHeight);
// This multiplication should probably be done after viewport clipping. Would let us very slightly simplify the clipping logic?
vpX0 *= renderWidthFactor;
vpY0 *= renderHeightFactor;
vpWidth *= renderWidthFactor;
vpHeight *= renderHeightFactor;
// We used to apply the viewport here via glstate, but there are limits which vary by driver. // We used to apply the viewport here via glstate, but there are limits which vary by driver.
// This may mean some games won't work, or at least won't work at higher render resolutions. // This may mean some games won't work, or at least won't work at higher render resolutions.
// So we apply it in the shader instead. // So we apply it in the shader instead.
@ -648,27 +640,46 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
float hScale = 1.0f; float hScale = 1.0f;
float yOffset = 0.0f; float yOffset = 0.0f;
if (!gstate_c.Supports(GPU_SUPPORTS_LARGE_VIEWPORTS)) { // If we're within the bounds, we want clipping the viewport way. So leave it be.
// If we're within the bounds, we want clipping the viewport way. So leave it be. {
if (left < 0.0f || right > renderWidth) { float overageLeft = std::max(-left, 0.0f);
float overageLeft = std::max(-left, 0.0f); float overageRight = std::max(right - bufferWidth, 0.0f);
float overageRight = std::max(right - renderWidth, 0.0f);
// Our center drifted by the difference in overages.
float drift = overageRight - overageLeft;
// Expand viewport to cover scissor region. The viewport doesn't clip on the PSP.
if (right < scissorX2) {
overageRight -= scissorX2 - right;
}
if (left > scissorX1) {
overageLeft += scissorX1 - left;
}
// Our center drifted by the difference in overages.
float drift = overageRight - overageLeft;
if (overageLeft != 0.0f || overageRight != 0.0f) {
left += overageLeft; left += overageLeft;
right -= overageRight; right -= overageRight;
wScale = vpWidth / (right - left); wScale = vpWidth / (right - left);
xOffset = drift / (right - left); xOffset = drift / (right - left);
} }
}
if (top < 0.0f || bottom > renderHeight) { {
float overageTop = std::max(-top, 0.0f); float overageTop = std::max(-top, 0.0f);
float overageBottom = std::max(bottom - renderHeight, 0.0f); float overageBottom = std::max(bottom - bufferHeight, 0.0f);
// Our center drifted by the difference in overages.
float drift = overageBottom - overageTop;
// Expand viewport to cover scissor region. The viewport doesn't clip on the PSP.
if (bottom < scissorY2) {
overageBottom -= scissorY2 - bottom;
}
if (top > scissorY1) {
overageTop += scissorY1 - top;
}
// Our center drifted by the difference in overages.
float drift = overageBottom - overageTop;
if (overageTop != 0.0f || overageBottom != 0.0f) {
top += overageTop; top += overageTop;
bottom -= overageBottom; bottom -= overageBottom;
@ -677,10 +688,10 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
} }
} }
out.viewportX = left + displayOffsetX; out.viewportX = left * renderWidthFactor + displayOffsetX;
out.viewportY = top + displayOffsetY; out.viewportY = top * renderHeightFactor + displayOffsetY;
out.viewportW = right - left; out.viewportW = (right - left) * renderWidthFactor;
out.viewportH = bottom - top; out.viewportH = (bottom - top) * renderHeightFactor;
// The depth viewport parameters are the same, but we handle it a bit differently. // The depth viewport parameters are the same, but we handle it a bit differently.
// When clipping is enabled, depth is clamped to [0, 65535]. And minz/maxz discard. // When clipping is enabled, depth is clamped to [0, 65535]. And minz/maxz discard.

View file

@ -15,13 +15,13 @@
using namespace Lin; using namespace Lin;
static void ConvertProjMatrixToVulkan(Matrix4x4 &in) { static void ConvertProjMatrixToVulkan(Matrix4x4 &in) {
const Vec3 trans(0, 0, gstate_c.vpZOffset * 0.5f + 0.5f); const Vec3 trans(gstate_c.vpXOffset, gstate_c.vpYOffset, gstate_c.vpZOffset * 0.5f + 0.5f);
const Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f); const Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f);
in.translateAndScale(trans, scale); in.translateAndScale(trans, scale);
} }
static void ConvertProjMatrixToD3D11(Matrix4x4 &in) { static void ConvertProjMatrixToD3D11(Matrix4x4 &in) {
const Vec3 trans(0, 0, gstate_c.vpZOffset * 0.5f + 0.5f); const Vec3 trans(gstate_c.vpXOffset, gstate_c.vpYOffset, gstate_c.vpZOffset * 0.5f + 0.5f);
const Vec3 scale(gstate_c.vpWidthScale, -gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f); const Vec3 scale(gstate_c.vpWidthScale, -gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f);
in.translateAndScale(trans, scale); in.translateAndScale(trans, scale);
} }

View file

@ -143,7 +143,6 @@ void GPU_D3D11::CheckGPUFeatures() {
#endif #endif
features |= GPU_SUPPORTS_OES_TEXTURE_NPOT; features |= GPU_SUPPORTS_OES_TEXTURE_NPOT;
features |= GPU_SUPPORTS_LARGE_VIEWPORTS;
if (draw_->GetDeviceCaps().dualSourceBlend) if (draw_->GetDeviceCaps().dualSourceBlend)
features |= GPU_SUPPORTS_DUALSOURCE_BLEND; features |= GPU_SUPPORTS_DUALSOURCE_BLEND;
if (draw_->GetDeviceCaps().depthClampSupported) if (draw_->GetDeviceCaps().depthClampSupported)

View file

@ -482,7 +482,7 @@ enum {
GPU_SUPPORTS_16BIT_FORMATS = FLAG_BIT(13), GPU_SUPPORTS_16BIT_FORMATS = FLAG_BIT(13),
GPU_SUPPORTS_DEPTH_CLAMP = FLAG_BIT(14), GPU_SUPPORTS_DEPTH_CLAMP = FLAG_BIT(14),
GPU_SUPPORTS_32BIT_INT_FSHADER = FLAG_BIT(15), GPU_SUPPORTS_32BIT_INT_FSHADER = FLAG_BIT(15),
GPU_SUPPORTS_LARGE_VIEWPORTS = FLAG_BIT(16), // Free bit: 16
GPU_SUPPORTS_ACCURATE_DEPTH = FLAG_BIT(17), GPU_SUPPORTS_ACCURATE_DEPTH = FLAG_BIT(17),
GPU_SUPPORTS_VAO = FLAG_BIT(18), GPU_SUPPORTS_VAO = FLAG_BIT(18),
GPU_SUPPORTS_ANY_COPY_IMAGE = FLAG_BIT(19), GPU_SUPPORTS_ANY_COPY_IMAGE = FLAG_BIT(19),

View file

@ -227,7 +227,6 @@ void GPU_Vulkan::CheckGPUFeatures() {
features |= GPU_SUPPORTS_BLEND_MINMAX; features |= GPU_SUPPORTS_BLEND_MINMAX;
features |= GPU_SUPPORTS_ANY_COPY_IMAGE; features |= GPU_SUPPORTS_ANY_COPY_IMAGE;
features |= GPU_SUPPORTS_OES_TEXTURE_NPOT; features |= GPU_SUPPORTS_OES_TEXTURE_NPOT;
features |= GPU_SUPPORTS_LARGE_VIEWPORTS;
features |= GPU_SUPPORTS_INSTANCE_RENDERING; features |= GPU_SUPPORTS_INSTANCE_RENDERING;
features |= GPU_SUPPORTS_VERTEX_TEXTURE_FETCH; features |= GPU_SUPPORTS_VERTEX_TEXTURE_FETCH;
features |= GPU_SUPPORTS_TEXTURE_FLOAT; features |= GPU_SUPPORTS_TEXTURE_FLOAT;