axmol/tests/cpp-tests/Source/NewRendererTest/NewRendererTest.cpp

1077 lines
32 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "NewRendererTest.h"
#include <chrono>
#include <sstream>
#include "renderer/backend/Device.h"
namespace
{
enum CustomProgramType : uint32_t
{
BLUR = 1,
SEPIA = 2,
};
}
USING_NS_AX;
2019-11-23 20:27:39 +08:00
class DurationRecorder
{
2019-11-23 20:27:39 +08:00
public:
void startTick(std::string_view key) { _durations[key] = -now(); }
2019-11-23 20:27:39 +08:00
int endTick(std::string_view key)
{
auto n = now();
2019-11-23 20:27:39 +08:00
auto itr = _durations.find(key);
if (_durations.find(key) == _durations.end())
2019-11-23 20:27:39 +08:00
{
return -1;
}
else if (itr->second < 0)
{
2019-11-23 20:27:39 +08:00
itr->second = n + itr->second;
}
return static_cast<int>(itr->second);
}
inline int64_t now() const
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch())
.count();
2019-11-23 20:27:39 +08:00
}
void reset() { _durations.clear(); }
2019-11-23 20:27:39 +08:00
private:
hlookup::string_map<int64_t> _durations;
2019-11-23 20:27:39 +08:00
};
NewRendererTests::NewRendererTests()
{
auto programManager = ProgramManager::getInstance();
programManager->registerCustomProgramFactory(CustomProgramType::BLUR, positionTextureColor_vert,
FileUtils::getInstance()->getStringFromFile("Shaders/example_Blur.fsh"),
VertexLayoutHelper::setupSprite);
programManager->registerCustomProgramFactory(
CustomProgramType::SEPIA, positionTextureColor_vert,
FileUtils::getInstance()->getStringFromFile("Shaders/example_Sepia.fsh"),
VertexLayoutHelper::setupSprite);
2019-11-23 20:27:39 +08:00
ADD_TEST_CASE(NewSpriteTest);
ADD_TEST_CASE(GroupCommandTest);
// ADD_TEST_CASE(NewClippingNodeTest); // When depth and stencil are used together, ...
2019-11-23 20:27:39 +08:00
ADD_TEST_CASE(NewDrawNodeTest);
ADD_TEST_CASE(NewCullingTest);
ADD_TEST_CASE(VBOFullTest);
ADD_TEST_CASE(CaptureScreenTest);
ADD_TEST_CASE(CaptureNodeTest);
ADD_TEST_CASE(BugAutoCulling);
ADD_TEST_CASE(RendererBatchQuadTri);
ADD_TEST_CASE(RendererUniformBatch);
2019-11-23 20:27:39 +08:00
ADD_TEST_CASE(RendererUniformBatch2);
ADD_TEST_CASE(SpriteCreation);
ADD_TEST_CASE(NonBatchSprites);
};
std::string MultiSceneTest::title() const
{
return "New Renderer";
}
std::string MultiSceneTest::subtitle() const
{
return "MultiSceneTest";
}
NewSpriteTest::NewSpriteTest()
{
auto touchListener = EventListenerTouchAllAtOnce::create();
2022-07-16 10:43:05 +08:00
touchListener->onTouchesEnded = AX_CALLBACK_2(NewSpriteTest::onTouchesEnded, this);
2019-11-23 20:27:39 +08:00
createSpriteTest();
createNewSpriteTest();
}
NewSpriteTest::~NewSpriteTest() {}
2019-11-23 20:27:39 +08:00
void NewSpriteTest::createSpriteTest()
{
Size winSize = Director::getInstance()->getWinSize();
Sprite* parent = Sprite::create("Images/grossini.png");
parent->setPosition(winSize.width / 4, winSize.height / 2);
2019-11-23 20:27:39 +08:00
Sprite* child1 = Sprite::create("Images/grossinis_sister1.png");
child1->setPosition(0.0f, -20.0f);
Sprite* child2 = Sprite::create("Images/grossinis_sister2.png");
child2->setPosition(20.0f, -20.0f);
Sprite* child3 = Sprite::create("Images/grossinis_sister1.png");
child3->setPosition(40.0f, -20.0f);
Sprite* child4 = Sprite::create("Images/grossinis_sister2.png");
child4->setPosition(60.0f, -20.0f);
Sprite* child5 = Sprite::create("Images/grossinis_sister2.png");
child5->setPosition(80.0f, -20.0f);
Sprite* child6 = Sprite::create("Images/grossinis_sister2.png");
child6->setPosition(100.0f, -20.0f);
Sprite* child7 = Sprite::create("Images/grossinis_sister2.png");
child7->setPosition(120.0f, -20.0f);
parent->addChild(child1);
parent->addChild(child2);
parent->addChild(child3);
parent->addChild(child4);
parent->addChild(child5);
parent->addChild(child6);
parent->addChild(child7);
addChild(parent);
}
void NewSpriteTest::createNewSpriteTest()
{
Size winSize = Director::getInstance()->getWinSize();
Sprite* parent = Sprite::create("Images/grossini.png");
parent->setPosition(winSize.width * 2 / 3, winSize.height / 2);
2019-11-23 20:27:39 +08:00
Sprite* child1 = Sprite::create("Images/grossinis_sister1.png");
child1->setPosition(0.0f, -20.0f);
Sprite* child2 = Sprite::create("Images/grossinis_sister2.png");
child2->setPosition(20.0f, -20.0f);
Sprite* child3 = Sprite::create("Images/grossinis_sister1.png");
child3->setPosition(40.0f, -20.0f);
Sprite* child4 = Sprite::create("Images/grossinis_sister2.png");
child4->setPosition(60.0f, -20.0f);
Sprite* child5 = Sprite::create("Images/grossinis_sister2.png");
child5->setPosition(80.0f, -20.0f);
Sprite* child6 = Sprite::create("Images/grossinis_sister2.png");
child6->setPosition(100.0f, -20.0f);
Sprite* child7 = Sprite::create("Images/grossinis_sister2.png");
child7->setPosition(120.0f, -20.0f);
parent->addChild(child1);
parent->addChild(child2);
parent->addChild(child3);
parent->addChild(child4);
parent->addChild(child5);
parent->addChild(child6);
parent->addChild(child7);
addChild(parent);
}
void NewSpriteTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event) {}
2019-11-23 20:27:39 +08:00
std::string NewSpriteTest::title() const
{
return "Renderer";
}
std::string NewSpriteTest::subtitle() const
{
return "SpriteTest";
}
class SpriteInGroupCommand : public Sprite
{
public:
static SpriteInGroupCommand* create(std::string_view filename);
virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
2019-11-23 20:27:39 +08:00
};
SpriteInGroupCommand* SpriteInGroupCommand::create(std::string_view filename)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
SpriteInGroupCommand* sprite = new SpriteInGroupCommand();
2019-11-23 20:27:39 +08:00
sprite->initWithFile(filename);
sprite->autorelease();
return sprite;
}
void SpriteInGroupCommand::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(renderer, "Render is null");
auto * spriteWrapperCommand = renderer->getNextGroupCommand();
spriteWrapperCommand->init(_globalZOrder);
renderer->addCommand(spriteWrapperCommand);
renderer->pushGroup(spriteWrapperCommand->getRenderQueueID());
2019-11-23 20:27:39 +08:00
Sprite::draw(renderer, transform, flags);
renderer->popGroup();
}
GroupCommandTest::GroupCommandTest()
{
auto sprite = SpriteInGroupCommand::create("Images/grossini.png");
2019-11-23 20:27:39 +08:00
Size winSize = Director::getInstance()->getWinSize();
sprite->setPosition(winSize.width / 2, winSize.height / 2);
2019-11-23 20:27:39 +08:00
addChild(sprite);
}
GroupCommandTest::~GroupCommandTest() {}
2019-11-23 20:27:39 +08:00
std::string GroupCommandTest::title() const
{
return "Renderer";
}
std::string GroupCommandTest::subtitle() const
{
return "GroupCommandTest: You should see a sprite";
}
NewClippingNodeTest::NewClippingNodeTest()
{
auto s = Director::getInstance()->getWinSize();
auto clipper = ClippingNode::create();
clipper->setTag(kTagClipperNode);
clipper->setContentSize(Size(200.0f, 200.0f));
clipper->setAnchorPoint(Vec2(0.5f, 0.5f));
clipper->setPosition(Vec2(s.width / 2, s.height / 2));
2019-11-23 20:27:39 +08:00
clipper->runAction(RepeatForever::create(RotateBy::create(1, 45)));
this->addChild(clipper);
// TODO: Fix draw node as clip node
// auto stencil = NewDrawNode::create();
// Vec2 rectangle[4];
// rectangle[0] = Vec2(0, 0);
// rectangle[1] = Vec2(clipper->getContentSize().width, 0);
// rectangle[2] = Vec2(clipper->getContentSize().width, clipper->getContentSize().height);
// rectangle[3] = Vec2(0, clipper->getContentSize().height);
//
// Color4F white(1, 1, 1, 1);
// stencil->drawPolygon(rectangle, 4, white, 1, white);
// clipper->setStencil(stencil);
// Test with alpha Test
2019-11-23 20:27:39 +08:00
clipper->setAlphaThreshold(0.05f);
auto stencil = Sprite::create("Images/grossini.png");
stencil->setPosition(s.width / 2, s.height / 2);
2019-11-23 20:27:39 +08:00
clipper->setStencil(stencil);
auto content = Sprite::create("Images/background2.png");
content->setTag(kTagContentNode);
content->setAnchorPoint(Vec2(0.5f, 0.5f));
content->setPosition(Vec2(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2));
2019-11-23 20:27:39 +08:00
clipper->addChild(content);
_scrolling = false;
auto listener = EventListenerTouchAllAtOnce::create();
2022-07-16 10:43:05 +08:00
listener->onTouchesBegan = AX_CALLBACK_2(NewClippingNodeTest::onTouchesBegan, this);
listener->onTouchesMoved = AX_CALLBACK_2(NewClippingNodeTest::onTouchesMoved, this);
listener->onTouchesEnded = AX_CALLBACK_2(NewClippingNodeTest::onTouchesEnded, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
NewClippingNodeTest::~NewClippingNodeTest() {}
2019-11-23 20:27:39 +08:00
std::string NewClippingNodeTest::title() const
{
return "New Render";
}
std::string NewClippingNodeTest::subtitle() const
{
return "ClipNode";
}
void NewClippingNodeTest::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
2019-11-23 20:27:39 +08:00
{
Touch* touch = touches[0];
2019-11-23 20:27:39 +08:00
auto clipper = this->getChildByTag(kTagClipperNode);
Vec2 point = clipper->convertToNodeSpace(Director::getInstance()->convertToGL(touch->getLocationInView()));
auto rect = Rect(0, 0, clipper->getContentSize().width, clipper->getContentSize().height);
_scrolling = rect.containsPoint(point);
_lastPoint = point;
2019-11-23 20:27:39 +08:00
}
void NewClippingNodeTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
2019-11-23 20:27:39 +08:00
{
if (!_scrolling)
return;
Touch* touch = touches[0];
2019-11-23 20:27:39 +08:00
auto clipper = this->getChildByTag(kTagClipperNode);
auto point = clipper->convertToNodeSpace(Director::getInstance()->convertToGL(touch->getLocationInView()));
Vec2 diff = point - _lastPoint;
2019-11-23 20:27:39 +08:00
auto content = clipper->getChildByTag(kTagContentNode);
content->setPosition(content->getPosition() + diff);
_lastPoint = point;
}
void NewClippingNodeTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
2019-11-23 20:27:39 +08:00
{
if (!_scrolling)
return;
2019-11-23 20:27:39 +08:00
_scrolling = false;
}
/**
* NewDrawNode
*/
2019-11-23 20:27:39 +08:00
NewDrawNodeTest::NewDrawNodeTest()
{
auto s = Director::getInstance()->getWinSize();
auto parent = Node::create();
parent->setPosition(s.width / 2, s.height / 2);
2019-11-23 20:27:39 +08:00
addChild(parent);
auto rectNode = DrawNode::create();
Vec2 rectangle[4];
rectangle[0] = Vec2(-50, -50);
rectangle[1] = Vec2(50, -50);
rectangle[2] = Vec2(50, 50);
rectangle[3] = Vec2(-50, 50);
Color4F white(1, 1, 1, 1);
rectNode->drawPolygon(rectangle, 4, white, 1, white);
parent->addChild(rectNode);
}
NewDrawNodeTest::~NewDrawNodeTest() {}
2019-11-23 20:27:39 +08:00
std::string NewDrawNodeTest::title() const
{
return "New Render";
}
std::string NewDrawNodeTest::subtitle() const
{
return "DrawNode";
}
NewCullingTest::NewCullingTest()
{
Size size = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
auto sprite = Sprite::create("Images/btn-about-normal-vertical.png");
sprite->setRotation(5);
sprite->setPosition(Vec2(size.width / 2, size.height / 3));
2019-11-23 20:27:39 +08:00
sprite->setScale(2);
addChild(sprite);
auto sprite2 = Sprite::create("Images/btn-about-normal-vertical.png");
sprite2->setRotation(-85);
sprite2->setPosition(Vec2(size.width / 2, size.height * 2 / 3));
2019-11-23 20:27:39 +08:00
sprite2->setScale(2);
addChild(sprite2);
2019-11-23 20:27:39 +08:00
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
2022-07-16 10:43:05 +08:00
listener->onTouchBegan = AX_CALLBACK_2(NewCullingTest::onTouchBegan, this);
listener->onTouchMoved = AX_CALLBACK_2(NewCullingTest::onTouchMoved, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
bool NewCullingTest::onTouchBegan(Touch* touch, Event* event)
2019-11-23 20:27:39 +08:00
{
auto pos = touch->getLocation();
_lastPos = pos;
return true;
}
void NewCullingTest::onTouchMoved(Touch* touch, Event* event)
{
2019-11-23 20:27:39 +08:00
auto pos = touch->getLocation();
2019-11-23 20:27:39 +08:00
auto offset = pos - _lastPos;
2019-11-23 20:27:39 +08:00
auto layerPos = getPosition();
auto newPos = layerPos + offset;
2019-11-23 20:27:39 +08:00
setPosition(newPos);
_lastPos = pos;
}
NewCullingTest::~NewCullingTest() {}
2019-11-23 20:27:39 +08:00
std::string NewCullingTest::title() const
{
return "New Render";
}
std::string NewCullingTest::subtitle() const
{
return "Drag the layer to test the result of culling";
}
SpriteCreation::SpriteCreation()
{
Size s = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
Node* parent = Node::create();
parent->setPosition(s.width / 2, s.height / 2);
2019-11-23 20:27:39 +08:00
addChild(parent);
#define KEY_CREATION "11"
#define KEY_DESTROYATION "22"
labelCreate = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "Sprite Creation: ..");
labelDestory = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "Destroy Sprites: ..");
2019-11-23 20:27:39 +08:00
MenuItemFont::setFontName("fonts/arial.ttf");
MenuItemFont::setFontSize(65);
2022-07-16 10:43:05 +08:00
auto decrease = MenuItemFont::create(" - ", AX_CALLBACK_1(SpriteCreation::delSpritesCallback, this));
2019-11-23 20:27:39 +08:00
decrease->setColor(Color3B(0, 200, 20));
2022-07-16 10:43:05 +08:00
auto increase = MenuItemFont::create(" + ", AX_CALLBACK_1(SpriteCreation::addSpritesCallback, this));
2019-11-23 20:27:39 +08:00
increase->setColor(Color3B(0, 200, 20));
auto menu = Menu::create(decrease, increase, nullptr);
menu->alignItemsHorizontally();
menu->setPosition(Vec2(s.width / 2, s.height - 105));
addChild(menu, 1);
TTFConfig ttfCount("fonts/Marker Felt.ttf", 30);
_labelSpriteNum = Label::createWithTTF(ttfCount, "Label");
_labelSpriteNum->setColor(Color3B(0, 200, 20));
_labelSpriteNum->setPosition(Vec2(s.width / 2, s.height - 130));
addChild(_labelSpriteNum);
updateSpriteCountLabel(totalSprites);
labelCreate->setPosition(0, -20);
labelDestory->setPosition(0, -50);
parent->addChild(labelCreate);
parent->addChild(labelDestory);
doTest();
}
void SpriteCreation::updateSpriteCountLabel(int x)
{
totalSprites = std::max(1, x);
std::stringstream ss;
ss << totalSprites << " sprites";
_labelSpriteNum->setString(ss.str());
}
void SpriteCreation::doTest()
{
DurationRecorder perf;
std::vector<std::string> predefineTextures = {"Images/concave.png",
"Images/atlastest.png",
"Images/grossini_dance_atlas-mono.png",
"Images/HelloWorld.png",
"Images/background1.png",
"Images/background2.png",
"Images/stone.png",
"Images/issue_17116.png",
"Images/sprite_polygon_crash.png",
"Images/bitmapFontTest3.png",
"Images/cocos-html5.png",
"Images/Fog.png",
"Images/poly_test_textures.png",
"Images/powered.png",
"Images/bug14017.png",
"Images/test-rgba1.png",
"Images/grossinis_heads.png",
"Images/cocos2dbanner.png"};
2019-11-23 20:27:39 +08:00
std::vector<Sprite*> spriteCache;
spriteCache.reserve(totalSprites);
perf.startTick(KEY_CREATION);
for (int i = 0; i < totalSprites; ++i)
2019-11-23 20:27:39 +08:00
{
auto* sprite = new Sprite();
if (sprite == nullptr)
2019-11-23 20:27:39 +08:00
{
break;
}
if (!sprite->initWithFile(predefineTextures[i % predefineTextures.size()]))
2019-11-23 20:27:39 +08:00
{
delete sprite;
break;
}
spriteCache.emplace_back(sprite);
2019-11-23 20:27:39 +08:00
}
auto creationDuration = perf.endTick(KEY_CREATION);
perf.startTick(KEY_DESTROYATION);
for (int i = 0; i < totalSprites; ++i)
2019-11-23 20:27:39 +08:00
{
spriteCache[i]->release();
}
auto destroyDuration = perf.endTick(KEY_DESTROYATION);
std::stringstream ss;
auto t1_ms = creationDuration * 1.0 / 1000000;
ss << "Create " << spriteCache.size() << " sprites takes " << t1_ms << " ms, "
<< (int64_t)(spriteCache.size() * 1000 / t1_ms) << " sprites per second!";
2019-11-23 20:27:39 +08:00
labelCreate->setString(ss.str());
if (t1_ms < 100)
{
suggestDelta = (int)(0.5 * totalSprites);
}
else if (t1_ms < 1000)
{
suggestDelta = (int)(0.2 * totalSprites);
}
else if (t1_ms)
{
suggestDelta = (int)(0.1 * totalSprites);
2019-11-23 20:27:39 +08:00
}
suggestDelta = suggestDelta < 1000 ? 1000 : suggestDelta - suggestDelta % 1000;
ss.str("");
auto t2_ms = destroyDuration * 1.0 / 1000000;
ss << "Destroy " << spriteCache.size() << " sprites takes " << t2_ms << " ms, "
<< (int64_t)(spriteCache.size() * 1000 / t2_ms) << " sprites per second!";
2019-11-23 20:27:39 +08:00
labelDestory->setString(ss.str());
spriteCache.clear();
}
2022-10-18 19:17:36 +08:00
void SpriteCreation::addSpritesCallback(ax::Ref*)
2019-11-23 20:27:39 +08:00
{
updateSpriteCountLabel(totalSprites + suggestDelta);
doTest();
}
2022-10-18 19:17:36 +08:00
void SpriteCreation::delSpritesCallback(ax::Ref*)
2019-11-23 20:27:39 +08:00
{
updateSpriteCountLabel(totalSprites - suggestDelta);
doTest();
}
SpriteCreation::~SpriteCreation() {}
2019-11-23 20:27:39 +08:00
std::string SpriteCreation::title() const
{
return "Sprite Creation";
}
std::string SpriteCreation::subtitle() const
{
2022-08-08 18:02:17 +08:00
#if defined(_AX_DEBUG) && _AX_DEBUG == 1
2019-11-23 20:27:39 +08:00
return "In debug mode";
#else
return "In release mode";
#endif
}
VBOFullTest::VBOFullTest()
{
Size s = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
Node* parent = Node::create();
parent->setPosition(0, 0);
2019-11-23 20:27:39 +08:00
addChild(parent);
for (int i = 0; i < Renderer::VBO_SIZE / 3.9; ++i)
2019-11-23 20:27:39 +08:00
{
Sprite* sprite = Sprite::create("Images/grossini_dance_01.png");
sprite->setScale(0.1f, 0.1f);
float x = ((float)std::rand()) / RAND_MAX;
float y = ((float)std::rand()) / RAND_MAX;
2019-11-23 20:27:39 +08:00
sprite->setPosition(Vec2(x * s.width, y * s.height));
parent->addChild(sprite);
}
}
VBOFullTest::~VBOFullTest() {}
2019-11-23 20:27:39 +08:00
std::string VBOFullTest::title() const
{
return "New Renderer";
}
std::string VBOFullTest::subtitle() const
{
return "VBO full Test, everything should render normally";
}
CaptureScreenTest::CaptureScreenTest()
{
Size s = Director::getInstance()->getWinSize();
Vec2 left(s.width / 4, s.height / 2);
Vec2 right(s.width / 4 * 3, s.height / 2);
2019-11-23 20:27:39 +08:00
auto sp1 = Sprite::create("Images/grossini.png");
sp1->setPosition(left);
auto move1 = MoveBy::create(1, Vec2(s.width / 2, 0.0f));
auto seq1 = RepeatForever::create(Sequence::create(move1, move1->reverse(), nullptr));
2019-11-23 20:27:39 +08:00
addChild(sp1);
sp1->runAction(seq1);
auto sp2 = Sprite::create("Images/grossinis_sister1.png");
sp2->setPosition(right);
auto move2 = MoveBy::create(1, Vec2(-s.width / 2, 0.0f));
auto seq2 = RepeatForever::create(Sequence::create(move2, move2->reverse(), nullptr));
2019-11-23 20:27:39 +08:00
addChild(sp2);
sp2->runAction(seq2);
auto label1 = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "capture all");
2022-07-16 10:43:05 +08:00
auto mi1 = MenuItemLabel::create(label1, AX_CALLBACK_1(CaptureScreenTest::onCaptured, this));
auto menu = Menu::create(mi1, nullptr);
2019-11-23 20:27:39 +08:00
addChild(menu);
menu->setPosition(s.width / 2, s.height / 4);
_filename = "";
}
CaptureScreenTest::~CaptureScreenTest()
{
Director::getInstance()->getTextureCache()->removeTextureForKey(_filename);
}
std::string CaptureScreenTest::title() const
{
return "New Renderer";
}
std::string CaptureScreenTest::subtitle() const
{
return "Capture screen test, press the menu items to capture the screen";
}
void CaptureScreenTest::onCaptured(Ref*)
{
Director::getInstance()->getTextureCache()->removeTextureForKey(_filename);
removeChildByTag(childTag);
_filename = "CaptureScreenTest.png";
// retain it to avoid crash caused by invoking afterCaptured
2019-11-23 20:27:39 +08:00
this->retain();
2022-07-16 10:43:05 +08:00
utils::captureScreen(AX_CALLBACK_2(CaptureScreenTest::afterCaptured, this), _filename);
2019-11-23 20:27:39 +08:00
}
void CaptureScreenTest::afterCaptured(bool succeed, std::string_view outputFile)
2019-11-23 20:27:39 +08:00
{
if (succeed)
{
auto sp = Sprite::create(outputFile);
addChild(sp, 0, childTag);
Size s = Director::getInstance()->getWinSize();
sp->setPosition(s.width / 2, s.height / 2);
sp->setScale(0.25);
_filename = outputFile;
}
else
{
log("Capture screen failed.");
}
// release it since it is retained in `CaptureScreenTest::onCaptured()`
this->release();
}
CaptureNodeTest::CaptureNodeTest()
{
Size s = Director::getInstance()->getWinSize();
Vec2 left(s.width / 4, s.height / 2);
Vec2 right(s.width / 4 * 3, s.height / 2);
auto sp1 = Sprite::create("Images/grossini.png");
sp1->setPosition(left);
auto move1 = MoveBy::create(1, Vec2(s.width / 2, 0.0f));
auto seq1 = RepeatForever::create(Sequence::create(move1, move1->reverse(), nullptr));
2019-11-23 20:27:39 +08:00
addChild(sp1);
sp1->runAction(seq1);
auto sp2 = Sprite::create("Images/grossinis_sister1.png");
sp2->setPosition(right);
auto move2 = MoveBy::create(1, Vec2(-s.width / 2, 0.0f));
auto seq2 = RepeatForever::create(Sequence::create(move2, move2->reverse(), nullptr));
2019-11-23 20:27:39 +08:00
addChild(sp2);
sp2->runAction(seq2);
auto label1 = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "capture this scene");
2022-07-16 10:43:05 +08:00
auto mi1 = MenuItemLabel::create(label1, AX_CALLBACK_1(CaptureNodeTest::onCaptured, this));
auto menu = Menu::create(mi1, nullptr);
2019-11-23 20:27:39 +08:00
addChild(menu);
menu->setPosition(s.width / 2, s.height / 4);
_filename = "";
}
CaptureNodeTest::~CaptureNodeTest()
{
Director::getInstance()->getTextureCache()->removeTextureForKey(_filename);
}
std::string CaptureNodeTest::title() const
{
return "New Renderer";
}
std::string CaptureNodeTest::subtitle() const
{
return "Capture node test, press the menu items to capture this scene with scale 0.5";
}
void CaptureNodeTest::onCaptured(Ref*)
{
2019-11-23 20:27:39 +08:00
Director::getInstance()->getTextureCache()->removeTextureForKey(_filename);
removeChildByTag(childTag);
2019-11-23 20:27:39 +08:00
_filename = FileUtils::getInstance()->getWritablePath() + "/CaptureNodeTest.png";
// capture this
auto callback = [&](RefPtr<Image> image) {
2019-11-23 20:27:39 +08:00
// create a sprite with the captured image directly
auto sp = Sprite::createWithTexture(Director::getInstance()->getTextureCache()->addImage(image, _filename));
addChild(sp, 0, childTag);
Size s = Director::getInstance()->getWinSize();
sp->setPosition(s.width / 2, s.height / 2);
2019-11-23 20:27:39 +08:00
// store to disk
image->saveToFile(_filename, false);
2019-11-23 20:27:39 +08:00
};
2019-11-23 20:27:39 +08:00
auto callbackFunction = std::bind(callback, std::placeholders::_1);
utils::captureNode(this, callbackFunction, 0.5);
}
BugAutoCulling::BugAutoCulling()
{
Size s = Director::getInstance()->getWinSize();
2022-10-18 19:17:36 +08:00
auto fastmap = ax::FastTMXTiledMap::create("TileMaps/orthogonal-test2.tmx");
2019-11-23 20:27:39 +08:00
this->addChild(fastmap);
for (int i = 0; i < 30; i++)
{
2019-11-23 20:27:39 +08:00
auto sprite = Sprite::create("Images/grossini.png");
sprite->setPosition(s.width / 2 + s.width / 10 * i, s.height / 2);
2019-11-23 20:27:39 +08:00
this->addChild(sprite);
auto label = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "Label");
label->setPosition(s.width / 2 + s.width / 10 * i, s.height / 2);
2019-11-23 20:27:39 +08:00
this->addChild(label);
}
this->scheduleOnce(
[=](float) {
auto camera = Director::getInstance()->getRunningScene()->getCameras().front();
auto move = MoveBy::create(2.0f, Vec2(2 * s.width, 0.0f));
camera->runAction(Sequence::create(move, move->reverse(), nullptr));
},
1.0f, "lambda-autoculling-bug");
2019-11-23 20:27:39 +08:00
}
std::string BugAutoCulling::title() const
{
return "Bug-AutoCulling";
}
std::string BugAutoCulling::subtitle() const
{
return "Moving the camera to the right instead of moving the layer";
}
//
// RendererBatchQuadTri
//
RendererBatchQuadTri::RendererBatchQuadTri()
{
Size s = Director::getInstance()->getWinSize();
for (int i = 0; i < 250; i++)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
int x = AXRANDOM_0_1() * s.width;
int y = AXRANDOM_0_1() * s.height;
2019-11-23 20:27:39 +08:00
auto label = LabelAtlas::create("This is a label", "fonts/tuffy_bold_italic-charmap.plist");
label->setColor(Color3B::RED);
label->setPosition(Vec2(x, y));
2019-11-23 20:27:39 +08:00
addChild(label);
auto sprite = Sprite::create("fonts/tuffy_bold_italic-charmap.png");
sprite->setTextureRect(Rect(0.0f, 0.0f, 100.0f, 100.0f));
sprite->setPosition(Vec2(x, y));
2019-11-23 20:27:39 +08:00
sprite->setColor(Color3B::BLUE);
addChild(sprite);
}
}
std::string RendererBatchQuadTri::title() const
{
return "RendererBatchQuadTri";
}
std::string RendererBatchQuadTri::subtitle() const
{
return "QuadCommand and TriangleCommands are batched together";
}
//
//
// RendererUniformBatch
//
RendererUniformBatch::RendererUniformBatch()
{
Size s = Director::getInstance()->getWinSize();
auto blurState = createBlurProgramState();
2019-11-23 20:27:39 +08:00
auto sepiaState = createSepiaProgramState();
auto x_inc = s.width / 20;
auto y_inc = s.height / 6;
for (int y = 0; y < 6; ++y)
2019-11-23 20:27:39 +08:00
{
for (int x = 0; x < 20; ++x)
2019-11-23 20:27:39 +08:00
{
auto sprite = Sprite::create("Images/grossini.png");
sprite->setPosition(Vec2(x * x_inc, y * y_inc));
sprite->setScale(0.4);
addChild(sprite);
if (y >= 4)
{
2019-11-23 20:27:39 +08:00
sprite->setProgramState(sepiaState);
}
else if (y >= 2)
{
2019-11-23 20:27:39 +08:00
sprite->setProgramState(blurState);
}
}
}
}
2022-10-18 19:17:36 +08:00
ax::backend::ProgramState* RendererUniformBatch::createBlurProgramState()
2019-11-23 20:27:39 +08:00
{
auto programState =
new backend::ProgramState(ProgramManager::getInstance()->getCustomProgram(CustomProgramType::BLUR));
2019-11-23 20:27:39 +08:00
programState->autorelease();
backend::UniformLocation loc = programState->getUniformLocation("resolution");
auto resolution = Vec2(85, 121);
2019-11-23 20:27:39 +08:00
programState->setUniform(loc, &resolution, sizeof(resolution));
loc = programState->getUniformLocation("blurRadius");
2019-11-23 20:27:39 +08:00
float blurRadius = 10.0f;
programState->setUniform(loc, &blurRadius, sizeof(blurRadius));
loc = programState->getUniformLocation("sampleNum");
2019-11-23 20:27:39 +08:00
float sampleNum = 5.0f;
programState->setUniform(loc, &sampleNum, sizeof(sampleNum));
return programState;
}
2022-10-18 19:17:36 +08:00
ax::backend::ProgramState* RendererUniformBatch::createSepiaProgramState()
2019-11-23 20:27:39 +08:00
{
auto programState =
new backend::ProgramState(ProgramManager::getInstance()->getCustomProgram(CustomProgramType::SEPIA));
// programState->updateUniformID();
2019-11-23 20:27:39 +08:00
programState->autorelease();
return programState;
}
std::string RendererUniformBatch::title() const
{
return "RendererUniformBatch";
}
std::string RendererUniformBatch::subtitle() const
{
return "Only 9 draw calls should appear";
}
////
//// RendererUniformBatch2
////
RendererUniformBatch2::RendererUniformBatch2()
{
Size s = Director::getInstance()->getWinSize();
auto blurState = createBlurProgramState();
2019-11-23 20:27:39 +08:00
auto sepiaState = createSepiaProgramState();
auto x_inc = s.width / 20;
auto y_inc = s.height / 6;
for (int y = 0; y < 6; ++y)
2019-11-23 20:27:39 +08:00
{
for (int x = 0; x < 20; ++x)
2019-11-23 20:27:39 +08:00
{
auto sprite = Sprite::create("Images/grossini.png");
sprite->setPosition(Vec2(x * x_inc, y * y_inc));
sprite->setScale(0.4);
addChild(sprite);
2022-07-16 10:43:05 +08:00
auto r = AXRANDOM_0_1();
2019-11-23 20:27:39 +08:00
if (r < 0.33)
sprite->setProgramState(sepiaState);
else if (r < 0.66)
sprite->setProgramState(blurState);
}
}
}
backend::ProgramState* RendererUniformBatch2::createBlurProgramState()
{
auto programState =
new backend::ProgramState(ProgramManager::getInstance()->getCustomProgram(CustomProgramType::BLUR));
2019-11-23 20:27:39 +08:00
backend::UniformLocation loc = programState->getUniformLocation("resolution");
auto resolution = Vec2(85, 121);
2019-11-23 20:27:39 +08:00
programState->setUniform(loc, &resolution, sizeof(resolution));
loc = programState->getUniformLocation("blurRadius");
2019-11-23 20:27:39 +08:00
float blurRadius = 10.0f;
programState->setUniform(loc, &blurRadius, sizeof(blurRadius));
loc = programState->getUniformLocation("sampleNum");
2019-11-23 20:27:39 +08:00
float sampleNum = 5.0f;
programState->setUniform(loc, &sampleNum, sizeof(sampleNum));
return programState;
}
backend::ProgramState* RendererUniformBatch2::createSepiaProgramState()
2019-11-23 20:27:39 +08:00
{
auto programState =
new backend::ProgramState(ProgramManager::getInstance()->getCustomProgram(CustomProgramType::SEPIA));
2019-11-23 20:27:39 +08:00
programState->autorelease();
return programState;
}
std::string RendererUniformBatch2::title() const
{
return "RendererUniformBatch 2";
}
std::string RendererUniformBatch2::subtitle() const
{
return "Mixing different shader states should work ok";
}
NonBatchSprites::NonBatchSprites()
{
Size s = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
_spritesAnchor = Node::create();
_spritesAnchor->setPosition(0, 0);
addChild(_spritesAnchor);
_totalSprites = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "sprites");
_totalSprites->setColor(Color3B::YELLOW);
_totalSprites->enableOutline(Color4B::RED, 2);
_totalSprites->setPosition(s.width / 2, s.height / 2);
2019-11-23 20:27:39 +08:00
addChild(_totalSprites);
scheduleUpdate();
}
void NonBatchSprites::createSprite()
{
Size s = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
Sprite* sprite = nullptr;
if (_spriteIndex % 2 == 0)
{
sprite = Sprite::create("Images/grossini_dance_05.png");
}
else
{
sprite = Sprite::create("Images/grossini_dance_01.png");
}
if (!sprite)
return;
2019-11-23 20:27:39 +08:00
auto r = rand_0_1() * 0.6 + 0.2;
sprite->setScale(r, r);
float x = ((float)std::rand()) / RAND_MAX;
float y = ((float)std::rand()) / RAND_MAX;
sprite->runAction(RepeatForever::create(RotateBy::create(1, 45)));
sprite->setPosition(Vec2(x * s.width, y * s.height));
_spritesAnchor->addChild(sprite);
_spriteIndex++;
std::stringstream ss;
ss << _spriteIndex << " sprites";
_totalSprites->setString(ss.str());
}
void NonBatchSprites::update(float dt)
{
if (dt <= 1.0f / 28.0f && dt >= 1.0f / 31.0f)
2019-11-23 20:27:39 +08:00
{
_around30fps.hit();
}
else
{
_around30fps.cancel();
}
_maDt = 0.7f * _maDt + 0.3f * dt;
_rmaDt = 0.5f * _rmaDt + 0.5f * dt;
if (_maDt <= DEST_DT_30FPS)
{
2019-11-23 20:27:39 +08:00
_contSlow.cancel();
_contFast.hit();
if (_contFast.ok())
{
auto t2 = DEST_DT_30FPS - _rmaDt;
2019-11-23 20:27:39 +08:00
auto delta = (int)(t2 / _rmaDt * _spriteIndex * 0.1);
delta = std::min(20, std::max(1, delta));
for (int i = 0; i < delta; i++)
{
2019-11-23 20:27:39 +08:00
createSprite();
}
}
}
else
{
2019-11-23 20:27:39 +08:00
_contSlow.hit();
_contFast.cancel();
}
if (_contSlow.ok() || _around30fps.ok())
2019-11-23 20:27:39 +08:00
{
unscheduleUpdate();
std::stringstream ss;
ss << _spriteIndex << " sprites, DONE!";
_totalSprites->setString(ss.str());
_totalSprites->setScale(1.2);
}
}
NonBatchSprites::~NonBatchSprites() {}
2019-11-23 20:27:39 +08:00
std::string NonBatchSprites::title() const
{
return "Non Batched Sprites";
}
std::string NonBatchSprites::subtitle() const
{
2022-08-08 18:02:17 +08:00
#if defined(_AX_DEBUG) && _AX_DEBUG == 1
2019-11-23 20:27:39 +08:00
return "DEBUG: simulate lots of sprites, drop to 30 fps";
#else
return "RELEASE: simulate lots of sprites, drop to 30 fps";
#endif
}