world generation

This commit is contained in:
Kbz-8
2025-05-10 14:48:27 +02:00
parent 55fa8b2c17
commit f4aeb50f91
12 changed files with 189 additions and 67 deletions

View File

@@ -19,18 +19,14 @@ void Chunk::GenerateChunk()
{
if(p_actor)
return;
#pragma omp parallel for
for(auto& z: m_data)
{
#pragma omp parallel for
for(auto& y: z)
std::fill(y.begin(), y.end(), 0);
}
#pragma omp parallel for
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
{
#pragma omp parallel for
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
{
// Implement noise here
@@ -46,12 +42,10 @@ void Chunk::GenerateMesh()
if(p_actor)
return;
std::vector<Scop::Vertex> mesh_data;
std::vector<std::uint32_t> indices;
mesh_data.reserve(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
indices.reserve(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
std::vector<Scop::Vertex> mesh_data(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
std::vector<std::uint32_t> indices(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
std::size_t index = 0;
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
{
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
@@ -62,84 +56,83 @@ void Chunk::GenerateMesh()
continue;
if(!GetBlock(Scop::Vec3i(x, y, static_cast<std::int32_t>(z) - 1)))
{
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[0].position + Scop::Vec3f(x, y, z), BLOCK_MESH[0].normal, BLOCK_MESH[0].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[1].position + Scop::Vec3f(x, y, z), BLOCK_MESH[1].normal, BLOCK_MESH[1].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[2].position + Scop::Vec3f(x, y, z), BLOCK_MESH[2].normal, BLOCK_MESH[2].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[0].position + Scop::Vec3f(x, y, z), BLOCK_MESH[0].normal, BLOCK_MESH[0].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[1].position + Scop::Vec3f(x, y, z), BLOCK_MESH[1].normal, BLOCK_MESH[1].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[2].position + Scop::Vec3f(x, y, z), BLOCK_MESH[2].normal, BLOCK_MESH[2].uv };
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[3].position + Scop::Vec3f(x, y, z), BLOCK_MESH[3].normal, BLOCK_MESH[3].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[4].position + Scop::Vec3f(x, y, z), BLOCK_MESH[4].normal, BLOCK_MESH[4].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[5].position + Scop::Vec3f(x, y, z), BLOCK_MESH[5].normal, BLOCK_MESH[5].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[3].position + Scop::Vec3f(x, y, z), BLOCK_MESH[3].normal, BLOCK_MESH[3].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[4].position + Scop::Vec3f(x, y, z), BLOCK_MESH[4].normal, BLOCK_MESH[4].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[5].position + Scop::Vec3f(x, y, z), BLOCK_MESH[5].normal, BLOCK_MESH[5].uv };
}
if(!GetBlock(Scop::Vec3i(x, y, z + 1)))
{
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[6].position + Scop::Vec3f(x, y, z), BLOCK_MESH[6].normal, BLOCK_MESH[6].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[7].position + Scop::Vec3f(x, y, z), BLOCK_MESH[7].normal, BLOCK_MESH[7].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[8].position + Scop::Vec3f(x, y, z), BLOCK_MESH[8].normal, BLOCK_MESH[8].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[6].position + Scop::Vec3f(x, y, z), BLOCK_MESH[6].normal, BLOCK_MESH[6].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[7].position + Scop::Vec3f(x, y, z), BLOCK_MESH[7].normal, BLOCK_MESH[7].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[8].position + Scop::Vec3f(x, y, z), BLOCK_MESH[8].normal, BLOCK_MESH[8].uv };
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[9].position + Scop::Vec3f(x, y, z), BLOCK_MESH[9].normal, BLOCK_MESH[9].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[10].position + Scop::Vec3f(x, y, z), BLOCK_MESH[10].normal, BLOCK_MESH[10].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[11].position + Scop::Vec3f(x, y, z), BLOCK_MESH[11].normal, BLOCK_MESH[11].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[9].position + Scop::Vec3f(x, y, z), BLOCK_MESH[9].normal, BLOCK_MESH[9].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[10].position + Scop::Vec3f(x, y, z), BLOCK_MESH[10].normal, BLOCK_MESH[10].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[11].position + Scop::Vec3f(x, y, z), BLOCK_MESH[11].normal, BLOCK_MESH[11].uv };
}
if(!GetBlock(Scop::Vec3i(static_cast<std::int32_t>(x) - 1, y, z)))
{
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[12].position + Scop::Vec3f(x, y, z), BLOCK_MESH[12].normal, BLOCK_MESH[12].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[13].position + Scop::Vec3f(x, y, z), BLOCK_MESH[13].normal, BLOCK_MESH[13].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[14].position + Scop::Vec3f(x, y, z), BLOCK_MESH[14].normal, BLOCK_MESH[14].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[12].position + Scop::Vec3f(x, y, z), BLOCK_MESH[12].normal, BLOCK_MESH[12].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[13].position + Scop::Vec3f(x, y, z), BLOCK_MESH[13].normal, BLOCK_MESH[13].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[14].position + Scop::Vec3f(x, y, z), BLOCK_MESH[14].normal, BLOCK_MESH[14].uv };
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[15].position + Scop::Vec3f(x, y, z), BLOCK_MESH[15].normal, BLOCK_MESH[15].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[16].position + Scop::Vec3f(x, y, z), BLOCK_MESH[16].normal, BLOCK_MESH[16].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[17].position + Scop::Vec3f(x, y, z), BLOCK_MESH[17].normal, BLOCK_MESH[17].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[15].position + Scop::Vec3f(x, y, z), BLOCK_MESH[15].normal, BLOCK_MESH[15].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[16].position + Scop::Vec3f(x, y, z), BLOCK_MESH[16].normal, BLOCK_MESH[16].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[17].position + Scop::Vec3f(x, y, z), BLOCK_MESH[17].normal, BLOCK_MESH[17].uv };
}
if(!GetBlock(Scop::Vec3i(x + 1, y, z)))
{
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[18].position + Scop::Vec3f(x, y, z), BLOCK_MESH[18].normal, BLOCK_MESH[18].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[19].position + Scop::Vec3f(x, y, z), BLOCK_MESH[19].normal, BLOCK_MESH[19].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[20].position + Scop::Vec3f(x, y, z), BLOCK_MESH[20].normal, BLOCK_MESH[20].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[18].position + Scop::Vec3f(x, y, z), BLOCK_MESH[18].normal, BLOCK_MESH[18].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[19].position + Scop::Vec3f(x, y, z), BLOCK_MESH[19].normal, BLOCK_MESH[19].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[20].position + Scop::Vec3f(x, y, z), BLOCK_MESH[20].normal, BLOCK_MESH[20].uv };
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[21].position + Scop::Vec3f(x, y, z), BLOCK_MESH[21].normal, BLOCK_MESH[21].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[22].position + Scop::Vec3f(x, y, z), BLOCK_MESH[22].normal, BLOCK_MESH[22].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[23].position + Scop::Vec3f(x, y, z), BLOCK_MESH[23].normal, BLOCK_MESH[23].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[21].position + Scop::Vec3f(x, y, z), BLOCK_MESH[21].normal, BLOCK_MESH[21].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[22].position + Scop::Vec3f(x, y, z), BLOCK_MESH[22].normal, BLOCK_MESH[22].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[23].position + Scop::Vec3f(x, y, z), BLOCK_MESH[23].normal, BLOCK_MESH[23].uv };
}
if(!GetBlock(Scop::Vec3i(x, static_cast<std::int32_t>(y) - 1, z)))
{
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[24].position + Scop::Vec3f(x, y, z), BLOCK_MESH[24].normal, BLOCK_MESH[24].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[25].position + Scop::Vec3f(x, y, z), BLOCK_MESH[25].normal, BLOCK_MESH[25].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[26].position + Scop::Vec3f(x, y, z), BLOCK_MESH[26].normal, BLOCK_MESH[26].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[24].position + Scop::Vec3f(x, y, z), BLOCK_MESH[24].normal, BLOCK_MESH[24].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[25].position + Scop::Vec3f(x, y, z), BLOCK_MESH[25].normal, BLOCK_MESH[25].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[26].position + Scop::Vec3f(x, y, z), BLOCK_MESH[26].normal, BLOCK_MESH[26].uv };
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[27].position + Scop::Vec3f(x, y, z), BLOCK_MESH[27].normal, BLOCK_MESH[27].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[28].position + Scop::Vec3f(x, y, z), BLOCK_MESH[28].normal, BLOCK_MESH[28].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[29].position + Scop::Vec3f(x, y, z), BLOCK_MESH[29].normal, BLOCK_MESH[29].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[27].position + Scop::Vec3f(x, y, z), BLOCK_MESH[27].normal, BLOCK_MESH[27].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[28].position + Scop::Vec3f(x, y, z), BLOCK_MESH[28].normal, BLOCK_MESH[28].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[29].position + Scop::Vec3f(x, y, z), BLOCK_MESH[29].normal, BLOCK_MESH[29].uv };
}
if(!GetBlock(Scop::Vec3i(x, y + 1, z)))
{
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[30].position + Scop::Vec3f(x, y, z), BLOCK_MESH[30].normal, BLOCK_MESH[30].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[31].position + Scop::Vec3f(x, y, z), BLOCK_MESH[31].normal, BLOCK_MESH[31].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[32].position + Scop::Vec3f(x, y, z), BLOCK_MESH[32].normal, BLOCK_MESH[32].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[30].position + Scop::Vec3f(x, y, z), BLOCK_MESH[30].normal, BLOCK_MESH[30].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[31].position + Scop::Vec3f(x, y, z), BLOCK_MESH[31].normal, BLOCK_MESH[31].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[32].position + Scop::Vec3f(x, y, z), BLOCK_MESH[32].normal, BLOCK_MESH[32].uv };
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[33].position + Scop::Vec3f(x, y, z), BLOCK_MESH[33].normal, BLOCK_MESH[33].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[34].position + Scop::Vec3f(x, y, z), BLOCK_MESH[34].normal, BLOCK_MESH[34].uv));
mesh_data.push_back(Scop::Vertex(BLOCK_MESH[35].position + Scop::Vec3f(x, y, z), BLOCK_MESH[35].normal, BLOCK_MESH[35].uv));
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[33].position + Scop::Vec3f(x, y, z), BLOCK_MESH[33].normal, BLOCK_MESH[33].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[34].position + Scop::Vec3f(x, y, z), BLOCK_MESH[34].normal, BLOCK_MESH[34].uv };
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[35].position + Scop::Vec3f(x, y, z), BLOCK_MESH[35].normal, BLOCK_MESH[35].uv };
}
}
}
}
for(std::uint32_t i = 0; i < mesh_data.size(); i++)
indices.push_back(i);
for(std::uint32_t i = 0; i < index; i++)
indices[i] = i;
std::shared_ptr<Scop::Mesh> mesh = std::make_shared<Scop::Mesh>();
mesh->AddSubMesh({ std::move(mesh_data), std::move(indices) });
std::cout << index << std::endl;
Scop::Actor& actor = m_world.GetScene().CreateActor(mesh);
actor.GetModelRef().SetMaterial(m_world.GetBlockMaterial(), 0);
actor.SetScale(Scop::Vec3f{ 2.0f });
actor.SetPosition(Scop::Vec3f(m_position.x, 0.0f, m_position.y));
p_actor = &actor;
p_actor = m_world.GetMeshPool().RequestActor(m_world.GetScene());
p_actor->GetModelRef().GetMesh()->GetSubMesh(0).SetData(mesh_data, indices, index);
p_actor->GetModelRef().SetMaterial(m_world.GetBlockMaterial(), 0);
p_actor->SetScale(Scop::Vec3f{ 2.0f });
p_actor->SetPosition(Scop::Vec3f(m_position.x, 0.0f, m_position.y));
}
std::uint32_t Chunk::GetBlock(Scop::Vec3i position) const noexcept
@@ -150,3 +143,8 @@ std::uint32_t Chunk::GetBlock(Scop::Vec3i position) const noexcept
return m_data[position.x][position.z][position.y];
return 0;
}
Chunk::~Chunk()
{
m_world.GetMeshPool().ReturnToPool(p_actor);
}

View File

@@ -17,7 +17,7 @@ class Chunk
[[nodiscard]] std::uint32_t GetBlock(Scop::Vec3i position) const noexcept;
[[nodiscard]] inline Scop::NonOwningPtr<Scop::Actor> GetActor() const noexcept { return p_actor; }
~Chunk() = default;
~Chunk();
private:
std::vector<std::vector<std::vector<std::uint32_t>>> m_data; // Should be a flat array but I cannot manage to flatten the 3D coordinates correctly

35
Application/MeshPool.cpp git.filemode.normal_file
View File

@@ -0,0 +1,35 @@
#include <algorithm>
#include <MeshPool.h>
#include <World.h>
MeshPool::MeshPool() : m_actors(4 * (RENDER_DISTANCE * RENDER_DISTANCE)) {}
Scop::NonOwningPtr<Scop::Actor> MeshPool::RequestActor(Scop::Scene& scene)
{
for(auto& [free, actor] : m_actors)
{
if(free)
{
free = false;
actor->SetVisibility(true);
return actor;
}
}
std::vector<Scop::Vertex> mesh_data(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
std::vector<std::uint32_t> indices(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
std::shared_ptr<Scop::Mesh> mesh = std::make_shared<Scop::Mesh>();
mesh->AddSubMesh({ std::move(mesh_data), std::move(indices) });
m_actors.push_back(std::make_pair(false, &scene.CreateActor(mesh)));
return m_actors.back().second;
}
void MeshPool::ReturnToPool(Scop::NonOwningPtr<Scop::Actor> actor)
{
auto it = std::find_if(m_actors.begin(), m_actors.end(), [actor](const auto& lhs) { return lhs.second == actor; });
if(it == m_actors.end())
return;
it->first = true;
it->second->SetVisibility(false);
}

20
Application/MeshPool.h git.filemode.normal_file
View File

@@ -0,0 +1,20 @@
#ifndef MESH_POOL_H
#define MESH_POOL_H
#include <vector>
#include <ScopGraphics.h>
class MeshPool
{
public:
MeshPool();
Scop::NonOwningPtr<Scop::Actor> RequestActor(Scop::Scene& scene);
void ReturnToPool(Scop::NonOwningPtr<Scop::Actor> actor);
~MeshPool() = default;
private:
std::vector<std::pair<bool, Scop::NonOwningPtr<Scop::Actor>>> m_actors;
};
#endif

View File

@@ -7,6 +7,9 @@
#include <Chunk.h>
#include <Utils.h>
#include <MeshPool.h>
constexpr std::uint8_t RENDER_DISTANCE = 2;
class World
{
@@ -16,6 +19,7 @@ class World
[[nodiscard]] inline Scop::Scene& GetScene() noexcept { return m_scene; }
[[nodiscard]] inline std::shared_ptr<Scop::Material> GetBlockMaterial() const { return p_block_material; }
[[nodiscard]] Scop::NonOwningPtr<Chunk> GetChunk(Scop::Vec2i position);
[[nodiscard]] inline MeshPool& GetMeshPool() { return m_mesh_pool; }
~World() = default;
@@ -23,8 +27,8 @@ class World
void GenerateWorld();
private:
static constexpr std::uint8_t RENDER_DISTANCE = 2;
std::unordered_map<Scop::Vec2i, Chunk> m_chunks;
MeshPool m_mesh_pool;
std::shared_ptr<Scop::Material> p_block_material;
Scop::Narrator& m_narrator;
Scop::Scene& m_scene;

View File

@@ -24,6 +24,7 @@ namespace Scop
inline void SetPosition(Vec3f position) noexcept { m_position = position; }
inline void SetScale(Vec3f scale) noexcept { m_scale = scale; }
inline void SetOrientation(Quatf orientation) noexcept { m_orientation = orientation; }
inline void SetVisibility(bool show) noexcept { m_is_visible = show; }
[[nodiscard]] inline const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] inline const Vec3f& GetPosition() const noexcept { return m_position; }
@@ -32,6 +33,7 @@ namespace Scop
[[nodiscard]] inline const Model& GetModel() const noexcept { return m_model; }
[[nodiscard]] inline Model& GetModelRef() noexcept { return m_model; }
[[nodiscard]] inline std::uint32_t GetUUID() const noexcept { return m_uuid; }
[[nodiscard]] inline bool IsVisible() const noexcept { return m_is_visible; }
~Actor();
@@ -46,6 +48,7 @@ namespace Scop
Vec3f m_scale = Vec3f{ 1.0f, 1.0f, 1.0f };
std::shared_ptr<ActorScript> p_script;
std::uint64_t m_uuid;
bool m_is_visible = true;
};
}

View File

@@ -20,14 +20,28 @@ namespace Scop
std::size_t index_size;
std::size_t triangle_count = 0;
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices, std::size_t index_size = 0)
{
CPUBuffer data(vertices.size() * sizeof(Vertex) + indices.size() * sizeof(std::uint32_t));
std::memcpy(data.GetData(), vertices.data(), vertices.size() * sizeof(Vertex));
std::memcpy(data.GetData() + vertices.size() * sizeof(Vertex), indices.data(), indices.size() * sizeof(std::uint32_t));
buffer.Init(vertices.size() * sizeof(Vertex), indices.size() * sizeof(std::uint32_t), 0, std::move(data));
triangle_count = vertices.size() / 3;
index_size = indices.size();
this->index_size = index_size == 0 ? indices.size() : index_size;
triangle_count = index_size / 3;
}
inline void SetData(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices, std::size_t index_size = 0)
{
CPUBuffer vertex_data(vertices.size() * sizeof(Scop::Vertex));
std::memcpy(vertex_data.GetData(), vertices.data(), vertex_data.GetSize());
CPUBuffer index_data(indices.size() * sizeof(std::uint32_t));
std::memcpy(index_data.GetData(), indices.data(), index_data.GetSize());
buffer.SetVertexData(std::move(vertex_data));
buffer.SetVertexData(std::move(index_data));
this->index_size = index_size == 0 ? indices.size() : index_size;
triangle_count = index_size / 3;
}
};
@@ -41,6 +55,12 @@ namespace Scop
inline void AddSubMesh(SubMesh mesh) { m_sub_meshes.emplace_back(std::move(mesh)); }
[[nodiscard]] inline SubMesh& GetSubMesh(std::size_t index) { return m_sub_meshes.at(index); }
inline void Reset()
{
for(auto& mesh : m_sub_meshes)
mesh.buffer.Destroy();
m_sub_meshes.clear();
}
~Mesh();

View File

@@ -27,6 +27,7 @@ namespace Scop
[[nodiscard]] inline std::shared_ptr<Material> GetMaterial(std::size_t mesh_index) { return m_materials[mesh_index]; }
[[nodiscard]] inline std::vector<std::shared_ptr<Material>>& GetAllMaterials() { return m_materials; }
[[nodiscard]] inline Vec3f GetCenter() const noexcept { return m_center; }
[[nodiscard]] inline std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
void Draw(VkCommandBuffer cmd, const DescriptorSet& matrices_set, const class GraphicPipeline& pipeline, DescriptorSet& set, std::size_t& drawcalls, std::size_t& polygondrawn, std::size_t frame_index) const;

View File

@@ -18,7 +18,7 @@ namespace Scop
void Init(BufferType type, VkDeviceSize size, VkBufferUsageFlags usage, CPUBuffer data, std::string_view name = {});
void Destroy() noexcept;
bool CopyFrom(const GPUBuffer& buffer) noexcept;
bool CopyFrom(const GPUBuffer& buffer, std::size_t src_offset = 0, std::size_t dst_offset = 0) noexcept;
void Swap(GPUBuffer& buffer) noexcept;
@@ -79,7 +79,8 @@ namespace Scop
m_index_offset = vertex_size;
GPUBuffer::Init(BufferType::LowDynamic, vertex_size + index_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | additional_flags, std::move(data), std::move(name));
}
// Full flemme de faire les fonctions SetData
void SetVertexData(CPUBuffer data);
void SetIndexData(CPUBuffer data);
inline void BindVertex(VkCommandBuffer cmd) const noexcept { RenderCore::Get().vkCmdBindVertexBuffers(cmd, 0, 1, &m_buffer, &m_vertex_offset); }
inline void BindIndex(VkCommandBuffer cmd) const noexcept { RenderCore::Get().vkCmdBindIndexBuffer(cmd, m_buffer, m_index_offset, VK_INDEX_TYPE_UINT32); }

View File

@@ -82,7 +82,7 @@ namespace Scop
s_buffer_count++;
}
bool GPUBuffer::CopyFrom(const GPUBuffer& buffer) noexcept
bool GPUBuffer::CopyFrom(const GPUBuffer& buffer, std::size_t src_offset, std::size_t dst_offset) noexcept
{
if(!(m_usage & VK_BUFFER_USAGE_TRANSFER_DST_BIT))
{
@@ -97,7 +97,7 @@ namespace Scop
VkCommandBuffer cmd = kvfCreateCommandBuffer(RenderCore::Get().GetDevice());
kvfBeginCommandBuffer(cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
kvfCopyBufferToBuffer(cmd, m_buffer, buffer.Get(), buffer.GetSize());
kvfCopyBufferToBuffer(cmd, m_buffer, buffer.Get(), buffer.GetSize(), src_offset, dst_offset);
kvfEndCommandBuffer(cmd);
VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice());
kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence);
@@ -144,7 +144,7 @@ namespace Scop
{
if(data.GetSize() > m_memory.size)
{
Error("Vulkan: trying to store to much data in a vertex buffer (% bytes in % bytes)", data.GetSize(), m_memory.size);
Error("Vulkan: trying to store too much data in a vertex buffer (% bytes in % bytes)", data.GetSize(), m_memory.size);
return;
}
if(data.Empty())
@@ -162,7 +162,7 @@ namespace Scop
{
if(data.GetSize() > m_memory.size)
{
Error("Vulkan: trying to store to much data in an index buffer (% bytes in % bytes)", data.GetSize(), m_memory.size);
Error("Vulkan: trying to store too much data in an index buffer (% bytes in % bytes)", data.GetSize(), m_memory.size);
return;
}
if(data.Empty())
@@ -176,6 +176,42 @@ namespace Scop
staging.Destroy();
}
void MeshBuffer::SetVertexData(CPUBuffer data)
{
if(data.GetSize() > m_index_offset)
{
Error("Vulkan: trying to store too much data in a vertex buffer (% bytes in % bytes)", data.GetSize(), m_index_offset);
return;
}
if(data.Empty())
{
Warning("Vulkan: cannot set empty data in a vertex buffer");
return;
}
GPUBuffer staging;
staging.Init(BufferType::Staging, data.GetSize(), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, data);
CopyFrom(staging, 0, 0);
staging.Destroy();
}
void MeshBuffer::SetIndexData(CPUBuffer data)
{
if(data.GetSize() > m_memory.size - m_index_offset)
{
Error("Vulkan: trying to store too much data in an index buffer (% bytes in % bytes)", data.GetSize(), m_memory.size - m_index_offset);
return;
}
if(data.Empty())
{
Warning("Vulkan: cannot set empty data in an index buffer");
return;
}
GPUBuffer staging;
staging.Init(BufferType::Staging, data.GetSize(), VK_BUFFER_USAGE_INDEX_BUFFER_BIT, data);
CopyFrom(staging, 0, m_index_offset);
staging.Destroy();
}
void UniformBuffer::Init(std::uint32_t size, std::string_view name)
{
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)

View File

@@ -45,6 +45,8 @@ namespace Scop
pipeline.BindPipeline(cmd, 0, {});
for(auto actor : scene.GetActors())
{
if(!actor->IsVisible())
continue;
ModelData model_data;
model_data.model_mat = Mat4f::Identity();
model_data.model_mat.SetTranslation(actor->GetPosition() - actor->GetModel().GetCenter());

View File

@@ -159,7 +159,7 @@ VkSampler kvfCreateSampler(VkDevice device, VkFilter filters, VkSamplerAddressMo
void kvfDestroySampler(VkDevice device, VkSampler sampler);
VkBuffer kvfCreateBuffer(VkDevice device, VkBufferUsageFlags usage, VkDeviceSize size);
void kvfCopyBufferToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkBuffer src, size_t size);
void kvfCopyBufferToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkBuffer src, size_t size, size_t src_offset, size_t dst_offset);
void kvfCopyBufferToImage(VkCommandBuffer cmd, VkImage dst, VkBuffer src, size_t buffer_offset, VkImageAspectFlagBits aspect, VkExtent3D extent);
void kvfDestroyBuffer(VkDevice device, VkBuffer buffer);
@@ -2278,7 +2278,7 @@ VkBuffer kvfCreateBuffer(VkDevice device, VkBufferUsageFlags usage, VkDeviceSize
return buffer;
}
void kvfCopyBufferToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkBuffer src, size_t size)
void kvfCopyBufferToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkBuffer src, size_t size, size_t src_offset, size_t dst_offset)
{
KVF_ASSERT(cmd != VK_NULL_HANDLE);
KVF_ASSERT(dst != VK_NULL_HANDLE);
@@ -2289,6 +2289,8 @@ void kvfCopyBufferToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkBuffer src, size
#endif
VkBufferCopy copy_region = {};
copy_region.size = size;
copy_region.srcOffset = src_offset;
copy_region.dstOffset = dst_offset;
KVF_GET_DEVICE_FUNCTION(vkCmdCopyBuffer)(cmd, src, dst, 1, &copy_region);
}