From 894840a31fdf1c46ab8ca2c79d8a573d8d289291 Mon Sep 17 00:00:00 2001 From: halx99 Date: Sat, 5 Oct 2024 17:30:44 +0800 Subject: [PATCH] Improve DrawNode, don't create backend::Buffer always --- core/2d/DrawNode.cpp | 167 +++++++++--------- core/2d/DrawNode.h | 116 ++++++------ core/renderer/CustomCommand.cpp | 23 ++- core/renderer/CustomCommand.h | 14 +- .../spine/src/spine/SkeletonRenderer.cpp | 45 +++-- .../Box2DTestBed/Box2DTestDebugDrawNode.cpp | 52 +++--- 6 files changed, 214 insertions(+), 203 deletions(-) diff --git a/core/2d/DrawNode.cpp b/core/2d/DrawNode.cpp index 8ba79e6f068..2ae0beb6280 100644 --- a/core/2d/DrawNode.cpp +++ b/core/2d/DrawNode.cpp @@ -72,7 +72,7 @@ static bool isConvex(const Vec2* verts, int count) return true; // is convex } -static V2F_C4B_T2F* expandBufferAndGetPointer(axstd::pod_vector& buffer, size_t count) +static V2F_C4F_T2F* expandBufferAndGetPointer(axstd::pod_vector& buffer, size_t count) { size_t oldSize = buffer.size(); buffer.expand(count); @@ -235,7 +235,7 @@ void DrawNode::draw(Renderer* renderer, const Mat4& transform, uint32_t flags) } } -static void udpateCommand(CustomCommand& cmd, const axstd::pod_vector& buffer) +static void udpateCommand(CustomCommand& cmd, const axstd::pod_vector& buffer) { if (buffer.empty()) { @@ -243,8 +243,9 @@ static void udpateCommand(CustomCommand& cmd, const axstd::pod_vector triangleList; + std::vector triangleList; int vertex_count = 0; @@ -859,7 +860,7 @@ void DrawNode::_drawPolygon(const Vec2* verts, p2t::Point* vec2 = t->GetPoint(1); p2t::Point* vec3 = t->GetPoint(2); - V2F_C4B_T2F_Triangle triangle = { + V2F_C4F_T2F_Triangle triangle = { {Vec2(vec1->x, vec1->y), fillColor, Vec2::ZERO}, {Vec2(vec2->x, vec2->y), fillColor, Vec2::ZERO}, {Vec2(vec3->x, vec3->y), fillColor, Vec2::ZERO}, @@ -886,7 +887,7 @@ void DrawNode::_drawPolygon(const Vec2* verts, vertex_count *= 3; - auto triangles = reinterpret_cast(expandBufferAndGetPointer(_triangles, vertex_count)); + auto triangles = reinterpret_cast(expandBufferAndGetPointer(_triangles, vertex_count)); _trianglesDirty = true; // start drawing... @@ -1019,7 +1020,7 @@ void DrawNode::_drawPolygon(const Vec2* verts, void DrawNode::_drawPoly(const Vec2* verts, unsigned int count, bool closedPolygon, - const Color4B& color, + const Color& color, float thickness, bool isconvex) { @@ -1046,13 +1047,13 @@ void DrawNode::_drawPoly(const Vec2* verts, } else { - _drawPolygon(verts, count, Color4B::TRANSPARENT, color, closedPolygon, thickness, isconvex); + _drawPolygon(verts, count, Color::TRANSPARENT, color, closedPolygon, thickness, isconvex); } } void DrawNode::_drawSegment(const Vec2& from, const Vec2& to, - const Color4B& color, + const Color& color, float thickness, DrawNode::EndType etStart, DrawNode::EndType etEnd) @@ -1087,7 +1088,7 @@ void DrawNode::_drawSegment(const Vec2& from, unsigned int vertex_count = 3 * ((etStart != DrawNode::EndType::Butt) ? 2 : 0) + 3 * 2 + 3 * ((etEnd != DrawNode::EndType::Butt) ? 2 : 0); - auto triangles = reinterpret_cast(expandBufferAndGetPointer(_triangles, vertex_count)); + auto triangles = reinterpret_cast(expandBufferAndGetPointer(_triangles, vertex_count)); _trianglesDirty = true; int ii = 0; @@ -1180,16 +1181,16 @@ void DrawNode::_drawSegment(const Vec2& from, } } -void DrawNode::_drawDot(const Vec2& pos, float radius, const Color4B& color) +void DrawNode::_drawDot(const Vec2& pos, float radius, const Color& color) { unsigned int vertex_count = 2 * 3; - auto triangles = reinterpret_cast(expandBufferAndGetPointer(_triangles, vertex_count)); + auto triangles = reinterpret_cast(expandBufferAndGetPointer(_triangles, vertex_count)); _trianglesDirty = true; - V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), color, Vec2(-1.0f, -1.0f)}; - V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), color, Vec2(-1.0f, 1.0f)}; - V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), color, Vec2(1.0f, 1.0f)}; - V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), color, Vec2(1.0f, -1.0f)}; + V2F_C4F_T2F a = {Vec2(pos.x - radius, pos.y - radius), color, Vec2(-1.0f, -1.0f)}; + V2F_C4F_T2F b = {Vec2(pos.x - radius, pos.y + radius), color, Vec2(-1.0f, 1.0f)}; + V2F_C4F_T2F c = {Vec2(pos.x + radius, pos.y + radius), color, Vec2(1.0f, 1.0f)}; + V2F_C4F_T2F d = {Vec2(pos.x + radius, pos.y - radius), color, Vec2(1.0f, -1.0f)}; triangles[0] = {a, b, c}; triangles[1] = {a, c, d}; @@ -1202,8 +1203,8 @@ void DrawNode::_drawCircle(const Vec2& center, bool drawLineToCenter, float scaleX, float scaleY, - const Color4B& borderColor, - const Color4B& fillColor, + const Color& borderColor, + const Color& fillColor, bool solid, float thickness) { @@ -1234,8 +1235,8 @@ void DrawNode::_drawCircle(const Vec2& center, } void DrawNode::_drawTriangle(Vec2* vertices3, - const Color4B& borderColor, - const Color4B& fillColor, + const Color& borderColor, + const Color& fillColor, bool solid, float thickness) { @@ -1249,7 +1250,7 @@ void DrawNode::_drawTriangle(Vec2* vertices3, { applyTransform(vertices3, vertices3, vertex_count); - auto triangles = reinterpret_cast(expandBufferAndGetPointer(_triangles, vertex_count)); + auto triangles = reinterpret_cast(expandBufferAndGetPointer(_triangles, vertex_count)); _trianglesDirty = true; triangles[0] = {{vertices3[0], fillColor, Vec2::ZERO}, @@ -1262,8 +1263,8 @@ void DrawNode::_drawAStar(const Vec2& center, float radiusI, // inner float radiusO, // outer unsigned int segments, - const Color4B& color, - const Color4B& filledColor, + const Color& color, + const Color& filledColor, float thickness, bool solid) { @@ -1294,7 +1295,7 @@ void DrawNode::_drawAStar(const Vec2& center, void DrawNode::_drawPoints(const Vec2* position, unsigned int numberOfPoints, const float pointSize, - const Color4B& color, + const Color& color, const DrawNode::PointType pointType) { if (properties.drawOrder == true) @@ -1307,7 +1308,7 @@ void DrawNode::_drawPoints(const Vec2* position, { case PointType::Circle: { - _drawCircle(position[i], pointSize4, 90, 32, false, 1.0f, 1.0f, Color4B(), color, true); + _drawCircle(position[i], pointSize4, 90, 32, false, 1.0f, 1.0f, Color(), color, true); break; } case PointType::Rect: @@ -1337,7 +1338,7 @@ void DrawNode::_drawPoints(const Vec2* position, void DrawNode::_drawPoint(const Vec2& position, const float pointSize, - const Color4B& color, + const Color& color, const DrawNode::PointType pointType) { if (properties.drawOrder == true) @@ -1349,7 +1350,7 @@ void DrawNode::_drawPoint(const Vec2& position, { case PointType::Circle: { - _drawCircle(position, pointSize4, 90, 32, false, 1.0f, 1.0f, Color4B(), color, true); + _drawCircle(position, pointSize4, 90, 32, false, 1.0f, 1.0f, Color(), color, true); break; } case PointType::Rect: @@ -1392,8 +1393,8 @@ void DrawNode::_drawPie(const Vec2& center, int endAngle, float scaleX, float scaleY, - const Color4B& fillColor, - const Color4B& borderColor, + const Color& fillColor, + const Color& borderColor, DrawMode drawMode, float thickness) { @@ -1412,11 +1413,11 @@ void DrawNode::_drawPie(const Vec2& center, _drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, fillColor, true, thickness); break; case DrawMode::Outline: - _drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, Color4B::TRANSPARENT, true, + _drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, Color::TRANSPARENT, true, thickness); break; case DrawMode::Line: - _drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, Color4B::TRANSPARENT, true, + _drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, Color::TRANSPARENT, true, thickness); break; case DrawMode::Semi: @@ -1466,10 +1467,10 @@ void DrawNode::_drawPie(const Vec2& center, case DrawMode::Outline: _vertices[n++] = center; _vertices[n++] = _vertices[0]; - _drawPolygon(_vertices.data(), n, Color4B::TRANSPARENT, borderColor, false, thickness, false); + _drawPolygon(_vertices.data(), n, Color::TRANSPARENT, borderColor, false, thickness, false); break; case DrawMode::Line: - _drawPolygon(_vertices.data(), n - 1, Color4B::TRANSPARENT, borderColor, false, thickness, false); + _drawPolygon(_vertices.data(), n - 1, Color::TRANSPARENT, borderColor, false, thickness, false); break; case DrawMode::Semi: _drawPolygon(_vertices.data(), n - 1, fillColor, borderColor, true, thickness, false); diff --git a/core/2d/DrawNode.h b/core/2d/DrawNode.h index e6710e3b0b3..0cf8942ae2d 100644 --- a/core/2d/DrawNode.h +++ b/core/2d/DrawNode.h @@ -101,7 +101,7 @@ class AX_DLL DrawNode : public Node */ void drawPoint(const Vec2& point, const float pointSize, - const Color4B& color, + const Color& color, DrawNode::PointType pointType = DrawNode::PointType::Rect); /** Draw a group point. @@ -113,7 +113,7 @@ class AX_DLL DrawNode : public Node */ void drawPoints(const Vec2* position, unsigned int numberOfPoints, - const Color4B& color, + const Color& color, DrawNode::PointType pointType = DrawNode::PointType::Rect); /** Draw a group point. @@ -127,7 +127,7 @@ class AX_DLL DrawNode : public Node void drawPoints(const Vec2* position, unsigned int numberOfPoints, const float pointSize, - const Color4B& color, + const Color& color, DrawNode::PointType pointType = DrawNode::PointType::Rect); /** Draw an line from origin to destination with color. @@ -139,7 +139,7 @@ class AX_DLL DrawNode : public Node */ void drawLine(const Vec2& origin, const Vec2& destination, - const Color4B& color, + const Color& color, float thickness = 1.0f, DrawNode::EndType etStart = DrawNode::EndType::Round, DrawNode::EndType etEnd = DrawNode::EndType::Round); @@ -151,7 +151,7 @@ class AX_DLL DrawNode : public Node * @param destination The rectangle destination. * @param color The rectangle color. */ - void drawRect(const Vec2& origin, const Vec2& destination, const Color4B& color, float thickness = 1.0f); + void drawRect(const Vec2& origin, const Vec2& destination, const Color& color, float thickness = 1.0f); /** Draws a polygon given a pointer to point coordinates and the number of vertices measured in points. * The polygon can be closed or open. @@ -164,7 +164,7 @@ class AX_DLL DrawNode : public Node void drawPoly(const Vec2* poli, unsigned int numberOfPoints, bool closedPolygon, - const Color4B& color, + const Color& color, float thickness = 1.0f); /** Draws a circle given the center, radius and number of segments. @@ -186,7 +186,7 @@ class AX_DLL DrawNode : public Node bool drawLineToCenter, float scaleX, float scaleY, - const Color4B& color, + const Color& color, float thickness = 1.0f); /** Draws a circle given the center, radius and number of segments. @@ -204,7 +204,7 @@ class AX_DLL DrawNode : public Node float angle, unsigned int segments, bool drawLineToCenter, - const Color4B& color, + const Color& color, float thickness = 1.0f); /** Draws a star given the center, radiusI, radiusO and number of segments. @@ -220,7 +220,7 @@ class AX_DLL DrawNode : public Node float radiusI, float radiusO, unsigned int segments, - const Color4B& color, + const Color& color, float thickness = 1.0f); /** Draws a solid star given the center, radiusI, radiusO and number of segments. @@ -236,8 +236,8 @@ class AX_DLL DrawNode : public Node float radiusI, float radiusO, unsigned int segments, - const Color4B& color, - const Color4B& filledColor, + const Color& color, + const Color& filledColor, float thickness = 1.0f); /** Draws a quad bezier path. @@ -252,7 +252,7 @@ class AX_DLL DrawNode : public Node const Vec2& control, const Vec2& destination, unsigned int segments, - const Color4B& color, + const Color& color, float thickness = 1.0f); /** Draw a cubic bezier curve with color and number of segments @@ -269,7 +269,7 @@ class AX_DLL DrawNode : public Node const Vec2& control2, const Vec2& destination, unsigned int segments, - const Color4B& color, + const Color& color, float thickness = 1.0f); /** Draws a Cardinal Spline path. @@ -282,7 +282,7 @@ class AX_DLL DrawNode : public Node void drawCardinalSpline(PointArray* config, float tension, unsigned int segments, - const Color4B& color, + const Color& color, float thickness = 1.0f); /** Draws a Catmull Rom path. @@ -291,7 +291,7 @@ class AX_DLL DrawNode : public Node * @param segments The number of segments. * @param color The Catmull Rom color. */ - void drawCatmullRom(PointArray* points, unsigned int segments, const Color4B& color, float thickness = 1.0f); + void drawCatmullRom(PointArray* points, unsigned int segments, const Color& color, float thickness = 1.0f); /** draw a dot at a position, with a given radius and color. * @@ -299,7 +299,7 @@ class AX_DLL DrawNode : public Node * @param radius The dot radius. * @param color The dot color. */ - void drawDot(const Vec2& pos, float radius, const Color4B& color); + void drawDot(const Vec2& pos, float radius, const Color& color); /** Draws a rectangle with 4 points. * @@ -313,7 +313,7 @@ class AX_DLL DrawNode : public Node const Vec2& p2, const Vec2& p3, const Vec2& p4, - const Color4B& color, + const Color& color, float thickness = 1.0f); /** Draws a solid rectangle given the origin and destination point measured in points. @@ -326,9 +326,9 @@ class AX_DLL DrawNode : public Node */ void drawSolidRect(const Vec2& origin, const Vec2& destination, - const Color4B& color, + const Color& color, float thickness = 0, - const Color4B& borderColor = Color4B(0, 0, 0, 0)); + const Color& borderColor = Color(0, 0, 0, 0)); /** Draws a solid polygon given a pointer to CGPoint coordinates, the number of vertices measured in points, and a * color. @@ -340,9 +340,9 @@ class AX_DLL DrawNode : public Node */ void drawSolidPoly(const Vec2* poli, unsigned int numberOfPoints, - const Color4B& color, + const Color& color, float thickness = 0, - const Color4B& borderColor = Color4B(0, 0, 0, 0), + const Color& borderColor = Color(0, 0, 0, 0), bool isconvex = false); /** Draws a solid circle given the center, radius and number of segments. @@ -364,9 +364,9 @@ class AX_DLL DrawNode : public Node unsigned int segments, float scaleX, float scaleY, - const Color4B& fillColor, + const Color& fillColor, float thickness, - const Color4B& borderColor, + const Color& borderColor, bool drawLineToCenter = false); /** Draws a solid circle given the center, radius and number of segments. @@ -385,7 +385,7 @@ class AX_DLL DrawNode : public Node unsigned int segments, float scaleX, float scaleY, - const Color4B& color); + const Color& color); /** Draws a solid circle given the center, radius and number of segments. * @param center The circle center point. @@ -395,7 +395,7 @@ class AX_DLL DrawNode : public Node * @param color The solid circle color. * @js NA */ - void drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, const Color4B& color); + void drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, const Color& color); /** Draws a pie given the center, radius, angle, start angle, end angle and number of segments. * @param center The circle center point. @@ -417,8 +417,8 @@ class AX_DLL DrawNode : public Node int endAngle, float scaleX, float scaleY, - const Color4B& fillColor, - const Color4B& borderColor, + const Color& fillColor, + const Color& borderColor, DrawMode drawMode = DrawMode::Outline, float thickness = 1.0f); @@ -442,7 +442,7 @@ class AX_DLL DrawNode : public Node int endAngle, float scaleX, float scaleY, - const Color4B& color, + const Color& color, DrawMode drawMode = DrawMode::Outline); void setIsConvex(bool isConvex) @@ -461,7 +461,7 @@ class AX_DLL DrawNode : public Node void drawSegment(const Vec2& from, const Vec2& to, float radius, - const Color4B& color, + const Color& color, DrawNode::EndType etStart = DrawNode::EndType::Round, DrawNode::EndType etEnd = DrawNode::EndType::Round); @@ -480,18 +480,18 @@ class AX_DLL DrawNode : public Node */ void drawPolygon(Vec2* verts, int count, - const Color4B& fillColor, + const Color& fillColor, float thickness, - const Color4B& borderColor, + const Color& borderColor, bool isconvex = false); - void drawPolygon(Vec2* verts, int count, float thickness, const Color4B& borderColor, bool isconvex = false); + void drawPolygon(Vec2* verts, int count, float thickness, const Color& borderColor, bool isconvex = false); void drawSolidPolygon(Vec2* verts, int count, - const Color4B& fillColor, + const Color& fillColor, float thickness, - const Color4B& borderColor, + const Color& borderColor, bool isconvex = false); /** draw a triangle with color. @@ -503,20 +503,20 @@ class AX_DLL DrawNode : public Node * @js NA */ - void drawTriangle(const Vec2* vertices3, const Color4B& color); + void drawTriangle(const Vec2* vertices3, const Color& color); - void drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color4B& color); + void drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color& color); void drawSolidTriangle(const Vec2* vertices3, - const Color4B& fillColor, - const Color4B& borderColor, + const Color& fillColor, + const Color& borderColor, float thickness = 1.0f); void drawSolidTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, - const Color4B& fillColor, - const Color4B& borderColor, + const Color& fillColor, + const Color& borderColor, float thickness = 1.0f); /** Clear the geometry in the node's buffer. */ @@ -577,33 +577,33 @@ class AX_DLL DrawNode : public Node CustomCommand _customCommandPoint; CustomCommand _customCommandLine; - axstd::pod_vector _triangles; - axstd::pod_vector _points; - axstd::pod_vector _lines; + axstd::pod_vector _triangles; + axstd::pod_vector _points; + axstd::pod_vector _lines; private: // Internal function _drawPoint void _drawPoint(const Vec2& position, const float pointSize, - const Color4B& color, + const Color& color, const DrawNode::PointType pointType); // Internal function _drawPoints void _drawPoints(const Vec2* position, unsigned int numberOfPoints, const float pointSize, - const Color4B& color, + const Color& color, const DrawNode::PointType pointType); // Internal function _drawDot - void _drawDot(const Vec2& pos, float radius, const Color4B& color); + void _drawDot(const Vec2& pos, float radius, const Color& color); // Internal function _drawTriangle // Note: modifies supplied vertex array void _drawTriangle(Vec2* vertices3, - const Color4B& borderColor, - const Color4B& fillColor, + const Color& borderColor, + const Color& fillColor, bool solid = true, float thickness = 0.0f); @@ -612,8 +612,8 @@ class AX_DLL DrawNode : public Node float radiusI, float radiusO, unsigned int segments, - const Color4B& color, - const Color4B& filledColor, + const Color& color, + const Color& filledColor, float thickness = 1.0f, bool solid = false); @@ -621,15 +621,15 @@ class AX_DLL DrawNode : public Node void _drawPoly(const Vec2* poli, unsigned int numberOfPoints, bool closedPolygon, - const Color4B& color, + const Color& color, float thickness = 1.0f, bool isconvex = true); // Internal function _drawPolygon void _drawPolygon(const Vec2* verts, unsigned int count, - const Color4B& fillColor, - const Color4B& borderColor, + const Color& fillColor, + const Color& borderColor, bool closedPolygon = true, float thickness = 1.0f, bool isconvex = true); @@ -637,7 +637,7 @@ class AX_DLL DrawNode : public Node // Internal function _drawSegment void _drawSegment(const Vec2& origin, const Vec2& destination, - const Color4B& color, + const Color& color, float thickness = 1.0f, DrawNode::EndType etStart = DrawNode::EndType::Square, DrawNode::EndType etEnd = DrawNode::EndType::Square); @@ -650,8 +650,8 @@ class AX_DLL DrawNode : public Node bool drawLineToCenter, float scaleX, float scaleY, - const Color4B& borderColor, - const Color4B& fillColor, + const Color& borderColor, + const Color& fillColor, bool solid, float thickness = 1.0f); @@ -663,8 +663,8 @@ class AX_DLL DrawNode : public Node int endAngle, float scaleX, float scaleY, - const Color4B& fillColor, - const Color4B& borderColor, + const Color& fillColor, + const Color& borderColor, DrawMode drawMode, float thickness = 1.0f); diff --git a/core/renderer/CustomCommand.cpp b/core/renderer/CustomCommand.cpp index 1d1e484bf21..93cf6cbc15a 100644 --- a/core/renderer/CustomCommand.cpp +++ b/core/renderer/CustomCommand.cpp @@ -143,14 +143,17 @@ void CustomCommand::createVertexBuffer(std::size_t vertexSize, std::size_t capac _vertexCapacity = capacity; _vertexDrawCount = capacity; - _vertexBuffer = backend::DriverBase::getInstance()->newBuffer(vertexSize * capacity, backend::BufferType::VERTEX, usage); + _vertexBuffer = + backend::DriverBase::getInstance()->newBuffer(vertexSize * capacity, backend::BufferType::VERTEX, usage); } -void CustomCommand::createInstanceBuffer(std::size_t vertexSize, std::size_t capacity, BufferUsage usage) +void CustomCommand::createInstanceBuffer(std::size_t vertexSize, int capacity, BufferUsage usage) { AX_SAFE_RELEASE(_instanceBuffer); - _instanceBuffer = backend::DriverBase::getInstance()->newBuffer(vertexSize * capacity, backend::BufferType::VERTEX, usage); - _instanceCount = capacity; + _instanceBuffer = + backend::DriverBase::getInstance()->newBuffer(vertexSize * capacity, backend::BufferType::VERTEX, usage); + _instanceCapacity = capacity; + _instanceCount = capacity; } void CustomCommand::setInstanceBuffer(backend::Buffer* instanceBuffer, int count) @@ -160,6 +163,7 @@ void CustomCommand::setInstanceBuffer(backend::Buffer* instanceBuffer, int count AX_SAFE_RELEASE(_instanceBuffer); _instanceBuffer = instanceBuffer; _instanceCount = count; + _instanceCapacity = count; AX_SAFE_RETAIN(_instanceBuffer); } } @@ -173,7 +177,8 @@ void CustomCommand::createIndexBuffer(IndexFormat format, std::size_t capacity, _indexCapacity = capacity; _indexDrawCount = capacity; - _indexBuffer = backend::DriverBase::getInstance()->newBuffer(_indexSize * capacity, backend::BufferType::INDEX, usage); + _indexBuffer = + backend::DriverBase::getInstance()->newBuffer(_indexSize * capacity, backend::BufferType::INDEX, usage); } void CustomCommand::updateVertexBuffer(const void* data, std::size_t offset, std::size_t length) @@ -223,6 +228,12 @@ void CustomCommand::updateIndexBuffer(const void* data, std::size_t length) _indexBuffer->updateData(data, length); } +void CustomCommand::updateInstanceBuffer(const void* data, std::size_t length) +{ + assert(_instanceBuffer); + _instanceBuffer->updateData(data, length); +} + std::size_t CustomCommand::computeIndexSize() const { if (IndexFormat::U_SHORT == _indexFormat) @@ -231,4 +242,4 @@ std::size_t CustomCommand::computeIndexSize() const return sizeof(unsigned int); } -} +} // namespace ax diff --git a/core/renderer/CustomCommand.h b/core/renderer/CustomCommand.h index 3cd0a97d58d..4ce40912cae 100644 --- a/core/renderer/CustomCommand.h +++ b/core/renderer/CustomCommand.h @@ -113,7 +113,7 @@ TODO: should remove it. every frame, otherwise use DYNAMIC. */ void createVertexBuffer(std::size_t vertexSize, std::size_t capacity, BufferUsage usage); - void createInstanceBuffer(std::size_t vertexSize, std::size_t capacity, BufferUsage usage); + void createInstanceBuffer(std::size_t vertexSize, int capacity, BufferUsage usage); /** Create an index buffer of the custom command. The buffer size is (indexSize * capacity). @@ -139,6 +139,9 @@ TODO: should remove it. @param length Specifies the size in bytes of the data store region being replaced. */ void updateIndexBuffer(const void* data, std::size_t length); + + void updateInstanceBuffer(const 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. @@ -165,6 +168,8 @@ TODO: should remove it. */ inline std::size_t getIndexCapacity() const { return _indexCapacity; } + int getInstanceCapacity() const { return _instanceCapacity; } + inline void setDrawType(DrawType drawType) { _drawType = drawType; } inline DrawType getDrawType() const { return _drawType; } @@ -207,6 +212,8 @@ TODO: should remove it. _indexDrawCount = count; } + void setInstanceDrawInfo(int count) { _instanceCount = count; } + inline std::size_t getIndexDrawOffset() const { return _indexDrawOffset; } inline std::size_t getIndexDrawCount() const { return _indexDrawCount; } @@ -239,7 +246,8 @@ TODO: should remove it. backend::Buffer* _indexBuffer = nullptr; backend::Buffer* _instanceBuffer = nullptr; - int _instanceCount = 0; + int _instanceCount = 0; + int _instanceCapacity = 0; std::size_t _vertexDrawStart = 0; std::size_t _vertexDrawCount = 0; @@ -262,7 +270,7 @@ TODO: should remove it. CallBackFunc _afterCallback = nullptr; }; -} +} // namespace ax /** end of support group @} diff --git a/extensions/spine/src/spine/SkeletonRenderer.cpp b/extensions/spine/src/spine/SkeletonRenderer.cpp index e4fd0b16b5d..c74d627914e 100644 --- a/extensions/spine/src/spine/SkeletonRenderer.cpp +++ b/extensions/spine/src/spine/SkeletonRenderer.cpp @@ -44,7 +44,6 @@ namespace spine { BlendFunc makeBlendFunc(BlendMode blendMode, bool premultipliedAlpha); void transformWorldVertices(float *dstCoord, int coordCount, Skeleton &skeleton, int startSlotIndex, int endSlotIndex); bool cullRectangle(Renderer *renderer, const Mat4 &transform, const axmol::Rect &rect); - Color4B ColorToColor4B(const Color &color); bool slotIsOutRange(Slot &slot, int startSlotIndex, int endSlotIndex); bool nothingToDraw(Slot &slot, int startSlotIndex, int endSlotIndex); }// namespace @@ -291,7 +290,7 @@ namespace spine { texCoords.u = attachment->getUVs()[i]; texCoords.v = attachment->getUVs()[i + 1]; } - dstStride = sizeof(V3F_C4B_T2F) / sizeof(float); + dstStride = sizeof(V3F_C4F_T2F) / sizeof(float); dstTriangleVertices = reinterpret_cast(triangles.verts); } else { trianglesTwoColor.indices = quadIndices; @@ -330,7 +329,7 @@ namespace spine { texCoords.v = attachment->getUVs()[i + 1]; } dstTriangleVertices = (float *) triangles.verts; - dstStride = sizeof(V3F_C4B_T2F) / sizeof(float); + dstStride = sizeof(V3F_C4F_T2F) / sizeof(float); dstVertexCount = triangles.vertCount; } else { trianglesTwoColor.indices = attachment->getTriangles().buffer(); @@ -385,14 +384,14 @@ namespace spine { color.b *= color.a; } - const axmol::Color4B color4B = ColorToColor4B(color); - const axmol::Color4B darkColor4B = ColorToColor4B(darkColor); + const axmol::Color color_r{color}; + const axmol::Color darkColor_r{darkColor}; const BlendFunc blendFunc = makeBlendFunc(slot->getData().getBlendMode(), texture->hasPremultipliedAlpha()); _blendFunc = blendFunc; if (hasSingleTint) { if (_clipper->isClipping()) { - _clipper->clipTriangles((float *) &triangles.verts[0].vertices, triangles.indices, triangles.indexCount, (float *) &triangles.verts[0].texCoords, sizeof(axmol::V3F_C4B_T2F) / 4); + _clipper->clipTriangles((float *) &triangles.verts[0].vertices, triangles.indices, triangles.indexCount, (float *) &triangles.verts[0].texCoords, sizeof(axmol::V3F_C4F_T2F) / 4); batch->deallocateVertices(triangles.vertCount); if (_clipper->getClippedTriangles().size() == 0) { @@ -408,7 +407,7 @@ namespace spine { const float* verts = _clipper->getClippedVertices().buffer(); const float* uvs = _clipper->getClippedUVs().buffer(); - V3F_C4B_T2F* vertex = triangles.verts; + V3F_C4F_T2F* vertex = triangles.verts; for (int v = 0, vn = triangles.vertCount, vv = 0; v < vn; ++v, vv += 2, ++vertex) { @@ -416,16 +415,16 @@ namespace spine { vertex->vertices.y = verts[vv + 1]; vertex->texCoords.u = uvs[vv]; vertex->texCoords.v = uvs[vv + 1]; - vertex->colors = color4B; + vertex->colors = color_r; } batch->addCommand(renderer, _globalZOrder, texture, _programState, blendFunc, triangles, transform, transformFlags); } else { // Not clipping. - V3F_C4B_T2F* vertex = triangles.verts; + V3F_C4F_T2F* vertex = triangles.verts; for (int v = 0, vn = triangles.vertCount; v < vn; ++v, ++vertex) { - vertex->colors = color4B; + vertex->colors = color_r; } batch->addCommand(renderer, _globalZOrder, texture, _programState, blendFunc, triangles, transform, transformFlags); } @@ -458,8 +457,8 @@ namespace spine { vertex->position.y = verts[vv + 1]; vertex->texCoords.u = uvs[vv]; vertex->texCoords.v = uvs[vv + 1]; - vertex->color = color4B; - vertex->color2 = darkColor4B; + vertex->color = color_r; + vertex->color2 = darkColor_r; } lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, texture, _programState, blendFunc, trianglesTwoColor, transform, transformFlags); } else { @@ -467,8 +466,8 @@ namespace spine { for (int v = 0, vn = trianglesTwoColor.vertCount; v < vn; ++v, ++vertex) { - vertex->color = color4B; - vertex->color2 = darkColor4B; + vertex->color = color_r; + vertex->color2 = darkColor_r; } lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, texture, _programState, blendFunc, trianglesTwoColor, transform, transformFlags); } @@ -539,13 +538,13 @@ namespace spine { {brect.origin.x + brect.size.width, brect.origin.y}, {brect.origin.x + brect.size.width, brect.origin.y + brect.size.height}, {brect.origin.x, brect.origin.y + brect.size.height}}; - drawNode->drawPoly(points, 4, true, Color4B::GREEN, 2.0f); + drawNode->drawPoly(points, 4, true, ax::Color::GREEN, 2.0f); } if (_debugSlots) { // Slots. // DrawPrimitives::setDrawColor4B(0, 0, 255, 255); - V3F_C4B_T2F_Quad quad; + V3F_C4F_T2F_Quad quad; for (int i = 0, n = (int)_skeleton->getSlots().size(); i < n; i++) { Slot *slot = _skeleton->getDrawOrder()[i]; @@ -565,7 +564,7 @@ namespace spine { {worldVertices[2], worldVertices[3]}, {worldVertices[4], worldVertices[5]}, {worldVertices[6], worldVertices[7]}}; - drawNode->drawPoly(points, 4, true, Color4B::BLUE, 2.0f); + drawNode->drawPoly(points, 4, true, ax::Color::BLUE, 2.0f); } } @@ -576,15 +575,15 @@ namespace spine { if (!bone->isActive()) continue; float x = bone->getData().getLength() * bone->getA() + bone->getWorldX(); float y = bone->getData().getLength() * bone->getC() + bone->getWorldY(); - drawNode->drawLine(Vec2(bone->getWorldX(), bone->getWorldY()), Vec2(x, y), Color4B::RED, 2.0f); + drawNode->drawLine(Vec2(bone->getWorldX(), bone->getWorldY()), Vec2(x, y), ax::Color::RED, 2.0f); } // Bone origins. - auto color = Color4B::BLUE;// Root bone is blue. + auto color = ax::Color::BLUE; // Root bone is blue. for (int i = 0, n = (int)_skeleton->getBones().size(); i < n; i++) { Bone *bone = _skeleton->getBones()[i]; if (!bone->isActive()) continue; drawNode->drawPoint(Vec2(bone->getWorldX(), bone->getWorldY()), 4, color); - if (i == 0) color = Color4B::GREEN; + if (i == 0) color = ax::Color::GREEN; } } @@ -607,7 +606,7 @@ namespace spine { worldCoord + (idx0 * 2), worldCoord + (idx1 * 2), worldCoord + (idx2 * 2)}; - drawNode->drawPoly(v, 3, true, Color4B::YELLOW, 2.0f); + drawNode->drawPoly(v, 3, true, ax::Color::YELLOW, 2.0f); } VLA_FREE(worldCoord); } @@ -916,10 +915,6 @@ namespace spine { return !visibleRect.containsPoint(v2p); } - - Color4B ColorToColor4B(const Color &color) { - return {(uint8_t) (color.r * 255.f), (uint8_t) (color.g * 255.f), (uint8_t) (color.b * 255.f), (uint8_t) (color.a * 255.f)}; - } }// namespace }// namespace spine diff --git a/tests/cpp-tests/Source/Box2DTestBed/Box2DTestDebugDrawNode.cpp b/tests/cpp-tests/Source/Box2DTestBed/Box2DTestDebugDrawNode.cpp index ca92060babe..4beaa32e3fb 100644 --- a/tests/cpp-tests/Source/Box2DTestBed/Box2DTestDebugDrawNode.cpp +++ b/tests/cpp-tests/Source/Box2DTestBed/Box2DTestDebugDrawNode.cpp @@ -7,14 +7,6 @@ Box2DTestDebugDrawNode* g_pDebugDrawNode; GLFWwindow* g_mainWindow; b2SampleCamera g_camera; -static Color4F toColor4F(b2HexColor color) -{ - unsigned int r = ((unsigned int)color >> 16) & 0xff; - unsigned int g = ((unsigned int)color >> 8) & 0xff; - unsigned int b = ((unsigned int)color) & 0xff; - return Color4F{r / 255.f, g / 255.f, b / 255.f, 1.0f}; -} - static Vec2 toVec2(const b2Vec2& v) { return Vec2{v.x, v.y}; @@ -54,7 +46,8 @@ static void b2DrawCircle(b2Vec2 center, float radius, b2HexColor color, Box2DTes { auto ratio = context->getPTMRatio(); auto offset = context->getWorldOffset(); - context->AddCircle(CircleData{b2Vec2{center.x * ratio + offset.x, center.y * ratio + offset.y}, radius * ratio, toColor4F(color)}); + context->AddCircle(CircleData{b2Vec2{center.x * ratio + offset.x, center.y * ratio + offset.y}, radius * ratio, + Color::fromHex(color)}); } static void b2DrawSolidCircle(b2Transform t, float radius, b2HexColor color, Box2DTestDebugDrawNode* context) @@ -64,7 +57,7 @@ static void b2DrawSolidCircle(b2Transform t, float radius, b2HexColor color, Box auto ratio = context->getPTMRatio(); auto offset = context->getWorldOffset(); context->AddCircle( - {{t.p.x * ratio + offset.x, t.p.y * ratio + offset.y, t.q.c, t.q.s}, radius * ratio, toColor4F(color)}); + {{t.p.x * ratio + offset.x, t.p.y * ratio + offset.y, t.q.c, t.q.s}, radius * ratio, Color::fromHex(color)}); } static void b2DrawSolidCapsule(b2Vec2 pt1, b2Vec2 pt2, float radius, b2HexColor c, Box2DTestDebugDrawNode* context) @@ -87,7 +80,7 @@ static void b2DrawSolidCapsule(b2Vec2 pt1, b2Vec2 pt2, float radius, b2HexColor transform.q.c = axis.x; transform.q.s = axis.y; - ax::Color4F rgba = toColor4F(c); + ax::Color4F rgba = Color::fromHex(c); context->AddCapsule({{transform.p.x + offset.x, transform.p.y + offset.y, transform.q.c, transform.q.s}, radius * ratio, @@ -122,10 +115,9 @@ bool Box2DTestDebugDrawNode::initWithWorld(b2WorldId worldId) AX_SAFE_RELEASE(pipelinePS); // vertex attributes - auto program = - backend::ProgramManager::getInstance()->loadProgram("custom/circle_vs", "custom/circle_fs"); - pipelinePS = new backend::ProgramState(program); - auto vfmt = pipelinePS->getMutableVertexLayout(); + auto program = backend::ProgramManager::getInstance()->loadProgram("custom/circle_vs", "custom/circle_fs"); + pipelinePS = new backend::ProgramState(program); + auto vfmt = pipelinePS->getMutableVertexLayout(); vfmt->setAttrib("a_localPosition", program->getAttributeLocation("a_localPosition"), backend::VertexFormat::FLOAT2, 0, false); vfmt->setStride(sizeof(Vec2)); @@ -321,10 +313,11 @@ void Box2DTestDebugDrawNode::draw(Renderer* renderer, const Mat4& transform, uin _customCommandCircle.setInstanceBuffer(nullptr, 0); else { - _customCommandCircle.createInstanceBuffer(sizeof(CircleData), _circles.size(), - CustomCommand::BufferUsage::DYNAMIC); - auto instanceBuffer = _customCommandCircle.getInstanceBuffer(); - instanceBuffer->updateData(_circles.data(), _circles.size() * sizeof(CircleData)); + if (_customCommandCircle.getInstanceCapacity() < static_cast(_circles.size())) + _customCommandCircle.createInstanceBuffer(sizeof(CircleData), _circles.size(), + CustomCommand::BufferUsage::DYNAMIC); + _customCommandCircle.updateInstanceBuffer(_circles.data(), _circles.size() * sizeof(CircleData)); + _customCommandCircle.setInstanceDrawInfo(static_cast(_circles.size())); } } @@ -342,10 +335,12 @@ void Box2DTestDebugDrawNode::draw(Renderer* renderer, const Mat4& transform, uin _customCommandSolidCircle.setInstanceBuffer(nullptr, 0); else { - _customCommandSolidCircle.createInstanceBuffer(sizeof(SolidCircleData), _solidCircles.size(), - CustomCommand::BufferUsage::DYNAMIC); - auto instanceBuffer = _customCommandSolidCircle.getInstanceBuffer(); - instanceBuffer->updateData(_solidCircles.data(), _solidCircles.size() * sizeof(SolidCircleData)); + if (_customCommandSolidCircle.getInstanceCapacity() < static_cast(_solidCircles.size())) + _customCommandSolidCircle.createInstanceBuffer(sizeof(SolidCircleData), _solidCircles.size(), + CustomCommand::BufferUsage::DYNAMIC); + _customCommandSolidCircle.updateInstanceBuffer(_solidCircles.data(), + _solidCircles.size() * sizeof(SolidCircleData)); + _customCommandSolidCircle.setInstanceDrawInfo(static_cast(_solidCircles.size())); } } @@ -363,10 +358,11 @@ void Box2DTestDebugDrawNode::draw(Renderer* renderer, const Mat4& transform, uin _customCommandCapsule.setInstanceBuffer(nullptr, 0); else { - _customCommandCapsule.createInstanceBuffer(sizeof(CapsuleData), _capsules.size(), - CustomCommand::BufferUsage::DYNAMIC); - auto instanceBuffer = _customCommandCapsule.getInstanceBuffer(); - instanceBuffer->updateData(_capsules.data(), _capsules.size() * sizeof(CapsuleData)); + if (_customCommandCapsule.getInstanceCapacity() < static_cast(_capsules.size())) + _customCommandCapsule.createInstanceBuffer(sizeof(CapsuleData), _capsules.size(), + CustomCommand::BufferUsage::DYNAMIC); + _customCommandCapsule.updateInstanceBuffer(_capsules.data(), _capsules.size() * sizeof(CapsuleData)); + _customCommandCapsule.setInstanceDrawInfo(static_cast(_capsules.size())); } } @@ -419,5 +415,5 @@ void Box2DTestDebugDrawNode::clear() void Box2DTestDebugDrawNode::DrawAABB(b2AABB aabb, b2HexColor color) { this->drawRect(Vec2{aabb.lowerBound.x, aabb.lowerBound.y}, Vec2{aabb.upperBound.x, aabb.upperBound.y}, - toColor4F(color)); + Color::fromHex(color)); }