Compare commits

...

10 Commits

Author SHA1 Message Date
Thraix 9a3b3aa13c Rework tracing system and enum creators
- Rework tracing system, now using "{}" for formatting instead of the
  standard %s,%d,%f formatting that C++ uses.
- Rework enums creators to not take in namespace
- Fix single use CommandBuffers sometimes failing due to indexing them
  with the current frame in flight
- Add DropEvent to Window
2026-05-03 12:40:47 +02:00
Thraix 9d5a5314a7 Rework ECS framework
- Add queue systems when adding/removing components and systems
- Add GlobalData to ECSManager, used to store data that is not specific
  to entities
- Add View class, used to for loop all entities that contains the given
  Components
- Rework how signaling works
2026-02-07 18:25:13 +01:00
Thraix e5866b2dcb Format all files based on clang-format file 2026-01-28 23:04:31 +01:00
Thraix 6c1c558998 Add EcsManager::Find() 2025-08-24 22:05:09 +02:00
Thraix 71e02f3015 Fix multiple creation of runtime framebuffers
- Framebuffer names are now generated based on Uuids, as such we can
  create multiple runtime Framebuffers
- Framebuffers now uses ints instead of uint32_t, to avoid issues where
  we do "-framebuffer.GetWidth()", which will return an odd uint32_t
  value
- Add some additional AssetRef constructors
2025-08-19 20:14:04 +02:00
Thraix 3c4365083f Add GetWidth and GetHeight to Texture2D 2025-08-13 21:25:56 +02:00
Thraix 717f452908 Set all descriptor sets when calling Pipeline::SetDescriptorSets
- When calling Pipeline::SetDescriptorSets, it now sets all
  descriptor sets for all frames that can be in flight. Meaning
  SetDescriptorSets doesn't need to be called each render pass.
2025-08-12 22:18:23 +02:00
Thraix aca495960f Fix varius minor issues
- Asset will now generate a uuid if it is not inside the meta file
- Device cleanup/idling improved
- FileSystem now prints which file failed to open
- Renderer header file now has the same parameter names as the source file
2025-08-10 20:18:41 +02:00
Tim Håkansson 4d2dfce31c Add Linux support
- Add linux build system using MakeGen
- Fix a swapchain validation error, likelydue to my linux system using a
  different vulkan version
- Make DescriptorPool take in amount of descriptors it needs, instead of
  allocating a mass amount for every pool, causing loads of RAM/VRAM usage
2025-08-09 21:42:15 +02:00
Thraix ecc11f07db Improve vulkan resource freeing
and some minor improvements to the Renderer, now taking in an
AssetRef<Pipeline> as parameter.
2024-10-11 20:06:22 +02:00
156 changed files with 3505 additions and 1491 deletions
+334
View File
@@ -0,0 +1,334 @@
---
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionDeclarations: false
AlignFunctionPointers: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionDeclarations: false
AlignFunctionPointers: false
PadOperators: false
AlignConsecutiveDeclarations:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionDeclarations: true
AlignFunctionPointers: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionDeclarations: false
AlignFunctionPointers: false
PadOperators: false
AlignConsecutiveShortCaseStatements:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCaseArrows: false
AlignCaseColons: false
AlignConsecutiveTableGenBreakingDAGArgColons:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionDeclarations: false
AlignFunctionPointers: false
PadOperators: false
AlignConsecutiveTableGenCondOperatorColons:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionDeclarations: false
AlignFunctionPointers: false
PadOperators: false
AlignConsecutiveTableGenDefinitionColons:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionDeclarations: false
AlignFunctionPointers: false
PadOperators: false
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments:
Kind: Always
OverEmptyLines: 0
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowBreakBeforeNoexceptSpecifier: Never
AllowShortBlocksOnASingleLine: Never
AllowShortCaseExpressionOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false
AllowShortCompoundRequirementOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AllowShortNamespacesOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AttributeMacros:
- __capability
- absl_nonnull
- absl_nullable
- absl_nullability_unknown
BinPackArguments: false
BinPackLongBracedList: true
BinPackParameters: false
BitFieldColonSpacing: Both
BracedInitializerIndentWidth: -1
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterExternBlock: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAdjacentStringLiterals: true
BreakAfterAttributes: Leave
BreakAfterJavaFieldAnnotations: false
BreakAfterReturnType: None
BreakArrays: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Allman
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTemplateCloser: false
BreakBeforeTernaryOperators: true
BreakBinaryOperations: Never
BreakConstructorInitializers: BeforeColon
BreakFunctionDefinitionParameters: false
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
BreakTemplateDeclarations: Yes
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
EnumTrailingComma: Leave
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 3
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: true
IndentExportBlock: true
IndentExternBlock: AfterExternBlock
IndentGotoLabels: true
IndentPPDirectives: None
IndentRequiresClause: true
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertBraces: false
InsertNewlineAtEOF: false
InsertTrailingCommas: None
IntegerLiteralSeparator:
Binary: 0
BinaryMinDigits: 0
Decimal: 0
DecimalMinDigits: 0
Hex: 0
HexMinDigits: 0
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLines:
AtEndOfFile: false
AtStartOfBlock: false
AtStartOfFile: true
KeepFormFeed: false
LambdaBodyIndentation: Signature
LineEnding: DeriveLF
MacroBlockBegin: ''
MacroBlockEnd: ''
MainIncludeChar: Quote
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
OneLineFormatOffRegex: ''
PackConstructorInitializers: Never
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakBeforeMemberAccess: 150
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakScopeResolution: 500
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
PPIndentWidth: -1
QualifierAlignment: Leave
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
CanonicalDelimiter: ''
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: pb
BasedOnStyle: google
ReferenceAlignment: Pointer
ReflowComments: Always
RemoveBracesLLVM: false
RemoveEmptyLinesInUnwrappedLines: false
RemoveParentheses: Leave
RemoveSemicolon: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 1
SkipMacroDefinitionBody: false
SortIncludes:
Enabled: true
IgnoreCase: false
SortJavaStaticImport: Before
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterOperatorKeyword: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeJsonColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterNot: false
AfterOverloadedOperator: false
AfterPlacementOperator: true
AfterRequiresInClause: false
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Never
SpacesInContainerLiterals: true
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParens: Never
SpacesInParensOptions:
ExceptDoubleParentheses: false
InCStyleCasts: false
InConditionalStatements: false
InEmptyParentheses: false
Other: false
SpacesInSquareBrackets: false
Standard: Auto
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TableGenBreakInsideDAGArg: DontBreak
TabWidth: 2
UseTab: Never
VerilogBreakBetweenInstancePorts: true
WhitespaceSensitiveMacros:
- BOOST_PP_STRINGIZE
- CF_SWIFT_NAME
- NS_SWIFT_NAME
- PP_STRINGIZE
- STRINGIZE
WrapNamespaceBodyWithEmptyLines: Leave
...
+2
View File
@@ -9,3 +9,5 @@ bin/
.cache
libs
imgui.ini
Makefile
compile_flags.txt
+1 -1
View File
@@ -202,7 +202,6 @@
<ClCompile Include="src\copium\event\WindowResizeEvent.cpp" />
<ClCompile Include="src\copium\mesh\Mesh.cpp" />
<ClCompile Include="src\copium\renderer\LineVertex.cpp" />
<ClCompile Include="src\copium\renderer\LineVertex.h" />
<ClCompile Include="src\copium\pipeline\ShaderBinding.cpp" />
<ClCompile Include="src\copium\renderer\Batch.cpp" />
<ClCompile Include="src\copium\renderer\LineRenderer.cpp" />
@@ -287,6 +286,7 @@
<ClInclude Include="src\copium\pipeline\ShaderBinding.h" />
<ClInclude Include="src\copium\renderer\Batch.h" />
<ClInclude Include="src\copium\renderer\LineRenderer.h" />
<ClInclude Include="src\copium\renderer\LineVertex.h" />
<ClInclude Include="src\copium\renderer\Renderer.h" />
<ClInclude Include="src\copium\renderer\RendererVertex.h" />
<ClInclude Include="src\copium\sampler\DepthAttachment.h" />
+3 -3
View File
@@ -216,9 +216,6 @@
<ClCompile Include="src\copium\event\ViewportResize.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\renderer\LineVertex.h">
<Filter>Header Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\renderer\LineRenderer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -479,5 +476,8 @@
<ClInclude Include="src\copium\renderer\LineRenderer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\renderer\LineVertex.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
+53
View File
@@ -0,0 +1,53 @@
<makegen>
<configuration name="Release">
<define>MSDF_ATLAS_PUBLIC=</define>
<define>GLM_FORCE_LEFT_HANDED</define>
<define>GLM_FORCE_RADIANS</define>
<define>GLM_FORCE_DEPTH_ZERO_TO_ONE</define>
<dependency>../ext/projects/imgui/</dependency>
<dependency>../ext/projects/msdf-atlas-gen/</dependency>
<dependency>../ext/projects/msdfgen-core/</dependency>
<dependency>../ext/projects/msdfgen-ext/</dependency>
<generatehfile>false</generatehfile>
<outputdir>bin/Release/</outputdir>
<outputname>libcopium.so</outputname>
<outputtype>sharedlibrary</outputtype>
<projectname>Copium Engine</projectname>
<srcdir>src/</srcdir>
<includedir>src/</includedir>
<includedir>../ext/repos/imgui/</includedir>
<includedir>../ext/repos/imgui/backends/</includedir>
<includedir>../ext/repos/freetype/</includedir>
<includedir>../ext/repos/msdf-atlas-gen/msdfgen/</includedir>
<includedir>../ext/repos/msdf-atlas-gen/</includedir>
<includedir>../ext/repos/stb/</includedir>
</configuration>
<configuration name="Debug">
<cflag>-g3</cflag>
<cflag>-w</cflag>
<define>_DEBUG</define>
<define>MSDF_ATLAS_PUBLIC=</define>
<define>GLM_FORCE_LEFT_HANDED</define>
<define>GLM_FORCE_RADIANS</define>
<define>GLM_FORCE_DEPTH_ZERO_TO_ONE</define>
<dependency>../ext/projects/imgui/</dependency>
<dependency>../ext/projects/msdf-atlas-gen/</dependency>
<dependency>../ext/projects/msdfgen-core/</dependency>
<dependency>../ext/projects/msdfgen-ext/</dependency>
<generatehfile>false</generatehfile>
<outputdir>bin/Debug/</outputdir>
<outputname>libcopium.so</outputname>
<outputtype>sharedlibrary</outputtype>
<projectname>Copium Engine</projectname>
<srcdir>src/</srcdir>
<includedir>src/</includedir>
<includedir>../ext/repos/imgui/</includedir>
<includedir>../ext/repos/imgui/backends/</includedir>
<includedir>../ext/repos/freetype/</includedir>
<includedir>../ext/repos/msdf-atlas-gen/msdfgen/</includedir>
<includedir>../ext/repos/msdf-atlas-gen/</includedir>
<includedir>../ext/repos/stb/</includedir>
</configuration>
<target>Debug</target>
<version>v1.3.4</version>
</makegen>
+4 -4
View File
@@ -1,17 +1,17 @@
#pragma once
#include "copium/asset/AssetMeta.h"
#include "copium/util/MetaFile.h"
#include "copium/util/Uuid.h"
#include <stdint.h>
#include "copium/asset/AssetMeta.h"
#include "copium/util/Uuid.h"
namespace Copium
{
class Asset
{
friend class AssetManager;
public:
Asset();
virtual ~Asset();
+15 -5
View File
@@ -1,6 +1,9 @@
#include "copium/asset/AssetFile.h"
#include <fstream>
#include "copium/util/FileSystem.h"
#include "copium/util/Trace.h"
namespace Copium
{
@@ -17,7 +20,7 @@ namespace Copium
return dateModified < FileSystem::DateModified(path);
}
void AssetFile::Load()
void AssetFile::Load()
{
MetaFile metaFile{path};
for (auto&& assetType : assetTypes)
@@ -28,7 +31,7 @@ namespace Copium
Load(metaFile, assetType);
return;
}
CP_WARN("Unknown Asset type in file: %s", metaFile.GetFilePath().c_str());
CP_WARN("Unknown Asset type in file: {}", metaFile.GetFilePath());
}
const std::string& AssetFile::GetPath() const
@@ -41,9 +44,16 @@ namespace Copium
return uuid;
}
void AssetFile::Load(const MetaFile& metaFile, const std::string& className)
void AssetFile::Load(MetaFile& metaFile, const std::string& className)
{
const MetaFileClass& metaClass = metaFile.GetMetaClass(className);
MetaFileClass& metaClass = metaFile.GetMetaClass(className);
if (!metaClass.HasValue("uuid"))
{
CP_WARN("Asset ({}) has no UUID assigned, generating new one", path);
metaClass.AddValue("uuid", Uuid{}.ToString());
std::fstream file{path};
file << metaFile;
}
uuid = Uuid{metaClass.GetValue("uuid")};
dateModified = FileSystem::DateModified(path);
}
@@ -52,4 +62,4 @@ namespace Copium
{
assetTypes.emplace_back(assetType);
}
}
}
+4 -3
View File
@@ -1,6 +1,5 @@
#pragma once
#include "copium/asset/AssetMeta.h"
#include "copium/util/MetaFile.h"
#include "copium/util/Uuid.h"
@@ -9,6 +8,7 @@ namespace Copium
class AssetFile
{
friend class AssetManager;
private:
static std::vector<std::string> assetTypes;
std::string path;
@@ -23,8 +23,9 @@ namespace Copium
const std::string& GetPath() const;
Uuid GetUuid() const;
private:
void Load(const MetaFile& metaFile, const std::string& className);
void Load(MetaFile& metaFile, const std::string& className);
static void RegisterAssetType(const std::string& assetType);
};
}
}
+14 -7
View File
@@ -1,7 +1,7 @@
#pragma once
#include "copium/asset/AssetMeta.h"
#include "copium/asset/AssetManager.h"
#include "copium/asset/AssetMeta.h"
namespace Copium
{
@@ -11,27 +11,33 @@ namespace Copium
public:
AssetHandle()
: id{NULL_ASSET_ID}
{}
{
}
AssetHandle(const std::string& assetName)
: id{AssetManager::LoadAsset(assetName).GetId()}
{}
{
}
AssetHandle(const Uuid& uuid)
: id{AssetManager::LoadAsset(uuid).GetId()}
{}
{
}
AssetHandle(AssetType& asset)
: id{AssetManager::DuplicateAsset(asset.GetId())}
{}
{
}
AssetHandle(const std::string& name, std::unique_ptr<AssetType>&& runtimeAsset)
: id{AssetManager::RegisterRuntimeAsset(name, std::move(runtimeAsset)).GetId()}
{}
{
}
AssetHandle(AssetId id)
: id{AssetManager::DuplicateAsset(id)}
{}
{
}
AssetId GetId() const
{
@@ -53,6 +59,7 @@ namespace Copium
{
return id != NULL_ASSET_ID;
}
private:
AssetId id;
};
+30 -30
View File
@@ -1,13 +1,10 @@
#include "copium/asset/AssetManager.h"
#include "copium/buffer/Framebuffer.h"
#include "copium/sampler/ColorAttachment.h"
#include "copium/sampler/Texture2D.h"
#include "copium/util/Common.h"
#include "copium/util/MetaFile.h"
#include <fstream>
#include <filesystem>
#include <fstream>
#include "copium/util/MetaFile.h"
#include "copium/util/Trace.h"
namespace Copium
{
@@ -30,15 +27,19 @@ namespace Copium
if (std::filesystem::is_directory(it->path()))
continue;
std::filesystem::path assetDirPath{assetDir};
std::string assetPath = assetDir + "/" + std::filesystem::absolute(it->path()).string().substr(std::filesystem::absolute(assetDirPath).string().size() + 1).c_str();
std::string assetPath = assetDir + "/" +
std::filesystem::absolute(it->path())
.string()
.substr(std::filesystem::absolute(assetDirPath).string().size() + 1)
.c_str();
try
{
CP_DEBUG("Registering Asset: %s", assetPath.c_str());
CP_DEBUG("Registering Asset: {}", assetPath);
cachedAssetFiles.emplace_back(assetPath);
}
catch (RuntimeException& exception)
{
CP_ERR("Failed to register Asset: %s", assetPath.c_str());
CP_ERR("Failed to register Asset: {}", assetPath);
}
}
}
@@ -48,7 +49,7 @@ namespace Copium
if (assetDir.back() == '/')
assetDir.pop_back();
for (auto it = assetDirs.begin(); it != assetDirs.end(); ++it)
for (auto it = assetDirs.begin(); it != assetDirs.end(); ++it)
{
if (*it == assetDir)
{
@@ -67,7 +68,7 @@ namespace Copium
Asset& AssetManager::LoadAsset(const std::string& assetPath)
{
CP_DEBUG("Loading Asset: %s", assetPath.c_str());
CP_DEBUG("Loading Asset: {}", assetPath);
for (auto& dir : assetDirs)
{
@@ -79,13 +80,12 @@ namespace Copium
return LoadAssetFromPath(path);
}
CP_ABORT("Unknown Asset: %s", assetPath.c_str());
CP_ABORT("Unknown Asset: {}", assetPath);
}
Asset& AssetManager::LoadAsset(const Uuid& uuid)
Asset& AssetManager::LoadAsset(const Uuid& uuid)
{
CP_DEBUG("Loading uuid Asset: %s", uuid.ToString().c_str());
CP_DEBUG("Loading uuid Asset: {}", uuid);
for (auto&& assetFile : cachedAssetFiles)
{
if (assetFile.NeedReload())
@@ -96,16 +96,16 @@ namespace Copium
return LoadAssetFromPath(assetFile.GetPath());
}
CP_ABORT("Asset not found with uuid=%s", uuid.ToString().c_str());
CP_ABORT("Asset not found with uuid={}", uuid);
// TODO: Reload the assetCache to see if a new file has appeared with that uuid
}
AssetId AssetManager::DuplicateAsset(AssetId id)
{
auto it = assets.find(id);
CP_ASSERT(it != assets.end(), "Failed to find asset with id=%d", id);
CP_ASSERT(it != assets.end(), "Failed to find asset with id={}", id);
CP_DEBUG("Duplicating asset: %s", it->second->GetName().c_str());
CP_DEBUG("Duplicating asset: {}", it->second->GetName());
it->second->metaData.loadCount++;
return id;
}
@@ -118,7 +118,7 @@ namespace Copium
CP_WARN("Asset not loaded");
return;
}
CP_DEBUG("Unloading Asset: %s (%d instances left)", it->second->GetName().c_str(), it->second->metaData.loadCount - 1);
CP_DEBUG("Unloading Asset: {} ({} instances left)", it->second->GetName(), it->second->metaData.loadCount - 1);
it->second->metaData.loadCount--;
if (it->second->metaData.loadCount > 0)
@@ -136,11 +136,11 @@ namespace Copium
return cachedAssetFiles;
}
void AssetManager::Cleanup()
void AssetManager::Cleanup()
{
if (assets.empty())
return;
CP_WARN("Performing auto clean up of %d non unloaded assets", assets.size());
CP_WARN("Performing auto clean up of {} non unloaded assets", assets.size());
while (!assets.empty())
{
UnloadAsset(assets.begin()->second->GetId());
@@ -149,18 +149,18 @@ namespace Copium
CP_ASSERT(pathToAssetCache.empty(), "Path To Asset Cache not empty after full unload");
}
Asset& AssetManager::RegisterRuntimeAsset(const std::string& name, std::unique_ptr<Asset>&& asset)
Asset& AssetManager::RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr<Asset>&& asset)
{
CP_DEBUG("Registering Runtime Asset: %s", name.c_str());
CP_DEBUG("Registering Runtime Asset: {}", name);
auto it = nameToAssetCache.find(name);
CP_ASSERT(it == nameToAssetCache.end(), "Asset already exists: %s", name);
CP_ASSERT(it == nameToAssetCache.end(), "Asset already exists: {}", name);
AssetId id = runtimeAssetId++;
Asset* asset2 = assets.emplace(id, std::move(asset)).first->second.get();
asset2->metaData.id = id;
asset2->metaData.name = name;
asset2->metaData.uuid = Uuid();
asset2->metaData.uuid = uuid;
asset2->metaData.isRuntime = true;
nameToAssetCache.emplace(name, id);
return *asset2;
@@ -168,7 +168,7 @@ namespace Copium
Asset& AssetManager::LoadAssetFromPath(const std::string& filepath)
{
CP_DEBUG("Loading Asset: %s", filepath.c_str());
CP_DEBUG("Loading Asset: {}", filepath);
auto it = pathToAssetCache.find(filepath);
if (it != pathToAssetCache.end())
{
@@ -180,9 +180,9 @@ namespace Copium
MetaFile metaFile{filepath};
for (auto& assetType : assetTypes)
{
if(metaFile.HasMetaClass(assetType.first))
if (metaFile.HasMetaClass(assetType.first))
return assetType.second(metaFile, assetType.first);
}
CP_ABORT("Unknown Asset type: %s", filepath.c_str());
CP_ABORT("Unknown Asset type: {}", filepath);
}
}
}
+26 -10
View File
@@ -1,18 +1,20 @@
#pragma once
#include "copium/asset/Asset.h"
#include "copium/asset/AssetFile.h"
#include "copium/util/Common.h"
#include <functional>
#include <map>
#include <memory>
#include <vector>
#include "copium/asset/Asset.h"
#include "copium/asset/AssetFile.h"
#include "copium/util/Trace.h"
namespace Copium
{
class AssetManager
{
CP_STATIC_CLASS(AssetManager);
private:
using CreateAssetFunc = std::function<Asset&(const MetaFile& metaFile, const std::string& str)>;
static std::map<std::string, CreateAssetFunc> assetTypes;
@@ -24,7 +26,7 @@ namespace Copium
static AssetId assetId;
static AssetId runtimeAssetId;
static std::vector<AssetFile> cachedAssetFiles; // TODO: Make a set?
static std::vector<AssetFile> cachedAssetFiles; // TODO: Make a set?
public:
static void RegisterAssetDir(std::string assetDir);
static void UnregisterAssetDir(std::string assetDir);
@@ -33,14 +35,16 @@ namespace Copium
static Asset& LoadAsset(const Uuid& uuid);
static AssetId DuplicateAsset(AssetId id);
static void UnloadAsset(AssetId id);
static Asset& RegisterRuntimeAsset(const std::string& name, std::unique_ptr<Asset>&& asset);
static Asset& RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr<Asset>&& asset);
static const std::vector<AssetFile>& GetAssetFiles();
static void Cleanup();
template <typename AssetType>
static void RegisterAssetType(const std::string& assetType)
{
CP_ASSERT(assetTypes.emplace(assetType, &AssetManager::CreateAsset<AssetType>).second, "Asset type already exists: %s", assetType.c_str());
CP_ASSERT(assetTypes.emplace(assetType, &AssetManager::CreateAsset<AssetType>).second,
"Asset type already exists: {}",
assetType.c_str());
AssetFile::RegisterAssetType(assetType);
}
@@ -61,7 +65,7 @@ namespace Copium
}
template <typename AssetT>
static AssetT& GetAsset(AssetId id)
static AssetT& GetAsset(AssetId id)
{
Asset& asset = GetAsset(id);
AssetT* assetT = dynamic_cast<AssetT*>(&asset);
@@ -70,13 +74,25 @@ namespace Copium
}
template <typename AssetT>
static AssetT& RegisterRuntimeAsset(const std::string& name, std::unique_ptr<AssetT>&& assetT)
static AssetT& RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr<AssetT>&& assetT)
{
AssetT* ptr = assetT.release();
Asset& asset = RegisterRuntimeAsset(name, std::unique_ptr<Asset>((Asset*)ptr));
Asset& asset = RegisterRuntimeAsset(name, uuid, std::unique_ptr<Asset>((Asset*)ptr));
return *(AssetT*)&asset;
}
template <typename AssetT>
static AssetT& RegisterRuntimeAsset(const std::string& name, std::unique_ptr<AssetT>&& assetT)
{
return RegisterRuntimeAsset(name, Uuid{}, std::move(assetT));
}
template <typename AssetT>
static AssetT& RegisterRuntimeAsset(const Uuid& uuid, std::unique_ptr<AssetT>&& assetT)
{
return RegisterRuntimeAsset(uuid.ToString(), uuid, std::move(assetT));
}
private:
static Asset& LoadAssetFromPath(const std::string& filepath);
+3 -2
View File
@@ -1,10 +1,11 @@
#pragma once
#include "copium/util/Uuid.h"
#include <stdint.h>
#include <string>
#include "copium/util/Uuid.h"
namespace Copium
{
static constexpr int NULL_ASSET_ID = 0;
+39 -11
View File
@@ -10,27 +10,53 @@ namespace Copium
public:
AssetRef()
: id{std::shared_ptr<AssetId>(new AssetId(NULL_ASSET_ID), AssetIdUnloader{})}
{}
{
}
AssetRef(const std::string& assetName)
explicit AssetRef(const std::string& assetName)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::LoadAsset(assetName).GetId()), AssetIdUnloader{})}
{}
{
}
AssetRef(const Uuid& uuid)
explicit AssetRef(const Uuid& uuid)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::LoadAsset(uuid).GetId()), AssetIdUnloader{})}
{}
{
}
AssetRef(AssetType& asset)
explicit AssetRef(AssetType& asset)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::DuplicateAsset(asset.GetId())), AssetIdUnloader{})}
{}
{
}
explicit AssetRef(std::unique_ptr<AssetType>&& runtimeAsset)
: id{std::shared_ptr<AssetId>(
new AssetId(AssetManager::RegisterRuntimeAsset(Uuid{}, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})}
{
}
AssetRef(const Uuid& uuid, std::unique_ptr<AssetType>&& runtimeAsset)
: id{std::shared_ptr<AssetId>(
new AssetId(AssetManager::RegisterRuntimeAsset(uuid, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})}
{
}
AssetRef(const std::string& name, std::unique_ptr<AssetType>&& runtimeAsset)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::RegisterRuntimeAsset(name, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})}
{}
: id{std::shared_ptr<AssetId>(
new AssetId(AssetManager::RegisterRuntimeAsset(name, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})}
{
}
AssetRef(const std::string& name, const Uuid& uuid, std::unique_ptr<AssetType>&& runtimeAsset)
: id{std::shared_ptr<AssetId>(
new AssetId(AssetManager::RegisterRuntimeAsset(name, uuid, std::move(runtimeAsset)).GetId()),
AssetIdUnloader{})}
{
}
AssetRef(AssetId id)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::DuplicateAsset(id)), AssetIdUnloader{})}
{}
{
}
AssetId GetId() const
{
@@ -48,7 +74,8 @@ namespace Copium
}
private:
struct AssetIdUnloader {
struct AssetIdUnloader
{
void operator()(AssetId* id)
{
if (*id != NULL_ASSET_ID)
@@ -57,6 +84,7 @@ namespace Copium
}
}
};
private:
std::shared_ptr<AssetId> id;
};
+23 -8
View File
@@ -5,7 +5,8 @@
namespace Copium
{
Buffer::Buffer(VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count)
: size{size}, count{count}
: size{size},
count{count}
{
VkBufferCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
@@ -23,15 +24,22 @@ namespace Copium
allocateInfo.allocationSize = memoryRequirements.size;
allocateInfo.memoryTypeIndex = Vulkan::GetDevice().FindMemoryType(memoryRequirements.memoryTypeBits, properties);
CP_VK_ASSERT(vkAllocateMemory(Vulkan::GetDevice(), &allocateInfo, nullptr, &memory), "Failed to allocate buffer memory");
CP_VK_ASSERT(vkAllocateMemory(Vulkan::GetDevice(), &allocateInfo, nullptr, &memory),
"Failed to allocate buffer memory");
vkBindBufferMemory(Vulkan::GetDevice(), handle, memory, 0);
}
Buffer::~Buffer()
{
vkFreeMemory(Vulkan::GetDevice(), memory, nullptr);
vkDestroyBuffer(Vulkan::GetDevice(), handle, nullptr);
VkDeviceMemory memoryCpy = memory;
VkBuffer handleCpy = handle;
Vulkan::GetDevice().QueueIdleCommand(
[memoryCpy, handleCpy]()
{
vkFreeMemory(Vulkan::GetDevice(), memoryCpy, nullptr);
vkDestroyBuffer(Vulkan::GetDevice(), handleCpy, nullptr);
});
}
void Buffer::Update(void* indexData, int index)
@@ -54,7 +62,10 @@ namespace Copium
void Buffer::UpdateStaging(void* data)
{
VkDeviceSize bufferSize = size * count;
Buffer stagingBuffer{VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, 1};
Buffer stagingBuffer{VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
bufferSize,
1};
stagingBuffer.Update(data, 0);
@@ -63,7 +74,10 @@ namespace Copium
void Buffer::UpdateStaging(void* data, VkDeviceSize offset, VkDeviceSize size)
{
Buffer stagingBuffer{VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, 1};
Buffer stagingBuffer{VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
size,
1};
stagingBuffer.Update(data, 0);
@@ -85,7 +99,7 @@ namespace Copium
mappedData = nullptr;
}
Buffer::operator VkBuffer() const
Buffer::operator VkBuffer() const
{
return handle;
}
@@ -111,7 +125,8 @@ namespace Copium
allocateInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer;
CP_VK_ASSERT(vkAllocateCommandBuffers(Vulkan::GetDevice(), &allocateInfo, &commandBuffer), "Failed to initialize command buffer");
CP_VK_ASSERT(vkAllocateCommandBuffers(Vulkan::GetDevice(), &allocateInfo, &commandBuffer),
"Failed to initialize command buffer");
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+3 -2
View File
@@ -1,14 +1,15 @@
#pragma once
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
#include "copium/util/Common.h"
namespace Copium
{
class Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(Buffer);
protected:
VkDeviceMemory memory;
VkBuffer handle;
@@ -1,6 +1,7 @@
#include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/util/Trace.h"
namespace Copium
{
@@ -9,14 +10,14 @@ namespace Copium
{
switch (type)
{
case CommandBufferType::SingleUse:
commandBuffers.resize(1);
break;
case CommandBufferType::Dynamic:
commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
break;
default:
CP_ABORT("Unreachable switch case: %s", ToString(type).c_str());
case CommandBufferType::SingleUse:
commandBuffers.resize(1);
break;
case CommandBufferType::Dynamic:
commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
break;
default:
CP_ABORT("Unreachable switch case: {}", type);
}
VkCommandBufferAllocateInfo allocateInfo{};
@@ -24,12 +25,21 @@ namespace Copium
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandPool = Vulkan::GetDevice().GetCommandPool();
allocateInfo.commandBufferCount = commandBuffers.size();
CP_VK_ASSERT(vkAllocateCommandBuffers(Vulkan::GetDevice(), &allocateInfo, commandBuffers.data()), "Failed to allocate CommandBuffer");
CP_VK_ASSERT(vkAllocateCommandBuffers(Vulkan::GetDevice(), &allocateInfo, commandBuffers.data()),
"Failed to allocate CommandBuffer");
}
CommandBuffer::~CommandBuffer()
{
vkFreeCommandBuffers(Vulkan::GetDevice(), Vulkan::GetDevice().GetCommandPool(), commandBuffers.size(), commandBuffers.data());
std::vector<VkCommandBuffer> commandBuffersCpy = commandBuffers;
Vulkan::GetDevice().QueueIdleCommand(
[commandBuffersCpy]()
{
vkFreeCommandBuffers(Vulkan::GetDevice(),
Vulkan::GetDevice().GetCommandPool(),
commandBuffersCpy.size(),
commandBuffersCpy.data());
});
}
// TODO: Test as constexpr function to see if it avoids the switch case
@@ -40,32 +50,46 @@ namespace Copium
beginInfo.flags = 0;
beginInfo.pInheritanceInfo = nullptr;
int index = Vulkan::GetSwapChain().GetFlightIndex();
switch (type)
{
case CommandBufferType::SingleUse:
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
break;
case CommandBufferType::Dynamic:
break;
default:
CP_ABORT("Unreachable switch case: %s", ToString(type).c_str());
case CommandBufferType::SingleUse:
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
index = 0;
break;
case CommandBufferType::Dynamic:
break;
default:
CP_ABORT("Unreachable switch case: {}", type);
}
vkResetCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], 0);
CP_VK_ASSERT(vkBeginCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], &beginInfo), "Failed to begin command buffer");
vkResetCommandBuffer(commandBuffers[index], 0);
CP_VK_ASSERT(vkBeginCommandBuffer(commandBuffers[index], &beginInfo), "Failed to begin command buffer");
}
void CommandBuffer::End()
{
vkEndCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()]);
int index = Vulkan::GetSwapChain().GetFlightIndex();
if (type == CommandBufferType::SingleUse)
{
index = 0;
}
vkEndCommandBuffer(commandBuffers[index]);
}
void CommandBuffer::Submit()
{
int index = Vulkan::GetSwapChain().GetFlightIndex();
if (type == CommandBufferType::SingleUse)
{
index = 0;
}
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()];
submitInfo.pCommandBuffers = &commandBuffers[index];
vkQueueSubmit(Vulkan::GetDevice().GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
// TODO: if singleUse?
@@ -74,6 +98,11 @@ namespace Copium
CommandBuffer::operator VkCommandBuffer() const
{
return commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()];
int index = Vulkan::GetSwapChain().GetFlightIndex();
if (type == CommandBufferType::SingleUse)
{
index = 0;
}
return commandBuffers[index];
}
}
}
@@ -1,18 +1,18 @@
#pragma once
#include <vulkan/vulkan.hpp>
#include "copium/util/Common.h"
#include "copium/util/Enum.h"
#include <vulkan/vulkan.hpp>
#define CP_COMMAND_BUFFER_TYPE_ENUMS SingleUse, Dynamic
CP_ENUM_CREATOR(Copium, CommandBufferType, CP_COMMAND_BUFFER_TYPE_ENUMS);
namespace Copium
{
CP_ENUM_CREATOR(CommandBufferType, SingleUse, Dynamic);
class CommandBuffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer);
private:
std::vector<VkCommandBuffer> commandBuffers;
const CommandBufferType type;
@@ -8,6 +8,7 @@ namespace Copium
class CommandBufferScoped final : public CommandBuffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped);
public:
CommandBufferScoped();
+37 -19
View File
@@ -1,6 +1,5 @@
#include "copium/buffer/Framebuffer.h"
#include "copium/asset/AssetManager.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/sampler/Image.h"
@@ -12,17 +11,25 @@ namespace Copium
const MetaFileClass& metaClass = metaFile.GetMetaClass("Framebuffer");
colorAttachment = AssetRef<ColorAttachment>(Uuid{metaClass.GetValue("rendertexture-uuid")});
ColorAttachment& attachment = colorAttachment.GetAsset();
width = attachment.GetWidth();
height = attachment.GetHeight();
CP_ASSERT(width > 0, "Width of framebuffer is less than 1: {}", width);
CP_ASSERT(height > 0, "Height of framebuffer is less than 1: {}", height);
InitializeDepthBuffer();
InitializeRenderPass();
InitializeFramebuffers();
}
Framebuffer::Framebuffer(uint32_t width, uint32_t height)
: width{width}, height{height}
Framebuffer::Framebuffer(int width, int height, const SamplerCreator& samplerCreator)
: width{width},
height{height}
{
InitializeImage();
CP_ASSERT(width > 0, "Width of framebuffer is less than 1: {}", width);
CP_ASSERT(height > 0, "Height of framebuffer is less than 1: {}", height);
InitializeImage(samplerCreator);
InitializeDepthBuffer();
InitializeRenderPass();
InitializeFramebuffers();
@@ -30,14 +37,23 @@ namespace Copium
Framebuffer::~Framebuffer()
{
for (auto& framebuffer : framebuffers)
vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr);
std::vector<VkFramebuffer> framebuffersCpy = framebuffers;
VkRenderPass renderPassCpy = renderPass;
Vulkan::GetDevice().QueueIdleCommand(
[framebuffersCpy, renderPassCpy]()
{
for (auto& framebuffer : framebuffersCpy)
vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
vkDestroyRenderPass(Vulkan::GetDevice(), renderPassCpy, nullptr);
});
}
void Framebuffer::Resize(uint32_t width, uint32_t height)
void Framebuffer::Resize(int width, int height)
{
vkDeviceWaitIdle(Vulkan::GetDevice());
CP_ASSERT(width > 0, "Width of framebuffer is less than 1: {}", width);
CP_ASSERT(height > 0, "Height of framebuffer is less than 1: {}", height);
Vulkan::GetDevice().WaitIdle();
this->width = width;
this->height = height;
for (auto&& framebuffer : framebuffers)
@@ -57,9 +73,9 @@ namespace Copium
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.framebuffer = framebuffers[Vulkan::GetSwapChain().GetFlightIndex()];
;
renderPassBeginInfo.renderArea.offset = {0, 0};
renderPassBeginInfo.renderArea.extent = {width, height};
renderPassBeginInfo.renderArea.extent = {static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
renderPassBeginInfo.clearValueCount = clearValues.size();
renderPassBeginInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
@@ -74,7 +90,7 @@ namespace Copium
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
VkRect2D scissor{};
scissor.offset = {0, 0};
scissor.extent = {width, height};
scissor.extent = {static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
}
@@ -98,19 +114,19 @@ namespace Copium
return colorAttachment.GetAsset();
}
uint32_t Framebuffer::GetWidth() const
int Framebuffer::GetWidth() const
{
return width;
}
uint32_t Framebuffer::GetHeight() const
int Framebuffer::GetHeight() const
{
return height;
}
void Framebuffer::InitializeImage()
void Framebuffer::InitializeImage(const SamplerCreator& samplerCreator)
{
colorAttachment = AssetManager::RegisterRuntimeAsset("Framebuffer::ColorAttachment", std::make_unique<ColorAttachment>(width, height, SamplerCreator{}));
colorAttachment = AssetRef(std::make_unique<ColorAttachment>(width, height, samplerCreator));
}
void Framebuffer::InitializeDepthBuffer()
@@ -179,7 +195,8 @@ namespace Copium
renderPassCreateInfo.dependencyCount = dependencies.size();
renderPassCreateInfo.pDependencies = dependencies.data();
CP_VK_ASSERT(vkCreateRenderPass(Vulkan::GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "Failed to initialze render pass");
CP_VK_ASSERT(vkCreateRenderPass(Vulkan::GetDevice(), &renderPassCreateInfo, nullptr, &renderPass),
"Failed to initialze render pass");
}
void Framebuffer::InitializeFramebuffers()
@@ -199,7 +216,8 @@ namespace Copium
createInfo.height = height;
createInfo.layers = 1;
CP_VK_ASSERT(vkCreateFramebuffer(Vulkan::GetDevice(), &createInfo, nullptr, &framebuffers[i]), "Failed to initialize framebuffer");
CP_VK_ASSERT(vkCreateFramebuffer(Vulkan::GetDevice(), &createInfo, nullptr, &framebuffers[i]),
"Failed to initialize framebuffer");
}
}
}
}
+12 -11
View File
@@ -1,47 +1,48 @@
#pragma once
#include <vulkan/vulkan.hpp>
#include "copium/asset/Asset.h"
#include "copium/asset/AssetMeta.h"
#include "copium/asset/AssetRef.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/sampler/ColorAttachment.h"
#include "copium/sampler/DepthAttachment.h"
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
namespace Copium
{
class Framebuffer final : public Asset
{
CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer);
private:
AssetRef<ColorAttachment> colorAttachment;
std::unique_ptr<DepthAttachment> depthAttachment;
std::vector<VkFramebuffer> framebuffers;
VkRenderPass renderPass;
uint32_t width;
uint32_t height;
int width;
int height;
public:
Framebuffer(const MetaFile& metaFile);
Framebuffer(uint32_t width, uint32_t height);
Framebuffer(int width, int height, const SamplerCreator& samplerCreator);
~Framebuffer();
void Resize(uint32_t width, uint32_t height);
void Resize(int width, int height);
void Bind(const CommandBuffer& commandBuffer);
void Unbind(const CommandBuffer& commandBuffer);
VkRenderPass GetRenderPass() const;
VkFramebuffer GetFramebuffer() const;
const ColorAttachment& GetColorAttachment() const;
uint32_t GetWidth() const;
uint32_t GetHeight() const;
int GetWidth() const;
int GetHeight() const;
private:
void InitializeImage();
void InitializeImage(const SamplerCreator& samplerCreator);
void InitializeDepthBuffer();
void InitializeRenderPass();
void InitializeFramebuffers();
};
}
}
@@ -2,12 +2,18 @@
#include <vulkan/vulkan.hpp>
#include "copium/util/Trace.h"
namespace Copium
{
IndexBuffer::IndexBuffer(int indexCount)
: Buffer{VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexCount * sizeof(uint16_t), 1},
: Buffer{VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
indexCount * sizeof(uint16_t),
1},
indexCount{indexCount}
{}
{
}
void IndexBuffer::Bind(const CommandBuffer& commandBuffer)
{
@@ -24,4 +30,4 @@ namespace Copium
CP_ASSERT(indices >= 0 && indices <= indexCount, "amount of indices is out of range");
vkCmdDrawIndexed(commandBuffer, indices, 1, 0, 0, 0);
}
}
}
+3 -1
View File
@@ -9,8 +9,10 @@ namespace Copium
class IndexBuffer final : public Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(IndexBuffer);
private:
int indexCount;
public:
IndexBuffer(int indexCount);
@@ -18,4 +20,4 @@ namespace Copium
void Draw(const CommandBuffer& commandBuffer);
void Draw(const CommandBuffer& commandBuffer, int indices);
};
}
}
@@ -6,8 +6,12 @@ namespace Copium
{
RendererVertexBuffer::RendererVertexBuffer(const VertexDescriptor& descriptor, int vertexCount)
: Buffer{VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, descriptor.GetVertexSize() * vertexCount, SwapChain::MAX_FRAMES_IN_FLIGHT}
{}
: Buffer{VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
descriptor.GetVertexSize() * vertexCount,
SwapChain::MAX_FRAMES_IN_FLIGHT}
{
}
void RendererVertexBuffer::Bind(const CommandBuffer& commandBuffer)
{
@@ -9,6 +9,7 @@ namespace Copium
class RendererVertexBuffer : public Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(RendererVertexBuffer);
public:
RendererVertexBuffer(const VertexDescriptor& descriptor, int vertexCount);
@@ -5,7 +5,11 @@
namespace Copium
{
UniformBuffer::UniformBuffer(ShaderBinding binding)
: Buffer{VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, binding.GetUniformBufferSize(), SwapChain::MAX_FRAMES_IN_FLIGHT}, binding{binding}
: Buffer{VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
binding.GetUniformBufferSize(),
SwapChain::MAX_FRAMES_IN_FLIGHT},
binding{binding}
{
buffer.resize(Buffer::GetSize());
}
@@ -21,49 +25,53 @@ namespace Copium
void UniformBuffer::Set(const std::string& str, const glm::mat3& data)
{
CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat3, "Uniform type missmatch = %s", str.c_str());
CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat3, "Uniform type missmatch = {}", str);
uint32_t offset = binding.GetUniformOffset(str);
memcpy(buffer.data() + offset, &data, sizeof(glm::mat3));
// memcpy(buffer.data() + offset, &data[0], sizeof(glm::vec3));
// memcpy(buffer.data() + offset + 16, &data[1], sizeof(glm::vec3));
// memcpy(buffer.data() + offset + 32, &data[2], sizeof(glm::vec3));
glm::mat4x3 mat43{data};
memcpy(buffer.data() + offset, &mat43, sizeof(glm::mat4x3));
}
void UniformBuffer::Set(const std::string& str, const glm::mat4& data)
{
CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat4, "Uniform type missmatch = %s", str.c_str());
CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat4, "Uniform type missmatch = {}", str);
uint32_t offset = binding.GetUniformOffset(str);
memcpy(buffer.data() + offset, &data, sizeof(glm::mat4));
}
void UniformBuffer::Set(const std::string& str, const glm::vec2& data)
{
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec2, "Uniform type missmatch = %s", str.c_str());
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec2, "Uniform type missmatch = {}", str);
uint32_t offset = binding.GetUniformOffset(str);
memcpy(buffer.data() + offset, &data, sizeof(glm::vec2));
}
void UniformBuffer::Set(const std::string& str, const glm::vec3& data)
{
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec3, "Uniform type missmatch = %s", str.c_str());
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec3, "Uniform type missmatch = {}", str);
uint32_t offset = binding.GetUniformOffset(str);
memcpy(buffer.data() + offset, &data, sizeof(glm::vec3));
}
void UniformBuffer::Set(const std::string& str, const glm::vec4& data)
{
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec4, "Uniform type missmatch = %s", str.c_str());
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec4, "Uniform type missmatch = {}", str);
uint32_t offset = binding.GetUniformOffset(str);
memcpy(buffer.data() + offset, &data, sizeof(glm::vec4));
}
void UniformBuffer::Set(const std::string& str, float data)
{
CP_ASSERT(binding.GetUniformType(str) == UniformType::Float, "Uniform type missmatch = %s", str.c_str());
CP_ASSERT(binding.GetUniformType(str) == UniformType::Float, "Uniform type missmatch = {}", str);
uint32_t offset = binding.GetUniformOffset(str);
memcpy(buffer.data() + offset, &data, sizeof(float));
}
void UniformBuffer::Set(const std::string& str, int data)
{
CP_ASSERT(binding.GetUniformType(str) == UniformType::Int, "Uniform type missmatch = %s", str.c_str());
CP_ASSERT(binding.GetUniformType(str) == UniformType::Int, "Uniform type missmatch = {}", str);
uint32_t offset = binding.GetUniformOffset(str);
memcpy(buffer.data() + offset, &data, sizeof(int));
}
@@ -80,4 +88,4 @@ namespace Copium
Buffer::Update(buffer.data(), i);
}
}
}
}
@@ -1,17 +1,18 @@
#pragma once
#include <glm/glm.hpp>
#include <vulkan/vulkan.hpp>
#include "copium/buffer/Buffer.h"
#include "copium/pipeline/ShaderBinding.h"
#include "copium/util/Common.h"
#include <glm/glm.hpp>
#include <vulkan/vulkan.hpp>
namespace Copium
{
class UniformBuffer final : public Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer);
private:
ShaderBinding binding;
std::vector<uint8_t> buffer;
@@ -3,7 +3,10 @@
namespace Copium
{
VertexBuffer::VertexBuffer(const VertexDescriptor& descriptor, int vertexCount)
: Buffer{VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, descriptor.GetVertexSize() * vertexCount, 1}
: Buffer{VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
descriptor.GetVertexSize() * vertexCount,
1}
{
VkDeviceSize offset = 0;
for (auto&& binding : descriptor.GetBindings())
@@ -1,21 +1,23 @@
#pragma once
#include <vector>
#include <vulkan/vulkan.hpp>
#include "copium/buffer/Buffer.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/pipeline/VertexDescriptor.h"
#include "copium/util/Common.h"
#include <vector>
#include <vulkan/vulkan.hpp>
namespace Copium
{
class VertexBuffer final : public Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(VertexBuffer);
private:
std::vector<VkDeviceSize> bindingOffsets;
std::vector<VkDeviceSize> bindingSizes;
public:
VertexBuffer(const VertexDescriptor& descriptor, int vertexCount);
+25 -16
View File
@@ -1,6 +1,7 @@
#include "copium/core/DebugMessenger.h"
#include "copium/core/Instance.h"
#include "copium/util/Trace.h"
namespace Copium
{
@@ -18,7 +19,8 @@ namespace Copium
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
createInfo.pfnUserCallback = DebugCallback;
createInfo.pUserData = nullptr;
CP_VK_ASSERT(vkCreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger), "Failed to initialze debug messenger");
CP_VK_ASSERT(vkCreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger),
"Failed to initialze debug messenger");
}
DebugMessenger::~DebugMessenger()
@@ -38,29 +40,34 @@ namespace Copium
#else
DebugMessenger::DebugMessenger(Instance& instance)
: instance{instance}
{}
{
}
DebugMessenger::~DebugMessenger()
{}
{
}
void DebugMessenger::AddRequiredExtensions(std::vector<const char*>* extensions)
{}
{
}
void DebugMessenger::AddRequiredLayers(std::vector<const char*>* layers)
{}
{
}
#endif
VKAPI_ATTR VkBool32 VKAPI_CALL DebugMessenger::DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* pUserData)
VKAPI_ATTR VkBool32 VKAPI_CALL
DebugMessenger::DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* pUserData)
{
if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
{
if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
CP_ABORT("%s", pCallbackData->pMessage);
CP_ABORT("{}", pCallbackData->pMessage);
else
CP_WARN("%s", pCallbackData->pMessage);
CP_WARN("{}", pCallbackData->pMessage);
}
return VK_FALSE;
}
@@ -78,11 +85,13 @@ namespace Copium
void DebugMessenger::vkDestroyDebugUtilsMessengerEXT(VkInstance instance,
VkDebugUtilsMessengerEXT debugMessenger,
const VkAllocationCallbacks* pAllocator) {
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
if (func != nullptr) {
func(instance, debugMessenger, pAllocator);
const VkAllocationCallbacks* pAllocator)
{
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
if (func != nullptr)
{
func(instance, debugMessenger, pAllocator);
}
}
}
}
@@ -1,9 +1,9 @@
#pragma once
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
#include "copium/util/Common.h"
namespace Copium
{
class Instance;
@@ -11,10 +11,12 @@ namespace Copium
class DebugMessenger final
{
CP_DELETE_COPY_AND_MOVE_CTOR(DebugMessenger);
private:
Instance& instance;
VkDebugUtilsMessengerEXT debugMessenger;
public:
DebugMessenger(Instance& instance);
~DebugMessenger();
+37 -4
View File
@@ -1,5 +1,6 @@
#include "Device.h"
#include "copium/core/QueueFamilies.h"
#include "copium/core/Vulkan.h"
#include "copium/core/Window.h"
@@ -11,6 +12,7 @@ namespace Copium
InitializeLogicalDevice();
InitializeCommandPool();
}
Device::~Device()
{
vkDestroyCommandPool(device, commandPool, nullptr);
@@ -33,7 +35,7 @@ namespace Copium
return graphicsQueue;
}
VkQueue Device::GetPresentQueue() const
VkQueue Device::GetPresentQueue() const
{
return presentQueue;
}
@@ -65,6 +67,34 @@ namespace Copium
CP_ABORT("Failed to find suitable memory type");
}
void Device::WaitIdle()
{
vkDeviceWaitIdle(device);
CleanupIdleQueue();
}
void Device::WaitIdleIfCommandQueued()
{
if (!idleCommands.empty())
{
WaitIdle();
}
}
void Device::CleanupIdleQueue()
{
while (!idleCommands.empty())
{
idleCommands.front()();
idleCommands.pop();
}
}
void Device::QueueIdleCommand(std::function<void()> idleCommand)
{
idleCommands.emplace(idleCommand);
}
void Device::SelectPhysicalDevice()
{
uint32_t deviceCount;
@@ -81,18 +111,21 @@ namespace Copium
{
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
CP_INFO_CONT("\t%s", deviceProperties.deviceName);
CP_INFO_CONT("\t{}", deviceProperties.deviceName);
devicePriorities.emplace_back(device, GetPhysicalDevicePriority(device));
}
std::sort(devicePriorities.begin(), devicePriorities.end(), [](const std::pair<VkPhysicalDevice, uint32_t>& lhs, const std::pair<VkPhysicalDevice, uint32_t>& rhs) { return lhs.second > rhs.second; });
std::sort(devicePriorities.begin(),
devicePriorities.end(),
[](const std::pair<VkPhysicalDevice, uint32_t>& lhs, const std::pair<VkPhysicalDevice, uint32_t>& rhs)
{ return lhs.second > rhs.second; });
auto&& it = devicePriorities.begin();
CP_ASSERT(it->second != 0, "Failed to find suitable gpu");
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(it->first, &deviceProperties);
physicalDevice = it->first;
CP_INFO("Selecting device: %s", deviceProperties.deviceName);
CP_INFO("Selecting device: {}", deviceProperties.deviceName);
}
void Device::InitializeLogicalDevice()
+25 -17
View File
@@ -1,16 +1,36 @@
#pragma once
#include "copium/core/QueueFamilies.h"
#include "copium/util/Common.h"
#include <functional>
#include <queue>
#include <vulkan/vulkan.hpp>
#include "copium/util/Common.h"
namespace Copium
{
class Vulkan;
class Device
{
CP_DELETE_COPY_AND_MOVE_CTOR(Device);
public:
Device();
~Device();
uint32_t GetGraphicsQueueFamily() const;
uint32_t GetPresentQueueFamily() const;
VkQueue GetGraphicsQueue() const;
VkQueue GetPresentQueue() const;
VkCommandPool GetCommandPool() const;
VkPhysicalDevice GetPhysicalDevice() const;
operator VkDevice() const;
uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
void WaitIdle();
void WaitIdleIfCommandQueued();
void CleanupIdleQueue();
void QueueIdleCommand(std::function<void()> idleCommand);
private:
VkPhysicalDevice physicalDevice;
VkDevice device;
@@ -21,21 +41,9 @@ namespace Copium
uint32_t presentQueueIndex;
VkQueue graphicsQueue;
VkQueue presentQueue;
std::queue<std::function<void()>> idleCommands;
// TODO end
public:
Device();
~Device();
uint32_t GetGraphicsQueueFamily() const;
uint32_t GetPresentQueueFamily() const;
VkQueue GetGraphicsQueue() const;
VkQueue GetPresentQueue() const;
VkCommandPool GetCommandPool() const;
VkPhysicalDevice GetPhysicalDevice() const;
operator VkDevice() const;
uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
private:
void SelectPhysicalDevice();
void InitializeLogicalDevice();
@@ -44,4 +52,4 @@ namespace Copium
bool CheckDeviceExtensionSupport(VkPhysicalDevice device);
std::vector<const char*> GetRequiredDeviceExtensions();
};
}
}
@@ -1,16 +1,16 @@
#include "ImGuiInstance.h"
#include "copium/core/Vulkan.h"
#include "copium/buffer/CommandBufferScoped.h"
#include <imgui.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_vulkan.h>
#include <imgui.h>
#include "copium/buffer/CommandBufferScoped.h"
#include "copium/core/Vulkan.h"
namespace Copium
{
ImGuiInstance::ImGuiInstance()
: descriptorPool{std::make_unique<DescriptorPool>()}
: descriptorPool{std::make_unique<DescriptorPool>(SwapChain::MAX_FRAMES_IN_FLIGHT, 1)}
{
InitializeImGui();
InitializeDescriptorSetLayout();
@@ -92,7 +92,8 @@ namespace Copium
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
createInfo.bindingCount = 1;
createInfo.pBindings = layoutBindings.data();
CP_VK_ASSERT(vkCreateDescriptorSetLayout(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorSetLayout), "Failed to create ImGui DescriptorSetLayout");
CP_VK_ASSERT(vkCreateDescriptorSetLayout(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorSetLayout),
"Failed to create ImGui DescriptorSetLayout");
ShaderBinding binding;
binding.name = "texture";
@@ -109,4 +110,4 @@ namespace Copium
{
CP_VK_ASSERT(err, "Failed to initialize ImGui");
}
}
}
+7 -6
View File
@@ -1,7 +1,8 @@
#include "Instance.h"
#include "copium/core/Instance.h"
#include "copium/core/QueueFamilies.h"
#include "copium/util/Common.h"
#include <GLFW/glfw3.h>
#include "copium/util/Trace.h"
namespace Copium
{
@@ -42,7 +43,7 @@ namespace Copium
CP_INFO("Supported Extensions:");
for (auto&& extension : extensions)
{
CP_INFO_CONT("\t%s", extension.extensionName);
CP_INFO_CONT("\t{}", extension.extensionName);
}
std::vector<const char*> layers{};
@@ -88,7 +89,7 @@ namespace Copium
CP_INFO("Supported Layers:");
for (auto&& availableLayer : availableLayers)
{
CP_INFO_CONT("\t%s", availableLayer.layerName);
CP_INFO_CONT("\t{}", availableLayer.layerName);
}
for (auto&& layer : layers)
@@ -107,4 +108,4 @@ namespace Copium
}
return true;
}
}
}
+3 -3
View File
@@ -1,15 +1,15 @@
#pragma once
#include "copium/core/DebugMessenger.h"
#include <memory>
#include <GLFW/glfw3.h>
#include <set>
#include "copium/core/DebugMessenger.h"
namespace Copium
{
class Instance final
{
CP_DELETE_COPY_AND_MOVE_CTOR(Instance);
private:
VkInstance instance;
std::unique_ptr<DebugMessenger> debugMessenger;
+2 -1
View File
@@ -1,8 +1,9 @@
#pragma once
#include <optional>
#include <vulkan/vulkan.h>
#include <optional>
namespace Copium
{
struct QueueFamiliesQuery
+39 -19
View File
@@ -4,10 +4,10 @@
#include "copium/core/Vulkan.h"
#include "copium/sampler/Image.h"
#include <GLFW/glfw3.h>
namespace Copium
{
const int SwapChain::MAX_FRAMES_IN_FLIGHT = 2;
SwapChainSupportDetails::SwapChainSupportDetails(VkSurfaceKHR surface, VkPhysicalDevice physicalDevice)
{
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities);
@@ -35,6 +35,8 @@ namespace Copium
}
SwapChain::SwapChain()
: flightIndex{0},
resizeFramebuffer{false}
{
Initialize();
InitializeImageViews();
@@ -49,9 +51,13 @@ namespace Copium
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
{
vkDestroyFence(Vulkan::GetDevice(), inFlightFences[i], nullptr);
vkDestroySemaphore(Vulkan::GetDevice(), renderFinishedSemaphores[i], nullptr);
vkDestroySemaphore(Vulkan::GetDevice(), imageAvailableSemaphores[i], nullptr);
}
for (size_t i = 0; i < images.size(); ++i)
{
vkDestroySemaphore(Vulkan::GetDevice(), renderFinishedSemaphores[i], nullptr);
}
Destroy();
vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr);
}
@@ -115,7 +121,8 @@ namespace Copium
{
vkWaitForFences(Vulkan::GetDevice(), 1, &inFlightFences[flightIndex], VK_TRUE, UINT64_MAX);
VkResult result = vkAcquireNextImageKHR(Vulkan::GetDevice(), handle, UINT64_MAX, imageAvailableSemaphores[flightIndex], VK_NULL_HANDLE, &imageIndex);
VkResult result = vkAcquireNextImageKHR(
Vulkan::GetDevice(), handle, UINT64_MAX, imageAvailableSemaphores[flightIndex], VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR)
{
Recreate();
@@ -137,9 +144,10 @@ namespace Copium
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmd;
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &renderFinishedSemaphores[flightIndex];
submitInfo.pSignalSemaphores = &renderFinishedSemaphores[imageIndex];
CP_VK_ASSERT(vkQueueSubmit(Vulkan::GetDevice().GetGraphicsQueue(), 1, &submitInfo, inFlightFences[flightIndex]), "Failed to submit command buffer");
CP_VK_ASSERT(vkQueueSubmit(Vulkan::GetDevice().GetGraphicsQueue(), 1, &submitInfo, inFlightFences[flightIndex]),
"Failed to submit command buffer");
}
void SwapChain::EndPresent()
@@ -147,7 +155,7 @@ namespace Copium
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = &renderFinishedSemaphores[flightIndex];
presentInfo.pWaitSemaphores = &renderFinishedSemaphores[imageIndex];
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &handle;
presentInfo.pImageIndices = &imageIndex;
@@ -179,7 +187,7 @@ namespace Copium
glfwWaitEvents();
}
vkDeviceWaitIdle(Vulkan::GetDevice());
Vulkan::GetDevice().WaitIdle();
Destroy();
@@ -243,7 +251,8 @@ namespace Copium
createInfo.pQueueFamilyIndices = nullptr;
}
CP_VK_ASSERT(vkCreateSwapchainKHR(Vulkan::GetDevice(), &createInfo, nullptr, &handle), "Failed to initialize the swapchain");
CP_VK_ASSERT(vkCreateSwapchainKHR(Vulkan::GetDevice(), &createInfo, nullptr, &handle),
"Failed to initialize the swapchain");
vkGetSwapchainImagesKHR(Vulkan::GetDevice(), handle, &imageCount, nullptr);
images.resize(imageCount);
@@ -303,8 +312,10 @@ namespace Copium
VkSubpassDependency dependency{};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.srcStageMask =
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.dstStageMask =
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.srcAccessMask = 0;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
@@ -318,7 +329,8 @@ namespace Copium
renderPassCreateInfo.dependencyCount = 1;
renderPassCreateInfo.pDependencies = &dependency;
CP_VK_ASSERT(vkCreateRenderPass(Vulkan::GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "Failed to initialze render pass");
CP_VK_ASSERT(vkCreateRenderPass(Vulkan::GetDevice(), &renderPassCreateInfo, nullptr, &renderPass),
"Failed to initialze render pass");
}
void SwapChain::InitializeFramebuffers()
@@ -338,27 +350,34 @@ namespace Copium
createInfo.height = extent.height;
createInfo.layers = 1;
CP_VK_ASSERT(vkCreateFramebuffer(Vulkan::GetDevice(), &createInfo, nullptr, &framebuffers[i]), "Failed to initialize swap chain framebuffer");
CP_VK_ASSERT(vkCreateFramebuffer(Vulkan::GetDevice(), &createInfo, nullptr, &framebuffers[i]),
"Failed to initialize swap chain framebuffer");
}
}
void SwapChain::InitializeSyncObjects()
{
imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
VkSemaphoreCreateInfo semaphoreCreateInfo{};
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
{
CP_VK_ASSERT(vkCreateSemaphore(Vulkan::GetDevice(), &semaphoreCreateInfo, nullptr, &imageAvailableSemaphores[i]), "Failed to initialize available image semaphore");
CP_VK_ASSERT(vkCreateSemaphore(Vulkan::GetDevice(), &semaphoreCreateInfo, nullptr, &renderFinishedSemaphores[i]), "Failed to initialize render finished semaphore");
CP_VK_ASSERT(vkCreateSemaphore(Vulkan::GetDevice(), &semaphoreCreateInfo, nullptr, &imageAvailableSemaphores[i]),
"Failed to initialize available image semaphore");
VkFenceCreateInfo fenceCreateInfo{};
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
CP_VK_ASSERT(vkCreateFence(Vulkan::GetDevice(), &fenceCreateInfo, nullptr, &inFlightFences[i]), "Failed to initialize in flight fence");
CP_VK_ASSERT(vkCreateFence(Vulkan::GetDevice(), &fenceCreateInfo, nullptr, &inFlightFences[i]),
"Failed to initialize in flight fence");
}
renderFinishedSemaphores.resize(images.size());
for (size_t i = 0; i < renderFinishedSemaphores.size(); i++)
{
CP_VK_ASSERT(vkCreateSemaphore(Vulkan::GetDevice(), &semaphoreCreateInfo, nullptr, &renderFinishedSemaphores[i]),
"Failed to initialize render finished semaphore");
}
}
@@ -379,7 +398,8 @@ namespace Copium
{
for (auto&& availableFormat : availableFormats)
{
if (availableFormat.format == VK_FORMAT_R8G8B8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
if (availableFormat.format == VK_FORMAT_R8G8B8A8_UNORM &&
availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
{
return availableFormat;
}
@@ -410,7 +430,7 @@ namespace Copium
int width, height;
glfwGetFramebufferSize(window, &width, &height);
VkExtent2D extent{width, height};
VkExtent2D extent{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
extent.width = std::clamp(extent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
extent.height = std::clamp(extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
return extent;
+10 -6
View File
@@ -1,13 +1,15 @@
#pragma once
#include <GLFW/glfw3.h>
#include <vulkan/vulkan.h>
#include <memory>
#include <vector>
#include "copium/buffer/CommandBuffer.h"
#include "copium/sampler/DepthAttachment.h"
#include "copium/util/Common.h"
#include <GLFW/glfw3.h>
#include <vector>
#include <vulkan/vulkan.h>
namespace Copium
{
struct SwapChainSupportDetails
@@ -23,8 +25,10 @@ namespace Copium
class SwapChain final
{
CP_DELETE_COPY_AND_MOVE_CTOR(SwapChain);
public:
static const int MAX_FRAMES_IN_FLIGHT = 2;
static const int MAX_FRAMES_IN_FLIGHT;
private:
VkSwapchainKHR handle;
VkRenderPass renderPass;
@@ -34,7 +38,7 @@ namespace Copium
std::vector<VkImageView> imageViews;
std::vector<VkImage> images;
std::vector<VkFramebuffer> framebuffers;
uint32_t imageIndex;
uint32_t imageIndex;
bool resizeFramebuffer;
int flightIndex;
+27 -5
View File
@@ -16,17 +16,22 @@ namespace Copium
std::unique_ptr<SwapChain> Vulkan::swapChain;
std::unique_ptr<ImGuiInstance> Vulkan::imGuiInstance;
AssetHandle<Texture2D> Vulkan::emptyTexture2D;
AssetHandle<Texture2D> Vulkan::whiteTexture2D;
void Vulkan::Initialize()
{
Timer timer;
timer.Start();
glfwSetErrorCallback(glfw_error_callback);
CP_ASSERT(glfwInit() == GLFW_TRUE, "Failed to initialize the glfw context");
instance = std::make_unique<Instance>("Copium Engine");
window = std::make_unique<Window>("Copium Engine", 1440, 810, WindowMode::Windowed);
device = std::make_unique<Device>();
swapChain = std::make_unique<SwapChain>();
imGuiInstance = std::make_unique<ImGuiInstance>();
CP_INFO("Initialized Vulkan in %f seconds", timer.Elapsed());
CP_INFO("Initialized Vulkan in {} seconds", timer.Elapsed());
timer.Start();
AssetManager::RegisterAssetType<Texture2D>("Texture2D");
@@ -36,19 +41,26 @@ namespace Copium
AssetManager::RegisterAssetType<Font>("Font");
// TODO: Make the working directory always be relative to the assets folder
// By looking at where the executable is, since that should always be in the bin folder (it currently isn't though)
// By looking at where the executable is, since that should always be in the bin folder (it currently isn't
// though)
AssetManager::RegisterAssetDir("assets/");
emptyTexture2D = AssetHandle<Texture2D>{"empty_texture2d", std::make_unique<Texture2D>(std::vector<uint8_t>{255, 0, 255, 255}, 1, 1, SamplerCreator{})};
CP_INFO("Initialized AssetManager in %f seconds", timer.Elapsed());
emptyTexture2D = AssetHandle<Texture2D>{
"empty_texture2d", std::make_unique<Texture2D>(std::vector<uint8_t>{255, 0, 255, 255}, 1, 1, SamplerCreator{})};
whiteTexture2D = AssetHandle<Texture2D>{
"white_texture2d", std::make_unique<Texture2D>(std::vector<uint8_t>{255, 255, 255, 255}, 1, 1, SamplerCreator{})};
CP_INFO("Initialized AssetManager in {} seconds", timer.Elapsed());
}
void Vulkan::Destroy()
{
emptyTexture2D.UnloadAsset();
whiteTexture2D.UnloadAsset();
AssetManager::UnregisterAssetDir("assets/");
AssetManager::Cleanup();
imGuiInstance.reset();
device->WaitIdle();
swapChain.reset();
device->CleanupIdleQueue();
device.reset();
window.reset();
instance.reset();
@@ -79,6 +91,11 @@ namespace Copium
return *imGuiInstance;
}
AssetHandle<Texture2D> Vulkan::GetWhiteTexture2D()
{
return whiteTexture2D;
}
AssetHandle<Texture2D> Vulkan::GetEmptyTexture2D()
{
return emptyTexture2D;
@@ -88,4 +105,9 @@ namespace Copium
{
return instance && window && device && swapChain;
}
}
void Vulkan::glfw_error_callback(int error, const char* description)
{
CP_ABORT("GLFW Error {}: {}\n", error, description);
}
}
+12 -5
View File
@@ -1,21 +1,22 @@
#pragma once
#include <memory>
#include "copium/asset/AssetHandle.h"
#include "copium/core/Device.h"
#include "copium/core/ImGuiInstance.h"
#include "copium/core/Instance.h"
#include "copium/core/SwapChain.h"
#include "copium/core/Window.h"
#include "copium/core/ImGuiInstance.h"
#include "copium/util/Common.h"
#include "copium/sampler/Texture2D.h"
#include <memory>
#include "copium/util/Common.h"
namespace Copium
{
class Vulkan
{
CP_STATIC_CLASS(Vulkan);
private:
static std::unique_ptr<Instance> instance;
static std::unique_ptr<Window> window;
@@ -24,6 +25,8 @@ namespace Copium
static std::unique_ptr<ImGuiInstance> imGuiInstance;
static AssetHandle<Texture2D> emptyTexture2D;
static AssetHandle<Texture2D> whiteTexture2D;
public:
static void Initialize();
static void Destroy();
@@ -33,6 +36,10 @@ namespace Copium
static SwapChain& GetSwapChain();
static ImGuiInstance& GetImGuiInstance();
static bool Valid();
static AssetHandle<Texture2D> GetWhiteTexture2D();
static AssetHandle<Texture2D> GetEmptyTexture2D();
private:
static void glfw_error_callback(int error, const char* description);
};
}
}
+52 -35
View File
@@ -1,6 +1,9 @@
#include "copium/core/Window.h"
#include <GLFW/glfw3.h>
#include "copium/core/Vulkan.h"
#include "copium/event/DropEvent.h"
#include "copium/event/EventDispatcher.h"
#include "copium/event/Input.h"
#include "copium/event/KeyPressEvent.h"
@@ -9,15 +12,14 @@
#include "copium/event/MousePressEvent.h"
#include "copium/event/MouseReleaseEvent.h"
#include "copium/event/MouseScrollEvent.h"
#include "copium/event/WindowResizeEvent.h"
#include "copium/event/WindowFocusEvent.h"
#include <GLFW/glfw3.h>
#include "copium/event/WindowResizeEvent.h"
namespace Copium
{
Window::Window(const std::string& windowName, int width, int height, WindowMode mode)
: width{width}, height{height}
: width{width},
height{height}
{
InitializeWindow(windowName, width, height, mode);
InitializeSurface();
@@ -32,7 +34,6 @@ namespace Copium
bool Window::ShouldClose() const
{
return glfwWindowShouldClose(window);
}
int Window::GetWidth() const
@@ -72,34 +73,36 @@ namespace Copium
{
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
CP_ASSERT(glfwVulkanSupported(), "Vulkan is not supported");
switch (mode)
{
case WindowMode::Fullscreen:
{
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), monitor, nullptr);
width = mode->width;
height = mode->height;
break;
}
case WindowMode::BorderlessWindowed:
{
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
case WindowMode::Fullscreen:
{
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), monitor, nullptr);
width = mode->width;
height = mode->height;
break;
}
case WindowMode::BorderlessWindowed:
{
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwWindowHint(GLFW_DECORATED, false);
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), nullptr, nullptr);
width = mode->width;
height = mode->height;
break;
}
case WindowMode::Windowed:
{
window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
break;
}
default:
CP_ABORT("Unreachable switch case: %s", ToString(mode).c_str());
glfwWindowHint(GLFW_DECORATED, false);
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), nullptr, nullptr);
width = mode->width;
height = mode->height;
break;
}
case WindowMode::Windowed:
{
window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
break;
}
default:
CP_ABORT("Unreachable switch case: {}", mode);
}
CP_ASSERT(window, "Failed to initialize glfw window");
@@ -111,11 +114,13 @@ namespace Copium
glfwSetCursorPosCallback(window, MouseMoveCallback);
glfwSetWindowFocusCallback(window, WindowFocusCallback);
glfwSetScrollCallback(window, MouseScrollCallback);
glfwSetDropCallback(window, DropCallback);
}
void Window::InitializeSurface()
{
CP_VK_ASSERT(glfwCreateWindowSurface(Vulkan::GetInstance(), window, nullptr, &surface), "Failed to create Vulkan surface");
CP_VK_ASSERT(glfwCreateWindowSurface(Vulkan::GetInstance(), window, nullptr, &surface),
"Failed to create Vulkan surface");
}
void Window::FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height)
@@ -131,7 +136,7 @@ namespace Copium
void Window::KeyCallback(GLFWwindow* glfwWindow, int key, int scancode, int action, int mods)
{
if (key == -1) // For some reason media keys count as keys with value -1
if (key == -1) // For some reason media keys count as keys with value -1
return;
try
{
@@ -147,7 +152,8 @@ namespace Copium
}
}
catch (RuntimeException& exception)
{}
{
}
}
void Window::MouseButtonCallback(GLFWwindow* glfwWindow, int button, int action, int mods)
@@ -166,7 +172,8 @@ namespace Copium
}
}
catch (RuntimeException& exception)
{}
{
}
}
void Window::MouseMoveCallback(GLFWwindow* glfwWindow, double xpos, double ypos)
@@ -187,4 +194,14 @@ namespace Copium
{
EventDispatcher::QueueEvent(MouseScrollEvent{(int)xoffset, (int)yoffset});
}
}
void Window::DropCallback(GLFWwindow* window, int pathCount, const char* paths[])
{
std::vector<std::string> filePaths;
for (int i = 0; i < pathCount; i++)
{
filePaths.emplace_back(paths[i]);
}
EventDispatcher::QueueEvent(DropEvent{filePaths});
}
}
+8 -9
View File
@@ -1,24 +1,22 @@
#pragma once
#include <vulkan/vulkan.hpp>
#include "copium/util/Common.h"
#include "copium/util/Enum.h"
#include <vulkan/vulkan.hpp>
#define CP_WINDOW_MODE_ENUMS \
Fullscreen, \
BorderlessWindowed, \
Windowed
CP_ENUM_CREATOR(Copium, WindowMode, CP_WINDOW_MODE_ENUMS);
#define CP_WINDOW_MODE_ENUMS
struct GLFWwindow;
namespace Copium
{
CP_ENUM_CREATOR(WindowMode, Fullscreen, BorderlessWindowed, Windowed);
class Window final
{
CP_DELETE_COPY_AND_MOVE_CTOR(Window);
private:
GLFWwindow* window;
VkSurfaceKHR surface;
@@ -50,5 +48,6 @@ namespace Copium
static void MouseMoveCallback(GLFWwindow* window, double xpos, double ypos);
static void WindowFocusCallback(GLFWwindow* window, int focused);
static void MouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
static void DropCallback(GLFWwindow* window, int path_count, const char* paths[]);
};
}
}
@@ -1,19 +1,28 @@
#pragma once
#include "copium/ecs/ECSManager.h"
#include "copium/ecs/Config.h"
namespace Copium
{
class ECSManager;
template <typename Component>
class ComponentListener
{
public:
using component_type = Component;
friend class ECSManager;
protected:
ECSManager* manager;
public:
virtual void Added(EntityId entityId, Component& component) {}
virtual void Removed(EntityId entityId, Component& component) {}
virtual void Added(EntityId entityId, Component& component)
{
}
virtual void Removed(EntityId entityId, Component& component)
{
}
};
}
+116 -26
View File
@@ -1,12 +1,12 @@
#pragma once
#include <vector>
#include "copium/ecs/ComponentListener.h"
#include "copium/ecs/ComponentPoolBase.h"
#include "copium/ecs/Config.h"
#include "copium/ecs/EntitySet.h"
#include "copium/ecs/ComponentPoolBase.h"
#include "copium/ecs/ComponentListener.h"
#include "copium/util/Common.h"
#include <vector>
#include "copium/util/Trace.h"
namespace Copium
{
@@ -14,17 +14,29 @@ namespace Copium
class ComponentPool : public ComponentPoolBase
{
using Iterator = typename std::vector<Component>::iterator;
private:
std::vector<Component> components;
ComponentListener<Component>* listener = nullptr;
enum class QueueOperation
{
Add,
Remove
};
std::vector<QueueOperation> queueOperationOrder;
std::vector<std::pair<EntityId, Component>> addQueue;
std::vector<EntityId> removeQueue;
public:
ComponentPool()
{}
{
}
~ComponentPool() override
{
if(listener)
if (listener)
delete listener;
}
@@ -33,34 +45,61 @@ namespace Copium
Emplace(entity, component);
}
Component& Emplace(EntityId entity, const Component& component)
void Emplace(EntityId entity, const Component& component)
{
components.push_back(component);
entities.Emplace(entity);
if(listener)
listener->Added(entity, components.back());
return components.back();
addQueue.emplace_back(entity, component);
queueOperationOrder.emplace_back(QueueOperation::Add);
}
bool Erase(EntityId entity) override
{
size_t index = entities.Find(entity);
if (!entities.Erase(entity))
if (entities.Find(entity) == entities.Size())
return false;
if (listener)
{
auto it = components.begin() + index;
Component component = *it;
components.erase(it);
listener->Removed(entity, component);
}
else
{
components.erase(components.begin() + index);
}
removeQueue.emplace_back(entity);
queueOperationOrder.emplace_back(QueueOperation::Remove);
return true;
}
void CommitUpdates() override
{
if (queueOperationOrder.empty())
return;
CP_ASSERT(queueOperationOrder.size() == addQueue.size() + removeQueue.size(),
"queueOperationOrder size={} doesn't match the sum of the addQueue size={} and removeQueue size={}, "
"which is {}",
queueOperationOrder.size(),
addQueue.size(),
removeQueue.size(),
addQueue.size() + removeQueue.size());
int addQueueIndex = 0;
int removeQueueIndex = 0;
for (QueueOperation queueOperation : queueOperationOrder)
{
switch (queueOperation)
{
case QueueOperation::Add:
{
CommitAddComponent(addQueueIndex);
addQueueIndex++;
break;
}
case QueueOperation::Remove:
{
CommitRemoveComponent(removeQueueIndex);
removeQueueIndex++;
break;
}
}
}
removeQueue.clear();
addQueue.clear();
queueOperationOrder.clear();
}
Component& At(size_t index)
{
return operator[](index);
@@ -109,5 +148,56 @@ namespace Copium
{
return components.end();
}
void CommitAddComponent(int queueIndex)
{
CP_ASSERT(
queueIndex < addQueue.size(), "queueIndex={} is greater than the addQueueSize={}", queueIndex, addQueue.size());
const auto& [entity, component] = addQueue[queueIndex];
// TODO: Debugging errors caused by this assert might be a bit difficult, since there wont be any stacktrace for
// where the component was added. Might want to validate this in AddComponent somehow (like looping through
// the queued changes and work out if the component already exists)
CP_ASSERT(Find(entity) == Size(),
"Component already exists in entity (entity={}, Component={})",
entity,
typeid(Component).name());
components.push_back(component);
entities.Emplace(entity);
if (listener)
listener->Added(entity, components.back());
}
void CommitRemoveComponent(int queueIndex)
{
CP_ASSERT(queueIndex < removeQueue.size(),
"queueIndex={} is greater than the removeQueueSize={}",
queueIndex,
removeQueue.size());
const auto& entity = removeQueue[queueIndex];
size_t index = entities.Find(entity);
if (!entities.Erase(entity))
{
// TODO: Debugging warnings caused by this might be a bit difficult, since there wont be any stacktrace for
// where the component was added. Might want to validate this in AddComponent somehow (like looping
// through the queued changes and work out if the component already exists)
CP_WARN("Entity did not contain component (entity={}, Component={})", entity, typeid(Component).name());
return;
}
if (listener)
{
auto it = components.begin() + index;
Component component = *it;
components.erase(it);
listener->Removed(entity, component);
}
else
{
components.erase(components.begin() + index);
}
}
};
}
@@ -1,21 +1,23 @@
#pragma once
#include <vector>
#include "copium/ecs/Config.h"
#include "copium/ecs/EntitySet.h"
#include <vector>
namespace Copium
{
class ComponentPoolBase
{
protected:
EntitySet entities;
public:
virtual ~ComponentPoolBase() = default;
virtual size_t Size() = 0;
virtual bool Erase(EntityId entity) = 0;
virtual void CommitUpdates() = 0;
std::vector<EntityId>& GetEntities();
const std::vector<EntityId>& GetEntities() const;
};
+1
View File
@@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#include <limits>
namespace Copium
+41 -13
View File
@@ -1,32 +1,40 @@
#include "copium/ecs/ECSManager.h"
#include "copium/util/Common.h"
#include "copium/util/Trace.h"
namespace Copium
{
std::vector<EntityId> ECSManager::emptyEntities = {};
ECSManager::ECSManager()
: systemPool{std::make_unique<SystemPool>(this)}
{}
{
}
ECSManager::~ECSManager()
{
for (auto&& components : componentPool)
for (auto&& components : componentPools)
{
delete components.second;
}
componentPool.clear();
componentPools.clear();
}
void ECSManager::UpdateSystems()
void ECSManager::CommitEntityUpdates()
{
systemPool->Update();
for (auto& componentPool : componentPools)
{
componentPool.second->CommitUpdates();
}
}
void ECSManager::UpdateSystems(const Signal& signal)
void ECSManager::UpdateSystems(const Uuid& systemPoolId)
{
// TODO: Maybe we want a different pool for Signal based Systems for performance reasons?
// Maybe even a pool for each type of Signal?
systemPool->Update(signal);
auto it = systemPools.find(systemPoolId);
CP_ASSERT(it != systemPools.end(), "SystemPool doesn't exist with Uuid={}", systemPoolId);
it->second->CommitSignals();
CommitEntityUpdates();
it->second->CommitUpdates();
it->second->Update();
}
size_t ECSManager::GetEntityCount() const
@@ -36,6 +44,13 @@ namespace Copium
EntityId ECSManager::CreateEntity()
{
if (!destroyedEntityIds.empty())
{
EntityId newId = *destroyedEntityIds.begin();
destroyedEntityIds.erase(destroyedEntityIds.begin());
return newId;
}
CP_ASSERT(currentEntityId != MAX_NUM_ENTITIES, "No more entities available");
entities.emplace(currentEntityId);
currentEntityId++;
@@ -45,9 +60,22 @@ namespace Copium
void ECSManager::DestroyEntity(EntityId entity)
{
auto it = entities.find(entity);
CP_ASSERT(it != entities.end(), "Entity does not exist in ECSManager (entity=%u)", entity);
CP_ASSERT(it != entities.end(), "Entity does not exist in ECSManager (entity={})", entity);
if (entity == currentEntityId - 1)
{
currentEntityId--;
while (!destroyedEntityIds.empty() && *destroyedEntityIds.rbegin() == currentEntityId - 1)
{
destroyedEntityIds.erase(std::prev(destroyedEntityIds.end()));
}
}
else
{
destroyedEntityIds.emplace(entity);
}
entities.erase(it);
for (auto&& pool : componentPool)
for (auto&& pool : componentPools)
{
pool.second->Erase(entity);
}
+110 -36
View File
@@ -1,46 +1,80 @@
#pragma once
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <typeindex>
#include <unordered_set>
#include "copium/ecs/ComponentPool.h"
#include "copium/ecs/Config.h"
#include "copium/ecs/Signal.h"
#include "copium/ecs/SystemPool.h"
#include "copium/util/Common.h"
#include <functional>
#include <map>
#include <typeindex>
#include <unordered_set>
#include <vector>
#include "copium/util/GenericType.h"
#include "copium/util/Trace.h"
#include "copium/util/Uuid.h"
namespace Copium
{
class ECSManager final
{
CP_DELETE_COPY_AND_MOVE_CTOR(ECSManager);
private:
std::unordered_set<EntityId> entities;
std::map<std::type_index, ComponentPoolBase*> componentPool;
std::map<std::type_index, ComponentPoolBase*> componentPools;
std::map<Uuid, std::unique_ptr<SystemPool>> systemPools;
EntityId currentEntityId = 1;
std::set<EntityId> destroyedEntityIds;
std::map<std::type_index, GenericType> globalDatas;
std::unique_ptr<SystemPool> systemPool;
int currentEntityId = 1;
public:
static std::vector<EntityId> emptyEntities;
ECSManager();
~ECSManager();
template <typename SystemClass, typename... Args>
SystemOrderer AddSystem(const Args&... args)
SystemOrderer& AddSystem(const Uuid& systemPoolId)
{
return systemPool->AddSystem(typeid(SystemClass), new SystemClass{args...});
auto it = systemPools.find(systemPoolId);
if (it == systemPools.end())
it = systemPools.emplace(systemPoolId, std::make_unique<SystemPool>(this)).first;
return it->second->AddSystem(typeid(SystemClass), new SystemClass{});
}
template <typename SystemClass>
void RemoveSystem()
void RemoveSystem(const Uuid& systemPoolId)
{
systemPool->MoveSystemBefore(typeid(SystemClass));
auto it = systemPools.find(systemPoolId);
CP_ASSERT(it != systemPools.end(), "SystemPool doesn't exist with Uuid={}", systemPoolId);
it->second->RemoveSystem(typeid(SystemClass));
if (it->second->IsEmpty())
systemPools.erase(it);
}
void UpdateSystems();
void UpdateSystems(const Signal& signal);
void CommitEntityUpdates();
void UpdateSystems(const Uuid& systemPoolId);
template <typename S, typename... Args>
void SendSignal(const Args&... args)
{
Signal::ValidateSignal<S>();
for (auto& systemPool : systemPools)
{
S* newSignal = new S{args...};
newSignal->uuid = S::UUID;
systemPool.second->SendSignal(newSignal);
}
}
EntityId CreateEntity();
void DestroyEntity(EntityId entity);
@@ -62,38 +96,36 @@ namespace Copium
else
{
ComponentPool<Component>* pool{new ComponentPool<Component>{}};
componentPool.emplace(GetComponentId<Component>(), pool);
componentPools.emplace(GetComponentId<Component>(), pool);
pool->SetComponentListener(listener);
}
}
template <typename... Components>
std::tuple<Components&...> AddComponents(EntityId entity, Components&&... components)
void AddComponents(EntityId entity, Components&&... components)
{
return std::forward_as_tuple(AddComponent(entity, Components(components))...);
(AddComponent(entity, components), ...);
}
template <typename Component, typename... Args>
Component& AddComponent(EntityId entity, Args&&... args)
void AddComponent(EntityId entity, Args&&... args)
{
return AddComponent(entity, Component{args...});
AddComponent(entity, Component{args...});
}
template <typename Component>
Component& AddComponent(EntityId entity, const Component& component)
void AddComponent(EntityId entity, const Component& component)
{
auto pool = GetComponentPool<Component>();
if (pool)
{
CP_ASSERT(!HasComponent<Component>(entity), "Component already exists in entity (entity=%u, Component=%s)", entity, typeid(Component).name());
return pool->Emplace(entity, component);
pool->Emplace(entity, component);
}
else
{
ComponentPool<Component>* pool{new ComponentPool{entity, component}};
auto ret = componentPool.emplace(GetComponentId<Component>(), pool);
return pool->At(0);
auto ret = componentPools.emplace(GetComponentId<Component>(), pool);
}
}
@@ -101,7 +133,7 @@ namespace Copium
void RemoveComponent(EntityId entity)
{
auto pool = GetComponentPoolAssure<Component>();
CP_ASSERT(pool->Erase(entity), "Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name());
pool->Erase(entity);
}
template <typename... Components>
@@ -115,7 +147,8 @@ namespace Copium
{
auto pool = GetComponentPoolAssure<Component>();
Component* component = pool->FindComponent(entity);
CP_ASSERT(component, "Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name());
CP_ASSERT(
component, "Entity did not contain component (entity={}, Component={})", entity, typeid(Component).name());
return *component;
}
@@ -210,26 +243,67 @@ namespace Copium
return 0;
}
template <typename Component, typename... Components>
EntityId Find()
{
return Find<Component, Components...>([](EntityId, const Component& component, const Components&... components)
{ return true; });
}
template <typename T>
std::type_index GetComponentId()
{
return std::type_index(typeid(T));
}
private:
template <typename Component>
ComponentPool<Component>* GetComponentPool()
template <typename T, typename... Args>
void AddGlobalData(const Args&... args)
{
auto it = componentPool.find(GetComponentId<Component>());
return it == componentPool.end() ? nullptr : static_cast<ComponentPool<Component>*>(it->second);
auto it = globalDatas.find(typeid(T));
CP_ASSERT(!HasGlobalData<T>(), "Global with typeid={} already exists. Do nothing", typeid(T).name());
globalDatas.emplace(typeid(T), GenericType::Create<T>(args...));
}
template <typename T>
T& GetGlobalData()
{
auto it = globalDatas.find(typeid(T));
CP_ASSERT(it != globalDatas.end(), "Global with typeid={} doesn't exist");
return it->second.Get<T>();
}
template <typename T>
bool HasGlobalData()
{
return globalDatas.find(typeid(T)) != globalDatas.end();
}
template <typename T>
void RemoveGlobalData()
{
auto it = globalDatas.find(typeid(T));
CP_ASSERT("Global with typeid={} doesn't exist. Do nothing", typeid(T).name());
globalDatas.erase(it);
}
template <typename Component>
ComponentPool<Component>* GetComponentPoolAssure()
ComponentPool<std::remove_const_t<Component>>* GetComponentPool()
{
auto it = componentPool.find(GetComponentId<Component>());
CP_ASSERT(it != componentPool.end(), "Component has not been added to an entity (Component=%s)", typeid(Component).name());
return static_cast<ComponentPool<Component>*>(it->second);
auto it = componentPools.find(GetComponentId<Component>());
return it == componentPools.end() ? nullptr
: static_cast<ComponentPool<std::remove_const_t<Component>>*>(it->second);
}
template <typename Component>
ComponentPool<std::remove_const_t<Component>>* GetComponentPoolAssure()
{
auto it = componentPools.find(GetComponentId<Component>());
CP_ASSERT(it != componentPools.end(),
"Component has not been added to an entity (Component={})",
typeid(Component).name());
return static_cast<ComponentPool<std::remove_const_t<Component>>*>(it->second);
}
};
}
+19 -8
View File
@@ -3,16 +3,22 @@
namespace Copium
{
Entity::Entity()
: manager{nullptr}, id{INVALID_ENTITY}
{}
: manager{nullptr},
id{INVALID_ENTITY}
{
}
Entity::Entity(ECSManager* manager)
: manager{manager}, id{INVALID_ENTITY}
{}
: manager{manager},
id{INVALID_ENTITY}
{
}
Entity::Entity(ECSManager* manager, EntityId id)
: manager{manager}, id{id}
{}
: manager{manager},
id{id}
{
}
Entity::operator EntityId() const
{
@@ -34,7 +40,7 @@ namespace Copium
return id != entity.id;
}
Entity::operator bool() const
bool Entity::IsValid() const
{
if (id == INVALID_ENTITY)
return false;
@@ -43,6 +49,11 @@ namespace Copium
return false;
}
Entity::operator bool() const
{
return IsValid();
}
void Entity::Invalidate()
{
id = INVALID_ENTITY;
@@ -73,4 +84,4 @@ namespace Copium
{
return {manager, manager->CreateEntity()};
}
}
}
+5 -4
View File
@@ -24,6 +24,7 @@ namespace Copium
bool operator!=(const Entity& entity);
operator bool() const;
bool IsValid() const;
void Invalidate();
void Destroy();
void SetId(EntityId entityId);
@@ -33,15 +34,15 @@ namespace Copium
static Entity Create(ECSManager* manager);
template <typename Component, typename... Args>
inline Component& AddComponent(Args... args)
inline void AddComponent(Args... args)
{
return manager->AddComponent<Component>(id, args...);
manager->AddComponent<Component>(id, args...);
}
template <typename... Components>
std::tuple<Components&...> AddComponents(Components&&... components)
void AddComponents(Components&&... components)
{
return manager->AddComponents(id, components...);
manager->AddComponents(id, components...);
}
template <typename Component>
+18 -4
View File
@@ -55,9 +55,23 @@ namespace Copium
return entitiesList.size();
}
std::vector<EntityId>& EntitySet::GetList() { return entitiesList; }
const std::vector<EntityId>& EntitySet::GetList() const { return entitiesList; }
std::vector<EntityId>& EntitySet::GetList()
{
return entitiesList;
}
std::vector<EntityId>::iterator EntitySet::begin() { return entitiesList.begin(); }
std::vector<EntityId>::iterator EntitySet::end() { return entitiesList.end(); }
const std::vector<EntityId>& EntitySet::GetList() const
{
return entitiesList;
}
std::vector<EntityId>::iterator EntitySet::begin()
{
return entitiesList.begin();
}
std::vector<EntityId>::iterator EntitySet::end()
{
return entitiesList.end();
}
}
+3 -3
View File
@@ -1,17 +1,17 @@
#pragma once
#include "copium/ecs/Config.h"
#include <unordered_map>
#include <vector>
#include "copium/ecs/Config.h"
namespace Copium
{
class EntitySet
{
private:
std::vector<EntityId> entitiesList;
std::unordered_map<EntityId, size_t> entitiesMap; // Maps the entity id to a component index
std::unordered_map<EntityId, size_t> entitiesMap; // Maps the entity id to a component index
public:
bool Emplace(EntityId entity);
bool Erase(EntityId entity);
-10
View File
@@ -1,10 +0,0 @@
#include "copium/ecs/Signal.h"
namespace Copium
{
int Signal::GetAllocatedId()
{
allocatedIds++;
return allocatedIds;
}
}
+28 -34
View File
@@ -1,48 +1,42 @@
#pragma once
#define CP_SIGNAL_DECLERATION_DEFINITION() \
static int GetIdStatic() \
{ \
static int id = GetAllocatedId(); \
return id; \
} \
\
int GetId() const \
{ \
return GetIdStatic(); \
}
#include "copium/util/Uuid.h"
#define CP_SIGNAL_DECLERATION(SignalClass) \
static int GetIdStatic(); \
int GetId() const override
#define CP_SIGNAL_DEFINITION(SignalClass) \
int SignalClass::GetIdStatic() \
{ \
static int id = GetAllocatedId(); \
return id; \
} \
\
int SignalClass::GetId() const \
{ \
return GetIdStatic(); \
}
#define CP_REGISTER_SIGNAL static inline Copium::Uuid UUID = Copium::Uuid()
namespace Copium
{
class Signal
{
private:
static inline int allocatedIds = 0;
public:
Signal() = default;
virtual ~Signal() = default;
virtual int GetId() const = 0;
const Uuid& GetUuid() const
{
return uuid;
}
protected:
static int GetAllocatedId();
template <typename T>
static void ValidateSignal()
{
static_assert(std::is_base_of_v<Signal, T>, "ValidateSignal : Given T is does not have Signal as base class");
static_assert(
has_static_uuid<T>(),
"ValidateSignal : Given T has not defined a UUID. Did you add CP_REGISTER_SIGNAL to your Signal class?");
}
private:
friend class ECSManager;
Uuid uuid;
template <typename, typename = void>
struct has_static_uuid : std::false_type
{
};
template <typename T>
struct has_static_uuid<T, std::void_t<decltype(T::UUID)>> : std::true_type
{
};
};
}
+38 -25
View File
@@ -1,43 +1,56 @@
#pragma once
#include <set>
#include "copium/ecs/ECSManager.h"
#include "copium/ecs/SystemBase.h"
#include "copium/ecs/Entity.h"
#include "copium/ecs/Signal.h"
namespace Copium
{
template <typename... Components>
class System : public SystemBase
class System
{
public:
void Run() override
virtual ~System() = default;
virtual void Run() = 0;
virtual void HandleSignal(const Signal& signal)
{
manager->Each<Components...>([&](EntityId entityId, Components&... components) { RunEntity(Entity{manager, entityId}, components...); });
}
void Run(const Signal& signal) override
const bool IsSignalSubscribed(const Uuid& uuid) const
{
manager->Each<Components...>([&](EntityId entityId, Components&... components) { RunEntity(signal, Entity{manager, entityId}, components...); });
return subscribedSignals.count(uuid) != 0;
}
virtual void RunEntity(Entity entity, Components&... components) {};
virtual void RunEntity(const Signal& signal, Entity entity, Components&... components) {};
// TODO: Not sure if this is the way entities should be validated
std::set<EntityId> loggedEntities;
template <typename... Components>
bool ValidateEntity(Entity entity)
template <typename S>
void SubscribeToSignal()
{
if (entity && entity.HasComponents<Components...>())
{
loggedEntities.erase(entity);
return true;
}
if (loggedEntities.find(entity) == loggedEntities.end())
CP_WARN("Invalid Entity");
loggedEntities.emplace(entity);
return false;
Signal::ValidateSignal<S>();
subscribedSignals.emplace(S::UUID);
}
template <typename S>
void UnsubscribeToSignal()
{
Signal::ValidateSignal<S>();
subscribedSignals.erase(S::UUID);
}
void SetECSManager(ECSManager* manager)
{
this->manager = manager;
}
template <typename S, typename... Args>
void SendSignal(const Args&... args)
{
manager->SendSignal<S>(args...);
}
protected:
ECSManager* manager;
private:
std::set<Uuid> subscribedSignals{};
};
}
-19
View File
@@ -1,19 +0,0 @@
#pragma once
#include "copium/ecs/Signal.h"
namespace Copium
{
class ECSManager;
class SystemBase
{
friend class SystemPool;
protected:
ECSManager* manager;
public:
virtual void Run() = 0;
virtual void Run(const Signal& signal) = 0;
};
}
+26 -4
View File
@@ -7,15 +7,37 @@ namespace Copium
SystemOrderer::SystemOrderer(std::type_index systemId, SystemPool* systemPool)
: systemId{systemId},
systemPool{systemPool}
{}
{
}
void SystemOrderer::Before(const std::type_index& otherSystemId)
void SystemOrderer::CommitOrdering()
{
for (const auto& [orderOperation, systemId] : orderQueue)
{
switch (orderOperation)
{
case OrderOperation::Before:
{
CommitBefore(systemId);
break;
}
case OrderOperation::After:
{
CommitAfter(systemId);
break;
}
}
}
orderQueue.clear();
}
void SystemOrderer::CommitBefore(const std::type_index& otherSystemId)
{
systemPool->MoveSystemBefore(systemId, otherSystemId);
}
void SystemOrderer::After(const std::type_index& otherSystemId)
void SystemOrderer::CommitAfter(const std::type_index& otherSystemId)
{
systemPool->MoveSystemAfter(systemId, otherSystemId);
}
}
}
+21 -11
View File
@@ -1,8 +1,7 @@
#pragma once
#include <vector>
#include <map>
#include <typeindex>
#include <vector>
namespace Copium
{
@@ -10,26 +9,37 @@ namespace Copium
class SystemOrderer
{
private:
std::type_index systemId;
SystemPool* systemPool = nullptr;
public:
SystemOrderer(std::type_index systemId, SystemPool* systemPool);
template <typename Other>
template <typename System>
void Before()
{
Before(typeid(Other));
orderQueue.emplace_back(OrderOperation::Before, typeid(System));
}
template <typename Other>
template <typename System>
void After()
{
After(typeid(Other));
orderQueue.emplace_back(OrderOperation::After, typeid(System));
}
private:
void Before(const std::type_index& otherSystemId);
void After(const std::type_index& otherSystemId);
enum class OrderOperation
{
Before,
After
};
std::type_index systemId;
SystemPool* systemPool = nullptr;
std::vector<std::pair<OrderOperation, std::type_index>> orderQueue;
private:
friend class SystemPool;
void CommitOrdering();
void CommitBefore(const std::type_index& otherSystemId);
void CommitAfter(const std::type_index& otherSystemId);
};
}
+113 -24
View File
@@ -1,10 +1,15 @@
#include "copium/ecs/SystemPool.h"
#include <algorithm>
#include "copium/ecs/System.h"
namespace Copium
{
SystemPool::SystemPool(ECSManager* manager)
: manager{manager}
{}
{
}
SystemPool::~SystemPool()
{
@@ -16,28 +21,56 @@ namespace Copium
systems.clear();
}
SystemOrderer SystemPool::AddSystem(const std::type_index& systemId, SystemBase* system)
SystemOrderer& SystemPool::AddSystem(const std::type_index& systemId, System* system)
{
system->manager = manager;
CP_ASSERT(systems.find(systemId) == systems.end(), "System already exist in Ecs");
systems.emplace(systemId, system);
systemOrder.emplace_back(system);
return SystemOrderer{systemId, this};
system->SetECSManager(manager);
addQueue.emplace_back(systemId, system, SystemOrderer{systemId, this});
queueOperationOrder.emplace_back(QueueOperation::Add);
return std::get<2>(addQueue.back());
}
void SystemPool::RemoveSystem(const std::type_index& systemId)
{
auto it = systems.find(systemId);
if (it == systems.end())
{
CP_WARN("System does not exist in Ecs");
return;
}
removeQueue.emplace_back(systemId);
queueOperationOrder.emplace_back(QueueOperation::Remove);
}
auto itOrder = std::find(systemOrder.begin(), systemOrder.end(), it->second);
delete it->second;
systems.erase(it);
systemOrder.erase(itOrder);
void SystemPool::CommitUpdates()
{
if (queueOperationOrder.empty())
return;
CP_ASSERT(queueOperationOrder.size() == addQueue.size() + removeQueue.size(),
"queueOperationOrder size={} doesn't match the sum of the addQueue size={} and removeQueue size={}, "
"which is {}",
queueOperationOrder.size(),
addQueue.size(),
removeQueue.size(),
addQueue.size() + removeQueue.size());
int addQueueIndex = 0;
int removeQueueIndex = 0;
for (QueueOperation queueOperation : queueOperationOrder)
{
switch (queueOperation)
{
case QueueOperation::Add:
{
CommitAddSystem(addQueueIndex);
addQueueIndex++;
break;
}
case QueueOperation::Remove:
{
CommitRemoveSystem(removeQueueIndex);
removeQueueIndex++;
break;
}
}
}
removeQueue.clear();
addQueue.clear();
queueOperationOrder.clear();
}
void SystemPool::Update()
@@ -48,20 +81,44 @@ namespace Copium
}
}
void SystemPool::Update(const Signal& signal)
void SystemPool::SendSignal(Signal* signal)
{
for (auto& system : systemOrder)
signalQueue.emplace(signal);
}
void SystemPool::CommitSignals()
{
while (!signalQueue.empty())
{
system->Run(signal);
auto& signal = signalQueue.front();
for (auto& system : systemOrder)
{
if (system->IsSignalSubscribed(signal->GetUuid()))
{
system->HandleSignal(*signal);
}
}
delete signal;
signalQueue.pop();
}
}
size_t SystemPool::Size() const
{
return systemOrder.size();
}
bool SystemPool::IsEmpty() const
{
return systemOrder.empty();
}
void SystemPool::MoveSystemAfter(const std::type_index& systemId, const std::type_index& afterSystemId)
{
auto it1 = systems.find(systemId);
CP_ASSERT(it1 != systems.end(), "System does not exist in SystemPool");
CP_ASSERT(it1 != systems.end(), "System with typeid={} does not exist in SystemPool", systemId.name());
auto it2 = systems.find(afterSystemId);
CP_ASSERT(it2 != systems.end(), "System does not exist in SystemPool");
CP_ASSERT(it2 != systems.end(), "System with typeid={} does not exist in SystemPool", afterSystemId.name());
auto itSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it1->second);
auto itAfterSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it2->second);
@@ -71,13 +128,45 @@ namespace Copium
void SystemPool::MoveSystemBefore(const std::type_index& systemId, const std::type_index& beforeSystemId)
{
auto it1 = systems.find(systemId);
CP_ASSERT(it1 != systems.end(), "System does not exist in SystemPool");
CP_ASSERT(it1 != systems.end(), "System with typeid={} does not exist in SystemPool", systemId.name());
auto it2 = systems.find(beforeSystemId);
CP_ASSERT(it2 != systems.end(), "System does not exist in SystemPool");
CP_ASSERT(it2 != systems.end(), "System with typeid={} does not exist in SystemPool", beforeSystemId.name());
auto itSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it1->second);
auto itBeforeSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it2->second);
std::rotate(itSystemId, itSystemId + 1, itBeforeSystemId + 1);
}
void SystemPool::CommitAddSystem(int queueIndex)
{
CP_ASSERT(
queueIndex < addQueue.size(), "queueIndex={} is greater than the addQueueSize={}", queueIndex, addQueue.size());
auto& [systemId, system, ordering] = addQueue[queueIndex];
CP_ASSERT(systems.find(systemId) == systems.end(), "System with typeid={} already exists in SystemPool");
systems.emplace(systemId, system);
systemOrder.emplace_back(system);
ordering.CommitOrdering();
}
void SystemPool::CommitRemoveSystem(int queueIndex)
{
CP_ASSERT(queueIndex < removeQueue.size(),
"queueIndex={} is greater than the removeQueueSize={}",
queueIndex,
removeQueue.size());
const auto& systemId = removeQueue[queueIndex];
auto it = systems.find(systemId);
CP_ASSERT(it != systems.end(), "System with typeid={} does not exist in SystemPool", systemId.name());
auto itOrder = std::find(systemOrder.begin(), systemOrder.end(), it->second);
CP_ASSERT(itOrder != systemOrder.end(), "System with typeid={} does not exist in systemOrder", systemId.name());
delete it->second;
systems.erase(it);
systemOrder.erase(itOrder);
}
}
+33 -12
View File
@@ -1,36 +1,57 @@
#pragma once
#include "copium/ecs/SystemBase.h"
#include "copium/ecs/SystemOrderer.h"
#include "copium/ecs/Signal.h"
#include "copium/util/Common.h"
#include <vector>
#include <map>
#include <queue>
#include <typeindex>
#include <vector>
#include "copium/ecs/Signal.h"
#include "copium/ecs/SystemOrderer.h"
#include "copium/util/Common.h"
namespace Copium
{
class ECSManager;
class System;
class SystemPool final
{
CP_DELETE_COPY_AND_MOVE_CTOR(SystemPool);
private:
ECSManager* manager;
std::map<std::type_index, SystemBase*> systems;
std::vector<SystemBase*> systemOrder;
public:
SystemPool(ECSManager* manager);
~SystemPool();
SystemOrderer AddSystem(const std::type_index& systemId, SystemBase* system);
SystemOrderer& AddSystem(const std::type_index& systemId, System* system);
void RemoveSystem(const std::type_index& systemId);
void Update();
void Update(const Signal& signal);
void SendSignal(Signal* signal);
void CommitSignals();
void CommitUpdates();
size_t Size() const;
bool IsEmpty() const;
void MoveSystemAfter(const std::type_index& systemId, const std::type_index& afterSystemId);
void MoveSystemBefore(const std::type_index& systemId, const std::type_index& beforeSystemId);
private:
ECSManager* manager;
std::map<std::type_index, System*> systems;
std::vector<System*> systemOrder;
enum class QueueOperation
{
Add,
Remove
};
std::vector<QueueOperation> queueOperationOrder;
std::vector<std::tuple<std::type_index, System*, SystemOrderer>> addQueue;
std::vector<std::type_index> removeQueue;
std::queue<Signal*> signalQueue;
void CommitAddSystem(int queueIndex);
void CommitRemoveSystem(int queueIndex);
};
}
+113
View File
@@ -0,0 +1,113 @@
#pragma once
#include "copium/ecs/ECSManager.h"
#include "copium/ecs/Entity.h"
namespace Copium
{
template <typename Component, typename... Components>
struct View
{
class Iterator
{
public:
std::tuple<Entity, Component&, Components&...> operator*()
{
EntityId entityId{entities[index]};
CP_ASSERT((manager->HasComponents<Component, Components...>(entityId)),
"entity doesn't contains all components");
return std::forward_as_tuple<Entity, Component&, Components&...>(
Entity{manager, entityId}, firstPool->At(index), manager->GetComponent<Components>(entityId)...);
}
Iterator& operator++()
{
++index;
FindNextEntity();
return *this;
}
Iterator operator++(int)
{
Iterator it = *this;
++*this;
return it;
}
Iterator operator+(size_t advance)
{
Iterator it = *this;
for (int i = 0; i < advance; i++)
++it;
return it;
}
bool operator==(const Iterator& other) const
{
return index == other.index;
}
bool operator!=(const Iterator& other) const
{
return !(*this == other);
}
private:
friend class View;
ECSManager* manager;
ComponentPool<std::remove_const_t<Component>>* firstPool;
const std::vector<EntityId>& entities;
int index;
Iterator(ECSManager* manager, ComponentPool<std::remove_const_t<Component>>* firstPool)
: manager{manager},
firstPool{firstPool},
entities{firstPool ? firstPool->GetEntities() : ECSManager::emptyEntities},
index{0}
{
}
static Iterator Begin(ECSManager* manager)
{
Iterator iterator{manager, manager->GetComponentPool<Component>()};
iterator.FindNextEntity();
return iterator;
}
static Iterator End(ECSManager* manager)
{
Iterator iterator{manager, manager->GetComponentPool<Component>()};
if (iterator.firstPool)
iterator.index = iterator.firstPool->GetEntities().size();
return iterator;
}
void FindNextEntity()
{
while (index < entities.size() && !manager->HasComponents<Components...>(entities[index]))
{
++index;
}
}
};
View(ECSManager* manager)
: manager{manager}
{
}
Iterator begin()
{
return Iterator::Begin(manager);
}
Iterator end()
{
return Iterator::End(manager);
}
private:
ECSManager* manager;
};
}
@@ -0,0 +1,15 @@
#include "copium/event/DropEvent.h"
namespace Copium
{
DropEvent::DropEvent(const std::vector<std::string>& filePaths)
: Event{EventType::Drop},
filePaths{filePaths}
{
}
const std::vector<std::string>& DropEvent::GetFilePaths() const
{
return filePaths;
}
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include "copium/event/Event.h"
namespace Copium
{
class DropEvent : public Event
{
private:
std::vector<std::string> filePaths;
public:
DropEvent(const std::vector<std::string>& filePaths);
const std::vector<std::string>& GetFilePaths() const;
};
}
+2 -1
View File
@@ -5,7 +5,8 @@ namespace Copium
Event::Event(EventType type)
: type{type}
{}
{
}
EventType Event::GetType() const
{
+3
View File
@@ -8,9 +8,12 @@ namespace Copium
{
private:
EventType type;
public:
Event(EventType type);
virtual ~Event() = default;
EventType GetType() const;
};
}
@@ -1,6 +1,6 @@
#include "copium/event/EventDispatcher.h"
#include "copium/util/Common.h"
#include "copium/util/Trace.h"
namespace Copium
{
@@ -27,15 +27,15 @@ namespace Copium
EventResult result = eventHandler->OnEvent(*event);
switch (result)
{
case EventResult::Continue:
continue;
case EventResult::Handled:
return;
case EventResult::Focus:
focusedEventHandler = eventHandler;
return;
default:
CP_ABORT("Unreachable switch case: %s", ToString(result).c_str());
case EventResult::Continue:
continue;
case EventResult::Handled:
return;
case EventResult::Focus:
focusedEventHandler = eventHandler;
return;
default:
CP_ABORT("Unreachable switch case: {}", result);
}
}
}
@@ -1,12 +1,12 @@
#pragma once
#include <memory>
#include <queue>
#include <vector>
#include "copium/event/Event.h"
#include "copium/event/EventHandler.h"
#include <memory>
#include <vector>
#include <queue>
namespace Copium
{
class EventDispatcher
@@ -15,12 +15,14 @@ namespace Copium
static EventHandler* focusedEventHandler;
static std::vector<EventHandler*> eventHandlers;
static std::queue<std::unique_ptr<Event>> events;
public:
template <typename EventType>
static void QueueEvent(const EventType& event)
{
events.push(std::make_unique<EventType>(event));
}
static void Dispatch();
static void AddEventHandler(EventHandler* eventHandler);
static void RemoveEventHandler(EventHandler* eventHandler);
+4 -6
View File
@@ -2,9 +2,7 @@
#include "copium/util/Enum.h"
#define CP_EVENT_RESULT_ENUMS \
Continue, \
Handled, \
Focus
CP_ENUM_CREATOR(Copium, EventResult, CP_EVENT_RESULT_ENUMS);
namespace Copium
{
CP_ENUM_CREATOR(EventResult, Continue, Handled, Focus);
}
@@ -1,15 +0,0 @@
#include "copium/event/EventSignal.h"
namespace Copium
{
EventSignal::EventSignal(const Event& event)
: event{event}
{}
const Event& EventSignal::GetEvent() const
{
return event;
}
CP_SIGNAL_DEFINITION(EventSignal);
}
@@ -1,22 +0,0 @@
#pragma once
#include "copium/event/Event.h"
#include "copium/ecs/Signal.h"
namespace Copium
{
class EventSignal : public Signal
{
private:
const Event& event;
public:
EventSignal(const Event& event);
const Event& GetEvent() const;
CP_SIGNAL_DECLERATION(EventSignal);
};
}
+14 -7
View File
@@ -2,10 +2,17 @@
#include "copium/util/Enum.h"
#define CP_EVENT_TYPE_ENUMS \
MouseMove, MousePress, MouseRelease, MouseScroll, \
KeyPress, KeyRelease, \
WindowResize, WindowFocus, \
ViewportResize
CP_ENUM_CREATOR(Copium, EventType, CP_EVENT_TYPE_ENUMS);
namespace Copium
{
CP_ENUM_CREATOR(EventType,
MouseMove,
MousePress,
MouseRelease,
MouseScroll,
KeyPress,
KeyRelease,
WindowResize,
WindowFocus,
ViewportResize,
Drop);
}
+260 -136
View File
@@ -1,10 +1,11 @@
#include "copium/event/Input.h"
#include "copium/util/Common.h"
#include "copium/core/Vulkan.h"
#include <glm/gtc/matrix_transform.hpp>
#include "copium/core/Vulkan.h"
#include "copium/event/InputCode.h"
#include "copium/util/Trace.h"
namespace Copium
{
bool Input::keyDownList[MAX_NUM_KEYS];
@@ -16,49 +17,49 @@ namespace Copium
bool Input::IsKeyPressed(int keyCode)
{
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode);
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode);
return keyEventList[keyCode] && keyDownList[keyCode];
}
bool Input::IsKeyReleased(int keyCode)
{
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode);
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode);
return keyEventList[keyCode] && !keyDownList[keyCode];
}
bool Input::IsKeyDown(int keyCode)
{
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode);
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode);
return keyDownList[keyCode];
}
bool Input::IsKeyUp(int keyCode)
{
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode);
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode);
return !keyDownList[keyCode];
}
bool Input::IsMousePressed(int button)
{
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button);
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button);
return mouseEventList[button] && mouseDownList[button];
}
bool Input::IsMouseReleased(int button)
{
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button);
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button);
return mouseEventList[button] && !mouseDownList[button];
}
bool Input::IsMouseDown(int button)
{
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button);
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button);
return mouseDownList[button];
}
bool Input::IsMouseUp(int button)
{
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button);
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button);
return !mouseDownList[button];
}
@@ -74,19 +75,20 @@ namespace Copium
glm::vec2 Input::GetMouseWindowPos()
{
return glm::vec2{(mousePos.x + 1.0f) * 0.5f * Vulkan::GetWindow().GetWidth(), (1.0f - mousePos.y) * 0.5f * Vulkan::GetWindow().GetHeight()};
return glm::vec2{(mousePos.x + 1.0f) * 0.5f * Vulkan::GetWindow().GetWidth(),
(1.0f - mousePos.y) * 0.5f * Vulkan::GetWindow().GetHeight()};
}
void Input::OnKey(int keyCode, bool pressed)
{
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode);
CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode);
keyDownList[keyCode] = pressed;
keyEventList[keyCode] = true;
}
void Input::OnMouse(int button, bool pressed)
{
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button);
CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button);
mouseDownList[button] = pressed;
mouseEventList[button] = true;
}
@@ -119,128 +121,250 @@ namespace Copium
{
switch (key)
{
case CP_KEY_SPACE: return "Space";
case CP_KEY_APOSTROPHE: return "\'";
case CP_KEY_COMMA: return ",";
case CP_KEY_MINUS: return "-";
case CP_KEY_PERIOD: return ".";
case CP_KEY_SLASH: return "/";
case CP_KEY_0: return "0";
case CP_KEY_1: return "1";
case CP_KEY_2: return "2";
case CP_KEY_3: return "3";
case CP_KEY_4: return "4";
case CP_KEY_5: return "5";
case CP_KEY_6: return "6";
case CP_KEY_7: return "7";
case CP_KEY_8: return "8";
case CP_KEY_9: return "9";
case CP_KEY_SEMICOLON: return ";";
case CP_KEY_EQUAL: return "=";
case CP_KEY_A: return "A";
case CP_KEY_B: return "B";
case CP_KEY_C: return "C";
case CP_KEY_D: return "D";
case CP_KEY_E: return "E";
case CP_KEY_F: return "F";
case CP_KEY_G: return "G";
case CP_KEY_H: return "H";
case CP_KEY_I: return "I";
case CP_KEY_J: return "J";
case CP_KEY_K: return "K";
case CP_KEY_L: return "L";
case CP_KEY_M: return "M";
case CP_KEY_N: return "N";
case CP_KEY_O: return "O";
case CP_KEY_P: return "P";
case CP_KEY_Q: return "Q";
case CP_KEY_R: return "R";
case CP_KEY_S: return "S";
case CP_KEY_T: return "T";
case CP_KEY_U: return "U";
case CP_KEY_V: return "V";
case CP_KEY_W: return "W";
case CP_KEY_X: return "X";
case CP_KEY_Y: return "Y";
case CP_KEY_Z: return "Z";
case CP_KEY_LEFT_BRACKET: return "[";
case CP_KEY_BACKSLASH: return "\\";
case CP_KEY_RIGHT_BRACKET: return "]";
case CP_KEY_GRAVE_ACCENT: return "Grave Accent ?";
case CP_KEY_WORLD_1: return "World 1 ?";
case CP_KEY_WORLD_2: return "World 2 ?";
case CP_KEY_ESCAPE: return "Escape";
case CP_KEY_ENTER: return "Enter";
case CP_KEY_TAB: return "Tab";
case CP_KEY_BACKSPACE: return "Return";
case CP_KEY_INSERT: return "Insert";
case CP_KEY_DELETE: return "Delete";
case CP_KEY_RIGHT: return "Right Arrow";
case CP_KEY_LEFT: return "Left Arrow";
case CP_KEY_DOWN: return "Down Arrow";
case CP_KEY_UP: return "Up Arrow";
case CP_KEY_PAGE_UP: return "Page Up";
case CP_KEY_PAGE_DOWN: return "Page Down";
case CP_KEY_HOME: return "Home";
case CP_KEY_END: return "End";
case CP_KEY_CAPS_LOCK: return "Caps Lock";
case CP_KEY_SCROLL_LOCK: return "Scroll Lock";
case CP_KEY_NUM_LOCK: return "Num Lock";
case CP_KEY_PRINT_SCREEN: return "Print Screen";
case CP_KEY_PAUSE: return "Pause";
case CP_KEY_F1: return "F1";
case CP_KEY_F2: return "F2";
case CP_KEY_F3: return "F3";
case CP_KEY_F4: return "F4";
case CP_KEY_F5: return "F5";
case CP_KEY_F6: return "F6";
case CP_KEY_F7: return "F7";
case CP_KEY_F8: return "F8";
case CP_KEY_F9: return "F9";
case CP_KEY_F10: return "F10";
case CP_KEY_F11: return "F11";
case CP_KEY_F12: return "F12";
case CP_KEY_F13: return "F13";
case CP_KEY_F14: return "F14";
case CP_KEY_F15: return "F15";
case CP_KEY_F16: return "F16";
case CP_KEY_F17: return "F17";
case CP_KEY_F18: return "F18";
case CP_KEY_F19: return "F19";
case CP_KEY_F20: return "F20";
case CP_KEY_F21: return "F21";
case CP_KEY_F22: return "F22";
case CP_KEY_F23: return "F23";
case CP_KEY_F24: return "F24";
case CP_KEY_F25: return "F25";
case CP_KEY_KP_0: return "Keypad 0";
case CP_KEY_KP_1: return "Keypad 1";
case CP_KEY_KP_2: return "Keypad 2";
case CP_KEY_KP_3: return "Keypad 3";
case CP_KEY_KP_4: return "Keypad 4";
case CP_KEY_KP_5: return "Keypad 5";
case CP_KEY_KP_6: return "Keypad 6";
case CP_KEY_KP_7: return "Keypad 7";
case CP_KEY_KP_8: return "Keypad 8";
case CP_KEY_KP_9: return "Keypad 9";
case CP_KEY_KP_DECIMAL: return "Keypad ,";
case CP_KEY_KP_DIVIDE: return "Keypad /";
case CP_KEY_KP_MULTIPLY: return "Keypad *";
case CP_KEY_KP_SUBTRACT: return "Keypad -";
case CP_KEY_KP_ADD: return "Keypad +";
case CP_KEY_KP_ENTER: return "Keypad Enter";
case CP_KEY_KP_EQUAL: return "Keypad =";
case CP_KEY_LEFT_SHIFT: return "Left Shift";
case CP_KEY_LEFT_CONTROL: return "Left Control";
case CP_KEY_LEFT_ALT: return "Left Alt";
case CP_KEY_LEFT_SUPER: return "Left Super";
case CP_KEY_RIGHT_SHIFT: return "Right Shift";
case CP_KEY_RIGHT_CONTROL: return "Right Control";
case CP_KEY_RIGHT_ALT: return "Right Alt";
case CP_KEY_RIGHT_SUPER: return "Right Super";
case CP_KEY_MENU: return "Menu";
case CP_KEY_UNBOUND: return "Unbound";
default: return "Unknown " + std::to_string(key);
case CP_KEY_SPACE:
return "Space";
case CP_KEY_APOSTROPHE:
return "\'";
case CP_KEY_COMMA:
return ",";
case CP_KEY_MINUS:
return "-";
case CP_KEY_PERIOD:
return ".";
case CP_KEY_SLASH:
return "/";
case CP_KEY_0:
return "0";
case CP_KEY_1:
return "1";
case CP_KEY_2:
return "2";
case CP_KEY_3:
return "3";
case CP_KEY_4:
return "4";
case CP_KEY_5:
return "5";
case CP_KEY_6:
return "6";
case CP_KEY_7:
return "7";
case CP_KEY_8:
return "8";
case CP_KEY_9:
return "9";
case CP_KEY_SEMICOLON:
return ";";
case CP_KEY_EQUAL:
return "=";
case CP_KEY_A:
return "A";
case CP_KEY_B:
return "B";
case CP_KEY_C:
return "C";
case CP_KEY_D:
return "D";
case CP_KEY_E:
return "E";
case CP_KEY_F:
return "F";
case CP_KEY_G:
return "G";
case CP_KEY_H:
return "H";
case CP_KEY_I:
return "I";
case CP_KEY_J:
return "J";
case CP_KEY_K:
return "K";
case CP_KEY_L:
return "L";
case CP_KEY_M:
return "M";
case CP_KEY_N:
return "N";
case CP_KEY_O:
return "O";
case CP_KEY_P:
return "P";
case CP_KEY_Q:
return "Q";
case CP_KEY_R:
return "R";
case CP_KEY_S:
return "S";
case CP_KEY_T:
return "T";
case CP_KEY_U:
return "U";
case CP_KEY_V:
return "V";
case CP_KEY_W:
return "W";
case CP_KEY_X:
return "X";
case CP_KEY_Y:
return "Y";
case CP_KEY_Z:
return "Z";
case CP_KEY_LEFT_BRACKET:
return "[";
case CP_KEY_BACKSLASH:
return "\\";
case CP_KEY_RIGHT_BRACKET:
return "]";
case CP_KEY_GRAVE_ACCENT:
return "Grave Accent ?";
case CP_KEY_WORLD_1:
return "World 1 ?";
case CP_KEY_WORLD_2:
return "World 2 ?";
case CP_KEY_ESCAPE:
return "Escape";
case CP_KEY_ENTER:
return "Enter";
case CP_KEY_TAB:
return "Tab";
case CP_KEY_BACKSPACE:
return "Return";
case CP_KEY_INSERT:
return "Insert";
case CP_KEY_DELETE:
return "Delete";
case CP_KEY_RIGHT:
return "Right Arrow";
case CP_KEY_LEFT:
return "Left Arrow";
case CP_KEY_DOWN:
return "Down Arrow";
case CP_KEY_UP:
return "Up Arrow";
case CP_KEY_PAGE_UP:
return "Page Up";
case CP_KEY_PAGE_DOWN:
return "Page Down";
case CP_KEY_HOME:
return "Home";
case CP_KEY_END:
return "End";
case CP_KEY_CAPS_LOCK:
return "Caps Lock";
case CP_KEY_SCROLL_LOCK:
return "Scroll Lock";
case CP_KEY_NUM_LOCK:
return "Num Lock";
case CP_KEY_PRINT_SCREEN:
return "Print Screen";
case CP_KEY_PAUSE:
return "Pause";
case CP_KEY_F1:
return "F1";
case CP_KEY_F2:
return "F2";
case CP_KEY_F3:
return "F3";
case CP_KEY_F4:
return "F4";
case CP_KEY_F5:
return "F5";
case CP_KEY_F6:
return "F6";
case CP_KEY_F7:
return "F7";
case CP_KEY_F8:
return "F8";
case CP_KEY_F9:
return "F9";
case CP_KEY_F10:
return "F10";
case CP_KEY_F11:
return "F11";
case CP_KEY_F12:
return "F12";
case CP_KEY_F13:
return "F13";
case CP_KEY_F14:
return "F14";
case CP_KEY_F15:
return "F15";
case CP_KEY_F16:
return "F16";
case CP_KEY_F17:
return "F17";
case CP_KEY_F18:
return "F18";
case CP_KEY_F19:
return "F19";
case CP_KEY_F20:
return "F20";
case CP_KEY_F21:
return "F21";
case CP_KEY_F22:
return "F22";
case CP_KEY_F23:
return "F23";
case CP_KEY_F24:
return "F24";
case CP_KEY_F25:
return "F25";
case CP_KEY_KP_0:
return "Keypad 0";
case CP_KEY_KP_1:
return "Keypad 1";
case CP_KEY_KP_2:
return "Keypad 2";
case CP_KEY_KP_3:
return "Keypad 3";
case CP_KEY_KP_4:
return "Keypad 4";
case CP_KEY_KP_5:
return "Keypad 5";
case CP_KEY_KP_6:
return "Keypad 6";
case CP_KEY_KP_7:
return "Keypad 7";
case CP_KEY_KP_8:
return "Keypad 8";
case CP_KEY_KP_9:
return "Keypad 9";
case CP_KEY_KP_DECIMAL:
return "Keypad ,";
case CP_KEY_KP_DIVIDE:
return "Keypad /";
case CP_KEY_KP_MULTIPLY:
return "Keypad *";
case CP_KEY_KP_SUBTRACT:
return "Keypad -";
case CP_KEY_KP_ADD:
return "Keypad +";
case CP_KEY_KP_ENTER:
return "Keypad Enter";
case CP_KEY_KP_EQUAL:
return "Keypad =";
case CP_KEY_LEFT_SHIFT:
return "Left Shift";
case CP_KEY_LEFT_CONTROL:
return "Left Control";
case CP_KEY_LEFT_ALT:
return "Left Alt";
case CP_KEY_LEFT_SUPER:
return "Left Super";
case CP_KEY_RIGHT_SHIFT:
return "Right Shift";
case CP_KEY_RIGHT_CONTROL:
return "Right Control";
case CP_KEY_RIGHT_ALT:
return "Right Alt";
case CP_KEY_RIGHT_SUPER:
return "Right Super";
case CP_KEY_MENU:
return "Menu";
case CP_KEY_UNBOUND:
return "Unbound";
default:
return "Unknown " + std::to_string(key);
}
}
}
+3 -5
View File
@@ -1,11 +1,9 @@
#pragma once
#include <vector>
#include "copium/event/InputCode.h"
#include "copium/util/BoundingBox.h"
#include <glm/glm.hpp>
#include <string>
#include "copium/util/BoundingBox.h"
namespace Copium
{
+126 -128
View File
@@ -1,131 +1,129 @@
#pragma once
#include "copium/util/Enum.h"
#define CP_MOUSE_LEFT 0
#define CP_MOUSE_RIGHT 1
#define CP_MOUSE_MIDDLE 2
#define CP_MOUSE_BACK 3
#define CP_MOUSE_FORWARD 4
#define CP_MOUSE_LEFT 0
#define CP_MOUSE_RIGHT 1
#define CP_MOUSE_MIDDLE 2
#define CP_MOUSE_BACK 3
#define CP_MOUSE_FORWARD 4
#define CP_KEY_SPACE 32
#define CP_KEY_APOSTROPHE 39
#define CP_KEY_COMMA 44
#define CP_KEY_MINUS 45
#define CP_KEY_PERIOD 46
#define CP_KEY_SLASH 47
#define CP_KEY_0 48
#define CP_KEY_1 49
#define CP_KEY_2 50
#define CP_KEY_3 51
#define CP_KEY_4 52
#define CP_KEY_5 53
#define CP_KEY_6 54
#define CP_KEY_7 55
#define CP_KEY_8 56
#define CP_KEY_9 57
#define CP_KEY_SEMICOLON 59
#define CP_KEY_EQUAL 61
#define CP_KEY_A 65
#define CP_KEY_B 66
#define CP_KEY_C 67
#define CP_KEY_D 68
#define CP_KEY_E 69
#define CP_KEY_F 70
#define CP_KEY_G 71
#define CP_KEY_H 72
#define CP_KEY_I 73
#define CP_KEY_J 74
#define CP_KEY_K 75
#define CP_KEY_L 76
#define CP_KEY_M 77
#define CP_KEY_N 78
#define CP_KEY_O 79
#define CP_KEY_P 80
#define CP_KEY_Q 81
#define CP_KEY_R 82
#define CP_KEY_S 83
#define CP_KEY_T 84
#define CP_KEY_U 85
#define CP_KEY_V 86
#define CP_KEY_W 87
#define CP_KEY_X 88
#define CP_KEY_Y 89
#define CP_KEY_Z 90
#define CP_KEY_LEFT_BRACKET 91
#define CP_KEY_BACKSLASH 92
#define CP_KEY_RIGHT_BRACKET 93
#define CP_KEY_GRAVE_ACCENT 96
#define CP_KEY_WORLD_1 161
#define CP_KEY_WORLD_2 162
#define CP_KEY_ESCAPE 256
#define CP_KEY_ENTER 257
#define CP_KEY_TAB 258
#define CP_KEY_BACKSPACE 259
#define CP_KEY_INSERT 260
#define CP_KEY_DELETE 261
#define CP_KEY_RIGHT 262
#define CP_KEY_LEFT 263
#define CP_KEY_DOWN 264
#define CP_KEY_UP 265
#define CP_KEY_PAGE_UP 266
#define CP_KEY_PAGE_DOWN 267
#define CP_KEY_HOME 268
#define CP_KEY_END 269
#define CP_KEY_CAPS_LOCK 280
#define CP_KEY_SCROLL_LOCK 281
#define CP_KEY_NUM_LOCK 282
#define CP_KEY_PRINT_SCREEN 283
#define CP_KEY_PAUSE 284
#define CP_KEY_F1 290
#define CP_KEY_F2 291
#define CP_KEY_F3 292
#define CP_KEY_F4 293
#define CP_KEY_F5 294
#define CP_KEY_F6 295
#define CP_KEY_F7 296
#define CP_KEY_F8 297
#define CP_KEY_F9 298
#define CP_KEY_F10 299
#define CP_KEY_F11 300
#define CP_KEY_F12 301
#define CP_KEY_F13 302
#define CP_KEY_F14 303
#define CP_KEY_F15 304
#define CP_KEY_F16 305
#define CP_KEY_F17 306
#define CP_KEY_F18 307
#define CP_KEY_F19 308
#define CP_KEY_F20 309
#define CP_KEY_F21 310
#define CP_KEY_F22 311
#define CP_KEY_F23 312
#define CP_KEY_F24 313
#define CP_KEY_F25 314
#define CP_KEY_KP_0 320
#define CP_KEY_KP_1 321
#define CP_KEY_KP_2 322
#define CP_KEY_KP_3 323
#define CP_KEY_KP_4 324
#define CP_KEY_KP_5 325
#define CP_KEY_KP_6 326
#define CP_KEY_KP_7 327
#define CP_KEY_KP_8 328
#define CP_KEY_KP_9 329
#define CP_KEY_KP_DECIMAL 330
#define CP_KEY_KP_DIVIDE 331
#define CP_KEY_KP_MULTIPLY 332
#define CP_KEY_KP_SUBTRACT 333
#define CP_KEY_KP_ADD 334
#define CP_KEY_KP_ENTER 335
#define CP_KEY_KP_EQUAL 336
#define CP_KEY_LEFT_SHIFT 340
#define CP_KEY_LEFT_CONTROL 341
#define CP_KEY_LEFT_ALT 342
#define CP_KEY_LEFT_SUPER 343
#define CP_KEY_RIGHT_SHIFT 344
#define CP_KEY_RIGHT_CONTROL 345
#define CP_KEY_RIGHT_ALT 346
#define CP_KEY_RIGHT_SUPER 347
#define CP_KEY_MENU 348
#define CP_KEY_UNBOUND 0xffffffff
#define CP_KEY_SPACE 32
#define CP_KEY_APOSTROPHE 39
#define CP_KEY_COMMA 44
#define CP_KEY_MINUS 45
#define CP_KEY_PERIOD 46
#define CP_KEY_SLASH 47
#define CP_KEY_0 48
#define CP_KEY_1 49
#define CP_KEY_2 50
#define CP_KEY_3 51
#define CP_KEY_4 52
#define CP_KEY_5 53
#define CP_KEY_6 54
#define CP_KEY_7 55
#define CP_KEY_8 56
#define CP_KEY_9 57
#define CP_KEY_SEMICOLON 59
#define CP_KEY_EQUAL 61
#define CP_KEY_A 65
#define CP_KEY_B 66
#define CP_KEY_C 67
#define CP_KEY_D 68
#define CP_KEY_E 69
#define CP_KEY_F 70
#define CP_KEY_G 71
#define CP_KEY_H 72
#define CP_KEY_I 73
#define CP_KEY_J 74
#define CP_KEY_K 75
#define CP_KEY_L 76
#define CP_KEY_M 77
#define CP_KEY_N 78
#define CP_KEY_O 79
#define CP_KEY_P 80
#define CP_KEY_Q 81
#define CP_KEY_R 82
#define CP_KEY_S 83
#define CP_KEY_T 84
#define CP_KEY_U 85
#define CP_KEY_V 86
#define CP_KEY_W 87
#define CP_KEY_X 88
#define CP_KEY_Y 89
#define CP_KEY_Z 90
#define CP_KEY_LEFT_BRACKET 91
#define CP_KEY_BACKSLASH 92
#define CP_KEY_RIGHT_BRACKET 93
#define CP_KEY_GRAVE_ACCENT 96
#define CP_KEY_WORLD_1 161
#define CP_KEY_WORLD_2 162
#define CP_KEY_ESCAPE 256
#define CP_KEY_ENTER 257
#define CP_KEY_TAB 258
#define CP_KEY_BACKSPACE 259
#define CP_KEY_INSERT 260
#define CP_KEY_DELETE 261
#define CP_KEY_RIGHT 262
#define CP_KEY_LEFT 263
#define CP_KEY_DOWN 264
#define CP_KEY_UP 265
#define CP_KEY_PAGE_UP 266
#define CP_KEY_PAGE_DOWN 267
#define CP_KEY_HOME 268
#define CP_KEY_END 269
#define CP_KEY_CAPS_LOCK 280
#define CP_KEY_SCROLL_LOCK 281
#define CP_KEY_NUM_LOCK 282
#define CP_KEY_PRINT_SCREEN 283
#define CP_KEY_PAUSE 284
#define CP_KEY_F1 290
#define CP_KEY_F2 291
#define CP_KEY_F3 292
#define CP_KEY_F4 293
#define CP_KEY_F5 294
#define CP_KEY_F6 295
#define CP_KEY_F7 296
#define CP_KEY_F8 297
#define CP_KEY_F9 298
#define CP_KEY_F10 299
#define CP_KEY_F11 300
#define CP_KEY_F12 301
#define CP_KEY_F13 302
#define CP_KEY_F14 303
#define CP_KEY_F15 304
#define CP_KEY_F16 305
#define CP_KEY_F17 306
#define CP_KEY_F18 307
#define CP_KEY_F19 308
#define CP_KEY_F20 309
#define CP_KEY_F21 310
#define CP_KEY_F22 311
#define CP_KEY_F23 312
#define CP_KEY_F24 313
#define CP_KEY_F25 314
#define CP_KEY_KP_0 320
#define CP_KEY_KP_1 321
#define CP_KEY_KP_2 322
#define CP_KEY_KP_3 323
#define CP_KEY_KP_4 324
#define CP_KEY_KP_5 325
#define CP_KEY_KP_6 326
#define CP_KEY_KP_7 327
#define CP_KEY_KP_8 328
#define CP_KEY_KP_9 329
#define CP_KEY_KP_DECIMAL 330
#define CP_KEY_KP_DIVIDE 331
#define CP_KEY_KP_MULTIPLY 332
#define CP_KEY_KP_SUBTRACT 333
#define CP_KEY_KP_ADD 334
#define CP_KEY_KP_ENTER 335
#define CP_KEY_KP_EQUAL 336
#define CP_KEY_LEFT_SHIFT 340
#define CP_KEY_LEFT_CONTROL 341
#define CP_KEY_LEFT_ALT 342
#define CP_KEY_LEFT_SUPER 343
#define CP_KEY_RIGHT_SHIFT 344
#define CP_KEY_RIGHT_CONTROL 345
#define CP_KEY_RIGHT_ALT 346
#define CP_KEY_RIGHT_SUPER 347
#define CP_KEY_MENU 348
#define CP_KEY_UNBOUND 0x0fffffff
@@ -3,8 +3,10 @@
namespace Copium
{
KeyPressEvent::KeyPressEvent(int button)
: Event{EventType::KeyPress}, button{button}
{}
: Event{EventType::KeyPress},
button{button}
{
}
int KeyPressEvent::GetButton() const
{
@@ -8,6 +8,7 @@ namespace Copium
{
private:
int button;
public:
KeyPressEvent(int button);
@@ -3,8 +3,10 @@
namespace Copium
{
KeyReleaseEvent::KeyReleaseEvent(int button)
: Event{EventType::KeyRelease}, button{button}
{}
: Event{EventType::KeyRelease},
button{button}
{
}
int KeyReleaseEvent::GetButton() const
{
@@ -8,6 +8,7 @@ namespace Copium
{
private:
int button;
public:
KeyReleaseEvent(int button);
@@ -3,8 +3,11 @@
namespace Copium
{
MouseMoveEvent::MouseMoveEvent(glm::vec2 pos, glm::vec2 delta)
: Event{EventType::MouseMove}, pos{pos}, delta{delta}
{}
: Event{EventType::MouseMove},
pos{pos},
delta{delta}
{
}
glm::vec2 MouseMoveEvent::GetPos() const
{
@@ -1,9 +1,9 @@
#pragma once
#include "copium/event/Event.h"
#include <glm/glm.hpp>
#include "copium/event/Event.h"
namespace Copium
{
class MouseMoveEvent : public Event
@@ -11,6 +11,7 @@ namespace Copium
private:
glm::vec2 pos;
glm::vec2 delta;
public:
MouseMoveEvent(glm::vec2 pos, glm::vec2 delta);
@@ -3,10 +3,12 @@
namespace Copium
{
MousePressEvent::MousePressEvent(int button)
: Event{EventType::MousePress}, button{button}
{}
: Event{EventType::MousePress},
button{button}
{
}
int MousePressEvent::GetButton() const
int MousePressEvent::GetButton() const
{
return button;
}
@@ -1,15 +1,16 @@
#pragma once
#include "copium/event/Event.h"
#include <glm/glm.hpp>
#include "copium/event/Event.h"
namespace Copium
{
class MousePressEvent : public Event
{
private:
int button;
public:
MousePressEvent(int button);
@@ -3,10 +3,12 @@
namespace Copium
{
MouseReleaseEvent::MouseReleaseEvent(int button)
: Event{EventType::MouseRelease}, button{button}
{}
: Event{EventType::MouseRelease},
button{button}
{
}
int MouseReleaseEvent::GetButton() const
int MouseReleaseEvent::GetButton() const
{
return button;
}
@@ -1,15 +1,16 @@
#pragma once
#include "copium/event/Event.h"
#include <glm/glm.hpp>
#include "copium/event/Event.h"
namespace Copium
{
class MouseReleaseEvent : public Event
{
private:
int button;
public:
MouseReleaseEvent(int button);
@@ -3,8 +3,11 @@
namespace Copium
{
MouseScrollEvent::MouseScrollEvent(int scrollX, int scrollY)
: Event{EventType::MouseScroll}, scrollX{scrollX}, scrollY{scrollY}
{}
: Event{EventType::MouseScroll},
scrollX{scrollX},
scrollY{scrollY}
{
}
int MouseScrollEvent::GetScrollX() const
{
@@ -9,6 +9,7 @@ namespace Copium
private:
int scrollX;
int scrollY;
public:
MouseScrollEvent(int scrollX, int scrollY);
@@ -3,8 +3,10 @@
namespace Copium
{
ViewportResize::ViewportResize(const BoundingBox& viewport)
: Event(EventType::ViewportResize), viewport{viewport}
{}
: Event(EventType::ViewportResize),
viewport{viewport}
{
}
const BoundingBox& ViewportResize::GetViewport() const
{
@@ -1,7 +1,6 @@
#pragma once
#include "copium/event/Event.h"
#include "copium/event/EventType.h"
#include "copium/util/BoundingBox.h"
namespace Copium
@@ -9,6 +8,7 @@ namespace Copium
class ViewportResize : public Event
{
BoundingBox viewport;
public:
ViewportResize(const BoundingBox& viewport);
@@ -3,8 +3,10 @@
namespace Copium
{
WindowFocusEvent::WindowFocusEvent(bool focused)
: Event{EventType::WindowFocus}, focused{focused}
{}
: Event{EventType::WindowFocus},
focused{focused}
{
}
bool WindowFocusEvent::IsFocused() const
{
@@ -8,6 +8,7 @@ namespace Copium
{
private:
bool focused;
public:
WindowFocusEvent(bool focused);
@@ -3,8 +3,11 @@
namespace Copium
{
WindowResizeEvent::WindowResizeEvent(int width, int height)
: Event{EventType::WindowResize}, width{width}, height{height}
{}
: Event{EventType::WindowResize},
width{width},
height{height}
{
}
int WindowResizeEvent::GetWidth() const
{
@@ -9,6 +9,7 @@ namespace Copium
private:
int width;
int height;
public:
WindowResizeEvent(int width, int height);
+7 -5
View File
@@ -1,21 +1,23 @@
#pragma once
#include <memory>
#include <vector>
#include "copium/buffer/CommandBuffer.h"
#include "copium/buffer/IndexBuffer.h"
#include "copium/buffer/VertexBuffer.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/util/Common.h"
#include <vector>
#include <memory>
namespace Copium
{
class Mesh
{
CP_DELETE_COPY_AND_MOVE_CTOR(Mesh);
private:
std::unique_ptr<IndexBuffer> indexBuffer;
std::unique_ptr<VertexBuffer> vertexBuffer;
public:
template <typename T>
Mesh(const std::vector<T>& vertices, const std::vector<uint16_t>& indices);
@@ -30,6 +32,6 @@ namespace Copium
indexBuffer = std::make_unique<IndexBuffer>(indices.size());
indexBuffer->UpdateStaging((void*)indices.data());
vertexBuffer = std::make_unique<VertexBuffer>(T::GetDescriptor(), vertices.size());
vertexBuffer ->UpdateStaging(0, (void*)vertices.data());
vertexBuffer->UpdateStaging(0, (void*)vertices.data());
}
}
+3 -4
View File
@@ -1,12 +1,12 @@
#pragma once
#include "copium/pipeline/VertexDescriptor.h"
#include <glm/glm.hpp>
#include "copium/pipeline/VertexDescriptor.h"
namespace Copium
{
struct Vertex
struct Vertex
{
glm::vec3 pos;
glm::vec3 color;
@@ -15,4 +15,3 @@ namespace Copium
static VertexDescriptor GetDescriptor();
};
}
@@ -5,7 +5,8 @@ namespace Copium
VertexDescriptor VertexPassthrough::GetDescriptor()
{
VertexDescriptor descriptor{};
descriptor.AddAttribute(0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(VertexPassthrough, texCoord), sizeof(VertexPassthrough));
descriptor.AddAttribute(
0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(VertexPassthrough, texCoord), sizeof(VertexPassthrough));
return descriptor;
}
}
@@ -1,12 +1,12 @@
#pragma once
#include "copium/pipeline/VertexDescriptor.h"
#include <glm/glm.hpp>
#include "copium/pipeline/VertexDescriptor.h"
namespace Copium
{
struct VertexPassthrough
struct VertexPassthrough
{
glm::vec2 texCoord;
@@ -1,55 +1,77 @@
#include "copium/pipeline/DescriptorPool.h"
#include "copium/core/SwapChain.h"
#include "copium/core/Vulkan.h"
namespace Copium
{
DescriptorPool::DescriptorPool()
DescriptorPool::DescriptorPool(int uniformDescriptorSets, int imageDescriptorSets)
{
std::vector<VkDescriptorPoolSize> poolSizes{2};
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
poolSizes[0].descriptorCount = DESCRIPTOR_SET_COUNT * SwapChain::MAX_FRAMES_IN_FLIGHT; // TODO: how should this actually be determined?
std::vector<VkDescriptorPoolSize> poolSizes;
if (uniformDescriptorSets != 0)
{
VkDescriptorPoolSize descriptorSetPoolSize;
descriptorSetPoolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorSetPoolSize.descriptorCount = uniformDescriptorSets;
poolSizes.emplace_back(descriptorSetPoolSize);
}
poolSizes[1].type = VK_DESCRIPTOR_TYPE_SAMPLER;
poolSizes[1].descriptorCount = DESCRIPTOR_SET_COUNT * SwapChain::MAX_FRAMES_IN_FLIGHT; // TODO: how should this actually be determined?
if (imageDescriptorSets != 0)
{
VkDescriptorPoolSize descriptorSetPoolSize;
descriptorSetPoolSize.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorSetPoolSize.descriptorCount = imageDescriptorSets;
poolSizes.emplace_back(descriptorSetPoolSize);
}
VkDescriptorPoolCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
createInfo.poolSizeCount = poolSizes.size();
createInfo.pPoolSizes = poolSizes.data();
createInfo.maxSets = DESCRIPTOR_SET_COUNT * SwapChain::MAX_FRAMES_IN_FLIGHT;
createInfo.maxSets = uniformDescriptorSets + imageDescriptorSets; // I have no actual idea if this is fine
createInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
CP_VK_ASSERT(vkCreateDescriptorPool(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorPool), "Failed to initialize descriptor pool");
CP_VK_ASSERT(vkCreateDescriptorPool(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorPool),
"Failed to initialize descriptor pool");
}
DescriptorPool::~DescriptorPool()
{
vkDestroyDescriptorPool(Vulkan::GetDevice(), descriptorPool, nullptr);
VkDescriptorPool descriptorPoolCpy = descriptorPool;
Vulkan::GetDevice().QueueIdleCommand([descriptorPoolCpy]()
{ vkDestroyDescriptorPool(Vulkan::GetDevice(), descriptorPoolCpy, nullptr); });
}
std::vector<VkDescriptorSet> DescriptorPool::AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout)
{
std::vector<VkDescriptorSet> descriptorSets{SwapChain::MAX_FRAMES_IN_FLIGHT};
std::vector<VkDescriptorSetLayout> layouts{SwapChain::MAX_FRAMES_IN_FLIGHT, descriptorSetLayout};
std::vector<VkDescriptorSet> descriptorSets{(size_t)SwapChain::MAX_FRAMES_IN_FLIGHT};
std::vector<VkDescriptorSetLayout> layouts{(size_t)SwapChain::MAX_FRAMES_IN_FLIGHT, descriptorSetLayout};
VkDescriptorSetAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocateInfo.descriptorPool = descriptorPool;
allocateInfo.descriptorSetCount = descriptorSets.size();
allocateInfo.pSetLayouts = layouts.data();
CP_VK_ASSERT(vkAllocateDescriptorSets(Vulkan::GetDevice(), &allocateInfo, descriptorSets.data()), "Failed to allocate descriptor sets");
CP_VK_ASSERT(vkAllocateDescriptorSets(Vulkan::GetDevice(), &allocateInfo, descriptorSets.data()),
"Failed to allocate descriptor sets");
return descriptorSets;
}
void DescriptorPool::FreeDescriptorSets(const std::vector<VkDescriptorSet>& descriptorSets)
{
vkFreeDescriptorSets(Vulkan::GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data());
VkDescriptorPool descriptorPoolCpy = descriptorPool;
std::vector<VkDescriptorSet> descriptorSetsCpy = descriptorSets;
Vulkan::GetDevice().QueueIdleCommand(
[descriptorPoolCpy, descriptorSetsCpy]()
{
vkFreeDescriptorSets(
Vulkan::GetDevice(), descriptorPoolCpy, descriptorSetsCpy.size(), descriptorSetsCpy.data());
});
}
DescriptorPool::operator VkDescriptorPool() const
{
return descriptorPool;
}
}
}
@@ -1,19 +1,20 @@
#pragma once
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
#include "copium/util/Common.h"
namespace Copium
{
class DescriptorPool final
{
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool);
private:
VkDescriptorPool descriptorPool;
static const int DESCRIPTOR_SET_COUNT = 1000000;
public:
DescriptorPool();
DescriptorPool(int uniformDescriptorSets, int imageDescriptorSets);
~DescriptorPool();
std::vector<VkDescriptorSet> AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout);
@@ -4,8 +4,12 @@
namespace Copium
{
DescriptorSet::DescriptorSet(DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings)
: descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}, bindings{bindings}
DescriptorSet::DescriptorSet(DescriptorPool& descriptorPool,
VkDescriptorSetLayout descriptorSetLayout,
const std::set<ShaderBinding>& bindings)
: descriptorPool{descriptorPool},
descriptorSetLayout{descriptorSetLayout},
bindings{bindings}
{
CP_ASSERT(!bindings.empty(), "Cannot initialize DescriptorSet with empty ShaderBindings");
@@ -64,7 +68,8 @@ namespace Copium
void DescriptorSet::SetSamplers(const std::vector<const Sampler*>& samplers, uint32_t binding)
{
for (size_t i = 0; i < descriptorSets.size(); ++i) {
for (size_t i = 0; i < descriptorSets.size(); ++i)
{
std::vector<VkWriteDescriptorSet> descriptorWrites{samplers.size()};
for (size_t j = 0; j < samplers.size(); j++)
{
@@ -105,7 +110,7 @@ namespace Copium
UniformBuffer& DescriptorSet::GetUniformBuffer(const std::string& uniformBuffer)
{
auto it = uniformBuffers.find(uniformBuffer);
CP_ASSERT(it != uniformBuffers.end(), "UniformBuffer not found = %s", uniformBuffer.c_str());
CP_ASSERT(it != uniformBuffers.end(), "UniformBuffer not found = {}", uniformBuffer);
return *it->second;
}
@@ -114,6 +119,11 @@ namespace Copium
return bindings.begin()->set;
}
VkDescriptorSet DescriptorSet::GetVkDescriptorSet(int flightIndex) const
{
return descriptorSets[flightIndex];
}
DescriptorSet::operator VkDescriptorSet() const
{
return descriptorSets[Vulkan::GetSwapChain().GetFlightIndex()];
@@ -121,7 +131,8 @@ namespace Copium
void DescriptorSet::SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding)
{
for (size_t i = 0; i < descriptorSets.size(); ++i) {
for (size_t i = 0; i < descriptorSets.size(); ++i)
{
VkDescriptorBufferInfo bufferInfo = uniformBuffer.GetDescriptorBufferInfo(i);
VkWriteDescriptorSet descriptorWrite{};
@@ -138,4 +149,4 @@ namespace Copium
}
}
}
}
@@ -1,24 +1,24 @@
#pragma once
#include <glm/glm.hpp>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <vulkan/vulkan.hpp>
#include "copium/buffer/UniformBuffer.h"
#include "copium/pipeline/DescriptorPool.h"
#include "copium/pipeline/ShaderBinding.h"
#include "copium/pipeline/ShaderReflector.h"
#include "copium/sampler/Sampler.h"
#include "copium/util/Common.h"
#include <glm/glm.hpp>
#include <vulkan/vulkan.hpp>
#include <map>
#include <set>
#include <vector>
namespace Copium
{
class DescriptorSet final
{
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet);
private:
DescriptorPool& descriptorPool;
VkDescriptorSetLayout descriptorSetLayout;
@@ -28,7 +28,9 @@ namespace Copium
std::map<std::string, std::unique_ptr<UniformBuffer>> uniformBuffers;
public:
DescriptorSet(DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings);
DescriptorSet(DescriptorPool& descriptorPool,
VkDescriptorSetLayout descriptorSetLayout,
const std::set<ShaderBinding>& bindings);
~DescriptorSet();
void SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex = 0);
@@ -37,9 +39,11 @@ namespace Copium
void SetSamplersDynamic(const std::vector<const Sampler*>& samplers, uint32_t binding);
UniformBuffer& GetUniformBuffer(const std::string& uniformBuffer);
uint32_t GetSetIndex() const;
VkDescriptorSet GetVkDescriptorSet(int flightIndex) const;
operator VkDescriptorSet() const;
private:
void SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding);
};
}
}

Some files were not shown because too many files have changed in this diff Show More