update asset

This commit is contained in:
ouczbs 2024-03-09 18:17:53 +08:00
parent 1bf3f23303
commit 71bb63a487
39 changed files with 314 additions and 102 deletions

View File

@ -1,13 +0,0 @@
#include "glm/glm.hpp"
#include "glm/fwd.hpp"
#include <iostream>
using namespace std;
int main() {
glm::vec3 v0(1,1,0),n1(0,0,1);
glm::vec3 p0(1, 2, 3), p1(3, 4, 5), p2 = p0;
p2.z += 10;
glm::vec3 up = glm::cross(v0, p1 - p0);
//glm::quat rt{};
cout << sizeof(v0)<< " " << sizeof(glm::mat3x3) << " ";
}

View File

@ -1,6 +0,0 @@
add_requires("glm")
target("glm_test01_bezier")
set_kind("binary")
add_packages("glm")
add_files("test/01bezier.cpp")

View File

@ -1,3 +1,4 @@
#pragma once
#include <cstdint>
#include <string>
namespace engineapi
@ -5,18 +6,17 @@ namespace engineapi
class Asset {
public:
enum {
ASSET_NONE,
ASSET_SHARED_TYPE,
ASSET_COPY_TYPE,
ASSET_ASYNC_TYPE,
ASSET_LOADED_TYPE,
ASSET_NUM,
ASSET_NONE = 0,
ASSET_SHARED_TYPE = 1 << 0,
ASSET_COPY_TYPE = 1 << 1,
ASSET_ASYNC_TYPE = 1 << 2,
ASSET_LOADED_TYPE = 1 << 3,
};
protected:
uint32_t mFlags;
std::string mName;
public:
Asset(std::string& name, uint32_t flags):mName(name),mFlags(flags) {};
Asset(std::string name, uint32_t flags):mName(name),mFlags(flags) {};
virtual void onLoadFinished() { mFlags |= ASSET_LOADED_TYPE; };
virtual void SyncLoad() {};
virtual void AsyncLoad() {};

View File

@ -12,33 +12,34 @@ namespace engineapi
{
public:
struct AssetWrap {
Asset& asset;
Asset* asset;
uint32_t count;
AssetWrap(Asset& asset): asset(asset),count(0) {
AssetWrap(Asset* asset): asset(asset),count(0) {
}
};
private:
std::map<const std::string, AssetWrap> mAssetMap;
public:
template<typename TAsset>
TAsset* LoadAsset(const std::string& asset_url,uint32_t flags)
TAsset* LoadAsset(const std::string& asset_url, uint32_t types, uint32_t flags)
{
std::filesystem::path asset_path = getFullPath(asset_url);
std::filesystem::path asset_path = getFullPath(asset_url, types);
auto it = mAssetMap.find(asset_path.string());
if (it != mAssetMap.end()) {
if (it->second.asset.IsShared()) {
return &(it->second.asset);
if (it->second.asset->IsShared()) {
return (TAsset*)(it->second.asset);
}
if (it->second.asset.IsCopyed()) {
if (it->second.asset->IsCopyed()) {
TAsset* asset = new TAsset(asset_path.string(), flags);
*asset = it->second.asset;
*asset = *(TAsset*)(it->second.asset);
return asset;
}
}
TAsset* asset = new TAsset(asset_path.string(), flags);
if (asset->IsShared() || asset->IsCopyed()) {
mAssetMap.emplace(asset_path.string(), *asset);
mAssetMap.emplace(asset_path.string(), asset);
}
if (asset->IsAsync()) {
asset->AsyncLoad();
@ -48,6 +49,8 @@ namespace engineapi
}
return asset;
}
std::filesystem::path getFullPath(const std::string& relative_path) const;
std::filesystem::path getFullPath(const std::string& relative_path, uint32_t types) const;
public:
static AssetManager* Instance;
};
}

View File

@ -1,6 +1,9 @@
#include "zasset/asset_manager.h"
std::filesystem::path engineapi::AssetManager::getFullPath(const std::string& relative_path) const
namespace engineapi {
AssetManager* AssetManager::Instance = nullptr;
std::filesystem::path AssetManager::getFullPath(const std::string& relative_path, uint32_t types) const
{
return std::filesystem::path(relative_path);
}
}

View File

@ -0,0 +1,24 @@
#include "actor.h"
#include "zasset/asset_manager.h"
#include "../property/actor_property.h"
namespace engineapi {
ActorMesh::ActorMesh(Mesh& mesh, ActorProperty& property)
:DynamicMesh(mesh)
,mProperty(property)
{
}
ActorMesh* ActorMesh::New(ActorProperty& property)
{
Mesh* mesh = AssetManager::Instance->LoadAsset<Mesh>(property.path, property.types, property.flags);
return new ActorMesh(*mesh, property);
}
ActorMesh* ActorMesh::New(uint32_t id)
{
ActorProperty* property = Property::GetProperty<ActorProperty>(id);
if (property) {
return New(*property);
}
return nullptr;
}
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "dynamic.h"
namespace engineapi {
class ActorProperty;
class ActorMesh : public DynamicMesh {
protected:
ActorProperty& mProperty;
public:
ActorMesh(Mesh& mesh, ActorProperty& property);
public:
static ActorMesh* New(ActorProperty& property);
static ActorMesh* New(uint32_t id);
};
}

View File

@ -0,0 +1,9 @@
#include "dynamic.h"
namespace engineapi {
DynamicMesh::DynamicMesh(Mesh& mesh)
:StaticMesh(mesh)
{
}
}

View File

@ -0,0 +1,11 @@
#pragma once
#include "static.h"
namespace engineapi {
class Mesh;
class DynamicMesh : public StaticMesh{
protected:
public:
DynamicMesh(Mesh& mesh);
};
}

View File

@ -0,0 +1,13 @@
#include "static.h"
#include "engine/render/asset/mesh.h"
namespace engineapi {
StaticMesh::StaticMesh(Mesh& mesh)
:mPtr(mesh)
{
}
void StaticMesh::LoadMesh()
{
}
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <string>
#include <map>
#include "glm/vec3.hpp"
#include "engine/render/asset/mesh.h"
namespace engineapi {
class StaticMesh {
protected:
Mesh& mPtr;
glm::vec3 mCenter;
float mRadius;
public:
StaticMesh(Mesh& mesh);
void LoadMesh();
};
}

View File

@ -0,0 +1,8 @@
#pragma once
#include "property.h"
namespace engineapi {
class ActorProperty :public Property{
public:
static const uint32_t Type = Property::ActorPropertyType;
};
}

View File

@ -0,0 +1,5 @@
#include "property.h"
namespace engineapi {
std::map<uint32_t, Property> Property::PropertyMap;
}

View File

@ -0,0 +1,27 @@
#pragma once
#include <string>
#include <map>
namespace engineapi {
class Property {
public:
enum {
ActorPropertyType = 1,
};
public:
uint32_t id = 0;
uint32_t types = 0;
uint32_t flags = 0;
std::string path = "";
public:
static std::map<uint32_t, Property> PropertyMap;
template<typename TProperty>
static TProperty* GetProperty(uint32_t id) {
auto it = PropertyMap.find(id);
if (it != PropertyMap.end() && it->second.types && TProperty::Type) {
return (TProperty*)(&it->second);
}
return nullptr;
}
};
}

View File

View File

@ -0,0 +1,9 @@
#pragma once
#include "zasset/asset.h"
namespace engineapi {
class AssetRender : public Asset {
public:
using Asset::Asset;
};
};

View File

@ -0,0 +1,7 @@
#include "asset.h"
namespace engineapi {
class Material : public AssetRender {
};
};

View File

@ -0,0 +1,70 @@
#include "mesh.h"
#include "assimp/Importer.hpp"
#include "assimp/scene.h"
#include "assimp/postprocess.h"
#include "zlog.h"
namespace engineapi {
void Mesh::SyncLoad()
{
std::vector<glm::vec3> vertexPositions = std::vector<glm::vec3>();
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(mName, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
// process ASSIMP's root node recursively
aiMesh* mesh = scene->mMeshes[scene->mRootNode->mMeshes[0]];
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
VertexData vertexData;
glm::vec3 vector;
// positions
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertexData.position = vector;
// normals
if (mesh->HasNormals())
{
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertexData.normal = vector;
}
// texture coordinates
if (mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
{
glm::vec2 vec;
// a vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't
// use models where a vertex can have multiple texture coordinates so we always take the first set (0).
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertexData.texCoords = vec;
// tangent
vector.x = mesh->mTangents[i].x;
vector.y = mesh->mTangents[i].y;
vector.z = mesh->mTangents[i].z;
vertexData.tangent = vector;
// bitangent
vector.x = mesh->mBitangents[i].x;
vector.y = mesh->mBitangents[i].y;
vector.z = mesh->mBitangents[i].z;
vertexData.bitangent = vector;
}
else
{
zlog::info("Mesh do not contains uv.");
}
mVertices.push_back(vertexData);
vertexPositions.emplace_back(vertexData.position);
}
// now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];
// retrieve all indices of the face and store them in the indices vector
for (unsigned int j = 0; j < face.mNumIndices; j++)
mIndices.push_back(face.mIndices[j]);
}
importer.FreeScene();
}
}

View File

@ -0,0 +1,31 @@
#pragma once
#include "asset.h"
#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include <vector>
#include <map>
namespace engineapi {
class Texture;
class Material;
class Mesh : public AssetRender {
public:
struct VertexData
{
glm::vec3 position;
glm::vec2 texCoords;
glm::vec3 normal;
glm::vec3 tangent;
glm::vec3 bitangent;
};
protected:
Material* mMaterial;
std::map<const std::string*, Texture*> mTextureMap;
std::vector<VertexData> mVertices;
std::vector<uint32_t> mIndices;
public:
using AssetRender::AssetRender;
void SyncLoad()override;
};
};

View File

@ -0,0 +1,7 @@
#include "asset.h"
namespace engineapi {
class Texture : public AssetRender {
};
};

View File

@ -1,5 +1,5 @@
#include "window.h"
namespace renderapi {
namespace engineapi {
Window::WindowClass Window::WindowClass::wndClass;
Window::WindowClass::WindowClass() noexcept

View File

@ -1,6 +1,6 @@
#pragma once
#include <Windows.h>
namespace renderapi {
namespace engineapi {
class Window {
protected:
int mWidth;

View File

@ -68,10 +68,6 @@ namespace vulkanapi {
}
importer.FreeScene();
Backend::TransferWorker->InvokeBuffer([&](CommandBuffer* cmd) {
cmd->CopyBuffer(&stageVertexBuffer, _vertexBuffer);
cmd->CopyBuffer(&stageIndexBuffer, _indexBuffer);
});
}
void Mesh::AsyncLoad()
{
@ -79,6 +75,7 @@ namespace vulkanapi {
}
void Mesh::BindCommand(CommandBuffer& cmd)
{
//cmd->CopyBuffer(&stageVertexBuffer, _vertexBuffer);
// cmd->CopyBuffer(&stageIndexBuffer, _indexBuffer);
}
}

View File

@ -12,7 +12,9 @@ namespace vulkanapi {
auto deviceCreator = DeviceCreator(*mInstance);
deviceCreator.AddWindowExtension();
deviceCreator.desiredPhysicalDeviceFeatures.geometryShader = VK_TRUE;
deviceCreator.desiredPhysicalDeviceFeatures.independentBlend = true;
deviceCreator.desiredPhysicalDeviceFeatures.depthClamp = true;
//deviceCreator.desiredPhysicalDeviceFeatures.geometryShader = VK_TRUE;
deviceCreator.AddQueue(Queue::TransferQueue, VkQueueFlagBits::VK_QUEUE_GRAPHICS_BIT, 1.0);
deviceCreator.AddQueue(Queue::RenderQueue, VkQueueFlagBits::VK_QUEUE_GRAPHICS_BIT, 1.0);
deviceCreator.AddQueue(Queue::ComputeQueue, VkQueueFlagBits::VK_QUEUE_GRAPHICS_BIT, 1.0);
@ -40,12 +42,12 @@ namespace vulkanapi {
auto queue = mDevice->GetQueue(name);
if (queue) {
auto worker = new CommandWorker(name, *mDevice, *queue, flag);
mWorkerMap.emplace(name, worker);
mWorkerMap.emplace(&name, worker);
}
}
CommandWorker* Backend::GetWorker(const std::string& name)
{
auto it = mWorkerMap.find(name);
auto it = mWorkerMap.find(&name);
if (it != mWorkerMap.end()) {
return it->second;
}

View File

@ -11,7 +11,7 @@ namespace vulkanapi {
protected:
Instance* mInstance;
Device* mDevice;
std::map<const std::string&, CommandWorker*> mWorkerMap;
std::map<const std::string*, CommandWorker*> mWorkerMap;
public:
static CommandWorker* TransferWorker;
public:

View File

@ -1,19 +0,0 @@
#include "record.h"
#include <iostream>
#include <engine/vulkanapi/device/device.h>
namespace vulkanapi {
CommandRecord::CommandRecord()
{
mParts.reserve(64);
}
void CommandRecord::Apply(CommandBuffer* buf)
{
for (auto cmd : mParts) {
cmd(buf);
}
}
void CommandRecord::Record(CommandFn cmd)
{
mParts.push_back(cmd);
}
}

View File

@ -1,16 +0,0 @@
#pragma once
#include <string>
#include <vector>
#include "engine/vulkanapi/vulkan.h"
namespace vulkanapi {
class CommandBuffer;
typedef void (*CommandFn)(CommandBuffer* buf);
class CommandRecord {
protected:
std::vector<CommandFn> mParts;
public:
CommandRecord();
void Apply(CommandBuffer* buf);
void Record(CommandFn cmd);
};
};

View File

@ -7,7 +7,7 @@
#include <vulkan/vulkan_win32.h>
namespace vulkanapi {
Window::Window(Backend& backend, int frames, uint32_t width, uint32_t height, const char* title)
:renderapi::Window(width, height , title)
:engineapi::Window(width, height , title)
, mSurfaceKHR(nullptr)
, mSwapchain(nullptr)
{

View File

@ -1,12 +1,12 @@
#pragma once
#include <string>
#include <vector>
#include "engine/renderapi/window.h"
#include "engine/render/window.h"
#include "vulkan.h"
namespace vulkanapi {
class Backend;
class Swapchain;
class Window : public renderapi::Window {
class Window : public engineapi::Window {
protected:
VkSurfaceKHR mSurfaceKHR{NULL};
Swapchain* mSwapchain;

View File

@ -29,14 +29,15 @@ namespace vulkanapi {
device_create_info.enabledLayerCount = layers.size();
device_create_info.ppEnabledLayerNames = layers.data();
#endif // _USE_GRAPHIC_DEBUG
system("cls");
VkResult result = vkCreateDevice(mPhysical, &device_create_info, nullptr, &mPtr);
if ((result != VK_SUCCESS) || (mPtr == VK_NULL_HANDLE)) {
zlog::error("Could not create logical device.");
zlog::error("Could not create logical device. VkResult {}", result);
}
for (auto& queue : Creator.desiredQueues) {
Queue* gq = new Queue(queue.name, queue.queueFamilyIndex, VK_NULL_HANDLE);
vkGetDeviceQueue(mPtr, queue.queueFamilyIndex, 0, &(gq->Ptr()));
mQueueMap.emplace(queue.name, gq);
mQueueMap.emplace(&queue.name, gq);
}
}
Device::~Device() {
@ -47,7 +48,7 @@ namespace vulkanapi {
}
Queue* Device::GetQueue(const std::string& name)
{
auto it = mQueueMap.find(name);
auto it = mQueueMap.find(&name);
if (it != mQueueMap.end()) {
return it->second;
}

View File

@ -11,7 +11,7 @@ namespace vulkanapi {
protected:
VkDevice mPtr{ NULL };
VkPhysicalDevice mPhysical{NULL};
std::map<const std::string&, Queue*> mQueueMap;
std::map<const std::string*, Queue*> mQueueMap;
public:
VkDevice& Ptr() {
return mPtr;

View File

@ -1,7 +1,7 @@
#pragma once
#include "device.h"
#include "device_creator.h"
#include "instance.h"
#include "queue.h"
#include "zlog.h"
namespace vulkanapi {
DeviceCreator::DeviceCreator(Instance& instance)
@ -18,10 +18,10 @@ namespace vulkanapi {
void DeviceCreator::AddWindowExtension()
{
AddExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
AddQueue("PresentQueue", VK_QUEUE_FLAG_BITS_MAX_ENUM, 1.0f);
//AddQueue(Queue::PresentQueue, VK_QUEUE_FLAG_BITS_MAX_ENUM, 1.0f);
#ifdef _USE_GRAPHIC_DEBUG
AddLayer("VK_LAYER_KHRONOS_validation");
AddLayer("VK_LAYER_RENDERDOC_Capture");
//AddLayer("VK_LAYER_KHRONOS_validation");
//AddLayer("VK_LAYER_RENDERDOC_Capture");
#endif
}
bool DeviceCreator::CheckProperty(const VkPhysicalDevice device)
@ -101,7 +101,7 @@ namespace vulkanapi {
}
}
}
if (index != -1 && queue_create_infos[index].queueCount < queue_families[index].queueCount) {
if (index != -1 && queue_create_infos[index].queueCount < 1) {
queue.queueFamilyIndex = index;
queue_create_infos[index].queueCount++;
queue_prioritie[index].push_back(queue.prioritie);

View File

@ -22,7 +22,7 @@ namespace vulkanapi {
public:
VkPhysicalDeviceFeatures desiredPhysicalDeviceFeatures;
VkPhysicalDeviceType desiredPhysicalDeviceType;
std::vector<const std::string> desiredExtensions;
std::vector<std::string> desiredExtensions;
std::vector<DesiredQueue> desiredQueues;
public:
Instance& instance;

View File

@ -1,4 +1,3 @@
#pragma once
#include "instance.h"
#include "instance_creator.h"
#include "zlog.h"
@ -36,6 +35,7 @@ namespace vulkanapi {
{
AddLayer("VK_LAYER_KHRONOS_validation");
AddLayer("VK_LAYER_RENDERDOC_Capture");
AddLayer("VK_LAYER_LUNARG_api_dump");
AddExtension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
AddExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
}

View File

@ -5,7 +5,7 @@ namespace vulkanapi {
const std::string Queue::ComputeQueue("ComputeQueue");
const std::string Queue::PresentQueue("PresentQueue");
Queue::Queue(const std::string& name, uint32_t queueFamilyIndex, VkQueue queue)
: mName(name), mQueueFamilyIndex(queueFamilyIndex), mPtr(queue), mMtx()
: mName(name), mQueueFamilyIndex(queueFamilyIndex), mPtr(queue)
{
}

View File

@ -4,8 +4,10 @@
#include "engine/vulkanapi/window.h"
#include "engine/vulkanapi/pass/gbuffer.h"
#include "engine/vulkanapi/pass/forwardpass.h"
#include "engine/object/mesh/actor.h"
#include "engine/object/property/actor_property.h"
#include "zlog.h"
using namespace engineapi;
int main(int argc, char** argv)
{
const char* name = "zengine";
@ -14,8 +16,13 @@ int main(int argc, char** argv)
auto wnd = vulkanapi::Window(vulkan, 3, 640, 720, name);
auto gbuffer = vulkanapi::GeometryBuffer(vulkan.GetDevice(), 3, 640, 720);
auto forwardpass = vulkanapi::ForwardPass(vulkan.GetDevice(), gbuffer);
ActorProperty property;
property.id = 1;
property.path = "assets/models/cube.obj";
auto actor = ActorMesh::New(property);
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
delete actor;
return 0;
}

View File

@ -5,7 +5,7 @@ target("zengine")
set_kind("binary")
set_rundir(".")
add_deps("zcoro","zlog","zasset")
add_packages("vulkansdk","tinyobjloader","glm","assimp")
add_packages("vulkansdk","tinyobjloader","assimp")
add_includedirs("src")
add_syslinks("user32")
add_files("src/*.cpp", "src/**.cpp")