Skip to content

Commit

Permalink
perf: fps stability ~10%
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah authored Dec 16, 2024
1 parent daf394a commit 01f8e20
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 68 deletions.
10 changes: 5 additions & 5 deletions src/client/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,20 @@ void MapView::updateVisibleTiles()
if (!m_lastCameraPosition.isValid() || m_lastCameraPosition.z != m_posInfo.camera.z || m_lastCameraPosition.distance(m_posInfo.camera) >= 3) {
m_fadeType = FadeType::NONE$;
for (int iz = m_cachedLastVisibleFloor; iz >= cachedFirstVisibleFloor; --iz) {
m_floors[iz].fadingTimers.restart(m_floorFading * 1000);
m_floors[iz].fadingTimers.restart(m_floorFading);
}
} else if (prevFirstVisibleFloor < m_cachedFirstVisibleFloor) { // hiding new floor
m_fadeType = FadeType::OUT$;
for (int iz = prevFirstVisibleFloor; iz < m_cachedFirstVisibleFloor; ++iz) {
const int shift = std::max<int>(0, m_floorFading - m_floors[iz].fadingTimers.elapsed_millis());
m_floors[iz].fadingTimers.restart(shift * 1000);
const int shift = std::max<int>(0, m_floorFading - m_floors[iz].fadingTimers.ticksElapsed());
m_floors[iz].fadingTimers.restart(shift);
}
} else if (prevFirstVisibleFloor > m_cachedFirstVisibleFloor) { // showing floor
m_fadeType = FadeType::IN$;
m_fadeFinish = false;
for (int iz = m_cachedFirstVisibleFloor; iz < prevFirstVisibleFloor; ++iz) {
const int shift = std::max<int>(0, m_floorFading - m_floors[iz].fadingTimers.elapsed_millis());
m_floors[iz].fadingTimers.restart(shift * 1000);
const int shift = std::max<int>(0, m_floorFading - m_floors[iz].fadingTimers.ticksElapsed());
m_floors[iz].fadingTimers.restart(shift);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/client/mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class MapView final : public LuaObject
struct FloorData
{
MapObject cachedVisibleTiles;
stdext::timer fadingTimers;
Timer fadingTimers;
};

struct Crosshair
Expand Down Expand Up @@ -251,7 +251,7 @@ class MapView final : public LuaObject
{
if (!canFloorFade()) return 1.f;

float fading = std::clamp<float>(static_cast<float>(m_floors[z].fadingTimers.elapsed_millis()) / static_cast<float>(m_floorFading), 0.f, 1.f);
float fading = std::clamp<float>(static_cast<float>(m_floors[z].fadingTimers.ticksElapsed()) / static_cast<float>(m_floorFading), 0.f, 1.f);
if (z < m_cachedFirstVisibleFloor)
fading = 1.0 - fading;
return fading;
Expand Down
4 changes: 2 additions & 2 deletions src/framework/core/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include "timer.h"
#include "clock.h"

void Timer::restart()
void Timer::restart(const int shift)
{
m_startTicks = g_clock.millis();
m_startTicks = g_clock.millis() - shift;
m_stopped = false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/framework/core/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Timer
public:
Timer() { restart(); }

void restart();
void restart(const int shift = 0);
void stop() { m_stopped = true; }
void update(const ticks_t tick) { m_startTicks += tick; }

Expand Down
31 changes: 13 additions & 18 deletions src/framework/graphics/drawpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ DrawPool* DrawPool::create(const DrawPoolType type)
// creates a temporary framebuffer with smoothing.
pool->m_temporaryFramebuffers.emplace_back(std::make_shared<FrameBuffer>());
}
} else if (type == DrawPoolType::LIGHT) {
pool->m_hashCtrl = true;
} else {
pool->m_alwaysGroupDrawings = true; // CREATURE_INFORMATION & TEXT
pool->setFPS(60);
Expand All @@ -45,8 +47,7 @@ DrawPool* DrawPool::create(const DrawPoolType type)
return pool;
}

void DrawPool::add(const Color& color, const TexturePtr& texture, DrawMethod&& method,
DrawMode drawMode, const DrawConductor& conductor, const CoordsBufferPtr& coordsBuffer)
void DrawPool::add(const Color& color, const TexturePtr& texture, DrawMethod&& method, const DrawConductor& conductor, const CoordsBufferPtr& coordsBuffer)
{
if (!updateHash(method, texture, color, coordsBuffer != nullptr))
return;
Expand All @@ -67,53 +68,47 @@ void DrawPool::add(const Color& color, const TexturePtr& texture, DrawMethod&& m
if (coordsBuffer)
coords->append(coordsBuffer.get());
else
addCoords(coords, method, DrawMode::TRIANGLES);
addCoords(coords, method);
} else {
bool addNewObj = true;

auto& list = m_objects[order];
if (!list.empty()) {
auto& prevObj = list.back();
if (prevObj.state == getCurrentState()) {
if (!prevObj.coords)
prevObj.addMethod(std::move(method));
else if (coordsBuffer)
if (coordsBuffer)
prevObj.coords->append(coordsBuffer.get());
else
addCoords(prevObj.coords.get(), method, DrawMode::TRIANGLES);
addCoords(prevObj.coords.get(), method);

addNewObj = false;
}
}

if (addNewObj) {
auto state = getState(texture, color);
auto& draw = list.emplace_back(std::move(state), getCoordsBuffer());

if (coordsBuffer) {
list.emplace_back(std::move(state), getCoordsBuffer()).coords->append(coordsBuffer.get());
draw.coords->append(coordsBuffer.get());
} else
list.emplace_back(drawMode, std::move(state), std::move(method));
addCoords(draw.coords.get(), method);
}
}

resetOnlyOnceParameters();
}

void DrawPool::addCoords(CoordsBuffer* buffer, const DrawMethod& method, const DrawMode drawMode)
void DrawPool::addCoords(CoordsBuffer* buffer, const DrawMethod& method)
{
if (method.type == DrawMethodType::BOUNDING_RECT) {
buffer->addBoudingRect(method.dest, method.intValue);
} else if (method.type == DrawMethodType::RECT) {
if (drawMode == DrawMode::TRIANGLES)
buffer->addRect(method.dest, method.src);
else
buffer->addQuad(method.dest, method.src);
buffer->addRect(method.dest, method.src);
} else if (method.type == DrawMethodType::TRIANGLE) {
buffer->addTriangle(method.a, method.b, method.c);
} else if (method.type == DrawMethodType::UPSIDEDOWN_RECT) {
if (drawMode == DrawMode::TRIANGLES)
buffer->addUpsideDownRect(method.dest, method.src);
else
buffer->addUpsideDownQuad(method.dest, method.src);
buffer->addUpsideDownRect(method.dest, method.src);
} else if (method.type == DrawMethodType::REPEATED_RECT) {
buffer->addRepeatedRects(method.dest, method.src);
}
Expand Down
28 changes: 7 additions & 21 deletions src/framework/graphics/drawpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ enum DrawOrder : uint8_t

struct DrawHashController
{
bool put(size_t hash) {
m_lastObjectHash = hash;
DrawHashController(bool agroup = false) : m_agroup(agroup) {}

if (m_hashs.emplace(hash).second) {
bool put(size_t hash) {
if ((m_agroup && m_hashs.emplace(hash).second) || m_lastObjectHash != hash) {
m_lastObjectHash = hash;
stdext::hash_union(m_currentHash, hash);
return true;
}
Expand Down Expand Up @@ -92,6 +93,7 @@ struct DrawHashController
size_t m_lastHash{ 0 };
size_t m_currentHash{ 0 };
size_t m_lastObjectHash{ 0 };
bool m_agroup{ false };
};

struct DrawConductor
Expand Down Expand Up @@ -195,24 +197,9 @@ class DrawPool
{
DrawObject(std::function<void()> action) : action(std::move(action)) {}
DrawObject(PoolState&& state, const std::shared_ptr<CoordsBuffer>& coords) : coords(coords), state(std::move(state)) {}
DrawObject(const DrawMode drawMode, PoolState&& state, DrawMethod&& method) :
state(std::move(state)), drawMode(drawMode) {
methods.reserve(10);
methods.emplace_back(std::move(method));
}

void addMethod(DrawMethod&& method)
{
drawMode = DrawMode::TRIANGLES;
methods.emplace_back(std::move(method));
}

std::vector<DrawMethod> methods;
std::function<void()> action{ nullptr };
std::shared_ptr<CoordsBuffer> coords;

PoolState state;
DrawMode drawMode{ DrawMode::TRIANGLES };
};

struct DrawObjectState
Expand All @@ -227,7 +214,7 @@ class DrawPool

private:
static DrawPool* create(DrawPoolType type);
static void addCoords(CoordsBuffer* buffer, const DrawMethod& method, DrawMode drawMode);
static void addCoords(CoordsBuffer* buffer, const DrawMethod& method);

enum STATE_TYPE : uint32_t
{
Expand All @@ -238,8 +225,7 @@ class DrawPool
STATE_BLEND_EQUATION = 1 << 4,
};

void add(const Color& color, const TexturePtr& texture, DrawMethod&& method,
DrawMode drawMode = DrawMode::TRIANGLES, const DrawConductor& conductor = DEFAULT_DRAW_CONDUCTOR,
void add(const Color& color, const TexturePtr& texture, DrawMethod&& method, const DrawConductor& conductor = DEFAULT_DRAW_CONDUCTOR,
const CoordsBufferPtr& coordsBuffer = nullptr);

void addAction(const std::function<void()>& action);
Expand Down
28 changes: 10 additions & 18 deletions src/framework/graphics/drawpoolmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,15 @@ void DrawPoolManager::drawObject(const DrawPool::DrawObject& obj)
{
if (obj.action) {
obj.action();
return;
}

auto& coords = !obj.coords ? m_coordsBuffer : *obj.coords;
if (!obj.coords) {
coords.clear();
for (const auto& method : obj.methods)
DrawPool::addCoords(&coords, method, obj.drawMode);
} else {
obj.state.execute();
g_painter->drawCoords(*obj.coords, DrawMode::TRIANGLES);
}

obj.state.execute();
g_painter->drawCoords(coords, obj.drawMode);
}

void DrawPoolManager::addTexturedCoordsBuffer(const TexturePtr& texture, const CoordsBufferPtr& coords, const Color& color, const DrawConductor& condutor) const
{
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{}, DrawMode::TRIANGLE_STRIP, condutor, coords);
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{}, condutor, coords);
}

void DrawPoolManager::addTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src, const Color& color, const DrawConductor& condutor) const
Expand All @@ -101,7 +93,7 @@ void DrawPoolManager::addTexturedRect(const Rect& dest, const TexturePtr& textur
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{
.type = DrawPool::DrawMethodType::RECT,
.dest = dest, .src = src
}, DrawMode::TRIANGLE_STRIP, condutor);
}, condutor);
}

void DrawPoolManager::addUpsideDownTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src, const Color& color, const DrawConductor& condutor) const
Expand All @@ -114,7 +106,7 @@ void DrawPoolManager::addUpsideDownTexturedRect(const Rect& dest, const TextureP
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{ .type = DrawPool::DrawMethodType::UPSIDEDOWN_RECT, .dest =
dest,
.src = src
}, DrawMode::TRIANGLE_STRIP, condutor);
}, condutor);
}

void DrawPoolManager::addTexturedRepeatedRect(const Rect& dest, const TexturePtr& texture, const Rect& src, const Color& color, const DrawConductor& condutor) const
Expand All @@ -127,7 +119,7 @@ void DrawPoolManager::addTexturedRepeatedRect(const Rect& dest, const TexturePtr
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{ .type = DrawPool::DrawMethodType::REPEATED_RECT, .dest =
dest,
.src = src
}, DrawMode::TRIANGLES, condutor);
}, condutor);
}

void DrawPoolManager::addFilledRect(const Rect& dest, const Color& color, const DrawConductor& condutor) const
Expand All @@ -137,7 +129,7 @@ void DrawPoolManager::addFilledRect(const Rect& dest, const Color& color, const
return;
}

getCurrentPool()->add(color, nullptr, DrawPool::DrawMethod{ .type = DrawPool::DrawMethodType::RECT, .dest = dest }, DrawMode::TRIANGLES, condutor);
getCurrentPool()->add(color, nullptr, DrawPool::DrawMethod{ .type = DrawPool::DrawMethodType::RECT, .dest = dest }, condutor);
}

void DrawPoolManager::addFilledTriangle(const Point& a, const Point& b, const Point& c, const Color& color, const DrawConductor& condutor) const
Expand All @@ -152,7 +144,7 @@ void DrawPoolManager::addFilledTriangle(const Point& a, const Point& b, const Po
.a = a,
.b = b,
.c = c
}, DrawMode::TRIANGLES, condutor);
}, condutor);
}

void DrawPoolManager::addBoundingRect(const Rect& dest, const Color& color, const uint16_t innerLineWidth, const DrawConductor& condutor) const
Expand All @@ -166,7 +158,7 @@ void DrawPoolManager::addBoundingRect(const Rect& dest, const Color& color, cons
.type = DrawPool::DrawMethodType::BOUNDING_RECT,
.dest = dest,
.intValue = innerLineWidth
}, DrawMode::TRIANGLES, condutor);
}, condutor);
}

void DrawPoolManager::preDraw(const DrawPoolType type, const std::function<void()>& f, const std::function<void()>& beforeRelease, const Rect& dest, const Rect& src, const Color& colorClear, const bool alwaysDraw)
Expand Down
1 change: 0 additions & 1 deletion src/framework/graphics/drawpoolmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class DrawPoolManager
void drawObject(const DrawPool::DrawObject& obj);
void drawPool(DrawPoolType type);

CoordsBuffer m_coordsBuffer;
std::array<DrawPool*, static_cast<uint8_t>(DrawPoolType::LAST)> m_pools{};

Size m_size;
Expand Down

0 comments on commit 01f8e20

Please sign in to comment.