fix wrongly render target size (#3053)

This commit is contained in:
coulsonwang
2020-11-20 13:36:31 +08:00
committed by GitHub
parent 21da438185
commit ff205210bb
5 changed files with 20 additions and 10 deletions
@@ -92,7 +92,7 @@ void CCMTLCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *fb
MTLRenderPassDescriptor *mtlRenderPassDescriptor = static_cast<CCMTLRenderPass *>(renderPass)->getMTLRenderPassDescriptor();
if(!isRenderingEntireDrawable(renderArea)){
//Metal doesn't apply the viewports and scissors to renderpass load-action clearing.
mu::clearRenderArea(_mtlDevice, _mtlCommandBuffer, _mtkView.drawableSize, renderPass, mtlRenderPassDescriptor, renderArea, colors, depth, stencil, _hasScreenClean);
mu::clearRenderArea(_mtlDevice, _mtlCommandBuffer, renderPass, renderArea, colors, depth, stencil, _hasScreenClean);
} else {
const auto &colorAttachments = renderPass->getColorAttachments();
const auto colorAttachmentCount = colorAttachments.size();
@@ -22,10 +22,14 @@ public:
CC_INLINE MTLRenderPassDescriptor *getMTLRenderPassDescriptor() const { return _mtlRenderPassDescriptor; }
CC_INLINE size_t getColorRenderTargetNums() const { return _colorRenderTargetNums; }
CC_INLINE uint getRenderTargetWidth() const { return _renderTargetWidth; }
CC_INLINE uint getRenderTargetHeight() const { return _renderTargetHeight; }
private:
MTLRenderPassDescriptor *_mtlRenderPassDescriptor = nil;
size_t _colorRenderTargetNums = 0;
uint _renderTargetWidth = 0;
uint _renderTargetHeight = 0;
};
} // namespace gfx
@@ -54,6 +54,10 @@ void CCMTLRenderPass::setColorAttachment(size_t slot, id<MTLTexture> texture, in
_mtlRenderPassDescriptor.colorAttachments[slot].texture = texture;
_mtlRenderPassDescriptor.colorAttachments[slot].level = level;
if(slot == 0) {
_renderTargetWidth = texture.width;
_renderTargetHeight = texture.height;
}
}
void CCMTLRenderPass::setDepthStencilAttachment(id<MTLTexture> texture, int level) {
+1 -2
View File
@@ -6,7 +6,6 @@
#import <Metal/MTLSampler.h>
#import <Metal/MTLTexture.h>
#import <Metal/MTLVertexDescriptor.h>
#import <CoreGraphics/CGGeometry.h>
#include <unordered_map>
namespace cc {
@@ -71,7 +70,7 @@ uint getBlockSzie(Format format);
uint getBytesPerRow(Format format, uint width);
bool pixelFormatIsColorRenderable(Format format);
bool isSamplerDescriptorCompareFunctionSupported(uint family);
void clearRenderArea(CCMTLDevice* device, id<MTLCommandBuffer> commandBuffer, CGSize drawableSize, RenderPass *renderPass, MTLRenderPassDescriptor *renderPassDescriptor, const Rect &renderArea, const Color *colors, float depth, int stencil, bool &hasScreenClean);
void clearRenderArea(CCMTLDevice* device, id<MTLCommandBuffer> commandBuffer, RenderPass *renderPass, const Rect &renderArea, const Color *colors, float depth, int stencil, bool &hasScreenClean);
CC_INLINE uint alignUp(uint inSize, uint align) { return ((inSize + align - 1) / align) * align; }
} // namespace mu
+10 -7
View File
@@ -11,6 +11,7 @@
#include "MTLDevice.h"
#include "MTLShader.h"
#include "MTLPipelineState.h"
#include "MTLRenderPass.h"
#include <vector>
namespace cc {
@@ -1535,18 +1536,20 @@ CCMTLGPUPipelineState* getClearRenderPassPipelineState(CCMTLDevice *device, Rend
return static_cast<CCMTLPipelineState*>(pipelineState)->getGPUPipelineState();
}
void clearRenderArea(CCMTLDevice *device, id<MTLCommandBuffer> commandBuffer, CGSize drawableSize, RenderPass *renderPass, MTLRenderPassDescriptor *renderPassDescriptor, const Rect &renderArea, const Color *colors, float depth, int stencil, bool &hasScreenClean) {
void clearRenderArea(CCMTLDevice *device, id<MTLCommandBuffer> commandBuffer, RenderPass *renderPass, const Rect &renderArea, const Color *colors, float depth, int stencil, bool &hasScreenClean) {
const auto gpuPSO = getClearRenderPassPipelineState(device, renderPass);
const auto mtlRenderPass = static_cast<CCMTLRenderPass *>(renderPass);
MTLRenderPassDescriptor *renderPassDescriptor = mtlRenderPass->getMTLRenderPassDescriptor();
if(!hasScreenClean) {
renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
renderPassDescriptor.colorAttachments[0].clearColor = toMTLClearColor(colors[0]);
renderPassDescriptor.depthAttachment.clearDepth = depth;
renderPassDescriptor.stencilAttachment.clearStencil = stencil;
}
float halfWidth = drawableSize.width * 0.5f ;
float halfHeight = drawableSize.height * 0.5f;
uint renderTargetWidth = mtlRenderPass->getRenderTargetWidth();
uint renderTargetHeight = mtlRenderPass->getRenderTargetHeight();
float halfWidth = renderTargetWidth * 0.5f ;
float halfHeight = renderTargetHeight * 0.5f;
float rcpWidth = 1.0f / halfWidth ;
float rcpHeight = 1.0f / halfHeight;
float width = renderArea.x+ renderArea.width;
@@ -1573,8 +1576,8 @@ void clearRenderArea(CCMTLDevice *device, id<MTLCommandBuffer> commandBuffer, CG
}
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
[renderEncoder setViewport:(MTLViewport){0, 0, drawableSize.width, drawableSize.height}];
[renderEncoder setScissorRect:(MTLScissorRect){0, 0, (NSUInteger)drawableSize.width, (NSUInteger)drawableSize.height}];
[renderEncoder setViewport:(MTLViewport){0, 0, static_cast<double>(renderTargetWidth), static_cast<double>(renderTargetHeight)}];
[renderEncoder setScissorRect:(MTLScissorRect){0, 0, renderTargetWidth, renderTargetHeight}];
[renderEncoder setRenderPipelineState:gpuPSO->mtlRenderPipelineState];
if (gpuPSO->mtlDepthStencilState) {
[renderEncoder setStencilFrontReferenceValue:gpuPSO->stencilRefFront