Skip to content

Commit

Permalink
Schedule use frame rsrc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Mar 18, 2021
1 parent 0f7deb1 commit 18d2835
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 84 deletions.
35 changes: 25 additions & 10 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include "details/SystemFunc.h"
#include "SystemFunc.h"
#include "details/Job.h"

#include <map>
#include <memory>
#include <memory_resource>

#include <USmallFlat/pmr/small_vector.hpp>

namespace Ubpa::UECS::details {
struct Compiler;
}
Expand Down Expand Up @@ -36,7 +38,7 @@ namespace Ubpa::UECS {
template<typename Func>
const SystemFunc* RegisterEntityJob(
Func&&,
std::string name,
std::string_view name,
bool isParallel = true,
ArchetypeFilter = {},
CmptLocator = {},
Expand All @@ -52,7 +54,7 @@ namespace Ubpa::UECS {
template<typename Func>
const SystemFunc* RegisterChunkJob(
Func&&,
std::string name,
std::string_view name,
ArchetypeFilter = {},
bool isParallel = true,
SingletonLocator = {},
Expand All @@ -66,7 +68,7 @@ namespace Ubpa::UECS {
template<typename Func>
const SystemFunc* RegisterJob(
Func&&,
std::string name,
std::string_view name,
SingletonLocator = {},
RandomAccessor = {}
);
Expand All @@ -82,6 +84,8 @@ namespace Ubpa::UECS {

// clear every frame
std::pmr::monotonic_buffer_resource* GetFrameMonotonicResource() { return &frame_rsrc; }
template<typename T, typename... Args>
T* CreateFrameObject(Args&&... args) const;

~Schedule();
private:
Expand All @@ -91,14 +95,24 @@ namespace Ubpa::UECS {
void Clear();

struct CmptSysFuncs {
std::vector<SystemFunc*> lastFrameSysFuncs;
std::vector<SystemFunc*> writeSysFuncs;
std::vector<SystemFunc*> latestSysFuncs;
CmptSysFuncs(std::pmr::memory_resource* rsrc) :
lastFrameSysFuncs{ std::pmr::vector<SystemFunc*>(std::pmr::polymorphic_allocator<SystemFunc*>{rsrc}) },
writeSysFuncs{ std::pmr::vector<SystemFunc*>(std::pmr::polymorphic_allocator<SystemFunc*>{rsrc}) },
latestSysFuncs{ std::pmr::vector<SystemFunc*>(std::pmr::polymorphic_allocator<SystemFunc*>{rsrc}) } {}

pmr::small_vector<SystemFunc*> lastFrameSysFuncs;
pmr::small_vector<SystemFunc*> writeSysFuncs;
pmr::small_vector<SystemFunc*> latestSysFuncs;
};
friend struct details::Compiler;
std::unordered_map<TypeID, CmptSysFuncs> GenCmptSysFuncsMap() const;

SysFuncGraph GenSysFuncGraph() const;
using CmptSysFuncsMap = std::pmr::unordered_map<TypeID, CmptSysFuncs>;

// use frame_rsrc, so no need to delete
CmptSysFuncsMap* GenCmptSysFuncsMap() const;

// use frame_rsrc, so no need to delete
SysFuncGraph* GenSysFuncGraph() const;

// SystemFunc's hashcode to pointer of SystemFunc
std::unordered_map<std::size_t, SystemFunc*> sysFuncs;
Expand All @@ -115,7 +129,8 @@ namespace Ubpa::UECS {

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

std::pmr::monotonic_buffer_resource frame_rsrc; // release in every frame
mutable std::pmr::monotonic_buffer_resource frame_rsrc; // release in every frame
std::string_view RegisterFrameString(std::string_view str);

friend class World;
};
Expand Down
26 changes: 13 additions & 13 deletions include/UECS/details/SystemFunc.h → include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "../EntityQuery.h"
#include "../SingletonLocator.h"
#include "../RandomAccessor.h"
#include "../Entity.h"
#include "../CmptsView.h"
#include "../SingletonsView.h"
#include "../ChunkView.h"
#include "EntityQuery.h"
#include "SingletonLocator.h"
#include "RandomAccessor.h"
#include "Entity.h"
#include "CmptsView.h"
#include "SingletonsView.h"
#include "ChunkView.h"

#include <functional>

Expand Down Expand Up @@ -42,17 +42,17 @@ namespace Ubpa::UECS {

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

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

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

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

static constexpr std::size_t GetValue(std::string_view name) noexcept { return string_hash(name); }

Expand All @@ -68,11 +68,11 @@ namespace Ubpa::UECS {
bool operator==(const SystemFunc& sysFunc) const noexcept { return name == sysFunc.name; }
private:
Mode mode;
std::string name;
std::string_view name;
std::size_t hashCode; // after name
bool isParallel;
std::function<void(World*, SingletonsView, Entity, std::size_t, CmptsView, ChunkView)> func;
};
}

#include "SystemFunc.inl"
#include "details/SystemFunc.inl"
18 changes: 12 additions & 6 deletions include/UECS/details/Schedule.inl
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#pragma once

namespace Ubpa::UECS {
template<typename T, typename... Args>
T* Schedule::CreateFrameObject(Args&&... args) const {
void* buffer = frame_rsrc.allocate(sizeof(T), alignof(T));
return new(buffer)T(std::forward<Args>(args)...);
}

template<typename Func>
const SystemFunc* Schedule::RegisterEntityJob(
Func&& func,
std::string name,
std::string_view name,
bool isParallel,
ArchetypeFilter filter,
CmptLocator cmptLocator,
Expand All @@ -13,7 +19,7 @@ namespace Ubpa::UECS {
) {
return Request(
std::forward<Func>(func),
std::move(name),
RegisterFrameString(name),
std::move(filter),
std::move(cmptLocator),
std::move(singletonLocator),
Expand All @@ -25,15 +31,15 @@ namespace Ubpa::UECS {
template<typename Func>
const SystemFunc* Schedule::RegisterChunkJob(
Func&& func,
std::string name,
std::string_view name,
ArchetypeFilter filter,
bool isParallel,
SingletonLocator singletonLocator,
RandomAccessor randomAccessor
) {
return Request(
std::forward<Func>(func),
std::move(name),
RegisterFrameString(name),
std::move(filter),
std::move(singletonLocator),
std::move(randomAccessor),
Expand All @@ -44,13 +50,13 @@ namespace Ubpa::UECS {
template<typename Func>
const SystemFunc* Schedule::RegisterJob(
Func&& func,
std::string name,
std::string_view name,
SingletonLocator singletonLocator,
RandomAccessor randomAccessor
) {
return Request(
std::forward<Func>(func),
std::move(name),
RegisterFrameString(name),
std::move(singletonLocator),
std::move(randomAccessor)
);
Expand Down
12 changes: 6 additions & 6 deletions include/UECS/details/SystemFunc.inl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Ubpa::UECS {
template<typename Func>
SystemFunc::SystemFunc(
Func&& func,
std::string name,
std::string_view name,
ArchetypeFilter archetypeFilter,
CmptLocator cmptLocator,
SingletonLocator singletonLocator,
Expand All @@ -23,7 +23,7 @@ namespace Ubpa::UECS {
singletonLocator{ std::move(singletonLocator.Combine<decltype(func)>()) },
randomAccessor{std::move(randomAccessor)},
mode{ Mode::Entity },
name{ std::move(name) },
name{ name },
hashCode{ GetValue(this->name) },
isParallel{ isParallel },
func{ details::Pack(std::forward<Func>(func)) }
Expand All @@ -43,7 +43,7 @@ namespace Ubpa::UECS {
template<typename Func>
SystemFunc::SystemFunc(
Func&& func,
std::string name,
std::string_view name,
ArchetypeFilter archetypeFilter,
SingletonLocator singletonLocator,
RandomAccessor randomAccessor,
Expand All @@ -53,7 +53,7 @@ namespace Ubpa::UECS {
singletonLocator{ std::move(singletonLocator.Combine<decltype(func)>()) },
randomAccessor{ std::move(randomAccessor) },
mode{ Mode::Chunk },
name{ std::move(name) },
name{ name },
hashCode{ GetValue(this->name) },
isParallel{ isParallel },
func{ details::Pack(std::forward<Func>(func)) }
Expand All @@ -80,12 +80,12 @@ namespace Ubpa::UECS {

// Mode::Job
template<typename Func>
SystemFunc::SystemFunc(Func&& func, std::string name, SingletonLocator singletonLocator, RandomAccessor randomAccessor)
SystemFunc::SystemFunc(Func&& func, std::string_view name, SingletonLocator singletonLocator, RandomAccessor randomAccessor)
:
singletonLocator{ std::move(singletonLocator.Combine<decltype(func)>()) },
randomAccessor {std::move(randomAccessor)},
mode{ Mode::Job },
name{ std::move(name) },
name{ name },
hashCode{ GetValue(this->name) },
isParallel{ false },
func{ details::Pack(std::forward<Func>(func)) }
Expand Down
2 changes: 1 addition & 1 deletion src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Archetype.h"

#include <UECS/details/SystemFunc.h>
#include <UECS/SystemFunc.h>
#include <UECS/IListener.h>

using namespace Ubpa::UECS;
Expand Down
Loading

0 comments on commit 18d2835

Please sign in to comment.