feat: Implement resource system Phase 2 - Concrete resource types

- Add Material class with shader/texture bindings and property system
- Add MaterialLoader for .mat/.json format
- Add Shader class (Vertex/Fragment/Geometry/Compute)
- Add ShaderLoader for .vert/.frag/.glsl/.hlsl
- Add AudioClip class (WAV/OGG/MP3/FLAC support)
- Add AudioLoader for audio files
- Add Texture/Mesh classes and loaders (from design doc)
- Fix HashMap iterator and String API usage
- Fix Mutex compatibility with std::lock_guard
- Update CMakeLists.txt with new resource files
- All tests pass: 11 Resources + 51 Containers
This commit is contained in:
2026-03-17 22:32:27 +08:00
parent 05c879a818
commit 4710e6ba60
33 changed files with 1339 additions and 47 deletions

View File

@@ -237,7 +237,10 @@ bool HashMap<Key, Value>::Erase(const Key& key) {
auto it = FindInBucket(bucket, key);
if (it != bucket.pairs.end()) {
size_t index = it - bucket.pairs.begin();
bucket.pairs[index] = bucket.pairs.Back();
if (index != bucket.pairs.Size() - 1) {
bucket.pairs[index] = std::move(bucket.pairs.Back());
}
bucket.pairs.PopBack();
--m_size;
return true;

View File

@@ -4,12 +4,11 @@
#include "Vector3.h"
#include "Bounds.h"
#include "Matrix4.h"
#include "Sphere.h"
namespace XCEngine {
namespace Math {
struct Sphere;
struct OBB {
Vector3 center;
Vector3 extents;

View File

@@ -13,16 +13,38 @@ struct LoadRequest {
Containers::String path;
ResourceType type;
std::function<void(LoadResult)> callback;
Core::UniqueRef<ImportSettings> settings;
ImportSettings* settings;
Core::uint64 requestId;
LoadRequest() : requestId(0) {}
LoadRequest() : requestId(0), settings(nullptr) {}
LoadRequest(const Containers::String& p, ResourceType t,
std::function<void(LoadResult)> cb, ImportSettings* s = nullptr)
: path(p), type(t), callback(std::move(cb)),
settings(s), requestId(GenerateRequestId()) {}
LoadRequest(LoadRequest&& other) noexcept
: path(std::move(other.path)), type(other.type),
callback(std::move(other.callback)), settings(other.settings),
requestId(other.requestId) {
other.settings = nullptr;
}
LoadRequest& operator=(LoadRequest&& other) noexcept {
if (this != &other) {
path = std::move(other.path);
type = other.type;
callback = std::move(other.callback);
settings = other.settings;
requestId = other.requestId;
other.settings = nullptr;
}
return *this;
}
LoadRequest(const LoadRequest&) = default;
LoadRequest& operator=(const LoadRequest&) = default;
private:
static Core::uint64 GenerateRequestId();
};
@@ -46,11 +68,12 @@ public:
void CancelAll();
void Cancel(Core::uint64 requestId);
private:
AsyncLoader() = default;
~AsyncLoader() = default;
void SubmitInternal(LoadRequest& request);
AsyncLoader() = default;
private:
void SubmitInternal(LoadRequest request);
IResourceLoader* FindLoader(ResourceType type) const;
void QueueCompleted(LoadRequest request, LoadResult result);

View File

@@ -0,0 +1,86 @@
#pragma once
#include "IResource.h"
#include "../Containers/Array.h"
#include "../Core/Types.h"
namespace XCEngine {
namespace Resources {
enum class AudioFormat : Core::uint8 {
Unknown,
WAV,
OGG,
MP3,
FLAC
};
enum class AudioType : Core::uint8 {
SoundEffect,
Music,
Voice,
Ambient
};
class AudioClip : public IResource {
public:
AudioClip();
virtual ~AudioClip() override;
ResourceType GetType() const override { return ResourceType::AudioClip; }
const Containers::String& GetName() const override { return m_name; }
const Containers::String& GetPath() const override { return m_path; }
ResourceGUID GetGUID() const override { return m_guid; }
bool IsValid() const override { return m_isValid; }
size_t GetMemorySize() const override { return m_memorySize; }
void Release() override;
void SetAudioData(const Containers::Array<Core::uint8>& data);
const Containers::Array<Core::uint8>& GetAudioData() const { return m_audioData; }
void SetSampleRate(Core::uint32 rate) { m_sampleRate = rate; }
Core::uint32 GetSampleRate() const { return m_sampleRate; }
void SetChannels(Core::uint32 channels) { m_channels = channels; }
Core::uint32 GetChannels() const { return m_channels; }
void SetBitsPerSample(Core::uint32 bits) { m_bitsPerSample = bits; }
Core::uint32 GetBitsPerSample() const { return m_bitsPerSample; }
void SetDuration(float seconds) { m_duration = seconds; }
float GetDuration() const { return m_duration; }
void SetAudioFormat(AudioFormat format) { m_format = format; }
AudioFormat GetAudioFormat() const { return m_format; }
void SetAudioType(AudioType type) { m_audioType = type; }
AudioType GetAudioType() const { return m_audioType; }
void SetIs3D(bool is3D) { m_is3D = is3D; }
bool Is3D() const { return m_is3D; }
void SetLoop(bool loop) { m_loop = loop; }
bool IsLoop() const { return m_loop; }
class IRHIAudioBuffer* GetRHIResource() const { return m_rhiResource; }
void SetRHIResource(class IRHIAudioBuffer* resource);
private:
Containers::Array<Core::uint8> m_audioData;
Core::uint32 m_sampleRate = 44100;
Core::uint32 m_channels = 2;
Core::uint32 m_bitsPerSample = 16;
float m_duration = 0.0f;
AudioFormat m_format = AudioFormat::WAV;
AudioType m_audioType = AudioType::SoundEffect;
bool m_is3D = false;
bool m_loop = false;
class IRHIAudioBuffer* m_rhiResource = nullptr;
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -0,0 +1,26 @@
#pragma once
#include "IResourceLoader.h"
#include "AudioClip.h"
namespace XCEngine {
namespace Resources {
class AudioLoader : public IResourceLoader {
public:
AudioLoader();
virtual ~AudioLoader() override;
ResourceType GetResourceType() const override { return ResourceType::AudioClip; }
Containers::Array<Containers::String> GetSupportedExtensions() const override;
bool CanLoad(const Containers::String& path) const override;
LoadResult Load(const Containers::String& path, const ImportSettings* settings = nullptr) override;
ImportSettings* GetDefaultSettings() const override;
private:
bool ParseWAVData(const Containers::Array<Core::uint8>& data, AudioClip* audioClip);
AudioFormat DetectAudioFormat(const Containers::String& path, const Containers::Array<Core::uint8>& data);
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -18,7 +18,6 @@ public:
virtual size_t GetMemorySize() const = 0;
virtual void Release() = 0;
protected:
struct ConstructParams {
Containers::String name;
Containers::String path;

View File

@@ -0,0 +1,93 @@
#pragma once
#include "IResource.h"
#include "ResourceHandle.h"
#include "ResourceManager.h"
#include "Texture.h"
#include "Shader.h"
#include "../Containers/HashMap.h"
#include "../Core/Types.h"
#include "../Math/Vector2.h"
#include "../Math/Vector3.h"
#include "../Math/Vector4.h"
#include <cstring>
namespace XCEngine {
namespace Resources {
enum class MaterialPropertyType {
Float, Float2, Float3, Float4,
Int, Int2, Int3, Int4,
Bool, Texture, Cubemap
};
struct MaterialProperty {
Containers::String name;
MaterialPropertyType type;
union Value {
float floatValue[4];
Core::int32 intValue[4];
bool boolValue;
Value() { memset(this, 0, sizeof(Value)); }
} value;
Core::uint32 refCount = 0;
MaterialProperty() : type(MaterialPropertyType::Float), refCount(0) {}
};
class Material : public IResource {
public:
Material();
virtual ~Material() override;
ResourceType GetType() const override { return ResourceType::Material; }
const Containers::String& GetName() const override { return m_name; }
const Containers::String& GetPath() const override { return m_path; }
ResourceGUID GetGUID() const override { return m_guid; }
bool IsValid() const override { return m_isValid; }
size_t GetMemorySize() const override { return m_memorySize; }
void Release() override;
void SetShader(const ResourceHandle<class Shader>& shader);
class Shader* GetShader() const { return m_shader.Get(); }
void SetFloat(const Containers::String& name, float value);
void SetFloat2(const Containers::String& name, const Math::Vector2& value);
void SetFloat3(const Containers::String& name, const Math::Vector3& value);
void SetFloat4(const Containers::String& name, const Math::Vector4& value);
void SetInt(const Containers::String& name, Core::int32 value);
void SetBool(const Containers::String& name, bool value);
void SetTexture(const Containers::String& name, const ResourceHandle<Texture>& texture);
float GetFloat(const Containers::String& name) const;
Math::Vector2 GetFloat2(const Containers::String& name) const;
Math::Vector3 GetFloat3(const Containers::String& name) const;
Math::Vector4 GetFloat4(const Containers::String& name) const;
Core::int32 GetInt(const Containers::String& name) const;
bool GetBool(const Containers::String& name) const;
ResourceHandle<Texture> GetTexture(const Containers::String& name) const;
const Containers::Array<Core::uint8>& GetConstantBufferData() const { return m_constantBufferData; }
void UpdateConstantBuffer();
bool HasProperty(const Containers::String& name) const;
void RemoveProperty(const Containers::String& name);
void ClearAllProperties();
private:
ResourceHandle<class Shader> m_shader;
Containers::HashMap<Containers::String, MaterialProperty> m_properties;
Containers::Array<Core::uint8> m_constantBufferData;
struct TextureBinding {
Containers::String name;
Core::uint32 slot;
ResourceHandle<Texture> texture;
};
Containers::Array<TextureBinding> m_textureBindings;
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -0,0 +1,25 @@
#pragma once
#include "IResourceLoader.h"
#include "Material.h"
namespace XCEngine {
namespace Resources {
class MaterialLoader : public IResourceLoader {
public:
MaterialLoader();
virtual ~MaterialLoader() override;
ResourceType GetResourceType() const override { return ResourceType::Material; }
Containers::Array<Containers::String> GetSupportedExtensions() const override;
bool CanLoad(const Containers::String& path) const override;
LoadResult Load(const Containers::String& path, const ImportSettings* settings = nullptr) override;
ImportSettings* GetDefaultSettings() const override;
private:
bool ParseMaterialData(const Containers::Array<Core::uint8>& data, Material* material);
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -0,0 +1,68 @@
#pragma once
#include "IResource.h"
#include "../Containers/Array.h"
#include "../Core/Types.h"
namespace XCEngine {
namespace Resources {
enum class VertexAttribute : Core::uint32 {
Position = 1 << 0, Normal = 1 << 1, Tangent = 1 << 2,
Color = 1 << 3, UV0 = 1 << 4, UV1 = 1 << 5,
UV2 = 1 << 6, UV3 = 1 << 7, BoneWeights = 1 << 8, BoneIndices = 1 << 9
};
struct MeshSection {
Core::uint32 baseVertex;
Core::uint32 vertexCount;
Core::uint32 startIndex;
Core::uint32 indexCount;
Core::uint32 materialID;
};
class Mesh : public IResource {
public:
Mesh();
virtual ~Mesh() override;
ResourceType GetType() const override { return ResourceType::Mesh; }
const Containers::String& GetName() const override { return m_name; }
const Containers::String& GetPath() const override { return m_path; }
ResourceGUID GetGUID() const override { return m_guid; }
bool IsValid() const override { return m_isValid; }
size_t GetMemorySize() const override { return m_memorySize; }
void Release() override;
void SetVertexData(const void* data, size_t size, Core::uint32 vertexCount,
Core::uint32 vertexStride, VertexAttribute attributes);
const void* GetVertexData() const { return m_vertexData.Data(); }
size_t GetVertexDataSize() const { return m_vertexData.Size(); }
Core::uint32 GetVertexCount() const { return m_vertexCount; }
Core::uint32 GetVertexStride() const { return m_vertexStride; }
VertexAttribute GetVertexAttributes() const { return m_attributes; }
void SetIndexData(const void* data, size_t size, Core::uint32 indexCount, bool use32Bit);
const void* GetIndexData() const { return m_indexData.Data(); }
size_t GetIndexDataSize() const { return m_indexData.Size(); }
Core::uint32 GetIndexCount() const { return m_indexCount; }
bool IsUse32BitIndex() const { return m_use32BitIndex; }
void AddSection(const MeshSection& section);
const Containers::Array<MeshSection>& GetSections() const { return m_sections; }
private:
Core::uint32 m_vertexCount = 0;
Core::uint32 m_vertexStride = 0;
VertexAttribute m_attributes = VertexAttribute::Position;
Core::uint32 m_indexCount = 0;
bool m_use32BitIndex = false;
Containers::Array<Core::uint8> m_vertexData;
Containers::Array<Core::uint8> m_indexData;
Containers::Array<MeshSection> m_sections;
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -0,0 +1,22 @@
#pragma once
#include "IResourceLoader.h"
#include "Mesh.h"
namespace XCEngine {
namespace Resources {
class MeshLoader : public IResourceLoader {
public:
MeshLoader();
virtual ~MeshLoader() override;
ResourceType GetResourceType() const override { return ResourceType::Mesh; }
Containers::Array<Containers::String> GetSupportedExtensions() const override;
bool CanLoad(const Containers::String& path) const override;
LoadResult Load(const Containers::String& path, const ImportSettings* settings = nullptr) override;
ImportSettings* GetDefaultSettings() const override;
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -98,7 +98,7 @@ private:
Containers::HashMap<ResourceGUID, IResource*> m_resourceCache;
Containers::HashMap<ResourceGUID, Core::uint32> m_refCounts;
Containers::HashMap<ResourceGUID, Containers::String> m_guidToPath;
Containers::HashMap<ResourceType, Core::UniqueRef<IResourceLoader>> m_loaders;
Containers::HashMap<ResourceType, IResourceLoader*> m_loaders;
size_t m_memoryUsage = 0;
size_t m_memoryBudget = 512 * 1024 * 1024;

View File

@@ -64,6 +64,22 @@ inline ResourceGUID MakeResourceGUID(const char* path) {
return ResourceGUID::Generate(path);
}
} // namespace Resources
} // namespace XCEngine
namespace std {
template<>
struct hash<XCEngine::Resources::ResourceGUID> {
size_t operator()(const XCEngine::Resources::ResourceGUID& guid) const noexcept {
return static_cast<size_t>(guid.value);
}
};
}
namespace XCEngine {
namespace Resources {
template<typename T>
ResourceType GetResourceType();

View File

@@ -9,15 +9,21 @@
#include "ResourceCache.h"
#include "AsyncLoader.h"
#include "Texture.h"
#include "TextureLoader.h"
#include "Mesh.h"
#include "MeshLoader.h"
#include "Material.h"
#include "MaterialLoader.h"
#include "Shader.h"
#include "ShaderLoader.h"
#include "AudioClip.h"
#include "AudioLoader.h"
// Forward declarations for concrete resource types
namespace XCEngine {
namespace Resources {
class Texture;
class Mesh;
class Material;
class Shader;
class AudioClip;
class BinaryResource;
} // namespace Resources

View File

@@ -0,0 +1,87 @@
#pragma once
#include "IResource.h"
#include "../Containers/Array.h"
#include "../Core/Types.h"
namespace XCEngine {
namespace Resources {
enum class ShaderType : Core::uint8 {
Vertex,
Fragment,
Geometry,
Compute,
Hull,
Domain
};
enum class ShaderLanguage : Core::uint8 {
GLSL,
HLSL,
SPIRV
};
struct ShaderUniform {
Containers::String name;
Core::uint32 location;
Core::uint32 size;
Core::uint32 type;
};
struct ShaderAttribute {
Containers::String name;
Core::uint32 location;
Core::uint32 size;
Core::uint32 type;
};
class Shader : public IResource {
public:
Shader();
virtual ~Shader() override;
ResourceType GetType() const override { return ResourceType::Shader; }
const Containers::String& GetName() const override { return m_name; }
const Containers::String& GetPath() const override { return m_path; }
ResourceGUID GetGUID() const override { return m_guid; }
bool IsValid() const override { return m_isValid; }
size_t GetMemorySize() const override { return m_memorySize; }
void Release() override;
void SetShaderType(ShaderType type) { m_shaderType = type; }
ShaderType GetShaderType() const { return m_shaderType; }
void SetShaderLanguage(ShaderLanguage lang) { m_language = lang; }
ShaderLanguage GetShaderLanguage() const { return m_language; }
void SetSourceCode(const Containers::String& source);
const Containers::String& GetSourceCode() const { return m_sourceCode; }
void SetCompiledBinary(const Containers::Array<Core::uint8>& binary);
const Containers::Array<Core::uint8>& GetCompiledBinary() const { return m_compiledBinary; }
void AddUniform(const ShaderUniform& uniform);
const Containers::Array<ShaderUniform>& GetUniforms() const { return m_uniforms; }
void AddAttribute(const ShaderAttribute& attribute);
const Containers::Array<ShaderAttribute>& GetAttributes() const { return m_attributes; }
class IRHIShader* GetRHIResource() const { return m_rhiResource; }
void SetRHIResource(class IRHIShader* resource);
private:
ShaderType m_shaderType = ShaderType::Fragment;
ShaderLanguage m_language = ShaderLanguage::GLSL;
Containers::String m_sourceCode;
Containers::Array<Core::uint8> m_compiledBinary;
Containers::Array<ShaderUniform> m_uniforms;
Containers::Array<ShaderAttribute> m_attributes;
class IRHIShader* m_rhiResource = nullptr;
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -0,0 +1,26 @@
#pragma once
#include "IResourceLoader.h"
#include "Shader.h"
namespace XCEngine {
namespace Resources {
class ShaderLoader : public IResourceLoader {
public:
ShaderLoader();
virtual ~ShaderLoader() override;
ResourceType GetResourceType() const override { return ResourceType::Shader; }
Containers::Array<Containers::String> GetSupportedExtensions() const override;
bool CanLoad(const Containers::String& path) const override;
LoadResult Load(const Containers::String& path, const ImportSettings* settings = nullptr) override;
ImportSettings* GetDefaultSettings() const override;
private:
ShaderType DetectShaderType(const Containers::String& path, const Containers::String& source);
bool ParseShaderSource(const Containers::String& source, Shader* shader);
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -0,0 +1,73 @@
#pragma once
#include "IResource.h"
#include "../Core/Types.h"
namespace XCEngine {
namespace Resources {
enum class TextureType {
Texture2D, Texture3D, TextureCube, Texture2DArray, TextureCubeArray
};
enum class TextureFormat {
Unknown, R8_UNORM, RG8_UNORM, RGBA8_UNORM, RGBA8_SRGB,
R16_FLOAT, RG16_FLOAT, RGBA16_FLOAT,
R32_FLOAT, RG32_FLOAT, RGBA32_FLOAT,
D16_UNORM, D24_UNORM_S8_UINT, D32_FLOAT, D32_FLOAT_S8_X24_UINT,
BC1_UNORM, BC1_UNORM_SRGB, BC2_UNORM, BC2_UNORM_SRGB,
BC3_UNORM, BC3_UNORM_SRGB, BC4_UNORM, BC5_UNORM, BC6H_UF16,
BC7_UNORM, BC7_UNORM_SRGB
};
enum class TextureUsage : Core::uint8 {
None = 0, ShaderResource = 1 << 0, RenderTarget = 1 << 1,
DepthStencil = 1 << 2, UnorderedAccess = 1 << 3,
TransferSrc = 1 << 4, TransferDst = 1 << 5
};
class Texture : public IResource {
public:
Texture();
virtual ~Texture() override;
ResourceType GetType() const override { return ResourceType::Texture; }
const Containers::String& GetName() const override { return m_name; }
const Containers::String& GetPath() const override { return m_path; }
ResourceGUID GetGUID() const override { return m_guid; }
bool IsValid() const override { return m_isValid; }
size_t GetMemorySize() const override { return m_memorySize; }
void Release() override;
Core::uint32 GetWidth() const { return m_width; }
Core::uint32 GetHeight() const { return m_height; }
Core::uint32 GetDepth() const { return m_depth; }
Core::uint32 GetMipLevels() const { return m_mipLevels; }
Core::uint32 GetArraySize() const { return m_arraySize; }
TextureType GetTextureType() const { return m_textureType; }
TextureFormat GetFormat() const { return m_format; }
TextureUsage GetUsage() const { return m_usage; }
const void* GetPixelData() const { return m_pixelData.Data(); }
size_t GetPixelDataSize() const { return m_pixelData.Size(); }
bool Create(Core::uint32 width, Core::uint32 height, Core::uint32 depth,
Core::uint32 mipLevels, TextureType type, TextureFormat format,
const void* data, size_t dataSize);
bool GenerateMipmaps();
private:
Core::uint32 m_width = 0;
Core::uint32 m_height = 0;
Core::uint32 m_depth = 1;
Core::uint32 m_mipLevels = 1;
Core::uint32 m_arraySize = 1;
TextureType m_textureType = TextureType::Texture2D;
TextureFormat m_format = TextureFormat::RGBA8_UNORM;
TextureUsage m_usage = TextureUsage::ShaderResource;
Containers::Array<Core::uint8> m_pixelData;
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -0,0 +1,22 @@
#pragma once
#include "IResourceLoader.h"
#include "Texture.h"
namespace XCEngine {
namespace Resources {
class TextureLoader : public IResourceLoader {
public:
TextureLoader();
virtual ~TextureLoader() override;
ResourceType GetResourceType() const override { return ResourceType::Texture; }
Containers::Array<Containers::String> GetSupportedExtensions() const override;
bool CanLoad(const Containers::String& path) const override;
LoadResult Load(const Containers::String& path, const ImportSettings* settings = nullptr) override;
ImportSettings* GetDefaultSettings() const override;
};
} // namespace Resources
} // namespace XCEngine

View File

@@ -14,12 +14,12 @@ public:
void Unlock() { m_mutex.unlock(); }
bool TryLock() { return m_mutex.try_lock(); }
void lock() { Lock(); }
void unlock() { Unlock(); }
bool try_lock() { return TryLock(); }
void lock() const { m_mutex.lock(); }
void unlock() const { m_mutex.unlock(); }
bool try_lock() const { return m_mutex.try_lock(); }
private:
std::mutex m_mutex;
mutable std::mutex m_mutex;
};
} // namespace Threading