2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "DeviceMTL.h"
|
|
|
|
#include "CommandBufferMTL.h"
|
|
|
|
#include "BufferMTL.h"
|
|
|
|
#include "RenderPipelineMTL.h"
|
|
|
|
#include "ShaderModuleMTL.h"
|
|
|
|
#include "DepthStencilStateMTL.h"
|
|
|
|
#include "TextureMTL.h"
|
|
|
|
#include "ProgramMTL.h"
|
|
|
|
#include "DeviceInfoMTL.h"
|
2020-09-21 22:10:50 +08:00
|
|
|
#include "RenderTargetMTL.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
|
|
|
|
CAMetalLayer* DeviceMTL::_metalLayer = nil;
|
|
|
|
id<CAMetalDrawable> DeviceMTL::_currentDrawable = nil;
|
|
|
|
|
|
|
|
Device* Device::getInstance()
|
|
|
|
{
|
|
|
|
if (! Device::_instance)
|
2021-12-08 00:11:53 +08:00
|
|
|
Device::_instance = new DeviceMTL();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return Device::_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceMTL::setCAMetalLayer(CAMetalLayer* metalLayer)
|
|
|
|
{
|
|
|
|
DeviceMTL::_metalLayer = metalLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
id<CAMetalDrawable> DeviceMTL::getCurrentDrawable()
|
|
|
|
{
|
|
|
|
if (! DeviceMTL::_currentDrawable)
|
|
|
|
DeviceMTL::_currentDrawable = [DeviceMTL::_metalLayer nextDrawable];
|
|
|
|
|
|
|
|
return DeviceMTL::_currentDrawable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceMTL::resetCurrentDrawable()
|
|
|
|
{
|
|
|
|
DeviceMTL::_currentDrawable = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceMTL::DeviceMTL()
|
|
|
|
{
|
|
|
|
_mtlDevice = DeviceMTL::_metalLayer.device;
|
|
|
|
_mtlCommandQueue = [_mtlDevice newCommandQueue];
|
2021-12-08 00:11:53 +08:00
|
|
|
_deviceInfo = new DeviceInfoMTL(_mtlDevice);
|
|
|
|
if(!_deviceInfo->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
delete _deviceInfo;
|
|
|
|
_deviceInfo = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceMTL::~DeviceMTL()
|
|
|
|
{
|
|
|
|
ProgramCache::destroyInstance();
|
|
|
|
delete _deviceInfo;
|
|
|
|
_deviceInfo = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandBuffer* DeviceMTL::newCommandBuffer()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
return new CommandBufferMTL(this);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Buffer* DeviceMTL::newBuffer(std::size_t size, BufferType type, BufferUsage usage)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
return new BufferMTL(_mtlDevice, size, type, usage);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureBackend* DeviceMTL::newTexture(const TextureDescriptor& descriptor)
|
|
|
|
{
|
|
|
|
switch(descriptor.textureType)
|
|
|
|
{
|
|
|
|
case TextureType::TEXTURE_2D:
|
2021-12-08 00:11:53 +08:00
|
|
|
return new TextureMTL(_mtlDevice, descriptor);
|
2019-11-23 20:27:39 +08:00
|
|
|
case TextureType::TEXTURE_CUBE:
|
2021-12-08 00:11:53 +08:00
|
|
|
return new TextureCubeMTL(_mtlDevice, descriptor);
|
2019-11-23 20:27:39 +08:00
|
|
|
default:
|
|
|
|
CCASSERT(false, "invalidate texture type");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 22:10:50 +08:00
|
|
|
RenderTarget* DeviceMTL::newDefaultRenderTarget(TargetBufferFlags rtf)
|
|
|
|
{
|
|
|
|
auto rtGL = new RenderTargetMTL(true);
|
|
|
|
rtGL->setTargetFlags(rtf);
|
|
|
|
return rtGL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderTarget* DeviceMTL::newRenderTarget(TargetBufferFlags rtf,
|
|
|
|
TextureBackend* colorAttachment,
|
|
|
|
TextureBackend* depthAttachment,
|
|
|
|
TextureBackend* stencilAttachhment)
|
|
|
|
{
|
|
|
|
auto rtGL = new RenderTargetMTL(false);
|
|
|
|
rtGL->setTargetFlags(rtf);
|
|
|
|
rtGL->bindFrameBuffer();
|
2020-09-25 11:07:56 +08:00
|
|
|
RenderTarget::ColorAttachment colors{ {colorAttachment, 0} };
|
|
|
|
rtGL->setColorAttachment(colors);
|
2020-09-21 22:10:50 +08:00
|
|
|
rtGL->setDepthAttachment(depthAttachment);
|
|
|
|
rtGL->setStencilAttachment(stencilAttachhment);
|
|
|
|
return rtGL;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ShaderModule* DeviceMTL::newShaderModule(ShaderStage stage, const std::string& source)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
return new ShaderModuleMTL(_mtlDevice, stage, source);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:32:17 +08:00
|
|
|
DepthStencilState* DeviceMTL::newDepthStencilState()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
return new DepthStencilStateMTL(_mtlDevice);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderPipeline* DeviceMTL::newRenderPipeline()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
return new RenderPipelineMTL(_mtlDevice);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Program* DeviceMTL::newProgram(const std::string& vertexShader, const std::string& fragmentShader)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
return new ProgramMTL(vertexShader, fragmentShader);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceMTL::setFrameBufferOnly(bool frameBufferOnly)
|
|
|
|
{
|
|
|
|
[DeviceMTL::_metalLayer setFramebufferOnly:frameBufferOnly];
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BACKEND_END
|