Skip to content

Commit

Permalink
Merge pull request #12 from GuusKemperman/async-thumbnails
Browse files Browse the repository at this point in the history
Thumbnails generated asynchronously
  • Loading branch information
GuusKemperman authored Aug 15, 2024
2 parents 331b167 + 1d79e85 commit d17a6ba
Show file tree
Hide file tree
Showing 36 changed files with 330 additions and 550 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/WindowsBuildAndRun.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,4 @@ jobs:
echo "Exit Code: "
echo $process.ExitCode
# Check if we are currently building for Release or EditorRelease AND $process.ExitCode == -1073741819,
# we are ignoring this specific exit code due to a bug. Somehow the compiler causes an invalid memory access
# BUT only sometimes! And ONLY on the github servers (so far).
# The bug is difficult to resolve due to its nature, it has many possible origins.
# We are ignoring this specific warning because it is slowing down our pull requests too much to have to
# re-run the tests
if (("${{matrix.build-configuration}}" -eq "Release" -or "${{matrix.build-configuration}}" -eq "EditorRelease") -and ($process.ExitCode -eq -1073741819))
{
echo "Ignoring specific exit code -1073741819 for Release or EditorRelease configuration"
echo "This bug is of unknown origin and will be resolved in time"
exit 0
}
exit $process.ExitCode
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.vcxproj.user
Intermediate/
Build/
/Assets/PolygonPack
4 changes: 2 additions & 2 deletions Coral.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@
</ClCompile>
<ClCompile Include="Source\Assets\Core\AssetSaveInfo.cpp" />
<ClCompile Include="Source\Assets\Core\AssetLoadInfo.cpp" />
<ClCompile Include="Source\Assets\Core\AssetFileMetaData.cpp" />
<ClCompile Include="Source\Assets\Core\AssetMetaData.cpp" />
<ClCompile Include="source\Meta\MetaType.cpp" />
<ClCompile Include="source\Assets\Material.cpp" />
<ClCompile Include="source\Systems\Particles\ParticleDebugVisualizationSystem.cpp" />
Expand Down Expand Up @@ -893,7 +893,7 @@
<ClInclude Include="External\predicates\constants.h" />
<ClInclude Include="External\predicates\predicates.h" />
<ClInclude Include="Include\Assets\Asset.h" />
<ClInclude Include="Include\Assets\Core\AssetFileMetaData.h" />
<ClInclude Include="Include\Assets\Core\AssetMetaData.h" />
<ClInclude Include="Include\Assets\Core\AssetLoadInfo.h" />
<ClInclude Include="Include\Assets\Core\AssetSaveInfo.h" />
<ClInclude Include="Include\Assets\Core\ImportedAsset.h" />
Expand Down
4 changes: 2 additions & 2 deletions Include/Assets/Asset.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "Meta/MetaReflect.h"
#include "Assets/Core/AssetFileMetaData.h"
#include "Assets/Core/AssetMetaData.h"

namespace CE
{
Expand Down Expand Up @@ -46,7 +46,7 @@ namespace CE
In order to implement your own implementation
for saving an asset, override OnSave.
*/
[[nodiscard]] AssetSaveInfo Save(const std::optional<AssetFileMetaData::ImporterInfo>& importerInfo = std::nullopt) const;
[[nodiscard]] AssetSaveInfo Save(const std::optional<AssetMetaData::ImporterInfo>& importerInfo = std::nullopt) const;

private:
//********************************//
Expand Down
55 changes: 42 additions & 13 deletions Include/Assets/Core/AssetHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace CE
AssetHandleBase();
AssetHandleBase(nullptr_t);

AssetHandleBase(std::shared_ptr<Internal::AssetInternal> assetInternal);

AssetHandleBase(AssetHandleBase&& other) noexcept;

AssetHandleBase& operator=(AssetHandleBase&& other) noexcept;
Expand All @@ -26,7 +28,7 @@ namespace CE

operator bool() const;

const AssetFileMetaData& GetMetaData() const;
const AssetMetaData& GetMetaData() const;

bool IsLoaded() const;

Expand All @@ -47,7 +49,7 @@ namespace CE

bool IsA(TypeId type) const;

Internal::AssetInternal* mAssetInternal{};
std::shared_ptr<Internal::AssetInternal> mAssetInternal{};
};

template<Internal::AssetInternal::RefCountType IndexOfCounter>
Expand All @@ -59,7 +61,7 @@ namespace CE

AssetHandleRefCounter(nullptr_t);

AssetHandleRefCounter(Internal::AssetInternal* assetInternal);
AssetHandleRefCounter(std::shared_ptr<Internal::AssetInternal> assetInternal);

AssetHandleRefCounter(AssetHandleRefCounter&& other) noexcept;

Expand All @@ -85,6 +87,17 @@ namespace CE
template<typename T = Asset>
class WeakAssetHandle;

/**
* \brief Create an assethandle whose lifetime is not managed by the asset manager.
*
* The asset is destroyed when there are no AssetHandles or WeakAssetHandles that
* reference the asset.
*
* \param args The arguments used to construct an instance of the asset
*/
template<typename T, typename... Args>
AssetHandle<T> MakeAssetHandle(Args&&... args);

template<typename T, typename O, std::enable_if_t<std::is_convertible_v<T*, O*>, bool> = true>
AssetHandle<T> StaticAssetHandleCast(const AssetHandle<O>& other);

Expand Down Expand Up @@ -182,35 +195,43 @@ namespace CE
{}

template <Internal::AssetInternal::RefCountType IndexOfCounter>
AssetHandleRefCounter<IndexOfCounter>::AssetHandleRefCounter(Internal::AssetInternal* assetInternal)
AssetHandleRefCounter<IndexOfCounter>::AssetHandleRefCounter(std::shared_ptr<Internal::AssetInternal> assetInternal)
{
mAssetInternal = assetInternal;
mAssetInternal = std::move(assetInternal);
IncreaseRef();
}

template <Internal::AssetInternal::RefCountType IndexOfCounter>
AssetHandleRefCounter<IndexOfCounter>::AssetHandleRefCounter(AssetHandleRefCounter&& other) noexcept :
AssetHandleBase(std::move(other))
{}
{
}

template <Internal::AssetInternal::RefCountType IndexOfCounter>
AssetHandleRefCounter<IndexOfCounter>::AssetHandleRefCounter(const AssetHandleRefCounter& other)
{
DecreaseRef(); // Our ref is overwritten
mAssetInternal = other.mAssetInternal;
IncreaseRef();
IncreaseRef(); // We made a copy, so increment
}

template <Internal::AssetInternal::RefCountType IndexOfCounter>
AssetHandleRefCounter<IndexOfCounter>& AssetHandleRefCounter<IndexOfCounter>::operator=(
AssetHandleRefCounter&& other) noexcept
AssetHandleRefCounter<IndexOfCounter>& AssetHandleRefCounter<IndexOfCounter>::operator=(AssetHandleRefCounter&& other) noexcept
{
DecreaseRef();
return static_cast<AssetHandleRefCounter&>(AssetHandleBase::operator=(std::move(other)));
if (&other == this)
{
return *this;
}

DecreaseRef(); // Our ref is overwritten
mAssetInternal = std::move(other.mAssetInternal);
// Ref count is not incremented; we stole it from other

return *this;
}

template <Internal::AssetInternal::RefCountType IndexOfCounter>
AssetHandleRefCounter<IndexOfCounter>& AssetHandleRefCounter<IndexOfCounter>::operator=(
const AssetHandleRefCounter& other)
AssetHandleRefCounter<IndexOfCounter>& AssetHandleRefCounter<IndexOfCounter>::operator=(const AssetHandleRefCounter& other)
{
DecreaseRef();
mAssetInternal = other.mAssetInternal;
Expand Down Expand Up @@ -249,6 +270,14 @@ namespace CE
}
}

template <typename T, typename ... Args>
AssetHandle<T> MakeAssetHandle(Args&&... args)
{
std::unique_ptr<Asset, InPlaceDeleter<Asset, true>> asset = MakeUniqueInPlace<T, Asset>(std::forward<Args>(args)...);
std::shared_ptr<Internal::AssetInternal> assetInternal = std::make_shared<Internal::AssetInternal>(std::move(asset));
return { std::move(assetInternal) };
}

template <typename T>
template <typename O, std::enable_if_t<std::is_convertible_v<O*, T*>, bool>>
AssetHandle<T>::AssetHandle(AssetHandle<O>&& other) noexcept :
Expand Down
13 changes: 10 additions & 3 deletions Include/Assets/Core/AssetInternal.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <atomic>

#include "AssetFileMetaData.h"
#include "AssetMetaData.h"
#include "Utilities/MemFunctions.h"

namespace CE
Expand All @@ -14,19 +14,26 @@ namespace CE::Internal
// A potentially unloaded asset.
struct AssetInternal
{
AssetInternal(AssetFileMetaData&& metaData, const std::optional<std::filesystem::path>& path);
AssetInternal(AssetMetaData metaData, std::optional<std::filesystem::path> path);
AssetInternal(std::unique_ptr<Asset, InPlaceDeleter<Asset, true>> asset);

Asset* TryGetLoadedAsset();

enum class RefCountType : bool { Strong, Weak };

std::unique_ptr<Asset, InPlaceDeleter<Asset, true>> mAsset{};

// Data that is used to hint to the asset manager
// whether the asset is in use or if it can be offloaded.
std::array<std::atomic<uint32>, 2> mRefCounters{};
std::atomic<bool> mHasBeenDereferencedSinceGarbageCollect{};

// Used for reading/writing to mAsset
std::mutex mAccessMutex{};

AssetFileMetaData mMetaData;
// Certain data can be accessed without having
// to load the entire asset in
AssetMetaData mMetaData;

// The .asset file. Is only nullopt if this
// asset was generated at runtime, and no path
Expand Down
6 changes: 3 additions & 3 deletions Include/Assets/Core/AssetLoadInfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "AssetFileMetaData.h"
#include "AssetMetaData.h"

namespace CE
{
Expand All @@ -24,7 +24,7 @@ namespace CE

std::istream& GetStream() { return *mStream; }

const AssetFileMetaData& GetMetaData() const { return *mMetaData; }
const AssetMetaData& GetMetaData() const { return *mMetaData; }

private:
// Returns true on succes
Expand All @@ -33,6 +33,6 @@ namespace CE
std::unique_ptr<std::istream> mStream;

friend class AssetManager;
std::unique_ptr<AssetFileMetaData> mMetaData;
std::unique_ptr<AssetMetaData> mMetaData;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CE
{
class MetaType;

class AssetFileMetaData
class AssetMetaData
{
public:
// For assets that were imported from somewhere, useful for when we are reimporting
Expand All @@ -21,15 +21,18 @@ namespace CE
};

// If no version is provided, the current version is used.
AssetFileMetaData(std::string_view name, const MetaType& assetClass, uint32 assetVersion = std::numeric_limits<uint32>::max(), const std::optional<ImporterInfo>& importerInfo = std::nullopt);
AssetMetaData(std::string_view name,
const MetaType& assetClass,
uint32 assetVersion = std::numeric_limits<uint32>::max(),
const std::optional<ImporterInfo>& importerInfo = std::nullopt);

AssetFileMetaData(const AssetFileMetaData&) = default;
AssetFileMetaData(AssetFileMetaData&&) noexcept = default;
AssetMetaData(const AssetMetaData&) = default;
AssetMetaData(AssetMetaData&&) noexcept = default;

~AssetFileMetaData() = default;
~AssetMetaData() = default;

AssetFileMetaData& operator=(const AssetFileMetaData&) = default;
AssetFileMetaData& operator=(AssetFileMetaData&&) noexcept = default;
AssetMetaData& operator=(const AssetMetaData&) = default;
AssetMetaData& operator=(AssetMetaData&&) noexcept = default;

const std::string& GetName() const { return mAssetName; }
const MetaType& GetClass() const { return mClass; }
Expand All @@ -40,7 +43,7 @@ namespace CE

const std::optional<ImporterInfo>& GetImporterInfo() const { return mImporterInfo; }

static std::optional<AssetFileMetaData> ReadMetaData(std::istream& fromStream);
static std::optional<AssetMetaData> ReadMetaData(std::istream& fromStream);

void WriteMetaData(std::ostream& toStream) const;

Expand All @@ -50,8 +53,8 @@ namespace CE
friend class AssetSaveInfo;

// Backwards compatibility
static std::optional<AssetFileMetaData> ReadMetaDataV1V2V3(std::istream& fromStream, uint32 version);
static std::optional<AssetFileMetaData> ReadMetaDataV4(cereal::BinaryInputArchive& fromArchive);
static std::optional<AssetMetaData> ReadMetaDataV1V2V3(std::istream& fromStream, uint32 version);
static std::optional<AssetMetaData> ReadMetaDataV4(cereal::BinaryInputArchive& fromArchive);

static constexpr uint32 sMetaDataVersion = 4;
uint32 mMetaDataVersion{};
Expand Down
8 changes: 4 additions & 4 deletions Include/Assets/Core/AssetSaveInfo.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <sstream>

#include "AssetFileMetaData.h"
#include "AssetMetaData.h"

namespace CE
{
Expand All @@ -10,7 +10,7 @@ namespace CE
class AssetSaveInfo
{
public:
AssetSaveInfo(const std::string& name, const MetaType& assetClass, const std::optional<AssetFileMetaData::ImporterInfo>& importerInfo = std::nullopt);
AssetSaveInfo(const std::string& name, const MetaType& assetClass, const std::optional<AssetMetaData::ImporterInfo>& importerInfo = std::nullopt);

AssetSaveInfo(AssetSaveInfo&& other) noexcept = default;
AssetSaveInfo(const AssetSaveInfo&) = delete;
Expand All @@ -28,12 +28,12 @@ namespace CE

std::string ToString() const;

const AssetFileMetaData& GetMetaData() const { return mMetaData; }
const AssetMetaData& GetMetaData() const { return mMetaData; }

private:
// First save to a string stream; if anything goes wrong, our original file is left unmodified
std::ostringstream mStream{};

AssetFileMetaData mMetaData;
AssetMetaData mMetaData;
};
}
2 changes: 1 addition & 1 deletion Include/Assets/Core/ImportedAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CE
{
public:
ImportedAsset(const std::string& name, const MetaType& assetClass, const std::filesystem::path& importedFromFile, uint32 importerVersion) :
AssetSaveInfo(name, assetClass, AssetFileMetaData::ImporterInfo{ importedFromFile, importerVersion })
AssetSaveInfo(name, assetClass, AssetMetaData::ImporterInfo{ importedFromFile, importerVersion })
{
}
};
Expand Down
Loading

0 comments on commit d17a6ba

Please sign in to comment.