Skip to content

Commit

Permalink
Added the abilty to slect a shadow map to debug, Fixed the dirlight p…
Browse files Browse the repository at this point in the history
…os in the light matrix calculation, Added back the abilty to update the shadow map on the fly.
  • Loading branch information
Alex Mollard committed Aug 4, 2024
1 parent 70dbbe2 commit 6600c9a
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 164 deletions.
6 changes: 3 additions & 3 deletions SlimeOdyssey/include/Entity.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once
#include <memory>
#include <ostream>
#include <spdlog/spdlog.h>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#include "Component.h"

#include "Component.h"
#include "EntityManager.h"
#include <spdlog/spdlog.h>

class Entity
{
Expand All @@ -24,7 +24,7 @@ class Entity

void SetActive(bool isActive);

template<typename T, typename... Args>
template<typename T, typename... Args>
T& AddComponent(Args&&... args)
{
static_assert(std::is_base_of_v<Component, T>, "T must inherit from Component");
Expand Down
30 changes: 15 additions & 15 deletions SlimeOdyssey/include/EntityManager.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include <bitset>
#include <typeindex>
#include <unordered_map>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <typeindex>
#include <unordered_map>
#include <vector>

class Entity;
Expand Down Expand Up @@ -44,8 +44,8 @@ class EntityManager
// Get entity by name
std::shared_ptr<Entity> GetEntityByName(const std::string& name) const;

// Get entities with specific components
template<typename... Ts>
// Get entities with specific components
template<typename... Ts>
std::vector<std::shared_ptr<Entity>> GetEntitiesWithComponents()
{
ComponentMask mask = GetComponentMask<Ts...>();
Expand All @@ -60,7 +60,7 @@ class EntityManager
return result;
}

// Get entity count with specific components
// Get entity count with specific components
template<typename... Ts>
size_t GetEntityCountWithComponents() const
{
Expand All @@ -76,7 +76,7 @@ class EntityManager
return count;
}

// Execute a function for each entity with specific components
// Execute a function for each entity with specific components
template<typename... Ts, typename Func>
void ForEachEntityWith(Func func)
{
Expand All @@ -93,8 +93,8 @@ class EntityManager
// Update component masks when an entity's components change
void OnEntityComponentChanged(const Entity& entity);

// Shows a window of all entities and their components
void ImGuiDebug();
// Shows a window of all entities and their components
void ImGuiDebug();

private:
// Helper function to get or create component type index
Expand Down Expand Up @@ -123,13 +123,13 @@ class EntityManager
// Get entity index
size_t GetEntityIndex(const Entity& entity) const;

// Imgui debug
Entity* m_selectedEntity = nullptr;
float m_splitRatio = 0.5f;
void RenderComponentDetails();
void RenderEntityTree(const std::string& searchStr);
std::vector<std::shared_ptr<Entity>> m_entities;
// Imgui debug
Entity* m_selectedEntity = nullptr;
float m_splitRatio = 0.5f;
void RenderComponentDetails();
void RenderEntityTree(const std::string& searchStr);

std::vector<std::shared_ptr<Entity>> m_entities;
std::vector<ComponentMask> m_entityMasks;

mutable std::unordered_map<std::type_index, std::uint64_t> m_componentTypeIndices;
Expand Down
21 changes: 18 additions & 3 deletions SlimeOdyssey/include/ShadowSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ShadowSystem
void Initialize(vkb::DispatchTable& disp, VmaAllocator allocator, VulkanDebugUtils& debugUtils);
void Cleanup(vkb::DispatchTable& disp, VmaAllocator allocator);

void UpdateShadowMaps(vkb::DispatchTable& disp,
bool UpdateShadowMaps(vkb::DispatchTable& disp,
VkCommandBuffer& cmd,
ModelManager& modelManager,
VmaAllocator allocator,
Expand All @@ -49,27 +49,42 @@ class ShadowSystem
TextureResource GetShadowMap(const std::shared_ptr<Light> light) const;
glm::mat4 GetLightSpaceMatrix(const std::shared_ptr<Light> light) const;

void SetShadowMapResolution(uint32_t width, uint32_t height);
void SetShadowMapResolution(vkb::DispatchTable& disp, VmaAllocator allocator, VulkanDebugUtils& debugUtils, uint32_t width, uint32_t height, bool reconstructImmediately = false);
void ReconstructShadowMaps(vkb::DispatchTable& disp, VmaAllocator allocator, VulkanDebugUtils& debugUtils);

void SetShadowNearPlane(float near);
void SetShadowFarPlane(float far);

void SetDirectionalLightDistance(float distance);
float GetDirectionalLightDistance() const;

float GetShadowMapPixelValue(vkb::DispatchTable& disp, VmaAllocator allocator, VkCommandPool commandPool, VkQueue graphicsQueue, const std::shared_ptr<Light> light, int x, int y) const;

void RenderShadowMapInspector(vkb::DispatchTable& disp, VmaAllocator allocator, VkCommandPool commandPool, VkQueue graphicsQueue, ModelManager& modelManager, VulkanDebugUtils& debugUtils);

private:
struct ShadowData
{
ImTextureID textureId;
ImTextureID textureId = 0;
TextureResource shadowMap;
glm::mat4 lightSpaceMatrix;
VkBuffer stagingBuffer = VK_NULL_HANDLE;
VmaAllocation stagingBufferAllocation = VK_NULL_HANDLE;
VkDeviceSize stagingBufferSize = 0;

// For optimizing the recalculations
glm::vec3 lastCameraPosition;
float frustumRadius;
};

std::unordered_map<std::shared_ptr<Light>, ShadowData> m_shadowData;

float m_directionalLightDistance = 100.0f;

uint32_t m_pendingShadowMapWidth = 0;
uint32_t m_pendingShadowMapHeight = 0;
bool m_shadowMapNeedsReconstruction = false;

uint32_t m_shadowMapWidth = 4096;
uint32_t m_shadowMapHeight = 4096;
float m_shadowNear = 0.1f;
Expand Down
137 changes: 68 additions & 69 deletions SlimeOdyssey/src/EntityManager.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include "EntityManager.h"

#include <functional>
#include <memory>
#include <vector>

#include "Entity.h"
#include <algorithm>
#include <cstdint>
#include <functional>
#include <glm/common.hpp>
#include <iterator>
#include <memory>
#include <string>
#include <typeindex>
#include <type_traits>
#include <algorithm>
#include <typeindex>
#include <vector>

#include "Entity.h"
#include "imgui.h"
#include <glm/common.hpp>

// Update entity's component mask
void EntityManager::UpdateEntityMask(const Entity& entity)
Expand Down Expand Up @@ -111,45 +110,45 @@ void EntityManager::OnEntityComponentChanged(const Entity& entity)
// Shows a window of all entities and their components
void EntityManager::ImGuiDebug()
{
ImGui::SetNextWindowSizeConstraints(ImVec2(300, 300), ImVec2(FLT_MAX, FLT_MAX));
ImGui::Begin("Entity Manager", nullptr, ImGuiWindowFlags_NoScrollbar);

// Search bar and controls
static char searchBuffer[256] = "";
ImGui::InputText("Search Entities", searchBuffer, IM_ARRAYSIZE(searchBuffer));
std::string searchStr = searchBuffer;
ImGui::Text("Total Entities: %zu", m_entities.size());
ImGui::Separator();

// Calculate available height for split view
float availableHeight = ImGui::GetContentRegionAvail().y;

// Splitter
static float splitHeight = availableHeight * 0.5f; // Initial split at 50%
ImGui::BeginChild("TopRegion", ImVec2(0, splitHeight), true);
RenderEntityTree(searchStr);
ImGui::EndChild();

// Splitter bar
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.7f, 0.7f, 0.7f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.9f, 0.9f, 0.9f, 1.0f));

ImGui::Button("##splitter", ImVec2(-1, 5));
if (ImGui::IsItemActive())
{
splitHeight += ImGui::GetIO().MouseDelta.y;
splitHeight = glm::clamp(splitHeight, availableHeight * 0.1f, availableHeight * 0.9f);
}

ImGui::PopStyleColor(3);

ImGui::BeginChild("BottomRegion", ImVec2(0, 0), true);
RenderComponentDetails();
ImGui::EndChild();

ImGui::End();
ImGui::SetNextWindowSizeConstraints(ImVec2(300, 300), ImVec2(FLT_MAX, FLT_MAX));
ImGui::Begin("Entity Manager", nullptr, ImGuiWindowFlags_NoScrollbar);

// Search bar and controls
static char searchBuffer[256] = "";
ImGui::InputText("Search Entities", searchBuffer, IM_ARRAYSIZE(searchBuffer));
std::string searchStr = searchBuffer;
ImGui::Text("Total Entities: %zu", m_entities.size());
ImGui::Separator();

// Calculate available height for split view
float availableHeight = ImGui::GetContentRegionAvail().y;

// Splitter
static float splitHeight = availableHeight * 0.5f; // Initial split at 50%

ImGui::BeginChild("TopRegion", ImVec2(0, splitHeight), true);
RenderEntityTree(searchStr);
ImGui::EndChild();

// Splitter bar
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.7f, 0.7f, 0.7f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.9f, 0.9f, 0.9f, 1.0f));

ImGui::Button("##splitter", ImVec2(-1, 5));
if (ImGui::IsItemActive())
{
splitHeight += ImGui::GetIO().MouseDelta.y;
splitHeight = glm::clamp(splitHeight, availableHeight * 0.1f, availableHeight * 0.9f);
}

ImGui::PopStyleColor(3);

ImGui::BeginChild("BottomRegion", ImVec2(0, 0), true);
RenderComponentDetails();
ImGui::EndChild();

ImGui::End();
}

void EntityManager::RenderEntityTree(const std::string& searchStr)
Expand Down Expand Up @@ -195,25 +194,25 @@ void EntityManager::RenderEntityTree(const std::string& searchStr)

void EntityManager::RenderComponentDetails()
{
if (m_selectedEntity)
{
// Component Count
ImGui::Text("Component Count: %zu", m_selectedEntity->GetComponentCount());
ImGui::Separator();
for (const auto& [type, component] : m_selectedEntity->GetComponents())
{
ImGui::PushID(type.name());
if (ImGui::CollapsingHeader(type.name(), ImGuiTreeNodeFlags_DefaultOpen))
{
ImGui::Indent();
component->ImGuiDebug();
ImGui::Unindent();
}
ImGui::PopID();
}
}
else
{
ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "Select an entity to view its components");
}
}
if (m_selectedEntity)
{
// Component Count
ImGui::Text("Component Count: %zu", m_selectedEntity->GetComponentCount());
ImGui::Separator();
for (const auto& [type, component]: m_selectedEntity->GetComponents())
{
ImGui::PushID(type.name());
if (ImGui::CollapsingHeader(type.name(), ImGuiTreeNodeFlags_DefaultOpen))
{
ImGui::Indent();
component->ImGuiDebug();
ImGui::Unindent();
}
ImGui::PopID();
}
}
else
{
ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "Select an entity to view its components");
}
}
5 changes: 4 additions & 1 deletion SlimeOdyssey/src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ int Renderer::Draw(vkb::DispatchTable& disp,

std::function<void(vkb::DispatchTable, VulkanDebugUtils&, VkCommandBuffer&, ModelManager&, Scene*)> DrawModelsForShadowMap = std::bind(&Renderer::DrawModelsForShadowMap, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, scene);

m_shadowSystem.UpdateShadowMaps(disp, cmd, modelManager, allocator, commandPool, graphicsQueue, debugUtils, scene, DrawModelsForShadowMap, lights, camera);
if (m_shadowSystem.UpdateShadowMaps(disp, cmd, modelManager, allocator, commandPool, graphicsQueue, debugUtils, scene, DrawModelsForShadowMap, lights, camera))
{
m_forceInvalidateDecriptorSets = true;
}

SetupViewportAndScissor(swapchain, disp, cmd);
SlimeUtil::SetupDepthTestingAndLineWidth(disp, cmd);
Expand Down
Loading

0 comments on commit 6600c9a

Please sign in to comment.