axmol/core/renderer/CustomCommand.h

260 lines
9.6 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
#pragma once
#include "renderer/RenderCommand.h"
2019-11-23 20:27:39 +08:00
/**
* @addtogroup renderer
* @{
*/
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
namespace backend
{
2021-12-25 10:04:45 +08:00
class Buffer;
2019-11-23 20:27:39 +08:00
}
/**
Custom command is used to draw all things except triangle commands. You can use
this command to draw things, just provide vertex/index data and set corret flags.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL CustomCommand : public RenderCommand
2019-11-23 20:27:39 +08:00
{
public:
enum class DrawType
{
ARRAY,
2023-08-31 18:59:12 +08:00
ELEMENT
2019-11-23 20:27:39 +08:00
};
using PrimitiveType = backend::PrimitiveType;
/**
Buffer usage of vertex/index buffer. If the contents are not updated every frame,
then STATIC should be used. Otherwise, DYNAMIC should be used.
This flag is not improtant because most GPU drivers ignore it, so it's best left to STATIC.
2019-11-23 20:27:39 +08:00
*/
using BufferUsage = backend::BufferUsage;
/**
The index format that determines the size of index data. U_SHORT (65535 vertices) is enough for most
cases, But support for U_INT (4294967295 vertices) has been added.
2019-11-23 20:27:39 +08:00
*/
using IndexFormat = backend::IndexFormat;
typedef std::function<void()> CallBackFunc;
2021-12-25 10:04:45 +08:00
/**Constructor.*/
2019-11-23 20:27:39 +08:00
CustomCommand();
2020-10-06 16:46:38 +08:00
CustomCommand(const CustomCommand& rhs);
CustomCommand(CustomCommand&& rhs);
2019-11-23 20:27:39 +08:00
/**Destructor.*/
~CustomCommand();
2020-10-06 16:46:38 +08:00
CustomCommand& operator=(const CustomCommand& rhs);
CustomCommand& operator=(CustomCommand&& rhs);
void assign(const CustomCommand& rhs);
void assign(CustomCommand&& rhs);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
2021-12-25 10:04:45 +08:00
/**
TODO: should remove it.
Init function.
@param globalZOrder GlobalZOrder of the render command.
@param modelViewTransform When in 3D mode, depth sorting needs modelViewTransform.
@param flags Use to identify that the render command is 3D mode or not.
*/
2019-11-23 20:27:39 +08:00
void init(float globalZOrder, const Mat4& modelViewTransform, unsigned int flags);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
Init function. The render command will be in 2D mode.
@param globalZOrder GlobalZOrder of the render command.
*/
void init(float globalZOrder);
/**
Init function. The render command will be in 2D mode.
@param globalZOrder GlobalZOrder of the render command.
@param blendFunc blend function of the render command.
*/
void init(float globalZOrder, const BlendFunc& blendFunc);
/**
Create a vertex buffer of the custom command. The buffer size is (vertexSize * capacity).
If the buffer already exists, then it will delete the old buffer and create a new one.
@param vertexSize the size of every vertex data.
@param capacity how many vertices of the buffer
@param usage the usage of the vertex buffer. Use Static of the vertex data are not updated
every frame, otherwise use DYNAMIC.
*/
void createVertexBuffer(std::size_t vertexSize, std::size_t capacity, BufferUsage usage);
/**
Create an index buffer of the custom command. The buffer size is (indexSize * capacity).
Index size is determined by format. If the buffer already exists, then it will delete the
old buffer and create a new one.
@param format index fomrat, if format is U_SHORT, then the index data type should be
unsigned short, otherwise should be unsigned int.
@param capacity how many indices of the buffer
@param usage the usage of the vertex buffer. Use Static of the index data are not updated
every frame, otherwise use DYNAMIC.
*/
void createIndexBuffer(IndexFormat format, std::size_t capacity, BufferUsage usage);
/**
Update vertex buffer contents.
@param data Specifies a pointer to the new data that will be copied into the data store.
@param length Specifies the length in bytes of the data store region being replaced.
*/
void updateVertexBuffer(void* data, std::size_t length);
/**
Update index buffer contents.
@param data Specifies a pointer to the new data that will be copied into the data store.
@param length Specifies the size in bytes of the data store region being replaced.
*/
void updateIndexBuffer(void* data, std::size_t length);
/**
Update some or all contents of vertex buffer.
@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 length Specifies the size in bytes of the data store region being replaced.
*/
void updateVertexBuffer(void* data, std::size_t offset, std::size_t length);
/**
Update some or call contents of index buffer
@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 length Specifies the size in bytes of the data store region being replaced.
*/
void updateIndexBuffer(void* data, std::size_t offset, std::size_t length);
/**
Get vertex buffer capacity.
*/
inline std::size_t getVertexCapacity() const { return _vertexCapacity; }
/**
Get index buffer capacity.
*/
inline std::size_t getIndexCapacity() const { return _indexCapacity; }
inline void setDrawType(DrawType drawType) { _drawType = drawType; }
inline DrawType getDrawType() const { return _drawType; }
inline void setPrimitiveType(PrimitiveType primitiveType) { _primitiveType = primitiveType; }
inline PrimitiveType getPrimitiveType() const { return _primitiveType; }
/**
Set the vertex buffer. The existing vertex buffer will be replaced if exist.
*/
void setVertexBuffer(backend::Buffer* vertexBuffer);
inline backend::Buffer* getVertexBuffer() const { return _vertexBuffer; }
/**
Set the index buffer. The existing index buffer will be replaced if exist.
*/
void setIndexBuffer(backend::Buffer* indexBuffer, IndexFormat indexFormat);
inline backend::Buffer* getIndexBuffer() const { return _indexBuffer; }
/**
Set the drawing information if the drawing type is ARRAY.
@start specifices the starting index of vertex buffer
@count specifices the number of vertices to be rendered
*/
2021-12-25 10:04:45 +08:00
inline void setVertexDrawInfo(std::size_t start, std::size_t count)
{
_vertexDrawStart = start;
_vertexDrawCount = count;
}
2019-11-23 20:27:39 +08:00
inline std::size_t getVertexDrawStart() const { return _vertexDrawStart; }
2021-12-25 10:04:45 +08:00
inline std::size_t getVertexDrawCount() const { return _vertexDrawCount; }
2019-11-23 20:27:39 +08:00
/**
Set the drawing information if the drawing type is ELEMENT.
@start specifices the starting index of index buffer
@count specifices the number of indices to be rendered
*/
2021-12-25 10:04:45 +08:00
inline void setIndexDrawInfo(std::size_t start, std::size_t count)
{
_indexDrawOffset = start * _indexSize;
_indexDrawCount = count;
}
2019-11-23 20:27:39 +08:00
inline std::size_t getIndexDrawOffset() const { return _indexDrawOffset; }
inline std::size_t getIndexDrawCount() const { return _indexDrawCount; }
2021-12-25 10:04:45 +08:00
2023-08-31 18:59:12 +08:00
inline void setLineWidth(float lineWidth) { _lineWidth = lineWidth; }
inline float getLineWidth() const { return _lineWidth; }
2019-11-23 20:27:39 +08:00
inline IndexFormat getIndexFormat() const { return _indexFormat; }
inline void setIndexFormat(IndexFormat format) { _indexFormat = format; }
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* set a callback which will be invoke before rendering
*/
void setBeforeCallback(const CallBackFunc& before) { _beforeCallback = before; }
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* set a callback which will be invoke after rendering
*/
void setAfterCallback(const CallBackFunc& after) { _afterCallback = after; }
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
const CallBackFunc& getBeforeCallback() { return _beforeCallback; }
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
const CallBackFunc& getAfterCallback() { return _afterCallback; }
2019-11-23 20:27:39 +08:00
protected:
std::size_t computeIndexSize() const;
backend::Buffer* _vertexBuffer = nullptr;
2021-12-25 10:04:45 +08:00
backend::Buffer* _indexBuffer = nullptr;
2019-11-23 20:27:39 +08:00
std::size_t _vertexDrawStart = 0;
std::size_t _vertexDrawCount = 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
std::size_t _indexDrawOffset = 0;
2021-12-25 10:04:45 +08:00
std::size_t _indexDrawCount = 0;
DrawType _drawType = DrawType::ELEMENT;
2019-11-23 20:27:39 +08:00
PrimitiveType _primitiveType = PrimitiveType::TRIANGLE;
2021-12-25 10:04:45 +08:00
IndexFormat _indexFormat = IndexFormat::U_SHORT;
2019-11-23 20:27:39 +08:00
float _lineWidth = 0.0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
std::size_t _indexSize = 0;
std::size_t _vertexCapacity = 0;
2021-12-25 10:04:45 +08:00
std::size_t _indexCapacity = 0;
2019-11-23 20:27:39 +08:00
CallBackFunc _beforeCallback = nullptr;
2021-12-25 10:04:45 +08:00
CallBackFunc _afterCallback = nullptr;
2019-11-23 20:27:39 +08:00
};
NS_AX_END
2019-11-23 20:27:39 +08:00
/**
end of support group
@}
*/