update ytl lib

This commit is contained in:
ouczbs 2024-03-28 22:10:55 +08:00
parent c6e986bb69
commit 378e0c12a7
13 changed files with 174 additions and 94 deletions

View File

@ -4,4 +4,5 @@ add_requires("spdlog")
add_requires("tinyobjloader")
add_requires("vulkansdk")
add_requires("assimp")
add_requires("nlohmann_json")
add_requires("nlohmann_json")
add_requires("ylt")

View File

@ -0,0 +1,58 @@
#include <ylt/struct_pack.hpp>
#include <iostream>
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 <typename T, typename construct_param_t, typename = void, typename... Args>
struct is_constructable_impl : std::false_type {};
template <typename T, typename construct_param_t, typename... Args>
struct is_constructable_impl < T, construct_param_t,
std::void_t<
decltype(T{ {Args{}}..., {construct_param_t{}} }) > , Args... >
: std::true_type {};
template <typename T, typename construct_param_t, typename... Args>
bool is_constructable = is_constructable_impl<T, construct_param_t, void, Args...>::value;
template <typename T, typename... Args>
std::size_t members_count_impl() {
if (is_constructable<T, UniversalVectorType, Args...>) {
cout << "is_constructable UniversalVectorType \n";
//return members_count_impl<T, Args..., UniversalVectorType>();
}
else if (is_constructable<T, UniversalType, Args...>) {
cout << "is_constructable UniversalType \n";
//return members_count_impl<T, UniversalType, Args...>();
}
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<decltype(person1)>;
using type2 = remove_cvref_t<type>;
auto Count = members_count<type>;
int isc = members_count_impl<type2>();
int c2 = detail::members_count<type>();
// 1行代码序列化
vector<char> buffer = serialize(person1);
// 只反序列化person的第2个字段
auto name = get_field<person, 1>(buffer.data(), buffer.size());
cout << "hello " << name.value() << "Count == " << Count << c2;
}

12
engine/3rdparty/ylt/xmake.lua vendored Normal file
View File

@ -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")

View File

@ -0,0 +1,50 @@
#pragma once
#include <concepts>
namespace zstd {
template<typename T>
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<typename S>
requires std::is_base_of<T, S>::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);
};
};
}

View File

@ -1,59 +0,0 @@
#pragma once
#include <concepts>
namespace zstd {
template<size_t T>
struct static_buffer {
char data[T];
};
template<std::move_constructible T, size_t T_capacity = 1>
class channel_static {
static_assert(T_capacity != 0);
protected:
int m_tail;
int m_head;
static_buffer<sizeof(T)> 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<typename... Args>
void release(Args... args) {
std::unique_lock<std::mutex> 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>(args)...);
if (m_head++ == m_tail)
m_cv.notify_one();
};
T acquire() {
std::unique_lock<std::mutex> 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));
};
};
}

View File

@ -1,8 +1,6 @@
#include "asset_struct.h"
namespace engineapi {
void Vertex::AddBoneData(uint32_t boneID, float weight)
{
}
}

View File

@ -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;

View File

@ -1,7 +1,7 @@
#include "mesh.h"
#include "render/renderapi.h"
namespace engineapi {
Mesh::Mesh(string name, uint32_t flags, vector<Vertex>& vertices, vector<uint32_t>& indices)
Mesh::Mesh(string name, uint32_t flags, parray<Vertex>& vertices, vector<uint32_t>& indices)
:Asset(name, flags)
,mVertices(vertices)
,mIndices(indices)

View File

@ -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<Vertex> mVertices;
parray<Vertex> mVertices;
vector<uint32_t> mIndices;
public:
Mesh(string name, uint32_t flags, vector<Vertex>& vertices, vector<uint32_t>& indices);
Mesh(string name, uint32_t flags, parray<Vertex>& vertices, vector<uint32_t>& indices);
void BeginLoad()override;
public:
uint32_t& GetVAO() {
return VAO;
}
vector<Vertex>& GetVertices() {
parray<Vertex>& GetVertices() {
return mVertices;
}
vector<uint32_t>& GetIndices() {

View File

@ -45,12 +45,15 @@ namespace engineapi {
}
Mesh* Model::ProcessMesh(const aiMesh* mesh) {
// data to fill
vector<Vertex> vertices;
parray<Vertex> vertices(mesh->mNumVertices, sizeof(BoneVertex));
parray<Vertex> vertices2(mesh->mNumVertices, sizeof(BoneVertex));
BoneVertex vertex2;
vertices2.push_back(vertex2);
vector<uint32_t> 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.

View File

@ -0,0 +1,8 @@
#include "vertex.h"
namespace engineapi {
void BoneVertex::AddBoneData(uint32_t boneID, float weight)
{
}
}

View File

@ -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);
};
};

View File

@ -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;