117 lines
3.6 KiB
C++
117 lines
3.6 KiB
C++
// =============================================
|
|
// Aster: asset_loader.h
|
|
// Copyright (c) 2020-2024 Anish Bhobe
|
|
// =============================================
|
|
|
|
#pragma once
|
|
|
|
#include "aster/aster.h"
|
|
|
|
#include "aster/core/buffer.h"
|
|
#include "render_resource_manager.h"
|
|
|
|
#include "ecs_adapter.h"
|
|
|
|
namespace tinygltf
|
|
{
|
|
class Model;
|
|
struct Image;
|
|
} // namespace tinygltf
|
|
|
|
struct Image;
|
|
struct Texture;
|
|
|
|
constexpr auto GLTF_ASCII_FILE_EXTENSION = ".gltf";
|
|
constexpr auto GLTF_BINARY_FILE_EXTENSION = ".glb";
|
|
|
|
struct Material
|
|
{
|
|
vec4 m_AlbedoFactor; // 16 16
|
|
vec3 m_EmissionFactor; // 12 28
|
|
f32 m_MetalFactor; // 04 32
|
|
f32 m_RoughFactor; // 04 36
|
|
TextureHandle m_AlbedoTex; // 04 40
|
|
TextureHandle m_NormalTex; // 04 44
|
|
TextureHandle m_MetalRoughTex; // 04 48
|
|
TextureHandle m_OcclusionTex; // 04 52
|
|
TextureHandle m_EmissionTex; // 04 56
|
|
f32 m_AlphaBlend; /* 04 60
|
|
AlphaBlend < 0 means Opaque
|
|
AlphaBlend > 1 means Blend
|
|
AlphaBlend in [0,1] means cutoff */
|
|
};
|
|
|
|
// If this is changed, ensure the shaders are changed accordingly.
|
|
static_assert(sizeof(Material) == 60);
|
|
|
|
struct VertexData
|
|
{
|
|
vec4 m_Normal;
|
|
vec2 m_TexCoord0 = vec2{0.0f, 0.0f};
|
|
vec2 m_TexCoord1 = vec2{0.0f, 0.0f};
|
|
vec4 m_Color0 = vec4{1.0f, 1.0f, 1.0f, 1.0f};
|
|
};
|
|
|
|
struct Model
|
|
{
|
|
eastl::vector<TextureHandle> m_Textures;
|
|
eastl::vector<Entity> m_Entities;
|
|
|
|
IndexHandle m_IndexHandle;
|
|
GeometryHandle m_VertexPositionHandle;
|
|
GeometryHandle m_VertexDataHandle;
|
|
MaterialHandle m_MaterialHandle;
|
|
Entity m_RootEntity;
|
|
|
|
void Destroy(RenderResourceManager *resourceManager, EcsRegistry *registry);
|
|
};
|
|
|
|
struct CModel
|
|
{
|
|
};
|
|
|
|
struct AssetLoader
|
|
{
|
|
RenderResourceManager *m_ResourceManager;
|
|
entt::registry *m_Registry;
|
|
|
|
vk::CommandPool m_CommandPool;
|
|
vk::CommandBuffer m_CommandBuffer;
|
|
vk::Queue m_TransferQueue;
|
|
u32 m_TransferQueueIndex;
|
|
u32 m_GraphicsQueueIndex;
|
|
|
|
void LoadHdrImage(Texture *texture, cstr path, cstr name = nullptr) const;
|
|
Model LoadModelToGpu(cstr path, cstr name = nullptr);
|
|
|
|
constexpr static auto ANormal = "NORMAL";
|
|
constexpr static auto APosition = "POSITION";
|
|
constexpr static auto ATangent = "TANGENT";
|
|
constexpr static auto ATexCoord0 = "TEXCOORD_0";
|
|
constexpr static auto ATexCoord1 = "TEXCOORD_1";
|
|
constexpr static auto AColor0 = "COLOR_0";
|
|
constexpr static auto AJoints0 = "JOINTS_0";
|
|
constexpr static auto AWeights0 = "WEIGHTS_0";
|
|
|
|
AssetLoader(RenderResourceManager *resourceManager, EcsRegistry *registry, vk::Queue transferQueue,
|
|
u32 transferQueueIndex, u32 graphicsQueueIndex);
|
|
~AssetLoader();
|
|
|
|
AssetLoader(AssetLoader &&other) noexcept;
|
|
AssetLoader &operator=(AssetLoader &&other) noexcept;
|
|
|
|
private:
|
|
TextureHandle LoadImageToGpu(StagingBuffer *stagingBuffer, tinygltf::Image *image, bool isSrgb) const;
|
|
void
|
|
ProcessNode(tinygltf::Model *model, eastl::vector<vec4> *vertexPositions, eastl::vector<VertexData> *vertexData,
|
|
eastl::vector<u32> *indices, eastl::vector<Entity> *entities,
|
|
const std::function<u32(i32)> &loadMaterial, int current, Entity parent);
|
|
|
|
public:
|
|
DISALLOW_COPY_AND_ASSIGN(AssetLoader);
|
|
};
|
|
|
|
void GenerateMipMaps(vk::CommandBuffer commandBuffer, Texture *texture, vk::ImageLayout initialLayout,
|
|
vk::ImageLayout finalLayout,
|
|
vk::PipelineStageFlags2 prevStage = vk::PipelineStageFlagBits2::eAllCommands,
|
|
vk::PipelineStageFlags2 finalStage = vk::PipelineStageFlagBits2::eAllCommands); |