Skip to content

Commit

Permalink
RandomAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Nov 11, 2020
1 parent 1d67e74 commit a90698c
Show file tree
Hide file tree
Showing 17 changed files with 442 additions and 157 deletions.
2 changes: 1 addition & 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.14.1)
project(UECS VERSION 0.14.2)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
8 changes: 8 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Change Log

- 0.14.2
- add `RandomAccessor` for random access other entity's component
- `SystemFunc` add the member
- `Schedule::Register*` add the parameter
- feature `None Parallel` is updated to support `RandomAccessor`
- `World::GenUpdateFrameGraph` is updated to support `RandomAccessor`
- `CmptAccessType`'s default `AccessMode` change from `LATEST` to `WRITE`
- `World` command buffer layer's type change from `size_t` to `int`
- 0.14.1: `CmptAccessMode` remove singleton
- 0.14.0: System Lifecycle
2 changes: 1 addition & 1 deletion doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
- [x] system base -> `System`
- [x] singleton
- [ ] doxygen
- [ ] lock / random access
- [x] random access
- [x] system traits

### maybe support in future
Expand Down
Empty file added include/UECS/.txt
Empty file.
7 changes: 3 additions & 4 deletions include/UECS/CmptType.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ namespace Ubpa::UECS {
// CmptType with AccessMode
class CmptAccessType {
public:
constexpr CmptAccessType(size_t id, AccessMode mode) noexcept
constexpr CmptAccessType(size_t id, AccessMode mode = AccessMode::WRITE) noexcept
: type{ id }, mode{ mode } {}
constexpr CmptAccessType(std::string_view type_name, AccessMode mode) noexcept
constexpr CmptAccessType(std::string_view type_name, AccessMode mode = AccessMode::WRITE) noexcept
: type{ RuntimeTypeID(type_name) }, mode{ mode } {}
constexpr CmptAccessType(CmptType type, AccessMode mode) noexcept
constexpr CmptAccessType(CmptType type, AccessMode mode = AccessMode::WRITE) noexcept
: type{ type }, mode{ mode } {}
explicit constexpr CmptAccessType(CmptType type) noexcept : CmptAccessType{ type, AccessMode::LATEST } {}
explicit constexpr CmptAccessType() noexcept : CmptAccessType{ Invalid() } {}

template<typename Cmpt>
Expand Down
12 changes: 12 additions & 0 deletions include/UECS/RandomAccessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "CmptType.h"

#include <set>

namespace Ubpa::UECS {
class RandomAccessor {
public:
CmptAccessTypeSet types;
};
}
13 changes: 8 additions & 5 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ namespace Ubpa::UECS {
bool isParallel = true,
ArchetypeFilter = {},
CmptLocator = {},
SingletonLocator = {}
SingletonLocator = {},
RandomAccessor = {}
);

// Func's argument list:
Expand All @@ -54,7 +55,8 @@ namespace Ubpa::UECS {
std::string name,
ArchetypeFilter = {},
bool isParallel = true,
SingletonLocator = {}
SingletonLocator = {},
RandomAccessor = {}
);

// Func's argument list:
Expand All @@ -65,10 +67,11 @@ namespace Ubpa::UECS {
const SystemFunc* RegisterJob(
Func&&,
std::string name,
SingletonLocator = {}
SingletonLocator = {},
RandomAccessor = {}
);

void RegisterCommand(std::function<void(World*)> command, size_t layer = 0) {
void RegisterCommand(std::function<void(World*)> command, int layer = 0) {
commandBuffer[layer].push_back(std::move(command));
}

Expand Down Expand Up @@ -109,7 +112,7 @@ namespace Ubpa::UECS {
std::unordered_map<size_t, FilterChange> sysFilterChange;
std::unordered_set<size_t> sysLockFilter;

std::map<size_t, std::vector<std::function<void(World*)>>> commandBuffer;
std::map<int, std::vector<std::function<void(World*)>>> commandBuffer;

Pool<SystemFunc> sysFuncPool;
friend class World;
Expand Down
4 changes: 2 additions & 2 deletions include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Ubpa::UECS {
// 4. run commands in main thread
void Update();

void AddCommand(std::function<void()> command, size_t layer = 0);
void AddCommand(std::function<void()> command, int layer = 0);

// after running Update()
// you can use graphviz to vistualize the graph
Expand Down Expand Up @@ -120,7 +120,7 @@ namespace Ubpa::UECS {
Pool<Job> jobPool;

// command
std::map<size_t, std::vector<std::function<void()>>> commandBuffer;
std::map<int, std::vector<std::function<void()>>> commandBuffer;
std::mutex commandBufferMutex;
void RunCommands();

Expand Down
15 changes: 10 additions & 5 deletions include/UECS/detail/Schedule.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,35 @@ namespace Ubpa::UECS {
bool isParallel,
ArchetypeFilter filter,
CmptLocator cmptLocator,
SingletonLocator singletonLocator
SingletonLocator singletonLocator,
RandomAccessor randomAccessor
) {
return Request(
std::forward<Func>(func),
std::move(name),
std::move(filter),
std::move(cmptLocator),
std::move(singletonLocator),
std::move(randomAccessor),
isParallel
);
}


template<typename Func>
const SystemFunc* Schedule::RegisterChunkJob(
Func&& func,
std::string name,
ArchetypeFilter filter,
bool isParallel,
SingletonLocator singletonLocator
SingletonLocator singletonLocator,
RandomAccessor randomAccessor
) {
return Request(
std::forward<Func>(func),
std::move(name),
std::move(filter),
std::move(singletonLocator),
std::move(randomAccessor),
isParallel
);
}
Expand All @@ -42,12 +45,14 @@ namespace Ubpa::UECS {
const SystemFunc* Schedule::RegisterJob(
Func&& func,
std::string name,
SingletonLocator singletonLocator
SingletonLocator singletonLocator,
RandomAccessor randomAccessor
) {
return Request(
std::forward<Func>(func),
std::move(name),
std::move(singletonLocator)
std::move(singletonLocator),
std::move(randomAccessor)
);
}

Expand Down
8 changes: 5 additions & 3 deletions include/UECS/detail/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../EntityQuery.h"
#include "../SingletonLocator.h"
#include "../RandomAccessor.h"
#include "../Entity.h"
#include "../CmptsView.h"
#include "../SingletonsView.h"
Expand Down Expand Up @@ -37,18 +38,19 @@ namespace Ubpa::UECS {

EntityQuery entityQuery;
SingletonLocator singletonLocator;
RandomAccessor randomAccessor;

// Mode::Entity
template<typename Func>
SystemFunc(Func&&, std::string name, ArchetypeFilter, CmptLocator, SingletonLocator, bool isParallel);
SystemFunc(Func&&, std::string name, ArchetypeFilter, CmptLocator, SingletonLocator, RandomAccessor, bool isParallel);

// Mode::Chunk
template<typename Func>
SystemFunc(Func&&, std::string name, ArchetypeFilter, SingletonLocator, bool isParallel);
SystemFunc(Func&&, std::string name, ArchetypeFilter, SingletonLocator, RandomAccessor, bool isParallel);

// Mode::Job
template<typename Func>
SystemFunc(Func&&, std::string name, SingletonLocator);
SystemFunc(Func&&, std::string name, SingletonLocator, RandomAccessor);

const std::string& Name() const noexcept { return name; }

Expand Down
9 changes: 7 additions & 2 deletions include/UECS/detail/SystemFunc.inl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ namespace Ubpa::UECS {
ArchetypeFilter archetypeFilter,
CmptLocator cmptLocator,
SingletonLocator singletonLocator,
RandomAccessor randomAccessor,
bool isParallel
) :
entityQuery{ std::move(archetypeFilter), std::move(cmptLocator.Combine<decltype(func)>()) },
singletonLocator{ std::move(singletonLocator.Combine<decltype(func)>()) },
randomAccessor{std::move(randomAccessor)},
mode{ Mode::Entity },
name{ std::move(name) },
hashCode{ HashCode(this->name) },
Expand All @@ -44,10 +46,12 @@ namespace Ubpa::UECS {
std::string name,
ArchetypeFilter archetypeFilter,
SingletonLocator singletonLocator,
RandomAccessor randomAccessor,
bool isParallel
) :
entityQuery{ std::move(archetypeFilter) },
singletonLocator{ std::move(singletonLocator.Combine<decltype(func)>()) },
randomAccessor{ std::move(randomAccessor) },
mode{ Mode::Chunk },
name{ std::move(name) },
hashCode{ HashCode(this->name) },
Expand Down Expand Up @@ -76,9 +80,10 @@ namespace Ubpa::UECS {

// Mode::Job
template<typename Func>
SystemFunc::SystemFunc(Func&& func, std::string name, SingletonLocator singletonLocator)
SystemFunc::SystemFunc(Func&& func, std::string name, SingletonLocator singletonLocator, RandomAccessor randomAccessor)
:
singletonLocator{ std::move(singletonLocator.Combine<decltype(func)>()) },
randomAccessor {std::move(randomAccessor)},
mode{ Mode::Job },
name{ std::move(name) },
hashCode{ HashCode(this->name) },
Expand All @@ -96,7 +101,7 @@ namespace Ubpa::UECS {
&& !Contain_v<ArgList, CmptsView>
&& !Contain_v<ArgList, ChunkView>,
"(Mode::Job) SystemFunc's argument list cann't have Entity, indexInQuery CmptsView or ChunkView"
);
);
}
}

Expand Down
6 changes: 4 additions & 2 deletions include/UECS/detail/World.inl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace Ubpa::UECS {
std::move(filter),
std::move(cmptLocator),
std::move(singletonLocator),
std::move(isParallel)
{},
isParallel
};
Run(&sys);
}
Expand Down Expand Up @@ -57,7 +58,8 @@ namespace Ubpa::UECS {
"",
std::move(filter),
std::move(singletonLocator),
std::move(isParallel)
{},
isParallel
};
Run(&sys);
}
Expand Down
Loading

0 comments on commit a90698c

Please sign in to comment.