From 4081539ee4ce2c444b4aa3ad5663150a0352894f Mon Sep 17 00:00:00 2001 From: Patedam Date: Tue, 28 Jul 2026 15:44:41 -0400 Subject: [PATCH] Using memory arena in the game that supports hot reload --- Game/Entity/Entity.h | 88 +++++++++--------- Game/Entity/EntityManager.cpp | 45 +++++----- Game/Entity/EntityManager.h | 29 +++--- Game/Game.vcxproj | 1 + Game/Game.vcxproj.filters | 1 + Game/game.cpp | 89 +++++++++++-------- Game/game.h | 23 +++++ Juliet.sln | 9 -- Juliet/Juliet.vcxproj | 3 - Juliet/Juliet.vcxproj.filters | 9 -- Juliet/include/Core/Common/NonNullPtr.h | 31 ++++--- Juliet/include/Core/Container/Vector.h | 17 ++-- Juliet/include/Core/JulietInit.h | 6 +- Juliet/include/Core/Memory/MemoryArena.h | 1 - Juliet/src/Core/HotReload/HotReload.cpp | 8 +- .../Core/HotReload/Win32/Win32HotReload.cpp | 3 +- .../Graphics/D3D12/D3D12GraphicsDevice.cpp | 16 ++-- Juliet/src/Graphics/D3D12/D3D12Texture.cpp | 8 ++ Juliet/src/Graphics/Graphics.cpp | 4 +- JulietApp/main.cpp | 21 ++--- JulietApp/main.h | 2 - JulietShaderCompiler.sln | 6 -- Romeo.sln | 6 -- tools/build_system.c | 10 ++- 24 files changed, 219 insertions(+), 217 deletions(-) create mode 100644 Game/game.h diff --git a/Game/Entity/Entity.h b/Game/Entity/Entity.h index 317a7a1..ec65859 100644 --- a/Game/Entity/Entity.h +++ b/Game/Entity/Entity.h @@ -18,57 +18,53 @@ constexpr Juliet::Class entityKind##entity(#entity, sizeof(#entity) / sizeof(char)); \ const Juliet::Class* entity::Kind = &entityKind##entity; -namespace Game +using DerivedType = void*; + +struct Entity final { - using DerivedType = void*; + EntityID ID; + const Juliet::Class* Kind; + DerivedType Derived; + float X, Y; +}; - struct Entity final - { - EntityID ID; - const Juliet::Class* Kind; - DerivedType Derived; - float X, Y; - }; +template +concept EntityConcept = requires(EntityType entity) { + requires std::same_as; + requires std::same_as; +}; - template - concept EntityConcept = requires(EntityType entity) { - requires std::same_as; - requires std::same_as; - }; +template + requires EntityConcept +bool IsA(const Entity* entity) +{ + return entity->Kind == EntityType::Kind; +} - template - requires EntityConcept - bool IsA(const Entity* entity) - { - return entity->Kind == EntityType::Kind; - } +template + requires EntityConcept +EntityType* MakeEntity(EntityManager& manager, float x, float y) +{ + auto* arena = manager.Arena; + EntityType* result = Juliet::ArenaPushStruct(arena); + Entity base; + base.X = x; + base.Y = y; + base.Derived = result; + base.Kind = EntityType::Kind; + manager.Entities.PushBack(base); - template - requires EntityConcept - EntityType* MakeEntity(EntityManager& manager, float x, float y) - { - auto* arena = manager.Arena; - EntityType* result = Juliet::ArenaPushStruct(arena); - Entity base; - base.X = x; - base.Y = y; - base.Derived = result; - base.Kind = EntityType::Kind; - manager.Entities.PushBack(base); + result->Base = manager.Entities.Back(); - result->Base = manager.Entities.Back(); + RegisterEntity(manager, &base); - RegisterEntity(manager, &base); + return result; +} - return result; - } - - template - requires EntityConcept - EntityType* DownCast(Entity* entity) - { - Assert(IsA(entity)); - return static_cast(entity->Derived); - } - -} // namespace Game +template + requires EntityConcept +EntityType* DownCast(Entity* entity) +{ + Assert(IsA(entity)); + return static_cast(entity->Derived); +} diff --git a/Game/Entity/EntityManager.cpp b/Game/Entity/EntityManager.cpp index f09696d..71c4c62 100644 --- a/Game/Entity/EntityManager.cpp +++ b/Game/Entity/EntityManager.cpp @@ -2,33 +2,30 @@ #include -namespace Game +EntityID EntityManager::ID = 0; + +void InitEntityManager(Juliet::NonNullPtr world) { - namespace - { - EntityManager Manager; - } + EntityManager* newManager = Juliet::ArenaPushStruct(world->WorldArena); + world->EntityManager = newManager; - EntityID EntityManager::ID = 0; + newManager->Entities.Create(world->WorldArena JULIET_DEBUG_PARAM("Entities")); - void InitEntityManager(Juliet::NonNullPtr arena) - { - Manager.Arena = arena.Get(); - Manager.Entities.Create(arena JULIET_DEBUG_PARAM("Entities")); - } + newManager->Arena = Juliet::ArenaAllocate({} JULIET_DEBUG_PARAM("Entity Arena")); +} - void ShutdownEntityManager() - { - Manager.Entities.Destroy(); - } +void ShutdownEntityManager() +{ + GetEntityManager().Entities.Destroy(); +} - EntityManager& GetEntityManager() - { - return Manager; - } +EntityManager& GetEntityManager() +{ + Juliet::NonNullPtr entityManager = GetGameState()->World->EntityManager; + return *entityManager; +} - void RegisterEntity(EntityManager& /*manager*/, Entity* entity) - { - entity->ID = EntityManager::ID++; - } -} // namespace Game +void RegisterEntity(EntityManager& /*manager*/, Entity* entity) +{ + entity->ID = EntityManager::ID++; +} diff --git a/Game/Entity/EntityManager.h b/Game/Entity/EntityManager.h index acc508d..6ec9ead 100644 --- a/Game/Entity/EntityManager.h +++ b/Game/Entity/EntityManager.h @@ -2,23 +2,22 @@ #include #include +#include -namespace Game +using EntityID = uint64_t; + +struct Entity; +struct EntityManager { - using EntityID = uint64_t; + static EntityID ID; - struct Entity; - struct EntityManager - { - static EntityID ID; + Juliet::Arena* Arena; - Juliet::Arena* Arena; - // TODO: Should be a pool - Juliet::VectorArena Entities; - }; + // TODO: Should be a pool + Juliet::VectorArena Entities; +}; - void InitEntityManager(Juliet::NonNullPtr arena); - void ShutdownEntityManager(); - EntityManager& GetEntityManager(); - void RegisterEntity(EntityManager& manager, Entity* entity); -} // namespace Game +void InitEntityManager(Juliet::NonNullPtr world); +void ShutdownEntityManager(); +EntityManager& GetEntityManager(); +void RegisterEntity(EntityManager& manager, Entity* entity); diff --git a/Game/Game.vcxproj b/Game/Game.vcxproj index 28660bf..ff807e3 100644 --- a/Game/Game.vcxproj +++ b/Game/Game.vcxproj @@ -84,6 +84,7 @@ + diff --git a/Game/Game.vcxproj.filters b/Game/Game.vcxproj.filters index 9fdb17f..4902938 100644 --- a/Game/Game.vcxproj.filters +++ b/Game/Game.vcxproj.filters @@ -15,5 +15,6 @@ Entity + diff --git a/Game/game.cpp b/Game/game.cpp index df4c4b0..0de671f 100644 --- a/Game/game.cpp +++ b/Game/game.cpp @@ -3,6 +3,8 @@ #undef min #undef max +#include + #include #include #include @@ -10,6 +12,12 @@ #include #include +GameState* gGameState = nullptr; +GameState* GetGameState() +{ + return gGameState; +} + // Test code namespace Game { @@ -26,56 +34,59 @@ namespace Game int Health; }; DEFINE_ENTITY(Rock); - } // namespace Game -using namespace Juliet; - -extern "C" JULIET_API void GameInit(GameInitParams* params) -{ - // Example allocation in GameArena - struct GameState - { - float TotalTime; - int Score; - }; - - auto* gameState = ArenaPushStruct(params->GameArena); - gameState->TotalTime = 0.0f; - gameState->Score = 0; - - printf("Game Arena Allocated: %p\n", static_cast(gameState)); - - using namespace Game; - - // Entity Use case - InitEntityManager(params->GameArena); - auto& manager = GetEntityManager(); - Door* door = MakeEntity(manager, 10.0f, 2.0f); - door->IsOpened = true; - - Entity* ent = door->Base; - [[maybe_unused]] Door* stillDoor = DownCast(ent); - Assert(door == stillDoor); - - Rock* rock = MakeEntity(manager, 1.f, 2.f); - rock->Health = 100; - Assert(door->Base != rock->Base); - - printf("Door is %s\n", door->IsOpened ? "Opened" : "Closed"); - printf("Rock has %d health points\n", rock->Health); -} - extern "C" JULIET_API void __cdecl GameShutdown() { printf("Shutting down game...\n"); + using namespace Juliet; using namespace Game; ShutdownEntityManager(); } -extern "C" JULIET_API void __cdecl GameUpdate([[maybe_unused]] float deltaTime) +extern "C" JULIET_API void __cdecl GameUpdate(Juliet::GameData* params, [[maybe_unused]] float deltaTime) { + using namespace Juliet; + using namespace Game; + + gGameState = params->GameState; + if (!gGameState) + { + Arena* gameStateArena = ArenaAllocate({ .ReserveSize = Megabytes(1) } JULIET_DEBUG_PARAM("Game Total Arena")); + auto* gameState = ArenaPushStruct(gameStateArena); + gGameState = params->GameState = gameState; + gameState->TotalArena = gameStateArena; + + gameState->TotalTime = 0.0f; + gameState->Score = 0; + + printf("Game Arena Allocated: %p\n", static_cast(gameState)); + + // Bootstrap world + auto* worldArena = ArenaAllocate({ .ReserveSize = Megabytes(1) } JULIET_DEBUG_PARAM("World Arena")); + World* world = ArenaPushStruct(worldArena JULIET_DEBUG_PARAM("World")); + gameState->World = world; + gameState->World->WorldArena = worldArena; + + // Entity Use case + InitEntityManager(gameState->World); + auto& manager = GetEntityManager(); + Door* door = MakeEntity(manager, 10.0f, 2.0f); + door->IsOpened = true; + + Entity* ent = door->Base; + [[maybe_unused]] Door* stillDoor = DownCast(ent); + Assert(door == stillDoor); + + Rock* rock = MakeEntity(manager, 1.f, 2.f); + rock->Health = 100; + Assert(door->Base != rock->Base); + + printf("Door is %s\n", door->IsOpened ? "Opened" : "Closed"); + printf("Rock has %d health points\n", rock->Health); + } + // printf("Updating game...\n"); } diff --git a/Game/game.h b/Game/game.h new file mode 100644 index 0000000..91bc696 --- /dev/null +++ b/Game/game.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +struct EntityManager; + +struct World +{ + Juliet::Arena* WorldArena; + EntityManager* EntityManager; +}; + +struct GameState +{ + Juliet::Arena* TotalArena; + + World* World; + + float TotalTime; + int Score; +}; + +extern GameState* GetGameState(); diff --git a/Juliet.sln b/Juliet.sln index bc8ee6c..346b18a 100644 --- a/Juliet.sln +++ b/Juliet.sln @@ -24,23 +24,14 @@ Global {1720427b-c8ba-3195-f931-e97f6d6125c1}.Release|x64.ActiveCfg = Release|x64 {1720427b-c8ba-3195-f931-e97f6d6125c1}.Release|x64.Build.0 = Release|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Debug|x64.ActiveCfg = Debug|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Debug|x64.Build.0 = Debug|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Profile|x64.ActiveCfg = Profile|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Profile|x64.Build.0 = Profile|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Release|x64.ActiveCfg = Release|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Release|x64.Build.0 = Release|x64 {b568a67e-05a1-9907-ff4b-a129119528bd}.Debug|x64.ActiveCfg = Debug|x64 - {b568a67e-05a1-9907-ff4b-a129119528bd}.Debug|x64.Build.0 = Debug|x64 {b568a67e-05a1-9907-ff4b-a129119528bd}.Profile|x64.ActiveCfg = Profile|x64 - {b568a67e-05a1-9907-ff4b-a129119528bd}.Profile|x64.Build.0 = Profile|x64 {b568a67e-05a1-9907-ff4b-a129119528bd}.Release|x64.ActiveCfg = Release|x64 - {b568a67e-05a1-9907-ff4b-a129119528bd}.Release|x64.Build.0 = Release|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Debug|x64.ActiveCfg = Debug|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Debug|x64.Build.0 = Debug|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Profile|x64.ActiveCfg = Profile|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Profile|x64.Build.0 = Profile|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Release|x64.ActiveCfg = Release|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Juliet/Juliet.vcxproj b/Juliet/Juliet.vcxproj index e2ae34c..49ec57d 100644 --- a/Juliet/Juliet.vcxproj +++ b/Juliet/Juliet.vcxproj @@ -147,10 +147,7 @@ - - - diff --git a/Juliet/Juliet.vcxproj.filters b/Juliet/Juliet.vcxproj.filters index 765e4dc..84e3f52 100644 --- a/Juliet/Juliet.vcxproj.filters +++ b/Juliet/Juliet.vcxproj.filters @@ -318,18 +318,9 @@ include\Core\Common - - include\Core\Common - - - include\Core\Common - include\Core\Common - - include\Core\Common - include\Core\Common diff --git a/Juliet/include/Core/Common/NonNullPtr.h b/Juliet/include/Core/Common/NonNullPtr.h index e80f939..134d10d 100644 --- a/Juliet/include/Core/Common/NonNullPtr.h +++ b/Juliet/include/Core/Common/NonNullPtr.h @@ -15,7 +15,7 @@ namespace Juliet class NonNullPtr { public: - inline NonNullPtr(Type* ptr) + constexpr NonNullPtr(Type* ptr) : InternalPtr(ptr) { Assert(ptr, "Tried to initialize a NonNullPtr with a null pointer"); @@ -23,14 +23,14 @@ namespace Juliet template requires NonNullPtr_Convertible - NonNullPtr(const NonNullPtr& otherPtr) + constexpr NonNullPtr(const NonNullPtr& otherPtr) : InternalPtr(otherPtr.Get()) { Assert(InternalPtr, "Fatal Error: Assigned a non null ptr using another NonNullPtr but its was null."); } // Assignment - NonNullPtr& operator=(Type* ptr) + [[nodiscard]] constexpr NonNullPtr& operator=(Type* ptr) { Assert(ptr, "Tried to assign a null pointer to a NonNullPtr!"); InternalPtr = ptr; @@ -39,68 +39,71 @@ namespace Juliet template requires NonNullPtr_Convertible - NonNullPtr& operator=(const NonNullPtr& otherPtr) + [[nodiscard]] constexpr NonNullPtr& operator=(const NonNullPtr& otherPtr) { InternalPtr = otherPtr.Get(); return *this; } // Accessors - operator Type*() const + [[nodiscard]] constexpr operator Type*() const { Assert(InternalPtr, "NonNullPtr: Internal Pointer is Null"); return InternalPtr; } - Type* Get() const + [[nodiscard]] constexpr Type* Get() const { Assert(InternalPtr, "NonNullPtr: Internal Pointer is Null"); return InternalPtr; } - Type& operator*() const + [[nodiscard]] constexpr Type& operator*() const { Assert(InternalPtr, "NonNullPtr: Internal Pointer is Null"); return *InternalPtr; } - inline Type* operator->() const + [[nodiscard]] constexpr Type* operator->() const { Assert(InternalPtr, "NonNullPtr: Internal Pointer is Null"); return InternalPtr; } // Comparisons - bool operator==(const NonNullPtr& otherPtr) const { return InternalPtr == otherPtr.InternalPtr; } + [[nodiscard]] constexpr bool operator==(const NonNullPtr& otherPtr) const + { + return InternalPtr == otherPtr.InternalPtr; + } template requires NonNullPtr_SameType - bool operator==(OtherType* otherRawPtr) const + [[nodiscard]] constexpr bool operator==(OtherType* otherRawPtr) const { return InternalPtr == otherRawPtr; } template requires NonNullPtr_SameType - friend bool operator==(OtherType* otherRawPtr, const NonNullPtr& nonNullPtr) + [[nodiscard]] constexpr friend bool operator==(OtherType* otherRawPtr, const NonNullPtr& nonNullPtr) { return otherRawPtr == nonNullPtr.InternalPtr; } // Forbid assigning a nullptr at compile time - NonNullPtr(std::nullptr_t) + constexpr NonNullPtr(std::nullptr_t) : InternalPtr(nullptr) { static_assert(sizeof(Type) == 0, "Trying to initialize a NonNullPtr with a nullptr value"); } - NonNullPtr& operator=(std::nullptr_t) + [[nodiscard]] constexpr NonNullPtr& operator=(std::nullptr_t) { static_assert(sizeof(Type) == 0, "Trying to assign a NonNullPtr with a nullptr value"); return *this; } - explicit operator bool() const { return true; } + [[nodiscard]] constexpr explicit operator bool() const { return InternalPtr != nullptr; } private: Type* InternalPtr; diff --git a/Juliet/include/Core/Container/Vector.h b/Juliet/include/Core/Container/Vector.h index 63e510c..e93221b 100644 --- a/Juliet/include/Core/Container/Vector.h +++ b/Juliet/include/Core/Container/Vector.h @@ -18,12 +18,16 @@ namespace Juliet Count = 0; Capacity = 0; Arena = arena; + ArenaPosAtCreation = ArenaPos(Arena); Reserve(ReserveSize); } void Destroy() { + Assert(ArenaPos(Arena) == ArenaPosAtCreation + sizeof(Type) * Capacity); + ArenaPopTo(Arena, ArenaPosAtCreation); + DataFirst = DataLast = Data = nullptr; Count = 0; Capacity = 0; @@ -197,12 +201,13 @@ namespace Juliet [[nodiscard]] size_t Size() const { return Count; } - Arena* Arena = nullptr; - Type* DataFirst = nullptr; - Type* DataLast = nullptr; - Type* Data = nullptr; - size_t Count = 0; - size_t Capacity = 0; + Arena* Arena = nullptr; + Type* DataFirst = nullptr; + Type* DataLast = nullptr; + Type* Data = nullptr; + size_t Count = 0; + size_t Capacity = 0; + index_t ArenaPosAtCreation = 0; JULIET_DEBUG_ONLY(const char* Name = "VectorArena";) }; static_assert(std::is_standard_layout_v>, diff --git a/Juliet/include/Core/JulietInit.h b/Juliet/include/Core/JulietInit.h index 573246a..01e935a 100644 --- a/Juliet/include/Core/JulietInit.h +++ b/Juliet/include/Core/JulietInit.h @@ -15,10 +15,10 @@ namespace Juliet struct Arena; - struct GameInitParams + struct GameData { - Arena* GameArena; - Arena* ScratchArena; + struct GameState* GameState; + Arena* ScratchArena; }; void JulietInit(JulietInit_Flags flags); diff --git a/Juliet/include/Core/Memory/MemoryArena.h b/Juliet/include/Core/Memory/MemoryArena.h index 6abb815..a907bf8 100644 --- a/Juliet/include/Core/Memory/MemoryArena.h +++ b/Juliet/include/Core/Memory/MemoryArena.h @@ -77,7 +77,6 @@ namespace Juliet const std::source_location& loc = std::source_location::current()); JULIET_API void ArenaRelease(NonNullPtr arena); - // Raw Push, can be used but templated helpers exists below // Raw Push, can be used but templated helpers exists below [[nodiscard]] JULIET_API void* ArenaPush(NonNullPtr arena, size_t size, size_t align, bool shouldBeZeroed JULIET_DEBUG_ONLY(, const char* tag)); diff --git a/Juliet/src/Core/HotReload/HotReload.cpp b/Juliet/src/Core/HotReload/HotReload.cpp index 8e24d89..0ed5ef0 100644 --- a/Juliet/src/Core/HotReload/HotReload.cpp +++ b/Juliet/src/Core/HotReload/HotReload.cpp @@ -11,7 +11,7 @@ namespace Juliet { void InitHotReloadCode(HotReloadCode& code, String dllName, String transientDllName, String lockFilename) { - code.Arena = ArenaAllocate({} JULIET_DEBUG_ONLY(, "Hot Reload")); + code.Arena = ArenaAllocate({ .ReserveSize = Megabytes(1) } JULIET_DEBUG_ONLY(, "Hot Reload")); // Get the app base path and build the dll path from there. String basePath = GetBasePath(); @@ -25,7 +25,8 @@ namespace Juliet const size_t dllFullPathLength = basePathLength + StringLength(dllName) + 1; // Need +1 because snprintf needs 0 terminated strings - code.DLLFullPath.Data = static_cast(ArenaPush(code.Arena, dllFullPathLength, alignof(char), true JULIET_DEBUG_ONLY(, "DLL Path"))); + code.DLLFullPath.Data = + static_cast(ArenaPush(code.Arena, dllFullPathLength, alignof(char), true JULIET_DEBUG_ONLY(, "DLL Path"))); int writtenSize = snprintf(CStr(code.DLLFullPath), dllFullPathLength, "%s%s", CStr(basePath), CStr(dllName)); if (writtenSize < static_cast(dllFullPathLength) - 1) { @@ -38,7 +39,8 @@ namespace Juliet // Lock filename path const size_t lockPathLength = basePathLength + StringLength(lockFilename) + 1; // Need +1 because snprintf needs 0 terminated strings - code.LockFullPath.Data = static_cast(ArenaPush(code.Arena, lockPathLength, alignof(char), true JULIET_DEBUG_ONLY(, "Lock File Path"))); + code.LockFullPath.Data = + static_cast(ArenaPush(code.Arena, lockPathLength, alignof(char), true JULIET_DEBUG_ONLY(, "Lock File Path"))); writtenSize = snprintf(CStr(code.LockFullPath), lockPathLength, "%s%s", CStr(basePath), CStr(lockFilename)); if (writtenSize < static_cast(lockPathLength) - 1) { diff --git a/Juliet/src/Core/HotReload/Win32/Win32HotReload.cpp b/Juliet/src/Core/HotReload/Win32/Win32HotReload.cpp index ef90048..b40e778 100644 --- a/Juliet/src/Core/HotReload/Win32/Win32HotReload.cpp +++ b/Juliet/src/Core/HotReload/Win32/Win32HotReload.cpp @@ -112,11 +112,10 @@ namespace Juliet else { code.IsValid = false; + break; } } } - - // Scratch memory, no free needed } if (!code.IsValid) diff --git a/Juliet/src/Graphics/D3D12/D3D12GraphicsDevice.cpp b/Juliet/src/Graphics/D3D12/D3D12GraphicsDevice.cpp index e24366a..069cc3b 100644 --- a/Juliet/src/Graphics/D3D12/D3D12GraphicsDevice.cpp +++ b/Juliet/src/Graphics/D3D12/D3D12GraphicsDevice.cpp @@ -549,8 +549,8 @@ namespace Juliet::D3D12 driver->D3D12SerializeVersionedRootSignatureFct = nullptr; + Assert(ArenaPos(driver->DriverArena) == sizeof(D3D12Driver)); // Verify we didnt forget to release something ArenaRelease(driver->DriverArena); - Free(driver.Get()); } bool AttachToWindow(NonNullPtr driver, NonNullPtr window) @@ -698,10 +698,10 @@ namespace Juliet::D3D12 GraphicsDevice* CreateGraphicsDevice(bool enableDebug) { - auto driver = static_cast(Calloc(1, sizeof(D3D12Driver))); + Arena* driverArena = ArenaAllocate({} JULIET_DEBUG_ONLY(, "D3D12 Driver Arena")); + D3D12Driver* driver = ArenaPushStruct(driverArena JULIET_DEBUG_PARAM("D3D12Driver struct")); - // TODO : Convert everything to arena - driver->DriverArena = ArenaAllocate({} JULIET_DEBUG_ONLY(, "D3D12 Driver")); + driver->DriverArena = driverArena; #if JULIET_DEBUG #ifdef IDXGIINFOQUEUE_SUPPORTED @@ -1032,10 +1032,10 @@ namespace Juliet::D3D12 device->DestroyShader = DestroyShader; device->CreateGraphicsPipeline = CreateGraphicsPipeline; device->DestroyGraphicsPipeline = DestroyGraphicsPipeline; - device->CreateGraphicsBuffer = CreateGraphicsBuffer; - device->DestroyGraphicsBuffer = DestroyGraphicsBuffer; - device->MapGraphicsBuffer = MapBuffer; - device->UnmapGraphicsBuffer = UnmapBuffer; + device->CreateGraphicsBuffer = CreateGraphicsBuffer; + device->DestroyGraphicsBuffer = DestroyGraphicsBuffer; + device->MapGraphicsBuffer = MapBuffer; + device->UnmapGraphicsBuffer = UnmapBuffer; device->CreateGraphicsTransferBuffer = CreateGraphicsTransferBuffer; device->DestroyGraphicsTransferBuffer = DestroyGraphicsTransferBuffer; device->MapGraphicsTransferBuffer = MapBuffer; diff --git a/Juliet/src/Graphics/D3D12/D3D12Texture.cpp b/Juliet/src/Graphics/D3D12/D3D12Texture.cpp index 4a286c9..e052c2f 100644 --- a/Juliet/src/Graphics/D3D12/D3D12Texture.cpp +++ b/Juliet/src/Graphics/D3D12/D3D12Texture.cpp @@ -517,13 +517,21 @@ namespace Juliet::D3D12 // Fix SRV format for Depth Buffers (TypeLess -> Typed) if (createInfo.Format == TextureFormat::D32_FLOAT) + { srvDesc.Format = DXGI_FORMAT_R32_FLOAT; + } else if (createInfo.Format == TextureFormat::D16_UNORM) + { srvDesc.Format = DXGI_FORMAT_R16_UNORM; + } else if (createInfo.Format == TextureFormat::D24_UNORM_S8_UINT) + { srvDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; + } else if (createInfo.Format == TextureFormat::D32_FLOAT_S8_UINT) + { srvDesc.Format = DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS; + } srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; diff --git a/Juliet/src/Graphics/Graphics.cpp b/Juliet/src/Graphics/Graphics.cpp index c484b4c..457ab2d 100644 --- a/Juliet/src/Graphics/Graphics.cpp +++ b/Juliet/src/Graphics/Graphics.cpp @@ -58,7 +58,6 @@ namespace Juliet { if (GraphicsDevice* newDevice = chosenFactory->CreateGraphicsDevice(config.EnableDebug)) { - newDevice->Name = chosenFactory->Name; return newDevice; } @@ -371,7 +370,8 @@ namespace Juliet GraphicsBuffer* CreateGraphicsBuffer(NonNullPtr device, const BufferCreateInfo& createInfo) { - return device->CreateGraphicsBuffer(device->Driver, createInfo.Size, createInfo.Stride, createInfo.Usage, createInfo.IsDynamic); + return device->CreateGraphicsBuffer(device->Driver, createInfo.Size, createInfo.Stride, createInfo.Usage, + createInfo.IsDynamic); } GraphicsTransferBuffer* CreateGraphicsTransferBuffer(NonNullPtr device, const TransferBufferCreateInfo& createInfo) diff --git a/JulietApp/main.cpp b/JulietApp/main.cpp index d36f45f..3651aa2 100644 --- a/JulietApp/main.cpp +++ b/JulietApp/main.cpp @@ -87,17 +87,16 @@ using namespace Juliet; namespace { - using GameInit_t = void (*)(GameInitParams*); using GameShutdown_t = void (*)(void); - using GameUpdate_t = void (*)(float deltaTime); + using GameUpdate_t = void (*)(GameData* params, float deltaTime); struct GameFunctionTable { - GameInit_t Init = nullptr; GameShutdown_t Shutdown = nullptr; GameUpdate_t Update = nullptr; } Game; + GameData Data; - const char* GameFunctionTable[] = { "GameInit", "GameShutdown", "GameUpdate" }; + const char* GameFunctionTable[] = { "GameShutdown", "GameUpdate" }; LightID RedLightID = 0; LightID BlueLightID = 0; @@ -178,13 +177,7 @@ void JulietApplication::Init(NonNullPtr) GameCode.FunctionCount = ArraySize(GameFunctionTable); GameCode.FunctionNames = GameFunctionTable; InitHotReloadCode(GameCode, ConstString("Game.dll"), ConstString("Game_Temp.dll"), ConstString("lock.tmp")); - if ((Running = GameCode.IsValid)) - { - GameInitParams params; - params.GameArena = GameArena = ArenaAllocate({} JULIET_DEBUG_ONLY(, "Game Arena")); - params.ScratchArena = GameScratchArena = ArenaAllocate({} JULIET_DEBUG_ONLY(, "Scratch Arena")); - Game.Init(¶ms); - } + Running = GameCode.IsValid; } } @@ -429,8 +422,6 @@ void JulietApplication::Update() ImGui::End(); #endif - ArenaClear(GameScratchArena); - Vector3 redLightPos = { 5.0f, 5.0f, 2.0f }; Vector3 blueLightPos = { -5.0f, 0.0f, 2.0f }; @@ -497,7 +488,7 @@ void JulietApplication::Update() DebugDisplay_DrawSphere(blueLightPos, 0.5f, { 0.0f, 0.0f, 1.0f, 1.0f }, true); DebugDisplay_DrawSphere(redLightPos, 0.5f, { 1.0f, 0.0f, 0.0f, 1.0f }, true); - Game.Update(0.0f); + Game.Update(&Data, 0.0f); if (ShouldReloadCode(GameCode)) { @@ -545,8 +536,6 @@ void JulietApplication::Update() Debug::DebugDrawMemoryArena(); #endif } - - ArenaClear(GameScratchArena); } void JulietApplication::OnPreRender(CommandList* /*cmd*/) {} diff --git a/JulietApp/main.h b/JulietApp/main.h index 3c4480f..e8a41d5 100644 --- a/JulietApp/main.h +++ b/JulietApp/main.h @@ -43,8 +43,6 @@ class JulietApplication : public Juliet::IApplication Juliet::HotReloadCode GameCode = {}; Juliet::GraphicsPipeline* GraphicsPipeline = {}; Juliet::Texture* DepthBuffer = {}; - Juliet::Arena* GameArena = nullptr; - Juliet::Arena* GameScratchArena = nullptr; int AutoCloseFrameCount = -1; bool Running = false; diff --git a/JulietShaderCompiler.sln b/JulietShaderCompiler.sln index 33db499..5e58359 100644 --- a/JulietShaderCompiler.sln +++ b/JulietShaderCompiler.sln @@ -22,17 +22,11 @@ Global {c7df05fe-d5d5-db2a-af36-e7d71d325fda}.Release|x64.ActiveCfg = Release|x64 {c7df05fe-d5d5-db2a-af36-e7d71d325fda}.Release|x64.Build.0 = Release|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Debug|x64.ActiveCfg = Debug|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Debug|x64.Build.0 = Debug|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Profile|x64.ActiveCfg = Profile|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Profile|x64.Build.0 = Profile|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Release|x64.ActiveCfg = Release|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Release|x64.Build.0 = Release|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Debug|x64.ActiveCfg = Debug|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Debug|x64.Build.0 = Debug|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Profile|x64.ActiveCfg = Profile|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Profile|x64.Build.0 = Profile|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Release|x64.ActiveCfg = Release|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Romeo.sln b/Romeo.sln index 6f1f418..904c7b8 100644 --- a/Romeo.sln +++ b/Romeo.sln @@ -22,17 +22,11 @@ Global {767620a7-b286-e7a1-9f79-3e14ce9e5ab1}.Release|x64.ActiveCfg = Release|x64 {767620a7-b286-e7a1-9f79-3e14ce9e5ab1}.Release|x64.Build.0 = Release|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Debug|x64.ActiveCfg = Debug|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Debug|x64.Build.0 = Debug|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Profile|x64.ActiveCfg = Profile|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Profile|x64.Build.0 = Profile|x64 {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Release|x64.ActiveCfg = Release|x64 - {a93aa30f-8f29-02fe-57e2-cd3626948b68}.Release|x64.Build.0 = Release|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Debug|x64.ActiveCfg = Debug|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Debug|x64.Build.0 = Debug|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Profile|x64.ActiveCfg = Profile|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Profile|x64.Build.0 = Profile|x64 {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Release|x64.ActiveCfg = Release|x64 - {652d3fea-b417-1d5e-9f79-3f32e9604ddc}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/tools/build_system.c b/tools/build_system.c index 59d6807..b0abfcc 100644 --- a/tools/build_system.c +++ b/tools/build_system.c @@ -1091,10 +1091,14 @@ static void GenerateSolution(const char* slnName, const char** projectNames, con const char* configs[] = { "Debug", "Profile", "Release" }; for (int c = 0; c < 3; c++) { offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, - " {%s}.%s|x64.ActiveCfg = %s|x64\n" - " {%s}.%s|x64.Build.0 = %s|x64\n", - projectGuids[i], configs[c], configs[c], + " {%s}.%s|x64.ActiveCfg = %s|x64\n", projectGuids[i], configs[c], configs[c]); + + if (i == 0) { + offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, + " {%s}.%s|x64.Build.0 = %s|x64\n", + projectGuids[i], configs[c], configs[c]); + } } }