From 378e0c12a7732be11d382a77621551d59c97011f Mon Sep 17 00:00:00 2001 From: ouczbs Date: Thu, 28 Mar 2024 22:10:55 +0800 Subject: [PATCH] update ytl lib --- engine/3rdparty/xmake.lua | 3 +- engine/3rdparty/ylt/test/struct_pack.cpp | 58 ++++++++++++++++++ engine/3rdparty/ylt/xmake.lua | 12 ++++ engine/3rdparty/zlib/include/zstd/parray.h | 50 ++++++++++++++++ .../3rdparty/zlib/include/zstd/pool_static.h | 59 ------------------- .../src/engine/asset/render/asset_struct.cpp | 4 +- engine/src/engine/asset/render/asset_struct.h | 15 ----- engine/src/engine/render/asset/mesh.cpp | 2 +- engine/src/engine/render/asset/mesh.h | 8 ++- engine/src/engine/render/asset/model.cpp | 8 ++- engine/src/engine/render/wrapper/vertex.cpp | 8 +++ engine/src/engine/render/wrapper/vertex.h | 27 ++++++++- engine/src/engine/vulkanapi/vulkanapi.cpp | 14 ++--- 13 files changed, 174 insertions(+), 94 deletions(-) create mode 100644 engine/3rdparty/ylt/test/struct_pack.cpp create mode 100644 engine/3rdparty/ylt/xmake.lua create mode 100644 engine/3rdparty/zlib/include/zstd/parray.h delete mode 100644 engine/3rdparty/zlib/include/zstd/pool_static.h diff --git a/engine/3rdparty/xmake.lua b/engine/3rdparty/xmake.lua index 64086c2..f460306 100644 --- a/engine/3rdparty/xmake.lua +++ b/engine/3rdparty/xmake.lua @@ -4,4 +4,5 @@ add_requires("spdlog") add_requires("tinyobjloader") add_requires("vulkansdk") add_requires("assimp") -add_requires("nlohmann_json") \ No newline at end of file +add_requires("nlohmann_json") +add_requires("ylt") \ No newline at end of file diff --git a/engine/3rdparty/ylt/test/struct_pack.cpp b/engine/3rdparty/ylt/test/struct_pack.cpp new file mode 100644 index 0000000..5a120e6 --- /dev/null +++ b/engine/3rdparty/ylt/test/struct_pack.cpp @@ -0,0 +1,58 @@ +#include +#include +using namespace std; +using namespace struct_pack; +using UniversalVectorType = detail::UniversalVectorType; +using UniversalType = detail::UniversalType; +using UniversalOptionalType = detail::UniversalOptionalType; +using UniversalIntegralType = detail::UniversalIntegralType; +using UniversalNullptrType = detail::UniversalNullptrType; +using UniversalCompatibleType = detail::UniversalCompatibleType; +struct person { + int64_t id; + std::string name; + int age; + double salary; +}; +template +struct is_constructable_impl : std::false_type {}; + +template +struct is_constructable_impl < T, construct_param_t, + std::void_t< + decltype(T{ {Args{}}..., {construct_param_t{}} }) > , Args... > + : std::true_type {}; + +template +bool is_constructable = is_constructable_impl::value; + +template +std::size_t members_count_impl() { + if (is_constructable) { + cout << "is_constructable UniversalVectorType \n"; + //return members_count_impl(); + } + else if (is_constructable) { + cout << "is_constructable UniversalType \n"; + //return members_count_impl(); + } + else { + return sizeof...(Args); + } + return sizeof...(Args); +} +int main() { + person person1{ .id = 1, .name = "hello struct pack", .age = 20, .salary = 1024.42 }; + + using type = remove_cvref_t; + using type2 = remove_cvref_t; + auto Count = members_count; + int isc = members_count_impl(); + int c2 = detail::members_count(); + // 1行代码序列化 + vector buffer = serialize(person1); + + // 只反序列化person的第2个字段 + auto name = get_field(buffer.data(), buffer.size()); + cout << "hello " << name.value() << "Count == " << Count << c2; +} \ No newline at end of file diff --git a/engine/3rdparty/ylt/xmake.lua b/engine/3rdparty/ylt/xmake.lua new file mode 100644 index 0000000..4e74fe9 --- /dev/null +++ b/engine/3rdparty/ylt/xmake.lua @@ -0,0 +1,12 @@ +package("ylt") + set_kind("library", {headeronly = true}) + set_urls("https://github.com/alibaba/yalantinglibs.git") + add_includedirs("include") + on_install(function (package) + os.cp("include", package:installdir()) + end) + +target("ylt_struct_pack") + set_kind("binary") + add_packages("ylt") + add_files("test/struct_pack.cpp") \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/zstd/parray.h b/engine/3rdparty/zlib/include/zstd/parray.h new file mode 100644 index 0000000..e44b36a --- /dev/null +++ b/engine/3rdparty/zlib/include/zstd/parray.h @@ -0,0 +1,50 @@ +#pragma once +#include +namespace zstd { + template + class parray { + protected: + uint32_t m_size; + uint32_t m_head{0}; + uint32_t m_tsize; + char* m_buf; + public: + ~parray() { + if (m_buf) { + free(m_buf); + } + } + parray(uint32_t size = 1, uint32_t tsize = sizeof(T)) : m_buf((char*)malloc(tsize * size)), m_size(size) , m_tsize(tsize){} + parray(parray& other) noexcept{ + m_size = other.m_size; + m_head = other.m_head; + m_tsize = other.m_tsize; + m_buf = other.m_buf; + other.m_buf = nullptr; + } + uint32_t size() { + return m_head; + } + uint32_t data_size() { + return m_tsize * m_head; + } + void* data() { + return m_buf; + } + template + requires std::is_base_of::value + void push_back(S& s) { + if (m_head >= m_size) { + return; + } + *(S*)(m_buf + m_head * m_tsize) = s; + m_head++; + }; + T* at(int i) { + if (i >= m_head) { + return nullptr; + } + return (T*)(m_buf + i * m_tsize); + }; + }; +} \ No newline at end of file diff --git a/engine/3rdparty/zlib/include/zstd/pool_static.h b/engine/3rdparty/zlib/include/zstd/pool_static.h deleted file mode 100644 index deb67ae..0000000 --- a/engine/3rdparty/zlib/include/zstd/pool_static.h +++ /dev/null @@ -1,59 +0,0 @@ -#pragma once -#include -namespace zstd { - template - struct static_buffer { - char data[T]; - }; - template - class channel_static { - static_assert(T_capacity != 0); - protected: - int m_tail; - int m_head; - static_buffer m_buf[T_capacity]; - std::mutex m_mtx; - std::condition_variable m_cv; - public: - ~channel_static() { - reset(); - } - channel_static() : m_tail(0), m_head(0), m_buf() {} - int head() { - return m_head; - } - int tail() { - return m_tail; - } - int count() { - return m_head - m_tail; - } - void reset() { - for (int i = m_tail; i < m_head; i++) { - std::destroy_at((T*)(m_buf + m_tail % T_capacity)); - } - m_tail = 0; - m_head = 0; - } - template - void release(Args... args) { - std::unique_lock lck(m_mtx); - while (m_head >= m_tail + T_capacity) { - m_cv.wait(lck); - } - std::construct_at((T*)(m_buf + m_head % T_capacity), std::forward(args)...); - if (m_head++ == m_tail) - m_cv.notify_one(); - }; - T acquire() { - std::unique_lock lck(m_mtx); - while (m_tail >= m_head) { - m_cv.wait(lck); - } - int tail = m_tail % T_capacity; - if (m_tail++ == m_head - T_capacity) - m_cv.notify_one(); - return std::move(*(T*)(m_buf + tail)); - }; - }; -} \ No newline at end of file diff --git a/engine/src/engine/asset/render/asset_struct.cpp b/engine/src/engine/asset/render/asset_struct.cpp index 3f7c9d5..4cc032c 100644 --- a/engine/src/engine/asset/render/asset_struct.cpp +++ b/engine/src/engine/asset/render/asset_struct.cpp @@ -1,8 +1,6 @@ #include "asset_struct.h" namespace engineapi { - void Vertex::AddBoneData(uint32_t boneID, float weight) - { - } + } diff --git a/engine/src/engine/asset/render/asset_struct.h b/engine/src/engine/asset/render/asset_struct.h index 95ecc1a..938a822 100644 --- a/engine/src/engine/asset/render/asset_struct.h +++ b/engine/src/engine/asset/render/asset_struct.h @@ -2,27 +2,12 @@ #include "../asset.h" #include "math/math.h" #include "asset_enum.h" -// 椤剁偣鏈澶氬叧鑱4涓楠 -#define MAX_NUM_BONES_PER_VERTEX 4 using std::string; using std::vector; namespace engineapi { - struct Vertex - { - Vector3 Position = {}; - Vector2 TexCoords = {}; - Vector3 Normal = {}; - Vector3 Tangent = {}; - // 楠ㄩ钂欑毊鏁版嵁 - float Weights[MAX_NUM_BONES_PER_VERTEX] = {}; - uint32_t BoneIDs[MAX_NUM_BONES_PER_VERTEX] = {}; - - void AddBoneData(uint32_t boneID, float weight); - }; - struct BoneNode { string name; diff --git a/engine/src/engine/render/asset/mesh.cpp b/engine/src/engine/render/asset/mesh.cpp index ac0f679..a528137 100644 --- a/engine/src/engine/render/asset/mesh.cpp +++ b/engine/src/engine/render/asset/mesh.cpp @@ -1,7 +1,7 @@ #include "mesh.h" #include "render/renderapi.h" namespace engineapi { - Mesh::Mesh(string name, uint32_t flags, vector& vertices, vector& indices) + Mesh::Mesh(string name, uint32_t flags, parray& vertices, vector& indices) :Asset(name, flags) ,mVertices(vertices) ,mIndices(indices) diff --git a/engine/src/engine/render/asset/mesh.h b/engine/src/engine/render/asset/mesh.h index eeb7ced..81f5a00 100644 --- a/engine/src/engine/render/asset/mesh.h +++ b/engine/src/engine/render/asset/mesh.h @@ -1,23 +1,25 @@ #pragma once +#include "zstd/parray.h" #include "asset_render.h" #include "../wrapper/vertex.h" namespace engineapi { + using zstd::parray; class Texture; class Material; class Mesh : public Asset { protected: uint32_t VAO = 0; - vector mVertices; + parray mVertices; vector mIndices; public: - Mesh(string name, uint32_t flags, vector& vertices, vector& indices); + Mesh(string name, uint32_t flags, parray& vertices, vector& indices); void BeginLoad()override; public: uint32_t& GetVAO() { return VAO; } - vector& GetVertices() { + parray& GetVertices() { return mVertices; } vector& GetIndices() { diff --git a/engine/src/engine/render/asset/model.cpp b/engine/src/engine/render/asset/model.cpp index 6e6e90b..e176a00 100644 --- a/engine/src/engine/render/asset/model.cpp +++ b/engine/src/engine/render/asset/model.cpp @@ -45,12 +45,15 @@ namespace engineapi { } Mesh* Model::ProcessMesh(const aiMesh* mesh) { // data to fill - vector vertices; + parray vertices(mesh->mNumVertices, sizeof(BoneVertex)); + parray vertices2(mesh->mNumVertices, sizeof(BoneVertex)); + BoneVertex vertex2; + vertices2.push_back(vertex2); vector indices; // Walk through each of the mesh's vertices for (unsigned int i = 0; i < mesh->mNumVertices; i++) { - Vertex vertex; + BoneVertex vertex; Vector3 vector; // we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to Vector3 class so we transfer the data to this placeholder Vector3 first. // positions @@ -92,7 +95,6 @@ namespace engineapi { { vertex.Tangent = Vector3(0.0f, 0.0f, 0.0f); } - vertices.push_back(vertex); } // now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices. diff --git a/engine/src/engine/render/wrapper/vertex.cpp b/engine/src/engine/render/wrapper/vertex.cpp index e69de29..08034f7 100644 --- a/engine/src/engine/render/wrapper/vertex.cpp +++ b/engine/src/engine/render/wrapper/vertex.cpp @@ -0,0 +1,8 @@ +#include "vertex.h" + +namespace engineapi { + void BoneVertex::AddBoneData(uint32_t boneID, float weight) + { + } +} + diff --git a/engine/src/engine/render/wrapper/vertex.h b/engine/src/engine/render/wrapper/vertex.h index b7722e0..3419e1c 100644 --- a/engine/src/engine/render/wrapper/vertex.h +++ b/engine/src/engine/render/wrapper/vertex.h @@ -1,8 +1,31 @@ #pragma once #include "math/vector3.h" +#include "math/vector2.h" +// 椤剁偣鏈澶氬叧鑱4涓楠 +#define MAX_NUM_BONES_PER_VERTEX 4 namespace engineapi { - struct VertexBase{ - virtual Vector3& GetPosition() = 0; + struct Vertex { + //virtual Vector3& GetPosition() = 0; + }; + struct PosVertex : public Vertex{ + Vector3 Position = {}; + }; + struct TexVertex : public Vertex { + Vector3 Position = {}; + Vector3 Normal = {}; + Vector2 TexCoords = {}; + }; + struct BoneVertex : public Vertex + { + Vector3 Position = {}; + Vector2 TexCoords = {}; + Vector3 Normal = {}; + Vector3 Tangent = {}; + // 楠ㄩ钂欑毊鏁版嵁 + float Weights[MAX_NUM_BONES_PER_VERTEX] = {}; + uint32_t BoneIDs[MAX_NUM_BONES_PER_VERTEX] = {}; + + void AddBoneData(uint32_t boneID, float weight); }; }; \ No newline at end of file diff --git a/engine/src/engine/vulkanapi/vulkanapi.cpp b/engine/src/engine/vulkanapi/vulkanapi.cpp index 6afdfe4..040315b 100644 --- a/engine/src/engine/vulkanapi/vulkanapi.cpp +++ b/engine/src/engine/vulkanapi/vulkanapi.cpp @@ -67,7 +67,7 @@ namespace vulkanapi { meshBuffer->vertexCount = Vertices.size(); // ----------------------------------------------- Vertex Buffer ----------------------------------------------- - VkDeviceSize vertexBufferSize = sizeof(Vertex) * Vertices.size(); + VkDeviceSize vertexBufferSize = Vertices.data_size(); VmaAllocationCreateInfo vertexVmaStagingInfo = {}; VmaAllocation vertexVmaStagingAlloc; VkBuffer vertexStagingBuffer = Buffer::CreateStageBuffer(vertexVmaStagingInfo, vertexVmaStagingAlloc, vertexBufferSize); @@ -173,27 +173,27 @@ namespace vulkanapi { attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[0].offset = offsetof(Vertex, Position); + attributeDescriptions[0].offset = offsetof(BoneVertex, Position); attributeDescriptions[1].binding = 0; attributeDescriptions[1].location = 1; attributeDescriptions[1].format = VK_FORMAT_R32G32_SFLOAT; - attributeDescriptions[1].offset = offsetof(Vertex, TexCoords); + attributeDescriptions[1].offset = offsetof(BoneVertex, TexCoords); attributeDescriptions[2].binding = 0; attributeDescriptions[2].location = 2; attributeDescriptions[2].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[2].offset = offsetof(Vertex, Normal); + attributeDescriptions[2].offset = offsetof(BoneVertex, Normal); attributeDescriptions[3].binding = 0; attributeDescriptions[3].location = 3; attributeDescriptions[3].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[3].offset = offsetof(Vertex, Tangent); + attributeDescriptions[3].offset = offsetof(BoneVertex, Tangent); attributeDescriptions[4].binding = 0; attributeDescriptions[4].location = 4; attributeDescriptions[4].format = VK_FORMAT_R32G32B32A32_SFLOAT; - attributeDescriptions[4].offset = offsetof(Vertex, Weights); + attributeDescriptions[4].offset = offsetof(BoneVertex, Weights); attributeDescriptions[5].binding = 0; attributeDescriptions[5].location = 5; attributeDescriptions[5].format = VK_FORMAT_R32G32B32A32_UINT; - attributeDescriptions[5].offset = offsetof(Vertex, BoneIDs); + attributeDescriptions[5].offset = offsetof(BoneVertex, BoneIDs); VkPipelineVertexInputStateCreateInfo vertexInputInfo = {}; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;