axmol/core/renderer/backend/metal/DeviceMTL.h

170 lines
5.6 KiB
C
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.
****************************************************************************/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#pragma once
#include "../Device.h"
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _metal
* @{
*/
/**
* Create resources from MTLDevice.
*/
class DeviceMTL : public Device
{
public:
/**
* Set CAMetalLayer.
* @param metalLayer A CAMetalLayer object.
*/
static void setCAMetalLayer(CAMetalLayer* metalLayer);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Invoke by engine internally at the beginning of rendering a new frame.
*/
static void updateDrawable();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get a CAMetalLayer.
* @return A CAMetalLayer object.
*/
static CAMetalLayer* getCAMetalLayer() { return DeviceMTL::_metalLayer; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get available Drawable.
* @return an available drawable.
*/
static id<CAMetalDrawable> getCurrentDrawable();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Reset current drawable to nil.
*/
static void resetCurrentDrawable();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/// @name Constructor, Destructor and Initializers
DeviceMTL();
~DeviceMTL();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/// @name Setters & Getters
/**
* New a CommandBuffer object.
* @return A CommandBuffer object.
*/
virtual CommandBuffer* newCommandBuffer() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* New a Buffer object.
* @param size Specifies the size in bytes of the buffer object's new data store.
2021-12-25 10:04:45 +08:00
* @param type Specifies the target buffer object. The symbolic constant must be BufferType::VERTEX or
* BufferType::INDEX.
* @param usage Specifies the expected usage pattern of the data store. The symbolic constant must be
* BufferUsage::STATIC, BufferUsage::DYNAMIC.
2019-11-23 20:27:39 +08:00
* @return A Buffer object.
*/
virtual Buffer* newBuffer(std::size_t size, BufferType type, BufferUsage usage) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* New a TextureBackend object.
* @param descriptor Specifies texture description.
* @return A TextureBackend object.
*/
virtual TextureBackend* newTexture(const TextureDescriptor& descriptor) override;
RenderTarget* newDefaultRenderTarget(TargetBufferFlags rtf) override;
RenderTarget* newRenderTarget(TargetBufferFlags rtf,
2021-12-25 10:04:45 +08:00
TextureBackend* colorAttachment,
TextureBackend* depthAttachment,
TextureBackend* stencilAttachhment) override;
2019-11-23 20:27:39 +08:00
/**
* New a DepthStencilState object.
2019-11-23 20:27:39 +08:00
*/
virtual DepthStencilState* newDepthStencilState() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* New a RenderPipeline object.
* @param descriptor Specifies render pipeline description.
* @return A RenderPipeline object.
*/
virtual RenderPipeline* newRenderPipeline() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* This property controls whether or not the drawables'
* MTLTextures may only be used for framebuffer attachments (YES) or
* whether they may also be used for texture sampling and pixel
* read/write operations (NO).
2021-12-25 10:04:45 +08:00
* @param frameBufferOnly A value of YES allows CAMetalLayer to allocate the MTLTexture objects in ways that are
* optimized for display purposes that makes them unsuitable for sampling. The recommended value for most
* applications is YES.
2019-11-23 20:27:39 +08:00
*/
virtual void setFrameBufferOnly(bool frameBufferOnly) override;
/**
* New a Program, not auto release.
* @param vertexShader Specifes this is a vertex shader source.
* @param fragmentShader Specifes this is a fragment shader source.
* @return A Program instance.
*/
virtual Program* newProgram(std::string_view vertexShader, std::string_view fragmentShader) override;
2019-11-23 20:27:39 +08:00
/**
* Get a MTLDevice object.
* @return A MTLDevice object.
*/
inline id<MTLDevice> getMTLDevice() const { return _mtlDevice; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get a MTLCommandQueue object.
* @return A MTLCommandQueue object.
*/
inline id<MTLCommandQueue> getMTLCommandQueue() const { return _mtlCommandQueue; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
/**
* New a shaderModule.
* @param stage Specifies whether is vertex shader or fragment shader.
* @param source Specifies shader source.
* @return A ShaderModule object.
*/
virtual ShaderModule* newShaderModule(ShaderStage stage, std::string_view source) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
private:
static CAMetalLayer* _metalLayer;
static id<CAMetalDrawable> _currentDrawable;
2021-12-25 10:04:45 +08:00
id<MTLDevice> _mtlDevice = nil;
2019-11-23 20:27:39 +08:00
id<MTLCommandQueue> _mtlCommandQueue = nil;
};
// end of _metal group
/// @}
NS_AX_BACKEND_END