Skip to content

Commit

Permalink
gh-81: mesh manager for gpu mesh data
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Oct 20, 2024
1 parent 36e9f30 commit fb698e3
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 118 deletions.
19 changes: 9 additions & 10 deletions engine/plugins/assimp/assimp_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,21 @@ namespace wmoge {
return StatusCode::Error;
}

MeshManager* mesh_manager = context.ioc->resolve_value<MeshManager>();

MeshFlags flags;
flags.set(MeshFlag::FromDisk);

asset = mesh_manager->create_mesh(flags);
asset->set_id(asset_id);

MeshBuilder& builder = importer.get_builder();
builder.set_mesh(asset);
if (!builder.build()) {
WG_LOG_ERROR("failed to build mesh " << asset_id);
return StatusCode::Error;
}

mesh_manager->init_mesh(asset.get());
MeshDesc& desc = builder.get_mesh();
desc.flags.set(MeshFlag::FromDisk);

MeshManager* mesh_manager = context.ioc->resolve_value<MeshManager>();

asset = mesh_manager->create_mesh(desc);
asset->set_id(asset_id);

mesh_manager->queue_mesh_upload(asset.get());

return WG_OK;
}
Expand Down
17 changes: 10 additions & 7 deletions engine/runtime/core/string_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace wmoge {
class StringStorage {
public:
void get_or_create(const std::string& key, const std::string*& str);
static StringStorage& instance();
static StringStorage& instance(StridPool pool);

private:
struct Entry {
Expand All @@ -69,22 +69,25 @@ namespace wmoge {
str = &entry->str;
}

StringStorage& StringStorage::instance() {
static StringStorage g_storage;
return g_storage;
StringStorage& StringStorage::instance(StridPool pool) {
static StringStorage g_storage[static_cast<int>(StridPool::Max)];
return g_storage[static_cast<int>(pool)];
}

Strid::Strid() {
static std::string g_empty;
m_string = &g_empty;
}

Strid::Strid(const char* string) : Strid(std::string(string)) {
Strid::Strid(const char* string) : Strid(std::string(string), StridPool::Release) {
}
Strid::Strid(const std::string& string) : Strid() {
Strid::Strid(const std::string& string) : Strid(string, StridPool::Release) {
}

Strid::Strid(const std::string& string, StridPool pool) {
if (string.empty())
return;
StringStorage::instance().get_or_create(string, m_string);
StringStorage::instance(pool).get_or_create(string, m_string);
}

bool Strid::operator==(const Strid& other) const {
Expand Down
10 changes: 10 additions & 0 deletions engine/runtime/core/string_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@

namespace wmoge {

/** @brief Pool to allocate strid, separated for profiling purposes */
enum class StridPool {
Release = 0,
Debug,
Max
};

/**
* @class Strid
* @brief Interned globally-stored string based ids for fast lookups
Expand All @@ -44,6 +51,7 @@ namespace wmoge {
Strid();
Strid(const char* string);
Strid(const std::string& string);
Strid(const std::string& string, StridPool pool);

bool operator==(const Strid& other) const;
bool operator!=(const Strid& other) const;
Expand All @@ -68,6 +76,8 @@ namespace wmoge {

#define SID(id) ::wmoge::Strid(id)

#define SIDDBG(id) ::wmoge::Strid(id, ::wmoge::StridPool::Debug)

}// namespace wmoge

namespace std {
Expand Down
3 changes: 3 additions & 0 deletions engine/runtime/gfx/vulkan/vk_cmd_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,14 @@ namespace wmoge {
buffered_vector<VkPipelineStageFlags> wait_flags;

for (CmdBufferQueue& queue : m_queues) {
WG_PROFILE_CPU_VULKAN("submit_queue");
std::size_t num_commands = queue.submits.size();

const bool is_graphics_queue = queue.queue_type == GfxQueueType::Graphics;

for (std::size_t i = 0; i < num_commands; i++) {
WG_PROFILE_CPU_VULKAN("submit_cmd_buffer");

CmdBufferSubmitInfo& submit = queue.submits[i];

if (is_graphics_queue) {
Expand Down
5 changes: 3 additions & 2 deletions engine/runtime/grc/texture_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ namespace wmoge {
const GfxTextureDesc desc = texture->get_desc();

if (is_pooled) {
texture->set_texture(m_pool->allocate(desc, texture->get_name()));
texture->set_texture(m_pool->allocate(desc, SIDDBG(texture->get_name().str())));
} else {
texture->set_texture(m_gfx_driver->make_texture(desc, texture->get_name()));
texture->set_texture(m_gfx_driver->make_texture(desc, SIDDBG(texture->get_name().str())));
}

texture->set_texture_callback(m_callback);
Expand All @@ -298,6 +298,7 @@ namespace wmoge {
}

void TextureManager::upload_texture(Texture* texture, const GfxCmdListRef& cmd) {
WG_PROFILE_CPU_GRC("TextureManager::upload_texture");
WG_PROFILE_GPU_SCOPE(cmd, "TextureManager::upload_texture");

assert(texture);
Expand Down
5 changes: 2 additions & 3 deletions engine/runtime/grc/texture_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ namespace wmoge {
Texture::CallbackRef m_callback;
std::unique_ptr<TexturePool> m_pool;
bool m_need_upload_default = true;

Ref<Texture> m_default_textures[int(DefaultTexture::Total)];
Ref<GfxSampler> m_default_samplers[int(DefaultSampler::Total)];
Ref<Texture> m_default_textures[int(DefaultTexture::Total)];
Ref<GfxSampler> m_default_samplers[int(DefaultSampler::Total)];

GfxDriver* m_gfx_driver;

Expand Down
59 changes: 35 additions & 24 deletions engine/runtime/mesh/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,23 @@

namespace wmoge {

Mesh::Mesh(MeshFlags flags) {
m_flags = flags;
Mesh::~Mesh() {
if (m_callback) {
(*m_callback)(this);
}
}

void Mesh::add_chunk(const MeshChunk& mesh_chunk, const Ref<ArrayMesh>& mesh) {
m_chunks.push_back(mesh_chunk);
m_array_meshes.push_back(mesh);
}
void Mesh::add_vertex_buffer(Ref<Data> buffer) {
m_vertex_buffers.push_back(std::move(buffer));
}
void Mesh::add_index_buffer(Ref<Data> buffer) {
m_index_buffers.push_back(std::move(buffer));
}
void Mesh::add_vert_stream(const MeshVertStream& stream) {
m_vert_streams.push_back(stream);
}
void Mesh::add_intex_stream(const MeshIndexStream& stream) {
m_index_streams.push_back(stream);
}
void Mesh::set_aabb(const Aabbf& aabb) {
m_aabb = aabb;
Mesh::Mesh(MeshDesc& mesh_desc) {
m_chunks = std::move(mesh_desc.chunks);
m_array_meshes = std::move(mesh_desc.array_meshes);
m_vertex_buffers = std::move(mesh_desc.vertex_buffers);
m_index_buffers = std::move(mesh_desc.index_buffers);
m_vert_streams = std::move(mesh_desc.vert_streams);
m_index_streams = std::move(mesh_desc.index_streams);
m_aabb = mesh_desc.aabb;
m_flags = mesh_desc.flags;
}

void Mesh::set_mesh_callback(CallbackRef callback) {
m_callback = std::move(callback);
}
Expand All @@ -66,6 +60,11 @@ namespace wmoge {
m_gfx_index_buffers = std::move(gfx_index_buffers);
}

void Mesh::release_gfx_buffers() {
m_gfx_vertex_buffers.clear();
m_gfx_index_buffers.clear();
}

GfxVertBuffersSetup Mesh::get_vert_buffers_setup(int chunk_id) const {
GfxVertBuffersSetup setup;
const MeshChunk& chunk = get_chunk(chunk_id);
Expand Down Expand Up @@ -94,23 +93,35 @@ namespace wmoge {
array_view<const Ref<ArrayMesh>> Mesh::get_array_meshes() const {
return m_array_meshes;
}
array_view<const Ref<Data>> Mesh::get_vertex_buffers() const {
return m_vertex_buffers;
}
array_view<const Ref<Data>> Mesh::get_index_buffers() const {
return m_index_buffers;
}
array_view<const Ref<GfxVertBuffer>> Mesh::get_gfx_vertex_buffers() const {
return m_gfx_vertex_buffers;
}
array_view<const Ref<GfxIndexBuffer>> Mesh::get_gfx_index_buffers() const {
return m_gfx_index_buffers;
}
const MeshChunk& Mesh::get_chunk(int i) const {
assert(i < m_chunks.size());
return m_chunks[i];
}
const Ref<GfxVertBuffer>& Mesh::get_gfx_vertex_buffers(int i) const {
const Ref<GfxVertBuffer>& Mesh::get_gfx_vertex_buffer(int i) const {
assert(i < m_gfx_vertex_buffers.size());
return m_gfx_vertex_buffers[i];
}
const Ref<GfxIndexBuffer>& Mesh::get_gfx_index_buffers(int i) const {
const Ref<GfxIndexBuffer>& Mesh::get_gfx_index_buffer(int i) const {
assert(i < m_gfx_vertex_buffers.size());
return m_gfx_index_buffers[i];
}
const MeshVertStream& Mesh::get_vert_streams(int i) const {
const MeshVertStream& Mesh::get_vert_stream(int i) const {
assert(i < m_gfx_vertex_buffers.size());
return m_vert_streams[i];
}
const MeshIndexStream& Mesh::get_index_streams(int i) const {
const MeshIndexStream& Mesh::get_index_stream(int i) const {
assert(i < m_gfx_vertex_buffers.size());
return m_index_streams[i];
}
Expand Down
53 changes: 27 additions & 26 deletions engine/runtime/mesh/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ namespace wmoge {
WG_RTTI_END;

/**
* @class MeshData
* @class MeshDesc
* @brief Struct used to serialize mesh asset data
*/
struct MeshData {
WG_RTTI_STRUCT(MeshData);
struct MeshDesc {
WG_RTTI_STRUCT(MeshDesc);

std::vector<MeshChunk> chunks;
std::vector<Ref<ArrayMesh>> array_meshes;
Expand All @@ -100,9 +100,10 @@ namespace wmoge {
std::vector<MeshVertStream> vert_streams;
std::vector<MeshIndexStream> index_streams;
Aabbf aabb;
MeshFlags flags;
};

WG_RTTI_STRUCT_BEGIN(MeshData) {
WG_RTTI_STRUCT_BEGIN(MeshDesc) {
WG_RTTI_META_DATA();
WG_RTTI_FIELD(chunks, {});
WG_RTTI_FIELD(array_meshes, {});
Expand All @@ -111,6 +112,7 @@ namespace wmoge {
WG_RTTI_FIELD(vert_streams, {});
WG_RTTI_FIELD(index_streams, {});
WG_RTTI_FIELD(aabb, {});
WG_RTTI_FIELD(flags, {});
}
WG_RTTI_END;

Expand All @@ -125,33 +127,32 @@ namespace wmoge {
using Callback = std::function<void(Mesh*)>;
using CallbackRef = std::shared_ptr<Callback>;

Mesh() = default;
~Mesh() override = default;
Mesh() = default;
~Mesh() override;

Mesh(MeshFlags flags);
Mesh(MeshDesc& mesh_desc);

void add_chunk(const MeshChunk& mesh_chunk, const Ref<ArrayMesh>& mesh);
void add_vertex_buffer(Ref<Data> buffer);
void add_index_buffer(Ref<Data> buffer);
void add_vert_stream(const MeshVertStream& stream);
void add_intex_stream(const MeshIndexStream& stream);
void set_aabb(const Aabbf& aabb);
void set_mesh_callback(CallbackRef callback);
void set_gfx_vertex_buffers(std::vector<Ref<GfxVertBuffer>> gfx_vertex_buffers);
void set_gfx_index_buffers(std::vector<Ref<GfxIndexBuffer>> gfx_index_buffers);

[[nodiscard]] GfxVertBuffersSetup get_vert_buffers_setup(int chunk_id) const;
[[nodiscard]] GfxIndexBufferSetup get_index_buffer_setup(int chunk_id) const;
[[nodiscard]] array_view<const MeshChunk> get_chunks() const;
[[nodiscard]] array_view<const Ref<ArrayMesh>> get_array_meshes() const;
[[nodiscard]] const MeshChunk& get_chunk(int i) const;
[[nodiscard]] const Ref<GfxVertBuffer>& get_gfx_vertex_buffers(int i) const;
[[nodiscard]] const Ref<GfxIndexBuffer>& get_gfx_index_buffers(int i) const;
[[nodiscard]] const MeshVertStream& get_vert_streams(int i) const;
[[nodiscard]] const MeshIndexStream& get_index_streams(int i) const;
[[nodiscard]] const Aabbf& get_aabb() const;
[[nodiscard]] const MeshFlags& get_flags() const;
[[nodiscard]] GfxMemUsage get_mem_usage() const;
void release_gfx_buffers();

[[nodiscard]] GfxVertBuffersSetup get_vert_buffers_setup(int chunk_id) const;
[[nodiscard]] GfxIndexBufferSetup get_index_buffer_setup(int chunk_id) const;
[[nodiscard]] array_view<const MeshChunk> get_chunks() const;
[[nodiscard]] array_view<const Ref<ArrayMesh>> get_array_meshes() const;
[[nodiscard]] array_view<const Ref<Data>> get_vertex_buffers() const;
[[nodiscard]] array_view<const Ref<Data>> get_index_buffers() const;
[[nodiscard]] array_view<const Ref<GfxVertBuffer>> get_gfx_vertex_buffers() const;
[[nodiscard]] array_view<const Ref<GfxIndexBuffer>> get_gfx_index_buffers() const;
[[nodiscard]] const MeshChunk& get_chunk(int i) const;
[[nodiscard]] const Ref<GfxVertBuffer>& get_gfx_vertex_buffer(int i) const;
[[nodiscard]] const Ref<GfxIndexBuffer>& get_gfx_index_buffer(int i) const;
[[nodiscard]] const MeshVertStream& get_vert_stream(int i) const;
[[nodiscard]] const MeshIndexStream& get_index_stream(int i) const;
[[nodiscard]] const Aabbf& get_aabb() const;
[[nodiscard]] const MeshFlags& get_flags() const;
[[nodiscard]] GfxMemUsage get_mem_usage() const;

private:
std::vector<MeshChunk> m_chunks;
Expand Down
20 changes: 7 additions & 13 deletions engine/runtime/mesh/mesh_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@

namespace wmoge {

void MeshBuilder::set_mesh(Ref<Mesh> mesh) {
m_mesh = std::move(mesh);
}

void MeshBuilder::add_chunk(const Strid& name, const Ref<ArrayMesh>& data) {
assert(data);

Expand All @@ -50,8 +46,6 @@ namespace wmoge {
Status MeshBuilder::build() {
WG_PROFILE_CPU_MESH("MeshBuilder::build");

assert(m_mesh);

const GfxVertAttribs attribs_stream1 = {GfxVertAttrib::Pos3f, GfxVertAttrib::Pos2f, GfxVertAttrib::Norm3f, GfxVertAttrib::Tang3f};
const GfxVertAttribs attribs_stream2 = {GfxVertAttrib::BoneIds4i, GfxVertAttrib::BoneWeights4f};
const GfxVertAttribs attribs_stream3 = {GfxVertAttrib::Col04f, GfxVertAttrib::Col14f, GfxVertAttrib::Col24f, GfxVertAttrib::Col34f, GfxVertAttrib::Uv02f, GfxVertAttrib::Uv12f, GfxVertAttrib::Uv22f, GfxVertAttrib::Uv32f};
Expand Down Expand Up @@ -80,7 +74,6 @@ namespace wmoge {
m_chunks[i]->pack_faces(index_data, index_stream);

index_stream.buffer = curr_index_buffer;
m_mesh->add_intex_stream(index_stream);

MeshChunk chunk;
chunk.name = m_chunks_names[i];
Expand All @@ -94,14 +87,15 @@ namespace wmoge {

aabb = aabb.join(chunk.aabb);

m_mesh->add_chunk(chunk, m_chunks[i]);

m_mesh->add_vertex_buffer(vert_data);
m_mesh->add_index_buffer(index_data);
m_mesh.chunks.push_back(chunk);
m_mesh.array_meshes.push_back(m_chunks[i]);
m_mesh.vertex_buffers.push_back(vert_data);
m_mesh.index_buffers.push_back(index_data);

for (auto& vert_stream : vert_streams) {
m_mesh->add_vert_stream(vert_stream);
m_mesh.vert_streams.push_back(vert_stream);
}
m_mesh.index_streams.push_back(index_stream);

curr_index_stream += 1;
curr_vert_stream += int(vert_streams.size());
Expand All @@ -110,7 +104,7 @@ namespace wmoge {
curr_index_buffer += 1;
}

m_mesh->set_aabb(aabb);
m_mesh.aabb = aabb;

return WG_OK;
}
Expand Down
Loading

0 comments on commit fb698e3

Please sign in to comment.