Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: lighting system multithreading #570

Merged
merged 7 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 63 additions & 52 deletions src/client/lightview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,35 @@
LightView::LightView(const Size& size, const uint16_t tileSize) : m_pool(g_drawPool.get(DrawPoolType::LIGHT)) {
resize(size, tileSize);
g_mainDispatcher.addEvent([&] {
m_texture = std::make_shared<Texture>(m_mapSize);
m_texture = std::make_shared<Texture>(m_lightData.mapSize);
m_texture->setSmooth(true);
});

m_thread = std::thread([this]() {
std::unique_lock lock(m_pool->getMutex());
m_condition.wait(lock, [this]() -> bool {
updatePixels();
return m_texture == nullptr;
});
});
}

void LightView::resize(const Size& size, const uint16_t tileSize) {
m_mapSize = size;
m_tileSize = tileSize;
m_tiles.resize(size.area());
if (m_pixels.size() < 4u * m_mapSize.area())
m_pixels.resize(m_mapSize.area() * 4);
std::scoped_lock l(m_pool->getMutex());
m_lightData.mapSize = size;
m_lightData.tileSize = tileSize;
m_lightData.tiles.resize(size.area());
if (m_pixels.size() < 4u * m_lightData.mapSize.area())
m_pixels.resize(m_lightData.mapSize.area() * 4);
if (m_texture)
m_texture->setupSize(m_mapSize);

m_tileColors.clear();
m_tileColors.reserve(m_tiles.size());
for (int x = -1; ++x < m_mapSize.width();) {
for (int y = -1; ++y < m_mapSize.height();) {
const int index = (y * m_mapSize.width() + x);
const int colorIndex = index * 4;
Point pos(x * m_tileSize + m_tileSize / 2, y * m_tileSize + m_tileSize / 2);

m_tileColors.emplace_back(pos, m_tiles[index], m_pixels[colorIndex], m_pixels[colorIndex + 1],
m_pixels[colorIndex + 2], m_pixels[colorIndex + 3]);
}
}
m_texture->setupSize(m_lightData.mapSize);

g_drawPool.use(DrawPoolType::LIGHT);
g_drawPool.addAction([&] {
m_texture->updatePixels(m_pixels.data());
g_drawPool.addAction([this] {
{
std::scoped_lock l(m_pool->getMutex());
m_texture->updatePixels(m_pixels.data());
}
g_painter->resetColor();
g_painter->resetTransformMatrix();
g_painter->setTexture(m_texture.get());
Expand All @@ -74,14 +73,14 @@ void LightView::addLightSource(const Point& pos, const Light& light, float brigh
if (light.intensity == 0)
return;

if (!m_lights.empty()) {
auto& prevLight = m_lights.back();
if (!m_lightData.lights.empty()) {
auto& prevLight = m_lightData.lights.back();
if (prevLight.pos == pos && prevLight.color == light.color) {
prevLight.intensity = std::max<uint8_t>(prevLight.intensity, light.intensity);
return;
}
}
m_lights.emplace_back(pos, light.intensity, light.color, std::min<float>(brightness, g_drawPool.getOpacity()));
m_lightData.lights.emplace_back(pos, light.intensity, light.color, std::min<float>(brightness, g_drawPool.getOpacity()));

stdext::hash_union(m_updatedHash, pos.hash());
stdext::hash_combine(m_updatedHash, light.intensity);
Expand All @@ -93,17 +92,24 @@ void LightView::addLightSource(const Point& pos, const Light& light, float brigh

void LightView::resetShade(const Point& pos)
{
size_t index = (pos.y / m_tileSize) * m_mapSize.width() + (pos.x / m_tileSize);
if (index >= m_tiles.size()) return;
m_tiles[index] = m_lights.size();
size_t index = (pos.y / m_lightData.tileSize) * m_lightData.mapSize.width() + (pos.x / m_lightData.tileSize);
if (index >= m_lightData.tiles.size()) return;
m_lightData.tiles[index] = m_lightData.lights.size();
}

void LightView::draw(const Rect& dest, const Rect& src)
{
updateCoords(dest, src);
updatePixels();
m_lights.clear();
m_tiles.assign(m_mapSize.area(), {});

if (m_updatedHash != m_hash) {
m_hash = m_updatedHash;
m_updatedHash = 0;
m_threadLightData = m_lightData;
m_condition.notify_one();
}

m_lightData.lights.clear();
m_lightData.tiles.assign(m_lightData.mapSize.area(), {});
}

void LightView::updateCoords(const Rect& dest, const Rect& src) {
Expand All @@ -118,30 +124,35 @@ void LightView::updateCoords(const Rect& dest, const Rect& src) {

m_coords.clear();
m_coords.addRect(RectF(m_dest.left(), m_dest.top(), m_dest.width(), m_dest.height()),
RectF(static_cast<float>(offset.x) / m_tileSize, static_cast<float>(offset.y) / m_tileSize,
static_cast<float>(size.width()) / m_tileSize, static_cast<float>(size.height()) / m_tileSize));
RectF(static_cast<float>(offset.x) / m_lightData.tileSize, static_cast<float>(offset.y) / m_lightData.tileSize,
static_cast<float>(size.width()) / m_lightData.tileSize, static_cast<float>(size.height()) / m_lightData.tileSize));
}

void LightView::updatePixels() {
if (m_updatedHash == m_hash)
return;

const size_t lightSize = m_lights.size();
for (auto& tile : m_tileColors) {
tile.setLight(m_globalLightColor, 255);
for (size_t i = tile.shadeIndex; i < lightSize; ++i) {
const auto& light = m_lights[i];
float distance = std::sqrt((tile.pos.x - light.pos.x) * (tile.pos.x - light.pos.x) +
(tile.pos.y - light.pos.y) * (tile.pos.y - light.pos.y));
distance /= m_tileSize;

float intensity = (-distance + (light.intensity * light.brightness)) * .2f;
if (intensity < .01f) continue;
if (intensity > 1.f) intensity = 1.f;
tile.setLight(Color::from8bit(light.color, intensity));
const size_t lightSize = m_threadLightData.lights.size();

for (int x = 0; x < m_threadLightData.mapSize.width(); ++x) {
for (int y = 0; y < m_threadLightData.mapSize.height(); ++y) {
Point pos(x * m_threadLightData.tileSize + m_threadLightData.tileSize / 2, y * m_threadLightData.tileSize + m_threadLightData.tileSize / 2);
int index = (y * m_threadLightData.mapSize.width() + x);
int colorIndex = index * 4;
m_pixels[colorIndex] = m_threadLightData.globalLightColor.r();
m_pixels[colorIndex + 1] = m_threadLightData.globalLightColor.g();
m_pixels[colorIndex + 2] = m_threadLightData.globalLightColor.b();
m_pixels[colorIndex + 3] = 255; // alpha channel
for (size_t i = m_threadLightData.tiles[index]; i < lightSize; ++i) {
const auto& light = m_threadLightData.lights[i];
float distance = std::sqrt((pos.x - light.pos.x) * (pos.x - light.pos.x) +
(pos.y - light.pos.y) * (pos.y - light.pos.y));
distance /= m_threadLightData.tileSize;
float intensity = (-distance + light.intensity) * 0.2f;
if (intensity < 0.01f) continue;
if (intensity > 1.0f) intensity = 1.0f;
Color lightColor = Color::from8bit(light.color) * intensity;
m_pixels[colorIndex] = std::max<int>(m_pixels[colorIndex], lightColor.r());
m_pixels[colorIndex + 1] = std::max<int>(m_pixels[colorIndex + 1], lightColor.g());
m_pixels[colorIndex + 2] = std::max<int>(m_pixels[colorIndex + 2], lightColor.b());
}
}
}

m_hash = m_updatedHash;
m_updatedHash = 0;
}
47 changes: 13 additions & 34 deletions src/client/lightview.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
#include <framework/graphics/framebuffer.h>
#include "declarations.h"
#include "thingtype.h"
#include <thread>

class LightView : public LuaObject
{
public:
LightView(const Size& size, const uint16_t tileSize);
~LightView() { m_texture = nullptr; }
~LightView() { m_texture = nullptr; m_condition.notify_one(); m_thread.join(); }

void resize(const Size& size, uint16_t tileSize);
void draw(const Rect& dest, const Rect& src);
Expand All @@ -42,7 +43,7 @@ class LightView : public LuaObject
void setGlobalLight(const Light& light)
{
m_isDark = light.intensity < 250;
m_globalLightColor = Color::from8bit(light.color, light.intensity / static_cast<float>(UINT8_MAX));
m_lightData.globalLightColor = Color::from8bit(light.color, light.intensity / static_cast<float>(UINT8_MAX));
}

bool isDark() const { return m_isDark; }
Expand All @@ -58,53 +59,31 @@ class LightView : public LuaObject
TileLight(const Point& pos, uint8_t intensity, uint8_t color, float brightness) : Light(intensity, color), pos(pos), brightness(brightness) {}
};

struct TileColor
struct LightData
{
TileColor(Point& pos, size_t& shadeIndex, uint8_t& r, uint8_t& g, uint8_t& b, uint8_t& a) :
pos(std::move(pos)), shadeIndex(shadeIndex), r(r), g(g), b(b), a(a) {}

Point pos;
size_t& shadeIndex;

void setLight(const Color& color, uint8_t alpha) {
r = color.r();
g = color.g();
b = color.b();
a = alpha;
}

void setLight(const Color& color) {
r = std::max<int>(r, color.r());
g = std::max<int>(g, color.g());
b = std::max<int>(b, color.b());
}

private:
uint8_t& r;
uint8_t& g;
uint8_t& b;
uint8_t& a;
Size mapSize;
uint16_t tileSize{ 0 };
std::vector<size_t> tiles;
std::vector<TileLight> lights;
Color globalLightColor{ Color::white };
};

void updateCoords(const Rect& dest, const Rect& src);
void updatePixels();

bool m_isDark{ false };

uint16_t m_tileSize{ 0 };
size_t m_hash{ 0 }, m_updatedHash{ 0 };

DrawPool* m_pool{ nullptr };

Size m_mapSize;
Rect m_dest, m_src;
Color m_globalLightColor{ Color::white };

CoordsBuffer m_coords;
TexturePtr m_texture;
LightData m_lightData, m_threadLightData;

std::thread m_thread;
std::condition_variable m_condition;

std::vector<size_t> m_tiles;
std::vector<uint8_t> m_pixels;
std::vector<TileLight> m_lights;
std::vector<TileColor> m_tileColors;
};