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

multi-threaded vertex loading #47

Merged
merged 16 commits into from
Oct 31, 2024
Merged
Prev Previous commit
Next Next commit
builder maintenance
beaumanvienna committed Oct 19, 2024
commit 5a567b8585f80ae71b8ff105c605653bcc81853c
161 changes: 64 additions & 97 deletions engine/renderer/builder/builder.cpp
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@
#include "renderer/builder/builder.h"
#include "renderer/model.h"


#include "VKmodel.h"

namespace std
@@ -52,117 +51,88 @@ namespace GfxRenderEngine
{
m_Vertices.clear();
m_Indices.clear();

m_Vertices.reserve(4);
m_Indices.reserve(6);
// 0 - 1
// | / |
// 3 - 2

Vertex vertex[4];

// index 0, 0.0f, 1.0f
vertex[0] = {/*pos*/ {-1.0f, 1.0f, 0.0f},
/*col*/ {0.0f, 0.1f, 0.9f, 1.0f},
/*norm*/ {0.0f, 0.0f, 1.0f},
/*uv*/ {sprite.m_Pos1X, sprite.m_Pos1Y},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)};

m_Vertices.emplace_back(/*pos*/ glm::vec3{-1.0f, 1.0f, 0.0f},
/*col*/ glm::vec4{0.0f, 0.1f, 0.9f, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, 1.0f},
/*uv*/ glm::vec2{sprite.m_Pos1X, sprite.m_Pos1Y});
// index 1, 1.0f, 1.0f
vertex[1] = {/*pos*/ {1.0f, 1.0f, 0.0f},
/*col*/ {0.0f, 0.1f, 0.9f, 1.0f},
/*norm*/ {0.0f, 0.0f, 1.0f},
/*uv*/ {sprite.m_Pos2X, sprite.m_Pos1Y},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)};

m_Vertices.emplace_back(/*pos*/ glm::vec3{1.0f, 1.0f, 0.0f},
/*col*/ glm::vec4{0.0f, 0.1f, 0.9f, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, 1.0f},
/*uv*/ glm::vec2{sprite.m_Pos2X, sprite.m_Pos1Y});
// index 2, 1.0f, 0.0f
vertex[2] = {/*pos*/ {1.0f, -1.0f, 0.0f},
/*col*/ {0.0f, 0.9f, 0.1f, 1.0f},
/*norm*/ {0.0f, 0.0f, 1.0f},
/*uv*/ {sprite.m_Pos2X, sprite.m_Pos2Y},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)};

m_Vertices.emplace_back(/*pos*/ glm::vec3{1.0f, -1.0f, 0.0f},
/*col*/ glm::vec4{0.0f, 0.9f, 0.1f, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, 1.0f},
/*uv*/ glm::vec2{sprite.m_Pos2X, sprite.m_Pos2Y});
// index 3, 0.0f, 0.0f
vertex[3] = {/*pos*/ {-1.0f, -1.0f, 0.0f},
/*col*/ {0.0f, 0.9f, 0.1f, 1.0f},
/*norm*/ {0.0f, 0.0f, 1.0f},
/*uv*/ {sprite.m_Pos1X, sprite.m_Pos2Y},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)};

for (int i = 0; i < 4; i++)
m_Vertices.push_back(vertex[i]);

m_Indices.push_back(0);
m_Indices.push_back(1);
m_Indices.push_back(3);
m_Indices.push_back(1);
m_Indices.push_back(2);
m_Indices.push_back(3);
m_Vertices.emplace_back(/*pos*/ glm::vec3{-1.0f, -1.0f, 0.0f},
/*col*/ glm::vec4{0.0f, 0.9f, 0.1f, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, 1.0f},
/*uv*/ glm::vec2{sprite.m_Pos1X, sprite.m_Pos2Y});

m_Indices.emplace_back(0);
m_Indices.emplace_back(1);
m_Indices.emplace_back(3);
m_Indices.emplace_back(1);
m_Indices.emplace_back(2);
m_Indices.emplace_back(3);
}

void Builder::LoadParticle(glm::vec4 const& color)
{
m_Vertices.clear();
m_Indices.clear();

m_Vertices.reserve(4);
m_Indices.reserve(6);

// 0 - 1
// | / |
// 3 - 2

Vertex vertex[4]{// index 0, 0.0f, 1.0f
{/*pos*/ {-1.0f, 1.0f, 0.0f},
{color.x, color.y, color.z, 1.0f},
/*norm*/ {0.0f, 0.0f, -1.0f},
/*uv*/ {0.0f, 1.0f},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)},

// index 1, 1.0f, 1.0f
{/*pos*/ {1.0f, 1.0f, 0.0f},
{color.x, color.y, color.z, 1.0f},
/*norm*/ {0.0f, 0.0f, -1.0f},
/*uv*/ {1.0f, 1.0f},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)},

// index 2, 1.0f, 0.0f
{/*pos*/ {1.0f, -1.0f, 0.0f},
{color.x, color.y, color.z, 1.0f},
/*norm*/ {0.0f, 0.0f, -1.0f},
/*uv*/ {1.0f, 0.0f},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)},

// index 3, 0.0f, 0.0f
{/*pos*/ {-1.0f, -1.0f, 0.0f},
{color.x, color.y, color.z, 1.0f},
/*norm*/ {0.0f, 0.0f, -1.0f},
/*uv*/ {0.0f, 0.0f},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)}};
for (int i = 0; i < 4; i++)
m_Vertices.push_back(vertex[i]);

m_Indices.push_back(0);
m_Indices.push_back(1);
m_Indices.push_back(3);
m_Indices.push_back(1);
m_Indices.push_back(2);
m_Indices.push_back(3);
// index 0, 0.0f, 1.0f
m_Vertices.emplace_back(/*pos*/ glm::vec3{-1.0f, 1.0f, 0.0f},
/*col*/ glm::vec4{color.x, color.y, color.z, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, -1.0f},
/*uv*/ glm::vec2{0.0f, 1.0f});

// index 1, 1.0f, 1.0f
m_Vertices.emplace_back(/*pos*/ glm::vec3{1.0f, 1.0f, 0.0f},
/*col*/ glm::vec4{color.x, color.y, color.z, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, -1.0f},
/*uv*/ glm::vec2{1.0f, 1.0f});

// index 2, 1.0f, 0.0f
m_Vertices.emplace_back(/*pos*/ glm::vec3{1.0f, -1.0f, 0.0f},
/*col*/ glm::vec4{color.x, color.y, color.z, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, -1.0f},
/*uv*/ glm::vec2{1.0f, 0.0f});

// index 3, 0.0f, 0.0f
m_Vertices.emplace_back(/*pos*/ glm::vec3{-1.0f, -1.0f, 0.0f},
/*col*/ glm::vec4{color.x, color.y, color.z, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, -1.0f},
/*uv*/ glm::vec2{0.0f, 0.0f});

m_Indices.emplace_back(0);
m_Indices.emplace_back(1);
m_Indices.emplace_back(3);
m_Indices.emplace_back(1);
m_Indices.emplace_back(2);
m_Indices.emplace_back(3);
}

entt::entity Builder::LoadCubemap(std::vector<std::string> const& faces, Registry& registry)
{
ZoneScopedN("Builder::LoadCubemap()");
entt::entity entity;
static constexpr uint VERTEX_COUNT = 36;

@@ -191,16 +161,13 @@ namespace GfxRenderEngine

// create vertices
{
m_Vertices.reserve(VERTEX_COUNT);
for (uint i = 0; i < VERTEX_COUNT; i++)
{
Vertex vertex = {/*pos*/ cubemapVertices[i],
/*col*/ {0.0f, 0.0f, 0.0f, 1.0f},
/*norm*/ {0.0f, 0.0f, 0.0f},
/*uv*/ {0.0f, 0.0f},
/*tangent*/ glm::vec3(0.0),
glm::ivec4(0.0),
glm::vec4(0.0f)};
m_Vertices.push_back(vertex);
m_Vertices.emplace_back(/*pos*/ std::move(cubemapVertices[i]),
/*col*/ glm::vec4{0.0f, 0.0f, 0.0f, 1.0f},
/*norm*/ glm::vec3{0.0f, 0.0f, 0.0f},
/*uv*/ glm::vec2{0.0f, 0.0f});
}
}

2 changes: 1 addition & 1 deletion engine/renderer/builder/builder.h
Original file line number Diff line number Diff line change
@@ -49,8 +49,8 @@ namespace GfxRenderEngine
void CalculateTangentsFromIndexBuffer(std::vector<uint> const& indices);

public:
std::vector<uint> m_Indices{};
std::vector<Vertex> m_Vertices{};
std::vector<uint> m_Indices{};
std::vector<Submesh> m_Submeshes{};

// cubemap
30 changes: 30 additions & 0 deletions engine/renderer/model.h
Original file line number Diff line number Diff line change
@@ -56,6 +56,36 @@ namespace GfxRenderEngine
{
struct Vertex // 3D, with animation
{

// default
Vertex()
: m_Position{0.0f}, m_Color{0.0f}, m_Normal{0.0f}, m_UV{0.0f}, m_Tangent{0.0f}, m_JointIds{0}, m_Weights{0.0f}
{
}

// copy
Vertex(glm::vec3& position, glm::vec4& color, glm::vec3& normal, glm::vec2& uv, glm::vec3& tangent,
glm::ivec4& jointIds, glm::vec4& weights)
: m_Position{position}, m_Color{color}, m_Normal{normal}, m_UV{uv}, m_Tangent{tangent}, m_JointIds{jointIds},
m_Weights{weights}
{
}

// move
Vertex(glm::vec3&& position, glm::vec4&& color, glm::vec3&& normal, glm::vec2&& uv, glm::vec3&& tangent,
glm::ivec4&& jointIds, glm::vec4&& weights)
: m_Position{std::move(position)}, m_Color{std::move(color)}, m_Normal{std::move(normal)}, m_UV{std::move(uv)},
m_Tangent{std::move(tangent)}, m_JointIds{std::move(jointIds)}, m_Weights{std::move(weights)}
{
}

// move with some default args
Vertex(glm::vec3&& position, glm::vec4&& color, glm::vec3&& normal, glm::vec2&& uv)
: m_Position{std::move(position)}, m_Color{std::move(color)}, m_Normal{std::move(normal)}, m_UV{std::move(uv)},
m_Tangent{0.0f}, m_JointIds{0}, m_Weights{0.0f}
{
}

glm::vec3 m_Position; // layout(location = 0)
glm::vec4 m_Color; // layout(location = 1)
glm::vec3 m_Normal; // layout(location = 2)