Skip to content

Commit

Permalink
generate frame graph in Graphviz
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Jun 6, 2020
1 parent 8c371a7 commit 99e2937
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 149 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(UECS VERSION 0.9.4)
project(UECS VERSION 0.9.5)
message(STATUS "[Project] ${PROJECT_NAME}")

include(FetchContent)
Expand All @@ -24,6 +24,7 @@ Ubpa_InitProject()

Ubpa_AddDep(UContainer 0.0.4)
Ubpa_AddDep(UTemplate 0.4.4)
Ubpa_AddDep(UGraphviz 0.1.0)

Ubpa_AddSubDirsRec(include)
Ubpa_AddSubDirsRec(src)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int main() {
- [parrallel with `None` filter](src/test/06_none_parallel/main.cpp)
- [system **overload**](src/test/07_overload/main.cpp)
- [runtime dynamic component and system](src/test/11_runtime_cmpt/main.cpp)
- [generate **frame graph** in **Graphviz**](src/test/12_framegraph/main.cpp)
## Licensing
Expand Down
8 changes: 3 additions & 5 deletions doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,19 @@

### not sure

- [ ] `Any` influence `EntityLocator`
- [ ] ChunkJob
- [ ] serialize

## tool

- [ ] SysFuncGraph dump
- [ ] serialize
- [ ] doc
- [x] SysFuncGraph dump
- [x] doc

## detial

- [x] remove `EntityMngr::ai2ei`
- [x] Archetype share `Pool<Chunk>`
- [x] simplify `Schedule`
- [ ] parallel `Schedule`
- [x] `constexpr SystemFunc::HashCode()`
- [x] cache `CmptTypeSet`'s hashcode
- [x] store `EntityMngr` and `SystemMngr` instead `World` in `Schedule`
Expand Down
45 changes: 30 additions & 15 deletions include/UECS/RTDCmptTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,44 @@ namespace Ubpa {
// - copy constructor: memcpy as default
// - move constructor: memcpy as default
// - destructor: do nothing as default
// - name
class RTDCmptTraits {
public:
static constexpr size_t default_alignment = alignof(std::max_align_t);

inline static RTDCmptTraits& Instance() noexcept;
static RTDCmptTraits& Instance() noexcept;

// neccessary
inline RTDCmptTraits& RegisterSize(CmptType type, size_t size);
RTDCmptTraits& RegisterSize(CmptType type, size_t size);

// optional
inline RTDCmptTraits& RegisterAlignment(CmptType type, size_t alignment);
RTDCmptTraits& RegisterAlignment(CmptType type, size_t alignment);

// optional
inline RTDCmptTraits& RegisterDefaultConstructor(CmptType type, std::function<void(void*)> f);
RTDCmptTraits& RegisterDefaultConstructor(CmptType type, std::function<void(void*)> f);

// optional
inline RTDCmptTraits& RegisterCopyConstructor(CmptType type, std::function<void(void*, void*)> f);
RTDCmptTraits& RegisterCopyConstructor(CmptType type, std::function<void(void*, void*)> f);

// optional
inline RTDCmptTraits& RegisterMoveConstructor(CmptType type, std::function<void(void*, void*)> f);
RTDCmptTraits& RegisterMoveConstructor(CmptType type, std::function<void(void*, void*)> f);

// optional
inline RTDCmptTraits& RegisterDestructor(CmptType type, std::function<void(void*)> f);
RTDCmptTraits& RegisterDestructor(CmptType type, std::function<void(void*)> f);

inline RTDCmptTraits& Deregister(CmptType type) noexcept;
// optional
RTDCmptTraits& RegisterName(CmptType type, std::string name);

// register all for Cmpt
// static_assert
// - is_default_constructible_v
// - is_copy_constructible_v
// - is_move_constructible_v
// - is_destructible_v
template<typename Cmpt>
size_t Sizeof(CmptType type) const;
size_t Alignof(CmptType type) const;
void CopyConstruct(CmptType type, void* dst, void* src) const;
void MoveConstruct(CmptType type, void* dst, void* src) const;
void Destruct(CmptType type, void* cmpt) const;
std::string_view Nameof(CmptType type) const;

RTDCmptTraits& Deregister(CmptType type) noexcept;

template<typename... Cmpts>
void Register();

template<typename Cmpt>
Expand All @@ -59,12 +64,22 @@ namespace Ubpa {

RTDCmptTraits() = default;

// register all for Cmpt
// static_assert
// - is_default_constructible_v
// - is_copy_constructible_v
// - is_move_constructible_v
// - is_destructible_v
template<typename Cmpt>
void RegisterOne();

std::unordered_map<CmptType, size_t> sizeofs;
std::unordered_map<CmptType, size_t> alignments;
std::unordered_map<CmptType, std::function<void(void*)>> default_constructors; // dst <- src
std::unordered_map<CmptType, std::function<void(void*, void*)>> copy_constructors; // dst <- src
std::unordered_map<CmptType, std::function<void(void*, void*)>> move_constructors; // dst <- src
std::unordered_map<CmptType, std::function<void(void*)>> destructors;
std::unordered_map<CmptType, std::string> names;
};
}

Expand Down
15 changes: 14 additions & 1 deletion include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include <map>

namespace Ubpa::detail::Schedule_ {
struct Compiler;
}

namespace Ubpa {
class EntityMngr;
class SystemMngr;
Expand Down Expand Up @@ -54,10 +58,19 @@ namespace Ubpa {

private:
template<typename... Args>
Schedule& Request(Args&&... args);
void Request(Args&&... args);

Schedule(EntityMngr* entityMngr, SystemMngr* systemMngr);
void Clear();

struct CmptSysFuncs {
std::vector<SystemFunc*> lastFrameSysFuncs;
std::vector<SystemFunc*> writeSysFuncs;
std::vector<SystemFunc*> latestSysFuncs;
};
friend struct detail::Schedule_::Compiler;
std::unordered_map<CmptType, CmptSysFuncs> cmptSysFuncsMap;

SysFuncGraph GenSysFuncGraph() const;

// SystemFunc's hashcode to pointer of SystemFunc
Expand Down
6 changes: 6 additions & 0 deletions include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "SystemMngr.h"
#include "EntityMngr.h"

#include <UGraphviz/UGraphviz.h>

namespace Ubpa {
// SystemMngr + EntityMngr
class World {
Expand All @@ -23,6 +25,10 @@ namespace Ubpa {
// you can use graphviz to vistualize the graph
std::string DumpUpdateJobGraph() const;

// after running Update
// use RTDCmptTraits' registered component name
Graphviz::Graph GenUpdateFrameGraph() const;

private:
mutable JobExecutor executor;
Schedule schedule;
Expand Down
62 changes: 55 additions & 7 deletions include/UECS/detail/RTDCmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,90 @@ namespace Ubpa {
return instance;
}

// neccessary
inline RTDCmptTraits& RTDCmptTraits::RegisterSize(CmptType type, size_t size) {
sizeofs[type] = size;
return *this;
}

// optional
inline RTDCmptTraits& RTDCmptTraits::RegisterAlignment(CmptType type, size_t alignment) {
alignments[type] = alignment;
return *this;
}

// optional
inline RTDCmptTraits& RTDCmptTraits::RegisterDefaultConstructor(CmptType type, std::function<void(void*)> f) {
default_constructors[type] = std::move(f);
return *this;
}

// optional
inline RTDCmptTraits& RTDCmptTraits::RegisterCopyConstructor(CmptType type, std::function<void(void*, void*)> f) {
copy_constructors[type] = std::move(f);
return *this;
}

// optional
inline RTDCmptTraits& RTDCmptTraits::RegisterMoveConstructor(CmptType type, std::function<void(void*, void*)> f) {
move_constructors[type] = std::move(f);
return *this;
}

// optional
inline RTDCmptTraits& RTDCmptTraits::RegisterDestructor(CmptType type, std::function<void(void*)> f) {
destructors[type] = std::move(f);
return *this;
}

template<typename Cmpt>
inline RTDCmptTraits& RTDCmptTraits::RegisterName(CmptType type, std::string name) {
names[type] = std::move(name);
return *this;
}

inline size_t RTDCmptTraits::Sizeof(CmptType type) const {
assert(sizeofs.find(type) != sizeofs.end());
return sizeofs.find(type)->second;
}

inline size_t RTDCmptTraits::Alignof(CmptType type) const {
assert(alignments.find(type) != alignments.end());
return alignments.find(type)->second;
}

inline void RTDCmptTraits::CopyConstruct(CmptType type, void* dst, void* src) const {
auto target = copy_constructors.find(type);

if (target != copy_constructors.end())
target->second(dst, src);
else
memcpy(dst, src, Sizeof(type));
}

inline void RTDCmptTraits::MoveConstruct(CmptType type, void* dst, void* src) const {
auto target = move_constructors.find(type);

if (target != move_constructors.end())
target->second(dst, src);
else
memcpy(dst, src, Sizeof(type));
}

inline void RTDCmptTraits::Destruct(CmptType type, void* cmpt) const {
auto target = destructors.find(type);
if (target != destructors.end())
target->second(cmpt);
}

inline std::string_view RTDCmptTraits::Nameof(CmptType type) const {
auto target = names.find(type);
if (target != names.end())
return target->second;
else
return {};
}

template<typename... Cmpts>
void RTDCmptTraits::Register() {
(RegisterOne<Cmpts>(), ...);
}

template<typename Cmpt>
void RTDCmptTraits::RegisterOne() {
static_assert(std::is_default_constructible_v<Cmpt>, "<Cmpt> must be default-constructible");
static_assert(std::is_copy_constructible_v<Cmpt>, "<Cmpt> must be copy-constructible");
static_assert(std::is_move_constructible_v<Cmpt>, "<Cmpt> must be move-constructible");
Expand All @@ -55,6 +101,7 @@ namespace Ubpa {

sizeofs[type] = sizeof(Cmpt);
alignments[type] = alignof(Cmpt);
names[type] = std::string{ nameof::nameof_type<Cmpt>() };

if constexpr (!std::is_trivially_default_constructible_v<Cmpt>) {
default_constructors[type] = [](void* cmpt) {
Expand Down Expand Up @@ -84,6 +131,7 @@ namespace Ubpa {

sizeofs.erase(type);
alignments.erase(type);
names.erase(type);

if constexpr (!std::is_trivially_constructible_v<Cmpt>)
default_constructors.erase(type);
Expand Down
16 changes: 12 additions & 4 deletions include/UECS/detail/Schedule.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
namespace Ubpa {
template<typename Func>
Schedule& Schedule::Register(Func&& func, std::string name, EntityFilter filter) {
return Request(std::forward<Func>(func), std::move(name), std::move(filter));
Request(std::forward<Func>(func), std::move(name), std::move(filter));
return *this;
}

template<typename Func>
Schedule& Schedule::Register(Func&& func, std::string name, EntityLocator locator, EntityFilter filter) {
return Request(std::forward<Func>(func), std::move(name), std::move(locator), std::move(filter));
Request(std::forward<Func>(func), std::move(name), std::move(locator), std::move(filter));
return *this;
}

template<typename... Args>
Schedule& Schedule::Request(Args&&... args) {
void Schedule::Request(Args&&... args) {
SystemFunc* sysFunc = sysFuncPool.Request(std::forward<Args>(args)...);
sysFuncs.emplace(sysFunc->HashCode(), sysFunc);
return *this;
const auto& locator = sysFunc->query.locator;
for (const auto& type : locator.LastFrameCmptTypes())
cmptSysFuncsMap[type].lastFrameSysFuncs.push_back(sysFunc);
for (const auto& type : locator.WriteCmptTypes())
cmptSysFuncsMap[type].writeSysFuncs.push_back(sysFunc);
for (const auto& type : locator.LatestCmptTypes())
cmptSysFuncsMap[type].latestSysFuncs.push_back(sysFunc);
}

inline Schedule::Schedule(EntityMngr* entityMngr, SystemMngr* systemMngr)
Expand Down
Loading

0 comments on commit 99e2937

Please sign in to comment.