From 9a56e6923fb1b0f67e98816a117de0d21c583410 Mon Sep 17 00:00:00 2001 From: Arnold <40414978+PatriceJiang@users.noreply.github.com> Date: Wed, 3 Apr 2019 17:27:15 +0800 Subject: [PATCH] [bugfix] motionstreak & DrawNode3D on GL (#19564) --- cocos/2d/CCMotionStreak.cpp | 9 ++++++++- cocos/renderer/backend/opengl/BufferGL.cpp | 17 +++++------------ cocos/renderer/backend/opengl/BufferGL.h | 2 +- .../Classes/Sprite3DTest/DrawNode3D.cpp | 13 +++++++++++-- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cocos/2d/CCMotionStreak.cpp b/cocos/2d/CCMotionStreak.cpp index 3fec359f50..3f6074cfa3 100644 --- a/cocos/2d/CCMotionStreak.cpp +++ b/cocos/2d/CCMotionStreak.cpp @@ -133,11 +133,18 @@ bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Co _pointState = (float *)malloc(sizeof(float) * _maxPoints); _pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints); + const size_t VERTEX_SIZE = sizeof(Vec2) + sizeof(Tex2F) + sizeof(uint8_t) * 4; + _vertexCount = _maxPoints * 2; _vertices = (Vec2*)malloc(sizeof(Vec2) * _vertexCount); _texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _vertexCount); _colorPointer = (uint8_t*)malloc(sizeof(uint8_t) * 4 * _vertexCount); - _customCommand.createVertexBuffer(sizeof(Vec2) + sizeof(Tex2F) + sizeof(uint8_t) * 4, _vertexCount, CustomCommand::BufferUsage::DYNAMIC); + _customCommand.createVertexBuffer(VERTEX_SIZE, _vertexCount, CustomCommand::BufferUsage::DYNAMIC); + + std::vector zeros; + zeros.resize(VERTEX_SIZE * _vertexCount); + std::fill(zeros.begin(), zeros.end(), 0); + _customCommand.updateVertexBuffer(zeros.data(), zeros.size()); setTexture(texture); setColor(color); diff --git a/cocos/renderer/backend/opengl/BufferGL.cpp b/cocos/renderer/backend/opengl/BufferGL.cpp index d559474a45..d2faf13b6f 100644 --- a/cocos/renderer/backend/opengl/BufferGL.cpp +++ b/cocos/renderer/backend/opengl/BufferGL.cpp @@ -17,7 +17,7 @@ BufferGL::~BufferGL() void BufferGL::updateData(void* data, unsigned int size) { - assert(size); + assert(size && size <= _size); if (_buffer) { @@ -32,22 +32,16 @@ void BufferGL::updateData(void* data, unsigned int size) glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); } CHECK_GL_ERROR_DEBUG(); - _bufferAllocated = true; + _bufferAllocated = size; } } void BufferGL::updateSubData(void* data, unsigned int offset, unsigned int size) { - assert(offset + size <= _size); - - //invoke updateData if buffer is not allocated - if (!_bufferAllocated) - { - CCASSERT(offset == 0, "offset should be zero when allocate buffer"); - updateData(data, size); - return; - } + CCASSERT(_bufferAllocated != 0, "updateData should be invoke before updateSubData"); + CCASSERT(offset + size <= _bufferAllocated, "buffer size overflow"); + if (_buffer) { CHECK_GL_ERROR_DEBUG(); @@ -55,7 +49,6 @@ void BufferGL::updateSubData(void* data, unsigned int offset, unsigned int size) { glBindBuffer(GL_ARRAY_BUFFER, _buffer); glBufferSubData(GL_ARRAY_BUFFER, offset, size, data); - } else { diff --git a/cocos/renderer/backend/opengl/BufferGL.h b/cocos/renderer/backend/opengl/BufferGL.h index 0c0748b7ae..c99e22e907 100644 --- a/cocos/renderer/backend/opengl/BufferGL.h +++ b/cocos/renderer/backend/opengl/BufferGL.h @@ -19,7 +19,7 @@ public: private: GLuint _buffer = 0; - bool _bufferAllocated = false; + unsigned int _bufferAllocated = 0; }; CC_BACKEND_END diff --git a/tests/cpp-tests/Classes/Sprite3DTest/DrawNode3D.cpp b/tests/cpp-tests/Classes/Sprite3DTest/DrawNode3D.cpp index 4fd2b885e5..3991b239fe 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/DrawNode3D.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/DrawNode3D.cpp @@ -24,7 +24,7 @@ ****************************************************************************/ #include "DrawNode3D.h" - +#include "renderer/backend/Buffer.h" NS_CC_BEGIN @@ -58,7 +58,15 @@ void DrawNode3D::ensureCapacity(int count) { CCASSERT(count>=0, "capacity must be >= 0"); - _bufferLines.reserve(_bufferLines.size() + count); + const int EXTENDED_SIZE = _bufferLines.size() + count; + + _bufferLines.reserve(EXTENDED_SIZE); + + if (!_customCommand.getVertexBuffer() || _customCommand.getVertexBuffer()->getSize() < (EXTENDED_SIZE * sizeof(_bufferLines[0]))) + { + _customCommand.createVertexBuffer(sizeof(V3F_C4B), EXTENDED_SIZE + (EXTENDED_SIZE >> 1), CustomCommand::BufferUsage::DYNAMIC); + } + } bool DrawNode3D::init() @@ -120,6 +128,7 @@ void DrawNode3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) if (_isDirty && !_bufferLines.empty()) { _customCommand.updateVertexBuffer(_bufferLines.data(), (unsigned int)(_bufferLines.size() * sizeof(_bufferLines[0]))); + _customCommand.setVertexDrawInfo(0, _bufferLines.size()); _isDirty = false; }