Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nepp95 committed Nov 24, 2024
1 parent ea92a2c commit b0a37b5
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 121 deletions.
2 changes: 1 addition & 1 deletion EppoEditor/Projects/Test/Assets/AssetRegistry.epporeg
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
Filepath: Scenes\Test.epscene
- AssetHandle: 17362452449132321929
Type: Mesh
Filepath: Meshes\Sponza.glb
Filepath: Meshes\Sponza.glb
14 changes: 11 additions & 3 deletions EppoEditor/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ DockId=0x00000004,0

[Window][Performance]
Pos=1283,20
Size=317,347
Size=317,390
Collapsed=0
DockId=0x00000007,0
DockId=0x0000000D,0

[Window][Content Browser Panel]
Pos=305,609
Expand All @@ -50,6 +50,12 @@ Size=317,531
Collapsed=0
DockId=0x00000008,0

[Window][Debug Maps]
Pos=1283,412
Size=317,488
Collapsed=0
DockId=0x0000000E,0

[Docking][Data]
DockSpace ID=0x09EF459F Window=0x9A404470 Pos=25,70 Size=1600,880 Split=X
DockNode ID=0x0000000B Parent=0x09EF459F SizeRef=1281,880 Split=X
Expand All @@ -62,6 +68,8 @@ DockSpace ID=0x09EF459F Window=0x9A404470 Pos=25,70 Size=1600,880 Split=
DockNode ID=0x0000000A Parent=0x00000001 SizeRef=976,553 CentralNode=1 Selected=0x13926F0B
DockNode ID=0x00000002 Parent=0x00000006 SizeRef=1600,291 Selected=0x1A4A3508
DockNode ID=0x0000000C Parent=0x09EF459F SizeRef=317,880 Split=Y Selected=0xD841E954
DockNode ID=0x00000007 Parent=0x0000000C SizeRef=317,347 Selected=0x60B79D0E
DockNode ID=0x00000007 Parent=0x0000000C SizeRef=317,347 Split=Y Selected=0x60B79D0E
DockNode ID=0x0000000D Parent=0x00000007 SizeRef=317,390 Selected=0x60B79D0E
DockNode ID=0x0000000E Parent=0x00000007 SizeRef=317,488 Selected=0xE22F261B
DockNode ID=0x00000008 Parent=0x0000000C SizeRef=317,531 Selected=0xD841E954

4 changes: 2 additions & 2 deletions EppoEngine/Source/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ namespace Eppo
}
}

bool Application::OnWindowClose(WindowCloseEvent& e)
bool Application::OnWindowClose(const WindowCloseEvent& e)
{
Close();

return true;
}

bool Application::OnWindowResize(WindowResizeEvent& e)
bool Application::OnWindowResize(const WindowResizeEvent& e)
{
EPPO_PROFILE_FUNCTION("Application::OnWindowResize");

Expand Down
4 changes: 2 additions & 2 deletions EppoEngine/Source/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ namespace Eppo
void Run();
void ExecuteMainThreadQueue();

bool OnWindowClose(WindowCloseEvent& e);
bool OnWindowResize(WindowResizeEvent& e);
bool OnWindowClose(const WindowCloseEvent& e);
bool OnWindowResize(const WindowResizeEvent& e);

private:
ApplicationSpecification m_Specification;
Expand Down
71 changes: 14 additions & 57 deletions EppoEngine/Source/Platform/Vulkan/VulkanShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ namespace Eppo
{
namespace Utils
{
inline std::filesystem::path GetCacheDirectory()
{
if (!Filesystem::Exists("Resources/Shaders/Cache"))
std::filesystem::create_directories("Resources/Shaders/Cache");

return "Resources/Shaders/Cache";
}

inline shaderc_shader_kind ShaderStageToShaderCKind(ShaderStage stage)
{
switch (stage)
Expand All @@ -34,27 +26,6 @@ namespace Eppo
return (shaderc_shader_kind)-1;
}

inline std::string ShaderStageToString(ShaderStage stage)
{
switch (stage)
{
case ShaderStage::Vertex: return "vert";
case ShaderStage::Fragment: return "frag";
}

EPPO_ASSERT(false);
return "Invalid";
}

inline ShaderStage StringToShaderStage(std::string_view stage)
{
if (stage == "vert") return ShaderStage::Vertex;
if (stage == "frag") return ShaderStage::Fragment;

EPPO_ASSERT(false);
return ShaderStage::None;
}

inline VkShaderStageFlagBits ShaderStageToVkShaderStage(ShaderStage stage)
{
if (stage == ShaderStage::Vertex) return VK_SHADER_STAGE_VERTEX_BIT;
Expand All @@ -73,15 +44,13 @@ namespace Eppo
}

VulkanShader::VulkanShader(const ShaderSpecification& specification)
: m_Specification(specification)
: Shader(specification)
{
m_Name = m_Specification.Filepath.stem().string();

// Read shader source
const std::string shaderSource = Filesystem::ReadText(m_Specification.Filepath);
const std::string shaderSource = Filesystem::ReadText(GetSpecification().Filepath);

// Preprocess by shader stage
std::unordered_map<ShaderStage, std::string> sources = PreProcess(shaderSource);
auto sources = PreProcess(shaderSource);

// Compile or get cache
CompileOrGetCache(sources);
Expand All @@ -92,19 +61,14 @@ namespace Eppo
m_ShaderResources[3] = {};

// Reflection
for (auto&& [type, data] : m_ShaderBytes)
for (const auto& [type, data] : m_ShaderBytes)
Reflect(type, data);

CreatePipelineShaderInfos();
CreateDescriptorSetLayouts();
}

VulkanShader::~VulkanShader()
{

}

std::unordered_map<ShaderStage, std::string> VulkanShader::PreProcess(std::string_view source)
std::unordered_map<Eppo::ShaderStage, std::string> VulkanShader::PreProcess(std::string_view source)
{
std::unordered_map<ShaderStage, std::string> shaderSources;

Expand All @@ -122,7 +86,7 @@ namespace Eppo

// Extract shader stage
const size_t begin = pos + stageTokenLength + 1;
const std::string stage = std::string(source.substr(begin, eol - begin));
const auto stage = std::string(source.substr(begin, eol - begin));
EPPO_ASSERT((bool)Utils::StringToShaderStage(stage)); // "Invalid stage specified!"

// If there is no other stage token, take the string till eof. Otherwise till the next stage token
Expand All @@ -144,10 +108,10 @@ namespace Eppo
options.SetOptimizationLevel(shaderc_optimization_level_zero);

// Compile source
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(source, Utils::ShaderStageToShaderCKind(stage), m_Specification.Filepath.string().c_str(), options);
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(source, Utils::ShaderStageToShaderCKind(stage), GetSpecification().Filepath.string().c_str(), options);
if (result.GetCompilationStatus() != shaderc_compilation_status_success)
{
EPPO_ERROR("Failed to compile shader with filename: {}", m_Specification.Filepath);
EPPO_ERROR("Failed to compile shader with filename: {}", GetSpecification().Filepath);
EPPO_ERROR(result.GetErrorMessage());
EPPO_ASSERT(false);
}
Expand All @@ -156,7 +120,7 @@ namespace Eppo

// TODO:
// Write cache
std::string cachePath = Utils::GetCacheDirectory().string() + "/" + m_Name + "." + Utils::ShaderStageToString(stage);
std::string cachePath = Utils::GetOrCreateCacheDirectory().string() + "/" + GetName() + "." + Utils::ShaderStageToString(stage);
Filesystem::WriteBytes(cachePath, m_ShaderBytes.at(stage));

// Write cache hash
Expand All @@ -167,11 +131,11 @@ namespace Eppo

void VulkanShader::CompileOrGetCache(const std::unordered_map<ShaderStage, std::string>& sources)
{
const std::filesystem::path cacheDir = Utils::GetCacheDirectory();
const std::filesystem::path cacheDir = Utils::GetOrCreateCacheDirectory();

for (const auto& [stage, source] : sources)
{
std::string cacheFile = cacheDir.string() + "/" + m_Name + "." + Utils::ShaderStageToString(stage);
std::string cacheFile = cacheDir.string() + "/" + GetName() + "." + Utils::ShaderStageToString(stage);
std::string cacheHashFile = cacheFile + ".hash";

// Check if cache needs to be busted
Expand All @@ -188,7 +152,7 @@ namespace Eppo

if (cacheVerified)
{
EPPO_INFO("Loading shader cache: {}.glsl (Stage: {})", m_Name, Utils::ShaderStageToString(stage));
EPPO_INFO("Loading shader cache: {}.glsl (Stage: {})", GetName(), Utils::ShaderStageToString(stage));

// Read shader cache
ScopedBuffer buffer = Filesystem::ReadBytes(cacheFile);
Expand All @@ -202,7 +166,7 @@ namespace Eppo
}
else
{
EPPO_INFO("Triggering recompilation of shader due to hash mismatch: {}.glsl (Stage: {})", m_Name, Utils::ShaderStageToString(stage));
EPPO_INFO("Triggering recompilation of shader due to hash mismatch: {}.glsl (Stage: {})", GetName(), Utils::ShaderStageToString(stage));

Compile(stage, source);
}
Expand All @@ -214,7 +178,7 @@ namespace Eppo
spirv_cross::Compiler compiler(shaderBytes);
spirv_cross::ShaderResources resources = compiler.get_shader_resources();

EPPO_TRACE("Shader::Reflect - {}.glsl (Stage: {})", m_Name, Utils::ShaderStageToString(stage));
EPPO_TRACE("Shader::Reflect - {}.glsl (Stage: {})", GetName(), Utils::ShaderStageToString(stage));
EPPO_TRACE(" {} Push constants", resources.push_constant_buffers.size());
EPPO_TRACE(" {} Uniform buffers", resources.uniform_buffers.size());
EPPO_TRACE(" {} Sampled images", resources.sampled_images.size());
Expand All @@ -229,13 +193,6 @@ namespace Eppo
size_t bufferSize = compiler.get_declared_struct_size(bufferType);
size_t memberCount = bufferType.member_types.size();

/*uint32_t offset = 0;
if (!m_PushConstantRanges.empty())
{
for (const auto& pcr : m_PushConstantRanges)
offset += pcr.size;
}*/

if (m_PushConstantRanges.empty())
{
VkPushConstantRange& pcr = m_PushConstantRanges.emplace_back();
Expand Down
10 changes: 2 additions & 8 deletions EppoEngine/Source/Platform/Vulkan/VulkanShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ namespace Eppo
class VulkanShader : public Shader
{
public:
VulkanShader(const ShaderSpecification& specification);
virtual ~VulkanShader();
explicit VulkanShader(const ShaderSpecification& specification);
~VulkanShader() override = default;

const std::unordered_map<uint32_t, std::vector<ShaderResource>>& GetShaderResources() const { return m_ShaderResources; }
const std::vector<VkPipelineShaderStageCreateInfo>& GetPipelineShaderStageInfos() const { return m_ShaderInfos; }
const std::vector<VkDescriptorSetLayout>& GetDescriptorSetLayouts() const { return m_DescriptorSetLayouts; }
const std::vector<VkPushConstantRange>& GetPushConstantRanges() const { return m_PushConstantRanges; }

const std::string& GetName() const override { return m_Name; }

private:
std::unordered_map<ShaderStage, std::string> PreProcess(std::string_view source);
void Compile(ShaderStage stage, const std::string& source);
Expand All @@ -27,9 +24,6 @@ namespace Eppo
void CreateDescriptorSetLayouts();

private:
ShaderSpecification m_Specification;
std::string m_Name;

std::unordered_map<ShaderStage, std::vector<uint32_t>> m_ShaderBytes;
std::unordered_map<uint32_t, std::vector<ShaderResource>> m_ShaderResources;

Expand Down
2 changes: 1 addition & 1 deletion EppoEngine/Source/Platform/Vulkan/VulkanUniformBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Eppo
{
public:
VulkanUniformBuffer(uint32_t size, uint32_t binding);
~VulkanUniformBuffer();
~VulkanUniformBuffer() override;

void SetData(void* data, uint32_t size) override;

Expand Down
56 changes: 24 additions & 32 deletions EppoEngine/Source/Platform/Vulkan/VulkanVertexBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,12 @@

namespace Eppo
{

VulkanVertexBuffer::VulkanVertexBuffer(void* data, uint32_t size)
{
m_LocalStorage = Buffer::Copy(data, size);
CreateBuffer(VMA_MEMORY_USAGE_GPU_ONLY);
}

VulkanVertexBuffer::~VulkanVertexBuffer()
{
EPPO_WARN("Releasing vertex buffer {}", (void*)this);
VulkanAllocator::DestroyBuffer(m_Buffer, m_Allocation);
}

void VulkanVertexBuffer::RT_Bind(Ref<CommandBuffer> commandBuffer) const
{
Renderer::SubmitCommand([this, commandBuffer]()
{
auto cmd = std::static_pointer_cast<VulkanCommandBuffer>(commandBuffer);

VkBuffer vb = { m_Buffer };
VkDeviceSize offsets[] = { 0 };

vkCmdBindVertexBuffers(cmd->GetCurrentCommandBuffer(), 0, 1, &vb, offsets);
});
}

void VulkanVertexBuffer::CreateBuffer(VmaMemoryUsage usage)
VulkanVertexBuffer::VulkanVertexBuffer(Buffer buffer)
{
// Create staging buffer
VkBufferCreateInfo stagingBufferInfo{};
stagingBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
stagingBufferInfo.size = m_LocalStorage.Size;
stagingBufferInfo.size = buffer.Size;
stagingBufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
stagingBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

Expand All @@ -46,13 +20,13 @@ namespace Eppo

// Copy data to staging buffer
void* memData = VulkanAllocator::MapMemory(stagingBufferAlloc);
memcpy(memData, m_LocalStorage.Data, m_LocalStorage.Size);
memcpy(memData, buffer.Data, buffer.Size);
VulkanAllocator::UnmapMemory(stagingBufferAlloc);

// Create GPU local buffer
VkBufferCreateInfo vertexBufferInfo{};
vertexBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
vertexBufferInfo.size = m_LocalStorage.Size;
vertexBufferInfo.size = buffer.Size;
vertexBufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;

m_Allocation = VulkanAllocator::AllocateBuffer(m_Buffer, vertexBufferInfo, VMA_MEMORY_USAGE_GPU_ONLY);
Expand All @@ -65,13 +39,31 @@ namespace Eppo
VkBufferCopy copyRegion{};
copyRegion.srcOffset = 0;
copyRegion.dstOffset = 0;
copyRegion.size = m_LocalStorage.Size;
copyRegion.size = buffer.Size;

vkCmdCopyBuffer(commandBuffer, stagingBuffer, m_Buffer, 1, &copyRegion);
logicalDevice->FlushCommandBuffer(commandBuffer);

// Clean up
VulkanAllocator::DestroyBuffer(stagingBuffer, stagingBufferAlloc);
m_LocalStorage.Release();
}

VulkanVertexBuffer::~VulkanVertexBuffer()
{
EPPO_WARN("Releasing vertex buffer {}", (void*)this);
VulkanAllocator::DestroyBuffer(m_Buffer, m_Allocation);
}

void VulkanVertexBuffer::RT_Bind(Ref<CommandBuffer> commandBuffer) const
{
Renderer::SubmitCommand([this, commandBuffer]()
{
auto cmd = std::static_pointer_cast<VulkanCommandBuffer>(commandBuffer);

VkBuffer vb = { m_Buffer };
VkDeviceSize offsets[] = { 0 };

vkCmdBindVertexBuffers(cmd->GetCurrentCommandBuffer(), 0, 1, &vb, offsets);
});
}
}
10 changes: 2 additions & 8 deletions EppoEngine/Source/Platform/Vulkan/VulkanVertexBuffer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "Core/Buffer.h"
#include "Platform/Vulkan/VulkanAllocator.h"
#include "Platform/Vulkan/VulkanCommandBuffer.h"
#include "Renderer/VertexBuffer.h"
Expand All @@ -10,18 +9,13 @@ namespace Eppo
class VulkanVertexBuffer : public VertexBuffer
{
public:
VulkanVertexBuffer(void* data, uint32_t size);
virtual ~VulkanVertexBuffer();
explicit VulkanVertexBuffer(Buffer buffer);
~VulkanVertexBuffer() override;

VkBuffer GetBuffer() const { return m_Buffer; }
void RT_Bind(Ref<CommandBuffer> commandBuffer) const override;

private:
void CreateBuffer(VmaMemoryUsage usage);

private:
Buffer m_LocalStorage;

VkBuffer m_Buffer;
VmaAllocation m_Allocation;
};
Expand Down
Loading

0 comments on commit b0a37b5

Please sign in to comment.