2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.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.
|
|
|
|
****************************************************************************/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "DepthStencilStateMTL.h"
|
2021-04-22 22:01:47 +08:00
|
|
|
#include "../RenderTarget.h"
|
2020-09-22 16:32:17 +08:00
|
|
|
#include "xxhash.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
MTLCompareFunction toMTLCompareFunction(CompareFunction compareFunction)
|
|
|
|
{
|
|
|
|
MTLCompareFunction ret = MTLCompareFunctionNever;
|
|
|
|
switch (compareFunction)
|
2021-12-31 11:00:35 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case CompareFunction::NEVER:
|
|
|
|
ret = MTLCompareFunctionNever;
|
|
|
|
break;
|
|
|
|
case CompareFunction::LESS:
|
|
|
|
ret = MTLCompareFunctionLess;
|
|
|
|
break;
|
|
|
|
case CompareFunction::LESS_EQUAL:
|
|
|
|
ret = MTLCompareFunctionLessEqual;
|
|
|
|
break;
|
|
|
|
case CompareFunction::GREATER:
|
|
|
|
ret = MTLCompareFunctionGreater;
|
|
|
|
break;
|
|
|
|
case CompareFunction::GREATER_EQUAL:
|
|
|
|
ret = MTLCompareFunctionGreaterEqual;
|
|
|
|
break;
|
|
|
|
case CompareFunction::EQUAL:
|
|
|
|
ret = MTLCompareFunctionEqual;
|
|
|
|
break;
|
|
|
|
case CompareFunction::NOT_EQUAL:
|
|
|
|
ret = MTLCompareFunctionNotEqual;
|
|
|
|
break;
|
|
|
|
case CompareFunction::ALWAYS:
|
|
|
|
ret = MTLCompareFunctionAlways;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
2021-12-31 11:00:35 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTLStencilOperation toMTLStencilOperation(StencilOperation operation)
|
|
|
|
{
|
|
|
|
MTLStencilOperation ret = MTLStencilOperationKeep;
|
|
|
|
switch (operation)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case StencilOperation::KEEP:
|
|
|
|
ret = MTLStencilOperationKeep;
|
|
|
|
break;
|
|
|
|
case StencilOperation::ZERO:
|
|
|
|
ret = MTLStencilOperationZero;
|
|
|
|
break;
|
|
|
|
case StencilOperation::REPLACE:
|
|
|
|
ret = MTLStencilOperationReplace;
|
|
|
|
break;
|
|
|
|
case StencilOperation::INVERT:
|
|
|
|
ret = MTLStencilOperationInvert;
|
|
|
|
break;
|
|
|
|
case StencilOperation::INCREMENT_WRAP:
|
|
|
|
ret = MTLStencilOperationIncrementWrap;
|
|
|
|
break;
|
|
|
|
case StencilOperation::DECREMENT_WRAP:
|
|
|
|
ret = MTLStencilOperationDecrementWrap;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void setMTLStencilDescriptor(MTLStencilDescriptor* stencilDescriptor, const StencilDescriptor& descriptor)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
stencilDescriptor.stencilFailureOperation = toMTLStencilOperation(descriptor.stencilFailureOperation);
|
|
|
|
stencilDescriptor.depthFailureOperation = toMTLStencilOperation(descriptor.depthFailureOperation);
|
|
|
|
stencilDescriptor.depthStencilPassOperation = toMTLStencilOperation(descriptor.depthStencilPassOperation);
|
|
|
|
stencilDescriptor.stencilCompareFunction = toMTLCompareFunction(descriptor.stencilCompareFunction);
|
|
|
|
stencilDescriptor.readMask = descriptor.readMask;
|
|
|
|
stencilDescriptor.writeMask = descriptor.writeMask;
|
|
|
|
}
|
2020-09-22 16:32:17 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
DepthStencilStateMTL::DepthStencilStateMTL(id<MTLDevice> mtlDevice) : _mtlDevice(mtlDevice) {}
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
void DepthStencilStateMTL::update(const DepthStencilDescriptor& dsDesc)
|
2020-09-22 16:32:17 +08:00
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
DepthStencilState::update(dsDesc);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
if (!isEnabled())
|
|
|
|
{
|
2020-09-22 16:32:17 +08:00
|
|
|
_mtlDepthStencilState = nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
2020-09-22 16:32:17 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
DepthStencilDescriptor hashMe;
|
|
|
|
memset(&hashMe, 0, sizeof(hashMe));
|
|
|
|
hashMe.depthCompareFunction = dsDesc.depthCompareFunction;
|
2021-12-31 12:12:40 +08:00
|
|
|
hashMe.backFaceStencil = dsDesc.backFaceStencil;
|
|
|
|
hashMe.frontFaceStencil = dsDesc.frontFaceStencil;
|
|
|
|
hashMe.flags = dsDesc.flags;
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
auto key = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto it = _mtlStateCache.find(key);
|
|
|
|
if (it != _mtlStateCache.end())
|
|
|
|
{
|
2020-09-22 16:32:17 +08:00
|
|
|
_mtlDepthStencilState = it->second;
|
|
|
|
return;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MTLDepthStencilDescriptor* mtlDescriptor = [[MTLDepthStencilDescriptor alloc] init];
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
if (bitmask::any(dsDesc.flags, DepthStencilFlags::DEPTH_TEST))
|
|
|
|
mtlDescriptor.depthCompareFunction = toMTLCompareFunction(_depthStencilInfo.depthCompareFunction);
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
|
|
|
mtlDescriptor.depthCompareFunction = MTLCompareFunctionAlways;
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
mtlDescriptor.depthWriteEnabled = bitmask::any(dsDesc.flags, DepthStencilFlags::DEPTH_WRITE);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
if (bitmask::any(dsDesc.flags, DepthStencilFlags::STENCIL_TEST))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
setMTLStencilDescriptor(mtlDescriptor.frontFaceStencil, _depthStencilInfo.frontFaceStencil);
|
|
|
|
setMTLStencilDescriptor(mtlDescriptor.backFaceStencil, _depthStencilInfo.backFaceStencil);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:32:17 +08:00
|
|
|
_mtlDepthStencilState = [_mtlDevice newDepthStencilStateWithDescriptor:mtlDescriptor];
|
2019-11-23 20:27:39 +08:00
|
|
|
[mtlDescriptor release];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-09-22 16:32:17 +08:00
|
|
|
// emplace to state cache
|
|
|
|
_mtlStateCache.emplace(key, _mtlDepthStencilState);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DepthStencilStateMTL::~DepthStencilStateMTL()
|
|
|
|
{
|
2020-09-22 16:32:17 +08:00
|
|
|
_mtlDepthStencilState = nullptr;
|
2021-12-31 12:12:40 +08:00
|
|
|
for (auto& stateItem : _mtlStateCache)
|
2020-09-22 16:32:17 +08:00
|
|
|
[stateItem.second release];
|
|
|
|
_mtlStateCache.clear();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|