/****************************************************************************
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.
****************************************************************************/
#pragma once
#include "../CommandBuffer.h"
#include "DeviceMTL.h"
#include <unordered_map>
CC_BACKEND_BEGIN
class RenderPipelineMTL;
class CommandBufferMTL final : public CommandBuffer
{
public:
CommandBufferMTL(DeviceMTL* deviceMTL);
~CommandBufferMTL();
virtual void beginFrame() override;
virtual void beginRenderPass(const RenderPassDescriptor& descriptor) override;
virtual void setRenderPipeline(RenderPipeline* renderPipeline) override;
virtual void setViewport(int x, int y, unsigned int w, unsigned int h) override;
virtual void setCullMode(CullMode mode) override;
virtual void setWinding(Winding winding) override;
virtual void setVertexBuffer(unsigned int index, Buffer* buffer) override;
virtual void setProgramState(ProgramState* programState) override;
virtual void setIndexBuffer(Buffer* buffer) override;
virtual void drawArrays(PrimitiveType primitiveType, unsigned int start, unsigned int count) override;
virtual void drawElements(PrimitiveType primitiveType, IndexFormat indexType, unsigned int count, unsigned int offset) override;
virtual void endRenderPass() override;
virtual void endFrame() override;
virtual void setLineWidth(float lineWidth) override;
virtual void setScissorRect(bool isEnabled, float x, float y, float width, float height) override;
virtual void setDepthStencilState(DepthStencilState* depthStencilState) override;
virtual void captureScreen(std::function<void(const unsigned char*, int, int)> callback) override;
private:
void prepareDrawing() const;
void setTextures() const;
void doSetTextures(bool isVertex) const;
void setUniformBuffer() const;
void afterDraw();
id<MTLRenderCommandEncoder> getRenderCommandEncoder(const RenderPassDescriptor& renderPassDescriptor);
id<MTLCommandBuffer> _mtlCommandBuffer = nil;
id<MTLCommandQueue> _mtlCommandQueue = nil;
id<MTLRenderCommandEncoder> _mtlRenderEncoder = nil;
id<MTLBuffer> _mtlIndexBuffer = nil;
id<MTLTexture> _drawableTexture = nil;
RenderPipelineMTL* _renderPipelineMTL = nullptr;
ProgramState* _programState = nullptr;
id<MTLDepthStencilState> _mtlDepthStencilState = nil;
unsigned int _renderTargetWidth = 0;
unsigned int _renderTargetHeight = 0;
dispatch_semaphore_t _frameBoundarySemaphore;
RenderPassDescriptor _prevRenderPassDescriptor;
};
CC_BACKEND_END