cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.22k stars 7.05k forks source link

CommandBufferMTL renderEncoder crash #20453

Open dqkona opened 4 years ago

dqkona commented 4 years ago

Getting EXC_BAD_ACCESS, when trying to access _mtlRenderEncoder on Director::mainLoop() flow

Screenshot 2020-01-31 at 15 38 45

Mee-gu commented 4 years ago

How can i reproduce the crash?

dqkona commented 4 years ago

It crashes on random places, while the Xcode debugger is attached to the app. Disabling the debugger solves the issues, so I'm not sure if is something from Apple's Metal code or your integration with Metal or both.

qoozta commented 4 years ago

Try add a ClippingNode which running stencil animation and use the same image as Sprite to cover it on top, run and wait several minutes, it should be able to reproduce this crash.

PJChow commented 4 years ago

Have you solved the problem

sgosztyla commented 4 years ago

I also have this issue.

ddtme commented 4 years ago

I also have this issue.Have you solved the problem?

Mee-gu commented 4 years ago

Try add a ClippingNode which running stencil animation and use the same image as Sprite to cover it on top, run and wait several minutes, it should be able to reproduce this crash.

Can you provide the demo?

Mee-gu commented 4 years ago

I also have this issue.Have you solved the problem?

Any available demo to reproduce the crash?

losttoken commented 4 years ago

I have the same issue.

zhongfq commented 4 years ago

i have same issue too

losttoken commented 4 years ago

I can avoid this problem in my game. Edit Scheme-->Run-->Options-->Metal API Validation:Disabled. {335355B5-4083-4957-BDCC-65673BF0D08E}

dqkona commented 4 years ago

@losttoken I did that as well. The result was it crashed again, but it took more time in order for crash to happen.

qoozta commented 4 years ago

Try add a ClippingNode which running stencil animation and use the same image as Sprite to cover it on top, run and wait several minutes, it should be able to reproduce this crash.

Can you provide the demo?

ClippingNode *clipperNode = ClippingNode::create();
clipperNode->setAlphaThreshold(1);
addChild(clipperNode, LAYER_OBJ);

Sprite *contentImg = Sprite::create("image.png");
contentImg->setPosition(Point(SCREEN_CENTER.x, SCREEN_CENTER.y));
clipperNode->addChild(contentImg);

Sprite *holesImg = Sprite::createWithSpriteFrame(contentImg->getSpriteFrame());
holesImg->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT);
holesImg->setPosition(contentImg->getPosition()-Point(holesImg->getContentSize().width/2,0));

Node* holesStencil = Node::create();
holesStencil->addChild(holesImg, -1);
clipperNode->setStencil(holesStencil);

holesStencil->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(0, 0, 1),
                                                               ScaleTo::create(1, 1),
                                                               DelayTime::create(1.5),
                                                               NULL)));

Sprite* coverImg = Sprite::create("image.png");
coverImg->setPosition(Point(SCREEN_CENTER.x, SCREEN_CENTER.y));
addChild(coverImg);

Something similar can also reproduce this.

qoozta commented 4 years ago

After a deep look, I found the problem.

DepthStencilStateMTL::~DepthStencilStateMTL() { _mtlDepthStencilState = nil; }

With this change, will not crash when Metal API validation turned on, can be verified by Test 24 Raw Stencil Tests.

designerfuzzi commented 4 years ago

https://stackoverflow.com/questions/29546193/the-concept-of-nil-in-c/29546646

id<MTLDepthStencilState> _mtlDepthStencilState = nil;
// versus..
_mtlDepthStencilState = static_cast<DepthStencilStateMTL*>(depthStencilState)->getMTLDepthStencilState()

looks tricky.