宝爷Debug小记——Cocos2d-x(3.13之前的版本)底层BUG导致Spine渲染花屏
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了宝爷Debug小记——Cocos2d-x(3.13之前的版本)底层BUG导致Spine渲染花屏,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含10320字,纯文字阅读大概需要15分钟。
内容图文
![宝爷Debug小记——Cocos2d-x(3.13之前的版本)底层BUG导致Spine渲染花屏](/upload/InfoBanner/zyjiaocheng/1256/ee824647f15942fcaf5e737dec635f6f.jpg)
最近在工作中碰到不少棘手的BUG,其中的一个是Spine骨骼的渲染花屏,在战斗中派发出大量士兵之后有概率出现花屏闪烁(如下图所示),这种莫名奇妙且难以重现的BUG最为蛋疼。
![技术分享](/upload/getfiles/default/2022/11/8/20221108095301669.jpg)
![技术分享](/upload/getfiles/default/2022/11/8/20221108095301669.jpg)
![技术分享](/upload/getfiles/default/2022/11/8/20221108095302684.jpg)
1 static int skiptype = 0; 2 3 void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) { 4 SkeletonBatch* batch = SkeletonBatch::getInstance(); 5 6for (auto t : _curTriangles) 7 { 8 TrianglesMgr::getInstance()->freeTriangles(t); 9 } 10 _curTriangles.clear(); 11 _triCmds.clear(); 1213 Color3B nodeColor = getColor(); 14 _skeleton->r = nodeColor.r / (float)255; 15 _skeleton->g = nodeColor.g / (float)255; 16 _skeleton->b = nodeColor.b / (float)255; 17 _skeleton->a = getDisplayedOpacity() / (float)255; 1819 Color4F color; 20 AttachmentVertices* attachmentVertices = nullptr; 21for (int i = 0, n = _skeleton->slotsCount; i < n; ++i) { 22 spSlot* slot = _skeleton->drawOrder[i]; 23if (!slot->attachment) continue; 24if (slot->attachment->type == skiptype) continue; 2526switch (slot->attachment->type) { 27case SP_ATTACHMENT_REGION: { 28 spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment; 29 spRegionAttachment_computeWorldVertices(attachment, slot->bone, _worldVertices); 30 attachmentVertices = getAttachmentVertices(attachment); 31 color.r = attachment->r; 32 color.g = attachment->g; 33 color.b = attachment->b; 34 color.a = attachment->a; 35break; 36 } 37case SP_ATTACHMENT_MESH: { 38 spMeshAttachment* attachment = (spMeshAttachment*)slot->attachment; 39 spMeshAttachment_computeWorldVertices(attachment, slot, _worldVertices); 40 attachmentVertices = getAttachmentVertices(attachment); 41 color.r = attachment->r; 42 color.g = attachment->g; 43 color.b = attachment->b; 44 color.a = attachment->a; 45break; 46 } 47default:
![技术分享](http://image.bubuko.com/info/201705/20180110231328635327.png)
![技术分享](http://image.bubuko.com/info/201705/20180110231328649976.png)
![技术分享](file:///C:/Users/WangYongBao/AppData/Local/YNote/Data/wyb10a10@163.com/e49f0b0cbb954819ab3913f71e625c38/clipboard.png)
![技术分享](file:///C:/Users/WangYongBao/AppData/Local/YNote/Data/wyb10a10@163.com/b821e26fc0424966a44533792a5d4cc4/%60gf5daym_~bi.png)
1 void Renderer::processRenderCommand(RenderCommand* command) 2{ 3 auto commandType = command->getType(); 4if( RenderCommand::Type::TRIANGLES_COMMAND == commandType) 5 { 6// flush other queues 7 flush3D(); 8 9 auto cmd = static_cast<TrianglesCommand*>(command); 1011// flush own queue when buffer is full12if(_filledVertex + cmd->getVertexCount() > VBO_SIZE || _filledIndex + cmd->getIndexCount() > INDEX_VBO_SIZE) 13 { 14 CCASSERT(cmd->getVertexCount()>= 0 && cmd->getVertexCount() < VBO_SIZE, "VBO for vertex is not big enough, please break the data down or use customized render command"); 15 CCASSERT(cmd->getIndexCount()>= 0 && cmd->getIndexCount() < INDEX_VBO_SIZE, "VBO for index is not big enough, please break the data down or use customized render command"); 16 drawBatchedTriangles(); 17 } 1819// queue it20 _queuedTriangleCommands.push_back(cmd); 21 _filledIndex += cmd->getIndexCount(); 22 _filledVertex += cmd->getVertexCount(); 23 } 24
1 // 增加一些调试用的静态变量 2 static bool __dbg = false; 3staticbool __deepDbg = false; 4staticint __cmdCount = 68; 5staticint __curCmdCount = 0; 6staticint __idxCount = 0; 7staticint __vexCount = 0; 8staticint __maxidx = 0; 9 10 void Renderer::fillVerticesAndIndices(const TrianglesCommand* cmd) 11{ 12 memcpy(&_verts[_filledVertex], cmd->getVertices(), sizeof(V3F_C4B_T2F) * cmd->getVertexCount()); 13 14// fill vertex, and convert them to world coordinates 15const Mat4& modelView = cmd->getModelView(); 16for(ssize_t i=0; i < cmd->getVertexCount(); ++i) 17 { 18 modelView.transformPoint(&(_verts[i + _filledVertex].vertices)); 19// 打印所有顶点的xyz和纹理uv 20if(__dbg && __deepDbg) 21 { 22 CCLOG("vertex %d is xyz %.2f,%.2f,%.2f uv %.2f,%.2f", i + _filledVertex - __vexCount,_verts[i + _filledVertex].vertices.x, 23 _verts[i + _filledVertex].vertices.y, _verts[i + _filledVertex].vertices.z, 24 _verts[i + _filledVertex].texCoords.u, _verts[i + _filledVertex].texCoords.v); 25 } 26 } 27 28// fill index 29const unsigned short* indices = cmd->getIndices(); 30for(ssize_t i=0; i< cmd->getIndexCount(); ++i) 31 { 32 _indices[_filledIndex + i] = _filledVertex + indices[i]; 33if (__dbg) 34 { 35if (__maxidx < _indices[_filledIndex + i]) 36 { 37 __maxidx = _indices[_filledIndex + i]; 38 } 39if (__deepDbg) 40 { 41 CCLOG("index %d is %d", _filledIndex + i - __idxCount, _indices[_filledIndex + i] - __vexCount); 42 } 43 } 44 } 45 46 _filledVertex += cmd->getVertexCount(); 47 _filledIndex += cmd->getIndexCount(); 48} 49 50void Renderer::drawBatchedTriangles() 51{ 52if(_queuedTriangleCommands.empty()) 53return; 54 55 CCGL_DEBUG_INSERT_EVENT_MARKER("RENDERER_BATCH_TRIANGLES"); 56 57if (__dbg) 58 { 59 __vexCount = 0; 60 __idxCount = 0; 61 __curCmdCount = 0; 62 } 63 64 _filledVertex = 0; 65 _filledIndex = 0; 66 67/************** 1: Setup up vertices/indices *************/ 68 69 _triBatchesToDraw[0].offset = 0; 70 _triBatchesToDraw[0].indicesToDraw = 0; 71 _triBatchesToDraw[0].cmd = nullptr; 72 73int batchesTotal = 0; 74int prevMaterialID = -1; 75bool firstCommand = true; 76 77for(auto it = std::begin(_queuedTriangleCommands); it != std::end(_queuedTriangleCommands); ++it) 78 { 79const auto& cmd = *it; 80 auto currentMaterialID = cmd->getMaterialID(); 81constbool batchable = !cmd->isSkipBatching(); 82if (__dbg) 83 { 84if (__curCmdCount % __cmdCount == 0) 85 { 86 CCLOG("begin %d =====================================", __curCmdCount / __cmdCount); 87 __vexCount = _filledVertex; 88 __idxCount = _filledIndex; 89 } 90 ++__curCmdCount; 91 } 92 93 fillVerticesAndIndices(cmd); 94 95// in the same batch ? 96if (batchable && (prevMaterialID == currentMaterialID || firstCommand)) 97 { 98 CC_ASSERT(firstCommand || _triBatchesToDraw[batchesTotal].cmd->getMaterialID() == cmd->getMaterialID() && "argh... error in logic"); 99 _triBatchesToDraw[batchesTotal].indicesToDraw += cmd->getIndexCount(); 100 _triBatchesToDraw[batchesTotal].cmd = cmd; 101 } 102else103 { 104// is this the first one?105if (!firstCommand) { 106 batchesTotal++; 107 _triBatchesToDraw[batchesTotal].offset = _triBatchesToDraw[batchesTotal-1].offset + _triBatchesToDraw[batchesTotal-1].indicesToDraw; 108 } 109110 _triBatchesToDraw[batchesTotal].cmd = cmd; 111 _triBatchesToDraw[batchesTotal].indicesToDraw = (int) cmd->getIndexCount(); 112113// is this a single batch ? Prevent creating a batch group then114if (!batchable) 115 currentMaterialID = -1; 116 } 117118// capacity full ?119if (batchesTotal + 1 >= _triBatchesToDrawCapacity) { 120 _triBatchesToDrawCapacity *= 1.4; 121 _triBatchesToDraw = (TriBatchToDraw*) realloc(_triBatchesToDraw, sizeof(_triBatchesToDraw[0]) * _triBatchesToDrawCapacity); 122 } 123124 prevMaterialID = currentMaterialID; 125 firstCommand = false; 126 } 127 batchesTotal++; 128if (__dbg) 129 { 130 CCLOG("MAX IDX %d", __maxidx); 131 } 132 __dbg = false; 133
1 for (int i=0; i<batchesTotal; ++i) 2 { 3 CC_ASSERT(_triBatchesToDraw[i].cmd && "Invalid batch"); 4 _triBatchesToDraw[i].cmd->useMaterial(); 5 glDrawElements(GL_TRIANGLES, (GLsizei) _triBatchesToDraw[i].indicesToDraw, GL_UNSIGNED_SHORT, (GLvoid*) (_triBatchesToDraw[i].offset*sizeof(_indices[0])) ); 6 _drawnBatches++; 7 _drawnVertices += _triBatchesToDraw[i].indicesToDraw; 8 }
原文:http://www.cnblogs.com/ybgame/p/6820473.html
内容总结
以上是互联网集市为您收集整理的宝爷Debug小记——Cocos2d-x(3.13之前的版本)底层BUG导致Spine渲染花屏全部内容,希望文章能够帮你解决宝爷Debug小记——Cocos2d-x(3.13之前的版本)底层BUG导致Spine渲染花屏所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。