axmol/core/renderer/backend/metal/RenderPipelineMTL.mm

347 lines
13 KiB
Plaintext
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2019-11-23 20:27:39 +08:00
#include "RenderPipelineMTL.h"
#include "DeviceMTL.h"
#include "RenderTargetMTL.h"
2019-11-23 20:27:39 +08:00
#include "ShaderModuleMTL.h"
#include "DepthStencilStateMTL.h"
2020-09-11 00:10:44 +08:00
#include "UtilsMTL.h"
2019-11-23 20:27:39 +08:00
#include "ProgramMTL.h"
#include "xxhash.h"
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
namespace
{
MTLVertexStepFunction toMTLVertexStepFunction(VertexStepMode vertexStepMode)
{
if (VertexStepMode::VERTEX == vertexStepMode)
return MTLVertexStepFunctionPerVertex;
else
return MTLVertexStepFunctionPerInstance;
}
MTLVertexFormat toMTLVertexFormat(VertexFormat vertexFormat, bool needNormalize)
{
MTLVertexFormat ret = MTLVertexFormatFloat4;
switch (vertexFormat)
2019-11-23 20:27:39 +08:00
{
case VertexFormat::FLOAT4:
ret = MTLVertexFormatFloat4;
break;
case VertexFormat::FLOAT3:
ret = MTLVertexFormatFloat3;
break;
case VertexFormat::FLOAT2:
ret = MTLVertexFormatFloat2;
break;
case VertexFormat::FLOAT:
ret = MTLVertexFormatFloat;
break;
case VertexFormat::INT4:
ret = MTLVertexFormatInt4;
break;
case VertexFormat::INT3:
ret = MTLVertexFormatInt3;
break;
case VertexFormat::INT2:
ret = MTLVertexFormatInt2;
break;
case VertexFormat::INT:
ret = MTLVertexFormatInt;
break;
case VertexFormat::USHORT4:
ret = MTLVertexFormatUShort4;
break;
case VertexFormat::USHORT2:
ret = MTLVertexFormatUShort2;
break;
case VertexFormat::UBYTE4:
if (needNormalize)
ret = MTLVertexFormatUChar4Normalized;
2019-11-23 20:27:39 +08:00
else
ret = MTLVertexFormatUChar4;
break;
default:
assert(false);
break;
}
return ret;
}
MTLColorWriteMask toMTLColorWriteMask(ColorWriteMask mask)
{
switch (mask)
{
case ColorWriteMask::NONE:
return MTLColorWriteMaskNone;
case ColorWriteMask::RED:
return MTLColorWriteMaskRed;
case ColorWriteMask::GREEN:
return MTLColorWriteMaskGreen;
case ColorWriteMask::BLUE:
return MTLColorWriteMaskBlue;
case ColorWriteMask::ALPHA:
return MTLColorWriteMaskAlpha;
case ColorWriteMask::ALL:
return MTLColorWriteMaskAll;
2021-12-31 11:00:35 +08:00
}
}
MTLBlendFactor toMTLBlendFactor(BlendFactor factor)
{
switch (factor)
2021-12-31 11:00:35 +08:00
{
case BlendFactor::ZERO:
return MTLBlendFactorZero;
case BlendFactor::ONE:
return MTLBlendFactorOne;
case BlendFactor::SRC_COLOR:
return MTLBlendFactorSourceColor;
case BlendFactor::ONE_MINUS_SRC_COLOR:
return MTLBlendFactorOneMinusSourceColor;
case BlendFactor::SRC_ALPHA:
return MTLBlendFactorSourceAlpha;
case BlendFactor::ONE_MINUS_SRC_ALPHA:
return MTLBlendFactorOneMinusSourceAlpha;
case BlendFactor::DST_COLOR:
return MTLBlendFactorDestinationColor;
case BlendFactor::ONE_MINUS_DST_COLOR:
return MTLBlendFactorOneMinusDestinationColor;
case BlendFactor::DST_ALPHA:
return MTLBlendFactorDestinationAlpha;
case BlendFactor::ONE_MINUS_DST_ALPHA:
return MTLBlendFactorOneMinusDestinationAlpha;
case BlendFactor::SRC_ALPHA_SATURATE:
return MTLBlendFactorSourceAlphaSaturated;
case BlendFactor::BLEND_CLOLOR:
return MTLBlendFactorBlendColor;
default:
return MTLBlendFactorZero;
}
}
MTLBlendOperation toMTLBlendOperation(BlendOperation operation)
2021-12-31 11:00:35 +08:00
{
switch (operation)
{
case BlendOperation::ADD:
return MTLBlendOperationAdd;
case BlendOperation::SUBTRACT:
return MTLBlendOperationSubtract;
case BlendOperation::RESERVE_SUBTRACT:
return MTLBlendOperationReverseSubtract;
default:
return MTLBlendOperationAdd;
}
}
2021-12-31 11:00:35 +08:00
}
RenderPipelineMTL::RenderPipelineMTL(id<MTLDevice> mtlDevice) : _mtlDevice(mtlDevice) {}
void RenderPipelineMTL::update(const RenderTarget* renderTarget, const PipelineDescriptor& pipelineDescirptor)
2019-11-23 20:27:39 +08:00
{
struct
{
size_t vertexShaderHash;
size_t fragmentShaderHash;
2019-11-23 20:27:39 +08:00
unsigned int vertexLayoutInfo[32];
backend::PixelFormat colorAttachment[MAX_COLOR_ATTCHMENT];
2019-11-23 20:27:39 +08:00
backend::PixelFormat depthAttachment;
backend::PixelFormat stencilAttachment;
bool blendEnabled;
unsigned int writeMask;
unsigned int rgbBlendOperation;
unsigned int alphaBlendOperation;
unsigned int sourceRGBBlendFactor;
unsigned int destinationRGBBlendFactor;
unsigned int sourceAlphaBlendFactor;
unsigned int destinationAlphaBlendFactor;
} hashMe;
2019-11-23 20:27:39 +08:00
memset(&hashMe, 0, sizeof(hashMe));
const auto& blendDescriptor = pipelineDescirptor.blendDescriptor;
chooseAttachmentFormat(renderTarget, _colorAttachmentsFormat, _depthAttachmentFormat, _stencilAttachmentFormat);
auto program = static_cast<ProgramMTL*>(pipelineDescirptor.programState->getProgram());
hashMe.vertexShaderHash = program->getVertexShader()->getHashValue();
hashMe.fragmentShaderHash = program->getFragmentShader()->getHashValue();
memcpy(&hashMe.colorAttachment, &_colorAttachmentsFormat, sizeof(_colorAttachmentsFormat));
hashMe.depthAttachment = _depthAttachmentFormat;
hashMe.stencilAttachment = _stencilAttachmentFormat;
hashMe.blendEnabled = blendDescriptor.blendEnabled;
hashMe.writeMask = (unsigned int)blendDescriptor.writeMask;
hashMe.rgbBlendOperation = (unsigned int)blendDescriptor.rgbBlendOperation;
hashMe.alphaBlendOperation = (unsigned int)blendDescriptor.alphaBlendOperation;
hashMe.sourceRGBBlendFactor = (unsigned int)blendDescriptor.sourceRGBBlendFactor;
hashMe.destinationRGBBlendFactor = (unsigned int)blendDescriptor.destinationRGBBlendFactor;
hashMe.sourceAlphaBlendFactor = (unsigned int)blendDescriptor.sourceAlphaBlendFactor;
2019-11-23 20:27:39 +08:00
hashMe.destinationAlphaBlendFactor = (unsigned int)blendDescriptor.destinationAlphaBlendFactor;
int index = 0;
auto vertexLayout = pipelineDescirptor.programState->getVertexLayout();
const auto& attributes = vertexLayout->getAttributes();
2019-11-23 20:27:39 +08:00
for (const auto& it : attributes)
{
auto& attribute = it.second;
2019-11-23 20:27:39 +08:00
/*
stepFunction:1 stride:15 offest:10 format:5 needNormalized:1
bit31 bit30 ~ bit16 bit15 ~ bit6 bit5 ~ bit1 bit0
*/
hashMe.vertexLayoutInfo[index++] =
((unsigned int)vertexLayout->getVertexStepMode() & 0x1) << 31 |
((unsigned int)(vertexLayout->getStride() & 0x7FFF)) << 16 | ((unsigned int)attribute.offset & 0x3FF) << 6 |
((unsigned int)attribute.format & 0x1F) << 1 | ((unsigned int)attribute.needToBeNormallized & 0x1);
2019-11-23 20:27:39 +08:00
}
unsigned int hash = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
auto it = _mtlStateCache.find(hash);
if (it != _mtlStateCache.end())
{
_mtlRenderPipelineState = it->second;
2019-11-23 20:27:39 +08:00
return;
}
2019-11-23 20:27:39 +08:00
_mtlRenderPipelineDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
2019-11-23 20:27:39 +08:00
setShaderModules(pipelineDescirptor);
setVertexLayout(_mtlRenderPipelineDescriptor, pipelineDescirptor);
setBlendStateAndFormat(pipelineDescirptor.blendDescriptor);
NSError* error = nil;
_mtlRenderPipelineState = [_mtlDevice newRenderPipelineStateWithDescriptor:_mtlRenderPipelineDescriptor
error:&error];
2019-11-23 20:27:39 +08:00
if (error)
NSLog(@"Can not create renderpipeline state: %@", error);
2019-11-23 20:27:39 +08:00
[_mtlRenderPipelineDescriptor release];
_mtlStateCache.emplace(hash, _mtlRenderPipelineState);
2019-11-23 20:27:39 +08:00
}
RenderPipelineMTL::~RenderPipelineMTL()
{
for (auto& item : _mtlStateCache)
[item.second release];
2019-11-23 20:27:39 +08:00
}
void RenderPipelineMTL::setVertexLayout(MTLRenderPipelineDescriptor* mtlDescriptor,
const PipelineDescriptor& descriptor)
2019-11-23 20:27:39 +08:00
{
auto vertexLayout = descriptor.programState->getVertexLayout();
if (!vertexLayout->isValid())
return;
2023-08-09 14:37:43 +08:00
int stride = vertexLayout->getStride();
auto vertexDesc = mtlDescriptor.vertexDescriptor;
vertexDesc.layouts[DeviceMTL::DEFAULT_ATTRIBS_BINDING_INDEX].stride = stride;
vertexDesc.layouts[DeviceMTL::DEFAULT_ATTRIBS_BINDING_INDEX].stepFunction =
toMTLVertexStepFunction(vertexLayout->getVertexStepMode());
2019-11-23 20:27:39 +08:00
const auto& attributes = vertexLayout->getAttributes();
2023-08-09 14:37:43 +08:00
2019-11-23 20:27:39 +08:00
for (const auto& it : attributes)
{
auto attribute = it.second;
2023-08-09 14:37:43 +08:00
vertexDesc.attributes[attribute.index].format =
toMTLVertexFormat(attribute.format, attribute.needToBeNormallized);
2023-08-09 14:37:43 +08:00
vertexDesc.attributes[attribute.index].offset = attribute.offset;
2019-11-23 20:27:39 +08:00
// Buffer index will always be 0;
2023-08-09 14:37:43 +08:00
vertexDesc.attributes[attribute.index].bufferIndex = DeviceMTL::DEFAULT_ATTRIBS_BINDING_INDEX;
2019-11-23 20:27:39 +08:00
}
}
void RenderPipelineMTL::setBlendState(MTLRenderPipelineColorAttachmentDescriptor* colorAttachmentDescriptor,
const BlendDescriptor& blendDescriptor)
{
colorAttachmentDescriptor.blendingEnabled = blendDescriptor.blendEnabled;
colorAttachmentDescriptor.writeMask = toMTLColorWriteMask(blendDescriptor.writeMask);
colorAttachmentDescriptor.rgbBlendOperation = toMTLBlendOperation(blendDescriptor.rgbBlendOperation);
2019-11-23 20:27:39 +08:00
colorAttachmentDescriptor.alphaBlendOperation = toMTLBlendOperation(blendDescriptor.alphaBlendOperation);
colorAttachmentDescriptor.sourceRGBBlendFactor = toMTLBlendFactor(blendDescriptor.sourceRGBBlendFactor);
2019-11-23 20:27:39 +08:00
colorAttachmentDescriptor.destinationRGBBlendFactor = toMTLBlendFactor(blendDescriptor.destinationRGBBlendFactor);
colorAttachmentDescriptor.sourceAlphaBlendFactor = toMTLBlendFactor(blendDescriptor.sourceAlphaBlendFactor);
colorAttachmentDescriptor.destinationAlphaBlendFactor =
toMTLBlendFactor(blendDescriptor.destinationAlphaBlendFactor);
2019-11-23 20:27:39 +08:00
}
void RenderPipelineMTL::setShaderModules(const PipelineDescriptor& descriptor)
{
auto vertexShaderModule = static_cast<ProgramMTL*>(descriptor.programState->getProgram())->getVertexShader();
_mtlRenderPipelineDescriptor.vertexFunction = vertexShaderModule->getMTLFunction();
2019-11-23 20:27:39 +08:00
auto fragShaderModule = static_cast<ProgramMTL*>(descriptor.programState->getProgram())->getFragmentShader();
_mtlRenderPipelineDescriptor.fragmentFunction = fragShaderModule->getMTLFunction();
}
void RenderPipelineMTL::chooseAttachmentFormat(const RenderTarget* renderTarget,
PixelFormat colorAttachmentsFormat[MAX_COLOR_ATTCHMENT],
PixelFormat& depthFormat,
PixelFormat& stencilFormat)
2019-11-23 20:27:39 +08:00
{
// Choose color attachment format
auto rtMTL = static_cast<const RenderTargetMTL*>(renderTarget);
auto rtflags = rtMTL->getTargetFlags();
for (auto i = 0; i < MAX_COLOR_ATTCHMENT; ++i)
{
colorAttachmentsFormat[i] =
bitmask::any(rtflags, getMRTColorFlag(i)) ? rtMTL->getColorAttachmentPixelFormat(i) : PixelFormat::NONE;
2019-11-23 20:27:39 +08:00
}
if (bitmask::any(rtflags, RenderTargetFlag::DEPTH_AND_STENCIL))
{
depthFormat = rtMTL->getDepthAttachmentPixelFormat();
stencilFormat = rtMTL->getStencilAttachmentPixelFormat();
}
else
{
depthFormat = stencilFormat = PixelFormat::NONE;
}
2019-11-23 20:27:39 +08:00
}
void RenderPipelineMTL::setBlendStateAndFormat(const BlendDescriptor& blendDescriptor)
2019-11-23 20:27:39 +08:00
{
for (int i = 0; i < MAX_COLOR_ATTCHMENT; ++i)
{
if (PixelFormat::NONE == _colorAttachmentsFormat[i])
{
_mtlRenderPipelineDescriptor.colorAttachments[i].pixelFormat = MTLPixelFormat::MTLPixelFormatInvalid;
2019-11-23 20:27:39 +08:00
continue;
}
_mtlRenderPipelineDescriptor.colorAttachments[i].pixelFormat =
UtilsMTL::toMTLPixelFormat(_colorAttachmentsFormat[i]);
2019-11-23 20:27:39 +08:00
setBlendState(_mtlRenderPipelineDescriptor.colorAttachments[i], blendDescriptor);
}
_mtlRenderPipelineDescriptor.depthAttachmentPixelFormat = UtilsMTL::toMTLPixelFormat(_depthAttachmentFormat);
2020-09-11 00:10:44 +08:00
_mtlRenderPipelineDescriptor.stencilAttachmentPixelFormat = UtilsMTL::toMTLPixelFormat(_stencilAttachmentFormat);
2019-11-23 20:27:39 +08:00
}
NS_AX_BACKEND_END