Skip to content

Commit

Permalink
Merge pull request #68 from KTStephano/v0.10
Browse files Browse the repository at this point in the history
V0.10
  • Loading branch information
KTStephano authored Jul 27, 2023
2 parents f11c020 + b07e455 commit 1d16f3b
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Examples/ExampleEnv05/Bistro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class Bistro : public stratus::Application {

// Check for key/mouse events
auto events = INSTANCE(InputManager)->GetInputEventsLastFrame();
for (auto e : events) {
for (auto& e : events) {
switch (e.type) {
case SDL_QUIT:
return stratus::SystemStatus::SYSTEM_SHUTDOWN;
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/StratusEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ComponentAllocator_<Component>& GetComponentAllocator_() {
if (ptr == nullptr) return; \
auto& allocator = GetComponentAllocator_<name>(); \
auto ul = std::unique_lock(allocator.m); \
allocator.allocator.Deallocate(ptr); \
allocator.allocator.DestroyDeallocate(ptr); \
}

namespace stratus {
Expand Down
21 changes: 13 additions & 8 deletions Source/Engine/StratusPoolAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <shared_mutex>
#include <functional>
#include <atomic>
#include "StratusPointer.h"

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

E * Allocate(const size_t count) {

}

template<typename Construct, typename ... Types>
E * AllocateCustomConstruct(Construct c, const Types&... args) {
uint8_t * bytes = nullptr;
uint8_t* bytes = nullptr;
{
//auto wlf = _frontBufferLock.LockWrite();
if (!frontBuffer_) {
Expand All @@ -117,12 +114,12 @@ namespace stratus {

MemBlock_* next = frontBuffer_;
frontBuffer_ = frontBuffer_->next;
bytes = reinterpret_cast<uint8_t *>(next);
bytes = reinterpret_cast<uint8_t*>(next);
}
return c(bytes, args...);
}

void Deallocate(E * ptr) {
void DestroyDeallocate(E * ptr) {
if (ptr == nullptr) return;
ptr->~E();
auto wlb = backBufferLock_.LockWrite();
Expand Down Expand Up @@ -192,6 +189,14 @@ namespace stratus {
virtual ~PoolAllocator() = default;
};

// This is meant to be used with C++ standard containers
template<typename T>
struct ContainerPoolAllocator {
typedef PoolAllocator<T> Allocator;


};

struct Lock_ {
struct LockHeld {
typedef void (*UnlockFunction)(std::shared_mutex *);
Expand Down Expand Up @@ -274,7 +279,7 @@ namespace stratus {

void operator()(E * ptr) {
//if (*allocator == nullptr) return;
allocator->Deallocate(ptr);
allocator->DestroyDeallocate(ptr);
}
};

Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/StratusRenderComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace stratus {
void Mesh::Destroy(MeshPtr ptr) {
auto& allocator = GetAllocator();
auto ul = std::unique_lock<std::mutex>(allocator.m);
allocator.allocator.Deallocate(ptr);
allocator.allocator.DestroyDeallocate(ptr);
}

EntityPtr CreateRenderEntity() {
Expand Down
72 changes: 41 additions & 31 deletions Source/Engine/StratusRendererBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,16 +858,19 @@ void RendererBackend::Begin(const std::shared_ptr<RendererFrame>& frame, bool cl
// projection * glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))
// };
//}
static std::vector<glm::mat4> GenerateLightViewTransforms(const glm::vec3 & lightPos) {
return std::vector<glm::mat4>{
static std::vector<glm::mat4, StackBasedPoolAllocator<glm::mat4>> GenerateLightViewTransforms(const glm::vec3 & lightPos, const UnsafePtr<StackAllocator>& allocator) {
return std::vector<glm::mat4, StackBasedPoolAllocator<glm::mat4>>({
// pos pos + dir up
glm::lookAt(lightPos, lightPos + glm::vec3( 1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3( 0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))
};
glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))
},

StackBasedPoolAllocator<glm::mat4>(allocator)
);
}

void RendererBackend::BindShader_(Pipeline * s) {
Expand Down Expand Up @@ -1266,7 +1269,7 @@ void RendererBackend::RenderAtmosphericShadowing_() {
glEnable(GL_DEPTH_TEST);
}

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

void RendererBackend::UpdatePointLights_(std::vector<std::pair<LightPtr, double>>& perLightDistToViewer,
std::vector<std::pair<LightPtr, double>>& perLightShadowCastingDistToViewer,
std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer,
std::vector<int>& visibleVplIndices) {
void RendererBackend::UpdatePointLights_(std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perLightDistToViewer,
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perLightShadowCastingDistToViewer,
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer,
std::vector<int, StackBasedPoolAllocator<int>>& visibleVplIndices) {
const Camera& c = *frame_->camera;

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

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

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

void RendererBackend::PerformVirtualPointLightCullingStage1_(
std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer,
std::vector<int>& visibleVplIndices) {
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer,
std::vector<int, StackBasedPoolAllocator<int>>& visibleVplIndices) {

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

Expand Down Expand Up @@ -1538,7 +1541,7 @@ void RendererBackend::PerformVirtualPointLightCullingStage1_(
// const std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer,
// const std::vector<int>& visibleVplIndices) {
void RendererBackend::PerformVirtualPointLightCullingStage2_(
const std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer) {
const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer) {

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

// Pack data into system memory
std::vector<GpuTextureHandle> diffuseHandles;
std::vector<GpuTextureHandle, StackBasedPoolAllocator<GpuTextureHandle>> diffuseHandles(StackBasedPoolAllocator<GpuTextureHandle>(frame_->perFrameScratchMemory));
diffuseHandles.reserve(totalVisible);
std::vector<GpuAtlasEntry> shadowDiffuseIndices;
std::vector<GpuAtlasEntry, StackBasedPoolAllocator<GpuAtlasEntry>> shadowDiffuseIndices(StackBasedPoolAllocator<GpuAtlasEntry>(frame_->perFrameScratchMemory));
shadowDiffuseIndices.reserve(totalVisible);
for (size_t i = 0; i < totalVisible; ++i) {
const int index = visibleVplIndices[i];
Expand Down Expand Up @@ -1683,7 +1686,7 @@ void RendererBackend::PerformVirtualPointLightCullingStage2_(
// _state.vpls.vplNumVisible.UnmapMemory();
}

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

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

std::vector<FrameBuffer *> buffers = {
std::vector<FrameBuffer*, StackBasedPoolAllocator<FrameBuffer*>> buffers({
&state_.vpls.vplGIDenoisedFbo1,
&state_.vpls.vplGIDenoisedFbo2
};
},

StackBasedPoolAllocator<FrameBuffer*>(frame_->perFrameScratchMemory)
);

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

std::vector<std::pair<LightPtr, double>>& perLightDistToViewer = perLightDistToViewer_;
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> perLightDistToViewer(LightDistancePairAllocator(frame_->perFrameScratchMemory));
// // This one is just for shadow-casting lights
std::vector<std::pair<LightPtr, double>>& perLightShadowCastingDistToViewer = perLightShadowCastingDistToViewer_;
std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer = perVPLDistToViewer_;
std::vector<int>& visibleVplIndices = visibleVplIndices_;
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> perLightShadowCastingDistToViewer(LightDistancePairAllocator(frame_->perFrameScratchMemory));
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> perVPLDistToViewer(LightDistancePairAllocator(frame_->perFrameScratchMemory));
std::vector<int, StackBasedPoolAllocator<int>> visibleVplIndices(StackBasedPoolAllocator<int>(frame_->perFrameScratchMemory));

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

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

Pipeline* bloom = state_.bloom.get();
BindShader_(bloom);
Expand Down Expand Up @@ -2322,7 +2331,7 @@ void RendererBackend::InitCoreCSMData_(Pipeline * s) {
}
}

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

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

std::vector<GpuPointLight> gpuLights;
std::vector<GpuAtlasEntry> gpuShadowCubeMaps;
std::vector<GpuPointLight> gpuShadowLights;
auto allocator = frame_->perFrameScratchMemory;
auto gpuLights = std::vector<GpuPointLight, StackBasedPoolAllocator<GpuPointLight>>(StackBasedPoolAllocator<GpuPointLight>(allocator));
auto gpuShadowCubeMaps = std::vector<GpuAtlasEntry, StackBasedPoolAllocator<GpuAtlasEntry>>(StackBasedPoolAllocator<GpuAtlasEntry>(allocator));
auto gpuShadowLights = std::vector<GpuPointLight, StackBasedPoolAllocator<GpuPointLight>>(StackBasedPoolAllocator<GpuPointLight>(allocator));
gpuLights.reserve(lights.size());
gpuShadowCubeMaps.reserve(maxShadowLights);
gpuShadowLights.reserve(maxShadowLights);
Expand Down
28 changes: 15 additions & 13 deletions Source/Engine/StratusRendererBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "StratusGpuMaterialBuffer.h"
#include "StratusGpuCommandBuffer.h"
#include <functional>
#include "StratusStackAllocator.h"

namespace stratus {
class Pipeline;
Expand Down Expand Up @@ -170,6 +171,9 @@ namespace stratus {
bool bloomEnabled = true;
bool usePerceptualRoughness = true;
RendererCascadeResolution cascadeResolution = RendererCascadeResolution::CASCADE_RESOLUTION_1024;
// Records how much temporary memory the renderer is allowed to use
// per frame
size_t perFrameMaxScratchMemoryBytes = 134217728; // 128 mb

float GetEmissionStrength() const {
return emissionStrength_;
Expand Down Expand Up @@ -279,6 +283,7 @@ namespace stratus {
glm::mat4 prevProjectionView = glm::mat4(1.0f);
glm::vec4 clearColor;
RendererSettings settings;
UnsafePtr<StackAllocator> perFrameScratchMemory;
bool viewportDirty;
};

Expand Down Expand Up @@ -500,10 +505,7 @@ namespace stratus {
GpuBuffer haltonSequence_;

// Used for point light sorting and culling
std::vector<std::pair<LightPtr, double>> perLightDistToViewer_;
std::vector<std::pair<LightPtr, double>> perLightShadowCastingDistToViewer_;
std::vector<std::pair<LightPtr, double>> perVPLDistToViewer_;
std::vector<int> visibleVplIndices_;
using LightDistancePairAllocator = StackBasedPoolAllocator<std::pair<LightPtr, double>>;

/**
* If the renderer was setup properly then this will be marked
Expand Down Expand Up @@ -570,7 +572,7 @@ namespace stratus {
void InitPointShadowMaps_();
// void _InitAllEntityMeshData();
void InitCoreCSMData_(Pipeline *);
void InitLights_(Pipeline * s, const std::vector<std::pair<LightPtr, double>> & lights, const size_t maxShadowLights);
void InitLights_(Pipeline * s, const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator> & lights, const size_t maxShadowLights);
void InitSSAO_();
void InitAtmosphericShadowing_();
// void _InitEntityMeshData(RendererEntityData &);
Expand All @@ -591,16 +593,16 @@ namespace stratus {
void RenderImmediate_(const RenderFaceCulling, GpuCommandBuffer2Ptr&, const CommandBufferSelectionFunction&);
void Render_(Pipeline&, const RenderFaceCulling, GpuCommandBuffer2Ptr&, const CommandBufferSelectionFunction&, bool isLightInteracting, bool removeViewTranslation = false);
void Render_(Pipeline&, std::unordered_map<RenderFaceCulling, GpuCommandBuffer2Ptr>&, const CommandBufferSelectionFunction&, bool isLightInteracting, bool removeViewTranslation = false);
void InitVplFrameData_(const std::vector<std::pair<LightPtr, double>>& perVPLDistToViewer);
void InitVplFrameData_(const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>& perVPLDistToViewer);
void RenderImmediate_(std::unordered_map<RenderFaceCulling, GpuCommandBuffer2Ptr>&, const CommandBufferSelectionFunction&, const bool reverseCullFace);
void UpdatePointLights_(std::vector<std::pair<LightPtr, double>>&,
std::vector<std::pair<LightPtr, double>>&,
std::vector<std::pair<LightPtr, double>>&,
std::vector<int>& visibleVplIndices);
void PerformVirtualPointLightCullingStage1_(std::vector<std::pair<LightPtr, double>>&, std::vector<int>& visibleVplIndices);
void UpdatePointLights_(std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&,
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&,
std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&,
std::vector<int, StackBasedPoolAllocator<int>>& visibleVplIndices);
void PerformVirtualPointLightCullingStage1_(std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&, std::vector<int, StackBasedPoolAllocator<int>>& visibleVplIndices);
//void PerformVirtualPointLightCullingStage2_(const std::vector<std::pair<LightPtr, double>>&, const std::vector<int>& visibleVplIndices);
void PerformVirtualPointLightCullingStage2_(const std::vector<std::pair<LightPtr, double>>&);
void ComputeVirtualPointLightGlobalIllumination_(const std::vector<std::pair<LightPtr, double>>&, const double);
void PerformVirtualPointLightCullingStage2_(const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&);
void ComputeVirtualPointLightGlobalIllumination_(const std::vector<std::pair<LightPtr, double>, LightDistancePairAllocator>&, const double);
void RenderCSMDepth_();
void RenderQuad_();
void RenderSkybox_(Pipeline *, const glm::mat4&);
Expand Down
Loading

0 comments on commit 1d16f3b

Please sign in to comment.