Génération de Chunks en multithreads

This commit is contained in:
MarcEricMartel 2021-12-12 23:31:38 -05:00
parent f4ec4816af
commit 7eb9f44d41
8 changed files with 223 additions and 115 deletions

1
.gitignore vendored
View File

@ -367,3 +367,4 @@ FodyWeavers.xsd
/SQCSim2021/Release
/SQCSim2021/media/chunks
/x64/Release/SQCSim2021.exe
/SQCSim2021/x64/Release/SQCSim2021.tlog

View File

@ -1,13 +1,73 @@
#include "chunk.h"
#include "world.h"
Chunk::Chunk(int x, int y) : m_posX(x), m_posY(y) { m_blocks.Reset(BTYPE_AIR); }
Chunk::Chunk(int x, int y) : m_posX(x), m_posY(y) {
std::ostringstream pos; // Vérifie l'existence d'un fichier .chunk avec sa position.
pos << CHUNK_PATH << x << '_' << y << ".chunk";
std::ifstream input(pos.str(), std::fstream::binary);
Chunk::Chunk(int x, int y, char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]) : m_posX(x), m_posY(y) {
for (int x = 0; x < CHUNK_SIZE_X; ++x)
for (int z = 0; z < CHUNK_SIZE_Z; ++z)
for (int y = 0; y < CHUNK_SIZE_Y; ++y)
m_blocks.Set(x, y, z, data[x + (z * CHUNK_SIZE_X) + (y * CHUNK_SIZE_Z * CHUNK_SIZE_X)]);
if (input.fail()) {
Perlin perlin = Perlin(8, 45.f, 7.f, PERLIN_SEED);
m_blocks.Reset(BTYPE_AIR);
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Montagnes
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
float xnoiz, ynoiz;
xnoiz = (double)(ix + x * CHUNK_SIZE_X) / (double)INT16_MAX;
ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / (double)INT16_MAX;
float height = (perlin.Get(xnoiz, ynoiz)) * 20.f + 5.f;
for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy)
SetBlock(ix, iy, iz, BTYPE_METAL, nullptr);
}
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Collines
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
float xnoiz, ynoiz;
xnoiz = (double)(ix + x * CHUNK_SIZE_X) / (double)INT16_MAX;
ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / (double)INT16_MAX;
float height = perlin.Get(xnoiz, ynoiz) * 5.f + 24.f;
for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy) {
if (GetBlock(ix, iy, iz) == BTYPE_AIR)
SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr);
}
}
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Lacs"
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
for (int iy = 0; iy < 13; ++iy) {
if (GetBlock(ix, iy, iz) == BTYPE_AIR)
SetBlock(ix, iy, iz, BTYPE_ICE, nullptr);
}
}
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Arbres"
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
float xnoiz, ynoiz;
xnoiz = (double)(iz * CHUNK_SIZE_Y + x * CHUNK_SIZE_X) / (double)INT16_MAX;
ynoiz = (double)(ix * CHUNK_SIZE_Y + y * CHUNK_SIZE_Z) / (double)INT16_MAX;
bool tree = (int)(abs(perlin.Get(xnoiz, ynoiz)) * 17933.f) % CHUNK_SIZE_Y > 126 ? true : false;
for (int iy = 0; iy < CHUNK_SIZE_Y - 10; ++iy)
if (GetBlock(ix, iy, iz) == BTYPE_AIR)
if (GetBlock(ix, iy - 1, iz) == BTYPE_GRASS)
if (tree) {
for (int i = 0; i < (int)(abs(perlin.Get(xnoiz, ynoiz))) % 3 + 1; ++i)
SetBlock(ix, iy + i, iz, BTYPE_DIRT, nullptr);
break;
}
}
}
else {
input.seekg(0, std::ios_base::end);
int size = input.tellg();
input.seekg(0, std::ios_base::beg);
char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
input.read(data, size);
input.close();
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix)
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz)
for (int iy = 0; iy < CHUNK_SIZE_Y; ++iy)
m_blocks.Set(ix, iy, iz, data[ix + (iz * CHUNK_SIZE_X) + (iy * CHUNK_SIZE_Z * CHUNK_SIZE_X)]);
}
}
Chunk::~Chunk() {
@ -36,7 +96,7 @@ void Chunk::RemoveBlock(int x, int y, int z, World* world) {
void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
m_blocks.Set(x, y, z, type);
CheckNeighbors(x, z, world);
if (world) CheckNeighbors(x, z, world); // Si nullptr, ne pas vérifier les chunks voisines.
m_isDirty = true;
}
@ -99,7 +159,6 @@ void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST], World* world) {
void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt,
int x, int y, int z, float u, float v, float s, World* world) {
static Perlin perlin = Perlin(2, 4.f, 1.f, 362436);
int cex, cey;
world->GetScope(cex, cey);

View File

@ -22,7 +22,6 @@ class Chunk {
public:
Chunk(int x, int y);
Chunk(int x, int y, char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]);
~Chunk();
void RemoveBlock(int x, int y, int z, World* world);

View File

@ -16,6 +16,7 @@
#define CHUNK_SIZE_Y 128
#define CHUNK_SIZE_Z 16
#define MAX_SELECTION_DISTANCE 5
#define PERLIN_SEED 12345
#ifdef _DEBUG
#define WORLD_SIZE_X 64
@ -25,6 +26,9 @@
#define FRAMES_UPDATE_CHUNKS 4
#define FRAMES_DELETE_CHUNKS 4
#define THREADS_GENERATE_CHUNKS 1
#define THREADS_UPDATE_CHUNKS 1
#define VIEW_DISTANCE 256
#define TEXTURE_SIZE 128
#define MAX_BULLETS 64
@ -38,6 +42,9 @@
#define FRAMES_UPDATE_CHUNKS 1
#define FRAMES_DELETE_CHUNKS 1
#define THREADS_GENERATE_CHUNKS 10
#define THREADS_UPDATE_CHUNKS 10
#define VIEW_DISTANCE 1024
#define TEXTURE_SIZE 512
#define MAX_BULLETS 512

View File

@ -16,7 +16,10 @@ bool VertexBuffer::IsValid() const {
return m_isValid;
}
std::mutex VertexBuffer::m_opgl;
void VertexBuffer::SetMeshData(VertexData* vd, int vertexCount) {
const std::lock_guard<std::mutex> prout(VertexBuffer::m_opgl);
assert(vertexCount <= USHRT_MAX);
if(vertexCount == 0)
return;

View File

@ -2,6 +2,7 @@
#define VERTEXBUFFER_H__
#include "define.h"
#include <mutex>
class VertexBuffer
{
@ -33,6 +34,7 @@ class VertexBuffer
private:
static std::mutex m_opgl;
bool m_isValid;
int m_vertexCount;
GLuint m_vertexVboId;

View File

@ -113,109 +113,19 @@ void World::Update(int& rendercount, Bullet* bullets[MAX_BULLETS], Player& playe
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
atlas.Bind();
RenderWorld(rendercount, player, world, shader);
TransposeWorld(player, bullets);
UpdateWorld(player, perlin, blockinfo);
TransposeWorld(player, bullets);
shader.Disable();
glStencilFunc(GL_GREATER, 1, 0xFF);
}
bool World::GenerateChunk(int chx, int chy, Perlin& perlin) {
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
chx >= 0 && chy >= 0)
if (!ChunkAt(chx, 1, chy)) {
for (int index = 0; index < m_tbDeleted.size(); ++index) { // Vérifie l'existence d'un chunk dans le buffer de suppression avec sa position.
int x, y;
if (m_tbDeleted.at(index)) {
m_tbDeleted.at(index)->GetPosition(x, y);
if (chx / CHUNK_SIZE_X + m_center[0] == x &&
chy / CHUNK_SIZE_Z + m_center[1] == y) {
GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, std::move(m_tbDeleted.at(index)));
return true;
}
}
}
std::ostringstream pos; // Vérifie l'existence d'un fichier .chunk avec sa position.
pos << CHUNK_PATH << chx / CHUNK_SIZE_X + m_center[0] << '_' << chy / CHUNK_SIZE_Z + m_center[1] << ".chunk";
std::ifstream input(pos.str(), std::fstream::binary);
if (input.fail()) {
GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, new Chunk(chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]));
Chunk* chunk = GetChunks().Get(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z);
for (int x = 0; x < CHUNK_SIZE_X; ++x) // Montagnes
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
float xnoiz, ynoiz;
xnoiz = (double)(x + (chx / CHUNK_SIZE_X + m_center[0]) * CHUNK_SIZE_X) / (double)INT16_MAX;
ynoiz = (double)(z + (chy / CHUNK_SIZE_Z + m_center[1]) * CHUNK_SIZE_Z) / (double)INT16_MAX;
float height = (perlin.Get(xnoiz, ynoiz)) * 20.f + 5.f;
for (int y = 0; y <= (int)height % CHUNK_SIZE_Y; ++y)
chunk->SetBlock(x, y, z, BTYPE_METAL, this);
}
for (int x = 0; x < CHUNK_SIZE_X; ++x) // Collines
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
float xnoiz, ynoiz;
xnoiz = (double)(x + (chx / CHUNK_SIZE_X + m_center[0]) * CHUNK_SIZE_X) / (double)INT16_MAX;
ynoiz = (double)(z + (chy / CHUNK_SIZE_Z + m_center[1]) * CHUNK_SIZE_Z) / (double)INT16_MAX;
float height = perlin.Get(xnoiz, ynoiz) * 5.f + 24.f;
for (int y = 0; y <= (int)height % CHUNK_SIZE_Y; ++y) {
if (chunk->GetBlock(x, y, z) == BTYPE_AIR)
chunk->SetBlock(x, y, z, BTYPE_GRASS, this);
}
}
for (int x = 0; x < CHUNK_SIZE_X; ++x) // "Lacs"
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
for (int y = 0; y < 13; ++y) {
if (chunk->GetBlock(x, y, z) == BTYPE_AIR)
chunk->SetBlock(x, y, z, BTYPE_ICE, this);
}
}
for (int x = 0; x < CHUNK_SIZE_X; ++x) // "Arbres"
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
float xnoiz, ynoiz;
xnoiz = (double)(z * CHUNK_SIZE_Y + (chx / CHUNK_SIZE_X + m_center[0]) * CHUNK_SIZE_X) / (double)INT16_MAX;
ynoiz = (double)(x * CHUNK_SIZE_Y + (chy / CHUNK_SIZE_Z + m_center[1]) * CHUNK_SIZE_Z) / (double)INT16_MAX;
bool tree = (int)(abs(perlin.Get(xnoiz, ynoiz)) * 17933.f) % CHUNK_SIZE_Y > 126 ? true: false;
for (int y = 0; y < CHUNK_SIZE_Y - 10; ++y)
if (chunk->GetBlock(x, y, z) == BTYPE_AIR)
if (chunk->GetBlock(x, y - 1, z) == BTYPE_GRASS)
if (tree) {
for (int i = 0; i < (int)(abs(perlin.Get(xnoiz, ynoiz))) % 3 + 1; ++i)
chunk->SetBlock(x, y + i, z, BTYPE_DIRT, this);
break;
}
}
}
else {
input.seekg(0, std::ios_base::end);
int size = input.tellg();
input.seekg(0, std::ios_base::beg);
char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
input.read(data, size);
input.close();
GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, new Chunk(chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1], data));
}
return true;
}
return false;
}
void World::UpdateChunk(int& generates, int& updates, int chx, int chy, Perlin& perlin, BlockInfo* blockinfo[BTYPE_LAST]) {
void World::UpdateChunk(int& updates, int chx, int chy, BlockInfo* blockinfo[BTYPE_LAST]) {
if (updates == 0 && ChunkAt(chx, 1, chy) &&
ChunkAt(chx, 1, chy)->IsDirty()) {
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
updates = FRAMES_UPDATE_CHUNKS;
}
if (generates == 0 && GenerateChunk(chx, chy, perlin)) generates = FRAMES_RENDER_CHUNKS;
}
void World::ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block) {
@ -377,26 +287,152 @@ void World::UpdateWorld(Player& player, Perlin& perlin, BlockInfo* blockinfo[BTY
static int frameUpdate = 0;
static int frameDelete = 0;
int side = 0;
int threads = 0;
std::future<Chunk*> genThList[THREADS_GENERATE_CHUNKS];
//std::future<Chunk*> updateThList[THREADS_UPDATE_CHUNKS];
if (frameGenerate > 0) --frameGenerate;
if (frameUpdate > 0) --frameUpdate;
if (frameDelete > 0) --frameDelete;
if (!frameGenerate || !frameUpdate)
if (!frameGenerate)
while (side * CHUNK_SIZE_X <= VIEW_DISTANCE * 2) {
int tx = -side, ty = -side;
int chx = 0;
int chy = 0;
for (; tx <= side; ++tx)
UpdateChunk(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z, perlin, blockinfo);
for (; ty <= side; ++ty)
UpdateChunk(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z, perlin, blockinfo);
for (; tx >= -side; --tx)
UpdateChunk(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z, perlin, blockinfo);
for (; ty >= -side; --ty)
UpdateChunk(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z, perlin, blockinfo);
for (; tx <= side; ++tx) {
if (frameGenerate)
break;
chx = cx + tx * CHUNK_SIZE_X;
chy = cy + ty * CHUNK_SIZE_Z;
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
genThList[threads++] = std::async(std::launch::async, [](int x, int y) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]);
if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
}
for (; ty <= side; ++ty) {
if (frameGenerate)
break;
chx = cx + tx * CHUNK_SIZE_X;
chy = cy + ty * CHUNK_SIZE_Z;
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
genThList[threads++] = std::async(std::launch::async, [](int x, int y) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]);
if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
}
for (; tx >= -side; --tx) {
if (frameGenerate)
break;
chx = cx + tx * CHUNK_SIZE_X;
chy = cy + ty * CHUNK_SIZE_Z;
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
genThList[threads++] = std::async(std::launch::async, [](int x, int y) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]);
if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
}
for (; ty >= -side; --ty) {
if (frameGenerate)
break;
chx = cx + tx * CHUNK_SIZE_X;
chy = cy + ty * CHUNK_SIZE_Z;
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
genThList[threads++] = std::async(std::launch::async, [](int x, int y) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]);
if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
}
if (frameGenerate)
break;
++side;
}
if (threads > 0) {
for (int i = 0; i < threads; ++i)
genThList[i].wait();
for (int i = 0; i < threads; ++i) {
int x, y;
Chunk* chunk = genThList[i].get();
chunk->GetPosition(x,y);
m_chunks.Set(x - m_center[0], y - m_center[1], chunk);
}
}
side = 0;
threads = 0;
if (!frameUpdate)
while (side * CHUNK_SIZE_X <= VIEW_DISTANCE * 2) {
int tx = -side, ty = -side;
for (; tx <= side; ++tx) {
if (frameUpdate)
break;
int chx = cx + tx * CHUNK_SIZE_X;
int chy = cy + ty * CHUNK_SIZE_Z;
if (ChunkAt(chx, 1, chy) &&
ChunkAt(chx, 1, chy)->IsDirty()) {
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
if (threads++ == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
}
}
for (; ty <= side; ++ty) {
if (frameUpdate)
break;
int chx = cx + tx * CHUNK_SIZE_X;
int chy = cy + ty * CHUNK_SIZE_Z;
if (ChunkAt(chx, 1, chy) &&
ChunkAt(chx, 1, chy)->IsDirty()) {
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
if (threads++ == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
}
}
for (; tx >= -side; --tx) {
if (frameUpdate)
break;
int chx = cx + tx * CHUNK_SIZE_X;
int chy = cy + ty * CHUNK_SIZE_Z;
if (ChunkAt(chx, 1, chy) &&
ChunkAt(chx, 1, chy)->IsDirty()) {
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
if (threads++ == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
}
}
for (; ty >= -side; --ty) {
if (frameUpdate)
break;
int chx = cx + tx * CHUNK_SIZE_X;
int chy = cy + ty * CHUNK_SIZE_Z;
if (ChunkAt(chx, 1, chy) &&
ChunkAt(chx, 1, chy)->IsDirty()) {
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
if (threads++ == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
}
/*if (ChunkAt(chx, 1, chy) && // Version multithread d'UpdateChunks qui ne fonctionne pas.
ChunkAt(chx, 1, chy)->IsDirty()) {
updateThList[threads++] =
std::async(std::launch::async,
[](Chunk* chunk, BlockInfo* blockinfo[BTYPE_LAST], World* world) {
chunk->Update(blockinfo, world); return chunk; }, ChunkAt(chx, 1, chy), blockinfo, this);
if (threads == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
}*/
}
if (frameUpdate)
break;
++side;
}
/*if (threads > 0) {
for (int i = 0; i < threads; ++i)
updateThList[i].wait();
for (int i = 0; i < threads; ++i) {
int x, y;
Chunk* chunk = updateThList[i].get();
chunk->GetPosition(x, y);
m_chunks.Set(x - m_center[0], y - m_center[1], chunk);
}
}*/
CleanUpWorld(frameDelete);
}

View File

@ -13,6 +13,8 @@
#include <fstream>
#include <string>
#include <vector>
#include <future>
#include <thread>
class Chunk;
class Player;
@ -44,8 +46,7 @@ private:
int m_center[2] = { INT16_MAX / 2 - WORLD_SIZE_X / 2, INT16_MAX / 2 - WORLD_SIZE_Y / 2 };
bool GenerateChunk(int x, int y, Perlin& perlin);
void UpdateChunk(int& generates, int& updates, int chx, int chy, Perlin& perlin, BlockInfo* blockinfo[BTYPE_LAST]);
void UpdateChunk(int& updates, int chx, int chy, BlockInfo* blockinfo[BTYPE_LAST]);
void RenderWorld(int& rendercount, Player& player, Transformation& world, Shader& shader);
void UpdateWorld(Player& player, Perlin& perlin, BlockInfo* blockinfo[BTYPE_LAST]);
void TransposeWorld(Player& player, Bullet* bullets[MAX_BULLETS]);