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

104 lines
4.0 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 "../Buffer.h"
#import <Metal/Metal.h>
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _metal
* @{
*/
/**
* @brief Used to store vertex and index data data.
* Dynamic buffer data refers to frequently updated data stored in a buffer.
2021-12-25 10:04:45 +08:00
* To avoid creating new buffers per frame and to minimize processor idle time between frames, implement a triple
* buffering model to update dynamic buffer.
2019-11-23 20:27:39 +08:00
*/
class BufferMTL : public Buffer
{
public:
/// @name Constructor, Destructor and Initializers
/**
* @brief BufferMTL constructor
* @param mtlDevice The device for which MTLBuffer object was created.
* @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
*/
BufferMTL(id<MTLDevice> mtlDevice, std::size_t size, BufferType type, BufferUsage usage);
~BufferMTL();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/// @name Update Buffer
/**
* @brief Update buffer data
* @param data Specifies a pointer to data that will be copied into the data store for initialization.
* @param size Specifies the size in bytes of the data store region being replaced.
* @see `updateSubData(void* data, unsigned int offset, unsigned int size)`
*/
2023-08-31 18:07:23 +08:00
virtual void updateData(void* data, std::size_t size) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Update buffer sub-region data
* @param data Specifies a pointer to the new data that will be copied into the data store.
2021-12-25 10:04:45 +08:00
* @param offset Specifies the offset into the buffer object's data store where data replacement will begin,
* measured in bytes.
2019-11-23 20:27:39 +08:00
* @param size Specifies the size in bytes of the data store region being replaced.
* @see `updateData(void* data, unsigned int size)`
*/
2023-08-31 18:07:23 +08:00
virtual void updateSubData(void* data, std::size_t offset, std::size_t size) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Emply implementation. Mainly used in EGL context lost.
*/
2021-12-25 10:04:45 +08:00
virtual void usingDefaultStoredData(bool needDefaultStoredData) override{};
2019-11-23 20:27:39 +08:00
/// @name Setters & Getters
id<MTLBuffer> getMTLBuffer() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief a triple buffering
* Will switch to next buffer and use the buffer in the following render pass in current frame.
*/
void beginFrame();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
private:
void updateIndex();
2021-12-25 10:04:45 +08:00
id<MTLBuffer> _mtlBuffer = nil;
2019-11-23 20:27:39 +08:00
NSMutableArray* _dynamicDataBuffers = nil;
2021-12-25 10:04:45 +08:00
int _currentFrameIndex = 0;
bool _indexUpdated = false;
2019-11-23 20:27:39 +08:00
};
// end of _metal group
/// @}
NS_AX_BACKEND_END