Skip to content

Commit 1d16f3b

Browse files
authored
Merge pull request #68 from KTStephano/v0.10
V0.10
2 parents f11c020 + b07e455 commit 1d16f3b

File tree

10 files changed

+281
-85
lines changed

10 files changed

+281
-85
lines changed

Examples/ExampleEnv05/Bistro.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ class Bistro : public stratus::Application {
512512

513513
// Check for key/mouse events
514514
auto events = INSTANCE(InputManager)->GetInputEventsLastFrame();
515-
for (auto e : events) {
515+
for (auto& e : events) {
516516
switch (e.type) {
517517
case SDL_QUIT:
518518
return stratus::SystemStatus::SYSTEM_SHUTDOWN;

Source/Engine/StratusEntity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ComponentAllocator_<Component>& GetComponentAllocator_() {
5959
if (ptr == nullptr) return; \
6060
auto& allocator = GetComponentAllocator_<name>(); \
6161
auto ul = std::unique_lock(allocator.m); \
62-
allocator.allocator.Deallocate(ptr); \
62+
allocator.allocator.DestroyDeallocate(ptr); \
6363
}
6464

6565
namespace stratus {

Source/Engine/StratusPoolAllocator.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <shared_mutex>
77
#include <functional>
88
#include <atomic>
9+
#include "StratusPointer.h"
910

1011
// See https://www.qt.io/blog/a-fast-and-thread-safe-pool-allocator-for-qt-part-1
1112
// for some more information
@@ -93,13 +94,9 @@ namespace stratus {
9394
return AllocateCustomConstruct(PlacementNew_<Types...>, args...);
9495
}
9596

96-
E * Allocate(const size_t count) {
97-
98-
}
99-
10097
template<typename Construct, typename ... Types>
10198
E * AllocateCustomConstruct(Construct c, const Types&... args) {
102-
uint8_t * bytes = nullptr;
99+
uint8_t* bytes = nullptr;
103100
{
104101
//auto wlf = _frontBufferLock.LockWrite();
105102
if (!frontBuffer_) {
@@ -117,12 +114,12 @@ namespace stratus {
117114

118115
MemBlock_* next = frontBuffer_;
119116
frontBuffer_ = frontBuffer_->next;
120-
bytes = reinterpret_cast<uint8_t *>(next);
117+
bytes = reinterpret_cast<uint8_t*>(next);
121118
}
122119
return c(bytes, args...);
123120
}
124121

125-
void Deallocate(E * ptr) {
122+
void DestroyDeallocate(E * ptr) {
126123
if (ptr == nullptr) return;
127124
ptr->~E();
128125
auto wlb = backBufferLock_.LockWrite();
@@ -192,6 +189,14 @@ namespace stratus {
192189
virtual ~PoolAllocator() = default;
193190
};
194191

192+
// This is meant to be used with C++ standard containers
193+
template<typename T>
194+
struct ContainerPoolAllocator {
195+
typedef PoolAllocator<T> Allocator;
196+
197+
198+
};
199+
195200
struct Lock_ {
196201
struct LockHeld {
197202
typedef void (*UnlockFunction)(std::shared_mutex *);
@@ -274,7 +279,7 @@ namespace stratus {
274279

275280
void operator()(E * ptr) {
276281
//if (*allocator == nullptr) return;
277-
allocator->Deallocate(ptr);
282+
allocator->DestroyDeallocate(ptr);
278283
}
279284
};
280285

Source/Engine/StratusRenderComponents.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace stratus {
3232
void Mesh::Destroy(MeshPtr ptr) {
3333
auto& allocator = GetAllocator();
3434
auto ul = std::unique_lock<std::mutex>(allocator.m);
35-
allocator.allocator.Deallocate(ptr);
35+
allocator.allocator.DestroyDeallocate(ptr);
3636
}
3737

3838
EntityPtr CreateRenderEntity() {

Source/Engine/StratusRendererBackend.cpp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -858,16 +858,19 @@ void RendererBackend::Begin(const std::shared_ptr<RendererFrame>& frame, bool cl
858858
// projection * glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))
859859
// };
860860
//}
861-
static std::vector<glm::mat4> GenerateLightViewTransforms(const glm::vec3 & lightPos) {
862-
return std::vector<glm::mat4>{
861+
static std::vector<glm::mat4, StackBasedPoolAllocator<glm::mat4>> GenerateLightViewTransforms(const glm::vec3 & lightPos, const UnsafePtr<StackAllocator>& allocator) {
862+
return std::vector<glm::mat4, StackBasedPoolAllocator<glm::mat4>>({
863863
// pos pos + dir up
864-
glm::lookAt(lightPos, lightPos + glm::vec3( 1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
864+
glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
865865
glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
866-
glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)),
867-
glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)),
868-
glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
869-
glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))
870-
};
866+
glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)),
867+
glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)),
868+
glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
869+
glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))
870+
},
871+
872+
StackBasedPoolAllocator<glm::mat4>(allocator)
873+
);
871874
}
872875

873876
void RendererBackend::BindShader_(Pipeline * s) {
@@ -1266,7 +1269,7 @@ void RendererBackend::RenderAtmosphericShadowing_() {
12661269
glEnable(GL_DEPTH_TEST);
12671270
}
12681271

1269-
void RendererBackend::InitVplFrameData_(const std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer) {
1272+
void RendererBackend::InitVplFrameData_(const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer) {
12701273
std::vector<GpuVplData> vplData(perVPLDistToViewer.size());
12711274
for (size_t i = 0; i < perVPLDistToViewer.size(); ++i) {
12721275
VirtualPointLight * point = (VirtualPointLight *)perVPLDistToViewer[i].first.get();
@@ -1279,10 +1282,10 @@ void RendererBackend::InitVplFrameData_(const std::vector<std::pair<LightPtr, do
12791282
state_.vpls.vplData.CopyDataToBuffer(0, sizeof(GpuVplData) * vplData.size(), (const void *)vplData.data());
12801283
}
12811284

1282-
void RendererBackend::UpdatePointLights_(std::vector<std::pair<LightPtr, double>>& perLightDistToViewer,
1283-
std::vector<std::pair<LightPtr, double>>& perLightShadowCastingDistToViewer,
1284-
std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer,
1285-
std::vector<int>& visibleVplIndices) {
1285+
void RendererBackend::UpdatePointLights_(std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perLightDistToViewer,
1286+
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perLightShadowCastingDistToViewer,
1287+
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer,
1288+
std::vector<int, StackBasedPoolAllocator<int>>& visibleVplIndices) {
12861289
const Camera& c = *frame_->camera;
12871290

12881291
const bool worldLightEnabled = frame_->csc.worldLight->GetEnabled();
@@ -1400,7 +1403,7 @@ void RendererBackend::UpdatePointLights_(std::vector<std::pair<LightPtr, double>
14001403
// glClear(GL_DEPTH_BUFFER_BIT);
14011404

14021405
Pipeline * shader = light->IsVirtualLight() ? state_.vplShadows.get() : state_.shadows.get();
1403-
auto transforms = GenerateLightViewTransforms(point->GetPosition());
1406+
auto transforms = GenerateLightViewTransforms(point->GetPosition(), frame_->perFrameScratchMemory);
14041407

14051408
for (size_t i = 0; i < transforms.size(); ++i) {
14061409
const glm::mat4 projectionView = lightPerspective * transforms[i];
@@ -1455,8 +1458,8 @@ void RendererBackend::UpdatePointLights_(std::vector<std::pair<LightPtr, double>
14551458
}
14561459

14571460
void RendererBackend::PerformVirtualPointLightCullingStage1_(
1458-
std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer,
1459-
std::vector<int>& visibleVplIndices) {
1461+
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer,
1462+
std::vector<int, StackBasedPoolAllocator<int>>& visibleVplIndices) {
14601463

14611464
if (perVPLDistToViewer.size() == 0) return;
14621465

@@ -1538,7 +1541,7 @@ void RendererBackend::PerformVirtualPointLightCullingStage1_(
15381541
// const std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer,
15391542
// const std::vector<int>& visibleVplIndices) {
15401543
void RendererBackend::PerformVirtualPointLightCullingStage2_(
1541-
const std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer) {
1544+
const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer) {
15421545

15431546
// int totalVisible = *(int *)state_.vpls.vplNumVisible.MapMemory();
15441547
// state_.vpls.vplNumVisible.UnmapMemory();
@@ -1556,9 +1559,9 @@ void RendererBackend::PerformVirtualPointLightCullingStage2_(
15561559
}
15571560

15581561
// Pack data into system memory
1559-
std::vector<GpuTextureHandle> diffuseHandles;
1562+
std::vector<GpuTextureHandle, StackBasedPoolAllocator<GpuTextureHandle>> diffuseHandles(StackBasedPoolAllocator<GpuTextureHandle>(frame_->perFrameScratchMemory));
15601563
diffuseHandles.reserve(totalVisible);
1561-
std::vector<GpuAtlasEntry> shadowDiffuseIndices;
1564+
std::vector<GpuAtlasEntry, StackBasedPoolAllocator<GpuAtlasEntry>> shadowDiffuseIndices(StackBasedPoolAllocator<GpuAtlasEntry>(frame_->perFrameScratchMemory));
15621565
shadowDiffuseIndices.reserve(totalVisible);
15631566
for (size_t i = 0; i < totalVisible; ++i) {
15641567
const int index = visibleVplIndices[i];
@@ -1683,7 +1686,7 @@ void RendererBackend::PerformVirtualPointLightCullingStage2_(
16831686
// _state.vpls.vplNumVisible.UnmapMemory();
16841687
}
16851688

1686-
void RendererBackend::ComputeVirtualPointLightGlobalIllumination_(const std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer, const double deltaSeconds) {
1689+
void RendererBackend::ComputeVirtualPointLightGlobalIllumination_(const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer, const double deltaSeconds) {
16871690
if (perVPLDistToViewer.size() == 0) return;
16881691

16891692
// auto space = LogSpace<float>(1, 512, 30);
@@ -1745,10 +1748,13 @@ void RendererBackend::ComputeVirtualPointLightGlobalIllumination_(const std::vec
17451748
UnbindShader_();
17461749
state_.vpls.vplGIFbo.Unbind();
17471750

1748-
std::vector<FrameBuffer *> buffers = {
1751+
std::vector<FrameBuffer*, StackBasedPoolAllocator<FrameBuffer*>> buffers({
17491752
&state_.vpls.vplGIDenoisedFbo1,
17501753
&state_.vpls.vplGIDenoisedFbo2
1751-
};
1754+
},
1755+
1756+
StackBasedPoolAllocator<FrameBuffer*>(frame_->perFrameScratchMemory)
1757+
);
17521758

17531759
Texture indirectIllum = state_.vpls.vplGIFbo.GetColorAttachments()[0];
17541760
Texture indirectShadows = state_.vpls.vplGIFbo.GetColorAttachments()[1];
@@ -1829,11 +1835,11 @@ void RendererBackend::RenderScene(const double deltaSeconds) {
18291835
RenderCSMDepth_();
18301836
}
18311837

1832-
std::vector<std::pair<LightPtr, double>>& perLightDistToViewer = perLightDistToViewer_;
1838+
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> perLightDistToViewer(LightDistancePairAllocator(frame_->perFrameScratchMemory));
18331839
// // This one is just for shadow-casting lights
1834-
std::vector<std::pair<LightPtr, double>>& perLightShadowCastingDistToViewer = perLightShadowCastingDistToViewer_;
1835-
std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer = perVPLDistToViewer_;
1836-
std::vector<int>& visibleVplIndices = visibleVplIndices_;
1840+
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> perLightShadowCastingDistToViewer(LightDistancePairAllocator(frame_->perFrameScratchMemory));
1841+
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> perVPLDistToViewer(LightDistancePairAllocator(frame_->perFrameScratchMemory));
1842+
std::vector<int, StackBasedPoolAllocator<int>> visibleVplIndices(StackBasedPoolAllocator<int>(frame_->perFrameScratchMemory));
18371843

18381844
// Perform point light pass
18391845
UpdatePointLights_(perLightDistToViewer, perLightShadowCastingDistToViewer, perVPLDistToViewer, visibleVplIndices);
@@ -2008,7 +2014,10 @@ void RendererBackend::PerformBloomPostFx_() {
20082014
if (!frame_->settings.bloomEnabled) return;
20092015

20102016
// We use this so that we can avoid a final copy between the downsample and blurring stages
2011-
std::vector<PostFXBuffer> finalizedPostFxFrames(state_.numDownsampleIterations + state_.numUpsampleIterations);
2017+
std::vector<PostFXBuffer, StackBasedPoolAllocator<PostFXBuffer>> finalizedPostFxFrames(
2018+
state_.numDownsampleIterations + state_.numUpsampleIterations,
2019+
StackBasedPoolAllocator<PostFXBuffer>(frame_->perFrameScratchMemory)
2020+
);
20122021

20132022
Pipeline* bloom = state_.bloom.get();
20142023
BindShader_(bloom);
@@ -2322,7 +2331,7 @@ void RendererBackend::InitCoreCSMData_(Pipeline * s) {
23222331
}
23232332
}
23242333

2325-
void RendererBackend::InitLights_(Pipeline * s, const std::vector<std::pair<LightPtr, double>> & lights, const size_t maxShadowLights) {
2334+
void RendererBackend::InitLights_(Pipeline * s, const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> & lights, const size_t maxShadowLights) {
23262335
// Set up point lights
23272336

23282337
// Make sure everything is set to some sort of default to prevent shader crashes or huge performance drops
@@ -2368,9 +2377,10 @@ void RendererBackend::InitLights_(Pipeline * s, const std::vector<std::pair<Ligh
23682377
// ++lightIndex;
23692378
//}
23702379

2371-
std::vector<GpuPointLight> gpuLights;
2372-
std::vector<GpuAtlasEntry> gpuShadowCubeMaps;
2373-
std::vector<GpuPointLight> gpuShadowLights;
2380+
auto allocator = frame_->perFrameScratchMemory;
2381+
auto gpuLights = std::vector<GpuPointLight, StackBasedPoolAllocator<GpuPointLight>>(StackBasedPoolAllocator<GpuPointLight>(allocator));
2382+
auto gpuShadowCubeMaps = std::vector<GpuAtlasEntry, StackBasedPoolAllocator<GpuAtlasEntry>>(StackBasedPoolAllocator<GpuAtlasEntry>(allocator));
2383+
auto gpuShadowLights = std::vector<GpuPointLight, StackBasedPoolAllocator<GpuPointLight>>(StackBasedPoolAllocator<GpuPointLight>(allocator));
23742384
gpuLights.reserve(lights.size());
23752385
gpuShadowCubeMaps.reserve(maxShadowLights);
23762386
gpuShadowLights.reserve(maxShadowLights);

Source/Engine/StratusRendererBackend.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "StratusGpuMaterialBuffer.h"
2828
#include "StratusGpuCommandBuffer.h"
2929
#include <functional>
30+
#include "StratusStackAllocator.h"
3031

3132
namespace stratus {
3233
class Pipeline;
@@ -170,6 +171,9 @@ namespace stratus {
170171
bool bloomEnabled = true;
171172
bool usePerceptualRoughness = true;
172173
RendererCascadeResolution cascadeResolution = RendererCascadeResolution::CASCADE_RESOLUTION_1024;
174+
// Records how much temporary memory the renderer is allowed to use
175+
// per frame
176+
size_t perFrameMaxScratchMemoryBytes = 134217728; // 128 mb
173177

174178
float GetEmissionStrength() const {
175179
return emissionStrength_;
@@ -279,6 +283,7 @@ namespace stratus {
279283
glm::mat4 prevProjectionView = glm::mat4(1.0f);
280284
glm::vec4 clearColor;
281285
RendererSettings settings;
286+
UnsafePtr<StackAllocator> perFrameScratchMemory;
282287
bool viewportDirty;
283288
};
284289

@@ -500,10 +505,7 @@ namespace stratus {
500505
GpuBuffer haltonSequence_;
501506

502507
// Used for point light sorting and culling
503-
std::vector<std::pair<LightPtr, double>> perLightDistToViewer_;
504-
std::vector<std::pair<LightPtr, double>> perLightShadowCastingDistToViewer_;
505-
std::vector<std::pair<LightPtr, double>> perVPLDistToViewer_;
506-
std::vector<int> visibleVplIndices_;
508+
using LightDistancePairAllocator = StackBasedPoolAllocator<std::pair<LightPtr, double>>;
507509

508510
/**
509511
* If the renderer was setup properly then this will be marked
@@ -570,7 +572,7 @@ namespace stratus {
570572
void InitPointShadowMaps_();
571573
// void _InitAllEntityMeshData();
572574
void InitCoreCSMData_(Pipeline *);
573-
void InitLights_(Pipeline * s, const std::vector<std::pair<LightPtr, double>> & lights, const size_t maxShadowLights);
575+
void InitLights_(Pipeline * s, const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> & lights, const size_t maxShadowLights);
574576
void InitSSAO_();
575577
void InitAtmosphericShadowing_();
576578
// void _InitEntityMeshData(RendererEntityData &);
@@ -591,16 +593,16 @@ namespace stratus {
591593
void RenderImmediate_(const RenderFaceCulling, GpuCommandBuffer2Ptr&, const CommandBufferSelectionFunction&);
592594
void Render_(Pipeline&, const RenderFaceCulling, GpuCommandBuffer2Ptr&, const CommandBufferSelectionFunction&, bool isLightInteracting, bool removeViewTranslation = false);
593595
void Render_(Pipeline&, std::unordered_map<RenderFaceCulling, GpuCommandBuffer2Ptr>&, const CommandBufferSelectionFunction&, bool isLightInteracting, bool removeViewTranslation = false);
594-
void InitVplFrameData_(const std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer);
596+
void InitVplFrameData_(const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer);
595597
void RenderImmediate_(std::unordered_map<RenderFaceCulling, GpuCommandBuffer2Ptr>&, const CommandBufferSelectionFunction&, const bool reverseCullFace);
596-
void UpdatePointLights_(std::vector<std::pair<LightPtr, double>>&,
597-
std::vector<std::pair<LightPtr, double>>&,
598-
std::vector<std::pair<LightPtr, double>>&,
599-
std::vector<int>& visibleVplIndices);
600-
void PerformVirtualPointLightCullingStage1_(std::vector<std::pair<LightPtr, double>>&, std::vector<int>& visibleVplIndices);
598+
void UpdatePointLights_(std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&,
599+
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&,
600+
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&,
601+
std::vector<int, StackBasedPoolAllocator<int>>& visibleVplIndices);
602+
void PerformVirtualPointLightCullingStage1_(std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&, std::vector<int, StackBasedPoolAllocator<int>>& visibleVplIndices);
601603
//void PerformVirtualPointLightCullingStage2_(const std::vector<std::pair<LightPtr, double>>&, const std::vector<int>& visibleVplIndices);
602-
void PerformVirtualPointLightCullingStage2_(const std::vector<std::pair<LightPtr, double>>&);
603-
void ComputeVirtualPointLightGlobalIllumination_(const std::vector<std::pair<LightPtr, double>>&, const double);
604+
void PerformVirtualPointLightCullingStage2_(const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&);
605+
void ComputeVirtualPointLightGlobalIllumination_(const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&, const double);
604606
void RenderCSMDepth_();
605607
void RenderQuad_();
606608
void RenderSkybox_(Pipeline *, const glm::mat4&);

0 commit comments

Comments
 (0)