Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ add_library(ProjectDelta SHARED
"include/delta/definitions.h"
"src/delta/platform/os_internal.h"
"src/delta/platform/os_win32.cpp"
"include/delta/platform/os_types.h"
"include/delta/platform/os.h"
"include/delta/pch.h"
"src/delta/pch.h"
"src/delta/pch.cpp"
"src/delta/core/EngineTypes.h"
"src/delta/core/ThreadContext.h"
"src/delta/core/ThreadContext.cpp"
"src/delta/core/MemoryConfig.h"
"src/delta/core/MemoryConfig.cpp"
"src/delta/core/Worker.h"
"src/delta/core/Worker.cpp"
"include/delta/utils/StaticArray.h"
"src/delta/platform/os_internal_types.h"
"include/delta/core/core_types.h"
"include/delta/utils/CTMath.h"
"src/delta/core/aos_helper.h"
"src/delta/core/ThreadContextManager.h"
"src/delta/core/ThreadContextManager.cpp"
)

target_compile_definitions(ProjectDelta
Expand Down
18 changes: 18 additions & 0 deletions Engine/include/delta/core/core_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <delta/platform/os_types.h>

namespace delta::core
{
struct Context
{
delta::platform::WindowHandle window;
bool isRunning;
};

enum class AllocationType : uint8_t { TRANSIENT, PERSISTENT };

typedef void (*GameInitFunc)(Context*);
typedef void (*GameUpdateFunc)(Context*);
typedef void (*GameShutdownFunc)(Context*);
}
13 changes: 3 additions & 10 deletions Engine/include/delta/core/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,11 @@
*/

#include <delta/definitions.h>
#include <delta/core/core_types.h>
#include <delta/platform/os_types.h>

namespace delta::Engine
namespace delta::core
{
struct Context
{
bool isRunning;
};

typedef void (*GameInitFunc)(Context*);
typedef void (*GameUpdateFunc)(Context*);
typedef void (*GameShutdownFunc)(Context*);

DLT_API void Initialize(Context& context);
DLT_API void Update(Context& context);
DLT_API void Shutdown(Context& context);
Expand Down
21 changes: 10 additions & 11 deletions Engine/include/delta/core/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@
#pragma once

#include <delta/definitions.h>
#include <delta/core/core_types.h>

namespace delta::Engine
namespace delta::core
{
enum class AllocationType : uint8_t { TRANSIENT, PERSISTENT };

[[nodiscard]] DLT_API void* Allocate(size_t size, AllocationType type, size_t alignment = 8) noexcept;
DLT_API void Free(void* ptr) noexcept;
}

[[nodiscard]] inline void* operator new(size_t size) { return delta::Engine::Allocate(size, delta::Engine::AllocationType::PERSISTENT); }
[[nodiscard]] inline void* operator new(size_t size, delta::Engine::AllocationType type) { return delta::Engine::Allocate(size, type); }
[[nodiscard]] inline void* operator new[](size_t size) { return delta::Engine::Allocate(size, delta::Engine::AllocationType::PERSISTENT); }
[[nodiscard]] inline void* operator new[](size_t size, delta::Engine::AllocationType type) { return delta::Engine::Allocate(size, type); }
[[nodiscard]] inline void* operator new(size_t size) { return delta::core::Allocate(size, delta::core::AllocationType::PERSISTENT); }
[[nodiscard]] inline void* operator new(size_t size, delta::core::AllocationType type) { return delta::core::Allocate(size, type); }
[[nodiscard]] inline void* operator new[](size_t size) { return delta::core::Allocate(size, delta::core::AllocationType::PERSISTENT); }
[[nodiscard]] inline void* operator new[](size_t size, delta::core::AllocationType type) { return delta::core::Allocate(size, type); }

inline void operator delete(void* ptr) { return delta::Engine::Free(ptr); }
inline void operator delete(void* ptr, delta::Engine::AllocationType) { return delta::Engine::Free(ptr); }
inline void operator delete[](void* ptr) { return delta::Engine::Free(ptr); }
inline void operator delete[](void* ptr, delta::Engine::AllocationType) { return delta::Engine::Free(ptr); }
inline void operator delete(void* ptr) { return delta::core::Free(ptr); }
inline void operator delete(void* ptr, delta::core::AllocationType) { return delta::core::Free(ptr); }
inline void operator delete[](void* ptr) { return delta::core::Free(ptr); }
inline void operator delete[](void* ptr, delta::core::AllocationType) { return delta::core::Free(ptr); }
37 changes: 12 additions & 25 deletions Engine/include/delta/platform/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,21 @@
*/

#pragma once
#include <delta/platform/os_types.h>

namespace delta::platform
{
struct OSInfo
{
const char* cpuArchitecture;

uint32_t cpuPhysicalCoreCount;
uint32_t cpuLogicalProcessorCount;
uint32_t osPageSize;

char cpuBrandString[sizeof(int) * 12 + 1];
char cpuManufacturerId[13];

bool cpuHasSMT;
bool cpuHasAVX2;
bool cpuHasAVX512f;
bool cpuHasAVX512cd;
bool cpuHasAVX512er;
bool cpuHasAVX512pf;
};

struct MemoryStatus
{
uint64_t physicalInstalled;
uint64_t physicalFree;
};

// General
// TODO: Change names to the adequate ones
DLT_API const OSInfo* getOSInfo() noexcept;
DLT_API MemoryStatus getMemoryStatus();

// Window API
DLT_API void Window_SetTitle(WindowHandle window, const char* newTitle);
DLT_API void Window_Show(WindowHandle window);
DLT_API void Window_Hide(WindowHandle window);
DLT_API void Window_SetSize(WindowHandle window, uint32_t w, uint32_t h);
DLT_API void Window_SetPos(WindowHandle window, uint32_t x, uint32_t y);
DLT_API void Window_ShowCursor(WindowHandle window);
DLT_API void Window_HideCursor(WindowHandle window);
}
36 changes: 36 additions & 0 deletions Engine/include/delta/platform/os_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#define DLT_DEFINE_HANDLE(name)\
struct name;\
using name##Handle = name*;\
inline constexpr name##Handle INVALID_##name##_HANDLE = nullptr

namespace delta::platform
{
DLT_DEFINE_HANDLE(Window);

struct OSInfo
{
const char* cpuArchitecture;

uint32_t cpuPhysicalCoreCount;
uint32_t cpuLogicalProcessorCount;
uint32_t osPageSize;

char cpuBrandString[sizeof(int) * 12 + 1];
char cpuManufacturerId[13];

bool cpuHasSMT;
bool cpuHasAVX2;
bool cpuHasAVX512f;
bool cpuHasAVX512cd;
bool cpuHasAVX512er;
bool cpuHasAVX512pf;
};

struct MemoryStatus
{
uint64_t physicalInstalled;
uint64_t physicalFree;
};
}
21 changes: 21 additions & 0 deletions Engine/include/delta/utils/CTMath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <concepts>

namespace delta::utils
{
template <std::unsigned_integral T>
inline constexpr bool is_power_of_two(T value) noexcept
{
return value > 0 && (value & (value - 1)) == 0;
}

template <std::unsigned_integral T>
[[nodiscard]] inline constexpr T align_up(T value, T alignment) noexcept
{
T a = alignment - 1;
return ((alignment & a) == 0)
? ((value + a) & ~(a))
: (((value + a) / alignment) * alignment);
}
}
132 changes: 44 additions & 88 deletions Engine/src/delta/core/EngineTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,95 +17,51 @@
#pragma once

#include <delta/platform/os_internal.h>
#include <delta/utils/CTMath.h>
#include <delta/core/aos_helper.h>

namespace delta::core
{
// TODO: revise this structure and its purpose
struct ThreadPageCoordinator
{
uint8_t* virtualAddressBase;
size_t commitedOffset;
size_t reservedCapacity;
size_t pageSize;
};

struct ThreadArena
{
uint8_t* backingMemory;
size_t capacity;
size_t offset;
size_t maxCapacity;
};

struct DependencyCounter
{
std::atomic<uint32_t> count;
};

using task_t = void (*)(void*);
using payload_t = void*;
using queue_index_t = int64_t;
struct TaskQueue // SoA structure, Chase-Lev queue
{
alignas(64) std::atomic<queue_index_t> top;
alignas(64) std::atomic<queue_index_t> bottom;

alignas(64) queue_index_t size;
queue_index_t mask;

task_t* tasks;
payload_t* payloads;
DependencyCounter* depCounterPtr;

static inline constexpr size_t FIELD_SIZE = sizeof(task_t) + sizeof(payload_t);
};

enum class ThreadType : uint8_t { MAIN, WORKER };

struct alignas(64) GenericExecutionContext
{
// SHARED TRAITS
ThreadType type;
uint32_t threadIx;
delta::platform::ThreadHandle threadHandle;

ThreadPageCoordinator pageCoordinator;
ThreadArena transientArena;
delta::platform::Timer perThreadTimer;
};

struct alignas(64) MainExecutionContext
{
// SHARED TRAITS
GenericExecutionContext generic;

// ROLE TRAITS
ThreadArena persistentStorage;
DependencyCounter depCounter;
};

struct alignas(64) WorkerExecutionContext
{
GenericExecutionContext generic;

// ROLE TRAITS
TaskQueue taskQueue;
delta::platform::SemaphoreHandle sleepSemaphore;
std::atomic<bool> isAsleep;
std::atomic<bool> shouldClose;
};

template <typename T>
concept ExecutionContext =
std::is_same_v<std::remove_cvref_t<T>, GenericExecutionContext> ||
std::is_same_v<std::remove_cvref_t<T>, MainExecutionContext> ||
std::is_same_v<std::remove_cvref_t<T>, WorkerExecutionContext>;

// COMPILE TIME CONSTANTS
inline constexpr size_t THREAD_EXECUTION_CONTEXT_STRIDE = std::max<size_t>(sizeof(MainExecutionContext), sizeof(WorkerExecutionContext));
inline constexpr size_t THREAD_EXECUTION_CONTEXT_SIZE = (THREAD_EXECUTION_CONTEXT_STRIDE + 63) & ~(63);

// EXTERN VARIABLES & FUNCTIONS
extern uintptr_t g_MasterSlabStart;
extern uintptr_t g_MasterSlabEnd;
inline constexpr size_t THREADCTX_ALIGNMENT = 64ull;

#define DLT_CORE_PAGECRD_FIELDS(Layout) \
DLT_DEFAULT_FIELD_DEF(Layout, uintptr_t, base) \
DLT_DEFAULT_FIELD_DEF(Layout, size_t, size)

#define DLT_CORE_THREADARENA_FIELDS(Layout) \
DLT_DEFAULT_FIELD_DEF(Layout, uintptr_t, base) \
DLT_DEFAULT_FIELD_DEF(Layout, size_t, offset) \
DLT_DEFAULT_FIELD_DEF(Layout, size_t, reserved) \
DLT_DEFAULT_FIELD_DEF(Layout, size_t, size)

#define DLT_CORE_MASTERTHREADCTX_FIELDS(Layout) \
DLT_DEFAULT_FIELD_DEF(Layout, uint32_t, threadId) \
\
DLT_RESOURCE_GROUP_OPEN(Layout, pagecrd) \
DLT_CORE_PAGECRD_FIELDS(Layout) \
DLT_RESOURCE_GROUP_CLOSE(Layout, pagecrd) \
\
DLT_RESOURCE_GROUP_OPEN(Layout, transientStorage) \
DLT_CORE_THREADARENA_FIELDS(Layout) \
DLT_RESOURCE_GROUP_CLOSE(Layout, transientStorage) \
\
DLT_RESOURCE_GROUP_OPEN(Layout, persistentStorage) \
DLT_CORE_THREADARENA_FIELDS(Layout) \
DLT_RESOURCE_GROUP_CLOSE(Layout, persistentStorage)

#define DLT_CORE_WORKERTHREADCTX_FIELDS(Layout) \
DLT_DEFAULT_FIELD_DEF(Layout, uint32_t, threadId) \
\
DLT_RESOURCE_GROUP_OPEN(Layout, pagecrd) \
DLT_CORE_PAGECRD_FIELDS(Layout) \
DLT_RESOURCE_GROUP_CLOSE(Layout, pagecrd) \
\
DLT_RESOURCE_GROUP_OPEN(Layout, transientStorage) \
DLT_CORE_THREADARENA_FIELDS(Layout) \
DLT_RESOURCE_GROUP_CLOSE(Layout, transientStorage)

DLT_GEN_STRUCT_REPRESENTATIONS(PageCoordinator, DLT_CORE_PAGECRD_FIELDS)
DLT_GEN_STRUCT_REPRESENTATIONS(ThreadArena, DLT_CORE_THREADARENA_FIELDS)
DLT_GEN_ALIGNED_STRUCT_REPRESENTATIONS(MasterThreadContext, THREADCTX_ALIGNMENT, DLT_CORE_MASTERTHREADCTX_FIELDS)
DLT_GEN_ALIGNED_STRUCT_REPRESENTATIONS(WorkerThreadContext, THREADCTX_ALIGNMENT, DLT_CORE_WORKERTHREADCTX_FIELDS)
}
Loading