rebuild asset
This commit is contained in:
parent
26ac7f246a
commit
b67411cb43
12
engine/3rdparty/zlib/include/meta/pad.h
vendored
Normal file
12
engine/3rdparty/zlib/include/meta/pad.h
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
#include <vector>
|
||||
namespace meta {
|
||||
template<typename T>
|
||||
void padding_vector(std::vector<T>& vec, size_t multiple, const T& paddingValue = T()) {
|
||||
if (multiple == 0) return; // 避免除以零
|
||||
size_t remainder = vec.size() % multiple;
|
||||
if (remainder != 0) {
|
||||
size_t padding = multiple - remainder;
|
||||
vec.insert(vec.end(), padding, paddingValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -45,6 +45,7 @@ namespace refl {
|
||||
uint32_t flag{0};
|
||||
const UClass* parent;
|
||||
vtable_uclass vtable{};
|
||||
inline static std::unordered_map<Name, const UClass*> MetaTable;
|
||||
public:
|
||||
constexpr UClass(std::string_view name, uint32_t size, const UClass* parent = nullptr)
|
||||
:name(name), size(size), parent(parent){}
|
||||
|
||||
27
engine/assets/shader/simple.vert
Normal file
27
engine/assets/shader/simple.vert
Normal file
@ -0,0 +1,27 @@
|
||||
#version 450
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
|
||||
// 顶点位置输入变量
|
||||
layout (location = 0) in vec3 position;
|
||||
layout (location = 1) in vec2 texture;
|
||||
layout (location = 2) in vec3 normal;
|
||||
layout (location = 3) in vec3 tangent;
|
||||
layout (location = 4) in vec4 weights;
|
||||
layout (location = 5) in uvec4 bones;
|
||||
|
||||
|
||||
// 顶点位置输出变量
|
||||
//out vec4 outPosition;
|
||||
out gl_PerVertex {
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
vec2 positions[3] = vec2[](
|
||||
vec2(0.0, -0.5),
|
||||
vec2(0.5, 0.5),
|
||||
vec2(-0.5, 0.5)
|
||||
);
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
|
||||
}
|
||||
@ -4,7 +4,6 @@
|
||||
#include "object/mesh/actor.h"
|
||||
#include "data/property/actor_property.h"
|
||||
#include "asset/file_manager.h"
|
||||
#include "asset/asset_manager.h"
|
||||
#include "asset/resource_manager.h"
|
||||
#include "object/scene/scene_manager.h"
|
||||
namespace engineapi {
|
||||
@ -12,7 +11,6 @@ namespace engineapi {
|
||||
{
|
||||
const char* name = "zengine";
|
||||
SystemList.push_back(new FileManager());
|
||||
SystemList.push_back(new AssetManager());
|
||||
SystemList.push_back(new ResourceManager());
|
||||
SystemList.push_back(RenderAPI::MakeInstance());
|
||||
SystemList.push_back(Window::MakeInstance(3, 640, 720, name));
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
#include "asset/asset.h"
|
||||
#include "asset_manager.h"
|
||||
namespace engineapi
|
||||
{
|
||||
Asset::~Asset()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,50 +1,11 @@
|
||||
#pragma once
|
||||
#include "res/guid.h"
|
||||
#include "render/asset_struct.h"
|
||||
#include "res/resource_handle.h"
|
||||
#include "res/package_path.h"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
namespace engineapi
|
||||
{
|
||||
using std::vector;
|
||||
using std::map;
|
||||
class Asset {
|
||||
namespace engineapi {
|
||||
class Asset : public Resource<Asset> {
|
||||
public:
|
||||
friend class AssetManager;
|
||||
enum :uint32_t {
|
||||
ASSET_NONE_FLAG = 0,
|
||||
ASSET_SHARED_FLAG = 1 << 0,
|
||||
ASSET_COPY_FLAG = 1 << 1,
|
||||
ASSET_ASYNC_FLAG = 1 << 2,
|
||||
ASSET_LOADING_FLAG = 1 << 3,
|
||||
ASSET_LOADED_FLAG = 1 << 4,
|
||||
ASSET_ANIM_FLAG = 1 << 5,
|
||||
};
|
||||
protected:
|
||||
uint32_t mFlags;
|
||||
string_view mName;
|
||||
public:
|
||||
Asset(string_view name, uint32_t flags):mName(name),mFlags(flags) {};
|
||||
~Asset();
|
||||
virtual void BeginLoad() {
|
||||
mFlags |= ASSET_LOADING_FLAG;
|
||||
};
|
||||
virtual void EndLoad() {
|
||||
mFlags &= ~ASSET_LOADING_FLAG;
|
||||
mFlags |= ASSET_LOADED_FLAG;
|
||||
};
|
||||
string_view& GetName() {
|
||||
return mName;
|
||||
}
|
||||
public:
|
||||
inline bool IsShared() {
|
||||
return mFlags & ASSET_SHARED_FLAG;
|
||||
}
|
||||
inline bool IsCopyed() {
|
||||
return mFlags & ASSET_COPY_FLAG;
|
||||
}
|
||||
inline bool IsAsync() {
|
||||
return mFlags & ASSET_ASYNC_FLAG;
|
||||
}
|
||||
using Base = Resource<Asset>;
|
||||
using Base::Base;
|
||||
};
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
#include "asset_manager.h"
|
||||
#include "zlog.h"
|
||||
using namespace std;
|
||||
namespace engineapi {
|
||||
void AssetManager::Init()
|
||||
{
|
||||
}
|
||||
void AssetManager::Shutdown()
|
||||
{
|
||||
}
|
||||
void AssetManager::ClearAsset(Asset* asset)
|
||||
{
|
||||
auto it = AssetMap.find(asset->mName);
|
||||
if (!asset->IsShared()) {
|
||||
delete asset;
|
||||
}
|
||||
if (it != AssetMap.end()) {
|
||||
it->second.count--;
|
||||
if (it->second.count < 1) {
|
||||
delete it->second.asset;
|
||||
AssetMap.erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,50 +0,0 @@
|
||||
#pragma once
|
||||
#include "asset.h"
|
||||
#include "singleton.h"
|
||||
namespace engineapi
|
||||
{
|
||||
class AssetManager : public ISingleton<AssetManager>
|
||||
{
|
||||
public:
|
||||
struct AssetWrap {
|
||||
Asset* asset;
|
||||
uint32_t count;
|
||||
AssetWrap(Asset* asset): asset(asset),count(1) {
|
||||
|
||||
}
|
||||
};
|
||||
public:
|
||||
void Init() override;
|
||||
void Shutdown() override;
|
||||
public:
|
||||
void ClearAsset(Asset* asset);
|
||||
template<typename TAsset>
|
||||
TAsset* LoadAsset(const string& name, uint32_t flags)
|
||||
{
|
||||
string_view view = PackagePath::StringView(name);
|
||||
auto it = AssetMap.find(view);
|
||||
bool isFind = it != AssetMap.end();
|
||||
if (isFind) {
|
||||
it->second.count++;
|
||||
if (it->second.asset->IsShared()) {
|
||||
return (TAsset*)(it->second.asset);
|
||||
}
|
||||
if (it->second.asset->IsCopyed()) {
|
||||
TAsset* asset = new TAsset(name, flags);
|
||||
*asset = *(TAsset*)(it->second.asset);
|
||||
return asset;
|
||||
}
|
||||
}
|
||||
TAsset* asset = new TAsset(name, flags);
|
||||
if (!isFind && (asset->IsShared() || asset->IsCopyed())) {
|
||||
AssetMap.emplace(name, asset);
|
||||
}
|
||||
asset->BeginLoad();
|
||||
return asset;
|
||||
}
|
||||
|
||||
AssetManager() = default;
|
||||
private:
|
||||
inline static table<string_view, AssetWrap> AssetMap;
|
||||
};
|
||||
}
|
||||
@ -6,7 +6,7 @@ namespace engineapi {
|
||||
void FileManager::Init()
|
||||
{
|
||||
std::filesystem::path path = std::filesystem::current_path();
|
||||
Mount(string_view("engine"), path.string());
|
||||
Mount("engine", path.string());
|
||||
}
|
||||
void FileManager::Shutdown()
|
||||
{
|
||||
|
||||
@ -11,17 +11,25 @@ namespace engineapi
|
||||
void Init() override;
|
||||
void Shutdown() override;
|
||||
public:
|
||||
static void Mount(NameID id, const string& path) {
|
||||
MountMap.emplace(id, path);
|
||||
static void Mount(const string& name, const string& path) {
|
||||
MountMap.emplace(NameID(name), std::make_pair(name,path));
|
||||
}
|
||||
static string FindMount(NameID id) {
|
||||
static std::pair<string, string> FindMount(NameID id) {
|
||||
auto it = MountMap.find(id);
|
||||
if (it != MountMap.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return "";
|
||||
return {};
|
||||
}
|
||||
inline static table<NameID, string> MountMap;
|
||||
static string FindMountName(NameID id) {
|
||||
auto pair = FindMount(id);
|
||||
return pair.first;
|
||||
}
|
||||
static string FindMountPath(NameID id) {
|
||||
auto pair = FindMount(id);
|
||||
return pair.second;
|
||||
}
|
||||
inline static table<NameID, std::pair<string, string>> MountMap;
|
||||
public:
|
||||
static string LoadTextFile(const string& path);
|
||||
static vector<char> LoadBinaryFile(const string& path);
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include "../asset.h"
|
||||
#include "math/math.h"
|
||||
#include "asset_enum.h"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
|
||||
@ -1,10 +1,17 @@
|
||||
#include "package_path.h"
|
||||
#include "asset/file_manager.h"
|
||||
namespace engineapi {
|
||||
PackagePath PackagePath::ToSuffixPath(const string_view& suffix)
|
||||
{
|
||||
if (CheckPackage()) {
|
||||
return { FileManager::FindMountName(name) + string(suffix) , path};
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
string PackagePath::AbsolutePath()
|
||||
{
|
||||
if (CheckPackage()) {
|
||||
return FileManager::FindMount(name) + string(path);
|
||||
return FileManager::FindMountPath(name) + string(path);
|
||||
}
|
||||
return string(path);
|
||||
}
|
||||
|
||||
@ -11,14 +11,15 @@ namespace engineapi
|
||||
using Ubpa::Name;
|
||||
using Ubpa::NameID;
|
||||
struct PackagePath {
|
||||
string_view name;
|
||||
NameID name;
|
||||
string_view path;
|
||||
PackagePath(const string& name,const string_view& path) : name(name), path(path) {}
|
||||
PackagePath(const char* path) : path(path) {}
|
||||
PackagePath(const string_view& path) : path(path) {}
|
||||
PackagePath(const string& path) : path(StringView(path)) {};
|
||||
PackagePath(const string&& path) : path(path) {}
|
||||
bool CheckPackage() {
|
||||
if (!name.empty()) {
|
||||
if (name.Valid()) {
|
||||
return true;
|
||||
}
|
||||
if (path[0] == '/') {
|
||||
@ -37,6 +38,7 @@ namespace engineapi
|
||||
return path.substr(pos);
|
||||
return "";
|
||||
}
|
||||
PackagePath ToSuffixPath(const string_view& suffix);
|
||||
string AbsolutePath();
|
||||
static string AbsolutePath(string_view path) {
|
||||
return PackagePath(path).AbsolutePath();
|
||||
|
||||
@ -11,7 +11,7 @@ namespace engineapi
|
||||
ResourceBundle(const RscHandle<Res>& handle);
|
||||
// will reshuffle vector and invalidate span, but you shouldn't be accessing vector directly anyway so this is ok
|
||||
template<typename T> void Add(RscHandle<T> handle);
|
||||
|
||||
template<typename T> RscHandle<T> Get() const; // get a resource from the bundle
|
||||
private:
|
||||
struct sub_array { short index = 0, count = 0; };
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
#include "resource_bundle.h"
|
||||
#pragma once
|
||||
namespace engineapi
|
||||
{
|
||||
@ -24,4 +25,10 @@ namespace engineapi
|
||||
for (auto& elem : span<sub_array>{ &sub_arr + 1, subarrays.data() + subarrays.size() })
|
||||
++elem.index;
|
||||
}
|
||||
template<typename T>
|
||||
inline RscHandle<T> engineapi::ResourceBundle::Get() const
|
||||
{
|
||||
auto& subarray = subarrays[ResourceID<T>];
|
||||
return subarray.count > 0 ? handles[subarray.index].template AsHandle<T>() : RscHandle<T>();
|
||||
}
|
||||
}
|
||||
@ -13,12 +13,11 @@ namespace engineapi {
|
||||
Resource() = default;
|
||||
private:
|
||||
RscHandle<Res> mHandle;
|
||||
|
||||
friend class ResourceManager;
|
||||
friend class RscHandle<Res>;
|
||||
};
|
||||
using Resources = std::tuple<
|
||||
class Scene
|
||||
, class Shader
|
||||
class ShaderProgram
|
||||
, class Asset
|
||||
>;
|
||||
template<typename Resource>
|
||||
concept is_resource_v = requires { typename Resource::BaseResource; };
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#include "resource_handle.h"
|
||||
namespace engineapi
|
||||
{
|
||||
|
||||
GenericResourceHandle::GenericResourceHandle(string_view type_name, Guid guid)
|
||||
{
|
||||
|
||||
|
||||
@ -8,9 +8,15 @@ namespace engineapi
|
||||
struct RscHandle
|
||||
{
|
||||
Guid guid{};
|
||||
void* res{};
|
||||
constexpr size_t RscID()const { return ResourceID<Res>; }
|
||||
constexpr RscHandle() noexcept = default;
|
||||
constexpr RscHandle(const Guid& guid) noexcept : guid{ guid } {}
|
||||
template<typename T>
|
||||
constexpr RscHandle(const RscHandle<T>& other) noexcept : guid{ other.guid }, res(other.res) {};
|
||||
constexpr RscHandle(const Guid& guid, Res* res) noexcept : guid{ guid }, res(res) { ((Resource<Res>*)res)->mHandle = *this; }
|
||||
void init();
|
||||
Res* operator->() { if (!res && guid) init(); return (Res*)res; }
|
||||
Res& operator*() { if (!res && guid) init(); return *(Res*)res; }
|
||||
};
|
||||
|
||||
struct GenericResourceHandle
|
||||
@ -21,8 +27,12 @@ namespace engineapi
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::operator=;
|
||||
template<typename T>
|
||||
GenericResourceHandle(RscHandle<T> handle) : Base(RscHandle<typename T::BaseResource>{handle}) {}
|
||||
GenericResourceHandle(string_view type_name, Guid guid);
|
||||
|
||||
template<typename T> RscHandle<T> AsHandle() const {
|
||||
return std::get<ResourceID<T>>(*this);
|
||||
}
|
||||
Guid guid() const;
|
||||
size_t resource_id() const;
|
||||
};
|
||||
|
||||
@ -24,8 +24,7 @@ namespace engineapi {
|
||||
auto ext = path.GetExtension();
|
||||
auto* loader = GetLoader(ext);
|
||||
MetaBundle meta = GetMeta(path);
|
||||
auto res = loader->LoadFile(path, meta);
|
||||
return res;
|
||||
return loader->LoadFile(path, meta);
|
||||
}
|
||||
|
||||
MetaBundle ResourceManager::GetMeta(PackagePath path)
|
||||
|
||||
@ -35,6 +35,9 @@ namespace engineapi {
|
||||
void Shutdown() override;
|
||||
|
||||
public:
|
||||
template<typename Res>
|
||||
Res* Get(const RscHandle<Res>&);
|
||||
|
||||
template<typename Res>
|
||||
auto& GetTable() {
|
||||
return *reinterpret_cast<ResourceStorage<Res>*> (mResourceTable[ResourceID<Res>].get());
|
||||
@ -51,7 +54,7 @@ namespace engineapi {
|
||||
FLoader& RegisterLoader(Name ext, Args&& ... args);
|
||||
|
||||
template<typename Res>
|
||||
LoadResult<Res> Load(PackagePath path, bool reload_resource = true);
|
||||
RscHandle<Res> Load(PackagePath path, bool reload_resource = true);
|
||||
LoadResult<ResourceBundle> Load(PackagePath path, bool reload_resource = true);
|
||||
MetaBundle GetMeta(PackagePath path);
|
||||
};
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
#include "resource_manager.h"
|
||||
#pragma once
|
||||
namespace engineapi {
|
||||
class IFileLoader
|
||||
@ -34,31 +35,43 @@ namespace engineapi {
|
||||
using ResourceHelper = ResourceManager_detail<Resources>;
|
||||
}
|
||||
template<typename Res, typename ...Args>
|
||||
inline RscHandle<Res> ResourceManager::LoaderEmplaceResource(Guid guid, Args && ...args)
|
||||
inline RscHandle<Res> ResourceManager::LoaderEmplaceResource(Guid guid, Args&& ...args)
|
||||
{
|
||||
auto& table = GetTable<Res>();
|
||||
auto& control_block = table[guid]; // don't care just replace
|
||||
// attempt to put on other thread
|
||||
{
|
||||
control_block.resource = new Res(std::forward<Args>(args)...);
|
||||
control_block.resource->mHandle = RscHandle<Res>{ guid };
|
||||
}
|
||||
return RscHandle<Res>{guid};
|
||||
Res* res = new Res(std::forward<Args>(args)...);
|
||||
control_block.resource = res;
|
||||
return RscHandle<Res>{guid, res};
|
||||
}
|
||||
template<typename FLoader, typename ...Args>
|
||||
inline FLoader& ResourceManager::RegisterLoader(Name ext, Args && ...args)
|
||||
inline FLoader& ResourceManager::RegisterLoader(Name ext, Args&& ...args)
|
||||
{
|
||||
FLoader* ptr = new FLoader(std::forward<Args>(args)...);
|
||||
mFileLoader[ext] = ptr;
|
||||
return *ptr;
|
||||
}
|
||||
template<typename Res>
|
||||
inline LoadResult<Res> ResourceManager::Load(PackagePath path, bool reload_resource)
|
||||
inline RscHandle<Res> ResourceManager::Load(PackagePath path, bool reload_resource)
|
||||
{
|
||||
auto res = Load(path, reload_resource);
|
||||
if (!res)
|
||||
return nullptr;
|
||||
return {};
|
||||
|
||||
return res.value().Get<Res>();
|
||||
}
|
||||
template<typename Res>
|
||||
inline Res* ResourceManager::Get(const RscHandle<Res>& handle)
|
||||
{
|
||||
auto& table = GetTable<Res>();
|
||||
auto itr = table.find(handle.guid);
|
||||
if (itr == table.end())
|
||||
return nullptr;
|
||||
return itr->second.resource;
|
||||
}
|
||||
template<typename Res>
|
||||
inline void RscHandle<Res>::init()
|
||||
{
|
||||
res = ResourceManager::GetSingleton().Get(*this);
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include "component.h"
|
||||
#include "asset/asset.h"
|
||||
#include "component.h"
|
||||
#include <map>
|
||||
namespace engineapi {
|
||||
class GameObject {
|
||||
protected:
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
#include "actor.h"
|
||||
#include "asset/asset_manager.h"
|
||||
#include "data/property/actor_property.h"
|
||||
namespace engineapi {
|
||||
ActorMesh::ActorMesh(Model& model, ActorProperty& property)
|
||||
@ -10,8 +9,9 @@ namespace engineapi {
|
||||
}
|
||||
ActorMesh* ActorMesh::New(ActorProperty& property)
|
||||
{
|
||||
Model* model = AssetManager::GetSingletonPtr()->LoadAsset<Model>(property.path, property.flags);
|
||||
return new ActorMesh(*model, property);
|
||||
//Model* model = AssetManager::GetSingletonPtr()->LoadAsset<Model>(property.path, property.flags);
|
||||
//return new ActorMesh(*model, property);
|
||||
return nullptr;
|
||||
}
|
||||
ActorMesh* ActorMesh::New(uint32_t id)
|
||||
{
|
||||
|
||||
@ -2,13 +2,21 @@
|
||||
#include "object/camera/camera.h"
|
||||
#include "render/renderapi.h"
|
||||
#include "object/mesh/actor.h"
|
||||
#include "render/asset/shader.h"
|
||||
#include "data/property/actor_property.h"
|
||||
#include "asset/resource_manager.h"
|
||||
#include "asset/file_manager.h"
|
||||
namespace engineapi {
|
||||
Scene::Scene()
|
||||
{
|
||||
mCamera = new Camera();
|
||||
auto flags = Asset::ASSET_SHARED_FLAG | Asset::ASSET_ASYNC_FLAG;
|
||||
Material* material = new Material("/engine/assets/shader/simple", flags);
|
||||
int flags = 1;
|
||||
auto shader = ResourceManager::GetSingleton().LoaderEmplaceResource<Shader>();
|
||||
shader->mVertexName = type_name<BoneVertex>().View();
|
||||
//shader->mVert = ResourceManager::GetSingleton().Load<ShaderProgram>("/engine/assets/shader/simple.vert");
|
||||
shader->mFrag = ResourceManager::GetSingleton().Load<ShaderProgram>("/engine/assets/shader/simple.frag");
|
||||
RenderAPI::GetSingleton().LoadShader(&*shader);
|
||||
Material* material = new Material("/engine/assets/shader/simple.shader", flags);
|
||||
{
|
||||
ActorProperty property;
|
||||
property.id = 1;
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "scene.h"
|
||||
#include "singleton.h"
|
||||
namespace engineapi {
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
#pragma once
|
||||
#include "asset/render/asset_struct.h"
|
||||
#include "asset/res/resource_handle.h"
|
||||
@ -1,10 +1,8 @@
|
||||
#include "material.h"
|
||||
#include "shader.h"
|
||||
#include "asset/asset_manager.h"
|
||||
#include "../renderapi.h"
|
||||
namespace engineapi {
|
||||
Material::Material(string_view name, uint32_t flags)
|
||||
:Asset(name, flags)
|
||||
{
|
||||
//mShader = new Shader(name, flags);
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "asset_render.h"
|
||||
#include "asset/asset.h"
|
||||
|
||||
namespace engineapi {
|
||||
class Shader;
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
namespace engineapi {
|
||||
void Mesh::BeginLoad()
|
||||
{
|
||||
Asset::BeginLoad();
|
||||
RenderAPI::GetSingletonPtr()->SetStaticMesh(this);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "refl/std/parray.h"
|
||||
#include "asset_render.h"
|
||||
#include "asset/asset.h"
|
||||
#include "../meta/vertex.h"
|
||||
namespace engineapi {
|
||||
using refl::parray;
|
||||
@ -14,14 +14,13 @@ namespace engineapi {
|
||||
public:
|
||||
template<typename T>
|
||||
requires std::is_base_of_v<Vertex, T>
|
||||
Mesh(string_view name, uint32_t flags,vector<T>& vertices, vector<uint32_t>& indices)
|
||||
:Asset(name, flags)
|
||||
, mVertices(vertices)
|
||||
Mesh(vector<T>& vertices, vector<uint32_t>& indices)
|
||||
: mVertices(vertices)
|
||||
, mIndices(indices)
|
||||
{
|
||||
BeginLoad();
|
||||
}
|
||||
void BeginLoad()override;
|
||||
void BeginLoad();
|
||||
|
||||
public:
|
||||
uint32_t& GetVAO() {
|
||||
|
||||
@ -3,11 +3,10 @@
|
||||
#include "assimp/Importer.hpp"
|
||||
#include "assimp/scene.h"
|
||||
#include "assimp/postprocess.h"
|
||||
#include "asset/asset_manager.h"
|
||||
namespace engineapi {
|
||||
void Model::BeginLoad()
|
||||
{
|
||||
Asset::BeginLoad();
|
||||
string mName = "";
|
||||
// 用ASSIMP加载模型文件
|
||||
Assimp::Importer importer;
|
||||
const aiScene* scene = importer.ReadFile(PackagePath::AbsolutePath(mName), aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace
|
||||
@ -104,7 +103,7 @@ namespace engineapi {
|
||||
indices.push_back(face.mIndices[j]);
|
||||
}
|
||||
}
|
||||
return new Mesh(mesh->mName.C_Str(), mFlags, vertices, indices);
|
||||
return new Mesh(vertices, indices);
|
||||
}
|
||||
void Model::Use()
|
||||
{
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "asset_render.h"
|
||||
#include "asset/asset.h"
|
||||
#include "mesh.h"
|
||||
#include "material.h"
|
||||
class aiNode;
|
||||
@ -12,7 +12,7 @@ namespace engineapi {
|
||||
Material* mMaterial;
|
||||
public:
|
||||
using Asset::Asset;
|
||||
void BeginLoad()override;
|
||||
void BeginLoad();
|
||||
void ProcessNode(const aiNode* pNode, const aiScene* pScene);
|
||||
Mesh* ProcessMesh(const aiMesh* mesh);
|
||||
vector<Mesh*>& GetMeshs() {
|
||||
|
||||
@ -9,12 +9,4 @@ namespace engineapi {
|
||||
Shader::~Shader()
|
||||
{
|
||||
}
|
||||
vector<char> Shader::GetVertData()
|
||||
{
|
||||
return FileManager::LoadBinaryFile(PackagePath::AbsolutePath("") + ".vert.spv");
|
||||
}
|
||||
vector<char> Shader::GetFragData()
|
||||
{
|
||||
return FileManager::LoadBinaryFile(PackagePath::AbsolutePath("") + ".frag.spv");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,16 @@
|
||||
#pragma once
|
||||
#include "asset_render.h"
|
||||
#include "asset/asset.h"
|
||||
|
||||
namespace engineapi {
|
||||
class Shader : public Resource<Shader> {
|
||||
class ShaderProgram : public Resource<ShaderProgram> {};
|
||||
class Shader : public Asset {
|
||||
private:
|
||||
uint32_t mId;
|
||||
ShaderInfo mInfo;
|
||||
string mVertexName;
|
||||
RscHandle<ShaderProgram> mVert;
|
||||
RscHandle<ShaderProgram> mFrag;
|
||||
friend class Scene;
|
||||
public:
|
||||
Shader();
|
||||
~Shader();
|
||||
@ -14,7 +20,16 @@ namespace engineapi {
|
||||
ShaderInfo& GetInfo() {
|
||||
return mInfo;
|
||||
}
|
||||
vector<char> GetVertData();
|
||||
vector<char> GetFragData();
|
||||
string_view GetVertexName() {
|
||||
return mVertexName;
|
||||
}
|
||||
template<typename T = ShaderProgram>
|
||||
RscHandle<T> GetVertHandle() {
|
||||
return mVert;
|
||||
}
|
||||
template<typename T = ShaderProgram>
|
||||
RscHandle<T> GetFragHandle() {
|
||||
return mFrag;
|
||||
}
|
||||
};
|
||||
};
|
||||
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "asset_render.h"
|
||||
#include "asset/asset.h"
|
||||
|
||||
namespace engineapi {
|
||||
class Texture : public Asset {
|
||||
|
||||
@ -3,12 +3,11 @@
|
||||
#include "../window.h"
|
||||
#include "object/camera/camera.h"
|
||||
#include "../asset/model.h"
|
||||
#include "asset/asset_manager.h"
|
||||
namespace engineapi {
|
||||
RenderNodeForwardRendering::RenderNodeForwardRendering()
|
||||
:RenderNode(RENDER_FORWARD_RENDERING)
|
||||
{
|
||||
mSky = AssetManager::GetSingletonPtr()->LoadAsset<Model>("assets/models/SkyBoxMesh.ply", Asset::ASSET_SHARED_FLAG | Asset::ASSET_ASYNC_FLAG);
|
||||
//mSky = AssetManager::GetSingletonPtr()->LoadAsset<Model>("assets/models/SkyBoxMesh.ply", Asset::ASSET_SHARED_FLAG | Asset::ASSET_ASYNC_FLAG);
|
||||
//skyBoxMaterial = new Material(new Shader(Resources::GetAssetFullPath("Shaders/SkyBox.zxshader", true), FrameBufferType::Normal));
|
||||
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset_render.h"
|
||||
#include "asset/asset.h"
|
||||
namespace engineapi
|
||||
{
|
||||
class RenderContext {
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
#include "vulkan_glsl_loader.h"
|
||||
#include "vulkanapi/tool/glsl_to_spirv.h"
|
||||
#include "vulkanapi/vulkanapi.h"
|
||||
#include "asset/file_manager.h"
|
||||
#include "render/asset/shader.h"
|
||||
namespace engineapi {
|
||||
#include "render/meta/vertex.h"
|
||||
#include "vkmeta_vertex_gen.inl"
|
||||
using namespace engineapi;
|
||||
namespace vulkanapi {
|
||||
vk::ShaderStageFlagBits GetShaderType(string_view ext)
|
||||
{
|
||||
switch (string_hash(ext))
|
||||
@ -16,19 +19,38 @@ namespace engineapi {
|
||||
default: return vk::ShaderStageFlagBits::eAll;
|
||||
}
|
||||
}
|
||||
void VulkanGlslLoader::Init()
|
||||
{
|
||||
ResourceManager::GetSingleton().RegisterLoader<VulkanGlslLoader>(".geom");
|
||||
ResourceManager::GetSingleton().RegisterLoader<VulkanGlslLoader>(".frag");
|
||||
ResourceManager::GetSingleton().RegisterLoader<VulkanGlslLoader>(".vert");
|
||||
}
|
||||
void VulkanGlslLoader::LoadShaderInfo(const std::string_view& name)
|
||||
{
|
||||
auto it = refl::UClass::MetaTable.find(Name(name));
|
||||
auto meta = it->second->vtable.GetMeta("vkMeta");
|
||||
int x = 1;
|
||||
int y = 2;
|
||||
}
|
||||
//string PreprocessGlsl(string glsl);
|
||||
ResourceBundle VulkanGlslLoader::LoadFile(PackagePath handle, const MetaBundle& meta)
|
||||
{
|
||||
auto m = meta.FetchMeta<Shader>();
|
||||
auto program = m ? ResourceManager::GetSingleton().LoaderEmplaceResource<Shader>(m->guid)
|
||||
: ResourceManager::GetSingleton().LoaderEmplaceResource<Shader>();
|
||||
auto m = meta.FetchMeta<ShaderProgram>();
|
||||
auto program = m ? ResourceManager::GetSingleton().LoaderEmplaceResource<vkShaderProgram>(m->guid)
|
||||
: ResourceManager::GetSingleton().LoaderEmplaceResource<vkShaderProgram>();
|
||||
string glsl = FileManager::LoadTextFile(handle.AbsolutePath());
|
||||
auto shader_enum = GetShaderType(handle.GetExtension());
|
||||
glsl = vulkanapi::GlslToSpirv::PreprocessGlsl(glsl);
|
||||
auto spirv = vulkanapi::GlslToSpirv::spirv(glsl, shader_enum);
|
||||
//if (spirv)
|
||||
//program->Load(shader_enum, {}, string_view{ r_cast<const char*>(spirv->data()),hlp::buffer_size(*spirv) }, glsl);
|
||||
glsl = GlslToSpirv::PreprocessGlsl(glsl);
|
||||
auto spirv = GlslToSpirv::spirv(glsl, shader_enum);
|
||||
if (spirv) {
|
||||
program->Load(*spirv);
|
||||
}
|
||||
return program;
|
||||
}
|
||||
void vkShaderProgram::Load(const std::vector<unsigned int>& spirv)
|
||||
{
|
||||
RenderVulkanAPI* renderApi = RenderVulkanAPI::GetSingletonPtr();
|
||||
mPtr = renderApi->backend.GetDevice().CreateShaderModule(spirv);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,8 +1,22 @@
|
||||
#pragma once
|
||||
#include "render/asset/shader.h"
|
||||
#include "asset/resource_manager.h"
|
||||
namespace engineapi {
|
||||
class VulkanGlslLoader : public IFileLoader
|
||||
#include "vulkanapi/vulkan.h"
|
||||
namespace vulkanapi {
|
||||
class vkShaderProgram : public engineapi::ShaderProgram {
|
||||
private:
|
||||
VkShaderModule mPtr;
|
||||
public:
|
||||
VkShaderModule Ptr() {
|
||||
return mPtr;
|
||||
}
|
||||
void Load(const std::vector<unsigned int>& spirv);
|
||||
};
|
||||
class VulkanGlslLoader : public engineapi::IFileLoader
|
||||
{
|
||||
ResourceBundle LoadFile(PackagePath handle, const MetaBundle& meta) override;
|
||||
public:
|
||||
static void Init();
|
||||
static void LoadShaderInfo(const std::string_view& name);
|
||||
engineapi::ResourceBundle LoadFile(engineapi::PackagePath handle, const engineapi::MetaBundle& meta) override;
|
||||
};
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#include "glsl_to_spirv.h"
|
||||
#include "zstd/table.h"
|
||||
#include "meta/pad.h"
|
||||
#include "meta/hash.h"
|
||||
#include "yaml/yaml.h"
|
||||
#include <shaderc/shaderc.hpp>
|
||||
@ -53,22 +54,10 @@ namespace vulkanapi
|
||||
shaderc::CompileOptions opt;
|
||||
opt.SetTargetEnvironment(shaderc_target_env::shaderc_target_env_vulkan, 0);
|
||||
auto result = compiler.CompileGlslToSpv(val, ConvertStageSC(v_stage), code_id.data(), opt);
|
||||
if (result.GetCompilationStatus() == shaderc_compilation_status::shaderc_compilation_status_success)
|
||||
if (result.GetCompilationStatus() != shaderc_compilation_status::shaderc_compilation_status_success)
|
||||
return spirv_out;
|
||||
spirv_out = vector<unsigned int>{ result.begin(),result.end() };
|
||||
else
|
||||
{
|
||||
string filename = "/" + string{ code_id } + YAML::Text_Serialize(meta::string_hash(glsl));
|
||||
auto err_m = result.GetErrorMessage();
|
||||
auto err_msg = err_m.c_str();
|
||||
//LOG_TO(LogPool::GFX, "%s", err_msg);
|
||||
try
|
||||
{
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
//LOG_TO(LogPool::GFX, "Failed to output glsl [%s] that generated the above error.", filename.c_str());
|
||||
}
|
||||
}
|
||||
//meta::padding_vector(*spirv_out, (unsigned int)4, (unsigned int)0);
|
||||
}
|
||||
return spirv_out;
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
// AMD写的Vulkan内存分配器
|
||||
#include "vk_mem_alloc.h"
|
||||
#include "math/matrix4.h"
|
||||
#include "render/asset/asset_render.h"
|
||||
#include "asset/asset.h"
|
||||
using engineapi::Matrix4;
|
||||
using engineapi::FrameBufferType;
|
||||
using engineapi::ClearInfo;
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
#include "wrapper/descriptorpool.h"
|
||||
#include "window.h"
|
||||
#include "zlog.h"
|
||||
#include "vkmeta_vertex_gen.inl"
|
||||
#include "loader/vulkan_glsl_loader.h"
|
||||
namespace vulkanapi {
|
||||
RenderVulkanAPI::RenderVulkanAPI()
|
||||
:backend("vulkan")
|
||||
@ -27,6 +27,11 @@ namespace vulkanapi {
|
||||
void RenderVulkanAPI::SwitchContext()
|
||||
{
|
||||
|
||||
}
|
||||
void RenderVulkanAPI::Init()
|
||||
{
|
||||
VulkanGlslLoader::Init();
|
||||
RenderAPI::Init();
|
||||
}
|
||||
void RenderVulkanAPI::InitRenderPass()
|
||||
{
|
||||
@ -101,9 +106,9 @@ namespace vulkanapi {
|
||||
// 销毁StagingBuffer
|
||||
Buffer::DestroyBuffer(vertexStagingBuffer, vertexVmaStagingAlloc);
|
||||
Buffer::DestroyBuffer(indexStagingBuffer, indexVmaStagingAlloc);
|
||||
mesh->EndLoad();
|
||||
//mesh->EndLoad();
|
||||
};
|
||||
if (mesh->IsAsync()) {
|
||||
if (true) {
|
||||
Backend::TransferWorker->InvokeBuffer(fn, callback);
|
||||
}
|
||||
else {
|
||||
@ -146,14 +151,13 @@ namespace vulkanapi {
|
||||
}
|
||||
void RenderVulkanAPI::LoadShader(Shader* shader)
|
||||
{
|
||||
VulkanGlslLoader::LoadShaderInfo(shader->GetVertexName());
|
||||
vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
||||
map<VkShaderStageFlagBits, VkShaderModule> shaderModules;
|
||||
std::map<VkShaderStageFlagBits, VkShaderModule> shaderModules;
|
||||
auto device = backend.GetDevice();
|
||||
auto vertData = shader->GetVertData();
|
||||
auto vertModule = device.CreateShaderModule(vertData);
|
||||
auto vertModule = shader->GetVertHandle<vkShaderProgram>()->Ptr();
|
||||
shaderModules.insert(make_pair(VK_SHADER_STAGE_VERTEX_BIT, vertModule));
|
||||
auto fragData = shader->GetFragData();
|
||||
auto fragModule = device.CreateShaderModule(fragData);
|
||||
auto fragModule = shader->GetVertHandle<vkShaderProgram>()->Ptr();
|
||||
shaderModules.insert(make_pair(VK_SHADER_STAGE_FRAGMENT_BIT, fragModule));
|
||||
for (auto& shaderModule : shaderModules)
|
||||
{
|
||||
|
||||
@ -18,6 +18,7 @@ namespace vulkanapi
|
||||
vector<VulkanVAO*> VAOList;
|
||||
vector<RenderPass*> PassList;
|
||||
vector<VulkanPipeline*> PiPelineList;
|
||||
hash_table<Guid, void*> ShaderInfoList;
|
||||
public:
|
||||
RenderVulkanAPI();
|
||||
~RenderVulkanAPI()override;
|
||||
@ -28,6 +29,7 @@ namespace vulkanapi
|
||||
public:
|
||||
void SwitchContext()override;
|
||||
public:
|
||||
void Init() override;
|
||||
void InitRenderPass()override;
|
||||
public:
|
||||
void SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset = 0, uint32_t yOffset = 0)override;
|
||||
|
||||
@ -91,5 +91,17 @@ namespace vulkanapi {
|
||||
VkResult result = vkCreateShaderModule(mPtr, &createInfo, nullptr, &module);
|
||||
return module;
|
||||
}
|
||||
VkShaderModule Device::CreateShaderModule(vector<unsigned int> code)
|
||||
{
|
||||
VkShaderModuleCreateInfo createInfo = {};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||
// 这里需要确保数据满足uint32_t的对齐要求,存储在vector中,默认分配器已经确保数据满足最差情况下的对齐要求
|
||||
createInfo.codeSize = code.size();
|
||||
// 转换为Vulkan要求的uint32_t指针
|
||||
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
|
||||
VkShaderModule module;
|
||||
VkResult result = vkCreateShaderModule(mPtr, &createInfo, nullptr, &module);
|
||||
return module;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -25,5 +25,6 @@ namespace vulkanapi {
|
||||
VkFence CreateFence();
|
||||
VkSemaphore CreateSemaphore();
|
||||
VkShaderModule CreateShaderModule(vector<char> code);
|
||||
VkShaderModule CreateShaderModule(vector<unsigned int> code);
|
||||
};
|
||||
};
|
||||
@ -1,22 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include "asset/file_manager.h"
|
||||
#include "asset/resource_manager.h"
|
||||
#include "vulkanapi/loader/vulkan_glsl_loader.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "zlog.h"
|
||||
using namespace engineapi;
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
vector<ISystem*> SystemList;
|
||||
SystemList.push_back(new FileManager());
|
||||
SystemList.push_back(new ResourceManager());
|
||||
for (auto system : SystemList) {
|
||||
system->Init();
|
||||
}
|
||||
for (auto system : SystemList) {
|
||||
system->LateInit();
|
||||
}
|
||||
ResourceManager::GetSingleton().RegisterLoader<VulkanGlslLoader>(FName(".frag"));
|
||||
ResourceManager::GetSingleton().Load("/engine/assets/shader/simple.frag", true);
|
||||
const char* name = "zengine";
|
||||
App game(name);
|
||||
game.Launch();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user