Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.4.4] fix: do not update vertexbuffer with empty array #2884

Merged
merged 3 commits into from Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions cocos/renderer/gfx/IndexBuffer.cpp
Expand Up @@ -86,9 +86,12 @@ void IndexBuffer::update(uint32_t offset, const void* data, size_t dataByteLengt
return;
}

if (dataByteLength == 0) return;

if (data && dataByteLength + offset > _bytes)
if (data == nullptr || dataByteLength == 0)
{
return;
}

if (dataByteLength + offset > _bytes)
{
if (offset) {
RENDERER_LOGE("Failed to update index buffer data, bytes exceed.");
Expand Down
4 changes: 0 additions & 4 deletions cocos/renderer/gfx/IndexBuffer.h
Expand Up @@ -117,10 +117,6 @@ class IndexBuffer final : public GraphicsHandle
* Gets byte size of the index buffer
*/
inline uint32_t getBytes() const { return _bytes; }
/**
* Sets byte size of the index buffer
*/
inline void setBytes(uint32_t bytes) { _bytes = bytes; }

using FetchDataCallback = std::function<uint8_t*(size_t*)>;
void setFetchDataCallback(const FetchDataCallback& cb) { _fetchDataCallback = cb; }
Expand Down
7 changes: 6 additions & 1 deletion cocos/renderer/gfx/VertexBuffer.cpp
Expand Up @@ -76,13 +76,18 @@ void VertexBuffer::setFormat(VertexFormat* format)

void VertexBuffer::update(uint32_t offset, const void* data, size_t dataByteLength)
{
if(data == nullptr || dataByteLength == 0)
{
return;
}

if (_glID == 0)
{
RENDERER_LOGE("The buffer is destroyed");
return;
}

if (data && dataByteLength + offset > _bytes)
if (dataByteLength + offset > _bytes)
{
if (offset) {
RENDERER_LOGE("Failed to update index buffer data, bytes exceed.");
Expand Down
4 changes: 0 additions & 4 deletions cocos/renderer/gfx/VertexBuffer.h
Expand Up @@ -110,10 +110,6 @@ class VertexBuffer final : public GraphicsHandle
* Gets byte size of the vertex buffer
*/
inline uint32_t getBytes() const { return _bytes; }
/**
* Sets byte size of the vertex buffer
*/
inline void setBytes(uint32_t bytes) { _bytes = bytes; }

using FetchDataCallback = std::function<uint8_t*(size_t*)>;
void setFetchDataCallback(const FetchDataCallback& cb) { _fetchDataCallback = cb; }
Expand Down
2 changes: 0 additions & 2 deletions cocos/renderer/scene/MeshBuffer.cpp
Expand Up @@ -179,11 +179,9 @@ void MeshBuffer::switchBuffer(uint32_t vertexCount)
{
DeviceGraphics* device = _batcher->getFlow()->getDevice();
_vb = VertexBuffer::create(device, _vertexFmt, Usage::DYNAMIC, nullptr, 0, 0);
_vb->setBytes(_vDataCount * VDATA_BYTE);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

导致 _bytes 和 实际的 buffer 大小不一致

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改动是为了规避iOS的crash是吗?但是这么不setBytes的话,会导致graphics画着画着画不下去了吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不会导致画不下去.
_bytes 用于记录 glbuffer 的大小, 不能单独改变.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

将此合并到 2.3.4 版本,绘制时间长之后仍然会有 js exception,导致画不下去

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

什么异常?

Copy link

@potato47 potato47 Oct 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我提了一个issue,里面有复现无法画图问题的项目#2930 ,麻烦看下

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PatriceJiang potato47提的issue麻烦看看了,native 下的确有超出顶点数后画不下去的问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wzpan 我这里验证 #2930 中的问题已经在这个 commit 中解决. 如果还有其他问题, 麻烦提供下 demo.

_vbArr.pushBack(_vb);

_ib = IndexBuffer::create(device, IndexFormat::UINT16, Usage::STATIC, nullptr, 0, 0);
_ib->setBytes(_iDataCount * IDATA_BYTE);
_ibArr.pushBack(_ib);
}
}
Expand Down
44 changes: 2 additions & 42 deletions cocos/scripting/js-bindings/manual/jsb_gfx_manual.cpp
Expand Up @@ -428,26 +428,6 @@ static bool js_gfx_VertexBuffer_prop_getNumVertices(se::State& s)
}
SE_BIND_PROP_GET(js_gfx_VertexBuffer_prop_getNumVertices)

static bool js_gfx_VertexBuffer_prop_setBytes(se::State& s)
{
cocos2d::renderer::VertexBuffer* cobj = (cocos2d::renderer::VertexBuffer*)s.nativeThisObject();
SE_PRECONDITION2(cobj, false, "js_gfx_VertexBuffer_prop_setBytes : Invalid Native Object");
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc == 1) {
uint32_t count = 0;
ok = seval_to_uint32(args[0], &count);
SE_PRECONDITION2(ok, false, "Convert arg0 offset failed!");
cobj->setBytes(count);
return true;
}

SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
return false;
}
SE_BIND_PROP_SET(js_gfx_VertexBuffer_prop_setBytes)

static bool js_gfx_VertexBuffer_prop_getBytes(se::State& s)
{
cocos2d::renderer::VertexBuffer* cobj = (cocos2d::renderer::VertexBuffer*)s.nativeThisObject();
Expand Down Expand Up @@ -728,26 +708,6 @@ static bool js_gfx_IndexBuffer_prop_getBytesPerIndex(se::State& s)
}
SE_BIND_PROP_GET(js_gfx_IndexBuffer_prop_getBytesPerIndex)

static bool js_gfx_IndexBuffer_prop_setBytes(se::State& s)
{
cocos2d::renderer::IndexBuffer* cobj = (cocos2d::renderer::IndexBuffer*)s.nativeThisObject();
SE_PRECONDITION2(cobj, false, "js_gfx_IndexBuffer_prop_setBytes : Invalid Native Object");
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc == 1) {
uint32_t count = 0;
ok = seval_to_uint32(args[0], &count);
SE_PRECONDITION2(ok, false, "Convert arg0 offset failed!");
cobj->setBytes(count);
return true;
}

SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
return false;
}
SE_BIND_PROP_SET(js_gfx_IndexBuffer_prop_setBytes)

static bool js_gfx_IndexBuffer_prop_getBytes(se::State& s)
{
cocos2d::renderer::IndexBuffer* cobj = (cocos2d::renderer::IndexBuffer*)s.nativeThisObject();
Expand Down Expand Up @@ -904,7 +864,7 @@ bool jsb_register_gfx_manual(se::Object* global)
__jsb_cocos2d_renderer_VertexBuffer_proto->defineFunction("update", _SE(js_gfx_VertexBuffer_update));
__jsb_cocos2d_renderer_VertexBuffer_proto->defineProperty("_format", nullptr, _SE(js_gfx_VertexBuffer_prop_setFormat));
__jsb_cocos2d_renderer_VertexBuffer_proto->defineProperty("_usage", _SE(js_gfx_VertexBuffer_prop_getUsage), _SE(js_gfx_VertexBuffer_prop_setUsage));
__jsb_cocos2d_renderer_VertexBuffer_proto->defineProperty("_bytes", _SE(js_gfx_VertexBuffer_prop_getBytes), _SE(js_gfx_VertexBuffer_prop_setBytes));
__jsb_cocos2d_renderer_VertexBuffer_proto->defineProperty("_bytes", _SE(js_gfx_VertexBuffer_prop_getBytes), nullptr);
__jsb_cocos2d_renderer_VertexBuffer_proto->defineProperty("_numVertices", _SE(js_gfx_VertexBuffer_prop_getNumVertices), _SE(js_gfx_VertexBuffer_prop_setNumVertices));
__jsb_cocos2d_renderer_VertexBuffer_proto->defineFunction("self", _SE(js_gfx_VertexBuffer_self));

Expand All @@ -913,7 +873,7 @@ bool jsb_register_gfx_manual(se::Object* global)
__jsb_cocos2d_renderer_IndexBuffer_proto->defineProperty("_format", _SE(js_gfx_IndexBuffer_prop_getFormat), _SE(js_gfx_IndexBuffer_prop_setFormat));
__jsb_cocos2d_renderer_IndexBuffer_proto->defineProperty("_usage", _SE(js_gfx_IndexBuffer_prop_getUsage), _SE(js_gfx_IndexBuffer_prop_setUsage));
__jsb_cocos2d_renderer_IndexBuffer_proto->defineProperty("_bytesPerIndex", _SE(js_gfx_IndexBuffer_prop_getBytesPerIndex), _SE(js_gfx_IndexBuffer_prop_setBytesPerIndex));
__jsb_cocos2d_renderer_IndexBuffer_proto->defineProperty("_bytes", _SE(js_gfx_IndexBuffer_prop_getBytes), _SE(js_gfx_IndexBuffer_prop_setBytes));
__jsb_cocos2d_renderer_IndexBuffer_proto->defineProperty("_bytes", _SE(js_gfx_IndexBuffer_prop_getBytes), nullptr);
__jsb_cocos2d_renderer_IndexBuffer_proto->defineProperty("_numIndices", _SE(js_gfx_IndexBuffer_prop_getNumIndices), _SE(js_gfx_IndexBuffer_prop_setNumIndices));
__jsb_cocos2d_renderer_IndexBuffer_proto->defineFunction("self", _SE(js_gfx_IndexBuffer_self));

Expand Down