标签:das ota nsf 根据 3.3 打印 处理 index users
最近在工作中碰到不少棘手的BUG,其中的一个是Spine骨骼的渲染花屏,在战斗中派发出大量士兵之后有概率出现花屏闪烁(如下图所示),这种莫名奇妙且难以重现的BUG最为蛋疼。
1 static int skiptype = 0; 2 3 void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) { 4 SkeletonBatch* batch = SkeletonBatch::getInstance(); 5 6 for (auto t : _curTriangles) 7 { 8 TrianglesMgr::getInstance()->freeTriangles(t); 9 } 10 _curTriangles.clear(); 11 _triCmds.clear(); 12 13 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; 18 19 Color4F color; 20 AttachmentVertices* attachmentVertices = nullptr; 21 for (int i = 0, n = _skeleton->slotsCount; i < n; ++i) { 22 spSlot* slot = _skeleton->drawOrder[i]; 23 if (!slot->attachment) continue; 24 if (slot->attachment->type == skiptype) continue; 25 26 switch (slot->attachment->type) { 27 case 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; 35 break; 36 } 37 case 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; 45 break; 46 } 47 default:
1 void Renderer::processRenderCommand(RenderCommand* command) 2 { 3 auto commandType = command->getType(); 4 if( RenderCommand::Type::TRIANGLES_COMMAND == commandType) 5 { 6 // flush other queues 7 flush3D(); 8 9 auto cmd = static_cast<TrianglesCommand*>(command); 10 11 // flush own queue when buffer is full 12 if(_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 } 18 19 // queue it 20 _queuedTriangleCommands.push_back(cmd); 21 _filledIndex += cmd->getIndexCount(); 22 _filledVertex += cmd->getVertexCount(); 23 } 24
1 // 增加一些调试用的静态变量 2 static bool __dbg = false; 3 static bool __deepDbg = false; 4 static int __cmdCount = 68; 5 static int __curCmdCount = 0; 6 static int __idxCount = 0; 7 static int __vexCount = 0; 8 static int __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 15 const Mat4& modelView = cmd->getModelView(); 16 for(ssize_t i=0; i < cmd->getVertexCount(); ++i) 17 { 18 modelView.transformPoint(&(_verts[i + _filledVertex].vertices)); 19 // 打印所有顶点的xyz和纹理uv 20 if(__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 29 const unsigned short* indices = cmd->getIndices(); 30 for(ssize_t i=0; i< cmd->getIndexCount(); ++i) 31 { 32 _indices[_filledIndex + i] = _filledVertex + indices[i]; 33 if (__dbg) 34 { 35 if (__maxidx < _indices[_filledIndex + i]) 36 { 37 __maxidx = _indices[_filledIndex + i]; 38 } 39 if (__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 50 void Renderer::drawBatchedTriangles() 51 { 52 if(_queuedTriangleCommands.empty()) 53 return; 54 55 CCGL_DEBUG_INSERT_EVENT_MARKER("RENDERER_BATCH_TRIANGLES"); 56 57 if (__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 73 int batchesTotal = 0; 74 int prevMaterialID = -1; 75 bool firstCommand = true; 76 77 for(auto it = std::begin(_queuedTriangleCommands); it != std::end(_queuedTriangleCommands); ++it) 78 { 79 const auto& cmd = *it; 80 auto currentMaterialID = cmd->getMaterialID(); 81 const bool batchable = !cmd->isSkipBatching(); 82 if (__dbg) 83 { 84 if (__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 ? 96 if (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 } 102 else 103 { 104 // is this the first one? 105 if (!firstCommand) { 106 batchesTotal++; 107 _triBatchesToDraw[batchesTotal].offset = _triBatchesToDraw[batchesTotal-1].offset + _triBatchesToDraw[batchesTotal-1].indicesToDraw; 108 } 109 110 _triBatchesToDraw[batchesTotal].cmd = cmd; 111 _triBatchesToDraw[batchesTotal].indicesToDraw = (int) cmd->getIndexCount(); 112 113 // is this a single batch ? Prevent creating a batch group then 114 if (!batchable) 115 currentMaterialID = -1; 116 } 117 118 // capacity full ? 119 if (batchesTotal + 1 >= _triBatchesToDrawCapacity) { 120 _triBatchesToDrawCapacity *= 1.4; 121 _triBatchesToDraw = (TriBatchToDraw*) realloc(_triBatchesToDraw, sizeof(_triBatchesToDraw[0]) * _triBatchesToDrawCapacity); 122 } 123 124 prevMaterialID = currentMaterialID; 125 firstCommand = false; 126 } 127 batchesTotal++; 128 if (__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 }
宝爷Debug小记——Cocos2d-x(3.13之前的版本)底层BUG导致Spine渲染花屏
标签:das ota nsf 根据 3.3 打印 处理 index users
原文地址:http://www.cnblogs.com/ybgame/p/6820473.html