OpenGL and Metal viewport rectangle fixes

This commit is contained in:
Jack Andersen 2016-02-26 15:38:13 -10:00
parent 8296514a61
commit cf9e9d80eb
2 changed files with 17 additions and 19 deletions

View File

@ -1137,19 +1137,21 @@ struct GLCommandQueue : IGraphicsCommandQueue
void resolveBindTexture(ITextureR* texture, const SWindowRect& rect, bool tlOrigin, bool color, bool depth) void resolveBindTexture(ITextureR* texture, const SWindowRect& rect, bool tlOrigin, bool color, bool depth)
{ {
GLTextureR* tex = static_cast<GLTextureR*>(texture);
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf]; std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
cmds.emplace_back(Command::Op::ResolveBindTexture); cmds.emplace_back(Command::Op::ResolveBindTexture);
cmds.back().resolveTex = texture; cmds.back().resolveTex = texture;
cmds.back().resolveColor = color; cmds.back().resolveColor = color;
cmds.back().resolveDepth = depth; cmds.back().resolveDepth = depth;
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height));
SWindowRect& targetRect = cmds.back().rect; SWindowRect& targetRect = cmds.back().rect;
targetRect.location[0] = std::max(0, rect.location[0]); targetRect.location[0] = intersectRect.location[0];
if (tlOrigin) if (tlOrigin)
targetRect.location[1] = std::max(0, int(static_cast<GLTextureR*>(texture)->m_height) - rect.location[1] - rect.size[1]); targetRect.location[1] = tex->m_height - intersectRect.location[1] - intersectRect.size[1];
else else
targetRect.location[1] = std::max(0, rect.location[1]); targetRect.location[1] = intersectRect.location[1];
targetRect.size[0] = std::max(0, rect.size[0]); targetRect.size[0] = intersectRect.size[0];
targetRect.size[1] = std::max(0, rect.size[1]); targetRect.size[1] = intersectRect.size[1];
} }
void resolveDisplay(ITextureR* source) void resolveDisplay(ITextureR* source)

View File

@ -249,6 +249,7 @@ class MetalTextureR : public ITextureR
{ {
desc.textureType = MTLTextureType2DMultisample; desc.textureType = MTLTextureType2DMultisample;
desc.sampleCount = samples; desc.sampleCount = samples;
desc.usage = MTLTextureUsageRenderTarget;
m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc]; m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc];
if (enableShaderBindTexture) if (enableShaderBindTexture)
@ -260,19 +261,12 @@ class MetalTextureR : public ITextureR
desc.usage = MTLTextureUsageRenderTarget; desc.usage = MTLTextureUsageRenderTarget;
desc.pixelFormat = MTLPixelFormatDepth32Float; desc.pixelFormat = MTLPixelFormatDepth32Float;
m_depthTex = [ctx->m_dev newTextureWithDescriptor:desc]; 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 else
{ {
desc.textureType = MTLTextureType2D; desc.textureType = MTLTextureType2D;
desc.sampleCount = 1; desc.sampleCount = 1;
desc.usage = MTLTextureUsageRenderTarget;
m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc]; m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc];
if (enableShaderBindTexture) if (enableShaderBindTexture)
@ -627,9 +621,10 @@ struct MetalCommandQueue : IGraphicsCommandQueue
{ {
if (m_boundTarget) if (m_boundTarget)
{ {
MTLScissorRect scissor = {NSUInteger(rect.location[0]), SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, m_boundTarget->m_width, m_boundTarget->m_height));
NSUInteger(m_boundTarget->m_height - rect.location[1] - rect.size[1]), MTLScissorRect scissor = {NSUInteger(intersectRect.location[0]),
NSUInteger(rect.size[0]), NSUInteger(rect.size[1])}; NSUInteger(m_boundTarget->m_height - intersectRect.location[1] - intersectRect.size[1]),
NSUInteger(intersectRect.size[0]), NSUInteger(intersectRect.size[1])};
[m_enc setScissorRect:scissor]; [m_enc setScissorRect:scissor];
} }
} }
@ -702,14 +697,15 @@ struct MetalCommandQueue : IGraphicsCommandQueue
[m_enc endEncoding]; [m_enc endEncoding];
@autoreleasepool @autoreleasepool
{ {
NSUInteger y = tlOrigin ? rect.location[1] : tex->m_height - rect.location[1] - rect.size[1]; SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height));
MTLOrigin origin = {NSUInteger(rect.location[0]), y, 0}; 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<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder]; id<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder];
[blitEnc copyFromTexture:tex->m_colorTex [blitEnc copyFromTexture:tex->m_colorTex
sourceSlice:0 sourceSlice:0
sourceLevel:0 sourceLevel:0
sourceOrigin:origin sourceOrigin:origin
sourceSize:MTLSizeMake(rect.size[0], rect.size[1], 1) sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1)
toTexture:tex->m_colorBindTex toTexture:tex->m_colorBindTex
destinationSlice:0 destinationSlice:0
destinationLevel:0 destinationLevel:0