Skip to content

Commit

Permalink
ADD link between the block update and render
Browse files Browse the repository at this point in the history
  • Loading branch information
SaumonDesMers authored and afaure42 committed Mar 2, 2024
1 parent 5c633a1 commit dd5ab9c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 41 deletions.
23 changes: 12 additions & 11 deletions src/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ Application::Application():
m_window("Vox", 800, 600),
m_vulkan_api(m_window.getGLFWwindow()),
m_render_thread(m_settings, m_vulkan_api, m_world_scene, m_start_time),
m_update_thread(m_settings, m_window, m_world_scene, m_start_time)
m_update_thread(m_settings, m_window, m_world_scene, m_start_time),
m_block_update_thread(m_world_scene, m_vulkan_api)
{
LOG_INFO("Application::Application()");

m_world_scene.camera().setPosition(glm::vec3(15.0f, 15.0f, 15.0f));
m_world_scene.camera().lookAt(glm::vec3(0.0f, 0.0f, 0.0f));

int size = 10;
for (int x = -size; x < size; x++)
{
for (int z = -size; z < size; z++)
{
// 2d circular sine wave
int y = 3 * sin(0.4 * sqrt(x * x + z * z));
m_world_scene.addMeshData(1, WorldScene::Transform(glm::vec3(x, y, z)));
}
}
// int size = 10;
// for (int x = -size; x < size; x++)
// {
// for (int z = -size; z < size; z++)
// {
// // 2d circular sine wave
// int y = 3 * sin(0.4 * sqrt(x * x + z * z));
// m_world_scene.addMeshData(1, WorldScene::Transform(glm::vec3(x, y, z)));
// }
// }
}

Application::~Application()
Expand Down
32 changes: 31 additions & 1 deletion src/app/threads/block_update/BlockUpdateThread.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#include "BlockUpdateThread.hpp"

BlockUpdateThread::BlockUpdateThread()
BlockUpdateThread::BlockUpdateThread(
WorldScene & worldScene,
VulkanAPI & vulkanAPI
):
m_worldScene(worldScene),
m_vulkanAPI(vulkanAPI
)
{
}

Expand All @@ -10,6 +16,30 @@ BlockUpdateThread::~BlockUpdateThread()

void BlockUpdateThread::init()
{
for (int x = 0; x < 10; x++)
{
for (int z = 0; z < 10; z++)
{
for (int y = 0; y < 10; y++)
{
m_world.chunks().insert(
std::make_pair(
glm::vec3(x, y, z),
m_world.m_worldGenerator.generateChunk(x, y, z)
)
);
}
}
}

for (auto & [position, chunk] : m_world.chunks())
{
uint64_t mesh_id = m_vulkanAPI.createMesh(chunk);
if (mesh_id != VulkanAPI::no_mesh_id)
{
m_worldScene.addMeshData(mesh_id, glm::vec3(position * CHUNK_SIZE));
}
}
}

void BlockUpdateThread::loop()
Expand Down
9 changes: 8 additions & 1 deletion src/app/threads/block_update/BlockUpdateThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "define.hpp"
#include "AThreadWrapper.hpp"
#include "World.hpp"
#include "WorldScene.hpp"
#include "VulkanAPI.hpp"

/**
* @brief An implementation of the thread wrapper for the thread that handles block updates
Expand All @@ -18,7 +20,10 @@ class BlockUpdateThread : public AThreadWrapper
{
public:

BlockUpdateThread();
BlockUpdateThread(
WorldScene & worldScene,
VulkanAPI & vulkanAPI
);
~BlockUpdateThread();

BlockUpdateThread(BlockUpdateThread& other) = delete;
Expand All @@ -28,6 +33,8 @@ class BlockUpdateThread : public AThreadWrapper
private:

World m_world;
WorldScene & m_worldScene;
VulkanAPI & m_vulkanAPI;

/**
* @brief WIP
Expand Down
59 changes: 32 additions & 27 deletions src/app/threads/render/RenderThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ RenderThread::~RenderThread()

void RenderThread::init()
{
for (int x = 0; x < 10; x++)
{
for (int z = 0; z < 10; z++)
{
for (int y = 0; y < 10; y++)
{
m_chunks.emplace_back(
std::make_pair(
glm::vec3(x, y, z),
world_generator.generateChunk(x, y, z)
)
);
}
}
}
// for (int x = 0; x < 10; x++)
// {
// for (int z = 0; z < 10; z++)
// {
// for (int y = 0; y < 10; y++)
// {
// m_chunks.emplace_back(
// std::make_pair(
// glm::vec3(x, y, z),
// world_generator.generateChunk(x, y, z)
// )
// );
// }
// }
// }

for (auto & [position, chunk] : m_chunks)
{
uint64_t mesh_id = vk.createMesh(chunk);
if (mesh_id != VulkanAPI::no_mesh_id)
{
m_chunks_to_draw.push_back({ position, mesh_id });
}
}
// for (auto & [position, chunk] : m_chunks)
// {
// uint64_t mesh_id = vk.createMesh(chunk);
// if (mesh_id != VulkanAPI::no_mesh_id)
// {
// m_chunks_to_draw.push_back({ position, mesh_id });
// }
// }
}

void RenderThread::loop()
Expand Down Expand Up @@ -92,6 +92,8 @@ void RenderThread::loop()
// #
//############################################################################################################

std::lock_guard<std::mutex> lock(vk.global_mutex);

vkWaitForFences(vk.device, 1, &vk.in_flight_fences[vk.current_frame], VK_TRUE, std::numeric_limits<uint64_t>::max());
vkResetFences(vk.device, 1, &vk.in_flight_fences[vk.current_frame]);

Expand Down Expand Up @@ -148,18 +150,21 @@ void RenderThread::loop()
);

m_triangle_count = 0;

auto mesh_render_data = m_world_scene.getMeshRenderData();

for (auto & [position, mesh_id] : m_chunks_to_draw)
for (auto & mesh_data : mesh_render_data)
{
VkBuffer vertex_buffers[] = { vk.meshes[mesh_id].buffer };
VkBuffer vertex_buffers[] = { vk.meshes[mesh_data.id].buffer };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(vk.render_command_buffers[vk.current_frame], 0, 1, vertex_buffers, offsets);

vkCmdBindIndexBuffer(vk.render_command_buffers[vk.current_frame], vk.meshes[mesh_id].buffer, vk.meshes[mesh_id].index_offset, VK_INDEX_TYPE_UINT32);
vkCmdBindIndexBuffer(vk.render_command_buffers[vk.current_frame], vk.meshes[mesh_data.id].buffer, vk.meshes[mesh_data.id].index_offset, VK_INDEX_TYPE_UINT32);

// LOG_DEBUG("Drawing chunk at position: " << position.x << " " << position.y << " " << position.z);
ModelMatrice model_matrice = {};
model_matrice.model = glm::translate(glm::mat4(1.0f), position * static_cast<float>(CHUNK_SIZE));
// model_matrice.model = glm::translate(glm::mat4(1.0f), position * static_cast<float>(CHUNK_SIZE));
model_matrice.model = mesh_data.transform.model();
vkCmdPushConstants(
vk.render_command_buffers[vk.current_frame],
vk.pipeline_layout,
Expand Down
1 change: 1 addition & 0 deletions src/app/vulkan/VulkanAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ void VulkanAPI::createDrawImage()

uint64_t VulkanAPI::createMesh(const Chunk & chunk)
{
std::lock_guard<std::mutex> lock(global_mutex);
#define ADD_INDEX \
indices.push_back(vertices.size() - 4); \
indices.push_back(vertices.size() - 3); \
Expand Down
4 changes: 4 additions & 0 deletions src/app/vulkan/VulkanAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <stdexcept>
#include <vector>
#include <optional>
#include <unordered_map>
#include <mutex>

// #define NDEBUG

Expand Down Expand Up @@ -289,6 +291,8 @@ class VulkanAPI
static const uint64_t no_mesh_id = 0;
std::unordered_map<uint64_t, Mesh> meshes;

std::mutex global_mutex;


private:

Expand Down
5 changes: 4 additions & 1 deletion src/app/world/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ class World
~World();

void update(glm::vec3 playerPosition);
private:

std::unordered_map<glm::ivec3, Chunk> & chunks() { return m_chunks; }

WorldGenerator m_worldGenerator;
private:
std::unordered_map<glm::ivec3, Chunk> m_chunks;
std::unordered_set<glm::ivec3> m_visible_chunks;
};

0 comments on commit dd5ab9c

Please sign in to comment.