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 "../CommandBuffer.h"
|
|
|
|
#include "DeviceMTL.h"
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
class RenderPipelineMTL;
|
2020-09-22 16:32:17 +08:00
|
|
|
class DepthStencilStateMTL;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup _metal
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Store encoded commands for the GPU to execute.
|
|
|
|
* A command buffer stores encoded commands until the buffer is committed for execution by the GPU
|
|
|
|
*/
|
|
|
|
class CommandBufferMTL : public CommandBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// @name Constructor, Destructor and Initializers
|
|
|
|
/**
|
|
|
|
* @param deviceMTL The device for which MTLCommandQueue object was created.
|
|
|
|
*/
|
|
|
|
CommandBufferMTL(DeviceMTL* deviceMTL);
|
|
|
|
~CommandBufferMTL();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-09-22 16:32:17 +08:00
|
|
|
/**
|
|
|
|
* Set depthStencil status
|
|
|
|
* @param depthStencilState Specifies the depth and stencil status
|
|
|
|
*/
|
|
|
|
virtual void setDepthStencilState(DepthStencilState* depthStencilState) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-09-22 16:32:17 +08:00
|
|
|
/**
|
|
|
|
* Sets the current render pipeline state object once
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param renderPipeline An object that contains the graphics functions and configuration state used in a render
|
|
|
|
* pass.
|
2020-09-22 16:32:17 +08:00
|
|
|
*/
|
|
|
|
virtual void setRenderPipeline(RenderPipeline* renderPipeline) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/// @name Setters & Getters
|
|
|
|
/**
|
|
|
|
* @brief Indicate the begining of a frame
|
|
|
|
* Wait until the inflight command buffer has completed its work.
|
|
|
|
* Then create MTLCommandBuffer and enqueue it to MTLCommandQueue.
|
|
|
|
* Then start schedule available MTLBuffer
|
|
|
|
*/
|
2021-04-22 22:01:47 +08:00
|
|
|
virtual bool beginFrame() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-04-22 22:01:47 +08:00
|
|
|
* Create a MTLRenderCommandEncoder object for graphics rendering to an attachment in a RenderPassDescriptor.
|
|
|
|
* MTLRenderCommandEncoder is cached if current RenderPassDescriptor is identical to previous one.
|
2019-11-23 20:27:39 +08:00
|
|
|
* @param descriptor Specifies a group of render targets that hold the results of a render pass.
|
|
|
|
*/
|
2021-04-22 22:01:47 +08:00
|
|
|
virtual void beginRenderPass(const RenderTarget* renderTarget, const RenderPassDescriptor& descriptor) override;
|
2020-09-22 16:32:17 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2020-09-22 16:32:17 +08:00
|
|
|
* Update depthStencil status, improvment: for metal backend cache it
|
|
|
|
* @param depthStencilState Specifies the depth and stencil status
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2020-09-22 16:32:17 +08:00
|
|
|
virtual void updateDepthStencilState(const DepthStencilDescriptor& descriptor) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update render pipeline status
|
|
|
|
* Building a programmable pipeline involves an expensive evaluation of GPU state.
|
|
|
|
* So a new render pipeline object will be created only if it hasn't been created before.
|
|
|
|
* @param rt Specifies the render target.
|
|
|
|
* @param pipelineDescriptor Specifies the pipeline descriptor.
|
|
|
|
*/
|
|
|
|
virtual void updatePipelineState(const RenderTarget* rt, const PipelineDescriptor& descriptor) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Fixed-function state
|
|
|
|
* @param x The x coordinate of the upper-left corner of the viewport.
|
|
|
|
* @param y The y coordinate of the upper-left corner of the viewport.
|
|
|
|
* @param w The width of the viewport, in pixels.
|
|
|
|
* @param h The height of the viewport, in pixels.
|
|
|
|
*/
|
|
|
|
virtual void setViewport(int x, int y, unsigned int w, unsigned int h) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Fixed-function state
|
|
|
|
* @param mode Controls if primitives are culled when front facing, back facing, or not culled at all.
|
|
|
|
*/
|
|
|
|
virtual void setCullMode(CullMode mode) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Fixed-function state
|
|
|
|
* @param winding The winding order of front-facing primitives.
|
|
|
|
*/
|
|
|
|
virtual void setWinding(Winding winding) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Set a global buffer for all vertex shaders at the given bind point index 0.
|
|
|
|
* @param buffer The buffer to set in the buffer argument table.
|
|
|
|
*/
|
|
|
|
virtual void setVertexBuffer(Buffer* buffer) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Set the uniform data at a given vertex and fragment buffer binding point 1
|
|
|
|
* Set a global texture for all vertex and fragment shaders at the given bind location.
|
|
|
|
* @param programState A programState object that hold the uniform and texture data.
|
|
|
|
*/
|
|
|
|
virtual void setProgramState(ProgramState* programState) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Set indexes when drawing primitives with index list
|
|
|
|
* @ buffer A buffer object that the device will read indexes from.
|
|
|
|
* @ see `drawElements(PrimitiveType primitiveType, IndexFormat indexType, unsigned int count, unsigned int offset)`
|
|
|
|
*/
|
|
|
|
virtual void setIndexBuffer(Buffer* buffer) override;
|
2023-08-31 17:57:36 +08:00
|
|
|
|
|
|
|
void setInstanceBuffer(Buffer* buffer) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Draw primitives without an index list.
|
|
|
|
* @param primitiveType The type of primitives that elements are assembled into.
|
|
|
|
* @param start For each instance, the first index to draw
|
|
|
|
* @param count For each instance, the number of indexes to draw
|
|
|
|
* @see `drawElements(PrimitiveType primitiveType, IndexFormat indexType, unsigned int count, unsigned int offset)`
|
2022-08-06 16:17:55 +08:00
|
|
|
*
|
|
|
|
* TODO: Implement a wireframe mode for METAL devices. Refer to: https://forums.ogre3d.org/viewtopic.php?t=95089
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2022-08-06 16:17:55 +08:00
|
|
|
virtual void drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count, bool wireframe) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Draw primitives with an index list.
|
|
|
|
* @param primitiveType The type of primitives that elements are assembled into.
|
|
|
|
* @param indexType The type if indexes, either 16 bit integer or 32 bit integer.
|
|
|
|
* @param count The number of indexes to read from the index buffer for each instance.
|
|
|
|
* @param offset Byte offset within indexBuffer to start reading indexes from.
|
|
|
|
* @see `setIndexBuffer(Buffer* buffer)`
|
|
|
|
* @see `drawArrays(PrimitiveType primitiveType, unsigned int start, unsigned int count)`
|
2022-08-06 16:17:55 +08:00
|
|
|
*
|
|
|
|
* TODO: Implement a wireframe mode for METAL devices. Refer to: https://forums.ogre3d.org/viewtopic.php?t=95089
|
2021-12-25 10:04:45 +08:00
|
|
|
*/
|
|
|
|
virtual void drawElements(PrimitiveType primitiveType,
|
|
|
|
IndexFormat indexType,
|
|
|
|
std::size_t count,
|
2022-08-06 16:17:55 +08:00
|
|
|
std::size_t offset,
|
|
|
|
bool wireframe) override;
|
2023-08-31 17:57:36 +08:00
|
|
|
|
|
|
|
void drawElementsInstanced(PrimitiveType primitiveType,
|
|
|
|
IndexFormat indexType,
|
|
|
|
std::size_t count,
|
|
|
|
std::size_t offset,
|
|
|
|
int instanceCount,
|
|
|
|
bool wireframe = false) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Do some resources release.
|
|
|
|
*/
|
|
|
|
virtual void endRenderPass() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Present a drawable and commit a command buffer so it can be executed as soon as possible.
|
|
|
|
*/
|
|
|
|
virtual void endFrame() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-11-01 16:02:13 +08:00
|
|
|
void endEncoding();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Fixed-function state
|
|
|
|
* @param x, y Specifies the lower left corner of the scissor box
|
|
|
|
* @param wdith Specifies the width of the scissor box
|
|
|
|
* @param height Specifies the height of the scissor box
|
|
|
|
*/
|
|
|
|
virtual void setScissorRect(bool isEnabled, float x, float y, float width, float height) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2020-09-21 22:10:50 +08:00
|
|
|
* Read pixels from RenderTarget
|
|
|
|
* @param callback A callback to deal with pixel data read.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2020-09-21 22:10:50 +08:00
|
|
|
virtual void readPixels(RenderTarget* rt, std::function<void(const PixelBufferDescriptor&)> callback) override;
|
2022-11-01 16:02:13 +08:00
|
|
|
|
|
|
|
id<MTLRenderCommandEncoder> getRenderCommandEncoder() const { return _mtlRenderEncoder; }
|
|
|
|
|
|
|
|
id<MTLCommandBuffer> getMTLCommandBuffer() const { return _mtlCommandBuffer; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
protected:
|
2020-09-21 22:10:50 +08:00
|
|
|
/**
|
|
|
|
* Read a block of pixels from the given texture
|
|
|
|
* @param texture Specifies the texture to get the image.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param origX,origY Specify the window coordinates of the first pixel that is read from the given texture. This
|
|
|
|
* location is the lower left corner of a rectangular block of pixels.
|
|
|
|
* @param rectWidth,rectHeight Specify the dimensions of the pixel rectangle. rectWidth and rectHeight of one
|
|
|
|
* correspond to a single pixel.
|
2020-09-21 22:10:50 +08:00
|
|
|
* @param pbd, the output buffer for fill texels data
|
|
|
|
* @remark: !!!this function only can call after endFrame, then it's could be works well.
|
2021-12-25 10:04:45 +08:00
|
|
|
*/
|
|
|
|
static void readPixels(TextureBackend* texture,
|
|
|
|
std::size_t origX,
|
|
|
|
std::size_t origY,
|
|
|
|
std::size_t rectWidth,
|
|
|
|
std::size_t rectHeight,
|
|
|
|
PixelBufferDescriptor& pbd);
|
|
|
|
static void readPixels(id<MTLTexture> texture,
|
|
|
|
std::size_t origX,
|
|
|
|
std::size_t origY,
|
|
|
|
std::size_t rectWidth,
|
|
|
|
std::size_t rectHeight,
|
|
|
|
PixelBufferDescriptor& pbd);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
|
|
|
void prepareDrawing() const;
|
|
|
|
void setTextures() const;
|
|
|
|
void doSetTextures(bool isVertex) const;
|
|
|
|
void setUniformBuffer() const;
|
|
|
|
void afterDraw();
|
2020-09-10 18:12:46 +08:00
|
|
|
void flush();
|
2020-09-11 00:10:44 +08:00
|
|
|
void flushCaptureCommands();
|
2021-04-22 22:01:47 +08:00
|
|
|
void updateRenderCommandEncoder(const RenderTarget* renderTarget, const RenderPassDescriptor& renderPassParams);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
id<MTLCommandBuffer> _mtlCommandBuffer = nil;
|
|
|
|
id<MTLCommandQueue> _mtlCommandQueue = nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
id<MTLRenderCommandEncoder> _mtlRenderEncoder = nil;
|
2021-12-25 10:04:45 +08:00
|
|
|
id<MTLBuffer> _mtlIndexBuffer = nil;
|
|
|
|
id<MTLTexture> _drawableTexture = nil;
|
|
|
|
|
2020-09-22 16:32:17 +08:00
|
|
|
DepthStencilStateMTL* _depthStencilStateMTL = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
RenderPipelineMTL* _renderPipelineMTL = nullptr;
|
|
|
|
ProgramState* _programState = nullptr;
|
|
|
|
|
|
|
|
unsigned int _renderTargetWidth = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int _renderTargetHeight = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
dispatch_semaphore_t _frameBoundarySemaphore;
|
2021-12-25 10:04:45 +08:00
|
|
|
const RenderTarget* _currentRenderTarget = nil; // weak ref
|
2021-04-22 22:01:47 +08:00
|
|
|
RenderPassDescriptor _currentRenderPassDesc;
|
2020-11-08 13:12:06 +08:00
|
|
|
TargetBufferFlags _currentRenderTargetFlags = TargetBufferFlags::NONE;
|
2021-12-25 10:04:45 +08:00
|
|
|
NSAutoreleasePool* _autoReleasePool = nil;
|
|
|
|
|
|
|
|
std::vector<std::pair<TextureBackend*, std::function<void(const PixelBufferDescriptor&)>>> _captureCallbacks;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// end of _metal group
|
|
|
|
/// @}
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|