2020-09-21 22:10:50 +08:00
|
|
|
#include "RenderTargetMTL.h"
|
|
|
|
#include "UtilsMTL.h"
|
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static MTLLoadAction getLoadAction(const RenderPassDescriptor& params, TargetBufferFlags buffer)
|
|
|
|
{
|
|
|
|
const auto clearFlags = (TargetBufferFlags)params.flags.clear;
|
2020-09-21 22:10:50 +08:00
|
|
|
const auto discardStartFlags = params.flags.discardStart;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (bitmask::any(clearFlags, buffer))
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
return MTLLoadActionClear;
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
else if (bitmask::any(discardStartFlags, buffer))
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
return MTLLoadActionDontCare;
|
|
|
|
}
|
|
|
|
return MTLLoadActionLoad;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static MTLStoreAction getStoreAction(const RenderPassDescriptor& params, TargetBufferFlags buffer)
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
const auto discardEndFlags = params.flags.discardEnd;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (bitmask::any(discardEndFlags, buffer))
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
return MTLStoreActionDontCare;
|
|
|
|
}
|
|
|
|
return MTLStoreActionStore;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
RenderTargetMTL::RenderTargetMTL(bool defaultRenderTarget) : RenderTarget(defaultRenderTarget) {}
|
|
|
|
RenderTargetMTL::~RenderTargetMTL() {}
|
2020-09-21 22:10:50 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RenderTargetMTL::bindFrameBuffer() const {}
|
2020-09-21 22:10:50 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RenderTargetMTL::unbindFrameBuffer() const {}
|
2020-09-21 22:10:50 +08:00
|
|
|
void RenderTargetMTL::setColorAttachment(ColorAttachment attachment)
|
|
|
|
{
|
|
|
|
RenderTarget::setColorAttachment(attachment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderTargetMTL::setDepthAttachment(TextureBackend* attachment, int level)
|
|
|
|
{
|
|
|
|
RenderTarget::setDepthAttachment(attachment, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderTargetMTL::setStencilAttachment(TextureBackend* attachment, int level)
|
|
|
|
{
|
|
|
|
RenderTarget::setStencilAttachment(attachment, level);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RenderTargetMTL::applyRenderPassAttachments(const RenderPassDescriptor& params,
|
|
|
|
MTLRenderPassDescriptor* descriptor) const
|
2020-09-21 22:10:50 +08:00
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
// const auto discardFlags = params.flags.discardEnd;
|
|
|
|
auto clearFlags = params.flags.clear;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < MAX_COLOR_ATTCHMENT; i++)
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
auto attachment = getColorAttachment(i);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!attachment)
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
const auto MRTColorFlag = getMRTColorFlag(i);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-09-21 22:10:50 +08:00
|
|
|
descriptor.colorAttachments[i].texture = attachment.texture;
|
2021-12-31 12:12:40 +08:00
|
|
|
descriptor.colorAttachments[i].level = attachment.level;
|
2020-09-21 22:10:50 +08:00
|
|
|
// descriptor.colorAttachments[i].slice = attachment.layer;
|
2021-12-31 12:12:40 +08:00
|
|
|
descriptor.colorAttachments[i].loadAction = getLoadAction(params, MRTColorFlag);
|
|
|
|
descriptor.colorAttachments[i].storeAction = getStoreAction(params, MRTColorFlag);
|
|
|
|
if (bitmask::any(clearFlags, MRTColorFlag))
|
|
|
|
descriptor.colorAttachments[i].clearColor =
|
|
|
|
MTLClearColorMake(params.clearColorValue[0], params.clearColorValue[1], params.clearColorValue[2],
|
|
|
|
params.clearColorValue[3]);
|
2020-09-21 22:10:50 +08:00
|
|
|
#if 0
|
|
|
|
if (multisampledColor[i]) {
|
|
|
|
// We're rendering into our temporary MSAA texture and doing an automatic resolve.
|
|
|
|
// We should not be attempting to load anything into the MSAA texture.
|
|
|
|
assert(descriptor.colorAttachments[i].loadAction != MTLLoadActionLoad);
|
|
|
|
|
|
|
|
descriptor.colorAttachments[i].texture = multisampledColor[i];
|
|
|
|
descriptor.colorAttachments[i].level = 0;
|
|
|
|
descriptor.colorAttachments[i].slice = 0;
|
|
|
|
const bool discard = any(discardFlags & getMRTColorFlag(i));
|
|
|
|
if (!discard) {
|
|
|
|
descriptor.colorAttachments[i].resolveTexture = attachment.texture;
|
|
|
|
descriptor.colorAttachments[i].resolveLevel = attachment.level;
|
|
|
|
descriptor.colorAttachments[i].resolveSlice = attachment.layer;
|
|
|
|
descriptor.colorAttachments[i].storeAction = MTLStoreActionMultisampleResolve;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-11-08 13:12:06 +08:00
|
|
|
// Sets descriptor depth and stencil params, should match RenderTargetMTL::chooseAttachmentFormat
|
2021-12-31 12:12:40 +08:00
|
|
|
if (bitmask::any(this->_flags, RenderTargetFlag::DEPTH_AND_STENCIL))
|
|
|
|
{
|
2020-11-08 13:12:06 +08:00
|
|
|
auto depthAttachment = getDepthAttachment();
|
2021-12-31 12:12:40 +08:00
|
|
|
if (depthAttachment)
|
|
|
|
{
|
2020-11-08 13:12:06 +08:00
|
|
|
descriptor.depthAttachment.texture = depthAttachment.texture;
|
2021-12-31 12:12:40 +08:00
|
|
|
descriptor.depthAttachment.level = depthAttachment.level;
|
2020-11-08 13:12:06 +08:00
|
|
|
// descriptor.depthAttachment.slice = depthAttachment.layer;
|
2021-12-31 12:12:40 +08:00
|
|
|
descriptor.depthAttachment.loadAction = getLoadAction(params, TargetBufferFlags::DEPTH);
|
2020-11-08 13:12:06 +08:00
|
|
|
descriptor.depthAttachment.storeAction = getStoreAction(params, TargetBufferFlags::DEPTH);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (bitmask::any(clearFlags, TargetBufferFlags::DEPTH))
|
2021-04-22 22:01:47 +08:00
|
|
|
descriptor.depthAttachment.clearDepth = params.clearDepthValue;
|
2020-11-08 13:12:06 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-11-08 13:12:06 +08:00
|
|
|
auto stencilAttachment = getStencilAttachment();
|
2021-12-31 12:12:40 +08:00
|
|
|
if (stencilAttachment)
|
|
|
|
{
|
2020-11-08 13:12:06 +08:00
|
|
|
descriptor.stencilAttachment.texture = stencilAttachment.texture;
|
2021-12-31 12:12:40 +08:00
|
|
|
descriptor.stencilAttachment.level = stencilAttachment.level;
|
2020-11-08 13:12:06 +08:00
|
|
|
// descriptor.stencilAttachment.slice = depthAttachment.layer;
|
2021-12-31 12:12:40 +08:00
|
|
|
descriptor.stencilAttachment.loadAction = getLoadAction(params, TargetBufferFlags::STENCIL);
|
2020-11-08 13:12:06 +08:00
|
|
|
descriptor.stencilAttachment.storeAction = getStoreAction(params, TargetBufferFlags::STENCIL);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (bitmask::any(clearFlags, TargetBufferFlags::STENCIL))
|
2021-04-22 22:01:47 +08:00
|
|
|
descriptor.stencilAttachment.clearStencil = params.clearStencilValue;
|
2020-11-08 13:12:06 +08:00
|
|
|
}
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-09-21 22:10:50 +08:00
|
|
|
#if 0
|
|
|
|
if (multisampledDepth) {
|
|
|
|
// We're rendering into our temporary MSAA texture and doing an automatic resolve.
|
|
|
|
// We should not be attempting to load anything into the MSAA texture.
|
|
|
|
assert(descriptor.depthAttachment.loadAction != MTLLoadActionLoad);
|
|
|
|
|
|
|
|
descriptor.depthAttachment.texture = multisampledDepth;
|
|
|
|
descriptor.depthAttachment.level = 0;
|
|
|
|
descriptor.depthAttachment.slice = 0;
|
|
|
|
const bool discard = any(discardFlags & TargetBufferFlags::DEPTH);
|
|
|
|
if (!discard) {
|
|
|
|
descriptor.depthAttachment.resolveTexture = depthAttachment.texture;
|
|
|
|
descriptor.depthAttachment.resolveLevel = depthAttachment.level;
|
|
|
|
descriptor.depthAttachment.resolveSlice = depthAttachment.layer;
|
|
|
|
descriptor.depthAttachment.storeAction = MTLStoreActionMultisampleResolve;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderTargetMTL::Attachment RenderTargetMTL::getColorAttachment(int index) const
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (isDefaultRenderTarget() && index == 0)
|
2020-09-21 22:10:50 +08:00
|
|
|
return {DeviceMTL::getCurrentDrawable().texture, 0};
|
|
|
|
auto& rb = this->_color[index];
|
2021-12-31 12:12:40 +08:00
|
|
|
return RenderTargetMTL::Attachment{static_cast<bool>(rb) ? (id<MTLTexture>)(rb.texture->getHandler()) : nil,
|
|
|
|
rb.level};
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderTargetMTL::Attachment RenderTargetMTL::getDepthAttachment() const
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (isDefaultRenderTarget())
|
2020-09-21 22:10:50 +08:00
|
|
|
return {UtilsMTL::getDefaultDepthStencilTexture(), 0};
|
|
|
|
auto& rb = this->_depth;
|
|
|
|
return RenderTargetMTL::Attachment{!!rb ? (id<MTLTexture>)(rb.texture->getHandler()) : nil, rb.level};
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderTargetMTL::Attachment RenderTargetMTL::getStencilAttachment() const
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (isDefaultRenderTarget())
|
2020-09-25 11:07:56 +08:00
|
|
|
return RenderTargetMTL::Attachment{UtilsMTL::getDefaultDepthStencilTexture(), 0};
|
2020-09-21 22:10:50 +08:00
|
|
|
auto& rb = this->_stencil;
|
|
|
|
return RenderTargetMTL::Attachment{!!rb ? (id<MTLTexture>)(rb.texture->getHandler()) : nil, rb.level};
|
|
|
|
}
|
|
|
|
|
|
|
|
PixelFormat RenderTargetMTL::getColorAttachmentPixelFormat(int index) const
|
|
|
|
{
|
2020-09-25 11:07:56 +08:00
|
|
|
// !!!important
|
|
|
|
// the default framebuffer pixel format is: MTLPixelFormatBGRA8Unorm
|
2021-12-31 12:12:40 +08:00
|
|
|
if (isDefaultRenderTarget() && index == 0)
|
2020-09-25 11:07:56 +08:00
|
|
|
return PixelFormat::BGRA8;
|
2020-09-21 22:10:50 +08:00
|
|
|
auto& rb = this->_color[index];
|
|
|
|
return rb ? rb.texture->getTextureFormat() : PixelFormat::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PixelFormat RenderTargetMTL::getDepthAttachmentPixelFormat() const
|
2021-12-31 12:12:40 +08:00
|
|
|
{ // FIXME: adxe only support D24S8
|
|
|
|
if (bitmask::any(_flags, TargetBufferFlags::DEPTH_AND_STENCIL))
|
|
|
|
{
|
|
|
|
if (isDefaultRenderTarget() || !_depth)
|
2020-09-25 23:16:48 +08:00
|
|
|
return PixelFormat::D24S8;
|
|
|
|
return _depth.texture->getTextureFormat();
|
|
|
|
}
|
|
|
|
return PixelFormat::NONE;
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PixelFormat RenderTargetMTL::getStencilAttachmentPixelFormat() const
|
2021-12-31 12:12:40 +08:00
|
|
|
{ // FIXME: adxe only support D24S8
|
|
|
|
if (bitmask::any(_flags, TargetBufferFlags::DEPTH_AND_STENCIL))
|
|
|
|
{
|
|
|
|
if (isDefaultRenderTarget() || !_stencil)
|
2020-09-25 23:16:48 +08:00
|
|
|
return PixelFormat::D24S8;
|
|
|
|
return _stencil.texture->getTextureFormat();
|
|
|
|
}
|
|
|
|
return PixelFormat::NONE;
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CC_BACKEND_END
|