From cf9e9d80eb21c086e3be2cf0a93ca30121320996 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 26 Feb 2016 15:38:13 -1000 Subject: [PATCH] OpenGL and Metal viewport rectangle fixes --- lib/graphicsdev/GL.cpp | 12 +++++++----- lib/graphicsdev/Metal.mm | 24 ++++++++++-------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/graphicsdev/GL.cpp b/lib/graphicsdev/GL.cpp index f09a8f0..5412b42 100644 --- a/lib/graphicsdev/GL.cpp +++ b/lib/graphicsdev/GL.cpp @@ -1137,19 +1137,21 @@ struct GLCommandQueue : IGraphicsCommandQueue void resolveBindTexture(ITextureR* texture, const SWindowRect& rect, bool tlOrigin, bool color, bool depth) { + GLTextureR* tex = static_cast(texture); std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::ResolveBindTexture); cmds.back().resolveTex = texture; cmds.back().resolveColor = color; cmds.back().resolveDepth = depth; + SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height)); SWindowRect& targetRect = cmds.back().rect; - targetRect.location[0] = std::max(0, rect.location[0]); + targetRect.location[0] = intersectRect.location[0]; if (tlOrigin) - targetRect.location[1] = std::max(0, int(static_cast(texture)->m_height) - rect.location[1] - rect.size[1]); + targetRect.location[1] = tex->m_height - intersectRect.location[1] - intersectRect.size[1]; else - targetRect.location[1] = std::max(0, rect.location[1]); - targetRect.size[0] = std::max(0, rect.size[0]); - targetRect.size[1] = std::max(0, rect.size[1]); + targetRect.location[1] = intersectRect.location[1]; + targetRect.size[0] = intersectRect.size[0]; + targetRect.size[1] = intersectRect.size[1]; } void resolveDisplay(ITextureR* source) diff --git a/lib/graphicsdev/Metal.mm b/lib/graphicsdev/Metal.mm index 303aab8..1190bd4 100644 --- a/lib/graphicsdev/Metal.mm +++ b/lib/graphicsdev/Metal.mm @@ -249,6 +249,7 @@ class MetalTextureR : public ITextureR { desc.textureType = MTLTextureType2DMultisample; desc.sampleCount = samples; + desc.usage = MTLTextureUsageRenderTarget; m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc]; if (enableShaderBindTexture) @@ -260,19 +261,12 @@ class MetalTextureR : public ITextureR desc.usage = MTLTextureUsageRenderTarget; desc.pixelFormat = MTLPixelFormatDepth32Float; m_depthTex = [ctx->m_dev newTextureWithDescriptor:desc]; - - m_passDesc.colorAttachments[0].texture = m_colorTex; - m_passDesc.colorAttachments[0].loadAction = MTLLoadActionLoad; - m_passDesc.colorAttachments[0].storeAction = MTLStoreActionStore; - - m_passDesc.depthAttachment.texture = m_depthTex; - m_passDesc.depthAttachment.loadAction = MTLLoadActionLoad; - m_passDesc.depthAttachment.storeAction = MTLStoreActionStore; } else { desc.textureType = MTLTextureType2D; desc.sampleCount = 1; + desc.usage = MTLTextureUsageRenderTarget; m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc]; if (enableShaderBindTexture) @@ -627,9 +621,10 @@ struct MetalCommandQueue : IGraphicsCommandQueue { if (m_boundTarget) { - MTLScissorRect scissor = {NSUInteger(rect.location[0]), - NSUInteger(m_boundTarget->m_height - rect.location[1] - rect.size[1]), - NSUInteger(rect.size[0]), NSUInteger(rect.size[1])}; + SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, m_boundTarget->m_width, m_boundTarget->m_height)); + MTLScissorRect scissor = {NSUInteger(intersectRect.location[0]), + NSUInteger(m_boundTarget->m_height - intersectRect.location[1] - intersectRect.size[1]), + NSUInteger(intersectRect.size[0]), NSUInteger(intersectRect.size[1])}; [m_enc setScissorRect:scissor]; } } @@ -702,14 +697,15 @@ struct MetalCommandQueue : IGraphicsCommandQueue [m_enc endEncoding]; @autoreleasepool { - NSUInteger y = tlOrigin ? rect.location[1] : tex->m_height - rect.location[1] - rect.size[1]; - MTLOrigin origin = {NSUInteger(rect.location[0]), y, 0}; + SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height)); + NSUInteger y = tlOrigin ? intersectRect.location[1] : int(tex->m_height) - intersectRect.location[1] - intersectRect.size[1]; + MTLOrigin origin = {NSUInteger(intersectRect.location[0]), y, 0}; id blitEnc = [m_cmdBuf blitCommandEncoder]; [blitEnc copyFromTexture:tex->m_colorTex sourceSlice:0 sourceLevel:0 sourceOrigin:origin - sourceSize:MTLSizeMake(rect.size[0], rect.size[1], 1) + sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1) toTexture:tex->m_colorBindTex destinationSlice:0 destinationLevel:0