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
4 changes: 4 additions & 0 deletions Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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"
Expand All @@ -32,6 +33,9 @@ add_library(ProjectDelta SHARED
"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"
)

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);
}
}
2 changes: 1 addition & 1 deletion Engine/src/delta/core/ThreadContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ namespace delta::core
WorkerExecutionContext& ctx = GetExecutionContext<WorkerExecutionContext>(i);
ctx.generic.type = ThreadType::WORKER;
ctx.generic.threadIx = i;
ctx.generic.threadHandle = delta::platform::INVALID_THREAD_HANDLE; // Initialized when thread starts
ctx.generic.threadHandle = delta::platform::INVALID_Thread_HANDLE; // Initialized when thread starts
ctx.isAsleep.store(false, std::memory_order_relaxed);
ctx.shouldClose.store(false, std::memory_order_relaxed);
ctx.sleepSemaphore = delta::platform::Sync_CreateSemaphore();
Expand Down
4 changes: 2 additions & 2 deletions Engine/src/delta/core/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ namespace delta::core

// TODO: Take a look at native thread api and figure out how this could be done better
// TODO: Set thread affinity mask
s_Threads = new(delta::Engine::AllocationType::PERSISTENT) ThreadHandle[count];
ThreadCreateInfo* cs = new(delta::Engine::AllocationType::TRANSIENT) ThreadCreateInfo[count];
s_Threads = new(delta::core::AllocationType::PERSISTENT) ThreadHandle[count];
ThreadCreateInfo* cs = new(delta::core::AllocationType::TRANSIENT) ThreadCreateInfo[count];

for (uint32_t i = 0; i < count; i++)
{
Expand Down
11 changes: 9 additions & 2 deletions Engine/src/delta/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "ThreadContext.h"
#include "Worker.h"

namespace delta::Engine
namespace delta::core
{
using GenericExecutionContext = delta::core::GenericExecutionContext;

Expand All @@ -43,18 +43,25 @@ namespace delta::Engine
delta::platform::ThreadHandle th = delta::platform::Thread_GetCurrentHandle();
delta::platform::Thread_AssignPhysicalCore(th, 0);
delta::core::Worker_Init(totalThreads-1);

context.window = delta::platform::Window_Create();
delta::platform::Window_Show(context.window);
delta::platform::Window_Win32_Update(context.window);
}

void Update(Context& context)
{
// blah blah blah
// do something
delta::platform::Sync_Sleep(100);

delta::platform::Window_ProcessEvents();
delta::platform::Sync_Sleep(10);
delta::core::ThreadArena_Reset(delta::core::GetTransientArena());
}

void Shutdown(Context& context)
{
delta::core::Free(context.window);
delta::core::Worker_Shutdown();
delta::core::ThreadContext_Shutdown();
delta::core::MemoryConfig_Shutdown();
Expand Down
26 changes: 7 additions & 19 deletions Engine/src/delta/platform/os_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

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

namespace delta::platform
{
Expand All @@ -31,28 +32,12 @@ namespace delta::platform
bool Memory_ElevateLockLimit(size_t maxBytesToLock);

// Timer API
struct Timer_Internal;
struct Timer
{
alignas(8) uint8_t opaqueData[32];
};

void Timer_Initialize(Timer* timer);
int64_t Timer_GetTimestamp();
double Timer_TicksToMilliseconds(const Timer* timer, int64_t startTicks, int64_t endTicks);
double Timer_TicksToMicroseconds(const Timer* timer, int64_t startTicks, int64_t endTicks);

// Thread API
struct Thread;
using ThreadHandle = Thread*;
inline constexpr ThreadHandle INVALID_THREAD_HANDLE = nullptr; // random number 696767

struct ThreadCreateInfo
{
void (*fn)(void*);
void* args;
};

uint32_t Thread_GetCurrentId();
uint32_t Thread_GetId(ThreadHandle thread);
ThreadHandle Thread_GetCurrentHandle();
Expand All @@ -63,15 +48,18 @@ namespace delta::platform
void Thread_JoinMultiple(ThreadHandle* threads, uint32_t count);

// Sync API
struct Semaphore;
using SemaphoreHandle = Semaphore*;

SemaphoreHandle Sync_CreateSemaphore();
void Sync_DestroySemaphore(SemaphoreHandle sem);
void Sync_SignalSemaphore(SemaphoreHandle sem);
void Sync_WaitSemaphore(SemaphoreHandle sem);
void Sync_Sleep(uint32_t milliseconds);

// Window API
WindowHandle Window_Create();
void Window_Win32_Update(WindowHandle window);
void Window_ProcessEvents();
void Window_Destroy(WindowHandle window);

// Misc
uint32_t Misc_GetLastError() noexcept;
}
27 changes: 27 additions & 0 deletions Engine/src/delta/platform/os_internal_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <delta/platform/os_types.h>

namespace delta::platform
{
// Timer API
struct Timer_Internal;
struct Timer
{
alignas(8) uint8_t opaqueData[32];
};

// Thread API
DLT_DEFINE_HANDLE(Thread);

struct ThreadCreateInfo
{
void (*fn)(void*);
void* args;
};

// Sync API
DLT_DEFINE_HANDLE(Semaphore);

// Window API
// Window defined in the public header
}
Loading